rxtx.c 31.3 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
 *      vnt_generate_tx_parameter - Generate tx dma required parameter.
29
 *      vnt_get_duration_le - get tx data required duration
30
 *      vnt_get_rtscts_duration_le- get rtx/cts required duration
31
 *      vnt_get_rtscts_rsvtime_le- get rts/cts reserved time
32
 *      vnt_get_rsvtime- get frame reserved time
33
 *      vnt_fill_cts_head- fulfill CTS ctl header
34 35 36 37 38 39 40 41 42 43 44
 *
 * Revision History:
 *
 */

#include "device.h"
#include "rxtx.h"
#include "card.h"
#include "mac.h"
#include "rf.h"
#include "usbpipe.h"
45

46 47 48 49
static const u16 vnt_time_stampoff[2][MAX_RATE] = {
	{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 */
};
50

51 52 53 54 55
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 */
};

56 57 58 59
static const u16 vnt_fb_opt1[2][5] = {
	{RATE_12M, RATE_18M, RATE_24M, RATE_24M, RATE_36M}, /* fallback_rate0 */
	{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

#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

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

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

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

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

			context->hdr = NULL;

96 97 98 99
			return context;
		}
	}

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

103
	return NULL;
104 105
}

106
static __le16 vnt_time_stamp_off(struct vnt_private *priv, u16 rate)
107
{
108
	return cpu_to_le16(vnt_time_stampoff[priv->preamble_type % 2]
109 110 111
							[rate % MAX_RATE]);
}

112
static u32 vnt_get_rsvtime(struct vnt_private *priv, u8 pkt_type,
113
	u32 frame_length, u16 rate, int need_ack)
114
{
115
	u32 data_time, ack_time;
116

117
	data_time = vnt_get_frame_time(priv->preamble_type, pkt_type,
118
							frame_length, rate);
119

120
	if (pkt_type == PK_TYPE_11B)
121
		ack_time = vnt_get_frame_time(priv->preamble_type, pkt_type,
122
					14, (u16)priv->top_cck_basic_rate);
123
	else
124
		ack_time = vnt_get_frame_time(priv->preamble_type, pkt_type,
125
					14, (u16)priv->top_ofdm_basic_rate);
126 127

	if (need_ack)
128
		return data_time + priv->sifs + ack_time;
129 130

	return data_time;
131 132
}

133
static __le16 vnt_rxtx_rsvtime_le16(struct vnt_private *priv, u8 pkt_type,
134 135
	u32 frame_length, u16 rate, int need_ack)
{
136
	return cpu_to_le16((u16)vnt_get_rsvtime(priv, pkt_type,
137 138 139
		frame_length, rate, need_ack));
}

140
static __le16 vnt_get_rtscts_rsvtime_le(struct vnt_private *priv,
141
	u8 rsv_type, u8 pkt_type, u32 frame_length, u16 current_rate)
142
{
143
	u32 rrv_time, rts_time, cts_time, ack_time, data_time;
144

145
	rrv_time = rts_time = cts_time = ack_time = data_time = 0;
146

147
	data_time = vnt_get_frame_time(priv->preamble_type, pkt_type,
148
						frame_length, current_rate);
149 150

	if (rsv_type == 0) {
151
		rts_time = vnt_get_frame_time(priv->preamble_type,
152
			pkt_type, 20, priv->top_cck_basic_rate);
153
		cts_time = ack_time = vnt_get_frame_time(priv->preamble_type,
154
			pkt_type, 14, priv->top_cck_basic_rate);
155
	} else if (rsv_type == 1) {
156
		rts_time = vnt_get_frame_time(priv->preamble_type,
157
			pkt_type, 20, priv->top_cck_basic_rate);
158
		cts_time = vnt_get_frame_time(priv->preamble_type, pkt_type,
159
			14, priv->top_cck_basic_rate);
160
		ack_time = vnt_get_frame_time(priv->preamble_type, pkt_type,
161
			14, priv->top_ofdm_basic_rate);
162
	} else if (rsv_type == 2) {
163
		rts_time = vnt_get_frame_time(priv->preamble_type, pkt_type,
164
			20, priv->top_ofdm_basic_rate);
165
		cts_time = ack_time = vnt_get_frame_time(priv->preamble_type,
166
			pkt_type, 14, priv->top_ofdm_basic_rate);
167
	} else if (rsv_type == 3) {
168
		cts_time = vnt_get_frame_time(priv->preamble_type, pkt_type,
169
			14, priv->top_cck_basic_rate);
170
		ack_time = vnt_get_frame_time(priv->preamble_type, pkt_type,
171
			14, priv->top_ofdm_basic_rate);
172

173
		rrv_time = cts_time + ack_time + data_time + 2 * priv->sifs;
174

175
		return cpu_to_le16((u16)rrv_time);
176 177
	}

178
	rrv_time = rts_time + cts_time + ack_time + data_time + 3 * priv->sifs;
179

180
	return cpu_to_le16((u16)rrv_time);
181 182
}

183
static __le16 vnt_get_duration_le(struct vnt_private *priv,
184
					u8 pkt_type, int need_ack)
185
{
186
	u32 ack_time = 0;
187

188 189
	if (need_ack) {
		if (pkt_type == PK_TYPE_11B)
190 191
			ack_time = vnt_get_frame_time(priv->preamble_type,
				pkt_type, 14, priv->top_cck_basic_rate);
192
		else
193 194
			ack_time = vnt_get_frame_time(priv->preamble_type,
				pkt_type, 14, priv->top_ofdm_basic_rate);
195

196
		return cpu_to_le16((u16)(priv->sifs + ack_time));
197
	}
198 199 200 201

	return 0;
}

202 203
static __le16 vnt_get_rtscts_duration_le(struct vnt_usb_send_context *context,
					 u8 dur_type, u8 pkt_type, u16 rate)
204
{
205
	struct vnt_private *priv = context->priv;
206
	u32 cts_time = 0, dur_time = 0;
207 208
	u32 frame_length = context->frame_len;
	u8 need_ack = context->need_ack;
209

210
	switch (dur_type) {
211 212 213 214
	case RTSDUR_BB:
	case RTSDUR_BA:
	case RTSDUR_BA_F0:
	case RTSDUR_BA_F1:
215
		cts_time = vnt_get_frame_time(priv->preamble_type,
216
				pkt_type, 14, priv->top_cck_basic_rate);
217
		dur_time = cts_time + 2 * priv->sifs +
218
			vnt_get_rsvtime(priv, pkt_type,
219
						frame_length, rate, need_ack);
220
		break;
221

222 223 224
	case RTSDUR_AA:
	case RTSDUR_AA_F0:
	case RTSDUR_AA_F1:
225
		cts_time = vnt_get_frame_time(priv->preamble_type,
226
				pkt_type, 14, priv->top_ofdm_basic_rate);
227
		dur_time = cts_time + 2 * priv->sifs +
228
			vnt_get_rsvtime(priv, pkt_type,
229
						frame_length, rate, need_ack);
230
		break;
231

232 233 234
	case CTSDUR_BA:
	case CTSDUR_BA_F0:
	case CTSDUR_BA_F1:
235
		dur_time = priv->sifs + vnt_get_rsvtime(priv,
236
				pkt_type, frame_length, rate, need_ack);
237
		break;
238

239 240 241
	default:
		break;
	}
242

243
	return cpu_to_le16((u16)dur_time);
244 245
}

246 247 248 249 250 251 252 253 254 255 256 257 258
static u16 vnt_mac_hdr_pos(struct vnt_usb_send_context *tx_context,
	struct ieee80211_hdr *hdr)
{
	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);
}

259
static u16 vnt_rxtx_datahead_g(struct vnt_usb_send_context *tx_context,
260
			       struct vnt_tx_datahead_g *buf)
261
{
262 263

	struct vnt_private *priv = tx_context->priv;
264 265
	struct ieee80211_hdr *hdr =
				(struct ieee80211_hdr *)tx_context->skb->data;
266
	u32 frame_len = tx_context->frame_len;
267
	u16 rate = tx_context->tx_rate;
268
	u8 need_ack = tx_context->need_ack;
269

270
	/* Get SignalField,ServiceField,Length */
271
	vnt_get_phy_field(priv, frame_len, rate, tx_context->pkt_type, &buf->a);
272
	vnt_get_phy_field(priv, frame_len, priv->top_cck_basic_rate,
273 274 275
							PK_TYPE_11B, &buf->b);

	/* Get Duration and TimeStamp */
276 277 278 279 280 281
	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 {
282 283
		buf->duration_a = vnt_get_duration_le(priv,
						tx_context->pkt_type, need_ack);
284
		buf->duration_b = vnt_get_duration_le(priv,
285 286
							PK_TYPE_11B, need_ack);
	}
287

288 289
	buf->time_stamp_off_a = vnt_time_stamp_off(priv, rate);
	buf->time_stamp_off_b = vnt_time_stamp_off(priv,
290
					priv->top_cck_basic_rate);
291

292 293
	tx_context->tx_hdr_size = vnt_mac_hdr_pos(tx_context, &buf->hdr);

294
	return le16_to_cpu(buf->duration_a);
295 296
}

297
static u16 vnt_rxtx_datahead_g_fb(struct vnt_usb_send_context *tx_context,
298
				  struct vnt_tx_datahead_g_fb *buf)
299
{
300
	struct vnt_private *priv = tx_context->priv;
301
	u32 frame_len = tx_context->frame_len;
302
	u16 rate = tx_context->tx_rate;
303
	u8 need_ack = tx_context->need_ack;
304

305
	/* Get SignalField,ServiceField,Length */
306
	vnt_get_phy_field(priv, frame_len, rate, tx_context->pkt_type, &buf->a);
307

308
	vnt_get_phy_field(priv, frame_len, priv->top_cck_basic_rate,
309 310 311
						PK_TYPE_11B, &buf->b);

	/* Get Duration and TimeStamp */
312 313
	buf->duration_a = vnt_get_duration_le(priv, tx_context->pkt_type,
					      need_ack);
314
	buf->duration_b = vnt_get_duration_le(priv, PK_TYPE_11B, need_ack);
315

316 317 318 319
	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);
320

321 322
	buf->time_stamp_off_a = vnt_time_stamp_off(priv, rate);
	buf->time_stamp_off_b = vnt_time_stamp_off(priv,
323
						priv->top_cck_basic_rate);
324

325 326
	tx_context->tx_hdr_size = vnt_mac_hdr_pos(tx_context, &buf->hdr);

327
	return le16_to_cpu(buf->duration_a);
328 329
}

330
static u16 vnt_rxtx_datahead_a_fb(struct vnt_usb_send_context *tx_context,
331
				  struct vnt_tx_datahead_a_fb *buf)
332
{
333
	struct vnt_private *priv = tx_context->priv;
334
	u16 rate = tx_context->tx_rate;
335
	u8 pkt_type = tx_context->pkt_type;
336
	u8 need_ack = tx_context->need_ack;
337
	u32 frame_len = tx_context->frame_len;
338

339
	/* Get SignalField,ServiceField,Length */
340
	vnt_get_phy_field(priv, frame_len, rate, pkt_type, &buf->a);
341
	/* Get Duration and TimeStampOff */
342
	buf->duration = vnt_get_duration_le(priv, pkt_type, need_ack);
343

344 345
	buf->duration_f0 = vnt_get_duration_le(priv, pkt_type, need_ack);
	buf->duration_f1 = vnt_get_duration_le(priv, pkt_type, need_ack);
346

347
	buf->time_stamp_off = vnt_time_stamp_off(priv, rate);
348

349 350
	tx_context->tx_hdr_size = vnt_mac_hdr_pos(tx_context, &buf->hdr);

351
	return le16_to_cpu(buf->duration);
352 353
}

354
static u16 vnt_rxtx_datahead_ab(struct vnt_usb_send_context *tx_context,
355
				struct vnt_tx_datahead_ab *buf)
356
{
357
	struct vnt_private *priv = tx_context->priv;
358 359
	struct ieee80211_hdr *hdr =
				(struct ieee80211_hdr *)tx_context->skb->data;
360
	u32 frame_len = tx_context->frame_len;
361
	u16 rate = tx_context->tx_rate;
362
	u8 need_ack = tx_context->need_ack;
363

364
	/* Get SignalField,ServiceField,Length */
365 366
	vnt_get_phy_field(priv, frame_len, rate,
			  tx_context->pkt_type, &buf->ab);
367

368
	/* Get Duration and TimeStampOff */
369 370 371 372 373
	if (ieee80211_is_pspoll(hdr->frame_control)) {
		__le16 dur = cpu_to_le16(priv->current_aid | BIT(14) | BIT(15));

		buf->duration = dur;
	} else {
374 375
		buf->duration = vnt_get_duration_le(priv, tx_context->pkt_type,
						    need_ack);
376
	}
377

378
	buf->time_stamp_off = vnt_time_stamp_off(priv, rate);
379

380 381
	tx_context->tx_hdr_size = vnt_mac_hdr_pos(tx_context, &buf->hdr);

382
	return le16_to_cpu(buf->duration);
383 384
}

385 386
static int vnt_fill_ieee80211_rts(struct vnt_usb_send_context *tx_context,
	struct ieee80211_rts *rts, __le16 duration)
387
{
388 389 390
	struct ieee80211_hdr *hdr =
				(struct ieee80211_hdr *)tx_context->skb->data;

391
	rts->duration = duration;
392 393
	rts->frame_control =
		cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS);
394

395 396
	memcpy(rts->ra, hdr->addr1, ETH_ALEN);
	memcpy(rts->ta, hdr->addr2, ETH_ALEN);
397 398 399 400

	return 0;
}

401
static u16 vnt_rxtx_rts_g_head(struct vnt_usb_send_context *tx_context,
402
			       struct vnt_rts_g *buf)
403
{
404
	struct vnt_private *priv = tx_context->priv;
405
	u16 rts_frame_len = 20;
406
	u16 current_rate = tx_context->tx_rate;
407

408
	vnt_get_phy_field(priv, rts_frame_len, priv->top_cck_basic_rate,
409
		PK_TYPE_11B, &buf->b);
410 411
	vnt_get_phy_field(priv, rts_frame_len, priv->top_ofdm_basic_rate,
			  tx_context->pkt_type, &buf->a);
412

413 414 415 416 417 418 419 420 421
	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);
422

423
	vnt_fill_ieee80211_rts(tx_context, &buf->data, buf->duration_aa);
424

425
	return vnt_rxtx_datahead_g(tx_context, &buf->data_head);
426 427
}

428
static u16 vnt_rxtx_rts_g_fb_head(struct vnt_usb_send_context *tx_context,
429
				  struct vnt_rts_g_fb *buf)
430
{
431
	struct vnt_private *priv = tx_context->priv;
432
	u16 current_rate = tx_context->tx_rate;
433
	u16 rts_frame_len = 20;
434

435
	vnt_get_phy_field(priv, rts_frame_len, priv->top_cck_basic_rate,
436
		PK_TYPE_11B, &buf->b);
437 438
	vnt_get_phy_field(priv, rts_frame_len, priv->top_ofdm_basic_rate,
			  tx_context->pkt_type, &buf->a);
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
	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);
466

467
	vnt_fill_ieee80211_rts(tx_context, &buf->data, buf->duration_aa);
468

469
	return vnt_rxtx_datahead_g_fb(tx_context, &buf->data_head);
470 471
}

472
static u16 vnt_rxtx_rts_ab_head(struct vnt_usb_send_context *tx_context,
473
				struct vnt_rts_ab *buf)
474
{
475
	struct vnt_private *priv = tx_context->priv;
476
	u16 current_rate = tx_context->tx_rate;
477 478
	u16 rts_frame_len = 20;

479 480
	vnt_get_phy_field(priv, rts_frame_len, priv->top_ofdm_basic_rate,
			  tx_context->pkt_type, &buf->ab);
481

482
	buf->duration = vnt_get_rtscts_duration_le(tx_context, RTSDUR_AA,
483
						   tx_context->pkt_type,
484
						   current_rate);
485

486
	vnt_fill_ieee80211_rts(tx_context, &buf->data, buf->duration);
487

488
	return vnt_rxtx_datahead_ab(tx_context, &buf->data_head);
489 490
}

491
static u16 vnt_rxtx_rts_a_fb_head(struct vnt_usb_send_context *tx_context,
492
				  struct vnt_rts_a_fb *buf)
493
{
494
	struct vnt_private *priv = tx_context->priv;
495
	u16 current_rate = tx_context->tx_rate;
496
	u16 rts_frame_len = 20;
497

498
	vnt_get_phy_field(priv, rts_frame_len,
499
		priv->top_ofdm_basic_rate, tx_context->pkt_type, &buf->a);
500

501
	buf->duration = vnt_get_rtscts_duration_le(tx_context, RTSDUR_AA,
502
						   tx_context->pkt_type,
503
						   current_rate);
504

505 506 507 508
	buf->rts_duration_f0 =
		vnt_get_rtscts_duration_le(tx_context, RTSDUR_AA_F0,
					   tx_context->pkt_type,
					   priv->tx_rate_fb0);
509

510 511 512 513
	buf->rts_duration_f1 =
		vnt_get_rtscts_duration_le(tx_context, RTSDUR_AA_F1,
					   tx_context->pkt_type,
					   priv->tx_rate_fb1);
514

515
	vnt_fill_ieee80211_rts(tx_context, &buf->data, buf->duration);
516

517
	return vnt_rxtx_datahead_a_fb(tx_context, &buf->data_head);
518 519
}

520
static u16 vnt_fill_cts_head(struct vnt_usb_send_context *tx_context,
521
	union vnt_tx_data_head *head)
522
{
523 524
	struct vnt_private *priv = tx_context->priv;
	u32 cts_frame_len = 14;
525
	u16 current_rate = tx_context->tx_rate;
526

527
	if (!head)
528
		return 0;
529

530
	if (tx_context->fb_option) {
531
		/* Auto Fall back */
532
		struct vnt_cts_fb *buf = &head->cts_g_fb;
533
		/* Get SignalField,ServiceField,Length */
534
		vnt_get_phy_field(priv, cts_frame_len,
535
			priv->top_cck_basic_rate, PK_TYPE_11B, &buf->b);
536 537 538 539
		buf->duration_ba =
			vnt_get_rtscts_duration_le(tx_context, CTSDUR_BA,
						   tx_context->pkt_type,
						   current_rate);
540
		/* Get CTSDuration_ba_f0 */
541 542 543 544
		buf->cts_duration_ba_f0 =
			vnt_get_rtscts_duration_le(tx_context, CTSDUR_BA_F0,
						   tx_context->pkt_type,
						   priv->tx_rate_fb0);
545
		/* Get CTSDuration_ba_f1 */
546 547 548 549
		buf->cts_duration_ba_f1 =
			vnt_get_rtscts_duration_le(tx_context, CTSDUR_BA_F1,
						   tx_context->pkt_type,
						   priv->tx_rate_fb1);
550
		/* Get CTS Frame body */
551 552
		buf->data.duration = buf->duration_ba;
		buf->data.frame_control =
553 554
			cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS);

555
		memcpy(buf->data.ra, priv->current_net_addr, ETH_ALEN);
556

557
		return vnt_rxtx_datahead_g_fb(tx_context, &buf->data_head);
558
	} else {
559
		struct vnt_cts *buf = &head->cts_g;
560
		/* Get SignalField,ServiceField,Length */
561
		vnt_get_phy_field(priv, cts_frame_len,
562
			priv->top_cck_basic_rate, PK_TYPE_11B, &buf->b);
563
		/* Get CTSDuration_ba */
564 565 566 567
		buf->duration_ba =
			vnt_get_rtscts_duration_le(tx_context, CTSDUR_BA,
						   tx_context->pkt_type,
						   current_rate);
568
		/*Get CTS Frame body*/
569 570
		buf->data.duration = buf->duration_ba;
		buf->data.frame_control =
571 572
			cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS);

573
		memcpy(buf->data.ra, priv->current_net_addr, ETH_ALEN);
574

575
		return vnt_rxtx_datahead_g(tx_context, &buf->data_head);
576
        }
577 578

	return 0;
579 580
}

581
static u16 vnt_rxtx_rts(struct vnt_usb_send_context *tx_context,
582
			union vnt_tx_head *tx_head, bool need_mic)
583 584 585
{
	struct vnt_private *priv = tx_context->priv;
	struct vnt_rrv_time_rts *buf = &tx_head->tx_rts.rts;
586
	union vnt_tx_data_head *head = &tx_head->tx_rts.tx.head;
587
	u32 frame_len = tx_context->frame_len;
588
	u16 current_rate = tx_context->tx_rate;
589
	u8 need_ack = tx_context->need_ack;
590

591
	buf->rts_rrv_time_aa = vnt_get_rtscts_rsvtime_le(priv, 2,
592
			tx_context->pkt_type, frame_len, current_rate);
593
	buf->rts_rrv_time_ba = vnt_get_rtscts_rsvtime_le(priv, 1,
594
			tx_context->pkt_type, frame_len, current_rate);
595
	buf->rts_rrv_time_bb = vnt_get_rtscts_rsvtime_le(priv, 0,
596
			tx_context->pkt_type, frame_len, current_rate);
597

598
	buf->rrv_time_a = vnt_rxtx_rsvtime_le16(priv, tx_context->pkt_type,
599
						frame_len, current_rate,
600
						need_ack);
601
	buf->rrv_time_b = vnt_rxtx_rsvtime_le16(priv, PK_TYPE_11B, frame_len,
602
					priv->top_cck_basic_rate, need_ack);
603

604 605 606 607
	if (need_mic)
		head = &tx_head->tx_rts.tx.mic.head;

	if (tx_context->fb_option)
608
		return vnt_rxtx_rts_g_fb_head(tx_context, &head->rts_g_fb);
609

610
	return vnt_rxtx_rts_g_head(tx_context, &head->rts_g);
611 612
}

613
static u16 vnt_rxtx_cts(struct vnt_usb_send_context *tx_context,
614
			union vnt_tx_head *tx_head, bool need_mic)
615 616 617
{
	struct vnt_private *priv = tx_context->priv;
	struct vnt_rrv_time_cts *buf = &tx_head->tx_cts.cts;
618
	union vnt_tx_data_head *head = &tx_head->tx_cts.tx.head;
619
	u32 frame_len = tx_context->frame_len;
620
	u16 current_rate = tx_context->tx_rate;
621 622
	u8 need_ack = tx_context->need_ack;

623
	buf->rrv_time_a = vnt_rxtx_rsvtime_le16(priv, tx_context->pkt_type,
624
					frame_len, current_rate, need_ack);
625
	buf->rrv_time_b = vnt_rxtx_rsvtime_le16(priv, PK_TYPE_11B,
626
				frame_len, priv->top_cck_basic_rate, need_ack);
627

628
	buf->cts_rrv_time_ba = vnt_get_rtscts_rsvtime_le(priv, 3,
629
			tx_context->pkt_type, frame_len, current_rate);
630

631 632 633 634
	if (need_mic)
		head = &tx_head->tx_cts.tx.mic.head;

	/* Fill CTS */
635
	return vnt_fill_cts_head(tx_context, head);
636 637
}

638
static u16 vnt_rxtx_ab(struct vnt_usb_send_context *tx_context,
639
		       union vnt_tx_head *tx_head, bool need_rts, bool need_mic)
640 641 642
{
	struct vnt_private *priv = tx_context->priv;
	struct vnt_rrv_time_ab *buf = &tx_head->tx_ab.ab;
643
	union vnt_tx_data_head *head = &tx_head->tx_ab.tx.head;
644
	u32 frame_len = tx_context->frame_len;
645
	u16 current_rate = tx_context->tx_rate;
646
	u8 need_ack = tx_context->need_ack;
647

648
	buf->rrv_time = vnt_rxtx_rsvtime_le16(priv, tx_context->pkt_type,
649
			frame_len, current_rate, need_ack);
650

651 652 653
	if (need_mic)
		head = &tx_head->tx_ab.tx.mic.head;

654
	if (need_rts) {
655
		if (tx_context->pkt_type == PK_TYPE_11B)
656
			buf->rts_rrv_time = vnt_get_rtscts_rsvtime_le(priv, 0,
657
				tx_context->pkt_type, frame_len, current_rate);
658
		else /* PK_TYPE_11A */
659
			buf->rts_rrv_time = vnt_get_rtscts_rsvtime_le(priv, 2,
660
				tx_context->pkt_type, frame_len, current_rate);
661

662 663
		if (tx_context->fb_option &&
		    tx_context->pkt_type == PK_TYPE_11A)
664
			return vnt_rxtx_rts_a_fb_head(tx_context,
665
						      &head->rts_a_fb);
666

667
		return vnt_rxtx_rts_ab_head(tx_context, &head->rts_ab);
668 669
	}

670
	if (tx_context->pkt_type == PK_TYPE_11A)
671
		return vnt_rxtx_datahead_a_fb(tx_context,
672
					      &head->data_head_a_fb);
673

674
	return vnt_rxtx_datahead_ab(tx_context, &head->data_head_ab);
675 676
}

677
static u16 vnt_generate_tx_parameter(struct vnt_usb_send_context *tx_context,
678
	struct vnt_tx_buffer *tx_buffer,
679
	struct vnt_mic_hdr **mic_hdr, u32 need_mic,
680
	bool need_rts)
681 682
{

683 684
	if (tx_context->pkt_type == PK_TYPE_11GB ||
	    tx_context->pkt_type == PK_TYPE_11GA) {
685
		if (need_rts) {
686
			if (need_mic)
687 688
				*mic_hdr = &tx_buffer->
						tx_head.tx_rts.tx.mic.hdr;
689

690
			return vnt_rxtx_rts(tx_context, &tx_buffer->tx_head,
691
					    need_mic);
692
		}
693

694
		if (need_mic)
695
			*mic_hdr = &tx_buffer->tx_head.tx_cts.tx.mic.hdr;
696

697
		return vnt_rxtx_cts(tx_context, &tx_buffer->tx_head, need_mic);
698
	}
699

700 701 702
	if (need_mic)
		*mic_hdr = &tx_buffer->tx_head.tx_ab.tx.mic.hdr;

703
	return vnt_rxtx_ab(tx_context, &tx_buffer->tx_head, need_rts, need_mic);
704
}
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745

static void vnt_fill_txkey(struct vnt_usb_send_context *tx_context,
	u8 *key_buffer, struct ieee80211_key_conf *tx_key, struct sk_buff *skb,
	u16 payload_len, struct vnt_mic_hdr *mic_hdr)
{
	struct ieee80211_hdr *hdr = tx_context->hdr;
	struct ieee80211_key_seq seq;
	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,
					tx_key->key, WLAN_KEY_LEN_WEP40);
		}

		break;
	case WLAN_CIPHER_SUITE_TKIP:
		ieee80211_get_tkip_p2k(tx_key, skb, key_buffer);

		break;
	case WLAN_CIPHER_SUITE_CCMP:

		if (!mic_hdr)
			return;

		mic_hdr->id = 0x59;
		mic_hdr->payload_len = cpu_to_be16(payload_len);
		memcpy(mic_hdr->mic_addr2, hdr->addr2, ETH_ALEN);

		ieee80211_get_key_tx_seq(tx_key, &seq);

746
		memcpy(mic_hdr->ccmp_pn, seq.ccmp.pn, IEEE80211_CCMP_PN_LEN);
747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788

		if (ieee80211_has_a4(hdr->frame_control))
			mic_hdr->hlen = cpu_to_be16(28);
		else
			mic_hdr->hlen = cpu_to_be16(22);

		memcpy(mic_hdr->addr1, hdr->addr1, ETH_ALEN);
		memcpy(mic_hdr->addr2, hdr->addr2, ETH_ALEN);
		memcpy(mic_hdr->addr3, hdr->addr3, ETH_ALEN);

		mic_hdr->frame_control = cpu_to_le16(
			le16_to_cpu(hdr->frame_control) & 0xc78f);
		mic_hdr->seq_ctrl = cpu_to_le16(
				le16_to_cpu(hdr->seq_ctrl) & 0xf);

		if (ieee80211_has_a4(hdr->frame_control))
			memcpy(mic_hdr->addr4, hdr->addr4, ETH_ALEN);


		memcpy(key_buffer, tx_key->key, WLAN_KEY_LEN_CCMP);

		break;
	default:
		break;
	}

}

int vnt_tx_packet(struct vnt_private *priv, struct sk_buff *skb)
{
	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
	struct ieee80211_tx_rate *tx_rate = &info->control.rates[0];
	struct ieee80211_rate *rate;
	struct ieee80211_key_conf *tx_key;
	struct ieee80211_hdr *hdr;
	struct vnt_mic_hdr *mic_hdr = NULL;
	struct vnt_tx_buffer *tx_buffer;
	struct vnt_tx_fifo_head *tx_buffer_head;
	struct vnt_usb_send_context *tx_context;
	unsigned long flags;
	u16 tx_bytes, tx_header_size, tx_body_size, current_rate, duration_id;
	u8 pkt_type, fb_option = AUTO_FB_NONE;
789
	bool need_rts = false, is_pspoll = false;
790 791 792 793 794 795 796
	bool need_mic = false;

	hdr = (struct ieee80211_hdr *)(skb->data);

	rate = ieee80211_get_tx_rate(priv->hw, info);

	current_rate = rate->hw_value;
797
	if (priv->current_rate != current_rate &&
798
			!(priv->hw->conf.flags & IEEE80211_CONF_OFFCHANNEL)) {
799
		priv->current_rate = current_rate;
800
		vnt_schedule_command(priv, WLAN_CMD_SETPOWER);
801 802 803
	}

	if (current_rate > RATE_11M)
804
		pkt_type = priv->packet_type;
805 806 807 808 809
	else
		pkt_type = PK_TYPE_11B;

	spin_lock_irqsave(&priv->lock, flags);

810
	tx_context = vnt_get_free_context(priv);
811 812 813 814 815 816 817
	if (!tx_context) {
		dev_dbg(&priv->usb->dev, "%s No free context\n", __func__);
		spin_unlock_irqrestore(&priv->lock, flags);
		return -ENOMEM;
	}

	tx_context->skb = skb;
818
	tx_context->pkt_type = pkt_type;
819
	tx_context->need_ack = false;
820
	tx_context->frame_len = skb->len + 4;
821
	tx_context->tx_rate = current_rate;
822 823 824 825 826 827 828 829 830

	spin_unlock_irqrestore(&priv->lock, flags);

	tx_buffer = (struct vnt_tx_buffer *)tx_context->data;
	tx_buffer_head = &tx_buffer->fifo_head;
	tx_body_size = skb->len;

	/*Set fifo controls */
	if (pkt_type == PK_TYPE_11A)
831
		tx_buffer_head->fifo_ctl = 0;
832
	else if (pkt_type == PK_TYPE_11B)
Malcolm Priestley's avatar