rxtx.c 101 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * File: rxtx.c
 *
 * Purpose: handle WMAC/802.3/802.11 rx & tx functions
 *
 * Author: Lyndon Chen
 *
 * Date: May 20, 2003
 *
 * Functions:
28
 *      s_vGenerateTxParameter - Generate tx dma required parameter.
29 30 31 32 33
 *      s_vGenerateMACHeader - Translate 802.3 to 802.11 header
 *      csBeacon_xmit - beacon tx function
 *      csMgmt_xmit - management tx function
 *      s_uGetDataDuration - get tx data required duration
 *      s_uFillDataHead- fulfill tx data duration header
34
 *      s_uGetRTSCTSDuration- get rtx/cts required duration
35 36 37
 *      s_uGetRTSCTSRsvTime- get rts/cts reserved time
 *      s_uGetTxRsvTime- get frame reserved time
 *      s_vFillCTSHead- fulfill CTS ctl header
38
 *      s_vFillFragParameter- Set fragment ctl parameter.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
 *      s_vFillRTSHead- fulfill RTS ctl header
 *      s_vFillTxKey- fulfill tx encrypt key
 *      s_vSWencryption- Software encrypt header
 *      vDMA0_tx_80211- tx 802.11 frame via dma0
 *      vGenerateFIFOHeader- Generate tx FIFO ctl header
 *
 * Revision History:
 *
 */

#include "device.h"
#include "rxtx.h"
#include "tether.h"
#include "card.h"
#include "bssdb.h"
#include "mac.h"
#include "michael.h"
#include "tkip.h"
#include "tcrc.h"
#include "wctl.h"
#include "hostap.h"
#include "rf.h"
#include "datarate.h"
#include "usbpipe.h"
#include "iocmd.h"
64

65
static int          msglevel                = MSG_LEVEL_INFO;
66

67
const u16 wTimeStampOff[2][MAX_RATE] = {
68 69 70 71
        {384, 288, 226, 209, 54, 43, 37, 31, 28, 25, 24, 23}, // Long Preamble
        {384, 192, 130, 113, 54, 43, 37, 31, 28, 25, 24, 23}, // Short Preamble
    };

72
const u16 wFB_Opt0[2][5] = {
73 74 75
        {RATE_12M, RATE_18M, RATE_24M, RATE_36M, RATE_48M}, // fallback_rate0
        {RATE_12M, RATE_12M, RATE_18M, RATE_24M, RATE_36M}, // fallback_rate1
    };
76
const u16 wFB_Opt1[2][5] = {
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
        {RATE_12M, RATE_18M, RATE_24M, RATE_24M, RATE_36M}, // fallback_rate0
        {RATE_6M , RATE_6M,  RATE_12M, RATE_12M, RATE_18M}, // fallback_rate1
    };

#define RTSDUR_BB       0
#define RTSDUR_BA       1
#define RTSDUR_AA       2
#define CTSDUR_BA       3
#define RTSDUR_BA_F0    4
#define RTSDUR_AA_F0    5
#define RTSDUR_BA_F1    6
#define RTSDUR_AA_F1    7
#define CTSDUR_BA_F0    8
#define CTSDUR_BA_F1    9
#define DATADUR_B       10
#define DATADUR_A       11
#define DATADUR_A_F0    12
#define DATADUR_A_F1    13

96 97 98 99 100 101 102 103
static void s_vSaveTxPktInfo(struct vnt_private *pDevice, u8 byPktNum,
	u8 *pbyDestAddr, u16 wPktLength, u16 wFIFOCtl);

static void *s_vGetFreeContext(struct vnt_private *pDevice);

static void s_vGenerateTxParameter(struct vnt_private *pDevice,
	u8 byPktType, u16 wCurrentRate,	void *pTxBufHead, void *pvRrvTime,
	void *pvRTS, void *pvCTS, u32 cbFrameSize, int bNeedACK, u32 uDMAIdx,
104
	struct ethhdr *psEthHeader);
105 106 107

static u32 s_uFillDataHead(struct vnt_private *pDevice,
	u8 byPktType, u16 wCurrentRate, void *pTxDataHead, u32 cbFrameLength,
108
	u32 uDMAIdx, int bNeedAck, u8 byFBOption);
109 110

static void s_vGenerateMACHeader(struct vnt_private *pDevice,
111
	u8 *pbyBufferAddr, u16 wDuration, struct ethhdr *psEthHeader,
112 113 114 115
	int bNeedEncrypt, u16 wFragType, u32 uDMAIdx, u32 uFragIdx);

static void s_vFillTxKey(struct vnt_private *pDevice, u8 *pbyBuf,
	u8 *pbyIVHead, PSKeyItem pTransmitKey, u8 *pbyHdrBuf, u16 wPayloadLen,
116
	struct vnt_mic_hdr *mic_hdr);
117 118 119 120 121 122 123

static void s_vSWencryption(struct vnt_private *pDevice,
	PSKeyItem pTransmitKey, u8 *pbyPayloadHead, u16 wPayloadSize);

static unsigned int s_uGetTxRsvTime(struct vnt_private *pDevice, u8 byPktType,
	u32 cbFrameLength, u16 wRate, int bNeedAck);

124
static u16 s_uGetRTSCTSRsvTime(struct vnt_private *pDevice, u8 byRTSRsvType,
125 126 127 128
	u8 byPktType, u32 cbFrameLength, u16 wCurrentRate);

static void s_vFillCTSHead(struct vnt_private *pDevice, u32 uDMAIdx,
	u8 byPktType, void *pvCTS, u32 cbFrameLength, int bNeedAck,
129
	u16 wCurrentRate, u8 byFBOption);
130 131

static void s_vFillRTSHead(struct vnt_private *pDevice, u8 byPktType,
132
	void *pvRTS, u32 cbFrameLength, int bNeedAck,
133
	struct ethhdr *psEthHeader, u16 wCurrentRate, u8 byFBOption);
134

135
static u16 s_uGetDataDuration(struct vnt_private *pDevice,
136
	u8 byPktType, int bNeedAck);
137

138
static u16 s_uGetRTSCTSDuration(struct vnt_private *pDevice,
139 140
	u8 byDurType, u32 cbFrameLength, u8 byPktType, u16 wRate,
	int bNeedAck, u8 byFBOption);
141

142
static void *s_vGetFreeContext(struct vnt_private *pDevice)
143
{
144 145 146
	PUSB_SEND_CONTEXT pContext = NULL;
	PUSB_SEND_CONTEXT pReturnContext = NULL;
	int ii;
147 148 149 150 151

    DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"GetFreeContext()\n");

    for (ii = 0; ii < pDevice->cbTD; ii++) {
        pContext = pDevice->apTD[ii];
152
        if (pContext->bBoolInUse == false) {
153
            pContext->bBoolInUse = true;
154
		memset(pContext->Data, 0, MAX_TOTAL_SIZE_WITH_ALL_HEADERS);
155 156 157 158 159 160 161
            pReturnContext = pContext;
            break;
        }
    }
    if ( ii == pDevice->cbTD ) {
        DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"No Free Tx Context\n");
    }
162
    return (void *) pReturnContext;
163 164
}

165 166
static void s_vSaveTxPktInfo(struct vnt_private *pDevice, u8 byPktNum,
	u8 *pbyDestAddr, u16 wPktLength, u16 wFIFOCtl)
167
{
168
	PSStatCounter pStatistic = &pDevice->scStatistic;
169

170
    if (is_broadcast_ether_addr(pbyDestAddr))
171
        pStatistic->abyTxPktInfo[byPktNum].byBroadMultiUni = TX_PKT_BROAD;
172
    else if (is_multicast_ether_addr(pbyDestAddr))
173 174 175 176 177 178
        pStatistic->abyTxPktInfo[byPktNum].byBroadMultiUni = TX_PKT_MULTI;
    else
        pStatistic->abyTxPktInfo[byPktNum].byBroadMultiUni = TX_PKT_UNI;

    pStatistic->abyTxPktInfo[byPktNum].wLength = wPktLength;
    pStatistic->abyTxPktInfo[byPktNum].wFIFOCtl = wFIFOCtl;
179 180 181
    memcpy(pStatistic->abyTxPktInfo[byPktNum].abyDestAddr,
	   pbyDestAddr,
	   ETH_ALEN);
182 183
}

184 185
static void s_vFillTxKey(struct vnt_private *pDevice, u8 *pbyBuf,
	u8 *pbyIVHead, PSKeyItem pTransmitKey, u8 *pbyHdrBuf,
186
	u16 wPayloadLen, struct vnt_mic_hdr *mic_hdr)
187
{
188 189
	u32 *pdwIV = (u32 *)pbyIVHead;
	u32 *pdwExtIV = (u32 *)((u8 *)pbyIVHead + 4);
190
	struct ieee80211_hdr *pMACHeader = (struct ieee80211_hdr *)pbyHdrBuf;
191
	u32 dwRevIVCounter;
192 193 194 195 196 197 198 199 200 201 202

    //Fill TXKEY
    if (pTransmitKey == NULL)
        return;

    dwRevIVCounter = cpu_to_le32(pDevice->dwIVCounter);
    *pdwIV = pDevice->dwIVCounter;
    pDevice->byKeyIndex = pTransmitKey->dwKeyIndex & 0xf;

    if (pTransmitKey->byCipherSuite == KEY_CTL_WEP) {
        if (pTransmitKey->uKeyLength == WLAN_WEP232_KEYLEN ){
203
            memcpy(pDevice->abyPRNG, (u8 *)&(dwRevIVCounter), 3);
204
            memcpy(pDevice->abyPRNG+3, pTransmitKey->abyKey, pTransmitKey->uKeyLength);
205
        } else {
206
            memcpy(pbyBuf, (u8 *)&(dwRevIVCounter), 3);
207
            memcpy(pbyBuf+3, pTransmitKey->abyKey, pTransmitKey->uKeyLength);
208
            if(pTransmitKey->uKeyLength == WLAN_WEP40_KEYLEN) {
209
                memcpy(pbyBuf+8, (u8 *)&(dwRevIVCounter), 3);
210
                memcpy(pbyBuf+11, pTransmitKey->abyKey, pTransmitKey->uKeyLength);
211
            }
212
            memcpy(pDevice->abyPRNG, pbyBuf, 16);
213 214 215
        }
        // Append IV after Mac Header
        *pdwIV &= WEP_IV_MASK;//00000000 11111111 11111111 11111111
216
	*pdwIV |= (u32)pDevice->byKeyIndex << 30;
217 218 219 220 221 222 223 224 225 226 227 228
        *pdwIV = cpu_to_le32(*pdwIV);
        pDevice->dwIVCounter++;
        if (pDevice->dwIVCounter > WEP_IV_MASK) {
            pDevice->dwIVCounter = 0;
        }
    } else if (pTransmitKey->byCipherSuite == KEY_CTL_TKIP) {
        pTransmitKey->wTSC15_0++;
        if (pTransmitKey->wTSC15_0 == 0) {
            pTransmitKey->dwTSC47_16++;
        }
        TKIPvMixKey(pTransmitKey->abyKey, pDevice->abyCurrentNetAddr,
                    pTransmitKey->wTSC15_0, pTransmitKey->dwTSC47_16, pDevice->abyPRNG);
229
        memcpy(pbyBuf, pDevice->abyPRNG, 16);
230
        // Make IV
231
        memcpy(pdwIV, pDevice->abyPRNG, 3);
232

233
        *(pbyIVHead+3) = (u8)(((pDevice->byKeyIndex << 6) & 0xc0) | 0x20); // 0x20 is ExtIV
234 235
        // Append IV&ExtIV after Mac Header
        *pdwExtIV = cpu_to_le32(pTransmitKey->dwTSC47_16);
236 237
	DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"vFillTxKey()---- pdwExtIV: %x\n",
		*pdwExtIV);
238 239 240 241 242 243

    } else if (pTransmitKey->byCipherSuite == KEY_CTL_CCMP) {
        pTransmitKey->wTSC15_0++;
        if (pTransmitKey->wTSC15_0 == 0) {
            pTransmitKey->dwTSC47_16++;
        }
244
        memcpy(pbyBuf, pTransmitKey->abyKey, 16);
245 246 247

        // Make IV
        *pdwIV = 0;
248
        *(pbyIVHead+3) = (u8)(((pDevice->byKeyIndex << 6) & 0xc0) | 0x20); // 0x20 is ExtIV
249
        *pdwIV |= cpu_to_le16((u16)(pTransmitKey->wTSC15_0));
250 251 252
        //Append IV&ExtIV after Mac Header
        *pdwExtIV = cpu_to_le32(pTransmitKey->dwTSC47_16);

253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
	if (!mic_hdr)
		return;

	/* MICHDR0 */
	mic_hdr->id = 0x59;
	mic_hdr->payload_len = cpu_to_be16(wPayloadLen);
	memcpy(mic_hdr->mic_addr2, pMACHeader->addr2, ETH_ALEN);

	mic_hdr->tsc_47_16 = cpu_to_be32(pTransmitKey->dwTSC47_16);
	mic_hdr->tsc_15_0 = cpu_to_be16(pTransmitKey->wTSC15_0);

	/* MICHDR1 */
	if (pDevice->bLongHeader)
		mic_hdr->hlen = cpu_to_be16(28);
	else
		mic_hdr->hlen = cpu_to_be16(22);

	memcpy(mic_hdr->addr1, pMACHeader->addr1, ETH_ALEN);
	memcpy(mic_hdr->addr2, pMACHeader->addr2, ETH_ALEN);

	/* MICHDR2 */
	memcpy(mic_hdr->addr3, pMACHeader->addr3, ETH_ALEN);
	mic_hdr->frame_control = cpu_to_le16(pMACHeader->frame_control
								& 0xc78f);
	mic_hdr->seq_ctrl = cpu_to_le16(pMACHeader->seq_ctrl & 0xf);

	if (pDevice->bLongHeader)
		memcpy(mic_hdr->addr4, pMACHeader->addr4, ETH_ALEN);
281 282 283
    }
}

284 285
static void s_vSWencryption(struct vnt_private *pDevice,
	PSKeyItem pTransmitKey, u8 *pbyPayloadHead, u16 wPayloadSize)
286
{
287 288 289
	u32 cbICVlen = 4;
	u32 dwICV = 0xffffffff;
	u32 *pdwICV;
290 291 292 293 294 295 296 297

    if (pTransmitKey == NULL)
        return;

    if (pTransmitKey->byCipherSuite == KEY_CTL_WEP) {
        //=======================================================================
        // Append ICV after payload
        dwICV = CRCdwGetCrc32Ex(pbyPayloadHead, wPayloadSize, dwICV);//ICV(Payload)
298
        pdwICV = (u32 *)(pbyPayloadHead + wPayloadSize);
299 300 301 302 303 304 305 306 307 308
        // finally, we must invert dwCRC to get the correct answer
        *pdwICV = cpu_to_le32(~dwICV);
        // RC4 encryption
        rc4_init(&pDevice->SBox, pDevice->abyPRNG, pTransmitKey->uKeyLength + 3);
        rc4_encrypt(&pDevice->SBox, pbyPayloadHead, pbyPayloadHead, wPayloadSize+cbICVlen);
        //=======================================================================
    } else if (pTransmitKey->byCipherSuite == KEY_CTL_TKIP) {
        //=======================================================================
        //Append ICV after payload
        dwICV = CRCdwGetCrc32Ex(pbyPayloadHead, wPayloadSize, dwICV);//ICV(Payload)
309
        pdwICV = (u32 *)(pbyPayloadHead + wPayloadSize);
310 311 312 313 314 315 316 317 318
        // finally, we must invert dwCRC to get the correct answer
        *pdwICV = cpu_to_le32(~dwICV);
        // RC4 encryption
        rc4_init(&pDevice->SBox, pDevice->abyPRNG, TKIP_KEY_LEN);
        rc4_encrypt(&pDevice->SBox, pbyPayloadHead, pbyPayloadHead, wPayloadSize+cbICVlen);
        //=======================================================================
    }
}

319 320 321 322 323 324
static u16 vnt_time_stamp_off(struct vnt_private *priv, u16 rate)
{
	return cpu_to_le16(wTimeStampOff[priv->byPreambleType % 2]
							[rate % MAX_RATE]);
}

325 326 327 328 329
/*byPktType : PK_TYPE_11A     0
             PK_TYPE_11B     1
             PK_TYPE_11GB    2
             PK_TYPE_11GA    3
*/
330 331
static u32 s_uGetTxRsvTime(struct vnt_private *pDevice, u8 byPktType,
	u32 cbFrameLength, u16 wRate, int bNeedAck)
332
{
333
	u32 uDataTime, uAckTime;
334 335 336

    uDataTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, cbFrameLength, wRate);
    if (byPktType == PK_TYPE_11B) {//llb,CCK mode
337
        uAckTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, (u16)pDevice->byTopCCKBasicRate);
338
    } else {//11g 2.4G OFDM mode & 11a 5G OFDM mode
339
        uAckTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, (u16)pDevice->byTopOFDMBasicRate);
340 341 342 343 344 345 346 347 348 349
    }

    if (bNeedAck) {
        return (uDataTime + pDevice->uSIFS + uAckTime);
    }
    else {
        return uDataTime;
    }
}

350 351 352 353 354 355 356
static u16 vnt_rxtx_rsvtime_le16(struct vnt_private *priv, u8 pkt_type,
	u32 frame_length, u16 rate, int need_ack)
{
	return cpu_to_le16((u16)s_uGetTxRsvTime(priv, pkt_type,
		frame_length, rate, need_ack));
}

357
//byFreqType: 0=>5GHZ 1=>2.4GHZ
358
static u16 s_uGetRTSCTSRsvTime(struct vnt_private *pDevice,
359
	u8 byRTSRsvType, u8 byPktType, u32 cbFrameLength, u16 wCurrentRate)
360
{
361
	u32 uRrvTime, uRTSTime, uCTSTime, uAckTime, uDataTime;
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387

    uRrvTime = uRTSTime = uCTSTime = uAckTime = uDataTime = 0;

    uDataTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, cbFrameLength, wCurrentRate);
    if (byRTSRsvType == 0) { //RTSTxRrvTime_bb
        uRTSTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 20, pDevice->byTopCCKBasicRate);
        uCTSTime = uAckTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopCCKBasicRate);
    }
    else if (byRTSRsvType == 1){ //RTSTxRrvTime_ba, only in 2.4GHZ
        uRTSTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 20, pDevice->byTopCCKBasicRate);
        uCTSTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopCCKBasicRate);
        uAckTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopOFDMBasicRate);
    }
    else if (byRTSRsvType == 2) { //RTSTxRrvTime_aa
        uRTSTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 20, pDevice->byTopOFDMBasicRate);
        uCTSTime = uAckTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopOFDMBasicRate);
    }
    else if (byRTSRsvType == 3) { //CTSTxRrvTime_ba, only in 2.4GHZ
        uCTSTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopCCKBasicRate);
        uAckTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopOFDMBasicRate);
        uRrvTime = uCTSTime + uAckTime + uDataTime + 2*pDevice->uSIFS;
        return uRrvTime;
    }

    //RTSRrvTime
    uRrvTime = uRTSTime + uCTSTime + uAckTime + uDataTime + 3*pDevice->uSIFS;
388
	return cpu_to_le16((u16)uRrvTime);
389 390 391
}

//byFreqType 0: 5GHz, 1:2.4Ghz
392 393
static u16 s_uGetDataDuration(struct vnt_private *pDevice,
					u8 byPktType, int bNeedAck)
394
{
395
	u32 uAckTime = 0;
396

397
	if (bNeedAck) {
398
		if (byPktType == PK_TYPE_11B)
399 400 401 402 403
			uAckTime = BBuGetFrameTime(pDevice->byPreambleType,
				byPktType, 14, pDevice->byTopCCKBasicRate);
		else
			uAckTime = BBuGetFrameTime(pDevice->byPreambleType,
				byPktType, 14, pDevice->byTopOFDMBasicRate);
404
		return cpu_to_le16((u16)(pDevice->uSIFS + uAckTime));
405
	}
406 407 408 409 410

	return 0;
}

//byFreqType: 0=>5GHZ 1=>2.4GHZ
411
static u16 s_uGetRTSCTSDuration(struct vnt_private *pDevice, u8 byDurType,
412 413
	u32 cbFrameLength, u8 byPktType, u16 wRate, int bNeedAck,
	u8 byFBOption)
414
{
415
	u32 uCTSTime = 0, uDurTime = 0;
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493

    switch (byDurType) {

    case RTSDUR_BB:    //RTSDuration_bb
        uCTSTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopCCKBasicRate);
        uDurTime = uCTSTime + 2*pDevice->uSIFS + s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wRate, bNeedAck);
        break;

    case RTSDUR_BA:    //RTSDuration_ba
        uCTSTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopCCKBasicRate);
        uDurTime = uCTSTime + 2*pDevice->uSIFS + s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wRate, bNeedAck);
        break;

    case RTSDUR_AA:    //RTSDuration_aa
        uCTSTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopOFDMBasicRate);
        uDurTime = uCTSTime + 2*pDevice->uSIFS + s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wRate, bNeedAck);
        break;

    case CTSDUR_BA:    //CTSDuration_ba
        uDurTime = pDevice->uSIFS + s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wRate, bNeedAck);
        break;

    case RTSDUR_BA_F0: //RTSDuration_ba_f0
        uCTSTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopCCKBasicRate);
        if ((byFBOption == AUTO_FB_0) && (wRate >= RATE_18M) && (wRate <=RATE_54M)) {
            uDurTime = uCTSTime + 2*pDevice->uSIFS + s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wFB_Opt0[FB_RATE0][wRate-RATE_18M], bNeedAck);
        } else if ((byFBOption == AUTO_FB_1) && (wRate >= RATE_18M) && (wRate <=RATE_54M)) {
            uDurTime = uCTSTime + 2*pDevice->uSIFS + s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wFB_Opt1[FB_RATE0][wRate-RATE_18M], bNeedAck);
        }
        break;

    case RTSDUR_AA_F0: //RTSDuration_aa_f0
        uCTSTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopOFDMBasicRate);
        if ((byFBOption == AUTO_FB_0) && (wRate >= RATE_18M) && (wRate <=RATE_54M)) {
            uDurTime = uCTSTime + 2*pDevice->uSIFS + s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wFB_Opt0[FB_RATE0][wRate-RATE_18M], bNeedAck);
        } else if ((byFBOption == AUTO_FB_1) && (wRate >= RATE_18M) && (wRate <=RATE_54M)) {
            uDurTime = uCTSTime + 2*pDevice->uSIFS + s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wFB_Opt1[FB_RATE0][wRate-RATE_18M], bNeedAck);
        }
        break;

    case RTSDUR_BA_F1: //RTSDuration_ba_f1
        uCTSTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopCCKBasicRate);
        if ((byFBOption == AUTO_FB_0) && (wRate >= RATE_18M) && (wRate <=RATE_54M)) {
            uDurTime = uCTSTime + 2*pDevice->uSIFS + s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wFB_Opt0[FB_RATE1][wRate-RATE_18M], bNeedAck);
        } else if ((byFBOption == AUTO_FB_1) && (wRate >= RATE_18M) && (wRate <=RATE_54M)) {
            uDurTime = uCTSTime + 2*pDevice->uSIFS + s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wFB_Opt1[FB_RATE1][wRate-RATE_18M], bNeedAck);
        }
        break;

    case RTSDUR_AA_F1: //RTSDuration_aa_f1
        uCTSTime = BBuGetFrameTime(pDevice->byPreambleType, byPktType, 14, pDevice->byTopOFDMBasicRate);
        if ((byFBOption == AUTO_FB_0) && (wRate >= RATE_18M) && (wRate <=RATE_54M)) {
            uDurTime = uCTSTime + 2*pDevice->uSIFS + s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wFB_Opt0[FB_RATE1][wRate-RATE_18M], bNeedAck);
        } else if ((byFBOption == AUTO_FB_1) && (wRate >= RATE_18M) && (wRate <=RATE_54M)) {
            uDurTime = uCTSTime + 2*pDevice->uSIFS + s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wFB_Opt1[FB_RATE1][wRate-RATE_18M], bNeedAck);
        }
        break;

    case CTSDUR_BA_F0: //CTSDuration_ba_f0
        if ((byFBOption == AUTO_FB_0) && (wRate >= RATE_18M) && (wRate <=RATE_54M)) {
            uDurTime = pDevice->uSIFS + s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wFB_Opt0[FB_RATE0][wRate-RATE_18M], bNeedAck);
        } else if ((byFBOption == AUTO_FB_1) && (wRate >= RATE_18M) && (wRate <=RATE_54M)) {
            uDurTime = pDevice->uSIFS + s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wFB_Opt1[FB_RATE0][wRate-RATE_18M], bNeedAck);
        }
        break;

    case CTSDUR_BA_F1: //CTSDuration_ba_f1
        if ((byFBOption == AUTO_FB_0) && (wRate >= RATE_18M) && (wRate <=RATE_54M)) {
            uDurTime = pDevice->uSIFS + s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wFB_Opt0[FB_RATE1][wRate-RATE_18M], bNeedAck);
        } else if ((byFBOption == AUTO_FB_1) && (wRate >= RATE_18M) && (wRate <=RATE_54M)) {
            uDurTime = pDevice->uSIFS + s_uGetTxRsvTime(pDevice, byPktType, cbFrameLength, wFB_Opt1[FB_RATE1][wRate-RATE_18M], bNeedAck);
        }
        break;

    default:
        break;
    }

494
	return cpu_to_le16((u16)uDurTime);
495 496
}

497 498
static u32 s_uFillDataHead(struct vnt_private *pDevice,
	u8 byPktType, u16 wCurrentRate, void *pTxDataHead, u32 cbFrameLength,
499
	u32 uDMAIdx, int bNeedAck, u8 byFBOption)
500 501 502 503 504 505 506 507
{

    if (pTxDataHead == NULL) {
        return 0;
    }

    if (byPktType == PK_TYPE_11GB || byPktType == PK_TYPE_11GA) {
            if (byFBOption == AUTO_FB_NONE) {
508 509
		struct vnt_tx_datahead_g *pBuf =
				(struct vnt_tx_datahead_g *)pTxDataHead;
510
                //Get SignalField,ServiceField,Length
511 512 513 514
		BBvCalculateParameter(pDevice, cbFrameLength, wCurrentRate,
			byPktType, &pBuf->a);
		BBvCalculateParameter(pDevice, cbFrameLength,
			pDevice->byTopCCKBasicRate, PK_TYPE_11B, &pBuf->b);
515
                //Get Duration and TimeStamp
516
		pBuf->wDuration_a = s_uGetDataDuration(pDevice,
517
							byPktType, bNeedAck);
518
		pBuf->wDuration_b = s_uGetDataDuration(pDevice,
519
							PK_TYPE_11B, bNeedAck);
520

521 522 523 524
		pBuf->wTimeStampOff_a =	vnt_time_stamp_off(pDevice,
								wCurrentRate);
		pBuf->wTimeStampOff_b = vnt_time_stamp_off(pDevice,
						pDevice->byTopCCKBasicRate);
525 526 527
                return (pBuf->wDuration_a);
             } else {
                // Auto Fallback
528 529
		struct vnt_tx_datahead_g_fb *pBuf =
			(struct vnt_tx_datahead_g_fb *)pTxDataHead;
530
                //Get SignalField,ServiceField,Length
531 532 533 534
		BBvCalculateParameter(pDevice, cbFrameLength, wCurrentRate,
			byPktType, &pBuf->a);
		BBvCalculateParameter(pDevice, cbFrameLength,
			pDevice->byTopCCKBasicRate, PK_TYPE_11B, &pBuf->b);
535
                //Get Duration and TimeStamp
536
		pBuf->wDuration_a = s_uGetDataDuration(pDevice,
537
							byPktType, bNeedAck);
538
		pBuf->wDuration_b = s_uGetDataDuration(pDevice,
539
							PK_TYPE_11B, bNeedAck);
540
		pBuf->wDuration_a_f0 = s_uGetDataDuration(pDevice,
541
							byPktType, bNeedAck);
542
		pBuf->wDuration_a_f1 = s_uGetDataDuration(pDevice,
543
							byPktType, bNeedAck);
544 545 546 547
		pBuf->wTimeStampOff_a = vnt_time_stamp_off(pDevice,
								wCurrentRate);
		pBuf->wTimeStampOff_b = vnt_time_stamp_off(pDevice,
						pDevice->byTopCCKBasicRate);
548 549 550 551
                return (pBuf->wDuration_a);
            } //if (byFBOption == AUTO_FB_NONE)
    }
    else if (byPktType == PK_TYPE_11A) {
552
	if (byFBOption != AUTO_FB_NONE) {
553 554
		struct vnt_tx_datahead_a_fb *pBuf =
			(struct vnt_tx_datahead_a_fb *)pTxDataHead;
555
            //Get SignalField,ServiceField,Length
556 557
		BBvCalculateParameter(pDevice, cbFrameLength, wCurrentRate,
			byPktType, &pBuf->a);
558
            //Get Duration and TimeStampOff
559
		pBuf->wDuration = s_uGetDataDuration(pDevice,
560
					byPktType, bNeedAck);
561
		pBuf->wDuration_f0 = s_uGetDataDuration(pDevice,
562
					byPktType, bNeedAck);
563
		pBuf->wDuration_f1 = s_uGetDataDuration(pDevice,
564
							byPktType, bNeedAck);
565 566
		pBuf->wTimeStampOff = vnt_time_stamp_off(pDevice,
								wCurrentRate);
567 568
            return (pBuf->wDuration);
        } else {
569 570
		struct vnt_tx_datahead_ab *pBuf =
			(struct vnt_tx_datahead_ab *)pTxDataHead;
571
            //Get SignalField,ServiceField,Length
572 573
		BBvCalculateParameter(pDevice, cbFrameLength, wCurrentRate,
			byPktType, &pBuf->ab);
574
            //Get Duration and TimeStampOff
575
		pBuf->wDuration = s_uGetDataDuration(pDevice,
576
				byPktType, bNeedAck);
577 578
		pBuf->wTimeStampOff = vnt_time_stamp_off(pDevice,
								wCurrentRate);
579 580 581 582
            return (pBuf->wDuration);
        }
    }
    else if (byPktType == PK_TYPE_11B) {
583 584
		struct vnt_tx_datahead_ab *pBuf =
			(struct vnt_tx_datahead_ab *)pTxDataHead;
585
            //Get SignalField,ServiceField,Length
586 587
		BBvCalculateParameter(pDevice, cbFrameLength, wCurrentRate,
			byPktType, &pBuf->ab);
588
            //Get Duration and TimeStampOff
589
		pBuf->wDuration = s_uGetDataDuration(pDevice,
590
				byPktType, bNeedAck);
591 592
		pBuf->wTimeStampOff = vnt_time_stamp_off(pDevice,
								wCurrentRate);
593 594 595 596 597
            return (pBuf->wDuration);
    }
    return 0;
}

598
static void s_vFillRTSHead(struct vnt_private *pDevice, u8 byPktType,
599
	void *pvRTS, u32 cbFrameLength, int bNeedAck,
600
	struct ethhdr *psEthHeader, u16 wCurrentRate, u8 byFBOption)
601
{
602
	u32 uRTSFrameLen = 20;
603 604 605 606

    if (pvRTS == NULL)
    	return;

607
    // Note: So far RTSHead doesn't appear in ATIM & Beacom DMA, so we don't need to take them into account.
608 609 610
    //       Otherwise, we need to modified codes for them.
    if (byPktType == PK_TYPE_11GB || byPktType == PK_TYPE_11GA) {
        if (byFBOption == AUTO_FB_NONE) {
611
		struct vnt_rts_g *pBuf = (struct vnt_rts_g *)pvRTS;
612
            //Get SignalField,ServiceField,Length
613 614 615 616
		BBvCalculateParameter(pDevice, uRTSFrameLen,
			pDevice->byTopCCKBasicRate, PK_TYPE_11B, &pBuf->b);
		BBvCalculateParameter(pDevice, uRTSFrameLen,
			pDevice->byTopOFDMBasicRate, byPktType, &pBuf->a);
617
            //Get Duration
618 619 620 621 622 623 624 625 626
		pBuf->wDuration_bb = s_uGetRTSCTSDuration(pDevice, RTSDUR_BB,
			cbFrameLength, PK_TYPE_11B,
			pDevice->byTopCCKBasicRate, bNeedAck, byFBOption);
		pBuf->wDuration_aa = s_uGetRTSCTSDuration(pDevice, RTSDUR_AA,
			cbFrameLength, byPktType,
			wCurrentRate, bNeedAck, byFBOption);
		pBuf->wDuration_ba = s_uGetRTSCTSDuration(pDevice, RTSDUR_BA,
			cbFrameLength, byPktType,
			wCurrentRate, bNeedAck, byFBOption);
627 628 629 630 631 632 633 634 635 636 637 638 639 640
		pBuf->data.duration = pBuf->wDuration_aa;
		/*Get RTS Frame body */
		pBuf->data.frame_control = TYPE_CTL_RTS;

		if (pDevice->eOPMode == OP_MODE_ADHOC ||
				pDevice->eOPMode == OP_MODE_AP)
			memcpy(pBuf->data.ra, psEthHeader->h_dest, ETH_ALEN);
		else
			memcpy(pBuf->data.ra, pDevice->abyBSSID, ETH_ALEN);

		if (pDevice->eOPMode == OP_MODE_AP)
			memcpy(pBuf->data.ta, pDevice->abyBSSID, ETH_ALEN);
		else
			memcpy(pBuf->data.ta, psEthHeader->h_source, ETH_ALEN);
641 642
        }
        else {
643
		struct vnt_rts_g_fb *pBuf = (struct vnt_rts_g_fb *)pvRTS;
644
            //Get SignalField,ServiceField,Length
645 646 647 648
		BBvCalculateParameter(pDevice, uRTSFrameLen,
			pDevice->byTopCCKBasicRate, PK_TYPE_11B, &pBuf->b);
		BBvCalculateParameter(pDevice, uRTSFrameLen,
			pDevice->byTopOFDMBasicRate, byPktType, &pBuf->a);
649
            //Get Duration
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
		pBuf->wDuration_bb = s_uGetRTSCTSDuration(pDevice, RTSDUR_BB,
			cbFrameLength, PK_TYPE_11B,
			pDevice->byTopCCKBasicRate, bNeedAck, byFBOption);
		pBuf->wDuration_aa = s_uGetRTSCTSDuration(pDevice, RTSDUR_AA,
			cbFrameLength, byPktType,
			wCurrentRate, bNeedAck, byFBOption);
		pBuf->wDuration_ba = s_uGetRTSCTSDuration(pDevice, RTSDUR_BA,
			cbFrameLength, byPktType,
			wCurrentRate, bNeedAck, byFBOption);
		pBuf->wRTSDuration_ba_f0 = s_uGetRTSCTSDuration(pDevice,
			RTSDUR_BA_F0, cbFrameLength, byPktType, wCurrentRate,
			bNeedAck, byFBOption);
		pBuf->wRTSDuration_aa_f0 = s_uGetRTSCTSDuration(pDevice,
			RTSDUR_AA_F0, cbFrameLength, byPktType,
			wCurrentRate, bNeedAck, byFBOption);
		pBuf->wRTSDuration_ba_f1 = s_uGetRTSCTSDuration(pDevice,
			RTSDUR_BA_F1, cbFrameLength, byPktType, wCurrentRate,
			bNeedAck, byFBOption);
		pBuf->wRTSDuration_aa_f1 = s_uGetRTSCTSDuration(pDevice,
			RTSDUR_AA_F1, cbFrameLength, byPktType, wCurrentRate,
			bNeedAck, byFBOption);
671 672 673 674 675 676 677 678 679 680 681 682 683 684
		pBuf->data.duration = pBuf->wDuration_aa;
		/*Get RTS Frame body*/
		pBuf->data.frame_control = TYPE_CTL_RTS;

		if (pDevice->eOPMode == OP_MODE_ADHOC ||
				pDevice->eOPMode == OP_MODE_AP)
			memcpy(pBuf->data.ra, psEthHeader->h_dest, ETH_ALEN);
		else
			memcpy(pBuf->data.ra, pDevice->abyBSSID, ETH_ALEN);

		if (pDevice->eOPMode == OP_MODE_AP)
			memcpy(pBuf->data.ta, pDevice->abyBSSID, ETH_ALEN);
		else
			memcpy(pBuf->data.ta, psEthHeader->h_source, ETH_ALEN);
685 686 687 688
        } // if (byFBOption == AUTO_FB_NONE)
    }
    else if (byPktType == PK_TYPE_11A) {
        if (byFBOption == AUTO_FB_NONE) {
689
		struct vnt_rts_ab *pBuf = (struct vnt_rts_ab *)pvRTS;
690
            //Get SignalField,ServiceField,Length
691 692
		BBvCalculateParameter(pDevice, uRTSFrameLen,
			pDevice->byTopOFDMBasicRate, byPktType, &pBuf->ab);
693
            //Get Duration
694 695 696
		pBuf->wDuration = s_uGetRTSCTSDuration(pDevice, RTSDUR_AA,
			cbFrameLength, byPktType, wCurrentRate,
				bNeedAck, byFBOption);
697 698 699 700 701 702 703 704 705 706 707 708 709 710
		pBuf->data.duration = pBuf->wDuration;
		/* Get RTS Frame body */
		pBuf->data.frame_control = TYPE_CTL_RTS;

		if (pDevice->eOPMode == OP_MODE_ADHOC ||
				pDevice->eOPMode == OP_MODE_AP)
			memcpy(pBuf->data.ra, psEthHeader->h_dest, ETH_ALEN);
		else
			memcpy(pBuf->data.ra, pDevice->abyBSSID, ETH_ALEN);

		if (pDevice->eOPMode == OP_MODE_AP)
			memcpy(pBuf->data.ta, pDevice->abyBSSID, ETH_ALEN);
		else
			memcpy(pBuf->data.ta, psEthHeader->h_source, ETH_ALEN);
711 712
        }
        else {
713
		struct vnt_rts_a_fb *pBuf = (struct vnt_rts_a_fb *)pvRTS;
714
            //Get SignalField,ServiceField,Length
715 716
		BBvCalculateParameter(pDevice, uRTSFrameLen,
			pDevice->byTopOFDMBasicRate, byPktType, &pBuf->a);
717
            //Get Duration
718 719 720 721 722 723 724 725 726
		pBuf->wDuration = s_uGetRTSCTSDuration(pDevice, RTSDUR_AA,
			cbFrameLength, byPktType, wCurrentRate,
			bNeedAck, byFBOption);
		pBuf->wRTSDuration_f0 = s_uGetRTSCTSDuration(pDevice,
			RTSDUR_AA_F0, cbFrameLength, byPktType,
			wCurrentRate, bNeedAck, byFBOption);
		pBuf->wRTSDuration_f1 = s_uGetRTSCTSDuration(pDevice,
			RTSDUR_AA_F1, cbFrameLength, byPktType,
			wCurrentRate, bNeedAck, byFBOption);
727 728 729 730 731 732 733 734 735 736 737 738 739 740
		pBuf->data.duration = pBuf->wDuration;
		/* Get RTS Frame body */
		pBuf->data.frame_control = TYPE_CTL_RTS;

		if (pDevice->eOPMode == OP_MODE_ADHOC ||
				pDevice->eOPMode == OP_MODE_AP)
			memcpy(pBuf->data.ra, psEthHeader->h_dest, ETH_ALEN);
		else
			memcpy(pBuf->data.ra, pDevice->abyBSSID, ETH_ALEN);

		if (pDevice->eOPMode == OP_MODE_AP)
			memcpy(pBuf->data.ta, pDevice->abyBSSID, ETH_ALEN);
		else
			memcpy(pBuf->data.ta, psEthHeader->h_source, ETH_ALEN);
741 742 743
        }
    }
    else if (byPktType == PK_TYPE_11B) {
744
	struct vnt_rts_ab *pBuf = (struct vnt_rts_ab *)pvRTS;
745
        //Get SignalField,ServiceField,Length
746 747
	BBvCalculateParameter(pDevice, uRTSFrameLen, pDevice->byTopCCKBasicRate,
		PK_TYPE_11B, &pBuf->ab);
748
        //Get Duration
749 750 751
	pBuf->wDuration = s_uGetRTSCTSDuration(pDevice, RTSDUR_BB,
		cbFrameLength, byPktType, wCurrentRate,
		bNeedAck, byFBOption);
752

753 754 755 756 757 758 759 760 761 762 763 764 765 766
	pBuf->data.duration = pBuf->wDuration;
	/* Get RTS Frame body */
	pBuf->data.frame_control = TYPE_CTL_RTS;

	if (pDevice->eOPMode == OP_MODE_ADHOC ||
			pDevice->eOPMode == OP_MODE_AP)
		memcpy(pBuf->data.ra, psEthHeader->h_dest, ETH_ALEN);
	else
		memcpy(pBuf->data.ra, pDevice->abyBSSID, ETH_ALEN);

	if (pDevice->eOPMode == OP_MODE_AP)
		memcpy(pBuf->data.ta, pDevice->abyBSSID, ETH_ALEN);
	else
		memcpy(pBuf->data.ta, psEthHeader->h_source, ETH_ALEN);
767 768 769
    }
}

770 771
static void s_vFillCTSHead(struct vnt_private *pDevice, u32 uDMAIdx,
	u8 byPktType, void *pvCTS, u32 cbFrameLength, int bNeedAck,
772
	u16 wCurrentRate, u8 byFBOption)
773
{
774
	u32 uCTSFrameLen = 14;
775 776 777 778 779 780

    if (pvCTS == NULL) {
        return;
    }

    if (byPktType == PK_TYPE_11GB || byPktType == PK_TYPE_11GA) {
781 782
	if (byFBOption != AUTO_FB_NONE) {
		/* Auto Fall back */
783
		struct vnt_cts_fb *pBuf = (struct vnt_cts_fb *)pvCTS;
784 785 786
		/* Get SignalField,ServiceField,Length */
		BBvCalculateParameter(pDevice, uCTSFrameLen,
			pDevice->byTopCCKBasicRate, PK_TYPE_11B, &pBuf->b);
787 788 789 790 791 792 793 794 795 796 797
		pBuf->wDuration_ba = s_uGetRTSCTSDuration(pDevice, CTSDUR_BA,
			cbFrameLength, byPktType,
			wCurrentRate, bNeedAck, byFBOption);
		/* Get CTSDuration_ba_f0 */
		pBuf->wCTSDuration_ba_f0 = s_uGetRTSCTSDuration(pDevice,
			CTSDUR_BA_F0, cbFrameLength, byPktType, wCurrentRate,
			bNeedAck, byFBOption);
		/* Get CTSDuration_ba_f1 */
		pBuf->wCTSDuration_ba_f1 = s_uGetRTSCTSDuration(pDevice,
			CTSDUR_BA_F1, cbFrameLength, byPktType, wCurrentRate,
			bNeedAck, byFBOption);
798 799 800 801
		/* Get CTS Frame body */
		pBuf->data.duration = pBuf->wDuration_ba;
		pBuf->data.frame_control = TYPE_CTL_CTS;
		memcpy(pBuf->data.ra, pDevice->abyCurrentNetAddr, ETH_ALEN);
802
	} else {
803
		struct vnt_cts *pBuf = (struct vnt_cts *)pvCTS;
804 805 806
		/* Get SignalField,ServiceField,Length */
		BBvCalculateParameter(pDevice, uCTSFrameLen,
			pDevice->byTopCCKBasicRate, PK_TYPE_11B, &pBuf->b);
807 808 809 810
		/* Get CTSDuration_ba */
		pBuf->wDuration_ba = s_uGetRTSCTSDuration(pDevice,
			CTSDUR_BA, cbFrameLength, byPktType,
			wCurrentRate, bNeedAck, byFBOption);
811 812 813 814
		/*Get CTS Frame body*/
		pBuf->data.duration = pBuf->wDuration_ba;
		pBuf->data.frame_control = TYPE_CTL_CTS;
		memcpy(pBuf->data.ra, pDevice->abyCurrentNetAddr, ETH_ALEN);
815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840
        }
    }
}

/*+
 *
 * Description:
 *      Generate FIFO control for MAC & Baseband controller
 *
 * Parameters:
 *  In:
 *      pDevice         - Pointer to adpater
 *      pTxDataHead     - Transmit Data Buffer
 *      pTxBufHead      - pTxBufHead
 *      pvRrvTime        - pvRrvTime
 *      pvRTS            - RTS Buffer
 *      pCTS            - CTS Buffer
 *      cbFrameSize     - Transmit Data Length (Hdr+Payload+FCS)
 *      bNeedACK        - If need ACK
 *      uDMAIdx         - DMA Index
 *  Out:
 *      none
 *
 * Return Value: none
 *
-*/
841

842 843 844
static void s_vGenerateTxParameter(struct vnt_private *pDevice,
	u8 byPktType, u16 wCurrentRate,	void *pTxBufHead, void *pvRrvTime,
	void *pvRTS, void *pvCTS, u32 cbFrameSize, int bNeedACK, u32 uDMAIdx,
845
	struct ethhdr *psEthHeader)
846
{
847 848 849
	u32 cbMACHdLen = WLAN_HDR_ADDR3_LEN; /* 24 */
	u16 wFifoCtl;
	u8 byFBOption = AUTO_FB_NONE;
850 851 852 853 854 855 856 857 858 859 860 861 862

    //DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"s_vGenerateTxParameter...\n");
    PSTxBufHead pFifoHead = (PSTxBufHead)pTxBufHead;
    pFifoHead->wReserved = wCurrentRate;
    wFifoCtl = pFifoHead->wFIFOCtl;

    if (wFifoCtl & FIFOCTL_AUTO_FB_0) {
        byFBOption = AUTO_FB_0;
    }
    else if (wFifoCtl & FIFOCTL_AUTO_FB_1) {
        byFBOption = AUTO_FB_1;
    }

863 864 865
	if (!pvRrvTime)
		return;

866 867 868 869 870 871 872
    if (pDevice->bLongHeader)
        cbMACHdLen = WLAN_HDR_ADDR3_LEN + 6;

    if (byPktType == PK_TYPE_11GB || byPktType == PK_TYPE_11GA) {

        if (pvRTS != NULL) { //RTS_need
            //Fill RsvTime
873 874
		struct vnt_rrv_time_rts *pBuf =
			(struct vnt_rrv_time_rts *)pvRrvTime;
875 876 877 878 879 880
		pBuf->wRTSTxRrvTime_aa = s_uGetRTSCTSRsvTime(pDevice, 2,
				byPktType, cbFrameSize, wCurrentRate);
		pBuf->wRTSTxRrvTime_ba = s_uGetRTSCTSRsvTime(pDevice, 1,
				byPktType, cbFrameSize, wCurrentRate);
		pBuf->wRTSTxRrvTime_bb = s_uGetRTSCTSRsvTime(pDevice, 0,
				byPktType, cbFrameSize, wCurrentRate);
881 882 883 884 885
		pBuf->wTxRrvTime_a = vnt_rxtx_rsvtime_le16(pDevice,
			byPktType, cbFrameSize, wCurrentRate, bNeedACK);
		pBuf->wTxRrvTime_b = vnt_rxtx_rsvtime_le16(pDevice,
			PK_TYPE_11B, cbFrameSize, pDevice->byTopCCKBasicRate,
				bNeedACK);
886
            //Fill RTS
887 888
	    s_vFillRTSHead(pDevice, byPktType, pvRTS, cbFrameSize, bNeedACK,
				psEthHeader, wCurrentRate, byFBOption);
889 890 891
        }
        else {//RTS_needless, PCF mode
            //Fill RsvTime
892 893
		struct vnt_rrv_time_cts *pBuf =
				(struct vnt_rrv_time_cts *)pvRrvTime;
894 895 896 897 898
		pBuf->wTxRrvTime_a = vnt_rxtx_rsvtime_le16(pDevice, byPktType,
			cbFrameSize, wCurrentRate, bNeedACK);
		pBuf->wTxRrvTime_b = vnt_rxtx_rsvtime_le16(pDevice,
			PK_TYPE_11B, cbFrameSize,
			pDevice->byTopCCKBasicRate, bNeedACK);
899 900
		pBuf->wCTSTxRrvTime_ba = s_uGetRTSCTSRsvTime(pDevice, 3,
				byPktType, cbFrameSize, wCurrentRate);
901
            //Fill CTS
902 903
	    s_vFillCTSHead(pDevice, uDMAIdx, byPktType, pvCTS, cbFrameSize,
			bNeedACK, wCurrentRate, byFBOption);
904 905 906 907 908 909
        }
    }
    else if (byPktType == PK_TYPE_11A) {

        if (pvRTS != NULL) {//RTS_need, non PCF mode
            //Fill RsvTime
910 911
		struct vnt_rrv_time_ab *pBuf =
				(struct vnt_rrv_time_ab *)pvRrvTime;
912 913
		pBuf->wRTSTxRrvTime = s_uGetRTSCTSRsvTime(pDevice, 2,
				byPktType, cbFrameSize, wCurrentRate);
914 915
		pBuf->wTxRrvTime = vnt_rxtx_rsvtime_le16(pDevice, byPktType,
				cbFrameSize, wCurrentRate, bNeedACK);
916
            //Fill RTS
917 918
	    s_vFillRTSHead(pDevice, byPktType, pvRTS, cbFrameSize, bNeedACK,
			psEthHeader, wCurrentRate, byFBOption);
919 920 921
        }
        else if (pvRTS == NULL) {//RTS_needless, non PCF mode
            //Fill RsvTime
922 923
		struct vnt_rrv_time_ab *pBuf =
				(struct vnt_rrv_time_ab *)pvRrvTime;
924 925
		pBuf->wTxRrvTime = vnt_rxtx_rsvtime_le16(pDevice, PK_TYPE_11A,
			cbFrameSize, wCurrentRate, bNeedACK);
926 927 928 929 930 931
        }
    }
    else if (byPktType == PK_TYPE_11B) {

        if ((pvRTS != NULL)) {//RTS_need, non PCF mode
            //Fill RsvTime
932 933
		struct vnt_rrv_time_ab *pBuf =
				(struct vnt_rrv_time_ab *)pvRrvTime;
934 935
		pBuf->wRTSTxRrvTime = s_uGetRTSCTSRsvTime(pDevice, 0,
				byPktType, cbFrameSize, wCurrentRate);
936 937
		pBuf->wTxRrvTime = vnt_rxtx_rsvtime_le16(pDevice, PK_TYPE_11B,
				cbFrameSize, wCurrentRate, bNeedACK);
938
            //Fill RTS
939 940
	    s_vFillRTSHead(pDevice, byPktType, pvRTS, cbFrameSize, bNeedACK,
			psEthHeader, wCurrentRate, byFBOption);
941 942 943
        }
        else { //RTS_needless, non PCF mode
            //Fill RsvTime
944 945
		struct vnt_rrv_time_ab *pBuf =
				(struct vnt_rrv_time_ab *)pvRrvTime;
946 947
		pBuf->wTxRrvTime = vnt_rxtx_rsvtime_le16(pDevice, PK_TYPE_11B,
			cbFrameSize, wCurrentRate, bNeedACK);
948 949 950 951 952
        }
    }
    //DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"s_vGenerateTxParameter END.\n");
}
/*
953
    u8 * pbyBuffer,//point to pTxBufHead
954
    u16  wFragType,//00:Non-Frag, 01:Start, 02:Mid, 03:Last
955
    unsigned int  cbFragmentSize,//Hdr+payoad+FCS
956 957
*/

958
static int s_bPacketToWirelessUsb(struct vnt_private *pDevice, u8 byPktType,
959 960 961 962
	struct vnt_tx_buffer *pTxBufHead, int bNeedEncryption,
	u32 uSkbPacketLen, u32 uDMAIdx,	struct ethhdr *psEthHeader,
	u8 *pPacket, PSKeyItem pTransmitKey, u32 uNodeIndex, u16 wCurrentRate,
	u32 *pcbHeaderLen, u32 *pcbTotalLen)
963
{
964 965 966 967 968 969 970 971 972 973 974 975 976
	struct vnt_manager *pMgmt = &pDevice->vnt_mgmt;
	u32 cbFrameSize, cbFrameBodySize;
	u32 cb802_1_H_len;
	u32 cbIVlen = 0, cbICVlen = 0, cbMIClen = 0, cbMACHdLen = 0;
	u32 cbFCSlen = 4, cbMICHDR = 0;
	int bNeedACK, bRTS;
	u8 *pbyType, *pbyMacHdr, *pbyIVHead, *pbyPayloadHead, *pbyTxBufferAddr;
	u8 abySNAP_RFC1042[ETH_ALEN] = {0xAA, 0xAA, 0x03, 0x00, 0x00, 0x00};
	u8 abySNAP_Bridgetunnel[ETH_ALEN]
		= {0xAA, 0xAA, 0x03, 0x00, 0x00, 0xF8};
	u32 uDuration;
	u32 cbHeaderLength = 0, uPadding = 0;
	void *pvRrvTime;
977
	struct vnt_mic_hdr *pMICHDR;
978 979 980 981 982
	void *pvRTS;
	void *pvCTS;
	void *pvTxDataHd;
	u8 byFBOption = AUTO_FB_NONE, byFragType;
	u16 wTxBufSize;
983
	u32 dwMICKey0, dwMICKey1, dwMIC_Priority;
984
	u32 *pdwMIC_L, *pdwMIC_R;
985
	int bSoftWEP = false;
986 987

	pvRrvTime = pMICHDR = pvRTS = pvCTS = pvTxDataHd = NULL;
988

989
	if (bNeedEncryption && pTransmitKey->pvKeyTable) {
990 991
		if (((PSKeyTable)pTransmitKey->pvKeyTable)->bSoftWEP == true)
			bSoftWEP = true; /* WEP 256 */
992
	}
993 994

    // Get pkt type
995
    if (ntohs(psEthHeader->h_proto) > ETH_DATA_LEN) {
996 997 998 999 1000 1001 1002 1003 1004
        if (pDevice->dwDiagRefCount == 0) {
            cb802_1_H_len = 8;
        } else {
            cb802_1_H_len = 2;
        }
    } else {
        cb802_1_H_len = 0;
    }

1005
    cbFrameBodySize = uSkbPacketLen - ETH_HLEN + cb802_1_H_len;
1006 1007

    //Set packet type
1008
    pTxBufHead->wFIFOCtl |= (u16)(byPktType<<8);
1009 1010

    if (pDevice->dwDiagRefCount != 0) {
1011
        bNeedACK = false;
1012 1013
        pTxBufHead->wFIFOCtl = pTxBufHead->wFIFOCtl & (~FIFOCTL_NEEDACK);
    } else { //if (pDevice->dwDiagRefCount != 0) {
1014 1015
	if ((pDevice->eOPMode == OP_MODE_ADHOC) ||
	    (pDevice->eOPMode == OP_MODE_AP)) {
1016
		if (is_multicast_ether_addr(psEthHeader->h_dest)) {
1017
			bNeedACK = false;
1018 1019 1020
			pTxBufHead->wFIFOCtl =
				pTxBufHead->wFIFOCtl & (~FIFOCTL_NEEDACK);
		} else {
1021
			bNeedACK = true;
1022 1023
			pTxBufHead->wFIFOCtl |= FIFOCTL_NEEDACK;
		}
1024 1025 1026
        }
        else {
            // MSDUs in Infra mode always need ACK
1027
            bNeedACK = true;
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
            pTxBufHead->wFIFOCtl |= FIFOCTL_NEEDACK;
        }
    } //if (pDevice->dwDiagRefCount != 0) {

    pTxBufHead->wTimeStamp = DEFAULT_MSDU_LIFETIME_RES_64us;

    //Set FIFOCTL_LHEAD
    if (pDevice->bLongHeader)
        pTxBufHead->wFIFOCtl |= FIFOCTL_LHEAD;

    //Set FRAGCTL_MACHDCNT
    if (pDevice->bLongHeader) {
        cbMACHdLen = WLAN_HDR_ADDR3_LEN + 6;
    } else {
        cbMACHdLen = WLAN_HDR_ADDR3_LEN;
    }
1044
    pTxBufHead->wFragCtl |= (u16)(cbMACHdLen << 10);
1045 1046

    //Set FIFOCTL_GrpAckPolicy
1047
    if (pDevice->bGrpAckPolicy == true) {//0000 0100 0000 0000
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
        pTxBufHead->wFIFOCtl |=	FIFOCTL_GRPACK;
    }

    //Set Auto Fallback Ctl
    if (wCurrentRate >= RATE_18M) {
        if (pDevice->byAutoFBCtrl == AUTO_FB_0) {
            pTxBufHead->wFIFOCtl |= FIFOCTL_AUTO_FB_0;
            byFBOption = AUTO_FB_0;
        } else if (pDevice->byAutoFBCtrl == AUTO_FB_1) {
            pTxBufHead->wFIFOCtl |= FIFOCTL_AUTO_FB_1;
            byFBOption = AUTO_FB_1;
        }
    }

1062
    if (bSoftWEP != true) {
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
        if ((bNeedEncryption) && (pTransmitKey != NULL))  { //WEP enabled
            if (pTransmitKey->byCipherSuite == KEY_CTL_WEP) { //WEP40 or WEP104
                pTxBufHead->wFragCtl |= FRAGCTL_LEGACY;
            }
            if (pTransmitKey->byCipherSuite == KEY_CTL_TKIP) {
                DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Tx Set wFragCtl == FRAGCTL_TKIP\n");
                pTxBufHead->wFragCtl |= FRAGCTL_TKIP;
            }
            else if (pTransmitKey->byCipherSuite == KEY_CTL_CCMP) { //CCMP
                pTxBufHead->wFragCtl |= FRAGCTL_AES;
            }
        }
    }

    if ((bNeedEncryption) && (pTransmitKey != NULL))  {
        if (pTransmitKey->byCipherSuite == KEY_CTL_WEP) {
            cbIVlen = 4;
            cbICVlen = 4;
        }
        else if (pTransmitKey->byCipherSuite == KEY_CTL_TKIP) {
            cbIVlen = 8;//IV+ExtIV
            cbMIClen = 8;
            cbICVlen = 4;
        }
        if (pTransmitKey->byCipherSuite == KEY_CTL_CCMP) {
            cbIVlen = 8;//RSN Header
            cbICVlen = 8;//MIC
1090
	    cbMICHDR = sizeof(struct vnt_mic_hdr);
1091
        }
1092
        if (bSoftWEP == false) {
1093 1094 1095 1096 1097 1098 1099 1100
            //MAC Header should be padding 0 to DW alignment.
            uPadding = 4 - (cbMACHdLen%4);
            uPadding %= 4;
        }
    }

    cbFrameSize = cbMACHdLen + cbIVlen + (cbFrameBodySize + cbMIClen) + cbICVlen + cbFCSlen;

1101 1102
    if ( (bNeedACK == false) ||(cbFrameSize < pDevice->wRTSThreshold) ) {
        bRTS = false;
1103
    } else {
1104
        bRTS = true;
1105 1106 1107
        pTxBufHead->wFIFOCtl |= (FIFOCTL_RTS | FIFOCTL_LRETRY);
    }

1108
    pbyTxBufferAddr = (u8 *) &(pTxBufHead->adwTxKey[0]);
1109 1110 1111
    wTxBufSize = sizeof(STxBufHead);
    if (byPktType == PK_TYPE_11GB || byPktType == PK_TYPE_11GA) {//802.11g packet
        if (byFBOption == AUTO_FB_NONE) {
1112
            if (bRTS == true) {//RTS_need
1113 1114
		pvRrvTime = (struct vnt_rrv_time_rts *)
					(pbyTxBufferAddr + wTxBufSize);
1115
		pMICHDR = (struct vnt_mic_hdr *)(pbyTxBufferAddr + wTxBufSize +
1116
					sizeof(struct vnt_rrv_time_rts));
1117
		pvRTS = (struct vnt_rts_g *) (pbyTxBufferAddr + wTxBufSize +
1118
				sizeof(struct vnt_rrv_time_rts) + cbMICHDR);
1119
                pvCTS = NULL;
1120 1121 1122
		pvTxDataHd = (struct vnt_tx_datahead_g *) (pbyTxBufferAddr +
			wTxBufSize + sizeof(struct vnt_rrv_time_rts) +
				cbMICHDR + sizeof(struct vnt_rts_g));
1123
		cbHeaderLength = wTxBufSize + sizeof(struct vnt_rrv_time_rts) +
1124
			cbMICHDR + sizeof(struct vnt_rts_g) +
1125
				sizeof(struct vnt_tx_datahead_g);
1126 1127
            }
            else { //RTS_needless
1128 1129
		pvRrvTime = (struct vnt_rrv_time_cts *)
				(pbyTxBufferAddr + wTxBufSize);
1130
		pMICHDR = (struct vnt_mic_hdr *) (pbyTxBufferAddr + wTxBufSize +
1131
			sizeof(struct vnt_rrv_time_cts));
1132
                pvRTS = NULL;
1133
		pvCTS = (struct vnt_cts *) (pbyTxBufferAddr + wTxBufSize +
1134
				sizeof(struct vnt_rrv_time_cts) + cbMICHDR);
1135 1136 1137
		pvTxDataHd = (struct vnt_tx_datahead_g *)(pbyTxBufferAddr +
			wTxBufSize + sizeof(struct vnt_rrv_time_cts) +
				cbMICHDR + sizeof(struct vnt_cts));
1138 1139
		cbHeaderLength = wTxBufSize + sizeof(struct vnt_rrv_time_cts) +
			cbMICHDR + sizeof(struct vnt_cts) +
1140
				sizeof(struct vnt_tx_datahead_g);
1141 1142 1143
            }
        } else {
            // Auto Fall Back
1144
            if (bRTS == true) {//RTS_need
1145 1146
		pvRrvTime = (struct vnt_rrv_time_rts *)(pbyTxBufferAddr +
								wTxBufSize);
1147
		pMICHDR = (struct vnt_mic_hdr *) (pbyTxBufferAddr + wTxBufSize +
1148
					sizeof(struct vnt_rrv_time_rts));
1149
		pvRTS = (struct vnt_rts_g_fb *) (pbyTxBufferAddr + wTxBufSize +
1150
				sizeof(struct vnt_rrv_time_rts) + cbMICHDR);
1151
                pvCTS = NULL;
1152 1153 1154
		pvTxDataHd = (struct vnt_tx_datahead_g_fb *) (pbyTxBufferAddr +
			wTxBufSize + sizeof(struct vnt_rrv_time_rts) +
				cbMICHDR + sizeof(struct vnt_rts_g_fb));
1155 1156
		cbHeaderLength = wTxBufSize + sizeof(struct vnt_rrv_time_rts) +
			cbMICHDR + sizeof(struct vnt_rts_g_fb) +
1157
				sizeof(struct vnt_tx_datahead_g_fb);
1158
            }
1159
            else if (bRTS == false) { //RTS_needless
1160 1161
		pvRrvTime = (struct vnt_rrv_time_cts *)
				(pbyTxBufferAddr + wTxBufSize);
1162
		pMICHDR = (struct vnt_mic_hdr *) (pbyTxBufferAddr + wTxBufSize +
1163
				sizeof(struct vnt_rrv_time_cts));
1164
                pvRTS = NULL;
1165
		pvCTS = (struct vnt_cts_fb *) (pbyTxBufferAddr + wTxBufSize +
1166
			sizeof(struct vnt_rrv_time_cts) + cbMICHDR);
1167
		pvTxDataHd = (struct vnt_tx_datahead_g_fb *) (pbyTxBufferAddr +