OSDN Git Service

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