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