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