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 - Caculate TSFOffset
32 *      CARDbGetCurrentTSF - Read Current NIC TSF counter
33 *      CARDqGetNextTBTT - Caculate 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 defination type of dwIoBase.
44 *      09-01-2003 Bryan YC Fan:  Add vUpdateIFS().
45 *
46 */
47
48#include "tmacro.h"
49#include "card.h"
50#include "baseband.h"
51#include "mac.h"
52#include "desc.h"
53#include "rf.h"
54#include "vntwifi.h"
55#include "power.h"
56#include "key.h"
57#include "rc4.h"
58#include "country.h"
59#include "channel.h"
60
61/*---------------------  Static Definitions -------------------------*/
62
63//static int          msglevel                =MSG_LEVEL_DEBUG;
64static int          msglevel                =MSG_LEVEL_INFO;
65
66#define C_SIFS_A        16      // micro sec.
67#define C_SIFS_BG       10
68
69#define C_EIFS          80      // micro sec.
70
71
72#define C_SLOT_SHORT    9       // micro sec.
73#define C_SLOT_LONG     20
74
75#define C_CWMIN_A       15      // slot time
76#define C_CWMIN_B       31
77
78#define C_CWMAX         1023    // slot time
79
80#define WAIT_BEACON_TX_DOWN_TMO         3    // Times
81
82                                                              //1M,   2M,   5M,  11M,  18M,  24M,  36M,  54M
83static unsigned char abyDefaultSuppRatesG[] = {WLAN_EID_SUPP_RATES, 8, 0x02, 0x04, 0x0B, 0x16, 0x24, 0x30, 0x48, 0x6C};
84                                                                    //6M,   9M,  12M,  48M
85static unsigned char abyDefaultExtSuppRatesG[] = {WLAN_EID_EXTSUPP_RATES, 4, 0x0C, 0x12, 0x18, 0x60};
86                                                              //6M,   9M,  12M,  18M,  24M,  36M,  48M,  54M
87static unsigned char abyDefaultSuppRatesA[] = {WLAN_EID_SUPP_RATES, 8, 0x0C, 0x12, 0x18, 0x24, 0x30, 0x48, 0x60, 0x6C};
88                                                              //1M,   2M,   5M,  11M,
89static unsigned char abyDefaultSuppRatesB[] = {WLAN_EID_SUPP_RATES, 4, 0x02, 0x04, 0x0B, 0x16};
90
91
92/*---------------------  Static Variables  --------------------------*/
93
94
95const unsigned short cwRXBCNTSFOff[MAX_RATE] =
96{17, 17, 17, 17, 34, 23, 17, 11, 8, 5, 4, 3};
97
98
99/*---------------------  Static Functions  --------------------------*/
100
101static
102void
103s_vCaculateOFDMRParameter(
104    unsigned char byRate,
105    CARD_PHY_TYPE ePHYType,
106    unsigned char *pbyTxRate,
107    unsigned char *pbyRsvTime
108    );
109
110
111/*---------------------  Export Functions  --------------------------*/
112
113/*
114 * Description: Caculate TxRate and RsvTime fields for RSPINF in OFDM mode.
115 *
116 * Parameters:
117 *  In:
118 *      wRate           - Tx Rate
119 *      byPktType       - Tx Packet type
120 *  Out:
121 *      pbyTxRate       - pointer to RSPINF TxRate field
122 *      pbyRsvTime      - pointer to RSPINF RsvTime field
123 *
124 * Return Value: none
125 *
126 */
127static
128void
129s_vCaculateOFDMRParameter (
130    unsigned char byRate,
131    CARD_PHY_TYPE ePHYType,
132    unsigned char *pbyTxRate,
133    unsigned char *pbyRsvTime
134    )
135{
136    switch (byRate) {
137    case RATE_6M :
138        if (ePHYType == PHY_TYPE_11A) {//5GHZ
139            *pbyTxRate = 0x9B;
140            *pbyRsvTime = 44;
141        }
142        else {
143            *pbyTxRate = 0x8B;
144            *pbyRsvTime = 50;
145        }
146        break;
147
148    case RATE_9M :
149        if (ePHYType == PHY_TYPE_11A) {//5GHZ
150            *pbyTxRate = 0x9F;
151            *pbyRsvTime = 36;
152        }
153        else {
154            *pbyTxRate = 0x8F;
155            *pbyRsvTime = 42;
156        }
157        break;
158
159   case RATE_12M :
160        if (ePHYType == PHY_TYPE_11A) {//5GHZ
161            *pbyTxRate = 0x9A;
162            *pbyRsvTime = 32;
163        }
164        else {
165            *pbyTxRate = 0x8A;
166            *pbyRsvTime = 38;
167        }
168        break;
169
170   case RATE_18M :
171        if (ePHYType == PHY_TYPE_11A) {//5GHZ
172            *pbyTxRate = 0x9E;
173            *pbyRsvTime = 28;
174        }
175        else {
176            *pbyTxRate = 0x8E;
177            *pbyRsvTime = 34;
178        }
179        break;
180
181    case RATE_36M :
182        if (ePHYType == PHY_TYPE_11A) {//5GHZ
183            *pbyTxRate = 0x9D;
184            *pbyRsvTime = 24;
185        }
186        else {
187            *pbyTxRate = 0x8D;
188            *pbyRsvTime = 30;
189        }
190        break;
191
192    case RATE_48M :
193        if (ePHYType == PHY_TYPE_11A) {//5GHZ
194            *pbyTxRate = 0x98;
195            *pbyRsvTime = 24;
196        }
197        else {
198            *pbyTxRate = 0x88;
199            *pbyRsvTime = 30;
200        }
201        break;
202
203    case RATE_54M :
204        if (ePHYType == PHY_TYPE_11A) {//5GHZ
205            *pbyTxRate = 0x9C;
206            *pbyRsvTime = 24;
207        }
208        else {
209            *pbyTxRate = 0x8C;
210            *pbyRsvTime = 30;
211        }
212        break;
213
214    case RATE_24M :
215    default :
216        if (ePHYType == PHY_TYPE_11A) {//5GHZ
217            *pbyTxRate = 0x99;
218            *pbyRsvTime = 28;
219        }
220        else {
221            *pbyTxRate = 0x89;
222            *pbyRsvTime = 34;
223        }
224        break;
225    }
226}
227
228
229
230/*
231 * Description: Set RSPINF
232 *
233 * Parameters:
234 *  In:
235 *      pDevice             - The adapter to be set
236 *  Out:
237 *      none
238 *
239 * Return Value: None.
240 *
241 */
242static
243void
244s_vSetRSPINF (PSDevice pDevice, CARD_PHY_TYPE ePHYType, void *pvSupportRateIEs, void *pvExtSupportRateIEs)
245{
246    unsigned char byServ = 0, bySignal = 0; // For CCK
247    unsigned short wLen = 0;
248    unsigned char byTxRate = 0, byRsvTime = 0;    // For OFDM
249
250    //Set to Page1
251    MACvSelectPage1(pDevice->PortOffset);
252
253    //RSPINF_b_1
254    BBvCaculateParameter(pDevice,
255                         14,
256                         VNTWIFIbyGetACKTxRate(RATE_1M, pvSupportRateIEs, pvExtSupportRateIEs),
257                         PK_TYPE_11B,
258                         &wLen,
259                         &byServ,
260                         &bySignal
261    );
262
263    VNSvOutPortD(pDevice->PortOffset + MAC_REG_RSPINF_B_1, MAKEDWORD(wLen,MAKEWORD(bySignal,byServ)));
264    ///RSPINF_b_2
265    BBvCaculateParameter(pDevice,
266                         14,
267                         VNTWIFIbyGetACKTxRate(RATE_2M, pvSupportRateIEs, pvExtSupportRateIEs),
268                         PK_TYPE_11B,
269                         &wLen,
270                         &byServ,
271                         &bySignal
272    );
273
274    VNSvOutPortD(pDevice->PortOffset + MAC_REG_RSPINF_B_2, MAKEDWORD(wLen,MAKEWORD(bySignal,byServ)));
275    //RSPINF_b_5
276    BBvCaculateParameter(pDevice,
277                         14,
278                         VNTWIFIbyGetACKTxRate(RATE_5M, pvSupportRateIEs, pvExtSupportRateIEs),
279                         PK_TYPE_11B,
280                         &wLen,
281                         &byServ,
282                         &bySignal
283    );
284
285    VNSvOutPortD(pDevice->PortOffset + MAC_REG_RSPINF_B_5, MAKEDWORD(wLen,MAKEWORD(bySignal,byServ)));
286    //RSPINF_b_11
287    BBvCaculateParameter(pDevice,
288                         14,
289                         VNTWIFIbyGetACKTxRate(RATE_11M, pvSupportRateIEs, pvExtSupportRateIEs),
290                         PK_TYPE_11B,
291                         &wLen,
292                         &byServ,
293                         &bySignal
294    );
295
296    VNSvOutPortD(pDevice->PortOffset + MAC_REG_RSPINF_B_11, MAKEDWORD(wLen,MAKEWORD(bySignal,byServ)));
297    //RSPINF_a_6
298    s_vCaculateOFDMRParameter(RATE_6M,
299                              ePHYType,
300                              &byTxRate,
301                              &byRsvTime);
302    VNSvOutPortW(pDevice->PortOffset + MAC_REG_RSPINF_A_6, MAKEWORD(byTxRate,byRsvTime));
303    //RSPINF_a_9
304    s_vCaculateOFDMRParameter(RATE_9M,
305                              ePHYType,
306                              &byTxRate,
307                              &byRsvTime);
308    VNSvOutPortW(pDevice->PortOffset + MAC_REG_RSPINF_A_9, MAKEWORD(byTxRate,byRsvTime));
309    //RSPINF_a_12
310    s_vCaculateOFDMRParameter(RATE_12M,
311                              ePHYType,
312                              &byTxRate,
313                              &byRsvTime);
314    VNSvOutPortW(pDevice->PortOffset + MAC_REG_RSPINF_A_12, MAKEWORD(byTxRate,byRsvTime));
315    //RSPINF_a_18
316    s_vCaculateOFDMRParameter(RATE_18M,
317                              ePHYType,
318                              &byTxRate,
319                              &byRsvTime);
320    VNSvOutPortW(pDevice->PortOffset + MAC_REG_RSPINF_A_18, MAKEWORD(byTxRate,byRsvTime));
321    //RSPINF_a_24
322    s_vCaculateOFDMRParameter(RATE_24M,
323                              ePHYType,
324                              &byTxRate,
325                              &byRsvTime);
326    VNSvOutPortW(pDevice->PortOffset + MAC_REG_RSPINF_A_24, MAKEWORD(byTxRate,byRsvTime));
327    //RSPINF_a_36
328    s_vCaculateOFDMRParameter(
329                              VNTWIFIbyGetACKTxRate(RATE_36M, pvSupportRateIEs, pvExtSupportRateIEs),
330                              ePHYType,
331                              &byTxRate,
332                              &byRsvTime);
333    VNSvOutPortW(pDevice->PortOffset + MAC_REG_RSPINF_A_36, MAKEWORD(byTxRate,byRsvTime));
334    //RSPINF_a_48
335    s_vCaculateOFDMRParameter(
336                              VNTWIFIbyGetACKTxRate(RATE_48M, pvSupportRateIEs, pvExtSupportRateIEs),
337                              ePHYType,
338                              &byTxRate,
339                              &byRsvTime);
340    VNSvOutPortW(pDevice->PortOffset + MAC_REG_RSPINF_A_48, MAKEWORD(byTxRate,byRsvTime));
341    //RSPINF_a_54
342    s_vCaculateOFDMRParameter(
343                              VNTWIFIbyGetACKTxRate(RATE_54M, pvSupportRateIEs, pvExtSupportRateIEs),
344                              ePHYType,
345                              &byTxRate,
346                              &byRsvTime);
347    VNSvOutPortW(pDevice->PortOffset + MAC_REG_RSPINF_A_54, MAKEWORD(byTxRate,byRsvTime));
348    //RSPINF_a_72
349    VNSvOutPortW(pDevice->PortOffset + MAC_REG_RSPINF_A_72, MAKEWORD(byTxRate,byRsvTime));
350    //Set to Page0
351    MACvSelectPage0(pDevice->PortOffset);
352}
353
354/*---------------------  Export Functions  --------------------------*/
355
356/*
357 * Description: Card Send packet function
358 *
359 * Parameters:
360 *  In:
361 *      pDeviceHandler      - The adapter to be set
362 *      pPacket             - Packet buffer pointer
363 *      ePktType            - Packet type
364 *      uLength             - Packet length
365 *  Out:
366 *      none
367 *
368 * Return Value: true if succeeded; false if failed.
369 *
370 */
371/*
372bool CARDbSendPacket (void *pDeviceHandler, void *pPacket, CARD_PKT_TYPE ePktType, unsigned int uLength)
373{
374    PSDevice    pDevice = (PSDevice) pDeviceHandler;
375    if (ePktType == PKT_TYPE_802_11_MNG) {
376        return TXbTD0Send(pDevice, pPacket, uLength);
377    } else if (ePktType == PKT_TYPE_802_11_BCN) {
378        return TXbBeaconSend(pDevice, pPacket, uLength);
379    } if (ePktType == PKT_TYPE_802_11_DATA) {
380        return TXbTD1Send(pDevice, pPacket, uLength);
381    }
382
383    return (true);
384}
385*/
386
387
388/*
389 * Description: Get Card short preamble option value
390 *
391 * Parameters:
392 *  In:
393 *      pDevice             - The adapter to be set
394 *  Out:
395 *      none
396 *
397 * Return Value: true if short preamble; otherwise false
398 *
399 */
400bool CARDbIsShortPreamble (void *pDeviceHandler)
401{
402    PSDevice    pDevice = (PSDevice) pDeviceHandler;
403    if (pDevice->byPreambleType == 0) {
404        return(false);
405    }
406    return(true);
407}
408
409/*
410 * Description: Get Card short slot time option value
411 *
412 * Parameters:
413 *  In:
414 *      pDevice             - The adapter to be set
415 *  Out:
416 *      none
417 *
418 * Return Value: true if short slot time; otherwise false
419 *
420 */
421bool CARDbIsShorSlotTime (void *pDeviceHandler)
422{
423    PSDevice    pDevice = (PSDevice) pDeviceHandler;
424    return(pDevice->bShortSlotTime);
425}
426
427
428/*
429 * Description: Update IFS
430 *
431 * Parameters:
432 *  In:
433 *      pDevice             - The adapter to be set
434 *  Out:
435 *      none
436 *
437 * Return Value: None.
438 *
439 */
440bool CARDbSetPhyParameter (void *pDeviceHandler, CARD_PHY_TYPE ePHYType, unsigned short wCapInfo, unsigned char byERPField, void *pvSupportRateIEs, void *pvExtSupportRateIEs)
441{
442    PSDevice    pDevice = (PSDevice) pDeviceHandler;
443    unsigned char byCWMaxMin = 0;
444    unsigned char bySlot = 0;
445    unsigned char bySIFS = 0;
446    unsigned char byDIFS = 0;
447    unsigned char byData;
448//    PWLAN_IE_SUPP_RATES pRates = NULL;
449    PWLAN_IE_SUPP_RATES pSupportRates = (PWLAN_IE_SUPP_RATES) pvSupportRateIEs;
450    PWLAN_IE_SUPP_RATES pExtSupportRates = (PWLAN_IE_SUPP_RATES) pvExtSupportRateIEs;
451
452
453    //Set SIFS, DIFS, EIFS, SlotTime, CwMin
454    if (ePHYType == PHY_TYPE_11A) {
455        if (pSupportRates == NULL) {
456            pSupportRates = (PWLAN_IE_SUPP_RATES) abyDefaultSuppRatesA;
457        }
458        if (pDevice->byRFType == RF_AIROHA7230) {
459            // AL7230 use single PAPE and connect to PAPE_2.4G
460            MACvSetBBType(pDevice->PortOffset, BB_TYPE_11G);
461            pDevice->abyBBVGA[0] = 0x20;
462            pDevice->abyBBVGA[2] = 0x10;
463            pDevice->abyBBVGA[3] = 0x10;
464            BBbReadEmbeded(pDevice->PortOffset, 0xE7, &byData);
465            if (byData == 0x1C) {
466                BBbWriteEmbeded(pDevice->PortOffset, 0xE7, pDevice->abyBBVGA[0]);
467            }
468        } else if (pDevice->byRFType == RF_UW2452) {
469            MACvSetBBType(pDevice->PortOffset, BB_TYPE_11A);
470            pDevice->abyBBVGA[0] = 0x18;
471            BBbReadEmbeded(pDevice->PortOffset, 0xE7, &byData);
472            if (byData == 0x14) {
473                BBbWriteEmbeded(pDevice->PortOffset, 0xE7, pDevice->abyBBVGA[0]);
474                BBbWriteEmbeded(pDevice->PortOffset, 0xE1, 0x57);
475            }
476        } else {
477            MACvSetBBType(pDevice->PortOffset, BB_TYPE_11A);
478        }
479        BBbWriteEmbeded(pDevice->PortOffset, 0x88, 0x03);
480        bySlot = C_SLOT_SHORT;
481        bySIFS = C_SIFS_A;
482        byDIFS = C_SIFS_A + 2*C_SLOT_SHORT;
483        byCWMaxMin = 0xA4;
484    } else if (ePHYType == PHY_TYPE_11B) {
485        if (pSupportRates == NULL) {
486            pSupportRates = (PWLAN_IE_SUPP_RATES) abyDefaultSuppRatesB;
487        }
488        MACvSetBBType(pDevice->PortOffset, BB_TYPE_11B);
489        if (pDevice->byRFType == RF_AIROHA7230) {
490            pDevice->abyBBVGA[0] = 0x1C;
491            pDevice->abyBBVGA[2] = 0x00;
492            pDevice->abyBBVGA[3] = 0x00;
493            BBbReadEmbeded(pDevice->PortOffset, 0xE7, &byData);
494            if (byData == 0x20) {
495                BBbWriteEmbeded(pDevice->PortOffset, 0xE7, pDevice->abyBBVGA[0]);
496            }
497        } else if (pDevice->byRFType == RF_UW2452) {
498            pDevice->abyBBVGA[0] = 0x14;
499            BBbReadEmbeded(pDevice->PortOffset, 0xE7, &byData);
500            if (byData == 0x18) {
501                BBbWriteEmbeded(pDevice->PortOffset, 0xE7, pDevice->abyBBVGA[0]);
502                BBbWriteEmbeded(pDevice->PortOffset, 0xE1, 0xD3);
503            }
504        }
505        BBbWriteEmbeded(pDevice->PortOffset, 0x88, 0x02);
506        bySlot = C_SLOT_LONG;
507        bySIFS = C_SIFS_BG;
508        byDIFS = C_SIFS_BG + 2*C_SLOT_LONG;
509        byCWMaxMin = 0xA5;
510    } else {// PK_TYPE_11GA & PK_TYPE_11GB
511        if (pSupportRates == NULL) {
512            pSupportRates = (PWLAN_IE_SUPP_RATES) abyDefaultSuppRatesG;
513            pExtSupportRates = (PWLAN_IE_SUPP_RATES) abyDefaultExtSuppRatesG;
514        }
515        MACvSetBBType(pDevice->PortOffset, BB_TYPE_11G);
516        if (pDevice->byRFType == RF_AIROHA7230) {
517            pDevice->abyBBVGA[0] = 0x1C;
518            pDevice->abyBBVGA[2] = 0x00;
519            pDevice->abyBBVGA[3] = 0x00;
520            BBbReadEmbeded(pDevice->PortOffset, 0xE7, &byData);
521            if (byData == 0x20) {
522                BBbWriteEmbeded(pDevice->PortOffset, 0xE7, pDevice->abyBBVGA[0]);
523            }
524        } else if (pDevice->byRFType == RF_UW2452) {
525            pDevice->abyBBVGA[0] = 0x14;
526            BBbReadEmbeded(pDevice->PortOffset, 0xE7, &byData);
527            if (byData == 0x18) {
528                BBbWriteEmbeded(pDevice->PortOffset, 0xE7, pDevice->abyBBVGA[0]);
529                BBbWriteEmbeded(pDevice->PortOffset, 0xE1, 0xD3);
530            }
531        }
532        BBbWriteEmbeded(pDevice->PortOffset, 0x88, 0x08);
533        bySIFS = C_SIFS_BG;
534        if(VNTWIFIbIsShortSlotTime(wCapInfo)) {
535            bySlot = C_SLOT_SHORT;
536            byDIFS = C_SIFS_BG + 2*C_SLOT_SHORT;
537        } else {
538            bySlot = C_SLOT_LONG;
539            byDIFS = C_SIFS_BG + 2*C_SLOT_LONG;
540	    }
541        if (VNTWIFIbyGetMaxSupportRate(pSupportRates, pExtSupportRates) > RATE_11M) {
542            byCWMaxMin = 0xA4;
543        } else {
544            byCWMaxMin = 0xA5;
545        }
546        if (pDevice->bProtectMode != VNTWIFIbIsProtectMode(byERPField)) {
547            pDevice->bProtectMode = VNTWIFIbIsProtectMode(byERPField);
548            if (pDevice->bProtectMode) {
549                MACvEnableProtectMD(pDevice->PortOffset);
550            } else {
551                MACvDisableProtectMD(pDevice->PortOffset);
552            }
553        }
554        if (pDevice->bBarkerPreambleMd != VNTWIFIbIsBarkerMode(byERPField)) {
555            pDevice->bBarkerPreambleMd = VNTWIFIbIsBarkerMode(byERPField);
556            if (pDevice->bBarkerPreambleMd) {
557                MACvEnableBarkerPreambleMd(pDevice->PortOffset);
558            } else {
559                MACvDisableBarkerPreambleMd(pDevice->PortOffset);
560            }
561        }
562    }
563
564    if (pDevice->byRFType == RF_RFMD2959) {
565        // bcs TX_PE will reserve 3 us
566        // hardware's processing time here is 2 us.
567        bySIFS -= 3;
568        byDIFS -= 3;
569    //{{ RobertYu: 20041202
570    //// TX_PE will reserve 3 us for MAX2829 A mode only, it is for better TX throughput
571    //// MAC will need 2 us to process, so the SIFS, DIFS can be shorter by 2 us.
572    }
573
574    if (pDevice->bySIFS != bySIFS) {
575        pDevice->bySIFS = bySIFS;
576        VNSvOutPortB(pDevice->PortOffset + MAC_REG_SIFS, pDevice->bySIFS);
577    }
578    if (pDevice->byDIFS != byDIFS) {
579        pDevice->byDIFS = byDIFS;
580        VNSvOutPortB(pDevice->PortOffset + MAC_REG_DIFS, pDevice->byDIFS);
581    }
582    if (pDevice->byEIFS != C_EIFS) {
583        pDevice->byEIFS = C_EIFS;
584        VNSvOutPortB(pDevice->PortOffset + MAC_REG_EIFS, pDevice->byEIFS);
585    }
586    if (pDevice->bySlot != bySlot) {
587        pDevice->bySlot = bySlot;
588        VNSvOutPortB(pDevice->PortOffset + MAC_REG_SLOT, pDevice->bySlot);
589        if (pDevice->bySlot == C_SLOT_SHORT) {
590            pDevice->bShortSlotTime = true;
591        } else {
592            pDevice->bShortSlotTime = false;
593        }
594        BBvSetShortSlotTime(pDevice);
595    }
596    if (pDevice->byCWMaxMin != byCWMaxMin) {
597        pDevice->byCWMaxMin = byCWMaxMin;
598        VNSvOutPortB(pDevice->PortOffset + MAC_REG_CWMAXMIN0, pDevice->byCWMaxMin);
599    }
600    if (VNTWIFIbIsShortPreamble(wCapInfo)) {
601        pDevice->byPreambleType = pDevice->byShortPreamble;
602    } else {
603        pDevice->byPreambleType = 0;
604    }
605    s_vSetRSPINF(pDevice, ePHYType, pSupportRates, pExtSupportRates);
606    pDevice->eCurrentPHYType = ePHYType;
607    // set for NDIS OID_802_11SUPPORTED_RATES
608    return (true);
609}
610
611/*
612 * Description: Sync. TSF counter to BSS
613 *              Get TSF offset and write to HW
614 *
615 * Parameters:
616 *  In:
617 *      pDevice         - The adapter to be sync.
618 *      byRxRate        - data rate of receive beacon
619 *      qwBSSTimestamp  - Rx BCN's TSF
620 *      qwLocalTSF      - Local TSF
621 *  Out:
622 *      none
623 *
624 * Return Value: none
625 *
626 */
627bool CARDbUpdateTSF (void *pDeviceHandler, unsigned char byRxRate, QWORD qwBSSTimestamp, QWORD qwLocalTSF)
628{
629    PSDevice    pDevice = (PSDevice) pDeviceHandler;
630    QWORD       qwTSFOffset;
631
632    HIDWORD(qwTSFOffset) = 0;
633    LODWORD(qwTSFOffset) = 0;
634
635    if ((HIDWORD(qwBSSTimestamp) != HIDWORD(qwLocalTSF)) ||
636        (LODWORD(qwBSSTimestamp) != LODWORD(qwLocalTSF))) {
637        qwTSFOffset = CARDqGetTSFOffset(byRxRate, qwBSSTimestamp, qwLocalTSF);
638        // adjust TSF
639        // HW's TSF add TSF Offset reg
640        VNSvOutPortD(pDevice->PortOffset + MAC_REG_TSFOFST, LODWORD(qwTSFOffset));
641        VNSvOutPortD(pDevice->PortOffset + MAC_REG_TSFOFST + 4, HIDWORD(qwTSFOffset));
642        MACvRegBitsOn(pDevice->PortOffset, MAC_REG_TFTCTL, TFTCTL_TSFSYNCEN);
643    }
644    return(true);
645}
646
647
648/*
649 * Description: Set NIC TSF counter for first Beacon time
650 *              Get NEXTTBTT from adjusted TSF and Beacon Interval
651 *
652 * Parameters:
653 *  In:
654 *      pDevice         - The adapter to be set.
655 *      wBeaconInterval - Beacon Interval
656 *  Out:
657 *      none
658 *
659 * Return Value: true if succeed; otherwise false
660 *
661 */
662bool CARDbSetBeaconPeriod (void *pDeviceHandler, unsigned short wBeaconInterval)
663{
664    PSDevice    pDevice = (PSDevice) pDeviceHandler;
665    unsigned int uBeaconInterval = 0;
666    unsigned int uLowNextTBTT = 0;
667    unsigned int uHighRemain = 0;
668    unsigned int uLowRemain = 0;
669    QWORD       qwNextTBTT;
670
671    HIDWORD(qwNextTBTT) = 0;
672    LODWORD(qwNextTBTT) = 0;
673    CARDbGetCurrentTSF(pDevice->PortOffset, &qwNextTBTT); //Get Local TSF counter
674    uBeaconInterval = wBeaconInterval * 1024;
675    // Next TBTT = ((local_current_TSF / beacon_interval) + 1 ) * beacon_interval
676    uLowNextTBTT = (LODWORD(qwNextTBTT) >> 10) << 10;
677    uLowRemain = (uLowNextTBTT) % uBeaconInterval;
678    // high dword (mod) bcn
679    uHighRemain = (((0xffffffff % uBeaconInterval) + 1) * HIDWORD(qwNextTBTT))
680                  % uBeaconInterval;
681    uLowRemain = (uHighRemain + uLowRemain) % uBeaconInterval;
682    uLowRemain = uBeaconInterval - uLowRemain;
683
684    // check if carry when add one beacon interval
685    if ((~uLowNextTBTT) < uLowRemain) {
686        HIDWORD(qwNextTBTT) ++ ;
687    }
688    LODWORD(qwNextTBTT) = uLowNextTBTT + uLowRemain;
689
690    // set HW beacon interval
691    VNSvOutPortW(pDevice->PortOffset + MAC_REG_BI, wBeaconInterval);
692    pDevice->wBeaconInterval = wBeaconInterval;
693    // Set NextTBTT
694    VNSvOutPortD(pDevice->PortOffset + MAC_REG_NEXTTBTT, LODWORD(qwNextTBTT));
695    VNSvOutPortD(pDevice->PortOffset + MAC_REG_NEXTTBTT + 4, HIDWORD(qwNextTBTT));
696    MACvRegBitsOn(pDevice->PortOffset, MAC_REG_TFTCTL, TFTCTL_TBTTSYNCEN);
697
698    return(true);
699}
700
701
702
703/*
704 * Description: Card Stop Hardware Tx
705 *
706 * Parameters:
707 *  In:
708 *      pDeviceHandler      - The adapter to be set
709 *      ePktType            - Packet type to stop
710 *  Out:
711 *      none
712 *
713 * Return Value: true if all data packet complete; otherwise false.
714 *
715 */
716bool CARDbStopTxPacket (void *pDeviceHandler, CARD_PKT_TYPE ePktType)
717{
718    PSDevice    pDevice = (PSDevice) pDeviceHandler;
719
720
721    if (ePktType == PKT_TYPE_802_11_ALL) {
722        pDevice->bStopBeacon = true;
723        pDevice->bStopTx0Pkt = true;
724        pDevice->bStopDataPkt = true;
725    } else if (ePktType == PKT_TYPE_802_11_BCN) {
726        pDevice->bStopBeacon = true;
727    } else if (ePktType == PKT_TYPE_802_11_MNG) {
728        pDevice->bStopTx0Pkt = true;
729    } else if (ePktType == PKT_TYPE_802_11_DATA) {
730        pDevice->bStopDataPkt = true;
731    }
732
733    if (pDevice->bStopBeacon == true) {
734        if (pDevice->bIsBeaconBufReadySet == true) {
735            if (pDevice->cbBeaconBufReadySetCnt < WAIT_BEACON_TX_DOWN_TMO) {
736                pDevice->cbBeaconBufReadySetCnt ++;
737                return(false);
738            }
739        }
740        pDevice->bIsBeaconBufReadySet = false;
741        pDevice->cbBeaconBufReadySetCnt = 0;
742        MACvRegBitsOff(pDevice->PortOffset, MAC_REG_TCR, TCR_AUTOBCNTX);
743    }
744    // wait all TD0 complete
745    if (pDevice->bStopTx0Pkt == true) {
746         if (pDevice->iTDUsed[TYPE_TXDMA0] != 0){
747            return(false);
748        }
749    }
750    // wait all Data TD complete
751    if (pDevice->bStopDataPkt == true) {
752        if (pDevice->iTDUsed[TYPE_AC0DMA] != 0){
753            return(false);
754        }
755    }
756
757    return(true);
758}
759
760
761/*
762 * Description: Card Start Hardware Tx
763 *
764 * Parameters:
765 *  In:
766 *      pDeviceHandler      - The adapter to be set
767 *      ePktType            - Packet type to start
768 *  Out:
769 *      none
770 *
771 * Return Value: true if success; false if failed.
772 *
773 */
774bool CARDbStartTxPacket (void *pDeviceHandler, CARD_PKT_TYPE ePktType)
775{
776    PSDevice    pDevice = (PSDevice) pDeviceHandler;
777
778
779    if (ePktType == PKT_TYPE_802_11_ALL) {
780        pDevice->bStopBeacon = false;
781        pDevice->bStopTx0Pkt = false;
782        pDevice->bStopDataPkt = false;
783    } else if (ePktType == PKT_TYPE_802_11_BCN) {
784        pDevice->bStopBeacon = false;
785    } else if (ePktType == PKT_TYPE_802_11_MNG) {
786        pDevice->bStopTx0Pkt = false;
787    } else if (ePktType == PKT_TYPE_802_11_DATA) {
788        pDevice->bStopDataPkt = false;
789    }
790
791    if ((pDevice->bStopBeacon == false) &&
792        (pDevice->bBeaconBufReady == true) &&
793        (pDevice->eOPMode == OP_MODE_ADHOC)) {
794        MACvRegBitsOn(pDevice->PortOffset, MAC_REG_TCR, TCR_AUTOBCNTX);
795    }
796
797    return(true);
798}
799
800
801
802/*
803 * Description: Card Set BSSID value
804 *
805 * Parameters:
806 *  In:
807 *      pDeviceHandler      - The adapter to be set
808 *      pbyBSSID            - pointer to BSSID field
809 *      bAdhoc              - flag to indicate IBSS
810 *  Out:
811 *      none
812 *
813 * Return Value: true if success; false if failed.
814 *
815 */
816bool CARDbSetBSSID(void *pDeviceHandler, unsigned char *pbyBSSID, CARD_OP_MODE eOPMode)
817{
818    PSDevice    pDevice = (PSDevice) pDeviceHandler;
819
820    MACvWriteBSSIDAddress(pDevice->PortOffset, pbyBSSID);
821    memcpy(pDevice->abyBSSID, pbyBSSID, WLAN_BSSID_LEN);
822    if (eOPMode == OP_MODE_ADHOC) {
823        MACvRegBitsOn(pDevice->PortOffset, MAC_REG_HOSTCR, HOSTCR_ADHOC);
824    } else {
825        MACvRegBitsOff(pDevice->PortOffset, MAC_REG_HOSTCR, HOSTCR_ADHOC);
826    }
827    if (eOPMode == OP_MODE_AP) {
828        MACvRegBitsOn(pDevice->PortOffset, MAC_REG_HOSTCR, HOSTCR_AP);
829    } else {
830        MACvRegBitsOff(pDevice->PortOffset, MAC_REG_HOSTCR, HOSTCR_AP);
831    }
832    if (eOPMode == OP_MODE_UNKNOWN) {
833        MACvRegBitsOff(pDevice->PortOffset, MAC_REG_RCR, RCR_BSSID);
834        pDevice->bBSSIDFilter = false;
835        pDevice->byRxMode &= ~RCR_BSSID;
836        DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "wcmd: rx_mode = %x\n", pDevice->byRxMode );
837    } else {
838        if (is_zero_ether_addr(pDevice->abyBSSID) == false) {
839            MACvRegBitsOn(pDevice->PortOffset, MAC_REG_RCR, RCR_BSSID);
840            pDevice->bBSSIDFilter = true;
841            pDevice->byRxMode |= RCR_BSSID;
842	    }
843	    DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "wmgr: rx_mode = %x\n", pDevice->byRxMode );
844    }
845    // Adopt BSS state in Adapter Device Object
846    pDevice->eOPMode = eOPMode;
847    return(true);
848}
849
850
851/*
852 * Description: Card indicate status
853 *
854 * Parameters:
855 *  In:
856 *      pDeviceHandler      - The adapter to be set
857 *      eStatus             - Status
858 *  Out:
859 *      none
860 *
861 * Return Value: true if success; false if failed.
862 *
863 */
864
865
866
867
868/*
869 * Description: Save Assoc info. contain in assoc. response frame
870 *
871 * Parameters:
872 *  In:
873 *      pDevice             - The adapter to be set
874 *      wCapabilityInfo     - Capability information
875 *      wStatus             - Status code
876 *      wAID                - Assoc. ID
877 *      uLen                - Length of IEs
878 *      pbyIEs              - pointer to IEs
879 *  Out:
880 *      none
881 *
882 * Return Value: true if succeed; otherwise false
883 *
884 */
885bool CARDbSetTxDataRate(
886    void *pDeviceHandler,
887    unsigned short wDataRate
888    )
889{
890    PSDevice    pDevice = (PSDevice) pDeviceHandler;
891
892    pDevice->wCurrentRate = wDataRate;
893    return(true);
894}
895
896/*+
897 *
898 * Routine Description:
899 *      Consider to power down when no more packets to tx or rx.
900 *
901 * Parameters:
902 *  In:
903 *      pDevice             - The adapter to be set
904 *  Out:
905 *      none
906 *
907 * Return Value: true if power down success; otherwise false
908 *
909-*/
910bool
911CARDbPowerDown(
912    void *pDeviceHandler
913    )
914{
915    PSDevice        pDevice = (PSDevice)pDeviceHandler;
916    unsigned int uIdx;
917
918    // check if already in Doze mode
919    if (MACbIsRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_PS))
920        return true;
921
922    // Froce PSEN on
923    MACvRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_PSEN);
924
925    // check if all TD are empty,
926
927    for (uIdx = 0; uIdx < TYPE_MAXTD; uIdx ++) {
928        if (pDevice->iTDUsed[uIdx] != 0)
929            return false;
930    }
931
932    MACvRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_GO2DOZE);
933    DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Go to Doze ZZZZZZZZZZZZZZZ\n");
934    return true;
935}
936
937/*
938 * Description: Turn off Radio power
939 *
940 * Parameters:
941 *  In:
942 *      pDevice         - The adapter to be turned off
943 *  Out:
944 *      none
945 *
946 * Return Value: true if success; otherwise false
947 *
948 */
949bool CARDbRadioPowerOff (void *pDeviceHandler)
950{
951    PSDevice    pDevice = (PSDevice) pDeviceHandler;
952    bool bResult = true;
953
954    if (pDevice->bRadioOff == true)
955        return true;
956
957
958    switch (pDevice->byRFType) {
959
960        case RF_RFMD2959:
961            MACvWordRegBitsOff(pDevice->PortOffset, MAC_REG_SOFTPWRCTL, SOFTPWRCTL_TXPEINV);
962            MACvWordRegBitsOn(pDevice->PortOffset, MAC_REG_SOFTPWRCTL, SOFTPWRCTL_SWPE1);
963            break;
964
965        case RF_AIROHA:
966        case RF_AL2230S:
967        case RF_AIROHA7230: //RobertYu:20050104
968            MACvWordRegBitsOff(pDevice->PortOffset, MAC_REG_SOFTPWRCTL, SOFTPWRCTL_SWPE2);
969            MACvWordRegBitsOff(pDevice->PortOffset, MAC_REG_SOFTPWRCTL, SOFTPWRCTL_SWPE3);
970            break;
971
972    }
973
974    MACvRegBitsOff(pDevice->PortOffset, MAC_REG_HOSTCR, HOSTCR_RXON);
975
976    BBvSetDeepSleep(pDevice->PortOffset, pDevice->byLocalID);
977
978    pDevice->bRadioOff = true;
979     //2007-0409-03,<Add> by chester
980printk("chester power off\n");
981MACvRegBitsOn(pDevice->PortOffset, MAC_REG_GPIOCTL0, LED_ACTSET);  //LED issue
982    return bResult;
983}
984
985
986/*
987 * Description: Turn on Radio power
988 *
989 * Parameters:
990 *  In:
991 *      pDevice         - The adapter to be turned on
992 *  Out:
993 *      none
994 *
995 * Return Value: true if success; otherwise false
996 *
997 */
998bool CARDbRadioPowerOn (void *pDeviceHandler)
999{
1000    PSDevice    pDevice = (PSDevice) pDeviceHandler;
1001    bool bResult = true;
1002printk("chester power on\n");
1003    if (pDevice->bRadioControlOff == true){
1004if (pDevice->bHWRadioOff == true) printk("chester bHWRadioOff\n");
1005if (pDevice->bRadioControlOff == true) printk("chester bRadioControlOff\n");
1006        return false;}
1007
1008    if (pDevice->bRadioOff == false)
1009       {
1010printk("chester pbRadioOff\n");
1011return true;}
1012
1013    BBvExitDeepSleep(pDevice->PortOffset, pDevice->byLocalID);
1014
1015    MACvRegBitsOn(pDevice->PortOffset, MAC_REG_HOSTCR, HOSTCR_RXON);
1016
1017    switch (pDevice->byRFType) {
1018
1019        case RF_RFMD2959:
1020            MACvWordRegBitsOn(pDevice->PortOffset, MAC_REG_SOFTPWRCTL, SOFTPWRCTL_TXPEINV);
1021            MACvWordRegBitsOff(pDevice->PortOffset, MAC_REG_SOFTPWRCTL, SOFTPWRCTL_SWPE1);
1022            break;
1023
1024        case RF_AIROHA:
1025        case RF_AL2230S:
1026        case RF_AIROHA7230: //RobertYu:20050104
1027            MACvWordRegBitsOn(pDevice->PortOffset, MAC_REG_SOFTPWRCTL, (SOFTPWRCTL_SWPE2 |
1028                                                                        SOFTPWRCTL_SWPE3));
1029            break;
1030
1031    }
1032
1033    pDevice->bRadioOff = false;
1034//  2007-0409-03,<Add> by chester
1035printk("chester power on\n");
1036MACvRegBitsOff(pDevice->PortOffset, MAC_REG_GPIOCTL0, LED_ACTSET); //LED issue
1037    return bResult;
1038}
1039
1040
1041
1042bool CARDbRemoveKey (void *pDeviceHandler, unsigned char *pbyBSSID)
1043{
1044    PSDevice    pDevice = (PSDevice) pDeviceHandler;
1045
1046    KeybRemoveAllKey(&(pDevice->sKey), pbyBSSID, pDevice->PortOffset);
1047    return (true);
1048}
1049
1050
1051/*
1052 *
1053 * Description:
1054 *    Add BSSID in PMKID Candidate list.
1055 *
1056 * Parameters:
1057 *  In:
1058 *      hDeviceContext - device structure point
1059 *      pbyBSSID - BSSID address for adding
1060 *      wRSNCap - BSS's RSN capability
1061 *  Out:
1062 *      none
1063 *
1064 * Return Value: none.
1065 *
1066-*/
1067bool
1068CARDbAdd_PMKID_Candidate (
1069    void *pDeviceHandler,
1070    unsigned char *pbyBSSID,
1071    bool bRSNCapExist,
1072    unsigned short wRSNCap
1073    )
1074{
1075    PSDevice            pDevice = (PSDevice) pDeviceHandler;
1076    PPMKID_CANDIDATE    pCandidateList;
1077    unsigned int ii = 0;
1078
1079    DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"bAdd_PMKID_Candidate START: (%d)\n", (int)pDevice->gsPMKIDCandidate.NumCandidates);
1080
1081    if (pDevice->gsPMKIDCandidate.NumCandidates >= MAX_PMKIDLIST) {
1082        DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"vFlush_PMKID_Candidate: 3\n");
1083        memset(&pDevice->gsPMKIDCandidate, 0, sizeof(SPMKIDCandidateEvent));
1084    }
1085
1086    for (ii = 0; ii < 6; ii++) {
1087        DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"%02X ", *(pbyBSSID + ii));
1088    }
1089    DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"\n");
1090
1091
1092    // Update Old Candidate
1093    for (ii = 0; ii < pDevice->gsPMKIDCandidate.NumCandidates; ii++) {
1094        pCandidateList = &pDevice->gsPMKIDCandidate.CandidateList[ii];
1095        if ( !memcmp(pCandidateList->BSSID, pbyBSSID, ETH_ALEN)) {
1096            if ((bRSNCapExist == true) && (wRSNCap & BIT0)) {
1097                pCandidateList->Flags |= NDIS_802_11_PMKID_CANDIDATE_PREAUTH_ENABLED;
1098            } else {
1099                pCandidateList->Flags &= ~(NDIS_802_11_PMKID_CANDIDATE_PREAUTH_ENABLED);
1100            }
1101            return true;
1102        }
1103    }
1104
1105    // New Candidate
1106    pCandidateList = &pDevice->gsPMKIDCandidate.CandidateList[pDevice->gsPMKIDCandidate.NumCandidates];
1107    if ((bRSNCapExist == true) && (wRSNCap & BIT0)) {
1108        pCandidateList->Flags |= NDIS_802_11_PMKID_CANDIDATE_PREAUTH_ENABLED;
1109    } else {
1110        pCandidateList->Flags &= ~(NDIS_802_11_PMKID_CANDIDATE_PREAUTH_ENABLED);
1111    }
1112    memcpy(pCandidateList->BSSID, pbyBSSID, ETH_ALEN);
1113    pDevice->gsPMKIDCandidate.NumCandidates++;
1114    DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"NumCandidates:%d\n", (int)pDevice->gsPMKIDCandidate.NumCandidates);
1115    return true;
1116}
1117
1118void *
1119CARDpGetCurrentAddress (
1120    void *pDeviceHandler
1121    )
1122{
1123    PSDevice            pDevice = (PSDevice) pDeviceHandler;
1124
1125    return (pDevice->abyCurrentNetAddr);
1126}
1127
1128/*
1129 *
1130 * Description:
1131 *    Start Spectrum Measure defined in 802.11h
1132 *
1133 * Parameters:
1134 *  In:
1135 *      hDeviceContext - device structure point
1136 *  Out:
1137 *      none
1138 *
1139 * Return Value: none.
1140 *
1141-*/
1142bool
1143CARDbStartMeasure (
1144    void *pDeviceHandler,
1145    void *pvMeasureEIDs,
1146    unsigned int uNumOfMeasureEIDs
1147    )
1148{
1149    PSDevice                pDevice = (PSDevice) pDeviceHandler;
1150    PWLAN_IE_MEASURE_REQ    pEID = (PWLAN_IE_MEASURE_REQ) pvMeasureEIDs;
1151    QWORD                   qwCurrTSF;
1152    QWORD                   qwStartTSF;
1153    bool bExpired = true;
1154    unsigned short wDuration = 0;
1155
1156    if ((pEID == NULL) ||
1157        (uNumOfMeasureEIDs == 0)) {
1158        return (true);
1159    }
1160    CARDbGetCurrentTSF(pDevice->PortOffset, &qwCurrTSF);
1161    if (pDevice->bMeasureInProgress == true) {
1162        pDevice->bMeasureInProgress = false;
1163        VNSvOutPortB(pDevice->PortOffset + MAC_REG_RCR, pDevice->byOrgRCR);
1164        MACvSelectPage1(pDevice->PortOffset);
1165        VNSvOutPortD(pDevice->PortOffset + MAC_REG_MAR0, pDevice->dwOrgMAR0);
1166        VNSvOutPortD(pDevice->PortOffset + MAC_REG_MAR4, pDevice->dwOrgMAR4);
1167        // clear measure control
1168        MACvRegBitsOff(pDevice->PortOffset, MAC_REG_MSRCTL, MSRCTL_EN);
1169        MACvSelectPage0(pDevice->PortOffset);
1170        set_channel(pDevice, pDevice->byOrgChannel);
1171        MACvSelectPage1(pDevice->PortOffset);
1172        MACvRegBitsOn(pDevice->PortOffset, MAC_REG_MSRCTL+1, MSRCTL1_TXPAUSE);
1173        MACvSelectPage0(pDevice->PortOffset);
1174    }
1175    pDevice->uNumOfMeasureEIDs = uNumOfMeasureEIDs;
1176
1177    do {
1178        pDevice->pCurrMeasureEID = pEID;
1179        pEID++;
1180        pDevice->uNumOfMeasureEIDs--;
1181
1182        if (pDevice->byLocalID > REV_ID_VT3253_B1) {
1183            HIDWORD(qwStartTSF) = HIDWORD(*((PQWORD) (pDevice->pCurrMeasureEID->sReq.abyStartTime)));
1184            LODWORD(qwStartTSF) = LODWORD(*((PQWORD) (pDevice->pCurrMeasureEID->sReq.abyStartTime)));
1185            wDuration = *((unsigned short *) (pDevice->pCurrMeasureEID->sReq.abyDuration));
1186            wDuration += 1; // 1 TU for channel switching
1187
1188            if ((LODWORD(qwStartTSF) == 0) && (HIDWORD(qwStartTSF) == 0)) {
1189                // start immediately by setting start TSF == current TSF + 2 TU
1190                LODWORD(qwStartTSF) = LODWORD(qwCurrTSF) + 2048;
1191                HIDWORD(qwStartTSF) = HIDWORD(qwCurrTSF);
1192                if (LODWORD(qwCurrTSF) > LODWORD(qwStartTSF)) {
1193                    HIDWORD(qwStartTSF)++;
1194                }
1195                bExpired = false;
1196                break;
1197            } else {
1198                // start at setting start TSF - 1TU(for channel switching)
1199                if (LODWORD(qwStartTSF) < 1024) {
1200                    HIDWORD(qwStartTSF)--;
1201                }
1202                LODWORD(qwStartTSF) -= 1024;
1203            }
1204
1205            if ((HIDWORD(qwCurrTSF) < HIDWORD(qwStartTSF)) ||
1206                ((HIDWORD(qwCurrTSF) == HIDWORD(qwStartTSF)) &&
1207                (LODWORD(qwCurrTSF) < LODWORD(qwStartTSF)))
1208                ) {
1209                bExpired = false;
1210                break;
1211            }
1212            VNTWIFIbMeasureReport(  pDevice->pMgmt,
1213                                    false,
1214                                    pDevice->pCurrMeasureEID,
1215                                    MEASURE_MODE_LATE,
1216                                    pDevice->byBasicMap,
1217                                    pDevice->byCCAFraction,
1218                                    pDevice->abyRPIs
1219                                    );
1220        } else {
1221            // hardware do not support measure
1222            VNTWIFIbMeasureReport(  pDevice->pMgmt,
1223                                    false,
1224                                    pDevice->pCurrMeasureEID,
1225                                    MEASURE_MODE_INCAPABLE,
1226                                    pDevice->byBasicMap,
1227                                    pDevice->byCCAFraction,
1228                                    pDevice->abyRPIs
1229                                    );
1230        }
1231    } while (pDevice->uNumOfMeasureEIDs != 0);
1232
1233    if (bExpired == false) {
1234        MACvSelectPage1(pDevice->PortOffset);
1235        VNSvOutPortD(pDevice->PortOffset + MAC_REG_MSRSTART, LODWORD(qwStartTSF));
1236        VNSvOutPortD(pDevice->PortOffset + MAC_REG_MSRSTART + 4, HIDWORD(qwStartTSF));
1237        VNSvOutPortW(pDevice->PortOffset + MAC_REG_MSRDURATION, wDuration);
1238        MACvRegBitsOn(pDevice->PortOffset, MAC_REG_MSRCTL, MSRCTL_EN);
1239        MACvSelectPage0(pDevice->PortOffset);
1240    } else {
1241        // all measure start time expired we should complete action
1242        VNTWIFIbMeasureReport(  pDevice->pMgmt,
1243                                true,
1244                                NULL,
1245                                0,
1246                                pDevice->byBasicMap,
1247                                pDevice->byCCAFraction,
1248                                pDevice->abyRPIs
1249                                );
1250    }
1251    return (true);
1252}
1253
1254
1255/*
1256 *
1257 * Description:
1258 *    Do Channel Switch defined in 802.11h
1259 *
1260 * Parameters:
1261 *  In:
1262 *      hDeviceContext - device structure point
1263 *  Out:
1264 *      none
1265 *
1266 * Return Value: none.
1267 *
1268-*/
1269bool
1270CARDbChannelSwitch (
1271    void *pDeviceHandler,
1272    unsigned char byMode,
1273    unsigned char byNewChannel,
1274    unsigned char byCount
1275    )
1276{
1277    PSDevice    pDevice = (PSDevice) pDeviceHandler;
1278    bool bResult = true;
1279
1280    if (byCount == 0) {
1281        bResult = set_channel(pDevice, byNewChannel);
1282        VNTWIFIbChannelSwitch(pDevice->pMgmt, byNewChannel);
1283        MACvSelectPage1(pDevice->PortOffset);
1284        MACvRegBitsOn(pDevice->PortOffset, MAC_REG_MSRCTL+1, MSRCTL1_TXPAUSE);
1285        MACvSelectPage0(pDevice->PortOffset);
1286        return(bResult);
1287    }
1288    pDevice->byChannelSwitchCount = byCount;
1289    pDevice->byNewChannel = byNewChannel;
1290    pDevice->bChannelSwitch = true;
1291    if (byMode == 1) {
1292        bResult=CARDbStopTxPacket(pDevice, PKT_TYPE_802_11_ALL);
1293    }
1294    return (bResult);
1295}
1296
1297
1298/*
1299 *
1300 * Description:
1301 *    Handle Quiet EID defined in 802.11h
1302 *
1303 * Parameters:
1304 *  In:
1305 *      hDeviceContext - device structure point
1306 *  Out:
1307 *      none
1308 *
1309 * Return Value: none.
1310 *
1311-*/
1312bool
1313CARDbSetQuiet (
1314    void *pDeviceHandler,
1315    bool bResetQuiet,
1316    unsigned char byQuietCount,
1317    unsigned char byQuietPeriod,
1318    unsigned short wQuietDuration,
1319    unsigned short wQuietOffset
1320    )
1321{
1322    PSDevice    pDevice = (PSDevice) pDeviceHandler;
1323    unsigned int ii = 0;
1324
1325    if (bResetQuiet == true) {
1326        MACvRegBitsOff(pDevice->PortOffset, MAC_REG_MSRCTL, (MSRCTL_QUIETTXCHK | MSRCTL_QUIETEN));
1327        for(ii=0;ii<MAX_QUIET_COUNT;ii++) {
1328            pDevice->sQuiet[ii].bEnable = false;
1329        }
1330        pDevice->uQuietEnqueue = 0;
1331        pDevice->bEnableFirstQuiet = false;
1332        pDevice->bQuietEnable = false;
1333        pDevice->byQuietStartCount = byQuietCount;
1334    }
1335    if (pDevice->sQuiet[pDevice->uQuietEnqueue].bEnable == false) {
1336        pDevice->sQuiet[pDevice->uQuietEnqueue].bEnable = true;
1337        pDevice->sQuiet[pDevice->uQuietEnqueue].byPeriod = byQuietPeriod;
1338        pDevice->sQuiet[pDevice->uQuietEnqueue].wDuration = wQuietDuration;
1339        pDevice->sQuiet[pDevice->uQuietEnqueue].dwStartTime = (unsigned long) byQuietCount;
1340        pDevice->sQuiet[pDevice->uQuietEnqueue].dwStartTime *= pDevice->wBeaconInterval;
1341        pDevice->sQuiet[pDevice->uQuietEnqueue].dwStartTime += wQuietOffset;
1342        pDevice->uQuietEnqueue++;
1343        pDevice->uQuietEnqueue %= MAX_QUIET_COUNT;
1344        if (pDevice->byQuietStartCount < byQuietCount) {
1345            pDevice->byQuietStartCount = byQuietCount;
1346        }
1347    } else {
1348        // we can not handle Quiet EID more
1349    }
1350    return (true);
1351}
1352
1353
1354/*
1355 *
1356 * Description:
1357 *    Do Quiet, It will called by either ISR (after start) or VNTWIFI (before start) so do not need SPINLOCK
1358 *
1359 * Parameters:
1360 *  In:
1361 *      hDeviceContext - device structure point
1362 *  Out:
1363 *      none
1364 *
1365 * Return Value: none.
1366 *
1367-*/
1368bool
1369CARDbStartQuiet (
1370    void *pDeviceHandler
1371    )
1372{
1373    PSDevice    pDevice = (PSDevice) pDeviceHandler;
1374    unsigned int ii = 0;
1375    unsigned long dwStartTime = 0xFFFFFFFF;
1376    unsigned int uCurrentQuietIndex = 0;
1377    unsigned long dwNextTime = 0;
1378    unsigned long dwGap = 0;
1379    unsigned long dwDuration = 0;
1380
1381    for(ii=0;ii<MAX_QUIET_COUNT;ii++) {
1382        if ((pDevice->sQuiet[ii].bEnable == true) &&
1383            (dwStartTime > pDevice->sQuiet[ii].dwStartTime)) {
1384            dwStartTime = pDevice->sQuiet[ii].dwStartTime;
1385            uCurrentQuietIndex = ii;
1386        }
1387    }
1388    if (dwStartTime == 0xFFFFFFFF) {
1389        // no more quiet
1390        pDevice->bQuietEnable = false;
1391        MACvRegBitsOff(pDevice->PortOffset, MAC_REG_MSRCTL, (MSRCTL_QUIETTXCHK | MSRCTL_QUIETEN));
1392    } else {
1393        if (pDevice->bQuietEnable == false) {
1394            // first quiet
1395            pDevice->byQuietStartCount--;
1396            dwNextTime = pDevice->sQuiet[uCurrentQuietIndex].dwStartTime;
1397            dwNextTime %= pDevice->wBeaconInterval;
1398            MACvSelectPage1(pDevice->PortOffset);
1399            VNSvOutPortW(pDevice->PortOffset + MAC_REG_QUIETINIT, (unsigned short) dwNextTime);
1400            VNSvOutPortW(pDevice->PortOffset + MAC_REG_QUIETDUR, (unsigned short) pDevice->sQuiet[uCurrentQuietIndex].wDuration);
1401            if (pDevice->byQuietStartCount == 0) {
1402                pDevice->bEnableFirstQuiet = false;
1403                MACvRegBitsOn(pDevice->PortOffset, MAC_REG_MSRCTL, (MSRCTL_QUIETTXCHK | MSRCTL_QUIETEN));
1404            } else {
1405                pDevice->bEnableFirstQuiet = true;
1406            }
1407            MACvSelectPage0(pDevice->PortOffset);
1408        } else {
1409            if (pDevice->dwCurrentQuietEndTime > pDevice->sQuiet[uCurrentQuietIndex].dwStartTime) {
1410                // overlap with previous Quiet
1411                dwGap =  pDevice->dwCurrentQuietEndTime - pDevice->sQuiet[uCurrentQuietIndex].dwStartTime;
1412                if (dwGap >= pDevice->sQuiet[uCurrentQuietIndex].wDuration) {
1413                    // return false to indicate next quiet expired, should call this function again
1414                    return (false);
1415                }
1416                dwDuration = pDevice->sQuiet[uCurrentQuietIndex].wDuration - dwGap;
1417                dwGap = 0;
1418            } else {
1419                dwGap = pDevice->sQuiet[uCurrentQuietIndex].dwStartTime - pDevice->dwCurrentQuietEndTime;
1420                dwDuration = pDevice->sQuiet[uCurrentQuietIndex].wDuration;
1421            }
1422            // set GAP and Next duration
1423            MACvSelectPage1(pDevice->PortOffset);
1424            VNSvOutPortW(pDevice->PortOffset + MAC_REG_QUIETGAP, (unsigned short) dwGap);
1425            VNSvOutPortW(pDevice->PortOffset + MAC_REG_QUIETDUR, (unsigned short) dwDuration);
1426            MACvRegBitsOn(pDevice->PortOffset, MAC_REG_MSRCTL, MSRCTL_QUIETRPT);
1427            MACvSelectPage0(pDevice->PortOffset);
1428        }
1429        pDevice->bQuietEnable = true;
1430        pDevice->dwCurrentQuietEndTime = pDevice->sQuiet[uCurrentQuietIndex].dwStartTime;
1431        pDevice->dwCurrentQuietEndTime += pDevice->sQuiet[uCurrentQuietIndex].wDuration;
1432        if (pDevice->sQuiet[uCurrentQuietIndex].byPeriod == 0) {
1433            // not period disable current quiet element
1434            pDevice->sQuiet[uCurrentQuietIndex].bEnable = false;
1435        } else {
1436            // set next period start time
1437            dwNextTime = (unsigned long) pDevice->sQuiet[uCurrentQuietIndex].byPeriod;
1438            dwNextTime *= pDevice->wBeaconInterval;
1439            pDevice->sQuiet[uCurrentQuietIndex].dwStartTime = dwNextTime;
1440        }
1441        if (pDevice->dwCurrentQuietEndTime > 0x80010000) {
1442            // decreament all time to avoid wrap around
1443            for(ii=0;ii<MAX_QUIET_COUNT;ii++) {
1444                if (pDevice->sQuiet[ii].bEnable == true) {
1445                    pDevice->sQuiet[ii].dwStartTime -= 0x80000000;
1446                }
1447            }
1448            pDevice->dwCurrentQuietEndTime -= 0x80000000;
1449        }
1450    }
1451    return (true);
1452}
1453
1454/*
1455 *
1456 * Description:
1457 *    Set Local Power Constraint
1458 *
1459 * Parameters:
1460 *  In:
1461 *      hDeviceContext - device structure point
1462 *  Out:
1463 *      none
1464 *
1465 * Return Value: none.
1466 *
1467-*/
1468void
1469CARDvSetPowerConstraint (
1470    void *pDeviceHandler,
1471    unsigned char byChannel,
1472    char byPower
1473    )
1474{
1475    PSDevice    pDevice = (PSDevice) pDeviceHandler;
1476
1477    if (byChannel > CB_MAX_CHANNEL_24G) {
1478        if (pDevice->bCountryInfo5G == true) {
1479            pDevice->abyLocalPwr[byChannel] = pDevice->abyRegPwr[byChannel] - byPower;
1480        }
1481    } else {
1482        if (pDevice->bCountryInfo24G == true) {
1483            pDevice->abyLocalPwr[byChannel] = pDevice->abyRegPwr[byChannel] - byPower;
1484        }
1485    }
1486}
1487
1488
1489/*
1490 *
1491 * Description:
1492 *    Set Local Power Constraint
1493 *
1494 * Parameters:
1495 *  In:
1496 *      hDeviceContext - device structure point
1497 *  Out:
1498 *      none
1499 *
1500 * Return Value: none.
1501 *
1502-*/
1503void
1504CARDvGetPowerCapability (
1505    void *pDeviceHandler,
1506    unsigned char *pbyMinPower,
1507    unsigned char *pbyMaxPower
1508    )
1509{
1510    PSDevice    pDevice = (PSDevice) pDeviceHandler;
1511    unsigned char byDec = 0;
1512
1513    *pbyMaxPower = pDevice->abyOFDMDefaultPwr[pDevice->byCurrentCh];
1514    byDec = pDevice->abyOFDMPwrTbl[pDevice->byCurrentCh];
1515    if (pDevice->byRFType == RF_UW2452) {
1516        byDec *= 3;
1517        byDec >>= 1;
1518    } else {
1519        byDec <<= 1;
1520    }
1521    *pbyMinPower = pDevice->abyOFDMDefaultPwr[pDevice->byCurrentCh] - byDec;
1522}
1523
1524/*
1525 *
1526 * Description:
1527 *    Get Current Tx Power
1528 *
1529 * Parameters:
1530 *  In:
1531 *      hDeviceContext - device structure point
1532 *  Out:
1533 *      none
1534 *
1535 * Return Value: none.
1536 *
1537 */
1538char
1539CARDbyGetTransmitPower (
1540    void *pDeviceHandler
1541    )
1542{
1543    PSDevice    pDevice = (PSDevice) pDeviceHandler;
1544
1545    return (pDevice->byCurPwrdBm);
1546}
1547
1548//xxx
1549void
1550CARDvSafeResetTx (
1551    void *pDeviceHandler
1552    )
1553{
1554    PSDevice    pDevice = (PSDevice) pDeviceHandler;
1555    unsigned int uu;
1556    PSTxDesc    pCurrTD;
1557
1558    // initialize TD index
1559    pDevice->apTailTD[0] = pDevice->apCurrTD[0] = &(pDevice->apTD0Rings[0]);
1560    pDevice->apTailTD[1] = pDevice->apCurrTD[1] = &(pDevice->apTD1Rings[0]);
1561
1562    for (uu = 0; uu < TYPE_MAXTD; uu ++)
1563        pDevice->iTDUsed[uu] = 0;
1564
1565    for (uu = 0; uu < pDevice->sOpts.nTxDescs[0]; uu++) {
1566        pCurrTD = &(pDevice->apTD0Rings[uu]);
1567        pCurrTD->m_td0TD0.f1Owner = OWNED_BY_HOST;
1568        // init all Tx Packet pointer to NULL
1569    }
1570    for (uu = 0; uu < pDevice->sOpts.nTxDescs[1]; uu++) {
1571        pCurrTD = &(pDevice->apTD1Rings[uu]);
1572        pCurrTD->m_td0TD0.f1Owner = OWNED_BY_HOST;
1573        // init all Tx Packet pointer to NULL
1574    }
1575
1576    // set MAC TD pointer
1577    MACvSetCurrTXDescAddr(TYPE_TXDMA0, pDevice->PortOffset,
1578                        (pDevice->td0_pool_dma));
1579
1580    MACvSetCurrTXDescAddr(TYPE_AC0DMA, pDevice->PortOffset,
1581                        (pDevice->td1_pool_dma));
1582
1583    // set MAC Beacon TX pointer
1584    MACvSetCurrBCNTxDescAddr(pDevice->PortOffset,
1585                        (pDevice->tx_beacon_dma));
1586
1587}
1588
1589
1590
1591/*+
1592 *
1593 * Description:
1594 *      Reset Rx
1595 *
1596 * Parameters:
1597 *  In:
1598 *      pDevice     - Pointer to the adapter
1599 *  Out:
1600 *      none
1601 *
1602 * Return Value: none
1603 *
1604-*/
1605void
1606CARDvSafeResetRx (
1607    void *pDeviceHandler
1608    )
1609{
1610    PSDevice    pDevice = (PSDevice) pDeviceHandler;
1611    unsigned int uu;
1612    PSRxDesc    pDesc;
1613
1614
1615
1616    // initialize RD index
1617    pDevice->pCurrRD[0]=&(pDevice->aRD0Ring[0]);
1618    pDevice->pCurrRD[1]=&(pDevice->aRD1Ring[0]);
1619
1620    // init state, all RD is chip's
1621    for (uu = 0; uu < pDevice->sOpts.nRxDescs0; uu++) {
1622        pDesc =&(pDevice->aRD0Ring[uu]);
1623        pDesc->m_rd0RD0.wResCount = (unsigned short)(pDevice->rx_buf_sz);
1624        pDesc->m_rd0RD0.f1Owner=OWNED_BY_NIC;
1625        pDesc->m_rd1RD1.wReqCount = (unsigned short)(pDevice->rx_buf_sz);
1626    }
1627
1628    // init state, all RD is chip's
1629    for (uu = 0; uu < pDevice->sOpts.nRxDescs1; uu++) {
1630        pDesc =&(pDevice->aRD1Ring[uu]);
1631        pDesc->m_rd0RD0.wResCount = (unsigned short)(pDevice->rx_buf_sz);
1632        pDesc->m_rd0RD0.f1Owner=OWNED_BY_NIC;
1633        pDesc->m_rd1RD1.wReqCount = (unsigned short)(pDevice->rx_buf_sz);
1634    }
1635
1636    pDevice->cbDFCB = CB_MAX_RX_FRAG;
1637    pDevice->cbFreeDFCB = pDevice->cbDFCB;
1638
1639    // set perPkt mode
1640    MACvRx0PerPktMode(pDevice->PortOffset);
1641    MACvRx1PerPktMode(pDevice->PortOffset);
1642    // set MAC RD pointer
1643    MACvSetCurrRx0DescAddr(pDevice->PortOffset,
1644                            pDevice->rd0_pool_dma);
1645
1646    MACvSetCurrRx1DescAddr(pDevice->PortOffset,
1647                            pDevice->rd1_pool_dma);
1648}
1649
1650
1651
1652
1653/*
1654 * Description: Get response Control frame rate in CCK mode
1655 *
1656 * Parameters:
1657 *  In:
1658 *      pDevice             - The adapter to be set
1659 *      wRateIdx            - Receiving data rate
1660 *  Out:
1661 *      none
1662 *
1663 * Return Value: response Control frame rate
1664 *
1665 */
1666unsigned short CARDwGetCCKControlRate(void *pDeviceHandler, unsigned short wRateIdx)
1667{
1668    PSDevice    pDevice = (PSDevice) pDeviceHandler;
1669    unsigned int ui = (unsigned int) wRateIdx;
1670
1671    while (ui > RATE_1M) {
1672        if (pDevice->wBasicRate & ((unsigned short)1 << ui)) {
1673            return (unsigned short)ui;
1674        }
1675        ui --;
1676    }
1677    return (unsigned short)RATE_1M;
1678}
1679
1680/*
1681 * Description: Get response Control frame rate in OFDM mode
1682 *
1683 * Parameters:
1684 *  In:
1685 *      pDevice             - The adapter to be set
1686 *      wRateIdx            - Receiving data rate
1687 *  Out:
1688 *      none
1689 *
1690 * Return Value: response Control frame rate
1691 *
1692 */
1693unsigned short CARDwGetOFDMControlRate (void *pDeviceHandler, unsigned short wRateIdx)
1694{
1695    PSDevice pDevice = (PSDevice) pDeviceHandler;
1696    unsigned int ui = (unsigned int) wRateIdx;
1697
1698    DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"BASIC RATE: %X\n", pDevice->wBasicRate);
1699
1700    if (!CARDbIsOFDMinBasicRate((void *)pDevice)) {
1701        DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"CARDwGetOFDMControlRate:(NO OFDM) %d\n", wRateIdx);
1702        if (wRateIdx > RATE_24M)
1703            wRateIdx = RATE_24M;
1704        return wRateIdx;
1705    }
1706    while (ui > RATE_11M) {
1707        if (pDevice->wBasicRate & ((unsigned short)1 << ui)) {
1708            DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"CARDwGetOFDMControlRate : %d\n", ui);
1709            return (unsigned short)ui;
1710        }
1711        ui --;
1712    }
1713    DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"CARDwGetOFDMControlRate: 6M\n");
1714    return (unsigned short)RATE_24M;
1715}
1716
1717
1718/*
1719 * Description: Set RSPINF
1720 *
1721 * Parameters:
1722 *  In:
1723 *      pDevice             - The adapter to be set
1724 *  Out:
1725 *      none
1726 *
1727 * Return Value: None.
1728 *
1729 */
1730void CARDvSetRSPINF (void *pDeviceHandler, CARD_PHY_TYPE ePHYType)
1731{
1732    PSDevice pDevice = (PSDevice) pDeviceHandler;
1733    unsigned char byServ = 0x00, bySignal = 0x00; //For CCK
1734    unsigned short wLen = 0x0000;
1735    unsigned char byTxRate, byRsvTime;             //For OFDM
1736
1737    //Set to Page1
1738    MACvSelectPage1(pDevice->PortOffset);
1739
1740    //RSPINF_b_1
1741    BBvCaculateParameter(pDevice,
1742                         14,
1743                         CARDwGetCCKControlRate((void *)pDevice, RATE_1M),
1744                         PK_TYPE_11B,
1745                         &wLen,
1746                         &byServ,
1747                         &bySignal
1748    );
1749
1750    VNSvOutPortD(pDevice->PortOffset + MAC_REG_RSPINF_B_1, MAKEDWORD(wLen,MAKEWORD(bySignal,byServ)));
1751    ///RSPINF_b_2
1752    BBvCaculateParameter(pDevice,
1753                         14,
1754                         CARDwGetCCKControlRate((void *)pDevice, RATE_2M),
1755                         PK_TYPE_11B,
1756                         &wLen,
1757                         &byServ,
1758                         &bySignal
1759    );
1760
1761    VNSvOutPortD(pDevice->PortOffset + MAC_REG_RSPINF_B_2, MAKEDWORD(wLen,MAKEWORD(bySignal,byServ)));
1762    //RSPINF_b_5
1763    BBvCaculateParameter(pDevice,
1764                         14,
1765                         CARDwGetCCKControlRate((void *)pDevice, RATE_5M),
1766                         PK_TYPE_11B,
1767                         &wLen,
1768                         &byServ,
1769                         &bySignal
1770    );
1771
1772    VNSvOutPortD(pDevice->PortOffset + MAC_REG_RSPINF_B_5, MAKEDWORD(wLen,MAKEWORD(bySignal,byServ)));
1773    //RSPINF_b_11
1774    BBvCaculateParameter(pDevice,
1775                         14,
1776                         CARDwGetCCKControlRate((void *)pDevice, RATE_11M),
1777                         PK_TYPE_11B,
1778                         &wLen,
1779                         &byServ,
1780                         &bySignal
1781    );
1782
1783    VNSvOutPortD(pDevice->PortOffset + MAC_REG_RSPINF_B_11, MAKEDWORD(wLen,MAKEWORD(bySignal,byServ)));
1784    //RSPINF_a_6
1785    s_vCaculateOFDMRParameter(RATE_6M,
1786                              ePHYType,
1787                              &byTxRate,
1788                              &byRsvTime);
1789    VNSvOutPortW(pDevice->PortOffset + MAC_REG_RSPINF_A_6, MAKEWORD(byTxRate,byRsvTime));
1790    //RSPINF_a_9
1791    s_vCaculateOFDMRParameter(RATE_9M,
1792                              ePHYType,
1793                              &byTxRate,
1794                              &byRsvTime);
1795    VNSvOutPortW(pDevice->PortOffset + MAC_REG_RSPINF_A_9, MAKEWORD(byTxRate,byRsvTime));
1796    //RSPINF_a_12
1797    s_vCaculateOFDMRParameter(RATE_12M,
1798                              ePHYType,
1799                              &byTxRate,
1800                              &byRsvTime);
1801    VNSvOutPortW(pDevice->PortOffset + MAC_REG_RSPINF_A_12, MAKEWORD(byTxRate,byRsvTime));
1802    //RSPINF_a_18
1803    s_vCaculateOFDMRParameter(RATE_18M,
1804                              ePHYType,
1805                              &byTxRate,
1806                              &byRsvTime);
1807   VNSvOutPortW(pDevice->PortOffset + MAC_REG_RSPINF_A_18, MAKEWORD(byTxRate,byRsvTime));
1808    //RSPINF_a_24
1809    s_vCaculateOFDMRParameter(RATE_24M,
1810                              ePHYType,
1811                              &byTxRate,
1812                              &byRsvTime);
1813    VNSvOutPortW(pDevice->PortOffset + MAC_REG_RSPINF_A_24, MAKEWORD(byTxRate,byRsvTime));
1814    //RSPINF_a_36
1815    s_vCaculateOFDMRParameter(CARDwGetOFDMControlRate((void *)pDevice, RATE_36M),
1816                              ePHYType,
1817                              &byTxRate,
1818                              &byRsvTime);
1819    VNSvOutPortW(pDevice->PortOffset + MAC_REG_RSPINF_A_36, MAKEWORD(byTxRate,byRsvTime));
1820    //RSPINF_a_48
1821    s_vCaculateOFDMRParameter(CARDwGetOFDMControlRate((void *)pDevice, RATE_48M),
1822                              ePHYType,
1823                              &byTxRate,
1824                              &byRsvTime);
1825    VNSvOutPortW(pDevice->PortOffset + MAC_REG_RSPINF_A_48, MAKEWORD(byTxRate,byRsvTime));
1826    //RSPINF_a_54
1827    s_vCaculateOFDMRParameter(CARDwGetOFDMControlRate((void *)pDevice, RATE_54M),
1828                              ePHYType,
1829                              &byTxRate,
1830                              &byRsvTime);
1831    VNSvOutPortW(pDevice->PortOffset + MAC_REG_RSPINF_A_54, MAKEWORD(byTxRate,byRsvTime));
1832
1833    //RSPINF_a_72
1834    s_vCaculateOFDMRParameter(CARDwGetOFDMControlRate((void *)pDevice, RATE_54M),
1835                              ePHYType,
1836                              &byTxRate,
1837                              &byRsvTime);
1838    VNSvOutPortW(pDevice->PortOffset + MAC_REG_RSPINF_A_72, MAKEWORD(byTxRate,byRsvTime));
1839    //Set to Page0
1840    MACvSelectPage0(pDevice->PortOffset);
1841}
1842
1843/*
1844 * Description: Update IFS
1845 *
1846 * Parameters:
1847 *  In:
1848 *      pDevice             - The adapter to be set
1849 *  Out:
1850 *      none
1851 *
1852 * Return Value: None.
1853 *
1854 */
1855void vUpdateIFS (void *pDeviceHandler)
1856{
1857    //Set SIFS, DIFS, EIFS, SlotTime, CwMin
1858    PSDevice pDevice = (PSDevice) pDeviceHandler;
1859
1860    unsigned char byMaxMin = 0;
1861    if (pDevice->byPacketType==PK_TYPE_11A) {//0000 0000 0000 0000,11a
1862        pDevice->uSlot = C_SLOT_SHORT;
1863        pDevice->uSIFS = C_SIFS_A;
1864        pDevice->uDIFS = C_SIFS_A + 2*C_SLOT_SHORT;
1865        pDevice->uCwMin = C_CWMIN_A;
1866        byMaxMin = 4;
1867    }
1868    else if (pDevice->byPacketType==PK_TYPE_11B) {//0000 0001 0000 0000,11b
1869        pDevice->uSlot = C_SLOT_LONG;
1870        pDevice->uSIFS = C_SIFS_BG;
1871        pDevice->uDIFS = C_SIFS_BG + 2*C_SLOT_LONG;
1872	    pDevice->uCwMin = C_CWMIN_B;
1873        byMaxMin = 5;
1874    }
1875    else { // PK_TYPE_11GA & PK_TYPE_11GB
1876        pDevice->uSIFS = C_SIFS_BG;
1877        if (pDevice->bShortSlotTime) {
1878            pDevice->uSlot = C_SLOT_SHORT;
1879        } else {
1880	        pDevice->uSlot = C_SLOT_LONG;
1881	    }
1882	    pDevice->uDIFS = C_SIFS_BG + 2*pDevice->uSlot;
1883        if (pDevice->wBasicRate & 0x0150) { //0000 0001 0101 0000,24M,12M,6M
1884            pDevice->uCwMin = C_CWMIN_A;
1885            byMaxMin = 4;
1886        }
1887        else {
1888            pDevice->uCwMin = C_CWMIN_B;
1889            byMaxMin = 5;
1890        }
1891    }
1892
1893    pDevice->uCwMax = C_CWMAX;
1894    pDevice->uEIFS = C_EIFS;
1895    if (pDevice->byRFType == RF_RFMD2959) {
1896        // bcs TX_PE will reserve 3 us
1897        VNSvOutPortB(pDevice->PortOffset + MAC_REG_SIFS, (unsigned char)(pDevice->uSIFS - 3));
1898        VNSvOutPortB(pDevice->PortOffset + MAC_REG_DIFS, (unsigned char)(pDevice->uDIFS - 3));
1899    } else {
1900        VNSvOutPortB(pDevice->PortOffset + MAC_REG_SIFS, (unsigned char)pDevice->uSIFS);
1901        VNSvOutPortB(pDevice->PortOffset + MAC_REG_DIFS, (unsigned char)pDevice->uDIFS);
1902    }
1903    VNSvOutPortB(pDevice->PortOffset + MAC_REG_EIFS, (unsigned char)pDevice->uEIFS);
1904    VNSvOutPortB(pDevice->PortOffset + MAC_REG_SLOT, (unsigned char)pDevice->uSlot);
1905    byMaxMin |= 0xA0;//1010 1111,C_CWMAX = 1023
1906    VNSvOutPortB(pDevice->PortOffset + MAC_REG_CWMAXMIN0, (unsigned char)byMaxMin);
1907}
1908
1909void CARDvUpdateBasicTopRate (void *pDeviceHandler)
1910{
1911    PSDevice pDevice = (PSDevice) pDeviceHandler;
1912    unsigned char byTopOFDM = RATE_24M, byTopCCK = RATE_1M;
1913    unsigned char ii;
1914
1915     //Determines the highest basic rate.
1916     for (ii = RATE_54M; ii >= RATE_6M; ii --) {
1917         if ( (pDevice->wBasicRate) & ((unsigned short)(1<<ii)) ) {
1918             byTopOFDM = ii;
1919             break;
1920         }
1921     }
1922     pDevice->byTopOFDMBasicRate = byTopOFDM;
1923
1924     for (ii = RATE_11M;; ii --) {
1925         if ( (pDevice->wBasicRate) & ((unsigned short)(1<<ii)) ) {
1926             byTopCCK = ii;
1927             break;
1928         }
1929         if (ii == RATE_1M)
1930            break;
1931     }
1932     pDevice->byTopCCKBasicRate = byTopCCK;
1933}
1934
1935
1936/*
1937 * Description: Set NIC Tx Basic Rate
1938 *
1939 * Parameters:
1940 *  In:
1941 *      pDevice         - The adapter to be set
1942 *      wBasicRate      - Basic Rate to be set
1943 *  Out:
1944 *      none
1945 *
1946 * Return Value: true if succeeded; false if failed.
1947 *
1948 */
1949bool CARDbAddBasicRate (void *pDeviceHandler, unsigned short wRateIdx)
1950{
1951    PSDevice pDevice = (PSDevice) pDeviceHandler;
1952    unsigned short wRate = (unsigned short)(1<<wRateIdx);
1953
1954    pDevice->wBasicRate |= wRate;
1955
1956    //Determines the highest basic rate.
1957    CARDvUpdateBasicTopRate((void *)pDevice);
1958
1959    return(true);
1960}
1961
1962bool CARDbIsOFDMinBasicRate (void *pDeviceHandler)
1963{
1964    PSDevice pDevice = (PSDevice) pDeviceHandler;
1965    int ii;
1966
1967    for (ii = RATE_54M; ii >= RATE_6M; ii --) {
1968        if ((pDevice->wBasicRate) & ((unsigned short)(1<<ii)))
1969            return true;
1970    }
1971    return false;
1972}
1973
1974unsigned char CARDbyGetPktType (void *pDeviceHandler)
1975{
1976    PSDevice pDevice = (PSDevice) pDeviceHandler;
1977
1978    if (pDevice->byBBType == BB_TYPE_11A || pDevice->byBBType == BB_TYPE_11B) {
1979        return (unsigned char)pDevice->byBBType;
1980    }
1981    else if (CARDbIsOFDMinBasicRate((void *)pDevice)) {
1982        return PK_TYPE_11GA;
1983    }
1984    else {
1985    	return PK_TYPE_11GB;
1986    }
1987}
1988
1989/*
1990 * Description: Set NIC Loopback mode
1991 *
1992 * Parameters:
1993 *  In:
1994 *      pDevice         - The adapter to be set
1995 *      wLoopbackMode   - Loopback mode to be set
1996 *  Out:
1997 *      none
1998 *
1999 * Return Value: none
2000 *
2001 */
2002void CARDvSetLoopbackMode (unsigned long dwIoBase, unsigned short wLoopbackMode)
2003{
2004    switch(wLoopbackMode) {
2005    case CARD_LB_NONE:
2006    case CARD_LB_MAC:
2007    case CARD_LB_PHY:
2008        break;
2009    default:
2010        ASSERT(false);
2011        break;
2012    }
2013    // set MAC loopback
2014    MACvSetLoopbackMode(dwIoBase, LOBYTE(wLoopbackMode));
2015    // set Baseband loopback
2016}
2017
2018
2019/*
2020 * Description: Software Reset NIC
2021 *
2022 * Parameters:
2023 *  In:
2024 *      pDevice         - The adapter to be reset
2025 *  Out:
2026 *      none
2027 *
2028 * Return Value: none
2029 *
2030 */
2031bool CARDbSoftwareReset (void *pDeviceHandler)
2032{
2033    PSDevice pDevice = (PSDevice) pDeviceHandler;
2034
2035    // reset MAC
2036    if (!MACbSafeSoftwareReset(pDevice->PortOffset))
2037        return false;
2038
2039    return true;
2040}
2041
2042
2043/*
2044 * Description: Caculate TSF offset of two TSF input
2045 *              Get TSF Offset from RxBCN's TSF and local TSF
2046 *
2047 * Parameters:
2048 *  In:
2049 *      pDevice         - The adapter to be sync.
2050 *      qwTSF1          - Rx BCN's TSF
2051 *      qwTSF2          - Local TSF
2052 *  Out:
2053 *      none
2054 *
2055 * Return Value: TSF Offset value
2056 *
2057 */
2058QWORD CARDqGetTSFOffset (unsigned char byRxRate, QWORD qwTSF1, QWORD qwTSF2)
2059{
2060    QWORD   qwTSFOffset;
2061    unsigned short wRxBcnTSFOffst= 0;
2062
2063    HIDWORD(qwTSFOffset) = 0;
2064    LODWORD(qwTSFOffset) = 0;
2065    wRxBcnTSFOffst = cwRXBCNTSFOff[byRxRate%MAX_RATE];
2066    (qwTSF2).u.dwLowDword += (unsigned long)(wRxBcnTSFOffst);
2067    if ((qwTSF2).u.dwLowDword < (unsigned long)(wRxBcnTSFOffst)) {
2068        (qwTSF2).u.dwHighDword++;
2069    }
2070    LODWORD(qwTSFOffset) = LODWORD(qwTSF1) - LODWORD(qwTSF2);
2071    if (LODWORD(qwTSF1) < LODWORD(qwTSF2)) {
2072        // if borrow needed
2073        HIDWORD(qwTSFOffset) = HIDWORD(qwTSF1) - HIDWORD(qwTSF2) - 1 ;
2074    }
2075    else {
2076        HIDWORD(qwTSFOffset) = HIDWORD(qwTSF1) - HIDWORD(qwTSF2);
2077    };
2078    return (qwTSFOffset);
2079}
2080
2081
2082/*
2083 * Description: Read NIC TSF counter
2084 *              Get local TSF counter
2085 *
2086 * Parameters:
2087 *  In:
2088 *      pDevice         - The adapter to be read
2089 *  Out:
2090 *      qwCurrTSF       - Current TSF counter
2091 *
2092 * Return Value: true if success; otherwise false
2093 *
2094 */
2095bool CARDbGetCurrentTSF (unsigned long dwIoBase, PQWORD pqwCurrTSF)
2096{
2097    unsigned short ww;
2098    unsigned char byData;
2099
2100    MACvRegBitsOn(dwIoBase, MAC_REG_TFTCTL, TFTCTL_TSFCNTRRD);
2101    for (ww = 0; ww < W_MAX_TIMEOUT; ww++) {
2102        VNSvInPortB(dwIoBase + MAC_REG_TFTCTL, &byData);
2103        if ( !(byData & TFTCTL_TSFCNTRRD))
2104            break;
2105    }
2106    if (ww == W_MAX_TIMEOUT)
2107        return(false);
2108    VNSvInPortD(dwIoBase + MAC_REG_TSFCNTR, &LODWORD(*pqwCurrTSF));
2109    VNSvInPortD(dwIoBase + MAC_REG_TSFCNTR + 4, &HIDWORD(*pqwCurrTSF));
2110
2111    return(true);
2112}
2113
2114
2115/*
2116 * Description: Read NIC TSF counter
2117 *              Get NEXTTBTT from adjusted TSF and Beacon Interval
2118 *
2119 * Parameters:
2120 *  In:
2121 *      qwTSF           - Current TSF counter
2122 *      wbeaconInterval - Beacon Interval
2123 *  Out:
2124 *      qwCurrTSF       - Current TSF counter
2125 *
2126 * Return Value: TSF value of next Beacon
2127 *
2128 */
2129QWORD CARDqGetNextTBTT (QWORD qwTSF, unsigned short wBeaconInterval)
2130{
2131
2132    unsigned int uLowNextTBTT;
2133    unsigned int uHighRemain, uLowRemain;
2134    unsigned int uBeaconInterval;
2135
2136    uBeaconInterval = wBeaconInterval * 1024;
2137    // Next TBTT = ((local_current_TSF / beacon_interval) + 1 ) * beacon_interval
2138    uLowNextTBTT = (LODWORD(qwTSF) >> 10) << 10;
2139    // low dword (mod) bcn
2140    uLowRemain = (uLowNextTBTT) % uBeaconInterval;
2141//    uHighRemain = ((0x80000000 % uBeaconInterval)* 2 * HIDWORD(qwTSF))
2142//                  % uBeaconInterval;
2143    // high dword (mod) bcn
2144    uHighRemain = (((0xffffffff % uBeaconInterval) + 1) * HIDWORD(qwTSF))
2145                  % uBeaconInterval;
2146    uLowRemain = (uHighRemain + uLowRemain) % uBeaconInterval;
2147    uLowRemain = uBeaconInterval - uLowRemain;
2148
2149    // check if carry when add one beacon interval
2150    if ((~uLowNextTBTT) < uLowRemain)
2151        HIDWORD(qwTSF) ++ ;
2152
2153    LODWORD(qwTSF) = uLowNextTBTT + uLowRemain;
2154
2155    return (qwTSF);
2156}
2157
2158
2159/*
2160 * Description: Set NIC TSF counter for first Beacon time
2161 *              Get NEXTTBTT from adjusted TSF and Beacon Interval
2162 *
2163 * Parameters:
2164 *  In:
2165 *      dwIoBase        - IO Base
2166 *      wBeaconInterval - Beacon Interval
2167 *  Out:
2168 *      none
2169 *
2170 * Return Value: none
2171 *
2172 */
2173void CARDvSetFirstNextTBTT (unsigned long dwIoBase, unsigned short wBeaconInterval)
2174{
2175
2176    QWORD   qwNextTBTT;
2177
2178    HIDWORD(qwNextTBTT) = 0;
2179    LODWORD(qwNextTBTT) = 0;
2180    CARDbGetCurrentTSF(dwIoBase, &qwNextTBTT); //Get Local TSF counter
2181    qwNextTBTT = CARDqGetNextTBTT(qwNextTBTT, wBeaconInterval);
2182    // Set NextTBTT
2183    VNSvOutPortD(dwIoBase + MAC_REG_NEXTTBTT, LODWORD(qwNextTBTT));
2184    VNSvOutPortD(dwIoBase + MAC_REG_NEXTTBTT + 4, HIDWORD(qwNextTBTT));
2185    MACvRegBitsOn(dwIoBase, MAC_REG_TFTCTL, TFTCTL_TBTTSYNCEN);
2186    //DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Card:First Next TBTT[%8xh:%8xh] \n", HIDWORD(qwNextTBTT), LODWORD(qwNextTBTT));
2187    return;
2188}
2189
2190
2191/*
2192 * Description: Sync NIC TSF counter for Beacon time
2193 *              Get NEXTTBTT and write to HW
2194 *
2195 * Parameters:
2196 *  In:
2197 *      pDevice         - The adapter to be set
2198 *      qwTSF           - Current TSF counter
2199 *      wBeaconInterval - Beacon Interval
2200 *  Out:
2201 *      none
2202 *
2203 * Return Value: none
2204 *
2205 */
2206void CARDvUpdateNextTBTT (unsigned long dwIoBase, QWORD qwTSF, unsigned short wBeaconInterval)
2207{
2208
2209    qwTSF = CARDqGetNextTBTT(qwTSF, wBeaconInterval);
2210    // Set NextTBTT
2211    VNSvOutPortD(dwIoBase + MAC_REG_NEXTTBTT, LODWORD(qwTSF));
2212    VNSvOutPortD(dwIoBase + MAC_REG_NEXTTBTT + 4, HIDWORD(qwTSF));
2213    MACvRegBitsOn(dwIoBase, MAC_REG_TFTCTL, TFTCTL_TBTTSYNCEN);
2214    DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Card:Update Next TBTT[%8xh:%8xh] \n",
2215		    (unsigned int) HIDWORD(qwTSF), (unsigned int) LODWORD(qwTSF));
2216
2217    return;
2218}
2219
2220
2221
2222
2223
2224
2225
2226