2 * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
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.
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.
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.
21 * Purpose: handle WMAC/802.3/802.11 rx & tx 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
39 #include <linux/etherdevice.h>
47 static 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 */
52 static const u16 vnt_fb_opt0[2][5] = {
53 {RATE_12M, RATE_18M, RATE_24M, RATE_36M, RATE_48M}, /* fallback_rate0 */
54 {RATE_12M, RATE_12M, RATE_18M, RATE_24M, RATE_36M}, /* fallback_rate1 */
57 static const u16 vnt_fb_opt1[2][5] = {
58 {RATE_12M, RATE_18M, RATE_24M, RATE_24M, RATE_36M}, /* fallback_rate0 */
59 {RATE_6M, RATE_6M, RATE_12M, RATE_12M, RATE_18M}, /* fallback_rate1 */
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
74 #define DATADUR_A_F0 12
75 #define DATADUR_A_F1 13
77 static struct vnt_usb_send_context
78 *vnt_get_free_context(struct vnt_private *priv)
80 struct vnt_usb_send_context *context = NULL;
83 dev_dbg(&priv->usb->dev, "%s\n", __func__);
85 for (ii = 0; ii < priv->num_tx_context; ii++) {
86 if (!priv->tx_context[ii])
89 context = priv->tx_context[ii];
90 if (context->in_use == false) {
91 context->in_use = true;
92 memset(context->data, 0,
93 MAX_TOTAL_SIZE_WITH_ALL_HEADERS);
101 if (ii == priv->num_tx_context)
102 dev_dbg(&priv->usb->dev, "%s No Free Tx Context\n", __func__);
107 static __le16 vnt_time_stamp_off(struct vnt_private *priv, u16 rate)
109 return cpu_to_le16(vnt_time_stampoff[priv->preamble_type % 2]
113 static u32 vnt_get_rsvtime(struct vnt_private *priv, u8 pkt_type,
114 u32 frame_length, u16 rate, int need_ack)
116 u32 data_time, ack_time;
118 data_time = vnt_get_frame_time(priv->preamble_type, pkt_type,
121 if (pkt_type == PK_TYPE_11B)
122 ack_time = vnt_get_frame_time(priv->preamble_type, pkt_type,
123 14, (u16)priv->top_cck_basic_rate);
125 ack_time = vnt_get_frame_time(priv->preamble_type, pkt_type,
126 14, (u16)priv->top_ofdm_basic_rate);
129 return data_time + priv->sifs + ack_time;
134 static __le16 vnt_rxtx_rsvtime_le16(struct vnt_private *priv, u8 pkt_type,
135 u32 frame_length, u16 rate, int need_ack)
137 return cpu_to_le16((u16)vnt_get_rsvtime(priv, pkt_type,
138 frame_length, rate, need_ack));
141 static __le16 vnt_get_rtscts_rsvtime_le(struct vnt_private *priv,
142 u8 rsv_type, u8 pkt_type, u32 frame_length, u16 current_rate)
144 u32 rrv_time, rts_time, cts_time, ack_time, data_time;
146 rrv_time = rts_time = cts_time = ack_time = data_time = 0;
148 data_time = vnt_get_frame_time(priv->preamble_type, pkt_type,
149 frame_length, current_rate);
152 rts_time = vnt_get_frame_time(priv->preamble_type,
153 pkt_type, 20, priv->top_cck_basic_rate);
154 cts_time = ack_time = vnt_get_frame_time(priv->preamble_type,
155 pkt_type, 14, priv->top_cck_basic_rate);
156 } else if (rsv_type == 1) {
157 rts_time = vnt_get_frame_time(priv->preamble_type,
158 pkt_type, 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 cts_time = ack_time = vnt_get_frame_time(priv->preamble_type,
167 pkt_type, 14, priv->top_ofdm_basic_rate);
168 } else if (rsv_type == 3) {
169 cts_time = vnt_get_frame_time(priv->preamble_type, pkt_type,
170 14, priv->top_cck_basic_rate);
171 ack_time = vnt_get_frame_time(priv->preamble_type, pkt_type,
172 14, priv->top_ofdm_basic_rate);
174 rrv_time = cts_time + ack_time + data_time + 2 * priv->sifs;
176 return cpu_to_le16((u16)rrv_time);
179 rrv_time = rts_time + cts_time + ack_time + data_time + 3 * priv->sifs;
181 return cpu_to_le16((u16)rrv_time);
184 static __le16 vnt_get_duration_le(struct vnt_private *priv,
185 u8 pkt_type, int need_ack)
190 if (pkt_type == PK_TYPE_11B)
191 ack_time = vnt_get_frame_time(priv->preamble_type,
192 pkt_type, 14, priv->top_cck_basic_rate);
194 ack_time = vnt_get_frame_time(priv->preamble_type,
195 pkt_type, 14, priv->top_ofdm_basic_rate);
197 return cpu_to_le16((u16)(priv->sifs + ack_time));
203 static __le16 vnt_get_rtscts_duration_le(struct vnt_usb_send_context *context,
204 u8 dur_type, u8 pkt_type, u16 rate)
206 struct vnt_private *priv = context->priv;
207 u32 cts_time = 0, dur_time = 0;
208 u32 frame_length = context->frame_len;
209 u8 need_ack = context->need_ack;
216 cts_time = vnt_get_frame_time(priv->preamble_type,
217 pkt_type, 14, priv->top_cck_basic_rate);
218 dur_time = cts_time + 2 * priv->sifs +
219 vnt_get_rsvtime(priv, pkt_type,
220 frame_length, rate, need_ack);
226 cts_time = vnt_get_frame_time(priv->preamble_type,
227 pkt_type, 14, priv->top_ofdm_basic_rate);
228 dur_time = cts_time + 2 * priv->sifs +
229 vnt_get_rsvtime(priv, pkt_type,
230 frame_length, rate, need_ack);
236 dur_time = priv->sifs + vnt_get_rsvtime(priv,
237 pkt_type, frame_length, rate, need_ack);
244 return cpu_to_le16((u16)dur_time);
247 static u16 vnt_mac_hdr_pos(struct vnt_usb_send_context *tx_context,
248 struct ieee80211_hdr *hdr)
250 u8 *head = tx_context->data + offsetof(struct vnt_tx_buffer, fifo_head);
251 u8 *hdr_pos = (u8 *)hdr;
253 tx_context->hdr = hdr;
254 if (!tx_context->hdr)
257 return (u16)(hdr_pos - head);
260 static u16 vnt_rxtx_datahead_g(struct vnt_usb_send_context *tx_context,
261 struct vnt_tx_datahead_g *buf)
264 struct vnt_private *priv = tx_context->priv;
265 struct ieee80211_hdr *hdr =
266 (struct ieee80211_hdr *)tx_context->skb->data;
267 u32 frame_len = tx_context->frame_len;
268 u16 rate = tx_context->tx_rate;
269 u8 need_ack = tx_context->need_ack;
271 /* Get SignalField,ServiceField,Length */
272 vnt_get_phy_field(priv, frame_len, rate, tx_context->pkt_type, &buf->a);
273 vnt_get_phy_field(priv, frame_len, priv->top_cck_basic_rate,
274 PK_TYPE_11B, &buf->b);
276 /* Get Duration and TimeStamp */
277 if (ieee80211_is_pspoll(hdr->frame_control)) {
278 __le16 dur = cpu_to_le16(priv->current_aid | BIT(14) | BIT(15));
280 buf->duration_a = dur;
281 buf->duration_b = dur;
283 buf->duration_a = vnt_get_duration_le(priv,
284 tx_context->pkt_type, need_ack);
285 buf->duration_b = vnt_get_duration_le(priv,
286 PK_TYPE_11B, need_ack);
289 buf->time_stamp_off_a = vnt_time_stamp_off(priv, rate);
290 buf->time_stamp_off_b = vnt_time_stamp_off(priv,
291 priv->top_cck_basic_rate);
293 tx_context->tx_hdr_size = vnt_mac_hdr_pos(tx_context, &buf->hdr);
295 return le16_to_cpu(buf->duration_a);
298 static u16 vnt_rxtx_datahead_g_fb(struct vnt_usb_send_context *tx_context,
299 struct vnt_tx_datahead_g_fb *buf)
301 struct vnt_private *priv = tx_context->priv;
302 u32 frame_len = tx_context->frame_len;
303 u16 rate = tx_context->tx_rate;
304 u8 need_ack = tx_context->need_ack;
306 /* Get SignalField,ServiceField,Length */
307 vnt_get_phy_field(priv, frame_len, rate, tx_context->pkt_type, &buf->a);
309 vnt_get_phy_field(priv, frame_len, priv->top_cck_basic_rate,
310 PK_TYPE_11B, &buf->b);
312 /* Get Duration and TimeStamp */
313 buf->duration_a = vnt_get_duration_le(priv, tx_context->pkt_type,
315 buf->duration_b = vnt_get_duration_le(priv, PK_TYPE_11B, need_ack);
317 buf->duration_a_f0 = vnt_get_duration_le(priv, tx_context->pkt_type,
319 buf->duration_a_f1 = vnt_get_duration_le(priv, tx_context->pkt_type,
322 buf->time_stamp_off_a = vnt_time_stamp_off(priv, rate);
323 buf->time_stamp_off_b = vnt_time_stamp_off(priv,
324 priv->top_cck_basic_rate);
326 tx_context->tx_hdr_size = vnt_mac_hdr_pos(tx_context, &buf->hdr);
328 return le16_to_cpu(buf->duration_a);
331 static u16 vnt_rxtx_datahead_a_fb(struct vnt_usb_send_context *tx_context,
332 struct vnt_tx_datahead_a_fb *buf)
334 struct vnt_private *priv = tx_context->priv;
335 u16 rate = tx_context->tx_rate;
336 u8 pkt_type = tx_context->pkt_type;
337 u8 need_ack = tx_context->need_ack;
338 u32 frame_len = tx_context->frame_len;
340 /* Get SignalField,ServiceField,Length */
341 vnt_get_phy_field(priv, frame_len, rate, pkt_type, &buf->a);
342 /* Get Duration and TimeStampOff */
343 buf->duration = vnt_get_duration_le(priv, pkt_type, need_ack);
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);
348 buf->time_stamp_off = vnt_time_stamp_off(priv, rate);
350 tx_context->tx_hdr_size = vnt_mac_hdr_pos(tx_context, &buf->hdr);
352 return le16_to_cpu(buf->duration);
355 static u16 vnt_rxtx_datahead_ab(struct vnt_usb_send_context *tx_context,
356 struct vnt_tx_datahead_ab *buf)
358 struct vnt_private *priv = tx_context->priv;
359 struct ieee80211_hdr *hdr =
360 (struct ieee80211_hdr *)tx_context->skb->data;
361 u32 frame_len = tx_context->frame_len;
362 u16 rate = tx_context->tx_rate;
363 u8 need_ack = tx_context->need_ack;
365 /* Get SignalField,ServiceField,Length */
366 vnt_get_phy_field(priv, frame_len, rate,
367 tx_context->pkt_type, &buf->ab);
369 /* Get Duration and TimeStampOff */
370 if (ieee80211_is_pspoll(hdr->frame_control)) {
371 __le16 dur = cpu_to_le16(priv->current_aid | BIT(14) | BIT(15));
375 buf->duration = vnt_get_duration_le(priv, tx_context->pkt_type,
379 buf->time_stamp_off = vnt_time_stamp_off(priv, rate);
381 tx_context->tx_hdr_size = vnt_mac_hdr_pos(tx_context, &buf->hdr);
383 return le16_to_cpu(buf->duration);
386 static int vnt_fill_ieee80211_rts(struct vnt_usb_send_context *tx_context,
387 struct ieee80211_rts *rts, __le16 duration)
389 struct ieee80211_hdr *hdr =
390 (struct ieee80211_hdr *)tx_context->skb->data;
392 rts->duration = duration;
394 cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS);
396 ether_addr_copy(rts->ra, hdr->addr1);
397 ether_addr_copy(rts->ta, hdr->addr2);
402 static u16 vnt_rxtx_rts_g_head(struct vnt_usb_send_context *tx_context,
403 struct vnt_rts_g *buf)
405 struct vnt_private *priv = tx_context->priv;
406 u16 rts_frame_len = 20;
407 u16 current_rate = tx_context->tx_rate;
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);
414 buf->duration_bb = vnt_get_rtscts_duration_le(tx_context, RTSDUR_BB,
416 priv->top_cck_basic_rate);
417 buf->duration_aa = vnt_get_rtscts_duration_le(tx_context, RTSDUR_AA,
418 tx_context->pkt_type,
420 buf->duration_ba = vnt_get_rtscts_duration_le(tx_context, RTSDUR_BA,
421 tx_context->pkt_type,
424 vnt_fill_ieee80211_rts(tx_context, &buf->data, buf->duration_aa);
426 return vnt_rxtx_datahead_g(tx_context, &buf->data_head);
429 static u16 vnt_rxtx_rts_g_fb_head(struct vnt_usb_send_context *tx_context,
430 struct vnt_rts_g_fb *buf)
432 struct vnt_private *priv = tx_context->priv;
433 u16 current_rate = tx_context->tx_rate;
434 u16 rts_frame_len = 20;
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);
441 buf->duration_bb = vnt_get_rtscts_duration_le(tx_context, RTSDUR_BB,
443 priv->top_cck_basic_rate);
444 buf->duration_aa = vnt_get_rtscts_duration_le(tx_context, RTSDUR_AA,
445 tx_context->pkt_type,
447 buf->duration_ba = vnt_get_rtscts_duration_le(tx_context, RTSDUR_BA,
448 tx_context->pkt_type,
451 buf->rts_duration_ba_f0 =
452 vnt_get_rtscts_duration_le(tx_context, RTSDUR_BA_F0,
453 tx_context->pkt_type,
455 buf->rts_duration_aa_f0 =
456 vnt_get_rtscts_duration_le(tx_context, RTSDUR_AA_F0,
457 tx_context->pkt_type,
459 buf->rts_duration_ba_f1 =
460 vnt_get_rtscts_duration_le(tx_context, RTSDUR_BA_F1,
461 tx_context->pkt_type,
463 buf->rts_duration_aa_f1 =
464 vnt_get_rtscts_duration_le(tx_context, RTSDUR_AA_F1,
465 tx_context->pkt_type,
468 vnt_fill_ieee80211_rts(tx_context, &buf->data, buf->duration_aa);
470 return vnt_rxtx_datahead_g_fb(tx_context, &buf->data_head);
473 static u16 vnt_rxtx_rts_ab_head(struct vnt_usb_send_context *tx_context,
474 struct vnt_rts_ab *buf)
476 struct vnt_private *priv = tx_context->priv;
477 u16 current_rate = tx_context->tx_rate;
478 u16 rts_frame_len = 20;
480 vnt_get_phy_field(priv, rts_frame_len, priv->top_ofdm_basic_rate,
481 tx_context->pkt_type, &buf->ab);
483 buf->duration = vnt_get_rtscts_duration_le(tx_context, RTSDUR_AA,
484 tx_context->pkt_type,
487 vnt_fill_ieee80211_rts(tx_context, &buf->data, buf->duration);
489 return vnt_rxtx_datahead_ab(tx_context, &buf->data_head);
492 static u16 vnt_rxtx_rts_a_fb_head(struct vnt_usb_send_context *tx_context,
493 struct vnt_rts_a_fb *buf)
495 struct vnt_private *priv = tx_context->priv;
496 u16 current_rate = tx_context->tx_rate;
497 u16 rts_frame_len = 20;
499 vnt_get_phy_field(priv, rts_frame_len,
500 priv->top_ofdm_basic_rate, tx_context->pkt_type, &buf->a);
502 buf->duration = vnt_get_rtscts_duration_le(tx_context, RTSDUR_AA,
503 tx_context->pkt_type,
506 buf->rts_duration_f0 =
507 vnt_get_rtscts_duration_le(tx_context, RTSDUR_AA_F0,
508 tx_context->pkt_type,
511 buf->rts_duration_f1 =
512 vnt_get_rtscts_duration_le(tx_context, RTSDUR_AA_F1,
513 tx_context->pkt_type,
516 vnt_fill_ieee80211_rts(tx_context, &buf->data, buf->duration);
518 return vnt_rxtx_datahead_a_fb(tx_context, &buf->data_head);
521 static u16 vnt_fill_cts_head(struct vnt_usb_send_context *tx_context,
522 union vnt_tx_data_head *head)
524 struct vnt_private *priv = tx_context->priv;
525 u32 cts_frame_len = 14;
526 u16 current_rate = tx_context->tx_rate;
528 if (tx_context->fb_option) {
530 struct vnt_cts_fb *buf = &head->cts_g_fb;
531 /* Get SignalField,ServiceField,Length */
532 vnt_get_phy_field(priv, cts_frame_len,
533 priv->top_cck_basic_rate, PK_TYPE_11B, &buf->b);
535 vnt_get_rtscts_duration_le(tx_context, CTSDUR_BA,
536 tx_context->pkt_type,
538 /* Get CTSDuration_ba_f0 */
539 buf->cts_duration_ba_f0 =
540 vnt_get_rtscts_duration_le(tx_context, CTSDUR_BA_F0,
541 tx_context->pkt_type,
543 /* Get CTSDuration_ba_f1 */
544 buf->cts_duration_ba_f1 =
545 vnt_get_rtscts_duration_le(tx_context, CTSDUR_BA_F1,
546 tx_context->pkt_type,
548 /* Get CTS Frame body */
549 buf->data.duration = buf->duration_ba;
550 buf->data.frame_control =
551 cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS);
553 ether_addr_copy(buf->data.ra, priv->current_net_addr);
555 return vnt_rxtx_datahead_g_fb(tx_context, &buf->data_head);
557 struct vnt_cts *buf = &head->cts_g;
558 /* Get SignalField,ServiceField,Length */
559 vnt_get_phy_field(priv, cts_frame_len,
560 priv->top_cck_basic_rate, PK_TYPE_11B, &buf->b);
561 /* Get CTSDuration_ba */
563 vnt_get_rtscts_duration_le(tx_context, CTSDUR_BA,
564 tx_context->pkt_type,
566 /*Get CTS Frame body*/
567 buf->data.duration = buf->duration_ba;
568 buf->data.frame_control =
569 cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS);
571 ether_addr_copy(buf->data.ra, priv->current_net_addr);
573 return vnt_rxtx_datahead_g(tx_context, &buf->data_head);
577 static u16 vnt_rxtx_rts(struct vnt_usb_send_context *tx_context,
578 union vnt_tx_head *tx_head, bool need_mic)
580 struct vnt_private *priv = tx_context->priv;
581 struct vnt_rrv_time_rts *buf = &tx_head->tx_rts.rts;
582 union vnt_tx_data_head *head = &tx_head->tx_rts.tx.head;
583 u32 frame_len = tx_context->frame_len;
584 u16 current_rate = tx_context->tx_rate;
585 u8 need_ack = tx_context->need_ack;
587 buf->rts_rrv_time_aa = vnt_get_rtscts_rsvtime_le(priv, 2,
588 tx_context->pkt_type, frame_len, current_rate);
589 buf->rts_rrv_time_ba = vnt_get_rtscts_rsvtime_le(priv, 1,
590 tx_context->pkt_type, frame_len, current_rate);
591 buf->rts_rrv_time_bb = vnt_get_rtscts_rsvtime_le(priv, 0,
592 tx_context->pkt_type, frame_len, current_rate);
594 buf->rrv_time_a = vnt_rxtx_rsvtime_le16(priv, tx_context->pkt_type,
595 frame_len, current_rate,
597 buf->rrv_time_b = vnt_rxtx_rsvtime_le16(priv, PK_TYPE_11B, frame_len,
598 priv->top_cck_basic_rate, need_ack);
601 head = &tx_head->tx_rts.tx.mic.head;
603 if (tx_context->fb_option)
604 return vnt_rxtx_rts_g_fb_head(tx_context, &head->rts_g_fb);
606 return vnt_rxtx_rts_g_head(tx_context, &head->rts_g);
609 static u16 vnt_rxtx_cts(struct vnt_usb_send_context *tx_context,
610 union vnt_tx_head *tx_head, bool need_mic)
612 struct vnt_private *priv = tx_context->priv;
613 struct vnt_rrv_time_cts *buf = &tx_head->tx_cts.cts;
614 union vnt_tx_data_head *head = &tx_head->tx_cts.tx.head;
615 u32 frame_len = tx_context->frame_len;
616 u16 current_rate = tx_context->tx_rate;
617 u8 need_ack = tx_context->need_ack;
619 buf->rrv_time_a = vnt_rxtx_rsvtime_le16(priv, tx_context->pkt_type,
620 frame_len, current_rate, need_ack);
621 buf->rrv_time_b = vnt_rxtx_rsvtime_le16(priv, PK_TYPE_11B,
622 frame_len, priv->top_cck_basic_rate, need_ack);
624 buf->cts_rrv_time_ba = vnt_get_rtscts_rsvtime_le(priv, 3,
625 tx_context->pkt_type, frame_len, current_rate);
628 head = &tx_head->tx_cts.tx.mic.head;
631 return vnt_fill_cts_head(tx_context, head);
634 static u16 vnt_rxtx_ab(struct vnt_usb_send_context *tx_context,
635 union vnt_tx_head *tx_head, bool need_rts, bool need_mic)
637 struct vnt_private *priv = tx_context->priv;
638 struct vnt_rrv_time_ab *buf = &tx_head->tx_ab.ab;
639 union vnt_tx_data_head *head = &tx_head->tx_ab.tx.head;
640 u32 frame_len = tx_context->frame_len;
641 u16 current_rate = tx_context->tx_rate;
642 u8 need_ack = tx_context->need_ack;
644 buf->rrv_time = vnt_rxtx_rsvtime_le16(priv, tx_context->pkt_type,
645 frame_len, current_rate, need_ack);
648 head = &tx_head->tx_ab.tx.mic.head;
651 if (tx_context->pkt_type == PK_TYPE_11B)
652 buf->rts_rrv_time = vnt_get_rtscts_rsvtime_le(priv, 0,
653 tx_context->pkt_type, frame_len, current_rate);
654 else /* PK_TYPE_11A */
655 buf->rts_rrv_time = vnt_get_rtscts_rsvtime_le(priv, 2,
656 tx_context->pkt_type, frame_len, current_rate);
658 if (tx_context->fb_option &&
659 tx_context->pkt_type == PK_TYPE_11A)
660 return vnt_rxtx_rts_a_fb_head(tx_context,
663 return vnt_rxtx_rts_ab_head(tx_context, &head->rts_ab);
666 if (tx_context->pkt_type == PK_TYPE_11A)
667 return vnt_rxtx_datahead_a_fb(tx_context,
668 &head->data_head_a_fb);
670 return vnt_rxtx_datahead_ab(tx_context, &head->data_head_ab);
673 static u16 vnt_generate_tx_parameter(struct vnt_usb_send_context *tx_context,
674 struct vnt_tx_buffer *tx_buffer,
675 struct vnt_mic_hdr **mic_hdr, u32 need_mic,
679 if (tx_context->pkt_type == PK_TYPE_11GB ||
680 tx_context->pkt_type == PK_TYPE_11GA) {
683 *mic_hdr = &tx_buffer->
684 tx_head.tx_rts.tx.mic.hdr;
686 return vnt_rxtx_rts(tx_context, &tx_buffer->tx_head,
691 *mic_hdr = &tx_buffer->tx_head.tx_cts.tx.mic.hdr;
693 return vnt_rxtx_cts(tx_context, &tx_buffer->tx_head, need_mic);
697 *mic_hdr = &tx_buffer->tx_head.tx_ab.tx.mic.hdr;
699 return vnt_rxtx_ab(tx_context, &tx_buffer->tx_head, need_rts, need_mic);
702 static void vnt_fill_txkey(struct vnt_usb_send_context *tx_context,
703 u8 *key_buffer, struct ieee80211_key_conf *tx_key, struct sk_buff *skb,
704 u16 payload_len, struct vnt_mic_hdr *mic_hdr)
706 struct ieee80211_hdr *hdr = tx_context->hdr;
707 struct ieee80211_key_seq seq;
708 u8 *iv = ((u8 *)hdr + ieee80211_get_hdrlen_from_skb(skb));
710 /* strip header and icv len from payload */
711 payload_len -= ieee80211_get_hdrlen_from_skb(skb);
712 payload_len -= tx_key->icv_len;
714 switch (tx_key->cipher) {
715 case WLAN_CIPHER_SUITE_WEP40:
716 case WLAN_CIPHER_SUITE_WEP104:
717 memcpy(key_buffer, iv, 3);
718 memcpy(key_buffer + 3, tx_key->key, tx_key->keylen);
720 if (tx_key->keylen == WLAN_KEY_LEN_WEP40) {
721 memcpy(key_buffer + 8, iv, 3);
722 memcpy(key_buffer + 11,
723 tx_key->key, WLAN_KEY_LEN_WEP40);
727 case WLAN_CIPHER_SUITE_TKIP:
728 ieee80211_get_tkip_p2k(tx_key, skb, key_buffer);
731 case WLAN_CIPHER_SUITE_CCMP:
737 mic_hdr->payload_len = cpu_to_be16(payload_len);
738 ether_addr_copy(mic_hdr->mic_addr2, hdr->addr2);
740 ieee80211_get_key_tx_seq(tx_key, &seq);
742 memcpy(mic_hdr->ccmp_pn, seq.ccmp.pn, IEEE80211_CCMP_PN_LEN);
744 if (ieee80211_has_a4(hdr->frame_control))
745 mic_hdr->hlen = cpu_to_be16(28);
747 mic_hdr->hlen = cpu_to_be16(22);
749 memcpy(mic_hdr->addr1, hdr->addr1, ETH_ALEN);
750 memcpy(mic_hdr->addr2, hdr->addr2, ETH_ALEN);
751 memcpy(mic_hdr->addr3, hdr->addr3, ETH_ALEN);
753 mic_hdr->frame_control = cpu_to_le16(
754 le16_to_cpu(hdr->frame_control) & 0xc78f);
755 mic_hdr->seq_ctrl = cpu_to_le16(
756 le16_to_cpu(hdr->seq_ctrl) & 0xf);
758 if (ieee80211_has_a4(hdr->frame_control))
759 memcpy(mic_hdr->addr4, hdr->addr4, ETH_ALEN);
762 memcpy(key_buffer, tx_key->key, WLAN_KEY_LEN_CCMP);
771 int vnt_tx_packet(struct vnt_private *priv, struct sk_buff *skb)
773 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
774 struct ieee80211_tx_rate *tx_rate = &info->control.rates[0];
775 struct ieee80211_rate *rate;
776 struct ieee80211_key_conf *tx_key;
777 struct ieee80211_hdr *hdr;
778 struct vnt_mic_hdr *mic_hdr = NULL;
779 struct vnt_tx_buffer *tx_buffer;
780 struct vnt_tx_fifo_head *tx_buffer_head;
781 struct vnt_usb_send_context *tx_context;
783 u16 tx_bytes, tx_header_size, tx_body_size, current_rate, duration_id;
784 u8 pkt_type, fb_option = AUTO_FB_NONE;
785 bool need_rts = false, is_pspoll = false;
786 bool need_mic = false;
788 hdr = (struct ieee80211_hdr *)(skb->data);
790 rate = ieee80211_get_tx_rate(priv->hw, info);
792 current_rate = rate->hw_value;
793 if (priv->current_rate != current_rate &&
794 !(priv->hw->conf.flags & IEEE80211_CONF_OFFCHANNEL)) {
795 priv->current_rate = current_rate;
796 vnt_schedule_command(priv, WLAN_CMD_SETPOWER);
799 if (current_rate > RATE_11M)
800 pkt_type = priv->packet_type;
802 pkt_type = PK_TYPE_11B;
804 spin_lock_irqsave(&priv->lock, flags);
806 tx_context = vnt_get_free_context(priv);
808 dev_dbg(&priv->usb->dev, "%s No free context\n", __func__);
809 spin_unlock_irqrestore(&priv->lock, flags);
813 tx_context->skb = skb;
814 tx_context->pkt_type = pkt_type;
815 tx_context->need_ack = false;
816 tx_context->frame_len = skb->len + 4;
817 tx_context->tx_rate = current_rate;
819 spin_unlock_irqrestore(&priv->lock, flags);
821 tx_buffer = (struct vnt_tx_buffer *)tx_context->data;
822 tx_buffer_head = &tx_buffer->fifo_head;
823 tx_body_size = skb->len;
825 /*Set fifo controls */
826 if (pkt_type == PK_TYPE_11A)
827 tx_buffer_head->fifo_ctl = 0;
828 else if (pkt_type == PK_TYPE_11B)
829 tx_buffer_head->fifo_ctl = cpu_to_le16(FIFOCTL_11B);
830 else if (pkt_type == PK_TYPE_11GB)
831 tx_buffer_head->fifo_ctl = cpu_to_le16(FIFOCTL_11GB);
832 else if (pkt_type == PK_TYPE_11GA)
833 tx_buffer_head->fifo_ctl = cpu_to_le16(FIFOCTL_11GA);
835 if (!ieee80211_is_data(hdr->frame_control)) {
836 tx_buffer_head->fifo_ctl |= cpu_to_le16(FIFOCTL_GENINT |
838 tx_buffer_head->fifo_ctl |= cpu_to_le16(FIFOCTL_TMOEN);
840 tx_buffer_head->time_stamp =
841 cpu_to_le16(DEFAULT_MGN_LIFETIME_RES_64us);
843 tx_buffer_head->time_stamp =
844 cpu_to_le16(DEFAULT_MSDU_LIFETIME_RES_64us);
847 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
848 tx_buffer_head->fifo_ctl |= cpu_to_le16(FIFOCTL_NEEDACK);
849 tx_context->need_ack = true;
852 if (ieee80211_has_retry(hdr->frame_control))
853 tx_buffer_head->fifo_ctl |= cpu_to_le16(FIFOCTL_LRETRY);
855 if (tx_rate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
856 priv->preamble_type = PREAMBLE_SHORT;
858 priv->preamble_type = PREAMBLE_LONG;
860 if (tx_rate->flags & IEEE80211_TX_RC_USE_RTS_CTS) {
862 tx_buffer_head->fifo_ctl |= cpu_to_le16(FIFOCTL_RTS);
865 if (ieee80211_has_a4(hdr->frame_control))
866 tx_buffer_head->fifo_ctl |= cpu_to_le16(FIFOCTL_LHEAD);
868 if (info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER)
871 tx_buffer_head->frag_ctl =
872 cpu_to_le16(ieee80211_get_hdrlen_from_skb(skb) << 10);
874 if (info->control.hw_key) {
875 tx_key = info->control.hw_key;
876 switch (info->control.hw_key->cipher) {
877 case WLAN_CIPHER_SUITE_WEP40:
878 case WLAN_CIPHER_SUITE_WEP104:
879 tx_buffer_head->frag_ctl |= cpu_to_le16(FRAGCTL_LEGACY);
881 case WLAN_CIPHER_SUITE_TKIP:
882 tx_buffer_head->frag_ctl |= cpu_to_le16(FRAGCTL_TKIP);
884 case WLAN_CIPHER_SUITE_CCMP:
885 tx_buffer_head->frag_ctl |= cpu_to_le16(FRAGCTL_AES);
890 tx_context->frame_len += tx_key->icv_len;
893 tx_buffer_head->current_rate = cpu_to_le16(current_rate);
895 /* legacy rates TODO use ieee80211_tx_rate */
896 if (current_rate >= RATE_18M && ieee80211_is_data(hdr->frame_control)) {
897 if (priv->auto_fb_ctrl == AUTO_FB_0) {
898 tx_buffer_head->fifo_ctl |=
899 cpu_to_le16(FIFOCTL_AUTO_FB_0);
902 vnt_fb_opt0[FB_RATE0][current_rate - RATE_18M];
904 vnt_fb_opt0[FB_RATE1][current_rate - RATE_18M];
906 fb_option = AUTO_FB_0;
907 } else if (priv->auto_fb_ctrl == AUTO_FB_1) {
908 tx_buffer_head->fifo_ctl |=
909 cpu_to_le16(FIFOCTL_AUTO_FB_1);
912 vnt_fb_opt1[FB_RATE0][current_rate - RATE_18M];
914 vnt_fb_opt1[FB_RATE1][current_rate - RATE_18M];
916 fb_option = AUTO_FB_1;
920 tx_context->fb_option = fb_option;
922 duration_id = vnt_generate_tx_parameter(tx_context, tx_buffer, &mic_hdr,
925 tx_header_size = tx_context->tx_hdr_size;
926 if (!tx_header_size) {
927 tx_context->in_use = false;
931 tx_buffer_head->frag_ctl |= cpu_to_le16(FRAGCTL_NONFRAG);
933 tx_bytes = tx_header_size + tx_body_size;
935 memcpy(tx_context->hdr, skb->data, tx_body_size);
937 hdr->duration_id = cpu_to_le16(duration_id);
939 if (info->control.hw_key) {
940 tx_key = info->control.hw_key;
941 if (tx_key->keylen > 0)
942 vnt_fill_txkey(tx_context, tx_buffer_head->tx_key,
943 tx_key, skb, tx_body_size, mic_hdr);
946 priv->seq_counter = (le16_to_cpu(hdr->seq_ctrl) &
947 IEEE80211_SCTL_SEQ) >> 4;
949 tx_buffer->tx_byte_count = cpu_to_le16(tx_bytes);
950 tx_buffer->pkt_no = tx_context->pkt_no;
951 tx_buffer->type = 0x00;
955 tx_context->type = CONTEXT_DATA_PACKET;
956 tx_context->buf_len = tx_bytes;
958 spin_lock_irqsave(&priv->lock, flags);
960 if (vnt_tx_context(priv, tx_context) != STATUS_PENDING) {
961 spin_unlock_irqrestore(&priv->lock, flags);
965 spin_unlock_irqrestore(&priv->lock, flags);
970 static int vnt_beacon_xmit(struct vnt_private *priv,
973 struct vnt_beacon_buffer *beacon_buffer;
974 struct vnt_tx_short_buf_head *short_head;
975 struct ieee80211_tx_info *info;
976 struct vnt_usb_send_context *context;
977 struct ieee80211_mgmt *mgmt_hdr;
979 u32 frame_size = skb->len + 4;
980 u16 current_rate, count;
982 spin_lock_irqsave(&priv->lock, flags);
984 context = vnt_get_free_context(priv);
986 dev_dbg(&priv->usb->dev, "%s No free context!\n", __func__);
987 spin_unlock_irqrestore(&priv->lock, flags);
993 spin_unlock_irqrestore(&priv->lock, flags);
995 beacon_buffer = (struct vnt_beacon_buffer *)&context->data[0];
996 short_head = &beacon_buffer->short_head;
998 if (priv->bb_type == BB_TYPE_11A) {
999 current_rate = RATE_6M;
1001 /* Get SignalField,ServiceField,Length */
1002 vnt_get_phy_field(priv, frame_size, current_rate,
1003 PK_TYPE_11A, &short_head->ab);
1005 /* Get Duration and TimeStampOff */
1006 short_head->duration = vnt_get_duration_le(priv,
1007 PK_TYPE_11A, false);
1008 short_head->time_stamp_off =
1009 vnt_time_stamp_off(priv, current_rate);
1011 current_rate = RATE_1M;
1012 short_head->fifo_ctl |= cpu_to_le16(FIFOCTL_11B);
1014 /* Get SignalField,ServiceField,Length */
1015 vnt_get_phy_field(priv, frame_size, current_rate,
1016 PK_TYPE_11B, &short_head->ab);
1018 /* Get Duration and TimeStampOff */
1019 short_head->duration = vnt_get_duration_le(priv,
1020 PK_TYPE_11B, false);
1021 short_head->time_stamp_off =
1022 vnt_time_stamp_off(priv, current_rate);
1025 /* Generate Beacon Header */
1026 mgmt_hdr = &beacon_buffer->mgmt_hdr;
1027 memcpy(mgmt_hdr, skb->data, skb->len);
1029 /* time stamp always 0 */
1030 mgmt_hdr->u.beacon.timestamp = 0;
1032 info = IEEE80211_SKB_CB(skb);
1033 if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
1034 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)mgmt_hdr;
1036 hdr->duration_id = 0;
1037 hdr->seq_ctrl = cpu_to_le16(priv->seq_counter << 4);
1040 priv->seq_counter++;
1041 if (priv->seq_counter > 0x0fff)
1042 priv->seq_counter = 0;
1044 count = sizeof(struct vnt_tx_short_buf_head) + skb->len;
1046 beacon_buffer->tx_byte_count = cpu_to_le16(count);
1047 beacon_buffer->pkt_no = context->pkt_no;
1048 beacon_buffer->type = 0x01;
1050 context->type = CONTEXT_BEACON_PACKET;
1051 context->buf_len = count + 4; /* USB header */
1053 spin_lock_irqsave(&priv->lock, flags);
1055 if (vnt_tx_context(priv, context) != STATUS_PENDING)
1056 ieee80211_free_txskb(priv->hw, context->skb);
1058 spin_unlock_irqrestore(&priv->lock, flags);
1063 int vnt_beacon_make(struct vnt_private *priv, struct ieee80211_vif *vif)
1065 struct sk_buff *beacon;
1067 beacon = ieee80211_beacon_get(priv->hw, vif);
1071 if (vnt_beacon_xmit(priv, beacon)) {
1072 ieee80211_free_txskb(priv->hw, beacon);
1079 int vnt_beacon_enable(struct vnt_private *priv, struct ieee80211_vif *vif,
1080 struct ieee80211_bss_conf *conf)
1082 vnt_mac_reg_bits_off(priv, MAC_REG_TCR, TCR_AUTOBCNTX);
1084 vnt_mac_reg_bits_off(priv, MAC_REG_TFTCTL, TFTCTL_TSFCNTREN);
1086 vnt_mac_set_beacon_interval(priv, conf->beacon_int);
1088 vnt_clear_current_tsf(priv);
1090 vnt_mac_reg_bits_on(priv, MAC_REG_TFTCTL, TFTCTL_TSFCNTREN);
1092 vnt_reset_next_tbtt(priv, conf->beacon_int);
1094 return vnt_beacon_make(priv, vif);