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