1/*
2 * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
3 * All rights reserved.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * File: rxtx.c
20 *
21 * Purpose: handle WMAC/802.3/802.11 rx & tx functions
22 *
23 * Author: Lyndon Chen
24 *
25 * Date: May 20, 2003
26 *
27 * Functions:
28 *      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 * Revision History:
36 *
37 */
38
39#include <linux/etherdevice.h>
40#include "device.h"
41#include "rxtx.h"
42#include "card.h"
43#include "mac.h"
44#include "rf.h"
45#include "usbpipe.h"
46
47static const u16 vnt_time_stampoff[2][MAX_RATE] = {
48	{384, 288, 226, 209, 54, 43, 37, 31, 28, 25, 24, 23},/* Long Preamble */
49	{384, 192, 130, 113, 54, 43, 37, 31, 28, 25, 24, 23},/* Short Preamble */
50};
51
52static const u16 vnt_fb_opt0[2][5] = {
53	{RATE_12M, RATE_18M, RATE_24M, RATE_36M, RATE_48M}, /* fallback_rate0 */
54	{RATE_12M, RATE_12M, RATE_18M, RATE_24M, RATE_36M}, /* fallback_rate1 */
55};
56
57static const u16 vnt_fb_opt1[2][5] = {
58	{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#define RTSDUR_BB       0
63#define RTSDUR_BA       1
64#define RTSDUR_AA       2
65#define CTSDUR_BA       3
66#define RTSDUR_BA_F0    4
67#define RTSDUR_AA_F0    5
68#define RTSDUR_BA_F1    6
69#define RTSDUR_AA_F1    7
70#define CTSDUR_BA_F0    8
71#define CTSDUR_BA_F1    9
72#define DATADUR_B       10
73#define DATADUR_A       11
74#define DATADUR_A_F0    12
75#define DATADUR_A_F1    13
76
77static 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			return NULL;
88
89		context = priv->tx_context[ii];
90		if (context->in_use == false) {
91			context->in_use = true;
92			memset(context->data, 0,
93					MAX_TOTAL_SIZE_WITH_ALL_HEADERS);
94
95			context->hdr = NULL;
96
97			return context;
98		}
99	}
100
101	if (ii == priv->num_tx_context)
102		dev_dbg(&priv->usb->dev, "%s No Free Tx Context\n", __func__);
103
104	return NULL;
105}
106
107static __le16 vnt_time_stamp_off(struct vnt_private *priv, u16 rate)
108{
109	return cpu_to_le16(vnt_time_stampoff[priv->preamble_type % 2]
110							[rate % MAX_RATE]);
111}
112
113static u32 vnt_get_rsvtime(struct vnt_private *priv, u8 pkt_type,
114	u32 frame_length, u16 rate, int need_ack)
115{
116	u32 data_time, ack_time;
117
118	data_time = vnt_get_frame_time(priv->preamble_type, pkt_type,
119							frame_length, rate);
120
121	if (pkt_type == PK_TYPE_11B)
122		ack_time = vnt_get_frame_time(priv->preamble_type, pkt_type,
123					14, (u16)priv->top_cck_basic_rate);
124	else
125		ack_time = vnt_get_frame_time(priv->preamble_type, pkt_type,
126					14, (u16)priv->top_ofdm_basic_rate);
127
128	if (need_ack)
129		return data_time + priv->sifs + ack_time;
130
131	return data_time;
132}
133
134static __le16 vnt_rxtx_rsvtime_le16(struct vnt_private *priv, u8 pkt_type,
135	u32 frame_length, u16 rate, int need_ack)
136{
137	return cpu_to_le16((u16)vnt_get_rsvtime(priv, pkt_type,
138		frame_length, rate, need_ack));
139}
140
141static __le16 vnt_get_rtscts_rsvtime_le(struct vnt_private *priv,
142	u8 rsv_type, u8 pkt_type, u32 frame_length, u16 current_rate)
143{
144	u32 rrv_time, rts_time, cts_time, ack_time, data_time;
145
146	rrv_time = rts_time = cts_time = ack_time = data_time = 0;
147
148	data_time = vnt_get_frame_time(priv->preamble_type, pkt_type,
149						frame_length, current_rate);
150
151	if (rsv_type == 0) {
152		rts_time = vnt_get_frame_time(priv->preamble_type,
153			pkt_type, 20, priv->top_cck_basic_rate);
154		cts_time = ack_time = vnt_get_frame_time(priv->preamble_type,
155			pkt_type, 14, priv->top_cck_basic_rate);
156	} else if (rsv_type == 1) {
157		rts_time = vnt_get_frame_time(priv->preamble_type,
158			pkt_type, 20, priv->top_cck_basic_rate);
159		cts_time = vnt_get_frame_time(priv->preamble_type, pkt_type,
160			14, priv->top_cck_basic_rate);
161		ack_time = vnt_get_frame_time(priv->preamble_type, pkt_type,
162			14, priv->top_ofdm_basic_rate);
163	} else if (rsv_type == 2) {
164		rts_time = vnt_get_frame_time(priv->preamble_type, pkt_type,
165			20, priv->top_ofdm_basic_rate);
166		cts_time = ack_time = vnt_get_frame_time(priv->preamble_type,
167			pkt_type, 14, priv->top_ofdm_basic_rate);
168	} else if (rsv_type == 3) {
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
174		rrv_time = cts_time + ack_time + data_time + 2 * priv->sifs;
175
176		return cpu_to_le16((u16)rrv_time);
177	}
178
179	rrv_time = rts_time + cts_time + ack_time + data_time + 3 * priv->sifs;
180
181	return cpu_to_le16((u16)rrv_time);
182}
183
184static __le16 vnt_get_duration_le(struct vnt_private *priv,
185					u8 pkt_type, int need_ack)
186{
187	u32 ack_time = 0;
188
189	if (need_ack) {
190		if (pkt_type == PK_TYPE_11B)
191			ack_time = vnt_get_frame_time(priv->preamble_type,
192				pkt_type, 14, priv->top_cck_basic_rate);
193		else
194			ack_time = vnt_get_frame_time(priv->preamble_type,
195				pkt_type, 14, priv->top_ofdm_basic_rate);
196
197		return cpu_to_le16((u16)(priv->sifs + ack_time));
198	}
199
200	return 0;
201}
202
203static __le16 vnt_get_rtscts_duration_le(struct vnt_usb_send_context *context,
204					 u8 dur_type, u8 pkt_type, u16 rate)
205{
206	struct vnt_private *priv = context->priv;
207	u32 cts_time = 0, dur_time = 0;
208	u32 frame_length = context->frame_len;
209	u8 need_ack = context->need_ack;
210
211	switch (dur_type) {
212	case RTSDUR_BB:
213	case RTSDUR_BA:
214	case RTSDUR_BA_F0:
215	case RTSDUR_BA_F1:
216		cts_time = vnt_get_frame_time(priv->preamble_type,
217				pkt_type, 14, priv->top_cck_basic_rate);
218		dur_time = cts_time + 2 * priv->sifs +
219			vnt_get_rsvtime(priv, pkt_type,
220						frame_length, rate, need_ack);
221		break;
222
223	case RTSDUR_AA:
224	case RTSDUR_AA_F0:
225	case RTSDUR_AA_F1:
226		cts_time = vnt_get_frame_time(priv->preamble_type,
227				pkt_type, 14, priv->top_ofdm_basic_rate);
228		dur_time = cts_time + 2 * priv->sifs +
229			vnt_get_rsvtime(priv, pkt_type,
230						frame_length, rate, need_ack);
231		break;
232
233	case CTSDUR_BA:
234	case CTSDUR_BA_F0:
235	case CTSDUR_BA_F1:
236		dur_time = priv->sifs + vnt_get_rsvtime(priv,
237				pkt_type, frame_length, rate, need_ack);
238		break;
239
240	default:
241		break;
242	}
243
244	return cpu_to_le16((u16)dur_time);
245}
246
247static u16 vnt_mac_hdr_pos(struct vnt_usb_send_context *tx_context,
248	struct ieee80211_hdr *hdr)
249{
250	u8 *head = tx_context->data + offsetof(struct vnt_tx_buffer, fifo_head);
251	u8 *hdr_pos = (u8 *)hdr;
252
253	tx_context->hdr = hdr;
254	if (!tx_context->hdr)
255		return 0;
256
257	return (u16)(hdr_pos - head);
258}
259
260static u16 vnt_rxtx_datahead_g(struct vnt_usb_send_context *tx_context,
261			       struct vnt_tx_datahead_g *buf)
262{
263
264	struct vnt_private *priv = tx_context->priv;
265	struct ieee80211_hdr *hdr =
266				(struct ieee80211_hdr *)tx_context->skb->data;
267	u32 frame_len = tx_context->frame_len;
268	u16 rate = tx_context->tx_rate;
269	u8 need_ack = tx_context->need_ack;
270
271	/* Get SignalField,ServiceField,Length */
272	vnt_get_phy_field(priv, frame_len, rate, tx_context->pkt_type, &buf->a);
273	vnt_get_phy_field(priv, frame_len, priv->top_cck_basic_rate,
274							PK_TYPE_11B, &buf->b);
275
276	/* Get Duration and TimeStamp */
277	if (ieee80211_is_pspoll(hdr->frame_control)) {
278		__le16 dur = cpu_to_le16(priv->current_aid | BIT(14) | BIT(15));
279
280		buf->duration_a = dur;
281		buf->duration_b = dur;
282	} else {
283		buf->duration_a = vnt_get_duration_le(priv,
284						tx_context->pkt_type, need_ack);
285		buf->duration_b = vnt_get_duration_le(priv,
286							PK_TYPE_11B, need_ack);
287	}
288
289	buf->time_stamp_off_a = vnt_time_stamp_off(priv, rate);
290	buf->time_stamp_off_b = vnt_time_stamp_off(priv,
291					priv->top_cck_basic_rate);
292
293	tx_context->tx_hdr_size = vnt_mac_hdr_pos(tx_context, &buf->hdr);
294
295	return le16_to_cpu(buf->duration_a);
296}
297
298static u16 vnt_rxtx_datahead_g_fb(struct vnt_usb_send_context *tx_context,
299				  struct vnt_tx_datahead_g_fb *buf)
300{
301	struct vnt_private *priv = tx_context->priv;
302	u32 frame_len = tx_context->frame_len;
303	u16 rate = tx_context->tx_rate;
304	u8 need_ack = tx_context->need_ack;
305
306	/* Get SignalField,ServiceField,Length */
307	vnt_get_phy_field(priv, frame_len, rate, tx_context->pkt_type, &buf->a);
308
309	vnt_get_phy_field(priv, frame_len, priv->top_cck_basic_rate,
310						PK_TYPE_11B, &buf->b);
311
312	/* Get Duration and TimeStamp */
313	buf->duration_a = vnt_get_duration_le(priv, tx_context->pkt_type,
314					      need_ack);
315	buf->duration_b = vnt_get_duration_le(priv, PK_TYPE_11B, need_ack);
316
317	buf->duration_a_f0 = vnt_get_duration_le(priv, tx_context->pkt_type,
318						 need_ack);
319	buf->duration_a_f1 = vnt_get_duration_le(priv, tx_context->pkt_type,
320						 need_ack);
321
322	buf->time_stamp_off_a = vnt_time_stamp_off(priv, rate);
323	buf->time_stamp_off_b = vnt_time_stamp_off(priv,
324						priv->top_cck_basic_rate);
325
326	tx_context->tx_hdr_size = vnt_mac_hdr_pos(tx_context, &buf->hdr);
327
328	return le16_to_cpu(buf->duration_a);
329}
330
331static u16 vnt_rxtx_datahead_a_fb(struct vnt_usb_send_context *tx_context,
332				  struct vnt_tx_datahead_a_fb *buf)
333{
334	struct vnt_private *priv = tx_context->priv;
335	u16 rate = tx_context->tx_rate;
336	u8 pkt_type = tx_context->pkt_type;
337	u8 need_ack = tx_context->need_ack;
338	u32 frame_len = tx_context->frame_len;
339
340	/* Get SignalField,ServiceField,Length */
341	vnt_get_phy_field(priv, frame_len, rate, pkt_type, &buf->a);
342	/* Get Duration and TimeStampOff */
343	buf->duration = vnt_get_duration_le(priv, pkt_type, need_ack);
344
345	buf->duration_f0 = vnt_get_duration_le(priv, pkt_type, need_ack);
346	buf->duration_f1 = vnt_get_duration_le(priv, pkt_type, need_ack);
347
348	buf->time_stamp_off = vnt_time_stamp_off(priv, rate);
349
350	tx_context->tx_hdr_size = vnt_mac_hdr_pos(tx_context, &buf->hdr);
351
352	return le16_to_cpu(buf->duration);
353}
354
355static u16 vnt_rxtx_datahead_ab(struct vnt_usb_send_context *tx_context,
356				struct vnt_tx_datahead_ab *buf)
357{
358	struct vnt_private *priv = tx_context->priv;
359	struct ieee80211_hdr *hdr =
360				(struct ieee80211_hdr *)tx_context->skb->data;
361	u32 frame_len = tx_context->frame_len;
362	u16 rate = tx_context->tx_rate;
363	u8 need_ack = tx_context->need_ack;
364
365	/* Get SignalField,ServiceField,Length */
366	vnt_get_phy_field(priv, frame_len, rate,
367			  tx_context->pkt_type, &buf->ab);
368
369	/* Get Duration and TimeStampOff */
370	if (ieee80211_is_pspoll(hdr->frame_control)) {
371		__le16 dur = cpu_to_le16(priv->current_aid | BIT(14) | BIT(15));
372
373		buf->duration = dur;
374	} else {
375		buf->duration = vnt_get_duration_le(priv, tx_context->pkt_type,
376						    need_ack);
377	}
378
379	buf->time_stamp_off = vnt_time_stamp_off(priv, rate);
380
381	tx_context->tx_hdr_size = vnt_mac_hdr_pos(tx_context, &buf->hdr);
382
383	return le16_to_cpu(buf->duration);
384}
385
386static int vnt_fill_ieee80211_rts(struct vnt_usb_send_context *tx_context,
387	struct ieee80211_rts *rts, __le16 duration)
388{
389	struct ieee80211_hdr *hdr =
390				(struct ieee80211_hdr *)tx_context->skb->data;
391
392	rts->duration = duration;
393	rts->frame_control =
394		cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS);
395
396	ether_addr_copy(rts->ra, hdr->addr1);
397	ether_addr_copy(rts->ta, hdr->addr2);
398
399	return 0;
400}
401
402static u16 vnt_rxtx_rts_g_head(struct vnt_usb_send_context *tx_context,
403			       struct vnt_rts_g *buf)
404{
405	struct vnt_private *priv = tx_context->priv;
406	u16 rts_frame_len = 20;
407	u16 current_rate = tx_context->tx_rate;
408
409	vnt_get_phy_field(priv, rts_frame_len, priv->top_cck_basic_rate,
410		PK_TYPE_11B, &buf->b);
411	vnt_get_phy_field(priv, rts_frame_len, priv->top_ofdm_basic_rate,
412			  tx_context->pkt_type, &buf->a);
413
414	buf->duration_bb = vnt_get_rtscts_duration_le(tx_context, RTSDUR_BB,
415						      PK_TYPE_11B,
416						      priv->top_cck_basic_rate);
417	buf->duration_aa = vnt_get_rtscts_duration_le(tx_context, RTSDUR_AA,
418						      tx_context->pkt_type,
419						      current_rate);
420	buf->duration_ba = vnt_get_rtscts_duration_le(tx_context, RTSDUR_BA,
421						      tx_context->pkt_type,
422						      current_rate);
423
424	vnt_fill_ieee80211_rts(tx_context, &buf->data, buf->duration_aa);
425
426	return vnt_rxtx_datahead_g(tx_context, &buf->data_head);
427}
428
429static u16 vnt_rxtx_rts_g_fb_head(struct vnt_usb_send_context *tx_context,
430				  struct vnt_rts_g_fb *buf)
431{
432	struct vnt_private *priv = tx_context->priv;
433	u16 current_rate = tx_context->tx_rate;
434	u16 rts_frame_len = 20;
435
436	vnt_get_phy_field(priv, rts_frame_len, priv->top_cck_basic_rate,
437		PK_TYPE_11B, &buf->b);
438	vnt_get_phy_field(priv, rts_frame_len, priv->top_ofdm_basic_rate,
439			  tx_context->pkt_type, &buf->a);
440
441	buf->duration_bb = vnt_get_rtscts_duration_le(tx_context, RTSDUR_BB,
442						      PK_TYPE_11B,
443						      priv->top_cck_basic_rate);
444	buf->duration_aa = vnt_get_rtscts_duration_le(tx_context, RTSDUR_AA,
445						      tx_context->pkt_type,
446						      current_rate);
447	buf->duration_ba = vnt_get_rtscts_duration_le(tx_context, RTSDUR_BA,
448						      tx_context->pkt_type,
449						      current_rate);
450
451	buf->rts_duration_ba_f0 =
452		vnt_get_rtscts_duration_le(tx_context, RTSDUR_BA_F0,
453					   tx_context->pkt_type,
454					   priv->tx_rate_fb0);
455	buf->rts_duration_aa_f0 =
456		vnt_get_rtscts_duration_le(tx_context, RTSDUR_AA_F0,
457					   tx_context->pkt_type,
458					   priv->tx_rate_fb0);
459	buf->rts_duration_ba_f1 =
460		vnt_get_rtscts_duration_le(tx_context, RTSDUR_BA_F1,
461					   tx_context->pkt_type,
462					   priv->tx_rate_fb1);
463	buf->rts_duration_aa_f1 =
464		vnt_get_rtscts_duration_le(tx_context, RTSDUR_AA_F1,
465					   tx_context->pkt_type,
466					   priv->tx_rate_fb1);
467
468	vnt_fill_ieee80211_rts(tx_context, &buf->data, buf->duration_aa);
469
470	return vnt_rxtx_datahead_g_fb(tx_context, &buf->data_head);
471}
472
473static u16 vnt_rxtx_rts_ab_head(struct vnt_usb_send_context *tx_context,
474				struct vnt_rts_ab *buf)
475{
476	struct vnt_private *priv = tx_context->priv;
477	u16 current_rate = tx_context->tx_rate;
478	u16 rts_frame_len = 20;
479
480	vnt_get_phy_field(priv, rts_frame_len, priv->top_ofdm_basic_rate,
481			  tx_context->pkt_type, &buf->ab);
482
483	buf->duration = vnt_get_rtscts_duration_le(tx_context, RTSDUR_AA,
484						   tx_context->pkt_type,
485						   current_rate);
486
487	vnt_fill_ieee80211_rts(tx_context, &buf->data, buf->duration);
488
489	return vnt_rxtx_datahead_ab(tx_context, &buf->data_head);
490}
491
492static u16 vnt_rxtx_rts_a_fb_head(struct vnt_usb_send_context *tx_context,
493				  struct vnt_rts_a_fb *buf)
494{
495	struct vnt_private *priv = tx_context->priv;
496	u16 current_rate = tx_context->tx_rate;
497	u16 rts_frame_len = 20;
498
499	vnt_get_phy_field(priv, rts_frame_len,
500		priv->top_ofdm_basic_rate, tx_context->pkt_type, &buf->a);
501
502	buf->duration = vnt_get_rtscts_duration_le(tx_context, RTSDUR_AA,
503						   tx_context->pkt_type,
504						   current_rate);
505
506	buf->rts_duration_f0 =
507		vnt_get_rtscts_duration_le(tx_context, RTSDUR_AA_F0,
508					   tx_context->pkt_type,
509					   priv->tx_rate_fb0);
510
511	buf->rts_duration_f1 =
512		vnt_get_rtscts_duration_le(tx_context, RTSDUR_AA_F1,
513					   tx_context->pkt_type,
514					   priv->tx_rate_fb1);
515
516	vnt_fill_ieee80211_rts(tx_context, &buf->data, buf->duration);
517
518	return vnt_rxtx_datahead_a_fb(tx_context, &buf->data_head);
519}
520
521static u16 vnt_fill_cts_fb_head(struct vnt_usb_send_context *tx_context,
522				union vnt_tx_data_head *head)
523{
524	struct vnt_private *priv = tx_context->priv;
525	struct vnt_cts_fb *buf = &head->cts_g_fb;
526	u32 cts_frame_len = 14;
527	u16 current_rate = tx_context->tx_rate;
528
529	/* Get SignalField,ServiceField,Length */
530	vnt_get_phy_field(priv, cts_frame_len, priv->top_cck_basic_rate,
531			  PK_TYPE_11B, &buf->b);
532
533	buf->duration_ba =
534		vnt_get_rtscts_duration_le(tx_context, CTSDUR_BA,
535					   tx_context->pkt_type,
536					   current_rate);
537	/* Get CTSDuration_ba_f0 */
538	buf->cts_duration_ba_f0 =
539		vnt_get_rtscts_duration_le(tx_context, CTSDUR_BA_F0,
540					   tx_context->pkt_type,
541					   priv->tx_rate_fb0);
542	/* Get CTSDuration_ba_f1 */
543	buf->cts_duration_ba_f1 =
544		vnt_get_rtscts_duration_le(tx_context, CTSDUR_BA_F1,
545					   tx_context->pkt_type,
546					   priv->tx_rate_fb1);
547	/* Get CTS Frame body */
548	buf->data.duration = buf->duration_ba;
549	buf->data.frame_control =
550		cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS);
551
552	ether_addr_copy(buf->data.ra, priv->current_net_addr);
553
554	return vnt_rxtx_datahead_g_fb(tx_context, &buf->data_head);
555}
556
557static u16 vnt_fill_cts_head(struct vnt_usb_send_context *tx_context,
558			     union vnt_tx_data_head *head)
559{
560	struct vnt_private *priv = tx_context->priv;
561	struct vnt_cts *buf = &head->cts_g;
562	u32 cts_frame_len = 14;
563	u16 current_rate = tx_context->tx_rate;
564
565	/* Get SignalField,ServiceField,Length */
566	vnt_get_phy_field(priv, cts_frame_len, priv->top_cck_basic_rate,
567			  PK_TYPE_11B, &buf->b);
568	/* Get CTSDuration_ba */
569	buf->duration_ba =
570		vnt_get_rtscts_duration_le(tx_context, CTSDUR_BA,
571					   tx_context->pkt_type,
572					   current_rate);
573	/*Get CTS Frame body*/
574	buf->data.duration = buf->duration_ba;
575	buf->data.frame_control =
576		cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS);
577
578	ether_addr_copy(buf->data.ra, priv->current_net_addr);
579
580	return vnt_rxtx_datahead_g(tx_context, &buf->data_head);
581}
582
583static u16 vnt_rxtx_rts(struct vnt_usb_send_context *tx_context,
584			union vnt_tx_head *tx_head, bool need_mic)
585{
586	struct vnt_private *priv = tx_context->priv;
587	struct vnt_rrv_time_rts *buf = &tx_head->tx_rts.rts;
588	union vnt_tx_data_head *head = &tx_head->tx_rts.tx.head;
589	u32 frame_len = tx_context->frame_len;
590	u16 current_rate = tx_context->tx_rate;
591	u8 need_ack = tx_context->need_ack;
592
593	buf->rts_rrv_time_aa = vnt_get_rtscts_rsvtime_le(priv, 2,
594			tx_context->pkt_type, frame_len, current_rate);
595	buf->rts_rrv_time_ba = vnt_get_rtscts_rsvtime_le(priv, 1,
596			tx_context->pkt_type, frame_len, current_rate);
597	buf->rts_rrv_time_bb = vnt_get_rtscts_rsvtime_le(priv, 0,
598			tx_context->pkt_type, frame_len, current_rate);
599
600	buf->rrv_time_a = vnt_rxtx_rsvtime_le16(priv, tx_context->pkt_type,
601						frame_len, current_rate,
602						need_ack);
603	buf->rrv_time_b = vnt_rxtx_rsvtime_le16(priv, PK_TYPE_11B, frame_len,
604					priv->top_cck_basic_rate, need_ack);
605
606	if (need_mic)
607		head = &tx_head->tx_rts.tx.mic.head;
608
609	if (tx_context->fb_option)
610		return vnt_rxtx_rts_g_fb_head(tx_context, &head->rts_g_fb);
611
612	return vnt_rxtx_rts_g_head(tx_context, &head->rts_g);
613}
614
615static u16 vnt_rxtx_cts(struct vnt_usb_send_context *tx_context,
616			union vnt_tx_head *tx_head, bool need_mic)
617{
618	struct vnt_private *priv = tx_context->priv;
619	struct vnt_rrv_time_cts *buf = &tx_head->tx_cts.cts;
620	union vnt_tx_data_head *head = &tx_head->tx_cts.tx.head;
621	u32 frame_len = tx_context->frame_len;
622	u16 current_rate = tx_context->tx_rate;
623	u8 need_ack = tx_context->need_ack;
624
625	buf->rrv_time_a = vnt_rxtx_rsvtime_le16(priv, tx_context->pkt_type,
626					frame_len, current_rate, need_ack);
627	buf->rrv_time_b = vnt_rxtx_rsvtime_le16(priv, PK_TYPE_11B,
628				frame_len, priv->top_cck_basic_rate, need_ack);
629
630	buf->cts_rrv_time_ba = vnt_get_rtscts_rsvtime_le(priv, 3,
631			tx_context->pkt_type, frame_len, current_rate);
632
633	if (need_mic)
634		head = &tx_head->tx_cts.tx.mic.head;
635
636	/* Fill CTS */
637	if (tx_context->fb_option)
638		return vnt_fill_cts_fb_head(tx_context, head);
639
640	return vnt_fill_cts_head(tx_context, head);
641}
642
643static u16 vnt_rxtx_ab(struct vnt_usb_send_context *tx_context,
644		       union vnt_tx_head *tx_head, bool need_rts, bool need_mic)
645{
646	struct vnt_private *priv = tx_context->priv;
647	struct vnt_rrv_time_ab *buf = &tx_head->tx_ab.ab;
648	union vnt_tx_data_head *head = &tx_head->tx_ab.tx.head;
649	u32 frame_len = tx_context->frame_len;
650	u16 current_rate = tx_context->tx_rate;
651	u8 need_ack = tx_context->need_ack;
652
653	buf->rrv_time = vnt_rxtx_rsvtime_le16(priv, tx_context->pkt_type,
654			frame_len, current_rate, need_ack);
655
656	if (need_mic)
657		head = &tx_head->tx_ab.tx.mic.head;
658
659	if (need_rts) {
660		if (tx_context->pkt_type == PK_TYPE_11B)
661			buf->rts_rrv_time = vnt_get_rtscts_rsvtime_le(priv, 0,
662				tx_context->pkt_type, frame_len, current_rate);
663		else /* PK_TYPE_11A */
664			buf->rts_rrv_time = vnt_get_rtscts_rsvtime_le(priv, 2,
665				tx_context->pkt_type, frame_len, current_rate);
666
667		if (tx_context->fb_option &&
668		    tx_context->pkt_type == PK_TYPE_11A)
669			return vnt_rxtx_rts_a_fb_head(tx_context,
670						      &head->rts_a_fb);
671
672		return vnt_rxtx_rts_ab_head(tx_context, &head->rts_ab);
673	}
674
675	if (tx_context->pkt_type == PK_TYPE_11A)
676		return vnt_rxtx_datahead_a_fb(tx_context,
677					      &head->data_head_a_fb);
678
679	return vnt_rxtx_datahead_ab(tx_context, &head->data_head_ab);
680}
681
682static u16 vnt_generate_tx_parameter(struct vnt_usb_send_context *tx_context,
683	struct vnt_tx_buffer *tx_buffer,
684	struct vnt_mic_hdr **mic_hdr, u32 need_mic,
685	bool need_rts)
686{
687
688	if (tx_context->pkt_type == PK_TYPE_11GB ||
689	    tx_context->pkt_type == PK_TYPE_11GA) {
690		if (need_rts) {
691			if (need_mic)
692				*mic_hdr = &tx_buffer->
693						tx_head.tx_rts.tx.mic.hdr;
694
695			return vnt_rxtx_rts(tx_context, &tx_buffer->tx_head,
696					    need_mic);
697		}
698
699		if (need_mic)
700			*mic_hdr = &tx_buffer->tx_head.tx_cts.tx.mic.hdr;
701
702		return vnt_rxtx_cts(tx_context, &tx_buffer->tx_head, need_mic);
703	}
704
705	if (need_mic)
706		*mic_hdr = &tx_buffer->tx_head.tx_ab.tx.mic.hdr;
707
708	return vnt_rxtx_ab(tx_context, &tx_buffer->tx_head, need_rts, need_mic);
709}
710
711static void vnt_fill_txkey(struct vnt_usb_send_context *tx_context,
712	u8 *key_buffer, struct ieee80211_key_conf *tx_key, struct sk_buff *skb,
713	u16 payload_len, struct vnt_mic_hdr *mic_hdr)
714{
715	struct ieee80211_hdr *hdr = tx_context->hdr;
716	struct ieee80211_key_seq seq;
717	u8 *iv = ((u8 *)hdr + ieee80211_get_hdrlen_from_skb(skb));
718
719	/* strip header and icv len from payload */
720	payload_len -= ieee80211_get_hdrlen_from_skb(skb);
721	payload_len -= tx_key->icv_len;
722
723	switch (tx_key->cipher) {
724	case WLAN_CIPHER_SUITE_WEP40:
725	case WLAN_CIPHER_SUITE_WEP104:
726		memcpy(key_buffer, iv, 3);
727		memcpy(key_buffer + 3, tx_key->key, tx_key->keylen);
728
729		if (tx_key->keylen == WLAN_KEY_LEN_WEP40) {
730			memcpy(key_buffer + 8, iv, 3);
731			memcpy(key_buffer + 11,
732					tx_key->key, WLAN_KEY_LEN_WEP40);
733		}
734
735		break;
736	case WLAN_CIPHER_SUITE_TKIP:
737		ieee80211_get_tkip_p2k(tx_key, skb, key_buffer);
738
739		break;
740	case WLAN_CIPHER_SUITE_CCMP:
741
742		if (!mic_hdr)
743			return;
744
745		mic_hdr->id = 0x59;
746		mic_hdr->payload_len = cpu_to_be16(payload_len);
747		ether_addr_copy(mic_hdr->mic_addr2, hdr->addr2);
748
749		ieee80211_get_key_tx_seq(tx_key, &seq);
750
751		memcpy(mic_hdr->ccmp_pn, seq.ccmp.pn, IEEE80211_CCMP_PN_LEN);
752
753		if (ieee80211_has_a4(hdr->frame_control))
754			mic_hdr->hlen = cpu_to_be16(28);
755		else
756			mic_hdr->hlen = cpu_to_be16(22);
757
758		ether_addr_copy(mic_hdr->addr1, hdr->addr1);
759		ether_addr_copy(mic_hdr->addr2, hdr->addr2);
760		ether_addr_copy(mic_hdr->addr3, hdr->addr3);
761
762		mic_hdr->frame_control = cpu_to_le16(
763			le16_to_cpu(hdr->frame_control) & 0xc78f);
764		mic_hdr->seq_ctrl = cpu_to_le16(
765				le16_to_cpu(hdr->seq_ctrl) & 0xf);
766
767		if (ieee80211_has_a4(hdr->frame_control))
768			ether_addr_copy(mic_hdr->addr4, hdr->addr4);
769
770
771		memcpy(key_buffer, tx_key->key, WLAN_KEY_LEN_CCMP);
772
773		break;
774	default:
775		break;
776	}
777
778}
779
780int vnt_tx_packet(struct vnt_private *priv, struct sk_buff *skb)
781{
782	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
783	struct ieee80211_tx_rate *tx_rate = &info->control.rates[0];
784	struct ieee80211_rate *rate;
785	struct ieee80211_key_conf *tx_key;
786	struct ieee80211_hdr *hdr;
787	struct vnt_mic_hdr *mic_hdr = NULL;
788	struct vnt_tx_buffer *tx_buffer;
789	struct vnt_tx_fifo_head *tx_buffer_head;
790	struct vnt_usb_send_context *tx_context;
791	unsigned long flags;
792	u16 tx_bytes, tx_header_size, tx_body_size, current_rate, duration_id;
793	u8 pkt_type, fb_option = AUTO_FB_NONE;
794	bool need_rts = false, is_pspoll = false;
795	bool need_mic = false;
796
797	hdr = (struct ieee80211_hdr *)(skb->data);
798
799	rate = ieee80211_get_tx_rate(priv->hw, info);
800
801	current_rate = rate->hw_value;
802	if (priv->current_rate != current_rate &&
803			!(priv->hw->conf.flags & IEEE80211_CONF_OFFCHANNEL)) {
804		priv->current_rate = current_rate;
805		vnt_schedule_command(priv, WLAN_CMD_SETPOWER);
806	}
807
808	if (current_rate > RATE_11M) {
809		if (info->band == IEEE80211_BAND_5GHZ) {
810			pkt_type = PK_TYPE_11A;
811		} else {
812			if (tx_rate->flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
813				pkt_type = PK_TYPE_11GB;
814			else
815				pkt_type = PK_TYPE_11GA;
816		}
817	} else {
818		pkt_type = PK_TYPE_11B;
819	}
820
821	spin_lock_irqsave(&priv->lock, flags);
822
823	tx_context = vnt_get_free_context(priv);
824	if (!tx_context) {
825		dev_dbg(&priv->usb->dev, "%s No free context\n", __func__);
826		spin_unlock_irqrestore(&priv->lock, flags);
827		return -ENOMEM;
828	}
829
830	tx_context->skb = skb;
831	tx_context->pkt_type = pkt_type;
832	tx_context->need_ack = false;
833	tx_context->frame_len = skb->len + 4;
834	tx_context->tx_rate = current_rate;
835
836	spin_unlock_irqrestore(&priv->lock, flags);
837
838	tx_buffer = (struct vnt_tx_buffer *)tx_context->data;
839	tx_buffer_head = &tx_buffer->fifo_head;
840	tx_body_size = skb->len;
841
842	/*Set fifo controls */
843	if (pkt_type == PK_TYPE_11A)
844		tx_buffer_head->fifo_ctl = 0;
845	else if (pkt_type == PK_TYPE_11B)
846		tx_buffer_head->fifo_ctl = cpu_to_le16(FIFOCTL_11B);
847	else if (pkt_type == PK_TYPE_11GB)
848		tx_buffer_head->fifo_ctl = cpu_to_le16(FIFOCTL_11GB);
849	else if (pkt_type == PK_TYPE_11GA)
850		tx_buffer_head->fifo_ctl = cpu_to_le16(FIFOCTL_11GA);
851
852	if (!ieee80211_is_data(hdr->frame_control)) {
853		tx_buffer_head->fifo_ctl |= cpu_to_le16(FIFOCTL_GENINT |
854							FIFOCTL_ISDMA0);
855		tx_buffer_head->fifo_ctl |= cpu_to_le16(FIFOCTL_TMOEN);
856
857		tx_buffer_head->time_stamp =
858			cpu_to_le16(DEFAULT_MGN_LIFETIME_RES_64us);
859	} else {
860		tx_buffer_head->time_stamp =
861			cpu_to_le16(DEFAULT_MSDU_LIFETIME_RES_64us);
862	}
863
864	if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
865		tx_buffer_head->fifo_ctl |= cpu_to_le16(FIFOCTL_NEEDACK);
866		tx_context->need_ack = true;
867	}
868
869	if (ieee80211_has_retry(hdr->frame_control))
870		tx_buffer_head->fifo_ctl |= cpu_to_le16(FIFOCTL_LRETRY);
871
872	if (tx_rate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
873		priv->preamble_type = PREAMBLE_SHORT;
874	else
875		priv->preamble_type = PREAMBLE_LONG;
876
877	if (tx_rate->flags & IEEE80211_TX_RC_USE_RTS_CTS) {
878		need_rts = true;
879		tx_buffer_head->fifo_ctl |= cpu_to_le16(FIFOCTL_RTS);
880	}
881
882	if (ieee80211_has_a4(hdr->frame_control))
883		tx_buffer_head->fifo_ctl |= cpu_to_le16(FIFOCTL_LHEAD);
884
885	if (info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER)
886		is_pspoll = true;
887
888	tx_buffer_head->frag_ctl =
889			cpu_to_le16(ieee80211_get_hdrlen_from_skb(skb) << 10);
890
891	if (info->control.hw_key) {
892		tx_key = info->control.hw_key;
893		switch (info->control.hw_key->cipher) {
894		case WLAN_CIPHER_SUITE_WEP40:
895		case WLAN_CIPHER_SUITE_WEP104:
896			tx_buffer_head->frag_ctl |= cpu_to_le16(FRAGCTL_LEGACY);
897			break;
898		case WLAN_CIPHER_SUITE_TKIP:
899			tx_buffer_head->frag_ctl |= cpu_to_le16(FRAGCTL_TKIP);
900			break;
901		case WLAN_CIPHER_SUITE_CCMP:
902			tx_buffer_head->frag_ctl |= cpu_to_le16(FRAGCTL_AES);
903			need_mic = true;
904		default:
905			break;
906		}
907		tx_context->frame_len += tx_key->icv_len;
908	}
909
910	tx_buffer_head->current_rate = cpu_to_le16(current_rate);
911
912	/* legacy rates TODO use ieee80211_tx_rate */
913	if (current_rate >= RATE_18M && ieee80211_is_data(hdr->frame_control)) {
914		if (priv->auto_fb_ctrl == AUTO_FB_0) {
915			tx_buffer_head->fifo_ctl |=
916						cpu_to_le16(FIFOCTL_AUTO_FB_0);
917
918			priv->tx_rate_fb0 =
919				vnt_fb_opt0[FB_RATE0][current_rate - RATE_18M];
920			priv->tx_rate_fb1 =
921				vnt_fb_opt0[FB_RATE1][current_rate - RATE_18M];
922
923			fb_option = AUTO_FB_0;
924		} else if (priv->auto_fb_ctrl == AUTO_FB_1) {
925			tx_buffer_head->fifo_ctl |=
926						cpu_to_le16(FIFOCTL_AUTO_FB_1);
927
928			priv->tx_rate_fb0 =
929				vnt_fb_opt1[FB_RATE0][current_rate - RATE_18M];
930			priv->tx_rate_fb1 =
931				vnt_fb_opt1[FB_RATE1][current_rate - RATE_18M];
932
933			fb_option = AUTO_FB_1;
934		}
935	}
936
937	tx_context->fb_option = fb_option;
938
939	duration_id = vnt_generate_tx_parameter(tx_context, tx_buffer, &mic_hdr,
940						need_mic, need_rts);
941
942	tx_header_size = tx_context->tx_hdr_size;
943	if (!tx_header_size) {
944		tx_context->in_use = false;
945		return -ENOMEM;
946	}
947
948	tx_buffer_head->frag_ctl |= cpu_to_le16(FRAGCTL_NONFRAG);
949
950	tx_bytes = tx_header_size + tx_body_size;
951
952	memcpy(tx_context->hdr, skb->data, tx_body_size);
953
954	hdr->duration_id = cpu_to_le16(duration_id);
955
956	if (info->control.hw_key) {
957		tx_key = info->control.hw_key;
958		if (tx_key->keylen > 0)
959			vnt_fill_txkey(tx_context, tx_buffer_head->tx_key,
960				tx_key, skb, tx_body_size, mic_hdr);
961	}
962
963	priv->seq_counter = (le16_to_cpu(hdr->seq_ctrl) &
964						IEEE80211_SCTL_SEQ) >> 4;
965
966	tx_buffer->tx_byte_count = cpu_to_le16(tx_bytes);
967	tx_buffer->pkt_no = tx_context->pkt_no;
968	tx_buffer->type = 0x00;
969
970	tx_bytes += 4;
971
972	tx_context->type = CONTEXT_DATA_PACKET;
973	tx_context->buf_len = tx_bytes;
974
975	spin_lock_irqsave(&priv->lock, flags);
976
977	if (vnt_tx_context(priv, tx_context) != STATUS_PENDING) {
978		spin_unlock_irqrestore(&priv->lock, flags);
979		return -EIO;
980	}
981
982	spin_unlock_irqrestore(&priv->lock, flags);
983
984	return 0;
985}
986
987static int vnt_beacon_xmit(struct vnt_private *priv,
988	struct sk_buff *skb)
989{
990	struct vnt_beacon_buffer *beacon_buffer;
991	struct vnt_tx_short_buf_head *short_head;
992	struct ieee80211_tx_info *info;
993	struct vnt_usb_send_context *context;
994	struct ieee80211_mgmt *mgmt_hdr;
995	unsigned long flags;
996	u32 frame_size = skb->len + 4;
997	u16 current_rate, count;
998
999	spin_lock_irqsave(&priv->lock, flags);
1000
1001	context = vnt_get_free_context(priv);
1002	if (!context) {
1003		dev_dbg(&priv->usb->dev, "%s No free context!\n", __func__);
1004		spin_unlock_irqrestore(&priv->lock, flags);
1005		return -ENOMEM;
1006	}
1007
1008	context->skb = skb;
1009
1010	spin_unlock_irqrestore(&priv->lock, flags);
1011
1012	beacon_buffer = (struct vnt_beacon_buffer *)&context->data[0];
1013	short_head = &beacon_buffer->short_head;
1014
1015	if (priv->bb_type == BB_TYPE_11A) {
1016		current_rate = RATE_6M;
1017
1018		/* Get SignalField,ServiceField,Length */
1019		vnt_get_phy_field(priv, frame_size, current_rate,
1020			PK_TYPE_11A, &short_head->ab);
1021
1022		/* Get Duration and TimeStampOff */
1023		short_head->duration = vnt_get_duration_le(priv,
1024							PK_TYPE_11A, false);
1025		short_head->time_stamp_off =
1026				vnt_time_stamp_off(priv, current_rate);
1027	} else {
1028		current_rate = RATE_1M;
1029		short_head->fifo_ctl |= cpu_to_le16(FIFOCTL_11B);
1030
1031		/* Get SignalField,ServiceField,Length */
1032		vnt_get_phy_field(priv, frame_size, current_rate,
1033					PK_TYPE_11B, &short_head->ab);
1034
1035		/* Get Duration and TimeStampOff */
1036		short_head->duration = vnt_get_duration_le(priv,
1037						PK_TYPE_11B, false);
1038		short_head->time_stamp_off =
1039			vnt_time_stamp_off(priv, current_rate);
1040	}
1041
1042	/* Generate Beacon Header */
1043	mgmt_hdr = &beacon_buffer->mgmt_hdr;
1044	memcpy(mgmt_hdr, skb->data, skb->len);
1045
1046	/* time stamp always 0 */
1047	mgmt_hdr->u.beacon.timestamp = 0;
1048
1049	info = IEEE80211_SKB_CB(skb);
1050	if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
1051		struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)mgmt_hdr;
1052
1053		hdr->duration_id = 0;
1054		hdr->seq_ctrl = cpu_to_le16(priv->seq_counter << 4);
1055	}
1056
1057	priv->seq_counter++;
1058	if (priv->seq_counter > 0x0fff)
1059		priv->seq_counter = 0;
1060
1061	count = sizeof(struct vnt_tx_short_buf_head) + skb->len;
1062
1063	beacon_buffer->tx_byte_count = cpu_to_le16(count);
1064	beacon_buffer->pkt_no = context->pkt_no;
1065	beacon_buffer->type = 0x01;
1066
1067	context->type = CONTEXT_BEACON_PACKET;
1068	context->buf_len = count + 4; /* USB header */
1069
1070	spin_lock_irqsave(&priv->lock, flags);
1071
1072	if (vnt_tx_context(priv, context) != STATUS_PENDING)
1073		ieee80211_free_txskb(priv->hw, context->skb);
1074
1075	spin_unlock_irqrestore(&priv->lock, flags);
1076
1077	return 0;
1078}
1079
1080int vnt_beacon_make(struct vnt_private *priv, struct ieee80211_vif *vif)
1081{
1082	struct sk_buff *beacon;
1083
1084	beacon = ieee80211_beacon_get(priv->hw, vif);
1085	if (!beacon)
1086		return -ENOMEM;
1087
1088	if (vnt_beacon_xmit(priv, beacon)) {
1089		ieee80211_free_txskb(priv->hw, beacon);
1090		return -ENODEV;
1091	}
1092
1093	return 0;
1094}
1095
1096int vnt_beacon_enable(struct vnt_private *priv, struct ieee80211_vif *vif,
1097	struct ieee80211_bss_conf *conf)
1098{
1099	vnt_mac_reg_bits_off(priv, MAC_REG_TCR, TCR_AUTOBCNTX);
1100
1101	vnt_mac_reg_bits_off(priv, MAC_REG_TFTCTL, TFTCTL_TSFCNTREN);
1102
1103	vnt_mac_set_beacon_interval(priv, conf->beacon_int);
1104
1105	vnt_clear_current_tsf(priv);
1106
1107	vnt_mac_reg_bits_on(priv, MAC_REG_TFTCTL, TFTCTL_TSFCNTREN);
1108
1109	vnt_reset_next_tbtt(priv, conf->beacon_int);
1110
1111	return vnt_beacon_make(priv, vif);
1112}
1113