xref: /openbmc/linux/drivers/staging/vt6656/rxtx.c (revision 31e67366)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
4  * All rights reserved.
5  *
6  * File: rxtx.c
7  *
8  * Purpose: handle WMAC/802.3/802.11 rx & tx functions
9  *
10  * Author: Lyndon Chen
11  *
12  * Date: May 20, 2003
13  *
14  * Functions:
15  *      vnt_generate_tx_parameter - Generate tx dma required parameter.
16  *      vnt_get_rsvtime- get frame reserved time
17  *      vnt_fill_cts_head- fulfill CTS ctl header
18  *
19  * Revision History:
20  *
21  */
22 
23 #include <linux/etherdevice.h>
24 #include "device.h"
25 #include "rxtx.h"
26 #include "card.h"
27 #include "mac.h"
28 #include "rf.h"
29 #include "usbpipe.h"
30 
31 static const u16 vnt_time_stampoff[2][MAX_RATE] = {
32 	/* Long Preamble */
33 	{384, 288, 226, 209, 54, 43, 37, 31, 28, 25, 24, 23},
34 
35 	/* Short Preamble */
36 	{384, 192, 130, 113, 54, 43, 37, 31, 28, 25, 24, 23},
37 };
38 
39 #define DATADUR_B       10
40 #define DATADUR_A       11
41 
42 static const u8 vnt_phy_signal[] = {
43 	0x00,	/* RATE_1M  */
44 	0x01,	/* RATE_2M  */
45 	0x02,	/* RATE_5M  */
46 	0x03,	/* RATE_11M */
47 	0x8b,	/* RATE_6M  */
48 	0x8f,	/* RATE_9M  */
49 	0x8a,	/* RATE_12M */
50 	0x8e,	/* RATE_18M */
51 	0x89,	/* RATE_24M */
52 	0x8d,	/* RATE_36M */
53 	0x88,	/* RATE_48M */
54 	0x8c	/* RATE_54M */
55 };
56 
57 static struct vnt_usb_send_context
58 	*vnt_get_free_context(struct vnt_private *priv)
59 {
60 	struct vnt_usb_send_context *context = NULL;
61 	int ii;
62 
63 	dev_dbg(&priv->usb->dev, "%s\n", __func__);
64 
65 	for (ii = 0; ii < priv->num_tx_context; ii++) {
66 		if (!priv->tx_context[ii])
67 			return NULL;
68 
69 		context = priv->tx_context[ii];
70 		if (!context->in_use) {
71 			context->in_use = true;
72 			return context;
73 		}
74 	}
75 
76 	if (ii == priv->num_tx_context) {
77 		dev_dbg(&priv->usb->dev, "%s No Free Tx Context\n", __func__);
78 
79 		ieee80211_stop_queues(priv->hw);
80 	}
81 
82 	return NULL;
83 }
84 
85 /* Get Length, Service, and Signal fields of Phy for Tx */
86 static void vnt_get_phy_field(struct vnt_private *priv, u32 frame_length,
87 			      u16 tx_rate, u8 pkt_type,
88 			      struct vnt_phy_field *phy)
89 {
90 	u32 bit_count;
91 	u32 count = 0;
92 	u32 tmp;
93 	int ext_bit;
94 	int i;
95 	u8 mask = 0;
96 	u8 preamble_type = priv->preamble_type;
97 
98 	bit_count = frame_length * 8;
99 	ext_bit = false;
100 
101 	switch (tx_rate) {
102 	case RATE_1M:
103 		count = bit_count;
104 		break;
105 	case RATE_2M:
106 		count = bit_count / 2;
107 		break;
108 	case RATE_5M:
109 		count = DIV_ROUND_UP(bit_count * 10, 55);
110 		break;
111 	case RATE_11M:
112 		count = bit_count / 11;
113 		tmp = count * 11;
114 
115 		if (tmp != bit_count) {
116 			count++;
117 
118 			if ((bit_count - tmp) <= 3)
119 				ext_bit = true;
120 		}
121 
122 		break;
123 	}
124 
125 	if (tx_rate > RATE_11M) {
126 		if (pkt_type == PK_TYPE_11A)
127 			mask = BIT(4);
128 	} else if (tx_rate > RATE_1M) {
129 		if (preamble_type == PREAMBLE_SHORT)
130 			mask = BIT(3);
131 	}
132 
133 	i = tx_rate > RATE_54M ? RATE_54M : tx_rate;
134 	phy->signal = vnt_phy_signal[i] | mask;
135 	phy->service = 0x00;
136 
137 	if (pkt_type == PK_TYPE_11B) {
138 		if (ext_bit)
139 			phy->service |= 0x80;
140 		phy->len = cpu_to_le16((u16)count);
141 	} else {
142 		phy->len = cpu_to_le16((u16)frame_length);
143 	}
144 }
145 
146 static __le16 vnt_time_stamp_off(struct vnt_private *priv, u16 rate)
147 {
148 	return cpu_to_le16(vnt_time_stampoff[priv->preamble_type % 2]
149 							[rate % MAX_RATE]);
150 }
151 
152 static __le16 vnt_rxtx_rsvtime_le16(struct vnt_usb_send_context *context)
153 {
154 	struct vnt_private *priv = context->priv;
155 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(context->skb);
156 	struct ieee80211_rate *rate = ieee80211_get_tx_rate(priv->hw, info);
157 
158 	return ieee80211_generic_frame_duration(priv->hw,
159 						 info->control.vif, info->band,
160 						 context->frame_len,
161 						 rate);
162 }
163 
164 static __le16 vnt_get_rts_duration(struct vnt_usb_send_context *context)
165 {
166 	struct vnt_private *priv = context->priv;
167 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(context->skb);
168 
169 	return ieee80211_rts_duration(priv->hw, priv->vif,
170 				      context->frame_len, info);
171 }
172 
173 static __le16 vnt_get_cts_duration(struct vnt_usb_send_context *context)
174 {
175 	struct vnt_private *priv = context->priv;
176 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(context->skb);
177 
178 	return ieee80211_ctstoself_duration(priv->hw, priv->vif,
179 					    context->frame_len, info);
180 }
181 
182 static void vnt_rxtx_datahead_g(struct vnt_usb_send_context *tx_context,
183 				struct vnt_tx_datahead_g *buf)
184 {
185 	struct vnt_private *priv = tx_context->priv;
186 	struct ieee80211_hdr *hdr =
187 				(struct ieee80211_hdr *)tx_context->skb->data;
188 	u32 frame_len = tx_context->frame_len;
189 	u16 rate = tx_context->tx_rate;
190 
191 	/* Get SignalField,ServiceField,Length */
192 	vnt_get_phy_field(priv, frame_len, rate, tx_context->pkt_type, &buf->a);
193 	vnt_get_phy_field(priv, frame_len, priv->top_cck_basic_rate,
194 			  PK_TYPE_11B, &buf->b);
195 
196 	/* Get Duration and TimeStamp */
197 	buf->duration_a = hdr->duration_id;
198 	buf->duration_b = hdr->duration_id;
199 	buf->time_stamp_off_a = vnt_time_stamp_off(priv, rate);
200 	buf->time_stamp_off_b = vnt_time_stamp_off(priv,
201 						   priv->top_cck_basic_rate);
202 }
203 
204 static void vnt_rxtx_datahead_ab(struct vnt_usb_send_context *tx_context,
205 				 struct vnt_tx_datahead_ab *buf)
206 {
207 	struct vnt_private *priv = tx_context->priv;
208 	struct ieee80211_hdr *hdr =
209 				(struct ieee80211_hdr *)tx_context->skb->data;
210 	u32 frame_len = tx_context->frame_len;
211 	u16 rate = tx_context->tx_rate;
212 
213 	/* Get SignalField,ServiceField,Length */
214 	vnt_get_phy_field(priv, frame_len, rate,
215 			  tx_context->pkt_type, &buf->ab);
216 
217 	/* Get Duration and TimeStampOff */
218 	buf->duration = hdr->duration_id;
219 	buf->time_stamp_off = vnt_time_stamp_off(priv, rate);
220 }
221 
222 static void vnt_fill_ieee80211_rts(struct vnt_usb_send_context *tx_context,
223 				   struct ieee80211_rts *rts, __le16 duration)
224 {
225 	struct ieee80211_hdr *hdr =
226 				(struct ieee80211_hdr *)tx_context->skb->data;
227 
228 	rts->duration = duration;
229 	rts->frame_control =
230 		cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS);
231 
232 	ether_addr_copy(rts->ra, hdr->addr1);
233 	ether_addr_copy(rts->ta, hdr->addr2);
234 }
235 
236 static void vnt_rxtx_rts_g_head(struct vnt_usb_send_context *tx_context,
237 				struct vnt_rts_g *buf)
238 {
239 	struct vnt_private *priv = tx_context->priv;
240 	u16 rts_frame_len = 20;
241 
242 	vnt_get_phy_field(priv, rts_frame_len, priv->top_cck_basic_rate,
243 			  PK_TYPE_11B, &buf->b);
244 	vnt_get_phy_field(priv, rts_frame_len, priv->top_ofdm_basic_rate,
245 			  tx_context->pkt_type, &buf->a);
246 
247 	buf->duration_bb = vnt_get_rts_duration(tx_context);
248 	buf->duration_aa = buf->duration_bb;
249 	buf->duration_ba = buf->duration_bb;
250 
251 	vnt_fill_ieee80211_rts(tx_context, &buf->data, buf->duration_aa);
252 
253 	vnt_rxtx_datahead_g(tx_context, &buf->data_head);
254 }
255 
256 static void vnt_rxtx_rts_ab_head(struct vnt_usb_send_context *tx_context,
257 				 struct vnt_rts_ab *buf)
258 {
259 	struct vnt_private *priv = tx_context->priv;
260 	u16 rts_frame_len = 20;
261 
262 	vnt_get_phy_field(priv, rts_frame_len, priv->top_ofdm_basic_rate,
263 			  tx_context->pkt_type, &buf->ab);
264 
265 	buf->duration = vnt_get_rts_duration(tx_context);
266 
267 	vnt_fill_ieee80211_rts(tx_context, &buf->data, buf->duration);
268 
269 	vnt_rxtx_datahead_ab(tx_context, &buf->data_head);
270 }
271 
272 static void vnt_fill_cts_head(struct vnt_usb_send_context *tx_context,
273 			      union vnt_tx_data_head *head)
274 {
275 	struct vnt_private *priv = tx_context->priv;
276 	struct vnt_cts *buf = &head->cts_g;
277 	u32 cts_frame_len = 14;
278 
279 	/* Get SignalField,ServiceField,Length */
280 	vnt_get_phy_field(priv, cts_frame_len, priv->top_cck_basic_rate,
281 			  PK_TYPE_11B, &buf->b);
282 	/* Get CTSDuration_ba */
283 	buf->duration_ba = vnt_get_cts_duration(tx_context);
284 	/*Get CTS Frame body*/
285 	buf->data.duration = buf->duration_ba;
286 	buf->data.frame_control =
287 		cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS);
288 
289 	ether_addr_copy(buf->data.ra, priv->current_net_addr);
290 
291 	vnt_rxtx_datahead_g(tx_context, &buf->data_head);
292 }
293 
294 /* returns true if mic_hdr is needed */
295 static bool vnt_fill_txkey(struct vnt_tx_buffer *tx_buffer, struct sk_buff *skb)
296 {
297 	struct vnt_tx_fifo_head *fifo = &tx_buffer->fifo_head;
298 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
299 	struct ieee80211_key_conf *tx_key = info->control.hw_key;
300 	struct vnt_mic_hdr *mic_hdr;
301 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
302 	u64 pn64;
303 	u16 payload_len = skb->len;
304 	u8 *iv = ((u8 *)hdr + ieee80211_get_hdrlen_from_skb(skb));
305 
306 	/* strip header and icv len from payload */
307 	payload_len -= ieee80211_get_hdrlen_from_skb(skb);
308 	payload_len -= tx_key->icv_len;
309 
310 	switch (tx_key->cipher) {
311 	case WLAN_CIPHER_SUITE_WEP40:
312 	case WLAN_CIPHER_SUITE_WEP104:
313 		memcpy(fifo->tx_key, iv, 3);
314 		memcpy(fifo->tx_key + 3, tx_key->key, tx_key->keylen);
315 
316 		if (tx_key->keylen == WLAN_KEY_LEN_WEP40) {
317 			memcpy(fifo->tx_key + 8, iv, 3);
318 			memcpy(fifo->tx_key + 11,
319 			       tx_key->key, WLAN_KEY_LEN_WEP40);
320 		}
321 
322 		fifo->frag_ctl |= cpu_to_le16(FRAGCTL_LEGACY);
323 		break;
324 	case WLAN_CIPHER_SUITE_TKIP:
325 		ieee80211_get_tkip_p2k(tx_key, skb, fifo->tx_key);
326 
327 		fifo->frag_ctl |= cpu_to_le16(FRAGCTL_TKIP);
328 		break;
329 	case WLAN_CIPHER_SUITE_CCMP:
330 		if (info->control.use_cts_prot) {
331 			if (info->control.use_rts)
332 				mic_hdr = &tx_buffer->tx_head.tx_rts.tx.mic.hdr;
333 			else
334 				mic_hdr = &tx_buffer->tx_head.tx_cts.tx.mic.hdr;
335 		} else {
336 			mic_hdr = &tx_buffer->tx_head.tx_ab.tx.mic.hdr;
337 		}
338 
339 		mic_hdr->id = 0x59;
340 		mic_hdr->payload_len = cpu_to_be16(payload_len);
341 		ether_addr_copy(mic_hdr->mic_addr2, hdr->addr2);
342 
343 		pn64 = atomic64_read(&tx_key->tx_pn);
344 		mic_hdr->ccmp_pn[5] = pn64;
345 		mic_hdr->ccmp_pn[4] = pn64 >> 8;
346 		mic_hdr->ccmp_pn[3] = pn64 >> 16;
347 		mic_hdr->ccmp_pn[2] = pn64 >> 24;
348 		mic_hdr->ccmp_pn[1] = pn64 >> 32;
349 		mic_hdr->ccmp_pn[0] = pn64 >> 40;
350 
351 		if (ieee80211_has_a4(hdr->frame_control))
352 			mic_hdr->hlen = cpu_to_be16(28);
353 		else
354 			mic_hdr->hlen = cpu_to_be16(22);
355 
356 		ether_addr_copy(mic_hdr->addr1, hdr->addr1);
357 		ether_addr_copy(mic_hdr->addr2, hdr->addr2);
358 		ether_addr_copy(mic_hdr->addr3, hdr->addr3);
359 
360 		mic_hdr->frame_control = cpu_to_le16(
361 			le16_to_cpu(hdr->frame_control) & 0xc78f);
362 		mic_hdr->seq_ctrl = cpu_to_le16(
363 				le16_to_cpu(hdr->seq_ctrl) & 0xf);
364 
365 		if (ieee80211_has_a4(hdr->frame_control))
366 			ether_addr_copy(mic_hdr->addr4, hdr->addr4);
367 
368 		memcpy(fifo->tx_key, tx_key->key, WLAN_KEY_LEN_CCMP);
369 
370 		fifo->frag_ctl |= cpu_to_le16(FRAGCTL_AES);
371 		return true;
372 	default:
373 		break;
374 	}
375 
376 	return false;
377 }
378 
379 static void vnt_rxtx_rts(struct vnt_usb_send_context *tx_context)
380 {
381 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx_context->skb);
382 	struct vnt_tx_buffer *tx_buffer = tx_context->tx_buffer;
383 	union vnt_tx_head *tx_head = &tx_buffer->tx_head;
384 	struct vnt_rrv_time_rts *buf = &tx_head->tx_rts.rts;
385 	union vnt_tx_data_head *head = &tx_head->tx_rts.tx.head;
386 
387 	buf->rts_rrv_time_aa = vnt_get_rts_duration(tx_context);
388 	buf->rts_rrv_time_ba = buf->rts_rrv_time_aa;
389 	buf->rts_rrv_time_bb = buf->rts_rrv_time_aa;
390 
391 	buf->rrv_time_a = vnt_rxtx_rsvtime_le16(tx_context);
392 	buf->rrv_time_b = buf->rrv_time_a;
393 
394 	if (info->control.hw_key) {
395 		if (vnt_fill_txkey(tx_buffer, tx_context->skb))
396 			head = &tx_head->tx_rts.tx.mic.head;
397 	}
398 
399 	vnt_rxtx_rts_g_head(tx_context, &head->rts_g);
400 }
401 
402 static void vnt_rxtx_cts(struct vnt_usb_send_context *tx_context)
403 {
404 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx_context->skb);
405 	struct vnt_tx_buffer *tx_buffer = tx_context->tx_buffer;
406 	union vnt_tx_head *tx_head = &tx_buffer->tx_head;
407 	struct vnt_rrv_time_cts *buf = &tx_head->tx_cts.cts;
408 	union vnt_tx_data_head *head = &tx_head->tx_cts.tx.head;
409 
410 	buf->rrv_time_a = vnt_rxtx_rsvtime_le16(tx_context);
411 	buf->rrv_time_b = buf->rrv_time_a;
412 
413 	buf->cts_rrv_time_ba = vnt_get_cts_duration(tx_context);
414 
415 	if (info->control.hw_key) {
416 		if (vnt_fill_txkey(tx_buffer, tx_context->skb))
417 			head = &tx_head->tx_cts.tx.mic.head;
418 	}
419 
420 	vnt_fill_cts_head(tx_context, head);
421 }
422 
423 static void vnt_rxtx_ab(struct vnt_usb_send_context *tx_context)
424 {
425 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx_context->skb);
426 	struct vnt_tx_buffer *tx_buffer = tx_context->tx_buffer;
427 	union vnt_tx_head *tx_head = &tx_buffer->tx_head;
428 	struct vnt_rrv_time_ab *buf = &tx_head->tx_ab.ab;
429 	union vnt_tx_data_head *head = &tx_head->tx_ab.tx.head;
430 
431 	buf->rrv_time = vnt_rxtx_rsvtime_le16(tx_context);
432 
433 	if (info->control.hw_key) {
434 		if (vnt_fill_txkey(tx_buffer, tx_context->skb))
435 			head = &tx_head->tx_ab.tx.mic.head;
436 	}
437 
438 	if (info->control.use_rts) {
439 		buf->rts_rrv_time = vnt_get_rts_duration(tx_context);
440 
441 		vnt_rxtx_rts_ab_head(tx_context, &head->rts_ab);
442 
443 		return;
444 	}
445 
446 	vnt_rxtx_datahead_ab(tx_context, &head->data_head_ab);
447 }
448 
449 static void vnt_generate_tx_parameter(struct vnt_usb_send_context *tx_context)
450 {
451 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx_context->skb);
452 
453 	if (info->control.use_cts_prot) {
454 		if (info->control.use_rts) {
455 			vnt_rxtx_rts(tx_context);
456 
457 			return;
458 		}
459 
460 		vnt_rxtx_cts(tx_context);
461 
462 		return;
463 	}
464 
465 	vnt_rxtx_ab(tx_context);
466 }
467 
468 static u16 vnt_get_hdr_size(struct ieee80211_tx_info *info)
469 {
470 	u16 size = sizeof(struct vnt_tx_datahead_ab);
471 
472 	if (info->control.use_cts_prot) {
473 		if (info->control.use_rts)
474 			size = sizeof(struct vnt_rts_g);
475 		else
476 			size = sizeof(struct vnt_cts);
477 	} else if (info->control.use_rts) {
478 		size = sizeof(struct vnt_rts_ab);
479 	}
480 
481 	if (info->control.hw_key) {
482 		if (info->control.hw_key->cipher == WLAN_CIPHER_SUITE_CCMP)
483 			size += sizeof(struct vnt_mic_hdr);
484 	}
485 
486 	/* Get rrv_time header */
487 	if (info->control.use_cts_prot) {
488 		if (info->control.use_rts)
489 			size += sizeof(struct vnt_rrv_time_rts);
490 		else
491 			size += sizeof(struct vnt_rrv_time_cts);
492 	} else {
493 		size += sizeof(struct vnt_rrv_time_ab);
494 	}
495 
496 	size += sizeof(struct vnt_tx_fifo_head);
497 
498 	return size;
499 }
500 
501 int vnt_tx_packet(struct vnt_private *priv, struct sk_buff *skb)
502 {
503 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
504 	struct ieee80211_tx_rate *tx_rate = &info->control.rates[0];
505 	struct ieee80211_rate *rate;
506 	struct ieee80211_hdr *hdr;
507 	struct vnt_tx_buffer *tx_buffer;
508 	struct vnt_tx_fifo_head *tx_buffer_head;
509 	struct vnt_usb_send_context *tx_context;
510 	unsigned long flags;
511 	u8 pkt_type;
512 
513 	hdr = (struct ieee80211_hdr *)(skb->data);
514 
515 	rate = ieee80211_get_tx_rate(priv->hw, info);
516 
517 	if (rate->hw_value > RATE_11M) {
518 		if (info->band == NL80211_BAND_5GHZ) {
519 			pkt_type = PK_TYPE_11A;
520 		} else {
521 			if (tx_rate->flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
522 				if (priv->basic_rates & VNT_B_RATES)
523 					pkt_type = PK_TYPE_11GB;
524 				else
525 					pkt_type = PK_TYPE_11GA;
526 			} else {
527 				pkt_type = PK_TYPE_11A;
528 			}
529 		}
530 	} else {
531 		pkt_type = PK_TYPE_11B;
532 	}
533 
534 	spin_lock_irqsave(&priv->lock, flags);
535 
536 	tx_context = vnt_get_free_context(priv);
537 	if (!tx_context) {
538 		dev_dbg(&priv->usb->dev, "%s No free context\n", __func__);
539 		spin_unlock_irqrestore(&priv->lock, flags);
540 		return -ENOMEM;
541 	}
542 
543 	tx_context->pkt_type = pkt_type;
544 	tx_context->frame_len = skb->len + 4;
545 	tx_context->tx_rate =  rate->hw_value;
546 
547 	spin_unlock_irqrestore(&priv->lock, flags);
548 
549 	tx_context->skb = skb_clone(skb, GFP_ATOMIC);
550 	if (!tx_context->skb) {
551 		tx_context->in_use = false;
552 		return -ENOMEM;
553 	}
554 
555 	tx_buffer = skb_push(skb, vnt_get_hdr_size(info));
556 	tx_context->tx_buffer = tx_buffer;
557 	tx_buffer_head = &tx_buffer->fifo_head;
558 
559 	tx_context->type = CONTEXT_DATA_PACKET;
560 
561 	/*Set fifo controls */
562 	if (pkt_type == PK_TYPE_11A)
563 		tx_buffer_head->fifo_ctl = 0;
564 	else if (pkt_type == PK_TYPE_11B)
565 		tx_buffer_head->fifo_ctl = cpu_to_le16(FIFOCTL_11B);
566 	else if (pkt_type == PK_TYPE_11GB)
567 		tx_buffer_head->fifo_ctl = cpu_to_le16(FIFOCTL_11GB);
568 	else if (pkt_type == PK_TYPE_11GA)
569 		tx_buffer_head->fifo_ctl = cpu_to_le16(FIFOCTL_11GA);
570 
571 	if (!ieee80211_is_data(hdr->frame_control)) {
572 		tx_buffer_head->fifo_ctl |= cpu_to_le16(FIFOCTL_GENINT |
573 							FIFOCTL_ISDMA0);
574 		tx_buffer_head->fifo_ctl |= cpu_to_le16(FIFOCTL_TMOEN);
575 
576 		tx_buffer_head->time_stamp =
577 			cpu_to_le16(DEFAULT_MGN_LIFETIME_RES_64us);
578 	} else {
579 		tx_buffer_head->time_stamp =
580 			cpu_to_le16(DEFAULT_MSDU_LIFETIME_RES_64us);
581 	}
582 
583 	if (!(info->flags & IEEE80211_TX_CTL_NO_ACK))
584 		tx_buffer_head->fifo_ctl |= cpu_to_le16(FIFOCTL_NEEDACK);
585 
586 	if (ieee80211_has_retry(hdr->frame_control))
587 		tx_buffer_head->fifo_ctl |= cpu_to_le16(FIFOCTL_LRETRY);
588 
589 	if (info->control.use_rts)
590 		tx_buffer_head->fifo_ctl |= cpu_to_le16(FIFOCTL_RTS);
591 
592 	if (ieee80211_has_a4(hdr->frame_control))
593 		tx_buffer_head->fifo_ctl |= cpu_to_le16(FIFOCTL_LHEAD);
594 
595 	tx_buffer_head->frag_ctl =
596 			cpu_to_le16(ieee80211_hdrlen(hdr->frame_control) << 10);
597 
598 	if (info->control.hw_key)
599 		tx_context->frame_len += info->control.hw_key->icv_len;
600 
601 	tx_buffer_head->current_rate = cpu_to_le16(rate->hw_value);
602 
603 	vnt_generate_tx_parameter(tx_context);
604 
605 	tx_buffer_head->frag_ctl |= cpu_to_le16(FRAGCTL_NONFRAG);
606 
607 	priv->seq_counter = (le16_to_cpu(hdr->seq_ctrl) &
608 						IEEE80211_SCTL_SEQ) >> 4;
609 
610 	spin_lock_irqsave(&priv->lock, flags);
611 
612 	if (vnt_tx_context(priv, tx_context, skb)) {
613 		dev_kfree_skb(tx_context->skb);
614 		spin_unlock_irqrestore(&priv->lock, flags);
615 		return -EIO;
616 	}
617 
618 	dev_kfree_skb(skb);
619 
620 	spin_unlock_irqrestore(&priv->lock, flags);
621 
622 	return 0;
623 }
624 
625 static int vnt_beacon_xmit(struct vnt_private *priv, struct sk_buff *skb)
626 {
627 	struct vnt_tx_short_buf_head *short_head;
628 	struct ieee80211_tx_info *info;
629 	struct vnt_usb_send_context *context;
630 	struct ieee80211_mgmt *mgmt_hdr;
631 	unsigned long flags;
632 	u32 frame_size = skb->len + 4;
633 	u16 current_rate;
634 
635 	spin_lock_irqsave(&priv->lock, flags);
636 
637 	context = vnt_get_free_context(priv);
638 	if (!context) {
639 		dev_dbg(&priv->usb->dev, "%s No free context!\n", __func__);
640 		spin_unlock_irqrestore(&priv->lock, flags);
641 		return -ENOMEM;
642 	}
643 
644 	context->skb = skb;
645 
646 	spin_unlock_irqrestore(&priv->lock, flags);
647 
648 	mgmt_hdr = (struct ieee80211_mgmt *)skb->data;
649 	short_head = skb_push(skb, sizeof(*short_head));
650 
651 	if (priv->bb_type == BB_TYPE_11A) {
652 		current_rate = RATE_6M;
653 
654 		/* Get SignalField,ServiceField,Length */
655 		vnt_get_phy_field(priv, frame_size, current_rate,
656 				  PK_TYPE_11A, &short_head->ab);
657 
658 		/* Get TimeStampOff */
659 		short_head->time_stamp_off =
660 				vnt_time_stamp_off(priv, current_rate);
661 	} else {
662 		current_rate = RATE_1M;
663 		short_head->fifo_ctl |= cpu_to_le16(FIFOCTL_11B);
664 
665 		/* Get SignalField,ServiceField,Length */
666 		vnt_get_phy_field(priv, frame_size, current_rate,
667 				  PK_TYPE_11B, &short_head->ab);
668 
669 		/* Get TimeStampOff */
670 		short_head->time_stamp_off =
671 			vnt_time_stamp_off(priv, current_rate);
672 	}
673 
674 	/* Get Duration */
675 	short_head->duration = mgmt_hdr->duration;
676 
677 	/* time stamp always 0 */
678 	mgmt_hdr->u.beacon.timestamp = 0;
679 
680 	info = IEEE80211_SKB_CB(skb);
681 	if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
682 		struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)mgmt_hdr;
683 
684 		hdr->duration_id = 0;
685 		hdr->seq_ctrl = cpu_to_le16(priv->seq_counter << 4);
686 	}
687 
688 	priv->seq_counter++;
689 	if (priv->seq_counter > 0x0fff)
690 		priv->seq_counter = 0;
691 
692 	context->type = CONTEXT_BEACON_PACKET;
693 
694 	spin_lock_irqsave(&priv->lock, flags);
695 
696 	if (vnt_tx_context(priv, context, skb))
697 		ieee80211_free_txskb(priv->hw, context->skb);
698 
699 	spin_unlock_irqrestore(&priv->lock, flags);
700 
701 	return 0;
702 }
703 
704 int vnt_beacon_make(struct vnt_private *priv, struct ieee80211_vif *vif)
705 {
706 	struct sk_buff *beacon;
707 
708 	beacon = ieee80211_beacon_get(priv->hw, vif);
709 	if (!beacon)
710 		return -ENOMEM;
711 
712 	if (vnt_beacon_xmit(priv, beacon)) {
713 		ieee80211_free_txskb(priv->hw, beacon);
714 		return -ENODEV;
715 	}
716 
717 	return 0;
718 }
719 
720 int vnt_beacon_enable(struct vnt_private *priv, struct ieee80211_vif *vif,
721 		      struct ieee80211_bss_conf *conf)
722 {
723 	vnt_mac_reg_bits_off(priv, MAC_REG_TCR, TCR_AUTOBCNTX);
724 
725 	vnt_mac_reg_bits_off(priv, MAC_REG_TFTCTL, TFTCTL_TSFCNTREN);
726 
727 	vnt_mac_set_beacon_interval(priv, conf->beacon_int);
728 
729 	vnt_clear_current_tsf(priv);
730 
731 	vnt_mac_reg_bits_on(priv, MAC_REG_TFTCTL, TFTCTL_TSFCNTREN);
732 
733 	vnt_reset_next_tbtt(priv, conf->beacon_int);
734 
735 	return vnt_beacon_make(priv, vif);
736 }
737