b942e30cc1811726a26d06b927457f02a997aa6f
[safe/jmp/linux-2.6] / drivers / staging / rtl8192su / ieee80211 / ieee80211_tx.c
1 /******************************************************************************
2
3   Copyright(c) 2003 - 2004 Intel Corporation. All rights reserved.
4
5   This program is free software; you can redistribute it and/or modify it
6   under the terms of version 2 of the GNU General Public License as
7   published by the Free Software Foundation.
8
9   This program is distributed in the hope that it will be useful, but WITHOUT
10   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12   more details.
13
14   You should have received a copy of the GNU General Public License along with
15   this program; if not, write to the Free Software Foundation, Inc., 59
16   Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17
18   The full GNU General Public License is included in this distribution in the
19   file called LICENSE.
20
21   Contact Information:
22   James P. Ketrenos <ipw2100-admin@linux.intel.com>
23   Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24
25 ******************************************************************************
26
27   Few modifications for Realtek's Wi-Fi drivers by
28   Andrea Merello <andreamrl@tiscali.it>
29
30   A special thanks goes to Realtek for their support !
31
32 ******************************************************************************/
33
34 #include <linux/compiler.h>
35 //#include <linux/config.h>
36 #include <linux/errno.h>
37 #include <linux/if_arp.h>
38 #include <linux/in6.h>
39 #include <linux/in.h>
40 #include <linux/ip.h>
41 #include <linux/kernel.h>
42 #include <linux/module.h>
43 #include <linux/netdevice.h>
44 #include <linux/pci.h>
45 #include <linux/proc_fs.h>
46 #include <linux/skbuff.h>
47 #include <linux/slab.h>
48 #include <linux/tcp.h>
49 #include <linux/types.h>
50 #include <linux/version.h>
51 #include <linux/wireless.h>
52 #include <linux/etherdevice.h>
53 #include <asm/uaccess.h>
54 #include <linux/if_vlan.h>
55
56 #include "ieee80211.h"
57
58
59 /*
60
61
62 802.11 Data Frame
63
64
65 802.11 frame_contorl for data frames - 2 bytes
66      ,-----------------------------------------------------------------------------------------.
67 bits | 0  |  1  |  2  |  3  |  4  |  5  |  6  |  7  |  8  |  9  |  a  |  b  |  c  |  d  |  e   |
68      |----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|------|
69 val  | 0  |  0  |  0  |  1  |  x  |  0  |  0  |  0  |  1  |  0  |  x  |  x  |  x  |  x  |  x   |
70      |----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|------|
71 desc | ^-ver-^  |  ^type-^  |  ^-----subtype-----^  | to  |from |more |retry| pwr |more |wep   |
72      |          |           | x=0 data,x=1 data+ack | DS  | DS  |frag |     | mgm |data |      |
73      '-----------------------------------------------------------------------------------------'
74                                                     /\
75                                                     |
76 802.11 Data Frame                                   |
77            ,--------- 'ctrl' expands to >-----------'
78           |
79       ,--'---,-------------------------------------------------------------.
80 Bytes |  2   |  2   |    6    |    6    |    6    |  2   | 0..2312 |   4  |
81       |------|------|---------|---------|---------|------|---------|------|
82 Desc. | ctrl | dura |  DA/RA  |   TA    |    SA   | Sequ |  Frame  |  fcs |
83       |      | tion | (BSSID) |         |         | ence |  data   |      |
84       `--------------------------------------------------|         |------'
85 Total: 28 non-data bytes                                 `----.----'
86                                                               |
87        .- 'Frame data' expands to <---------------------------'
88        |
89        V
90       ,---------------------------------------------------.
91 Bytes |  1   |  1   |    1    |    3     |  2   |  0-2304 |
92       |------|------|---------|----------|------|---------|
93 Desc. | SNAP | SNAP | Control |Eth Tunnel| Type | IP      |
94       | DSAP | SSAP |         |          |      | Packet  |
95       | 0xAA | 0xAA |0x03 (UI)|0x00-00-F8|      |         |
96       `-----------------------------------------|         |
97 Total: 8 non-data bytes                         `----.----'
98                                                      |
99        .- 'IP Packet' expands, if WEP enabled, to <--'
100        |
101        V
102       ,-----------------------.
103 Bytes |  4  |   0-2296  |  4  |
104       |-----|-----------|-----|
105 Desc. | IV  | Encrypted | ICV |
106       |     | IP Packet |     |
107       `-----------------------'
108 Total: 8 non-data bytes
109
110
111 802.3 Ethernet Data Frame
112
113       ,-----------------------------------------.
114 Bytes |   6   |   6   |  2   |  Variable |   4  |
115       |-------|-------|------|-----------|------|
116 Desc. | Dest. | Source| Type | IP Packet |  fcs |
117       |  MAC  |  MAC  |      |           |      |
118       `-----------------------------------------'
119 Total: 18 non-data bytes
120
121 In the event that fragmentation is required, the incoming payload is split into
122 N parts of size ieee->fts.  The first fragment contains the SNAP header and the
123 remaining packets are just data.
124
125 If encryption is enabled, each fragment payload size is reduced by enough space
126 to add the prefix and postfix (IV and ICV totalling 8 bytes in the case of WEP)
127 So if you have 1500 bytes of payload with ieee->fts set to 500 without
128 encryption it will take 3 frames.  With WEP it will take 4 frames as the
129 payload of each frame is reduced to 492 bytes.
130
131 * SKB visualization
132 *
133 *  ,- skb->data
134 * |
135 * |    ETHERNET HEADER        ,-<-- PAYLOAD
136 * |                           |     14 bytes from skb->data
137 * |  2 bytes for Type --> ,T. |     (sizeof ethhdr)
138 * |                       | | |
139 * |,-Dest.--. ,--Src.---. | | |
140 * |  6 bytes| | 6 bytes | | | |
141 * v         | |         | | | |
142 * 0         | v       1 | v | v           2
143 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
144 *     ^     | ^         | ^ |
145 *     |     | |         | | |
146 *     |     | |         | `T' <---- 2 bytes for Type
147 *     |     | |         |
148 *     |     | '---SNAP--' <-------- 6 bytes for SNAP
149 *     |     |
150 *     `-IV--' <-------------------- 4 bytes for IV (WEP)
151 *
152 *      SNAP HEADER
153 *
154 */
155
156 static u8 P802_1H_OUI[P80211_OUI_LEN] = { 0x00, 0x00, 0xf8 };
157 static u8 RFC1042_OUI[P80211_OUI_LEN] = { 0x00, 0x00, 0x00 };
158
159 static inline int ieee80211_put_snap(u8 *data, u16 h_proto)
160 {
161         struct ieee80211_snap_hdr *snap;
162         u8 *oui;
163
164         snap = (struct ieee80211_snap_hdr *)data;
165         snap->dsap = 0xaa;
166         snap->ssap = 0xaa;
167         snap->ctrl = 0x03;
168
169         if (h_proto == 0x8137 || h_proto == 0x80f3)
170                 oui = P802_1H_OUI;
171         else
172                 oui = RFC1042_OUI;
173         snap->oui[0] = oui[0];
174         snap->oui[1] = oui[1];
175         snap->oui[2] = oui[2];
176
177         *(u16 *)(data + SNAP_SIZE) = htons(h_proto);
178
179         return SNAP_SIZE + sizeof(u16);
180 }
181
182 int ieee80211_encrypt_fragment(
183         struct ieee80211_device *ieee,
184         struct sk_buff *frag,
185         int hdr_len)
186 {
187         struct ieee80211_crypt_data* crypt = ieee->crypt[ieee->tx_keyidx];
188         int res;
189
190         if (!(crypt && crypt->ops))
191         {
192                 printk("=========>%s(), crypt is null\n", __FUNCTION__);
193                 return -1;
194         }
195 #ifdef CONFIG_IEEE80211_CRYPT_TKIP
196         struct rtl_ieee80211_hdr *header;
197
198         if (ieee->tkip_countermeasures &&
199             crypt && crypt->ops && strcmp(crypt->ops->name, "TKIP") == 0) {
200                 header = (struct rtl_ieee80211_hdr *)frag->data;
201                 if (net_ratelimit()) {
202                         printk(KERN_DEBUG "%s: TKIP countermeasures: dropped "
203                                "TX packet to " MAC_FMT "\n",
204                                ieee->dev->name, MAC_ARG(header->addr1));
205                 }
206                 return -1;
207         }
208 #endif
209         /* To encrypt, frame format is:
210          * IV (4 bytes), clear payload (including SNAP), ICV (4 bytes) */
211
212         // PR: FIXME: Copied from hostap. Check fragmentation/MSDU/MPDU encryption.
213         /* Host-based IEEE 802.11 fragmentation for TX is not yet supported, so
214          * call both MSDU and MPDU encryption functions from here. */
215         atomic_inc(&crypt->refcnt);
216         res = 0;
217         if (crypt->ops->encrypt_msdu)
218                 res = crypt->ops->encrypt_msdu(frag, hdr_len, crypt->priv);
219         if (res == 0 && crypt->ops->encrypt_mpdu)
220                 res = crypt->ops->encrypt_mpdu(frag, hdr_len, crypt->priv);
221
222         atomic_dec(&crypt->refcnt);
223         if (res < 0) {
224                 printk(KERN_INFO "%s: Encryption failed: len=%d.\n",
225                        ieee->dev->name, frag->len);
226                 ieee->ieee_stats.tx_discards++;
227                 return -1;
228         }
229
230         return 0;
231 }
232
233
234 void ieee80211_txb_free(struct ieee80211_txb *txb) {
235         //int i;
236         if (unlikely(!txb))
237                 return;
238         kfree(txb);
239 }
240
241 struct ieee80211_txb *ieee80211_alloc_txb(int nr_frags, int txb_size,
242                                           int gfp_mask)
243 {
244         struct ieee80211_txb *txb;
245         int i;
246         txb = kmalloc(
247                 sizeof(struct ieee80211_txb) + (sizeof(u8*) * nr_frags),
248                 gfp_mask);
249         if (!txb)
250                 return NULL;
251
252         memset(txb, 0, sizeof(struct ieee80211_txb));
253         txb->nr_frags = nr_frags;
254         txb->frag_size = txb_size;
255
256         for (i = 0; i < nr_frags; i++) {
257                 txb->fragments[i] = dev_alloc_skb(txb_size);
258                 if (unlikely(!txb->fragments[i])) {
259                         i--;
260                         break;
261                 }
262                 memset(txb->fragments[i]->cb, 0, sizeof(txb->fragments[i]->cb));
263         }
264         if (unlikely(i != nr_frags)) {
265                 while (i >= 0)
266                         dev_kfree_skb_any(txb->fragments[i--]);
267                 kfree(txb);
268                 return NULL;
269         }
270         return txb;
271 }
272
273 // Classify the to-be send data packet
274 // Need to acquire the sent queue index.
275 static int
276 ieee80211_classify(struct sk_buff *skb, struct ieee80211_network *network)
277 {
278         struct ethhdr *eth;
279         struct iphdr *ip;
280         eth = (struct ethhdr *)skb->data;
281         if (eth->h_proto != htons(ETH_P_IP))
282                 return 0;
283
284 //      IEEE80211_DEBUG_DATA(IEEE80211_DL_DATA, skb->data, skb->len);
285         ip = ip_hdr(skb);
286
287         switch (ip->tos & 0xfc) {
288                 case 0x20:
289                         return 2;
290                 case 0x40:
291                         return 1;
292                 case 0x60:
293                         return 3;
294                 case 0x80:
295                         return 4;
296                 case 0xa0:
297                         return 5;
298                 case 0xc0:
299                         return 6;
300                 case 0xe0:
301                         return 7;
302                 default:
303                         return 0;
304         }
305 }
306
307 #define SN_LESS(a, b)           (((a-b)&0x800)!=0)
308 void ieee80211_tx_query_agg_cap(struct ieee80211_device* ieee, struct sk_buff* skb, cb_desc* tcb_desc)
309 {
310         PRT_HIGH_THROUGHPUT     pHTInfo = ieee->pHTInfo;
311         PTX_TS_RECORD                   pTxTs = NULL;
312         struct ieee80211_hdr_1addr* hdr = (struct ieee80211_hdr_1addr*)skb->data;
313
314         if (!pHTInfo->bCurrentHTSupport||!pHTInfo->bEnableHT)
315                 return;
316         if (!IsQoSDataFrame(skb->data))
317                 return;
318
319         if (is_multicast_ether_addr(hdr->addr1) || is_broadcast_ether_addr(hdr->addr1))
320                 return;
321         //check packet and mode later
322 #ifdef TO_DO_LIST
323         if(pTcb->PacketLength >= 4096)
324                 return;
325         // For RTL819X, if pairwisekey = wep/tkip, we don't aggrregation.
326         if(!Adapter->HalFunc.GetNmodeSupportBySecCfgHandler(Adapter))
327                 return;
328 #endif
329
330         if(pHTInfo->IOTAction & HT_IOT_ACT_TX_NO_AGGREGATION)
331                 return;
332
333 #if 1
334         if(!ieee->GetNmodeSupportBySecCfg(ieee->dev))
335         {
336                 return;
337         }
338 #endif
339         if(pHTInfo->bCurrentAMPDUEnable)
340         {
341                 if (!GetTs(ieee, (PTS_COMMON_INFO*)(&pTxTs), hdr->addr1, skb->priority, TX_DIR, true))
342                 {
343                         printk("===>can't get TS\n");
344                         return;
345                 }
346                 if (pTxTs->TxAdmittedBARecord.bValid == false)
347                 {
348                         //as some AP will refuse our action frame until key handshake has been finished. WB
349                         if (ieee->wpa_ie_len && (ieee->pairwise_key_type == KEY_TYPE_NA))
350                         ;
351                         else
352                         TsStartAddBaProcess(ieee, pTxTs);
353                         goto FORCED_AGG_SETTING;
354                 }
355                 else if (pTxTs->bUsingBa == false)
356                 {
357                         if (SN_LESS(pTxTs->TxAdmittedBARecord.BaStartSeqCtrl.field.SeqNum, (pTxTs->TxCurSeq+1)%4096))
358                                 pTxTs->bUsingBa = true;
359                         else
360                                 goto FORCED_AGG_SETTING;
361                 }
362
363                 if (ieee->iw_mode == IW_MODE_INFRA)
364                 {
365                         tcb_desc->bAMPDUEnable = true;
366                         tcb_desc->ampdu_factor = pHTInfo->CurrentAMPDUFactor;
367                         tcb_desc->ampdu_density = pHTInfo->CurrentMPDUDensity;
368                 }
369         }
370 FORCED_AGG_SETTING:
371         switch(pHTInfo->ForcedAMPDUMode )
372         {
373                 case HT_AGG_AUTO:
374                         break;
375
376                 case HT_AGG_FORCE_ENABLE:
377                         tcb_desc->bAMPDUEnable = true;
378                         tcb_desc->ampdu_density = pHTInfo->ForcedMPDUDensity;
379                         tcb_desc->ampdu_factor = pHTInfo->ForcedAMPDUFactor;
380                         break;
381
382                 case HT_AGG_FORCE_DISABLE:
383                         tcb_desc->bAMPDUEnable = false;
384                         tcb_desc->ampdu_density = 0;
385                         tcb_desc->ampdu_factor = 0;
386                         break;
387
388         }
389                 return;
390 }
391
392 extern void ieee80211_qurey_ShortPreambleMode(struct ieee80211_device* ieee, cb_desc* tcb_desc)
393 {
394         tcb_desc->bUseShortPreamble = false;
395         if (tcb_desc->data_rate == 2)
396         {//// 1M can only use Long Preamble. 11B spec
397                 return;
398         }
399         else if (ieee->current_network.capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
400         {
401                 tcb_desc->bUseShortPreamble = true;
402         }
403         return;
404 }
405 extern  void
406 ieee80211_query_HTCapShortGI(struct ieee80211_device *ieee, cb_desc *tcb_desc)
407 {
408         PRT_HIGH_THROUGHPUT             pHTInfo = ieee->pHTInfo;
409
410         tcb_desc->bUseShortGI           = false;
411
412         if(!pHTInfo->bCurrentHTSupport||!pHTInfo->bEnableHT)
413                 return;
414
415         if(pHTInfo->bForcedShortGI)
416         {
417                 tcb_desc->bUseShortGI = true;
418                 return;
419         }
420
421         if((pHTInfo->bCurBW40MHz==true) && pHTInfo->bCurShortGI40MHz)
422                 tcb_desc->bUseShortGI = true;
423         else if((pHTInfo->bCurBW40MHz==false) && pHTInfo->bCurShortGI20MHz)
424                 tcb_desc->bUseShortGI = true;
425 }
426
427 void ieee80211_query_BandwidthMode(struct ieee80211_device* ieee, cb_desc *tcb_desc)
428 {
429         PRT_HIGH_THROUGHPUT     pHTInfo = ieee->pHTInfo;
430
431         tcb_desc->bPacketBW = false;
432
433         if(!pHTInfo->bCurrentHTSupport||!pHTInfo->bEnableHT)
434                 return;
435
436         if(tcb_desc->bMulticast || tcb_desc->bBroadcast)
437                 return;
438
439         if((tcb_desc->data_rate & 0x80)==0) // If using legacy rate, it shall use 20MHz channel.
440                 return;
441         //BandWidthAutoSwitch is for auto switch to 20 or 40 in long distance
442         if(pHTInfo->bCurBW40MHz && pHTInfo->bCurTxBW40MHz && !ieee->bandwidth_auto_switch.bforced_tx20Mhz)
443                 tcb_desc->bPacketBW = true;
444         return;
445 }
446
447 void ieee80211_query_protectionmode(struct ieee80211_device* ieee, cb_desc* tcb_desc, struct sk_buff* skb)
448 {
449         // Common Settings
450         tcb_desc->bRTSSTBC                      = false;
451         tcb_desc->bRTSUseShortGI                = false; // Since protection frames are always sent by legacy rate, ShortGI will never be used.
452         tcb_desc->bCTSEnable                    = false; // Most of protection using RTS/CTS
453         tcb_desc->RTSSC                         = 0;            // 20MHz: Don't care;  40MHz: Duplicate.
454         tcb_desc->bRTSBW                        = false; // RTS frame bandwidth is always 20MHz
455
456         if(tcb_desc->bBroadcast || tcb_desc->bMulticast)//only unicast frame will use rts/cts
457                 return;
458
459         if (is_broadcast_ether_addr(skb->data+16))  //check addr3 as infrastructure add3 is DA.
460                 return;
461
462         if (ieee->mode < IEEE_N_24G) //b, g mode
463         {
464                         // (1) RTS_Threshold is compared to the MPDU, not MSDU.
465                         // (2) If there are more than one frag in  this MSDU, only the first frag uses protection frame.
466                         //              Other fragments are protected by previous fragment.
467                         //              So we only need to check the length of first fragment.
468                 if (skb->len > ieee->rts)
469                 {
470                         tcb_desc->bRTSEnable = true;
471                         tcb_desc->rts_rate = MGN_24M;
472                 }
473                 else if (ieee->current_network.buseprotection)
474                 {
475                         // Use CTS-to-SELF in protection mode.
476                         tcb_desc->bRTSEnable = true;
477                         tcb_desc->bCTSEnable = true;
478                         tcb_desc->rts_rate = MGN_24M;
479                 }
480                 //otherwise return;
481                 return;
482         }
483         else
484         {// 11n High throughput case.
485                 PRT_HIGH_THROUGHPUT pHTInfo = ieee->pHTInfo;
486                 while (true)
487                 {
488                         //check IOT action
489                         if(pHTInfo->IOTAction & HT_IOT_ACT_FORCED_CTS2SELF)
490                         {
491                                 tcb_desc->bCTSEnable    = true;
492                                 tcb_desc->rts_rate  =   MGN_24M;
493                                 tcb_desc->bRTSEnable = false;
494                                 break;
495                         }
496                         else if(pHTInfo->IOTAction & (HT_IOT_ACT_FORCED_RTS|HT_IOT_ACT_PURE_N_MODE))
497                         {
498                                 tcb_desc->bRTSEnable = true;
499                                 tcb_desc->rts_rate  =   MGN_24M;
500                                 break;
501                         }
502                         //check ERP protection
503                         if (ieee->current_network.buseprotection)
504                         {// CTS-to-SELF
505                                 tcb_desc->bRTSEnable = true;
506                                 tcb_desc->bCTSEnable = true;
507                                 tcb_desc->rts_rate = MGN_24M;
508                                 break;
509                         }
510                         //check HT op mode
511                         if(pHTInfo->bCurrentHTSupport  && pHTInfo->bEnableHT)
512                         {
513                                 u8 HTOpMode = pHTInfo->CurrentOpMode;
514                                 if((pHTInfo->bCurBW40MHz && (HTOpMode == 2 || HTOpMode == 3)) ||
515                                                         (!pHTInfo->bCurBW40MHz && HTOpMode == 3) )
516                                 {
517                                         tcb_desc->rts_rate = MGN_24M; // Rate is 24Mbps.
518                                         tcb_desc->bRTSEnable = true;
519                                         break;
520                                 }
521                         }
522                         //check rts
523                         if (skb->len > ieee->rts)
524                         {
525                                 tcb_desc->rts_rate = MGN_24M; // Rate is 24Mbps.
526                                 tcb_desc->bRTSEnable = true;
527                                 break;
528                         }
529                         //to do list: check MIMO power save condition.
530                         //check AMPDU aggregation for TXOP
531                         if(tcb_desc->bAMPDUEnable)
532                         {
533                                 tcb_desc->rts_rate = MGN_24M; // Rate is 24Mbps.
534                                 // According to 8190 design, firmware sends CF-End only if RTS/CTS is enabled. However, it degrads
535                                 // throughput around 10M, so we disable of this mechanism. 2007.08.03 by Emily
536                                 tcb_desc->bRTSEnable = false;
537                                 break;
538                         }
539                         // Totally no protection case!!
540                         goto NO_PROTECTION;
541                 }
542                 }
543         // For test , CTS replace with RTS
544         if( 0 )
545         {
546                 tcb_desc->bCTSEnable    = true;
547                 tcb_desc->rts_rate = MGN_24M;
548                 tcb_desc->bRTSEnable    = true;
549         }
550         if (ieee->current_network.capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
551                 tcb_desc->bUseShortPreamble = true;
552         if (ieee->mode == IW_MODE_MASTER)
553                         goto NO_PROTECTION;
554         return;
555 NO_PROTECTION:
556         tcb_desc->bRTSEnable    = false;
557         tcb_desc->bCTSEnable    = false;
558         tcb_desc->rts_rate              = 0;
559         tcb_desc->RTSSC         = 0;
560         tcb_desc->bRTSBW                = false;
561 }
562
563
564 void ieee80211_txrate_selectmode(struct ieee80211_device* ieee, cb_desc* tcb_desc)
565 {
566 #ifdef TO_DO_LIST
567         if(!IsDataFrame(pFrame))
568         {
569                 pTcb->bTxDisableRateFallBack = TRUE;
570                 pTcb->bTxUseDriverAssingedRate = TRUE;
571                 pTcb->RATRIndex = 7;
572                 return;
573         }
574
575         if(pMgntInfo->ForcedDataRate!= 0)
576         {
577                 pTcb->bTxDisableRateFallBack = TRUE;
578                 pTcb->bTxUseDriverAssingedRate = TRUE;
579                 return;
580         }
581 #endif
582         if(ieee->bTxDisableRateFallBack)
583                 tcb_desc->bTxDisableRateFallBack = true;
584
585         if(ieee->bTxUseDriverAssingedRate)
586                 tcb_desc->bTxUseDriverAssingedRate = true;
587         if(!tcb_desc->bTxDisableRateFallBack || !tcb_desc->bTxUseDriverAssingedRate)
588         {
589                 if (ieee->iw_mode == IW_MODE_INFRA || ieee->iw_mode == IW_MODE_ADHOC)
590                         tcb_desc->RATRIndex = 0;
591         }
592 }
593
594 void ieee80211_query_seqnum(struct ieee80211_device*ieee, struct sk_buff* skb, u8* dst)
595 {
596         if (is_multicast_ether_addr(dst) || is_broadcast_ether_addr(dst))
597                 return;
598         if (IsQoSDataFrame(skb->data)) //we deal qos data only
599         {
600                 PTX_TS_RECORD pTS = NULL;
601                 if (!GetTs(ieee, (PTS_COMMON_INFO*)(&pTS), dst, skb->priority, TX_DIR, true))
602                 {
603                         return;
604                 }
605                 pTS->TxCurSeq = (pTS->TxCurSeq+1)%4096;
606         }
607 }
608
609 int rtl8192_ieee80211_xmit(struct sk_buff *skb, struct net_device *dev)
610 {
611         struct ieee80211_device *ieee = netdev_priv(dev);
612         struct ieee80211_txb *txb = NULL;
613         struct ieee80211_hdr_3addrqos *frag_hdr;
614         int i, bytes_per_frag, nr_frags, bytes_last_frag, frag_size;
615         unsigned long flags;
616         struct net_device_stats *stats = &ieee->stats;
617         int ether_type = 0, encrypt;
618         int bytes, fc, qos_ctl = 0, hdr_len;
619         struct sk_buff *skb_frag;
620         struct ieee80211_hdr_3addrqos header = { /* Ensure zero initialized */
621                 .duration_id = 0,
622                 .seq_ctl = 0,
623                 .qos_ctl = 0
624         };
625         u8 dest[ETH_ALEN], src[ETH_ALEN];
626         int qos_actived = ieee->current_network.qos_data.active;
627
628         struct ieee80211_crypt_data* crypt;
629
630         cb_desc *tcb_desc;
631
632         spin_lock_irqsave(&ieee->lock, flags);
633
634         /* If there is no driver handler to take the TXB, dont' bother
635          * creating it... */
636         if ((!ieee->hard_start_xmit && !(ieee->softmac_features & IEEE_SOFTMAC_TX_QUEUE))||
637            ((!ieee->softmac_data_hard_start_xmit && (ieee->softmac_features & IEEE_SOFTMAC_TX_QUEUE)))) {
638                 printk(KERN_WARNING "%s: No xmit handler.\n",
639                        ieee->dev->name);
640                 goto success;
641         }
642
643
644         if(likely(ieee->raw_tx == 0)){
645                 if (unlikely(skb->len < SNAP_SIZE + sizeof(u16))) {
646                         printk(KERN_WARNING "%s: skb too small (%d).\n",
647                         ieee->dev->name, skb->len);
648                         goto success;
649                 }
650
651                 memset(skb->cb, 0, sizeof(skb->cb));
652                 ether_type = ntohs(((struct ethhdr *)skb->data)->h_proto);
653
654                 crypt = ieee->crypt[ieee->tx_keyidx];
655
656                 encrypt = !(ether_type == ETH_P_PAE && ieee->ieee802_1x) &&
657                         ieee->host_encrypt && crypt && crypt->ops;
658
659                 if (!encrypt && ieee->ieee802_1x &&
660                 ieee->drop_unencrypted && ether_type != ETH_P_PAE) {
661                         stats->tx_dropped++;
662                         goto success;
663                 }
664         #ifdef CONFIG_IEEE80211_DEBUG
665                 if (crypt && !encrypt && ether_type == ETH_P_PAE) {
666                         struct eapol *eap = (struct eapol *)(skb->data +
667                                 sizeof(struct ethhdr) - SNAP_SIZE - sizeof(u16));
668                         IEEE80211_DEBUG_EAP("TX: IEEE 802.11 EAPOL frame: %s\n",
669                                 eap_get_type(eap->type));
670                 }
671         #endif
672
673                 /* Save source and destination addresses */
674                 memcpy(&dest, skb->data, ETH_ALEN);
675                 memcpy(&src, skb->data+ETH_ALEN, ETH_ALEN);
676
677                 /* Advance the SKB to the start of the payload */
678                 skb_pull(skb, sizeof(struct ethhdr));
679
680                 /* Determine total amount of storage required for TXB packets */
681                 bytes = skb->len + SNAP_SIZE + sizeof(u16);
682
683                 if (encrypt)
684                         fc = IEEE80211_FTYPE_DATA | IEEE80211_FCTL_WEP;
685                 else
686
687                         fc = IEEE80211_FTYPE_DATA;
688
689                 //if(ieee->current_network.QoS_Enable)
690                 if(qos_actived)
691                         fc |= IEEE80211_STYPE_QOS_DATA;
692                 else
693                         fc |= IEEE80211_STYPE_DATA;
694
695                 if (ieee->iw_mode == IW_MODE_INFRA) {
696                         fc |= IEEE80211_FCTL_TODS;
697                         /* To DS: Addr1 = BSSID, Addr2 = SA,
698                         Addr3 = DA */
699                         memcpy(&header.addr1, ieee->current_network.bssid, ETH_ALEN);
700                         memcpy(&header.addr2, &src, ETH_ALEN);
701                         memcpy(&header.addr3, &dest, ETH_ALEN);
702                 } else if (ieee->iw_mode == IW_MODE_ADHOC) {
703                         /* not From/To DS: Addr1 = DA, Addr2 = SA,
704                         Addr3 = BSSID */
705                         memcpy(&header.addr1, dest, ETH_ALEN);
706                         memcpy(&header.addr2, src, ETH_ALEN);
707                         memcpy(&header.addr3, ieee->current_network.bssid, ETH_ALEN);
708                 }
709
710                 header.frame_ctl = cpu_to_le16(fc);
711
712                 /* Determine fragmentation size based on destination (multicast
713                 * and broadcast are not fragmented) */
714                 if (is_multicast_ether_addr(header.addr1) ||
715                 is_broadcast_ether_addr(header.addr1)) {
716                         frag_size = MAX_FRAG_THRESHOLD;
717                         qos_ctl |= QOS_CTL_NOTCONTAIN_ACK;
718                 }
719                 else {
720                         frag_size = ieee->fts;//default:392
721                         qos_ctl = 0;
722                 }
723
724                 //if (ieee->current_network.QoS_Enable)
725                 if(qos_actived)
726                 {
727                         hdr_len = IEEE80211_3ADDR_LEN + 2;
728
729                         skb->priority = ieee80211_classify(skb, &ieee->current_network);
730                         qos_ctl |= skb->priority; //set in the ieee80211_classify
731                         header.qos_ctl = cpu_to_le16(qos_ctl & IEEE80211_QOS_TID);
732                 } else {
733                         hdr_len = IEEE80211_3ADDR_LEN;
734                 }
735                 /* Determine amount of payload per fragment.  Regardless of if
736                 * this stack is providing the full 802.11 header, one will
737                 * eventually be affixed to this fragment -- so we must account for
738                 * it when determining the amount of payload space. */
739                 bytes_per_frag = frag_size - hdr_len;
740                 if (ieee->config &
741                 (CFG_IEEE80211_COMPUTE_FCS | CFG_IEEE80211_RESERVE_FCS))
742                         bytes_per_frag -= IEEE80211_FCS_LEN;
743
744                 /* Each fragment may need to have room for encryptiong pre/postfix */
745                 if (encrypt)
746                         bytes_per_frag -= crypt->ops->extra_prefix_len +
747                                 crypt->ops->extra_postfix_len;
748
749                 /* Number of fragments is the total bytes_per_frag /
750                 * payload_per_fragment */
751                 nr_frags = bytes / bytes_per_frag;
752                 bytes_last_frag = bytes % bytes_per_frag;
753                 if (bytes_last_frag)
754                         nr_frags++;
755                 else
756                         bytes_last_frag = bytes_per_frag;
757
758                 /* When we allocate the TXB we allocate enough space for the reserve
759                 * and full fragment bytes (bytes_per_frag doesn't include prefix,
760                 * postfix, header, FCS, etc.) */
761                 txb = ieee80211_alloc_txb(nr_frags, frag_size + ieee->tx_headroom, GFP_ATOMIC);
762                 if (unlikely(!txb)) {
763                         printk(KERN_WARNING "%s: Could not allocate TXB\n",
764                         ieee->dev->name);
765                         goto failed;
766                 }
767                 txb->encrypted = encrypt;
768                 txb->payload_size = bytes;
769
770                 //if (ieee->current_network.QoS_Enable)
771                 if(qos_actived)
772                 {
773                         txb->queue_index = UP2AC(skb->priority);
774                 } else {
775                         txb->queue_index = WME_AC_BK;;
776                 }
777
778
779
780                 for (i = 0; i < nr_frags; i++) {
781                         skb_frag = txb->fragments[i];
782                         tcb_desc = (cb_desc *)(skb_frag->cb + MAX_DEV_ADDR_SIZE);
783                         if(qos_actived){
784                                 skb_frag->priority = skb->priority;//UP2AC(skb->priority);
785                                 tcb_desc->queue_index =  UP2AC(skb->priority);
786                         } else {
787                                 skb_frag->priority = WME_AC_BK;
788                                 tcb_desc->queue_index = WME_AC_BK;
789                         }
790                         skb_reserve(skb_frag, ieee->tx_headroom);
791
792                         if (encrypt){
793                                 if (ieee->hwsec_active)
794                                         tcb_desc->bHwSec = 1;
795                                 else
796                                         tcb_desc->bHwSec = 0;
797                                 skb_reserve(skb_frag, crypt->ops->extra_prefix_len);
798                         }
799                         else
800                         {
801                                 tcb_desc->bHwSec = 0;
802                         }
803                         frag_hdr = (struct ieee80211_hdr_3addrqos *)skb_put(skb_frag, hdr_len);
804                         memcpy(frag_hdr, &header, hdr_len);
805
806                         /* If this is not the last fragment, then add the MOREFRAGS
807                         * bit to the frame control */
808                         if (i != nr_frags - 1) {
809                                 frag_hdr->frame_ctl = cpu_to_le16(
810                                         fc | IEEE80211_FCTL_MOREFRAGS);
811                                 bytes = bytes_per_frag;
812
813                         } else {
814                                 /* The last fragment takes the remaining length */
815                                 bytes = bytes_last_frag;
816                         }
817                         //if(ieee->current_network.QoS_Enable)
818                         if(qos_actived)
819                         {
820                                 // add 1 only indicate to corresponding seq number control 2006/7/12
821                                 frag_hdr->seq_ctl = cpu_to_le16(ieee->seq_ctrl[UP2AC(skb->priority)+1]<<4 | i);
822                         } else {
823                                 frag_hdr->seq_ctl = cpu_to_le16(ieee->seq_ctrl[0]<<4 | i);
824                         }
825
826                         /* Put a SNAP header on the first fragment */
827                         if (i == 0) {
828                                 ieee80211_put_snap(
829                                         skb_put(skb_frag, SNAP_SIZE + sizeof(u16)),
830                                         ether_type);
831                                 bytes -= SNAP_SIZE + sizeof(u16);
832                         }
833
834                         memcpy(skb_put(skb_frag, bytes), skb->data, bytes);
835
836                         /* Advance the SKB... */
837                         skb_pull(skb, bytes);
838
839                         /* Encryption routine will move the header forward in order
840                         * to insert the IV between the header and the payload */
841                         if (encrypt)
842                                 ieee80211_encrypt_fragment(ieee, skb_frag, hdr_len);
843                         if (ieee->config &
844                         (CFG_IEEE80211_COMPUTE_FCS | CFG_IEEE80211_RESERVE_FCS))
845                                 skb_put(skb_frag, 4);
846                 }
847
848                 if(qos_actived)
849                 {
850                   if (ieee->seq_ctrl[UP2AC(skb->priority) + 1] == 0xFFF)
851                         ieee->seq_ctrl[UP2AC(skb->priority) + 1] = 0;
852                   else
853                         ieee->seq_ctrl[UP2AC(skb->priority) + 1]++;
854                 } else {
855                   if (ieee->seq_ctrl[0] == 0xFFF)
856                         ieee->seq_ctrl[0] = 0;
857                   else
858                         ieee->seq_ctrl[0]++;
859                 }
860         }else{
861                 if (unlikely(skb->len < sizeof(struct ieee80211_hdr_3addr))) {
862                         printk(KERN_WARNING "%s: skb too small (%d).\n",
863                         ieee->dev->name, skb->len);
864                         goto success;
865                 }
866
867                 txb = ieee80211_alloc_txb(1, skb->len, GFP_ATOMIC);
868                 if(!txb){
869                         printk(KERN_WARNING "%s: Could not allocate TXB\n",
870                         ieee->dev->name);
871                         goto failed;
872                 }
873
874                 txb->encrypted = 0;
875                 txb->payload_size = skb->len;
876                 memcpy(skb_put(txb->fragments[0],skb->len), skb->data, skb->len);
877         }
878
879  success:
880 //WB add to fill data tcb_desc here. only first fragment is considered, need to change, and you may remove to other place.
881         if (txb)
882         {
883 #if 1
884                 cb_desc *tcb_desc = (cb_desc *)(txb->fragments[0]->cb + MAX_DEV_ADDR_SIZE);
885                 tcb_desc->bTxEnableFwCalcDur = 1;
886                 if (is_multicast_ether_addr(header.addr1))
887                         tcb_desc->bMulticast = 1;
888                 if (is_broadcast_ether_addr(header.addr1))
889                         tcb_desc->bBroadcast = 1;
890                 ieee80211_txrate_selectmode(ieee, tcb_desc);
891                 if ( tcb_desc->bMulticast ||  tcb_desc->bBroadcast)
892                         tcb_desc->data_rate = ieee->basic_rate;
893                 else
894                         //tcb_desc->data_rate = CURRENT_RATE(ieee->current_network.mode, ieee->rate, ieee->HTCurrentOperaRate);
895                         tcb_desc->data_rate = CURRENT_RATE(ieee->mode, ieee->rate, ieee->HTCurrentOperaRate);
896                 ieee80211_qurey_ShortPreambleMode(ieee, tcb_desc);
897                 ieee80211_tx_query_agg_cap(ieee, txb->fragments[0], tcb_desc);
898                 ieee80211_query_HTCapShortGI(ieee, tcb_desc);
899                 ieee80211_query_BandwidthMode(ieee, tcb_desc);
900                 ieee80211_query_protectionmode(ieee, tcb_desc, txb->fragments[0]);
901                 ieee80211_query_seqnum(ieee, txb->fragments[0], header.addr1);
902 //              IEEE80211_DEBUG_DATA(IEEE80211_DL_DATA, txb->fragments[0]->data, txb->fragments[0]->len);
903                 //IEEE80211_DEBUG_DATA(IEEE80211_DL_DATA, tcb_desc, sizeof(cb_desc));
904 #endif
905         }
906         spin_unlock_irqrestore(&ieee->lock, flags);
907         dev_kfree_skb_any(skb);
908         if (txb) {
909                 if (ieee->softmac_features & IEEE_SOFTMAC_TX_QUEUE){
910                         ieee80211_softmac_xmit(txb, ieee);
911                 }else{
912                         if ((*ieee->hard_start_xmit)(txb, dev) == 0) {
913                                 stats->tx_packets++;
914                                 stats->tx_bytes += txb->payload_size;
915                                 return 0;
916                         }
917                         ieee80211_txb_free(txb);
918                 }
919         }
920
921         return 0;
922
923  failed:
924         spin_unlock_irqrestore(&ieee->lock, flags);
925         netif_stop_queue(dev);
926         stats->tx_errors++;
927         return 1;
928
929 }