rxtx.c 99.4 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 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
static int vnt_fill_ieee80211_rts(struct vnt_private *priv,
	struct ieee80211_rts *rts, struct ethhdr *eth_hdr,
		u16 duration)
{
	rts->duration = duration;
	rts->frame_control = TYPE_CTL_RTS;

	if (priv->eOPMode == OP_MODE_ADHOC || priv->eOPMode == OP_MODE_AP)
		memcpy(rts->ra, eth_hdr->h_dest, ETH_ALEN);
	else
		memcpy(rts->ra, priv->abyBSSID, ETH_ALEN);

	if (priv->eOPMode == OP_MODE_AP)
		memcpy(rts->ta, priv->abyBSSID, ETH_ALEN);
	else
		memcpy(rts->ta, eth_hdr->h_source, ETH_ALEN);

	return 0;
}

static int vnt_rxtx_rts_g_head(struct vnt_private *priv,
	struct vnt_rts_g *buf, struct ethhdr *eth_hdr,
	u8 pkt_type, u32 frame_len, int need_ack,
	u16 current_rate, u8 fb_option)
{
	u16 rts_frame_len = 20;

	BBvCalculateParameter(priv, rts_frame_len, priv->byTopCCKBasicRate,
		PK_TYPE_11B, &buf->b);
	BBvCalculateParameter(priv, rts_frame_len,
		priv->byTopOFDMBasicRate, pkt_type, &buf->a);

	buf->wDuration_bb = s_uGetRTSCTSDuration(priv, RTSDUR_BB, frame_len,
		PK_TYPE_11B, priv->byTopCCKBasicRate, need_ack, fb_option);
	buf->wDuration_aa = s_uGetRTSCTSDuration(priv, RTSDUR_AA, frame_len,
		pkt_type, current_rate, need_ack, fb_option);
	buf->wDuration_ba = s_uGetRTSCTSDuration(priv, RTSDUR_BA, frame_len,
		pkt_type, current_rate, need_ack, fb_option);

	vnt_fill_ieee80211_rts(priv, &buf->data, eth_hdr, buf->wDuration_aa);

	return 0;
}

642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
static int vnt_rxtx_rts_g_fb_head(struct vnt_private *priv,
	struct vnt_rts_g_fb *buf, struct ethhdr *eth_hdr,
	u8 pkt_type, u32 frame_len, int need_ack,
	u16 current_rate, u8 fb_option)
{
	u16 rts_frame_len = 20;

	BBvCalculateParameter(priv, rts_frame_len, priv->byTopCCKBasicRate,
		PK_TYPE_11B, &buf->b);
	BBvCalculateParameter(priv, rts_frame_len,
		priv->byTopOFDMBasicRate, pkt_type, &buf->a);


	buf->wDuration_bb = s_uGetRTSCTSDuration(priv, RTSDUR_BB, frame_len,
		PK_TYPE_11B, priv->byTopCCKBasicRate, need_ack, fb_option);
	buf->wDuration_aa = s_uGetRTSCTSDuration(priv, RTSDUR_AA, frame_len,
		pkt_type, current_rate, need_ack, fb_option);
	buf->wDuration_ba = s_uGetRTSCTSDuration(priv, RTSDUR_BA, frame_len,
		pkt_type, current_rate, need_ack, fb_option);


	buf->wRTSDuration_ba_f0 = s_uGetRTSCTSDuration(priv, RTSDUR_BA_F0,
		frame_len, pkt_type, current_rate, need_ack, fb_option);
	buf->wRTSDuration_aa_f0 = s_uGetRTSCTSDuration(priv, RTSDUR_AA_F0,
		frame_len, pkt_type, current_rate, need_ack, fb_option);
	buf->wRTSDuration_ba_f1 = s_uGetRTSCTSDuration(priv, RTSDUR_BA_F1,
		frame_len, pkt_type, current_rate, need_ack, fb_option);
	buf->wRTSDuration_aa_f1 = s_uGetRTSCTSDuration(priv, RTSDUR_AA_F1,
		frame_len, pkt_type, current_rate, need_ack, fb_option);

	vnt_fill_ieee80211_rts(priv, &buf->data, eth_hdr, buf->wDuration_aa);

	return 0;
}

677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
static int vnt_rxtx_rts_ab_head(struct vnt_private *priv,
	struct vnt_rts_ab *buf, struct ethhdr *eth_hdr,
	u8 pkt_type, u32 frame_len, int need_ack,
	u16 current_rate, u8 fb_option)
{
	u16 rts_frame_len = 20;

	BBvCalculateParameter(priv, rts_frame_len,
		priv->byTopOFDMBasicRate, pkt_type, &buf->ab);

	buf->wDuration = s_uGetRTSCTSDuration(priv, RTSDUR_AA, frame_len,
		pkt_type, current_rate, need_ack, fb_option);

	vnt_fill_ieee80211_rts(priv, &buf->data, eth_hdr, buf->wDuration);

	return 0;
}

695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
static int vnt_rxtx_rts_a_fb_head(struct vnt_private *priv,
	struct vnt_rts_a_fb *buf, struct ethhdr *eth_hdr,
	u8 pkt_type, u32 frame_len, int need_ack,
	u16 current_rate, u8 fb_option)
{
	u16 rts_frame_len = 20;

	BBvCalculateParameter(priv, rts_frame_len,
		priv->byTopOFDMBasicRate, pkt_type, &buf->a);

	buf->wDuration = s_uGetRTSCTSDuration(priv, RTSDUR_AA, frame_len,
		pkt_type, current_rate, need_ack, fb_option);

	buf->wRTSDuration_f0 = s_uGetRTSCTSDuration(priv, RTSDUR_AA_F0,
		frame_len, pkt_type, current_rate, need_ack, fb_option);

	buf->wRTSDuration_f1 = s_uGetRTSCTSDuration(priv, RTSDUR_AA_F1,
		frame_len, pkt_type, current_rate, need_ack, fb_option);

	vnt_fill_ieee80211_rts(priv, &buf->data, eth_hdr, buf->wDuration);

	return 0;
}

719
static void s_vFillRTSHead(struct vnt_private *pDevice, u8 byPktType,
720
	void *pvRTS, u32 cbFrameLength, int bNeedAck,
721
	struct ethhdr *psEthHeader, u16 wCurrentRate, u8 byFBOption)
722
{
723
	union vnt_tx_data_head *head = pvRTS;
724

725 726
	if (!head)
		return;
727

728 729 730 731 732
	/* Note: So far RTSHead doesn't appear in ATIM
	*	& Beacom DMA, so we don't need to take them
	*	into account.
	*	Otherwise, we need to modified codes for them.
	*/
733 734 735
	switch (byPktType) {
	case PK_TYPE_11GB:
	case PK_TYPE_11GA:
736 737
		if (byFBOption == AUTO_FB_NONE)
			vnt_rxtx_rts_g_head(pDevice, &head->rts_g,
738 739
				psEthHeader, byPktType, cbFrameLength,
				bNeedAck, wCurrentRate, byFBOption);
740 741
		else
			vnt_rxtx_rts_g_fb_head(pDevice, &head->rts_g_fb,
742 743
				psEthHeader, byPktType, cbFrameLength,
				bNeedAck, wCurrentRate, byFBOption);
744 745
		break;
	case PK_TYPE_11A:
746
		if (byFBOption) {
747
			vnt_rxtx_rts_a_fb_head(pDevice, &head->rts_a_fb,
748 749
				psEthHeader, byPktType, cbFrameLength,
				bNeedAck, wCurrentRate, byFBOption);
750 751
			break;
		}
752
	case PK_TYPE_11B:
753
		vnt_rxtx_rts_ab_head(pDevice, &head->rts_ab,
754 755
			psEthHeader, byPktType, cbFrameLength,
			bNeedAck, wCurrentRate, byFBOption);
756
	}
757 758
}

759 760
static void s_vFillCTSHead(struct vnt_private *pDevice, u32 uDMAIdx,
	u8 byPktType, void *pvCTS, u32 cbFrameLength, int bNeedAck,
761
	u16 wCurrentRate, u8 byFBOption)
762
{
763
	u32 uCTSFrameLen = 14;
764 765 766 767 768

    if (pvCTS == NULL) {
        return;
    }

769 770
	if (byFBOption != AUTO_FB_NONE) {
		/* Auto Fall back */
771
		struct vnt_cts_fb *pBuf = (struct vnt_cts_fb *)pvCTS;
772 773 774
		/* Get SignalField,ServiceField,Length */
		BBvCalculateParameter(pDevice, uCTSFrameLen,
			pDevice->byTopCCKBasicRate, PK_TYPE_11B, &pBuf->b);
775 776 777 778 779 780 781 782 783 784 785
		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);
786 787 788 789
		/* 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);
790
	} else {
791
		struct vnt_cts *pBuf = (struct vnt_cts *)pvCTS;
792 793 794
		/* Get SignalField,ServiceField,Length */
		BBvCalculateParameter(pDevice, uCTSFrameLen,
			pDevice->byTopCCKBasicRate, PK_TYPE_11B, &pBuf->b);
795 796 797 798
		/* Get CTSDuration_ba */
		pBuf->wDuration_ba = s_uGetRTSCTSDuration(pDevice,
			CTSDUR_BA, cbFrameLength, byPktType,
			wCurrentRate, bNeedAck, byFBOption);
799 800 801 802
		/*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);
803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827
        }
}

/*+
 *
 * 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
 *
-*/
828

829 830 831
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,
832
	struct ethhdr *psEthHeader)
833
{
834 835 836
	u32 cbMACHdLen = WLAN_HDR_ADDR3_LEN; /* 24 */
	u16 wFifoCtl;
	u8 byFBOption = AUTO_FB_NONE;
837 838 839 840 841 842 843 844 845 846 847 848 849

    //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;
    }

850 851 852
	if (!pvRrvTime)
		return;

853 854 855 856 857 858 859
    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
860 861
		struct vnt_rrv_time_rts *pBuf =
			(struct vnt_rrv_time_rts *)pvRrvTime;
862 863 864 865 866 867
		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);
868 869 870 871 872
		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);
873
            //Fill RTS
874 875
	    s_vFillRTSHead(pDevice, byPktType, pvRTS, cbFrameSize, bNeedACK,
				psEthHeader, wCurrentRate, byFBOption);
876 877 878
        }
        else {//RTS_needless, PCF mode
            //Fill RsvTime
879 880
		struct vnt_rrv_time_cts *pBuf =
				(struct vnt_rrv_time_cts *)pvRrvTime;
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 887
		pBuf->wCTSTxRrvTime_ba = s_uGetRTSCTSRsvTime(pDevice, 3,
				byPktType, cbFrameSize, wCurrentRate);
888
            //Fill CTS
889 890
	    s_vFillCTSHead(pDevice, uDMAIdx, byPktType, pvCTS, cbFrameSize,
			bNeedACK, wCurrentRate, byFBOption);
891 892 893 894 895 896
        }
    }
    else if (byPktType == PK_TYPE_11A) {

        if (pvRTS != NULL) {//RTS_need, non PCF mode
            //Fill RsvTime
897 898
		struct vnt_rrv_time_ab *pBuf =
				(struct vnt_rrv_time_ab *)pvRrvTime;
899 900
		pBuf->wRTSTxRrvTime = s_uGetRTSCTSRsvTime(pDevice, 2,
				byPktType, cbFrameSize, wCurrentRate);
901 902
		pBuf->wTxRrvTime = vnt_rxtx_rsvtime_le16(pDevice, byPktType,
				cbFrameSize, wCurrentRate, bNeedACK);
903
            //Fill RTS
904 905
	    s_vFillRTSHead(pDevice, byPktType, pvRTS, cbFrameSize, bNeedACK,
			psEthHeader, wCurrentRate, byFBOption);
906 907 908
        }
        else if (pvRTS == NULL) {//RTS_needless, non PCF mode
            //Fill RsvTime
909 910
		struct vnt_rrv_time_ab *pBuf =
				(struct vnt_rrv_time_ab *)pvRrvTime;
911 912
		pBuf->wTxRrvTime = vnt_rxtx_rsvtime_le16(pDevice, PK_TYPE_11A,
			cbFrameSize, wCurrentRate, bNeedACK);
913 914 915 916 917 918
        }
    }
    else if (byPktType == PK_TYPE_11B) {

        if ((pvRTS != NULL)) {//RTS_need, non PCF mode
            //Fill RsvTime
919 920
		struct vnt_rrv_time_ab *pBuf =
				(struct vnt_rrv_time_ab *)pvRrvTime;
921 922
		pBuf->wRTSTxRrvTime = s_uGetRTSCTSRsvTime(pDevice, 0,
				byPktType, cbFrameSize, wCurrentRate);
923 924
		pBuf->wTxRrvTime = vnt_rxtx_rsvtime_le16(pDevice, PK_TYPE_11B,
				cbFrameSize, wCurrentRate, bNeedACK);
925
            //Fill RTS
926 927
	    s_vFillRTSHead(pDevice, byPktType, pvRTS, cbFrameSize, bNeedACK,
			psEthHeader, wCurrentRate, byFBOption);
928 929 930
        }
        else { //RTS_needless, non PCF mode
            //Fill RsvTime
931 932
		struct vnt_rrv_time_ab *pBuf =
				(struct vnt_rrv_time_ab *)pvRrvTime;
933 934
		pBuf->wTxRrvTime = vnt_rxtx_rsvtime_le16(pDevice, PK_TYPE_11B,
			cbFrameSize, wCurrentRate, bNeedACK);
935 936 937 938 939
        }
    }
    //DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"s_vGenerateTxParameter END.\n");
}
/*
940
    u8 * pbyBuffer,//point to pTxBufHead
941
    u16  wFragType,//00:Non-Frag, 01:Start, 02:Mid, 03:Last
942
    unsigned int  cbFragmentSize,//Hdr+payoad+FCS
943 944
*/

945
static int s_bPacketToWirelessUsb(struct vnt_private *pDevice, u8 byPktType,
946 947 948 949
	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)
950
{
951 952 953 954 955 956 957 958 959 960 961 962 963
	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;
964
	struct vnt_mic_hdr *pMICHDR;
965 966 967 968 969
	void *pvRTS;
	void *pvCTS;
	void *pvTxDataHd;
	u8 byFBOption = AUTO_FB_NONE, byFragType;
	u16 wTxBufSize;
970
	u32 dwMICKey0, dwMICKey1, dwMIC_Priority;
971
	u32 *pdwMIC_L, *pdwMIC_R;
972
	int bSoftWEP = false;
973 974

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

976
	if (bNeedEncryption && pTransmitKey->pvKeyTable) {
977 978
		if (((PSKeyTable)pTransmitKey->pvKeyTable)->bSoftWEP == true)
			bSoftWEP = true; /* WEP 256 */
979
	}
980 981

    // Get pkt type
982
    if (ntohs(psEthHeader->h_proto) > ETH_DATA_LEN) {
983 984 985 986 987 988 989 990 991
        if (pDevice->dwDiagRefCount == 0) {
            cb802_1_H_len = 8;
        } else {
            cb802_1_H_len = 2;
        }
    } else {
        cb802_1_H_len = 0;
    }

992
    cbFrameBodySize = uSkbPacketLen - ETH_HLEN + cb802_1_H_len;
993 994

    //Set packet type
995
    pTxBufHead->wFIFOCtl |= (u16)(byPktType<<8);
996 997

    if (pDevice->dwDiagRefCount != 0) {
998
        bNeedACK = false;
999 1000
        pTxBufHead->wFIFOCtl = pTxBufHead->wFIFOCtl & (~FIFOCTL_NEEDACK);
    } else { //if (pDevice->dwDiagRefCount != 0) {
1001 1002
	if ((pDevice->eOPMode == OP_MODE_ADHOC) ||
	    (pDevice->eOPMode == OP_MODE_AP)) {
1003
		if (is_multicast_ether_addr(psEthHeader->h_dest)) {
1004
			bNeedACK = false;
1005 1006 1007
			pTxBufHead->wFIFOCtl =
				pTxBufHead->wFIFOCtl & (~FIFOCTL_NEEDACK);
		} else {
1008
			bNeedACK = true;
1009 1010
			pTxBufHead->wFIFOCtl |= FIFOCTL_NEEDACK;
		}
1011 1012 1013
        }
        else {
            // MSDUs in Infra mode always need ACK
1014
            bNeedACK = true;
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
            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;
    }
1031
    pTxBufHead->wFragCtl |= (u16)(cbMACHdLen << 10);
1032 1033

    //Set FIFOCTL_GrpAckPolicy
1034
    if (pDevice->bGrpAckPolicy == true) {//0000 0100 0000 0000
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
        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;
        }
    }

1049
    if (bSoftWEP != true) {
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
        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
1077
	    cbMICHDR = sizeof(struct vnt_mic_hdr);
1078
        }
1079
        if (bSoftWEP == false) {
1080 1081 1082 1083 1084 1085 1086 1087
            //MAC Header should be padding 0 to DW alignment.
            uPadding = 4 - (cbMACHdLen%4);
            uPadding %= 4;
        }
    }

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

1088 1089
    if ( (bNeedACK == false) ||(cbFrameSize < pDevice->wRTSThreshold) ) {
        bRTS = false;
1090
    } else {
1091
        bRTS = true;
1092 1093 1094
        pTxBufHead->wFIFOCtl |= (FIFOCTL_RTS | FIFOCTL_LRETRY);
    }

1095
    pbyTxBufferAddr = (u8 *) &(pTxBufHead->adwTxKey[0]);
1096 1097 1098
    wTxBufSize = sizeof(STxBufHead);
    if (byPktType == PK_TYPE_11GB || byPktType == PK_TYPE_11GA) {//802.11g packet
        if (byFBOption == AUTO_FB_NONE) {
1099
            if (bRTS == true) {//RTS_need
1100 1101
		pvRrvTime = (struct vnt_rrv_time_rts *)
					(pbyTxBufferAddr + wTxBufSize);
1102
		pMICHDR = (struct vnt_mic_hdr *)(pbyTxBufferAddr + wTxBufSize +
1103
					sizeof(struct vnt_rrv_time_rts));
1104
		pvRTS = (struct vnt_rts_g *) (pbyTxBufferAddr + wTxBufSize +
1105
				sizeof(struct vnt_rrv_time_rts) + cbMICHDR);
1106
                pvCTS = NULL;
1107 1108 1109
		pvTxDataHd = (struct vnt_tx_datahead_g *) (pbyTxBufferAddr +
			wTxBufSize + sizeof(struct vnt_rrv_time_rts) +
				cbMICHDR + sizeof(struct vnt_rts_g));
1110
		cbHeaderLength = wTxBufSize + sizeof(struct vnt_rrv_time_rts) +
1111
			cbMICHDR + sizeof(struct vnt_rts_g) +
1112
				sizeof(struct vnt_tx_datahead_g);
1113 1114
            }
            else { //RTS_needless
1115 1116
		pvRrvTime = (struct vnt_rrv_time_cts *)
				(pbyTxBufferAddr + wTxBufSize);
1117
		pMICHDR = (struct vnt_mic_hdr *) (pbyTxBufferAddr + wTxBufSize +
1118
			sizeof(struct vnt_rrv_time_cts));
1119
                pvRTS = NULL;
1120
		pvCTS = (struct vnt_cts *) (pbyTxBufferAddr + wTxBufSize +
1121
				sizeof(struct vnt_rrv_time_cts) + cbMICHDR);
1122 1123 1124
		pvTxDataHd = (struct vnt_tx_datahead_g *)(pbyTxBufferAddr +
			wTxBufSize + sizeof(struct vnt_rrv_time_cts) +
				cbMICHDR + sizeof(struct vnt_cts));
1125 1126
		cbHeaderLength = wTxBufSize + sizeof(struct vnt_rrv_time_cts) +
			cbMICHDR + sizeof(struct vnt_cts) +
1127
				sizeof(struct vnt_tx_datahead_g);
1128 1129 1130
            }
        } else {
            // Auto Fall Back
1131
            if (bRTS == true) {//RTS_need
1132 1133
		pvRrvTime = (struct vnt_rrv_time_rts *)(pbyTxBufferAddr +
								wTxBufSize);
1134
		pMICHDR = (struct vnt_mic_hdr *) (pbyTxBufferAddr + wTxBufSize +
1135
					sizeof(struct vnt_rrv_time_rts));
1136
		pvRTS = (struct vnt_rts_g_fb *) (pbyTxBufferAddr + wTxBufSize +
1137
				sizeof(struct vnt_rrv_time_rts) + cbMICHDR);
1138
                pvCTS = NULL;
1139 1140 1141
		pvTxDataHd = (struct vnt_tx_datahead_g_fb *) (pbyTxBufferAddr +
			wTxBufSize + sizeof(struct vnt_rrv_time_rts) +
				cbMICHDR + sizeof(struct vnt_rts_g_fb));
1142 1143
		cbHeaderLength = wTxBufSize + sizeof(struct vnt_rrv_time_rts) +
			cbMICHDR + sizeof(struct vnt_rts_g_fb) +
1144
				sizeof(struct vnt_tx_datahead_g_fb);
1145
            }
1146
            else if (bRTS == false) { //RTS_needless
1147 1148
		pvRrvTime = (struct vnt_rrv_time_cts *)
				(pbyTxBufferAddr + wTxBufSize);
1149
		pMICHDR = (struct vnt_mic_hdr *) (pbyTxBufferAddr + wTxBufSize +
1150
				sizeof(struct vnt_rrv_time_cts));
1151
                pvRTS = NULL;
1152
		pvCTS = (struct vnt_cts_fb *) (pbyTxBufferAddr + wTxBufSize +
1153
			sizeof(struct vnt_rrv_time_cts) + cbMICHDR);
1154
		pvTxDataHd = (struct vnt_tx_datahead_g_fb *) (pbyTxBufferAddr +
1155 1156 1157
			wTxBufSize + sizeof(struct vnt_rrv_time_cts) +
				cbMICHDR + sizeof(struct vnt_cts_fb));
		cbHeaderLength = wTxBufSize + sizeof(struct vnt_rrv_time_cts) +
1158
				cbMICHDR + sizeof(struct vnt_cts_fb) +
1159
					sizeof(struct vnt_tx_datahead_g_fb);
1160 1161 1162 1163 1164
            }
        } // Auto Fall Back
    }
    else {//802.11a/b packet
        if (byFBOption == AUTO_FB_NONE) {
1165
            if (bRTS == true) {//RTS_need
1166 1167
		pvRrvTime = (struct vnt_rrv_time_ab *) (pbyTxBufferAddr +
								wTxBufSize);
1168
		pMICHDR = (struct vnt_mic_hdr *)(pbyTxBufferAddr + wTxBufSize +
1169
						sizeof(struct vnt_rrv_time_ab));
1170
		pvRTS = (struct vnt_rts_ab *) (pbyTxBufferAddr + wTxBufSize +
1171
				sizeof(struct vnt_rrv_time_ab) + cbMICHDR);
1172
                pvCTS = NULL;
1173 1174
		pvTxDataHd = (struct vnt_tx_datahead_ab *)(pbyTxBufferAddr +
			wTxBufSize + sizeof(struct vnt_rrv_time_ab) + cbMICHDR +
1175
						sizeof(struct vnt_rts_ab));
1176 1177
		cbHeaderLength = wTxBufSize + sizeof(struct vnt_rrv_time_ab) +
			cbMICHDR + sizeof(struct vnt_rts_ab) +
1178
				sizeof(struct vnt_tx_datahead_ab);
Forest Bond's avatar