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