]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/vt6656/rxtx.c
Merge tag 'drm-intel-next-2015-09-11' of git://anongit.freedesktop.org/drm-intel...
[karo-tx-linux.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  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * File: rxtx.c
20  *
21  * Purpose: handle WMAC/802.3/802.11 rx & tx functions
22  *
23  * Author: Lyndon Chen
24  *
25  * Date: May 20, 2003
26  *
27  * Functions:
28  *      vnt_generate_tx_parameter - Generate tx dma required parameter.
29  *      vnt_get_duration_le - get tx data required duration
30  *      vnt_get_rtscts_duration_le- get rtx/cts required duration
31  *      vnt_get_rtscts_rsvtime_le- get rts/cts reserved time
32  *      vnt_get_rsvtime- get frame reserved time
33  *      vnt_fill_cts_head- fulfill CTS ctl header
34  *
35  * Revision History:
36  *
37  */
38
39 #include <linux/etherdevice.h>
40 #include "device.h"
41 #include "rxtx.h"
42 #include "card.h"
43 #include "mac.h"
44 #include "rf.h"
45 #include "usbpipe.h"
46
47 static const u16 vnt_time_stampoff[2][MAX_RATE] = {
48         /* Long Preamble */
49         {384, 288, 226, 209, 54, 43, 37, 31, 28, 25, 24, 23},
50
51         /* Short Preamble */
52         {384, 192, 130, 113, 54, 43, 37, 31, 28, 25, 24, 23},
53 };
54
55 static const u16 vnt_fb_opt0[2][5] = {
56         {RATE_12M, RATE_18M, RATE_24M, RATE_36M, RATE_48M}, /* fallback_rate0 */
57         {RATE_12M, RATE_12M, RATE_18M, RATE_24M, RATE_36M}, /* fallback_rate1 */
58 };
59
60 static const u16 vnt_fb_opt1[2][5] = {
61         {RATE_12M, RATE_18M, RATE_24M, RATE_24M, RATE_36M}, /* fallback_rate0 */
62         {RATE_6M,  RATE_6M,  RATE_12M, RATE_12M, RATE_18M}, /* fallback_rate1 */
63 };
64
65 #define RTSDUR_BB       0
66 #define RTSDUR_BA       1
67 #define RTSDUR_AA       2
68 #define CTSDUR_BA       3
69 #define RTSDUR_BA_F0    4
70 #define RTSDUR_AA_F0    5
71 #define RTSDUR_BA_F1    6
72 #define RTSDUR_AA_F1    7
73 #define CTSDUR_BA_F0    8
74 #define CTSDUR_BA_F1    9
75 #define DATADUR_B       10
76 #define DATADUR_A       11
77 #define DATADUR_A_F0    12
78 #define DATADUR_A_F1    13
79
80 static struct vnt_usb_send_context
81         *vnt_get_free_context(struct vnt_private *priv)
82 {
83         struct vnt_usb_send_context *context = NULL;
84         int ii;
85
86         dev_dbg(&priv->usb->dev, "%s\n", __func__);
87
88         for (ii = 0; ii < priv->num_tx_context; ii++) {
89                 if (!priv->tx_context[ii])
90                         return NULL;
91
92                 context = priv->tx_context[ii];
93                 if (!context->in_use) {
94                         context->in_use = true;
95                         memset(context->data, 0,
96                                         MAX_TOTAL_SIZE_WITH_ALL_HEADERS);
97
98                         context->hdr = NULL;
99
100                         return context;
101                 }
102         }
103
104         if (ii == priv->num_tx_context)
105                 dev_dbg(&priv->usb->dev, "%s No Free Tx Context\n", __func__);
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_pspoll(hdr->frame_control)) {
281                 __le16 dur = cpu_to_le16(priv->current_aid | BIT(14) | BIT(15));
282
283                 buf->duration_a = dur;
284                 buf->duration_b = dur;
285         } else {
286                 buf->duration_a = vnt_get_duration_le(priv,
287                                                 tx_context->pkt_type, need_ack);
288                 buf->duration_b = vnt_get_duration_le(priv,
289                                                         PK_TYPE_11B, need_ack);
290         }
291
292         buf->time_stamp_off_a = vnt_time_stamp_off(priv, rate);
293         buf->time_stamp_off_b = vnt_time_stamp_off(priv,
294                                         priv->top_cck_basic_rate);
295
296         tx_context->tx_hdr_size = vnt_mac_hdr_pos(tx_context, &buf->hdr);
297
298         return le16_to_cpu(buf->duration_a);
299 }
300
301 static u16 vnt_rxtx_datahead_g_fb(struct vnt_usb_send_context *tx_context,
302                                   struct vnt_tx_datahead_g_fb *buf)
303 {
304         struct vnt_private *priv = tx_context->priv;
305         u32 frame_len = tx_context->frame_len;
306         u16 rate = tx_context->tx_rate;
307         u8 need_ack = tx_context->need_ack;
308
309         /* Get SignalField,ServiceField,Length */
310         vnt_get_phy_field(priv, frame_len, rate, tx_context->pkt_type, &buf->a);
311
312         vnt_get_phy_field(priv, frame_len, priv->top_cck_basic_rate,
313                                                 PK_TYPE_11B, &buf->b);
314
315         /* Get Duration and TimeStamp */
316         buf->duration_a = vnt_get_duration_le(priv, tx_context->pkt_type,
317                                               need_ack);
318         buf->duration_b = vnt_get_duration_le(priv, PK_TYPE_11B, need_ack);
319
320         buf->duration_a_f0 = vnt_get_duration_le(priv, tx_context->pkt_type,
321                                                  need_ack);
322         buf->duration_a_f1 = vnt_get_duration_le(priv, tx_context->pkt_type,
323                                                  need_ack);
324
325         buf->time_stamp_off_a = vnt_time_stamp_off(priv, rate);
326         buf->time_stamp_off_b = vnt_time_stamp_off(priv,
327                                                 priv->top_cck_basic_rate);
328
329         tx_context->tx_hdr_size = vnt_mac_hdr_pos(tx_context, &buf->hdr);
330
331         return le16_to_cpu(buf->duration_a);
332 }
333
334 static u16 vnt_rxtx_datahead_a_fb(struct vnt_usb_send_context *tx_context,
335                                   struct vnt_tx_datahead_a_fb *buf)
336 {
337         struct vnt_private *priv = tx_context->priv;
338         u16 rate = tx_context->tx_rate;
339         u8 pkt_type = tx_context->pkt_type;
340         u8 need_ack = tx_context->need_ack;
341         u32 frame_len = tx_context->frame_len;
342
343         /* Get SignalField,ServiceField,Length */
344         vnt_get_phy_field(priv, frame_len, rate, pkt_type, &buf->a);
345         /* Get Duration and TimeStampOff */
346         buf->duration = vnt_get_duration_le(priv, pkt_type, need_ack);
347
348         buf->duration_f0 = vnt_get_duration_le(priv, pkt_type, need_ack);
349         buf->duration_f1 = vnt_get_duration_le(priv, pkt_type, need_ack);
350
351         buf->time_stamp_off = vnt_time_stamp_off(priv, rate);
352
353         tx_context->tx_hdr_size = vnt_mac_hdr_pos(tx_context, &buf->hdr);
354
355         return le16_to_cpu(buf->duration);
356 }
357
358 static u16 vnt_rxtx_datahead_ab(struct vnt_usb_send_context *tx_context,
359                                 struct vnt_tx_datahead_ab *buf)
360 {
361         struct vnt_private *priv = tx_context->priv;
362         struct ieee80211_hdr *hdr =
363                                 (struct ieee80211_hdr *)tx_context->skb->data;
364         u32 frame_len = tx_context->frame_len;
365         u16 rate = tx_context->tx_rate;
366         u8 need_ack = tx_context->need_ack;
367
368         /* Get SignalField,ServiceField,Length */
369         vnt_get_phy_field(priv, frame_len, rate,
370                           tx_context->pkt_type, &buf->ab);
371
372         /* Get Duration and TimeStampOff */
373         if (ieee80211_is_pspoll(hdr->frame_control)) {
374                 __le16 dur = cpu_to_le16(priv->current_aid | BIT(14) | BIT(15));
375
376                 buf->duration = dur;
377         } else {
378                 buf->duration = vnt_get_duration_le(priv, tx_context->pkt_type,
379                                                     need_ack);
380         }
381
382         buf->time_stamp_off = vnt_time_stamp_off(priv, rate);
383
384         tx_context->tx_hdr_size = vnt_mac_hdr_pos(tx_context, &buf->hdr);
385
386         return le16_to_cpu(buf->duration);
387 }
388
389 static int vnt_fill_ieee80211_rts(struct vnt_usb_send_context *tx_context,
390         struct ieee80211_rts *rts, __le16 duration)
391 {
392         struct ieee80211_hdr *hdr =
393                                 (struct ieee80211_hdr *)tx_context->skb->data;
394
395         rts->duration = duration;
396         rts->frame_control =
397                 cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS);
398
399         ether_addr_copy(rts->ra, hdr->addr1);
400         ether_addr_copy(rts->ta, hdr->addr2);
401
402         return 0;
403 }
404
405 static u16 vnt_rxtx_rts_g_head(struct vnt_usb_send_context *tx_context,
406                                struct vnt_rts_g *buf)
407 {
408         struct vnt_private *priv = tx_context->priv;
409         u16 rts_frame_len = 20;
410         u16 current_rate = tx_context->tx_rate;
411
412         vnt_get_phy_field(priv, rts_frame_len, priv->top_cck_basic_rate,
413                 PK_TYPE_11B, &buf->b);
414         vnt_get_phy_field(priv, rts_frame_len, priv->top_ofdm_basic_rate,
415                           tx_context->pkt_type, &buf->a);
416
417         buf->duration_bb = vnt_get_rtscts_duration_le(tx_context, RTSDUR_BB,
418                                                       PK_TYPE_11B,
419                                                       priv->top_cck_basic_rate);
420         buf->duration_aa = vnt_get_rtscts_duration_le(tx_context, RTSDUR_AA,
421                                                       tx_context->pkt_type,
422                                                       current_rate);
423         buf->duration_ba = vnt_get_rtscts_duration_le(tx_context, RTSDUR_BA,
424                                                       tx_context->pkt_type,
425                                                       current_rate);
426
427         vnt_fill_ieee80211_rts(tx_context, &buf->data, buf->duration_aa);
428
429         return vnt_rxtx_datahead_g(tx_context, &buf->data_head);
430 }
431
432 static u16 vnt_rxtx_rts_g_fb_head(struct vnt_usb_send_context *tx_context,
433                                   struct vnt_rts_g_fb *buf)
434 {
435         struct vnt_private *priv = tx_context->priv;
436         u16 current_rate = tx_context->tx_rate;
437         u16 rts_frame_len = 20;
438
439         vnt_get_phy_field(priv, rts_frame_len, priv->top_cck_basic_rate,
440                 PK_TYPE_11B, &buf->b);
441         vnt_get_phy_field(priv, rts_frame_len, priv->top_ofdm_basic_rate,
442                           tx_context->pkt_type, &buf->a);
443
444         buf->duration_bb = vnt_get_rtscts_duration_le(tx_context, RTSDUR_BB,
445                                                       PK_TYPE_11B,
446                                                       priv->top_cck_basic_rate);
447         buf->duration_aa = vnt_get_rtscts_duration_le(tx_context, RTSDUR_AA,
448                                                       tx_context->pkt_type,
449                                                       current_rate);
450         buf->duration_ba = vnt_get_rtscts_duration_le(tx_context, RTSDUR_BA,
451                                                       tx_context->pkt_type,
452                                                       current_rate);
453
454         buf->rts_duration_ba_f0 =
455                 vnt_get_rtscts_duration_le(tx_context, RTSDUR_BA_F0,
456                                            tx_context->pkt_type,
457                                            priv->tx_rate_fb0);
458         buf->rts_duration_aa_f0 =
459                 vnt_get_rtscts_duration_le(tx_context, RTSDUR_AA_F0,
460                                            tx_context->pkt_type,
461                                            priv->tx_rate_fb0);
462         buf->rts_duration_ba_f1 =
463                 vnt_get_rtscts_duration_le(tx_context, RTSDUR_BA_F1,
464                                            tx_context->pkt_type,
465                                            priv->tx_rate_fb1);
466         buf->rts_duration_aa_f1 =
467                 vnt_get_rtscts_duration_le(tx_context, RTSDUR_AA_F1,
468                                            tx_context->pkt_type,
469                                            priv->tx_rate_fb1);
470
471         vnt_fill_ieee80211_rts(tx_context, &buf->data, buf->duration_aa);
472
473         return vnt_rxtx_datahead_g_fb(tx_context, &buf->data_head);
474 }
475
476 static u16 vnt_rxtx_rts_ab_head(struct vnt_usb_send_context *tx_context,
477                                 struct vnt_rts_ab *buf)
478 {
479         struct vnt_private *priv = tx_context->priv;
480         u16 current_rate = tx_context->tx_rate;
481         u16 rts_frame_len = 20;
482
483         vnt_get_phy_field(priv, rts_frame_len, priv->top_ofdm_basic_rate,
484                           tx_context->pkt_type, &buf->ab);
485
486         buf->duration = vnt_get_rtscts_duration_le(tx_context, RTSDUR_AA,
487                                                    tx_context->pkt_type,
488                                                    current_rate);
489
490         vnt_fill_ieee80211_rts(tx_context, &buf->data, buf->duration);
491
492         return vnt_rxtx_datahead_ab(tx_context, &buf->data_head);
493 }
494
495 static u16 vnt_rxtx_rts_a_fb_head(struct vnt_usb_send_context *tx_context,
496                                   struct vnt_rts_a_fb *buf)
497 {
498         struct vnt_private *priv = tx_context->priv;
499         u16 current_rate = tx_context->tx_rate;
500         u16 rts_frame_len = 20;
501
502         vnt_get_phy_field(priv, rts_frame_len,
503                 priv->top_ofdm_basic_rate, tx_context->pkt_type, &buf->a);
504
505         buf->duration = vnt_get_rtscts_duration_le(tx_context, RTSDUR_AA,
506                                                    tx_context->pkt_type,
507                                                    current_rate);
508
509         buf->rts_duration_f0 =
510                 vnt_get_rtscts_duration_le(tx_context, RTSDUR_AA_F0,
511                                            tx_context->pkt_type,
512                                            priv->tx_rate_fb0);
513
514         buf->rts_duration_f1 =
515                 vnt_get_rtscts_duration_le(tx_context, RTSDUR_AA_F1,
516                                            tx_context->pkt_type,
517                                            priv->tx_rate_fb1);
518
519         vnt_fill_ieee80211_rts(tx_context, &buf->data, buf->duration);
520
521         return vnt_rxtx_datahead_a_fb(tx_context, &buf->data_head);
522 }
523
524 static u16 vnt_fill_cts_fb_head(struct vnt_usb_send_context *tx_context,
525                                 union vnt_tx_data_head *head)
526 {
527         struct vnt_private *priv = tx_context->priv;
528         struct vnt_cts_fb *buf = &head->cts_g_fb;
529         u32 cts_frame_len = 14;
530         u16 current_rate = tx_context->tx_rate;
531
532         /* Get SignalField,ServiceField,Length */
533         vnt_get_phy_field(priv, cts_frame_len, priv->top_cck_basic_rate,
534                           PK_TYPE_11B, &buf->b);
535
536         buf->duration_ba =
537                 vnt_get_rtscts_duration_le(tx_context, CTSDUR_BA,
538                                            tx_context->pkt_type,
539                                            current_rate);
540         /* Get CTSDuration_ba_f0 */
541         buf->cts_duration_ba_f0 =
542                 vnt_get_rtscts_duration_le(tx_context, CTSDUR_BA_F0,
543                                            tx_context->pkt_type,
544                                            priv->tx_rate_fb0);
545         /* Get CTSDuration_ba_f1 */
546         buf->cts_duration_ba_f1 =
547                 vnt_get_rtscts_duration_le(tx_context, CTSDUR_BA_F1,
548                                            tx_context->pkt_type,
549                                            priv->tx_rate_fb1);
550         /* Get CTS Frame body */
551         buf->data.duration = buf->duration_ba;
552         buf->data.frame_control =
553                 cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS);
554
555         ether_addr_copy(buf->data.ra, priv->current_net_addr);
556
557         return vnt_rxtx_datahead_g_fb(tx_context, &buf->data_head);
558 }
559
560 static u16 vnt_fill_cts_head(struct vnt_usb_send_context *tx_context,
561                              union vnt_tx_data_head *head)
562 {
563         struct vnt_private *priv = tx_context->priv;
564         struct vnt_cts *buf = &head->cts_g;
565         u32 cts_frame_len = 14;
566         u16 current_rate = tx_context->tx_rate;
567
568         /* Get SignalField,ServiceField,Length */
569         vnt_get_phy_field(priv, cts_frame_len, priv->top_cck_basic_rate,
570                           PK_TYPE_11B, &buf->b);
571         /* Get CTSDuration_ba */
572         buf->duration_ba =
573                 vnt_get_rtscts_duration_le(tx_context, CTSDUR_BA,
574                                            tx_context->pkt_type,
575                                            current_rate);
576         /*Get CTS Frame body*/
577         buf->data.duration = buf->duration_ba;
578         buf->data.frame_control =
579                 cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS);
580
581         ether_addr_copy(buf->data.ra, priv->current_net_addr);
582
583         return vnt_rxtx_datahead_g(tx_context, &buf->data_head);
584 }
585
586 static u16 vnt_rxtx_rts(struct vnt_usb_send_context *tx_context,
587                         union vnt_tx_head *tx_head, bool need_mic)
588 {
589         struct vnt_private *priv = tx_context->priv;
590         struct vnt_rrv_time_rts *buf = &tx_head->tx_rts.rts;
591         union vnt_tx_data_head *head = &tx_head->tx_rts.tx.head;
592         u32 frame_len = tx_context->frame_len;
593         u16 current_rate = tx_context->tx_rate;
594         u8 need_ack = tx_context->need_ack;
595
596         buf->rts_rrv_time_aa = vnt_get_rtscts_rsvtime_le(priv, 2,
597                         tx_context->pkt_type, frame_len, current_rate);
598         buf->rts_rrv_time_ba = vnt_get_rtscts_rsvtime_le(priv, 1,
599                         tx_context->pkt_type, frame_len, current_rate);
600         buf->rts_rrv_time_bb = vnt_get_rtscts_rsvtime_le(priv, 0,
601                         tx_context->pkt_type, frame_len, current_rate);
602
603         buf->rrv_time_a = vnt_rxtx_rsvtime_le16(priv, tx_context->pkt_type,
604                                                 frame_len, current_rate,
605                                                 need_ack);
606         buf->rrv_time_b = vnt_rxtx_rsvtime_le16(priv, PK_TYPE_11B, frame_len,
607                                         priv->top_cck_basic_rate, need_ack);
608
609         if (need_mic)
610                 head = &tx_head->tx_rts.tx.mic.head;
611
612         if (tx_context->fb_option)
613                 return vnt_rxtx_rts_g_fb_head(tx_context, &head->rts_g_fb);
614
615         return vnt_rxtx_rts_g_head(tx_context, &head->rts_g);
616 }
617
618 static u16 vnt_rxtx_cts(struct vnt_usb_send_context *tx_context,
619                         union vnt_tx_head *tx_head, bool need_mic)
620 {
621         struct vnt_private *priv = tx_context->priv;
622         struct vnt_rrv_time_cts *buf = &tx_head->tx_cts.cts;
623         union vnt_tx_data_head *head = &tx_head->tx_cts.tx.head;
624         u32 frame_len = tx_context->frame_len;
625         u16 current_rate = tx_context->tx_rate;
626         u8 need_ack = tx_context->need_ack;
627
628         buf->rrv_time_a = vnt_rxtx_rsvtime_le16(priv, tx_context->pkt_type,
629                                         frame_len, current_rate, need_ack);
630         buf->rrv_time_b = vnt_rxtx_rsvtime_le16(priv, PK_TYPE_11B,
631                                 frame_len, priv->top_cck_basic_rate, need_ack);
632
633         buf->cts_rrv_time_ba = vnt_get_rtscts_rsvtime_le(priv, 3,
634                         tx_context->pkt_type, frame_len, current_rate);
635
636         if (need_mic)
637                 head = &tx_head->tx_cts.tx.mic.head;
638
639         /* Fill CTS */
640         if (tx_context->fb_option)
641                 return vnt_fill_cts_fb_head(tx_context, head);
642
643         return vnt_fill_cts_head(tx_context, head);
644 }
645
646 static u16 vnt_rxtx_ab(struct vnt_usb_send_context *tx_context,
647                        union vnt_tx_head *tx_head, bool need_rts, bool need_mic)
648 {
649         struct vnt_private *priv = tx_context->priv;
650         struct vnt_rrv_time_ab *buf = &tx_head->tx_ab.ab;
651         union vnt_tx_data_head *head = &tx_head->tx_ab.tx.head;
652         u32 frame_len = tx_context->frame_len;
653         u16 current_rate = tx_context->tx_rate;
654         u8 need_ack = tx_context->need_ack;
655
656         buf->rrv_time = vnt_rxtx_rsvtime_le16(priv, tx_context->pkt_type,
657                         frame_len, current_rate, need_ack);
658
659         if (need_mic)
660                 head = &tx_head->tx_ab.tx.mic.head;
661
662         if (need_rts) {
663                 if (tx_context->pkt_type == PK_TYPE_11B)
664                         buf->rts_rrv_time = vnt_get_rtscts_rsvtime_le(priv, 0,
665                                 tx_context->pkt_type, frame_len, current_rate);
666                 else /* PK_TYPE_11A */
667                         buf->rts_rrv_time = vnt_get_rtscts_rsvtime_le(priv, 2,
668                                 tx_context->pkt_type, frame_len, current_rate);
669
670                 if (tx_context->fb_option &&
671                     tx_context->pkt_type == PK_TYPE_11A)
672                         return vnt_rxtx_rts_a_fb_head(tx_context,
673                                                       &head->rts_a_fb);
674
675                 return vnt_rxtx_rts_ab_head(tx_context, &head->rts_ab);
676         }
677
678         if (tx_context->pkt_type == PK_TYPE_11A)
679                 return vnt_rxtx_datahead_a_fb(tx_context,
680                                               &head->data_head_a_fb);
681
682         return vnt_rxtx_datahead_ab(tx_context, &head->data_head_ab);
683 }
684
685 static u16 vnt_generate_tx_parameter(struct vnt_usb_send_context *tx_context,
686         struct vnt_tx_buffer *tx_buffer,
687         struct vnt_mic_hdr **mic_hdr, u32 need_mic,
688         bool need_rts)
689 {
690
691         if (tx_context->pkt_type == PK_TYPE_11GB ||
692             tx_context->pkt_type == PK_TYPE_11GA) {
693                 if (need_rts) {
694                         if (need_mic)
695                                 *mic_hdr = &tx_buffer->
696                                                 tx_head.tx_rts.tx.mic.hdr;
697
698                         return vnt_rxtx_rts(tx_context, &tx_buffer->tx_head,
699                                             need_mic);
700                 }
701
702                 if (need_mic)
703                         *mic_hdr = &tx_buffer->tx_head.tx_cts.tx.mic.hdr;
704
705                 return vnt_rxtx_cts(tx_context, &tx_buffer->tx_head, need_mic);
706         }
707
708         if (need_mic)
709                 *mic_hdr = &tx_buffer->tx_head.tx_ab.tx.mic.hdr;
710
711         return vnt_rxtx_ab(tx_context, &tx_buffer->tx_head, need_rts, need_mic);
712 }
713
714 static void vnt_fill_txkey(struct vnt_usb_send_context *tx_context,
715         u8 *key_buffer, struct ieee80211_key_conf *tx_key, struct sk_buff *skb,
716         u16 payload_len, struct vnt_mic_hdr *mic_hdr)
717 {
718         struct ieee80211_hdr *hdr = tx_context->hdr;
719         struct ieee80211_key_seq seq;
720         u8 *iv = ((u8 *)hdr + ieee80211_get_hdrlen_from_skb(skb));
721
722         /* strip header and icv len from payload */
723         payload_len -= ieee80211_get_hdrlen_from_skb(skb);
724         payload_len -= tx_key->icv_len;
725
726         switch (tx_key->cipher) {
727         case WLAN_CIPHER_SUITE_WEP40:
728         case WLAN_CIPHER_SUITE_WEP104:
729                 memcpy(key_buffer, iv, 3);
730                 memcpy(key_buffer + 3, tx_key->key, tx_key->keylen);
731
732                 if (tx_key->keylen == WLAN_KEY_LEN_WEP40) {
733                         memcpy(key_buffer + 8, iv, 3);
734                         memcpy(key_buffer + 11,
735                                         tx_key->key, WLAN_KEY_LEN_WEP40);
736                 }
737
738                 break;
739         case WLAN_CIPHER_SUITE_TKIP:
740                 ieee80211_get_tkip_p2k(tx_key, skb, key_buffer);
741
742                 break;
743         case WLAN_CIPHER_SUITE_CCMP:
744
745                 if (!mic_hdr)
746                         return;
747
748                 mic_hdr->id = 0x59;
749                 mic_hdr->payload_len = cpu_to_be16(payload_len);
750                 ether_addr_copy(mic_hdr->mic_addr2, hdr->addr2);
751
752                 ieee80211_get_key_tx_seq(tx_key, &seq);
753
754                 memcpy(mic_hdr->ccmp_pn, seq.ccmp.pn, IEEE80211_CCMP_PN_LEN);
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 == IEEE80211_BAND_5GHZ) {
813                         pkt_type = PK_TYPE_11A;
814                 } else {
815                         if (tx_rate->flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
816                                 pkt_type = PK_TYPE_11GB;
817                         else
818                                 pkt_type = PK_TYPE_11GA;
819                 }
820         } else {
821                 pkt_type = PK_TYPE_11B;
822         }
823
824         spin_lock_irqsave(&priv->lock, flags);
825
826         tx_context = vnt_get_free_context(priv);
827         if (!tx_context) {
828                 dev_dbg(&priv->usb->dev, "%s No free context\n", __func__);
829                 spin_unlock_irqrestore(&priv->lock, flags);
830                 return -ENOMEM;
831         }
832
833         tx_context->skb = skb;
834         tx_context->pkt_type = pkt_type;
835         tx_context->need_ack = false;
836         tx_context->frame_len = skb->len + 4;
837         tx_context->tx_rate = current_rate;
838
839         spin_unlock_irqrestore(&priv->lock, flags);
840
841         tx_buffer = (struct vnt_tx_buffer *)tx_context->data;
842         tx_buffer_head = &tx_buffer->fifo_head;
843         tx_body_size = skb->len;
844
845         /*Set fifo controls */
846         if (pkt_type == PK_TYPE_11A)
847                 tx_buffer_head->fifo_ctl = 0;
848         else if (pkt_type == PK_TYPE_11B)
849                 tx_buffer_head->fifo_ctl = cpu_to_le16(FIFOCTL_11B);
850         else if (pkt_type == PK_TYPE_11GB)
851                 tx_buffer_head->fifo_ctl = cpu_to_le16(FIFOCTL_11GB);
852         else if (pkt_type == PK_TYPE_11GA)
853                 tx_buffer_head->fifo_ctl = cpu_to_le16(FIFOCTL_11GA);
854
855         if (!ieee80211_is_data(hdr->frame_control)) {
856                 tx_buffer_head->fifo_ctl |= cpu_to_le16(FIFOCTL_GENINT |
857                                                         FIFOCTL_ISDMA0);
858                 tx_buffer_head->fifo_ctl |= cpu_to_le16(FIFOCTL_TMOEN);
859
860                 tx_buffer_head->time_stamp =
861                         cpu_to_le16(DEFAULT_MGN_LIFETIME_RES_64us);
862         } else {
863                 tx_buffer_head->time_stamp =
864                         cpu_to_le16(DEFAULT_MSDU_LIFETIME_RES_64us);
865         }
866
867         if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
868                 tx_buffer_head->fifo_ctl |= cpu_to_le16(FIFOCTL_NEEDACK);
869                 tx_context->need_ack = true;
870         }
871
872         if (ieee80211_has_retry(hdr->frame_control))
873                 tx_buffer_head->fifo_ctl |= cpu_to_le16(FIFOCTL_LRETRY);
874
875         if (tx_rate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
876                 priv->preamble_type = PREAMBLE_SHORT;
877         else
878                 priv->preamble_type = PREAMBLE_LONG;
879
880         if (tx_rate->flags & IEEE80211_TX_RC_USE_RTS_CTS) {
881                 need_rts = true;
882                 tx_buffer_head->fifo_ctl |= cpu_to_le16(FIFOCTL_RTS);
883         }
884
885         if (ieee80211_has_a4(hdr->frame_control))
886                 tx_buffer_head->fifo_ctl |= cpu_to_le16(FIFOCTL_LHEAD);
887
888         if (info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER)
889                 is_pspoll = true;
890
891         tx_buffer_head->frag_ctl =
892                         cpu_to_le16(ieee80211_get_hdrlen_from_skb(skb) << 10);
893
894         if (info->control.hw_key) {
895                 tx_key = info->control.hw_key;
896                 switch (info->control.hw_key->cipher) {
897                 case WLAN_CIPHER_SUITE_WEP40:
898                 case WLAN_CIPHER_SUITE_WEP104:
899                         tx_buffer_head->frag_ctl |= cpu_to_le16(FRAGCTL_LEGACY);
900                         break;
901                 case WLAN_CIPHER_SUITE_TKIP:
902                         tx_buffer_head->frag_ctl |= cpu_to_le16(FRAGCTL_TKIP);
903                         break;
904                 case WLAN_CIPHER_SUITE_CCMP:
905                         tx_buffer_head->frag_ctl |= cpu_to_le16(FRAGCTL_AES);
906                         need_mic = true;
907                 default:
908                         break;
909                 }
910                 tx_context->frame_len += tx_key->icv_len;
911         }
912
913         tx_buffer_head->current_rate = cpu_to_le16(current_rate);
914
915         /* legacy rates TODO use ieee80211_tx_rate */
916         if (current_rate >= RATE_18M && ieee80211_is_data(hdr->frame_control)) {
917                 if (priv->auto_fb_ctrl == AUTO_FB_0) {
918                         tx_buffer_head->fifo_ctl |=
919                                                 cpu_to_le16(FIFOCTL_AUTO_FB_0);
920
921                         priv->tx_rate_fb0 =
922                                 vnt_fb_opt0[FB_RATE0][current_rate - RATE_18M];
923                         priv->tx_rate_fb1 =
924                                 vnt_fb_opt0[FB_RATE1][current_rate - RATE_18M];
925
926                         fb_option = AUTO_FB_0;
927                 } else if (priv->auto_fb_ctrl == AUTO_FB_1) {
928                         tx_buffer_head->fifo_ctl |=
929                                                 cpu_to_le16(FIFOCTL_AUTO_FB_1);
930
931                         priv->tx_rate_fb0 =
932                                 vnt_fb_opt1[FB_RATE0][current_rate - RATE_18M];
933                         priv->tx_rate_fb1 =
934                                 vnt_fb_opt1[FB_RATE1][current_rate - RATE_18M];
935
936                         fb_option = AUTO_FB_1;
937                 }
938         }
939
940         tx_context->fb_option = fb_option;
941
942         duration_id = vnt_generate_tx_parameter(tx_context, tx_buffer, &mic_hdr,
943                                                 need_mic, need_rts);
944
945         tx_header_size = tx_context->tx_hdr_size;
946         if (!tx_header_size) {
947                 tx_context->in_use = false;
948                 return -ENOMEM;
949         }
950
951         tx_buffer_head->frag_ctl |= cpu_to_le16(FRAGCTL_NONFRAG);
952
953         tx_bytes = tx_header_size + tx_body_size;
954
955         memcpy(tx_context->hdr, skb->data, tx_body_size);
956
957         hdr->duration_id = cpu_to_le16(duration_id);
958
959         if (info->control.hw_key) {
960                 tx_key = info->control.hw_key;
961                 if (tx_key->keylen > 0)
962                         vnt_fill_txkey(tx_context, tx_buffer_head->tx_key,
963                                 tx_key, skb, tx_body_size, mic_hdr);
964         }
965
966         priv->seq_counter = (le16_to_cpu(hdr->seq_ctrl) &
967                                                 IEEE80211_SCTL_SEQ) >> 4;
968
969         tx_buffer->tx_byte_count = cpu_to_le16(tx_bytes);
970         tx_buffer->pkt_no = tx_context->pkt_no;
971         tx_buffer->type = 0x00;
972
973         tx_bytes += 4;
974
975         tx_context->type = CONTEXT_DATA_PACKET;
976         tx_context->buf_len = tx_bytes;
977
978         spin_lock_irqsave(&priv->lock, flags);
979
980         if (vnt_tx_context(priv, tx_context) != STATUS_PENDING) {
981                 spin_unlock_irqrestore(&priv->lock, flags);
982                 return -EIO;
983         }
984
985         spin_unlock_irqrestore(&priv->lock, flags);
986
987         return 0;
988 }
989
990 static int vnt_beacon_xmit(struct vnt_private *priv,
991         struct sk_buff *skb)
992 {
993         struct vnt_beacon_buffer *beacon_buffer;
994         struct vnt_tx_short_buf_head *short_head;
995         struct ieee80211_tx_info *info;
996         struct vnt_usb_send_context *context;
997         struct ieee80211_mgmt *mgmt_hdr;
998         unsigned long flags;
999         u32 frame_size = skb->len + 4;
1000         u16 current_rate, count;
1001
1002         spin_lock_irqsave(&priv->lock, flags);
1003
1004         context = vnt_get_free_context(priv);
1005         if (!context) {
1006                 dev_dbg(&priv->usb->dev, "%s No free context!\n", __func__);
1007                 spin_unlock_irqrestore(&priv->lock, flags);
1008                 return -ENOMEM;
1009         }
1010
1011         context->skb = skb;
1012
1013         spin_unlock_irqrestore(&priv->lock, flags);
1014
1015         beacon_buffer = (struct vnt_beacon_buffer *)&context->data[0];
1016         short_head = &beacon_buffer->short_head;
1017
1018         if (priv->bb_type == BB_TYPE_11A) {
1019                 current_rate = RATE_6M;
1020
1021                 /* Get SignalField,ServiceField,Length */
1022                 vnt_get_phy_field(priv, frame_size, current_rate,
1023                         PK_TYPE_11A, &short_head->ab);
1024
1025                 /* Get Duration and TimeStampOff */
1026                 short_head->duration = vnt_get_duration_le(priv,
1027                                                         PK_TYPE_11A, false);
1028                 short_head->time_stamp_off =
1029                                 vnt_time_stamp_off(priv, current_rate);
1030         } else {
1031                 current_rate = RATE_1M;
1032                 short_head->fifo_ctl |= cpu_to_le16(FIFOCTL_11B);
1033
1034                 /* Get SignalField,ServiceField,Length */
1035                 vnt_get_phy_field(priv, frame_size, current_rate,
1036                                         PK_TYPE_11B, &short_head->ab);
1037
1038                 /* Get Duration and TimeStampOff */
1039                 short_head->duration = vnt_get_duration_le(priv,
1040                                                 PK_TYPE_11B, false);
1041                 short_head->time_stamp_off =
1042                         vnt_time_stamp_off(priv, current_rate);
1043         }
1044
1045         /* Generate Beacon Header */
1046         mgmt_hdr = &beacon_buffer->mgmt_hdr;
1047         memcpy(mgmt_hdr, skb->data, skb->len);
1048
1049         /* time stamp always 0 */
1050         mgmt_hdr->u.beacon.timestamp = 0;
1051
1052         info = IEEE80211_SKB_CB(skb);
1053         if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
1054                 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)mgmt_hdr;
1055
1056                 hdr->duration_id = 0;
1057                 hdr->seq_ctrl = cpu_to_le16(priv->seq_counter << 4);
1058         }
1059
1060         priv->seq_counter++;
1061         if (priv->seq_counter > 0x0fff)
1062                 priv->seq_counter = 0;
1063
1064         count = sizeof(struct vnt_tx_short_buf_head) + skb->len;
1065
1066         beacon_buffer->tx_byte_count = cpu_to_le16(count);
1067         beacon_buffer->pkt_no = context->pkt_no;
1068         beacon_buffer->type = 0x01;
1069
1070         context->type = CONTEXT_BEACON_PACKET;
1071         context->buf_len = count + 4; /* USB header */
1072
1073         spin_lock_irqsave(&priv->lock, flags);
1074
1075         if (vnt_tx_context(priv, context) != STATUS_PENDING)
1076                 ieee80211_free_txskb(priv->hw, context->skb);
1077
1078         spin_unlock_irqrestore(&priv->lock, flags);
1079
1080         return 0;
1081 }
1082
1083 int vnt_beacon_make(struct vnt_private *priv, struct ieee80211_vif *vif)
1084 {
1085         struct sk_buff *beacon;
1086
1087         beacon = ieee80211_beacon_get(priv->hw, vif);
1088         if (!beacon)
1089                 return -ENOMEM;
1090
1091         if (vnt_beacon_xmit(priv, beacon)) {
1092                 ieee80211_free_txskb(priv->hw, beacon);
1093                 return -ENODEV;
1094         }
1095
1096         return 0;
1097 }
1098
1099 int vnt_beacon_enable(struct vnt_private *priv, struct ieee80211_vif *vif,
1100         struct ieee80211_bss_conf *conf)
1101 {
1102         vnt_mac_reg_bits_off(priv, MAC_REG_TCR, TCR_AUTOBCNTX);
1103
1104         vnt_mac_reg_bits_off(priv, MAC_REG_TFTCTL, TFTCTL_TSFCNTREN);
1105
1106         vnt_mac_set_beacon_interval(priv, conf->beacon_int);
1107
1108         vnt_clear_current_tsf(priv);
1109
1110         vnt_mac_reg_bits_on(priv, MAC_REG_TFTCTL, TFTCTL_TSFCNTREN);
1111
1112         vnt_reset_next_tbtt(priv, conf->beacon_int);
1113
1114         return vnt_beacon_make(priv, vif);
1115 }