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