]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/staging/vt6655/card.c
633585fd4a96ce795baf60857c637b482d246f85
[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 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 static 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(struct vnt_private *pDevice, CARD_PHY_TYPE ePHYType,
228              void *pvSupportRateIEs, void *pvExtSupportRateIEs)
229 {
230         unsigned char byServ = 0, bySignal = 0; // For CCK
231         unsigned short wLen = 0;
232         unsigned char byTxRate = 0, byRsvTime = 0;    // For OFDM
233
234         //Set to Page1
235         MACvSelectPage1(pDevice->PortOffset);
236
237         //RSPINF_b_1
238         BBvCalculateParameter(pDevice,
239                               14,
240                               VNTWIFIbyGetACKTxRate(RATE_1M, pvSupportRateIEs, pvExtSupportRateIEs),
241                               PK_TYPE_11B,
242                               &wLen,
243                               &byServ,
244                               &bySignal
245 );
246
247         VNSvOutPortD(pDevice->PortOffset + MAC_REG_RSPINF_B_1, MAKEDWORD(wLen, MAKEWORD(bySignal, byServ)));
248         ///RSPINF_b_2
249         BBvCalculateParameter(pDevice,
250                               14,
251                               VNTWIFIbyGetACKTxRate(RATE_2M, pvSupportRateIEs, pvExtSupportRateIEs),
252                               PK_TYPE_11B,
253                               &wLen,
254                               &byServ,
255                               &bySignal
256 );
257
258         VNSvOutPortD(pDevice->PortOffset + MAC_REG_RSPINF_B_2, MAKEDWORD(wLen, MAKEWORD(bySignal, byServ)));
259         //RSPINF_b_5
260         BBvCalculateParameter(pDevice,
261                               14,
262                               VNTWIFIbyGetACKTxRate(RATE_5M, pvSupportRateIEs, pvExtSupportRateIEs),
263                               PK_TYPE_11B,
264                               &wLen,
265                               &byServ,
266                               &bySignal
267 );
268
269         VNSvOutPortD(pDevice->PortOffset + MAC_REG_RSPINF_B_5, MAKEDWORD(wLen, MAKEWORD(bySignal, byServ)));
270         //RSPINF_b_11
271         BBvCalculateParameter(pDevice,
272                               14,
273                               VNTWIFIbyGetACKTxRate(RATE_11M, pvSupportRateIEs, pvExtSupportRateIEs),
274                               PK_TYPE_11B,
275                               &wLen,
276                               &byServ,
277                               &bySignal
278 );
279
280         VNSvOutPortD(pDevice->PortOffset + MAC_REG_RSPINF_B_11, MAKEDWORD(wLen, MAKEWORD(bySignal, byServ)));
281         //RSPINF_a_6
282         s_vCalculateOFDMRParameter(RATE_6M,
283                                    ePHYType,
284                                    &byTxRate,
285                                    &byRsvTime);
286         VNSvOutPortW(pDevice->PortOffset + MAC_REG_RSPINF_A_6, MAKEWORD(byTxRate, byRsvTime));
287         //RSPINF_a_9
288         s_vCalculateOFDMRParameter(RATE_9M,
289                                    ePHYType,
290                                    &byTxRate,
291                                    &byRsvTime);
292         VNSvOutPortW(pDevice->PortOffset + MAC_REG_RSPINF_A_9, MAKEWORD(byTxRate, byRsvTime));
293         //RSPINF_a_12
294         s_vCalculateOFDMRParameter(RATE_12M,
295                                    ePHYType,
296                                    &byTxRate,
297                                    &byRsvTime);
298         VNSvOutPortW(pDevice->PortOffset + MAC_REG_RSPINF_A_12, MAKEWORD(byTxRate, byRsvTime));
299         //RSPINF_a_18
300         s_vCalculateOFDMRParameter(RATE_18M,
301                                    ePHYType,
302                                    &byTxRate,
303                                    &byRsvTime);
304         VNSvOutPortW(pDevice->PortOffset + MAC_REG_RSPINF_A_18, MAKEWORD(byTxRate, byRsvTime));
305         //RSPINF_a_24
306         s_vCalculateOFDMRParameter(RATE_24M,
307                                    ePHYType,
308                                    &byTxRate,
309                                    &byRsvTime);
310         VNSvOutPortW(pDevice->PortOffset + MAC_REG_RSPINF_A_24, MAKEWORD(byTxRate, byRsvTime));
311         //RSPINF_a_36
312         s_vCalculateOFDMRParameter(
313                 VNTWIFIbyGetACKTxRate(RATE_36M, pvSupportRateIEs, pvExtSupportRateIEs),
314                 ePHYType,
315                 &byTxRate,
316                 &byRsvTime);
317         VNSvOutPortW(pDevice->PortOffset + MAC_REG_RSPINF_A_36, MAKEWORD(byTxRate, byRsvTime));
318         //RSPINF_a_48
319         s_vCalculateOFDMRParameter(
320                 VNTWIFIbyGetACKTxRate(RATE_48M, pvSupportRateIEs, pvExtSupportRateIEs),
321                 ePHYType,
322                 &byTxRate,
323                 &byRsvTime);
324         VNSvOutPortW(pDevice->PortOffset + MAC_REG_RSPINF_A_48, MAKEWORD(byTxRate, byRsvTime));
325         //RSPINF_a_54
326         s_vCalculateOFDMRParameter(
327                 VNTWIFIbyGetACKTxRate(RATE_54M, pvSupportRateIEs, pvExtSupportRateIEs),
328                 ePHYType,
329                 &byTxRate,
330                 &byRsvTime);
331         VNSvOutPortW(pDevice->PortOffset + MAC_REG_RSPINF_A_54, MAKEWORD(byTxRate, byRsvTime));
332         //RSPINF_a_72
333         VNSvOutPortW(pDevice->PortOffset + MAC_REG_RSPINF_A_72, MAKEWORD(byTxRate, byRsvTime));
334         //Set to Page0
335         MACvSelectPage0(pDevice->PortOffset);
336 }
337
338 /*---------------------  Export Functions  --------------------------*/
339
340 /*
341  * Description: Get Card short preamble option value
342  *
343  * Parameters:
344  *  In:
345  *      pDevice             - The adapter to be set
346  *  Out:
347  *      none
348  *
349  * Return Value: true if short preamble; otherwise false
350  *
351  */
352 bool CARDbIsShortPreamble(struct vnt_private *pDevice)
353 {
354
355         if (pDevice->byPreambleType == 0)
356                 return false;
357
358         return true;
359 }
360
361 /*
362  * Description: Get Card short slot time option value
363  *
364  * Parameters:
365  *  In:
366  *      pDevice             - The adapter to be set
367  *  Out:
368  *      none
369  *
370  * Return Value: true if short slot time; otherwise false
371  *
372  */
373 bool CARDbIsShorSlotTime(struct vnt_private *pDevice)
374 {
375
376         return pDevice->bShortSlotTime;
377 }
378
379 /*
380  * Description: Update IFS
381  *
382  * Parameters:
383  *  In:
384  *      pDevice             - The adapter to be set
385  *  Out:
386  *      none
387  *
388  * Return Value: None.
389  *
390  */
391 bool CARDbSetPhyParameter(struct vnt_private *pDevice, CARD_PHY_TYPE ePHYType,
392                           unsigned short wCapInfo, unsigned char byERPField,
393                           void *pvSupportRateIEs, void *pvExtSupportRateIEs)
394 {
395         unsigned char byCWMaxMin = 0;
396         unsigned char bySlot = 0;
397         unsigned char bySIFS = 0;
398         unsigned char byDIFS = 0;
399         unsigned char byData;
400         PWLAN_IE_SUPP_RATES pSupportRates = (PWLAN_IE_SUPP_RATES) pvSupportRateIEs;
401         PWLAN_IE_SUPP_RATES pExtSupportRates = (PWLAN_IE_SUPP_RATES) pvExtSupportRateIEs;
402
403         //Set SIFS, DIFS, EIFS, SlotTime, CwMin
404         if (ePHYType == PHY_TYPE_11A) {
405                 if (pSupportRates == NULL)
406                         pSupportRates = (PWLAN_IE_SUPP_RATES) abyDefaultSuppRatesA;
407
408                 if (pDevice->byRFType == RF_AIROHA7230) {
409                         // AL7230 use single PAPE and connect to PAPE_2.4G
410                         MACvSetBBType(pDevice->PortOffset, BB_TYPE_11G);
411                         pDevice->abyBBVGA[0] = 0x20;
412                         pDevice->abyBBVGA[2] = 0x10;
413                         pDevice->abyBBVGA[3] = 0x10;
414                         BBbReadEmbedded(pDevice->PortOffset, 0xE7, &byData);
415                         if (byData == 0x1C)
416                                 BBbWriteEmbedded(pDevice->PortOffset, 0xE7, pDevice->abyBBVGA[0]);
417
418                 } else if (pDevice->byRFType == RF_UW2452) {
419                         MACvSetBBType(pDevice->PortOffset, BB_TYPE_11A);
420                         pDevice->abyBBVGA[0] = 0x18;
421                         BBbReadEmbedded(pDevice->PortOffset, 0xE7, &byData);
422                         if (byData == 0x14) {
423                                 BBbWriteEmbedded(pDevice->PortOffset, 0xE7, pDevice->abyBBVGA[0]);
424                                 BBbWriteEmbedded(pDevice->PortOffset, 0xE1, 0x57);
425                         }
426                 } else {
427                         MACvSetBBType(pDevice->PortOffset, BB_TYPE_11A);
428                 }
429                 BBbWriteEmbedded(pDevice->PortOffset, 0x88, 0x03);
430                 bySlot = C_SLOT_SHORT;
431                 bySIFS = C_SIFS_A;
432                 byDIFS = C_SIFS_A + 2*C_SLOT_SHORT;
433                 byCWMaxMin = 0xA4;
434         } else if (ePHYType == PHY_TYPE_11B) {
435                 if (pSupportRates == NULL)
436                         pSupportRates = (PWLAN_IE_SUPP_RATES) abyDefaultSuppRatesB;
437
438                 MACvSetBBType(pDevice->PortOffset, BB_TYPE_11B);
439                 if (pDevice->byRFType == RF_AIROHA7230) {
440                         pDevice->abyBBVGA[0] = 0x1C;
441                         pDevice->abyBBVGA[2] = 0x00;
442                         pDevice->abyBBVGA[3] = 0x00;
443                         BBbReadEmbedded(pDevice->PortOffset, 0xE7, &byData);
444                         if (byData == 0x20)
445                                 BBbWriteEmbedded(pDevice->PortOffset, 0xE7, pDevice->abyBBVGA[0]);
446
447                 } else if (pDevice->byRFType == RF_UW2452) {
448                         pDevice->abyBBVGA[0] = 0x14;
449                         BBbReadEmbedded(pDevice->PortOffset, 0xE7, &byData);
450                         if (byData == 0x18) {
451                                 BBbWriteEmbedded(pDevice->PortOffset, 0xE7, pDevice->abyBBVGA[0]);
452                                 BBbWriteEmbedded(pDevice->PortOffset, 0xE1, 0xD3);
453                         }
454                 }
455                 BBbWriteEmbedded(pDevice->PortOffset, 0x88, 0x02);
456                 bySlot = C_SLOT_LONG;
457                 bySIFS = C_SIFS_BG;
458                 byDIFS = C_SIFS_BG + 2*C_SLOT_LONG;
459                 byCWMaxMin = 0xA5;
460         } else {// PK_TYPE_11GA & PK_TYPE_11GB
461                 if (pSupportRates == NULL) {
462                         pSupportRates = (PWLAN_IE_SUPP_RATES) abyDefaultSuppRatesG;
463                         pExtSupportRates = (PWLAN_IE_SUPP_RATES) abyDefaultExtSuppRatesG;
464                 }
465                 MACvSetBBType(pDevice->PortOffset, BB_TYPE_11G);
466                 if (pDevice->byRFType == RF_AIROHA7230) {
467                         pDevice->abyBBVGA[0] = 0x1C;
468                         pDevice->abyBBVGA[2] = 0x00;
469                         pDevice->abyBBVGA[3] = 0x00;
470                         BBbReadEmbedded(pDevice->PortOffset, 0xE7, &byData);
471                         if (byData == 0x20)
472                                 BBbWriteEmbedded(pDevice->PortOffset, 0xE7, pDevice->abyBBVGA[0]);
473
474                 } else if (pDevice->byRFType == RF_UW2452) {
475                         pDevice->abyBBVGA[0] = 0x14;
476                         BBbReadEmbedded(pDevice->PortOffset, 0xE7, &byData);
477                         if (byData == 0x18) {
478                                 BBbWriteEmbedded(pDevice->PortOffset, 0xE7, pDevice->abyBBVGA[0]);
479                                 BBbWriteEmbedded(pDevice->PortOffset, 0xE1, 0xD3);
480                         }
481                 }
482                 BBbWriteEmbedded(pDevice->PortOffset, 0x88, 0x08);
483                 bySIFS = C_SIFS_BG;
484                 if (VNTWIFIbIsShortSlotTime(wCapInfo)) {
485                         bySlot = C_SLOT_SHORT;
486                         byDIFS = C_SIFS_BG + 2*C_SLOT_SHORT;
487                 } else {
488                         bySlot = C_SLOT_LONG;
489                         byDIFS = C_SIFS_BG + 2*C_SLOT_LONG;
490                 }
491                 if (VNTWIFIbyGetMaxSupportRate(pSupportRates, pExtSupportRates) > RATE_11M)
492                         byCWMaxMin = 0xA4;
493                 else
494                         byCWMaxMin = 0xA5;
495
496                 if (pDevice->bProtectMode != VNTWIFIbIsProtectMode(byERPField)) {
497                         pDevice->bProtectMode = VNTWIFIbIsProtectMode(byERPField);
498                         if (pDevice->bProtectMode)
499                                 MACvEnableProtectMD(pDevice->PortOffset);
500                         else
501                                 MACvDisableProtectMD(pDevice->PortOffset);
502
503                 }
504                 if (pDevice->bBarkerPreambleMd != VNTWIFIbIsBarkerMode(byERPField)) {
505                         pDevice->bBarkerPreambleMd = VNTWIFIbIsBarkerMode(byERPField);
506                         if (pDevice->bBarkerPreambleMd)
507                                 MACvEnableBarkerPreambleMd(pDevice->PortOffset);
508                         else
509                                 MACvDisableBarkerPreambleMd(pDevice->PortOffset);
510                 }
511         }
512
513         if (pDevice->byRFType == RF_RFMD2959) {
514                 // bcs TX_PE will reserve 3 us
515                 // hardware's processing time here is 2 us.
516                 bySIFS -= 3;
517                 byDIFS -= 3;
518                 //{{ RobertYu: 20041202
519                 //// TX_PE will reserve 3 us for MAX2829 A mode only, it is for better TX throughput
520                 //// MAC will need 2 us to process, so the SIFS, DIFS can be shorter by 2 us.
521         }
522
523         if (pDevice->bySIFS != bySIFS) {
524                 pDevice->bySIFS = bySIFS;
525                 VNSvOutPortB(pDevice->PortOffset + MAC_REG_SIFS, pDevice->bySIFS);
526         }
527         if (pDevice->byDIFS != byDIFS) {
528                 pDevice->byDIFS = byDIFS;
529                 VNSvOutPortB(pDevice->PortOffset + MAC_REG_DIFS, pDevice->byDIFS);
530         }
531         if (pDevice->byEIFS != C_EIFS) {
532                 pDevice->byEIFS = C_EIFS;
533                 VNSvOutPortB(pDevice->PortOffset + MAC_REG_EIFS, pDevice->byEIFS);
534         }
535         if (pDevice->bySlot != bySlot) {
536                 pDevice->bySlot = bySlot;
537                 VNSvOutPortB(pDevice->PortOffset + MAC_REG_SLOT, pDevice->bySlot);
538                 if (pDevice->bySlot == C_SLOT_SHORT)
539                         pDevice->bShortSlotTime = true;
540                 else
541                         pDevice->bShortSlotTime = false;
542
543                 BBvSetShortSlotTime(pDevice);
544         }
545         if (pDevice->byCWMaxMin != byCWMaxMin) {
546                 pDevice->byCWMaxMin = byCWMaxMin;
547                 VNSvOutPortB(pDevice->PortOffset + MAC_REG_CWMAXMIN0, pDevice->byCWMaxMin);
548         }
549         if (VNTWIFIbIsShortPreamble(wCapInfo))
550                 pDevice->byPreambleType = pDevice->byShortPreamble;
551         else
552                 pDevice->byPreambleType = 0;
553
554         s_vSetRSPINF(pDevice, ePHYType, pSupportRates, pExtSupportRates);
555         pDevice->eCurrentPHYType = ePHYType;
556         // set for NDIS OID_802_11SUPPORTED_RATES
557         return true;
558 }
559
560 /*
561  * Description: Sync. TSF counter to BSS
562  *              Get TSF offset and write to HW
563  *
564  * Parameters:
565  *  In:
566  *      pDevice         - The adapter to be sync.
567  *      byRxRate        - data rate of receive beacon
568  *      qwBSSTimestamp  - Rx BCN's TSF
569  *      qwLocalTSF      - Local TSF
570  *  Out:
571  *      none
572  *
573  * Return Value: none
574  *
575  */
576 bool CARDbUpdateTSF(struct vnt_private *pDevice, unsigned char byRxRate,
577                     u64 qwBSSTimestamp, u64 qwLocalTSF)
578 {
579         u64 qwTSFOffset = 0;
580
581         if (qwBSSTimestamp != qwLocalTSF) {
582                 qwTSFOffset = CARDqGetTSFOffset(byRxRate, qwBSSTimestamp, qwLocalTSF);
583                 // adjust TSF
584                 // HW's TSF add TSF Offset reg
585                 VNSvOutPortD(pDevice->PortOffset + MAC_REG_TSFOFST, (u32)qwTSFOffset);
586                 VNSvOutPortD(pDevice->PortOffset + MAC_REG_TSFOFST + 4, (u32)(qwTSFOffset >> 32));
587                 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_TFTCTL, TFTCTL_TSFSYNCEN);
588         }
589         return true;
590 }
591
592 /*
593  * Description: Set NIC TSF counter for first Beacon time
594  *              Get NEXTTBTT from adjusted TSF and Beacon Interval
595  *
596  * Parameters:
597  *  In:
598  *      pDevice         - The adapter to be set.
599  *      wBeaconInterval - Beacon Interval
600  *  Out:
601  *      none
602  *
603  * Return Value: true if succeed; otherwise false
604  *
605  */
606 bool CARDbSetBeaconPeriod(struct vnt_private *pDevice,
607                           unsigned short wBeaconInterval)
608 {
609         u64 qwNextTBTT = 0;
610
611         CARDbGetCurrentTSF(pDevice->PortOffset, &qwNextTBTT); //Get Local TSF counter
612
613         qwNextTBTT = CARDqGetNextTBTT(qwNextTBTT, wBeaconInterval);
614
615         // set HW beacon interval
616         VNSvOutPortW(pDevice->PortOffset + MAC_REG_BI, wBeaconInterval);
617         pDevice->wBeaconInterval = wBeaconInterval;
618         // Set NextTBTT
619         VNSvOutPortD(pDevice->PortOffset + MAC_REG_NEXTTBTT, (u32)qwNextTBTT);
620         VNSvOutPortD(pDevice->PortOffset + MAC_REG_NEXTTBTT + 4, (u32)(qwNextTBTT >> 32));
621         MACvRegBitsOn(pDevice->PortOffset, MAC_REG_TFTCTL, TFTCTL_TBTTSYNCEN);
622
623         return true;
624 }
625
626 /*
627  * Description: Card Stop Hardware Tx
628  *
629  * Parameters:
630  *  In:
631  *      pDeviceHandler      - The adapter to be set
632  *      ePktType            - Packet type to stop
633  *  Out:
634  *      none
635  *
636  * Return Value: true if all data packet complete; otherwise false.
637  *
638  */
639 bool CARDbStopTxPacket(struct vnt_private *pDevice, CARD_PKT_TYPE ePktType)
640 {
641
642         if (ePktType == PKT_TYPE_802_11_ALL) {
643                 pDevice->bStopBeacon = true;
644                 pDevice->bStopTx0Pkt = true;
645                 pDevice->bStopDataPkt = true;
646         } else if (ePktType == PKT_TYPE_802_11_BCN) {
647                 pDevice->bStopBeacon = true;
648         } else if (ePktType == PKT_TYPE_802_11_MNG) {
649                 pDevice->bStopTx0Pkt = true;
650         } else if (ePktType == PKT_TYPE_802_11_DATA) {
651                 pDevice->bStopDataPkt = true;
652         }
653
654         if (pDevice->bStopBeacon == true) {
655                 if (pDevice->bIsBeaconBufReadySet == true) {
656                         if (pDevice->cbBeaconBufReadySetCnt < WAIT_BEACON_TX_DOWN_TMO) {
657                                 pDevice->cbBeaconBufReadySetCnt++;
658                                 return false;
659                         }
660                 }
661                 pDevice->bIsBeaconBufReadySet = false;
662                 pDevice->cbBeaconBufReadySetCnt = 0;
663                 MACvRegBitsOff(pDevice->PortOffset, MAC_REG_TCR, TCR_AUTOBCNTX);
664         }
665         // wait all TD0 complete
666         if (pDevice->bStopTx0Pkt == true) {
667                 if (pDevice->iTDUsed[TYPE_TXDMA0] != 0)
668                         return false;
669         }
670         // wait all Data TD complete
671         if (pDevice->bStopDataPkt == true) {
672                 if (pDevice->iTDUsed[TYPE_AC0DMA] != 0)
673                         return false;
674         }
675
676         return true;
677 }
678
679 /*
680  * Description: Card Start Hardware Tx
681  *
682  * Parameters:
683  *  In:
684  *      pDeviceHandler      - The adapter to be set
685  *      ePktType            - Packet type to start
686  *  Out:
687  *      none
688  *
689  * Return Value: true if success; false if failed.
690  *
691  */
692 bool CARDbStartTxPacket(struct vnt_private *pDevice, CARD_PKT_TYPE ePktType)
693 {
694
695         if (ePktType == PKT_TYPE_802_11_ALL) {
696                 pDevice->bStopBeacon = false;
697                 pDevice->bStopTx0Pkt = false;
698                 pDevice->bStopDataPkt = false;
699         } else if (ePktType == PKT_TYPE_802_11_BCN) {
700                 pDevice->bStopBeacon = false;
701         } else if (ePktType == PKT_TYPE_802_11_MNG) {
702                 pDevice->bStopTx0Pkt = false;
703         } else if (ePktType == PKT_TYPE_802_11_DATA) {
704                 pDevice->bStopDataPkt = false;
705         }
706
707         if ((pDevice->bStopBeacon == false) &&
708             (pDevice->bBeaconBufReady == true) &&
709             (pDevice->eOPMode == OP_MODE_ADHOC)) {
710                 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_TCR, TCR_AUTOBCNTX);
711         }
712
713         return true;
714 }
715
716 /*
717  * Description: Card Set BSSID value
718  *
719  * Parameters:
720  *  In:
721  *      pDeviceHandler      - The adapter to be set
722  *      pbyBSSID            - pointer to BSSID field
723  *      bAdhoc              - flag to indicate IBSS
724  *  Out:
725  *      none
726  *
727  * Return Value: true if success; false if failed.
728  *
729  */
730 bool CARDbSetBSSID(struct vnt_private *pDevice,
731                    unsigned char *pbyBSSID, CARD_OP_MODE eOPMode)
732 {
733
734         MACvWriteBSSIDAddress(pDevice->PortOffset, pbyBSSID);
735         memcpy(pDevice->abyBSSID, pbyBSSID, WLAN_BSSID_LEN);
736         if (eOPMode == OP_MODE_ADHOC)
737                 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_HOSTCR, HOSTCR_ADHOC);
738         else
739                 MACvRegBitsOff(pDevice->PortOffset, MAC_REG_HOSTCR, HOSTCR_ADHOC);
740
741         if (eOPMode == OP_MODE_AP)
742                 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_HOSTCR, HOSTCR_AP);
743         else
744                 MACvRegBitsOff(pDevice->PortOffset, MAC_REG_HOSTCR, HOSTCR_AP);
745
746         if (eOPMode == OP_MODE_UNKNOWN) {
747                 MACvRegBitsOff(pDevice->PortOffset, MAC_REG_RCR, RCR_BSSID);
748                 pDevice->bBSSIDFilter = false;
749                 pDevice->byRxMode &= ~RCR_BSSID;
750                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "wcmd: rx_mode = %x\n", pDevice->byRxMode);
751         } else {
752                 if (is_zero_ether_addr(pDevice->abyBSSID) == false) {
753                         MACvRegBitsOn(pDevice->PortOffset, MAC_REG_RCR, RCR_BSSID);
754                         pDevice->bBSSIDFilter = true;
755                         pDevice->byRxMode |= RCR_BSSID;
756                 }
757                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "wmgr: rx_mode = %x\n", pDevice->byRxMode);
758         }
759         // Adopt BSS state in Adapter Device Object
760         pDevice->eOPMode = eOPMode;
761         return true;
762 }
763
764 /*
765  * Description: Card indicate status
766  *
767  * Parameters:
768  *  In:
769  *      pDeviceHandler      - The adapter to be set
770  *      eStatus             - Status
771  *  Out:
772  *      none
773  *
774  * Return Value: true if success; false if failed.
775  *
776  */
777
778 /*
779  * Description: Save Assoc info. contain in assoc. response frame
780  *
781  * Parameters:
782  *  In:
783  *      pDevice             - The adapter to be set
784  *      wCapabilityInfo     - Capability information
785  *      wStatus             - Status code
786  *      wAID                - Assoc. ID
787  *      uLen                - Length of IEs
788  *      pbyIEs              - pointer to IEs
789  *  Out:
790  *      none
791  *
792  * Return Value: true if succeed; otherwise false
793  *
794  */
795 bool CARDbSetTxDataRate(
796         struct vnt_private *pDevice,
797         unsigned short wDataRate
798 )
799 {
800
801         pDevice->wCurrentRate = wDataRate;
802         return true;
803 }
804
805 /*+
806  *
807  * Routine Description:
808  *      Consider to power down when no more packets to tx or rx.
809  *
810  * Parameters:
811  *  In:
812  *      pDevice             - The adapter to be set
813  *  Out:
814  *      none
815  *
816  * Return Value: true if power down success; otherwise false
817  *
818  -*/
819 bool
820 CARDbPowerDown(
821         struct vnt_private *pDevice
822 )
823 {
824         unsigned int uIdx;
825
826         // check if already in Doze mode
827         if (MACbIsRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_PS))
828                 return true;
829
830         // Froce PSEN on
831         MACvRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_PSEN);
832
833         // check if all TD are empty,
834
835         for (uIdx = 0; uIdx < TYPE_MAXTD; uIdx++) {
836                 if (pDevice->iTDUsed[uIdx] != 0)
837                         return false;
838         }
839
840         MACvRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_GO2DOZE);
841         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Go to Doze ZZZZZZZZZZZZZZZ\n");
842         return true;
843 }
844
845 /*
846  * Description: Turn off Radio power
847  *
848  * Parameters:
849  *  In:
850  *      pDevice         - The adapter to be turned off
851  *  Out:
852  *      none
853  *
854  * Return Value: true if success; otherwise false
855  *
856  */
857 bool CARDbRadioPowerOff(struct vnt_private *pDevice)
858 {
859         bool bResult = true;
860
861         if (pDevice->bRadioOff == true)
862                 return true;
863
864         switch (pDevice->byRFType) {
865         case RF_RFMD2959:
866                 MACvWordRegBitsOff(pDevice->PortOffset, MAC_REG_SOFTPWRCTL, SOFTPWRCTL_TXPEINV);
867                 MACvWordRegBitsOn(pDevice->PortOffset, MAC_REG_SOFTPWRCTL, SOFTPWRCTL_SWPE1);
868                 break;
869
870         case RF_AIROHA:
871         case RF_AL2230S:
872         case RF_AIROHA7230: //RobertYu:20050104
873                 MACvWordRegBitsOff(pDevice->PortOffset, MAC_REG_SOFTPWRCTL, SOFTPWRCTL_SWPE2);
874                 MACvWordRegBitsOff(pDevice->PortOffset, MAC_REG_SOFTPWRCTL, SOFTPWRCTL_SWPE3);
875                 break;
876
877         }
878
879         MACvRegBitsOff(pDevice->PortOffset, MAC_REG_HOSTCR, HOSTCR_RXON);
880
881         BBvSetDeepSleep(pDevice->PortOffset, pDevice->byLocalID);
882
883         pDevice->bRadioOff = true;
884         //2007-0409-03,<Add> by chester
885         pr_debug("chester power off\n");
886         MACvRegBitsOn(pDevice->PortOffset, MAC_REG_GPIOCTL0, LED_ACTSET);  //LED issue
887         return bResult;
888 }
889
890 /*
891  * Description: Turn on Radio power
892  *
893  * Parameters:
894  *  In:
895  *      pDevice         - The adapter to be turned on
896  *  Out:
897  *      none
898  *
899  * Return Value: true if success; otherwise false
900  *
901  */
902 bool CARDbRadioPowerOn(struct vnt_private *pDevice)
903 {
904         bool bResult = true;
905
906         pr_debug("chester power on\n");
907         if (pDevice->bRadioControlOff == true) {
908                 if (pDevice->bHWRadioOff == true)
909                         pr_debug("chester bHWRadioOff\n");
910                 if (pDevice->bRadioControlOff == true)
911                         pr_debug("chester bRadioControlOff\n");
912                 return false; }
913
914         if (pDevice->bRadioOff == false) {
915                 pr_debug("chester pbRadioOff\n");
916                 return true; }
917
918         BBvExitDeepSleep(pDevice->PortOffset, pDevice->byLocalID);
919
920         MACvRegBitsOn(pDevice->PortOffset, MAC_REG_HOSTCR, HOSTCR_RXON);
921
922         switch (pDevice->byRFType) {
923         case RF_RFMD2959:
924                 MACvWordRegBitsOn(pDevice->PortOffset, MAC_REG_SOFTPWRCTL, SOFTPWRCTL_TXPEINV);
925                 MACvWordRegBitsOff(pDevice->PortOffset, MAC_REG_SOFTPWRCTL, SOFTPWRCTL_SWPE1);
926                 break;
927
928         case RF_AIROHA:
929         case RF_AL2230S:
930         case RF_AIROHA7230: //RobertYu:20050104
931                 MACvWordRegBitsOn(pDevice->PortOffset, MAC_REG_SOFTPWRCTL, (SOFTPWRCTL_SWPE2 |
932                                                                             SOFTPWRCTL_SWPE3));
933                 break;
934
935         }
936
937         pDevice->bRadioOff = false;
938 //  2007-0409-03,<Add> by chester
939         pr_debug("chester power on\n");
940         MACvRegBitsOff(pDevice->PortOffset, MAC_REG_GPIOCTL0, LED_ACTSET); //LED issue
941         return bResult;
942 }
943
944 bool CARDbRemoveKey(struct vnt_private *pDevice, unsigned char *pbyBSSID)
945 {
946
947         KeybRemoveAllKey(&(pDevice->sKey), pbyBSSID, pDevice->PortOffset);
948         return true;
949 }
950
951 /*
952  *
953  * Description:
954  *    Add BSSID in PMKID Candidate list.
955  *
956  * Parameters:
957  *  In:
958  *      hDeviceContext - device structure point
959  *      pbyBSSID - BSSID address for adding
960  *      wRSNCap - BSS's RSN capability
961  *  Out:
962  *      none
963  *
964  * Return Value: none.
965  *
966  -*/
967 bool
968 CARDbAdd_PMKID_Candidate(
969         struct vnt_private *pDevice,
970         unsigned char *pbyBSSID,
971         bool bRSNCapExist,
972         unsigned short wRSNCap
973 )
974 {
975         struct pmkid_candidate *pCandidateList;
976         unsigned int ii = 0;
977
978         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "bAdd_PMKID_Candidate START: (%d)\n", (int)pDevice->gsPMKIDCandidate.NumCandidates);
979
980         if (pDevice->gsPMKIDCandidate.NumCandidates >= MAX_PMKIDLIST) {
981                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "vFlush_PMKID_Candidate: 3\n");
982                 memset(&pDevice->gsPMKIDCandidate, 0, sizeof(SPMKIDCandidateEvent));
983         }
984
985         for (ii = 0; ii < 6; ii++)
986                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "%02X ", *(pbyBSSID + ii));
987
988         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "\n");
989
990         // Update Old Candidate
991         for (ii = 0; ii < pDevice->gsPMKIDCandidate.NumCandidates; ii++) {
992                 pCandidateList = &pDevice->gsPMKIDCandidate.CandidateList[ii];
993                 if (!memcmp(pCandidateList->BSSID, pbyBSSID, ETH_ALEN)) {
994                         if (bRSNCapExist && (wRSNCap & BIT0))
995                                 pCandidateList->Flags |= NDIS_802_11_PMKID_CANDIDATE_PREAUTH_ENABLED;
996                         else
997                                 pCandidateList->Flags &= ~(NDIS_802_11_PMKID_CANDIDATE_PREAUTH_ENABLED);
998
999                         return true;
1000                 }
1001         }
1002
1003         // New Candidate
1004         pCandidateList = &pDevice->gsPMKIDCandidate.CandidateList[pDevice->gsPMKIDCandidate.NumCandidates];
1005         if (bRSNCapExist && (wRSNCap & BIT0))
1006                 pCandidateList->Flags |= NDIS_802_11_PMKID_CANDIDATE_PREAUTH_ENABLED;
1007         else
1008                 pCandidateList->Flags &= ~(NDIS_802_11_PMKID_CANDIDATE_PREAUTH_ENABLED);
1009
1010         memcpy(pCandidateList->BSSID, pbyBSSID, ETH_ALEN);
1011         pDevice->gsPMKIDCandidate.NumCandidates++;
1012         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "NumCandidates:%d\n", (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         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "BASIC RATE: %X\n", pDevice->wBasicRate);
1563
1564         if (!CARDbIsOFDMinBasicRate((void *)pDevice)) {
1565                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "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                         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "CARDwGetOFDMControlRate : %d\n", ui);
1573                         return (unsigned short)ui;
1574                 }
1575                 ui--;
1576         }
1577         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "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         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Card:Update Next TBTT[%8llx]\n", qwTSF);
2021 }