GNU Linux-libre 4.19.286-gnu1
[releases.git] / drivers / staging / vt6656 / rxtx.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
4  * All rights reserved.
5  *
6  * File: rxtx.c
7  *
8  * Purpose: handle WMAC/802.3/802.11 rx & tx functions
9  *
10  * Author: Lyndon Chen
11  *
12  * Date: May 20, 2003
13  *
14  * Functions:
15  *      vnt_generate_tx_parameter - Generate tx dma required parameter.
16  *      vnt_get_duration_le - get tx data required duration
17  *      vnt_get_rtscts_duration_le- get rtx/cts required duration
18  *      vnt_get_rtscts_rsvtime_le- get rts/cts reserved time
19  *      vnt_get_rsvtime- get frame reserved time
20  *      vnt_fill_cts_head- fulfill CTS ctl header
21  *
22  * Revision History:
23  *
24  */
25
26 #include <linux/etherdevice.h>
27 #include "device.h"
28 #include "rxtx.h"
29 #include "card.h"
30 #include "mac.h"
31 #include "rf.h"
32 #include "usbpipe.h"
33
34 static const u16 vnt_time_stampoff[2][MAX_RATE] = {
35         /* Long Preamble */
36         {384, 288, 226, 209, 54, 43, 37, 31, 28, 25, 24, 23},
37
38         /* Short Preamble */
39         {384, 192, 130, 113, 54, 43, 37, 31, 28, 25, 24, 23},
40 };
41
42 static const u16 vnt_fb_opt0[2][5] = {
43         {RATE_12M, RATE_18M, RATE_24M, RATE_36M, RATE_48M}, /* fallback_rate0 */
44         {RATE_12M, RATE_12M, RATE_18M, RATE_24M, RATE_36M}, /* fallback_rate1 */
45 };
46
47 static const u16 vnt_fb_opt1[2][5] = {
48         {RATE_12M, RATE_18M, RATE_24M, RATE_24M, RATE_36M}, /* fallback_rate0 */
49         {RATE_6M,  RATE_6M,  RATE_12M, RATE_12M, RATE_18M}, /* fallback_rate1 */
50 };
51
52 #define RTSDUR_BB       0
53 #define RTSDUR_BA       1
54 #define RTSDUR_AA       2
55 #define CTSDUR_BA       3
56 #define RTSDUR_BA_F0    4
57 #define RTSDUR_AA_F0    5
58 #define RTSDUR_BA_F1    6
59 #define RTSDUR_AA_F1    7
60 #define CTSDUR_BA_F0    8
61 #define CTSDUR_BA_F1    9
62 #define DATADUR_B       10
63 #define DATADUR_A       11
64 #define DATADUR_A_F0    12
65 #define DATADUR_A_F1    13
66
67 static struct vnt_usb_send_context
68         *vnt_get_free_context(struct vnt_private *priv)
69 {
70         struct vnt_usb_send_context *context = NULL;
71         int ii;
72
73         dev_dbg(&priv->usb->dev, "%s\n", __func__);
74
75         for (ii = 0; ii < priv->num_tx_context; ii++) {
76                 if (!priv->tx_context[ii])
77                         return NULL;
78
79                 context = priv->tx_context[ii];
80                 if (!context->in_use) {
81                         context->in_use = true;
82                         memset(context->data, 0,
83                                MAX_TOTAL_SIZE_WITH_ALL_HEADERS);
84
85                         context->hdr = NULL;
86
87                         return context;
88                 }
89         }
90
91         if (ii == priv->num_tx_context) {
92                 dev_dbg(&priv->usb->dev, "%s No Free Tx Context\n", __func__);
93
94                 ieee80211_stop_queues(priv->hw);
95         }
96
97         return NULL;
98 }
99
100 static __le16 vnt_time_stamp_off(struct vnt_private *priv, u16 rate)
101 {
102         return cpu_to_le16(vnt_time_stampoff[priv->preamble_type % 2]
103                                                         [rate % MAX_RATE]);
104 }
105
106 static u32 vnt_get_rsvtime(struct vnt_private *priv, u8 pkt_type,
107                            u32 frame_length, u16 rate, int need_ack)
108 {
109         u32 data_time, ack_time;
110
111         data_time = vnt_get_frame_time(priv->preamble_type, pkt_type,
112                                        frame_length, rate);
113
114         if (pkt_type == PK_TYPE_11B)
115                 ack_time = vnt_get_frame_time(priv->preamble_type, pkt_type,
116                                               14, (u16)priv->top_cck_basic_rate);
117         else
118                 ack_time = vnt_get_frame_time(priv->preamble_type, pkt_type,
119                                               14, (u16)priv->top_ofdm_basic_rate);
120
121         if (need_ack)
122                 return data_time + priv->sifs + ack_time;
123
124         return data_time;
125 }
126
127 static __le16 vnt_rxtx_rsvtime_le16(struct vnt_private *priv, u8 pkt_type,
128                                     u32 frame_length, u16 rate, int need_ack)
129 {
130         return cpu_to_le16((u16)vnt_get_rsvtime(priv, pkt_type,
131                 frame_length, rate, need_ack));
132 }
133
134 static __le16 vnt_get_rtscts_rsvtime_le(struct vnt_private *priv, u8 rsv_type,
135                                         u8 pkt_type, u32 frame_length,
136                                         u16 current_rate)
137 {
138         u32 rrv_time, rts_time, cts_time, ack_time, data_time;
139
140         rrv_time = 0;
141         rts_time = 0;
142         cts_time = 0;
143         ack_time = 0;
144
145         data_time = vnt_get_frame_time(priv->preamble_type, pkt_type,
146                                        frame_length, current_rate);
147
148         if (rsv_type == 0) {
149                 rts_time = vnt_get_frame_time(priv->preamble_type, pkt_type,
150                                               20, priv->top_cck_basic_rate);
151                 ack_time = vnt_get_frame_time(priv->preamble_type,
152                                               pkt_type, 14,
153                                               priv->top_cck_basic_rate);
154                 cts_time = ack_time;
155
156         } else if (rsv_type == 1) {
157                 rts_time = vnt_get_frame_time(priv->preamble_type, pkt_type,
158                                               20, priv->top_cck_basic_rate);
159                 cts_time = vnt_get_frame_time(priv->preamble_type, pkt_type,
160                                               14, priv->top_cck_basic_rate);
161                 ack_time = vnt_get_frame_time(priv->preamble_type, pkt_type,
162                                               14, priv->top_ofdm_basic_rate);
163         } else if (rsv_type == 2) {
164                 rts_time = vnt_get_frame_time(priv->preamble_type, pkt_type,
165                                               20, priv->top_ofdm_basic_rate);
166                 ack_time = vnt_get_frame_time(priv->preamble_type,
167                                               pkt_type, 14,
168                                               priv->top_ofdm_basic_rate);
169                 cts_time = ack_time;
170
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, u8 pkt_type,
188                                   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,
196                                                       priv->top_cck_basic_rate);
197                 else
198                         ack_time = vnt_get_frame_time(priv->preamble_type,
199                                                       pkt_type, 14,
200                                                       priv->top_ofdm_basic_rate);
201
202                 return cpu_to_le16((u16)(priv->sifs + ack_time));
203         }
204
205         return 0;
206 }
207
208 static __le16 vnt_get_rtscts_duration_le(struct vnt_usb_send_context *context,
209                                          u8 dur_type, u8 pkt_type, u16 rate)
210 {
211         struct vnt_private *priv = context->priv;
212         u32 cts_time = 0, dur_time = 0;
213         u32 frame_length = context->frame_len;
214         u8 need_ack = context->need_ack;
215
216         switch (dur_type) {
217         case RTSDUR_BB:
218         case RTSDUR_BA:
219         case RTSDUR_BA_F0:
220         case RTSDUR_BA_F1:
221                 cts_time = vnt_get_frame_time(priv->preamble_type, pkt_type,
222                                               14, priv->top_cck_basic_rate);
223                 dur_time = cts_time + 2 * priv->sifs +
224                         vnt_get_rsvtime(priv, pkt_type,
225                                         frame_length, rate, need_ack);
226                 break;
227
228         case RTSDUR_AA:
229         case RTSDUR_AA_F0:
230         case RTSDUR_AA_F1:
231                 cts_time = vnt_get_frame_time(priv->preamble_type, pkt_type,
232                                               14, priv->top_ofdm_basic_rate);
233                 dur_time = cts_time + 2 * priv->sifs +
234                         vnt_get_rsvtime(priv, pkt_type,
235                                         frame_length, rate, need_ack);
236                 break;
237
238         case CTSDUR_BA:
239         case CTSDUR_BA_F0:
240         case CTSDUR_BA_F1:
241                 dur_time = priv->sifs + vnt_get_rsvtime(priv,
242                                 pkt_type, frame_length, rate, need_ack);
243                 break;
244
245         default:
246                 break;
247         }
248
249         return cpu_to_le16((u16)dur_time);
250 }
251
252 static u16 vnt_mac_hdr_pos(struct vnt_usb_send_context *tx_context,
253                            struct ieee80211_hdr *hdr)
254 {
255         u8 *head = tx_context->data + offsetof(struct vnt_tx_buffer, fifo_head);
256         u8 *hdr_pos = (u8 *)hdr;
257
258         tx_context->hdr = hdr;
259         if (!tx_context->hdr)
260                 return 0;
261
262         return (u16)(hdr_pos - head);
263 }
264
265 static u16 vnt_rxtx_datahead_g(struct vnt_usb_send_context *tx_context,
266                                struct vnt_tx_datahead_g *buf)
267 {
268         struct vnt_private *priv = tx_context->priv;
269         struct ieee80211_hdr *hdr =
270                                 (struct ieee80211_hdr *)tx_context->skb->data;
271         u32 frame_len = tx_context->frame_len;
272         u16 rate = tx_context->tx_rate;
273         u8 need_ack = tx_context->need_ack;
274
275         /* Get SignalField,ServiceField,Length */
276         vnt_get_phy_field(priv, frame_len, rate, tx_context->pkt_type, &buf->a);
277         vnt_get_phy_field(priv, frame_len, priv->top_cck_basic_rate,
278                           PK_TYPE_11B, &buf->b);
279
280         /* Get Duration and TimeStamp */
281         if (ieee80211_is_nullfunc(hdr->frame_control)) {
282                 buf->duration_a = hdr->duration_id;
283                 buf->duration_b = hdr->duration_id;
284         } else {
285                 buf->duration_a = vnt_get_duration_le(priv,
286                                                 tx_context->pkt_type, need_ack);
287                 buf->duration_b = vnt_get_duration_le(priv,
288                                                         PK_TYPE_11B, need_ack);
289         }
290
291         buf->time_stamp_off_a = vnt_time_stamp_off(priv, rate);
292         buf->time_stamp_off_b = vnt_time_stamp_off(priv,
293                                         priv->top_cck_basic_rate);
294
295         tx_context->tx_hdr_size = vnt_mac_hdr_pos(tx_context, &buf->hdr);
296
297         return le16_to_cpu(buf->duration_a);
298 }
299
300 static u16 vnt_rxtx_datahead_g_fb(struct vnt_usb_send_context *tx_context,
301                                   struct vnt_tx_datahead_g_fb *buf)
302 {
303         struct vnt_private *priv = tx_context->priv;
304         u32 frame_len = tx_context->frame_len;
305         u16 rate = tx_context->tx_rate;
306         u8 need_ack = tx_context->need_ack;
307
308         /* Get SignalField,ServiceField,Length */
309         vnt_get_phy_field(priv, frame_len, rate, tx_context->pkt_type, &buf->a);
310
311         vnt_get_phy_field(priv, frame_len, priv->top_cck_basic_rate,
312                           PK_TYPE_11B, &buf->b);
313
314         /* Get Duration and TimeStamp */
315         buf->duration_a = vnt_get_duration_le(priv, tx_context->pkt_type,
316                                               need_ack);
317         buf->duration_b = vnt_get_duration_le(priv, PK_TYPE_11B, need_ack);
318
319         buf->duration_a_f0 = vnt_get_duration_le(priv, tx_context->pkt_type,
320                                                  need_ack);
321         buf->duration_a_f1 = vnt_get_duration_le(priv, tx_context->pkt_type,
322                                                  need_ack);
323
324         buf->time_stamp_off_a = vnt_time_stamp_off(priv, rate);
325         buf->time_stamp_off_b = vnt_time_stamp_off(priv,
326                                                 priv->top_cck_basic_rate);
327
328         tx_context->tx_hdr_size = vnt_mac_hdr_pos(tx_context, &buf->hdr);
329
330         return le16_to_cpu(buf->duration_a);
331 }
332
333 static u16 vnt_rxtx_datahead_a_fb(struct vnt_usb_send_context *tx_context,
334                                   struct vnt_tx_datahead_a_fb *buf)
335 {
336         struct vnt_private *priv = tx_context->priv;
337         u16 rate = tx_context->tx_rate;
338         u8 pkt_type = tx_context->pkt_type;
339         u8 need_ack = tx_context->need_ack;
340         u32 frame_len = tx_context->frame_len;
341
342         /* Get SignalField,ServiceField,Length */
343         vnt_get_phy_field(priv, frame_len, rate, pkt_type, &buf->a);
344         /* Get Duration and TimeStampOff */
345         buf->duration = vnt_get_duration_le(priv, pkt_type, need_ack);
346
347         buf->duration_f0 = vnt_get_duration_le(priv, pkt_type, need_ack);
348         buf->duration_f1 = vnt_get_duration_le(priv, pkt_type, need_ack);
349
350         buf->time_stamp_off = vnt_time_stamp_off(priv, rate);
351
352         tx_context->tx_hdr_size = vnt_mac_hdr_pos(tx_context, &buf->hdr);
353
354         return le16_to_cpu(buf->duration);
355 }
356
357 static u16 vnt_rxtx_datahead_ab(struct vnt_usb_send_context *tx_context,
358                                 struct vnt_tx_datahead_ab *buf)
359 {
360         struct vnt_private *priv = tx_context->priv;
361         struct ieee80211_hdr *hdr =
362                                 (struct ieee80211_hdr *)tx_context->skb->data;
363         u32 frame_len = tx_context->frame_len;
364         u16 rate = tx_context->tx_rate;
365         u8 need_ack = tx_context->need_ack;
366
367         /* Get SignalField,ServiceField,Length */
368         vnt_get_phy_field(priv, frame_len, rate,
369                           tx_context->pkt_type, &buf->ab);
370
371         /* Get Duration and TimeStampOff */
372         if (ieee80211_is_nullfunc(hdr->frame_control)) {
373                 buf->duration = hdr->duration_id;
374         } else {
375                 buf->duration = vnt_get_duration_le(priv, tx_context->pkt_type,
376                                                     need_ack);
377         }
378
379         buf->time_stamp_off = vnt_time_stamp_off(priv, rate);
380
381         tx_context->tx_hdr_size = vnt_mac_hdr_pos(tx_context, &buf->hdr);
382
383         return le16_to_cpu(buf->duration);
384 }
385
386 static int vnt_fill_ieee80211_rts(struct vnt_usb_send_context *tx_context,
387                                   struct ieee80211_rts *rts, __le16 duration)
388 {
389         struct ieee80211_hdr *hdr =
390                                 (struct ieee80211_hdr *)tx_context->skb->data;
391
392         rts->duration = duration;
393         rts->frame_control =
394                 cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS);
395
396         ether_addr_copy(rts->ra, hdr->addr1);
397         ether_addr_copy(rts->ta, hdr->addr2);
398
399         return 0;
400 }
401
402 static u16 vnt_rxtx_rts_g_head(struct vnt_usb_send_context *tx_context,
403                                struct vnt_rts_g *buf)
404 {
405         struct vnt_private *priv = tx_context->priv;
406         u16 rts_frame_len = 20;
407         u16 current_rate = tx_context->tx_rate;
408
409         vnt_get_phy_field(priv, rts_frame_len, priv->top_cck_basic_rate,
410                           PK_TYPE_11B, &buf->b);
411         vnt_get_phy_field(priv, rts_frame_len, priv->top_ofdm_basic_rate,
412                           tx_context->pkt_type, &buf->a);
413
414         buf->duration_bb = vnt_get_rtscts_duration_le(tx_context, RTSDUR_BB,
415                                                       PK_TYPE_11B,
416                                                       priv->top_cck_basic_rate);
417         buf->duration_aa = vnt_get_rtscts_duration_le(tx_context, RTSDUR_AA,
418                                                       tx_context->pkt_type,
419                                                       current_rate);
420         buf->duration_ba = vnt_get_rtscts_duration_le(tx_context, RTSDUR_BA,
421                                                       tx_context->pkt_type,
422                                                       current_rate);
423
424         vnt_fill_ieee80211_rts(tx_context, &buf->data, buf->duration_aa);
425
426         return vnt_rxtx_datahead_g(tx_context, &buf->data_head);
427 }
428
429 static u16 vnt_rxtx_rts_g_fb_head(struct vnt_usb_send_context *tx_context,
430                                   struct vnt_rts_g_fb *buf)
431 {
432         struct vnt_private *priv = tx_context->priv;
433         u16 current_rate = tx_context->tx_rate;
434         u16 rts_frame_len = 20;
435
436         vnt_get_phy_field(priv, rts_frame_len, priv->top_cck_basic_rate,
437                           PK_TYPE_11B, &buf->b);
438         vnt_get_phy_field(priv, rts_frame_len, priv->top_ofdm_basic_rate,
439                           tx_context->pkt_type, &buf->a);
440
441         buf->duration_bb = vnt_get_rtscts_duration_le(tx_context, RTSDUR_BB,
442                                                       PK_TYPE_11B,
443                                                       priv->top_cck_basic_rate);
444         buf->duration_aa = vnt_get_rtscts_duration_le(tx_context, RTSDUR_AA,
445                                                       tx_context->pkt_type,
446                                                       current_rate);
447         buf->duration_ba = vnt_get_rtscts_duration_le(tx_context, RTSDUR_BA,
448                                                       tx_context->pkt_type,
449                                                       current_rate);
450
451         buf->rts_duration_ba_f0 =
452                 vnt_get_rtscts_duration_le(tx_context, RTSDUR_BA_F0,
453                                            tx_context->pkt_type,
454                                            priv->tx_rate_fb0);
455         buf->rts_duration_aa_f0 =
456                 vnt_get_rtscts_duration_le(tx_context, RTSDUR_AA_F0,
457                                            tx_context->pkt_type,
458                                            priv->tx_rate_fb0);
459         buf->rts_duration_ba_f1 =
460                 vnt_get_rtscts_duration_le(tx_context, RTSDUR_BA_F1,
461                                            tx_context->pkt_type,
462                                            priv->tx_rate_fb1);
463         buf->rts_duration_aa_f1 =
464                 vnt_get_rtscts_duration_le(tx_context, RTSDUR_AA_F1,
465                                            tx_context->pkt_type,
466                                            priv->tx_rate_fb1);
467
468         vnt_fill_ieee80211_rts(tx_context, &buf->data, buf->duration_aa);
469
470         return vnt_rxtx_datahead_g_fb(tx_context, &buf->data_head);
471 }
472
473 static u16 vnt_rxtx_rts_ab_head(struct vnt_usb_send_context *tx_context,
474                                 struct vnt_rts_ab *buf)
475 {
476         struct vnt_private *priv = tx_context->priv;
477         u16 current_rate = tx_context->tx_rate;
478         u16 rts_frame_len = 20;
479
480         vnt_get_phy_field(priv, rts_frame_len, priv->top_ofdm_basic_rate,
481                           tx_context->pkt_type, &buf->ab);
482
483         buf->duration = vnt_get_rtscts_duration_le(tx_context, RTSDUR_AA,
484                                                    tx_context->pkt_type,
485                                                    current_rate);
486
487         vnt_fill_ieee80211_rts(tx_context, &buf->data, buf->duration);
488
489         return vnt_rxtx_datahead_ab(tx_context, &buf->data_head);
490 }
491
492 static u16 vnt_rxtx_rts_a_fb_head(struct vnt_usb_send_context *tx_context,
493                                   struct vnt_rts_a_fb *buf)
494 {
495         struct vnt_private *priv = tx_context->priv;
496         u16 current_rate = tx_context->tx_rate;
497         u16 rts_frame_len = 20;
498
499         vnt_get_phy_field(priv, rts_frame_len, priv->top_ofdm_basic_rate,
500                           tx_context->pkt_type, &buf->a);
501
502         buf->duration = vnt_get_rtscts_duration_le(tx_context, RTSDUR_AA,
503                                                    tx_context->pkt_type,
504                                                    current_rate);
505
506         buf->rts_duration_f0 =
507                 vnt_get_rtscts_duration_le(tx_context, RTSDUR_AA_F0,
508                                            tx_context->pkt_type,
509                                            priv->tx_rate_fb0);
510
511         buf->rts_duration_f1 =
512                 vnt_get_rtscts_duration_le(tx_context, RTSDUR_AA_F1,
513                                            tx_context->pkt_type,
514                                            priv->tx_rate_fb1);
515
516         vnt_fill_ieee80211_rts(tx_context, &buf->data, buf->duration);
517
518         return vnt_rxtx_datahead_a_fb(tx_context, &buf->data_head);
519 }
520
521 static u16 vnt_fill_cts_fb_head(struct vnt_usb_send_context *tx_context,
522                                 union vnt_tx_data_head *head)
523 {
524         struct vnt_private *priv = tx_context->priv;
525         struct vnt_cts_fb *buf = &head->cts_g_fb;
526         u32 cts_frame_len = 14;
527         u16 current_rate = tx_context->tx_rate;
528
529         /* Get SignalField,ServiceField,Length */
530         vnt_get_phy_field(priv, cts_frame_len, priv->top_cck_basic_rate,
531                           PK_TYPE_11B, &buf->b);
532
533         buf->duration_ba =
534                 vnt_get_rtscts_duration_le(tx_context, CTSDUR_BA,
535                                            tx_context->pkt_type,
536                                            current_rate);
537         /* Get CTSDuration_ba_f0 */
538         buf->cts_duration_ba_f0 =
539                 vnt_get_rtscts_duration_le(tx_context, CTSDUR_BA_F0,
540                                            tx_context->pkt_type,
541                                            priv->tx_rate_fb0);
542         /* Get CTSDuration_ba_f1 */
543         buf->cts_duration_ba_f1 =
544                 vnt_get_rtscts_duration_le(tx_context, CTSDUR_BA_F1,
545                                            tx_context->pkt_type,
546                                            priv->tx_rate_fb1);
547         /* Get CTS Frame body */
548         buf->data.duration = buf->duration_ba;
549         buf->data.frame_control =
550                 cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS);
551
552         ether_addr_copy(buf->data.ra, priv->current_net_addr);
553
554         return vnt_rxtx_datahead_g_fb(tx_context, &buf->data_head);
555 }
556
557 static u16 vnt_fill_cts_head(struct vnt_usb_send_context *tx_context,
558                              union vnt_tx_data_head *head)
559 {
560         struct vnt_private *priv = tx_context->priv;
561         struct vnt_cts *buf = &head->cts_g;
562         u32 cts_frame_len = 14;
563         u16 current_rate = tx_context->tx_rate;
564
565         /* Get SignalField,ServiceField,Length */
566         vnt_get_phy_field(priv, cts_frame_len, priv->top_cck_basic_rate,
567                           PK_TYPE_11B, &buf->b);
568         /* Get CTSDuration_ba */
569         buf->duration_ba =
570                 vnt_get_rtscts_duration_le(tx_context, CTSDUR_BA,
571                                            tx_context->pkt_type,
572                                            current_rate);
573         /*Get CTS Frame body*/
574         buf->data.duration = buf->duration_ba;
575         buf->data.frame_control =
576                 cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS);
577
578         ether_addr_copy(buf->data.ra, priv->current_net_addr);
579
580         return vnt_rxtx_datahead_g(tx_context, &buf->data_head);
581 }
582
583 static u16 vnt_rxtx_rts(struct vnt_usb_send_context *tx_context,
584                         union vnt_tx_head *tx_head, bool need_mic)
585 {
586         struct vnt_private *priv = tx_context->priv;
587         struct vnt_rrv_time_rts *buf = &tx_head->tx_rts.rts;
588         union vnt_tx_data_head *head = &tx_head->tx_rts.tx.head;
589         u32 frame_len = tx_context->frame_len;
590         u16 current_rate = tx_context->tx_rate;
591         u8 need_ack = tx_context->need_ack;
592
593         buf->rts_rrv_time_aa = vnt_get_rtscts_rsvtime_le(priv, 2,
594                         tx_context->pkt_type, frame_len, current_rate);
595         buf->rts_rrv_time_ba = vnt_get_rtscts_rsvtime_le(priv, 1,
596                         tx_context->pkt_type, frame_len, current_rate);
597         buf->rts_rrv_time_bb = vnt_get_rtscts_rsvtime_le(priv, 0,
598                         tx_context->pkt_type, frame_len, current_rate);
599
600         buf->rrv_time_a = vnt_rxtx_rsvtime_le16(priv, tx_context->pkt_type,
601                                                 frame_len, current_rate,
602                                                 need_ack);
603         buf->rrv_time_b = vnt_rxtx_rsvtime_le16(priv, PK_TYPE_11B, frame_len,
604                                         priv->top_cck_basic_rate, need_ack);
605
606         if (need_mic)
607                 head = &tx_head->tx_rts.tx.mic.head;
608
609         if (tx_context->fb_option)
610                 return vnt_rxtx_rts_g_fb_head(tx_context, &head->rts_g_fb);
611
612         return vnt_rxtx_rts_g_head(tx_context, &head->rts_g);
613 }
614
615 static u16 vnt_rxtx_cts(struct vnt_usb_send_context *tx_context,
616                         union vnt_tx_head *tx_head, bool need_mic)
617 {
618         struct vnt_private *priv = tx_context->priv;
619         struct vnt_rrv_time_cts *buf = &tx_head->tx_cts.cts;
620         union vnt_tx_data_head *head = &tx_head->tx_cts.tx.head;
621         u32 frame_len = tx_context->frame_len;
622         u16 current_rate = tx_context->tx_rate;
623         u8 need_ack = tx_context->need_ack;
624
625         buf->rrv_time_a = vnt_rxtx_rsvtime_le16(priv, tx_context->pkt_type,
626                                         frame_len, current_rate, need_ack);
627         buf->rrv_time_b = vnt_rxtx_rsvtime_le16(priv, PK_TYPE_11B,
628                                 frame_len, priv->top_cck_basic_rate, need_ack);
629
630         buf->cts_rrv_time_ba = vnt_get_rtscts_rsvtime_le(priv, 3,
631                         tx_context->pkt_type, frame_len, current_rate);
632
633         if (need_mic)
634                 head = &tx_head->tx_cts.tx.mic.head;
635
636         /* Fill CTS */
637         if (tx_context->fb_option)
638                 return vnt_fill_cts_fb_head(tx_context, head);
639
640         return vnt_fill_cts_head(tx_context, head);
641 }
642
643 static u16 vnt_rxtx_ab(struct vnt_usb_send_context *tx_context,
644                        union vnt_tx_head *tx_head, bool need_rts, bool need_mic)
645 {
646         struct vnt_private *priv = tx_context->priv;
647         struct vnt_rrv_time_ab *buf = &tx_head->tx_ab.ab;
648         union vnt_tx_data_head *head = &tx_head->tx_ab.tx.head;
649         u32 frame_len = tx_context->frame_len;
650         u16 current_rate = tx_context->tx_rate;
651         u8 need_ack = tx_context->need_ack;
652
653         buf->rrv_time = vnt_rxtx_rsvtime_le16(priv, tx_context->pkt_type,
654                         frame_len, current_rate, need_ack);
655
656         if (need_mic)
657                 head = &tx_head->tx_ab.tx.mic.head;
658
659         if (need_rts) {
660                 if (tx_context->pkt_type == PK_TYPE_11B)
661                         buf->rts_rrv_time = vnt_get_rtscts_rsvtime_le(priv, 0,
662                                 tx_context->pkt_type, frame_len, current_rate);
663                 else /* PK_TYPE_11A */
664                         buf->rts_rrv_time = vnt_get_rtscts_rsvtime_le(priv, 2,
665                                 tx_context->pkt_type, frame_len, current_rate);
666
667                 if (tx_context->fb_option &&
668                     tx_context->pkt_type == PK_TYPE_11A)
669                         return vnt_rxtx_rts_a_fb_head(tx_context,
670                                                       &head->rts_a_fb);
671
672                 return vnt_rxtx_rts_ab_head(tx_context, &head->rts_ab);
673         }
674
675         if (tx_context->pkt_type == PK_TYPE_11A)
676                 return vnt_rxtx_datahead_a_fb(tx_context,
677                                               &head->data_head_a_fb);
678
679         return vnt_rxtx_datahead_ab(tx_context, &head->data_head_ab);
680 }
681
682 static u16 vnt_generate_tx_parameter(struct vnt_usb_send_context *tx_context,
683                                      struct vnt_tx_buffer *tx_buffer,
684                                      struct vnt_mic_hdr **mic_hdr, u32 need_mic,
685                                      bool need_rts)
686 {
687         if (tx_context->pkt_type == PK_TYPE_11GB ||
688             tx_context->pkt_type == PK_TYPE_11GA) {
689                 if (need_rts) {
690                         if (need_mic)
691                                 *mic_hdr =
692                                         &tx_buffer->tx_head.tx_rts.tx.mic.hdr;
693
694                         return vnt_rxtx_rts(tx_context, &tx_buffer->tx_head,
695                                             need_mic);
696                 }
697
698                 if (need_mic)
699                         *mic_hdr = &tx_buffer->tx_head.tx_cts.tx.mic.hdr;
700
701                 return vnt_rxtx_cts(tx_context, &tx_buffer->tx_head, need_mic);
702         }
703
704         if (need_mic)
705                 *mic_hdr = &tx_buffer->tx_head.tx_ab.tx.mic.hdr;
706
707         return vnt_rxtx_ab(tx_context, &tx_buffer->tx_head, need_rts, need_mic);
708 }
709
710 static void vnt_fill_txkey(struct vnt_usb_send_context *tx_context,
711                            u8 *key_buffer, struct ieee80211_key_conf *tx_key,
712                            struct sk_buff *skb, u16 payload_len,
713                            struct vnt_mic_hdr *mic_hdr)
714 {
715         struct ieee80211_hdr *hdr = tx_context->hdr;
716         u64 pn64;
717         u8 *iv = ((u8 *)hdr + ieee80211_get_hdrlen_from_skb(skb));
718
719         /* strip header and icv len from payload */
720         payload_len -= ieee80211_get_hdrlen_from_skb(skb);
721         payload_len -= tx_key->icv_len;
722
723         switch (tx_key->cipher) {
724         case WLAN_CIPHER_SUITE_WEP40:
725         case WLAN_CIPHER_SUITE_WEP104:
726                 memcpy(key_buffer, iv, 3);
727                 memcpy(key_buffer + 3, tx_key->key, tx_key->keylen);
728
729                 if (tx_key->keylen == WLAN_KEY_LEN_WEP40) {
730                         memcpy(key_buffer + 8, iv, 3);
731                         memcpy(key_buffer + 11,
732                                tx_key->key, WLAN_KEY_LEN_WEP40);
733                 }
734
735                 break;
736         case WLAN_CIPHER_SUITE_TKIP:
737                 ieee80211_get_tkip_p2k(tx_key, skb, key_buffer);
738
739                 break;
740         case WLAN_CIPHER_SUITE_CCMP:
741
742                 if (!mic_hdr)
743                         return;
744
745                 mic_hdr->id = 0x59;
746                 mic_hdr->payload_len = cpu_to_be16(payload_len);
747                 ether_addr_copy(mic_hdr->mic_addr2, hdr->addr2);
748
749                 pn64 = atomic64_read(&tx_key->tx_pn);
750                 mic_hdr->ccmp_pn[5] = pn64;
751                 mic_hdr->ccmp_pn[4] = pn64 >> 8;
752                 mic_hdr->ccmp_pn[3] = pn64 >> 16;
753                 mic_hdr->ccmp_pn[2] = pn64 >> 24;
754                 mic_hdr->ccmp_pn[1] = pn64 >> 32;
755                 mic_hdr->ccmp_pn[0] = pn64 >> 40;
756
757                 if (ieee80211_has_a4(hdr->frame_control))
758                         mic_hdr->hlen = cpu_to_be16(28);
759                 else
760                         mic_hdr->hlen = cpu_to_be16(22);
761
762                 ether_addr_copy(mic_hdr->addr1, hdr->addr1);
763                 ether_addr_copy(mic_hdr->addr2, hdr->addr2);
764                 ether_addr_copy(mic_hdr->addr3, hdr->addr3);
765
766                 mic_hdr->frame_control = cpu_to_le16(
767                         le16_to_cpu(hdr->frame_control) & 0xc78f);
768                 mic_hdr->seq_ctrl = cpu_to_le16(
769                                 le16_to_cpu(hdr->seq_ctrl) & 0xf);
770
771                 if (ieee80211_has_a4(hdr->frame_control))
772                         ether_addr_copy(mic_hdr->addr4, hdr->addr4);
773
774                 memcpy(key_buffer, tx_key->key, WLAN_KEY_LEN_CCMP);
775
776                 break;
777         default:
778                 break;
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;
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 == NL80211_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         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, struct sk_buff *skb)
991 {
992         struct vnt_beacon_buffer *beacon_buffer;
993         struct vnt_tx_short_buf_head *short_head;
994         struct ieee80211_tx_info *info;
995         struct vnt_usb_send_context *context;
996         struct ieee80211_mgmt *mgmt_hdr;
997         unsigned long flags;
998         u32 frame_size = skb->len + 4;
999         u16 current_rate, count;
1000
1001         spin_lock_irqsave(&priv->lock, flags);
1002
1003         context = vnt_get_free_context(priv);
1004         if (!context) {
1005                 dev_dbg(&priv->usb->dev, "%s No free context!\n", __func__);
1006                 spin_unlock_irqrestore(&priv->lock, flags);
1007                 return -ENOMEM;
1008         }
1009
1010         context->skb = skb;
1011
1012         spin_unlock_irqrestore(&priv->lock, flags);
1013
1014         beacon_buffer = (struct vnt_beacon_buffer *)&context->data[0];
1015         short_head = &beacon_buffer->short_head;
1016
1017         if (priv->bb_type == BB_TYPE_11A) {
1018                 current_rate = RATE_6M;
1019
1020                 /* Get SignalField,ServiceField,Length */
1021                 vnt_get_phy_field(priv, frame_size, current_rate,
1022                                   PK_TYPE_11A, &short_head->ab);
1023
1024                 /* Get Duration and TimeStampOff */
1025                 short_head->duration = vnt_get_duration_le(priv,
1026                                                            PK_TYPE_11A, false);
1027                 short_head->time_stamp_off =
1028                                 vnt_time_stamp_off(priv, current_rate);
1029         } else {
1030                 current_rate = RATE_1M;
1031                 short_head->fifo_ctl |= cpu_to_le16(FIFOCTL_11B);
1032
1033                 /* Get SignalField,ServiceField,Length */
1034                 vnt_get_phy_field(priv, frame_size, current_rate,
1035                                   PK_TYPE_11B, &short_head->ab);
1036
1037                 /* Get Duration and TimeStampOff */
1038                 short_head->duration = vnt_get_duration_le(priv,
1039                                                 PK_TYPE_11B, false);
1040                 short_head->time_stamp_off =
1041                         vnt_time_stamp_off(priv, current_rate);
1042         }
1043
1044         /* Generate Beacon Header */
1045         mgmt_hdr = &beacon_buffer->mgmt_hdr;
1046         memcpy(mgmt_hdr, skb->data, skb->len);
1047
1048         /* time stamp always 0 */
1049         mgmt_hdr->u.beacon.timestamp = 0;
1050
1051         info = IEEE80211_SKB_CB(skb);
1052         if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
1053                 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)mgmt_hdr;
1054
1055                 hdr->duration_id = 0;
1056                 hdr->seq_ctrl = cpu_to_le16(priv->seq_counter << 4);
1057         }
1058
1059         priv->seq_counter++;
1060         if (priv->seq_counter > 0x0fff)
1061                 priv->seq_counter = 0;
1062
1063         count = sizeof(struct vnt_tx_short_buf_head) + skb->len;
1064
1065         beacon_buffer->tx_byte_count = cpu_to_le16(count);
1066         beacon_buffer->pkt_no = context->pkt_no;
1067         beacon_buffer->type = 0x01;
1068
1069         context->type = CONTEXT_BEACON_PACKET;
1070         context->buf_len = count + 4; /* USB header */
1071
1072         spin_lock_irqsave(&priv->lock, flags);
1073
1074         if (vnt_tx_context(priv, context) != STATUS_PENDING)
1075                 ieee80211_free_txskb(priv->hw, context->skb);
1076
1077         spin_unlock_irqrestore(&priv->lock, flags);
1078
1079         return 0;
1080 }
1081
1082 int vnt_beacon_make(struct vnt_private *priv, struct ieee80211_vif *vif)
1083 {
1084         struct sk_buff *beacon;
1085
1086         beacon = ieee80211_beacon_get(priv->hw, vif);
1087         if (!beacon)
1088                 return -ENOMEM;
1089
1090         if (vnt_beacon_xmit(priv, beacon)) {
1091                 ieee80211_free_txskb(priv->hw, beacon);
1092                 return -ENODEV;
1093         }
1094
1095         return 0;
1096 }
1097
1098 int vnt_beacon_enable(struct vnt_private *priv, struct ieee80211_vif *vif,
1099                       struct ieee80211_bss_conf *conf)
1100 {
1101         vnt_mac_reg_bits_off(priv, MAC_REG_TCR, TCR_AUTOBCNTX);
1102
1103         vnt_mac_reg_bits_off(priv, MAC_REG_TFTCTL, TFTCTL_TSFCNTREN);
1104
1105         vnt_mac_set_beacon_interval(priv, conf->beacon_int);
1106
1107         vnt_clear_current_tsf(priv);
1108
1109         vnt_mac_reg_bits_on(priv, MAC_REG_TFTCTL, TFTCTL_TSFCNTREN);
1110
1111         vnt_reset_next_tbtt(priv, conf->beacon_int);
1112
1113         return vnt_beacon_make(priv, vif);
1114 }