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