Linux-libre 3.16.85-gnu
[librecmc/linux-libre.git] / drivers / staging / vt6656 / 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 required parameter.
29  *      s_vGenerateMACHeader - Translate 802.3 to 802.11 header
30  *      csBeacon_xmit - beacon tx function
31  *      csMgmt_xmit - management tx function
32  *      s_uGetDataDuration - get tx data required duration
33  *      s_uFillDataHead- fulfill tx data duration header
34  *      s_uGetRTSCTSDuration- get rtx/cts required duration
35  *      s_uGetRTSCTSRsvTime- get rts/cts reserved time
36  *      s_uGetTxRsvTime- get frame reserved time
37  *      s_vFillCTSHead- fulfill CTS ctl header
38  *      s_vFillFragParameter- Set fragment ctl parameter.
39  *      s_vFillRTSHead- fulfill RTS ctl header
40  *      s_vFillTxKey- fulfill tx encrypt key
41  *      s_vSWencryption- Software encrypt header
42  *      vDMA0_tx_80211- tx 802.11 frame via dma0
43  *      vGenerateFIFOHeader- Generate tx FIFO ctl header
44  *
45  * Revision History:
46  *
47  */
48
49 #include "device.h"
50 #include "rxtx.h"
51 #include "tether.h"
52 #include "card.h"
53 #include "bssdb.h"
54 #include "mac.h"
55 #include "michael.h"
56 #include "tkip.h"
57 #include "wctl.h"
58 #include "rf.h"
59 #include "datarate.h"
60 #include "usbpipe.h"
61 #include "iocmd.h"
62
63 static int          msglevel                = MSG_LEVEL_INFO;
64
65 static const u16 wTimeStampOff[2][MAX_RATE] = {
66         {384, 288, 226, 209, 54, 43, 37, 31, 28, 25, 24, 23}, // Long Preamble
67         {384, 192, 130, 113, 54, 43, 37, 31, 28, 25, 24, 23}, // Short Preamble
68     };
69
70 static const u16 wFB_Opt0[2][5] = {
71         {RATE_12M, RATE_18M, RATE_24M, RATE_36M, RATE_48M}, // fallback_rate0
72         {RATE_12M, RATE_12M, RATE_18M, RATE_24M, RATE_36M}, // fallback_rate1
73     };
74 static const u16 wFB_Opt1[2][5] = {
75         {RATE_12M, RATE_18M, RATE_24M, RATE_24M, RATE_36M}, // fallback_rate0
76         {RATE_6M , RATE_6M,  RATE_12M, RATE_12M, RATE_18M}, // fallback_rate1
77     };
78
79 #define RTSDUR_BB       0
80 #define RTSDUR_BA       1
81 #define RTSDUR_AA       2
82 #define CTSDUR_BA       3
83 #define RTSDUR_BA_F0    4
84 #define RTSDUR_AA_F0    5
85 #define RTSDUR_BA_F1    6
86 #define RTSDUR_AA_F1    7
87 #define CTSDUR_BA_F0    8
88 #define CTSDUR_BA_F1    9
89 #define DATADUR_B       10
90 #define DATADUR_A       11
91 #define DATADUR_A_F0    12
92 #define DATADUR_A_F1    13
93
94 static void s_vSaveTxPktInfo(struct vnt_private *pDevice, u8 byPktNum,
95         u8 *pbyDestAddr, u16 wPktLength, u16 wFIFOCtl);
96
97 static struct vnt_usb_send_context *s_vGetFreeContext(struct vnt_private *);
98
99 static u16 s_vGenerateTxParameter(struct vnt_private *pDevice,
100         u8 byPktType, u16 wCurrentRate, struct vnt_tx_buffer *tx_buffer,
101         struct vnt_mic_hdr **mic_hdr, u32 need_mic, u32 cbFrameSize,
102         int bNeedACK, struct ethhdr *psEthHeader, bool need_rts);
103
104 static void s_vGenerateMACHeader(struct vnt_private *pDevice,
105         u8 *pbyBufferAddr, u16 wDuration, struct ethhdr *psEthHeader,
106         int bNeedEncrypt, u16 wFragType, u32 uFragIdx);
107
108 static void s_vFillTxKey(struct vnt_private *pDevice,
109         struct vnt_tx_fifo_head *fifo_head, u8 *pbyIVHead,
110         PSKeyItem pTransmitKey, u8 *pbyHdrBuf, u16 wPayloadLen,
111         struct vnt_mic_hdr *mic_hdr);
112
113 static void s_vSWencryption(struct vnt_private *pDevice,
114         PSKeyItem pTransmitKey, u8 *pbyPayloadHead, u16 wPayloadSize);
115
116 static unsigned int s_uGetTxRsvTime(struct vnt_private *pDevice, u8 byPktType,
117         u32 cbFrameLength, u16 wRate, int bNeedAck);
118
119 static __le16 s_uGetRTSCTSRsvTime(struct vnt_private *priv,
120         u8 rsv_type, u8 pkt_type, u32 frame_length, u16 current_rate);
121
122 static u16 s_vFillCTSHead(struct vnt_private *pDevice,
123         u8 byPktType, union vnt_tx_data_head *head, u32 cbFrameLength,
124         int bNeedAck, u16 wCurrentRate, u8 byFBOption);
125
126 static u16 s_vFillRTSHead(struct vnt_private *pDevice, u8 byPktType,
127         union vnt_tx_data_head *head, u32 cbFrameLength, int bNeedAck,
128         struct ethhdr *psEthHeader, u16 wCurrentRate, u8 byFBOption);
129
130 static __le16 s_uGetDataDuration(struct vnt_private *pDevice,
131         u8 byPktType, int bNeedAck);
132
133 static __le16 s_uGetRTSCTSDuration(struct vnt_private *pDevice,
134         u8 byDurType, u32 cbFrameLength, u8 byPktType, u16 wRate,
135         int bNeedAck, u8 byFBOption);
136
137 static struct vnt_usb_send_context
138         *s_vGetFreeContext(struct vnt_private *priv)
139 {
140         struct vnt_usb_send_context *context = NULL;
141         int ii;
142
143         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"GetFreeContext()\n");
144
145         for (ii = 0; ii < priv->cbTD; ii++) {
146                 if (!priv->apTD[ii])
147                         return NULL;
148
149                 context = priv->apTD[ii];
150                 if (context->in_use == false) {
151                         context->in_use = true;
152                         memset(context->data, 0,
153                                         MAX_TOTAL_SIZE_WITH_ALL_HEADERS);
154                         return context;
155                 }
156         }
157
158         if (ii == priv->cbTD)
159                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"No Free Tx Context\n");
160
161         return NULL;
162 }
163
164 static void s_vSaveTxPktInfo(struct vnt_private *pDevice, u8 byPktNum,
165         u8 *pbyDestAddr, u16 wPktLength, u16 wFIFOCtl)
166 {
167         struct net_device_stats *stats = &pDevice->stats;
168         struct vnt_tx_pkt_info *pkt_info = pDevice->pkt_info;
169
170         pkt_info[byPktNum].fifo_ctl = wFIFOCtl;
171         memcpy(pkt_info[byPktNum].dest_addr, pbyDestAddr, ETH_ALEN);
172
173         stats->tx_bytes += wPktLength;
174 }
175
176 static void s_vFillTxKey(struct vnt_private *pDevice,
177         struct vnt_tx_fifo_head *fifo_head, u8 *pbyIVHead,
178         PSKeyItem pTransmitKey, u8 *pbyHdrBuf, u16 wPayloadLen,
179         struct vnt_mic_hdr *mic_hdr)
180 {
181         u8 *pbyBuf = (u8 *)&fifo_head->adwTxKey[0];
182         __le32 *pdwIV = (__le32 *)pbyIVHead;
183         __le32 *pdwExtIV = (__le32 *)((u8 *)pbyIVHead + 4);
184         struct ieee80211_hdr *pMACHeader = (struct ieee80211_hdr *)pbyHdrBuf;
185         __le32 rev_iv_counter;
186
187         /* Fill TXKEY */
188         if (pTransmitKey == NULL)
189                 return;
190
191         rev_iv_counter = cpu_to_le32(pDevice->dwIVCounter);
192         *pdwIV = cpu_to_le32(pDevice->dwIVCounter);
193         pDevice->byKeyIndex = pTransmitKey->dwKeyIndex & 0xf;
194
195         switch (pTransmitKey->byCipherSuite) {
196         case KEY_CTL_WEP:
197                 if (pTransmitKey->uKeyLength == WLAN_WEP232_KEYLEN) {
198                         memcpy(pDevice->abyPRNG, (u8 *)&rev_iv_counter, 3);
199                         memcpy(pDevice->abyPRNG + 3, pTransmitKey->abyKey,
200                                                 pTransmitKey->uKeyLength);
201                 } else {
202                         memcpy(pbyBuf, (u8 *)&rev_iv_counter, 3);
203                         memcpy(pbyBuf + 3, pTransmitKey->abyKey,
204                                                 pTransmitKey->uKeyLength);
205                         if (pTransmitKey->uKeyLength == WLAN_WEP40_KEYLEN) {
206                                 memcpy(pbyBuf+8, (u8 *)&rev_iv_counter, 3);
207                                 memcpy(pbyBuf+11, pTransmitKey->abyKey,
208                                                 pTransmitKey->uKeyLength);
209                         }
210
211                         memcpy(pDevice->abyPRNG, pbyBuf, 16);
212                 }
213                 /* Append IV after Mac Header */
214                 *pdwIV &= cpu_to_le32(WEP_IV_MASK);
215                 *pdwIV |= cpu_to_le32((u32)pDevice->byKeyIndex << 30);
216
217                 pDevice->dwIVCounter++;
218                 if (pDevice->dwIVCounter > WEP_IV_MASK)
219                         pDevice->dwIVCounter = 0;
220
221                 break;
222         case KEY_CTL_TKIP:
223                 pTransmitKey->wTSC15_0++;
224                 if (pTransmitKey->wTSC15_0 == 0)
225                         pTransmitKey->dwTSC47_16++;
226
227                 TKIPvMixKey(pTransmitKey->abyKey, pDevice->abyCurrentNetAddr,
228                         pTransmitKey->wTSC15_0, pTransmitKey->dwTSC47_16,
229                                                         pDevice->abyPRNG);
230                 memcpy(pbyBuf, pDevice->abyPRNG, 16);
231
232                 /* Make IV */
233                 memcpy(pdwIV, pDevice->abyPRNG, 3);
234
235                 *(pbyIVHead+3) = (u8)(((pDevice->byKeyIndex << 6) &
236                                                         0xc0) | 0x20);
237                 /*  Append IV&ExtIV after Mac Header */
238                 *pdwExtIV = cpu_to_le32(pTransmitKey->dwTSC47_16);
239
240                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO
241                         "vFillTxKey()---- pdwExtIV: %x\n", *pdwExtIV);
242
243                 break;
244         case KEY_CTL_CCMP:
245                 pTransmitKey->wTSC15_0++;
246                 if (pTransmitKey->wTSC15_0 == 0)
247                         pTransmitKey->dwTSC47_16++;
248
249                 memcpy(pbyBuf, pTransmitKey->abyKey, 16);
250
251                 /* Make IV */
252                 *pdwIV = 0;
253                 *(pbyIVHead+3) = (u8)(((pDevice->byKeyIndex << 6) &
254                                                         0xc0) | 0x20);
255
256                 *pdwIV |= cpu_to_le32((u32)(pTransmitKey->wTSC15_0));
257
258                 /* Append IV&ExtIV after Mac Header */
259                 *pdwExtIV = cpu_to_le32(pTransmitKey->dwTSC47_16);
260
261                 if (!mic_hdr)
262                         return;
263
264                 /* MICHDR0 */
265                 mic_hdr->id = 0x59;
266                 mic_hdr->payload_len = cpu_to_be16(wPayloadLen);
267                 memcpy(mic_hdr->mic_addr2, pMACHeader->addr2, ETH_ALEN);
268
269                 mic_hdr->tsc_47_16 = cpu_to_be32(pTransmitKey->dwTSC47_16);
270                 mic_hdr->tsc_15_0 = cpu_to_be16(pTransmitKey->wTSC15_0);
271
272                 /* MICHDR1 */
273                 if (ieee80211_has_a4(pMACHeader->frame_control))
274                         mic_hdr->hlen = cpu_to_be16(28);
275                 else
276                         mic_hdr->hlen = cpu_to_be16(22);
277
278                 memcpy(mic_hdr->addr1, pMACHeader->addr1, ETH_ALEN);
279                 memcpy(mic_hdr->addr2, pMACHeader->addr2, ETH_ALEN);
280
281                 /* MICHDR2 */
282                 memcpy(mic_hdr->addr3, pMACHeader->addr3, ETH_ALEN);
283                 mic_hdr->frame_control = cpu_to_le16(
284                         le16_to_cpu(pMACHeader->frame_control) & 0xc78f);
285                 mic_hdr->seq_ctrl = cpu_to_le16(
286                                 le16_to_cpu(pMACHeader->seq_ctrl) & 0xf);
287
288                 if (ieee80211_has_a4(pMACHeader->frame_control))
289                         memcpy(mic_hdr->addr4, pMACHeader->addr4, ETH_ALEN);
290         }
291 }
292
293 static void s_vSWencryption(struct vnt_private *pDevice,
294         PSKeyItem pTransmitKey, u8 *pbyPayloadHead, u16 wPayloadSize)
295 {
296         u32 cbICVlen = 4;
297         u32 dwICV = 0xffffffff;
298         u32 *pdwICV;
299
300     if (pTransmitKey == NULL)
301         return;
302
303     if (pTransmitKey->byCipherSuite == KEY_CTL_WEP) {
304         //=======================================================================
305         // Append ICV after payload
306         dwICV = ether_crc_le(wPayloadSize, pbyPayloadHead);
307         pdwICV = (u32 *)(pbyPayloadHead + wPayloadSize);
308         // finally, we must invert dwCRC to get the correct answer
309         *pdwICV = cpu_to_le32(~dwICV);
310         // RC4 encryption
311         rc4_init(&pDevice->SBox, pDevice->abyPRNG, pTransmitKey->uKeyLength + 3);
312         rc4_encrypt(&pDevice->SBox, pbyPayloadHead, pbyPayloadHead, wPayloadSize+cbICVlen);
313         //=======================================================================
314     } else if (pTransmitKey->byCipherSuite == KEY_CTL_TKIP) {
315         //=======================================================================
316         //Append ICV after payload
317         dwICV = ether_crc_le(wPayloadSize, pbyPayloadHead);
318         pdwICV = (u32 *)(pbyPayloadHead + wPayloadSize);
319         // finally, we must invert dwCRC to get the correct answer
320         *pdwICV = cpu_to_le32(~dwICV);
321         // RC4 encryption
322         rc4_init(&pDevice->SBox, pDevice->abyPRNG, TKIP_KEY_LEN);
323         rc4_encrypt(&pDevice->SBox, pbyPayloadHead, pbyPayloadHead, wPayloadSize+cbICVlen);
324         //=======================================================================
325     }
326 }
327
328 static __le16 vnt_time_stamp_off(struct vnt_private *priv, u16 rate)
329 {
330         return cpu_to_le16(wTimeStampOff[priv->byPreambleType % 2]
331                                                         [rate % MAX_RATE]);
332 }
333
334 /*byPktType : PK_TYPE_11A     0
335              PK_TYPE_11B     1
336              PK_TYPE_11GB    2
337              PK_TYPE_11GA    3
338 */
339 static u32 s_uGetTxRsvTime(struct vnt_private *priv, u8 pkt_type,
340         u32 frame_length, u16 rate, int need_ack)
341 {
342         u32 data_time, ack_time;
343
344         data_time = BBuGetFrameTime(priv->byPreambleType, pkt_type,
345                                                         frame_length, rate);
346
347         if (pkt_type == PK_TYPE_11B)
348                 ack_time = BBuGetFrameTime(priv->byPreambleType, pkt_type, 14,
349                                                 (u16)priv->byTopCCKBasicRate);
350         else
351                 ack_time = BBuGetFrameTime(priv->byPreambleType, pkt_type, 14,
352                                                 (u16)priv->byTopOFDMBasicRate);
353
354         if (need_ack)
355                 return data_time + priv->uSIFS + ack_time;
356
357         return data_time;
358 }
359
360 static __le16 vnt_rxtx_rsvtime_le16(struct vnt_private *priv, u8 pkt_type,
361         u32 frame_length, u16 rate, int need_ack)
362 {
363         return cpu_to_le16((u16)s_uGetTxRsvTime(priv, pkt_type,
364                 frame_length, rate, need_ack));
365 }
366
367 //byFreqType: 0=>5GHZ 1=>2.4GHZ
368 static __le16 s_uGetRTSCTSRsvTime(struct vnt_private *priv,
369         u8 rsv_type, u8 pkt_type, u32 frame_length, u16 current_rate)
370 {
371         u32 rrv_time, rts_time, cts_time, ack_time, data_time;
372
373         rrv_time = rts_time = cts_time = ack_time = data_time = 0;
374
375         data_time = BBuGetFrameTime(priv->byPreambleType, pkt_type,
376                                                 frame_length, current_rate);
377
378         if (rsv_type == 0) {
379                 rts_time = BBuGetFrameTime(priv->byPreambleType,
380                         pkt_type, 20, priv->byTopCCKBasicRate);
381                 cts_time = ack_time = BBuGetFrameTime(priv->byPreambleType,
382                         pkt_type, 14, priv->byTopCCKBasicRate);
383         } else if (rsv_type == 1) {
384                 rts_time = BBuGetFrameTime(priv->byPreambleType,
385                         pkt_type, 20, priv->byTopCCKBasicRate);
386                 cts_time = BBuGetFrameTime(priv->byPreambleType, pkt_type,
387                         14, priv->byTopCCKBasicRate);
388                 ack_time = BBuGetFrameTime(priv->byPreambleType, pkt_type,
389                         14, priv->byTopOFDMBasicRate);
390         } else if (rsv_type == 2) {
391                 rts_time = BBuGetFrameTime(priv->byPreambleType, pkt_type,
392                         20, priv->byTopOFDMBasicRate);
393                 cts_time = ack_time = BBuGetFrameTime(priv->byPreambleType,
394                         pkt_type, 14, priv->byTopOFDMBasicRate);
395         } else if (rsv_type == 3) {
396                 cts_time = BBuGetFrameTime(priv->byPreambleType, pkt_type,
397                         14, priv->byTopCCKBasicRate);
398                 ack_time = BBuGetFrameTime(priv->byPreambleType, pkt_type,
399                         14, priv->byTopOFDMBasicRate);
400
401                 rrv_time = cts_time + ack_time + data_time + 2 * priv->uSIFS;
402
403                 return cpu_to_le16((u16)rrv_time);
404         }
405
406         rrv_time = rts_time + cts_time + ack_time + data_time + 3 * priv->uSIFS;
407
408         return cpu_to_le16((u16)rrv_time);
409 }
410
411 //byFreqType 0: 5GHz, 1:2.4Ghz
412 static __le16 s_uGetDataDuration(struct vnt_private *pDevice,
413                                         u8 byPktType, int bNeedAck)
414 {
415         u32 uAckTime = 0;
416
417         if (bNeedAck) {
418                 if (byPktType == PK_TYPE_11B)
419                         uAckTime = BBuGetFrameTime(pDevice->byPreambleType,
420                                 byPktType, 14, pDevice->byTopCCKBasicRate);
421                 else
422                         uAckTime = BBuGetFrameTime(pDevice->byPreambleType,
423                                 byPktType, 14, pDevice->byTopOFDMBasicRate);
424                 return cpu_to_le16((u16)(pDevice->uSIFS + uAckTime));
425         }
426
427         return 0;
428 }
429
430 //byFreqType: 0=>5GHZ 1=>2.4GHZ
431 static __le16 s_uGetRTSCTSDuration(struct vnt_private *pDevice, u8 byDurType,
432         u32 cbFrameLength, u8 byPktType, u16 wRate, int bNeedAck,
433         u8 byFBOption)
434 {
435         u32 uCTSTime = 0, uDurTime = 0;
436
437         switch (byDurType) {
438         case RTSDUR_BB:
439         case RTSDUR_BA:
440         case RTSDUR_BA_F0:
441         case RTSDUR_BA_F1:
442                 uCTSTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType,
443                         14, pDevice->byTopCCKBasicRate);
444                 uDurTime = uCTSTime + 2 * pDevice->uSIFS +
445                         s_uGetTxRsvTime(pDevice, byPktType,
446                                                 cbFrameLength, wRate, bNeedAck);
447                 break;
448
449         case RTSDUR_AA:
450         case RTSDUR_AA_F0:
451         case RTSDUR_AA_F1:
452                 uCTSTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType,
453                         14, pDevice->byTopOFDMBasicRate);
454                 uDurTime = uCTSTime + 2 * pDevice->uSIFS +
455                         s_uGetTxRsvTime(pDevice, byPktType,
456                                                 cbFrameLength, wRate, bNeedAck);
457                 break;
458
459         case CTSDUR_BA:
460         case CTSDUR_BA_F0:
461         case CTSDUR_BA_F1:
462                 uDurTime = pDevice->uSIFS + s_uGetTxRsvTime(pDevice,
463                                 byPktType, cbFrameLength, wRate, bNeedAck);
464                 break;
465
466         default:
467                 break;
468         }
469
470         return cpu_to_le16((u16)uDurTime);
471 }
472
473 static u16 vnt_rxtx_datahead_g(struct vnt_private *priv, u8 pkt_type, u16 rate,
474                 struct vnt_tx_datahead_g *buf, u32 frame_len, int need_ack)
475 {
476         /* Get SignalField,ServiceField,Length */
477         BBvCalculateParameter(priv, frame_len, rate, pkt_type, &buf->a);
478         BBvCalculateParameter(priv, frame_len, priv->byTopCCKBasicRate,
479                                                         PK_TYPE_11B, &buf->b);
480
481         /* Get Duration and TimeStamp */
482         buf->duration_a = s_uGetDataDuration(priv, pkt_type, need_ack);
483         buf->duration_b = s_uGetDataDuration(priv, PK_TYPE_11B, need_ack);
484
485         buf->time_stamp_off_a = vnt_time_stamp_off(priv, rate);
486         buf->time_stamp_off_b = vnt_time_stamp_off(priv,
487                                         priv->byTopCCKBasicRate);
488
489         return le16_to_cpu(buf->duration_a);
490 }
491
492 static u16 vnt_rxtx_datahead_g_fb(struct vnt_private *priv, u8 pkt_type,
493                 u16 rate, struct vnt_tx_datahead_g_fb *buf,
494                 u32 frame_len, int need_ack)
495 {
496         /* Get SignalField,ServiceField,Length */
497         BBvCalculateParameter(priv, frame_len, rate, pkt_type, &buf->a);
498
499         BBvCalculateParameter(priv, frame_len, priv->byTopCCKBasicRate,
500                                                 PK_TYPE_11B, &buf->b);
501
502         /* Get Duration and TimeStamp */
503         buf->duration_a = s_uGetDataDuration(priv, pkt_type, need_ack);
504         buf->duration_b = s_uGetDataDuration(priv, PK_TYPE_11B, need_ack);
505
506         buf->duration_a_f0 = s_uGetDataDuration(priv, pkt_type, need_ack);
507         buf->duration_a_f1 = s_uGetDataDuration(priv, pkt_type, need_ack);
508
509         buf->time_stamp_off_a = vnt_time_stamp_off(priv, rate);
510         buf->time_stamp_off_b = vnt_time_stamp_off(priv,
511                                                 priv->byTopCCKBasicRate);
512
513         return le16_to_cpu(buf->duration_a);
514 }
515
516 static u16 vnt_rxtx_datahead_a_fb(struct vnt_private *priv, u8 pkt_type,
517                 u16 rate, struct vnt_tx_datahead_a_fb *buf,
518                 u32 frame_len, int need_ack)
519 {
520         /* Get SignalField,ServiceField,Length */
521         BBvCalculateParameter(priv, frame_len, rate, pkt_type, &buf->a);
522         /* Get Duration and TimeStampOff */
523         buf->duration = s_uGetDataDuration(priv, pkt_type, need_ack);
524
525         buf->duration_f0 = s_uGetDataDuration(priv, pkt_type, need_ack);
526         buf->duration_f1 = s_uGetDataDuration(priv, pkt_type, need_ack);
527
528         buf->time_stamp_off = vnt_time_stamp_off(priv, rate);
529
530         return le16_to_cpu(buf->duration);
531 }
532
533 static u16 vnt_rxtx_datahead_ab(struct vnt_private *priv, u8 pkt_type,
534                 u16 rate, struct vnt_tx_datahead_ab *buf,
535                 u32 frame_len, int need_ack)
536 {
537         /* Get SignalField,ServiceField,Length */
538         BBvCalculateParameter(priv, frame_len, rate, pkt_type, &buf->ab);
539         /* Get Duration and TimeStampOff */
540         buf->duration = s_uGetDataDuration(priv, pkt_type, need_ack);
541
542         buf->time_stamp_off = vnt_time_stamp_off(priv, rate);
543
544         return le16_to_cpu(buf->duration);
545 }
546
547 static int vnt_fill_ieee80211_rts(struct vnt_private *priv,
548         struct ieee80211_rts *rts, struct ethhdr *eth_hdr,
549                 __le16 duration)
550 {
551         rts->duration = duration;
552         rts->frame_control =
553                 cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS);
554
555         if (priv->op_mode == NL80211_IFTYPE_ADHOC ||
556                                 priv->op_mode == NL80211_IFTYPE_AP)
557                 memcpy(rts->ra, eth_hdr->h_dest, ETH_ALEN);
558         else
559                 memcpy(rts->ra, priv->abyBSSID, ETH_ALEN);
560
561         if (priv->op_mode == NL80211_IFTYPE_AP)
562                 memcpy(rts->ta, priv->abyBSSID, ETH_ALEN);
563         else
564                 memcpy(rts->ta, eth_hdr->h_source, ETH_ALEN);
565
566         return 0;
567 }
568
569 static u16 vnt_rxtx_rts_g_head(struct vnt_private *priv,
570         struct vnt_rts_g *buf, struct ethhdr *eth_hdr,
571         u8 pkt_type, u32 frame_len, int need_ack,
572         u16 current_rate, u8 fb_option)
573 {
574         u16 rts_frame_len = 20;
575
576         BBvCalculateParameter(priv, rts_frame_len, priv->byTopCCKBasicRate,
577                 PK_TYPE_11B, &buf->b);
578         BBvCalculateParameter(priv, rts_frame_len,
579                 priv->byTopOFDMBasicRate, pkt_type, &buf->a);
580
581         buf->duration_bb = s_uGetRTSCTSDuration(priv, RTSDUR_BB, frame_len,
582                 PK_TYPE_11B, priv->byTopCCKBasicRate, need_ack, fb_option);
583         buf->duration_aa = s_uGetRTSCTSDuration(priv, RTSDUR_AA, frame_len,
584                 pkt_type, current_rate, need_ack, fb_option);
585         buf->duration_ba = s_uGetRTSCTSDuration(priv, RTSDUR_BA, frame_len,
586                 pkt_type, current_rate, need_ack, fb_option);
587
588         vnt_fill_ieee80211_rts(priv, &buf->data, eth_hdr, buf->duration_aa);
589
590         return vnt_rxtx_datahead_g(priv, pkt_type, current_rate,
591                         &buf->data_head, frame_len, need_ack);
592 }
593
594 static u16 vnt_rxtx_rts_g_fb_head(struct vnt_private *priv,
595         struct vnt_rts_g_fb *buf, struct ethhdr *eth_hdr,
596         u8 pkt_type, u32 frame_len, int need_ack,
597         u16 current_rate, u8 fb_option)
598 {
599         u16 rts_frame_len = 20;
600
601         BBvCalculateParameter(priv, rts_frame_len, priv->byTopCCKBasicRate,
602                 PK_TYPE_11B, &buf->b);
603         BBvCalculateParameter(priv, rts_frame_len,
604                 priv->byTopOFDMBasicRate, pkt_type, &buf->a);
605
606
607         buf->duration_bb = s_uGetRTSCTSDuration(priv, RTSDUR_BB, frame_len,
608                 PK_TYPE_11B, priv->byTopCCKBasicRate, need_ack, fb_option);
609         buf->duration_aa = s_uGetRTSCTSDuration(priv, RTSDUR_AA, frame_len,
610                 pkt_type, current_rate, need_ack, fb_option);
611         buf->duration_ba = s_uGetRTSCTSDuration(priv, RTSDUR_BA, frame_len,
612                 pkt_type, current_rate, need_ack, fb_option);
613
614
615         buf->rts_duration_ba_f0 = s_uGetRTSCTSDuration(priv, RTSDUR_BA_F0,
616                 frame_len, pkt_type, priv->tx_rate_fb0, need_ack, fb_option);
617         buf->rts_duration_aa_f0 = s_uGetRTSCTSDuration(priv, RTSDUR_AA_F0,
618                 frame_len, pkt_type, priv->tx_rate_fb0, need_ack, fb_option);
619         buf->rts_duration_ba_f1 = s_uGetRTSCTSDuration(priv, RTSDUR_BA_F1,
620                 frame_len, pkt_type, priv->tx_rate_fb1, need_ack, fb_option);
621         buf->rts_duration_aa_f1 = s_uGetRTSCTSDuration(priv, RTSDUR_AA_F1,
622                 frame_len, pkt_type, priv->tx_rate_fb1, need_ack, fb_option);
623
624         vnt_fill_ieee80211_rts(priv, &buf->data, eth_hdr, buf->duration_aa);
625
626         return vnt_rxtx_datahead_g_fb(priv, pkt_type, current_rate,
627                         &buf->data_head, frame_len, need_ack);
628 }
629
630 static u16 vnt_rxtx_rts_ab_head(struct vnt_private *priv,
631         struct vnt_rts_ab *buf, struct ethhdr *eth_hdr,
632         u8 pkt_type, u32 frame_len, int need_ack,
633         u16 current_rate, u8 fb_option)
634 {
635         u16 rts_frame_len = 20;
636
637         BBvCalculateParameter(priv, rts_frame_len,
638                 priv->byTopOFDMBasicRate, pkt_type, &buf->ab);
639
640         buf->duration = s_uGetRTSCTSDuration(priv, RTSDUR_AA, frame_len,
641                 pkt_type, current_rate, need_ack, fb_option);
642
643         vnt_fill_ieee80211_rts(priv, &buf->data, eth_hdr, buf->duration);
644
645         return vnt_rxtx_datahead_ab(priv, pkt_type, current_rate,
646                         &buf->data_head, frame_len, need_ack);
647 }
648
649 static u16 vnt_rxtx_rts_a_fb_head(struct vnt_private *priv,
650         struct vnt_rts_a_fb *buf, struct ethhdr *eth_hdr,
651         u8 pkt_type, u32 frame_len, int need_ack,
652         u16 current_rate, u8 fb_option)
653 {
654         u16 rts_frame_len = 20;
655
656         BBvCalculateParameter(priv, rts_frame_len,
657                 priv->byTopOFDMBasicRate, pkt_type, &buf->a);
658
659         buf->duration = s_uGetRTSCTSDuration(priv, RTSDUR_AA, frame_len,
660                 pkt_type, current_rate, need_ack, fb_option);
661
662         buf->rts_duration_f0 = s_uGetRTSCTSDuration(priv, RTSDUR_AA_F0,
663                 frame_len, pkt_type, priv->tx_rate_fb0, need_ack, fb_option);
664
665         buf->rts_duration_f1 = s_uGetRTSCTSDuration(priv, RTSDUR_AA_F1,
666                 frame_len, pkt_type, priv->tx_rate_fb1, need_ack, fb_option);
667
668         vnt_fill_ieee80211_rts(priv, &buf->data, eth_hdr, buf->duration);
669
670         return vnt_rxtx_datahead_a_fb(priv, pkt_type, current_rate,
671                         &buf->data_head, frame_len, need_ack);
672 }
673
674 static u16 s_vFillRTSHead(struct vnt_private *pDevice, u8 byPktType,
675         union vnt_tx_data_head *head, u32 cbFrameLength, int bNeedAck,
676         struct ethhdr *psEthHeader, u16 wCurrentRate, u8 byFBOption)
677 {
678
679         if (!head)
680                 return 0;
681
682         /* Note: So far RTSHead doesn't appear in ATIM
683         *       & Beacom DMA, so we don't need to take them
684         *       into account.
685         *       Otherwise, we need to modified codes for them.
686         */
687         switch (byPktType) {
688         case PK_TYPE_11GB:
689         case PK_TYPE_11GA:
690                 if (byFBOption == AUTO_FB_NONE)
691                         return vnt_rxtx_rts_g_head(pDevice, &head->rts_g,
692                                 psEthHeader, byPktType, cbFrameLength,
693                                 bNeedAck, wCurrentRate, byFBOption);
694                 else
695                         return vnt_rxtx_rts_g_fb_head(pDevice, &head->rts_g_fb,
696                                 psEthHeader, byPktType, cbFrameLength,
697                                 bNeedAck, wCurrentRate, byFBOption);
698                 break;
699         case PK_TYPE_11A:
700                 if (byFBOption) {
701                         return vnt_rxtx_rts_a_fb_head(pDevice, &head->rts_a_fb,
702                                 psEthHeader, byPktType, cbFrameLength,
703                                 bNeedAck, wCurrentRate, byFBOption);
704                         break;
705                 }
706         case PK_TYPE_11B:
707                 return vnt_rxtx_rts_ab_head(pDevice, &head->rts_ab,
708                         psEthHeader, byPktType, cbFrameLength,
709                         bNeedAck, wCurrentRate, byFBOption);
710         }
711
712         return 0;
713 }
714
715 static u16 s_vFillCTSHead(struct vnt_private *pDevice,
716         u8 byPktType, union vnt_tx_data_head *head, u32 cbFrameLength,
717         int bNeedAck, u16 wCurrentRate, u8 byFBOption)
718 {
719         u32 uCTSFrameLen = 14;
720
721         if (!head)
722                 return 0;
723
724         if (byFBOption != AUTO_FB_NONE) {
725                 /* Auto Fall back */
726                 struct vnt_cts_fb *pBuf = &head->cts_g_fb;
727                 /* Get SignalField,ServiceField,Length */
728                 BBvCalculateParameter(pDevice, uCTSFrameLen,
729                         pDevice->byTopCCKBasicRate, PK_TYPE_11B, &pBuf->b);
730                 pBuf->duration_ba = s_uGetRTSCTSDuration(pDevice, CTSDUR_BA,
731                         cbFrameLength, byPktType,
732                         wCurrentRate, bNeedAck, byFBOption);
733                 /* Get CTSDuration_ba_f0 */
734                 pBuf->cts_duration_ba_f0 = s_uGetRTSCTSDuration(pDevice,
735                         CTSDUR_BA_F0, cbFrameLength, byPktType,
736                         pDevice->tx_rate_fb0, bNeedAck, byFBOption);
737                 /* Get CTSDuration_ba_f1 */
738                 pBuf->cts_duration_ba_f1 = s_uGetRTSCTSDuration(pDevice,
739                         CTSDUR_BA_F1, cbFrameLength, byPktType,
740                         pDevice->tx_rate_fb1, bNeedAck, byFBOption);
741                 /* Get CTS Frame body */
742                 pBuf->data.duration = pBuf->duration_ba;
743                 pBuf->data.frame_control =
744                         cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS);
745
746                 memcpy(pBuf->data.ra, pDevice->abyCurrentNetAddr, ETH_ALEN);
747
748                 return vnt_rxtx_datahead_g_fb(pDevice, byPktType, wCurrentRate,
749                                 &pBuf->data_head, cbFrameLength, bNeedAck);
750         } else {
751                 struct vnt_cts *pBuf = &head->cts_g;
752                 /* Get SignalField,ServiceField,Length */
753                 BBvCalculateParameter(pDevice, uCTSFrameLen,
754                         pDevice->byTopCCKBasicRate, PK_TYPE_11B, &pBuf->b);
755                 /* Get CTSDuration_ba */
756                 pBuf->duration_ba = s_uGetRTSCTSDuration(pDevice,
757                         CTSDUR_BA, cbFrameLength, byPktType,
758                         wCurrentRate, bNeedAck, byFBOption);
759                 /*Get CTS Frame body*/
760                 pBuf->data.duration = pBuf->duration_ba;
761                 pBuf->data.frame_control =
762                         cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS);
763
764                 memcpy(pBuf->data.ra, pDevice->abyCurrentNetAddr, ETH_ALEN);
765
766                 return vnt_rxtx_datahead_g(pDevice, byPktType, wCurrentRate,
767                                 &pBuf->data_head, cbFrameLength, bNeedAck);
768         }
769
770         return 0;
771 }
772
773 /*+
774  *
775  * Description:
776  *      Generate FIFO control for MAC & Baseband controller
777  *
778  * Parameters:
779  *  In:
780  *      pDevice         - Pointer to adpater
781  *      pTxDataHead     - Transmit Data Buffer
782  *      pTxBufHead      - pTxBufHead
783  *      pvRrvTime        - pvRrvTime
784  *      pvRTS            - RTS Buffer
785  *      pCTS            - CTS Buffer
786  *      cbFrameSize     - Transmit Data Length (Hdr+Payload+FCS)
787  *      bNeedACK        - If need ACK
788  *  Out:
789  *      none
790  *
791  * Return Value: none
792  *
793 -*/
794
795 static u16 s_vGenerateTxParameter(struct vnt_private *pDevice,
796         u8 byPktType, u16 wCurrentRate, struct vnt_tx_buffer *tx_buffer,
797         struct vnt_mic_hdr **mic_hdr, u32 need_mic, u32 cbFrameSize,
798         int bNeedACK, struct ethhdr *psEthHeader, bool need_rts)
799 {
800         struct vnt_tx_fifo_head *pFifoHead = &tx_buffer->fifo_head;
801         union vnt_tx_data_head *head = NULL;
802         u16 wFifoCtl;
803         u8 byFBOption = AUTO_FB_NONE;
804
805         pFifoHead->current_rate = cpu_to_le16(wCurrentRate);
806         wFifoCtl = pFifoHead->wFIFOCtl;
807
808         if (wFifoCtl & FIFOCTL_AUTO_FB_0)
809                 byFBOption = AUTO_FB_0;
810         else if (wFifoCtl & FIFOCTL_AUTO_FB_1)
811                 byFBOption = AUTO_FB_1;
812
813         if (byPktType == PK_TYPE_11GB || byPktType == PK_TYPE_11GA) {
814                 if (need_rts) {
815                         struct vnt_rrv_time_rts *pBuf =
816                                         &tx_buffer->tx_head.tx_rts.rts;
817
818                         pBuf->rts_rrv_time_aa = s_uGetRTSCTSRsvTime(pDevice, 2,
819                                         byPktType, cbFrameSize, wCurrentRate);
820                         pBuf->rts_rrv_time_ba = s_uGetRTSCTSRsvTime(pDevice, 1,
821                                         byPktType, cbFrameSize, wCurrentRate);
822                         pBuf->rts_rrv_time_bb = s_uGetRTSCTSRsvTime(pDevice, 0,
823                                 byPktType, cbFrameSize, wCurrentRate);
824
825                         pBuf->rrv_time_a = vnt_rxtx_rsvtime_le16(pDevice,
826                                 byPktType, cbFrameSize, wCurrentRate, bNeedACK);
827                         pBuf->rrv_time_b = vnt_rxtx_rsvtime_le16(pDevice,
828                                         PK_TYPE_11B, cbFrameSize,
829                                         pDevice->byTopCCKBasicRate, bNeedACK);
830
831                         if (need_mic) {
832                                 *mic_hdr = &tx_buffer->
833                                                 tx_head.tx_rts.tx.mic.hdr;
834                                 head = &tx_buffer->tx_head.tx_rts.tx.mic.head;
835                         } else {
836                                 head = &tx_buffer->tx_head.tx_rts.tx.head;
837                         }
838
839                         /* Fill RTS */
840                         return s_vFillRTSHead(pDevice, byPktType, head,
841                                         cbFrameSize, bNeedACK, psEthHeader,
842                                                 wCurrentRate, byFBOption);
843
844                 } else {
845                         struct vnt_rrv_time_cts *pBuf = &tx_buffer->
846                                                         tx_head.tx_cts.cts;
847
848                         pBuf->rrv_time_a = vnt_rxtx_rsvtime_le16(pDevice,
849                                 byPktType, cbFrameSize, wCurrentRate, bNeedACK);
850                         pBuf->rrv_time_b = vnt_rxtx_rsvtime_le16(pDevice,
851                                 PK_TYPE_11B, cbFrameSize,
852                                         pDevice->byTopCCKBasicRate, bNeedACK);
853
854                         pBuf->cts_rrv_time_ba = s_uGetRTSCTSRsvTime(pDevice, 3,
855                                         byPktType, cbFrameSize, wCurrentRate);
856
857                         if (need_mic) {
858                                 *mic_hdr = &tx_buffer->
859                                                 tx_head.tx_cts.tx.mic.hdr;
860                                 head = &tx_buffer->tx_head.tx_cts.tx.mic.head;
861                         } else {
862                                 head = &tx_buffer->tx_head.tx_cts.tx.head;
863                         }
864
865                         /* Fill CTS */
866                         return s_vFillCTSHead(pDevice, byPktType,
867                                 head, cbFrameSize, bNeedACK, wCurrentRate,
868                                         byFBOption);
869                 }
870         } else if (byPktType == PK_TYPE_11A) {
871                 if (need_mic) {
872                         *mic_hdr = &tx_buffer->tx_head.tx_ab.tx.mic.hdr;
873                         head = &tx_buffer->tx_head.tx_ab.tx.mic.head;
874                 } else {
875                         head = &tx_buffer->tx_head.tx_ab.tx.head;
876                 }
877
878                 if (need_rts) {
879                         struct vnt_rrv_time_ab *pBuf = &tx_buffer->
880                                                         tx_head.tx_ab.ab;
881
882                         pBuf->rts_rrv_time = s_uGetRTSCTSRsvTime(pDevice, 2,
883                                 byPktType, cbFrameSize, wCurrentRate);
884
885                         pBuf->rrv_time = vnt_rxtx_rsvtime_le16(pDevice,
886                                 byPktType, cbFrameSize, wCurrentRate, bNeedACK);
887
888                         /* Fill RTS */
889                         return s_vFillRTSHead(pDevice, byPktType, head,
890                                 cbFrameSize, bNeedACK, psEthHeader,
891                                         wCurrentRate, byFBOption);
892                 } else {
893                         struct vnt_rrv_time_ab *pBuf = &tx_buffer->
894                                                         tx_head.tx_ab.ab;
895
896                         pBuf->rrv_time = vnt_rxtx_rsvtime_le16(pDevice,
897                                 PK_TYPE_11A, cbFrameSize,
898                                         wCurrentRate, bNeedACK);
899
900                         return vnt_rxtx_datahead_a_fb(pDevice, byPktType,
901                                 wCurrentRate, &head->data_head_a_fb,
902                                                 cbFrameSize, bNeedACK);
903                 }
904         } else if (byPktType == PK_TYPE_11B) {
905                 if (need_mic) {
906                         *mic_hdr = &tx_buffer->tx_head.tx_ab.tx.mic.hdr;
907                         head = &tx_buffer->tx_head.tx_ab.tx.mic.head;
908                 } else {
909                         head = &tx_buffer->tx_head.tx_ab.tx.head;
910                 }
911
912                 if (need_rts) {
913                         struct vnt_rrv_time_ab *pBuf = &tx_buffer->
914                                                         tx_head.tx_ab.ab;
915
916                         pBuf->rts_rrv_time = s_uGetRTSCTSRsvTime(pDevice, 0,
917                                 byPktType, cbFrameSize, wCurrentRate);
918
919                         pBuf->rrv_time = vnt_rxtx_rsvtime_le16(pDevice,
920                                 PK_TYPE_11B, cbFrameSize, wCurrentRate,
921                                                                 bNeedACK);
922
923                         /* Fill RTS */
924                         return s_vFillRTSHead(pDevice, byPktType, head,
925                                 cbFrameSize,
926                         bNeedACK, psEthHeader, wCurrentRate, byFBOption);
927                 } else {
928                         struct vnt_rrv_time_ab *pBuf = &tx_buffer->
929                                                         tx_head.tx_ab.ab;
930
931                         pBuf->rrv_time = vnt_rxtx_rsvtime_le16(pDevice,
932                                 PK_TYPE_11B, cbFrameSize,
933                                         wCurrentRate, bNeedACK);
934
935                         return vnt_rxtx_datahead_ab(pDevice, byPktType,
936                                 wCurrentRate, &head->data_head_ab,
937                                         cbFrameSize, bNeedACK);
938                 }
939         }
940
941         return 0;
942 }
943 /*
944     u8 * pbyBuffer,//point to pTxBufHead
945     u16  wFragType,//00:Non-Frag, 01:Start, 02:Mid, 03:Last
946     unsigned int  cbFragmentSize,//Hdr+payoad+FCS
947 */
948
949 static int s_bPacketToWirelessUsb(struct vnt_private *pDevice, u8 byPktType,
950         struct vnt_tx_buffer *tx_buffer, int bNeedEncryption,
951         u32 uSkbPacketLen, struct ethhdr *psEthHeader,
952         u8 *pPacket, PSKeyItem pTransmitKey, u32 uNodeIndex, u16 wCurrentRate,
953         u32 *pcbHeaderLen, u32 *pcbTotalLen)
954 {
955         struct vnt_tx_fifo_head *pTxBufHead = &tx_buffer->fifo_head;
956         u32 cbFrameSize, cbFrameBodySize;
957         u32 cb802_1_H_len;
958         u32 cbIVlen = 0, cbICVlen = 0, cbMIClen = 0, cbMACHdLen = 0;
959         u32 cbFCSlen = 4, cbMICHDR = 0;
960         int bNeedACK;
961         bool bRTS = false;
962         u8 *pbyType, *pbyMacHdr, *pbyIVHead, *pbyPayloadHead, *pbyTxBufferAddr;
963         u8 abySNAP_RFC1042[ETH_ALEN] = {0xAA, 0xAA, 0x03, 0x00, 0x00, 0x00};
964         u8 abySNAP_Bridgetunnel[ETH_ALEN]
965                 = {0xAA, 0xAA, 0x03, 0x00, 0x00, 0xF8};
966         u32 uDuration;
967         u32 cbHeaderLength = 0, uPadding = 0;
968         struct vnt_mic_hdr *pMICHDR;
969         u8 byFBOption = AUTO_FB_NONE, byFragType;
970         u16 wTxBufSize;
971         u32 dwMICKey0, dwMICKey1, dwMIC_Priority;
972         u32 *pdwMIC_L, *pdwMIC_R;
973         int bSoftWEP = false;
974
975         pMICHDR = NULL;
976
977         if (bNeedEncryption && pTransmitKey->pvKeyTable) {
978                 if (((PSKeyTable)pTransmitKey->pvKeyTable)->bSoftWEP == true)
979                         bSoftWEP = true; /* WEP 256 */
980         }
981
982         /* Get pkt type */
983         if (ntohs(psEthHeader->h_proto) > ETH_DATA_LEN)
984                 cb802_1_H_len = 8;
985         else
986                 cb802_1_H_len = 0;
987
988     cbFrameBodySize = uSkbPacketLen - ETH_HLEN + cb802_1_H_len;
989
990     //Set packet type
991     pTxBufHead->wFIFOCtl |= (u16)(byPktType<<8);
992
993         if (pDevice->op_mode == NL80211_IFTYPE_ADHOC ||
994                         pDevice->op_mode == NL80211_IFTYPE_AP) {
995                 if (is_multicast_ether_addr(psEthHeader->h_dest)) {
996                         bNeedACK = false;
997                         pTxBufHead->wFIFOCtl =
998                                 pTxBufHead->wFIFOCtl & (~FIFOCTL_NEEDACK);
999                 } else {
1000                         bNeedACK = true;
1001                         pTxBufHead->wFIFOCtl |= FIFOCTL_NEEDACK;
1002                 }
1003         } else {
1004                 /* MSDUs in Infra mode always need ACK */
1005                 bNeedACK = true;
1006                 pTxBufHead->wFIFOCtl |= FIFOCTL_NEEDACK;
1007         }
1008
1009     pTxBufHead->time_stamp = cpu_to_le16(DEFAULT_MSDU_LIFETIME_RES_64us);
1010
1011     //Set FRAGCTL_MACHDCNT
1012         cbMACHdLen = WLAN_HDR_ADDR3_LEN;
1013
1014     pTxBufHead->wFragCtl |= (u16)(cbMACHdLen << 10);
1015
1016     //Set FIFOCTL_GrpAckPolicy
1017     if (pDevice->bGrpAckPolicy == true) {//0000 0100 0000 0000
1018         pTxBufHead->wFIFOCtl |= FIFOCTL_GRPACK;
1019     }
1020
1021         /* Set Auto Fallback Ctl */
1022         if (wCurrentRate >= RATE_18M) {
1023                 if (pDevice->byAutoFBCtrl == AUTO_FB_0) {
1024                         pTxBufHead->wFIFOCtl |= FIFOCTL_AUTO_FB_0;
1025
1026                         pDevice->tx_rate_fb0 =
1027                                 wFB_Opt0[FB_RATE0][wCurrentRate - RATE_18M];
1028                         pDevice->tx_rate_fb1 =
1029                                 wFB_Opt0[FB_RATE1][wCurrentRate - RATE_18M];
1030
1031                         byFBOption = AUTO_FB_0;
1032                 } else if (pDevice->byAutoFBCtrl == AUTO_FB_1) {
1033                         pTxBufHead->wFIFOCtl |= FIFOCTL_AUTO_FB_1;
1034                         pDevice->tx_rate_fb0 =
1035                                 wFB_Opt1[FB_RATE0][wCurrentRate - RATE_18M];
1036                         pDevice->tx_rate_fb1 =
1037                                 wFB_Opt1[FB_RATE1][wCurrentRate - RATE_18M];
1038
1039                         byFBOption = AUTO_FB_1;
1040                 }
1041         }
1042
1043     if (bSoftWEP != true) {
1044         if ((bNeedEncryption) && (pTransmitKey != NULL))  { //WEP enabled
1045             if (pTransmitKey->byCipherSuite == KEY_CTL_WEP) { //WEP40 or WEP104
1046                 pTxBufHead->wFragCtl |= FRAGCTL_LEGACY;
1047             }
1048             if (pTransmitKey->byCipherSuite == KEY_CTL_TKIP) {
1049                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Tx Set wFragCtl == FRAGCTL_TKIP\n");
1050                 pTxBufHead->wFragCtl |= FRAGCTL_TKIP;
1051             }
1052             else if (pTransmitKey->byCipherSuite == KEY_CTL_CCMP) { //CCMP
1053                 pTxBufHead->wFragCtl |= FRAGCTL_AES;
1054             }
1055         }
1056     }
1057
1058     if ((bNeedEncryption) && (pTransmitKey != NULL))  {
1059         if (pTransmitKey->byCipherSuite == KEY_CTL_WEP) {
1060             cbIVlen = 4;
1061             cbICVlen = 4;
1062         }
1063         else if (pTransmitKey->byCipherSuite == KEY_CTL_TKIP) {
1064             cbIVlen = 8;//IV+ExtIV
1065             cbMIClen = 8;
1066             cbICVlen = 4;
1067         }
1068         if (pTransmitKey->byCipherSuite == KEY_CTL_CCMP) {
1069             cbIVlen = 8;//RSN Header
1070             cbICVlen = 8;//MIC
1071             cbMICHDR = sizeof(struct vnt_mic_hdr);
1072         }
1073         if (bSoftWEP == false) {
1074             //MAC Header should be padding 0 to DW alignment.
1075             uPadding = 4 - (cbMACHdLen%4);
1076             uPadding %= 4;
1077         }
1078     }
1079
1080     cbFrameSize = cbMACHdLen + cbIVlen + (cbFrameBodySize + cbMIClen) + cbICVlen + cbFCSlen;
1081
1082     if ( (bNeedACK == false) ||(cbFrameSize < pDevice->wRTSThreshold) ) {
1083         bRTS = false;
1084     } else {
1085         bRTS = true;
1086         pTxBufHead->wFIFOCtl |= (FIFOCTL_RTS | FIFOCTL_LRETRY);
1087     }
1088
1089     pbyTxBufferAddr = (u8 *) &(pTxBufHead->adwTxKey[0]);
1090         wTxBufSize = sizeof(struct vnt_tx_fifo_head);
1091
1092     if (byPktType == PK_TYPE_11GB || byPktType == PK_TYPE_11GA) {//802.11g packet
1093         if (byFBOption == AUTO_FB_NONE) {
1094             if (bRTS == true) {//RTS_need
1095                 cbHeaderLength = wTxBufSize + sizeof(struct vnt_rrv_time_rts) +
1096                         cbMICHDR + sizeof(struct vnt_rts_g);
1097             }
1098             else { //RTS_needless
1099                 cbHeaderLength = wTxBufSize + sizeof(struct vnt_rrv_time_cts) +
1100                         cbMICHDR + sizeof(struct vnt_cts);
1101             }
1102         } else {
1103             // Auto Fall Back
1104             if (bRTS == true) {//RTS_need
1105                 cbHeaderLength = wTxBufSize + sizeof(struct vnt_rrv_time_rts) +
1106                         cbMICHDR + sizeof(struct vnt_rts_g_fb);
1107             }
1108             else if (bRTS == false) { //RTS_needless
1109                 cbHeaderLength = wTxBufSize + sizeof(struct vnt_rrv_time_cts) +
1110                                 cbMICHDR + sizeof(struct vnt_cts_fb);
1111             }
1112         } // Auto Fall Back
1113     }
1114     else {//802.11a/b packet
1115         if (byFBOption == AUTO_FB_NONE) {
1116             if (bRTS == true) {//RTS_need
1117                 cbHeaderLength = wTxBufSize + sizeof(struct vnt_rrv_time_ab) +
1118                         cbMICHDR + sizeof(struct vnt_rts_ab);
1119             }
1120             else if (bRTS == false) { //RTS_needless, no MICHDR
1121                 cbHeaderLength = wTxBufSize + sizeof(struct vnt_rrv_time_ab) +
1122                                 cbMICHDR + sizeof(struct vnt_tx_datahead_ab);
1123             }
1124         } else {
1125             // Auto Fall Back
1126             if (bRTS == true) {//RTS_need
1127                 cbHeaderLength = wTxBufSize + sizeof(struct vnt_rrv_time_ab) +
1128                         cbMICHDR + sizeof(struct vnt_rts_a_fb);
1129             }
1130             else if (bRTS == false) { //RTS_needless
1131                 cbHeaderLength = wTxBufSize + sizeof(struct vnt_rrv_time_ab) +
1132                         cbMICHDR + sizeof(struct vnt_tx_datahead_a_fb);
1133             }
1134         } // Auto Fall Back
1135     }
1136
1137     pbyMacHdr = (u8 *)(pbyTxBufferAddr + cbHeaderLength);
1138     pbyIVHead = (u8 *)(pbyMacHdr + cbMACHdLen + uPadding);
1139     pbyPayloadHead = (u8 *)(pbyMacHdr + cbMACHdLen + uPadding + cbIVlen);
1140
1141     //=========================
1142     //    No Fragmentation
1143     //=========================
1144     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"No Fragmentation...\n");
1145     byFragType = FRAGCTL_NONFRAG;
1146     //pTxBufHead = (PSTxBufHead) &(pTxBufHead->adwTxKey[0]);
1147
1148         /* Fill FIFO, RrvTime, RTS and CTS */
1149         uDuration = s_vGenerateTxParameter(pDevice, byPktType, wCurrentRate,
1150                         tx_buffer, &pMICHDR, cbMICHDR,
1151                         cbFrameSize, bNeedACK, psEthHeader, bRTS);
1152
1153     // Generate TX MAC Header
1154     s_vGenerateMACHeader(pDevice, pbyMacHdr, (u16)uDuration, psEthHeader, bNeedEncryption,
1155                 byFragType, 0);
1156
1157     if (bNeedEncryption == true) {
1158         //Fill TXKEY
1159         s_vFillTxKey(pDevice, pTxBufHead, pbyIVHead, pTransmitKey,
1160                 pbyMacHdr, (u16)cbFrameBodySize, pMICHDR);
1161     }
1162
1163         /* 802.1H */
1164         if (ntohs(psEthHeader->h_proto) > ETH_DATA_LEN) {
1165                 if ((psEthHeader->h_proto == cpu_to_be16(ETH_P_IPX)) ||
1166                         (psEthHeader->h_proto == cpu_to_le16(0xF380)))
1167                         memcpy((u8 *) (pbyPayloadHead),
1168                                         abySNAP_Bridgetunnel, 6);
1169                 else
1170                         memcpy((u8 *) (pbyPayloadHead), &abySNAP_RFC1042[0], 6);
1171
1172                 pbyType = (u8 *) (pbyPayloadHead + 6);
1173
1174                 memcpy(pbyType, &(psEthHeader->h_proto), sizeof(u16));
1175         }
1176
1177     if (pPacket != NULL) {
1178         // Copy the Packet into a tx Buffer
1179         memcpy((pbyPayloadHead + cb802_1_H_len),
1180                  (pPacket + ETH_HLEN),
1181                  uSkbPacketLen - ETH_HLEN
1182                  );
1183
1184     } else {
1185         // while bRelayPacketSend psEthHeader is point to header+payload
1186         memcpy((pbyPayloadHead + cb802_1_H_len), ((u8 *)psEthHeader) + ETH_HLEN, uSkbPacketLen - ETH_HLEN);
1187     }
1188
1189     if ((bNeedEncryption == true) && (pTransmitKey != NULL) && (pTransmitKey->byCipherSuite == KEY_CTL_TKIP)) {
1190
1191         ///////////////////////////////////////////////////////////////////
1192
1193         if (pDevice->vnt_mgmt.eAuthenMode == WMAC_AUTH_WPANONE) {
1194                 dwMICKey0 = *(u32 *)(&pTransmitKey->abyKey[16]);
1195                 dwMICKey1 = *(u32 *)(&pTransmitKey->abyKey[20]);
1196         }
1197         else if ((pTransmitKey->dwKeyIndex & AUTHENTICATOR_KEY) != 0) {
1198             dwMICKey0 = *(u32 *)(&pTransmitKey->abyKey[16]);
1199             dwMICKey1 = *(u32 *)(&pTransmitKey->abyKey[20]);
1200         }
1201         else {
1202             dwMICKey0 = *(u32 *)(&pTransmitKey->abyKey[24]);
1203             dwMICKey1 = *(u32 *)(&pTransmitKey->abyKey[28]);
1204         }
1205         // DO Software Michael
1206         MIC_vInit(dwMICKey0, dwMICKey1);
1207         MIC_vAppend((u8 *)&(psEthHeader->h_dest[0]), 12);
1208         dwMIC_Priority = 0;
1209         MIC_vAppend((u8 *)&dwMIC_Priority, 4);
1210         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"MIC KEY: %X, %X\n",
1211                 dwMICKey0, dwMICKey1);
1212
1213         ///////////////////////////////////////////////////////////////////
1214
1215         //DBG_PRN_GRP12(("Length:%d, %d\n", cbFrameBodySize, uFromHDtoPLDLength));
1216         //for (ii = 0; ii < cbFrameBodySize; ii++) {
1217         //    DBG_PRN_GRP12(("%02x ", *((u8 *)((pbyPayloadHead + cb802_1_H_len) + ii))));
1218         //}
1219         //DBG_PRN_GRP12(("\n\n\n"));
1220
1221         MIC_vAppend(pbyPayloadHead, cbFrameBodySize);
1222
1223         pdwMIC_L = (u32 *)(pbyPayloadHead + cbFrameBodySize);
1224         pdwMIC_R = (u32 *)(pbyPayloadHead + cbFrameBodySize + 4);
1225
1226         MIC_vGetMIC(pdwMIC_L, pdwMIC_R);
1227         MIC_vUnInit();
1228
1229         if (pDevice->bTxMICFail == true) {
1230             *pdwMIC_L = 0;
1231             *pdwMIC_R = 0;
1232             pDevice->bTxMICFail = false;
1233         }
1234         //DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"uLength: %d, %d\n", uLength, cbFrameBodySize);
1235         //DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"cbReqCount:%d, %d, %d, %d\n", cbReqCount, cbHeaderLength, uPadding, cbIVlen);
1236         //DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"MIC:%lX, %lX\n", *pdwMIC_L, *pdwMIC_R);
1237     }
1238
1239     if (bSoftWEP == true) {
1240
1241         s_vSWencryption(pDevice, pTransmitKey, (pbyPayloadHead), (u16)(cbFrameBodySize + cbMIClen));
1242
1243     } else if (  ((pDevice->eEncryptionStatus == Ndis802_11Encryption1Enabled) && (bNeedEncryption == true))  ||
1244           ((pDevice->eEncryptionStatus == Ndis802_11Encryption2Enabled) && (bNeedEncryption == true))   ||
1245           ((pDevice->eEncryptionStatus == Ndis802_11Encryption3Enabled) && (bNeedEncryption == true))      ) {
1246         cbFrameSize -= cbICVlen;
1247     }
1248
1249         cbFrameSize -= cbFCSlen;
1250
1251     *pcbHeaderLen = cbHeaderLength;
1252     *pcbTotalLen = cbHeaderLength + cbFrameSize ;
1253
1254     //Set FragCtl in TxBufferHead
1255     pTxBufHead->wFragCtl |= (u16)byFragType;
1256
1257     return true;
1258
1259 }
1260
1261 /*+
1262  *
1263  * Description:
1264  *      Translate 802.3 to 802.11 header
1265  *
1266  * Parameters:
1267  *  In:
1268  *      pDevice         - Pointer to adapter
1269  *      dwTxBufferAddr  - Transmit Buffer
1270  *      pPacket         - Packet from upper layer
1271  *      cbPacketSize    - Transmit Data Length
1272  *  Out:
1273  *      pcbHeadSize         - Header size of MAC&Baseband control and 802.11 Header
1274  *      pcbAppendPayload    - size of append payload for 802.1H translation
1275  *
1276  * Return Value: none
1277  *
1278 -*/
1279
1280 static void s_vGenerateMACHeader(struct vnt_private *pDevice,
1281         u8 *pbyBufferAddr, u16 wDuration, struct ethhdr *psEthHeader,
1282         int bNeedEncrypt, u16 wFragType, u32 uFragIdx)
1283 {
1284         struct ieee80211_hdr *pMACHeader = (struct ieee80211_hdr *)pbyBufferAddr;
1285
1286         pMACHeader->frame_control = TYPE_802_11_DATA;
1287
1288     if (pDevice->op_mode == NL80211_IFTYPE_AP) {
1289         memcpy(&(pMACHeader->addr1[0]),
1290                &(psEthHeader->h_dest[0]),
1291                ETH_ALEN);
1292         memcpy(&(pMACHeader->addr2[0]), &(pDevice->abyBSSID[0]), ETH_ALEN);
1293         memcpy(&(pMACHeader->addr3[0]),
1294                &(psEthHeader->h_source[0]),
1295                ETH_ALEN);
1296         pMACHeader->frame_control |= FC_FROMDS;
1297     } else {
1298         if (pDevice->op_mode == NL80211_IFTYPE_ADHOC) {
1299                 memcpy(&(pMACHeader->addr1[0]),
1300                        &(psEthHeader->h_dest[0]),
1301                        ETH_ALEN);
1302                 memcpy(&(pMACHeader->addr2[0]),
1303                        &(psEthHeader->h_source[0]),
1304                        ETH_ALEN);
1305                 memcpy(&(pMACHeader->addr3[0]),
1306                        &(pDevice->abyBSSID[0]),
1307                        ETH_ALEN);
1308         } else {
1309                 memcpy(&(pMACHeader->addr3[0]),
1310                        &(psEthHeader->h_dest[0]),
1311                        ETH_ALEN);
1312                 memcpy(&(pMACHeader->addr2[0]),
1313                        &(psEthHeader->h_source[0]),
1314                        ETH_ALEN);
1315                 memcpy(&(pMACHeader->addr1[0]),
1316                        &(pDevice->abyBSSID[0]),
1317                        ETH_ALEN);
1318             pMACHeader->frame_control |= FC_TODS;
1319         }
1320     }
1321
1322     if (bNeedEncrypt)
1323         pMACHeader->frame_control |= cpu_to_le16((u16)WLAN_SET_FC_ISWEP(1));
1324
1325     pMACHeader->duration_id = cpu_to_le16(wDuration);
1326
1327     pMACHeader->seq_ctrl = cpu_to_le16(pDevice->wSeqCounter << 4);
1328
1329     //Set FragNumber in Sequence Control
1330     pMACHeader->seq_ctrl |= cpu_to_le16((u16)uFragIdx);
1331
1332     if ((wFragType == FRAGCTL_ENDFRAG) || (wFragType == FRAGCTL_NONFRAG)) {
1333         pDevice->wSeqCounter++;
1334         if (pDevice->wSeqCounter > 0x0fff)
1335             pDevice->wSeqCounter = 0;
1336     }
1337
1338     if ((wFragType == FRAGCTL_STAFRAG) || (wFragType == FRAGCTL_MIDFRAG)) { //StartFrag or MidFrag
1339         pMACHeader->frame_control |= FC_MOREFRAG;
1340     }
1341 }
1342
1343 /*+
1344  *
1345  * Description:
1346  *      Request instructs a MAC to transmit a 802.11 management packet through
1347  *      the adapter onto the medium.
1348  *
1349  * Parameters:
1350  *  In:
1351  *      hDeviceContext  - Pointer to the adapter
1352  *      pPacket         - A pointer to a descriptor for the packet to transmit
1353  *  Out:
1354  *      none
1355  *
1356  * Return Value: CMD_STATUS_PENDING if MAC Tx resource available; otherwise false
1357  *
1358 -*/
1359
1360 CMD_STATUS csMgmt_xmit(struct vnt_private *pDevice,
1361         struct vnt_tx_mgmt *pPacket)
1362 {
1363         struct vnt_manager *pMgmt = &pDevice->vnt_mgmt;
1364         struct vnt_tx_buffer *pTX_Buffer;
1365         struct vnt_usb_send_context *pContext;
1366         struct vnt_tx_fifo_head *pTxBufHead;
1367         struct ieee80211_hdr *pMACHeader;
1368         struct ethhdr sEthHeader;
1369         u8 byPktType, *pbyTxBufferAddr;
1370         struct vnt_mic_hdr *pMICHDR = NULL;
1371         u32 uDuration, cbReqCount, cbHeaderSize, cbFrameBodySize, cbFrameSize;
1372         int bNeedACK, bIsPSPOLL = false;
1373         u32 cbIVlen = 0, cbICVlen = 0, cbMIClen = 0, cbFCSlen = 4;
1374         u32 uPadding = 0;
1375         u16 wTxBufSize;
1376         u32 cbMacHdLen;
1377         u16 wCurrentRate = RATE_1M;
1378         unsigned long flags;
1379
1380         if (pDevice->byBBType == BB_TYPE_11A) {
1381                 wCurrentRate = RATE_6M;
1382                 byPktType = PK_TYPE_11A;
1383         } else {
1384                 wCurrentRate = RATE_1M;
1385                 byPktType = PK_TYPE_11B;
1386         }
1387
1388         if (pMgmt->eScanState != WMAC_NO_SCANNING)
1389                 vnt_rf_setpower(pDevice, wCurrentRate, pDevice->byCurrentCh);
1390         else
1391                 vnt_rf_setpower(pDevice, wCurrentRate, pMgmt->uCurrChannel);
1392
1393         pDevice->wCurrentRate = wCurrentRate;
1394
1395         spin_lock_irqsave(&pDevice->lock, flags);
1396
1397         pContext = s_vGetFreeContext(pDevice);
1398         if (!pContext) {
1399                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO
1400                         "ManagementSend TX...NO CONTEXT!\n");
1401                 spin_unlock_irqrestore(&pDevice->lock, flags);
1402                 return CMD_STATUS_RESOURCES;
1403         }
1404
1405         pTX_Buffer = (struct vnt_tx_buffer *)&pContext->data[0];
1406     cbFrameBodySize = pPacket->cbPayloadLen;
1407         pTxBufHead = &pTX_Buffer->fifo_head;
1408         pbyTxBufferAddr = (u8 *)&pTxBufHead->adwTxKey[0];
1409         wTxBufSize = sizeof(struct vnt_tx_fifo_head);
1410
1411
1412     //Set packet type
1413     if (byPktType == PK_TYPE_11A) {//0000 0000 0000 0000
1414         pTxBufHead->wFIFOCtl = 0;
1415     }
1416     else if (byPktType == PK_TYPE_11B) {//0000 0001 0000 0000
1417         pTxBufHead->wFIFOCtl |= FIFOCTL_11B;
1418     }
1419     else if (byPktType == PK_TYPE_11GB) {//0000 0010 0000 0000
1420         pTxBufHead->wFIFOCtl |= FIFOCTL_11GB;
1421     }
1422     else if (byPktType == PK_TYPE_11GA) {//0000 0011 0000 0000
1423         pTxBufHead->wFIFOCtl |= FIFOCTL_11GA;
1424     }
1425
1426     pTxBufHead->wFIFOCtl |= FIFOCTL_TMOEN;
1427     pTxBufHead->time_stamp = cpu_to_le16(DEFAULT_MGN_LIFETIME_RES_64us);
1428
1429     if (is_multicast_ether_addr(pPacket->p80211Header->sA3.abyAddr1)) {
1430         bNeedACK = false;
1431     }
1432     else {
1433         bNeedACK = true;
1434         pTxBufHead->wFIFOCtl |= FIFOCTL_NEEDACK;
1435     };
1436
1437     if ((pMgmt->eCurrMode == WMAC_MODE_ESS_AP) ||
1438         (pMgmt->eCurrMode == WMAC_MODE_IBSS_STA) ) {
1439
1440         pTxBufHead->wFIFOCtl |= FIFOCTL_LRETRY;
1441         //Set Preamble type always long
1442         //pDevice->byPreambleType = PREAMBLE_LONG;
1443         // probe-response don't retry
1444         //if ((pPacket->p80211Header->sA4.wFrameCtl & TYPE_SUBTYPE_MASK) == TYPE_MGMT_PROBE_RSP) {
1445         //     bNeedACK = false;
1446         //     pTxBufHead->wFIFOCtl  &= (~FIFOCTL_NEEDACK);
1447         //}
1448     }
1449
1450     pTxBufHead->wFIFOCtl |= (FIFOCTL_GENINT | FIFOCTL_ISDMA0);
1451
1452     if ((pPacket->p80211Header->sA4.wFrameCtl & TYPE_SUBTYPE_MASK) == TYPE_CTL_PSPOLL) {
1453         bIsPSPOLL = true;
1454         cbMacHdLen = WLAN_HDR_ADDR2_LEN;
1455     } else {
1456         cbMacHdLen = WLAN_HDR_ADDR3_LEN;
1457     }
1458
1459     //Set FRAGCTL_MACHDCNT
1460     pTxBufHead->wFragCtl |= cpu_to_le16((u16)(cbMacHdLen << 10));
1461
1462     // Notes:
1463     // Although spec says MMPDU can be fragmented; In most case,
1464     // no one will send a MMPDU under fragmentation. With RTS may occur.
1465
1466     if (WLAN_GET_FC_ISWEP(pPacket->p80211Header->sA4.wFrameCtl) != 0) {
1467         if (pDevice->eEncryptionStatus == Ndis802_11Encryption1Enabled) {
1468             cbIVlen = 4;
1469             cbICVlen = 4;
1470             pTxBufHead->wFragCtl |= FRAGCTL_LEGACY;
1471         }
1472         else if (pDevice->eEncryptionStatus == Ndis802_11Encryption2Enabled) {
1473             cbIVlen = 8;//IV+ExtIV
1474             cbMIClen = 8;
1475             cbICVlen = 4;
1476             pTxBufHead->wFragCtl |= FRAGCTL_TKIP;
1477             //We need to get seed here for filling TxKey entry.
1478             //TKIPvMixKey(pTransmitKey->abyKey, pDevice->abyCurrentNetAddr,
1479             //            pTransmitKey->wTSC15_0, pTransmitKey->dwTSC47_16, pDevice->abyPRNG);
1480         }
1481         else if (pDevice->eEncryptionStatus == Ndis802_11Encryption3Enabled) {
1482             cbIVlen = 8;//RSN Header
1483             cbICVlen = 8;//MIC
1484             pTxBufHead->wFragCtl |= FRAGCTL_AES;
1485         }
1486         //MAC Header should be padding 0 to DW alignment.
1487         uPadding = 4 - (cbMacHdLen%4);
1488         uPadding %= 4;
1489     }
1490
1491     cbFrameSize = cbMacHdLen + cbFrameBodySize + cbIVlen + cbMIClen + cbICVlen + cbFCSlen;
1492
1493     //Set FIFOCTL_GrpAckPolicy
1494     if (pDevice->bGrpAckPolicy == true) {//0000 0100 0000 0000
1495         pTxBufHead->wFIFOCtl |= FIFOCTL_GRPACK;
1496     }
1497     //the rest of pTxBufHead->wFragCtl:FragTyp will be set later in s_vFillFragParameter()
1498
1499     //Set RrvTime/RTS/CTS Buffer
1500     if (byPktType == PK_TYPE_11GB || byPktType == PK_TYPE_11GA) {//802.11g packet
1501         cbHeaderSize = wTxBufSize + sizeof(struct vnt_rrv_time_cts) +
1502                 sizeof(struct vnt_cts);
1503     }
1504     else { // 802.11a/b packet
1505         cbHeaderSize = wTxBufSize + sizeof(struct vnt_rrv_time_ab) +
1506                 sizeof(struct vnt_tx_datahead_ab);
1507     }
1508
1509     memcpy(&(sEthHeader.h_dest[0]),
1510            &(pPacket->p80211Header->sA3.abyAddr1[0]),
1511            ETH_ALEN);
1512     memcpy(&(sEthHeader.h_source[0]),
1513            &(pPacket->p80211Header->sA3.abyAddr2[0]),
1514            ETH_ALEN);
1515     //=========================
1516     //    No Fragmentation
1517     //=========================
1518     pTxBufHead->wFragCtl |= (u16)FRAGCTL_NONFRAG;
1519
1520         /* Fill FIFO,RrvTime,RTS,and CTS */
1521         uDuration = s_vGenerateTxParameter(pDevice, byPktType, wCurrentRate,
1522                 pTX_Buffer, &pMICHDR, 0,
1523                 cbFrameSize, bNeedACK, &sEthHeader, false);
1524
1525     pMACHeader = (struct ieee80211_hdr *) (pbyTxBufferAddr + cbHeaderSize);
1526
1527     cbReqCount = cbHeaderSize + cbMacHdLen + uPadding + cbIVlen + cbFrameBodySize;
1528
1529     if (WLAN_GET_FC_ISWEP(pPacket->p80211Header->sA4.wFrameCtl) != 0) {
1530         u8 *           pbyIVHead;
1531         u8 *           pbyPayloadHead;
1532         u8 *           pbyBSSID;
1533         PSKeyItem       pTransmitKey = NULL;
1534
1535         pbyIVHead = (u8 *)(pbyTxBufferAddr + cbHeaderSize + cbMacHdLen + uPadding);
1536         pbyPayloadHead = (u8 *)(pbyTxBufferAddr + cbHeaderSize + cbMacHdLen + uPadding + cbIVlen);
1537         do {
1538             if (pDevice->op_mode == NL80211_IFTYPE_STATION &&
1539                                         pDevice->bLinkPass == true) {
1540                 pbyBSSID = pDevice->abyBSSID;
1541                 // get pairwise key
1542                 if (KeybGetTransmitKey(&(pDevice->sKey), pbyBSSID, PAIRWISE_KEY, &pTransmitKey) == false) {
1543                     // get group key
1544                     if(KeybGetTransmitKey(&(pDevice->sKey), pbyBSSID, GROUP_KEY, &pTransmitKey) == true) {
1545                         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Get GTK.\n");
1546                         break;
1547                     }
1548                 } else {
1549                     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Get PTK.\n");
1550                     break;
1551                 }
1552             }
1553             // get group key
1554             pbyBSSID = pDevice->abyBroadcastAddr;
1555             if(KeybGetTransmitKey(&(pDevice->sKey), pbyBSSID, GROUP_KEY, &pTransmitKey) == false) {
1556                 pTransmitKey = NULL;
1557                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"KEY is NULL. OP Mode[%d]\n", pDevice->op_mode);
1558             } else {
1559                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Get GTK.\n");
1560             }
1561         } while(false);
1562         //Fill TXKEY
1563         s_vFillTxKey(pDevice, pTxBufHead, pbyIVHead, pTransmitKey,
1564                      (u8 *)pMACHeader, (u16)cbFrameBodySize, NULL);
1565
1566         memcpy(pMACHeader, pPacket->p80211Header, cbMacHdLen);
1567         memcpy(pbyPayloadHead, ((u8 *)(pPacket->p80211Header) + cbMacHdLen),
1568                  cbFrameBodySize);
1569     }
1570     else {
1571         // Copy the Packet into a tx Buffer
1572         memcpy(pMACHeader, pPacket->p80211Header, pPacket->cbMPDULen);
1573     }
1574
1575     pMACHeader->seq_ctrl = cpu_to_le16(pDevice->wSeqCounter << 4);
1576     pDevice->wSeqCounter++ ;
1577     if (pDevice->wSeqCounter > 0x0fff)
1578         pDevice->wSeqCounter = 0;
1579
1580     if (bIsPSPOLL) {
1581         // The MAC will automatically replace the Duration-field of MAC header by Duration-field
1582         // of FIFO control header.
1583         // This will cause AID-field of PS-POLL packet be incorrect (Because PS-POLL's AID field is
1584         // in the same place of other packet's Duration-field).
1585         // And it will cause Cisco-AP to issue Disassociation-packet
1586         if (byPktType == PK_TYPE_11GB || byPktType == PK_TYPE_11GA) {
1587                 struct vnt_tx_datahead_g *data_head = &pTX_Buffer->tx_head.
1588                                                 tx_cts.tx.head.cts_g.data_head;
1589                 data_head->duration_a =
1590                         cpu_to_le16(pPacket->p80211Header->sA2.wDurationID);
1591                 data_head->duration_b =
1592                         cpu_to_le16(pPacket->p80211Header->sA2.wDurationID);
1593         } else {
1594                 struct vnt_tx_datahead_ab *data_head = &pTX_Buffer->tx_head.
1595                                         tx_ab.tx.head.data_head_ab;
1596                 data_head->duration =
1597                         cpu_to_le16(pPacket->p80211Header->sA2.wDurationID);
1598         }
1599     }
1600
1601     pTX_Buffer->tx_byte_count = cpu_to_le16((u16)(cbReqCount));
1602     pTX_Buffer->byPKTNO = (u8) (((wCurrentRate<<4) &0x00F0) | ((pDevice->wSeqCounter - 1) & 0x000F));
1603     pTX_Buffer->byType = 0x00;
1604
1605         pContext->skb = NULL;
1606         pContext->type = CONTEXT_MGMT_PACKET;
1607         pContext->buf_len = (u16)cbReqCount + 4; /* USB header */
1608
1609     if (WLAN_GET_FC_TODS(pMACHeader->frame_control) == 0) {
1610         s_vSaveTxPktInfo(pDevice, (u8)(pTX_Buffer->byPKTNO & 0x0F),
1611                         &pMACHeader->addr1[0], (u16)cbFrameSize,
1612                         pTxBufHead->wFIFOCtl);
1613     }
1614     else {
1615         s_vSaveTxPktInfo(pDevice, (u8)(pTX_Buffer->byPKTNO & 0x0F),
1616                         &pMACHeader->addr3[0], (u16)cbFrameSize,
1617                         pTxBufHead->wFIFOCtl);
1618     }
1619
1620     PIPEnsSendBulkOut(pDevice,pContext);
1621
1622         spin_unlock_irqrestore(&pDevice->lock, flags);
1623
1624     return CMD_STATUS_PENDING;
1625 }
1626
1627 CMD_STATUS csBeacon_xmit(struct vnt_private *pDevice,
1628         struct vnt_tx_mgmt *pPacket)
1629 {
1630         struct vnt_beacon_buffer *pTX_Buffer;
1631         struct vnt_tx_short_buf_head *short_head;
1632         u32 cbFrameSize = pPacket->cbMPDULen + WLAN_FCS_LEN;
1633         u32 cbHeaderSize = 0;
1634         struct ieee80211_hdr *pMACHeader;
1635         u16 wCurrentRate;
1636         u32 cbFrameBodySize;
1637         u32 cbReqCount;
1638         struct vnt_usb_send_context *pContext;
1639         CMD_STATUS status;
1640
1641         pContext = s_vGetFreeContext(pDevice);
1642     if (NULL == pContext) {
1643         status = CMD_STATUS_RESOURCES;
1644         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ManagementSend TX...NO CONTEXT!\n");
1645         return status ;
1646     }
1647
1648         pTX_Buffer = (struct vnt_beacon_buffer *)&pContext->data[0];
1649         short_head = &pTX_Buffer->short_head;
1650
1651     cbFrameBodySize = pPacket->cbPayloadLen;
1652
1653         cbHeaderSize = sizeof(struct vnt_tx_short_buf_head);
1654
1655         if (pDevice->byBBType == BB_TYPE_11A) {
1656                 wCurrentRate = RATE_6M;
1657
1658                 /* Get SignalField,ServiceField,Length */
1659                 BBvCalculateParameter(pDevice, cbFrameSize, wCurrentRate,
1660                         PK_TYPE_11A, &short_head->ab);
1661
1662                 /* Get Duration and TimeStampOff */
1663                 short_head->duration = s_uGetDataDuration(pDevice,
1664                                                         PK_TYPE_11A, false);
1665                 short_head->time_stamp_off =
1666                                 vnt_time_stamp_off(pDevice, wCurrentRate);
1667         } else {
1668                 wCurrentRate = RATE_1M;
1669                 short_head->fifo_ctl |= FIFOCTL_11B;
1670
1671                 /* Get SignalField,ServiceField,Length */
1672                 BBvCalculateParameter(pDevice, cbFrameSize, wCurrentRate,
1673                                         PK_TYPE_11B, &short_head->ab);
1674
1675                 /* Get Duration and TimeStampOff */
1676                 short_head->duration = s_uGetDataDuration(pDevice,
1677                                                 PK_TYPE_11B, false);
1678                 short_head->time_stamp_off =
1679                         vnt_time_stamp_off(pDevice, wCurrentRate);
1680         }
1681
1682
1683         /* Generate Beacon Header */
1684         pMACHeader = &pTX_Buffer->hdr;
1685
1686         memcpy(pMACHeader, pPacket->p80211Header, pPacket->cbMPDULen);
1687
1688         pMACHeader->duration_id = 0;
1689         pMACHeader->seq_ctrl = cpu_to_le16(pDevice->wSeqCounter << 4);
1690         pDevice->wSeqCounter++;
1691         if (pDevice->wSeqCounter > 0x0fff)
1692                 pDevice->wSeqCounter = 0;
1693
1694     cbReqCount = cbHeaderSize + WLAN_HDR_ADDR3_LEN + cbFrameBodySize;
1695
1696     pTX_Buffer->tx_byte_count = cpu_to_le16((u16)cbReqCount);
1697     pTX_Buffer->byPKTNO = (u8) (((wCurrentRate<<4) &0x00F0) | ((pDevice->wSeqCounter - 1) & 0x000F));
1698     pTX_Buffer->byType = 0x01;
1699
1700         pContext->skb = NULL;
1701         pContext->type = CONTEXT_MGMT_PACKET;
1702         pContext->buf_len = (u16)cbReqCount + 4; /* USB header */
1703
1704     PIPEnsSendBulkOut(pDevice,pContext);
1705     return CMD_STATUS_PENDING;
1706
1707 }
1708
1709 //TYPE_AC0DMA data tx
1710 /*
1711  * Description:
1712  *      Tx packet via AC0DMA(DMA1)
1713  *
1714  * Parameters:
1715  *  In:
1716  *      pDevice         - Pointer to the adapter
1717  *      skb             - Pointer to tx skb packet
1718  *  Out:
1719  *      void
1720  *
1721  * Return Value: NULL
1722  */
1723
1724 int nsDMA_tx_packet(struct vnt_private *pDevice, struct sk_buff *skb)
1725 {
1726         struct net_device_stats *pStats = &pDevice->stats;
1727         struct vnt_manager *pMgmt = &pDevice->vnt_mgmt;
1728         struct vnt_tx_buffer *pTX_Buffer;
1729         u32 BytesToWrite = 0, uHeaderLen = 0;
1730         u32 uNodeIndex = 0;
1731         u8 byMask[8] = {1, 2, 4, 8, 0x10, 0x20, 0x40, 0x80};
1732         u16 wAID;
1733         u8 byPktType;
1734         int bNeedEncryption = false;
1735         PSKeyItem pTransmitKey = NULL;
1736         int ii;
1737         int bTKIP_UseGTK = false;
1738         int bNeedDeAuth = false;
1739         u8 *pbyBSSID;
1740         int bNodeExist = false;
1741         struct vnt_usb_send_context *pContext;
1742         bool fConvertedPacket;
1743         u32 status;
1744         u16 wKeepRate = pDevice->wCurrentRate;
1745         int bTxeapol_key = false;
1746
1747     if (pMgmt->eCurrMode == WMAC_MODE_ESS_AP) {
1748
1749         if (pDevice->uAssocCount == 0) {
1750             dev_kfree_skb_irq(skb);
1751             return 0;
1752         }
1753
1754         if (is_multicast_ether_addr((u8 *)(skb->data))) {
1755             uNodeIndex = 0;
1756             bNodeExist = true;
1757             if (pMgmt->sNodeDBTable[0].bPSEnable) {
1758
1759                 skb_queue_tail(&(pMgmt->sNodeDBTable[0].sTxPSQueue), skb);
1760                 pMgmt->sNodeDBTable[0].wEnQueueCnt++;
1761                 // set tx map
1762                 pMgmt->abyPSTxMap[0] |= byMask[0];
1763                 return 0;
1764             }
1765             // multicast/broadcast data rate
1766
1767             if (pDevice->byBBType != BB_TYPE_11A)
1768                 pDevice->wCurrentRate = RATE_2M;
1769             else
1770                 pDevice->wCurrentRate = RATE_24M;
1771             // long preamble type
1772             pDevice->byPreambleType = PREAMBLE_SHORT;
1773
1774         }else {
1775
1776             if (BSSbIsSTAInNodeDB(pDevice, (u8 *)(skb->data), &uNodeIndex)) {
1777
1778                 if (pMgmt->sNodeDBTable[uNodeIndex].bPSEnable) {
1779
1780                     skb_queue_tail(&pMgmt->sNodeDBTable[uNodeIndex].sTxPSQueue, skb);
1781
1782                     pMgmt->sNodeDBTable[uNodeIndex].wEnQueueCnt++;
1783                     // set tx map
1784                     wAID = pMgmt->sNodeDBTable[uNodeIndex].wAID;
1785                     pMgmt->abyPSTxMap[wAID >> 3] |=  byMask[wAID & 7];
1786                     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Set:pMgmt->abyPSTxMap[%d]= %d\n",
1787                              (wAID >> 3), pMgmt->abyPSTxMap[wAID >> 3]);
1788
1789                     return 0;
1790                 }
1791                 // AP rate decided from node
1792                 pDevice->wCurrentRate = pMgmt->sNodeDBTable[uNodeIndex].wTxDataRate;
1793                 // tx preamble decided from node
1794
1795                 if (pMgmt->sNodeDBTable[uNodeIndex].bShortPreamble) {
1796                     pDevice->byPreambleType = pDevice->byShortPreamble;
1797
1798                 }else {
1799                     pDevice->byPreambleType = PREAMBLE_LONG;
1800                 }
1801                 bNodeExist = true;
1802             }
1803         }
1804
1805         if (bNodeExist == false) {
1806             DBG_PRT(MSG_LEVEL_DEBUG, KERN_DEBUG"Unknown STA not found in node DB \n");
1807             dev_kfree_skb_irq(skb);
1808             return 0;
1809         }
1810     }
1811
1812         memcpy(&pDevice->sTxEthHeader, skb->data, ETH_HLEN);
1813
1814 //mike add:station mode check eapol-key challenge--->
1815 {
1816     u8  Protocol_Version;    //802.1x Authentication
1817     u8  Packet_Type;           //802.1x Authentication
1818     u8  Descriptor_type;
1819     u16 Key_info;
1820
1821     Protocol_Version = skb->data[ETH_HLEN];
1822     Packet_Type = skb->data[ETH_HLEN+1];
1823     Descriptor_type = skb->data[ETH_HLEN+1+1+2];
1824     Key_info = (skb->data[ETH_HLEN+1+1+2+1] << 8)|(skb->data[ETH_HLEN+1+1+2+2]);
1825         if (pDevice->sTxEthHeader.h_proto == cpu_to_be16(ETH_P_PAE)) {
1826                 /* 802.1x OR eapol-key challenge frame transfer */
1827                 if (((Protocol_Version == 1) || (Protocol_Version == 2)) &&
1828                         (Packet_Type == 3)) {
1829                         bTxeapol_key = true;
1830                        if(!(Key_info & BIT3) &&  //WPA or RSN group-key challenge
1831                            (Key_info & BIT8) && (Key_info & BIT9)) {    //send 2/2 key
1832                           if(Descriptor_type==254) {
1833                                pDevice->fWPA_Authened = true;
1834                              PRINT_K("WPA ");
1835                           }
1836                           else {
1837                                pDevice->fWPA_Authened = true;
1838                              PRINT_K("WPA2(re-keying) ");
1839                           }
1840                           PRINT_K("Authentication completed!!\n");
1841                         }
1842                     else if((Key_info & BIT3) && (Descriptor_type==2) &&  //RSN pairwise-key challenge
1843                                (Key_info & BIT8) && (Key_info & BIT9)) {
1844                           pDevice->fWPA_Authened = true;
1845                             PRINT_K("WPA2 Authentication completed!!\n");
1846                      }
1847              }
1848    }
1849 }
1850 //mike add:station mode check eapol-key challenge<---
1851
1852     if (pDevice->bEncryptionEnable == true) {
1853         bNeedEncryption = true;
1854         // get Transmit key
1855         do {
1856             if ((pMgmt->eCurrMode == WMAC_MODE_ESS_STA) &&
1857                 (pMgmt->eCurrState == WMAC_STATE_ASSOC)) {
1858                 pbyBSSID = pDevice->abyBSSID;
1859                 // get pairwise key
1860                 if (KeybGetTransmitKey(&(pDevice->sKey), pbyBSSID, PAIRWISE_KEY, &pTransmitKey) == false) {
1861                     // get group key
1862                     if(KeybGetTransmitKey(&(pDevice->sKey), pbyBSSID, GROUP_KEY, &pTransmitKey) == true) {
1863                         bTKIP_UseGTK = true;
1864                         DBG_PRT(MSG_LEVEL_DEBUG, KERN_DEBUG"Get GTK.\n");
1865                         break;
1866                     }
1867                 } else {
1868                     DBG_PRT(MSG_LEVEL_DEBUG, KERN_DEBUG"Get PTK.\n");
1869                     break;
1870                 }
1871             }else if (pMgmt->eCurrMode == WMAC_MODE_IBSS_STA) {
1872               /* TO_DS = 0 and FROM_DS = 0 --> 802.11 MAC Address1 */
1873                 pbyBSSID = pDevice->sTxEthHeader.h_dest;
1874                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_DEBUG"IBSS Serach Key: \n");
1875                 for (ii = 0; ii< 6; ii++)
1876                     DBG_PRT(MSG_LEVEL_DEBUG, KERN_DEBUG"%x \n", *(pbyBSSID+ii));
1877                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_DEBUG"\n");
1878
1879                 // get pairwise key
1880                 if(KeybGetTransmitKey(&(pDevice->sKey), pbyBSSID, PAIRWISE_KEY, &pTransmitKey) == true)
1881                     break;
1882             }
1883             // get group key
1884             pbyBSSID = pDevice->abyBroadcastAddr;
1885             if(KeybGetTransmitKey(&(pDevice->sKey), pbyBSSID, GROUP_KEY, &pTransmitKey) == false) {
1886                 pTransmitKey = NULL;
1887                 if (pMgmt->eCurrMode == WMAC_MODE_IBSS_STA) {
1888                     DBG_PRT(MSG_LEVEL_DEBUG, KERN_DEBUG"IBSS and KEY is NULL. [%d]\n", pMgmt->eCurrMode);
1889                 }
1890                 else
1891                     DBG_PRT(MSG_LEVEL_DEBUG, KERN_DEBUG"NOT IBSS and KEY is NULL. [%d]\n", pMgmt->eCurrMode);
1892             } else {
1893                 bTKIP_UseGTK = true;
1894                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_DEBUG"Get GTK.\n");
1895             }
1896         } while(false);
1897     }
1898
1899     byPktType = (u8)pDevice->byPacketType;
1900
1901     if (pDevice->bFixRate) {
1902         if (pDevice->byBBType == BB_TYPE_11B) {
1903             if (pDevice->uConnectionRate >= RATE_11M) {
1904                 pDevice->wCurrentRate = RATE_11M;
1905             } else {
1906                 pDevice->wCurrentRate = (u16)pDevice->uConnectionRate;
1907             }
1908         } else {
1909             if ((pDevice->byBBType == BB_TYPE_11A) &&
1910                 (pDevice->uConnectionRate <= RATE_6M)) {
1911                 pDevice->wCurrentRate = RATE_6M;
1912             } else {
1913                 if (pDevice->uConnectionRate >= RATE_54M)
1914                     pDevice->wCurrentRate = RATE_54M;
1915                 else
1916                     pDevice->wCurrentRate = (u16)pDevice->uConnectionRate;
1917             }
1918         }
1919     }
1920     else {
1921         if (pDevice->op_mode == NL80211_IFTYPE_ADHOC) {
1922             // Adhoc Tx rate decided from node DB
1923             if (is_multicast_ether_addr(pDevice->sTxEthHeader.h_dest)) {
1924                 // Multicast use highest data rate
1925                 pDevice->wCurrentRate = pMgmt->sNodeDBTable[0].wTxDataRate;
1926                 // preamble type
1927                 pDevice->byPreambleType = pDevice->byShortPreamble;
1928             }
1929             else {
1930                 if (BSSbIsSTAInNodeDB(pDevice, &(pDevice->sTxEthHeader.h_dest[0]), &uNodeIndex)) {
1931                     pDevice->wCurrentRate = pMgmt->sNodeDBTable[uNodeIndex].wTxDataRate;
1932                     if (pMgmt->sNodeDBTable[uNodeIndex].bShortPreamble) {
1933                         pDevice->byPreambleType = pDevice->byShortPreamble;
1934
1935                     }
1936                     else {
1937                         pDevice->byPreambleType = PREAMBLE_LONG;
1938                     }
1939                     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Found Node Index is [%d]  Tx Data Rate:[%d]\n",uNodeIndex, pDevice->wCurrentRate);
1940                 }
1941                 else {
1942                     if (pDevice->byBBType != BB_TYPE_11A)
1943                        pDevice->wCurrentRate = RATE_2M;
1944                     else
1945                        pDevice->wCurrentRate = RATE_24M; // refer to vMgrCreateOwnIBSS()'s
1946                                                          // abyCurrExtSuppRates[]
1947                     pDevice->byPreambleType = PREAMBLE_SHORT;
1948                     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Not Found Node use highest basic Rate.....\n");
1949                 }
1950             }
1951         }
1952         if (pDevice->op_mode == NL80211_IFTYPE_STATION) {
1953             // Infra STA rate decided from AP Node, index = 0
1954             pDevice->wCurrentRate = pMgmt->sNodeDBTable[0].wTxDataRate;
1955         }
1956     }
1957
1958         if (pDevice->sTxEthHeader.h_proto == cpu_to_be16(ETH_P_PAE)) {
1959                 if (pDevice->byBBType != BB_TYPE_11A) {
1960                         pDevice->wCurrentRate = RATE_1M;
1961                         pDevice->byTopCCKBasicRate = RATE_1M;
1962                         pDevice->byTopOFDMBasicRate = RATE_6M;
1963                 } else {
1964                         pDevice->wCurrentRate = RATE_6M;
1965                         pDevice->byTopCCKBasicRate = RATE_1M;
1966                         pDevice->byTopOFDMBasicRate = RATE_6M;
1967                 }
1968         }
1969
1970     DBG_PRT(MSG_LEVEL_DEBUG,
1971             KERN_INFO "dma_tx: pDevice->wCurrentRate = %d\n",
1972             pDevice->wCurrentRate);
1973
1974     if (wKeepRate != pDevice->wCurrentRate) {
1975         bScheduleCommand((void *) pDevice, WLAN_CMD_SETPOWER, NULL);
1976     }
1977
1978     if (pDevice->wCurrentRate <= RATE_11M) {
1979         byPktType = PK_TYPE_11B;
1980     }
1981
1982     if (bNeedEncryption == true) {
1983         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ntohs Pkt Type=%04x\n", ntohs(pDevice->sTxEthHeader.h_proto));
1984         if ((pDevice->sTxEthHeader.h_proto) == cpu_to_be16(ETH_P_PAE)) {
1985                 bNeedEncryption = false;
1986             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Pkt Type=%04x\n", (pDevice->sTxEthHeader.h_proto));
1987             if ((pMgmt->eCurrMode == WMAC_MODE_ESS_STA) && (pMgmt->eCurrState == WMAC_STATE_ASSOC)) {
1988                 if (pTransmitKey == NULL) {
1989                     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Don't Find TX KEY\n");
1990                 }
1991                 else {
1992                     if (bTKIP_UseGTK == true) {
1993                         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"error: KEY is GTK!!~~\n");
1994                     }
1995                     else {
1996                         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Find PTK [%X]\n",
1997                                 pTransmitKey->dwKeyIndex);
1998                         bNeedEncryption = true;
1999                     }
2000                 }
2001             }
2002         }
2003         else {
2004
2005             if (pTransmitKey == NULL) {
2006                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"return no tx key\n");
2007                 dev_kfree_skb_irq(skb);
2008                 pStats->tx_dropped++;
2009                 return STATUS_FAILURE;
2010             }
2011         }
2012     }
2013
2014         pContext = s_vGetFreeContext(pDevice);
2015         if (!pContext) {
2016                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_DEBUG" pContext == NULL\n");
2017                 dev_kfree_skb_irq(skb);
2018                 return STATUS_RESOURCES;
2019         }
2020
2021         pTX_Buffer = (struct vnt_tx_buffer *)&pContext->data[0];
2022
2023     fConvertedPacket = s_bPacketToWirelessUsb(pDevice, byPktType,
2024                         pTX_Buffer, bNeedEncryption,
2025                         skb->len, &pDevice->sTxEthHeader,
2026                         (u8 *)skb->data, pTransmitKey, uNodeIndex,
2027                         pDevice->wCurrentRate,
2028                         &uHeaderLen, &BytesToWrite
2029                        );
2030
2031         if (fConvertedPacket == false) {
2032                 pContext->in_use = false;
2033                 dev_kfree_skb_irq(skb);
2034                 return STATUS_FAILURE;
2035         }
2036
2037     if ( pDevice->bEnablePSMode == true ) {
2038         if ( !pDevice->bPSModeTxBurst ) {
2039                 bScheduleCommand((void *) pDevice,
2040                                  WLAN_CMD_MAC_DISPOWERSAVING,
2041                                  NULL);
2042             pDevice->bPSModeTxBurst = true;
2043         }
2044     }
2045
2046     pTX_Buffer->byPKTNO = (u8) (((pDevice->wCurrentRate<<4) &0x00F0) | ((pDevice->wSeqCounter - 1) & 0x000F));
2047     pTX_Buffer->tx_byte_count = cpu_to_le16((u16)BytesToWrite);
2048
2049         pContext->skb = skb;
2050         pContext->type = CONTEXT_DATA_PACKET;
2051         pContext->buf_len = (u16)BytesToWrite + 4 ; /* USB header */
2052
2053     s_vSaveTxPktInfo(pDevice, (u8)(pTX_Buffer->byPKTNO & 0x0F),
2054                         &pDevice->sTxEthHeader.h_dest[0],
2055                         (u16)(BytesToWrite-uHeaderLen),
2056                         pTX_Buffer->fifo_head.wFIFOCtl);
2057
2058     status = PIPEnsSendBulkOut(pDevice,pContext);
2059
2060     if (bNeedDeAuth == true) {
2061         u16 wReason = WLAN_MGMT_REASON_MIC_FAILURE;
2062
2063         bScheduleCommand((void *) pDevice, WLAN_CMD_DEAUTH, (u8 *) &wReason);
2064     }
2065
2066         if (status != STATUS_PENDING) {
2067                 pContext->in_use = false;
2068                 dev_kfree_skb_irq(skb);
2069                 return STATUS_FAILURE;
2070         }
2071
2072
2073         return 0;
2074 }