]> Pileus Git - ~andy/linux/blob - drivers/staging/vt6656/bssdb.c
be54553262626ff55aec171045134c542df62477
[~andy/linux] / drivers / staging / vt6656 / bssdb.c
1 /*
2  * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
3  * All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * File: bssdb.c
20  *
21  * Purpose: Handles the Basic Service Set & Node Database functions
22  *
23  * Functions:
24  *      BSSpSearchBSSList - Search known BSS list for Desire SSID or BSSID
25  *      BSSvClearBSSList - Clear BSS List
26  *      BSSbInsertToBSSList - Insert a BSS set into known BSS list
27  *      BSSbUpdateToBSSList - Update BSS set in known BSS list
28  *      BSSbIsSTAInNodeDB - Search Node DB table to find the index of matched DstAddr
29  *      BSSvCreateOneNode - Allocate an Node for Node DB
30  *      BSSvUpdateAPNode - Update AP Node content in Index 0 of KnownNodeDB
31  *      BSSvSecondCallBack - One second timer callback function to update Node DB info & AP link status
32  *      BSSvUpdateNodeTxCounter - Update Tx attemps, Tx failure counter in Node DB for auto-fall back rate control
33  *
34  * Revision History:
35  *
36  * Author: Lyndon Chen
37  *
38  * Date: July 17, 2002
39  *
40  */
41
42 #include "ttype.h"
43 #include "tmacro.h"
44 #include "tether.h"
45 #include "device.h"
46 #include "80211hdr.h"
47 #include "bssdb.h"
48 #include "wmgr.h"
49 #include "datarate.h"
50 #include "desc.h"
51 #include "wcmd.h"
52 #include "wpa.h"
53 #include "baseband.h"
54 #include "rf.h"
55 #include "card.h"
56 #include "mac.h"
57 #include "wpa2.h"
58 #include "control.h"
59 #include "rndis.h"
60 #include "iowpa.h"
61
62 /*---------------------  Static Definitions -------------------------*/
63
64
65
66
67 /*---------------------  Static Classes  ----------------------------*/
68
69 /*---------------------  Static Variables  --------------------------*/
70 static int          msglevel                =MSG_LEVEL_INFO;
71 //static int          msglevel                =MSG_LEVEL_DEBUG;
72
73
74
75 const WORD             awHWRetry0[5][5] = {
76                                             {RATE_18M, RATE_18M, RATE_12M, RATE_12M, RATE_12M},
77                                             {RATE_24M, RATE_24M, RATE_18M, RATE_12M, RATE_12M},
78                                             {RATE_36M, RATE_36M, RATE_24M, RATE_18M, RATE_18M},
79                                             {RATE_48M, RATE_48M, RATE_36M, RATE_24M, RATE_24M},
80                                             {RATE_54M, RATE_54M, RATE_48M, RATE_36M, RATE_36M}
81                                            };
82 const WORD             awHWRetry1[5][5] = {
83                                             {RATE_18M, RATE_18M, RATE_12M, RATE_6M, RATE_6M},
84                                             {RATE_24M, RATE_24M, RATE_18M, RATE_6M, RATE_6M},
85                                             {RATE_36M, RATE_36M, RATE_24M, RATE_12M, RATE_12M},
86                                             {RATE_48M, RATE_48M, RATE_24M, RATE_12M, RATE_12M},
87                                             {RATE_54M, RATE_54M, RATE_36M, RATE_18M, RATE_18M}
88                                            };
89
90
91
92 /*---------------------  Static Functions  --------------------------*/
93
94 void s_vCheckSensitivity(void *hDeviceContext);
95 void s_vCheckPreEDThreshold(void *hDeviceContext);
96
97 #ifdef Calcu_LinkQual
98 void s_uCalculateLinkQual(void *hDeviceContext);
99 #endif
100
101 /*---------------------  Export Variables  --------------------------*/
102
103
104 /*---------------------  Export Functions  --------------------------*/
105
106
107
108
109
110 /*+
111  *
112  * Routine Description:
113  *    Search known BSS list for Desire SSID or BSSID.
114  *
115  * Return Value:
116  *    PTR to KnownBSS or NULL
117  *
118 -*/
119
120 PKnownBSS BSSpSearchBSSList(void *hDeviceContext,
121                             PBYTE pbyDesireBSSID,
122                             PBYTE pbyDesireSSID,
123                             CARD_PHY_TYPE ePhyType)
124 {
125     PSDevice        pDevice = (PSDevice)hDeviceContext;
126     PSMgmtObject    pMgmt = &(pDevice->sMgmtObj);
127     PBYTE           pbyBSSID = NULL;
128     PWLAN_IE_SSID   pSSID = NULL;
129     PKnownBSS       pCurrBSS = NULL;
130     PKnownBSS       pSelect = NULL;
131     BYTE                 ZeroBSSID[WLAN_BSSID_LEN]={0x00,0x00,0x00,0x00,0x00,0x00};
132     unsigned int ii = 0;
133     unsigned int jj = 0;
134     if (pbyDesireBSSID != NULL) {
135         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"BSSpSearchBSSList BSSID[%02X %02X %02X-%02X %02X %02X]\n",
136                             *pbyDesireBSSID,*(pbyDesireBSSID+1),*(pbyDesireBSSID+2),
137                             *(pbyDesireBSSID+3),*(pbyDesireBSSID+4),*(pbyDesireBSSID+5));
138         if ((!is_broadcast_ether_addr(pbyDesireBSSID)) &&
139              (memcmp(pbyDesireBSSID, ZeroBSSID, 6)!= 0)){
140             pbyBSSID = pbyDesireBSSID;
141         }
142     }
143     if (pbyDesireSSID != NULL) {
144         if (((PWLAN_IE_SSID)pbyDesireSSID)->len != 0) {
145             pSSID = (PWLAN_IE_SSID) pbyDesireSSID;
146         }
147     }
148
149     if ((pbyBSSID != NULL)&&(pDevice->bRoaming == FALSE)) {
150         // match BSSID first
151         for (ii = 0; ii <MAX_BSS_NUM; ii++) {
152             pCurrBSS = &(pMgmt->sBSSList[ii]);
153
154         //2008-0718-01<Add>by MikeLiu
155            pCurrBSS->bSelected = FALSE;
156
157             if ((pCurrBSS->bActive) &&
158                 (pCurrBSS->bSelected == FALSE)) {
159                     if (!compare_ether_addr(pCurrBSS->abyBSSID, pbyBSSID)) {
160                     if (pSSID != NULL) {
161                         // compare ssid
162                         if ( !memcmp(pSSID->abySSID,
163                             ((PWLAN_IE_SSID)pCurrBSS->abySSID)->abySSID,
164                             pSSID->len)) {
165                             if ((pMgmt->eConfigMode == WMAC_CONFIG_AUTO) ||
166                                 ((pMgmt->eConfigMode == WMAC_CONFIG_IBSS_STA) && WLAN_GET_CAP_INFO_IBSS(pCurrBSS->wCapInfo)) ||
167                                 ((pMgmt->eConfigMode == WMAC_CONFIG_ESS_STA) && WLAN_GET_CAP_INFO_ESS(pCurrBSS->wCapInfo))
168                                 ) {
169                                 pCurrBSS->bSelected = TRUE;
170                                 return(pCurrBSS);
171                             }
172                         }
173                     } else {
174                         if ((pMgmt->eConfigMode == WMAC_CONFIG_AUTO) ||
175                             ((pMgmt->eConfigMode == WMAC_CONFIG_IBSS_STA) && WLAN_GET_CAP_INFO_IBSS(pCurrBSS->wCapInfo)) ||
176                             ((pMgmt->eConfigMode == WMAC_CONFIG_ESS_STA) && WLAN_GET_CAP_INFO_ESS(pCurrBSS->wCapInfo))
177                             ) {
178                             pCurrBSS->bSelected = TRUE;
179                             return(pCurrBSS);
180                         }
181                     }
182                 }
183             }
184         }
185     } else {
186         // ignore BSSID
187         for (ii = 0; ii <MAX_BSS_NUM; ii++) {
188             pCurrBSS = &(pMgmt->sBSSList[ii]);
189
190            //2007-0721-01<Mark>by MikeLiu
191          //   if ((pCurrBSS->bActive) &&
192          //       (pCurrBSS->bSelected == FALSE)) {
193
194         //2007-0721-01<Add>by MikeLiu
195           pCurrBSS->bSelected = FALSE;
196           if (pCurrBSS->bActive) {
197
198                 if (pSSID != NULL) {
199                     // matched SSID
200                     if (memcmp(pSSID->abySSID,
201                         ((PWLAN_IE_SSID)pCurrBSS->abySSID)->abySSID,
202                         pSSID->len) ||
203                         (pSSID->len != ((PWLAN_IE_SSID)pCurrBSS->abySSID)->len)) {
204                         // SSID not match skip this BSS
205                         continue;
206                       }
207                 }
208                 if (((pMgmt->eConfigMode == WMAC_CONFIG_IBSS_STA) && WLAN_GET_CAP_INFO_ESS(pCurrBSS->wCapInfo)) ||
209                     ((pMgmt->eConfigMode == WMAC_CONFIG_ESS_STA) && WLAN_GET_CAP_INFO_IBSS(pCurrBSS->wCapInfo))
210                     ){
211                     // Type not match skip this BSS
212                     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"BSS type mismatch.... Config[%d] BSS[0x%04x]\n", pMgmt->eConfigMode, pCurrBSS->wCapInfo);
213                     continue;
214                 }
215
216                 if (ePhyType != PHY_TYPE_AUTO) {
217                     if (((ePhyType == PHY_TYPE_11A) && (PHY_TYPE_11A != pCurrBSS->eNetworkTypeInUse)) ||
218                         ((ePhyType != PHY_TYPE_11A) && (PHY_TYPE_11A == pCurrBSS->eNetworkTypeInUse))) {
219                         // PhyType not match skip this BSS
220                         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Physical type mismatch.... ePhyType[%d] BSS[%d]\n", ePhyType, pCurrBSS->eNetworkTypeInUse);
221                         continue;
222                     }
223                 }
224 /*
225                 if (pMgmt->eAuthenMode < WMAC_AUTH_WPA) {
226                     if (pCurrBSS->bWPAValid == TRUE) {
227                         // WPA AP will reject connection of station without WPA enable.
228                         continue;
229                     }
230                 } else if ((pMgmt->eAuthenMode == WMAC_AUTH_WPA) ||
231                            (pMgmt->eAuthenMode == WMAC_AUTH_WPAPSK)) {
232                     if (pCurrBSS->bWPAValid == FALSE) {
233                         // station with WPA enable can't join NonWPA AP.
234                         continue;
235                     }
236                 } else if ((pMgmt->eAuthenMode == WMAC_AUTH_WPA2) ||
237                            (pMgmt->eAuthenMode == WMAC_AUTH_WPA2PSK)) {
238                     if (pCurrBSS->bWPA2Valid == FALSE) {
239                         // station with WPA2 enable can't join NonWPA2 AP.
240                         continue;
241                     }
242                 }
243 */
244 //DavidWang
245         pMgmt->pSameBSS[jj].uChannel = pCurrBSS->uChannel;
246         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"BSSpSearchBSSList pSelect1[%02X %02X %02X-%02X %02X %02X]\n",*pCurrBSS->abyBSSID,*(pCurrBSS->abyBSSID+1),*(pCurrBSS->abyBSSID+2),*(pCurrBSS->abyBSSID+3),*(pCurrBSS->abyBSSID+4),*(pCurrBSS->abyBSSID+5));
247         jj++;
248
249 //DavidWang
250                 if (pSelect == NULL) {
251                     pSelect = pCurrBSS;
252                 } else {
253                     // compare RSSI, select signal strong one
254                     if (pCurrBSS->uRSSI < pSelect->uRSSI) {
255                         pSelect = pCurrBSS;
256                     }
257                 }
258             }
259         }
260 //DavidWang
261 pDevice->bSameBSSMaxNum = jj;
262 //DavidWang
263         if (pSelect != NULL) {
264             pSelect->bSelected = TRUE;
265                         if (pDevice->bRoaming == FALSE)  {
266         //       Einsn Add @20070907
267                                 memset(pbyDesireSSID, 0, WLAN_IEHDR_LEN + WLAN_SSID_MAXLEN + 1);
268                         memcpy(pbyDesireSSID,pCurrBSS->abySSID,WLAN_IEHDR_LEN + WLAN_SSID_MAXLEN + 1) ;
269                                                 }
270
271             return(pSelect);
272         }
273     }
274     return(NULL);
275
276 }
277
278
279 /*+
280  *
281  * Routine Description:
282  *    Clear BSS List
283  *
284  * Return Value:
285  *    None.
286  *
287 -*/
288
289
290 void BSSvClearBSSList(void *hDeviceContext, BOOL bKeepCurrBSSID)
291 {
292     PSDevice     pDevice = (PSDevice)hDeviceContext;
293     PSMgmtObject    pMgmt = &(pDevice->sMgmtObj);
294     unsigned int            ii;
295
296     for (ii = 0; ii < MAX_BSS_NUM; ii++) {
297         if (bKeepCurrBSSID) {
298             if (pMgmt->sBSSList[ii].bActive &&
299                 !compare_ether_addr(pMgmt->sBSSList[ii].abyBSSID,
300                                     pMgmt->abyCurrBSSID)) {
301  //mike mark: there are two same BSSID in list if that AP is in hidden ssid mode,one 's SSID is null,
302  //                 but other's is obvious, so if it acssociate with your STA  exactly,you must keep two
303  //                 of them!!!!!!!!!
304                // bKeepCurrBSSID = FALSE;
305                 continue;
306             }
307         }
308 /*
309         if ((pMgmt->sBSSList[ii].bActive) && (pMgmt->sBSSList[ii].uClearCount < BSS_CLEAR_COUNT)) {
310              pMgmt->sBSSList[ii].uClearCount ++;
311              continue;
312         }
313 */
314         pMgmt->sBSSList[ii].bActive = FALSE;
315         memset(&pMgmt->sBSSList[ii], 0, sizeof(KnownBSS));
316     }
317     BSSvClearAnyBSSJoinRecord(pDevice);
318
319     return;
320 }
321
322
323
324 /*+
325  *
326  * Routine Description:
327  *    search BSS list by BSSID & SSID if matched
328  *
329  * Return Value:
330  *    TRUE if found.
331  *
332 -*/
333 PKnownBSS BSSpAddrIsInBSSList(void *hDeviceContext,
334                               PBYTE abyBSSID,
335                               PWLAN_IE_SSID pSSID)
336 {
337     PSDevice     pDevice = (PSDevice)hDeviceContext;
338     PSMgmtObject    pMgmt = &(pDevice->sMgmtObj);
339     PKnownBSS       pBSSList = NULL;
340     unsigned int            ii;
341
342     for (ii = 0; ii < MAX_BSS_NUM; ii++) {
343         pBSSList = &(pMgmt->sBSSList[ii]);
344         if (pBSSList->bActive) {
345                 if (!compare_ether_addr(pBSSList->abyBSSID, abyBSSID)) {
346                 if (pSSID->len == ((PWLAN_IE_SSID)pBSSList->abySSID)->len){
347                     if (memcmp(pSSID->abySSID,
348                             ((PWLAN_IE_SSID)pBSSList->abySSID)->abySSID,
349                             pSSID->len) == 0)
350                         return pBSSList;
351                 }
352             }
353         }
354     }
355
356     return NULL;
357 };
358
359
360
361 /*+
362  *
363  * Routine Description:
364  *    Insert a BSS set into known BSS list
365  *
366  * Return Value:
367  *    TRUE if success.
368  *
369 -*/
370
371 BOOL BSSbInsertToBSSList(void *hDeviceContext,
372                          PBYTE abyBSSIDAddr,
373                          QWORD qwTimestamp,
374                          WORD wBeaconInterval,
375                          WORD wCapInfo,
376                          BYTE byCurrChannel,
377                          PWLAN_IE_SSID pSSID,
378                          PWLAN_IE_SUPP_RATES pSuppRates,
379                          PWLAN_IE_SUPP_RATES pExtSuppRates,
380                          PERPObject psERP,
381                          PWLAN_IE_RSN pRSN,
382                          PWLAN_IE_RSN_EXT pRSNWPA,
383                          PWLAN_IE_COUNTRY pIE_Country,
384                          PWLAN_IE_QUIET pIE_Quiet,
385                          unsigned int uIELength,
386                          PBYTE pbyIEs,
387                          void *pRxPacketContext)
388 {
389
390     PSDevice     pDevice = (PSDevice)hDeviceContext;
391     PSMgmtObject    pMgmt = &(pDevice->sMgmtObj);
392     PSRxMgmtPacket  pRxPacket = (PSRxMgmtPacket)pRxPacketContext;
393     PKnownBSS       pBSSList = NULL;
394     unsigned int            ii;
395     BOOL            bParsingQuiet = FALSE;
396
397
398
399     pBSSList = (PKnownBSS)&(pMgmt->sBSSList[0]);
400
401     for (ii = 0; ii < MAX_BSS_NUM; ii++) {
402         pBSSList = (PKnownBSS)&(pMgmt->sBSSList[ii]);
403         if (!pBSSList->bActive)
404                 break;
405     }
406
407     if (ii == MAX_BSS_NUM){
408         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Get free KnowBSS node failed.\n");
409         return FALSE;
410     }
411     // save the BSS info
412     pBSSList->bActive = TRUE;
413     memcpy( pBSSList->abyBSSID, abyBSSIDAddr, WLAN_BSSID_LEN);
414     HIDWORD(pBSSList->qwBSSTimestamp) = cpu_to_le32(HIDWORD(qwTimestamp));
415     LODWORD(pBSSList->qwBSSTimestamp) = cpu_to_le32(LODWORD(qwTimestamp));
416     pBSSList->wBeaconInterval = cpu_to_le16(wBeaconInterval);
417     pBSSList->wCapInfo = cpu_to_le16(wCapInfo);
418     pBSSList->uClearCount = 0;
419
420     if (pSSID->len > WLAN_SSID_MAXLEN)
421         pSSID->len = WLAN_SSID_MAXLEN;
422     memcpy( pBSSList->abySSID, pSSID, pSSID->len + WLAN_IEHDR_LEN);
423
424     pBSSList->uChannel = byCurrChannel;
425
426     if (pSuppRates->len > WLAN_RATES_MAXLEN)
427         pSuppRates->len = WLAN_RATES_MAXLEN;
428     memcpy( pBSSList->abySuppRates, pSuppRates, pSuppRates->len + WLAN_IEHDR_LEN);
429
430     if (pExtSuppRates != NULL) {
431         if (pExtSuppRates->len > WLAN_RATES_MAXLEN)
432             pExtSuppRates->len = WLAN_RATES_MAXLEN;
433         memcpy(pBSSList->abyExtSuppRates, pExtSuppRates, pExtSuppRates->len + WLAN_IEHDR_LEN);
434         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"BSSbInsertToBSSList: pExtSuppRates->len = %d\n", pExtSuppRates->len);
435
436     } else {
437         memset(pBSSList->abyExtSuppRates, 0, WLAN_IEHDR_LEN + WLAN_RATES_MAXLEN + 1);
438     }
439     pBSSList->sERP.byERP = psERP->byERP;
440     pBSSList->sERP.bERPExist = psERP->bERPExist;
441
442     // Check if BSS is 802.11a/b/g
443     if (pBSSList->uChannel > CB_MAX_CHANNEL_24G) {
444         pBSSList->eNetworkTypeInUse = PHY_TYPE_11A;
445     } else {
446         if (pBSSList->sERP.bERPExist == TRUE) {
447             pBSSList->eNetworkTypeInUse = PHY_TYPE_11G;
448         } else {
449             pBSSList->eNetworkTypeInUse = PHY_TYPE_11B;
450         }
451     }
452
453     pBSSList->byRxRate = pRxPacket->byRxRate;
454     pBSSList->qwLocalTSF = pRxPacket->qwLocalTSF;
455     pBSSList->uRSSI = pRxPacket->uRSSI;
456     pBSSList->bySQ = pRxPacket->bySQ;
457
458    if ((pMgmt->eCurrMode == WMAC_MODE_ESS_STA) &&
459         (pMgmt->eCurrState == WMAC_STATE_ASSOC)) {
460         // assoc with BSS
461         if (pBSSList == pMgmt->pCurrBSS) {
462             bParsingQuiet = TRUE;
463         }
464     }
465
466     WPA_ClearRSN(pBSSList);
467
468     if (pRSNWPA != NULL) {
469         unsigned int uLen = pRSNWPA->len + 2;
470
471         if (uLen <= (uIELength -
472                      (unsigned int) (ULONG_PTR) ((PBYTE) pRSNWPA - pbyIEs))) {
473                 pBSSList->wWPALen = uLen;
474                 memcpy(pBSSList->byWPAIE, pRSNWPA, uLen);
475                 WPA_ParseRSN(pBSSList, pRSNWPA);
476         }
477     }
478
479     WPA2_ClearRSN(pBSSList);
480
481     if (pRSN != NULL) {
482         unsigned int uLen = pRSN->len + 2;
483
484         if (uLen <= (uIELength -
485                      (unsigned int) (ULONG_PTR) ((PBYTE) pRSN - pbyIEs))) {
486                 pBSSList->wRSNLen = uLen;
487                 memcpy(pBSSList->byRSNIE, pRSN, uLen);
488                 WPA2vParseRSN(pBSSList, pRSN);
489         }
490     }
491
492     if ((pMgmt->eAuthenMode == WMAC_AUTH_WPA2) || (pBSSList->bWPA2Valid == TRUE)) {
493
494         PSKeyItem  pTransmitKey = NULL;
495         BOOL       bIs802_1x = FALSE;
496
497         for (ii = 0; ii < pBSSList->wAKMSSAuthCount; ii ++) {
498             if (pBSSList->abyAKMSSAuthType[ii] == WLAN_11i_AKMSS_802_1X) {
499                 bIs802_1x = TRUE;
500                 break;
501             }
502         }
503         if ((bIs802_1x == TRUE) && (pSSID->len == ((PWLAN_IE_SSID)pMgmt->abyDesireSSID)->len) &&
504             ( !memcmp(pSSID->abySSID, ((PWLAN_IE_SSID)pMgmt->abyDesireSSID)->abySSID, pSSID->len))) {
505
506                 bAdd_PMKID_Candidate((void *) pDevice,
507                                      pBSSList->abyBSSID,
508                                      &pBSSList->sRSNCapObj);
509
510             if ((pDevice->bLinkPass == TRUE) && (pMgmt->eCurrState == WMAC_STATE_ASSOC)) {
511                 if ((KeybGetTransmitKey(&(pDevice->sKey), pDevice->abyBSSID, PAIRWISE_KEY, &pTransmitKey) == TRUE) ||
512                     (KeybGetTransmitKey(&(pDevice->sKey), pDevice->abyBSSID, GROUP_KEY, &pTransmitKey) == TRUE)) {
513                     pDevice->gsPMKIDCandidate.StatusType = Ndis802_11StatusType_PMKID_CandidateList;
514                     pDevice->gsPMKIDCandidate.Version = 1;
515
516                 }
517
518             }
519         }
520     }
521
522     if (pDevice->bUpdateBBVGA) {
523         // Moniter if RSSI is too strong.
524         pBSSList->byRSSIStatCnt = 0;
525         RFvRSSITodBm(pDevice, (BYTE)(pRxPacket->uRSSI), &pBSSList->ldBmMAX);
526         pBSSList->ldBmAverage[0] = pBSSList->ldBmMAX;
527         pBSSList->ldBmAverRange = pBSSList->ldBmMAX;
528         for (ii = 1; ii < RSSI_STAT_COUNT; ii++)
529             pBSSList->ldBmAverage[ii] = 0;
530     }
531
532 /*
533     if ((pIE_Country != NULL) &&
534         (pMgmt->b11hEnable == TRUE)) {
535         CARDvSetCountryInfo(pMgmt->pAdapter,
536                             pBSSList->eNetworkTypeInUse,
537                             pIE_Country);
538     }
539
540     if ((bParsingQuiet == TRUE) && (pIE_Quiet != NULL)) {
541         if ((((PWLAN_IE_QUIET)pIE_Quiet)->len == 8) &&
542             (((PWLAN_IE_QUIET)pIE_Quiet)->byQuietCount != 0)) {
543             // valid EID
544             if (pQuiet == NULL) {
545                 pQuiet = (PWLAN_IE_QUIET)pIE_Quiet;
546                 CARDbSetQuiet(  pMgmt->pAdapter,
547                                 TRUE,
548                                 pQuiet->byQuietCount,
549                                 pQuiet->byQuietPeriod,
550                                 *((PWORD)pQuiet->abyQuietDuration),
551                                 *((PWORD)pQuiet->abyQuietOffset)
552                                 );
553             } else {
554                 pQuiet = (PWLAN_IE_QUIET)pIE_Quiet;
555                 CARDbSetQuiet(  pMgmt->pAdapter,
556                                 FALSE,
557                                 pQuiet->byQuietCount,
558                                 pQuiet->byQuietPeriod,
559                                 *((PWORD)pQuiet->abyQuietDuration),
560                                 *((PWORD)pQuiet->abyQuietOffset)
561                                 );
562             }
563         }
564     }
565
566     if ((bParsingQuiet == TRUE) &&
567         (pQuiet != NULL)) {
568         CARDbStartQuiet(pMgmt->pAdapter);
569     }
570 */
571
572     pBSSList->uIELength = uIELength;
573     if (pBSSList->uIELength > WLAN_BEACON_FR_MAXLEN)
574         pBSSList->uIELength = WLAN_BEACON_FR_MAXLEN;
575     memcpy(pBSSList->abyIEs, pbyIEs, pBSSList->uIELength);
576
577     return TRUE;
578 }
579
580
581 /*+
582  *
583  * Routine Description:
584  *    Update BSS set in known BSS list
585  *
586  * Return Value:
587  *    TRUE if success.
588  *
589 -*/
590 // TODO: input structure modify
591
592 BOOL BSSbUpdateToBSSList(void *hDeviceContext,
593                          QWORD qwTimestamp,
594                          WORD wBeaconInterval,
595                          WORD wCapInfo,
596                          BYTE byCurrChannel,
597                          BOOL bChannelHit,
598                          PWLAN_IE_SSID pSSID,
599                          PWLAN_IE_SUPP_RATES pSuppRates,
600                          PWLAN_IE_SUPP_RATES pExtSuppRates,
601                          PERPObject psERP,
602                          PWLAN_IE_RSN pRSN,
603                          PWLAN_IE_RSN_EXT pRSNWPA,
604                          PWLAN_IE_COUNTRY pIE_Country,
605                          PWLAN_IE_QUIET pIE_Quiet,
606                          PKnownBSS pBSSList,
607                          unsigned int uIELength,
608                          PBYTE pbyIEs,
609                          void *pRxPacketContext)
610 {
611     int             ii, jj;
612     PSDevice        pDevice = (PSDevice)hDeviceContext;
613     PSMgmtObject    pMgmt = &(pDevice->sMgmtObj);
614     PSRxMgmtPacket  pRxPacket = (PSRxMgmtPacket)pRxPacketContext;
615     signed long            ldBm, ldBmSum;
616     BOOL            bParsingQuiet = FALSE;
617   //  BYTE            abyTmpSSID[WLAN_IEHDR_LEN + WLAN_SSID_MAXLEN + 1];
618
619
620     if (pBSSList == NULL)
621         return FALSE;
622
623
624     HIDWORD(pBSSList->qwBSSTimestamp) = cpu_to_le32(HIDWORD(qwTimestamp));
625     LODWORD(pBSSList->qwBSSTimestamp) = cpu_to_le32(LODWORD(qwTimestamp));
626     pBSSList->wBeaconInterval = cpu_to_le16(wBeaconInterval);
627     pBSSList->wCapInfo = cpu_to_le16(wCapInfo);
628     pBSSList->uClearCount = 0;
629     pBSSList->uChannel = byCurrChannel;
630 //    DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"BSSbUpdateToBSSList: pBSSList->uChannel: %d\n", pBSSList->uChannel);
631
632     if (pSSID->len > WLAN_SSID_MAXLEN)
633         pSSID->len = WLAN_SSID_MAXLEN;
634
635     if ((pSSID->len != 0) && (pSSID->abySSID[0] != 0))
636         memcpy(pBSSList->abySSID, pSSID, pSSID->len + WLAN_IEHDR_LEN);
637     memcpy(pBSSList->abySuppRates, pSuppRates,pSuppRates->len + WLAN_IEHDR_LEN);
638
639     if (pExtSuppRates != NULL) {
640         memcpy(pBSSList->abyExtSuppRates, pExtSuppRates,pExtSuppRates->len + WLAN_IEHDR_LEN);
641     } else {
642         memset(pBSSList->abyExtSuppRates, 0, WLAN_IEHDR_LEN + WLAN_RATES_MAXLEN + 1);
643     }
644     pBSSList->sERP.byERP = psERP->byERP;
645     pBSSList->sERP.bERPExist = psERP->bERPExist;
646
647     // Check if BSS is 802.11a/b/g
648     if (pBSSList->uChannel > CB_MAX_CHANNEL_24G) {
649         pBSSList->eNetworkTypeInUse = PHY_TYPE_11A;
650     } else {
651         if (pBSSList->sERP.bERPExist == TRUE) {
652             pBSSList->eNetworkTypeInUse = PHY_TYPE_11G;
653         } else {
654             pBSSList->eNetworkTypeInUse = PHY_TYPE_11B;
655         }
656     }
657
658     pBSSList->byRxRate = pRxPacket->byRxRate;
659     pBSSList->qwLocalTSF = pRxPacket->qwLocalTSF;
660     if(bChannelHit)
661         pBSSList->uRSSI = pRxPacket->uRSSI;
662     pBSSList->bySQ = pRxPacket->bySQ;
663
664    if ((pMgmt->eCurrMode == WMAC_MODE_ESS_STA) &&
665         (pMgmt->eCurrState == WMAC_STATE_ASSOC)) {
666         // assoc with BSS
667         if (pBSSList == pMgmt->pCurrBSS) {
668             bParsingQuiet = TRUE;
669         }
670     }
671
672    WPA_ClearRSN(pBSSList);         //mike update
673
674    if (pRSNWPA != NULL) {
675         unsigned int uLen = pRSNWPA->len + 2;
676         if (uLen <= (uIELength -
677                      (unsigned int) (ULONG_PTR) ((PBYTE) pRSNWPA - pbyIEs))) {
678                 pBSSList->wWPALen = uLen;
679                 memcpy(pBSSList->byWPAIE, pRSNWPA, uLen);
680                 WPA_ParseRSN(pBSSList, pRSNWPA);
681         }
682    }
683
684    WPA2_ClearRSN(pBSSList);  //mike update
685
686     if (pRSN != NULL) {
687         unsigned int uLen = pRSN->len + 2;
688         if (uLen <= (uIELength -
689                         (unsigned int) (ULONG_PTR) ((PBYTE) pRSN - pbyIEs))) {
690                 pBSSList->wRSNLen = uLen;
691                 memcpy(pBSSList->byRSNIE, pRSN, uLen);
692                 WPA2vParseRSN(pBSSList, pRSN);
693         }
694     }
695
696     if (pRxPacket->uRSSI != 0) {
697         RFvRSSITodBm(pDevice, (BYTE)(pRxPacket->uRSSI), &ldBm);
698         // Moniter if RSSI is too strong.
699         pBSSList->byRSSIStatCnt++;
700         pBSSList->byRSSIStatCnt %= RSSI_STAT_COUNT;
701         pBSSList->ldBmAverage[pBSSList->byRSSIStatCnt] = ldBm;
702         ldBmSum = 0;
703         for (ii = 0, jj = 0; ii < RSSI_STAT_COUNT; ii++) {
704                 if (pBSSList->ldBmAverage[ii] != 0) {
705                         pBSSList->ldBmMAX =
706                                 max(pBSSList->ldBmAverage[ii], ldBm);
707                         ldBmSum +=
708                                 pBSSList->ldBmAverage[ii];
709                         jj++;
710                 }
711         }
712         pBSSList->ldBmAverRange = ldBmSum /jj;
713     }
714
715     pBSSList->uIELength = uIELength;
716     if (pBSSList->uIELength > WLAN_BEACON_FR_MAXLEN)
717         pBSSList->uIELength = WLAN_BEACON_FR_MAXLEN;
718     memcpy(pBSSList->abyIEs, pbyIEs, pBSSList->uIELength);
719
720     return TRUE;
721 }
722
723
724
725
726
727 /*+
728  *
729  * Routine Description:
730  *    Search Node DB table to find the index of matched DstAddr
731  *
732  * Return Value:
733  *    None
734  *
735 -*/
736
737 BOOL BSSbIsSTAInNodeDB(void *hDeviceContext,
738                        PBYTE abyDstAddr,
739                        PUINT puNodeIndex)
740 {
741     PSDevice        pDevice = (PSDevice)hDeviceContext;
742     PSMgmtObject    pMgmt = &(pDevice->sMgmtObj);
743     unsigned int            ii;
744
745     // Index = 0 reserved for AP Node
746     for (ii = 1; ii < (MAX_NODE_NUM + 1); ii++) {
747         if (pMgmt->sNodeDBTable[ii].bActive) {
748                 if (!compare_ether_addr(abyDstAddr,
749                                         pMgmt->sNodeDBTable[ii].abyMACAddr)) {
750                 *puNodeIndex = ii;
751                 return TRUE;
752             }
753         }
754     }
755
756    return FALSE;
757 };
758
759
760
761 /*+
762  *
763  * Routine Description:
764  *    Find an empty node and allocated; if no empty found,
765  *    instand used of most inactive one.
766  *
767  * Return Value:
768  *    None
769  *
770 -*/
771 void BSSvCreateOneNode(void *hDeviceContext, PUINT puNodeIndex)
772 {
773
774     PSDevice     pDevice = (PSDevice)hDeviceContext;
775     PSMgmtObject    pMgmt = &(pDevice->sMgmtObj);
776     unsigned int            ii;
777     unsigned int            BigestCount = 0;
778     unsigned int            SelectIndex;
779     struct sk_buff  *skb;
780     // Index = 0 reserved for AP Node (In STA mode)
781     // Index = 0 reserved for Broadcast/MultiCast (In AP mode)
782     SelectIndex = 1;
783     for (ii = 1; ii < (MAX_NODE_NUM + 1); ii++) {
784         if (pMgmt->sNodeDBTable[ii].bActive) {
785             if (pMgmt->sNodeDBTable[ii].uInActiveCount > BigestCount) {
786                 BigestCount = pMgmt->sNodeDBTable[ii].uInActiveCount;
787                 SelectIndex = ii;
788             }
789         }
790         else {
791             break;
792         }
793     }
794
795     // if not found replace uInActiveCount is largest one.
796     if ( ii == (MAX_NODE_NUM + 1)) {
797         *puNodeIndex = SelectIndex;
798         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Replace inactive node = %d\n", SelectIndex);
799         // clear ps buffer
800         if (pMgmt->sNodeDBTable[*puNodeIndex].sTxPSQueue.next != NULL) {
801             while ((skb = skb_dequeue(&pMgmt->sNodeDBTable[*puNodeIndex].sTxPSQueue)) != NULL)
802             dev_kfree_skb(skb);
803         }
804     }
805     else {
806         *puNodeIndex = ii;
807     }
808
809     memset(&pMgmt->sNodeDBTable[*puNodeIndex], 0, sizeof(KnownNodeDB));
810     pMgmt->sNodeDBTable[*puNodeIndex].bActive = TRUE;
811     pMgmt->sNodeDBTable[*puNodeIndex].uRatePollTimeout = FALLBACK_POLL_SECOND;
812     // for AP mode PS queue
813     skb_queue_head_init(&pMgmt->sNodeDBTable[*puNodeIndex].sTxPSQueue);
814     pMgmt->sNodeDBTable[*puNodeIndex].byAuthSequence = 0;
815     pMgmt->sNodeDBTable[*puNodeIndex].wEnQueueCnt = 0;
816     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Create node index = %d\n", ii);
817     return;
818 };
819
820
821
822 /*+
823  *
824  * Routine Description:
825  *    Remove Node by NodeIndex
826  *
827  *
828  * Return Value:
829  *    None
830  *
831 -*/
832
833 void BSSvRemoveOneNode(void *hDeviceContext, unsigned int uNodeIndex)
834 {
835
836     PSDevice        pDevice = (PSDevice)hDeviceContext;
837     PSMgmtObject    pMgmt = &(pDevice->sMgmtObj);
838     BYTE            byMask[8] = {1, 2, 4, 8, 0x10, 0x20, 0x40, 0x80};
839     struct sk_buff  *skb;
840
841
842     while ((skb = skb_dequeue(&pMgmt->sNodeDBTable[uNodeIndex].sTxPSQueue)) != NULL)
843             dev_kfree_skb(skb);
844     // clear context
845     memset(&pMgmt->sNodeDBTable[uNodeIndex], 0, sizeof(KnownNodeDB));
846     // clear tx bit map
847     pMgmt->abyPSTxMap[pMgmt->sNodeDBTable[uNodeIndex].wAID >> 3] &=  ~byMask[pMgmt->sNodeDBTable[uNodeIndex].wAID & 7];
848
849     return;
850 };
851 /*+
852  *
853  * Routine Description:
854  *    Update AP Node content in Index 0 of KnownNodeDB
855  *
856  *
857  * Return Value:
858  *    None
859  *
860 -*/
861
862 void BSSvUpdateAPNode(void *hDeviceContext,
863                       PWORD pwCapInfo,
864                       PWLAN_IE_SUPP_RATES pSuppRates,
865                       PWLAN_IE_SUPP_RATES pExtSuppRates)
866 {
867     PSDevice     pDevice = (PSDevice)hDeviceContext;
868     PSMgmtObject    pMgmt = &(pDevice->sMgmtObj);
869     unsigned int            uRateLen = WLAN_RATES_MAXLEN;
870
871     memset(&pMgmt->sNodeDBTable[0], 0, sizeof(KnownNodeDB));
872
873     pMgmt->sNodeDBTable[0].bActive = TRUE;
874     if (pDevice->byBBType == BB_TYPE_11B) {
875         uRateLen = WLAN_RATES_MAXLEN_11B;
876     }
877     pMgmt->abyCurrSuppRates[1] = RATEuSetIE((PWLAN_IE_SUPP_RATES)pSuppRates,
878                                             (PWLAN_IE_SUPP_RATES)pMgmt->abyCurrSuppRates,
879                                             uRateLen);
880     pMgmt->abyCurrExtSuppRates[1] = RATEuSetIE((PWLAN_IE_SUPP_RATES)pExtSuppRates,
881                                             (PWLAN_IE_SUPP_RATES)pMgmt->abyCurrExtSuppRates,
882                                             uRateLen);
883     RATEvParseMaxRate((void *) pDevice,
884                        (PWLAN_IE_SUPP_RATES)pMgmt->abyCurrSuppRates,
885                        (PWLAN_IE_SUPP_RATES)pMgmt->abyCurrExtSuppRates,
886                        TRUE,
887                        &(pMgmt->sNodeDBTable[0].wMaxBasicRate),
888                        &(pMgmt->sNodeDBTable[0].wMaxSuppRate),
889                        &(pMgmt->sNodeDBTable[0].wSuppRate),
890                        &(pMgmt->sNodeDBTable[0].byTopCCKBasicRate),
891                        &(pMgmt->sNodeDBTable[0].byTopOFDMBasicRate)
892                       );
893     memcpy(pMgmt->sNodeDBTable[0].abyMACAddr, pMgmt->abyCurrBSSID, WLAN_ADDR_LEN);
894     pMgmt->sNodeDBTable[0].wTxDataRate = pMgmt->sNodeDBTable[0].wMaxSuppRate;
895     pMgmt->sNodeDBTable[0].bShortPreamble = WLAN_GET_CAP_INFO_SHORTPREAMBLE(*pwCapInfo);
896     pMgmt->sNodeDBTable[0].uRatePollTimeout = FALLBACK_POLL_SECOND;
897     // Auto rate fallback function initiation.
898     // RATEbInit(pDevice);
899     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"pMgmt->sNodeDBTable[0].wTxDataRate = %d \n", pMgmt->sNodeDBTable[0].wTxDataRate);
900
901 };
902
903 /*+
904  *
905  * Routine Description:
906  *    Add Multicast Node content in Index 0 of KnownNodeDB
907  *
908  *
909  * Return Value:
910  *    None
911  *
912 -*/
913
914 void BSSvAddMulticastNode(void *hDeviceContext)
915 {
916     PSDevice     pDevice = (PSDevice)hDeviceContext;
917     PSMgmtObject    pMgmt = &(pDevice->sMgmtObj);
918
919     if (!pDevice->bEnableHostWEP)
920         memset(&pMgmt->sNodeDBTable[0], 0, sizeof(KnownNodeDB));
921     memset(pMgmt->sNodeDBTable[0].abyMACAddr, 0xff, WLAN_ADDR_LEN);
922     pMgmt->sNodeDBTable[0].bActive = TRUE;
923     pMgmt->sNodeDBTable[0].bPSEnable = FALSE;
924     skb_queue_head_init(&pMgmt->sNodeDBTable[0].sTxPSQueue);
925     RATEvParseMaxRate((void *) pDevice,
926                       (PWLAN_IE_SUPP_RATES)pMgmt->abyCurrSuppRates,
927                       (PWLAN_IE_SUPP_RATES)pMgmt->abyCurrExtSuppRates,
928                       TRUE,
929                       &(pMgmt->sNodeDBTable[0].wMaxBasicRate),
930                       &(pMgmt->sNodeDBTable[0].wMaxSuppRate),
931                        &(pMgmt->sNodeDBTable[0].wSuppRate),
932                       &(pMgmt->sNodeDBTable[0].byTopCCKBasicRate),
933                       &(pMgmt->sNodeDBTable[0].byTopOFDMBasicRate)
934                      );
935     pMgmt->sNodeDBTable[0].wTxDataRate = pMgmt->sNodeDBTable[0].wMaxBasicRate;
936     pMgmt->sNodeDBTable[0].uRatePollTimeout = FALLBACK_POLL_SECOND;
937
938 };
939
940 /*+
941  *
942  * Routine Description:
943  *
944  *
945  *  Second call back function to update Node DB info & AP link status
946  *
947  *
948  * Return Value:
949  *    none.
950  *
951 -*/
952
953 void BSSvSecondCallBack(void *hDeviceContext)
954 {
955     PSDevice        pDevice = (PSDevice)hDeviceContext;
956     PSMgmtObject    pMgmt = &(pDevice->sMgmtObj);
957     unsigned int            ii;
958     PWLAN_IE_SSID   pItemSSID, pCurrSSID;
959     unsigned int            uSleepySTACnt = 0;
960     unsigned int            uNonShortSlotSTACnt = 0;
961     unsigned int            uLongPreambleSTACnt = 0;
962     viawget_wpa_header *wpahdr;  //DavidWang
963
964     spin_lock_irq(&pDevice->lock);
965
966     pDevice->uAssocCount = 0;
967
968     //Power Saving Mode Tx Burst
969     if ( pDevice->bEnablePSMode == TRUE ) {
970         pDevice->ulPSModeWaitTx++;
971         if ( pDevice->ulPSModeWaitTx >= 2 ) {
972             pDevice->ulPSModeWaitTx = 0;
973             pDevice->bPSModeTxBurst = FALSE;
974         }
975     }
976
977     pDevice->byERPFlag &=
978         ~(WLAN_SET_ERP_BARKER_MODE(1) | WLAN_SET_ERP_NONERP_PRESENT(1));
979
980     if (pDevice->wUseProtectCntDown > 0) {
981         pDevice->wUseProtectCntDown --;
982     }
983     else {
984         // disable protect mode
985         pDevice->byERPFlag &= ~(WLAN_SET_ERP_USE_PROTECTION(1));
986     }
987
988 if(pDevice->byReAssocCount > 0) {
989        pDevice->byReAssocCount++;
990    if((pDevice->byReAssocCount > 10) && (pDevice->bLinkPass != TRUE)) {  //10 sec timeout
991                      printk("Re-association timeout!!!\n");
992                    pDevice->byReAssocCount = 0;
993                      #ifdef WPA_SUPPLICANT_DRIVER_WEXT_SUPPORT
994                     // if(pDevice->bWPASuppWextEnabled == TRUE)
995                         {
996                         union iwreq_data  wrqu;
997                         memset(&wrqu, 0, sizeof (wrqu));
998                           wrqu.ap_addr.sa_family = ARPHRD_ETHER;
999                         PRINT_K("wireless_send_event--->SIOCGIWAP(disassociated)\n");
1000                         wireless_send_event(pDevice->dev, SIOCGIWAP, &wrqu, NULL);
1001                        }
1002                     #endif
1003      }
1004    else if(pDevice->bLinkPass == TRUE)
1005         pDevice->byReAssocCount = 0;
1006 }
1007
1008 #ifdef SndEvt_ToAPI
1009 if((pMgmt->eCurrState!=WMAC_STATE_ASSOC) &&
1010      (pMgmt->eLastState==WMAC_STATE_ASSOC))
1011 {
1012   union iwreq_data      wrqu;
1013   memset(&wrqu, 0, sizeof(wrqu));
1014   wrqu.data.flags = RT_DISCONNECTED_EVENT_FLAG;
1015   wireless_send_event(pDevice->dev, IWEVCUSTOM, &wrqu, NULL);
1016 }
1017  pMgmt->eLastState = pMgmt->eCurrState ;
1018 #endif
1019
1020 #ifdef Calcu_LinkQual
1021    s_uCalculateLinkQual((void *)pDevice);
1022 #endif
1023
1024     for (ii = 0; ii < (MAX_NODE_NUM + 1); ii++) {
1025
1026         if (pMgmt->sNodeDBTable[ii].bActive) {
1027             // Increase in-activity counter
1028             pMgmt->sNodeDBTable[ii].uInActiveCount++;
1029
1030             if (ii > 0) {
1031                 if (pMgmt->sNodeDBTable[ii].uInActiveCount > MAX_INACTIVE_COUNT) {
1032                     BSSvRemoveOneNode(pDevice, ii);
1033                     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO
1034                         "Inactive timeout [%d] sec, STA index = [%d] remove\n", MAX_INACTIVE_COUNT, ii);
1035                     continue;
1036                 }
1037
1038                 if (pMgmt->sNodeDBTable[ii].eNodeState >= NODE_ASSOC) {
1039
1040                     pDevice->uAssocCount++;
1041
1042                     // check if Non ERP exist
1043                     if (pMgmt->sNodeDBTable[ii].uInActiveCount < ERP_RECOVER_COUNT) {
1044                         if (!pMgmt->sNodeDBTable[ii].bShortPreamble) {
1045                             pDevice->byERPFlag |= WLAN_SET_ERP_BARKER_MODE(1);
1046                             uLongPreambleSTACnt ++;
1047                         }
1048                         if (!pMgmt->sNodeDBTable[ii].bERPExist) {
1049                             pDevice->byERPFlag |= WLAN_SET_ERP_NONERP_PRESENT(1);
1050                             pDevice->byERPFlag |= WLAN_SET_ERP_USE_PROTECTION(1);
1051                         }
1052                         if (!pMgmt->sNodeDBTable[ii].bShortSlotTime)
1053                             uNonShortSlotSTACnt++;
1054                     }
1055                 }
1056
1057                 // check if any STA in PS mode
1058                 if (pMgmt->sNodeDBTable[ii].bPSEnable)
1059                     uSleepySTACnt++;
1060
1061
1062             }
1063
1064             // Rate fallback check
1065             if (!pDevice->bFixRate) {
1066 /*
1067                 if ((pMgmt->eCurrMode == WMAC_MODE_ESS_STA) && (ii == 0))
1068                     RATEvTxRateFallBack(pDevice, &(pMgmt->sNodeDBTable[ii]));
1069 */
1070                 if (ii > 0) {
1071                     // ii = 0 for multicast node (AP & Adhoc)
1072                         RATEvTxRateFallBack((void *)pDevice,
1073                                             &(pMgmt->sNodeDBTable[ii]));
1074                 }
1075                 else {
1076                     // ii = 0 reserved for unicast AP node (Infra STA)
1077                         if (pMgmt->eCurrMode == WMAC_MODE_ESS_STA)
1078                                 RATEvTxRateFallBack((void *)pDevice,
1079                                                     &(pMgmt->sNodeDBTable[ii]));
1080                 }
1081
1082             }
1083
1084             // check if pending PS queue
1085             if (pMgmt->sNodeDBTable[ii].wEnQueueCnt != 0) {
1086                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Index= %d, Queue = %d pending \n",
1087                            ii, pMgmt->sNodeDBTable[ii].wEnQueueCnt);
1088                 if ((ii >0) && (pMgmt->sNodeDBTable[ii].wEnQueueCnt > 15)) {
1089                     BSSvRemoveOneNode(pDevice, ii);
1090                     DBG_PRT(MSG_LEVEL_NOTICE, KERN_INFO "Pending many queues PS STA Index = %d remove \n", ii);
1091                     continue;
1092                 }
1093             }
1094         }
1095
1096     }
1097
1098
1099     if ((pMgmt->eCurrMode == WMAC_MODE_ESS_AP) && (pDevice->byBBType == BB_TYPE_11G)) {
1100
1101         // on/off protect mode
1102         if (WLAN_GET_ERP_USE_PROTECTION(pDevice->byERPFlag)) {
1103             if (!pDevice->bProtectMode) {
1104                 MACvEnableProtectMD(pDevice);
1105                 pDevice->bProtectMode = TRUE;
1106             }
1107         }
1108         else {
1109             if (pDevice->bProtectMode) {
1110                 MACvDisableProtectMD(pDevice);
1111                 pDevice->bProtectMode = FALSE;
1112             }
1113         }
1114         // on/off short slot time
1115
1116         if (uNonShortSlotSTACnt > 0) {
1117             if (pDevice->bShortSlotTime) {
1118                 pDevice->bShortSlotTime = FALSE;
1119                 BBvSetShortSlotTime(pDevice);
1120                 vUpdateIFS((void *)pDevice);
1121             }
1122         }
1123         else {
1124             if (!pDevice->bShortSlotTime) {
1125                 pDevice->bShortSlotTime = TRUE;
1126                 BBvSetShortSlotTime(pDevice);
1127                 vUpdateIFS((void *)pDevice);
1128             }
1129         }
1130
1131         // on/off barker long preamble mode
1132
1133         if (uLongPreambleSTACnt > 0) {
1134             if (!pDevice->bBarkerPreambleMd) {
1135                 MACvEnableBarkerPreambleMd(pDevice);
1136                 pDevice->bBarkerPreambleMd = TRUE;
1137             }
1138         }
1139         else {
1140             if (pDevice->bBarkerPreambleMd) {
1141                 MACvDisableBarkerPreambleMd(pDevice);
1142                 pDevice->bBarkerPreambleMd = FALSE;
1143             }
1144         }
1145
1146     }
1147
1148
1149     // Check if any STA in PS mode, enable DTIM multicast deliver
1150     if (pMgmt->eCurrMode == WMAC_MODE_ESS_AP) {
1151         if (uSleepySTACnt > 0)
1152             pMgmt->sNodeDBTable[0].bPSEnable = TRUE;
1153         else
1154             pMgmt->sNodeDBTable[0].bPSEnable = FALSE;
1155     }
1156
1157     pItemSSID = (PWLAN_IE_SSID)pMgmt->abyDesireSSID;
1158     pCurrSSID = (PWLAN_IE_SSID)pMgmt->abyCurrSSID;
1159
1160     if ((pMgmt->eCurrMode == WMAC_MODE_STANDBY) ||
1161         (pMgmt->eCurrMode == WMAC_MODE_ESS_STA)) {
1162
1163         if (pMgmt->sNodeDBTable[0].bActive) { // Assoc with BSS
1164            // DBG_PRT(MSG_LEVEL_INFO, KERN_INFO "Callback inactive Count = [%d]\n", pMgmt->sNodeDBTable[0].uInActiveCount);
1165
1166             if (pDevice->bUpdateBBVGA) {
1167                 /* s_vCheckSensitivity((void *) pDevice); */
1168                 s_vCheckPreEDThreshold((void *) pDevice);
1169             }
1170
1171             if ((pMgmt->sNodeDBTable[0].uInActiveCount >= (LOST_BEACON_COUNT/2)) &&
1172                 (pDevice->byBBVGACurrent != pDevice->abyBBVGA[0]) ) {
1173                 pDevice->byBBVGANew = pDevice->abyBBVGA[0];
1174                 bScheduleCommand((void *) pDevice,
1175                                  WLAN_CMD_CHANGE_BBSENSITIVITY,
1176                                  NULL);
1177             }
1178
1179                 if (pMgmt->sNodeDBTable[0].uInActiveCount >= LOST_BEACON_COUNT) {
1180                 pMgmt->sNodeDBTable[0].bActive = FALSE;
1181                 pMgmt->eCurrMode = WMAC_MODE_STANDBY;
1182                 pMgmt->eCurrState = WMAC_STATE_IDLE;
1183                 netif_stop_queue(pDevice->dev);
1184                 pDevice->bLinkPass = FALSE;
1185                 ControlvMaskByte(pDevice,MESSAGE_REQUEST_MACREG,MAC_REG_PAPEDELAY,LEDSTS_STS,LEDSTS_SLOW);
1186                 pDevice->bRoaming = TRUE;
1187                 pDevice->bIsRoaming = FALSE;
1188
1189                 DBG_PRT(MSG_LEVEL_NOTICE, KERN_INFO "Lost AP beacon [%d] sec, disconnected !\n", pMgmt->sNodeDBTable[0].uInActiveCount);
1190         //let wpa supplicant know AP may disconnect.//20080717-01,<Add> by James Li
1191         if ((pDevice->bWPADEVUp) && (pDevice->skb != NULL)) {
1192              wpahdr = (viawget_wpa_header *)pDevice->skb->data;
1193              wpahdr->type = VIAWGET_DISASSOC_MSG;
1194              wpahdr->resp_ie_len = 0;
1195              wpahdr->req_ie_len = 0;
1196              skb_put(pDevice->skb, sizeof(viawget_wpa_header));
1197              pDevice->skb->dev = pDevice->wpadev;
1198              skb_reset_mac_header(pDevice->skb);
1199              pDevice->skb->pkt_type = PACKET_HOST;
1200              pDevice->skb->protocol = htons(ETH_P_802_2);
1201              memset(pDevice->skb->cb, 0, sizeof(pDevice->skb->cb));
1202              netif_rx(pDevice->skb);
1203              pDevice->skb = dev_alloc_skb((int)pDevice->rx_buf_sz);
1204          };
1205    #ifdef WPA_SUPPLICANT_DRIVER_WEXT_SUPPORT
1206   // if(pDevice->bWPASuppWextEnabled == TRUE)
1207       {
1208         union iwreq_data  wrqu;
1209         memset(&wrqu, 0, sizeof (wrqu));
1210         wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1211         PRINT_K("wireless_send_event--->SIOCGIWAP(disassociated)\n");
1212         wireless_send_event(pDevice->dev, SIOCGIWAP, &wrqu, NULL);
1213      }
1214   #endif
1215             }
1216         }
1217         else if (pItemSSID->len != 0) {
1218 //Davidwang
1219       if ((pDevice->bEnableRoaming == TRUE)&&(!(pMgmt->Cisco_cckm))) {
1220 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "bRoaming %d, !\n", pDevice->bRoaming );
1221 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "bIsRoaming %d, !\n", pDevice->bIsRoaming );
1222           if ((pDevice->bRoaming == TRUE)&&(pDevice->bIsRoaming == TRUE)){
1223                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Fast   Roaming ...\n");
1224                 BSSvClearBSSList((void *) pDevice, pDevice->bLinkPass);
1225                 bScheduleCommand((void *) pDevice,
1226                                  WLAN_CMD_BSSID_SCAN,
1227                                  pMgmt->abyDesireSSID);
1228                 bScheduleCommand((void *) pDevice,
1229                                  WLAN_CMD_SSID,
1230                                  pMgmt->abyDesireSSID);
1231                 pDevice->uAutoReConnectTime = 0;
1232                 pDevice->uIsroamingTime = 0;
1233                 pDevice->bRoaming = FALSE;
1234
1235 //            if ((pDevice->bWPADEVUp) && (pDevice->skb != NULL)) {
1236              wpahdr = (viawget_wpa_header *)pDevice->skb->data;
1237              wpahdr->type = VIAWGET_CCKM_ROAM_MSG;
1238              wpahdr->resp_ie_len = 0;
1239              wpahdr->req_ie_len = 0;
1240              skb_put(pDevice->skb, sizeof(viawget_wpa_header));
1241              pDevice->skb->dev = pDevice->wpadev;
1242              skb_reset_mac_header(pDevice->skb);
1243              pDevice->skb->pkt_type = PACKET_HOST;
1244              pDevice->skb->protocol = htons(ETH_P_802_2);
1245              memset(pDevice->skb->cb, 0, sizeof(pDevice->skb->cb));
1246              netif_rx(pDevice->skb);
1247             pDevice->skb = dev_alloc_skb((int)pDevice->rx_buf_sz);
1248
1249 //         }
1250           }
1251       else if ((pDevice->bRoaming == FALSE)&&(pDevice->bIsRoaming == TRUE)) {
1252                             pDevice->uIsroamingTime++;
1253        if (pDevice->uIsroamingTime >= 20)
1254             pDevice->bIsRoaming = FALSE;
1255          }
1256
1257    }
1258 else {
1259             if (pDevice->uAutoReConnectTime < 10) {
1260                 pDevice->uAutoReConnectTime++;
1261                #ifdef WPA_SUPPLICANT_DRIVER_WEXT_SUPPORT
1262                 //network manager support need not do Roaming scan???
1263                 if(pDevice->bWPASuppWextEnabled ==TRUE)
1264                  pDevice->uAutoReConnectTime = 0;
1265              #endif
1266             }
1267             else {
1268             //mike use old encryption status for wpa reauthen
1269               if(pDevice->bWPADEVUp)
1270                   pDevice->eEncryptionStatus = pDevice->eOldEncryptionStatus;
1271
1272                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Roaming ...\n");
1273                 BSSvClearBSSList((void *) pDevice, pDevice->bLinkPass);
1274                 pMgmt->eScanType = WMAC_SCAN_ACTIVE;
1275                 bScheduleCommand((void *) pDevice,
1276                                  WLAN_CMD_BSSID_SCAN,
1277                                  pMgmt->abyDesireSSID);
1278                 bScheduleCommand((void *) pDevice,
1279                                  WLAN_CMD_SSID,
1280                                  pMgmt->abyDesireSSID);
1281                 pDevice->uAutoReConnectTime = 0;
1282             }
1283         }
1284     }
1285     }
1286
1287     if (pMgmt->eCurrMode == WMAC_MODE_IBSS_STA) {
1288         // if adhoc started which essid is NULL string, rescaning.
1289         if ((pMgmt->eCurrState == WMAC_STATE_STARTED) && (pCurrSSID->len == 0)) {
1290             if (pDevice->uAutoReConnectTime < 10) {
1291                 pDevice->uAutoReConnectTime++;
1292             }
1293             else {
1294                 DBG_PRT(MSG_LEVEL_NOTICE, KERN_INFO "Adhoc re-scaning ...\n");
1295                pMgmt->eScanType = WMAC_SCAN_ACTIVE;
1296                 bScheduleCommand((void *) pDevice, WLAN_CMD_BSSID_SCAN, NULL);
1297                 bScheduleCommand((void *) pDevice, WLAN_CMD_SSID, NULL);
1298                 pDevice->uAutoReConnectTime = 0;
1299             };
1300         }
1301         if (pMgmt->eCurrState == WMAC_STATE_JOINTED) {
1302
1303                 if (pDevice->bUpdateBBVGA) {
1304                         /* s_vCheckSensitivity((void *) pDevice); */
1305                         s_vCheckPreEDThreshold((void *) pDevice);
1306                 }
1307                 if (pMgmt->sNodeDBTable[0].uInActiveCount >=ADHOC_LOST_BEACON_COUNT) {
1308                     DBG_PRT(MSG_LEVEL_NOTICE, KERN_INFO "Lost other STA beacon [%d] sec, started !\n", pMgmt->sNodeDBTable[0].uInActiveCount);
1309                 pMgmt->sNodeDBTable[0].uInActiveCount = 0;
1310                 pMgmt->eCurrState = WMAC_STATE_STARTED;
1311                 netif_stop_queue(pDevice->dev);
1312                 pDevice->bLinkPass = FALSE;
1313                 ControlvMaskByte(pDevice,MESSAGE_REQUEST_MACREG,MAC_REG_PAPEDELAY,LEDSTS_STS,LEDSTS_SLOW);
1314             }
1315         }
1316     }
1317
1318     if (pDevice->bLinkPass == TRUE) {
1319         if (netif_queue_stopped(pDevice->dev))
1320             netif_wake_queue(pDevice->dev);
1321     }
1322
1323     spin_unlock_irq(&pDevice->lock);
1324
1325     pMgmt->sTimerSecondCallback.expires = RUN_AT(HZ);
1326     add_timer(&pMgmt->sTimerSecondCallback);
1327     return;
1328 }
1329
1330 /*+
1331  *
1332  * Routine Description:
1333  *
1334  *
1335  *  Update Tx attemps, Tx failure counter in Node DB
1336  *
1337  *
1338  * Return Value:
1339  *    none.
1340  *
1341 -*/
1342
1343 void BSSvUpdateNodeTxCounter(void *hDeviceContext,
1344                              PSStatCounter pStatistic,
1345                              BYTE byTSR,
1346                              BYTE byPktNO)
1347 {
1348     PSDevice        pDevice = (PSDevice)hDeviceContext;
1349     PSMgmtObject    pMgmt = &(pDevice->sMgmtObj);
1350     unsigned int            uNodeIndex = 0;
1351     BYTE            byTxRetry;
1352     WORD            wRate;
1353     WORD            wFallBackRate = RATE_1M;
1354     BYTE            byFallBack;
1355     unsigned int            ii;
1356     PBYTE           pbyDestAddr;
1357     BYTE            byPktNum;
1358     WORD            wFIFOCtl;
1359
1360     byPktNum = (byPktNO & 0x0F) >> 4;
1361     byTxRetry = (byTSR & 0xF0) >> 4;
1362     wRate = (WORD) (byPktNO & 0xF0) >> 4;
1363     wFIFOCtl = pStatistic->abyTxPktInfo[byPktNum].wFIFOCtl;
1364     pbyDestAddr = (PBYTE) &( pStatistic->abyTxPktInfo[byPktNum].abyDestAddr[0]);
1365
1366     if (wFIFOCtl & FIFOCTL_AUTO_FB_0) {
1367         byFallBack = AUTO_FB_0;
1368     } else if (wFIFOCtl & FIFOCTL_AUTO_FB_1) {
1369         byFallBack = AUTO_FB_1;
1370     } else {
1371         byFallBack = AUTO_FB_NONE;
1372     }
1373
1374     // Only Unicast using support rates
1375     if (wFIFOCtl & FIFOCTL_NEEDACK) {
1376         //DBG_PRN_GRP21(("Device %08X, wRate %04X, byTSR %02X\n", hDeviceContext, wRate, byTSR));
1377         if (pMgmt->eCurrMode == WMAC_MODE_ESS_STA) {
1378             pMgmt->sNodeDBTable[0].uTxAttempts += 1;
1379             if ( !(byTSR & (TSR_TMO | TSR_RETRYTMO))) {
1380                 // transmit success, TxAttempts at least plus one
1381                 pMgmt->sNodeDBTable[0].uTxOk[MAX_RATE]++;
1382                 if ( (byFallBack == AUTO_FB_NONE) ||
1383                      (wRate < RATE_18M) ) {
1384                     wFallBackRate = wRate;
1385                 } else if (byFallBack == AUTO_FB_0) {
1386                     if (byTxRetry < 5)
1387                         wFallBackRate = awHWRetry0[wRate-RATE_18M][byTxRetry];
1388                     else
1389                         wFallBackRate = awHWRetry0[wRate-RATE_18M][4];
1390                 } else if (byFallBack == AUTO_FB_1) {
1391                     if (byTxRetry < 5)
1392                         wFallBackRate = awHWRetry1[wRate-RATE_18M][byTxRetry];
1393                     else
1394                         wFallBackRate = awHWRetry1[wRate-RATE_18M][4];
1395                 }
1396                 pMgmt->sNodeDBTable[0].uTxOk[wFallBackRate]++;
1397             } else {
1398                 pMgmt->sNodeDBTable[0].uTxFailures ++;
1399             }
1400             pMgmt->sNodeDBTable[0].uTxRetry += byTxRetry;
1401             if (byTxRetry != 0) {
1402                 pMgmt->sNodeDBTable[0].uTxFail[MAX_RATE]+=byTxRetry;
1403                 if ( (byFallBack == AUTO_FB_NONE) ||
1404                      (wRate < RATE_18M) ) {
1405                     pMgmt->sNodeDBTable[0].uTxFail[wRate]+=byTxRetry;
1406                 } else if (byFallBack == AUTO_FB_0) {
1407                         for (ii = 0; ii < byTxRetry; ii++) {
1408                                 if (ii < 5)
1409                                         wFallBackRate =
1410                                                 awHWRetry0[wRate-RATE_18M][ii];
1411                                 else
1412                                         wFallBackRate =
1413                                                 awHWRetry0[wRate-RATE_18M][4];
1414                                 pMgmt->sNodeDBTable[0].uTxFail[wFallBackRate]++;
1415                         }
1416                 } else if (byFallBack == AUTO_FB_1) {
1417                         for (ii = 0; ii < byTxRetry; ii++) {
1418                                 if (ii < 5)
1419                                         wFallBackRate =
1420                                                 awHWRetry1[wRate-RATE_18M][ii];
1421                                 else
1422                                         wFallBackRate =
1423                                                 awHWRetry1[wRate-RATE_18M][4];
1424                                 pMgmt->sNodeDBTable[0].uTxFail[wFallBackRate]++;
1425                         }
1426                 }
1427             }
1428         };
1429
1430         if ((pMgmt->eCurrMode == WMAC_MODE_IBSS_STA) ||
1431             (pMgmt->eCurrMode == WMAC_MODE_ESS_AP)) {
1432
1433                 if (BSSbIsSTAInNodeDB((void *) pDevice,
1434                                       pbyDestAddr,
1435                                       &uNodeIndex)) {
1436                         pMgmt->sNodeDBTable[uNodeIndex].uTxAttempts += 1;
1437                 if ( !(byTSR & (TSR_TMO | TSR_RETRYTMO))) {
1438                     // transmit success, TxAttempts at least plus one
1439                     pMgmt->sNodeDBTable[uNodeIndex].uTxOk[MAX_RATE]++;
1440                     if ( (byFallBack == AUTO_FB_NONE) ||
1441                          (wRate < RATE_18M) ) {
1442                         wFallBackRate = wRate;
1443                     } else if (byFallBack == AUTO_FB_0) {
1444                         if (byTxRetry < 5)
1445                             wFallBackRate = awHWRetry0[wRate-RATE_18M][byTxRetry];
1446                         else
1447                             wFallBackRate = awHWRetry0[wRate-RATE_18M][4];
1448                     } else if (byFallBack == AUTO_FB_1) {
1449                         if (byTxRetry < 5)
1450                             wFallBackRate = awHWRetry1[wRate-RATE_18M][byTxRetry];
1451                         else
1452                             wFallBackRate = awHWRetry1[wRate-RATE_18M][4];
1453                     }
1454                     pMgmt->sNodeDBTable[uNodeIndex].uTxOk[wFallBackRate]++;
1455                 } else {
1456                     pMgmt->sNodeDBTable[uNodeIndex].uTxFailures ++;
1457                 }
1458                 pMgmt->sNodeDBTable[uNodeIndex].uTxRetry += byTxRetry;
1459                 if (byTxRetry != 0) {
1460                     pMgmt->sNodeDBTable[uNodeIndex].uTxFail[MAX_RATE]+=byTxRetry;
1461                     if ( (byFallBack == AUTO_FB_NONE) ||
1462                          (wRate < RATE_18M) ) {
1463                         pMgmt->sNodeDBTable[uNodeIndex].uTxFail[wRate]+=byTxRetry;
1464                     } else if (byFallBack == AUTO_FB_0) {
1465                         for (ii = 0; ii < byTxRetry; ii++) {
1466                                 if (ii < 5)
1467                                         wFallBackRate =
1468                                                 awHWRetry0[wRate-RATE_18M][ii];
1469                                 else
1470                                         wFallBackRate =
1471                                                 awHWRetry0[wRate-RATE_18M][4];
1472                                 pMgmt->sNodeDBTable[uNodeIndex].uTxFail[wFallBackRate]++;
1473                         }
1474                     } else if (byFallBack == AUTO_FB_1) {
1475                       for (ii = 0; ii < byTxRetry; ii++) {
1476                         if (ii < 5)
1477                                 wFallBackRate = awHWRetry1[wRate-RATE_18M][ii];
1478                         else
1479                                 wFallBackRate = awHWRetry1[wRate-RATE_18M][4];
1480                         pMgmt->sNodeDBTable[uNodeIndex].uTxFail[wFallBackRate]++;
1481                       }
1482                     }
1483                 }
1484             };
1485         }
1486     };
1487
1488     return;
1489
1490
1491 }
1492
1493 /*+
1494  *
1495  * Routine Description:
1496  *    Clear Nodes & skb in DB Table
1497  *
1498  *
1499  * Parameters:
1500  *  In:
1501  *      hDeviceContext        - The adapter context.
1502  *      uStartIndex           - starting index
1503  *  Out:
1504  *      none
1505  *
1506  * Return Value:
1507  *    None.
1508  *
1509 -*/
1510
1511 void BSSvClearNodeDBTable(void *hDeviceContext,
1512                           unsigned int uStartIndex)
1513 {
1514     PSDevice     pDevice = (PSDevice)hDeviceContext;
1515     PSMgmtObject    pMgmt = &(pDevice->sMgmtObj);
1516     struct sk_buff  *skb;
1517     unsigned int            ii;
1518
1519     for (ii = uStartIndex; ii < (MAX_NODE_NUM + 1); ii++) {
1520         if (pMgmt->sNodeDBTable[ii].bActive) {
1521             // check if sTxPSQueue has been initial
1522             if (pMgmt->sNodeDBTable[ii].sTxPSQueue.next != NULL) {
1523                 while ((skb = skb_dequeue(&pMgmt->sNodeDBTable[ii].sTxPSQueue)) != NULL){
1524                         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "PS skb != NULL %d\n", ii);
1525                         dev_kfree_skb(skb);
1526                 }
1527             }
1528             memset(&pMgmt->sNodeDBTable[ii], 0, sizeof(KnownNodeDB));
1529         }
1530     }
1531
1532     return;
1533 };
1534
1535 void s_vCheckSensitivity(void *hDeviceContext)
1536 {
1537     PSDevice        pDevice = (PSDevice)hDeviceContext;
1538     PKnownBSS       pBSSList = NULL;
1539     PSMgmtObject    pMgmt = &(pDevice->sMgmtObj);
1540     int             ii;
1541
1542     if ((pMgmt->eCurrState == WMAC_STATE_ASSOC) ||
1543         ((pMgmt->eCurrMode == WMAC_MODE_IBSS_STA) && (pMgmt->eCurrState == WMAC_STATE_JOINTED))) {
1544         pBSSList = BSSpAddrIsInBSSList(pDevice, pMgmt->abyCurrBSSID, (PWLAN_IE_SSID)pMgmt->abyCurrSSID);
1545         if (pBSSList != NULL) {
1546                 /* Update BB register if RSSI is too strong */
1547                 signed long    LocalldBmAverage = 0;
1548                 signed long    uNumofdBm = 0;
1549             for (ii = 0; ii < RSSI_STAT_COUNT; ii++) {
1550                 if (pBSSList->ldBmAverage[ii] != 0) {
1551                     uNumofdBm ++;
1552                     LocalldBmAverage += pBSSList->ldBmAverage[ii];
1553                 }
1554             }
1555             if (uNumofdBm > 0) {
1556                 LocalldBmAverage = LocalldBmAverage/uNumofdBm;
1557                 for (ii=0;ii<BB_VGA_LEVEL;ii++) {
1558                     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"LocalldBmAverage:%ld, %ld %02x\n", LocalldBmAverage, pDevice->ldBmThreshold[ii], pDevice->abyBBVGA[ii]);
1559                     if (LocalldBmAverage < pDevice->ldBmThreshold[ii]) {
1560                             pDevice->byBBVGANew = pDevice->abyBBVGA[ii];
1561                         break;
1562                     }
1563                 }
1564                 if (pDevice->byBBVGANew != pDevice->byBBVGACurrent) {
1565                     pDevice->uBBVGADiffCount++;
1566                     if (pDevice->uBBVGADiffCount >= BB_VGA_CHANGE_THRESHOLD)
1567                         bScheduleCommand((void *) pDevice,
1568                                          WLAN_CMD_CHANGE_BBSENSITIVITY,
1569                                          NULL);
1570                 } else {
1571                     pDevice->uBBVGADiffCount = 0;
1572                 }
1573             }
1574         }
1575     }
1576 }
1577
1578 #ifdef Calcu_LinkQual
1579 void s_uCalculateLinkQual(void *hDeviceContext)
1580 {
1581    PSDevice        pDevice = (PSDevice)hDeviceContext;
1582    unsigned long TxOkRatio, TxCnt;
1583    unsigned long RxOkRatio, RxCnt;
1584    unsigned long RssiRatio;
1585    long ldBm;
1586
1587 TxCnt = pDevice->scStatistic.TxNoRetryOkCount +
1588               pDevice->scStatistic.TxRetryOkCount +
1589               pDevice->scStatistic.TxFailCount;
1590 RxCnt = pDevice->scStatistic.RxFcsErrCnt +
1591               pDevice->scStatistic.RxOkCnt;
1592 TxOkRatio = (TxCnt < 6) ? 4000:((pDevice->scStatistic.TxNoRetryOkCount * 4000) / TxCnt);
1593 RxOkRatio = (RxCnt < 6) ? 2000:((pDevice->scStatistic.RxOkCnt * 2000) / RxCnt);
1594 //decide link quality
1595 if(pDevice->bLinkPass !=TRUE)
1596 {
1597  //  printk("s_uCalculateLinkQual-->Link disconnect and Poor quality**\n");
1598    pDevice->scStatistic.LinkQuality = 0;
1599    pDevice->scStatistic.SignalStren = 0;
1600 }
1601 else
1602 {
1603    RFvRSSITodBm(pDevice, (BYTE)(pDevice->uCurrRSSI), &ldBm);
1604    if(-ldBm < 50)  {
1605         RssiRatio = 4000;
1606      }
1607    else if(-ldBm > 90) {
1608         RssiRatio = 0;
1609      }
1610    else {
1611         RssiRatio = (40-(-ldBm-50))*4000/40;
1612      }
1613    pDevice->scStatistic.SignalStren = RssiRatio/40;
1614    pDevice->scStatistic.LinkQuality = (RssiRatio+TxOkRatio+RxOkRatio)/100;
1615 }
1616    pDevice->scStatistic.RxFcsErrCnt = 0;
1617    pDevice->scStatistic.RxOkCnt = 0;
1618    pDevice->scStatistic.TxFailCount = 0;
1619    pDevice->scStatistic.TxNoRetryOkCount = 0;
1620    pDevice->scStatistic.TxRetryOkCount = 0;
1621    return;
1622 }
1623 #endif
1624
1625 void BSSvClearAnyBSSJoinRecord(void *hDeviceContext)
1626 {
1627     PSDevice        pDevice = (PSDevice)hDeviceContext;
1628     PSMgmtObject    pMgmt = &(pDevice->sMgmtObj);
1629     unsigned int            ii;
1630
1631     for (ii = 0; ii < MAX_BSS_NUM; ii++) {
1632         pMgmt->sBSSList[ii].bSelected = FALSE;
1633     }
1634     return;
1635 }
1636
1637 void s_vCheckPreEDThreshold(void *hDeviceContext)
1638 {
1639     PSDevice        pDevice = (PSDevice)hDeviceContext;
1640     PKnownBSS       pBSSList = NULL;
1641     PSMgmtObject    pMgmt = &(pDevice->sMgmtObj);
1642
1643     if ((pMgmt->eCurrState == WMAC_STATE_ASSOC) ||
1644         ((pMgmt->eCurrMode == WMAC_MODE_IBSS_STA) && (pMgmt->eCurrState == WMAC_STATE_JOINTED))) {
1645         pBSSList = BSSpAddrIsInBSSList(pDevice, pMgmt->abyCurrBSSID, (PWLAN_IE_SSID)pMgmt->abyCurrSSID);
1646         if (pBSSList != NULL) {
1647             pDevice->byBBPreEDRSSI = (BYTE) (~(pBSSList->ldBmAverRange) + 1);
1648             BBvUpdatePreEDThreshold(pDevice, FALSE);
1649         }
1650     }
1651     return;
1652 }
1653