2 * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
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.
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.
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.
20 * Purpose: Provide functions to setup NIC operation mode
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 * CARDbSetBasicRate - Set Basic Tx Rate
28 * CARDbIsOFDMinBasicRate - Check if any OFDM rate is in BasicRateSet
29 * CARDvSetLoopbackMode - Set Loopback mode
30 * CARDbSoftwareReset - Sortware reset NIC
31 * CARDqGetTSFOffset - Calculate TSFOffset
32 * CARDbGetCurrentTSF - Read Current NIC TSF counter
33 * CARDqGetNextTBTT - Calculate Next Beacon TSF counter
34 * CARDvSetFirstNextTBTT - Set NIC Beacon time
35 * CARDvUpdateNextTBTT - Sync. NIC Beacon time
36 * CARDbRadioPowerOff - Turn Off NIC Radio Power
37 * CARDbRadioPowerOn - Turn On NIC Radio Power
38 * CARDbSetWEPMode - Set NIC Wep mode
39 * CARDbSetTxPower - Set NIC tx power
42 * 06-10-2003 Bryan YC Fan: Re-write codes to support VT3253 spec.
43 * 08-26-2003 Kyle Hsu: Modify the definition type of dwIoBase.
44 * 09-01-2003 Bryan YC Fan: Add vUpdateIFS().
63 //static int msglevel =MSG_LEVEL_DEBUG;
64 static int msglevel =MSG_LEVEL_INFO;
66 //const u16 cwRXBCNTSFOff[MAX_RATE] =
67 //{17, 34, 96, 192, 34, 23, 17, 11, 8, 5, 4, 3};
69 static const u16 cwRXBCNTSFOff[MAX_RATE] =
70 {192, 96, 34, 17, 34, 23, 17, 11, 8, 5, 4, 3};
73 * Description: Set NIC media channel
77 * pDevice - The adapter to be set
78 * connection_channel - Channel to be set
82 void CARDbSetMediaChannel(struct vnt_private *priv, u32 connection_channel)
85 if (priv->byBBType == BB_TYPE_11A) {
86 if ((connection_channel < (CB_MAX_CHANNEL_24G + 1)) ||
87 (connection_channel > CB_MAX_CHANNEL))
88 connection_channel = (CB_MAX_CHANNEL_24G + 1);
90 if ((connection_channel > CB_MAX_CHANNEL_24G) ||
91 (connection_channel == 0))
92 connection_channel = 1;
96 MACvRegBitsOn(priv, MAC_REG_MACCR, MACCR_CLRNAV);
98 /* Set Channel[7] = 0 to tell H/W channel is changing now. */
99 MACvRegBitsOff(priv, MAC_REG_CHANNEL, 0xb0);
101 CONTROLnsRequestOut(priv, MESSAGE_TYPE_SELECT_CHANNLE,
102 connection_channel, 0, 0, NULL);
104 if (priv->byBBType == BB_TYPE_11A) {
105 priv->byCurPwr = 0xff;
107 priv->abyOFDMAPwrTbl[connection_channel-15], RATE_54M);
108 } else if (priv->byBBType == BB_TYPE_11G) {
109 priv->byCurPwr = 0xff;
111 priv->abyOFDMPwrTbl[connection_channel-1], RATE_54M);
113 priv->byCurPwr = 0xff;
115 priv->abyCCKPwrTbl[connection_channel-1], RATE_1M);
118 ControlvWriteByte(priv, MESSAGE_REQUEST_MACREG, MAC_REG_CHANNEL,
119 (u8)(connection_channel|0x80));
123 * Description: Get CCK mode basic rate
127 * pDevice - The adapter to be set
128 * wRateIdx - Receiving data rate
132 * Return Value: response Control frame rate
135 static u16 swGetCCKControlRate(struct vnt_private *pDevice, u16 wRateIdx)
139 while (ui > RATE_1M) {
140 if (pDevice->wBasicRate & (1 << ui))
149 * Description: Get OFDM mode basic rate
153 * pDevice - The adapter to be set
154 * wRateIdx - Receiving data rate
158 * Return Value: response Control frame rate
161 static u16 swGetOFDMControlRate(struct vnt_private *pDevice, u16 wRateIdx)
165 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"BASIC RATE: %X\n",
166 pDevice->wBasicRate);
168 if (!CARDbIsOFDMinBasicRate(pDevice)) {
169 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO
170 "swGetOFDMControlRate:(NO OFDM) %d\n", wRateIdx);
171 if (wRateIdx > RATE_24M)
176 while (ui > RATE_11M) {
177 if (pDevice->wBasicRate & (1 << ui)) {
178 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO
179 "swGetOFDMControlRate: %d\n", ui);
185 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"swGetOFDMControlRate: 6M\n");
191 * Description: Calculate TxRate and RsvTime fields for RSPINF in OFDM mode.
196 * byPktType - Tx Packet type
198 * pbyTxRate - pointer to RSPINF TxRate field
199 * pbyRsvTime - pointer to RSPINF RsvTime field
205 CARDvCalculateOFDMRParameter (
214 if (byBBType == BB_TYPE_11A) {//5GHZ
225 if (byBBType == BB_TYPE_11A) {//5GHZ
236 if (byBBType == BB_TYPE_11A) {//5GHZ
247 if (byBBType == BB_TYPE_11A) {//5GHZ
258 if (byBBType == BB_TYPE_11A) {//5GHZ
269 if (byBBType == BB_TYPE_11A) {//5GHZ
280 if (byBBType == BB_TYPE_11A) {//5GHZ
292 if (byBBType == BB_TYPE_11A) {//5GHZ
305 * Description: Set RSPINF
309 * pDevice - The adapter to be set
313 * Return Value: None.
316 void CARDvSetRSPINF(struct vnt_private *pDevice, u8 byBBType)
318 struct vnt_phy_field phy[4];
319 u8 abyTxRate[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0}; /* For OFDM */
320 u8 abyRsvTime[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
325 BBvCalculateParameter(pDevice, 14,
326 swGetCCKControlRate(pDevice, RATE_1M), PK_TYPE_11B, &phy[0]);
329 BBvCalculateParameter(pDevice, 14,
330 swGetCCKControlRate(pDevice, RATE_2M), PK_TYPE_11B, &phy[1]);
333 BBvCalculateParameter(pDevice, 14,
334 swGetCCKControlRate(pDevice, RATE_5M), PK_TYPE_11B, &phy[2]);
337 BBvCalculateParameter(pDevice, 14,
338 swGetCCKControlRate(pDevice, RATE_11M), PK_TYPE_11B, &phy[3]);
341 CARDvCalculateOFDMRParameter (RATE_6M,
347 CARDvCalculateOFDMRParameter (RATE_9M,
353 CARDvCalculateOFDMRParameter (RATE_12M,
359 CARDvCalculateOFDMRParameter (RATE_18M,
365 CARDvCalculateOFDMRParameter (RATE_24M,
371 CARDvCalculateOFDMRParameter (swGetOFDMControlRate(pDevice, RATE_36M),
377 CARDvCalculateOFDMRParameter (swGetOFDMControlRate(pDevice, RATE_48M),
383 CARDvCalculateOFDMRParameter (swGetOFDMControlRate(pDevice, RATE_54M),
389 CARDvCalculateOFDMRParameter (swGetOFDMControlRate(pDevice, RATE_54M),
394 put_unaligned(phy[0].len, (u16 *)&abyData[0]);
395 abyData[2] = phy[0].signal;
396 abyData[3] = phy[0].service;
398 put_unaligned(phy[1].len, (u16 *)&abyData[4]);
399 abyData[6] = phy[1].signal;
400 abyData[7] = phy[1].service;
402 put_unaligned(phy[2].len, (u16 *)&abyData[8]);
403 abyData[10] = phy[2].signal;
404 abyData[11] = phy[2].service;
406 put_unaligned(phy[3].len, (u16 *)&abyData[12]);
407 abyData[14] = phy[3].signal;
408 abyData[15] = phy[3].service;
410 for (i = 0; i < 9; i++) {
411 abyData[16+i*2] = abyTxRate[i];
412 abyData[16+i*2+1] = abyRsvTime[i];
415 CONTROLnsRequestOut(pDevice,
418 MESSAGE_REQUEST_MACREG,
425 * Description: Update IFS
429 * pDevice - The adapter to be set
433 * Return Value: None.
436 void vUpdateIFS(struct vnt_private *pDevice)
441 if (pDevice->byPacketType==PK_TYPE_11A) {//0000 0000 0000 0000,11a
442 pDevice->uSlot = C_SLOT_SHORT;
443 pDevice->uSIFS = C_SIFS_A;
444 pDevice->uDIFS = C_SIFS_A + 2*C_SLOT_SHORT;
445 pDevice->uCwMin = C_CWMIN_A;
448 else if (pDevice->byPacketType==PK_TYPE_11B) {//0000 0001 0000 0000,11b
449 pDevice->uSlot = C_SLOT_LONG;
450 pDevice->uSIFS = C_SIFS_BG;
451 pDevice->uDIFS = C_SIFS_BG + 2*C_SLOT_LONG;
452 pDevice->uCwMin = C_CWMIN_B;
455 else {// PK_TYPE_11GA & PK_TYPE_11GB
457 bool bOFDMRate = false;
459 PWLAN_IE_SUPP_RATES pItemRates = NULL;
461 pDevice->uSIFS = C_SIFS_BG;
462 if (pDevice->bShortSlotTime) {
463 pDevice->uSlot = C_SLOT_SHORT;
465 pDevice->uSlot = C_SLOT_LONG;
467 pDevice->uDIFS = C_SIFS_BG + 2*pDevice->uSlot;
469 pItemRates = (PWLAN_IE_SUPP_RATES)pDevice->vnt_mgmt.abyCurrSuppRates;
470 for (ii = 0; ii < pItemRates->len; ii++) {
471 byRate = (u8)(pItemRates->abyRates[ii]&0x7F);
472 if (RATEwGetRateIdx(byRate) > RATE_11M) {
477 if (bOFDMRate == false) {
478 pItemRates = (PWLAN_IE_SUPP_RATES)pDevice->vnt_mgmt
479 .abyCurrExtSuppRates;
480 for (ii = 0; ii < pItemRates->len; ii++) {
481 byRate = (u8)(pItemRates->abyRates[ii]&0x7F);
482 if (RATEwGetRateIdx(byRate) > RATE_11M) {
488 if (bOFDMRate == true) {
489 pDevice->uCwMin = C_CWMIN_A;
492 pDevice->uCwMin = C_CWMIN_B;
497 pDevice->uCwMax = C_CWMAX;
498 pDevice->uEIFS = C_EIFS;
500 byData[0] = (u8)pDevice->uSIFS;
501 byData[1] = (u8)pDevice->uDIFS;
502 byData[2] = (u8)pDevice->uEIFS;
503 byData[3] = (u8)pDevice->uSlot;
504 CONTROLnsRequestOut(pDevice,
507 MESSAGE_REQUEST_MACREG,
511 byMaxMin |= 0xA0;//1010 1111,C_CWMAX = 1023
512 CONTROLnsRequestOut(pDevice,
515 MESSAGE_REQUEST_MACREG,
520 void CARDvUpdateBasicTopRate(struct vnt_private *pDevice)
522 u8 byTopOFDM = RATE_24M, byTopCCK = RATE_1M;
525 //Determines the highest basic rate.
526 for (ii = RATE_54M; ii >= RATE_6M; ii --) {
527 if ( (pDevice->wBasicRate) & ((u16)(1<<ii)) ) {
532 pDevice->byTopOFDMBasicRate = byTopOFDM;
534 for (ii = RATE_11M;; ii --) {
535 if ( (pDevice->wBasicRate) & ((u16)(1<<ii)) ) {
542 pDevice->byTopCCKBasicRate = byTopCCK;
546 * Description: Set NIC Tx Basic Rate
550 * pDevice - The adapter to be set
551 * wBasicRate - Basic Rate to be set
555 * Return Value: true if succeeded; false if failed.
558 void CARDbAddBasicRate(struct vnt_private *pDevice, u16 wRateIdx)
560 u16 wRate = (1 << wRateIdx);
562 pDevice->wBasicRate |= wRate;
564 //Determines the highest basic rate.
565 CARDvUpdateBasicTopRate(pDevice);
568 int CARDbIsOFDMinBasicRate(struct vnt_private *pDevice)
572 for (ii = RATE_54M; ii >= RATE_6M; ii --) {
573 if ((pDevice->wBasicRate) & ((u16)(1<<ii)))
579 u8 CARDbyGetPktType(struct vnt_private *pDevice)
582 if (pDevice->byBBType == BB_TYPE_11A || pDevice->byBBType == BB_TYPE_11B) {
583 return (u8)pDevice->byBBType;
585 else if (CARDbIsOFDMinBasicRate(pDevice)) {
594 * Description: Calculate TSF offset of two TSF input
595 * Get TSF Offset from RxBCN's TSF and local TSF
599 * pDevice - The adapter to be sync.
600 * qwTSF1 - Rx BCN's TSF
605 * Return Value: TSF Offset value
608 u64 CARDqGetTSFOffset(u8 byRxRate, u64 qwTSF1, u64 qwTSF2)
611 u16 wRxBcnTSFOffst = 0;
613 wRxBcnTSFOffst = cwRXBCNTSFOff[byRxRate % MAX_RATE];
615 qwTSF2 += (u64)wRxBcnTSFOffst;
617 qwTSFOffset = qwTSF1 - qwTSF2;
623 * Description: Sync. TSF counter to BSS
624 * Get TSF offset and write to HW
628 * pDevice - The adapter to be sync.
629 * qwBSSTimestamp - Rx BCN's TSF
630 * qwLocalTSF - Local TSF
637 void CARDvAdjustTSF(struct vnt_private *pDevice, u8 byRxRate,
638 u64 qwBSSTimestamp, u64 qwLocalTSF)
643 qwTSFOffset = CARDqGetTSFOffset(byRxRate, qwBSSTimestamp, qwLocalTSF);
645 // HW's TSF add TSF Offset reg
647 pbyData[0] = (u8)qwTSFOffset;
648 pbyData[1] = (u8)(qwTSFOffset >> 8);
649 pbyData[2] = (u8)(qwTSFOffset >> 16);
650 pbyData[3] = (u8)(qwTSFOffset >> 24);
651 pbyData[4] = (u8)(qwTSFOffset >> 32);
652 pbyData[5] = (u8)(qwTSFOffset >> 40);
653 pbyData[6] = (u8)(qwTSFOffset >> 48);
654 pbyData[7] = (u8)(qwTSFOffset >> 56);
656 CONTROLnsRequestOut(pDevice,
657 MESSAGE_TYPE_SET_TSFTBTT,
666 * Description: Read NIC TSF counter
667 * Get local TSF counter
671 * pDevice - The adapter to be read
673 * qwCurrTSF - Current TSF counter
675 * Return Value: true if success; otherwise false
678 bool CARDbGetCurrentTSF(struct vnt_private *pDevice, u64 *pqwCurrTSF)
681 *pqwCurrTSF = pDevice->qwCurrTSF;
687 * Description: Clear NIC TSF counter
688 * Clear local TSF counter
692 * pDevice - The adapter to be read
694 * Return Value: true if success; otherwise false
697 bool CARDbClearCurrentTSF(struct vnt_private *pDevice)
700 MACvRegBitsOn(pDevice, MAC_REG_TFTCTL, TFTCTL_TSFCNTRST);
702 pDevice->qwCurrTSF = 0;
708 * Description: Read NIC TSF counter
709 * Get NEXTTBTT from adjusted TSF and Beacon Interval
713 * qwTSF - Current TSF counter
714 * wbeaconInterval - Beacon Interval
716 * qwCurrTSF - Current TSF counter
718 * Return Value: TSF value of next Beacon
721 u64 CARDqGetNextTBTT(u64 qwTSF, u16 wBeaconInterval)
725 uBeaconInterval = wBeaconInterval * 1024;
728 * ((local_current_TSF / beacon_interval) + 1) * beacon_interval
730 if (uBeaconInterval) {
731 do_div(qwTSF, uBeaconInterval);
733 qwTSF *= uBeaconInterval;
740 * Description: Set NIC TSF counter for first Beacon time
741 * Get NEXTTBTT from adjusted TSF and Beacon Interval
746 * wBeaconInterval - Beacon Interval
753 void CARDvSetFirstNextTBTT(struct vnt_private *pDevice, u16 wBeaconInterval)
758 CARDbClearCurrentTSF(pDevice);
759 //CARDbGetCurrentTSF(pDevice, &qwNextTBTT); //Get Local TSF counter
760 qwNextTBTT = CARDqGetNextTBTT(qwNextTBTT, wBeaconInterval);
763 pbyData[0] = (u8)qwNextTBTT;
764 pbyData[1] = (u8)(qwNextTBTT >> 8);
765 pbyData[2] = (u8)(qwNextTBTT >> 16);
766 pbyData[3] = (u8)(qwNextTBTT >> 24);
767 pbyData[4] = (u8)(qwNextTBTT >> 32);
768 pbyData[5] = (u8)(qwNextTBTT >> 40);
769 pbyData[6] = (u8)(qwNextTBTT >> 48);
770 pbyData[7] = (u8)(qwNextTBTT >> 56);
772 CONTROLnsRequestOut(pDevice,
773 MESSAGE_TYPE_SET_TSFTBTT,
774 MESSAGE_REQUEST_TBTT,
784 * Description: Sync NIC TSF counter for Beacon time
785 * Get NEXTTBTT and write to HW
789 * pDevice - The adapter to be set
790 * qwTSF - Current TSF counter
791 * wBeaconInterval - Beacon Interval
798 void CARDvUpdateNextTBTT(struct vnt_private *pDevice, u64 qwTSF,
803 qwTSF = CARDqGetNextTBTT(qwTSF, wBeaconInterval);
807 pbyData[0] = (u8)qwTSF;
808 pbyData[1] = (u8)(qwTSF >> 8);
809 pbyData[2] = (u8)(qwTSF >> 16);
810 pbyData[3] = (u8)(qwTSF >> 24);
811 pbyData[4] = (u8)(qwTSF >> 32);
812 pbyData[5] = (u8)(qwTSF >> 40);
813 pbyData[6] = (u8)(qwTSF >> 48);
814 pbyData[7] = (u8)(qwTSF >> 56);
816 CONTROLnsRequestOut(pDevice,
817 MESSAGE_TYPE_SET_TSFTBTT,
818 MESSAGE_REQUEST_TBTT,
824 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO
825 "Card:Update Next TBTT[%8lx]\n", (unsigned long)qwTSF);
831 * Description: Turn off Radio power
835 * pDevice - The adapter to be turned off
839 * Return Value: true if success; otherwise false
842 int CARDbRadioPowerOff(struct vnt_private *pDevice)
846 //if (pDevice->bRadioOff == true)
849 pDevice->bRadioOff = true;
851 switch (pDevice->byRFType) {
855 case RF_VT3226: //RobertYu:20051111
857 case RF_VT3342A0: //RobertYu:20060609
858 MACvRegBitsOff(pDevice, MAC_REG_SOFTPWRCTL, (SOFTPWRCTL_SWPE2 | SOFTPWRCTL_SWPE3));
862 MACvRegBitsOff(pDevice, MAC_REG_HOSTCR, HOSTCR_RXON);
864 BBvSetDeepSleep(pDevice);
870 * Description: Turn on Radio power
874 * pDevice - The adapter to be turned on
878 * Return Value: true if success; otherwise false
881 int CARDbRadioPowerOn(struct vnt_private *pDevice)
885 if ((pDevice->bHWRadioOff == true) || (pDevice->bRadioControlOff == true)) {
889 //if (pDevice->bRadioOff == false)
892 pDevice->bRadioOff = false;
894 BBvExitDeepSleep(pDevice);
896 MACvRegBitsOn(pDevice, MAC_REG_HOSTCR, HOSTCR_RXON);
898 switch (pDevice->byRFType) {
902 case RF_VT3226: //RobertYu:20051111
904 case RF_VT3342A0: //RobertYu:20060609
905 MACvRegBitsOn(pDevice, MAC_REG_SOFTPWRCTL, (SOFTPWRCTL_SWPE2 | SOFTPWRCTL_SWPE3));
912 void CARDvSetBSSMode(struct vnt_private *pDevice)
914 // Set BB and packet type at the same time.//{{RobertYu:20050222, AL7230 have two TX PA output, only connet to b/g now
915 // so in 11a mode need to set the MAC Reg0x4C to 11b/g mode to turn on PA
916 if( (pDevice->byRFType == RF_AIROHA7230 ) && (pDevice->byBBType == BB_TYPE_11A) )
918 MACvSetBBType(pDevice, BB_TYPE_11G);
922 MACvSetBBType(pDevice, pDevice->byBBType);
924 pDevice->byPacketType = CARDbyGetPktType(pDevice);
926 if (pDevice->byBBType == BB_TYPE_11A) {
927 ControlvWriteByte(pDevice, MESSAGE_REQUEST_BBREG, 0x88, 0x03);
928 } else if (pDevice->byBBType == BB_TYPE_11B) {
929 ControlvWriteByte(pDevice, MESSAGE_REQUEST_BBREG, 0x88, 0x02);
930 } else if (pDevice->byBBType == BB_TYPE_11G) {
931 ControlvWriteByte(pDevice, MESSAGE_REQUEST_BBREG, 0x88, 0x08);
935 CARDvSetRSPINF(pDevice, (u8)pDevice->byBBType);
937 if ( pDevice->byBBType == BB_TYPE_11A ) {
938 //request by Jack 2005-04-26
939 if (pDevice->byRFType == RF_AIROHA7230) {
940 pDevice->abyBBVGA[0] = 0x20;
941 ControlvWriteByte(pDevice, MESSAGE_REQUEST_BBREG, 0xE7, pDevice->abyBBVGA[0]);
943 pDevice->abyBBVGA[2] = 0x10;
944 pDevice->abyBBVGA[3] = 0x10;
946 //request by Jack 2005-04-26
947 if (pDevice->byRFType == RF_AIROHA7230) {
948 pDevice->abyBBVGA[0] = 0x1C;
949 ControlvWriteByte(pDevice, MESSAGE_REQUEST_BBREG, 0xE7, pDevice->abyBBVGA[0]);
951 pDevice->abyBBVGA[2] = 0x0;
952 pDevice->abyBBVGA[3] = 0x0;