]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/staging/vt6656/rxtx.c
staging: vt6655: remove unused function in mac.c
[mirror_ubuntu-hirsute-kernel.git] / drivers / staging / vt6656 / rxtx.c
CommitLineData
92b96797
FB
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:
9ec7f2d8 28 * vnt_generate_tx_parameter - Generate tx dma required parameter.
435ae3be 29 * vnt_get_duration_le - get tx data required duration
f2aabee6 30 * vnt_get_rtscts_duration_le- get rtx/cts required duration
aadf7d13 31 * vnt_get_rtscts_rsvtime_le- get rts/cts reserved time
3815a27b 32 * vnt_get_rsvtime- get frame reserved time
4f31668f 33 * vnt_fill_cts_head- fulfill CTS ctl header
92b96797
FB
34 *
35 * Revision History:
36 *
37 */
38
ccc103f5 39#include <linux/etherdevice.h>
92b96797 40#include "device.h"
92b96797 41#include "rxtx.h"
92b96797 42#include "card.h"
92b96797 43#include "mac.h"
92b96797 44#include "rf.h"
92b96797 45#include "usbpipe.h"
9d26d60f 46
1a4d4509
MP
47static const u16 vnt_time_stampoff[2][MAX_RATE] = {
48 {384, 288, 226, 209, 54, 43, 37, 31, 28, 25, 24, 23},/* Long Preamble */
49 {384, 192, 130, 113, 54, 43, 37, 31, 28, 25, 24, 23},/* Short Preamble */
50};
92b96797 51
e3f31874
MP
52static const u16 vnt_fb_opt0[2][5] = {
53 {RATE_12M, RATE_18M, RATE_24M, RATE_36M, RATE_48M}, /* fallback_rate0 */
54 {RATE_12M, RATE_12M, RATE_18M, RATE_24M, RATE_36M}, /* fallback_rate1 */
55};
56
2331ba42
MP
57static const u16 vnt_fb_opt1[2][5] = {
58 {RATE_12M, RATE_18M, RATE_24M, RATE_24M, RATE_36M}, /* fallback_rate0 */
59 {RATE_6M , RATE_6M, RATE_12M, RATE_12M, RATE_18M}, /* fallback_rate1 */
60};
92b96797 61
92b96797
FB
62#define RTSDUR_BB 0
63#define RTSDUR_BA 1
64#define RTSDUR_AA 2
65#define CTSDUR_BA 3
66#define RTSDUR_BA_F0 4
67#define RTSDUR_AA_F0 5
68#define RTSDUR_BA_F1 6
69#define RTSDUR_AA_F1 7
70#define CTSDUR_BA_F0 8
71#define CTSDUR_BA_F1 9
72#define DATADUR_B 10
73#define DATADUR_A 11
74#define DATADUR_A_F0 12
75#define DATADUR_A_F1 13
76
aceaf018 77static struct vnt_usb_send_context
a032b16c 78 *vnt_get_free_context(struct vnt_private *priv)
92b96797 79{
b89f3b94 80 struct vnt_usb_send_context *context = NULL;
d56131de 81 int ii;
92b96797 82
604631ff 83 dev_dbg(&priv->usb->dev, "%s\n", __func__);
b89f3b94 84
03b7e354 85 for (ii = 0; ii < priv->num_tx_context; ii++) {
f7e4a8f4 86 if (!priv->tx_context[ii])
b89f3b94
MP
87 return NULL;
88
f7e4a8f4 89 context = priv->tx_context[ii];
30a05b39
MP
90 if (context->in_use == false) {
91 context->in_use = true;
92 memset(context->data, 0,
b89f3b94 93 MAX_TOTAL_SIZE_WITH_ALL_HEADERS);
1622c8fc
MP
94
95 context->hdr = NULL;
96
b89f3b94
MP
97 return context;
98 }
99 }
100
03b7e354 101 if (ii == priv->num_tx_context)
604631ff 102 dev_dbg(&priv->usb->dev, "%s No Free Tx Context\n", __func__);
aceaf018 103
5c851383 104 return NULL;
92b96797
FB
105}
106
dab085b1 107static __le16 vnt_time_stamp_off(struct vnt_private *priv, u16 rate)
f115e76a 108{
98e93fe5 109 return cpu_to_le16(vnt_time_stampoff[priv->preamble_type % 2]
f115e76a
MP
110 [rate % MAX_RATE]);
111}
112
3815a27b 113static u32 vnt_get_rsvtime(struct vnt_private *priv, u8 pkt_type,
3fd5620f 114 u32 frame_length, u16 rate, int need_ack)
92b96797 115{
3fd5620f 116 u32 data_time, ack_time;
92b96797 117
98e93fe5 118 data_time = vnt_get_frame_time(priv->preamble_type, pkt_type,
3fd5620f 119 frame_length, rate);
92b96797 120
3fd5620f 121 if (pkt_type == PK_TYPE_11B)
98e93fe5 122 ack_time = vnt_get_frame_time(priv->preamble_type, pkt_type,
d80bf43c 123 14, (u16)priv->top_cck_basic_rate);
3fd5620f 124 else
98e93fe5 125 ack_time = vnt_get_frame_time(priv->preamble_type, pkt_type,
3c8a5b25 126 14, (u16)priv->top_ofdm_basic_rate);
3fd5620f
MP
127
128 if (need_ack)
ff5fee22 129 return data_time + priv->sifs + ack_time;
3fd5620f
MP
130
131 return data_time;
92b96797
FB
132}
133
2075f654 134static __le16 vnt_rxtx_rsvtime_le16(struct vnt_private *priv, u8 pkt_type,
9c3806d5
MP
135 u32 frame_length, u16 rate, int need_ack)
136{
3815a27b 137 return cpu_to_le16((u16)vnt_get_rsvtime(priv, pkt_type,
9c3806d5
MP
138 frame_length, rate, need_ack));
139}
140
aadf7d13 141static __le16 vnt_get_rtscts_rsvtime_le(struct vnt_private *priv,
3b6cee7b 142 u8 rsv_type, u8 pkt_type, u32 frame_length, u16 current_rate)
92b96797 143{
342e2e20 144 u32 rrv_time, rts_time, cts_time, ack_time, data_time;
92b96797 145
342e2e20 146 rrv_time = rts_time = cts_time = ack_time = data_time = 0;
92b96797 147
98e93fe5 148 data_time = vnt_get_frame_time(priv->preamble_type, pkt_type,
3b6cee7b 149 frame_length, current_rate);
342e2e20
MP
150
151 if (rsv_type == 0) {
98e93fe5 152 rts_time = vnt_get_frame_time(priv->preamble_type,
d80bf43c 153 pkt_type, 20, priv->top_cck_basic_rate);
98e93fe5 154 cts_time = ack_time = vnt_get_frame_time(priv->preamble_type,
d80bf43c 155 pkt_type, 14, priv->top_cck_basic_rate);
342e2e20 156 } else if (rsv_type == 1) {
98e93fe5 157 rts_time = vnt_get_frame_time(priv->preamble_type,
d80bf43c 158 pkt_type, 20, priv->top_cck_basic_rate);
98e93fe5 159 cts_time = vnt_get_frame_time(priv->preamble_type, pkt_type,
d80bf43c 160 14, priv->top_cck_basic_rate);
98e93fe5 161 ack_time = vnt_get_frame_time(priv->preamble_type, pkt_type,
3c8a5b25 162 14, priv->top_ofdm_basic_rate);
342e2e20 163 } else if (rsv_type == 2) {
98e93fe5 164 rts_time = vnt_get_frame_time(priv->preamble_type, pkt_type,
3c8a5b25 165 20, priv->top_ofdm_basic_rate);
98e93fe5 166 cts_time = ack_time = vnt_get_frame_time(priv->preamble_type,
3c8a5b25 167 pkt_type, 14, priv->top_ofdm_basic_rate);
342e2e20 168 } else if (rsv_type == 3) {
98e93fe5 169 cts_time = vnt_get_frame_time(priv->preamble_type, pkt_type,
d80bf43c 170 14, priv->top_cck_basic_rate);
98e93fe5 171 ack_time = vnt_get_frame_time(priv->preamble_type, pkt_type,
3c8a5b25 172 14, priv->top_ofdm_basic_rate);
342e2e20 173
ff5fee22 174 rrv_time = cts_time + ack_time + data_time + 2 * priv->sifs;
342e2e20 175
20338015 176 return cpu_to_le16((u16)rrv_time);
342e2e20
MP
177 }
178
ff5fee22 179 rrv_time = rts_time + cts_time + ack_time + data_time + 3 * priv->sifs;
92b96797 180
342e2e20 181 return cpu_to_le16((u16)rrv_time);
92b96797
FB
182}
183
0871dc69 184static __le16 vnt_get_duration_le(struct vnt_private *priv,
a4c2a8a4 185 u8 pkt_type, int need_ack)
92b96797 186{
a4c2a8a4 187 u32 ack_time = 0;
92b96797 188
a4c2a8a4
MP
189 if (need_ack) {
190 if (pkt_type == PK_TYPE_11B)
0871dc69
MP
191 ack_time = vnt_get_frame_time(priv->preamble_type,
192 pkt_type, 14, priv->top_cck_basic_rate);
b02ccd59 193 else
0871dc69
MP
194 ack_time = vnt_get_frame_time(priv->preamble_type,
195 pkt_type, 14, priv->top_ofdm_basic_rate);
a4c2a8a4 196
0871dc69 197 return cpu_to_le16((u16)(priv->sifs + ack_time));
b02ccd59 198 }
92b96797 199
92b96797
FB
200 return 0;
201}
202
cca48e3c
MP
203static __le16 vnt_get_rtscts_duration_le(struct vnt_usb_send_context *context,
204 u8 dur_type, u8 pkt_type, u16 rate)
92b96797 205{
cca48e3c 206 struct vnt_private *priv = context->priv;
7beae9a2 207 u32 cts_time = 0, dur_time = 0;
cca48e3c
MP
208 u32 frame_length = context->frame_len;
209 u8 need_ack = context->need_ack;
92b96797 210
7beae9a2 211 switch (dur_type) {
ecd80240
MP
212 case RTSDUR_BB:
213 case RTSDUR_BA:
214 case RTSDUR_BA_F0:
215 case RTSDUR_BA_F1:
98e93fe5 216 cts_time = vnt_get_frame_time(priv->preamble_type,
d80bf43c 217 pkt_type, 14, priv->top_cck_basic_rate);
ff5fee22 218 dur_time = cts_time + 2 * priv->sifs +
3815a27b 219 vnt_get_rsvtime(priv, pkt_type,
7beae9a2 220 frame_length, rate, need_ack);
ecd80240 221 break;
92b96797 222
ecd80240
MP
223 case RTSDUR_AA:
224 case RTSDUR_AA_F0:
225 case RTSDUR_AA_F1:
98e93fe5 226 cts_time = vnt_get_frame_time(priv->preamble_type,
3c8a5b25 227 pkt_type, 14, priv->top_ofdm_basic_rate);
ff5fee22 228 dur_time = cts_time + 2 * priv->sifs +
3815a27b 229 vnt_get_rsvtime(priv, pkt_type,
7beae9a2 230 frame_length, rate, need_ack);
ecd80240 231 break;
92b96797 232
ecd80240
MP
233 case CTSDUR_BA:
234 case CTSDUR_BA_F0:
235 case CTSDUR_BA_F1:
ff5fee22 236 dur_time = priv->sifs + vnt_get_rsvtime(priv,
7beae9a2 237 pkt_type, frame_length, rate, need_ack);
ecd80240 238 break;
92b96797 239
ecd80240
MP
240 default:
241 break;
242 }
92b96797 243
7beae9a2 244 return cpu_to_le16((u16)dur_time);
92b96797
FB
245}
246
1622c8fc
MP
247static u16 vnt_mac_hdr_pos(struct vnt_usb_send_context *tx_context,
248 struct ieee80211_hdr *hdr)
249{
250 u8 *head = tx_context->data + offsetof(struct vnt_tx_buffer, fifo_head);
251 u8 *hdr_pos = (u8 *)hdr;
252
253 tx_context->hdr = hdr;
254 if (!tx_context->hdr)
255 return 0;
256
257 return (u16)(hdr_pos - head);
258}
259
c2c32da3 260static u16 vnt_rxtx_datahead_g(struct vnt_usb_send_context *tx_context,
798f0601 261 struct vnt_tx_datahead_g *buf)
78363fd1 262{
c2c32da3
MP
263
264 struct vnt_private *priv = tx_context->priv;
893cc709
MP
265 struct ieee80211_hdr *hdr =
266 (struct ieee80211_hdr *)tx_context->skb->data;
0f5567cb 267 u32 frame_len = tx_context->frame_len;
798f0601 268 u16 rate = tx_context->tx_rate;
2eca8dbb 269 u8 need_ack = tx_context->need_ack;
c2c32da3 270
78363fd1 271 /* Get SignalField,ServiceField,Length */
e8c9875c 272 vnt_get_phy_field(priv, frame_len, rate, tx_context->pkt_type, &buf->a);
d80bf43c 273 vnt_get_phy_field(priv, frame_len, priv->top_cck_basic_rate,
78363fd1
MP
274 PK_TYPE_11B, &buf->b);
275
276 /* Get Duration and TimeStamp */
893cc709
MP
277 if (ieee80211_is_pspoll(hdr->frame_control)) {
278 __le16 dur = cpu_to_le16(priv->current_aid | BIT(14) | BIT(15));
279
280 buf->duration_a = dur;
281 buf->duration_b = dur;
282 } else {
e8c9875c
MP
283 buf->duration_a = vnt_get_duration_le(priv,
284 tx_context->pkt_type, need_ack);
435ae3be 285 buf->duration_b = vnt_get_duration_le(priv,
893cc709
MP
286 PK_TYPE_11B, need_ack);
287 }
78363fd1 288
10bb39a0
MP
289 buf->time_stamp_off_a = vnt_time_stamp_off(priv, rate);
290 buf->time_stamp_off_b = vnt_time_stamp_off(priv,
d80bf43c 291 priv->top_cck_basic_rate);
78363fd1 292
1622c8fc
MP
293 tx_context->tx_hdr_size = vnt_mac_hdr_pos(tx_context, &buf->hdr);
294
c4cf6dfb 295 return le16_to_cpu(buf->duration_a);
78363fd1
MP
296}
297
c2c32da3 298static u16 vnt_rxtx_datahead_g_fb(struct vnt_usb_send_context *tx_context,
798f0601 299 struct vnt_tx_datahead_g_fb *buf)
5b852f53 300{
c2c32da3 301 struct vnt_private *priv = tx_context->priv;
0f5567cb 302 u32 frame_len = tx_context->frame_len;
798f0601 303 u16 rate = tx_context->tx_rate;
2eca8dbb 304 u8 need_ack = tx_context->need_ack;
c2c32da3 305
5b852f53 306 /* Get SignalField,ServiceField,Length */
e8c9875c 307 vnt_get_phy_field(priv, frame_len, rate, tx_context->pkt_type, &buf->a);
5b852f53 308
d80bf43c 309 vnt_get_phy_field(priv, frame_len, priv->top_cck_basic_rate,
5b852f53
MP
310 PK_TYPE_11B, &buf->b);
311
312 /* Get Duration and TimeStamp */
e8c9875c
MP
313 buf->duration_a = vnt_get_duration_le(priv, tx_context->pkt_type,
314 need_ack);
435ae3be 315 buf->duration_b = vnt_get_duration_le(priv, PK_TYPE_11B, need_ack);
5b852f53 316
e8c9875c
MP
317 buf->duration_a_f0 = vnt_get_duration_le(priv, tx_context->pkt_type,
318 need_ack);
319 buf->duration_a_f1 = vnt_get_duration_le(priv, tx_context->pkt_type,
320 need_ack);
5b852f53 321
10bb39a0
MP
322 buf->time_stamp_off_a = vnt_time_stamp_off(priv, rate);
323 buf->time_stamp_off_b = vnt_time_stamp_off(priv,
d80bf43c 324 priv->top_cck_basic_rate);
5b852f53 325
1622c8fc
MP
326 tx_context->tx_hdr_size = vnt_mac_hdr_pos(tx_context, &buf->hdr);
327
c4cf6dfb 328 return le16_to_cpu(buf->duration_a);
5b852f53
MP
329}
330
c2c32da3 331static u16 vnt_rxtx_datahead_a_fb(struct vnt_usb_send_context *tx_context,
798f0601 332 struct vnt_tx_datahead_a_fb *buf)
bd3f51f1 333{
c2c32da3 334 struct vnt_private *priv = tx_context->priv;
798f0601 335 u16 rate = tx_context->tx_rate;
e8c9875c 336 u8 pkt_type = tx_context->pkt_type;
2eca8dbb 337 u8 need_ack = tx_context->need_ack;
0f5567cb 338 u32 frame_len = tx_context->frame_len;
c2c32da3 339
bd3f51f1 340 /* Get SignalField,ServiceField,Length */
205056f3 341 vnt_get_phy_field(priv, frame_len, rate, pkt_type, &buf->a);
bd3f51f1 342 /* Get Duration and TimeStampOff */
435ae3be 343 buf->duration = vnt_get_duration_le(priv, pkt_type, need_ack);
bd3f51f1 344
435ae3be
MP
345 buf->duration_f0 = vnt_get_duration_le(priv, pkt_type, need_ack);
346 buf->duration_f1 = vnt_get_duration_le(priv, pkt_type, need_ack);
bd3f51f1 347
10bb39a0 348 buf->time_stamp_off = vnt_time_stamp_off(priv, rate);
bd3f51f1 349
1622c8fc
MP
350 tx_context->tx_hdr_size = vnt_mac_hdr_pos(tx_context, &buf->hdr);
351
c4cf6dfb 352 return le16_to_cpu(buf->duration);
bd3f51f1
MP
353}
354
c2c32da3 355static u16 vnt_rxtx_datahead_ab(struct vnt_usb_send_context *tx_context,
798f0601 356 struct vnt_tx_datahead_ab *buf)
5634a5ab 357{
c2c32da3 358 struct vnt_private *priv = tx_context->priv;
893cc709
MP
359 struct ieee80211_hdr *hdr =
360 (struct ieee80211_hdr *)tx_context->skb->data;
0f5567cb 361 u32 frame_len = tx_context->frame_len;
798f0601 362 u16 rate = tx_context->tx_rate;
2eca8dbb 363 u8 need_ack = tx_context->need_ack;
c2c32da3 364
5634a5ab 365 /* Get SignalField,ServiceField,Length */
e8c9875c
MP
366 vnt_get_phy_field(priv, frame_len, rate,
367 tx_context->pkt_type, &buf->ab);
893cc709 368
5634a5ab 369 /* Get Duration and TimeStampOff */
893cc709
MP
370 if (ieee80211_is_pspoll(hdr->frame_control)) {
371 __le16 dur = cpu_to_le16(priv->current_aid | BIT(14) | BIT(15));
372
373 buf->duration = dur;
374 } else {
e8c9875c
MP
375 buf->duration = vnt_get_duration_le(priv, tx_context->pkt_type,
376 need_ack);
893cc709 377 }
5634a5ab 378
10bb39a0 379 buf->time_stamp_off = vnt_time_stamp_off(priv, rate);
5634a5ab 380
1622c8fc
MP
381 tx_context->tx_hdr_size = vnt_mac_hdr_pos(tx_context, &buf->hdr);
382
c4cf6dfb 383 return le16_to_cpu(buf->duration);
5634a5ab
MP
384}
385
d38b13aa
MP
386static int vnt_fill_ieee80211_rts(struct vnt_usb_send_context *tx_context,
387 struct ieee80211_rts *rts, __le16 duration)
5e67ee49 388{
d38b13aa
MP
389 struct ieee80211_hdr *hdr =
390 (struct ieee80211_hdr *)tx_context->skb->data;
391
5e67ee49 392 rts->duration = duration;
f4554d3b
MP
393 rts->frame_control =
394 cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS);
5e67ee49 395
ccc103f5
KA
396 ether_addr_copy(rts->ra, hdr->addr1);
397 ether_addr_copy(rts->ta, hdr->addr2);
5e67ee49
MP
398
399 return 0;
400}
401
c2c32da3 402static u16 vnt_rxtx_rts_g_head(struct vnt_usb_send_context *tx_context,
798f0601 403 struct vnt_rts_g *buf)
5e67ee49 404{
c2c32da3 405 struct vnt_private *priv = tx_context->priv;
5e67ee49 406 u16 rts_frame_len = 20;
798f0601 407 u16 current_rate = tx_context->tx_rate;
2eca8dbb 408
d80bf43c 409 vnt_get_phy_field(priv, rts_frame_len, priv->top_cck_basic_rate,
5e67ee49 410 PK_TYPE_11B, &buf->b);
e8c9875c
MP
411 vnt_get_phy_field(priv, rts_frame_len, priv->top_ofdm_basic_rate,
412 tx_context->pkt_type, &buf->a);
5e67ee49 413
cca48e3c
MP
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);
5e67ee49 423
d38b13aa 424 vnt_fill_ieee80211_rts(tx_context, &buf->data, buf->duration_aa);
5e67ee49 425
798f0601 426 return vnt_rxtx_datahead_g(tx_context, &buf->data_head);
5e67ee49
MP
427}
428
c2c32da3 429static u16 vnt_rxtx_rts_g_fb_head(struct vnt_usb_send_context *tx_context,
798f0601 430 struct vnt_rts_g_fb *buf)
ec91713a 431{
c2c32da3 432 struct vnt_private *priv = tx_context->priv;
798f0601 433 u16 current_rate = tx_context->tx_rate;
ec91713a 434 u16 rts_frame_len = 20;
2eca8dbb 435
d80bf43c 436 vnt_get_phy_field(priv, rts_frame_len, priv->top_cck_basic_rate,
ec91713a 437 PK_TYPE_11B, &buf->b);
e8c9875c
MP
438 vnt_get_phy_field(priv, rts_frame_len, priv->top_ofdm_basic_rate,
439 tx_context->pkt_type, &buf->a);
ec91713a 440
cca48e3c
MP
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);
ec91713a 467
d38b13aa 468 vnt_fill_ieee80211_rts(tx_context, &buf->data, buf->duration_aa);
ec91713a 469
798f0601 470 return vnt_rxtx_datahead_g_fb(tx_context, &buf->data_head);
ec91713a
MP
471}
472
c2c32da3 473static u16 vnt_rxtx_rts_ab_head(struct vnt_usb_send_context *tx_context,
798f0601 474 struct vnt_rts_ab *buf)
1712633d 475{
c2c32da3 476 struct vnt_private *priv = tx_context->priv;
798f0601 477 u16 current_rate = tx_context->tx_rate;
1712633d
MP
478 u16 rts_frame_len = 20;
479
e8c9875c
MP
480 vnt_get_phy_field(priv, rts_frame_len, priv->top_ofdm_basic_rate,
481 tx_context->pkt_type, &buf->ab);
1712633d 482
cca48e3c 483 buf->duration = vnt_get_rtscts_duration_le(tx_context, RTSDUR_AA,
e8c9875c 484 tx_context->pkt_type,
cca48e3c 485 current_rate);
1712633d 486
d38b13aa 487 vnt_fill_ieee80211_rts(tx_context, &buf->data, buf->duration);
1712633d 488
798f0601 489 return vnt_rxtx_datahead_ab(tx_context, &buf->data_head);
1712633d
MP
490}
491
c2c32da3 492static u16 vnt_rxtx_rts_a_fb_head(struct vnt_usb_send_context *tx_context,
798f0601 493 struct vnt_rts_a_fb *buf)
9d2578c1 494{
c2c32da3 495 struct vnt_private *priv = tx_context->priv;
798f0601 496 u16 current_rate = tx_context->tx_rate;
9d2578c1 497 u16 rts_frame_len = 20;
2eca8dbb 498
205056f3 499 vnt_get_phy_field(priv, rts_frame_len,
e8c9875c 500 priv->top_ofdm_basic_rate, tx_context->pkt_type, &buf->a);
9d2578c1 501
cca48e3c 502 buf->duration = vnt_get_rtscts_duration_le(tx_context, RTSDUR_AA,
e8c9875c 503 tx_context->pkt_type,
cca48e3c 504 current_rate);
9d2578c1 505
cca48e3c
MP
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);
9d2578c1 510
cca48e3c
MP
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);
9d2578c1 515
d38b13aa 516 vnt_fill_ieee80211_rts(tx_context, &buf->data, buf->duration);
9d2578c1 517
798f0601 518 return vnt_rxtx_datahead_a_fb(tx_context, &buf->data_head);
9d2578c1
MP
519}
520
4f31668f 521static u16 vnt_fill_cts_head(struct vnt_usb_send_context *tx_context,
798f0601 522 union vnt_tx_data_head *head)
92b96797 523{
ee16de8c
MP
524 struct vnt_private *priv = tx_context->priv;
525 u32 cts_frame_len = 14;
798f0601 526 u16 current_rate = tx_context->tx_rate;
2eca8dbb 527
27df3ebf 528 if (!head)
0a0f4b69 529 return 0;
92b96797 530
ce7b0db8 531 if (tx_context->fb_option) {
c921cc8c 532 /* Auto Fall back */
ee16de8c 533 struct vnt_cts_fb *buf = &head->cts_g_fb;
aed387c7 534 /* Get SignalField,ServiceField,Length */
ee16de8c 535 vnt_get_phy_field(priv, cts_frame_len,
d80bf43c 536 priv->top_cck_basic_rate, PK_TYPE_11B, &buf->b);
cca48e3c
MP
537 buf->duration_ba =
538 vnt_get_rtscts_duration_le(tx_context, CTSDUR_BA,
539 tx_context->pkt_type,
540 current_rate);
e34f9dbe 541 /* Get CTSDuration_ba_f0 */
cca48e3c
MP
542 buf->cts_duration_ba_f0 =
543 vnt_get_rtscts_duration_le(tx_context, CTSDUR_BA_F0,
544 tx_context->pkt_type,
545 priv->tx_rate_fb0);
e34f9dbe 546 /* Get CTSDuration_ba_f1 */
cca48e3c
MP
547 buf->cts_duration_ba_f1 =
548 vnt_get_rtscts_duration_le(tx_context, CTSDUR_BA_F1,
549 tx_context->pkt_type,
550 priv->tx_rate_fb1);
14840cdd 551 /* Get CTS Frame body */
ee16de8c
MP
552 buf->data.duration = buf->duration_ba;
553 buf->data.frame_control =
d9560ae5
MP
554 cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS);
555
e2382233 556 ether_addr_copy(buf->data.ra, priv->current_net_addr);
5b852f53 557
798f0601 558 return vnt_rxtx_datahead_g_fb(tx_context, &buf->data_head);
c921cc8c 559 } else {
ee16de8c 560 struct vnt_cts *buf = &head->cts_g;
aed387c7 561 /* Get SignalField,ServiceField,Length */
ee16de8c 562 vnt_get_phy_field(priv, cts_frame_len,
d80bf43c 563 priv->top_cck_basic_rate, PK_TYPE_11B, &buf->b);
e34f9dbe 564 /* Get CTSDuration_ba */
cca48e3c
MP
565 buf->duration_ba =
566 vnt_get_rtscts_duration_le(tx_context, CTSDUR_BA,
567 tx_context->pkt_type,
568 current_rate);
14840cdd 569 /*Get CTS Frame body*/
ee16de8c
MP
570 buf->data.duration = buf->duration_ba;
571 buf->data.frame_control =
d9560ae5
MP
572 cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS);
573
e2382233 574 ether_addr_copy(buf->data.ra, priv->current_net_addr);
78363fd1 575
798f0601 576 return vnt_rxtx_datahead_g(tx_context, &buf->data_head);
de657d59 577 }
0a0f4b69
MP
578
579 return 0;
92b96797
FB
580}
581
24cdd90f 582static u16 vnt_rxtx_rts(struct vnt_usb_send_context *tx_context,
798f0601 583 union vnt_tx_head *tx_head, bool need_mic)
24cdd90f
MP
584{
585 struct vnt_private *priv = tx_context->priv;
586 struct vnt_rrv_time_rts *buf = &tx_head->tx_rts.rts;
4f06c0dc 587 union vnt_tx_data_head *head = &tx_head->tx_rts.tx.head;
0f5567cb 588 u32 frame_len = tx_context->frame_len;
798f0601 589 u16 current_rate = tx_context->tx_rate;
2eca8dbb 590 u8 need_ack = tx_context->need_ack;
24cdd90f 591
aadf7d13 592 buf->rts_rrv_time_aa = vnt_get_rtscts_rsvtime_le(priv, 2,
0f5567cb 593 tx_context->pkt_type, frame_len, current_rate);
aadf7d13 594 buf->rts_rrv_time_ba = vnt_get_rtscts_rsvtime_le(priv, 1,
0f5567cb 595 tx_context->pkt_type, frame_len, current_rate);
aadf7d13 596 buf->rts_rrv_time_bb = vnt_get_rtscts_rsvtime_le(priv, 0,
0f5567cb 597 tx_context->pkt_type, frame_len, current_rate);
24cdd90f 598
e8c9875c 599 buf->rrv_time_a = vnt_rxtx_rsvtime_le16(priv, tx_context->pkt_type,
0f5567cb 600 frame_len, current_rate,
e8c9875c 601 need_ack);
0f5567cb 602 buf->rrv_time_b = vnt_rxtx_rsvtime_le16(priv, PK_TYPE_11B, frame_len,
d80bf43c 603 priv->top_cck_basic_rate, need_ack);
24cdd90f 604
4f06c0dc
MP
605 if (need_mic)
606 head = &tx_head->tx_rts.tx.mic.head;
607
608 if (tx_context->fb_option)
798f0601 609 return vnt_rxtx_rts_g_fb_head(tx_context, &head->rts_g_fb);
4f06c0dc 610
798f0601 611 return vnt_rxtx_rts_g_head(tx_context, &head->rts_g);
24cdd90f
MP
612}
613
bf9c0118 614static u16 vnt_rxtx_cts(struct vnt_usb_send_context *tx_context,
798f0601 615 union vnt_tx_head *tx_head, bool need_mic)
bf9c0118
MP
616{
617 struct vnt_private *priv = tx_context->priv;
618 struct vnt_rrv_time_cts *buf = &tx_head->tx_cts.cts;
b00cb684 619 union vnt_tx_data_head *head = &tx_head->tx_cts.tx.head;
0f5567cb 620 u32 frame_len = tx_context->frame_len;
798f0601 621 u16 current_rate = tx_context->tx_rate;
2eca8dbb
MP
622 u8 need_ack = tx_context->need_ack;
623
e8c9875c 624 buf->rrv_time_a = vnt_rxtx_rsvtime_le16(priv, tx_context->pkt_type,
0f5567cb 625 frame_len, current_rate, need_ack);
bf9c0118 626 buf->rrv_time_b = vnt_rxtx_rsvtime_le16(priv, PK_TYPE_11B,
0f5567cb 627 frame_len, priv->top_cck_basic_rate, need_ack);
bf9c0118 628
aadf7d13 629 buf->cts_rrv_time_ba = vnt_get_rtscts_rsvtime_le(priv, 3,
0f5567cb 630 tx_context->pkt_type, frame_len, current_rate);
bf9c0118 631
b00cb684
MP
632 if (need_mic)
633 head = &tx_head->tx_cts.tx.mic.head;
634
635 /* Fill CTS */
798f0601 636 return vnt_fill_cts_head(tx_context, head);
bf9c0118
MP
637}
638
3b03fea2 639static u16 vnt_rxtx_ab(struct vnt_usb_send_context *tx_context,
798f0601 640 union vnt_tx_head *tx_head, bool need_rts, bool need_mic)
3b03fea2
MP
641{
642 struct vnt_private *priv = tx_context->priv;
643 struct vnt_rrv_time_ab *buf = &tx_head->tx_ab.ab;
681acf68 644 union vnt_tx_data_head *head = &tx_head->tx_ab.tx.head;
0f5567cb 645 u32 frame_len = tx_context->frame_len;
798f0601 646 u16 current_rate = tx_context->tx_rate;
2eca8dbb 647 u8 need_ack = tx_context->need_ack;
3b03fea2 648
e8c9875c 649 buf->rrv_time = vnt_rxtx_rsvtime_le16(priv, tx_context->pkt_type,
0f5567cb 650 frame_len, current_rate, need_ack);
3b03fea2 651
681acf68
MP
652 if (need_mic)
653 head = &tx_head->tx_ab.tx.mic.head;
654
3b03fea2 655 if (need_rts) {
e8c9875c 656 if (tx_context->pkt_type == PK_TYPE_11B)
aadf7d13 657 buf->rts_rrv_time = vnt_get_rtscts_rsvtime_le(priv, 0,
0f5567cb 658 tx_context->pkt_type, frame_len, current_rate);
3b03fea2 659 else /* PK_TYPE_11A */
aadf7d13 660 buf->rts_rrv_time = vnt_get_rtscts_rsvtime_le(priv, 2,
0f5567cb 661 tx_context->pkt_type, frame_len, current_rate);
681acf68 662
e8c9875c
MP
663 if (tx_context->fb_option &&
664 tx_context->pkt_type == PK_TYPE_11A)
681acf68 665 return vnt_rxtx_rts_a_fb_head(tx_context,
798f0601 666 &head->rts_a_fb);
681acf68 667
798f0601 668 return vnt_rxtx_rts_ab_head(tx_context, &head->rts_ab);
3b03fea2
MP
669 }
670
e8c9875c 671 if (tx_context->pkt_type == PK_TYPE_11A)
798f0601 672 return vnt_rxtx_datahead_a_fb(tx_context,
0f5567cb 673 &head->data_head_a_fb);
681acf68 674
798f0601 675 return vnt_rxtx_datahead_ab(tx_context, &head->data_head_ab);
3b03fea2
MP
676}
677
9ec7f2d8 678static u16 vnt_generate_tx_parameter(struct vnt_usb_send_context *tx_context,
798f0601 679 struct vnt_tx_buffer *tx_buffer,
0f5567cb 680 struct vnt_mic_hdr **mic_hdr, u32 need_mic,
2eca8dbb 681 bool need_rts)
92b96797 682{
92b96797 683
e8c9875c
MP
684 if (tx_context->pkt_type == PK_TYPE_11GB ||
685 tx_context->pkt_type == PK_TYPE_11GA) {
92928f13 686 if (need_rts) {
4f06c0dc 687 if (need_mic)
92928f13
MP
688 *mic_hdr = &tx_buffer->
689 tx_head.tx_rts.tx.mic.hdr;
f0e0d505 690
4f06c0dc 691 return vnt_rxtx_rts(tx_context, &tx_buffer->tx_head,
798f0601 692 need_mic);
92928f13 693 }
da9ef9f4 694
681acf68 695 if (need_mic)
da9ef9f4 696 *mic_hdr = &tx_buffer->tx_head.tx_cts.tx.mic.hdr;
c12dca09 697
798f0601 698 return vnt_rxtx_cts(tx_context, &tx_buffer->tx_head, need_mic);
92928f13 699 }
0a0f4b69 700
da9ef9f4
MP
701 if (need_mic)
702 *mic_hdr = &tx_buffer->tx_head.tx_ab.tx.mic.hdr;
703
798f0601 704 return vnt_rxtx_ab(tx_context, &tx_buffer->tx_head, need_rts, need_mic);
92b96797 705}
d38b13aa
MP
706
707static void vnt_fill_txkey(struct vnt_usb_send_context *tx_context,
708 u8 *key_buffer, struct ieee80211_key_conf *tx_key, struct sk_buff *skb,
709 u16 payload_len, struct vnt_mic_hdr *mic_hdr)
710{
711 struct ieee80211_hdr *hdr = tx_context->hdr;
712 struct ieee80211_key_seq seq;
713 u8 *iv = ((u8 *)hdr + ieee80211_get_hdrlen_from_skb(skb));
714
715 /* strip header and icv len from payload */
716 payload_len -= ieee80211_get_hdrlen_from_skb(skb);
717 payload_len -= tx_key->icv_len;
718
719 switch (tx_key->cipher) {
720 case WLAN_CIPHER_SUITE_WEP40:
721 case WLAN_CIPHER_SUITE_WEP104:
722 memcpy(key_buffer, iv, 3);
723 memcpy(key_buffer + 3, tx_key->key, tx_key->keylen);
724
725 if (tx_key->keylen == WLAN_KEY_LEN_WEP40) {
726 memcpy(key_buffer + 8, iv, 3);
727 memcpy(key_buffer + 11,
728 tx_key->key, WLAN_KEY_LEN_WEP40);
729 }
730
731 break;
732 case WLAN_CIPHER_SUITE_TKIP:
733 ieee80211_get_tkip_p2k(tx_key, skb, key_buffer);
734
735 break;
736 case WLAN_CIPHER_SUITE_CCMP:
737
738 if (!mic_hdr)
739 return;
740
741 mic_hdr->id = 0x59;
742 mic_hdr->payload_len = cpu_to_be16(payload_len);
743 memcpy(mic_hdr->mic_addr2, hdr->addr2, ETH_ALEN);
744
745 ieee80211_get_key_tx_seq(tx_key, &seq);
746
79f976dc 747 memcpy(mic_hdr->ccmp_pn, seq.ccmp.pn, IEEE80211_CCMP_PN_LEN);
d38b13aa
MP
748
749 if (ieee80211_has_a4(hdr->frame_control))
750 mic_hdr->hlen = cpu_to_be16(28);
751 else
752 mic_hdr->hlen = cpu_to_be16(22);
753
754 memcpy(mic_hdr->addr1, hdr->addr1, ETH_ALEN);
755 memcpy(mic_hdr->addr2, hdr->addr2, ETH_ALEN);
756 memcpy(mic_hdr->addr3, hdr->addr3, ETH_ALEN);
757
758 mic_hdr->frame_control = cpu_to_le16(
759 le16_to_cpu(hdr->frame_control) & 0xc78f);
760 mic_hdr->seq_ctrl = cpu_to_le16(
761 le16_to_cpu(hdr->seq_ctrl) & 0xf);
762
763 if (ieee80211_has_a4(hdr->frame_control))
764 memcpy(mic_hdr->addr4, hdr->addr4, ETH_ALEN);
765
766
767 memcpy(key_buffer, tx_key->key, WLAN_KEY_LEN_CCMP);
768
769 break;
770 default:
771 break;
772 }
773
774}
775
776int vnt_tx_packet(struct vnt_private *priv, struct sk_buff *skb)
777{
778 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
779 struct ieee80211_tx_rate *tx_rate = &info->control.rates[0];
780 struct ieee80211_rate *rate;
781 struct ieee80211_key_conf *tx_key;
782 struct ieee80211_hdr *hdr;
783 struct vnt_mic_hdr *mic_hdr = NULL;
784 struct vnt_tx_buffer *tx_buffer;
785 struct vnt_tx_fifo_head *tx_buffer_head;
786 struct vnt_usb_send_context *tx_context;
787 unsigned long flags;
d38b13aa
MP
788 u16 tx_bytes, tx_header_size, tx_body_size, current_rate, duration_id;
789 u8 pkt_type, fb_option = AUTO_FB_NONE;
2eca8dbb 790 bool need_rts = false, is_pspoll = false;
d38b13aa
MP
791 bool need_mic = false;
792
793 hdr = (struct ieee80211_hdr *)(skb->data);
794
795 rate = ieee80211_get_tx_rate(priv->hw, info);
796
797 current_rate = rate->hw_value;
8b84c1da 798 if (priv->current_rate != current_rate &&
5091d963 799 !(priv->hw->conf.flags & IEEE80211_CONF_OFFCHANNEL)) {
8b84c1da 800 priv->current_rate = current_rate;
57981a65 801 vnt_schedule_command(priv, WLAN_CMD_SETPOWER);
d38b13aa
MP
802 }
803
804 if (current_rate > RATE_11M)
e12471db 805 pkt_type = priv->packet_type;
d38b13aa
MP
806 else
807 pkt_type = PK_TYPE_11B;
808
809 spin_lock_irqsave(&priv->lock, flags);
810
a032b16c 811 tx_context = vnt_get_free_context(priv);
d38b13aa
MP
812 if (!tx_context) {
813 dev_dbg(&priv->usb->dev, "%s No free context\n", __func__);
814 spin_unlock_irqrestore(&priv->lock, flags);
815 return -ENOMEM;
816 }
817
818 tx_context->skb = skb;
e8c9875c 819 tx_context->pkt_type = pkt_type;
2eca8dbb 820 tx_context->need_ack = false;
0f5567cb 821 tx_context->frame_len = skb->len + 4;
798f0601 822 tx_context->tx_rate = current_rate;
d38b13aa
MP
823
824 spin_unlock_irqrestore(&priv->lock, flags);
825
826 tx_buffer = (struct vnt_tx_buffer *)tx_context->data;
827 tx_buffer_head = &tx_buffer->fifo_head;
828 tx_body_size = skb->len;
829
d38b13aa
MP
830 /*Set fifo controls */
831 if (pkt_type == PK_TYPE_11A)
f0ff3fde 832 tx_buffer_head->fifo_ctl = 0;
d38b13aa 833 else if (pkt_type == PK_TYPE_11B)
47e08894 834 tx_buffer_head->fifo_ctl = cpu_to_le16(FIFOCTL_11B);
d38b13aa 835 else if (pkt_type == PK_TYPE_11GB)
47e08894 836 tx_buffer_head->fifo_ctl = cpu_to_le16(FIFOCTL_11GB);
d38b13aa 837 else if (pkt_type == PK_TYPE_11GA)
47e08894 838 tx_buffer_head->fifo_ctl = cpu_to_le16(FIFOCTL_11GA);
d38b13aa
MP
839
840 if (!ieee80211_is_data(hdr->frame_control)) {
47e08894
MP
841 tx_buffer_head->fifo_ctl |= cpu_to_le16(FIFOCTL_GENINT |
842 FIFOCTL_ISDMA0);
843 tx_buffer_head->fifo_ctl |= cpu_to_le16(FIFOCTL_TMOEN);
d38b13aa
MP
844
845 tx_buffer_head->time_stamp =
846 cpu_to_le16(DEFAULT_MGN_LIFETIME_RES_64us);
847 } else {
848 tx_buffer_head->time_stamp =
849 cpu_to_le16(DEFAULT_MSDU_LIFETIME_RES_64us);
850 }
851
852 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
47e08894 853 tx_buffer_head->fifo_ctl |= cpu_to_le16(FIFOCTL_NEEDACK);
2eca8dbb 854 tx_context->need_ack = true;
d38b13aa
MP
855 }
856
857 if (ieee80211_has_retry(hdr->frame_control))
47e08894 858 tx_buffer_head->fifo_ctl |= cpu_to_le16(FIFOCTL_LRETRY);
d38b13aa
MP
859
860 if (tx_rate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
98e93fe5 861 priv->preamble_type = PREAMBLE_SHORT;
d38b13aa 862 else
98e93fe5 863 priv->preamble_type = PREAMBLE_LONG;
d38b13aa
MP
864
865 if (tx_rate->flags & IEEE80211_TX_RC_USE_RTS_CTS) {
866 need_rts = true;
47e08894 867 tx_buffer_head->fifo_ctl |= cpu_to_le16(FIFOCTL_RTS);
d38b13aa
MP
868 }
869
870 if (ieee80211_has_a4(hdr->frame_control))
47e08894 871 tx_buffer_head->fifo_ctl |= cpu_to_le16(FIFOCTL_LHEAD);
d38b13aa
MP
872
873 if (info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER)
874 is_pspoll = true;
875
5deb1cfa
MP
876 tx_buffer_head->frag_ctl =
877 cpu_to_le16(ieee80211_get_hdrlen_from_skb(skb) << 10);
d38b13aa
MP
878
879 if (info->control.hw_key) {
880 tx_key = info->control.hw_key;
881 switch (info->control.hw_key->cipher) {
882 case WLAN_CIPHER_SUITE_WEP40:
883 case WLAN_CIPHER_SUITE_WEP104:
5deb1cfa 884 tx_buffer_head->frag_ctl |= cpu_to_le16(FRAGCTL_LEGACY);
d38b13aa
MP
885 break;
886 case WLAN_CIPHER_SUITE_TKIP:
5deb1cfa 887 tx_buffer_head->frag_ctl |= cpu_to_le16(FRAGCTL_TKIP);
d38b13aa
MP
888 break;
889 case WLAN_CIPHER_SUITE_CCMP:
5deb1cfa 890 tx_buffer_head->frag_ctl |= cpu_to_le16(FRAGCTL_AES);
d38b13aa
MP
891 need_mic = true;
892 default:
893 break;
894 }
0f5567cb 895 tx_context->frame_len += tx_key->icv_len;
d38b13aa
MP
896 }
897
cb8ee9de
MP
898 tx_buffer_head->current_rate = cpu_to_le16(current_rate);
899
d38b13aa
MP
900 /* legacy rates TODO use ieee80211_tx_rate */
901 if (current_rate >= RATE_18M && ieee80211_is_data(hdr->frame_control)) {
a6177aef 902 if (priv->auto_fb_ctrl == AUTO_FB_0) {
47e08894
MP
903 tx_buffer_head->fifo_ctl |=
904 cpu_to_le16(FIFOCTL_AUTO_FB_0);
d38b13aa
MP
905
906 priv->tx_rate_fb0 =
e3f31874 907 vnt_fb_opt0[FB_RATE0][current_rate - RATE_18M];
d38b13aa 908 priv->tx_rate_fb1 =
e3f31874 909 vnt_fb_opt0[FB_RATE1][current_rate - RATE_18M];
d38b13aa
MP
910
911 fb_option = AUTO_FB_0;
a6177aef 912 } else if (priv->auto_fb_ctrl == AUTO_FB_1) {
47e08894
MP
913 tx_buffer_head->fifo_ctl |=
914 cpu_to_le16(FIFOCTL_AUTO_FB_1);
d38b13aa
MP
915
916 priv->tx_rate_fb0 =
2331ba42 917 vnt_fb_opt1[FB_RATE0][current_rate - RATE_18M];
d38b13aa 918 priv->tx_rate_fb1 =
2331ba42 919 vnt_fb_opt1[FB_RATE1][current_rate - RATE_18M];
d38b13aa
MP
920
921 fb_option = AUTO_FB_1;
922 }
923 }
924
71d764ae
MP
925 tx_context->fb_option = fb_option;
926
798f0601
MP
927 duration_id = vnt_generate_tx_parameter(tx_context, tx_buffer, &mic_hdr,
928 need_mic, need_rts);
d38b13aa
MP
929
930 tx_header_size = tx_context->tx_hdr_size;
931 if (!tx_header_size) {
932 tx_context->in_use = false;
933 return -ENOMEM;
934 }
935
5deb1cfa 936 tx_buffer_head->frag_ctl |= cpu_to_le16(FRAGCTL_NONFRAG);
d38b13aa
MP
937
938 tx_bytes = tx_header_size + tx_body_size;
939
940 memcpy(tx_context->hdr, skb->data, tx_body_size);
941
942 hdr->duration_id = cpu_to_le16(duration_id);
943
944 if (info->control.hw_key) {
945 tx_key = info->control.hw_key;
946 if (tx_key->keylen > 0)
947 vnt_fill_txkey(tx_context, tx_buffer_head->tx_key,
948 tx_key, skb, tx_body_size, mic_hdr);
949 }
950
1b2bc0aa 951 priv->seq_counter = (le16_to_cpu(hdr->seq_ctrl) &
d38b13aa
MP
952 IEEE80211_SCTL_SEQ) >> 4;
953
954 tx_buffer->tx_byte_count = cpu_to_le16(tx_bytes);
d0335a03 955 tx_buffer->pkt_no = tx_context->pkt_no;
c8a3a4c0 956 tx_buffer->type = 0x00;
d38b13aa
MP
957
958 tx_bytes += 4;
959
960 tx_context->type = CONTEXT_DATA_PACKET;
961 tx_context->buf_len = tx_bytes;
962
963 spin_lock_irqsave(&priv->lock, flags);
964
476e7d97 965 if (vnt_tx_context(priv, tx_context) != STATUS_PENDING) {
d38b13aa
MP
966 spin_unlock_irqrestore(&priv->lock, flags);
967 return -EIO;
968 }
969
970 spin_unlock_irqrestore(&priv->lock, flags);
971
972 return 0;
973}
974
975static int vnt_beacon_xmit(struct vnt_private *priv,
976 struct sk_buff *skb)
977{
978 struct vnt_beacon_buffer *beacon_buffer;
979 struct vnt_tx_short_buf_head *short_head;
980 struct ieee80211_tx_info *info;
981 struct vnt_usb_send_context *context;
982 struct ieee80211_mgmt *mgmt_hdr;
983 unsigned long flags;
984 u32 frame_size = skb->len + 4;
985 u16 current_rate, count;
986
987 spin_lock_irqsave(&priv->lock, flags);
988
a032b16c 989 context = vnt_get_free_context(priv);
d38b13aa
MP
990 if (!context) {
991 dev_dbg(&priv->usb->dev, "%s No free context!\n", __func__);
992 spin_unlock_irqrestore(&priv->lock, flags);
993 return -ENOMEM;
994 }
995
996 context->skb = skb;
997
998 spin_unlock_irqrestore(&priv->lock, flags);
999
1000 beacon_buffer = (struct vnt_beacon_buffer *)&context->data[0];
1001 short_head = &beacon_buffer->short_head;
1002
65df77e2 1003 if (priv->bb_type == BB_TYPE_11A) {
d38b13aa
MP
1004 current_rate = RATE_6M;
1005
1006 /* Get SignalField,ServiceField,Length */
1007 vnt_get_phy_field(priv, frame_size, current_rate,
1008 PK_TYPE_11A, &short_head->ab);
1009
1010 /* Get Duration and TimeStampOff */
435ae3be 1011 short_head->duration = vnt_get_duration_le(priv,
d38b13aa
MP
1012 PK_TYPE_11A, false);
1013 short_head->time_stamp_off =
1014 vnt_time_stamp_off(priv, current_rate);
1015 } else {
1016 current_rate = RATE_1M;
14fb6ce8 1017 short_head->fifo_ctl |= cpu_to_le16(FIFOCTL_11B);
d38b13aa
MP
1018
1019 /* Get SignalField,ServiceField,Length */
1020 vnt_get_phy_field(priv, frame_size, current_rate,
1021 PK_TYPE_11B, &short_head->ab);
1022
1023 /* Get Duration and TimeStampOff */
435ae3be 1024 short_head->duration = vnt_get_duration_le(priv,
d38b13aa
MP
1025 PK_TYPE_11B, false);
1026 short_head->time_stamp_off =
1027 vnt_time_stamp_off(priv, current_rate);
1028 }
1029
1030 /* Generate Beacon Header */
1031 mgmt_hdr = &beacon_buffer->mgmt_hdr;
1032 memcpy(mgmt_hdr, skb->data, skb->len);
1033
1034 /* time stamp always 0 */
1035 mgmt_hdr->u.beacon.timestamp = 0;
1036
1037 info = IEEE80211_SKB_CB(skb);
1038 if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
1039 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)mgmt_hdr;
76be25ba 1040
d38b13aa 1041 hdr->duration_id = 0;
1b2bc0aa 1042 hdr->seq_ctrl = cpu_to_le16(priv->seq_counter << 4);
d38b13aa
MP
1043 }
1044
1b2bc0aa
MP
1045 priv->seq_counter++;
1046 if (priv->seq_counter > 0x0fff)
1047 priv->seq_counter = 0;
d38b13aa
MP
1048
1049 count = sizeof(struct vnt_tx_short_buf_head) + skb->len;
1050
1051 beacon_buffer->tx_byte_count = cpu_to_le16(count);
af90ab39 1052 beacon_buffer->pkt_no = context->pkt_no;
1e00b449 1053 beacon_buffer->type = 0x01;
d38b13aa
MP
1054
1055 context->type = CONTEXT_BEACON_PACKET;
1056 context->buf_len = count + 4; /* USB header */
1057
1058 spin_lock_irqsave(&priv->lock, flags);
1059
476e7d97 1060 if (vnt_tx_context(priv, context) != STATUS_PENDING)
d38b13aa
MP
1061 ieee80211_free_txskb(priv->hw, context->skb);
1062
1063 spin_unlock_irqrestore(&priv->lock, flags);
1064
1065 return 0;
1066}
1067
1068int vnt_beacon_make(struct vnt_private *priv, struct ieee80211_vif *vif)
1069{
1070 struct sk_buff *beacon;
1071
1072 beacon = ieee80211_beacon_get(priv->hw, vif);
1073 if (!beacon)
1074 return -ENOMEM;
1075
1076 if (vnt_beacon_xmit(priv, beacon)) {
1077 ieee80211_free_txskb(priv->hw, beacon);
1078 return -ENODEV;
1079 }
1080
1081 return 0;
1082}
1083
1084int vnt_beacon_enable(struct vnt_private *priv, struct ieee80211_vif *vif,
1085 struct ieee80211_bss_conf *conf)
1086{
d38b13aa
MP
1087 vnt_mac_reg_bits_off(priv, MAC_REG_TCR, TCR_AUTOBCNTX);
1088
1089 vnt_mac_reg_bits_off(priv, MAC_REG_TFTCTL, TFTCTL_TSFCNTREN);
1090
1091 vnt_mac_set_beacon_interval(priv, conf->beacon_int);
1092
1093 vnt_clear_current_tsf(priv);
1094
1095 vnt_mac_reg_bits_on(priv, MAC_REG_TFTCTL, TFTCTL_TSFCNTREN);
1096
1097 vnt_reset_next_tbtt(priv, conf->beacon_int);
1098
eab4e78d 1099 return vnt_beacon_make(priv, vif);
d38b13aa 1100}