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