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