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