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