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