linux/drivers/staging/vt6656/card.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: card.c
  20 * Purpose: Provide functions to setup NIC operation mode
  21 * Functions:
  22 *      s_vSafeResetTx - Rest Tx
  23 *      CARDvSetRSPINF - Set RSPINF
  24 *      vUpdateIFS - Update slotTime,SIFS,DIFS, and EIFS
  25 *      CARDvUpdateBasicTopRate - Update BasicTopRate
  26 *      CARDbAddBasicRate - Add to BasicRateSet
  27 *      CARDbSetBasicRate - Set Basic Tx Rate
  28 *      CARDbIsOFDMinBasicRate - Check if any OFDM rate is in BasicRateSet
  29 *      CARDvSetLoopbackMode - Set Loopback mode
  30 *      CARDbSoftwareReset - Sortware reset NIC
  31 *      CARDqGetTSFOffset - Calculate TSFOffset
  32 *      CARDbGetCurrentTSF - Read Current NIC TSF counter
  33 *      CARDqGetNextTBTT - Calculate Next Beacon TSF counter
  34 *      CARDvSetFirstNextTBTT - Set NIC Beacon time
  35 *      CARDvUpdateNextTBTT - Sync. NIC Beacon time
  36 *      CARDbRadioPowerOff - Turn Off NIC Radio Power
  37 *      CARDbRadioPowerOn - Turn On NIC Radio Power
  38 *      CARDbSetWEPMode - Set NIC Wep mode
  39 *      CARDbSetTxPower - Set NIC tx power
  40 *
  41 * Revision History:
  42 *      06-10-2003 Bryan YC Fan:  Re-write codes to support VT3253 spec.
  43 *      08-26-2003 Kyle Hsu:      Modify the definition type of dwIoBase.
  44 *      09-01-2003 Bryan YC Fan:  Add vUpdateIFS().
  45 *
  46 */
  47
  48#include "device.h"
  49#include "tmacro.h"
  50#include "card.h"
  51#include "baseband.h"
  52#include "mac.h"
  53#include "desc.h"
  54#include "rf.h"
  55#include "power.h"
  56#include "key.h"
  57#include "rc4.h"
  58#include "country.h"
  59#include "datarate.h"
  60#include "rndis.h"
  61#include "control.h"
  62
  63//static int          msglevel                =MSG_LEVEL_DEBUG;
  64static int          msglevel                =MSG_LEVEL_INFO;
  65
  66//const u16 cwRXBCNTSFOff[MAX_RATE] =
  67//{17, 34, 96, 192, 34, 23, 17, 11, 8, 5, 4, 3};
  68
  69const u16 cwRXBCNTSFOff[MAX_RATE] =
  70{192, 96, 34, 17, 34, 23, 17, 11, 8, 5, 4, 3};
  71
  72/*
  73 * Description: Set NIC media channel
  74 *
  75 * Parameters:
  76 *  In:
  77 *      pDevice             - The adapter to be set
  78 *      uConnectionChannel  - Channel to be set
  79 *  Out:
  80 *      none
  81 */
  82void CARDbSetMediaChannel(struct vnt_private *pDevice, u32 uConnectionChannel)
  83{
  84
  85    if (pDevice->byBBType == BB_TYPE_11A) { // 15 ~ 38
  86        if ((uConnectionChannel < (CB_MAX_CHANNEL_24G+1)) || (uConnectionChannel > CB_MAX_CHANNEL))
  87            uConnectionChannel = (CB_MAX_CHANNEL_24G+1);
  88    } else {
  89        if ((uConnectionChannel > CB_MAX_CHANNEL_24G) || (uConnectionChannel == 0)) // 1 ~ 14
  90            uConnectionChannel = 1;
  91    }
  92
  93    // clear NAV
  94    MACvRegBitsOn(pDevice, MAC_REG_MACCR, MACCR_CLRNAV);
  95
  96    // Set Channel[7] = 0 to tell H/W channel is changing now.
  97    MACvRegBitsOff(pDevice, MAC_REG_CHANNEL, 0x80);
  98
  99    //if (pMgmt->uCurrChannel == uConnectionChannel)
 100    //    return bResult;
 101
 102    CONTROLnsRequestOut(pDevice,
 103                        MESSAGE_TYPE_SELECT_CHANNLE,
 104                        (u16) uConnectionChannel,
 105                        0,
 106                        0,
 107                        NULL
 108                        );
 109
 110    //{{ RobertYu: 20041202
 111    //// TX_PE will reserve 3 us for MAX2829 A mode only, it is for better TX throughput
 112
 113    if (pDevice->byBBType == BB_TYPE_11A) {
 114        pDevice->byCurPwr = 0xFF;
 115        RFbRawSetPower(pDevice, pDevice->abyOFDMAPwrTbl[uConnectionChannel-15], RATE_54M);
 116    } else if (pDevice->byBBType == BB_TYPE_11G) {
 117        pDevice->byCurPwr = 0xFF;
 118        RFbRawSetPower(pDevice, pDevice->abyOFDMPwrTbl[uConnectionChannel-1], RATE_54M);
 119    } else {
 120        pDevice->byCurPwr = 0xFF;
 121        RFbRawSetPower(pDevice, pDevice->abyCCKPwrTbl[uConnectionChannel-1], RATE_1M);
 122    }
 123    ControlvWriteByte(pDevice,MESSAGE_REQUEST_MACREG,MAC_REG_CHANNEL,(u8)(uConnectionChannel|0x80));
 124}
 125
 126/*
 127 * Description: Get CCK mode basic rate
 128 *
 129 * Parameters:
 130 *  In:
 131 *      pDevice             - The adapter to be set
 132 *      wRateIdx            - Receiving data rate
 133 *  Out:
 134 *      none
 135 *
 136 * Return Value: response Control frame rate
 137 *
 138 */
 139static u16 swGetCCKControlRate(struct vnt_private *pDevice, u16 wRateIdx)
 140{
 141        u16 ui = wRateIdx;
 142
 143        while (ui > RATE_1M) {
 144                if (pDevice->wBasicRate & (1 << ui))
 145                        return ui;
 146                ui--;
 147        }
 148
 149        return RATE_1M;
 150}
 151
 152/*
 153 * Description: Get OFDM mode basic rate
 154 *
 155 * Parameters:
 156 *  In:
 157 *      pDevice             - The adapter to be set
 158 *      wRateIdx            - Receiving data rate
 159 *  Out:
 160 *      none
 161 *
 162 * Return Value: response Control frame rate
 163 *
 164 */
 165static u16 swGetOFDMControlRate(struct vnt_private *pDevice, u16 wRateIdx)
 166{
 167        u16 ui = wRateIdx;
 168
 169        DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"BASIC RATE: %X\n",
 170                pDevice->wBasicRate);
 171
 172        if (!CARDbIsOFDMinBasicRate(pDevice)) {
 173                DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO
 174                        "swGetOFDMControlRate:(NO OFDM) %d\n", wRateIdx);
 175        if (wRateIdx > RATE_24M)
 176                wRateIdx = RATE_24M;
 177                return wRateIdx;
 178        }
 179
 180        while (ui > RATE_11M) {
 181                if (pDevice->wBasicRate & (1 << ui)) {
 182                        DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO
 183                                "swGetOFDMControlRate: %d\n", ui);
 184                        return ui;
 185                }
 186                ui--;
 187        }
 188
 189        DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"swGetOFDMControlRate: 6M\n");
 190
 191        return RATE_24M;
 192}
 193
 194/*
 195 * Description: Calculate TxRate and RsvTime fields for RSPINF in OFDM mode.
 196 *
 197 * Parameters:
 198 *  In:
 199 *      wRate           - Tx Rate
 200 *      byPktType       - Tx Packet type
 201 *  Out:
 202 *      pbyTxRate       - pointer to RSPINF TxRate field
 203 *      pbyRsvTime      - pointer to RSPINF RsvTime field
 204 *
 205 * Return Value: none
 206 *
 207 */
 208void
 209CARDvCalculateOFDMRParameter (
 210      u16 wRate,
 211      u8 byBBType,
 212     u8 * pbyTxRate,
 213     u8 * pbyRsvTime
 214    )
 215{
 216    switch (wRate) {
 217    case RATE_6M :
 218        if (byBBType == BB_TYPE_11A) {//5GHZ
 219            *pbyTxRate = 0x9B;
 220            *pbyRsvTime = 24;
 221        }
 222        else {
 223            *pbyTxRate = 0x8B;
 224            *pbyRsvTime = 30;
 225        }
 226        break;
 227
 228    case RATE_9M :
 229        if (byBBType == BB_TYPE_11A) {//5GHZ
 230            *pbyTxRate = 0x9F;
 231            *pbyRsvTime = 16;
 232        }
 233        else {
 234            *pbyTxRate = 0x8F;
 235            *pbyRsvTime = 22;
 236        }
 237        break;
 238
 239   case RATE_12M :
 240        if (byBBType == BB_TYPE_11A) {//5GHZ
 241            *pbyTxRate = 0x9A;
 242            *pbyRsvTime = 12;
 243        }
 244        else {
 245            *pbyTxRate = 0x8A;
 246            *pbyRsvTime = 18;
 247        }
 248        break;
 249
 250   case RATE_18M :
 251        if (byBBType == BB_TYPE_11A) {//5GHZ
 252            *pbyTxRate = 0x9E;
 253            *pbyRsvTime = 8;
 254        }
 255        else {
 256            *pbyTxRate = 0x8E;
 257            *pbyRsvTime = 14;
 258        }
 259        break;
 260
 261    case RATE_36M :
 262        if (byBBType == BB_TYPE_11A) {//5GHZ
 263            *pbyTxRate = 0x9D;
 264            *pbyRsvTime = 4;
 265        }
 266        else {
 267            *pbyTxRate = 0x8D;
 268            *pbyRsvTime = 10;
 269        }
 270        break;
 271
 272    case RATE_48M :
 273        if (byBBType == BB_TYPE_11A) {//5GHZ
 274            *pbyTxRate = 0x98;
 275            *pbyRsvTime = 4;
 276        }
 277        else {
 278            *pbyTxRate = 0x88;
 279            *pbyRsvTime = 10;
 280        }
 281        break;
 282
 283    case RATE_54M :
 284        if (byBBType == BB_TYPE_11A) {//5GHZ
 285            *pbyTxRate = 0x9C;
 286            *pbyRsvTime = 4;
 287        }
 288        else {
 289            *pbyTxRate = 0x8C;
 290            *pbyRsvTime = 10;
 291        }
 292        break;
 293
 294    case RATE_24M :
 295    default :
 296        if (byBBType == BB_TYPE_11A) {//5GHZ
 297            *pbyTxRate = 0x99;
 298            *pbyRsvTime = 8;
 299        }
 300        else {
 301            *pbyTxRate = 0x89;
 302            *pbyRsvTime = 14;
 303        }
 304        break;
 305    }
 306}
 307
 308/*
 309 * Description: Set RSPINF
 310 *
 311 * Parameters:
 312 *  In:
 313 *      pDevice             - The adapter to be set
 314 *  Out:
 315 *      none
 316 *
 317 * Return Value: None.
 318 *
 319 */
 320void CARDvSetRSPINF(struct vnt_private *pDevice, u8 byBBType)
 321{
 322        u8 abyServ[4] = {0, 0, 0, 0}; /* For CCK */
 323        u8 abySignal[4] = {0, 0, 0, 0};
 324        u16 awLen[4] = {0, 0, 0, 0};
 325        u8 abyTxRate[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0}; /* For OFDM */
 326        u8 abyRsvTime[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
 327        u8 abyData[34];
 328        int i;
 329
 330    //RSPINF_b_1
 331    BBvCalculateParameter(pDevice,
 332                         14,
 333                         swGetCCKControlRate(pDevice, RATE_1M),
 334                         PK_TYPE_11B,
 335                         &awLen[0],
 336                         &abyServ[0],
 337                         &abySignal[0]
 338    );
 339
 340    ///RSPINF_b_2
 341    BBvCalculateParameter(pDevice,
 342                         14,
 343                         swGetCCKControlRate(pDevice, RATE_2M),
 344                         PK_TYPE_11B,
 345                         &awLen[1],
 346                         &abyServ[1],
 347                         &abySignal[1]
 348    );
 349
 350    //RSPINF_b_5
 351    BBvCalculateParameter(pDevice,
 352                         14,
 353                         swGetCCKControlRate(pDevice, RATE_5M),
 354                         PK_TYPE_11B,
 355                         &awLen[2],
 356                         &abyServ[2],
 357                         &abySignal[2]
 358    );
 359
 360    //RSPINF_b_11
 361    BBvCalculateParameter(pDevice,
 362                         14,
 363                         swGetCCKControlRate(pDevice, RATE_11M),
 364                         PK_TYPE_11B,
 365                         &awLen[3],
 366                         &abyServ[3],
 367                         &abySignal[3]
 368    );
 369
 370    //RSPINF_a_6
 371    CARDvCalculateOFDMRParameter (RATE_6M,
 372                                 byBBType,
 373                                 &abyTxRate[0],
 374                                 &abyRsvTime[0]);
 375
 376    //RSPINF_a_9
 377    CARDvCalculateOFDMRParameter (RATE_9M,
 378                                 byBBType,
 379                                 &abyTxRate[1],
 380                                 &abyRsvTime[1]);
 381
 382    //RSPINF_a_12
 383    CARDvCalculateOFDMRParameter (RATE_12M,
 384                                 byBBType,
 385                                 &abyTxRate[2],
 386                                 &abyRsvTime[2]);
 387
 388    //RSPINF_a_18
 389    CARDvCalculateOFDMRParameter (RATE_18M,
 390                                 byBBType,
 391                                 &abyTxRate[3],
 392                                 &abyRsvTime[3]);
 393
 394    //RSPINF_a_24
 395    CARDvCalculateOFDMRParameter (RATE_24M,
 396                                 byBBType,
 397                                 &abyTxRate[4],
 398                                 &abyRsvTime[4]);
 399
 400    //RSPINF_a_36
 401    CARDvCalculateOFDMRParameter (swGetOFDMControlRate(pDevice, RATE_36M),
 402                                 byBBType,
 403                                 &abyTxRate[5],
 404                                 &abyRsvTime[5]);
 405
 406    //RSPINF_a_48
 407    CARDvCalculateOFDMRParameter (swGetOFDMControlRate(pDevice, RATE_48M),
 408                                 byBBType,
 409                                 &abyTxRate[6],
 410                                 &abyRsvTime[6]);
 411
 412    //RSPINF_a_54
 413    CARDvCalculateOFDMRParameter (swGetOFDMControlRate(pDevice, RATE_54M),
 414                                 byBBType,
 415                                 &abyTxRate[7],
 416                                 &abyRsvTime[7]);
 417
 418    //RSPINF_a_72
 419    CARDvCalculateOFDMRParameter (swGetOFDMControlRate(pDevice, RATE_54M),
 420                                 byBBType,
 421                                 &abyTxRate[8],
 422                                 &abyRsvTime[8]);
 423
 424    abyData[0] = (u8)(awLen[0]&0xFF);
 425    abyData[1] = (u8)(awLen[0]>>8);
 426    abyData[2] = abySignal[0];
 427    abyData[3] = abyServ[0];
 428
 429    abyData[4] = (u8)(awLen[1]&0xFF);
 430    abyData[5] = (u8)(awLen[1]>>8);
 431    abyData[6] = abySignal[1];
 432    abyData[7] = abyServ[1];
 433
 434    abyData[8] = (u8)(awLen[2]&0xFF);
 435    abyData[9] = (u8)(awLen[2]>>8);
 436    abyData[10] = abySignal[2];
 437    abyData[11] = abyServ[2];
 438
 439    abyData[12] = (u8)(awLen[3]&0xFF);
 440    abyData[13] = (u8)(awLen[3]>>8);
 441    abyData[14] = abySignal[3];
 442    abyData[15] = abyServ[3];
 443
 444    for (i = 0; i < 9; i++) {
 445        abyData[16+i*2] = abyTxRate[i];
 446        abyData[16+i*2+1] = abyRsvTime[i];
 447    }
 448
 449    CONTROLnsRequestOut(pDevice,
 450                        MESSAGE_TYPE_WRITE,
 451                        MAC_REG_RSPINF_B_1,
 452                        MESSAGE_REQUEST_MACREG,
 453                        34,
 454                        &abyData[0]);
 455
 456}
 457
 458/*
 459 * Description: Update IFS
 460 *
 461 * Parameters:
 462 *  In:
 463 *      pDevice             - The adapter to be set
 464 *  Out:
 465 *      none
 466 *
 467 * Return Value: None.
 468 *
 469 */
 470void vUpdateIFS(struct vnt_private *pDevice)
 471{
 472        u8 byMaxMin = 0;
 473        u8 byData[4];
 474
 475    if (pDevice->byPacketType==PK_TYPE_11A) {//0000 0000 0000 0000,11a
 476        pDevice->uSlot = C_SLOT_SHORT;
 477        pDevice->uSIFS = C_SIFS_A;
 478        pDevice->uDIFS = C_SIFS_A + 2*C_SLOT_SHORT;
 479        pDevice->uCwMin = C_CWMIN_A;
 480        byMaxMin = 4;
 481    }
 482    else if (pDevice->byPacketType==PK_TYPE_11B) {//0000 0001 0000 0000,11b
 483        pDevice->uSlot = C_SLOT_LONG;
 484        pDevice->uSIFS = C_SIFS_BG;
 485        pDevice->uDIFS = C_SIFS_BG + 2*C_SLOT_LONG;
 486          pDevice->uCwMin = C_CWMIN_B;
 487        byMaxMin = 5;
 488    }
 489    else {// PK_TYPE_11GA & PK_TYPE_11GB
 490        u8 byRate = 0;
 491        bool bOFDMRate = false;
 492        unsigned int ii = 0;
 493        PWLAN_IE_SUPP_RATES pItemRates = NULL;
 494
 495        pDevice->uSIFS = C_SIFS_BG;
 496        if (pDevice->bShortSlotTime) {
 497            pDevice->uSlot = C_SLOT_SHORT;
 498        } else {
 499            pDevice->uSlot = C_SLOT_LONG;
 500        }
 501        pDevice->uDIFS = C_SIFS_BG + 2*pDevice->uSlot;
 502
 503        pItemRates = (PWLAN_IE_SUPP_RATES)pDevice->vnt_mgmt.abyCurrSuppRates;
 504        for (ii = 0; ii < pItemRates->len; ii++) {
 505            byRate = (u8)(pItemRates->abyRates[ii]&0x7F);
 506            if (RATEwGetRateIdx(byRate) > RATE_11M) {
 507                bOFDMRate = true;
 508                break;
 509            }
 510        }
 511        if (bOFDMRate == false) {
 512                pItemRates = (PWLAN_IE_SUPP_RATES)pDevice->vnt_mgmt
 513                        .abyCurrExtSuppRates;
 514            for (ii = 0; ii < pItemRates->len; ii++) {
 515                byRate = (u8)(pItemRates->abyRates[ii]&0x7F);
 516                if (RATEwGetRateIdx(byRate) > RATE_11M) {
 517                    bOFDMRate = true;
 518                    break;
 519                }
 520            }
 521        }
 522        if (bOFDMRate == true) {
 523            pDevice->uCwMin = C_CWMIN_A;
 524            byMaxMin = 4;
 525        } else {
 526            pDevice->uCwMin = C_CWMIN_B;
 527            byMaxMin = 5;
 528        }
 529    }
 530
 531    pDevice->uCwMax = C_CWMAX;
 532    pDevice->uEIFS = C_EIFS;
 533
 534    byData[0] = (u8)pDevice->uSIFS;
 535    byData[1] = (u8)pDevice->uDIFS;
 536    byData[2] = (u8)pDevice->uEIFS;
 537    byData[3] = (u8)pDevice->uSlot;
 538    CONTROLnsRequestOut(pDevice,
 539                        MESSAGE_TYPE_WRITE,
 540                        MAC_REG_SIFS,
 541                        MESSAGE_REQUEST_MACREG,
 542                        4,
 543                        &byData[0]);
 544
 545    byMaxMin |= 0xA0;//1010 1111,C_CWMAX = 1023
 546    CONTROLnsRequestOut(pDevice,
 547                        MESSAGE_TYPE_WRITE,
 548                        MAC_REG_CWMAXMIN0,
 549                        MESSAGE_REQUEST_MACREG,
 550                        1,
 551                        &byMaxMin);
 552}
 553
 554void CARDvUpdateBasicTopRate(struct vnt_private *pDevice)
 555{
 556        u8 byTopOFDM = RATE_24M, byTopCCK = RATE_1M;
 557        u8 ii;
 558
 559     //Determines the highest basic rate.
 560     for (ii = RATE_54M; ii >= RATE_6M; ii --) {
 561         if ( (pDevice->wBasicRate) & ((u16)(1<<ii)) ) {
 562             byTopOFDM = ii;
 563             break;
 564         }
 565     }
 566     pDevice->byTopOFDMBasicRate = byTopOFDM;
 567
 568     for (ii = RATE_11M;; ii --) {
 569         if ( (pDevice->wBasicRate) & ((u16)(1<<ii)) ) {
 570             byTopCCK = ii;
 571             break;
 572         }
 573         if (ii == RATE_1M)
 574            break;
 575     }
 576     pDevice->byTopCCKBasicRate = byTopCCK;
 577 }
 578
 579/*
 580 * Description: Set NIC Tx Basic Rate
 581 *
 582 * Parameters:
 583 *  In:
 584 *      pDevice         - The adapter to be set
 585 *      wBasicRate      - Basic Rate to be set
 586 *  Out:
 587 *      none
 588 *
 589 * Return Value: true if succeeded; false if failed.
 590 *
 591 */
 592void CARDbAddBasicRate(struct vnt_private *pDevice, u16 wRateIdx)
 593{
 594        u16 wRate = (1 << wRateIdx);
 595
 596    pDevice->wBasicRate |= wRate;
 597
 598    //Determines the highest basic rate.
 599    CARDvUpdateBasicTopRate(pDevice);
 600}
 601
 602int CARDbIsOFDMinBasicRate(struct vnt_private *pDevice)
 603{
 604        int ii;
 605
 606    for (ii = RATE_54M; ii >= RATE_6M; ii --) {
 607        if ((pDevice->wBasicRate) & ((u16)(1<<ii)))
 608            return true;
 609    }
 610    return false;
 611}
 612
 613u8 CARDbyGetPktType(struct vnt_private *pDevice)
 614{
 615
 616    if (pDevice->byBBType == BB_TYPE_11A || pDevice->byBBType == BB_TYPE_11B) {
 617        return (u8)pDevice->byBBType;
 618    }
 619    else if (CARDbIsOFDMinBasicRate(pDevice)) {
 620        return PK_TYPE_11GA;
 621    }
 622    else {
 623        return PK_TYPE_11GB;
 624    }
 625}
 626
 627/*
 628 * Description: Calculate TSF offset of two TSF input
 629 *              Get TSF Offset from RxBCN's TSF and local TSF
 630 *
 631 * Parameters:
 632 *  In:
 633 *      pDevice         - The adapter to be sync.
 634 *      qwTSF1          - Rx BCN's TSF
 635 *      qwTSF2          - Local TSF
 636 *  Out:
 637 *      none
 638 *
 639 * Return Value: TSF Offset value
 640 *
 641 */
 642u64 CARDqGetTSFOffset(u8 byRxRate, u64 qwTSF1, u64 qwTSF2)
 643{
 644        u64 qwTSFOffset = 0;
 645        u16 wRxBcnTSFOffst = 0;
 646
 647        wRxBcnTSFOffst = cwRXBCNTSFOff[byRxRate % MAX_RATE];
 648
 649        qwTSF2 += (u64)wRxBcnTSFOffst;
 650
 651        qwTSFOffset = qwTSF1 - qwTSF2;
 652
 653        return qwTSFOffset;
 654}
 655
 656/*
 657 * Description: Sync. TSF counter to BSS
 658 *              Get TSF offset and write to HW
 659 *
 660 * Parameters:
 661 *  In:
 662 *      pDevice         - The adapter to be sync.
 663 *      qwBSSTimestamp  - Rx BCN's TSF
 664 *      qwLocalTSF      - Local TSF
 665 *  Out:
 666 *      none
 667 *
 668 * Return Value: none
 669 *
 670 */
 671void CARDvAdjustTSF(struct vnt_private *pDevice, u8 byRxRate,
 672                u64 qwBSSTimestamp, u64 qwLocalTSF)
 673{
 674        u64 qwTSFOffset = 0;
 675        u8 pbyData[8];
 676
 677    qwTSFOffset = CARDqGetTSFOffset(byRxRate, qwBSSTimestamp, qwLocalTSF);
 678    // adjust TSF
 679    // HW's TSF add TSF Offset reg
 680
 681        pbyData[0] = (u8)qwTSFOffset;
 682        pbyData[1] = (u8)(qwTSFOffset >> 8);
 683        pbyData[2] = (u8)(qwTSFOffset >> 16);
 684        pbyData[3] = (u8)(qwTSFOffset >> 24);
 685        pbyData[4] = (u8)(qwTSFOffset >> 32);
 686        pbyData[5] = (u8)(qwTSFOffset >> 40);
 687        pbyData[6] = (u8)(qwTSFOffset >> 48);
 688        pbyData[7] = (u8)(qwTSFOffset >> 56);
 689
 690    CONTROLnsRequestOut(pDevice,
 691                        MESSAGE_TYPE_SET_TSFTBTT,
 692                        MESSAGE_REQUEST_TSF,
 693                        0,
 694                        8,
 695                        pbyData
 696                        );
 697
 698}
 699/*
 700 * Description: Read NIC TSF counter
 701 *              Get local TSF counter
 702 *
 703 * Parameters:
 704 *  In:
 705 *      pDevice         - The adapter to be read
 706 *  Out:
 707 *      qwCurrTSF       - Current TSF counter
 708 *
 709 * Return Value: true if success; otherwise false
 710 *
 711 */
 712bool CARDbGetCurrentTSF(struct vnt_private *pDevice, u64 *pqwCurrTSF)
 713{
 714
 715        *pqwCurrTSF = pDevice->qwCurrTSF;
 716
 717        return true;
 718}
 719
 720/*
 721 * Description: Clear NIC TSF counter
 722 *              Clear local TSF counter
 723 *
 724 * Parameters:
 725 *  In:
 726 *      pDevice         - The adapter to be read
 727 *
 728 * Return Value: true if success; otherwise false
 729 *
 730 */
 731bool CARDbClearCurrentTSF(struct vnt_private *pDevice)
 732{
 733
 734        MACvRegBitsOn(pDevice, MAC_REG_TFTCTL, TFTCTL_TSFCNTRST);
 735
 736        pDevice->qwCurrTSF = 0;
 737
 738        return true;
 739}
 740
 741/*
 742 * Description: Read NIC TSF counter
 743 *              Get NEXTTBTT from adjusted TSF and Beacon Interval
 744 *
 745 * Parameters:
 746 *  In:
 747 *      qwTSF           - Current TSF counter
 748 *      wbeaconInterval - Beacon Interval
 749 *  Out:
 750 *      qwCurrTSF       - Current TSF counter
 751 *
 752 * Return Value: TSF value of next Beacon
 753 *
 754 */
 755u64 CARDqGetNextTBTT(u64 qwTSF, u16 wBeaconInterval)
 756{
 757
 758    unsigned int    uLowNextTBTT;
 759    unsigned int    uHighRemain, uLowRemain;
 760    unsigned int    uBeaconInterval;
 761
 762    uBeaconInterval = wBeaconInterval * 1024;
 763    // Next TBTT = ((local_current_TSF / beacon_interval) + 1 ) * beacon_interval
 764        uLowNextTBTT = ((qwTSF & 0xffffffffU) >> 10) << 10;
 765        uLowRemain = (uLowNextTBTT) % uBeaconInterval;
 766        uHighRemain = ((0x80000000 % uBeaconInterval) * 2 * (u32)(qwTSF >> 32))
 767                % uBeaconInterval;
 768        uLowRemain = (uHighRemain + uLowRemain) % uBeaconInterval;
 769        uLowRemain = uBeaconInterval - uLowRemain;
 770
 771    // check if carry when add one beacon interval
 772        if ((~uLowNextTBTT) < uLowRemain)
 773                qwTSF = ((qwTSF >> 32) + 1) << 32;
 774
 775        qwTSF = (qwTSF & 0xffffffff00000000ULL) |
 776                (u64)(uLowNextTBTT + uLowRemain);
 777
 778    return (qwTSF);
 779}
 780
 781/*
 782 * Description: Set NIC TSF counter for first Beacon time
 783 *              Get NEXTTBTT from adjusted TSF and Beacon Interval
 784 *
 785 * Parameters:
 786 *  In:
 787 *      dwIoBase        - IO Base
 788 *      wBeaconInterval - Beacon Interval
 789 *  Out:
 790 *      none
 791 *
 792 * Return Value: none
 793 *
 794 */
 795void CARDvSetFirstNextTBTT(struct vnt_private *pDevice, u16 wBeaconInterval)
 796{
 797        u64 qwNextTBTT = 0;
 798        u8 pbyData[8];
 799
 800        CARDbClearCurrentTSF(pDevice);
 801    //CARDbGetCurrentTSF(pDevice, &qwNextTBTT); //Get Local TSF counter
 802        qwNextTBTT = CARDqGetNextTBTT(qwNextTBTT, wBeaconInterval);
 803    // Set NextTBTT
 804
 805        pbyData[0] = (u8)qwNextTBTT;
 806        pbyData[1] = (u8)(qwNextTBTT >> 8);
 807        pbyData[2] = (u8)(qwNextTBTT >> 16);
 808        pbyData[3] = (u8)(qwNextTBTT >> 24);
 809        pbyData[4] = (u8)(qwNextTBTT >> 32);
 810        pbyData[5] = (u8)(qwNextTBTT >> 40);
 811        pbyData[6] = (u8)(qwNextTBTT >> 48);
 812        pbyData[7] = (u8)(qwNextTBTT >> 56);
 813
 814    CONTROLnsRequestOut(pDevice,
 815                        MESSAGE_TYPE_SET_TSFTBTT,
 816                        MESSAGE_REQUEST_TBTT,
 817                        0,
 818                        8,
 819                        pbyData
 820                        );
 821
 822    return;
 823}
 824
 825/*
 826 * Description: Sync NIC TSF counter for Beacon time
 827 *              Get NEXTTBTT and write to HW
 828 *
 829 * Parameters:
 830 *  In:
 831 *      pDevice         - The adapter to be set
 832 *      qwTSF           - Current TSF counter
 833 *      wBeaconInterval - Beacon Interval
 834 *  Out:
 835 *      none
 836 *
 837 * Return Value: none
 838 *
 839 */
 840void CARDvUpdateNextTBTT(struct vnt_private *pDevice, u64 qwTSF,
 841                        u16 wBeaconInterval)
 842{
 843        u8 pbyData[8];
 844
 845    qwTSF = CARDqGetNextTBTT(qwTSF, wBeaconInterval);
 846
 847    // Set NextTBTT
 848
 849        pbyData[0] = (u8)qwTSF;
 850        pbyData[1] = (u8)(qwTSF >> 8);
 851        pbyData[2] = (u8)(qwTSF >> 16);
 852        pbyData[3] = (u8)(qwTSF >> 24);
 853        pbyData[4] = (u8)(qwTSF >> 32);
 854        pbyData[5] = (u8)(qwTSF >> 40);
 855        pbyData[6] = (u8)(qwTSF >> 48);
 856        pbyData[7] = (u8)(qwTSF >> 56);
 857
 858    CONTROLnsRequestOut(pDevice,
 859                        MESSAGE_TYPE_SET_TSFTBTT,
 860                        MESSAGE_REQUEST_TBTT,
 861                        0,
 862                        8,
 863                        pbyData
 864                        );
 865
 866        DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO
 867                "Card:Update Next TBTT[%8lx]\n", (unsigned long)qwTSF);
 868
 869    return;
 870}
 871
 872/*
 873 * Description: Turn off Radio power
 874 *
 875 * Parameters:
 876 *  In:
 877 *      pDevice         - The adapter to be turned off
 878 *  Out:
 879 *      none
 880 *
 881 * Return Value: true if success; otherwise false
 882 *
 883 */
 884int CARDbRadioPowerOff(struct vnt_private *pDevice)
 885{
 886        int bResult = true;
 887
 888    //if (pDevice->bRadioOff == true)
 889    //    return true;
 890
 891    pDevice->bRadioOff = true;
 892
 893    switch (pDevice->byRFType) {
 894        case RF_AL2230:
 895        case RF_AL2230S:
 896        case RF_AIROHA7230:
 897        case RF_VT3226:     //RobertYu:20051111
 898        case RF_VT3226D0:
 899        case RF_VT3342A0:   //RobertYu:20060609
 900            MACvRegBitsOff(pDevice, MAC_REG_SOFTPWRCTL, (SOFTPWRCTL_SWPE2 | SOFTPWRCTL_SWPE3));
 901            break;
 902    }
 903
 904    MACvRegBitsOff(pDevice, MAC_REG_HOSTCR, HOSTCR_RXON);
 905
 906    BBvSetDeepSleep(pDevice);
 907
 908    return bResult;
 909}
 910
 911/*
 912 * Description: Turn on Radio power
 913 *
 914 * Parameters:
 915 *  In:
 916 *      pDevice         - The adapter to be turned on
 917 *  Out:
 918 *      none
 919 *
 920 * Return Value: true if success; otherwise false
 921 *
 922 */
 923int CARDbRadioPowerOn(struct vnt_private *pDevice)
 924{
 925        int bResult = true;
 926
 927    if ((pDevice->bHWRadioOff == true) || (pDevice->bRadioControlOff == true)) {
 928        return false;
 929    }
 930
 931    //if (pDevice->bRadioOff == false)
 932    //    return true;
 933
 934    pDevice->bRadioOff = false;
 935
 936    BBvExitDeepSleep(pDevice);
 937
 938    MACvRegBitsOn(pDevice, MAC_REG_HOSTCR, HOSTCR_RXON);
 939
 940    switch (pDevice->byRFType) {
 941        case RF_AL2230:
 942        case RF_AL2230S:
 943        case RF_AIROHA7230:
 944        case RF_VT3226:     //RobertYu:20051111
 945        case RF_VT3226D0:
 946        case RF_VT3342A0:   //RobertYu:20060609
 947            MACvRegBitsOn(pDevice, MAC_REG_SOFTPWRCTL, (SOFTPWRCTL_SWPE2 | SOFTPWRCTL_SWPE3));
 948            break;
 949    }
 950
 951    return bResult;
 952}
 953
 954void CARDvSetBSSMode(struct vnt_private *pDevice)
 955{
 956    // Set BB and packet type at the same time.//{{RobertYu:20050222, AL7230 have two TX PA output, only connet to b/g now
 957    // so in 11a mode need to set the MAC Reg0x4C to 11b/g mode to turn on PA
 958    if( (pDevice->byRFType == RF_AIROHA7230 ) && (pDevice->byBBType == BB_TYPE_11A) )
 959    {
 960        MACvSetBBType(pDevice, BB_TYPE_11G);
 961    }
 962    else
 963    {
 964        MACvSetBBType(pDevice, pDevice->byBBType);
 965    }
 966    pDevice->byPacketType = CARDbyGetPktType(pDevice);
 967
 968    if (pDevice->byBBType == BB_TYPE_11A) {
 969        ControlvWriteByte(pDevice, MESSAGE_REQUEST_BBREG, 0x88, 0x03);
 970    } else if (pDevice->byBBType == BB_TYPE_11B) {
 971        ControlvWriteByte(pDevice, MESSAGE_REQUEST_BBREG, 0x88, 0x02);
 972    } else if (pDevice->byBBType == BB_TYPE_11G) {
 973        ControlvWriteByte(pDevice, MESSAGE_REQUEST_BBREG, 0x88, 0x08);
 974    }
 975
 976    vUpdateIFS(pDevice);
 977    CARDvSetRSPINF(pDevice, (u8)pDevice->byBBType);
 978
 979    if ( pDevice->byBBType == BB_TYPE_11A ) {
 980        //request by Jack 2005-04-26
 981        if (pDevice->byRFType == RF_AIROHA7230) {
 982            pDevice->abyBBVGA[0] = 0x20;
 983            ControlvWriteByte(pDevice, MESSAGE_REQUEST_BBREG, 0xE7, pDevice->abyBBVGA[0]);
 984        }
 985        pDevice->abyBBVGA[2] = 0x10;
 986        pDevice->abyBBVGA[3] = 0x10;
 987    } else {
 988        //request by Jack 2005-04-26
 989        if (pDevice->byRFType == RF_AIROHA7230) {
 990            pDevice->abyBBVGA[0] = 0x1C;
 991            ControlvWriteByte(pDevice, MESSAGE_REQUEST_BBREG, 0xE7, pDevice->abyBBVGA[0]);
 992        }
 993        pDevice->abyBBVGA[2] = 0x0;
 994        pDevice->abyBBVGA[3] = 0x0;
 995    }
 996}
 997