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: rxtx.c
20  *
21  * Purpose: handle WMAC/802.3/802.11 rx & tx functions
22  *
23  * Author: Lyndon Chen
24  *
25  * Date: May 20, 2003
26  *
27  * Functions:
28  *      s_vGenerateTxParameter - Generate tx dma required parameter.
29  *      vGenerateMACHeader - Translate 802.3 to 802.11 header
30  *      cbGetFragCount - Caculate fragment number count
31  *      csBeacon_xmit - beacon tx function
32  *      csMgmt_xmit - management tx function
33  *      s_cbFillTxBufHead - fulfill tx dma buffer header
34  *      s_uGetDataDuration - get tx data required duration
35  *      s_uFillDataHead- fulfill tx data duration header
36  *      s_uGetRTSCTSDuration- get rtx/cts required duration
37  *      s_uGetRTSCTSRsvTime- get rts/cts reserved time
38  *      s_uGetTxRsvTime- get frame reserved time
39  *      s_vFillCTSHead- fulfill CTS ctl header
40  *      s_vFillFragParameter- Set fragment ctl parameter.
41  *      s_vFillRTSHead- fulfill RTS ctl header
42  *      s_vFillTxKey- fulfill tx encrypt key
43  *      s_vSWencryption- Software encrypt header
44  *      vDMA0_tx_80211- tx 802.11 frame via dma0
45  *      vGenerateFIFOHeader- Generate tx FIFO ctl header
46  *
47  * Revision History:
48  *
49  */
50 
51 #include "device.h"
52 #include "rxtx.h"
53 #include "tether.h"
54 #include "card.h"
55 #include "bssdb.h"
56 #include "mac.h"
57 #include "baseband.h"
58 #include "michael.h"
59 #include "tkip.h"
60 #include "tcrc.h"
61 #include "wctl.h"
62 #include "wroute.h"
63 #include "hostap.h"
64 #include "rf.h"
65 
66 /*---------------------  Static Definitions -------------------------*/
67 
68 /*---------------------  Static Classes  ----------------------------*/
69 
70 /*---------------------  Static Variables  --------------------------*/
71 //static int          msglevel                =MSG_LEVEL_DEBUG;
72 static int          msglevel                =MSG_LEVEL_INFO;
73 
74 #define	PLICE_DEBUG
75 
76 
77 /*---------------------  Static Functions  --------------------------*/
78 
79 /*---------------------  Static Definitions -------------------------*/
80 #define CRITICAL_PACKET_LEN      256    // if packet size < 256 -> in-direct send
81                                         //    packet size >= 256 -> direct send
82 
83 const unsigned short wTimeStampOff[2][MAX_RATE] = {
84         {384, 288, 226, 209, 54, 43, 37, 31, 28, 25, 24, 23}, // Long Preamble
85         {384, 192, 130, 113, 54, 43, 37, 31, 28, 25, 24, 23}, // Short Preamble
86     };
87 
88 const unsigned short wFB_Opt0[2][5] = {
89         {RATE_12M, RATE_18M, RATE_24M, RATE_36M, RATE_48M}, // fallback_rate0
90         {RATE_12M, RATE_12M, RATE_18M, RATE_24M, RATE_36M}, // fallback_rate1
91     };
92 const unsigned short wFB_Opt1[2][5] = {
93         {RATE_12M, RATE_18M, RATE_24M, RATE_24M, RATE_36M}, // fallback_rate0
94         {RATE_6M , RATE_6M,  RATE_12M, RATE_12M, RATE_18M}, // fallback_rate1
95     };
96 
97 
98 #define RTSDUR_BB       0
99 #define RTSDUR_BA       1
100 #define RTSDUR_AA       2
101 #define CTSDUR_BA       3
102 #define RTSDUR_BA_F0    4
103 #define RTSDUR_AA_F0    5
104 #define RTSDUR_BA_F1    6
105 #define RTSDUR_AA_F1    7
106 #define CTSDUR_BA_F0    8
107 #define CTSDUR_BA_F1    9
108 #define DATADUR_B       10
109 #define DATADUR_A       11
110 #define DATADUR_A_F0    12
111 #define DATADUR_A_F1    13
112 
113 /*---------------------  Static Functions  --------------------------*/
114 
115 
116 
117 static
118 void
119 s_vFillTxKey(
120     PSDevice   pDevice,
121     unsigned char *pbyBuf,
122     unsigned char *pbyIVHead,
123     PSKeyItem  pTransmitKey,
124     unsigned char *pbyHdrBuf,
125     unsigned short wPayloadLen,
126     unsigned char *pMICHDR
127     );
128 
129 
130 
131 static
132 void
133 s_vFillRTSHead(
134     PSDevice         pDevice,
135     unsigned char byPktType,
136     void *           pvRTS,
137     unsigned int	cbFrameLength,
138     bool bNeedAck,
139     bool bDisCRC,
140     PSEthernetHeader psEthHeader,
141     unsigned short wCurrentRate,
142     unsigned char byFBOption
143     );
144 
145 static
146 void
147 s_vGenerateTxParameter(
148     PSDevice         pDevice,
149     unsigned char byPktType,
150     void *           pTxBufHead,
151     void *           pvRrvTime,
152     void *           pvRTS,
153     void *           pvCTS,
154     unsigned int	cbFrameSize,
155     bool bNeedACK,
156     unsigned int	uDMAIdx,
157     PSEthernetHeader psEthHeader,
158     unsigned short wCurrentRate
159     );
160 
161 
162 
163 static void s_vFillFragParameter(
164     PSDevice pDevice,
165     unsigned char *pbyBuffer,
166     unsigned int	uTxType,
167     void *   pvtdCurr,
168     unsigned short wFragType,
169     unsigned int	cbReqCount
170     );
171 
172 
173 static unsigned int
174 s_cbFillTxBufHead(PSDevice pDevice, unsigned char byPktType, unsigned char *pbyTxBufferAddr,
175 	unsigned int cbFrameBodySize, unsigned int uDMAIdx, PSTxDesc pHeadTD,
176 	PSEthernetHeader psEthHeader, unsigned char *pPacket, bool bNeedEncrypt,
177 	PSKeyItem pTransmitKey, unsigned int uNodeIndex, unsigned int *puMACfragNum);
178 
179 
180 static
181 unsigned int
182 s_uFillDataHead (
183     PSDevice pDevice,
184     unsigned char byPktType,
185     void *   pTxDataHead,
186     unsigned int cbFrameLength,
187     unsigned int uDMAIdx,
188     bool bNeedAck,
189     unsigned int uFragIdx,
190     unsigned int cbLastFragmentSize,
191     unsigned int uMACfragNum,
192     unsigned char byFBOption,
193     unsigned short wCurrentRate
194     );
195 
196 
197 /*---------------------  Export Variables  --------------------------*/
198 
199 
200 
201 static
202 void
s_vFillTxKey(PSDevice pDevice,unsigned char * pbyBuf,unsigned char * pbyIVHead,PSKeyItem pTransmitKey,unsigned char * pbyHdrBuf,unsigned short wPayloadLen,unsigned char * pMICHDR)203 s_vFillTxKey (
204     PSDevice   pDevice,
205     unsigned char *pbyBuf,
206     unsigned char *pbyIVHead,
207     PSKeyItem  pTransmitKey,
208     unsigned char *pbyHdrBuf,
209     unsigned short wPayloadLen,
210     unsigned char *pMICHDR
211     )
212 {
213     unsigned long *pdwIV = (unsigned long *) pbyIVHead;
214     unsigned long *pdwExtIV = (unsigned long *) ((unsigned char *)pbyIVHead+4);
215     unsigned short wValue;
216     PS802_11Header  pMACHeader = (PS802_11Header)pbyHdrBuf;
217     unsigned long dwRevIVCounter;
218     unsigned char byKeyIndex = 0;
219 
220 
221 
222     //Fill TXKEY
223     if (pTransmitKey == NULL)
224         return;
225 
226     dwRevIVCounter = cpu_to_le32(pDevice->dwIVCounter);
227     *pdwIV = pDevice->dwIVCounter;
228     byKeyIndex = pTransmitKey->dwKeyIndex & 0xf;
229 
230     if (pTransmitKey->byCipherSuite == KEY_CTL_WEP) {
231         if (pTransmitKey->uKeyLength == WLAN_WEP232_KEYLEN ){
232             memcpy(pDevice->abyPRNG, (unsigned char *)&(dwRevIVCounter), 3);
233             memcpy(pDevice->abyPRNG+3, pTransmitKey->abyKey, pTransmitKey->uKeyLength);
234         } else {
235             memcpy(pbyBuf, (unsigned char *)&(dwRevIVCounter), 3);
236             memcpy(pbyBuf+3, pTransmitKey->abyKey, pTransmitKey->uKeyLength);
237             if(pTransmitKey->uKeyLength == WLAN_WEP40_KEYLEN) {
238                 memcpy(pbyBuf+8, (unsigned char *)&(dwRevIVCounter), 3);
239                 memcpy(pbyBuf+11, pTransmitKey->abyKey, pTransmitKey->uKeyLength);
240             }
241             memcpy(pDevice->abyPRNG, pbyBuf, 16);
242         }
243         // Append IV after Mac Header
244         *pdwIV &= WEP_IV_MASK;//00000000 11111111 11111111 11111111
245         *pdwIV |= (byKeyIndex << 30);
246         *pdwIV = cpu_to_le32(*pdwIV);
247         pDevice->dwIVCounter++;
248         if (pDevice->dwIVCounter > WEP_IV_MASK) {
249             pDevice->dwIVCounter = 0;
250         }
251     } else if (pTransmitKey->byCipherSuite == KEY_CTL_TKIP) {
252         pTransmitKey->wTSC15_0++;
253         if (pTransmitKey->wTSC15_0 == 0) {
254             pTransmitKey->dwTSC47_16++;
255         }
256         TKIPvMixKey(pTransmitKey->abyKey, pDevice->abyCurrentNetAddr,
257                     pTransmitKey->wTSC15_0, pTransmitKey->dwTSC47_16, pDevice->abyPRNG);
258         memcpy(pbyBuf, pDevice->abyPRNG, 16);
259         // Make IV
260         memcpy(pdwIV, pDevice->abyPRNG, 3);
261 
262         *(pbyIVHead+3) = (unsigned char)(((byKeyIndex << 6) & 0xc0) | 0x20); // 0x20 is ExtIV
263         // Append IV&ExtIV after Mac Header
264         *pdwExtIV = cpu_to_le32(pTransmitKey->dwTSC47_16);
265         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"vFillTxKey()---- pdwExtIV: %lx\n", *pdwExtIV);
266 
267     } else if (pTransmitKey->byCipherSuite == KEY_CTL_CCMP) {
268         pTransmitKey->wTSC15_0++;
269         if (pTransmitKey->wTSC15_0 == 0) {
270             pTransmitKey->dwTSC47_16++;
271         }
272         memcpy(pbyBuf, pTransmitKey->abyKey, 16);
273 
274         // Make IV
275         *pdwIV = 0;
276         *(pbyIVHead+3) = (unsigned char)(((byKeyIndex << 6) & 0xc0) | 0x20); // 0x20 is ExtIV
277         *pdwIV |= cpu_to_le16((unsigned short)(pTransmitKey->wTSC15_0));
278         //Append IV&ExtIV after Mac Header
279         *pdwExtIV = cpu_to_le32(pTransmitKey->dwTSC47_16);
280 
281         //Fill MICHDR0
282         *pMICHDR = 0x59;
283         *((unsigned char *)(pMICHDR+1)) = 0; // TxPriority
284         memcpy(pMICHDR+2, &(pMACHeader->abyAddr2[0]), 6);
285         *((unsigned char *)(pMICHDR+8)) = HIBYTE(HIWORD(pTransmitKey->dwTSC47_16));
286         *((unsigned char *)(pMICHDR+9)) = LOBYTE(HIWORD(pTransmitKey->dwTSC47_16));
287         *((unsigned char *)(pMICHDR+10)) = HIBYTE(LOWORD(pTransmitKey->dwTSC47_16));
288         *((unsigned char *)(pMICHDR+11)) = LOBYTE(LOWORD(pTransmitKey->dwTSC47_16));
289         *((unsigned char *)(pMICHDR+12)) = HIBYTE(pTransmitKey->wTSC15_0);
290         *((unsigned char *)(pMICHDR+13)) = LOBYTE(pTransmitKey->wTSC15_0);
291         *((unsigned char *)(pMICHDR+14)) = HIBYTE(wPayloadLen);
292         *((unsigned char *)(pMICHDR+15)) = LOBYTE(wPayloadLen);
293 
294         //Fill MICHDR1
295         *((unsigned char *)(pMICHDR+16)) = 0; // HLEN[15:8]
296         if (pDevice->bLongHeader) {
297             *((unsigned char *)(pMICHDR+17)) = 28; // HLEN[7:0]
298         } else {
299             *((unsigned char *)(pMICHDR+17)) = 22; // HLEN[7:0]
300         }
301         wValue = cpu_to_le16(pMACHeader->wFrameCtl & 0xC78F);
302         memcpy(pMICHDR+18, (unsigned char *)&wValue, 2); // MSKFRACTL
303         memcpy(pMICHDR+20, &(pMACHeader->abyAddr1[0]), 6);
304         memcpy(pMICHDR+26, &(pMACHeader->abyAddr2[0]), 6);
305 
306         //Fill MICHDR2
307         memcpy(pMICHDR+32, &(pMACHeader->abyAddr3[0]), 6);
308         wValue = pMACHeader->wSeqCtl;
309         wValue &= 0x000F;
310         wValue = cpu_to_le16(wValue);
311         memcpy(pMICHDR+38, (unsigned char *)&wValue, 2); // MSKSEQCTL
312         if (pDevice->bLongHeader) {
313             memcpy(pMICHDR+40, &(pMACHeader->abyAddr4[0]), 6);
314         }
315     }
316 }
317 
318 
319 static
320 void
s_vSWencryption(PSDevice pDevice,PSKeyItem pTransmitKey,unsigned char * pbyPayloadHead,unsigned short wPayloadSize)321 s_vSWencryption (
322     PSDevice            pDevice,
323     PSKeyItem           pTransmitKey,
324     unsigned char *pbyPayloadHead,
325     unsigned short wPayloadSize
326     )
327 {
328     unsigned int cbICVlen = 4;
329     unsigned long dwICV = 0xFFFFFFFFL;
330     unsigned long *pdwICV;
331 
332     if (pTransmitKey == NULL)
333         return;
334 
335     if (pTransmitKey->byCipherSuite == KEY_CTL_WEP) {
336         //=======================================================================
337         // Append ICV after payload
338         dwICV = CRCdwGetCrc32Ex(pbyPayloadHead, wPayloadSize, dwICV);//ICV(Payload)
339         pdwICV = (unsigned long *)(pbyPayloadHead + wPayloadSize);
340         // finally, we must invert dwCRC to get the correct answer
341         *pdwICV = cpu_to_le32(~dwICV);
342         // RC4 encryption
343         rc4_init(&pDevice->SBox, pDevice->abyPRNG, pTransmitKey->uKeyLength + 3);
344         rc4_encrypt(&pDevice->SBox, pbyPayloadHead, pbyPayloadHead, wPayloadSize+cbICVlen);
345         //=======================================================================
346     } else if (pTransmitKey->byCipherSuite == KEY_CTL_TKIP) {
347         //=======================================================================
348         //Append ICV after payload
349         dwICV = CRCdwGetCrc32Ex(pbyPayloadHead, wPayloadSize, dwICV);//ICV(Payload)
350         pdwICV = (unsigned long *)(pbyPayloadHead + wPayloadSize);
351         // finally, we must invert dwCRC to get the correct answer
352         *pdwICV = cpu_to_le32(~dwICV);
353         // RC4 encryption
354         rc4_init(&pDevice->SBox, pDevice->abyPRNG, TKIP_KEY_LEN);
355         rc4_encrypt(&pDevice->SBox, pbyPayloadHead, pbyPayloadHead, wPayloadSize+cbICVlen);
356         //=======================================================================
357     }
358 }
359 
360 
361 
362 
363 /*byPktType : PK_TYPE_11A     0
364              PK_TYPE_11B     1
365              PK_TYPE_11GB    2
366              PK_TYPE_11GA    3
367 */
368 static
369 unsigned int
s_uGetTxRsvTime(PSDevice pDevice,unsigned char byPktType,unsigned int cbFrameLength,unsigned short wRate,bool bNeedAck)370 s_uGetTxRsvTime (
371     PSDevice pDevice,
372     unsigned char byPktType,
373     unsigned int cbFrameLength,
374     unsigned short wRate,
375     bool bNeedAck
376     )
377 {
378     unsigned int uDataTime, uAckTime;
379 
380     uDataTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, cbFrameLength, wRate);
381 #ifdef	PLICE_DEBUG
382 	//printk("s_uGetTxRsvTime is %d\n",uDataTime);
383 #endif
384     if (byPktType == PK_TYPE_11B) {//llb,CCK mode
385         uAckTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, (unsigned short)pDevice->byTopCCKBasicRate);
386     } else {//11g 2.4G OFDM mode & 11a 5G OFDM mode
387         uAckTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, (unsigned short)pDevice->byTopOFDMBasicRate);
388     }
389 
390     if (bNeedAck) {
391         return (uDataTime + pDevice->uSIFS + uAckTime);
392     }
393     else {
394         return uDataTime;
395     }
396 }
397 
398 //byFreqType: 0=>5GHZ 1=>2.4GHZ
399 static
400 unsigned int
s_uGetRTSCTSRsvTime(PSDevice pDevice,unsigned char byRTSRsvType,unsigned char byPktType,unsigned int cbFrameLength,unsigned short wCurrentRate)401 s_uGetRTSCTSRsvTime (
402     PSDevice pDevice,
403     unsigned char byRTSRsvType,
404     unsigned char byPktType,
405     unsigned int cbFrameLength,
406     unsigned short wCurrentRate
407     )
408 {
409     unsigned int uRrvTime  , uRTSTime, uCTSTime, uAckTime, uDataTime;
410 
411     uRrvTime = uRTSTime = uCTSTime = uAckTime = uDataTime = 0;
412 
413 
414     uDataTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, cbFrameLength, wCurrentRate);
415     if (byRTSRsvType == 0) { //RTSTxRrvTime_bb
416         uRTSTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 20, pDevice->byTopCCKBasicRate);
417         uCTSTime = uAckTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopCCKBasicRate);
418     }
419     else if (byRTSRsvType == 1){ //RTSTxRrvTime_ba, only in 2.4GHZ
420         uRTSTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 20, pDevice->byTopCCKBasicRate);
421         uCTSTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopCCKBasicRate);
422         uAckTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopOFDMBasicRate);
423     }
424     else if (byRTSRsvType == 2) { //RTSTxRrvTime_aa
425         uRTSTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 20, pDevice->byTopOFDMBasicRate);
426         uCTSTime = uAckTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopOFDMBasicRate);
427     }
428     else if (byRTSRsvType == 3) { //CTSTxRrvTime_ba, only in 2.4GHZ
429         uCTSTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopCCKBasicRate);
430         uAckTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopOFDMBasicRate);
431         uRrvTime = uCTSTime + uAckTime + uDataTime + 2*pDevice->uSIFS;
432         return uRrvTime;
433     }
434 
435     //RTSRrvTime
436     uRrvTime = uRTSTime + uCTSTime + uAckTime + uDataTime + 3*pDevice->uSIFS;
437     return uRrvTime;
438 }
439 
440 //byFreqType 0: 5GHz, 1:2.4Ghz
441 static
442 unsigned int
s_uGetDataDuration(PSDevice pDevice,unsigned char byDurType,unsigned int cbFrameLength,unsigned char byPktType,unsigned short wRate,bool bNeedAck,unsigned int uFragIdx,unsigned int cbLastFragmentSize,unsigned int uMACfragNum,unsigned char byFBOption)443 s_uGetDataDuration (
444     PSDevice pDevice,
445     unsigned char byDurType,
446     unsigned int cbFrameLength,
447     unsigned char byPktType,
448     unsigned short wRate,
449     bool bNeedAck,
450     unsigned int uFragIdx,
451     unsigned int cbLastFragmentSize,
452     unsigned int uMACfragNum,
453     unsigned char byFBOption
454     )
455 {
456     bool bLastFrag = 0;
457     unsigned int uAckTime =0, uNextPktTime = 0;
458 
459 
460 
461     if (uFragIdx == (uMACfragNum-1)) {
462         bLastFrag = 1;
463     }
464 
465 
466     switch (byDurType) {
467 
468     case DATADUR_B:    //DATADUR_B
469         if (((uMACfragNum == 1)) || (bLastFrag == 1)) {//Non Frag or Last Frag
470             if (bNeedAck) {
471             	uAckTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopCCKBasicRate);
472                 return (pDevice->uSIFS + uAckTime);
473             } else {
474                 return 0;
475             }
476         }
477         else {//First Frag or Mid Frag
478             if (uFragIdx == (uMACfragNum-2)) {
479             	uNextPktTime = s_uGetTxRsvTime(pDevice, byPktType, cbLastFragmentSize, wRate, bNeedAck);
480             } else {
481                 uNextPktTime = s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wRate, bNeedAck);
482             }
483             if (bNeedAck) {
484             	uAckTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopCCKBasicRate);
485                 return (pDevice->uSIFS + uAckTime + uNextPktTime);
486             } else {
487                 return (pDevice->uSIFS + uNextPktTime);
488             }
489         }
490         break;
491 
492     case DATADUR_A:    //DATADUR_A
493         if (((uMACfragNum==1)) || (bLastFrag==1)) {//Non Frag or Last Frag
494             if(bNeedAck){
495             	uAckTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopOFDMBasicRate);
496                 return (pDevice->uSIFS + uAckTime);
497             } else {
498                 return 0;
499             }
500         }
501         else {//First Frag or Mid Frag
502             if(uFragIdx == (uMACfragNum-2)){
503             	uNextPktTime = s_uGetTxRsvTime(pDevice, byPktType, cbLastFragmentSize, wRate, bNeedAck);
504             } else {
505                 uNextPktTime = s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wRate, bNeedAck);
506             }
507             if(bNeedAck){
508             	uAckTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopOFDMBasicRate);
509                 return (pDevice->uSIFS + uAckTime + uNextPktTime);
510             } else {
511                 return (pDevice->uSIFS + uNextPktTime);
512             }
513         }
514         break;
515 
516     case DATADUR_A_F0:    //DATADUR_A_F0
517 	    if (((uMACfragNum==1)) || (bLastFrag==1)) {//Non Frag or Last Frag
518             if(bNeedAck){
519             	uAckTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopOFDMBasicRate);
520                 return (pDevice->uSIFS + uAckTime);
521             } else {
522                 return 0;
523             }
524         }
525 	    else { //First Frag or Mid Frag
526 	        if (byFBOption == AUTO_FB_0) {
527                 if (wRate < RATE_18M)
528                     wRate = RATE_18M;
529                 else if (wRate > RATE_54M)
530                     wRate = RATE_54M;
531 
532 	            if(uFragIdx == (uMACfragNum-2)){
533             	    uNextPktTime = s_uGetTxRsvTime(pDevice, byPktType, cbLastFragmentSize, wFB_Opt0[FB_RATE0][wRate-RATE_18M], bNeedAck);
534                 } else {
535                     uNextPktTime = s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wFB_Opt0[FB_RATE0][wRate-RATE_18M], bNeedAck);
536                 }
537 	        } else { // (byFBOption == AUTO_FB_1)
538                 if (wRate < RATE_18M)
539                     wRate = RATE_18M;
540                 else if (wRate > RATE_54M)
541                     wRate = RATE_54M;
542 
543 	            if(uFragIdx == (uMACfragNum-2)){
544             	    uNextPktTime = s_uGetTxRsvTime(pDevice, byPktType, cbLastFragmentSize, wFB_Opt1[FB_RATE0][wRate-RATE_18M], bNeedAck);
545                 } else {
546                     uNextPktTime = s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wFB_Opt1[FB_RATE0][wRate-RATE_18M], bNeedAck);
547                 }
548 	        }
549 
550 	        if(bNeedAck){
551             	uAckTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopOFDMBasicRate);
552                 return (pDevice->uSIFS + uAckTime + uNextPktTime);
553             } else {
554                 return (pDevice->uSIFS + uNextPktTime);
555             }
556 	    }
557         break;
558 
559     case DATADUR_A_F1:    //DATADUR_A_F1
560         if (((uMACfragNum==1)) || (bLastFrag==1)) {//Non Frag or Last Frag
561             if(bNeedAck){
562             	uAckTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopOFDMBasicRate);
563                 return (pDevice->uSIFS + uAckTime);
564             } else {
565                 return 0;
566             }
567         }
568 	    else { //First Frag or Mid Frag
569 	        if (byFBOption == AUTO_FB_0) {
570                 if (wRate < RATE_18M)
571                     wRate = RATE_18M;
572                 else if (wRate > RATE_54M)
573                     wRate = RATE_54M;
574 
575 	            if(uFragIdx == (uMACfragNum-2)){
576             	    uNextPktTime = s_uGetTxRsvTime(pDevice, byPktType, cbLastFragmentSize, wFB_Opt0[FB_RATE1][wRate-RATE_18M], bNeedAck);
577                 } else {
578                     uNextPktTime = s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wFB_Opt0[FB_RATE1][wRate-RATE_18M], bNeedAck);
579                 }
580 
581 	        } else { // (byFBOption == AUTO_FB_1)
582                 if (wRate < RATE_18M)
583                     wRate = RATE_18M;
584                 else if (wRate > RATE_54M)
585                     wRate = RATE_54M;
586 
587 	            if(uFragIdx == (uMACfragNum-2)){
588             	    uNextPktTime = s_uGetTxRsvTime(pDevice, byPktType, cbLastFragmentSize, wFB_Opt1[FB_RATE1][wRate-RATE_18M], bNeedAck);
589                 } else {
590                     uNextPktTime = s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wFB_Opt1[FB_RATE1][wRate-RATE_18M], bNeedAck);
591                 }
592 	        }
593 	        if(bNeedAck){
594             	uAckTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopOFDMBasicRate);
595                 return (pDevice->uSIFS + uAckTime + uNextPktTime);
596             } else {
597                 return (pDevice->uSIFS + uNextPktTime);
598             }
599 	    }
600         break;
601 
602     default:
603         break;
604     }
605 
606 	ASSERT(false);
607 	return 0;
608 }
609 
610 
611 //byFreqType: 0=>5GHZ 1=>2.4GHZ
612 static
613 unsigned int
s_uGetRTSCTSDuration(PSDevice pDevice,unsigned char byDurType,unsigned int cbFrameLength,unsigned char byPktType,unsigned short wRate,bool bNeedAck,unsigned char byFBOption)614 s_uGetRTSCTSDuration (
615     PSDevice pDevice,
616     unsigned char byDurType,
617     unsigned int cbFrameLength,
618     unsigned char byPktType,
619     unsigned short wRate,
620     bool bNeedAck,
621     unsigned char byFBOption
622     )
623 {
624     unsigned int uCTSTime = 0, uDurTime = 0;
625 
626 
627     switch (byDurType) {
628 
629     case RTSDUR_BB:    //RTSDuration_bb
630         uCTSTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopCCKBasicRate);
631         uDurTime = uCTSTime + 2*pDevice->uSIFS + s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wRate, bNeedAck);
632         break;
633 
634     case RTSDUR_BA:    //RTSDuration_ba
635         uCTSTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopCCKBasicRate);
636         uDurTime = uCTSTime + 2*pDevice->uSIFS + s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wRate, bNeedAck);
637         break;
638 
639     case RTSDUR_AA:    //RTSDuration_aa
640         uCTSTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopOFDMBasicRate);
641         uDurTime = uCTSTime + 2*pDevice->uSIFS + s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wRate, bNeedAck);
642         break;
643 
644     case CTSDUR_BA:    //CTSDuration_ba
645         uDurTime = pDevice->uSIFS + s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wRate, bNeedAck);
646         break;
647 
648     case RTSDUR_BA_F0: //RTSDuration_ba_f0
649         uCTSTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopCCKBasicRate);
650         if ((byFBOption == AUTO_FB_0) && (wRate >= RATE_18M) && (wRate <=RATE_54M)) {
651             uDurTime = uCTSTime + 2*pDevice->uSIFS + s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wFB_Opt0[FB_RATE0][wRate-RATE_18M], bNeedAck);
652         } else if ((byFBOption == AUTO_FB_1) && (wRate >= RATE_18M) && (wRate <=RATE_54M)) {
653             uDurTime = uCTSTime + 2*pDevice->uSIFS + s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wFB_Opt1[FB_RATE0][wRate-RATE_18M], bNeedAck);
654         }
655         break;
656 
657     case RTSDUR_AA_F0: //RTSDuration_aa_f0
658         uCTSTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopOFDMBasicRate);
659         if ((byFBOption == AUTO_FB_0) && (wRate >= RATE_18M) && (wRate <=RATE_54M)) {
660             uDurTime = uCTSTime + 2*pDevice->uSIFS + s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wFB_Opt0[FB_RATE0][wRate-RATE_18M], bNeedAck);
661         } else if ((byFBOption == AUTO_FB_1) && (wRate >= RATE_18M) && (wRate <=RATE_54M)) {
662             uDurTime = uCTSTime + 2*pDevice->uSIFS + s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wFB_Opt1[FB_RATE0][wRate-RATE_18M], bNeedAck);
663         }
664         break;
665 
666     case RTSDUR_BA_F1: //RTSDuration_ba_f1
667         uCTSTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopCCKBasicRate);
668         if ((byFBOption == AUTO_FB_0) && (wRate >= RATE_18M) && (wRate <=RATE_54M)) {
669             uDurTime = uCTSTime + 2*pDevice->uSIFS + s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wFB_Opt0[FB_RATE1][wRate-RATE_18M], bNeedAck);
670         } else if ((byFBOption == AUTO_FB_1) && (wRate >= RATE_18M) && (wRate <=RATE_54M)) {
671             uDurTime = uCTSTime + 2*pDevice->uSIFS + s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wFB_Opt1[FB_RATE1][wRate-RATE_18M], bNeedAck);
672         }
673         break;
674 
675     case RTSDUR_AA_F1: //RTSDuration_aa_f1
676         uCTSTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopOFDMBasicRate);
677         if ((byFBOption == AUTO_FB_0) && (wRate >= RATE_18M) && (wRate <=RATE_54M)) {
678             uDurTime = uCTSTime + 2*pDevice->uSIFS + s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wFB_Opt0[FB_RATE1][wRate-RATE_18M], bNeedAck);
679         } else if ((byFBOption == AUTO_FB_1) && (wRate >= RATE_18M) && (wRate <=RATE_54M)) {
680             uDurTime = uCTSTime + 2*pDevice->uSIFS + s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wFB_Opt1[FB_RATE1][wRate-RATE_18M], bNeedAck);
681         }
682         break;
683 
684     case CTSDUR_BA_F0: //CTSDuration_ba_f0
685         if ((byFBOption == AUTO_FB_0) && (wRate >= RATE_18M) && (wRate <=RATE_54M)) {
686             uDurTime = pDevice->uSIFS + s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wFB_Opt0[FB_RATE0][wRate-RATE_18M], bNeedAck);
687         } else if ((byFBOption == AUTO_FB_1) && (wRate >= RATE_18M) && (wRate <=RATE_54M)) {
688             uDurTime = pDevice->uSIFS + s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wFB_Opt1[FB_RATE0][wRate-RATE_18M], bNeedAck);
689         }
690         break;
691 
692     case CTSDUR_BA_F1: //CTSDuration_ba_f1
693         if ((byFBOption == AUTO_FB_0) && (wRate >= RATE_18M) && (wRate <=RATE_54M)) {
694             uDurTime = pDevice->uSIFS + s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wFB_Opt0[FB_RATE1][wRate-RATE_18M], bNeedAck);
695         } else if ((byFBOption == AUTO_FB_1) && (wRate >= RATE_18M) && (wRate <=RATE_54M)) {
696             uDurTime = pDevice->uSIFS + s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wFB_Opt1[FB_RATE1][wRate-RATE_18M], bNeedAck);
697         }
698         break;
699 
700     default:
701         break;
702     }
703 
704     return uDurTime;
705 
706 }
707 
708 
709 
710 static
711 unsigned int
s_uFillDataHead(PSDevice pDevice,unsigned char byPktType,void * pTxDataHead,unsigned int cbFrameLength,unsigned int uDMAIdx,bool bNeedAck,unsigned int uFragIdx,unsigned int cbLastFragmentSize,unsigned int uMACfragNum,unsigned char byFBOption,unsigned short wCurrentRate)712 s_uFillDataHead (
713     PSDevice pDevice,
714     unsigned char byPktType,
715     void *   pTxDataHead,
716     unsigned int cbFrameLength,
717     unsigned int uDMAIdx,
718     bool bNeedAck,
719     unsigned int uFragIdx,
720     unsigned int cbLastFragmentSize,
721     unsigned int uMACfragNum,
722     unsigned char byFBOption,
723     unsigned short wCurrentRate
724     )
725 {
726     unsigned short wLen = 0x0000;
727 
728     if (pTxDataHead == NULL) {
729         return 0;
730     }
731 
732     if (byPktType == PK_TYPE_11GB || byPktType == PK_TYPE_11GA) {
733         if (byFBOption == AUTO_FB_NONE) {
734             PSTxDataHead_g pBuf = (PSTxDataHead_g)pTxDataHead;
735             //Get SignalField,ServiceField,Length
736             BBvCaculateParameter(pDevice, cbFrameLength, wCurrentRate, byPktType,
737                 (unsigned short *)&(wLen), (unsigned char *)&(pBuf->byServiceField_a), (unsigned char *)&(pBuf->bySignalField_a)
738             );
739             pBuf->wTransmitLength_a = cpu_to_le16(wLen);
740             BBvCaculateParameter(pDevice, cbFrameLength, pDevice->byTopCCKBasicRate, PK_TYPE_11B,
741                 (unsigned short *)&(wLen), (unsigned char *)&(pBuf->byServiceField_b), (unsigned char *)&(pBuf->bySignalField_b)
742             );
743             pBuf->wTransmitLength_b = cpu_to_le16(wLen);
744             //Get Duration and TimeStamp
745             pBuf->wDuration_a = cpu_to_le16((unsigned short)s_uGetDataDuration(pDevice, DATADUR_A, cbFrameLength,
746                                                          byPktType, wCurrentRate, bNeedAck, uFragIdx,
747                                                          cbLastFragmentSize, uMACfragNum,
748                                                          byFBOption)); //1: 2.4GHz
749             pBuf->wDuration_b = cpu_to_le16((unsigned short)s_uGetDataDuration(pDevice, DATADUR_B, cbFrameLength,
750                                                          PK_TYPE_11B, pDevice->byTopCCKBasicRate,
751                                                          bNeedAck, uFragIdx, cbLastFragmentSize,
752                                                          uMACfragNum, byFBOption)); //1: 2.4
753 
754             pBuf->wTimeStampOff_a = cpu_to_le16(wTimeStampOff[pDevice->byPreambleType%2][wCurrentRate%MAX_RATE]);
755             pBuf->wTimeStampOff_b = cpu_to_le16(wTimeStampOff[pDevice->byPreambleType%2][pDevice->byTopCCKBasicRate%MAX_RATE]);
756 
757             return (pBuf->wDuration_a);
758          } else {
759             // Auto Fallback
760             PSTxDataHead_g_FB pBuf = (PSTxDataHead_g_FB)pTxDataHead;
761             //Get SignalField,ServiceField,Length
762             BBvCaculateParameter(pDevice, cbFrameLength, wCurrentRate, byPktType,
763                 (unsigned short *)&(wLen), (unsigned char *)&(pBuf->byServiceField_a), (unsigned char *)&(pBuf->bySignalField_a)
764             );
765             pBuf->wTransmitLength_a = cpu_to_le16(wLen);
766             BBvCaculateParameter(pDevice, cbFrameLength, pDevice->byTopCCKBasicRate, PK_TYPE_11B,
767                 (unsigned short *)&(wLen), (unsigned char *)&(pBuf->byServiceField_b), (unsigned char *)&(pBuf->bySignalField_b)
768             );
769             pBuf->wTransmitLength_b = cpu_to_le16(wLen);
770             //Get Duration and TimeStamp
771             pBuf->wDuration_a = cpu_to_le16((unsigned short)s_uGetDataDuration(pDevice, DATADUR_A, cbFrameLength, byPktType,
772                                          wCurrentRate, bNeedAck, uFragIdx, cbLastFragmentSize, uMACfragNum, byFBOption)); //1: 2.4GHz
773             pBuf->wDuration_b = cpu_to_le16((unsigned short)s_uGetDataDuration(pDevice, DATADUR_B, cbFrameLength, PK_TYPE_11B,
774                                          pDevice->byTopCCKBasicRate, bNeedAck, uFragIdx, cbLastFragmentSize, uMACfragNum, byFBOption)); //1: 2.4GHz
775             pBuf->wDuration_a_f0 = cpu_to_le16((unsigned short)s_uGetDataDuration(pDevice, DATADUR_A_F0, cbFrameLength, byPktType,
776                                          wCurrentRate, bNeedAck, uFragIdx, cbLastFragmentSize, uMACfragNum, byFBOption)); //1: 2.4GHz
777             pBuf->wDuration_a_f1 = cpu_to_le16((unsigned short)s_uGetDataDuration(pDevice, DATADUR_A_F1, cbFrameLength, byPktType,
778                                          wCurrentRate, bNeedAck, uFragIdx, cbLastFragmentSize, uMACfragNum, byFBOption)); //1: 2.4GHz
779 
780             pBuf->wTimeStampOff_a = cpu_to_le16(wTimeStampOff[pDevice->byPreambleType%2][wCurrentRate%MAX_RATE]);
781             pBuf->wTimeStampOff_b = cpu_to_le16(wTimeStampOff[pDevice->byPreambleType%2][pDevice->byTopCCKBasicRate%MAX_RATE]);
782 
783             return (pBuf->wDuration_a);
784         } //if (byFBOption == AUTO_FB_NONE)
785     }
786     else if (byPktType == PK_TYPE_11A) {
787         if ((byFBOption != AUTO_FB_NONE)) {
788             // Auto Fallback
789             PSTxDataHead_a_FB pBuf = (PSTxDataHead_a_FB)pTxDataHead;
790             //Get SignalField,ServiceField,Length
791             BBvCaculateParameter(pDevice, cbFrameLength, wCurrentRate, byPktType,
792                 (unsigned short *)&(wLen), (unsigned char *)&(pBuf->byServiceField), (unsigned char *)&(pBuf->bySignalField)
793             );
794             pBuf->wTransmitLength = cpu_to_le16(wLen);
795             //Get Duration and TimeStampOff
796 
797             pBuf->wDuration = cpu_to_le16((unsigned short)s_uGetDataDuration(pDevice, DATADUR_A, cbFrameLength, byPktType,
798                                         wCurrentRate, bNeedAck, uFragIdx, cbLastFragmentSize, uMACfragNum, byFBOption)); //0: 5GHz
799             pBuf->wDuration_f0 = cpu_to_le16((unsigned short)s_uGetDataDuration(pDevice, DATADUR_A_F0, cbFrameLength, byPktType,
800                                         wCurrentRate, bNeedAck, uFragIdx, cbLastFragmentSize, uMACfragNum, byFBOption)); //0: 5GHz
801             pBuf->wDuration_f1 = cpu_to_le16((unsigned short)s_uGetDataDuration(pDevice, DATADUR_A_F1, cbFrameLength, byPktType,
802                                         wCurrentRate, bNeedAck, uFragIdx, cbLastFragmentSize, uMACfragNum, byFBOption)); //0: 5GHz
803             pBuf->wTimeStampOff = cpu_to_le16(wTimeStampOff[pDevice->byPreambleType%2][wCurrentRate%MAX_RATE]);
804             return (pBuf->wDuration);
805         } else {
806             PSTxDataHead_ab pBuf = (PSTxDataHead_ab)pTxDataHead;
807             //Get SignalField,ServiceField,Length
808             BBvCaculateParameter(pDevice, cbFrameLength, wCurrentRate, byPktType,
809                 (unsigned short *)&(wLen), (unsigned char *)&(pBuf->byServiceField), (unsigned char *)&(pBuf->bySignalField)
810             );
811             pBuf->wTransmitLength = cpu_to_le16(wLen);
812             //Get Duration and TimeStampOff
813 
814             pBuf->wDuration = cpu_to_le16((unsigned short)s_uGetDataDuration(pDevice, DATADUR_A, cbFrameLength, byPktType,
815                                                        wCurrentRate, bNeedAck, uFragIdx,
816                                                        cbLastFragmentSize, uMACfragNum,
817                                                        byFBOption));
818 
819             pBuf->wTimeStampOff = cpu_to_le16(wTimeStampOff[pDevice->byPreambleType%2][wCurrentRate%MAX_RATE]);
820             return (pBuf->wDuration);
821         }
822     }
823     else {
824             PSTxDataHead_ab pBuf = (PSTxDataHead_ab)pTxDataHead;
825             //Get SignalField,ServiceField,Length
826             BBvCaculateParameter(pDevice, cbFrameLength, wCurrentRate, byPktType,
827                 (unsigned short *)&(wLen), (unsigned char *)&(pBuf->byServiceField), (unsigned char *)&(pBuf->bySignalField)
828             );
829             pBuf->wTransmitLength = cpu_to_le16(wLen);
830             //Get Duration and TimeStampOff
831             pBuf->wDuration = cpu_to_le16((unsigned short)s_uGetDataDuration(pDevice, DATADUR_B, cbFrameLength, byPktType,
832                                                        wCurrentRate, bNeedAck, uFragIdx,
833                                                        cbLastFragmentSize, uMACfragNum,
834                                                        byFBOption));
835             pBuf->wTimeStampOff = cpu_to_le16(wTimeStampOff[pDevice->byPreambleType%2][wCurrentRate%MAX_RATE]);
836             return (pBuf->wDuration);
837     }
838     return 0;
839 }
840 
841 
842 static
843 void
s_vFillRTSHead(PSDevice pDevice,unsigned char byPktType,void * pvRTS,unsigned int cbFrameLength,bool bNeedAck,bool bDisCRC,PSEthernetHeader psEthHeader,unsigned short wCurrentRate,unsigned char byFBOption)844 s_vFillRTSHead (
845     PSDevice         pDevice,
846     unsigned char byPktType,
847     void *           pvRTS,
848     unsigned int cbFrameLength,
849     bool bNeedAck,
850     bool bDisCRC,
851     PSEthernetHeader psEthHeader,
852     unsigned short wCurrentRate,
853     unsigned char byFBOption
854     )
855 {
856     unsigned int uRTSFrameLen = 20;
857     unsigned short wLen = 0x0000;
858 
859     if (pvRTS == NULL)
860     	return;
861 
862     if (bDisCRC) {
863         // When CRCDIS bit is on, H/W forgot to generate FCS for RTS frame,
864         // in this case we need to decrease its length by 4.
865         uRTSFrameLen -= 4;
866     }
867 
868     // Note: So far RTSHead dosen't appear in ATIM & Beacom DMA, so we don't need to take them into account.
869     //       Otherwise, we need to modify codes for them.
870     if (byPktType == PK_TYPE_11GB || byPktType == PK_TYPE_11GA) {
871         if (byFBOption == AUTO_FB_NONE) {
872             PSRTS_g pBuf = (PSRTS_g)pvRTS;
873             //Get SignalField,ServiceField,Length
874             BBvCaculateParameter(pDevice, uRTSFrameLen, pDevice->byTopCCKBasicRate, PK_TYPE_11B,
875                 (unsigned short *)&(wLen), (unsigned char *)&(pBuf->byServiceField_b), (unsigned char *)&(pBuf->bySignalField_b)
876             );
877             pBuf->wTransmitLength_b = cpu_to_le16(wLen);
878             BBvCaculateParameter(pDevice, uRTSFrameLen, pDevice->byTopOFDMBasicRate, byPktType,
879                 (unsigned short *)&(wLen), (unsigned char *)&(pBuf->byServiceField_a), (unsigned char *)&(pBuf->bySignalField_a)
880             );
881             pBuf->wTransmitLength_a = cpu_to_le16(wLen);
882             //Get Duration
883             pBuf->wDuration_bb = cpu_to_le16((unsigned short)s_uGetRTSCTSDuration(pDevice, RTSDUR_BB, cbFrameLength, PK_TYPE_11B, pDevice->byTopCCKBasicRate, bNeedAck, byFBOption));    //0:RTSDuration_bb, 1:2.4G, 1:CCKData
884             pBuf->wDuration_aa = cpu_to_le16((unsigned short)s_uGetRTSCTSDuration(pDevice, RTSDUR_AA, cbFrameLength, byPktType, wCurrentRate, bNeedAck, byFBOption)); //2:RTSDuration_aa, 1:2.4G, 2,3: 2.4G OFDMData
885             pBuf->wDuration_ba = cpu_to_le16((unsigned short)s_uGetRTSCTSDuration(pDevice, RTSDUR_BA, cbFrameLength, byPktType, wCurrentRate, bNeedAck, byFBOption)); //1:RTSDuration_ba, 1:2.4G, 2,3:2.4G OFDM Data
886 
887             pBuf->Data.wDurationID = pBuf->wDuration_aa;
888             //Get RTS Frame body
889             pBuf->Data.wFrameControl = TYPE_CTL_RTS;//0x00B4
890             if ((pDevice->eOPMode == OP_MODE_ADHOC) ||
891                 (pDevice->eOPMode == OP_MODE_AP)) {
892                 memcpy(&(pBuf->Data.abyRA[0]), &(psEthHeader->abyDstAddr[0]), ETH_ALEN);
893             }
894             else {
895                 memcpy(&(pBuf->Data.abyRA[0]), &(pDevice->abyBSSID[0]), ETH_ALEN);
896             }
897             if (pDevice->eOPMode == OP_MODE_AP) {
898                 memcpy(&(pBuf->Data.abyTA[0]), &(pDevice->abyBSSID[0]), ETH_ALEN);
899             }
900             else {
901                 memcpy(&(pBuf->Data.abyTA[0]), &(psEthHeader->abySrcAddr[0]), ETH_ALEN);
902             }
903         }
904         else {
905            PSRTS_g_FB pBuf = (PSRTS_g_FB)pvRTS;
906             //Get SignalField,ServiceField,Length
907             BBvCaculateParameter(pDevice, uRTSFrameLen, pDevice->byTopCCKBasicRate, PK_TYPE_11B,
908                 (unsigned short *)&(wLen), (unsigned char *)&(pBuf->byServiceField_b), (unsigned char *)&(pBuf->bySignalField_b)
909             );
910             pBuf->wTransmitLength_b = cpu_to_le16(wLen);
911             BBvCaculateParameter(pDevice, uRTSFrameLen, pDevice->byTopOFDMBasicRate, byPktType,
912                 (unsigned short *)&(wLen), (unsigned char *)&(pBuf->byServiceField_a), (unsigned char *)&(pBuf->bySignalField_a)
913             );
914             pBuf->wTransmitLength_a = cpu_to_le16(wLen);
915 
916             //Get Duration
917             pBuf->wDuration_bb = cpu_to_le16((unsigned short)s_uGetRTSCTSDuration(pDevice, RTSDUR_BB, cbFrameLength, PK_TYPE_11B, pDevice->byTopCCKBasicRate, bNeedAck, byFBOption));    //0:RTSDuration_bb, 1:2.4G, 1:CCKData
918             pBuf->wDuration_aa = cpu_to_le16((unsigned short)s_uGetRTSCTSDuration(pDevice, RTSDUR_AA, cbFrameLength, byPktType, wCurrentRate, bNeedAck, byFBOption)); //2:RTSDuration_aa, 1:2.4G, 2,3:2.4G OFDMData
919             pBuf->wDuration_ba = cpu_to_le16((unsigned short)s_uGetRTSCTSDuration(pDevice, RTSDUR_BA, cbFrameLength, byPktType, wCurrentRate, bNeedAck, byFBOption)); //1:RTSDuration_ba, 1:2.4G, 2,3:2.4G OFDMData
920             pBuf->wRTSDuration_ba_f0 = cpu_to_le16((unsigned short)s_uGetRTSCTSDuration(pDevice, RTSDUR_BA_F0, cbFrameLength, byPktType, wCurrentRate, bNeedAck, byFBOption));    //4:wRTSDuration_ba_f0, 1:2.4G, 1:CCKData
921             pBuf->wRTSDuration_aa_f0 = cpu_to_le16((unsigned short)s_uGetRTSCTSDuration(pDevice, RTSDUR_AA_F0, cbFrameLength, byPktType, wCurrentRate, bNeedAck, byFBOption));    //5:wRTSDuration_aa_f0, 1:2.4G, 1:CCKData
922             pBuf->wRTSDuration_ba_f1 = cpu_to_le16((unsigned short)s_uGetRTSCTSDuration(pDevice, RTSDUR_BA_F1, cbFrameLength, byPktType, wCurrentRate, bNeedAck, byFBOption));    //6:wRTSDuration_ba_f1, 1:2.4G, 1:CCKData
923             pBuf->wRTSDuration_aa_f1 = cpu_to_le16((unsigned short)s_uGetRTSCTSDuration(pDevice, RTSDUR_AA_F1, cbFrameLength, byPktType, wCurrentRate, bNeedAck, byFBOption));    //7:wRTSDuration_aa_f1, 1:2.4G, 1:CCKData
924             pBuf->Data.wDurationID = pBuf->wDuration_aa;
925             //Get RTS Frame body
926             pBuf->Data.wFrameControl = TYPE_CTL_RTS;//0x00B4
927 
928             if ((pDevice->eOPMode == OP_MODE_ADHOC) ||
929                 (pDevice->eOPMode == OP_MODE_AP)) {
930                 memcpy(&(pBuf->Data.abyRA[0]), &(psEthHeader->abyDstAddr[0]), ETH_ALEN);
931             }
932             else {
933                 memcpy(&(pBuf->Data.abyRA[0]), &(pDevice->abyBSSID[0]), ETH_ALEN);
934             }
935 
936             if (pDevice->eOPMode == OP_MODE_AP) {
937                 memcpy(&(pBuf->Data.abyTA[0]), &(pDevice->abyBSSID[0]), ETH_ALEN);
938             }
939             else {
940                 memcpy(&(pBuf->Data.abyTA[0]), &(psEthHeader->abySrcAddr[0]), ETH_ALEN);
941             }
942 
943         } // if (byFBOption == AUTO_FB_NONE)
944     }
945     else if (byPktType == PK_TYPE_11A) {
946         if (byFBOption == AUTO_FB_NONE) {
947             PSRTS_ab pBuf = (PSRTS_ab)pvRTS;
948             //Get SignalField,ServiceField,Length
949             BBvCaculateParameter(pDevice, uRTSFrameLen, pDevice->byTopOFDMBasicRate, byPktType,
950                 (unsigned short *)&(wLen), (unsigned char *)&(pBuf->byServiceField), (unsigned char *)&(pBuf->bySignalField)
951             );
952             pBuf->wTransmitLength = cpu_to_le16(wLen);
953             //Get Duration
954             pBuf->wDuration = cpu_to_le16((unsigned short)s_uGetRTSCTSDuration(pDevice, RTSDUR_AA, cbFrameLength, byPktType, wCurrentRate, bNeedAck, byFBOption)); //0:RTSDuration_aa, 0:5G, 0: 5G OFDMData
955     	    pBuf->Data.wDurationID = pBuf->wDuration;
956             //Get RTS Frame body
957             pBuf->Data.wFrameControl = TYPE_CTL_RTS;//0x00B4
958 
959             if ((pDevice->eOPMode == OP_MODE_ADHOC) ||
960                 (pDevice->eOPMode == OP_MODE_AP)) {
961                 memcpy(&(pBuf->Data.abyRA[0]), &(psEthHeader->abyDstAddr[0]), ETH_ALEN);
962             }
963             else {
964                 memcpy(&(pBuf->Data.abyRA[0]), &(pDevice->abyBSSID[0]), ETH_ALEN);
965             }
966 
967             if (pDevice->eOPMode == OP_MODE_AP) {
968                 memcpy(&(pBuf->Data.abyTA[0]), &(pDevice->abyBSSID[0]), ETH_ALEN);
969             }
970             else {
971                 memcpy(&(pBuf->Data.abyTA[0]), &(psEthHeader->abySrcAddr[0]), ETH_ALEN);
972             }
973 
974         }
975         else {
976             PSRTS_a_FB pBuf = (PSRTS_a_FB)pvRTS;
977             //Get SignalField,ServiceField,Length
978             BBvCaculateParameter(pDevice, uRTSFrameLen, pDevice->byTopOFDMBasicRate, byPktType,
979                 (unsigned short *)&(wLen), (unsigned char *)&(pBuf->byServiceField), (unsigned char *)&(pBuf->bySignalField)
980             );
981             pBuf->wTransmitLength = cpu_to_le16(wLen);
982             //Get Duration
983             pBuf->wDuration = cpu_to_le16((unsigned short)s_uGetRTSCTSDuration(pDevice, RTSDUR_AA, cbFrameLength, byPktType, wCurrentRate, bNeedAck, byFBOption)); //0:RTSDuration_aa, 0:5G, 0: 5G OFDMData
984     	    pBuf->wRTSDuration_f0 = cpu_to_le16((unsigned short)s_uGetRTSCTSDuration(pDevice, RTSDUR_AA_F0, cbFrameLength, byPktType, wCurrentRate, bNeedAck, byFBOption)); //5:RTSDuration_aa_f0, 0:5G, 0: 5G OFDMData
985     	    pBuf->wRTSDuration_f1 = cpu_to_le16((unsigned short)s_uGetRTSCTSDuration(pDevice, RTSDUR_AA_F1, cbFrameLength, byPktType, wCurrentRate, bNeedAck, byFBOption)); //7:RTSDuration_aa_f1, 0:5G, 0:
986     	    pBuf->Data.wDurationID = pBuf->wDuration;
987     	    //Get RTS Frame body
988             pBuf->Data.wFrameControl = TYPE_CTL_RTS;//0x00B4
989 
990             if ((pDevice->eOPMode == OP_MODE_ADHOC) ||
991                 (pDevice->eOPMode == OP_MODE_AP)) {
992                 memcpy(&(pBuf->Data.abyRA[0]), &(psEthHeader->abyDstAddr[0]), ETH_ALEN);
993             }
994             else {
995                 memcpy(&(pBuf->Data.abyRA[0]), &(pDevice->abyBSSID[0]), ETH_ALEN);
996             }
997             if (pDevice->eOPMode == OP_MODE_AP) {
998                 memcpy(&(pBuf->Data.abyTA[0]), &(pDevice->abyBSSID[0]), ETH_ALEN);
999             }
1000             else {
1001                 memcpy(&(pBuf->Data.abyTA[0]), &(psEthHeader->abySrcAddr[0]), ETH_ALEN);
1002             }
1003         }
1004     }
1005     else if (byPktType == PK_TYPE_11B) {
1006         PSRTS_ab pBuf = (PSRTS_ab)pvRTS;
1007         //Get SignalField,ServiceField,Length
1008         BBvCaculateParameter(pDevice, uRTSFrameLen, pDevice->byTopCCKBasicRate, PK_TYPE_11B,
1009             (unsigned short *)&(wLen), (unsigned char *)&(pBuf->byServiceField), (unsigned char *)&(pBuf->bySignalField)
1010         );
1011         pBuf->wTransmitLength = cpu_to_le16(wLen);
1012         //Get Duration
1013         pBuf->wDuration = cpu_to_le16((unsigned short)s_uGetRTSCTSDuration(pDevice, RTSDUR_BB, cbFrameLength, byPktType, wCurrentRate, bNeedAck, byFBOption)); //0:RTSDuration_bb, 1:2.4G, 1:CCKData
1014         pBuf->Data.wDurationID = pBuf->wDuration;
1015         //Get RTS Frame body
1016         pBuf->Data.wFrameControl = TYPE_CTL_RTS;//0x00B4
1017 
1018 
1019         if ((pDevice->eOPMode == OP_MODE_ADHOC) ||
1020             (pDevice->eOPMode == OP_MODE_AP)) {
1021             memcpy(&(pBuf->Data.abyRA[0]), &(psEthHeader->abyDstAddr[0]), ETH_ALEN);
1022         }
1023         else {
1024             memcpy(&(pBuf->Data.abyRA[0]), &(pDevice->abyBSSID[0]), ETH_ALEN);
1025         }
1026 
1027         if (pDevice->eOPMode == OP_MODE_AP) {
1028             memcpy(&(pBuf->Data.abyTA[0]), &(pDevice->abyBSSID[0]), ETH_ALEN);
1029         }
1030         else {
1031             memcpy(&(pBuf->Data.abyTA[0]), &(psEthHeader->abySrcAddr[0]), ETH_ALEN);
1032         }
1033     }
1034 }
1035 
1036 static
1037 void
s_vFillCTSHead(PSDevice pDevice,unsigned int uDMAIdx,unsigned char byPktType,void * pvCTS,unsigned int cbFrameLength,bool bNeedAck,bool bDisCRC,unsigned short wCurrentRate,unsigned char byFBOption)1038 s_vFillCTSHead (
1039     PSDevice pDevice,
1040     unsigned int uDMAIdx,
1041     unsigned char byPktType,
1042     void *   pvCTS,
1043     unsigned int cbFrameLength,
1044     bool bNeedAck,
1045     bool bDisCRC,
1046     unsigned short wCurrentRate,
1047     unsigned char byFBOption
1048     )
1049 {
1050     unsigned int uCTSFrameLen = 14;
1051     unsigned short wLen = 0x0000;
1052 
1053     if (pvCTS == NULL) {
1054         return;
1055     }
1056 
1057     if (bDisCRC) {
1058         // When CRCDIS bit is on, H/W forgot to generate FCS for CTS frame,
1059         // in this case we need to decrease its length by 4.
1060         uCTSFrameLen -= 4;
1061     }
1062 
1063     if (byPktType == PK_TYPE_11GB || byPktType == PK_TYPE_11GA) {
1064         if (byFBOption != AUTO_FB_NONE && uDMAIdx != TYPE_ATIMDMA && uDMAIdx != TYPE_BEACONDMA) {
1065             // Auto Fall back
1066             PSCTS_FB pBuf = (PSCTS_FB)pvCTS;
1067             //Get SignalField,ServiceField,Length
1068             BBvCaculateParameter(pDevice, uCTSFrameLen, pDevice->byTopCCKBasicRate, PK_TYPE_11B,
1069                 (unsigned short *)&(wLen), (unsigned char *)&(pBuf->byServiceField_b), (unsigned char *)&(pBuf->bySignalField_b)
1070             );
1071 
1072 
1073             pBuf->wTransmitLength_b = cpu_to_le16(wLen);
1074 
1075             pBuf->wDuration_ba = (unsigned short)s_uGetRTSCTSDuration(pDevice, CTSDUR_BA, cbFrameLength, byPktType, wCurrentRate, bNeedAck, byFBOption); //3:CTSDuration_ba, 1:2.4G, 2,3:2.4G OFDM Data
1076             pBuf->wDuration_ba += pDevice->wCTSDuration;
1077             pBuf->wDuration_ba = cpu_to_le16(pBuf->wDuration_ba);
1078             //Get CTSDuration_ba_f0
1079             pBuf->wCTSDuration_ba_f0 = (unsigned short)s_uGetRTSCTSDuration(pDevice, CTSDUR_BA_F0, cbFrameLength, byPktType, wCurrentRate, bNeedAck, byFBOption); //8:CTSDuration_ba_f0, 1:2.4G, 2,3:2.4G OFDM Data
1080             pBuf->wCTSDuration_ba_f0 += pDevice->wCTSDuration;
1081             pBuf->wCTSDuration_ba_f0 = cpu_to_le16(pBuf->wCTSDuration_ba_f0);
1082             //Get CTSDuration_ba_f1
1083             pBuf->wCTSDuration_ba_f1 = (unsigned short)s_uGetRTSCTSDuration(pDevice, CTSDUR_BA_F1, cbFrameLength, byPktType, wCurrentRate, bNeedAck, byFBOption); //9:CTSDuration_ba_f1, 1:2.4G, 2,3:2.4G OFDM Data
1084             pBuf->wCTSDuration_ba_f1 += pDevice->wCTSDuration;
1085             pBuf->wCTSDuration_ba_f1 = cpu_to_le16(pBuf->wCTSDuration_ba_f1);
1086             //Get CTS Frame body
1087             pBuf->Data.wDurationID = pBuf->wDuration_ba;
1088             pBuf->Data.wFrameControl = TYPE_CTL_CTS;//0x00C4
1089             pBuf->Data.wReserved = 0x0000;
1090             memcpy(&(pBuf->Data.abyRA[0]), &(pDevice->abyCurrentNetAddr[0]), ETH_ALEN);
1091 
1092         } else { //if (byFBOption != AUTO_FB_NONE && uDMAIdx != TYPE_ATIMDMA && uDMAIdx != TYPE_BEACONDMA)
1093             PSCTS pBuf = (PSCTS)pvCTS;
1094             //Get SignalField,ServiceField,Length
1095             BBvCaculateParameter(pDevice, uCTSFrameLen, pDevice->byTopCCKBasicRate, PK_TYPE_11B,
1096                 (unsigned short *)&(wLen), (unsigned char *)&(pBuf->byServiceField_b), (unsigned char *)&(pBuf->bySignalField_b)
1097             );
1098             pBuf->wTransmitLength_b = cpu_to_le16(wLen);
1099             //Get CTSDuration_ba
1100             pBuf->wDuration_ba = cpu_to_le16((unsigned short)s_uGetRTSCTSDuration(pDevice, CTSDUR_BA, cbFrameLength, byPktType, wCurrentRate, bNeedAck, byFBOption)); //3:CTSDuration_ba, 1:2.4G, 2,3:2.4G OFDM Data
1101             pBuf->wDuration_ba += pDevice->wCTSDuration;
1102             pBuf->wDuration_ba = cpu_to_le16(pBuf->wDuration_ba);
1103 
1104             //Get CTS Frame body
1105             pBuf->Data.wDurationID = pBuf->wDuration_ba;
1106             pBuf->Data.wFrameControl = TYPE_CTL_CTS;//0x00C4
1107             pBuf->Data.wReserved = 0x0000;
1108             memcpy(&(pBuf->Data.abyRA[0]), &(pDevice->abyCurrentNetAddr[0]), ETH_ALEN);
1109         }
1110     }
1111 }
1112 
1113 
1114 
1115 
1116 
1117 
1118 /*+
1119  *
1120  * Description:
1121  *      Generate FIFO control for MAC & Baseband controller
1122  *
1123  * Parameters:
1124  *  In:
1125  *      pDevice         - Pointer to adapter
1126  *      pTxDataHead     - Transmit Data Buffer
1127  *      pTxBufHead      - pTxBufHead
1128  *      pvRrvTime        - pvRrvTime
1129  *      pvRTS            - RTS Buffer
1130  *      pCTS            - CTS Buffer
1131  *      cbFrameSize     - Transmit Data Length (Hdr+Payload+FCS)
1132  *      bNeedACK        - If need ACK
1133  *      uDescIdx        - Desc Index
1134  *  Out:
1135  *      none
1136  *
1137  * Return Value: none
1138  *
1139 -*/
1140 // unsigned int cbFrameSize,//Hdr+Payload+FCS
1141 static
1142 void
s_vGenerateTxParameter(PSDevice pDevice,unsigned char byPktType,void * pTxBufHead,void * pvRrvTime,void * pvRTS,void * pvCTS,unsigned int cbFrameSize,bool bNeedACK,unsigned int uDMAIdx,PSEthernetHeader psEthHeader,unsigned short wCurrentRate)1143 s_vGenerateTxParameter (
1144     PSDevice         pDevice,
1145     unsigned char byPktType,
1146     void *           pTxBufHead,
1147     void *           pvRrvTime,
1148     void *           pvRTS,
1149     void *           pvCTS,
1150     unsigned int cbFrameSize,
1151     bool bNeedACK,
1152     unsigned int uDMAIdx,
1153     PSEthernetHeader psEthHeader,
1154     unsigned short wCurrentRate
1155     )
1156 {
1157     unsigned int cbMACHdLen = WLAN_HDR_ADDR3_LEN; //24
1158     unsigned short wFifoCtl;
1159     bool bDisCRC = false;
1160     unsigned char byFBOption = AUTO_FB_NONE;
1161 //    unsigned short wCurrentRate = pDevice->wCurrentRate;
1162 
1163     //DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"s_vGenerateTxParameter...\n");
1164     PSTxBufHead pFifoHead = (PSTxBufHead)pTxBufHead;
1165     pFifoHead->wReserved = wCurrentRate;
1166     wFifoCtl = pFifoHead->wFIFOCtl;
1167 
1168     if (wFifoCtl & FIFOCTL_CRCDIS) {
1169         bDisCRC = true;
1170     }
1171 
1172     if (wFifoCtl & FIFOCTL_AUTO_FB_0) {
1173         byFBOption = AUTO_FB_0;
1174     }
1175     else if (wFifoCtl & FIFOCTL_AUTO_FB_1) {
1176         byFBOption = AUTO_FB_1;
1177     }
1178 
1179     if (pDevice->bLongHeader)
1180         cbMACHdLen = WLAN_HDR_ADDR3_LEN + 6;
1181 
1182     if (byPktType == PK_TYPE_11GB || byPktType == PK_TYPE_11GA) {
1183 
1184         if (pvRTS != NULL) { //RTS_need
1185             //Fill RsvTime
1186             if (pvRrvTime) {
1187                 PSRrvTime_gRTS pBuf = (PSRrvTime_gRTS)pvRrvTime;
1188                 pBuf->wRTSTxRrvTime_aa = cpu_to_le16((unsigned short)s_uGetRTSCTSRsvTime(pDevice, 2, byPktType, cbFrameSize, wCurrentRate));//2:RTSTxRrvTime_aa, 1:2.4GHz
1189                 pBuf->wRTSTxRrvTime_ba = cpu_to_le16((unsigned short)s_uGetRTSCTSRsvTime(pDevice, 1, byPktType, cbFrameSize, wCurrentRate));//1:RTSTxRrvTime_ba, 1:2.4GHz
1190                 pBuf->wRTSTxRrvTime_bb = cpu_to_le16((unsigned short)s_uGetRTSCTSRsvTime(pDevice, 0, byPktType, cbFrameSize, wCurrentRate));//0:RTSTxRrvTime_bb, 1:2.4GHz
1191                 pBuf->wTxRrvTime_a = cpu_to_le16((unsigned short) s_uGetTxRsvTime(pDevice, byPktType, cbFrameSize, wCurrentRate, bNeedACK));//2.4G OFDM
1192                 pBuf->wTxRrvTime_b = cpu_to_le16((unsigned short) s_uGetTxRsvTime(pDevice, PK_TYPE_11B, cbFrameSize, pDevice->byTopCCKBasicRate, bNeedACK));//1:CCK
1193             }
1194             //Fill RTS
1195             s_vFillRTSHead(pDevice, byPktType, pvRTS, cbFrameSize, bNeedACK, bDisCRC, psEthHeader, wCurrentRate, byFBOption);
1196         }
1197         else {//RTS_needless, PCF mode
1198 
1199             //Fill RsvTime
1200             if (pvRrvTime) {
1201                 PSRrvTime_gCTS pBuf = (PSRrvTime_gCTS)pvRrvTime;
1202                 pBuf->wTxRrvTime_a = cpu_to_le16((unsigned short)s_uGetTxRsvTime(pDevice, byPktType, cbFrameSize, wCurrentRate, bNeedACK));//2.4G OFDM
1203                 pBuf->wTxRrvTime_b = cpu_to_le16((unsigned short)s_uGetTxRsvTime(pDevice, PK_TYPE_11B, cbFrameSize, pDevice->byTopCCKBasicRate, bNeedACK));//1:CCK
1204                 pBuf->wCTSTxRrvTime_ba = cpu_to_le16((unsigned short)s_uGetRTSCTSRsvTime(pDevice, 3, byPktType, cbFrameSize, wCurrentRate));//3:CTSTxRrvTime_Ba, 1:2.4GHz
1205             }
1206 
1207 
1208             //Fill CTS
1209             s_vFillCTSHead(pDevice, uDMAIdx, byPktType, pvCTS, cbFrameSize, bNeedACK, bDisCRC, wCurrentRate, byFBOption);
1210         }
1211     }
1212     else if (byPktType == PK_TYPE_11A) {
1213 
1214         if (pvRTS != NULL) {//RTS_need, non PCF mode
1215             //Fill RsvTime
1216             if (pvRrvTime) {
1217                 PSRrvTime_ab pBuf = (PSRrvTime_ab)pvRrvTime;
1218                 pBuf->wRTSTxRrvTime = cpu_to_le16((unsigned short)s_uGetRTSCTSRsvTime(pDevice, 2, byPktType, cbFrameSize, wCurrentRate));//2:RTSTxRrvTime_aa, 0:5GHz
1219                 pBuf->wTxRrvTime = cpu_to_le16((unsigned short)s_uGetTxRsvTime(pDevice, byPktType, cbFrameSize, wCurrentRate, bNeedACK));//0:OFDM
1220             }
1221             //Fill RTS
1222             s_vFillRTSHead(pDevice, byPktType, pvRTS, cbFrameSize, bNeedACK, bDisCRC, psEthHeader, wCurrentRate, byFBOption);
1223         }
1224         else if (pvRTS == NULL) {//RTS_needless, non PCF mode
1225             //Fill RsvTime
1226             if (pvRrvTime) {
1227                 PSRrvTime_ab pBuf = (PSRrvTime_ab)pvRrvTime;
1228                 pBuf->wTxRrvTime = cpu_to_le16((unsigned short)s_uGetTxRsvTime(pDevice, PK_TYPE_11A, cbFrameSize, wCurrentRate, bNeedACK)); //0:OFDM
1229             }
1230         }
1231     }
1232     else if (byPktType == PK_TYPE_11B) {
1233 
1234         if ((pvRTS != NULL)) {//RTS_need, non PCF mode
1235             //Fill RsvTime
1236             if (pvRrvTime) {
1237                 PSRrvTime_ab pBuf = (PSRrvTime_ab)pvRrvTime;
1238                 pBuf->wRTSTxRrvTime = cpu_to_le16((unsigned short)s_uGetRTSCTSRsvTime(pDevice, 0, byPktType, cbFrameSize, wCurrentRate));//0:RTSTxRrvTime_bb, 1:2.4GHz
1239                 pBuf->wTxRrvTime = cpu_to_le16((unsigned short)s_uGetTxRsvTime(pDevice, PK_TYPE_11B, cbFrameSize, wCurrentRate, bNeedACK));//1:CCK
1240             }
1241             //Fill RTS
1242             s_vFillRTSHead(pDevice, byPktType, pvRTS, cbFrameSize, bNeedACK, bDisCRC, psEthHeader, wCurrentRate, byFBOption);
1243         }
1244         else { //RTS_needless, non PCF mode
1245             //Fill RsvTime
1246             if (pvRrvTime) {
1247                 PSRrvTime_ab pBuf = (PSRrvTime_ab)pvRrvTime;
1248                 pBuf->wTxRrvTime = cpu_to_le16((unsigned short)s_uGetTxRsvTime(pDevice, PK_TYPE_11B, cbFrameSize, wCurrentRate, bNeedACK)); //1:CCK
1249             }
1250         }
1251     }
1252     //DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"s_vGenerateTxParameter END.\n");
1253 }
1254 /*
1255     unsigned char *pbyBuffer,//point to pTxBufHead
1256     unsigned short wFragType,//00:Non-Frag, 01:Start, 02:Mid, 03:Last
1257     unsigned int cbFragmentSize,//Hdr+payoad+FCS
1258 */
1259 static
1260 void
s_vFillFragParameter(PSDevice pDevice,unsigned char * pbyBuffer,unsigned int uTxType,void * pvtdCurr,unsigned short wFragType,unsigned int cbReqCount)1261 s_vFillFragParameter(
1262     PSDevice pDevice,
1263     unsigned char *pbyBuffer,
1264     unsigned int uTxType,
1265     void *   pvtdCurr,
1266     unsigned short wFragType,
1267     unsigned int cbReqCount
1268     )
1269 {
1270     PSTxBufHead pTxBufHead = (PSTxBufHead) pbyBuffer;
1271     //DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"s_vFillFragParameter...\n");
1272 
1273     if (uTxType == TYPE_SYNCDMA) {
1274         //PSTxSyncDesc ptdCurr = (PSTxSyncDesc)s_pvGetTxDescHead(pDevice, uTxType, uCurIdx);
1275         PSTxSyncDesc ptdCurr = (PSTxSyncDesc)pvtdCurr;
1276 
1277          //Set FIFOCtl & TimeStamp in TxSyncDesc
1278         ptdCurr->m_wFIFOCtl = pTxBufHead->wFIFOCtl;
1279         ptdCurr->m_wTimeStamp = pTxBufHead->wTimeStamp;
1280         //Set TSR1 & ReqCount in TxDescHead
1281         ptdCurr->m_td1TD1.wReqCount = cpu_to_le16((unsigned short)(cbReqCount));
1282         if (wFragType == FRAGCTL_ENDFRAG) { //Last Fragmentation
1283             ptdCurr->m_td1TD1.byTCR |= (TCR_STP | TCR_EDP | EDMSDU);
1284         }
1285         else {
1286             ptdCurr->m_td1TD1.byTCR |= (TCR_STP | TCR_EDP);
1287         }
1288     }
1289     else {
1290         //PSTxDesc ptdCurr = (PSTxDesc)s_pvGetTxDescHead(pDevice, uTxType, uCurIdx);
1291         PSTxDesc ptdCurr = (PSTxDesc)pvtdCurr;
1292         //Set TSR1 & ReqCount in TxDescHead
1293         ptdCurr->m_td1TD1.wReqCount = cpu_to_le16((unsigned short)(cbReqCount));
1294         if (wFragType == FRAGCTL_ENDFRAG) { //Last Fragmentation
1295             ptdCurr->m_td1TD1.byTCR |= (TCR_STP | TCR_EDP | EDMSDU);
1296         }
1297         else {
1298             ptdCurr->m_td1TD1.byTCR |= (TCR_STP | TCR_EDP);
1299         }
1300     }
1301 
1302     pTxBufHead->wFragCtl |= (unsigned short)wFragType;//0x0001; //0000 0000 0000 0001
1303 
1304     //DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"s_vFillFragParameter END\n");
1305 }
1306 
1307 static unsigned int
s_cbFillTxBufHead(PSDevice pDevice,unsigned char byPktType,unsigned char * pbyTxBufferAddr,unsigned int cbFrameBodySize,unsigned int uDMAIdx,PSTxDesc pHeadTD,PSEthernetHeader psEthHeader,unsigned char * pPacket,bool bNeedEncrypt,PSKeyItem pTransmitKey,unsigned int uNodeIndex,unsigned int * puMACfragNum)1308 s_cbFillTxBufHead(PSDevice pDevice, unsigned char byPktType, unsigned char *pbyTxBufferAddr,
1309 	unsigned int cbFrameBodySize, unsigned int uDMAIdx, PSTxDesc pHeadTD,
1310 	PSEthernetHeader psEthHeader, unsigned char *pPacket, bool bNeedEncrypt,
1311 	PSKeyItem pTransmitKey, unsigned int uNodeIndex, unsigned int *puMACfragNum)
1312 {
1313     unsigned int cbMACHdLen;
1314     unsigned int cbFrameSize;
1315     unsigned int cbFragmentSize; //Hdr+(IV)+payoad+(MIC)+(ICV)+FCS
1316     unsigned int cbFragPayloadSize;
1317     unsigned int cbLastFragmentSize; //Hdr+(IV)+payoad+(MIC)+(ICV)+FCS
1318     unsigned int cbLastFragPayloadSize;
1319     unsigned int uFragIdx;
1320     unsigned char *pbyPayloadHead;
1321     unsigned char *pbyIVHead;
1322     unsigned char *pbyMacHdr;
1323     unsigned short wFragType; //00:Non-Frag, 01:Start, 10:Mid, 11:Last
1324     unsigned int uDuration;
1325     unsigned char *pbyBuffer;
1326 //    unsigned int uKeyEntryIdx = NUM_KEY_ENTRY+1;
1327 //    unsigned char byKeySel = 0xFF;
1328     unsigned int cbIVlen = 0;
1329     unsigned int cbICVlen = 0;
1330     unsigned int cbMIClen = 0;
1331     unsigned int cbFCSlen = 4;
1332     unsigned int cb802_1_H_len = 0;
1333     unsigned int uLength = 0;
1334     unsigned int uTmpLen = 0;
1335 //    unsigned char abyTmp[8];
1336 //    unsigned long dwCRC;
1337     unsigned int cbMICHDR = 0;
1338     unsigned long dwMICKey0, dwMICKey1;
1339     unsigned long dwMIC_Priority;
1340     unsigned long *pdwMIC_L;
1341     unsigned long *pdwMIC_R;
1342     unsigned long dwSafeMIC_L, dwSafeMIC_R; //Fix "Last Frag Size" < "MIC length".
1343     bool bMIC2Frag = false;
1344     unsigned int uMICFragLen = 0;
1345     unsigned int uMACfragNum = 1;
1346     unsigned int uPadding = 0;
1347     unsigned int cbReqCount = 0;
1348 
1349     bool bNeedACK;
1350     bool bRTS;
1351     bool bIsAdhoc;
1352     unsigned char *pbyType;
1353     PSTxDesc       ptdCurr;
1354     PSTxBufHead    psTxBufHd = (PSTxBufHead) pbyTxBufferAddr;
1355 //    unsigned int tmpDescIdx;
1356     unsigned int cbHeaderLength = 0;
1357     void *         pvRrvTime;
1358     PSMICHDRHead   pMICHDR;
1359     void *         pvRTS;
1360     void *         pvCTS;
1361     void *         pvTxDataHd;
1362     unsigned short wTxBufSize;   // FFinfo size
1363     unsigned int uTotalCopyLength = 0;
1364     unsigned char byFBOption = AUTO_FB_NONE;
1365     bool bIsWEP256 = false;
1366     PSMgmtObject    pMgmt = pDevice->pMgmt;
1367 
1368 
1369     pvRrvTime = pMICHDR = pvRTS = pvCTS = pvTxDataHd = NULL;
1370 
1371     //DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"s_cbFillTxBufHead...\n");
1372     if ((pDevice->eOPMode == OP_MODE_ADHOC) ||
1373         (pDevice->eOPMode == OP_MODE_AP)) {
1374 
1375 	if (is_multicast_ether_addr(&(psEthHeader->abyDstAddr[0])))
1376 		bNeedACK = false;
1377         else
1378             bNeedACK = true;
1379         bIsAdhoc = true;
1380     }
1381     else {
1382         // MSDUs in Infra mode always need ACK
1383         bNeedACK = true;
1384         bIsAdhoc = false;
1385     }
1386 
1387     if (pDevice->bLongHeader)
1388         cbMACHdLen = WLAN_HDR_ADDR3_LEN + 6;
1389     else
1390         cbMACHdLen = WLAN_HDR_ADDR3_LEN;
1391 
1392 
1393     if ((bNeedEncrypt == true) && (pTransmitKey != NULL)) {
1394         if (pTransmitKey->byCipherSuite == KEY_CTL_WEP) {
1395             cbIVlen = 4;
1396             cbICVlen = 4;
1397             if (pTransmitKey->uKeyLength == WLAN_WEP232_KEYLEN) {
1398                 bIsWEP256 = true;
1399             }
1400         }
1401         if (pTransmitKey->byCipherSuite == KEY_CTL_TKIP) {
1402             cbIVlen = 8;//IV+ExtIV
1403             cbMIClen = 8;
1404             cbICVlen = 4;
1405         }
1406         if (pTransmitKey->byCipherSuite == KEY_CTL_CCMP) {
1407             cbIVlen = 8;//RSN Header
1408             cbICVlen = 8;//MIC
1409             cbMICHDR = sizeof(SMICHDRHead);
1410         }
1411         if (pDevice->byLocalID > REV_ID_VT3253_A1) {
1412             //MAC Header should be padding 0 to DW alignment.
1413             uPadding = 4 - (cbMACHdLen%4);
1414             uPadding %= 4;
1415         }
1416     }
1417 
1418 
1419     cbFrameSize = cbMACHdLen + cbIVlen + (cbFrameBodySize + cbMIClen) + cbICVlen + cbFCSlen;
1420 
1421     if ((bNeedACK == false) ||
1422         (cbFrameSize < pDevice->wRTSThreshold) ||
1423         ((cbFrameSize >= pDevice->wFragmentationThreshold) && (pDevice->wFragmentationThreshold <= pDevice->wRTSThreshold))
1424         ) {
1425         bRTS = false;
1426     }
1427     else {
1428         bRTS = true;
1429         psTxBufHd->wFIFOCtl |= (FIFOCTL_RTS | FIFOCTL_LRETRY);
1430     }
1431     //
1432     // Use for AUTO FALL BACK
1433     //
1434     if (psTxBufHd->wFIFOCtl & FIFOCTL_AUTO_FB_0) {
1435         byFBOption = AUTO_FB_0;
1436     }
1437     else if (psTxBufHd->wFIFOCtl & FIFOCTL_AUTO_FB_1) {
1438         byFBOption = AUTO_FB_1;
1439     }
1440 
1441     //////////////////////////////////////////////////////
1442     //Set RrvTime/RTS/CTS Buffer
1443     wTxBufSize = sizeof(STxBufHead);
1444     if (byPktType == PK_TYPE_11GB || byPktType == PK_TYPE_11GA) {//802.11g packet
1445 
1446         if (byFBOption == AUTO_FB_NONE) {
1447             if (bRTS == true) {//RTS_need
1448                 pvRrvTime = (PSRrvTime_gRTS) (pbyTxBufferAddr + wTxBufSize);
1449                 pMICHDR = (PSMICHDRHead) (pbyTxBufferAddr + wTxBufSize + sizeof(SRrvTime_gRTS));
1450                 pvRTS = (PSRTS_g) (pbyTxBufferAddr + wTxBufSize + sizeof(SRrvTime_gRTS) + cbMICHDR);
1451                 pvCTS = NULL;
1452                 pvTxDataHd = (PSTxDataHead_g) (pbyTxBufferAddr + wTxBufSize + sizeof(SRrvTime_gRTS) + cbMICHDR + sizeof(SRTS_g));
1453                 cbHeaderLength = wTxBufSize + sizeof(SRrvTime_gRTS) + cbMICHDR + sizeof(SRTS_g) + sizeof(STxDataHead_g);
1454             }
1455             else { //RTS_needless
1456                 pvRrvTime = (PSRrvTime_gCTS) (pbyTxBufferAddr + wTxBufSize);
1457                 pMICHDR = (PSMICHDRHead) (pbyTxBufferAddr + wTxBufSize + sizeof(SRrvTime_gCTS));
1458                 pvRTS = NULL;
1459                 pvCTS = (PSCTS) (pbyTxBufferAddr + wTxBufSize + sizeof(SRrvTime_gCTS) + cbMICHDR);
1460                 pvTxDataHd = (PSTxDataHead_g) (pbyTxBufferAddr + wTxBufSize + sizeof(SRrvTime_gCTS) + cbMICHDR + sizeof(SCTS));
1461                 cbHeaderLength = wTxBufSize + sizeof(SRrvTime_gCTS) + cbMICHDR + sizeof(SCTS) + sizeof(STxDataHead_g);
1462             }
1463         } else {
1464             // Auto Fall Back
1465             if (bRTS == true) {//RTS_need
1466                 pvRrvTime = (PSRrvTime_gRTS) (pbyTxBufferAddr + wTxBufSize);
1467                 pMICHDR = (PSMICHDRHead) (pbyTxBufferAddr + wTxBufSize + sizeof(SRrvTime_gRTS));
1468                 pvRTS = (PSRTS_g_FB) (pbyTxBufferAddr + wTxBufSize + sizeof(SRrvTime_gRTS) + cbMICHDR);
1469                 pvCTS = NULL;
1470                 pvTxDataHd = (PSTxDataHead_g_FB) (pbyTxBufferAddr + wTxBufSize + sizeof(SRrvTime_gRTS) + cbMICHDR + sizeof(SRTS_g_FB));
1471                 cbHeaderLength = wTxBufSize + sizeof(SRrvTime_gRTS) + cbMICHDR + sizeof(SRTS_g_FB) + sizeof(STxDataHead_g_FB);
1472             }
1473             else { //RTS_needless
1474                 pvRrvTime = (PSRrvTime_gCTS) (pbyTxBufferAddr + wTxBufSize);
1475                 pMICHDR = (PSMICHDRHead) (pbyTxBufferAddr + wTxBufSize + sizeof(SRrvTime_gCTS));
1476                 pvRTS = NULL;
1477                 pvCTS = (PSCTS_FB) (pbyTxBufferAddr + wTxBufSize + sizeof(SRrvTime_gCTS) + cbMICHDR);
1478                 pvTxDataHd = (PSTxDataHead_g_FB) (pbyTxBufferAddr + wTxBufSize + sizeof(SRrvTime_gCTS) + cbMICHDR + sizeof(SCTS_FB));
1479                 cbHeaderLength = wTxBufSize + sizeof(SRrvTime_gCTS) + cbMICHDR + sizeof(SCTS_FB) + sizeof(STxDataHead_g_FB);
1480             }
1481         } // Auto Fall Back
1482     }
1483     else {//802.11a/b packet
1484 
1485         if (byFBOption == AUTO_FB_NONE) {
1486             if (bRTS == true) {
1487                 pvRrvTime = (PSRrvTime_ab) (pbyTxBufferAddr + wTxBufSize);
1488                 pMICHDR = (PSMICHDRHead) (pbyTxBufferAddr + wTxBufSize + sizeof(SRrvTime_ab));
1489                 pvRTS = (PSRTS_ab) (pbyTxBufferAddr + wTxBufSize + sizeof(SRrvTime_ab) + cbMICHDR);
1490                 pvCTS = NULL;
1491                 pvTxDataHd = (PSTxDataHead_ab) (pbyTxBufferAddr + wTxBufSize + sizeof(PSRrvTime_ab) + cbMICHDR + sizeof(SRTS_ab));
1492                 cbHeaderLength = wTxBufSize + sizeof(PSRrvTime_ab) + cbMICHDR + sizeof(SRTS_ab) + sizeof(STxDataHead_ab);
1493             }
1494             else { //RTS_needless, need MICHDR
1495                 pvRrvTime = (PSRrvTime_ab) (pbyTxBufferAddr + wTxBufSize);
1496                 pMICHDR = (PSMICHDRHead) (pbyTxBufferAddr + wTxBufSize + sizeof(SRrvTime_ab));
1497                 pvRTS = NULL;
1498                 pvCTS = NULL;
1499                 pvTxDataHd = (PSTxDataHead_ab) (pbyTxBufferAddr + wTxBufSize + sizeof(SRrvTime_ab) + cbMICHDR);
1500                 cbHeaderLength = wTxBufSize + sizeof(SRrvTime_ab) + cbMICHDR + sizeof(STxDataHead_ab);
1501             }
1502         } else {
1503             // Auto Fall Back
1504             if (bRTS == true) {//RTS_need
1505                 pvRrvTime = (PSRrvTime_ab) (pbyTxBufferAddr + wTxBufSize);
1506                 pMICHDR = (PSMICHDRHead) (pbyTxBufferAddr + wTxBufSize + sizeof(SRrvTime_ab));
1507                 pvRTS = (PSRTS_a_FB) (pbyTxBufferAddr + wTxBufSize + sizeof(SRrvTime_ab) + cbMICHDR);
1508                 pvCTS = NULL;
1509                 pvTxDataHd = (PSTxDataHead_a_FB) (pbyTxBufferAddr + wTxBufSize + sizeof(PSRrvTime_ab) + cbMICHDR + sizeof(SRTS_a_FB));
1510                 cbHeaderLength = wTxBufSize + sizeof(PSRrvTime_ab) + cbMICHDR + sizeof(SRTS_a_FB) + sizeof(STxDataHead_a_FB);
1511             }
1512             else { //RTS_needless
1513                 pvRrvTime = (PSRrvTime_ab) (pbyTxBufferAddr + wTxBufSize);
1514                 pMICHDR = (PSMICHDRHead) (pbyTxBufferAddr + wTxBufSize + sizeof(SRrvTime_ab));
1515                 pvRTS = NULL;
1516                 pvCTS = NULL;
1517                 pvTxDataHd = (PSTxDataHead_a_FB) (pbyTxBufferAddr + wTxBufSize + sizeof(SRrvTime_ab) + cbMICHDR);
1518                 cbHeaderLength = wTxBufSize + sizeof(SRrvTime_ab) + cbMICHDR + sizeof(STxDataHead_a_FB);
1519             }
1520         } // Auto Fall Back
1521     }
1522     memset((void *)(pbyTxBufferAddr + wTxBufSize), 0, (cbHeaderLength - wTxBufSize));
1523 
1524 //////////////////////////////////////////////////////////////////
1525     if ((bNeedEncrypt == true) && (pTransmitKey != NULL) && (pTransmitKey->byCipherSuite == KEY_CTL_TKIP)) {
1526         if (pDevice->pMgmt->eAuthenMode == WMAC_AUTH_WPANONE) {
1527             dwMICKey0 = *(unsigned long *)(&pTransmitKey->abyKey[16]);
1528             dwMICKey1 = *(unsigned long *)(&pTransmitKey->abyKey[20]);
1529         }
1530         else if ((pTransmitKey->dwKeyIndex & AUTHENTICATOR_KEY) != 0) {
1531             dwMICKey0 = *(unsigned long *)(&pTransmitKey->abyKey[16]);
1532             dwMICKey1 = *(unsigned long *)(&pTransmitKey->abyKey[20]);
1533         }
1534         else {
1535             dwMICKey0 = *(unsigned long *)(&pTransmitKey->abyKey[24]);
1536             dwMICKey1 = *(unsigned long *)(&pTransmitKey->abyKey[28]);
1537         }
1538         // DO Software Michael
1539         MIC_vInit(dwMICKey0, dwMICKey1);
1540         MIC_vAppend((unsigned char *)&(psEthHeader->abyDstAddr[0]), 12);
1541         dwMIC_Priority = 0;
1542         MIC_vAppend((unsigned char *)&dwMIC_Priority, 4);
1543         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"MIC KEY: %lX, %lX\n", dwMICKey0, dwMICKey1);
1544     }
1545 
1546 ///////////////////////////////////////////////////////////////////
1547 
1548     pbyMacHdr = (unsigned char *)(pbyTxBufferAddr + cbHeaderLength);
1549     pbyPayloadHead = (unsigned char *)(pbyMacHdr + cbMACHdLen + uPadding + cbIVlen);
1550     pbyIVHead = (unsigned char *)(pbyMacHdr + cbMACHdLen + uPadding);
1551 
1552     if ((cbFrameSize > pDevice->wFragmentationThreshold) && (bNeedACK == true) && (bIsWEP256 == false)) {
1553         // Fragmentation
1554         // FragThreshold = Fragment size(Hdr+(IV)+fragment payload+(MIC)+(ICV)+FCS)
1555         cbFragmentSize = pDevice->wFragmentationThreshold;
1556         cbFragPayloadSize = cbFragmentSize - cbMACHdLen - cbIVlen - cbICVlen - cbFCSlen;
1557         //FragNum = (FrameSize-(Hdr+FCS))/(Fragment Size -(Hrd+FCS)))
1558         uMACfragNum = (unsigned short) ((cbFrameBodySize + cbMIClen) / cbFragPayloadSize);
1559         cbLastFragPayloadSize = (cbFrameBodySize + cbMIClen) % cbFragPayloadSize;
1560         if (cbLastFragPayloadSize == 0) {
1561             cbLastFragPayloadSize = cbFragPayloadSize;
1562         } else {
1563             uMACfragNum++;
1564         }
1565         //[Hdr+(IV)+last fragment payload+(MIC)+(ICV)+FCS]
1566         cbLastFragmentSize = cbMACHdLen + cbLastFragPayloadSize + cbIVlen + cbICVlen + cbFCSlen;
1567 
1568         for (uFragIdx = 0; uFragIdx < uMACfragNum; uFragIdx ++) {
1569             if (uFragIdx == 0) {
1570                 //=========================
1571                 //    Start Fragmentation
1572                 //=========================
1573                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Start Fragmentation...\n");
1574                 wFragType = FRAGCTL_STAFRAG;
1575 
1576 
1577                 //Fill FIFO,RrvTime,RTS,and CTS
1578                 s_vGenerateTxParameter(pDevice, byPktType, (void *)psTxBufHd, pvRrvTime, pvRTS, pvCTS,
1579                                        cbFragmentSize, bNeedACK, uDMAIdx, psEthHeader, pDevice->wCurrentRate);
1580                 //Fill DataHead
1581                 uDuration = s_uFillDataHead(pDevice, byPktType, pvTxDataHd, cbFragmentSize, uDMAIdx, bNeedACK,
1582                                             uFragIdx, cbLastFragmentSize, uMACfragNum, byFBOption, pDevice->wCurrentRate);
1583                 // Generate TX MAC Header
1584                 vGenerateMACHeader(pDevice, pbyMacHdr, (unsigned short)uDuration, psEthHeader, bNeedEncrypt,
1585                                    wFragType, uDMAIdx, uFragIdx);
1586 
1587                 if (bNeedEncrypt == true) {
1588                     //Fill TXKEY
1589                     s_vFillTxKey(pDevice, (unsigned char *)(psTxBufHd->adwTxKey), pbyIVHead, pTransmitKey,
1590                                  pbyMacHdr, (unsigned short)cbFragPayloadSize, (unsigned char *)pMICHDR);
1591                     //Fill IV(ExtIV,RSNHDR)
1592                     if (pDevice->bEnableHostWEP) {
1593                         pMgmt->sNodeDBTable[uNodeIndex].dwTSC47_16 = pTransmitKey->dwTSC47_16;
1594                         pMgmt->sNodeDBTable[uNodeIndex].wTSC15_0 = pTransmitKey->wTSC15_0;
1595                     }
1596                 }
1597 
1598 
1599                 // 802.1H
1600                 if (ntohs(psEthHeader->wType) > ETH_DATA_LEN) {
1601                     if ((psEthHeader->wType == TYPE_PKT_IPX) ||
1602                         (psEthHeader->wType == cpu_to_le16(0xF380))) {
1603                         memcpy((unsigned char *) (pbyPayloadHead), &pDevice->abySNAP_Bridgetunnel[0], 6);
1604                     }
1605                     else {
1606                         memcpy((unsigned char *) (pbyPayloadHead), &pDevice->abySNAP_RFC1042[0], 6);
1607                     }
1608                     pbyType = (unsigned char *) (pbyPayloadHead + 6);
1609                     memcpy(pbyType, &(psEthHeader->wType), sizeof(unsigned short));
1610                     cb802_1_H_len = 8;
1611                 }
1612 
1613                 cbReqCount = cbHeaderLength + cbMACHdLen + uPadding + cbIVlen + cbFragPayloadSize;
1614                 //---------------------------
1615                 // S/W or H/W Encryption
1616                 //---------------------------
1617                 //Fill MICHDR
1618                 //if (pDevice->bAES) {
1619                 //    s_vFillMICHDR(pDevice, (unsigned char *)pMICHDR, pbyMacHdr, (unsigned short)cbFragPayloadSize);
1620                 //}
1621                 //cbReqCount += s_uDoEncryption(pDevice, psEthHeader, (void *)psTxBufHd, byKeySel,
1622                 //                                pbyPayloadHead, (unsigned short)cbFragPayloadSize, uDMAIdx);
1623 
1624 
1625 
1626                 //pbyBuffer = (unsigned char *)pDevice->aamTxBuf[uDMAIdx][uDescIdx].pbyVAddr;
1627                 pbyBuffer = (unsigned char *)pHeadTD->pTDInfo->buf;
1628 
1629                 uLength = cbHeaderLength + cbMACHdLen + uPadding + cbIVlen + cb802_1_H_len;
1630                 //copy TxBufferHeader + MacHeader to desc
1631                 memcpy(pbyBuffer, (void *)psTxBufHd, uLength);
1632 
1633                 // Copy the Packet into a tx Buffer
1634                 memcpy((pbyBuffer + uLength), (pPacket + 14), (cbFragPayloadSize - cb802_1_H_len));
1635 
1636 
1637                 uTotalCopyLength += cbFragPayloadSize - cb802_1_H_len;
1638 
1639                 if ((bNeedEncrypt == true) && (pTransmitKey != NULL) && (pTransmitKey->byCipherSuite == KEY_CTL_TKIP)) {
1640                     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Start MIC: %d\n", cbFragPayloadSize);
1641                     MIC_vAppend((pbyBuffer + uLength - cb802_1_H_len), cbFragPayloadSize);
1642 
1643                 }
1644 
1645                 //---------------------------
1646                 // S/W Encryption
1647                 //---------------------------
1648                 if ((pDevice->byLocalID <= REV_ID_VT3253_A1)) {
1649                     if (bNeedEncrypt) {
1650                         s_vSWencryption(pDevice, pTransmitKey, (pbyBuffer + uLength - cb802_1_H_len), (unsigned short)cbFragPayloadSize);
1651                         cbReqCount += cbICVlen;
1652                     }
1653                 }
1654 
1655                 ptdCurr = (PSTxDesc)pHeadTD;
1656                 //--------------------
1657                 //1.Set TSR1 & ReqCount in TxDescHead
1658                 //2.Set FragCtl in TxBufferHead
1659                 //3.Set Frame Control
1660                 //4.Set Sequence Control
1661                 //5.Get S/W generate FCS
1662                 //--------------------
1663                 s_vFillFragParameter(pDevice, pbyBuffer, uDMAIdx, (void *)ptdCurr, wFragType, cbReqCount);
1664 
1665                 ptdCurr->pTDInfo->dwReqCount = cbReqCount - uPadding;
1666                 ptdCurr->pTDInfo->dwHeaderLength = cbHeaderLength;
1667                 ptdCurr->pTDInfo->skb_dma = ptdCurr->pTDInfo->buf_dma;
1668                 ptdCurr->buff_addr = cpu_to_le32(ptdCurr->pTDInfo->skb_dma);
1669                 pDevice->iTDUsed[uDMAIdx]++;
1670                 pHeadTD = ptdCurr->next;
1671             }
1672             else if (uFragIdx == (uMACfragNum-1)) {
1673                 //=========================
1674                 //    Last Fragmentation
1675                 //=========================
1676                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Last Fragmentation...\n");
1677                 //tmpDescIdx = (uDescIdx + uFragIdx) % pDevice->cbTD[uDMAIdx];
1678 
1679                 wFragType = FRAGCTL_ENDFRAG;
1680 
1681                 //Fill FIFO,RrvTime,RTS,and CTS
1682                 s_vGenerateTxParameter(pDevice, byPktType, (void *)psTxBufHd, pvRrvTime, pvRTS, pvCTS,
1683                                        cbLastFragmentSize, bNeedACK, uDMAIdx, psEthHeader, pDevice->wCurrentRate);
1684                 //Fill DataHead
1685                 uDuration = s_uFillDataHead(pDevice, byPktType, pvTxDataHd, cbLastFragmentSize, uDMAIdx, bNeedACK,
1686                                             uFragIdx, cbLastFragmentSize, uMACfragNum, byFBOption, pDevice->wCurrentRate);
1687 
1688                 // Generate TX MAC Header
1689                 vGenerateMACHeader(pDevice, pbyMacHdr, (unsigned short)uDuration, psEthHeader, bNeedEncrypt,
1690                                    wFragType, uDMAIdx, uFragIdx);
1691 
1692                 if (bNeedEncrypt == true) {
1693                     //Fill TXKEY
1694                     s_vFillTxKey(pDevice, (unsigned char *)(psTxBufHd->adwTxKey), pbyIVHead, pTransmitKey,
1695                                  pbyMacHdr, (unsigned short)cbLastFragPayloadSize, (unsigned char *)pMICHDR);
1696 
1697                     if (pDevice->bEnableHostWEP) {
1698                         pMgmt->sNodeDBTable[uNodeIndex].dwTSC47_16 = pTransmitKey->dwTSC47_16;
1699                         pMgmt->sNodeDBTable[uNodeIndex].wTSC15_0 = pTransmitKey->wTSC15_0;
1700                     }
1701 
1702                 }
1703 
1704 
1705                 cbReqCount = cbHeaderLength + cbMACHdLen + uPadding + cbIVlen + cbLastFragPayloadSize;
1706                 //---------------------------
1707                 // S/W or H/W Encryption
1708                 //---------------------------
1709 
1710 
1711 
1712                 pbyBuffer = (unsigned char *)pHeadTD->pTDInfo->buf;
1713                 //pbyBuffer = (unsigned char *)pDevice->aamTxBuf[uDMAIdx][tmpDescIdx].pbyVAddr;
1714 
1715                 uLength = cbHeaderLength + cbMACHdLen + uPadding + cbIVlen;
1716 
1717                 //copy TxBufferHeader + MacHeader to desc
1718                 memcpy(pbyBuffer, (void *)psTxBufHd, uLength);
1719 
1720                 // Copy the Packet into a tx Buffer
1721                 if (bMIC2Frag == false) {
1722 
1723                     memcpy((pbyBuffer + uLength),
1724                              (pPacket + 14 + uTotalCopyLength),
1725                              (cbLastFragPayloadSize - cbMIClen)
1726                              );
1727                     //TODO check uTmpLen !
1728                     uTmpLen = cbLastFragPayloadSize - cbMIClen;
1729 
1730                 }
1731                 if ((bNeedEncrypt == true) && (pTransmitKey != NULL) && (pTransmitKey->byCipherSuite == KEY_CTL_TKIP)) {
1732                     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"LAST: uMICFragLen:%d, cbLastFragPayloadSize:%d, uTmpLen:%d\n",
1733                                    uMICFragLen, cbLastFragPayloadSize, uTmpLen);
1734 
1735                     if (bMIC2Frag == false) {
1736                         if (uTmpLen != 0)
1737                             MIC_vAppend((pbyBuffer + uLength), uTmpLen);
1738                         pdwMIC_L = (unsigned long *)(pbyBuffer + uLength + uTmpLen);
1739                         pdwMIC_R = (unsigned long *)(pbyBuffer + uLength + uTmpLen + 4);
1740                         MIC_vGetMIC(pdwMIC_L, pdwMIC_R);
1741                         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Last MIC:%lX, %lX\n", *pdwMIC_L, *pdwMIC_R);
1742                     } else {
1743                         if (uMICFragLen >= 4) {
1744                             memcpy((pbyBuffer + uLength), ((unsigned char *)&dwSafeMIC_R + (uMICFragLen - 4)),
1745                                      (cbMIClen - uMICFragLen));
1746                             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"LAST: uMICFragLen >= 4: %X, %d\n",
1747                                            *(unsigned char *)((unsigned char *)&dwSafeMIC_R + (uMICFragLen - 4)),
1748                                            (cbMIClen - uMICFragLen));
1749 
1750                         } else {
1751                             memcpy((pbyBuffer + uLength), ((unsigned char *)&dwSafeMIC_L + uMICFragLen),
1752                                      (4 - uMICFragLen));
1753                             memcpy((pbyBuffer + uLength + (4 - uMICFragLen)), &dwSafeMIC_R, 4);
1754                             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"LAST: uMICFragLen < 4: %X, %d\n",
1755                                            *(unsigned char *)((unsigned char *)&dwSafeMIC_R + uMICFragLen - 4),
1756                                            (cbMIClen - uMICFragLen));
1757                         }
1758                         /*
1759                         for (ii = 0; ii < cbLastFragPayloadSize + 8 + 24; ii++) {
1760                             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"%02x ", *((unsigned char *)((pbyBuffer + uLength) + ii - 8 - 24)));
1761                         }
1762                         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"\n\n");
1763                         */
1764                     }
1765                     MIC_vUnInit();
1766                 } else {
1767                     ASSERT(uTmpLen == (cbLastFragPayloadSize - cbMIClen));
1768                 }
1769 
1770 
1771                 //---------------------------
1772                 // S/W Encryption
1773                 //---------------------------
1774                 if ((pDevice->byLocalID <= REV_ID_VT3253_A1)) {
1775                     if (bNeedEncrypt) {
1776                         s_vSWencryption(pDevice, pTransmitKey, (pbyBuffer + uLength), (unsigned short)cbLastFragPayloadSize);
1777                         cbReqCount += cbICVlen;
1778                     }
1779                 }
1780 
1781                 ptdCurr = (PSTxDesc)pHeadTD;
1782 
1783                 //--------------------
1784                 //1.Set TSR1 & ReqCount in TxDescHead
1785                 //2.Set FragCtl in TxBufferHead
1786                 //3.Set Frame Control
1787                 //4.Set Sequence Control
1788                 //5.Get S/W generate FCS
1789                 //--------------------
1790 
1791 
1792                 s_vFillFragParameter(pDevice, pbyBuffer, uDMAIdx, (void *)ptdCurr, wFragType, cbReqCount);
1793 
1794                 ptdCurr->pTDInfo->dwReqCount = cbReqCount - uPadding;
1795                 ptdCurr->pTDInfo->dwHeaderLength = cbHeaderLength;
1796                 ptdCurr->pTDInfo->skb_dma = ptdCurr->pTDInfo->buf_dma;
1797                 ptdCurr->buff_addr = cpu_to_le32(ptdCurr->pTDInfo->skb_dma);
1798                 pDevice->iTDUsed[uDMAIdx]++;
1799                 pHeadTD = ptdCurr->next;
1800 
1801             }
1802             else {
1803                 //=========================
1804                 //    Middle Fragmentation
1805                 //=========================
1806                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Middle Fragmentation...\n");
1807                 //tmpDescIdx = (uDescIdx + uFragIdx) % pDevice->cbTD[uDMAIdx];
1808 
1809                 wFragType = FRAGCTL_MIDFRAG;
1810 
1811                 //Fill FIFO,RrvTime,RTS,and CTS
1812                 s_vGenerateTxParameter(pDevice, byPktType, (void *)psTxBufHd, pvRrvTime, pvRTS, pvCTS,
1813                                        cbFragmentSize, bNeedACK, uDMAIdx, psEthHeader, pDevice->wCurrentRate);
1814                 //Fill DataHead
1815                 uDuration = s_uFillDataHead(pDevice, byPktType, pvTxDataHd, cbFragmentSize, uDMAIdx, bNeedACK,
1816                                             uFragIdx, cbLastFragmentSize, uMACfragNum, byFBOption, pDevice->wCurrentRate);
1817 
1818                 // Generate TX MAC Header
1819                 vGenerateMACHeader(pDevice, pbyMacHdr, (unsigned short)uDuration, psEthHeader, bNeedEncrypt,
1820                                    wFragType, uDMAIdx, uFragIdx);
1821 
1822 
1823                 if (bNeedEncrypt == true) {
1824                     //Fill TXKEY
1825                     s_vFillTxKey(pDevice, (unsigned char *)(psTxBufHd->adwTxKey), pbyIVHead, pTransmitKey,
1826                                  pbyMacHdr, (unsigned short)cbFragPayloadSize, (unsigned char *)pMICHDR);
1827 
1828                     if (pDevice->bEnableHostWEP) {
1829                         pMgmt->sNodeDBTable[uNodeIndex].dwTSC47_16 = pTransmitKey->dwTSC47_16;
1830                         pMgmt->sNodeDBTable[uNodeIndex].wTSC15_0 = pTransmitKey->wTSC15_0;
1831                     }
1832                 }
1833 
1834                 cbReqCount = cbHeaderLength + cbMACHdLen + uPadding + cbIVlen + cbFragPayloadSize;
1835                 //---------------------------
1836                 // S/W or H/W Encryption
1837                 //---------------------------
1838                 //Fill MICHDR
1839                 //if (pDevice->bAES) {
1840                 //    s_vFillMICHDR(pDevice, (unsigned char *)pMICHDR, pbyMacHdr, (unsigned short)cbFragPayloadSize);
1841                 //}
1842                 //cbReqCount += s_uDoEncryption(pDevice, psEthHeader, (void *)psTxBufHd, byKeySel,
1843                 //                              pbyPayloadHead, (unsigned short)cbFragPayloadSize, uDMAIdx);
1844 
1845 
1846                 pbyBuffer = (unsigned char *)pHeadTD->pTDInfo->buf;
1847                 //pbyBuffer = (unsigned char *)pDevice->aamTxBuf[uDMAIdx][tmpDescIdx].pbyVAddr;
1848 
1849 
1850                 uLength = cbHeaderLength + cbMACHdLen + uPadding + cbIVlen;
1851 
1852                 //copy TxBufferHeader + MacHeader to desc
1853                 memcpy(pbyBuffer, (void *)psTxBufHd, uLength);
1854 
1855                 // Copy the Packet into a tx Buffer
1856                 memcpy((pbyBuffer + uLength),
1857                          (pPacket + 14 + uTotalCopyLength),
1858                          cbFragPayloadSize
1859                         );
1860                 uTmpLen = cbFragPayloadSize;
1861 
1862                 uTotalCopyLength += uTmpLen;
1863 
1864                 if ((bNeedEncrypt == true) && (pTransmitKey != NULL) && (pTransmitKey->byCipherSuite == KEY_CTL_TKIP)) {
1865 
1866                     MIC_vAppend((pbyBuffer + uLength), uTmpLen);
1867 
1868                     if (uTmpLen < cbFragPayloadSize) {
1869                         bMIC2Frag = true;
1870                         uMICFragLen = cbFragPayloadSize - uTmpLen;
1871                         ASSERT(uMICFragLen < cbMIClen);
1872 
1873                         pdwMIC_L = (unsigned long *)(pbyBuffer + uLength + uTmpLen);
1874                         pdwMIC_R = (unsigned long *)(pbyBuffer + uLength + uTmpLen + 4);
1875                         MIC_vGetMIC(pdwMIC_L, pdwMIC_R);
1876                         dwSafeMIC_L = *pdwMIC_L;
1877                         dwSafeMIC_R = *pdwMIC_R;
1878 
1879                         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"MIDDLE: uMICFragLen:%d, cbFragPayloadSize:%d, uTmpLen:%d\n",
1880                                        uMICFragLen, cbFragPayloadSize, uTmpLen);
1881                         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Fill MIC in Middle frag [%d]\n", uMICFragLen);
1882                         /*
1883                         for (ii = 0; ii < uMICFragLen; ii++) {
1884                             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"%02x ", *((unsigned char *)((pbyBuffer + uLength + uTmpLen) + ii)));
1885                         }
1886                         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"\n");
1887                         */
1888                         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Get MIC:%lX, %lX\n", *pdwMIC_L, *pdwMIC_R);
1889                     }
1890                     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Middle frag len: %d\n", uTmpLen);
1891                     /*
1892                     for (ii = 0; ii < uTmpLen; ii++) {
1893                         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"%02x ", *((unsigned char *)((pbyBuffer + uLength) + ii)));
1894                     }
1895                     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"\n\n");
1896                     */
1897 
1898                 } else {
1899                     ASSERT(uTmpLen == (cbFragPayloadSize));
1900                 }
1901 
1902                 if ((pDevice->byLocalID <= REV_ID_VT3253_A1)) {
1903                     if (bNeedEncrypt) {
1904                         s_vSWencryption(pDevice, pTransmitKey, (pbyBuffer + uLength), (unsigned short)cbFragPayloadSize);
1905                         cbReqCount += cbICVlen;
1906                     }
1907                 }
1908 
1909                 ptdCurr = (PSTxDesc)pHeadTD;
1910 
1911                 //--------------------
1912                 //1.Set TSR1 & ReqCount in TxDescHead
1913                 //2.Set FragCtl in TxBufferHead
1914                 //3.Set Frame Control
1915                 //4.Set Sequence Control
1916                 //5.Get S/W generate FCS
1917                 //--------------------
1918 
1919                 s_vFillFragParameter(pDevice, pbyBuffer, uDMAIdx, (void *)ptdCurr, wFragType, cbReqCount);
1920 
1921                 ptdCurr->pTDInfo->dwReqCount = cbReqCount - uPadding;
1922                 ptdCurr->pTDInfo->dwHeaderLength = cbHeaderLength;
1923                 ptdCurr->pTDInfo->skb_dma = ptdCurr->pTDInfo->buf_dma;
1924                 ptdCurr->buff_addr = cpu_to_le32(ptdCurr->pTDInfo->skb_dma);
1925                 pDevice->iTDUsed[uDMAIdx]++;
1926                 pHeadTD = ptdCurr->next;
1927             }
1928         }  // for (uMACfragNum)
1929     }
1930     else {
1931         //=========================
1932         //    No Fragmentation
1933         //=========================
1934         //DBG_PRTGRP03(("No Fragmentation...\n"));
1935         //DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"No Fragmentation...\n");
1936         wFragType = FRAGCTL_NONFRAG;
1937 
1938         //Set FragCtl in TxBufferHead
1939         psTxBufHd->wFragCtl |= (unsigned short)wFragType;
1940 
1941         //Fill FIFO,RrvTime,RTS,and CTS
1942         s_vGenerateTxParameter(pDevice, byPktType, (void *)psTxBufHd, pvRrvTime, pvRTS, pvCTS,
1943                                cbFrameSize, bNeedACK, uDMAIdx, psEthHeader, pDevice->wCurrentRate);
1944         //Fill DataHead
1945         uDuration = s_uFillDataHead(pDevice, byPktType, pvTxDataHd, cbFrameSize, uDMAIdx, bNeedACK,
1946                                     0, 0, uMACfragNum, byFBOption, pDevice->wCurrentRate);
1947 
1948         // Generate TX MAC Header
1949         vGenerateMACHeader(pDevice, pbyMacHdr, (unsigned short)uDuration, psEthHeader, bNeedEncrypt,
1950                            wFragType, uDMAIdx, 0);
1951 
1952         if (bNeedEncrypt == true) {
1953             //Fill TXKEY
1954             s_vFillTxKey(pDevice, (unsigned char *)(psTxBufHd->adwTxKey), pbyIVHead, pTransmitKey,
1955                          pbyMacHdr, (unsigned short)cbFrameBodySize, (unsigned char *)pMICHDR);
1956 
1957             if (pDevice->bEnableHostWEP) {
1958                 pMgmt->sNodeDBTable[uNodeIndex].dwTSC47_16 = pTransmitKey->dwTSC47_16;
1959                 pMgmt->sNodeDBTable[uNodeIndex].wTSC15_0 = pTransmitKey->wTSC15_0;
1960             }
1961         }
1962 
1963         // 802.1H
1964         if (ntohs(psEthHeader->wType) > ETH_DATA_LEN) {
1965             if ((psEthHeader->wType == TYPE_PKT_IPX) ||
1966                 (psEthHeader->wType == cpu_to_le16(0xF380))) {
1967                 memcpy((unsigned char *) (pbyPayloadHead), &pDevice->abySNAP_Bridgetunnel[0], 6);
1968             }
1969             else {
1970                 memcpy((unsigned char *) (pbyPayloadHead), &pDevice->abySNAP_RFC1042[0], 6);
1971             }
1972             pbyType = (unsigned char *) (pbyPayloadHead + 6);
1973             memcpy(pbyType, &(psEthHeader->wType), sizeof(unsigned short));
1974             cb802_1_H_len = 8;
1975         }
1976 
1977         cbReqCount = cbHeaderLength + cbMACHdLen + uPadding + cbIVlen + (cbFrameBodySize + cbMIClen);
1978         //---------------------------
1979         // S/W or H/W Encryption
1980         //---------------------------
1981         //Fill MICHDR
1982         //if (pDevice->bAES) {
1983         //    DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Fill MICHDR...\n");
1984         //    s_vFillMICHDR(pDevice, (unsigned char *)pMICHDR, pbyMacHdr, (unsigned short)cbFrameBodySize);
1985         //}
1986 
1987         pbyBuffer = (unsigned char *)pHeadTD->pTDInfo->buf;
1988         //pbyBuffer = (unsigned char *)pDevice->aamTxBuf[uDMAIdx][uDescIdx].pbyVAddr;
1989 
1990         uLength = cbHeaderLength + cbMACHdLen + uPadding + cbIVlen + cb802_1_H_len;
1991 
1992         //copy TxBufferHeader + MacHeader to desc
1993         memcpy(pbyBuffer, (void *)psTxBufHd, uLength);
1994 
1995         // Copy the Packet into a tx Buffer
1996         memcpy((pbyBuffer + uLength),
1997                  (pPacket + 14),
1998                  cbFrameBodySize - cb802_1_H_len
1999                  );
2000 
2001         if ((bNeedEncrypt == true) && (pTransmitKey != NULL) && (pTransmitKey->byCipherSuite == KEY_CTL_TKIP)){
2002 
2003             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Length:%d, %d\n", cbFrameBodySize - cb802_1_H_len, uLength);
2004             /*
2005             for (ii = 0; ii < (cbFrameBodySize - cb802_1_H_len); ii++) {
2006                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"%02x ", *((unsigned char *)((pbyBuffer + uLength) + ii)));
2007             }
2008             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"\n");
2009             */
2010 
2011             MIC_vAppend((pbyBuffer + uLength - cb802_1_H_len), cbFrameBodySize);
2012 
2013             pdwMIC_L = (unsigned long *)(pbyBuffer + uLength - cb802_1_H_len + cbFrameBodySize);
2014             pdwMIC_R = (unsigned long *)(pbyBuffer + uLength - cb802_1_H_len + cbFrameBodySize + 4);
2015 
2016             MIC_vGetMIC(pdwMIC_L, pdwMIC_R);
2017             MIC_vUnInit();
2018 
2019 
2020             if (pDevice->bTxMICFail == true) {
2021                 *pdwMIC_L = 0;
2022                 *pdwMIC_R = 0;
2023                 pDevice->bTxMICFail = false;
2024             }
2025 
2026             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"uLength: %d, %d\n", uLength, cbFrameBodySize);
2027             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"cbReqCount:%d, %d, %d, %d\n", cbReqCount, cbHeaderLength, uPadding, cbIVlen);
2028             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"MIC:%lx, %lx\n", *pdwMIC_L, *pdwMIC_R);
2029 /*
2030             for (ii = 0; ii < 8; ii++) {
2031                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"%02x ", *(((unsigned char *)(pdwMIC_L) + ii)));
2032             }
2033             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"\n");
2034 */
2035 
2036         }
2037 
2038 
2039         if ((pDevice->byLocalID <= REV_ID_VT3253_A1)){
2040             if (bNeedEncrypt) {
2041                 s_vSWencryption(pDevice, pTransmitKey, (pbyBuffer + uLength - cb802_1_H_len),
2042                                 (unsigned short)(cbFrameBodySize + cbMIClen));
2043                 cbReqCount += cbICVlen;
2044             }
2045         }
2046 
2047 
2048         ptdCurr = (PSTxDesc)pHeadTD;
2049 
2050         ptdCurr->pTDInfo->dwReqCount = cbReqCount - uPadding;
2051         ptdCurr->pTDInfo->dwHeaderLength = cbHeaderLength;
2052         ptdCurr->pTDInfo->skb_dma = ptdCurr->pTDInfo->buf_dma;
2053         ptdCurr->buff_addr = cpu_to_le32(ptdCurr->pTDInfo->skb_dma);
2054   	    //Set TSR1 & ReqCount in TxDescHead
2055         ptdCurr->m_td1TD1.byTCR |= (TCR_STP | TCR_EDP | EDMSDU);
2056         ptdCurr->m_td1TD1.wReqCount = cpu_to_le16((unsigned short)(cbReqCount));
2057 
2058         pDevice->iTDUsed[uDMAIdx]++;
2059 
2060 
2061 //   DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" ptdCurr->m_dwReserved0[%d] ptdCurr->m_dwReserved1[%d].\n", ptdCurr->pTDInfo->dwReqCount, ptdCurr->pTDInfo->dwHeaderLength);
2062 //   DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" cbHeaderLength[%d]\n", cbHeaderLength);
2063 
2064     }
2065     *puMACfragNum = uMACfragNum;
2066     //DBG_PRTGRP03(("s_cbFillTxBufHead END\n"));
2067     return cbHeaderLength;
2068 }
2069 
2070 
2071 void
vGenerateFIFOHeader(PSDevice pDevice,unsigned char byPktType,unsigned char * pbyTxBufferAddr,bool bNeedEncrypt,unsigned int cbPayloadSize,unsigned int uDMAIdx,PSTxDesc pHeadTD,PSEthernetHeader psEthHeader,unsigned char * pPacket,PSKeyItem pTransmitKey,unsigned int uNodeIndex,unsigned int * puMACfragNum,unsigned int * pcbHeaderSize)2072 vGenerateFIFOHeader(PSDevice pDevice, unsigned char byPktType, unsigned char *pbyTxBufferAddr,
2073 	bool bNeedEncrypt, unsigned int cbPayloadSize, unsigned int uDMAIdx,
2074 	PSTxDesc pHeadTD, PSEthernetHeader psEthHeader, unsigned char *pPacket,
2075 	PSKeyItem pTransmitKey, unsigned int uNodeIndex, unsigned int *puMACfragNum,
2076 	unsigned int *pcbHeaderSize)
2077 {
2078     unsigned int wTxBufSize;       // FFinfo size
2079     bool bNeedACK;
2080     bool bIsAdhoc;
2081     unsigned short cbMacHdLen;
2082     PSTxBufHead     pTxBufHead = (PSTxBufHead) pbyTxBufferAddr;
2083 
2084     wTxBufSize = sizeof(STxBufHead);
2085 
2086     memset(pTxBufHead, 0, wTxBufSize);
2087     //Set FIFOCTL_NEEDACK
2088 
2089     if ((pDevice->eOPMode == OP_MODE_ADHOC) ||
2090         (pDevice->eOPMode == OP_MODE_AP)) {
2091         if (is_multicast_ether_addr(&(psEthHeader->abyDstAddr[0]))) {
2092             bNeedACK = false;
2093             pTxBufHead->wFIFOCtl = pTxBufHead->wFIFOCtl & (~FIFOCTL_NEEDACK);
2094         }
2095         else {
2096             bNeedACK = true;
2097             pTxBufHead->wFIFOCtl |= FIFOCTL_NEEDACK;
2098         }
2099         bIsAdhoc = true;
2100     }
2101     else {
2102         // MSDUs in Infra mode always need ACK
2103         bNeedACK = true;
2104         pTxBufHead->wFIFOCtl |= FIFOCTL_NEEDACK;
2105         bIsAdhoc = false;
2106     }
2107 
2108 
2109     pTxBufHead->wFIFOCtl |= FIFOCTL_TMOEN;
2110     pTxBufHead->wTimeStamp = cpu_to_le16(DEFAULT_MSDU_LIFETIME_RES_64us);
2111 
2112     //Set FIFOCTL_LHEAD
2113     if (pDevice->bLongHeader)
2114         pTxBufHead->wFIFOCtl |= FIFOCTL_LHEAD;
2115 
2116     //Set FIFOCTL_GENINT
2117 
2118     pTxBufHead->wFIFOCtl |= FIFOCTL_GENINT;
2119 
2120 
2121     //Set FIFOCTL_ISDMA0
2122     if (TYPE_TXDMA0 == uDMAIdx) {
2123         pTxBufHead->wFIFOCtl |= FIFOCTL_ISDMA0;
2124     }
2125 
2126     //Set FRAGCTL_MACHDCNT
2127     if (pDevice->bLongHeader) {
2128         cbMacHdLen = WLAN_HDR_ADDR3_LEN + 6;
2129     } else {
2130         cbMacHdLen = WLAN_HDR_ADDR3_LEN;
2131     }
2132     pTxBufHead->wFragCtl |= cpu_to_le16((unsigned short)(cbMacHdLen << 10));
2133 
2134     //Set packet type
2135     if (byPktType == PK_TYPE_11A) {//0000 0000 0000 0000
2136         ;
2137     }
2138     else if (byPktType == PK_TYPE_11B) {//0000 0001 0000 0000
2139         pTxBufHead->wFIFOCtl |= FIFOCTL_11B;
2140     }
2141     else if (byPktType == PK_TYPE_11GB) {//0000 0010 0000 0000
2142         pTxBufHead->wFIFOCtl |= FIFOCTL_11GB;
2143     }
2144     else if (byPktType == PK_TYPE_11GA) {//0000 0011 0000 0000
2145         pTxBufHead->wFIFOCtl |= FIFOCTL_11GA;
2146     }
2147     //Set FIFOCTL_GrpAckPolicy
2148     if (pDevice->bGrpAckPolicy == true) {//0000 0100 0000 0000
2149         pTxBufHead->wFIFOCtl |=	FIFOCTL_GRPACK;
2150     }
2151 
2152     //Set Auto Fallback Ctl
2153     if (pDevice->wCurrentRate >= RATE_18M) {
2154         if (pDevice->byAutoFBCtrl == AUTO_FB_0) {
2155             pTxBufHead->wFIFOCtl |= FIFOCTL_AUTO_FB_0;
2156         } else if (pDevice->byAutoFBCtrl == AUTO_FB_1) {
2157             pTxBufHead->wFIFOCtl |= FIFOCTL_AUTO_FB_1;
2158         }
2159     }
2160 
2161     //Set FRAGCTL_WEPTYP
2162     pDevice->bAES = false;
2163 
2164     //Set FRAGCTL_WEPTYP
2165     if (pDevice->byLocalID > REV_ID_VT3253_A1) {
2166         if ((bNeedEncrypt) && (pTransmitKey != NULL))  { //WEP enabled
2167             if (pTransmitKey->byCipherSuite == KEY_CTL_TKIP) {
2168                 pTxBufHead->wFragCtl |= FRAGCTL_TKIP;
2169             }
2170             else if (pTransmitKey->byCipherSuite == KEY_CTL_WEP) { //WEP40 or WEP104
2171                 if (pTransmitKey->uKeyLength != WLAN_WEP232_KEYLEN)
2172                     pTxBufHead->wFragCtl |= FRAGCTL_LEGACY;
2173             }
2174             else if (pTransmitKey->byCipherSuite == KEY_CTL_CCMP) { //CCMP
2175                 pTxBufHead->wFragCtl |= FRAGCTL_AES;
2176             }
2177         }
2178     }
2179 
2180 #ifdef	PLICE_DEBUG
2181 	//printk("Func:vGenerateFIFOHeader:TxDataRate is %d,TxPower is %d\n",pDevice->wCurrentRate,pDevice->byCurPwr);
2182 
2183 	//if (pDevice->wCurrentRate <= 3)
2184 	//{
2185 	//	RFbRawSetPower(pDevice,36,pDevice->wCurrentRate);
2186 	//}
2187 	//else
2188 
2189 	RFbSetPower(pDevice, pDevice->wCurrentRate, pDevice->byCurrentCh);
2190 #endif
2191 		//if (pDevice->wCurrentRate == 3)
2192 		//pDevice->byCurPwr = 46;
2193 		pTxBufHead->byTxPower = pDevice->byCurPwr;
2194 
2195 
2196 
2197 
2198 /*
2199     if(pDevice->bEnableHostWEP)
2200         pTxBufHead->wFragCtl &=  ~(FRAGCTL_TKIP | FRAGCTL_LEGACY |FRAGCTL_AES);
2201 */
2202     *pcbHeaderSize = s_cbFillTxBufHead(pDevice, byPktType, pbyTxBufferAddr, cbPayloadSize,
2203                                    uDMAIdx, pHeadTD, psEthHeader, pPacket, bNeedEncrypt,
2204                                    pTransmitKey, uNodeIndex, puMACfragNum);
2205 
2206     return;
2207 }
2208 
2209 
2210 
2211 
2212 /*+
2213  *
2214  * Description:
2215  *      Translate 802.3 to 802.11 header
2216  *
2217  * Parameters:
2218  *  In:
2219  *      pDevice         - Pointer to adapter
2220  *      dwTxBufferAddr  - Transmit Buffer
2221  *      pPacket         - Packet from upper layer
2222  *      cbPacketSize    - Transmit Data Length
2223  *  Out:
2224  *      pcbHeadSize         - Header size of MAC&Baseband control and 802.11 Header
2225  *      pcbAppendPayload    - size of append payload for 802.1H translation
2226  *
2227  * Return Value: none
2228  *
2229 -*/
2230 
2231 void
vGenerateMACHeader(PSDevice pDevice,unsigned char * pbyBufferAddr,unsigned short wDuration,PSEthernetHeader psEthHeader,bool bNeedEncrypt,unsigned short wFragType,unsigned int uDMAIdx,unsigned int uFragIdx)2232 vGenerateMACHeader (
2233     PSDevice         pDevice,
2234     unsigned char *pbyBufferAddr,
2235     unsigned short wDuration,
2236     PSEthernetHeader psEthHeader,
2237     bool bNeedEncrypt,
2238     unsigned short wFragType,
2239     unsigned int uDMAIdx,
2240     unsigned int uFragIdx
2241     )
2242 {
2243     PS802_11Header  pMACHeader = (PS802_11Header)pbyBufferAddr;
2244 
2245     memset(pMACHeader, 0, (sizeof(S802_11Header)));  //- sizeof(pMACHeader->dwIV)));
2246 
2247     if (uDMAIdx == TYPE_ATIMDMA) {
2248     	pMACHeader->wFrameCtl = TYPE_802_11_ATIM;
2249     } else {
2250         pMACHeader->wFrameCtl = TYPE_802_11_DATA;
2251     }
2252 
2253     if (pDevice->eOPMode == OP_MODE_AP) {
2254         memcpy(&(pMACHeader->abyAddr1[0]), &(psEthHeader->abyDstAddr[0]), ETH_ALEN);
2255         memcpy(&(pMACHeader->abyAddr2[0]), &(pDevice->abyBSSID[0]), ETH_ALEN);
2256         memcpy(&(pMACHeader->abyAddr3[0]), &(psEthHeader->abySrcAddr[0]), ETH_ALEN);
2257         pMACHeader->wFrameCtl |= FC_FROMDS;
2258     }
2259     else {
2260         if (pDevice->eOPMode == OP_MODE_ADHOC) {
2261             memcpy(&(pMACHeader->abyAddr1[0]), &(psEthHeader->abyDstAddr[0]), ETH_ALEN);
2262             memcpy(&(pMACHeader->abyAddr2[0]), &(psEthHeader->abySrcAddr[0]), ETH_ALEN);
2263             memcpy(&(pMACHeader->abyAddr3[0]), &(pDevice->abyBSSID[0]), ETH_ALEN);
2264         }
2265         else {
2266             memcpy(&(pMACHeader->abyAddr3[0]), &(psEthHeader->abyDstAddr[0]), ETH_ALEN);
2267             memcpy(&(pMACHeader->abyAddr2[0]), &(psEthHeader->abySrcAddr[0]), ETH_ALEN);
2268             memcpy(&(pMACHeader->abyAddr1[0]), &(pDevice->abyBSSID[0]), ETH_ALEN);
2269             pMACHeader->wFrameCtl |= FC_TODS;
2270         }
2271     }
2272 
2273     if (bNeedEncrypt)
2274         pMACHeader->wFrameCtl |= cpu_to_le16((unsigned short)WLAN_SET_FC_ISWEP(1));
2275 
2276     pMACHeader->wDurationID = cpu_to_le16(wDuration);
2277 
2278     if (pDevice->bLongHeader) {
2279         PWLAN_80211HDR_A4 pMACA4Header  = (PWLAN_80211HDR_A4) pbyBufferAddr;
2280         pMACHeader->wFrameCtl |= (FC_TODS | FC_FROMDS);
2281         memcpy(pMACA4Header->abyAddr4, pDevice->abyBSSID, WLAN_ADDR_LEN);
2282     }
2283     pMACHeader->wSeqCtl = cpu_to_le16(pDevice->wSeqCounter << 4);
2284 
2285     //Set FragNumber in Sequence Control
2286     pMACHeader->wSeqCtl |= cpu_to_le16((unsigned short)uFragIdx);
2287 
2288     if ((wFragType == FRAGCTL_ENDFRAG) || (wFragType == FRAGCTL_NONFRAG)) {
2289         pDevice->wSeqCounter++;
2290         if (pDevice->wSeqCounter > 0x0fff)
2291             pDevice->wSeqCounter = 0;
2292     }
2293 
2294     if ((wFragType == FRAGCTL_STAFRAG) || (wFragType == FRAGCTL_MIDFRAG)) { //StartFrag or MidFrag
2295         pMACHeader->wFrameCtl |= FC_MOREFRAG;
2296     }
2297 }
2298 
2299 
2300 
2301 
2302 
2303 
csMgmt_xmit(PSDevice pDevice,PSTxMgmtPacket pPacket)2304 CMD_STATUS csMgmt_xmit(PSDevice pDevice, PSTxMgmtPacket pPacket) {
2305 
2306     PSTxDesc        pFrstTD;
2307     unsigned char byPktType;
2308     unsigned char *pbyTxBufferAddr;
2309     void *          pvRTS;
2310     PSCTS           pCTS;
2311     void *          pvTxDataHd;
2312     unsigned int uDuration;
2313     unsigned int cbReqCount;
2314     PS802_11Header  pMACHeader;
2315     unsigned int cbHeaderSize;
2316     unsigned int cbFrameBodySize;
2317     bool bNeedACK;
2318     bool bIsPSPOLL = false;
2319     PSTxBufHead     pTxBufHead;
2320     unsigned int cbFrameSize;
2321     unsigned int cbIVlen = 0;
2322     unsigned int cbICVlen = 0;
2323     unsigned int cbMIClen = 0;
2324     unsigned int cbFCSlen = 4;
2325     unsigned int uPadding = 0;
2326     unsigned short wTxBufSize;
2327     unsigned int cbMacHdLen;
2328     SEthernetHeader sEthHeader;
2329     void *          pvRrvTime;
2330     void *          pMICHDR;
2331     PSMgmtObject    pMgmt = pDevice->pMgmt;
2332     unsigned short wCurrentRate = RATE_1M;
2333 
2334 
2335     if (AVAIL_TD(pDevice, TYPE_TXDMA0) <= 0) {
2336         return CMD_STATUS_RESOURCES;
2337     }
2338 
2339     pFrstTD = pDevice->apCurrTD[TYPE_TXDMA0];
2340     pbyTxBufferAddr = (unsigned char *)pFrstTD->pTDInfo->buf;
2341     cbFrameBodySize = pPacket->cbPayloadLen;
2342     pTxBufHead = (PSTxBufHead) pbyTxBufferAddr;
2343     wTxBufSize = sizeof(STxBufHead);
2344     memset(pTxBufHead, 0, wTxBufSize);
2345 
2346     if (pDevice->eCurrentPHYType == PHY_TYPE_11A) {
2347         wCurrentRate = RATE_6M;
2348         byPktType = PK_TYPE_11A;
2349     } else {
2350         wCurrentRate = RATE_1M;
2351         byPktType = PK_TYPE_11B;
2352     }
2353 
2354     // SetPower will cause error power TX state for OFDM Date packet in TX buffer.
2355     // 2004.11.11 Kyle -- Using OFDM power to tx MngPkt will decrease the connection capability.
2356     //                    And cmd timer will wait data pkt TX finish before scanning so it's OK
2357     //                    to set power here.
2358     if (pDevice->pMgmt->eScanState != WMAC_NO_SCANNING) {
2359 
2360 		RFbSetPower(pDevice, wCurrentRate, pDevice->byCurrentCh);
2361     } else {
2362         RFbSetPower(pDevice, wCurrentRate, pMgmt->uCurrChannel);
2363     }
2364     pTxBufHead->byTxPower = pDevice->byCurPwr;
2365     //+++++++++++++++++++++ Patch VT3253 A1 performance +++++++++++++++++++++++++++
2366     if (pDevice->byFOETuning) {
2367         if ((pPacket->p80211Header->sA3.wFrameCtl & TYPE_DATE_NULL) == TYPE_DATE_NULL) {
2368             wCurrentRate = RATE_24M;
2369             byPktType = PK_TYPE_11GA;
2370         }
2371     }
2372 
2373     //Set packet type
2374     if (byPktType == PK_TYPE_11A) {//0000 0000 0000 0000
2375         pTxBufHead->wFIFOCtl = 0;
2376     }
2377     else if (byPktType == PK_TYPE_11B) {//0000 0001 0000 0000
2378         pTxBufHead->wFIFOCtl |= FIFOCTL_11B;
2379     }
2380     else if (byPktType == PK_TYPE_11GB) {//0000 0010 0000 0000
2381         pTxBufHead->wFIFOCtl |= FIFOCTL_11GB;
2382     }
2383     else if (byPktType == PK_TYPE_11GA) {//0000 0011 0000 0000
2384         pTxBufHead->wFIFOCtl |= FIFOCTL_11GA;
2385     }
2386 
2387     pTxBufHead->wFIFOCtl |= FIFOCTL_TMOEN;
2388     pTxBufHead->wTimeStamp = cpu_to_le16(DEFAULT_MGN_LIFETIME_RES_64us);
2389 
2390 
2391     if (is_multicast_ether_addr(&(pPacket->p80211Header->sA3.abyAddr1[0])))
2392         bNeedACK = false;
2393     else {
2394         bNeedACK = true;
2395         pTxBufHead->wFIFOCtl |= FIFOCTL_NEEDACK;
2396     };
2397 
2398     if ((pMgmt->eCurrMode == WMAC_MODE_ESS_AP) ||
2399         (pMgmt->eCurrMode == WMAC_MODE_IBSS_STA) ) {
2400 
2401         pTxBufHead->wFIFOCtl |= FIFOCTL_LRETRY;
2402         //Set Preamble type always long
2403         //pDevice->byPreambleType = PREAMBLE_LONG;
2404         // probe-response don't retry
2405         //if ((pPacket->p80211Header->sA4.wFrameCtl & TYPE_SUBTYPE_MASK) == TYPE_MGMT_PROBE_RSP) {
2406         //     bNeedACK = false;
2407         //     pTxBufHead->wFIFOCtl  &= (~FIFOCTL_NEEDACK);
2408         //}
2409     }
2410 
2411     pTxBufHead->wFIFOCtl |= (FIFOCTL_GENINT | FIFOCTL_ISDMA0);
2412 
2413     if ((pPacket->p80211Header->sA4.wFrameCtl & TYPE_SUBTYPE_MASK) == TYPE_CTL_PSPOLL) {
2414         bIsPSPOLL = true;
2415         cbMacHdLen = WLAN_HDR_ADDR2_LEN;
2416     } else {
2417         cbMacHdLen = WLAN_HDR_ADDR3_LEN;
2418     }
2419 
2420     //Set FRAGCTL_MACHDCNT
2421     pTxBufHead->wFragCtl |= cpu_to_le16((unsigned short)(cbMacHdLen << 10));
2422 
2423     // Notes:
2424     // Although spec says MMPDU can be fragmented; In most case,
2425     // no one will send a MMPDU under fragmentation. With RTS may occur.
2426     pDevice->bAES = false;  //Set FRAGCTL_WEPTYP
2427 
2428     if (WLAN_GET_FC_ISWEP(pPacket->p80211Header->sA4.wFrameCtl) != 0) {
2429         if (pDevice->eEncryptionStatus == Ndis802_11Encryption1Enabled) {
2430             cbIVlen = 4;
2431             cbICVlen = 4;
2432     	    pTxBufHead->wFragCtl |= FRAGCTL_LEGACY;
2433         }
2434         else if (pDevice->eEncryptionStatus == Ndis802_11Encryption2Enabled) {
2435             cbIVlen = 8;//IV+ExtIV
2436             cbMIClen = 8;
2437             cbICVlen = 4;
2438     	    pTxBufHead->wFragCtl |= FRAGCTL_TKIP;
2439     	    //We need to get seed here for filling TxKey entry.
2440             //TKIPvMixKey(pTransmitKey->abyKey, pDevice->abyCurrentNetAddr,
2441             //            pTransmitKey->wTSC15_0, pTransmitKey->dwTSC47_16, pDevice->abyPRNG);
2442         }
2443         else if (pDevice->eEncryptionStatus == Ndis802_11Encryption3Enabled) {
2444             cbIVlen = 8;//RSN Header
2445             cbICVlen = 8;//MIC
2446             pTxBufHead->wFragCtl |= FRAGCTL_AES;
2447             pDevice->bAES = true;
2448         }
2449         //MAC Header should be padding 0 to DW alignment.
2450         uPadding = 4 - (cbMacHdLen%4);
2451         uPadding %= 4;
2452     }
2453 
2454     cbFrameSize = cbMacHdLen + cbFrameBodySize + cbIVlen + cbMIClen + cbICVlen + cbFCSlen;
2455 
2456     //Set FIFOCTL_GrpAckPolicy
2457     if (pDevice->bGrpAckPolicy == true) {//0000 0100 0000 0000
2458         pTxBufHead->wFIFOCtl |=	FIFOCTL_GRPACK;
2459     }
2460     //the rest of pTxBufHead->wFragCtl:FragTyp will be set later in s_vFillFragParameter()
2461 
2462     //Set RrvTime/RTS/CTS Buffer
2463     if (byPktType == PK_TYPE_11GB || byPktType == PK_TYPE_11GA) {//802.11g packet
2464 
2465         pvRrvTime = (PSRrvTime_gCTS) (pbyTxBufferAddr + wTxBufSize);
2466         pMICHDR = NULL;
2467         pvRTS = NULL;
2468         pCTS = (PSCTS) (pbyTxBufferAddr + wTxBufSize + sizeof(SRrvTime_gCTS));
2469         pvTxDataHd = (PSTxDataHead_g) (pbyTxBufferAddr + wTxBufSize + sizeof(SRrvTime_gCTS) + sizeof(SCTS));
2470         cbHeaderSize = wTxBufSize + sizeof(SRrvTime_gCTS) + sizeof(SCTS) + sizeof(STxDataHead_g);
2471     }
2472     else { // 802.11a/b packet
2473         pvRrvTime = (PSRrvTime_ab) (pbyTxBufferAddr + wTxBufSize);
2474         pMICHDR = NULL;
2475         pvRTS = NULL;
2476         pCTS = NULL;
2477         pvTxDataHd = (PSTxDataHead_ab) (pbyTxBufferAddr + wTxBufSize + sizeof(SRrvTime_ab));
2478         cbHeaderSize = wTxBufSize + sizeof(SRrvTime_ab) + sizeof(STxDataHead_ab);
2479     }
2480 
2481     memset((void *)(pbyTxBufferAddr + wTxBufSize), 0, (cbHeaderSize - wTxBufSize));
2482 
2483     memcpy(&(sEthHeader.abyDstAddr[0]), &(pPacket->p80211Header->sA3.abyAddr1[0]), ETH_ALEN);
2484     memcpy(&(sEthHeader.abySrcAddr[0]), &(pPacket->p80211Header->sA3.abyAddr2[0]), ETH_ALEN);
2485     //=========================
2486     //    No Fragmentation
2487     //=========================
2488     pTxBufHead->wFragCtl |= (unsigned short)FRAGCTL_NONFRAG;
2489 
2490 
2491     //Fill FIFO,RrvTime,RTS,and CTS
2492     s_vGenerateTxParameter(pDevice, byPktType, pbyTxBufferAddr, pvRrvTime, pvRTS, pCTS,
2493                            cbFrameSize, bNeedACK, TYPE_TXDMA0, &sEthHeader, wCurrentRate);
2494 
2495     //Fill DataHead
2496     uDuration = s_uFillDataHead(pDevice, byPktType, pvTxDataHd, cbFrameSize, TYPE_TXDMA0, bNeedACK,
2497                                 0, 0, 1, AUTO_FB_NONE, wCurrentRate);
2498 
2499     pMACHeader = (PS802_11Header) (pbyTxBufferAddr + cbHeaderSize);
2500 
2501     cbReqCount = cbHeaderSize + cbMacHdLen + uPadding + cbIVlen + cbFrameBodySize;
2502 
2503     if (WLAN_GET_FC_ISWEP(pPacket->p80211Header->sA4.wFrameCtl) != 0) {
2504         unsigned char *pbyIVHead;
2505         unsigned char *pbyPayloadHead;
2506         unsigned char *pbyBSSID;
2507         PSKeyItem       pTransmitKey = NULL;
2508 
2509         pbyIVHead = (unsigned char *)(pbyTxBufferAddr + cbHeaderSize + cbMacHdLen + uPadding);
2510         pbyPayloadHead = (unsigned char *)(pbyTxBufferAddr + cbHeaderSize + cbMacHdLen + uPadding + cbIVlen);
2511 
2512         //Fill TXKEY
2513         //Kyle: Need fix: TKIP and AES did't encryt Mnt Packet.
2514         //s_vFillTxKey(pDevice, (unsigned char *)pTxBufHead->adwTxKey, NULL);
2515 
2516         //Fill IV(ExtIV,RSNHDR)
2517         //s_vFillPrePayload(pDevice, pbyIVHead, NULL);
2518         //---------------------------
2519         // S/W or H/W Encryption
2520         //---------------------------
2521         //Fill MICHDR
2522         //if (pDevice->bAES) {
2523         //    s_vFillMICHDR(pDevice, (unsigned char *)pMICHDR, (unsigned char *)pMACHeader, (unsigned short)cbFrameBodySize);
2524         //}
2525         do {
2526             if ((pDevice->eOPMode == OP_MODE_INFRASTRUCTURE) &&
2527                 (pDevice->bLinkPass == true)) {
2528                 pbyBSSID = pDevice->abyBSSID;
2529                 // get pairwise key
2530                 if (KeybGetTransmitKey(&(pDevice->sKey), pbyBSSID, PAIRWISE_KEY, &pTransmitKey) == false) {
2531                     // get group key
2532                     if(KeybGetTransmitKey(&(pDevice->sKey), pbyBSSID, GROUP_KEY, &pTransmitKey) == true) {
2533                         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Get GTK.\n");
2534                         break;
2535                     }
2536                 } else {
2537                     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Get PTK.\n");
2538                     break;
2539                 }
2540             }
2541             // get group key
2542             pbyBSSID = pDevice->abyBroadcastAddr;
2543             if(KeybGetTransmitKey(&(pDevice->sKey), pbyBSSID, GROUP_KEY, &pTransmitKey) == false) {
2544                 pTransmitKey = NULL;
2545                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"KEY is NULL. OP Mode[%d]\n", pDevice->eOPMode);
2546             } else {
2547                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Get GTK.\n");
2548             }
2549         } while(false);
2550         //Fill TXKEY
2551         s_vFillTxKey(pDevice, (unsigned char *)(pTxBufHead->adwTxKey), pbyIVHead, pTransmitKey,
2552                      (unsigned char *)pMACHeader, (unsigned short)cbFrameBodySize, NULL);
2553 
2554         memcpy(pMACHeader, pPacket->p80211Header, cbMacHdLen);
2555         memcpy(pbyPayloadHead, ((unsigned char *)(pPacket->p80211Header) + cbMacHdLen),
2556                  cbFrameBodySize);
2557     }
2558     else {
2559         // Copy the Packet into a tx Buffer
2560         memcpy(pMACHeader, pPacket->p80211Header, pPacket->cbMPDULen);
2561     }
2562 
2563     pMACHeader->wSeqCtl = cpu_to_le16(pDevice->wSeqCounter << 4);
2564     pDevice->wSeqCounter++ ;
2565     if (pDevice->wSeqCounter > 0x0fff)
2566         pDevice->wSeqCounter = 0;
2567 
2568     if (bIsPSPOLL) {
2569         // The MAC will automatically replace the Duration-field of MAC header by Duration-field
2570         // of  FIFO control header.
2571         // This will cause AID-field of PS-POLL packet be incorrect (Because PS-POLL's AID field is
2572         // in the same place of other packet's Duration-field).
2573         // And it will cause Cisco-AP to issue Disassociation-packet
2574         if (byPktType == PK_TYPE_11GB || byPktType == PK_TYPE_11GA) {
2575             ((PSTxDataHead_g)pvTxDataHd)->wDuration_a = cpu_to_le16(pPacket->p80211Header->sA2.wDurationID);
2576             ((PSTxDataHead_g)pvTxDataHd)->wDuration_b = cpu_to_le16(pPacket->p80211Header->sA2.wDurationID);
2577         } else {
2578             ((PSTxDataHead_ab)pvTxDataHd)->wDuration = cpu_to_le16(pPacket->p80211Header->sA2.wDurationID);
2579         }
2580     }
2581 
2582 
2583     // first TD is the only TD
2584     //Set TSR1 & ReqCount in TxDescHead
2585     pFrstTD->m_td1TD1.byTCR = (TCR_STP | TCR_EDP | EDMSDU);
2586     pFrstTD->pTDInfo->skb_dma = pFrstTD->pTDInfo->buf_dma;
2587     pFrstTD->m_td1TD1.wReqCount = cpu_to_le16((unsigned short)(cbReqCount));
2588     pFrstTD->buff_addr = cpu_to_le32(pFrstTD->pTDInfo->skb_dma);
2589     pFrstTD->pTDInfo->byFlags = 0;
2590 
2591     if (MACbIsRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_PS)) {
2592         // Disable PS
2593         MACbPSWakeup(pDevice->PortOffset);
2594     }
2595     pDevice->bPWBitOn = false;
2596 
2597     wmb();
2598     pFrstTD->m_td0TD0.f1Owner = OWNED_BY_NIC;
2599     wmb();
2600 
2601     pDevice->iTDUsed[TYPE_TXDMA0]++;
2602 
2603     if (AVAIL_TD(pDevice, TYPE_TXDMA0) <= 1) {
2604         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " available td0 <= 1\n");
2605     }
2606 
2607     pDevice->apCurrTD[TYPE_TXDMA0] = pFrstTD->next;
2608 #ifdef	PLICE_DEBUG
2609 		//printk("SCAN:CurrentRate is  %d,TxPower is %d\n",wCurrentRate,pTxBufHead->byTxPower);
2610 #endif
2611 
2612 #ifdef TxInSleep
2613   pDevice->nTxDataTimeCout=0; //2008-8-21 chester <add> for send null packet
2614   #endif
2615 
2616     // Poll Transmit the adapter
2617     MACvTransmit0(pDevice->PortOffset);
2618 
2619     return CMD_STATUS_PENDING;
2620 
2621 }
2622 
2623 
csBeacon_xmit(PSDevice pDevice,PSTxMgmtPacket pPacket)2624 CMD_STATUS csBeacon_xmit(PSDevice pDevice, PSTxMgmtPacket pPacket) {
2625 
2626     unsigned char byPktType;
2627     unsigned char *pbyBuffer = (unsigned char *)pDevice->tx_beacon_bufs;
2628     unsigned int cbFrameSize = pPacket->cbMPDULen + WLAN_FCS_LEN;
2629     unsigned int cbHeaderSize = 0;
2630     unsigned short wTxBufSize = sizeof(STxShortBufHead);
2631     PSTxShortBufHead pTxBufHead = (PSTxShortBufHead) pbyBuffer;
2632     PSTxDataHead_ab  pTxDataHead = (PSTxDataHead_ab) (pbyBuffer + wTxBufSize);
2633     PS802_11Header   pMACHeader;
2634     unsigned short wCurrentRate;
2635     unsigned short wLen = 0x0000;
2636 
2637 
2638     memset(pTxBufHead, 0, wTxBufSize);
2639 
2640     if (pDevice->eCurrentPHYType == PHY_TYPE_11A) {
2641         wCurrentRate = RATE_6M;
2642         byPktType = PK_TYPE_11A;
2643     } else {
2644         wCurrentRate = RATE_2M;
2645         byPktType = PK_TYPE_11B;
2646     }
2647 
2648     //Set Preamble type always long
2649     pDevice->byPreambleType = PREAMBLE_LONG;
2650 
2651     //Set FIFOCTL_GENINT
2652 
2653     pTxBufHead->wFIFOCtl |= FIFOCTL_GENINT;
2654 
2655 
2656     //Set packet type & Get Duration
2657     if (byPktType == PK_TYPE_11A) {//0000 0000 0000 0000
2658         pTxDataHead->wDuration = cpu_to_le16((unsigned short)s_uGetDataDuration(pDevice, DATADUR_A, cbFrameSize, byPktType,
2659                                                           wCurrentRate, false, 0, 0, 1, AUTO_FB_NONE));
2660     }
2661     else if (byPktType == PK_TYPE_11B) {//0000 0001 0000 0000
2662         pTxBufHead->wFIFOCtl |= FIFOCTL_11B;
2663         pTxDataHead->wDuration = cpu_to_le16((unsigned short)s_uGetDataDuration(pDevice, DATADUR_B, cbFrameSize, byPktType,
2664                                                           wCurrentRate, false, 0, 0, 1, AUTO_FB_NONE));
2665     }
2666 
2667     BBvCaculateParameter(pDevice, cbFrameSize, wCurrentRate, byPktType,
2668         (unsigned short *)&(wLen), (unsigned char *)&(pTxDataHead->byServiceField), (unsigned char *)&(pTxDataHead->bySignalField)
2669     );
2670     pTxDataHead->wTransmitLength = cpu_to_le16(wLen);
2671     //Get TimeStampOff
2672     pTxDataHead->wTimeStampOff = cpu_to_le16(wTimeStampOff[pDevice->byPreambleType%2][wCurrentRate%MAX_RATE]);
2673     cbHeaderSize = wTxBufSize + sizeof(STxDataHead_ab);
2674 
2675    //Generate Beacon Header
2676     pMACHeader = (PS802_11Header)(pbyBuffer + cbHeaderSize);
2677     memcpy(pMACHeader, pPacket->p80211Header, pPacket->cbMPDULen);
2678 
2679     pMACHeader->wDurationID = 0;
2680     pMACHeader->wSeqCtl = cpu_to_le16(pDevice->wSeqCounter << 4);
2681     pDevice->wSeqCounter++ ;
2682     if (pDevice->wSeqCounter > 0x0fff)
2683         pDevice->wSeqCounter = 0;
2684 
2685     // Set Beacon buffer length
2686     pDevice->wBCNBufLen = pPacket->cbMPDULen + cbHeaderSize;
2687 
2688     MACvSetCurrBCNTxDescAddr(pDevice->PortOffset, (pDevice->tx_beacon_dma));
2689 
2690     MACvSetCurrBCNLength(pDevice->PortOffset, pDevice->wBCNBufLen);
2691     // Set auto Transmit on
2692     MACvRegBitsOn(pDevice->PortOffset, MAC_REG_TCR, TCR_AUTOBCNTX);
2693     // Poll Transmit the adapter
2694     MACvTransmitBCN(pDevice->PortOffset);
2695 
2696     return CMD_STATUS_PENDING;
2697 }
2698 
2699 
2700 
2701 unsigned int
cbGetFragCount(PSDevice pDevice,PSKeyItem pTransmitKey,unsigned int cbFrameBodySize,PSEthernetHeader psEthHeader)2702 cbGetFragCount (
2703     PSDevice         pDevice,
2704     PSKeyItem        pTransmitKey,
2705     unsigned int cbFrameBodySize,
2706     PSEthernetHeader psEthHeader
2707     )
2708 {
2709     unsigned int cbMACHdLen;
2710     unsigned int cbFrameSize;
2711     unsigned int cbFragmentSize; //Hdr+(IV)+payoad+(MIC)+(ICV)+FCS
2712     unsigned int cbFragPayloadSize;
2713     unsigned int cbLastFragPayloadSize;
2714     unsigned int cbIVlen = 0;
2715     unsigned int cbICVlen = 0;
2716     unsigned int cbMIClen = 0;
2717     unsigned int cbFCSlen = 4;
2718     unsigned int uMACfragNum = 1;
2719     bool bNeedACK;
2720 
2721 
2722 
2723     if ((pDevice->eOPMode == OP_MODE_ADHOC) ||
2724         (pDevice->eOPMode == OP_MODE_AP)) {
2725         if (is_multicast_ether_addr(&(psEthHeader->abyDstAddr[0])))
2726             bNeedACK = false;
2727         else
2728             bNeedACK = true;
2729     }
2730     else {
2731         // MSDUs in Infra mode always need ACK
2732         bNeedACK = true;
2733     }
2734 
2735     if (pDevice->bLongHeader)
2736         cbMACHdLen = WLAN_HDR_ADDR3_LEN + 6;
2737     else
2738         cbMACHdLen = WLAN_HDR_ADDR3_LEN;
2739 
2740 
2741     if (pDevice->bEncryptionEnable == true) {
2742 
2743         if (pTransmitKey == NULL) {
2744             if ((pDevice->eEncryptionStatus == Ndis802_11Encryption1Enabled) ||
2745                 (pDevice->pMgmt->eAuthenMode < WMAC_AUTH_WPA)) {
2746                 cbIVlen = 4;
2747                 cbICVlen = 4;
2748             } else if (pDevice->eEncryptionStatus == Ndis802_11Encryption2Enabled) {
2749                 cbIVlen = 8;//IV+ExtIV
2750                 cbMIClen = 8;
2751                 cbICVlen = 4;
2752             } else if (pDevice->eEncryptionStatus == Ndis802_11Encryption3Enabled) {
2753                 cbIVlen = 8;//RSN Header
2754                 cbICVlen = 8;//MIC
2755             }
2756         } else if (pTransmitKey->byCipherSuite == KEY_CTL_WEP) {
2757             cbIVlen = 4;
2758             cbICVlen = 4;
2759         } else if (pTransmitKey->byCipherSuite == KEY_CTL_TKIP) {
2760             cbIVlen = 8;//IV+ExtIV
2761             cbMIClen = 8;
2762             cbICVlen = 4;
2763         } else if (pTransmitKey->byCipherSuite == KEY_CTL_CCMP) {
2764             cbIVlen = 8;//RSN Header
2765             cbICVlen = 8;//MIC
2766         }
2767     }
2768 
2769     cbFrameSize = cbMACHdLen + cbIVlen + (cbFrameBodySize + cbMIClen) + cbICVlen + cbFCSlen;
2770 
2771     if ((cbFrameSize > pDevice->wFragmentationThreshold) && (bNeedACK == true)) {
2772         // Fragmentation
2773         cbFragmentSize = pDevice->wFragmentationThreshold;
2774         cbFragPayloadSize = cbFragmentSize - cbMACHdLen - cbIVlen - cbICVlen - cbFCSlen;
2775         uMACfragNum = (unsigned short) ((cbFrameBodySize + cbMIClen) / cbFragPayloadSize);
2776         cbLastFragPayloadSize = (cbFrameBodySize + cbMIClen) % cbFragPayloadSize;
2777         if (cbLastFragPayloadSize == 0) {
2778             cbLastFragPayloadSize = cbFragPayloadSize;
2779         } else {
2780             uMACfragNum++;
2781         }
2782     }
2783     return uMACfragNum;
2784 }
2785 
2786 
2787 void
vDMA0_tx_80211(PSDevice pDevice,struct sk_buff * skb,unsigned char * pbMPDU,unsigned int cbMPDULen)2788 vDMA0_tx_80211(PSDevice  pDevice, struct sk_buff *skb, unsigned char *pbMPDU, unsigned int cbMPDULen) {
2789 
2790     PSTxDesc        pFrstTD;
2791     unsigned char byPktType;
2792     unsigned char *pbyTxBufferAddr;
2793     void *          pvRTS;
2794     void *          pvCTS;
2795     void *          pvTxDataHd;
2796     unsigned int uDuration;
2797     unsigned int cbReqCount;
2798     PS802_11Header  pMACHeader;
2799     unsigned int cbHeaderSize;
2800     unsigned int cbFrameBodySize;
2801     bool bNeedACK;
2802     bool bIsPSPOLL = false;
2803     PSTxBufHead     pTxBufHead;
2804     unsigned int cbFrameSize;
2805     unsigned int cbIVlen = 0;
2806     unsigned int cbICVlen = 0;
2807     unsigned int cbMIClen = 0;
2808     unsigned int cbFCSlen = 4;
2809     unsigned int uPadding = 0;
2810     unsigned int cbMICHDR = 0;
2811     unsigned int uLength = 0;
2812     unsigned long dwMICKey0, dwMICKey1;
2813     unsigned long dwMIC_Priority;
2814     unsigned long *pdwMIC_L;
2815     unsigned long *pdwMIC_R;
2816     unsigned short wTxBufSize;
2817     unsigned int cbMacHdLen;
2818     SEthernetHeader sEthHeader;
2819     void *          pvRrvTime;
2820     void *          pMICHDR;
2821     PSMgmtObject    pMgmt = pDevice->pMgmt;
2822     unsigned short wCurrentRate = RATE_1M;
2823     PUWLAN_80211HDR  p80211Header;
2824     unsigned int uNodeIndex = 0;
2825     bool bNodeExist = false;
2826     SKeyItem        STempKey;
2827     PSKeyItem       pTransmitKey = NULL;
2828     unsigned char *pbyIVHead;
2829     unsigned char *pbyPayloadHead;
2830     unsigned char *pbyMacHdr;
2831 
2832     unsigned int cbExtSuppRate = 0;
2833 //    PWLAN_IE        pItem;
2834 
2835 
2836     pvRrvTime = pMICHDR = pvRTS = pvCTS = pvTxDataHd = NULL;
2837 
2838     if(cbMPDULen <= WLAN_HDR_ADDR3_LEN) {
2839        cbFrameBodySize = 0;
2840     }
2841     else {
2842        cbFrameBodySize = cbMPDULen - WLAN_HDR_ADDR3_LEN;
2843     }
2844     p80211Header = (PUWLAN_80211HDR)pbMPDU;
2845 
2846 
2847     pFrstTD = pDevice->apCurrTD[TYPE_TXDMA0];
2848     pbyTxBufferAddr = (unsigned char *)pFrstTD->pTDInfo->buf;
2849     pTxBufHead = (PSTxBufHead) pbyTxBufferAddr;
2850     wTxBufSize = sizeof(STxBufHead);
2851     memset(pTxBufHead, 0, wTxBufSize);
2852 
2853     if (pDevice->eCurrentPHYType == PHY_TYPE_11A) {
2854         wCurrentRate = RATE_6M;
2855         byPktType = PK_TYPE_11A;
2856     } else {
2857         wCurrentRate = RATE_1M;
2858         byPktType = PK_TYPE_11B;
2859     }
2860 
2861     // SetPower will cause error power TX state for OFDM Date packet in TX buffer.
2862     // 2004.11.11 Kyle -- Using OFDM power to tx MngPkt will decrease the connection capability.
2863     //                    And cmd timer will wait data pkt TX finish before scanning so it's OK
2864     //                    to set power here.
2865     if (pDevice->pMgmt->eScanState != WMAC_NO_SCANNING) {
2866         RFbSetPower(pDevice, wCurrentRate, pDevice->byCurrentCh);
2867     } else {
2868         RFbSetPower(pDevice, wCurrentRate, pMgmt->uCurrChannel);
2869     }
2870     pTxBufHead->byTxPower = pDevice->byCurPwr;
2871 
2872     //+++++++++++++++++++++ Patch VT3253 A1 performance +++++++++++++++++++++++++++
2873     if (pDevice->byFOETuning) {
2874         if ((p80211Header->sA3.wFrameCtl & TYPE_DATE_NULL) == TYPE_DATE_NULL) {
2875             wCurrentRate = RATE_24M;
2876             byPktType = PK_TYPE_11GA;
2877         }
2878     }
2879 
2880     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"vDMA0_tx_80211: p80211Header->sA3.wFrameCtl = %x \n", p80211Header->sA3.wFrameCtl);
2881 
2882     //Set packet type
2883     if (byPktType == PK_TYPE_11A) {//0000 0000 0000 0000
2884         pTxBufHead->wFIFOCtl = 0;
2885     }
2886     else if (byPktType == PK_TYPE_11B) {//0000 0001 0000 0000
2887         pTxBufHead->wFIFOCtl |= FIFOCTL_11B;
2888     }
2889     else if (byPktType == PK_TYPE_11GB) {//0000 0010 0000 0000
2890         pTxBufHead->wFIFOCtl |= FIFOCTL_11GB;
2891     }
2892     else if (byPktType == PK_TYPE_11GA) {//0000 0011 0000 0000
2893         pTxBufHead->wFIFOCtl |= FIFOCTL_11GA;
2894     }
2895 
2896     pTxBufHead->wFIFOCtl |= FIFOCTL_TMOEN;
2897     pTxBufHead->wTimeStamp = cpu_to_le16(DEFAULT_MGN_LIFETIME_RES_64us);
2898 
2899 
2900     if (is_multicast_ether_addr(&(p80211Header->sA3.abyAddr1[0]))) {
2901         bNeedACK = false;
2902         if (pDevice->bEnableHostWEP) {
2903             uNodeIndex = 0;
2904             bNodeExist = true;
2905         };
2906     }
2907     else {
2908         if (pDevice->bEnableHostWEP) {
2909             if (BSSDBbIsSTAInNodeDB(pDevice->pMgmt, (unsigned char *)(p80211Header->sA3.abyAddr1), &uNodeIndex))
2910                 bNodeExist = true;
2911         };
2912         bNeedACK = true;
2913         pTxBufHead->wFIFOCtl |= FIFOCTL_NEEDACK;
2914     };
2915 
2916     if ((pMgmt->eCurrMode == WMAC_MODE_ESS_AP) ||
2917         (pMgmt->eCurrMode == WMAC_MODE_IBSS_STA) ) {
2918 
2919         pTxBufHead->wFIFOCtl |= FIFOCTL_LRETRY;
2920         //Set Preamble type always long
2921         //pDevice->byPreambleType = PREAMBLE_LONG;
2922 
2923         // probe-response don't retry
2924         //if ((p80211Header->sA4.wFrameCtl & TYPE_SUBTYPE_MASK) == TYPE_MGMT_PROBE_RSP) {
2925         //     bNeedACK = false;
2926         //     pTxBufHead->wFIFOCtl  &= (~FIFOCTL_NEEDACK);
2927         //}
2928     }
2929 
2930     pTxBufHead->wFIFOCtl |= (FIFOCTL_GENINT | FIFOCTL_ISDMA0);
2931 
2932     if ((p80211Header->sA4.wFrameCtl & TYPE_SUBTYPE_MASK) == TYPE_CTL_PSPOLL) {
2933         bIsPSPOLL = true;
2934         cbMacHdLen = WLAN_HDR_ADDR2_LEN;
2935     } else {
2936         cbMacHdLen = WLAN_HDR_ADDR3_LEN;
2937     }
2938 
2939     // hostapd deamon ext support rate patch
2940     if (WLAN_GET_FC_FSTYPE(p80211Header->sA4.wFrameCtl) == WLAN_FSTYPE_ASSOCRESP) {
2941 
2942         if (((PWLAN_IE_SUPP_RATES)pMgmt->abyCurrSuppRates)->len != 0) {
2943             cbExtSuppRate += ((PWLAN_IE_SUPP_RATES)pMgmt->abyCurrSuppRates)->len + WLAN_IEHDR_LEN;
2944          }
2945 
2946         if (((PWLAN_IE_SUPP_RATES)pMgmt->abyCurrExtSuppRates)->len != 0) {
2947             cbExtSuppRate += ((PWLAN_IE_SUPP_RATES)pMgmt->abyCurrExtSuppRates)->len + WLAN_IEHDR_LEN;
2948          }
2949 
2950          if (cbExtSuppRate >0) {
2951             cbFrameBodySize = WLAN_ASSOCRESP_OFF_SUPP_RATES;
2952          }
2953     }
2954 
2955 
2956     //Set FRAGCTL_MACHDCNT
2957     pTxBufHead->wFragCtl |= cpu_to_le16((unsigned short)cbMacHdLen << 10);
2958 
2959     // Notes:
2960     // Although spec says MMPDU can be fragmented; In most case,
2961     // no one will send a MMPDU under fragmentation. With RTS may occur.
2962     pDevice->bAES = false;  //Set FRAGCTL_WEPTYP
2963 
2964 
2965     if (WLAN_GET_FC_ISWEP(p80211Header->sA4.wFrameCtl) != 0) {
2966         if (pDevice->eEncryptionStatus == Ndis802_11Encryption1Enabled) {
2967             cbIVlen = 4;
2968             cbICVlen = 4;
2969     	    pTxBufHead->wFragCtl |= FRAGCTL_LEGACY;
2970         }
2971         else if (pDevice->eEncryptionStatus == Ndis802_11Encryption2Enabled) {
2972             cbIVlen = 8;//IV+ExtIV
2973             cbMIClen = 8;
2974             cbICVlen = 4;
2975     	    pTxBufHead->wFragCtl |= FRAGCTL_TKIP;
2976     	    //We need to get seed here for filling TxKey entry.
2977             //TKIPvMixKey(pTransmitKey->abyKey, pDevice->abyCurrentNetAddr,
2978             //            pTransmitKey->wTSC15_0, pTransmitKey->dwTSC47_16, pDevice->abyPRNG);
2979         }
2980         else if (pDevice->eEncryptionStatus == Ndis802_11Encryption3Enabled) {
2981             cbIVlen = 8;//RSN Header
2982             cbICVlen = 8;//MIC
2983             cbMICHDR = sizeof(SMICHDRHead);
2984             pTxBufHead->wFragCtl |= FRAGCTL_AES;
2985             pDevice->bAES = true;
2986         }
2987         //MAC Header should be padding 0 to DW alignment.
2988         uPadding = 4 - (cbMacHdLen%4);
2989         uPadding %= 4;
2990     }
2991 
2992     cbFrameSize = cbMacHdLen + cbFrameBodySize + cbIVlen + cbMIClen + cbICVlen + cbFCSlen + cbExtSuppRate;
2993 
2994     //Set FIFOCTL_GrpAckPolicy
2995     if (pDevice->bGrpAckPolicy == true) {//0000 0100 0000 0000
2996         pTxBufHead->wFIFOCtl |=	FIFOCTL_GRPACK;
2997     }
2998     //the rest of pTxBufHead->wFragCtl:FragTyp will be set later in s_vFillFragParameter()
2999 
3000 
3001     if (byPktType == PK_TYPE_11GB || byPktType == PK_TYPE_11GA) {//802.11g packet
3002 
3003         pvRrvTime = (PSRrvTime_gCTS) (pbyTxBufferAddr + wTxBufSize);
3004         pMICHDR = (PSMICHDRHead) (pbyTxBufferAddr + wTxBufSize + sizeof(SRrvTime_gCTS));
3005         pvRTS = NULL;
3006         pvCTS = (PSCTS) (pbyTxBufferAddr + wTxBufSize + sizeof(SRrvTime_gCTS) + cbMICHDR);
3007         pvTxDataHd = (PSTxDataHead_g) (pbyTxBufferAddr + wTxBufSize + sizeof(SRrvTime_gCTS) + cbMICHDR + sizeof(SCTS));
3008         cbHeaderSize = wTxBufSize + sizeof(SRrvTime_gCTS) + cbMICHDR + sizeof(SCTS) + sizeof(STxDataHead_g);
3009 
3010     }
3011     else {//802.11a/b packet
3012 
3013         pvRrvTime = (PSRrvTime_ab) (pbyTxBufferAddr + wTxBufSize);
3014         pMICHDR = (PSMICHDRHead) (pbyTxBufferAddr + wTxBufSize + sizeof(SRrvTime_ab));
3015         pvRTS = NULL;
3016         pvCTS = NULL;
3017         pvTxDataHd = (PSTxDataHead_ab) (pbyTxBufferAddr + wTxBufSize + sizeof(SRrvTime_ab) + cbMICHDR);
3018         cbHeaderSize = wTxBufSize + sizeof(SRrvTime_ab) + cbMICHDR + sizeof(STxDataHead_ab);
3019 
3020     }
3021 
3022     memset((void *)(pbyTxBufferAddr + wTxBufSize), 0, (cbHeaderSize - wTxBufSize));
3023     memcpy(&(sEthHeader.abyDstAddr[0]), &(p80211Header->sA3.abyAddr1[0]), ETH_ALEN);
3024     memcpy(&(sEthHeader.abySrcAddr[0]), &(p80211Header->sA3.abyAddr2[0]), ETH_ALEN);
3025     //=========================
3026     //    No Fragmentation
3027     //=========================
3028     pTxBufHead->wFragCtl |= (unsigned short)FRAGCTL_NONFRAG;
3029 
3030 
3031     //Fill FIFO,RrvTime,RTS,and CTS
3032     s_vGenerateTxParameter(pDevice, byPktType, pbyTxBufferAddr, pvRrvTime, pvRTS, pvCTS,
3033                            cbFrameSize, bNeedACK, TYPE_TXDMA0, &sEthHeader, wCurrentRate);
3034 
3035     //Fill DataHead
3036     uDuration = s_uFillDataHead(pDevice, byPktType, pvTxDataHd, cbFrameSize, TYPE_TXDMA0, bNeedACK,
3037                                 0, 0, 1, AUTO_FB_NONE, wCurrentRate);
3038 
3039     pMACHeader = (PS802_11Header) (pbyTxBufferAddr + cbHeaderSize);
3040 
3041     cbReqCount = cbHeaderSize + cbMacHdLen + uPadding + cbIVlen + (cbFrameBodySize + cbMIClen) + cbExtSuppRate;
3042 
3043     pbyMacHdr = (unsigned char *)(pbyTxBufferAddr + cbHeaderSize);
3044     pbyPayloadHead = (unsigned char *)(pbyMacHdr + cbMacHdLen + uPadding + cbIVlen);
3045     pbyIVHead = (unsigned char *)(pbyMacHdr + cbMacHdLen + uPadding);
3046 
3047     // Copy the Packet into a tx Buffer
3048     memcpy(pbyMacHdr, pbMPDU, cbMacHdLen);
3049 
3050     // version set to 0, patch for hostapd deamon
3051     pMACHeader->wFrameCtl &= cpu_to_le16(0xfffc);
3052     memcpy(pbyPayloadHead, (pbMPDU + cbMacHdLen), cbFrameBodySize);
3053 
3054     // replace support rate, patch for hostapd deamon( only support 11M)
3055     if (WLAN_GET_FC_FSTYPE(p80211Header->sA4.wFrameCtl) == WLAN_FSTYPE_ASSOCRESP) {
3056         if (cbExtSuppRate != 0) {
3057             if (((PWLAN_IE_SUPP_RATES)pMgmt->abyCurrSuppRates)->len != 0)
3058                 memcpy((pbyPayloadHead + cbFrameBodySize),
3059                         pMgmt->abyCurrSuppRates,
3060                         ((PWLAN_IE_SUPP_RATES)pMgmt->abyCurrSuppRates)->len + WLAN_IEHDR_LEN
3061                        );
3062              if (((PWLAN_IE_SUPP_RATES)pMgmt->abyCurrExtSuppRates)->len != 0)
3063                 memcpy((pbyPayloadHead + cbFrameBodySize) + ((PWLAN_IE_SUPP_RATES)pMgmt->abyCurrSuppRates)->len + WLAN_IEHDR_LEN,
3064                         pMgmt->abyCurrExtSuppRates,
3065                         ((PWLAN_IE_SUPP_RATES)pMgmt->abyCurrExtSuppRates)->len + WLAN_IEHDR_LEN
3066                        );
3067          }
3068     }
3069 
3070     // Set wep
3071     if (WLAN_GET_FC_ISWEP(p80211Header->sA4.wFrameCtl) != 0) {
3072 
3073         if (pDevice->bEnableHostWEP) {
3074             pTransmitKey = &STempKey;
3075             pTransmitKey->byCipherSuite = pMgmt->sNodeDBTable[uNodeIndex].byCipherSuite;
3076             pTransmitKey->dwKeyIndex = pMgmt->sNodeDBTable[uNodeIndex].dwKeyIndex;
3077             pTransmitKey->uKeyLength = pMgmt->sNodeDBTable[uNodeIndex].uWepKeyLength;
3078             pTransmitKey->dwTSC47_16 = pMgmt->sNodeDBTable[uNodeIndex].dwTSC47_16;
3079             pTransmitKey->wTSC15_0 = pMgmt->sNodeDBTable[uNodeIndex].wTSC15_0;
3080             memcpy(pTransmitKey->abyKey,
3081                 &pMgmt->sNodeDBTable[uNodeIndex].abyWepKey[0],
3082                 pTransmitKey->uKeyLength
3083                 );
3084         }
3085 
3086         if ((pTransmitKey != NULL) && (pTransmitKey->byCipherSuite == KEY_CTL_TKIP)) {
3087 
3088             dwMICKey0 = *(unsigned long *)(&pTransmitKey->abyKey[16]);
3089             dwMICKey1 = *(unsigned long *)(&pTransmitKey->abyKey[20]);
3090 
3091             // DO Software Michael
3092             MIC_vInit(dwMICKey0, dwMICKey1);
3093             MIC_vAppend((unsigned char *)&(sEthHeader.abyDstAddr[0]), 12);
3094             dwMIC_Priority = 0;
3095             MIC_vAppend((unsigned char *)&dwMIC_Priority, 4);
3096             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"DMA0_tx_8021:MIC KEY: %lX, %lX\n", dwMICKey0, dwMICKey1);
3097 
3098             uLength = cbHeaderSize + cbMacHdLen + uPadding + cbIVlen;
3099 
3100             MIC_vAppend((pbyTxBufferAddr + uLength), cbFrameBodySize);
3101 
3102             pdwMIC_L = (unsigned long *)(pbyTxBufferAddr + uLength + cbFrameBodySize);
3103             pdwMIC_R = (unsigned long *)(pbyTxBufferAddr + uLength + cbFrameBodySize + 4);
3104 
3105             MIC_vGetMIC(pdwMIC_L, pdwMIC_R);
3106             MIC_vUnInit();
3107 
3108             if (pDevice->bTxMICFail == true) {
3109                 *pdwMIC_L = 0;
3110                 *pdwMIC_R = 0;
3111                 pDevice->bTxMICFail = false;
3112             }
3113 
3114             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"uLength: %d, %d\n", uLength, cbFrameBodySize);
3115             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"cbReqCount:%d, %d, %d, %d\n", cbReqCount, cbHeaderSize, uPadding, cbIVlen);
3116             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"MIC:%lx, %lx\n", *pdwMIC_L, *pdwMIC_R);
3117 
3118         }
3119 
3120 
3121         s_vFillTxKey(pDevice, (unsigned char *)(pTxBufHead->adwTxKey), pbyIVHead, pTransmitKey,
3122                      pbyMacHdr, (unsigned short)cbFrameBodySize, (unsigned char *)pMICHDR);
3123 
3124         if (pDevice->bEnableHostWEP) {
3125             pMgmt->sNodeDBTable[uNodeIndex].dwTSC47_16 = pTransmitKey->dwTSC47_16;
3126             pMgmt->sNodeDBTable[uNodeIndex].wTSC15_0 = pTransmitKey->wTSC15_0;
3127         }
3128 
3129         if ((pDevice->byLocalID <= REV_ID_VT3253_A1)) {
3130             s_vSWencryption(pDevice, pTransmitKey, pbyPayloadHead, (unsigned short)(cbFrameBodySize + cbMIClen));
3131         }
3132     }
3133 
3134     pMACHeader->wSeqCtl = cpu_to_le16(pDevice->wSeqCounter << 4);
3135     pDevice->wSeqCounter++ ;
3136     if (pDevice->wSeqCounter > 0x0fff)
3137         pDevice->wSeqCounter = 0;
3138 
3139 
3140     if (bIsPSPOLL) {
3141         // The MAC will automatically replace the Duration-field of MAC header by Duration-field
3142         // of  FIFO control header.
3143         // This will cause AID-field of PS-POLL packet be incorrect (Because PS-POLL's AID field is
3144         // in the same place of other packet's Duration-field).
3145         // And it will cause Cisco-AP to issue Disassociation-packet
3146         if (byPktType == PK_TYPE_11GB || byPktType == PK_TYPE_11GA) {
3147             ((PSTxDataHead_g)pvTxDataHd)->wDuration_a = cpu_to_le16(p80211Header->sA2.wDurationID);
3148             ((PSTxDataHead_g)pvTxDataHd)->wDuration_b = cpu_to_le16(p80211Header->sA2.wDurationID);
3149         } else {
3150             ((PSTxDataHead_ab)pvTxDataHd)->wDuration = cpu_to_le16(p80211Header->sA2.wDurationID);
3151         }
3152     }
3153 
3154 
3155     // first TD is the only TD
3156     //Set TSR1 & ReqCount in TxDescHead
3157     pFrstTD->pTDInfo->skb = skb;
3158     pFrstTD->m_td1TD1.byTCR = (TCR_STP | TCR_EDP | EDMSDU);
3159     pFrstTD->pTDInfo->skb_dma = pFrstTD->pTDInfo->buf_dma;
3160     pFrstTD->m_td1TD1.wReqCount = cpu_to_le16(cbReqCount);
3161     pFrstTD->buff_addr = cpu_to_le32(pFrstTD->pTDInfo->skb_dma);
3162     pFrstTD->pTDInfo->byFlags = 0;
3163     pFrstTD->pTDInfo->byFlags |= TD_FLAGS_PRIV_SKB;
3164 
3165     if (MACbIsRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_PS)) {
3166         // Disable PS
3167         MACbPSWakeup(pDevice->PortOffset);
3168     }
3169     pDevice->bPWBitOn = false;
3170 
3171     wmb();
3172     pFrstTD->m_td0TD0.f1Owner = OWNED_BY_NIC;
3173     wmb();
3174 
3175     pDevice->iTDUsed[TYPE_TXDMA0]++;
3176 
3177     if (AVAIL_TD(pDevice, TYPE_TXDMA0) <= 1) {
3178         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " available td0 <= 1\n");
3179     }
3180 
3181     pDevice->apCurrTD[TYPE_TXDMA0] = pFrstTD->next;
3182 
3183     // Poll Transmit the adapter
3184     MACvTransmit0(pDevice->PortOffset);
3185 
3186     return;
3187 }
3188 
3189 
3190