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