Staging: merge staging patches into Linus's main branch
[sfrench/cifs-2.6.git] / drivers / staging / rtl8192e / 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 ieee80211_hdr *header;
197
198         if (ieee->tkip_countermeasures &&
199             crypt && crypt->ops && strcmp(crypt->ops->name, "TKIP") == 0) {
200                 header = (struct ieee80211_hdr *) frag->data;
201                 if (net_ratelimit()) {
202                         printk(KERN_DEBUG "%s: TKIP countermeasures: dropped "
203                                "TX packet to %pM\n",
204                                ieee->dev->name, 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 #if 0
239         for (i = 0; i < txb->nr_frags; i++)
240                 if (txb->fragments[i])
241                         dev_kfree_skb_any(txb->fragments[i]);
242 #endif
243         kfree(txb);
244 }
245
246 struct ieee80211_txb *ieee80211_alloc_txb(int nr_frags, int txb_size,
247                                           int gfp_mask)
248 {
249         struct ieee80211_txb *txb;
250         int i;
251         txb = kmalloc(
252                 sizeof(struct ieee80211_txb) + (sizeof(u8*) * nr_frags),
253                 gfp_mask);
254         if (!txb)
255                 return NULL;
256
257         memset(txb, 0, sizeof(struct ieee80211_txb));
258         txb->nr_frags = nr_frags;
259         txb->frag_size = txb_size;
260
261         for (i = 0; i < nr_frags; i++) {
262                 txb->fragments[i] = dev_alloc_skb(txb_size);
263                 if (unlikely(!txb->fragments[i])) {
264                         i--;
265                         break;
266                 }
267                 memset(txb->fragments[i]->cb, 0, sizeof(txb->fragments[i]->cb));
268         }
269         if (unlikely(i != nr_frags)) {
270                 while (i >= 0)
271                         dev_kfree_skb_any(txb->fragments[i--]);
272                 kfree(txb);
273                 return NULL;
274         }
275         return txb;
276 }
277
278 // Classify the to-be send data packet
279 // Need to acquire the sent queue index.
280 static int
281 ieee80211_classify(struct sk_buff *skb, struct ieee80211_network *network)
282 {
283         struct ethhdr *eth;
284         struct iphdr *ip;
285         eth = (struct ethhdr *)skb->data;
286         if (eth->h_proto != htons(ETH_P_IP))
287                 return 0;
288
289 //      IEEE80211_DEBUG_DATA(IEEE80211_DL_DATA, skb->data, skb->len);
290 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22))
291         ip = ip_hdr(skb);
292 #else
293         ip = (struct iphdr*)(skb->data + sizeof(struct ether_header));
294 #endif
295         switch (ip->tos & 0xfc) {
296                 case 0x20:
297                         return 2;
298                 case 0x40:
299                         return 1;
300                 case 0x60:
301                         return 3;
302                 case 0x80:
303                         return 4;
304                 case 0xa0:
305                         return 5;
306                 case 0xc0:
307                         return 6;
308                 case 0xe0:
309                         return 7;
310                 default:
311                         return 0;
312         }
313 }
314
315 #define SN_LESS(a, b)           (((a-b)&0x800)!=0)
316 void ieee80211_tx_query_agg_cap(struct ieee80211_device* ieee, struct sk_buff* skb, cb_desc* tcb_desc)
317 {
318         PRT_HIGH_THROUGHPUT     pHTInfo = ieee->pHTInfo;
319         PTX_TS_RECORD                   pTxTs = NULL;
320         struct ieee80211_hdr_1addr* hdr = (struct ieee80211_hdr_1addr*)skb->data;
321
322         if (!pHTInfo->bCurrentHTSupport||!pHTInfo->bEnableHT)
323                 return;
324         if (!IsQoSDataFrame(skb->data))
325                 return;
326
327         if (is_multicast_ether_addr(hdr->addr1) || is_broadcast_ether_addr(hdr->addr1))
328                 return;
329         //check packet and mode later
330 #ifdef TO_DO_LIST
331         if(pTcb->PacketLength >= 4096)
332                 return;
333         // For RTL819X, if pairwisekey = wep/tkip, we don't aggrregation.
334         if(!Adapter->HalFunc.GetNmodeSupportBySecCfgHandler(Adapter))
335                 return;
336 #endif
337
338         if(tcb_desc->bdhcp)// || ieee->CntAfterLink<2)
339         {
340                 return;
341         }
342
343
344 #if 1
345         if(!ieee->GetNmodeSupportBySecCfg(ieee->dev))
346         {
347                 return;
348         }
349 #endif
350         if(pHTInfo->bCurrentAMPDUEnable)
351         {
352                 if (!GetTs(ieee, (PTS_COMMON_INFO*)(&pTxTs), hdr->addr1, skb->priority, TX_DIR, true))
353                 {
354                         printk("===>can't get TS\n");
355                         return;
356                 }
357                 if (pTxTs->TxAdmittedBARecord.bValid == false)
358                 {
359                         //as some AP will refuse our action frame until key handshake has been finished. WB
360                         if (ieee->wpa_ie_len && (ieee->pairwise_key_type == KEY_TYPE_NA))
361                         ;
362                         else
363                         TsStartAddBaProcess(ieee, pTxTs);
364                         goto FORCED_AGG_SETTING;
365                 }
366                 else if (pTxTs->bUsingBa == false)
367                 {
368                         if (SN_LESS(pTxTs->TxAdmittedBARecord.BaStartSeqCtrl.field.SeqNum, (pTxTs->TxCurSeq+1)%4096))
369                                 pTxTs->bUsingBa = true;
370                         else
371                                 goto FORCED_AGG_SETTING;
372                 }
373
374                 if (ieee->iw_mode == IW_MODE_INFRA)
375                 {
376                         tcb_desc->bAMPDUEnable = true;
377                         tcb_desc->ampdu_factor = pHTInfo->CurrentAMPDUFactor;
378                         tcb_desc->ampdu_density = pHTInfo->CurrentMPDUDensity;
379                 }
380         }
381 FORCED_AGG_SETTING:
382         switch(pHTInfo->ForcedAMPDUMode )
383         {
384                 case HT_AGG_AUTO:
385                         break;
386
387                 case HT_AGG_FORCE_ENABLE:
388                         tcb_desc->bAMPDUEnable = true;
389                         tcb_desc->ampdu_density = pHTInfo->ForcedMPDUDensity;
390                         tcb_desc->ampdu_factor = pHTInfo->ForcedAMPDUFactor;
391                         break;
392
393                 case HT_AGG_FORCE_DISABLE:
394                         tcb_desc->bAMPDUEnable = false;
395                         tcb_desc->ampdu_density = 0;
396                         tcb_desc->ampdu_factor = 0;
397                         break;
398
399         }
400                 return;
401 }
402
403 extern void ieee80211_qurey_ShortPreambleMode(struct ieee80211_device* ieee, cb_desc* tcb_desc)
404 {
405         tcb_desc->bUseShortPreamble = false;
406         if (tcb_desc->data_rate == 2)
407         {//// 1M can only use Long Preamble. 11B spec
408                 return;
409         }
410         else if (ieee->current_network.capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
411         {
412                 tcb_desc->bUseShortPreamble = true;
413         }
414         return;
415 }
416 extern  void
417 ieee80211_query_HTCapShortGI(struct ieee80211_device *ieee, cb_desc *tcb_desc)
418 {
419         PRT_HIGH_THROUGHPUT             pHTInfo = ieee->pHTInfo;
420
421         tcb_desc->bUseShortGI           = false;
422
423         if(!pHTInfo->bCurrentHTSupport||!pHTInfo->bEnableHT)
424                 return;
425
426         if(pHTInfo->bForcedShortGI)
427         {
428                 tcb_desc->bUseShortGI = true;
429                 return;
430         }
431
432         if((pHTInfo->bCurBW40MHz==true) && pHTInfo->bCurShortGI40MHz)
433                 tcb_desc->bUseShortGI = true;
434         else if((pHTInfo->bCurBW40MHz==false) && pHTInfo->bCurShortGI20MHz)
435                 tcb_desc->bUseShortGI = true;
436 }
437
438 void ieee80211_query_BandwidthMode(struct ieee80211_device* ieee, cb_desc *tcb_desc)
439 {
440         PRT_HIGH_THROUGHPUT     pHTInfo = ieee->pHTInfo;
441
442         tcb_desc->bPacketBW = false;
443
444         if(!pHTInfo->bCurrentHTSupport||!pHTInfo->bEnableHT)
445                 return;
446
447         if(tcb_desc->bMulticast || tcb_desc->bBroadcast)
448                 return;
449
450         if((tcb_desc->data_rate & 0x80)==0) // If using legacy rate, it shall use 20MHz channel.
451                 return;
452         //BandWidthAutoSwitch is for auto switch to 20 or 40 in long distance
453         if(pHTInfo->bCurBW40MHz && pHTInfo->bCurTxBW40MHz && !ieee->bandwidth_auto_switch.bforced_tx20Mhz)
454                 tcb_desc->bPacketBW = true;
455         return;
456 }
457
458 void ieee80211_query_protectionmode(struct ieee80211_device* ieee, cb_desc* tcb_desc, struct sk_buff* skb)
459 {
460         // Common Settings
461         tcb_desc->bRTSSTBC                      = false;
462         tcb_desc->bRTSUseShortGI                = false; // Since protection frames are always sent by legacy rate, ShortGI will never be used.
463         tcb_desc->bCTSEnable                    = false; // Most of protection using RTS/CTS
464         tcb_desc->RTSSC                         = 0;            // 20MHz: Don't care;  40MHz: Duplicate.
465         tcb_desc->bRTSBW                        = false; // RTS frame bandwidth is always 20MHz
466
467         if(tcb_desc->bBroadcast || tcb_desc->bMulticast)//only unicast frame will use rts/cts
468                 return;
469
470         if (is_broadcast_ether_addr(skb->data+16))  //check addr3 as infrastructure add3 is DA.
471                 return;
472
473         if (ieee->mode < IEEE_N_24G) //b, g mode
474         {
475                         // (1) RTS_Threshold is compared to the MPDU, not MSDU.
476                         // (2) If there are more than one frag in  this MSDU, only the first frag uses protection frame.
477                         //              Other fragments are protected by previous fragment.
478                         //              So we only need to check the length of first fragment.
479                 if (skb->len > ieee->rts)
480                 {
481                         tcb_desc->bRTSEnable = true;
482                         tcb_desc->rts_rate = MGN_24M;
483                 }
484                 else if (ieee->current_network.buseprotection)
485                 {
486                         // Use CTS-to-SELF in protection mode.
487                         tcb_desc->bRTSEnable = true;
488                         tcb_desc->bCTSEnable = true;
489                         tcb_desc->rts_rate = MGN_24M;
490                 }
491                 //otherwise return;
492                 return;
493         }
494         else
495         {// 11n High throughput case.
496                 PRT_HIGH_THROUGHPUT pHTInfo = ieee->pHTInfo;
497                 while (true)
498                 {
499                         //check ERP protection
500                         if (ieee->current_network.buseprotection)
501                         {// CTS-to-SELF
502                                 tcb_desc->bRTSEnable = true;
503                                 tcb_desc->bCTSEnable = true;
504                                 tcb_desc->rts_rate = MGN_24M;
505                                 break;
506                         }
507                         //check HT op mode
508                         if(pHTInfo->bCurrentHTSupport  && pHTInfo->bEnableHT)
509                         {
510                                 u8 HTOpMode = pHTInfo->CurrentOpMode;
511                                 if((pHTInfo->bCurBW40MHz && (HTOpMode == 2 || HTOpMode == 3)) ||
512                                                         (!pHTInfo->bCurBW40MHz && HTOpMode == 3) )
513                                 {
514                                         tcb_desc->rts_rate = MGN_24M; // Rate is 24Mbps.
515                                         tcb_desc->bRTSEnable = true;
516                                         break;
517                                 }
518                         }
519                         //check rts
520                         if (skb->len > ieee->rts)
521                         {
522                                 tcb_desc->rts_rate = MGN_24M; // Rate is 24Mbps.
523                                 tcb_desc->bRTSEnable = true;
524                                 break;
525                         }
526                         //to do list: check MIMO power save condition.
527                         //check AMPDU aggregation for TXOP
528                         if(tcb_desc->bAMPDUEnable)
529                         {
530                                 tcb_desc->rts_rate = MGN_24M; // Rate is 24Mbps.
531                                 // According to 8190 design, firmware sends CF-End only if RTS/CTS is enabled. However, it degrads
532                                 // throughput around 10M, so we disable of this mechanism. 2007.08.03 by Emily
533                                 tcb_desc->bRTSEnable = false;
534                                 break;
535                         }
536                         //check IOT action
537                         if(pHTInfo->IOTAction & HT_IOT_ACT_FORCED_CTS2SELF)
538                         {
539                                 tcb_desc->bCTSEnable    = true;
540                                 tcb_desc->rts_rate  =   MGN_24M;
541                                 tcb_desc->bRTSEnable = true;
542                                 break;
543                         }
544                         // Totally no protection case!!
545                         goto NO_PROTECTION;
546                 }
547                 }
548         // For test , CTS replace with RTS
549         if( 0 )
550         {
551                 tcb_desc->bCTSEnable    = true;
552                 tcb_desc->rts_rate = MGN_24M;
553                 tcb_desc->bRTSEnable    = true;
554         }
555         if (ieee->current_network.capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
556                 tcb_desc->bUseShortPreamble = true;
557         if (ieee->mode == IW_MODE_MASTER)
558                         goto NO_PROTECTION;
559         return;
560 NO_PROTECTION:
561         tcb_desc->bRTSEnable    = false;
562         tcb_desc->bCTSEnable    = false;
563         tcb_desc->rts_rate              = 0;
564         tcb_desc->RTSSC         = 0;
565         tcb_desc->bRTSBW                = false;
566 }
567
568
569 void ieee80211_txrate_selectmode(struct ieee80211_device* ieee, cb_desc* tcb_desc)
570 {
571 #ifdef TO_DO_LIST
572         if(!IsDataFrame(pFrame))
573         {
574                 pTcb->bTxDisableRateFallBack = TRUE;
575                 pTcb->bTxUseDriverAssingedRate = TRUE;
576                 pTcb->RATRIndex = 7;
577                 return;
578         }
579
580         if(pMgntInfo->ForcedDataRate!= 0)
581         {
582                 pTcb->bTxDisableRateFallBack = TRUE;
583                 pTcb->bTxUseDriverAssingedRate = TRUE;
584                 return;
585         }
586 #endif
587         if(ieee->bTxDisableRateFallBack)
588                 tcb_desc->bTxDisableRateFallBack = true;
589
590         if(ieee->bTxUseDriverAssingedRate)
591                 tcb_desc->bTxUseDriverAssingedRate = true;
592         if(!tcb_desc->bTxDisableRateFallBack || !tcb_desc->bTxUseDriverAssingedRate)
593         {
594                 if (ieee->iw_mode == IW_MODE_INFRA || ieee->iw_mode == IW_MODE_ADHOC)
595                         tcb_desc->RATRIndex = 0;
596         }
597 }
598
599 void ieee80211_query_seqnum(struct ieee80211_device*ieee, struct sk_buff* skb, u8* dst)
600 {
601         if (is_multicast_ether_addr(dst) || is_broadcast_ether_addr(dst))
602                 return;
603         if (IsQoSDataFrame(skb->data)) //we deal qos data only
604         {
605                 PTX_TS_RECORD pTS = NULL;
606                 if (!GetTs(ieee, (PTS_COMMON_INFO*)(&pTS), dst, skb->priority, TX_DIR, true))
607                 {
608                         return;
609                 }
610                 pTS->TxCurSeq = (pTS->TxCurSeq+1)%4096;
611         }
612 }
613
614 int ieee80211_rtl_xmit(struct sk_buff *skb, struct net_device *dev)
615 {
616 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
617         struct ieee80211_device *ieee = netdev_priv(dev);
618 #else
619         struct ieee80211_device *ieee = (struct ieee80211_device *)dev->priv;
620 #endif
621         struct ieee80211_txb *txb = NULL;
622         struct ieee80211_hdr_3addrqos *frag_hdr;
623         int i, bytes_per_frag, nr_frags, bytes_last_frag, frag_size;
624         unsigned long flags;
625         struct net_device_stats *stats = &ieee->stats;
626         int ether_type = 0, encrypt;
627         int bytes, fc, qos_ctl = 0, hdr_len;
628         struct sk_buff *skb_frag;
629         struct ieee80211_hdr_3addrqos header = { /* Ensure zero initialized */
630                 .duration_id = 0,
631                 .seq_ctl = 0,
632                 .qos_ctl = 0
633         };
634         u8 dest[ETH_ALEN], src[ETH_ALEN];
635         int qos_actived = ieee->current_network.qos_data.active;
636
637         struct ieee80211_crypt_data* crypt;
638         bool    bdhcp =false;
639
640         cb_desc *tcb_desc;
641
642         spin_lock_irqsave(&ieee->lock, flags);
643
644         /* If there is no driver handler to take the TXB, dont' bother
645          * creating it... */
646         if ((!ieee->hard_start_xmit && !(ieee->softmac_features & IEEE_SOFTMAC_TX_QUEUE))||
647            ((!ieee->softmac_data_hard_start_xmit && (ieee->softmac_features & IEEE_SOFTMAC_TX_QUEUE)))) {
648                 printk(KERN_WARNING "%s: No xmit handler.\n",
649                        ieee->dev->name);
650                 goto success;
651         }
652
653
654         if(likely(ieee->raw_tx == 0)){
655                 if (unlikely(skb->len < SNAP_SIZE + sizeof(u16))) {
656                         printk(KERN_WARNING "%s: skb too small (%d).\n",
657                         ieee->dev->name, skb->len);
658                         goto success;
659                 }
660
661                 memset(skb->cb, 0, sizeof(skb->cb));
662                 ether_type = ntohs(((struct ethhdr *)skb->data)->h_proto);
663
664                 crypt = ieee->crypt[ieee->tx_keyidx];
665
666                 encrypt = !(ether_type == ETH_P_PAE && ieee->ieee802_1x) &&
667                         ieee->host_encrypt && crypt && crypt->ops;
668
669                 if (!encrypt && ieee->ieee802_1x &&
670                 ieee->drop_unencrypted && ether_type != ETH_P_PAE) {
671                         stats->tx_dropped++;
672                         goto success;
673                 }
674         #ifdef CONFIG_IEEE80211_DEBUG
675                 if (crypt && !encrypt && ether_type == ETH_P_PAE) {
676                         struct eapol *eap = (struct eapol *)(skb->data +
677                                 sizeof(struct ethhdr) - SNAP_SIZE - sizeof(u16));
678                         IEEE80211_DEBUG_EAP("TX: IEEE 802.11 EAPOL frame: %s\n",
679                                 eap_get_type(eap->type));
680                 }
681         #endif
682
683                 // The following is for DHCP and ARP packet, we use cck1M to tx these packets and let LPS awake some time
684                 // to prevent DHCP protocol fail
685                 if (skb->len > 282){//MINIMUM_DHCP_PACKET_SIZE) {
686                         if (ETH_P_IP == ether_type) {// IP header
687                                 const struct iphdr *ip = (struct iphdr *)((u8 *)skb->data+14);
688                                 if (IPPROTO_UDP == ip->protocol) {//FIXME windows is 11 but here UDP in linux kernel is 17.
689                                         struct udphdr *udp = (struct udphdr *)((u8 *)ip + (ip->ihl << 2));
690                                         //if(((ntohs(udp->source) == 68) && (ntohs(udp->dest) == 67)) ||
691                                         ///   ((ntohs(udp->source) == 67) && (ntohs(udp->dest) == 68))) {
692                                         if(((((u8 *)udp)[1] == 68) && (((u8 *)udp)[3] == 67)) ||
693                                                         ((((u8 *)udp)[1] == 67) && (((u8 *)udp)[3] == 68))) {
694                                                 // 68 : UDP BOOTP client
695                                                 // 67 : UDP BOOTP server
696                                                 printk("DHCP pkt src port:%d, dest port:%d!!\n", ((u8 *)udp)[1],((u8 *)udp)[3]);
697                                                 // Use low rate to send DHCP packet.
698                                                 //if(pMgntInfo->IOTAction & HT_IOT_ACT_WA_IOT_Broadcom)
699                                                 //{
700                                                 //      tcb_desc->DataRate = MgntQuery_TxRateExcludeCCKRates(ieee);//0xc;//ofdm 6m
701                                                 //      tcb_desc->bTxDisableRateFallBack = false;
702                                                 //}
703                                                 //else
704                                                 //pTcb->DataRate = Adapter->MgntInfo.LowestBasicRate;
705                                                 //RTPRINT(FDM, WA_IOT, ("DHCP TranslateHeader(), pTcb->DataRate = 0x%x\n", pTcb->DataRate));
706
707                                                 bdhcp = true;
708 #ifdef _RTL8192_EXT_PATCH_
709                                                 ieee->LPSDelayCnt = 100;//pPSC->LPSAwakeIntvl*2; //AMY,090701
710 #else
711                                                 ieee->LPSDelayCnt = 100;//pPSC->LPSAwakeIntvl*2;
712 #endif
713                                         }
714                                 }
715                                 }else if(ETH_P_ARP == ether_type){// IP ARP packet
716                                         printk("=================>DHCP Protocol start tx ARP pkt!!\n");
717                                         bdhcp = true;
718                                         ieee->LPSDelayCnt = ieee->current_network.tim.tim_count;
719
720                                         //if(pMgntInfo->IOTAction & HT_IOT_ACT_WA_IOT_Broadcom)
721                                         //{
722                                         //      tcb_desc->DataRate = MgntQuery_TxRateExcludeCCKRates(Adapter->MgntInfo.mBrates);//0xc;//ofdm 6m
723                                         //      tcb_desc->bTxDisableRateFallBack = FALSE;
724                                         //}
725                                         //else
726                                         //      tcb_desc->DataRate = Adapter->MgntInfo.LowestBasicRate;
727                                         //RTPRINT(FDM, WA_IOT, ("ARP TranslateHeader(), pTcb->DataRate = 0x%x\n", pTcb->DataRate));
728
729                                 }
730                         }
731
732                 /* Save source and destination addresses */
733                 memcpy(&dest, skb->data, ETH_ALEN);
734                 memcpy(&src, skb->data+ETH_ALEN, ETH_ALEN);
735
736                 /* Advance the SKB to the start of the payload */
737                 skb_pull(skb, sizeof(struct ethhdr));
738
739                 /* Determine total amount of storage required for TXB packets */
740                 bytes = skb->len + SNAP_SIZE + sizeof(u16);
741
742                 if (encrypt)
743                         fc = IEEE80211_FTYPE_DATA | IEEE80211_FCTL_WEP;
744                 else
745
746                         fc = IEEE80211_FTYPE_DATA;
747
748                 //if(ieee->current_network.QoS_Enable)
749                 if(qos_actived)
750                         fc |= IEEE80211_STYPE_QOS_DATA;
751                 else
752                         fc |= IEEE80211_STYPE_DATA;
753
754                 if (ieee->iw_mode == IW_MODE_INFRA) {
755                         fc |= IEEE80211_FCTL_TODS;
756                         /* To DS: Addr1 = BSSID, Addr2 = SA,
757                         Addr3 = DA */
758                         memcpy(&header.addr1, ieee->current_network.bssid, ETH_ALEN);
759                         memcpy(&header.addr2, &src, ETH_ALEN);
760                         memcpy(&header.addr3, &dest, ETH_ALEN);
761                 } else if (ieee->iw_mode == IW_MODE_ADHOC) {
762                         /* not From/To DS: Addr1 = DA, Addr2 = SA,
763                         Addr3 = BSSID */
764                         memcpy(&header.addr1, dest, ETH_ALEN);
765                         memcpy(&header.addr2, src, ETH_ALEN);
766                         memcpy(&header.addr3, ieee->current_network.bssid, ETH_ALEN);
767                 }
768
769                 header.frame_ctl = cpu_to_le16(fc);
770
771                 /* Determine fragmentation size based on destination (multicast
772                 * and broadcast are not fragmented) */
773                 if (is_multicast_ether_addr(header.addr1) ||
774                 is_broadcast_ether_addr(header.addr1)) {
775                         frag_size = MAX_FRAG_THRESHOLD;
776                         qos_ctl |= QOS_CTL_NOTCONTAIN_ACK;
777                 }
778                 else {
779                         frag_size = ieee->fts;//default:392
780                         qos_ctl = 0;
781                 }
782
783                 //if (ieee->current_network.QoS_Enable)
784                 if(qos_actived)
785                 {
786                         hdr_len = IEEE80211_3ADDR_LEN + 2;
787
788                         skb->priority = ieee80211_classify(skb, &ieee->current_network);
789                         qos_ctl |= skb->priority; //set in the ieee80211_classify
790                         header.qos_ctl = cpu_to_le16(qos_ctl & IEEE80211_QOS_TID);
791                 } else {
792                         hdr_len = IEEE80211_3ADDR_LEN;
793                 }
794                 /* Determine amount of payload per fragment.  Regardless of if
795                 * this stack is providing the full 802.11 header, one will
796                 * eventually be affixed to this fragment -- so we must account for
797                 * it when determining the amount of payload space. */
798                 bytes_per_frag = frag_size - hdr_len;
799                 if (ieee->config &
800                 (CFG_IEEE80211_COMPUTE_FCS | CFG_IEEE80211_RESERVE_FCS))
801                         bytes_per_frag -= IEEE80211_FCS_LEN;
802
803                 /* Each fragment may need to have room for encryptiong pre/postfix */
804                 if (encrypt)
805                         bytes_per_frag -= crypt->ops->extra_prefix_len +
806                                 crypt->ops->extra_postfix_len;
807
808                 /* Number of fragments is the total bytes_per_frag /
809                 * payload_per_fragment */
810                 nr_frags = bytes / bytes_per_frag;
811                 bytes_last_frag = bytes % bytes_per_frag;
812                 if (bytes_last_frag)
813                         nr_frags++;
814                 else
815                         bytes_last_frag = bytes_per_frag;
816
817                 /* When we allocate the TXB we allocate enough space for the reserve
818                 * and full fragment bytes (bytes_per_frag doesn't include prefix,
819                 * postfix, header, FCS, etc.) */
820                 txb = ieee80211_alloc_txb(nr_frags, frag_size + ieee->tx_headroom, GFP_ATOMIC);
821                 if (unlikely(!txb)) {
822                         printk(KERN_WARNING "%s: Could not allocate TXB\n",
823                         ieee->dev->name);
824                         goto failed;
825                 }
826                 txb->encrypted = encrypt;
827                 txb->payload_size = bytes;
828
829                 //if (ieee->current_network.QoS_Enable)
830                 if(qos_actived)
831                 {
832                         txb->queue_index = UP2AC(skb->priority);
833                 } else {
834                         txb->queue_index = WME_AC_BK;;
835                 }
836
837
838
839                 for (i = 0; i < nr_frags; i++) {
840                         skb_frag = txb->fragments[i];
841                         tcb_desc = (cb_desc *)(skb_frag->cb + MAX_DEV_ADDR_SIZE);
842                         if(qos_actived){
843                                 skb_frag->priority = skb->priority;//UP2AC(skb->priority);
844                                 tcb_desc->queue_index =  UP2AC(skb->priority);
845                         } else {
846                                 skb_frag->priority = WME_AC_BK;
847                                 tcb_desc->queue_index = WME_AC_BK;
848                         }
849                         skb_reserve(skb_frag, ieee->tx_headroom);
850
851                         if (encrypt){
852                                 if (ieee->hwsec_active)
853                                         tcb_desc->bHwSec = 1;
854                                 else
855                                         tcb_desc->bHwSec = 0;
856                                 skb_reserve(skb_frag, crypt->ops->extra_prefix_len);
857                         }
858                         else
859                         {
860                                 tcb_desc->bHwSec = 0;
861                         }
862                         frag_hdr = (struct ieee80211_hdr_3addrqos *)skb_put(skb_frag, hdr_len);
863                         memcpy(frag_hdr, &header, hdr_len);
864
865                         /* If this is not the last fragment, then add the MOREFRAGS
866                         * bit to the frame control */
867                         if (i != nr_frags - 1) {
868                                 frag_hdr->frame_ctl = cpu_to_le16(
869                                         fc | IEEE80211_FCTL_MOREFRAGS);
870                                 bytes = bytes_per_frag;
871
872                         } else {
873                                 /* The last fragment takes the remaining length */
874                                 bytes = bytes_last_frag;
875                         }
876                         //if(ieee->current_network.QoS_Enable)
877                         if(qos_actived)
878                         {
879                                 // add 1 only indicate to corresponding seq number control 2006/7/12
880                                 frag_hdr->seq_ctl = cpu_to_le16(ieee->seq_ctrl[UP2AC(skb->priority)+1]<<4 | i);
881                         } else {
882                                 frag_hdr->seq_ctl = cpu_to_le16(ieee->seq_ctrl[0]<<4 | i);
883                         }
884
885                         /* Put a SNAP header on the first fragment */
886                         if (i == 0) {
887                                 ieee80211_put_snap(
888                                         skb_put(skb_frag, SNAP_SIZE + sizeof(u16)),
889                                         ether_type);
890                                 bytes -= SNAP_SIZE + sizeof(u16);
891                         }
892
893                         memcpy(skb_put(skb_frag, bytes), skb->data, bytes);
894
895                         /* Advance the SKB... */
896                         skb_pull(skb, bytes);
897
898                         /* Encryption routine will move the header forward in order
899                         * to insert the IV between the header and the payload */
900                         if (encrypt)
901                                 ieee80211_encrypt_fragment(ieee, skb_frag, hdr_len);
902                         if (ieee->config &
903                         (CFG_IEEE80211_COMPUTE_FCS | CFG_IEEE80211_RESERVE_FCS))
904                                 skb_put(skb_frag, 4);
905                 }
906
907                 if(qos_actived)
908                 {
909                   if (ieee->seq_ctrl[UP2AC(skb->priority) + 1] == 0xFFF)
910                         ieee->seq_ctrl[UP2AC(skb->priority) + 1] = 0;
911                   else
912                         ieee->seq_ctrl[UP2AC(skb->priority) + 1]++;
913                 } else {
914                   if (ieee->seq_ctrl[0] == 0xFFF)
915                         ieee->seq_ctrl[0] = 0;
916                   else
917                         ieee->seq_ctrl[0]++;
918                 }
919         }else{
920                 if (unlikely(skb->len < sizeof(struct ieee80211_hdr_3addr))) {
921                         printk(KERN_WARNING "%s: skb too small (%d).\n",
922                         ieee->dev->name, skb->len);
923                         goto success;
924                 }
925
926                 txb = ieee80211_alloc_txb(1, skb->len, GFP_ATOMIC);
927                 if(!txb){
928                         printk(KERN_WARNING "%s: Could not allocate TXB\n",
929                         ieee->dev->name);
930                         goto failed;
931                 }
932
933                 txb->encrypted = 0;
934                 txb->payload_size = skb->len;
935                 memcpy(skb_put(txb->fragments[0],skb->len), skb->data, skb->len);
936         }
937
938  success:
939 //WB add to fill data tcb_desc here. only first fragment is considered, need to change, and you may remove to other place.
940         if (txb)
941         {
942 #if 1
943                 cb_desc *tcb_desc = (cb_desc *)(txb->fragments[0]->cb + MAX_DEV_ADDR_SIZE);
944                 tcb_desc->bTxEnableFwCalcDur = 1;
945                 if (is_multicast_ether_addr(header.addr1))
946                         tcb_desc->bMulticast = 1;
947                 if (is_broadcast_ether_addr(header.addr1))
948                         tcb_desc->bBroadcast = 1;
949                 ieee80211_txrate_selectmode(ieee, tcb_desc);
950                 if ( tcb_desc->bMulticast ||  tcb_desc->bBroadcast)
951                         tcb_desc->data_rate = ieee->basic_rate;
952                 else
953                         //tcb_desc->data_rate = CURRENT_RATE(ieee->current_network.mode, ieee->rate, ieee->HTCurrentOperaRate);
954                         tcb_desc->data_rate = CURRENT_RATE(ieee->mode, ieee->rate, ieee->HTCurrentOperaRate);
955
956                 if(bdhcp == true){
957                         // Use low rate to send DHCP packet.
958                         //if(ieee->pHTInfo->IOTAction & HT_IOT_ACT_WA_IOT_Broadcom) {
959                         //      tcb_desc->data_rate = MGN_1M;//MgntQuery_TxRateExcludeCCKRates(ieee);//0xc;//ofdm 6m
960                         //      tcb_desc->bTxDisableRateFallBack = false;
961                         //}
962                         //else
963                         {
964                                 tcb_desc->data_rate = MGN_1M;
965                                 tcb_desc->bTxDisableRateFallBack = 1;
966                         }
967
968                         tcb_desc->RATRIndex = 7;
969                         tcb_desc->bTxUseDriverAssingedRate = 1;
970                         tcb_desc->bdhcp = 1;
971                 }
972
973
974                 ieee80211_qurey_ShortPreambleMode(ieee, tcb_desc);
975                 ieee80211_tx_query_agg_cap(ieee, txb->fragments[0], tcb_desc);
976                 ieee80211_query_HTCapShortGI(ieee, tcb_desc);
977                 ieee80211_query_BandwidthMode(ieee, tcb_desc);
978                 ieee80211_query_protectionmode(ieee, tcb_desc, txb->fragments[0]);
979                 ieee80211_query_seqnum(ieee, txb->fragments[0], header.addr1);
980 //              IEEE80211_DEBUG_DATA(IEEE80211_DL_DATA, txb->fragments[0]->data, txb->fragments[0]->len);
981                 //IEEE80211_DEBUG_DATA(IEEE80211_DL_DATA, tcb_desc, sizeof(cb_desc));
982 #endif
983         }
984         spin_unlock_irqrestore(&ieee->lock, flags);
985         dev_kfree_skb_any(skb);
986         if (txb) {
987                 if (ieee->softmac_features & IEEE_SOFTMAC_TX_QUEUE){
988                         ieee80211_softmac_xmit(txb, ieee);
989                 }else{
990                         if ((*ieee->hard_start_xmit)(txb, dev) == 0) {
991                                 stats->tx_packets++;
992                                 stats->tx_bytes += txb->payload_size;
993                                 return 0;
994                         }
995                         ieee80211_txb_free(txb);
996                 }
997         }
998
999         return 0;
1000
1001  failed:
1002         spin_unlock_irqrestore(&ieee->lock, flags);
1003         netif_stop_queue(dev);
1004         stats->tx_errors++;
1005         return 1;
1006
1007 }
1008
1009 //EXPORT_SYMBOL(ieee80211_txb_free);