rxtx.c 32 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
/*
 * 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.
 *
 *
 * File: rxtx.c
 *
 * Purpose: handle WMAC/802.3/802.11 rx & tx functions
 *
 * Author: Lyndon Chen
 *
 * Date: May 20, 2003
 *
 * Functions:
25
 *      vnt_generate_tx_parameter - Generate tx dma required parameter.
26
 *      vnt_get_duration_le - get tx data required duration
27
 *      vnt_get_rtscts_duration_le- get rtx/cts required duration
28
 *      vnt_get_rtscts_rsvtime_le- get rts/cts reserved time
29
 *      vnt_get_rsvtime- get frame reserved time
30
 *      vnt_fill_cts_head- fulfill CTS ctl header
31 32 33 34 35
 *
 * Revision History:
 *
 */

36
#include <linux/etherdevice.h>
37 38 39 40 41 42
#include "device.h"
#include "rxtx.h"
#include "card.h"
#include "mac.h"
#include "rf.h"
#include "usbpipe.h"
43

44
static const u16 vnt_time_stampoff[2][MAX_RATE] = {
45 46 47 48 49
	/* Long Preamble */
	{384, 288, 226, 209, 54, 43, 37, 31, 28, 25, 24, 23},

	/* Short Preamble */
	{384, 192, 130, 113, 54, 43, 37, 31, 28, 25, 24, 23},
50
};
51

52 53 54 55 56
static const u16 vnt_fb_opt0[2][5] = {
	{RATE_12M, RATE_18M, RATE_24M, RATE_36M, RATE_48M}, /* fallback_rate0 */
	{RATE_12M, RATE_12M, RATE_18M, RATE_24M, RATE_36M}, /* fallback_rate1 */
};

57 58
static const u16 vnt_fb_opt1[2][5] = {
	{RATE_12M, RATE_18M, RATE_24M, RATE_24M, RATE_36M}, /* fallback_rate0 */
59
	{RATE_6M,  RATE_6M,  RATE_12M, RATE_12M, RATE_18M}, /* fallback_rate1 */
60
};
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76

#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

77
static struct vnt_usb_send_context
78
	*vnt_get_free_context(struct vnt_private *priv)
79
{
80
	struct vnt_usb_send_context *context = NULL;
81
	int ii;
82

83
	dev_dbg(&priv->usb->dev, "%s\n", __func__);
84

85
	for (ii = 0; ii < priv->num_tx_context; ii++) {
86
		if (!priv->tx_context[ii])
87 88
			return NULL;

89
		context = priv->tx_context[ii];
90
		if (!context->in_use) {
91 92
			context->in_use = true;
			memset(context->data, 0,
93
			       MAX_TOTAL_SIZE_WITH_ALL_HEADERS);
94 95 96

			context->hdr = NULL;

97 98 99 100
			return context;
		}
	}

101
	if (ii == priv->num_tx_context) {
102
		dev_dbg(&priv->usb->dev, "%s No Free Tx Context\n", __func__);
103

104 105 106
		ieee80211_stop_queues(priv->hw);
	}

107
	return NULL;
108 109
}

110
static __le16 vnt_time_stamp_off(struct vnt_private *priv, u16 rate)
111
{
112
	return cpu_to_le16(vnt_time_stampoff[priv->preamble_type % 2]
113 114 115
							[rate % MAX_RATE]);
}

116
static u32 vnt_get_rsvtime(struct vnt_private *priv, u8 pkt_type,
117
			   u32 frame_length, u16 rate, int need_ack)
118
{
119
	u32 data_time, ack_time;
120

121
	data_time = vnt_get_frame_time(priv->preamble_type, pkt_type,
122
				       frame_length, rate);
123

124
	if (pkt_type == PK_TYPE_11B)
125
		ack_time = vnt_get_frame_time(priv->preamble_type, pkt_type,
126
					      14, (u16)priv->top_cck_basic_rate);
127
	else
128
		ack_time = vnt_get_frame_time(priv->preamble_type, pkt_type,
129
					      14, (u16)priv->top_ofdm_basic_rate);
130 131

	if (need_ack)
132
		return data_time + priv->sifs + ack_time;
133 134

	return data_time;
135 136
}

137
static __le16 vnt_rxtx_rsvtime_le16(struct vnt_private *priv, u8 pkt_type,
138
				    u32 frame_length, u16 rate, int need_ack)
139
{
140
	return cpu_to_le16((u16)vnt_get_rsvtime(priv, pkt_type,
141 142 143
		frame_length, rate, need_ack));
}

144 145 146
static __le16 vnt_get_rtscts_rsvtime_le(struct vnt_private *priv, u8 rsv_type,
					u8 pkt_type, u32 frame_length,
					u16 current_rate)
147
{
148
	u32 rrv_time, rts_time, cts_time, ack_time, data_time;
149

150 151 152 153
	rrv_time = 0;
	rts_time = 0;
	cts_time = 0;
	ack_time = 0;
154

155
	data_time = vnt_get_frame_time(priv->preamble_type, pkt_type,
156
				       frame_length, current_rate);
157 158

	if (rsv_type == 0) {
159 160
		rts_time = vnt_get_frame_time(priv->preamble_type, pkt_type,
					      20, priv->top_cck_basic_rate);
161 162 163 164 165
		ack_time = vnt_get_frame_time(priv->preamble_type,
					      pkt_type, 14,
					      priv->top_cck_basic_rate);
		cts_time = ack_time;

166
	} else if (rsv_type == 1) {
167 168
		rts_time = vnt_get_frame_time(priv->preamble_type, pkt_type,
					      20, priv->top_cck_basic_rate);
169
		cts_time = vnt_get_frame_time(priv->preamble_type, pkt_type,
170
					      14, priv->top_cck_basic_rate);
171
		ack_time = vnt_get_frame_time(priv->preamble_type, pkt_type,
172
					      14, priv->top_ofdm_basic_rate);
173
	} else if (rsv_type == 2) {
174
		rts_time = vnt_get_frame_time(priv->preamble_type, pkt_type,
175
					      20, priv->top_ofdm_basic_rate);
176 177 178 179 180
		ack_time = vnt_get_frame_time(priv->preamble_type,
					      pkt_type, 14,
					      priv->top_ofdm_basic_rate);
		cts_time = ack_time;

181
	} else if (rsv_type == 3) {
182
		cts_time = vnt_get_frame_time(priv->preamble_type, pkt_type,
183
					      14, priv->top_cck_basic_rate);
184
		ack_time = vnt_get_frame_time(priv->preamble_type, pkt_type,
185
					      14, priv->top_ofdm_basic_rate);
186

187
		rrv_time = cts_time + ack_time + data_time + 2 * priv->sifs;
188

189
		return cpu_to_le16((u16)rrv_time);
190 191
	}

192
	rrv_time = rts_time + cts_time + ack_time + data_time + 3 * priv->sifs;
193

194
	return cpu_to_le16((u16)rrv_time);
195 196
}

197 198
static __le16 vnt_get_duration_le(struct vnt_private *priv, u8 pkt_type,
				  int need_ack)
199
{
200
	u32 ack_time = 0;
201

202 203
	if (need_ack) {
		if (pkt_type == PK_TYPE_11B)
204
			ack_time = vnt_get_frame_time(priv->preamble_type,
205 206
						      pkt_type, 14,
						      priv->top_cck_basic_rate);
207
		else
208
			ack_time = vnt_get_frame_time(priv->preamble_type,
209 210
						      pkt_type, 14,
						      priv->top_ofdm_basic_rate);
211

212
		return cpu_to_le16((u16)(priv->sifs + ack_time));
213
	}
214 215 216 217

	return 0;
}

218 219
static __le16 vnt_get_rtscts_duration_le(struct vnt_usb_send_context *context,
					 u8 dur_type, u8 pkt_type, u16 rate)
220
{
221
	struct vnt_private *priv = context->priv;
222
	u32 cts_time = 0, dur_time = 0;
223 224
	u32 frame_length = context->frame_len;
	u8 need_ack = context->need_ack;
225

226
	switch (dur_type) {
227 228 229 230
	case RTSDUR_BB:
	case RTSDUR_BA:
	case RTSDUR_BA_F0:
	case RTSDUR_BA_F1:
231 232
		cts_time = vnt_get_frame_time(priv->preamble_type, pkt_type,
					      14, priv->top_cck_basic_rate);
233
		dur_time = cts_time + 2 * priv->sifs +
234
			vnt_get_rsvtime(priv, pkt_type,
235
					frame_length, rate, need_ack);
236
		break;
237

238 239 240
	case RTSDUR_AA:
	case RTSDUR_AA_F0:
	case RTSDUR_AA_F1:
241 242
		cts_time = vnt_get_frame_time(priv->preamble_type, pkt_type,
					      14, priv->top_ofdm_basic_rate);
243
		dur_time = cts_time + 2 * priv->sifs +
244
			vnt_get_rsvtime(priv, pkt_type,
245
					frame_length, rate, need_ack);
246
		break;
247

248 249 250
	case CTSDUR_BA:
	case CTSDUR_BA_F0:
	case CTSDUR_BA_F1:
251
		dur_time = priv->sifs + vnt_get_rsvtime(priv,
252
				pkt_type, frame_length, rate, need_ack);
253
		break;
254

255 256 257
	default:
		break;
	}
258

259
	return cpu_to_le16((u16)dur_time);
260 261
}

262
static u16 vnt_mac_hdr_pos(struct vnt_usb_send_context *tx_context,
263
			   struct ieee80211_hdr *hdr)
264 265 266 267 268 269 270 271 272 273 274
{
	u8 *head = tx_context->data + offsetof(struct vnt_tx_buffer, fifo_head);
	u8 *hdr_pos = (u8 *)hdr;

	tx_context->hdr = hdr;
	if (!tx_context->hdr)
		return 0;

	return (u16)(hdr_pos - head);
}

275
static u16 vnt_rxtx_datahead_g(struct vnt_usb_send_context *tx_context,
276
			       struct vnt_tx_datahead_g *buf)
277
{
278
	struct vnt_private *priv = tx_context->priv;
279 280
	struct ieee80211_hdr *hdr =
				(struct ieee80211_hdr *)tx_context->skb->data;
281
	u32 frame_len = tx_context->frame_len;
282
	u16 rate = tx_context->tx_rate;
283
	u8 need_ack = tx_context->need_ack;
284

285
	/* Get SignalField,ServiceField,Length */
286
	vnt_get_phy_field(priv, frame_len, rate, tx_context->pkt_type, &buf->a);
287
	vnt_get_phy_field(priv, frame_len, priv->top_cck_basic_rate,
288
			  PK_TYPE_11B, &buf->b);
289 290

	/* Get Duration and TimeStamp */
291 292 293 294 295 296
	if (ieee80211_is_pspoll(hdr->frame_control)) {
		__le16 dur = cpu_to_le16(priv->current_aid | BIT(14) | BIT(15));

		buf->duration_a = dur;
		buf->duration_b = dur;
	} else {
297 298
		buf->duration_a = vnt_get_duration_le(priv,
						tx_context->pkt_type, need_ack);
299
		buf->duration_b = vnt_get_duration_le(priv,
300 301
							PK_TYPE_11B, need_ack);
	}
302

303 304
	buf->time_stamp_off_a = vnt_time_stamp_off(priv, rate);
	buf->time_stamp_off_b = vnt_time_stamp_off(priv,
305
					priv->top_cck_basic_rate);
306

307 308
	tx_context->tx_hdr_size = vnt_mac_hdr_pos(tx_context, &buf->hdr);

309
	return le16_to_cpu(buf->duration_a);
310 311
}

312
static u16 vnt_rxtx_datahead_g_fb(struct vnt_usb_send_context *tx_context,
313
				  struct vnt_tx_datahead_g_fb *buf)
314
{
315
	struct vnt_private *priv = tx_context->priv;
316
	u32 frame_len = tx_context->frame_len;
317
	u16 rate = tx_context->tx_rate;
318
	u8 need_ack = tx_context->need_ack;
319

320
	/* Get SignalField,ServiceField,Length */
321
	vnt_get_phy_field(priv, frame_len, rate, tx_context->pkt_type, &buf->a);
322

323
	vnt_get_phy_field(priv, frame_len, priv->top_cck_basic_rate,
324
			  PK_TYPE_11B, &buf->b);
325 326

	/* Get Duration and TimeStamp */
327 328
	buf->duration_a = vnt_get_duration_le(priv, tx_context->pkt_type,
					      need_ack);
329
	buf->duration_b = vnt_get_duration_le(priv, PK_TYPE_11B, need_ack);
330

331 332 333 334
	buf->duration_a_f0 = vnt_get_duration_le(priv, tx_context->pkt_type,
						 need_ack);
	buf->duration_a_f1 = vnt_get_duration_le(priv, tx_context->pkt_type,
						 need_ack);
335

336 337
	buf->time_stamp_off_a = vnt_time_stamp_off(priv, rate);
	buf->time_stamp_off_b = vnt_time_stamp_off(priv,
338
						priv->top_cck_basic_rate);
339

340 341
	tx_context->tx_hdr_size = vnt_mac_hdr_pos(tx_context, &buf->hdr);

342
	return le16_to_cpu(buf->duration_a);
343 344
}

345
static u16 vnt_rxtx_datahead_a_fb(struct vnt_usb_send_context *tx_context,
346
				  struct vnt_tx_datahead_a_fb *buf)
347
{
348
	struct vnt_private *priv = tx_context->priv;
349
	u16 rate = tx_context->tx_rate;
350
	u8 pkt_type = tx_context->pkt_type;
351
	u8 need_ack = tx_context->need_ack;
352
	u32 frame_len = tx_context->frame_len;
353

354
	/* Get SignalField,ServiceField,Length */
355
	vnt_get_phy_field(priv, frame_len, rate, pkt_type, &buf->a);
356
	/* Get Duration and TimeStampOff */
357
	buf->duration = vnt_get_duration_le(priv, pkt_type, need_ack);
358

359 360
	buf->duration_f0 = vnt_get_duration_le(priv, pkt_type, need_ack);
	buf->duration_f1 = vnt_get_duration_le(priv, pkt_type, need_ack);
361

362
	buf->time_stamp_off = vnt_time_stamp_off(priv, rate);
363

364 365
	tx_context->tx_hdr_size = vnt_mac_hdr_pos(tx_context, &buf->hdr);

366
	return le16_to_cpu(buf->duration);
367 368
}

369
static u16 vnt_rxtx_datahead_ab(struct vnt_usb_send_context *tx_context,
370
				struct vnt_tx_datahead_ab *buf)
371
{
372
	struct vnt_private *priv = tx_context->priv;
373 374
	struct ieee80211_hdr *hdr =
				(struct ieee80211_hdr *)tx_context->skb->data;
375
	u32 frame_len = tx_context->frame_len;
376
	u16 rate = tx_context->tx_rate;
377
	u8 need_ack = tx_context->need_ack;
378

379
	/* Get SignalField,ServiceField,Length */
380 381
	vnt_get_phy_field(priv, frame_len, rate,
			  tx_context->pkt_type, &buf->ab);
382

383
	/* Get Duration and TimeStampOff */
384 385 386 387 388
	if (ieee80211_is_pspoll(hdr->frame_control)) {
		__le16 dur = cpu_to_le16(priv->current_aid | BIT(14) | BIT(15));

		buf->duration = dur;
	} else {
389 390
		buf->duration = vnt_get_duration_le(priv, tx_context->pkt_type,
						    need_ack);
391
	}
392

393
	buf->time_stamp_off = vnt_time_stamp_off(priv, rate);
394

395 396
	tx_context->tx_hdr_size = vnt_mac_hdr_pos(tx_context, &buf->hdr);

397
	return le16_to_cpu(buf->duration);
398 399
}

400
static int vnt_fill_ieee80211_rts(struct vnt_usb_send_context *tx_context,
401
				  struct ieee80211_rts *rts, __le16 duration)
402
{
403 404 405
	struct ieee80211_hdr *hdr =
				(struct ieee80211_hdr *)tx_context->skb->data;

406
	rts->duration = duration;
407 408
	rts->frame_control =
		cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS);
409

410 411
	ether_addr_copy(rts->ra, hdr->addr1);
	ether_addr_copy(rts->ta, hdr->addr2);
412 413 414 415

	return 0;
}

416
static u16 vnt_rxtx_rts_g_head(struct vnt_usb_send_context *tx_context,
417
			       struct vnt_rts_g *buf)
418
{
419
	struct vnt_private *priv = tx_context->priv;
420
	u16 rts_frame_len = 20;
421
	u16 current_rate = tx_context->tx_rate;
422

423
	vnt_get_phy_field(priv, rts_frame_len, priv->top_cck_basic_rate,
424
			  PK_TYPE_11B, &buf->b);
425 426
	vnt_get_phy_field(priv, rts_frame_len, priv->top_ofdm_basic_rate,
			  tx_context->pkt_type, &buf->a);
427

428 429 430 431 432 433 434 435 436
	buf->duration_bb = vnt_get_rtscts_duration_le(tx_context, RTSDUR_BB,
						      PK_TYPE_11B,
						      priv->top_cck_basic_rate);
	buf->duration_aa = vnt_get_rtscts_duration_le(tx_context, RTSDUR_AA,
						      tx_context->pkt_type,
						      current_rate);
	buf->duration_ba = vnt_get_rtscts_duration_le(tx_context, RTSDUR_BA,
						      tx_context->pkt_type,
						      current_rate);
437

438
	vnt_fill_ieee80211_rts(tx_context, &buf->data, buf->duration_aa);
439

440
	return vnt_rxtx_datahead_g(tx_context, &buf->data_head);
441 442
}

443
static u16 vnt_rxtx_rts_g_fb_head(struct vnt_usb_send_context *tx_context,
444
				  struct vnt_rts_g_fb *buf)
445
{
446
	struct vnt_private *priv = tx_context->priv;
447
	u16 current_rate = tx_context->tx_rate;
448
	u16 rts_frame_len = 20;
449

450
	vnt_get_phy_field(priv, rts_frame_len, priv->top_cck_basic_rate,
451
			  PK_TYPE_11B, &buf->b);
452 453
	vnt_get_phy_field(priv, rts_frame_len, priv->top_ofdm_basic_rate,
			  tx_context->pkt_type, &buf->a);
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
	buf->duration_bb = vnt_get_rtscts_duration_le(tx_context, RTSDUR_BB,
						      PK_TYPE_11B,
						      priv->top_cck_basic_rate);
	buf->duration_aa = vnt_get_rtscts_duration_le(tx_context, RTSDUR_AA,
						      tx_context->pkt_type,
						      current_rate);
	buf->duration_ba = vnt_get_rtscts_duration_le(tx_context, RTSDUR_BA,
						      tx_context->pkt_type,
						      current_rate);

	buf->rts_duration_ba_f0 =
		vnt_get_rtscts_duration_le(tx_context, RTSDUR_BA_F0,
					   tx_context->pkt_type,
					   priv->tx_rate_fb0);
	buf->rts_duration_aa_f0 =
		vnt_get_rtscts_duration_le(tx_context, RTSDUR_AA_F0,
					   tx_context->pkt_type,
					   priv->tx_rate_fb0);
	buf->rts_duration_ba_f1 =
		vnt_get_rtscts_duration_le(tx_context, RTSDUR_BA_F1,
					   tx_context->pkt_type,
					   priv->tx_rate_fb1);
	buf->rts_duration_aa_f1 =
		vnt_get_rtscts_duration_le(tx_context, RTSDUR_AA_F1,
					   tx_context->pkt_type,
					   priv->tx_rate_fb1);
481

482
	vnt_fill_ieee80211_rts(tx_context, &buf->data, buf->duration_aa);
483

484
	return vnt_rxtx_datahead_g_fb(tx_context, &buf->data_head);
485 486
}

487
static u16 vnt_rxtx_rts_ab_head(struct vnt_usb_send_context *tx_context,
488
				struct vnt_rts_ab *buf)
489
{
490
	struct vnt_private *priv = tx_context->priv;
491
	u16 current_rate = tx_context->tx_rate;
492 493
	u16 rts_frame_len = 20;

494 495
	vnt_get_phy_field(priv, rts_frame_len, priv->top_ofdm_basic_rate,
			  tx_context->pkt_type, &buf->ab);
496

497
	buf->duration = vnt_get_rtscts_duration_le(tx_context, RTSDUR_AA,
498
						   tx_context->pkt_type,
499
						   current_rate);
500

501
	vnt_fill_ieee80211_rts(tx_context, &buf->data, buf->duration);
502

503
	return vnt_rxtx_datahead_ab(tx_context, &buf->data_head);
504 505
}

506
static u16 vnt_rxtx_rts_a_fb_head(struct vnt_usb_send_context *tx_context,
507
				  struct vnt_rts_a_fb *buf)
508
{
509
	struct vnt_private *priv = tx_context->priv;
510
	u16 current_rate = tx_context->tx_rate;
511
	u16 rts_frame_len = 20;
512

513 514
	vnt_get_phy_field(priv, rts_frame_len, priv->top_ofdm_basic_rate,
			  tx_context->pkt_type, &buf->a);
515

516
	buf->duration = vnt_get_rtscts_duration_le(tx_context, RTSDUR_AA,
517
						   tx_context->pkt_type,
518
						   current_rate);
519

520 521 522 523
	buf->rts_duration_f0 =
		vnt_get_rtscts_duration_le(tx_context, RTSDUR_AA_F0,
					   tx_context->pkt_type,
					   priv->tx_rate_fb0);
524

525 526 527 528
	buf->rts_duration_f1 =
		vnt_get_rtscts_duration_le(tx_context, RTSDUR_AA_F1,
					   tx_context->pkt_type,
					   priv->tx_rate_fb1);
529

530
	vnt_fill_ieee80211_rts(tx_context, &buf->data, buf->duration);
531

532
	return vnt_rxtx_datahead_a_fb(tx_context, &buf->data_head);
533 534
}

535 536
static u16 vnt_fill_cts_fb_head(struct vnt_usb_send_context *tx_context,
				union vnt_tx_data_head *head)
537
{
538
	struct vnt_private *priv = tx_context->priv;
539
	struct vnt_cts_fb *buf = &head->cts_g_fb;
540
	u32 cts_frame_len = 14;
541
	u16 current_rate = tx_context->tx_rate;
542

543 544 545
	/* Get SignalField,ServiceField,Length */
	vnt_get_phy_field(priv, cts_frame_len, priv->top_cck_basic_rate,
			  PK_TYPE_11B, &buf->b);
546

547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
	buf->duration_ba =
		vnt_get_rtscts_duration_le(tx_context, CTSDUR_BA,
					   tx_context->pkt_type,
					   current_rate);
	/* Get CTSDuration_ba_f0 */
	buf->cts_duration_ba_f0 =
		vnt_get_rtscts_duration_le(tx_context, CTSDUR_BA_F0,
					   tx_context->pkt_type,
					   priv->tx_rate_fb0);
	/* Get CTSDuration_ba_f1 */
	buf->cts_duration_ba_f1 =
		vnt_get_rtscts_duration_le(tx_context, CTSDUR_BA_F1,
					   tx_context->pkt_type,
					   priv->tx_rate_fb1);
	/* Get CTS Frame body */
	buf->data.duration = buf->duration_ba;
	buf->data.frame_control =
		cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS);
565

566 567 568 569
	ether_addr_copy(buf->data.ra, priv->current_net_addr);

	return vnt_rxtx_datahead_g_fb(tx_context, &buf->data_head);
}
570

571 572 573 574 575 576 577
static u16 vnt_fill_cts_head(struct vnt_usb_send_context *tx_context,
			     union vnt_tx_data_head *head)
{
	struct vnt_private *priv = tx_context->priv;
	struct vnt_cts *buf = &head->cts_g;
	u32 cts_frame_len = 14;
	u16 current_rate = tx_context->tx_rate;
578

579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
	/* Get SignalField,ServiceField,Length */
	vnt_get_phy_field(priv, cts_frame_len, priv->top_cck_basic_rate,
			  PK_TYPE_11B, &buf->b);
	/* Get CTSDuration_ba */
	buf->duration_ba =
		vnt_get_rtscts_duration_le(tx_context, CTSDUR_BA,
					   tx_context->pkt_type,
					   current_rate);
	/*Get CTS Frame body*/
	buf->data.duration = buf->duration_ba;
	buf->data.frame_control =
		cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS);

	ether_addr_copy(buf->data.ra, priv->current_net_addr);

	return vnt_rxtx_datahead_g(tx_context, &buf->data_head);
595 596
}

597
static u16 vnt_rxtx_rts(struct vnt_usb_send_context *tx_context,
598
			union vnt_tx_head *tx_head, bool need_mic)
599 600 601
{
	struct vnt_private *priv = tx_context->priv;
	struct vnt_rrv_time_rts *buf = &tx_head->tx_rts.rts;
602
	union vnt_tx_data_head *head = &tx_head->tx_rts.tx.head;
603
	u32 frame_len = tx_context->frame_len;
604
	u16 current_rate = tx_context->tx_rate;
605
	u8 need_ack = tx_context->need_ack;
606

607
	buf->rts_rrv_time_aa = vnt_get_rtscts_rsvtime_le(priv, 2,
608
			tx_context->pkt_type, frame_len, current_rate);
609
	buf->rts_rrv_time_ba = vnt_get_rtscts_rsvtime_le(priv, 1,
610
			tx_context->pkt_type, frame_len, current_rate);
611
	buf->rts_rrv_time_bb = vnt_get_rtscts_rsvtime_le(priv, 0,
612
			tx_context->pkt_type, frame_len, current_rate);
613

614
	buf->rrv_time_a = vnt_rxtx_rsvtime_le16(priv, tx_context->pkt_type,
615
						frame_len, current_rate,
616
						need_ack);
617
	buf->rrv_time_b = vnt_rxtx_rsvtime_le16(priv, PK_TYPE_11B, frame_len,
618
					priv->top_cck_basic_rate, need_ack);
619

620 621 622 623
	if (need_mic)
		head = &tx_head->tx_rts.tx.mic.head;

	if (tx_context->fb_option)
624
		return vnt_rxtx_rts_g_fb_head(tx_context, &head->rts_g_fb);
625

626
	return vnt_rxtx_rts_g_head(tx_context, &head->rts_g);
627 628
}

629
static u16 vnt_rxtx_cts(struct vnt_usb_send_context *tx_context,
630
			union vnt_tx_head *tx_head, bool need_mic)
631 632 633
{
	struct vnt_private *priv = tx_context->priv;
	struct vnt_rrv_time_cts *buf = &tx_head->tx_cts.cts;
634
	union vnt_tx_data_head *head = &tx_head->tx_cts.tx.head;
635
	u32 frame_len = tx_context->frame_len;
636
	u16 current_rate = tx_context->tx_rate;
637 638
	u8 need_ack = tx_context->need_ack;

639
	buf->rrv_time_a = vnt_rxtx_rsvtime_le16(priv, tx_context->pkt_type,
640
					frame_len, current_rate, need_ack);
641
	buf->rrv_time_b = vnt_rxtx_rsvtime_le16(priv, PK_TYPE_11B,
642
				frame_len, priv->top_cck_basic_rate, need_ack);
643

644
	buf->cts_rrv_time_ba = vnt_get_rtscts_rsvtime_le(priv, 3,
645
			tx_context->pkt_type, frame_len, current_rate);
646

647 648 649 650
	if (need_mic)
		head = &tx_head->tx_cts.tx.mic.head;

	/* Fill CTS */
651 652 653
	if (tx_context->fb_option)
		return vnt_fill_cts_fb_head(tx_context, head);

654
	return vnt_fill_cts_head(tx_context, head);
655 656
}

657
static u16 vnt_rxtx_ab(struct vnt_usb_send_context *tx_context,
658
		       union vnt_tx_head *tx_head, bool need_rts, bool need_mic)
659 660 661
{
	struct vnt_private *priv = tx_context->priv;
	struct vnt_rrv_time_ab *buf = &tx_head->tx_ab.ab;
662
	union vnt_tx_data_head *head = &tx_head->tx_ab.tx.head;
663
	u32 frame_len = tx_context->frame_len;
664
	u16 current_rate = tx_context->tx_rate;
665
	u8 need_ack = tx_context->need_ack;
666

667
	buf->rrv_time = vnt_rxtx_rsvtime_le16(priv, tx_context->pkt_type,
668
			frame_len, current_rate, need_ack);
669

670 671 672
	if (need_mic)
		head = &tx_head->tx_ab.tx.mic.head;

673
	if (need_rts) {
674
		if (tx_context->pkt_type == PK_TYPE_11B)
675
			buf->rts_rrv_time = vnt_get_rtscts_rsvtime_le(priv, 0,
676
				tx_context->pkt_type, frame_len, current_rate);
677
		else /* PK_TYPE_11A */
678
			buf->rts_rrv_time = vnt_get_rtscts_rsvtime_le(priv, 2,
679
				tx_context->pkt_type, frame_len, current_rate);
680

681 682
		if (tx_context->fb_option &&
		    tx_context->pkt_type == PK_TYPE_11A)
683
			return vnt_rxtx_rts_a_fb_head(tx_context,
684
						      &head->rts_a_fb);
685

686
		return vnt_rxtx_rts_ab_head(tx_context, &head->rts_ab);
687 688
	}

689
	if (tx_context->pkt_type == PK_TYPE_11A)
690
		return vnt_rxtx_datahead_a_fb(tx_context,
691
					      &head->data_head_a_fb);
692

693
	return vnt_rxtx_datahead_ab(tx_context, &head->data_head_ab);
694 695
}

696
static u16 vnt_generate_tx_parameter(struct vnt_usb_send_context *tx_context,
697 698 699
				     struct vnt_tx_buffer *tx_buffer,
				     struct vnt_mic_hdr **mic_hdr, u32 need_mic,
				     bool need_rts)
700
{
701 702
	if (tx_context->pkt_type == PK_TYPE_11GB ||
	    tx_context->pkt_type == PK_TYPE_11GA) {
703
		if (need_rts) {
704
			if (need_mic)
705 706
				*mic_hdr =
					&tx_buffer->tx_head.tx_rts.tx.mic.hdr;
707

708
			return vnt_rxtx_rts(tx_context, &tx_buffer->tx_head,
709
					    need_mic);
710
		}
711

712
		if (need_mic)
713
			*mic_hdr = &tx_buffer->tx_head.tx_cts.tx.mic.hdr;
714

715
		return vnt_rxtx_cts(tx_context, &tx_buffer->tx_head, need_mic);
716
	}
717

718 719 720
	if (need_mic)
		*mic_hdr = &tx_buffer->tx_head.tx_ab.tx.mic.hdr;

721
	return vnt_rxtx_ab(tx_context, &tx_buffer->tx_head, need_rts, need_mic);
722
}
723 724

static void vnt_fill_txkey(struct vnt_usb_send_context *tx_context,
725 726 727
			   u8 *key_buffer, struct ieee80211_key_conf *tx_key,
			   struct sk_buff *skb, u16 payload_len,
			   struct vnt_mic_hdr *mic_hdr)
728 729
{
	struct ieee80211_hdr *hdr = tx_context->hdr;
730
	u64 pn64;
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745
	u8 *iv = ((u8 *)hdr + ieee80211_get_hdrlen_from_skb(skb));

	/* strip header and icv len from payload */
	payload_len -= ieee80211_get_hdrlen_from_skb(skb);
	payload_len -= tx_key->icv_len;

	switch (tx_key->cipher) {
	case WLAN_CIPHER_SUITE_WEP40:
	case WLAN_CIPHER_SUITE_WEP104:
		memcpy(key_buffer, iv, 3);
		memcpy(key_buffer + 3, tx_key->key, tx_key->keylen);

		if (tx_key->keylen == WLAN_KEY_LEN_WEP40) {
			memcpy(key_buffer + 8, iv, 3);
			memcpy(key_buffer + 11,