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