~ubuntu-branches/ubuntu/precise/rt2570/precise

« back to all changes in this revision

Viewing changes to Module/rtusb_data.c

  • Committer: Bazaar Package Importer
  • Author(s): Aurelien Jarno
  • Date: 2005-05-29 19:03:04 UTC
  • Revision ID: james.westby@ubuntu.com-20050529190304-2brtsi5bwy4wp3bo
Tags: upstream-1.1.0+cvs20060421
ImportĀ upstreamĀ versionĀ 1.1.0+cvs20060421

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*************************************************************************** 
 
2
 * RT2x00 SourceForge Project - http://rt2x00.sourceforge.net              * 
 
3
 *                                                                         * 
 
4
 *   This program is free software; you can redistribute it and/or modify  * 
 
5
 *   it under the terms of the GNU General Public License as published by  * 
 
6
 *   the Free Software Foundation; either version 2 of the License, or     * 
 
7
 *   (at your option) any later version.                                   * 
 
8
 *                                                                         * 
 
9
 *   This program is distributed in the hope that it will be useful,       * 
 
10
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        * 
 
11
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         * 
 
12
 *   GNU General Public License for more details.                          * 
 
13
 *                                                                         * 
 
14
 *   You should have received a copy of the GNU General Public License     * 
 
15
 *   along with this program; if not, write to the                         * 
 
16
 *   Free Software Foundation, Inc.,                                       * 
 
17
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             * 
 
18
 *                                                                         * 
 
19
 *   Licensed under the GNU GPL                                            * 
 
20
 *   Original code supplied under license from RaLink Inc, 2004.           * 
 
21
 ***************************************************************************/
 
22
 
 
23
/*************************************************************************** 
 
24
 *      Module Name:    rtusb_data.c
 
25
 *
 
26
 *      Abstract:
 
27
 *
 
28
 *      Revision History:
 
29
 *      Who             When            What
 
30
 *      --------        ----------      -------------------------------
 
31
 *      Name            Date            Modification logs
 
32
 *      Jan Lee         2005-06-01      Release
 
33
 *      RobinC          02-06-2005      RFMON Mode added
 
34
 *      MathiasK        04-07-2005      big endian fix from gglomm
 
35
 ***************************************************************************/
 
36
 
 
37
#include        "rt_config.h"
 
38
 
 
39
static  UCHAR PlcpSignal[12] = { 
 
40
         0, /* RATE_1 */    1, /* RATE_2 */     2, /* RATE_5_5 */   3, /* RATE_11 */    // see BBP spec
 
41
        11, /* RATE_6 */   15, /* RATE_9 */    10, /* RATE_12 */   14, /* RATE_18 */    // see IEEE802.11a-1999 p.14
 
42
         9, /* RATE_24 */  13, /* RATE_36 */    8, /* RATE_48 */    12  /* RATE_54 */ }; // see IEEE802.11a-1999 p.14
 
43
static  UINT    _11G_RATES[12] = { 0, 0, 0, 0, 6, 9, 12, 18, 24, 36, 48, 54 };
 
44
static  UCHAR   SNAP_802_1H[] = {0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00};
 
45
static  UCHAR   SNAP_BRIDGE_TUNNEL[] = {0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8};
 
46
static  UCHAR   EAPOL[] = {0x88, 0x8e};
 
47
static  UCHAR   IPX[] = {0x81, 0x37};
 
48
static  UCHAR   APPLE_TALK[] = {0x80, 0xf3};
 
49
UCHAR   ARP[]={0x08, 0x06};
 
50
////////////////////////////////////////////////////////////////////////////
 
51
//
 
52
//      FUNCTION
 
53
//              RTUSBSendPackets
 
54
//              
 
55
//      DESCRIPTION
 
56
//        VNETMultipleSend handler is called by NDIS to transmit packets
 
57
//        through the adapter. If there are packets in the Q and the device
 
58
//        can accept the Tx requests initiate a transmission and queue the
 
59
//        rest of the packets (if any...). If we can not transmit or the
 
60
//        station is not ready we imediatelly complete the request
 
61
//              
 
62
//      INPUT
 
63
//        MiniportAdapterContext                Context registered with the wrapper
 
64
//                                                                      (Ptr to to the Adapter object)
 
65
//        PacketArray                                   Array of Ptrs to NDIS_PACKET structs
 
66
//        NumberOfPackets                               Number of packets in PacketArray
 
67
//              
 
68
//      OUTPUT
 
69
//              -
 
70
//              
 
71
////////////////////////////////////////////////////////////////////////////
 
72
int RTUSBSendPackets(struct sk_buff *skb, struct net_device *net_dev)
 
73
{
 
74
 
 
75
        NDIS_STATUS     Status = NDIS_STATUS_SUCCESS;
 
76
        PRT2570ADAPTER  pAdapter = net_dev->priv;
 
77
        
 
78
        skb->data_len = skb->len;
 
79
        if (RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_RESET_IN_PROGRESS) ||
 
80
                RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_REMOVE_IN_PROGRESS) ||
 
81
                RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_RADIO_OFF) ||
 
82
                RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_HALT_IN_PROGRESS))
 
83
        {
 
84
                RTUSBFreeSkbBuffer(skb);
 
85
                return 0;
 
86
        }
 
87
        // Drop packets if no associations
 
88
        else if (!INFRA_ON(pAdapter) && !ADHOC_ON(pAdapter) && 
 
89
                   !( pAdapter->PortCfg.BssType == BSS_MONITOR && pAdapter->PortCfg.MallowRFMONTx == TRUE )
 
90
                 )
 
91
        {
 
92
                RTUSBFreeSkbBuffer(skb);
 
93
                return 0;
 
94
        }
 
95
        else
 
96
        {
 
97
                Status = RTUSBSendPacket(pAdapter, skb);
 
98
                if (Status != NDIS_STATUS_SUCCESS)
 
99
                {
 
100
                        // Errors before enqueue stage
 
101
                        RTUSBFreeSkbBuffer(skb);
 
102
                        DBGPRINT(RT_DEBUG_TRACE,"<---RTUSBSendPackets not dequeue\n");
 
103
                        return 0;
 
104
                }
 
105
        }
 
106
        // Dequeue one frame from SendTxWait queue and process it
 
107
        // There are two place calling dequeue for TX ring.
 
108
        // 1. Here, right after queueing the frame.
 
109
        // 2. At the end of TxRingTxDone service routine.
 
110
        if ((!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_BSS_SCAN_IN_PROGRESS)) && 
 
111
                (!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_RADIO_OFF)) &&
 
112
                (!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_RESET_IN_PROGRESS)) &&
 
113
                (!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_REMOVE_IN_PROGRESS)) &&
 
114
                (!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_HALT_IN_PROGRESS)))
 
115
        {
 
116
            //RTMPDeQueuePacket(pAdapter, &pAdapter->TxSwQueue0);
 
117
            // Call dequeue without selected queue, let the subroutine select the right priority
 
118
            // Tx software queue
 
119
            RTUSBDeQueuePacket(pAdapter);
 
120
        }
 
121
 
 
122
        // Kick bulk out
 
123
        RTUSBKickBulkOut(pAdapter);
 
124
        return 0;
 
125
}
 
126
 
 
127
NDIS_STATUS     RTUSBSendPacket(
 
128
        IN      PRT2570ADAPTER  pAdapter,
 
129
        IN  struct sk_buff  *skb)
 
130
{
 
131
 
 
132
 
 
133
        PVOID                   pVirtualAddress;
 
134
        struct sk_buff_head     *pTxQueue;
 
135
        ULONG                                   Priority;
 
136
        UCHAR                   NumberOfFrag;
 
137
        UCHAR                   RTSRequired;
 
138
        UINT                    AllowFragSize;
 
139
        UCHAR                   AccessCategory;
 
140
        NDIS_STATUS             Status = NDIS_STATUS_FAILURE;
 
141
        UCHAR                   PsMode;
 
142
 
 
143
        // Init priority value
 
144
        Priority = 0;
 
145
        AccessCategory = 0;
 
146
 
 
147
        if (skb && pAdapter->PortCfg.BssType == BSS_MONITOR &&
 
148
                   pAdapter->PortCfg.MallowRFMONTx == TRUE)
 
149
        {
 
150
                NdisAcquireSpinLock(&pAdapter->SendTxWaitQueueLock);
 
151
                skb_queue_tail(&pAdapter->SendTxWaitQueue, skb);
 
152
                NdisReleaseSpinLock(&pAdapter->SendTxWaitQueueLock);
 
153
                return (NDIS_STATUS_SUCCESS);
 
154
        }
 
155
        
 
156
        if (skb)
 
157
        {
 
158
                Priority = skb->priority;
 
159
                // 802.11e/d4.4 June, 2003
 
160
                if (Priority <=2)
 
161
                    AccessCategory = 0;
 
162
                else if (Priority == 3)
 
163
                    AccessCategory = 1;
 
164
                else if (Priority <= 5)
 
165
                    AccessCategory = 2;
 
166
                else
 
167
                    AccessCategory = 3;
 
168
                DBGPRINT(RT_DEBUG_INFO, "Priority = %d, AC = %d\n", Priority, AccessCategory);
 
169
        }
 
170
        // For TKIP, MIC value is treated as payload, it might be fragmented through
 
171
        // different MPDUs.
 
172
        if (pAdapter->PortCfg.WepStatus == Ndis802_11Encryption2Enabled)
 
173
        {
 
174
                skb->data_len += 8;
 
175
        }
 
176
 
 
177
        pVirtualAddress = (PVOID)skb->data;
 
178
        // Check for virtual address allocation, it might fail !!!
 
179
        if (pVirtualAddress == NULL)
 
180
        {
 
181
                DBGPRINT(RT_DEBUG_TRACE,"<---RTUSBSendPacket NULL pVirtualAddress\n");
 
182
                // Resourece is low, system did not allocation virtual address
 
183
                // return NDIS_STATUS_FAILURE directly to upper layer
 
184
                return (Status);
 
185
        }
 
186
 
 
187
        //
 
188
        // Check for multicast or broadcast (First byte of DA)
 
189
        //
 
190
        if ((*((PUCHAR) pVirtualAddress) & 0x01) != 0)
 
191
        {
 
192
                // For multicast & broadcast, there is no fragment allowed
 
193
                NumberOfFrag = 1;
 
194
        }
 
195
        else
 
196
        {
 
197
                // Check for payload allowed for each fragment
 
198
                AllowFragSize = (pAdapter->PortCfg.FragmentThreshold) - LENGTH_802_11 - LENGTH_CRC;
 
199
 
 
200
                // Calculate fragments required
 
201
                NumberOfFrag = ((skb->data_len - LENGTH_802_3 + LENGTH_802_1_H) / AllowFragSize) + 1;
 
202
                // Minus 1 if the size just match to allowable fragment size
 
203
                if (((skb->data_len - LENGTH_802_3 + LENGTH_802_1_H) % AllowFragSize) == 0)
 
204
                {
 
205
                        NumberOfFrag--;
 
206
                }
 
207
        }
 
208
        
 
209
        // Check for requirement of RTS 
 
210
        if (NumberOfFrag > 1)
 
211
        {
 
212
                // If multiple fragment required, RTS is required only for the first fragment
 
213
                // if the fragment size large than RTS threshold
 
214
                RTSRequired = (pAdapter->PortCfg.FragmentThreshold > pAdapter->PortCfg.RtsThreshold) ? 1 : 0;
 
215
        }
 
216
        else
 
217
        {
 
218
                RTSRequired = (skb->data_len > pAdapter->PortCfg.RtsThreshold) ? 1 : 0;
 
219
        }
 
220
 
 
221
        // RTS/CTS may also be required in order to protect OFDM frame
 
222
        if ((pAdapter->PortCfg.TxRate >= RATE_FIRST_OFDM_RATE) && pAdapter->PortCfg.BGProtectionInUsed)
 
223
                RTSRequired = 1;
 
224
        //DBGPRINT(RT_DEBUG_TEMP, "Number of fragments :%d , include RTS :%d\n", NumberOfFrag, NumberOfFrag + RTSRequired);
 
225
        
 
226
        // Save framnet number to Ndis packet reserved field
 
227
        RTMP_SET_PACKET_FRAGMENTS(skb, NumberOfFrag);        
 
228
 
 
229
        // Save RTS requirement to Ndis packet reserved field
 
230
        RTMP_SET_PACKET_RTS(skb, RTSRequired);
 
231
 
 
232
        // Make sure SendTxWait queue resource won't be used by other threads
 
233
        NdisAcquireSpinLock(&pAdapter->SendTxWaitQueueLock);
 
234
 
 
235
        pTxQueue = &pAdapter->SendTxWaitQueue;
 
236
        if (INFRA_ON(pAdapter))
 
237
        {
 
238
                // In infrastructure mode, simply enqueue the packet into Tx waiting queue.
 
239
                DBGPRINT(RT_DEBUG_INFO, "Infrastructure -> Enqueue one frame\n");
 
240
 
 
241
                // Enqueue Ndis packet to end of Tx wait queue
 
242
                skb_queue_tail(pTxQueue, skb);
 
243
                Status = NDIS_STATUS_SUCCESS;
 
244
        }
 
245
        else
 
246
        {
 
247
                // In IBSS mode, power state of destination should be considered.
 
248
                PsMode = PWR_ACTIVE;            // Faked
 
249
                if (PsMode == PWR_ACTIVE)
 
250
                {
 
251
                        // Enqueue Ndis packet to end of Tx wait queue
 
252
                        skb_queue_tail(pTxQueue, skb);
 
253
                        Status = NDIS_STATUS_SUCCESS;
 
254
                }
 
255
        }
 
256
        
 
257
        NdisReleaseSpinLock(&pAdapter->SendTxWaitQueueLock);
 
258
        return Status;
 
259
}
 
260
 
 
261
VOID    RTUSBDeQueuePacket(
 
262
        IN      PRT2570ADAPTER  pAdapter)
 
263
{
 
264
 
 
265
        UCHAR                   FragmentRequired;
 
266
        NDIS_STATUS             Status = NDIS_STATUS_SUCCESS;
 
267
        struct sk_buff          *skb;
 
268
        struct sk_buff_head     *pQueue;
 
269
        UCHAR                   Count = 0;
 
270
        // Make sure SendTxWait queue resource won't be used by other threads
 
271
        NdisAcquireSpinLock(&pAdapter->SendTxWaitQueueLock);
 
272
 
 
273
        // Select Queue
 
274
        pQueue = &pAdapter->SendTxWaitQueue;
 
275
 
 
276
        // Check queue before dequeue
 
277
        while (!skb_queue_empty(pQueue) && (Count < MAX_TX_PROCESS))
 
278
        {
 
279
                // Reset is in progress, stop immediately
 
280
                if ( RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_RESET_IN_PROGRESS) ||
 
281
                         RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_REMOVE_IN_PROGRESS) ||
 
282
                     RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_HALT_IN_PROGRESS))
 
283
                {
 
284
                        break;
 
285
                }
 
286
                        
 
287
                // Dequeue the first entry from head of queue list
 
288
                skb = skb_dequeue(pQueue);
 
289
 
 
290
 
 
291
                // RTS or CTS-to-self for B/G protection mode has been set already.
 
292
                // There is no need to re-do it here. 
 
293
                // Total fragment required = number of fragment + RST if required
 
294
                FragmentRequired = RTMP_GET_PACKET_FRAGMENTS(skb) + RTMP_GET_PACKET_RTS(skb);
 
295
                
 
296
                if (RTUSBFreeDescriptorRequest(pAdapter, TX_RING, FragmentRequired) == NDIS_STATUS_SUCCESS)
 
297
                {
 
298
                        // Avaliable ring descriptors are enough for this frame
 
299
                        // Call hard transmit
 
300
                        // Nitro mode / Normal mode selection
 
301
                        NdisReleaseSpinLock(&pAdapter->SendTxWaitQueueLock);
 
302
                        if (pAdapter->PortCfg.EnableTxBurst == 1)
 
303
                                Status = RTUSBHardEncrypt(pAdapter, skb, FragmentRequired, TRUE);
 
304
                        else                            
 
305
                                Status = RTUSBHardEncrypt(pAdapter, skb, FragmentRequired, FALSE);
 
306
                        //
 
307
                        // Acquire the resource again, snice we may need to process it in this while-loop.
 
308
                        //
 
309
                        NdisAcquireSpinLock(&pAdapter->SendTxWaitQueueLock);
 
310
                        if (Status == NDIS_STATUS_FAILURE)
 
311
                        {
 
312
                                // Packet failed due to various Ndis Packet error
 
313
                                RTUSBFreeSkbBuffer(skb);
 
314
                                break;
 
315
                        }
 
316
                        else if (Status == NDIS_STATUS_RESOURCES)
 
317
                        {
 
318
                                // Not enough free tx ring, it might happen due to free descriptor inquery might be not correct
 
319
                                // It also might change to NDIS_STATUS_FAILURE to simply drop the frame
 
320
                                // Put the frame back into head of queue
 
321
                                skb_queue_head(pQueue, skb);
 
322
                                break;
 
323
                        }                       
 
324
                        Count++;
 
325
                }       
 
326
                else
 
327
                {
 
328
                        skb_queue_head(pQueue, skb);
 
329
                        break;
 
330
                }
 
331
        }
 
332
        
 
333
        NdisReleaseSpinLock(&pAdapter->SendTxWaitQueueLock);
 
334
        return;
 
335
        
 
336
}
 
337
 
 
338
NDIS_STATUS     RTUSBFreeDescriptorRequest(
 
339
        IN              PRT2570ADAPTER  pAdapter,
 
340
        IN              UCHAR                   RingType,
 
341
        IN              UCHAR                   NumberRequired)
 
342
{
 
343
        UCHAR                   FreeNumber = 0;
 
344
        UINT                    Index;
 
345
        NDIS_STATUS             Status = NDIS_STATUS_FAILURE;
 
346
 
 
347
        switch (RingType)
 
348
        {
 
349
                case TX_RING:
 
350
                        Index = pAdapter->NextTxIndex;
 
351
                        do
 
352
                        {
 
353
                                PTX_CONTEXT     pTxD  = &pAdapter->TxContext[Index];
 
354
                                
 
355
                                // While Owner bit is NIC, obviously ASIC still need it.
 
356
                                // If valid bit is TRUE, indicate that TxDone has not process yet
 
357
                                // We should not use it until TxDone finish cleanup job
 
358
                                if (pTxD->InUse == FALSE)
 
359
                                {
 
360
                                        // This one is free
 
361
                                        FreeNumber++;
 
362
                                }
 
363
                                else
 
364
                                {
 
365
                                        break;
 
366
                                }                                       
 
367
                                Index = (Index + 1) % TX_RING_SIZE;                             
 
368
                        }       while (FreeNumber < NumberRequired);    // Quit here ! Free number is enough !
 
369
                        
 
370
                        if (FreeNumber >= NumberRequired)
 
371
                        {
 
372
                                Status = NDIS_STATUS_SUCCESS;
 
373
                        }
 
374
                        
 
375
                        break;
 
376
                        
 
377
                case PRIO_RING:
 
378
                        Index = pAdapter->NextMLMEIndex;
 
379
                        do
 
380
                        {
 
381
                                PTX_CONTEXT     pTxD  = &pAdapter->MLMEContext[Index];
 
382
                                
 
383
                                // While Owner bit is NIC, obviously ASIC still need it.
 
384
                                // If valid bit is TRUE, indicate that TxDone has not process yet
 
385
                                // We should not use it until TxDone finish cleanup job
 
386
                                if (pTxD->InUse == FALSE)
 
387
                                {
 
388
                                        // This one is free
 
389
                                        FreeNumber++;
 
390
                                }
 
391
                                else
 
392
                                {
 
393
                                        break;
 
394
                                }
 
395
                                        
 
396
                                Index = (Index + 1) % PRIO_RING_SIZE;                           
 
397
                        }       while (FreeNumber < NumberRequired);    // Quit here ! Free number is enough !
 
398
                        
 
399
                        if (FreeNumber >= NumberRequired)
 
400
                        {
 
401
                                Status = NDIS_STATUS_SUCCESS;
 
402
                        }
 
403
                        break;
 
404
 
 
405
                default:
 
406
                        break;
 
407
 
 
408
 
 
409
        
 
410
        }
 
411
 
 
412
        return (Status);
 
413
 
 
414
}
 
415
/*
 
416
        ========================================================================
 
417
        
 
418
        Routine Description:
 
419
 
 
420
        Arguments:
 
421
 
 
422
        Return Value:
 
423
 
 
424
        IRQL = 
 
425
        
 
426
        Note:
 
427
        
 
428
        ========================================================================
 
429
*/
 
430
VOID    RTUSBRejectPendingPackets(
 
431
        IN      PRT2570ADAPTER  pAdapter)
 
432
{
 
433
        DBGPRINT_RAW(RT_DEBUG_TRACE, "--->RejectPendingPackets\n");
 
434
 
 
435
        NdisAcquireSpinLock(&pAdapter->SendTxWaitQueueLock);
 
436
        DBGPRINT_RAW(RT_DEBUG_TRACE, "Purge SendTxWaitQueue\n");
 
437
        skb_queue_purge(&pAdapter->SendTxWaitQueue);
 
438
        NdisReleaseSpinLock(&pAdapter->SendTxWaitQueueLock);
 
439
 
 
440
        DBGPRINT_RAW(RT_DEBUG_TRACE, "<---RejectPendingPackets\n");
 
441
}
 
442
 
 
443
/*
 
444
        ========================================================================
 
445
 
 
446
        Routine Description:
 
447
                Suspend MSDU transmission
 
448
                
 
449
        Arguments:
 
450
                pAdapter                Pointer to our adapter
 
451
                
 
452
        Return Value:
 
453
                None
 
454
                
 
455
        Note:
 
456
        
 
457
        ========================================================================
 
458
*/
 
459
VOID    RTUSBSuspendMsduTransmission(
 
460
        IN      PRT2570ADAPTER  pAdapter)
 
461
{
 
462
        DBGPRINT(RT_DEBUG_TRACE,"SCANNING, suspend MSDU transmission ...\n");
 
463
        RTMP_SET_FLAG(pAdapter, fRTMP_ADAPTER_BSS_SCAN_IN_PROGRESS);
 
464
}
 
465
 
 
466
/*
 
467
        ========================================================================
 
468
 
 
469
        Routine Description:
 
470
                Resume MSDU transmission
 
471
                
 
472
        Arguments:
 
473
                pAdapter                Pointer to our adapter
 
474
                
 
475
        Return Value:
 
476
                None
 
477
                
 
478
        Note:
 
479
        
 
480
        ========================================================================
 
481
*/
 
482
VOID    RTUSBResumeMsduTransmission(
 
483
        IN      PRT2570ADAPTER  pAdapter)
 
484
{
 
485
        DBGPRINT(RT_DEBUG_TRACE,"SCANNING, resume MSDU transmission ...\n");
 
486
        RTMP_CLEAR_FLAG(pAdapter, fRTMP_ADAPTER_BSS_SCAN_IN_PROGRESS);
 
487
        if ((!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_RESET_IN_PROGRESS)) &&
 
488
                (!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_HALT_IN_PROGRESS)) &&
 
489
                (!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_REMOVE_IN_PROGRESS)) &&
 
490
                (!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_RADIO_OFF)))
 
491
        {
 
492
                // Call dequeue without selected queue, let the subroutine select the right priority
 
493
                // Tx software queue
 
494
                RTUSBDeQueuePacket(pAdapter);
 
495
        }
 
496
 
 
497
        // Kick bulk out
 
498
        RTUSBKickBulkOut(pAdapter);
 
499
 
 
500
}
 
501
/*
 
502
        ========================================================================
 
503
 
 
504
        Routine Description:
 
505
                
 
506
        Arguments:
 
507
                
 
508
        Return Value:
 
509
                
 
510
        Note:
 
511
        
 
512
        ========================================================================
 
513
*/
 
514
USHORT  RTUSBCalcDuration(
 
515
        IN      PRT2570ADAPTER  pAdapter,
 
516
        IN      UCHAR                   Rate,
 
517
        IN      ULONG                   Size)
 
518
{
 
519
        ULONG   Duration = 0;
 
520
 
 
521
        if (Rate < RATE_FIRST_OFDM_RATE) // CCK
 
522
        {
 
523
            if ((Rate > RATE_1) && (pAdapter->PortCfg.TxPreambleInUsed == Rt802_11PreambleShort))
 
524
                Duration = 96;  // 72+24 preamble+plcp
 
525
                else
 
526
            Duration = 192; // 144+48 preamble+plcp
 
527
                
 
528
                Duration += (USHORT)((Size << 4) / RateIdTo500Kbps[Rate]);
 
529
                if ((Size << 4) % RateIdTo500Kbps[Rate])
 
530
                        Duration ++;
 
531
        }
 
532
        else // OFDM rates
 
533
        {
 
534
                Duration = 20 + 6;      // 16+4 preamble+plcp + Signal Extension
 
535
                Duration += 4 * (USHORT)((11 + Size * 4) / RateIdTo500Kbps[Rate]);
 
536
                if ((11 + Size * 4) % RateIdTo500Kbps[Rate])
 
537
                        Duration += 4;
 
538
        }
 
539
        
 
540
        return (USHORT)Duration;        
 
541
}
 
542
 
 
543
/*
 
544
        ========================================================================
 
545
        
 
546
        Routine Description:
 
547
                Calculates the duration which is required to transmit out frames 
 
548
        with given size and specified rate.
 
549
                
 
550
        Arguments:
 
551
                pTxD            Pointer to transmit descriptor
 
552
                Ack                     Setting for Ack requirement bit
 
553
                Fragment        Setting for Fragment bit
 
554
                RetryMode       Setting for retry mode
 
555
                Ifs                     Setting for IFS gap
 
556
                Rate            Setting for transmit rate
 
557
                Service         Setting for service
 
558
                Length          Frame length
 
559
                
 
560
        Return Value:
 
561
                None
 
562
                
 
563
        ========================================================================
 
564
*/
 
565
VOID    RTUSBWriteTxDescriptor(
 
566
        IN      PTXD_STRUC      pTxD,
 
567
        IN      BOOLEAN         Fragment,
 
568
        IN      UCHAR           RetryLimit,
 
569
        IN      BOOLEAN         Ack,
 
570
        IN  BOOLEAN     InsTimestamp,
 
571
        IN  BOOLEAN     new_seq,
 
572
        IN      UCHAR           Ifs,
 
573
        IN      UINT            Length,
 
574
        IN      BOOLEAN         Cipher,
 
575
        IN      UCHAR           KeyID,
 
576
        IN      UCHAR           CWMin,
 
577
        IN      UCHAR           CWMax,
 
578
        IN      UINT            PLCPLength,
 
579
        IN      UINT            Rate,
 
580
        IN      UCHAR           Service,
 
581
        IN  USHORT      TxPreamble)
 
582
{
 
583
        UINT    Residual;
 
584
 
 
585
        pTxD->RetryLimit  = RetryLimit;
 
586
        pTxD->MoreFrag    = Fragment;
 
587
        pTxD->ACK         = Ack;
 
588
        pTxD->Timestamp   = InsTimestamp;
 
589
        pTxD->newseq      = new_seq;
 
590
        pTxD->IFS         = Ifs;
 
591
        pTxD->DataByteCnt = Length;
 
592
        pTxD->Cipher      = Cipher;
 
593
        pTxD->KeyID               = KeyID;
 
594
        pTxD->CWmin       = CWMin;   // 2^5-1 = 31
 
595
        pTxD->CWmax       = CWMax;  // 2^10 -1 = 1023
 
596
        pTxD->Aifs        = 2;   // TC0: SIFS + 2*Slot + Random(CWmin,CWmax)*Slot
 
597
                
 
598
        if (Rate < RATE_FIRST_OFDM_RATE)
 
599
                pTxD->Ofdm = 0;
 
600
        else
 
601
                pTxD->Ofdm = 1;
 
602
 
 
603
        // fill up PLCP SIGNAL field
 
604
        pTxD->PlcpSignal = PlcpSignal[Rate];
 
605
        if (((Rate == RATE_2) || (Rate == RATE_5_5) || (Rate == RATE_11)) && (TxPreamble == Rt802_11PreambleShort)) // no short preamble for RATE_1
 
606
        {
 
607
                pTxD->PlcpSignal |= 0x0008;
 
608
        }
 
609
 
 
610
        // fill up PLCP SERVICE field, not used for OFDM rates
 
611
        pTxD->PlcpService = Service;
 
612
 
 
613
        // file up PLCP LENGTH_LOW and LENGTH_HIGH fields
 
614
        if (Rate < RATE_FIRST_OFDM_RATE)    // 11b - RATE_1, RATE_2, RATE_5_5, RATE_11
 
615
        {
 
616
                if ((Rate == RATE_1) || ( Rate == RATE_2))
 
617
                {
 
618
                        PLCPLength = PLCPLength * 8 / (Rate + 1);
 
619
                }
 
620
                else
 
621
                {
 
622
                        Residual = ((PLCPLength * 16) % (11 * (1 + Rate - RATE_5_5)));
 
623
                        PLCPLength = PLCPLength * 16 / (11 * (1 + Rate - RATE_5_5));
 
624
                        if (Residual != 0)
 
625
                        {
 
626
                                PLCPLength++;
 
627
                        }
 
628
                        if (Rate == RATE_11)
 
629
                        {
 
630
                        if ((Residual <= (3 * (1 + Rate - RATE_5_5))) && (Residual != 0))
 
631
                        {
 
632
                                pTxD->PlcpService |= 0x80; // 11b's PLCP Length extension bit
 
633
                        }
 
634
                        }
 
635
                }
 
636
 
 
637
                pTxD->PlcpLengthHigh = PLCPLength / 256;
 
638
                pTxD->PlcpLengthLow = PLCPLength % 256;
 
639
        }
 
640
        else    // OFDM - RATE_6, RATE_9, RATE_12, RATE_18, RATE_24, RATE_36, RATE_48, RATE_54
 
641
        {
 
642
                pTxD->PlcpLengthHigh = PLCPLength / 64;  // high 6-bit of total byte count
 
643
                pTxD->PlcpLengthLow = PLCPLength % 64;   // low 6-bit of total byte count
 
644
        }
 
645
}
 
646
 
 
647
/*
 
648
        ========================================================================
 
649
        
 
650
        Routine Description:
 
651
                Calculates the duration which is required to transmit out frames 
 
652
        with given size and specified rate.
 
653
                
 
654
        Arguments:
 
655
                pTxD            Pointer to transmit descriptor
 
656
                Ack                     Setting for Ack requirement bit
 
657
                Fragment        Setting for Fragment bit
 
658
                RetryMode       Setting for retry mode
 
659
                Ifs                     Setting for IFS gap
 
660
                Rate            Setting for transmit rate
 
661
                Service         Setting for service
 
662
                Length          Frame length
 
663
                
 
664
        Return Value:
 
665
                None
 
666
                
 
667
        ========================================================================
 
668
*/
 
669
VOID    RTUSBWriteBeaconDescriptor(
 
670
        IN      PTXD_STRUC      pTxD,
 
671
        IN      UINT            Length,
 
672
        IN      UINT            PLCPLength,
 
673
        IN      UINT            Rate,
 
674
        IN      UCHAR           Service,
 
675
        IN  USHORT      TxPreamble)
 
676
{
 
677
        UINT    Residual;
 
678
 
 
679
        pTxD->RetryLimit        = 0;
 
680
        pTxD->MoreFrag    = 0;
 
681
        pTxD->ACK         = 0;
 
682
        pTxD->Timestamp   = 1;
 
683
        pTxD->newseq      = 1;
 
684
        pTxD->IFS         = IFS_NEW_BACKOFF;
 
685
        pTxD->DataByteCnt = Length;
 
686
        pTxD->Cipher      = 0;
 
687
        pTxD->KeyID               = 0;
 
688
        pTxD->CWmin       = BEACON_CW_IN_BITS;   // 2^5-1 = 31
 
689
        pTxD->CWmax       = BEACON_CW_IN_BITS;  // 2^10 -1 = 1023
 
690
        pTxD->Aifs        = 2;   // TC0: SIFS + 2*Slot + Random(CWmin,CWmax)*Slot
 
691
                
 
692
        if (Rate < RATE_FIRST_OFDM_RATE)
 
693
                pTxD->Ofdm = 0;
 
694
        else
 
695
                pTxD->Ofdm = 1;
 
696
 
 
697
        // fill up PLCP SIGNAL field
 
698
        pTxD->PlcpSignal = PlcpSignal[Rate];
 
699
        if (((Rate == RATE_2) || (Rate == RATE_5_5) || (Rate == RATE_11)) && (TxPreamble == Rt802_11PreambleShort)) // no short preamble for RATE_1
 
700
        {
 
701
                pTxD->PlcpSignal |= 0x0008;
 
702
        }
 
703
 
 
704
        // fill up PLCP SERVICE field, not used for OFDM rates
 
705
        pTxD->PlcpService = Service;
 
706
 
 
707
        // file up PLCP LENGTH_LOW and LENGTH_HIGH fields
 
708
        if (Rate < RATE_FIRST_OFDM_RATE)    // 11b - RATE_1, RATE_2, RATE_5_5, RATE_11
 
709
        {
 
710
                if ((Rate == RATE_1) || ( Rate == RATE_2))
 
711
                {
 
712
                        PLCPLength = PLCPLength * 8 / (Rate + 1);
 
713
                }
 
714
                else
 
715
                {
 
716
                        Residual = ((PLCPLength * 16) % (11 * (1 + Rate - RATE_5_5)));
 
717
                        PLCPLength = PLCPLength * 16 / (11 * (1 + Rate - RATE_5_5));
 
718
                        if (Residual != 0)
 
719
                        {
 
720
                                PLCPLength++;
 
721
                        }
 
722
                        if ((Residual <= (3 * (1 + Rate - RATE_5_5))) && (Residual != 0))
 
723
                        {
 
724
                                pTxD->PlcpService |= 0x80; // 11b's PLCP Length extension bit
 
725
                        }
 
726
                }
 
727
 
 
728
                pTxD->PlcpLengthHigh = PLCPLength / 256;
 
729
                pTxD->PlcpLengthLow = PLCPLength % 256;
 
730
        }
 
731
        else    // OFDM - RATE_6, RATE_9, RATE_12, RATE_18, RATE_24, RATE_36, RATE_48, RATE_54
 
732
        {
 
733
                pTxD->PlcpLengthHigh = PLCPLength / 64;  // high 6-bit of total byte count
 
734
                pTxD->PlcpLengthLow = PLCPLength % 64;   // low 6-bit of total byte count
 
735
        }
 
736
}
 
737
 
 
738
/*
 
739
        ========================================================================
 
740
 
 
741
        Routine Description:
 
742
                Copy frame from waiting queue into relative ring buffer and set 
 
743
        appropriate ASIC register to kick hardware encryption before really
 
744
        sent out to air.
 
745
                
 
746
        Arguments:
 
747
                pAdapter                Pointer to our adapter
 
748
                PNDIS_PACKET    Pointer to outgoing Ndis frame
 
749
                NumberOfFrag    Number of fragment required
 
750
                
 
751
        Return Value:
 
752
                None
 
753
 
 
754
        IRQL = DISPATCH_LEVEL
 
755
        
 
756
        Note:
 
757
        
 
758
        ========================================================================
 
759
*/
 
760
NDIS_STATUS     RTUSBHardEncrypt(
 
761
        IN      PRT2570ADAPTER  pAdapter,
 
762
        IN  struct sk_buff  *skb,
 
763
        IN      UCHAR                   NumberRequired,
 
764
        IN      ULONG                   EnableTxBurst)
 
765
{
 
766
        PVOID                   pVirtualAddress;
 
767
        UINT                    NdisBufferLength;
 
768
        UINT                    BytesCopied;
 
769
        UINT                    TxSize, PLCPLength;
 
770
        UINT                    FreeFragSize;
 
771
        UINT                    RemainSize;
 
772
        USHORT                  Protocol;
 
773
        UCHAR                   FrameGap;
 
774
        HEADER_802_11   Header_802_11;
 
775
        PUCHAR                  pDest;
 
776
        PUCHAR                  pSrc;
 
777
        PUCHAR                  pEncap = NULL;
 
778
        PTX_CONTEXT             pTxContext;
 
779
        PTXD_STRUC              pTxD;
 
780
        BOOLEAN                 StartOfFrame;
 
781
        BOOLEAN                 EAPOLFrame;
 
782
        BOOLEAN                 Encapped;
 
783
        ULONG                   Iv16;
 
784
        ULONG                   Iv32;
 
785
        BOOLEAN                 MICFrag;
 
786
        PWPA_KEY                pWpaKey = (PWPA_KEY) NULL;
 
787
        BOOLEAN                 Cipher;
 
788
        UCHAR                   KeyID = 0;
 
789
        ULONG                   TransferBufferLength;
 
790
        BOOLEAN                 MoreFragment;
 
791
    UCHAR           AckRate = RATE_2;
 
792
    USHORT          AckDuration = 0;
 
793
    USHORT          EncryptionOverhead = 0;
 
794
        BOOLEAN                 Bcast_8023;
 
795
        BOOLEAN                 SingleFrag;
 
796
//for re-calculating the number of Fragment required.
 
797
        UINT                    AllowFragSize;
 
798
        UCHAR                   NumberOfFrag;
 
799
        UINT                    TotalPacketLength; 
 
800
        // To indicate cipher used for this packet
 
801
        NDIS_802_11_ENCRYPTION_STATUS   CipherSuite;
 
802
        
 
803
        CipherSuite = pAdapter->PortCfg.WepStatus;
 
804
        if (EnableTxBurst == 1)
 
805
                FrameGap = IFS_SIFS;
 
806
        else
 
807
                FrameGap = IFS_BACKOFF;         // Default frame gap mode
 
808
        // Sequence Number is identical for all fragments belonged to the same frame
 
809
        // Sequence is 0 - 4095
 
810
        pAdapter->Sequence = ((pAdapter->Sequence) + 1) & (MAX_SEQ_NUMBER);
 
811
        AckRate = pAdapter->PortCfg.ExpectedACKRate[pAdapter->PortCfg.TxRate];
 
812
        AckDuration = RTUSBCalcDuration(pAdapter, AckRate, 14);
 
813
 
 
814
        pVirtualAddress = skb->data;
 
815
        NdisBufferLength = skb->len;
 
816
        if(pVirtualAddress == NULL)
 
817
        {
 
818
                DBGPRINT(RT_DEBUG_ERROR, "Error, Null skb data buffer!!!\n");
 
819
                return (NDIS_STATUS_FAILURE);
 
820
        }
 
821
 
 
822
        if (pAdapter->PortCfg.BssType == BSS_MONITOR && pAdapter->PortCfg.MallowRFMONTx == TRUE)
 
823
        {
 
824
                pTxContext  = &pAdapter->TxContext[pAdapter->NextTxIndex];
 
825
                pTxContext->InUse   = TRUE;
 
826
                pTxContext->LastOne = TRUE;
 
827
                
 
828
                pAdapter->NextTxIndex++;
 
829
                if (pAdapter->NextTxIndex >= TX_RING_SIZE)
 
830
                        pAdapter->NextTxIndex = 0;
 
831
 
 
832
                pTxD  = &(pTxContext->TransferBuffer->TxDesc);
 
833
                memset(pTxD, 0, sizeof(TXD_STRUC));
 
834
                pDest = pTxContext->TransferBuffer->WirelessPacket;
 
835
 
 
836
                memcpy( pDest, skb->data, skb->len );
 
837
 
 
838
                RTUSBWriteTxDescriptor(pTxD, FALSE, 0, FALSE, FALSE, TRUE, IFS_BACKOFF, skb->len, FALSE, 0, CW_MIN_IN_BITS, CW_MAX_IN_BITS, skb->len + 4, pAdapter->PortCfg.TxRate, 4, pAdapter->PortCfg.TxPreambleInUsed);
 
839
 
 
840
                TransferBufferLength = skb->len + sizeof(TXD_STRUC);
 
841
                if ((TransferBufferLength % 2) == 1)
 
842
                        TransferBufferLength++;
 
843
 
 
844
                pTxContext->BulkOutSize = TransferBufferLength;
 
845
                atomic_inc(&pAdapter->TxCount);
 
846
                RTUSB_SET_BULK_FLAG(pAdapter, fRTUSB_BULK_OUT_DATA_FRAG);
 
847
                RTUSBFreeSkbBuffer(skb);
 
848
                return (NDIS_STATUS_SUCCESS);
 
849
        }
 
850
 
 
851
        if (NdisBufferLength < 14)
 
852
        {
 
853
                DBGPRINT_RAW(RT_DEBUG_ERROR, "RTUSBHardEncrypt --> Ndis Packet buffer error !!!\n");
 
854
                return (NDIS_STATUS_FAILURE);
 
855
        }
 
856
        if ((*((PUCHAR) pVirtualAddress) & 0x01) != 0)  // Multicast or Broadcast
 
857
        {
 
858
                INC_COUNTER(pAdapter->WlanCounters.MulticastTransmittedFrameCount);
 
859
                Bcast_8023 = TRUE;
 
860
        }
 
861
        else
 
862
        {
 
863
                Bcast_8023 = FALSE;
 
864
        }
 
865
 
 
866
        // New control flag for sending DHCP & BOOTP usinf 1MB rate
 
867
        if ((NumberRequired - RTUSB_GET_PACKET_RTS(skb)) == 1)
 
868
        {
 
869
                SingleFrag = TRUE;
 
870
        }
 
871
        else
 
872
        {
 
873
                SingleFrag = FALSE;
 
874
        }
 
875
 
 
876
        // Add 802.11x protocol check.
 
877
        // For non-WPA network, 802.11x message should not encrypt even
 
878
        // the privacy is on.
 
879
        if ((memcmp(EAPOL, ((PUCHAR) pVirtualAddress) + 12, 2) == 0))
 
880
        {
 
881
                EAPOLFrame = TRUE;
 
882
                if (pAdapter->PortCfg.MicErrCnt >= 2)//steven:???
 
883
                        pAdapter->PortCfg.MicErrCnt++;
 
884
        }
 
885
        else
 
886
        {
 
887
                EAPOLFrame = FALSE;
 
888
        }       // Initialize 802.11 header for each frame
 
889
 
 
890
        // WPA 802.1x secured port control
 
891
        if (((pAdapter->PortCfg.AuthMode == Ndis802_11AuthModeWPA) || 
 
892
                 (pAdapter->PortCfg.AuthMode == Ndis802_11AuthModeWPAPSK)) &&
 
893
                ((pAdapter->PortCfg.PortSecured == WPA_802_1X_PORT_NOT_SECURED) || 
 
894
                 (pAdapter->PortCfg.MicErrCnt >= 2)) &&
 
895
                (EAPOLFrame == FALSE))
 
896
        {
 
897
                DBGPRINT_RAW(RT_DEBUG_INFO, "RTUSBHardEncrypt --> Drop packet before port secured !!!\n");
 
898
                return (NDIS_STATUS_FAILURE);
 
899
        }               
 
900
        
 
901
        memset(&Header_802_11, 0, sizeof(HEADER_802_11));
 
902
        //
 
903
        // Start making 802.11 frame header
 
904
        //
 
905
        if (INFRA_ON(pAdapter))
 
906
        {
 
907
                // In BSS mode, AP's address(BSSID) is the destination address of all outgoing packets
 
908
                // Address 1 - BSSID
 
909
                memcpy(&Header_802_11.Controlhead.Addr1, &pAdapter->PortCfg.Bssid, ETH_LENGTH_OF_ADDRESS);
 
910
                // Address 3 - DA
 
911
                memcpy(&Header_802_11.Addr3, (PUCHAR) pVirtualAddress, ETH_LENGTH_OF_ADDRESS);
 
912
                Header_802_11.Controlhead.Frame.ToDs = 1;
 
913
        }
 
914
        else 
 
915
        {
 
916
                // Address 1 - DA
 
917
                memcpy(&Header_802_11.Controlhead.Addr1, (PUCHAR) pVirtualAddress, ETH_LENGTH_OF_ADDRESS);
 
918
                // Address 3 - BSSID
 
919
                memcpy(&Header_802_11.Addr3, &pAdapter->PortCfg.Bssid, ETH_LENGTH_OF_ADDRESS);
 
920
        }
 
921
        // Address 2 - SA in both infrastructure & ad-hoc modes
 
922
        memcpy(&Header_802_11.Controlhead.Addr2, pAdapter->CurrentAddress, ETH_LENGTH_OF_ADDRESS);
 
923
        
 
924
//      Header_802_11.Sequence = pAdapter->Sequence;            // Sequence number
 
925
        Header_802_11.Controlhead.Frame.Type = BTYPE_DATA;      // Frame type
 
926
        Header_802_11.Controlhead.Frame.PwrMgt = (pAdapter->PortCfg.Psm == PWR_SAVE);
 
927
        
 
928
        // For the purpose to calculate duration for the second last fragment
 
929
        RemainSize = skb->data_len - LENGTH_802_3 + LENGTH_CRC;
 
930
 
 
931
        MICFrag = FALSE;        // Flag to indicate MIC shall spread into two MPDUs
 
932
        Encapped = FALSE;
 
933
        pEncap = NULL;
 
934
        pSrc = (PUCHAR)pVirtualAddress;
 
935
        Protocol = *(pSrc + 12) * 256 + *(pSrc + 13);
 
936
 
 
937
        if (Protocol > 1500)    // CHeck for LLC encaped
 
938
        {
 
939
                //
 
940
                // Large than 1500 means it's a type field, and thus a D/I/X packet.
 
941
                //
 
942
                pEncap = SNAP_802_1H;
 
943
                Encapped = TRUE;
 
944
                if ((memcmp(IPX, pSrc + 12, 2) == 0) || 
 
945
                    (memcmp(APPLE_TALK, pSrc + 12, 2) == 0))
 
946
                {
 
947
                        pEncap = SNAP_BRIDGE_TUNNEL;
 
948
                }
 
949
        }
 
950
        else
 
951
        {
 
952
                //
 
953
                //means it's a length field, thus an 802.3 packet
 
954
                //And we need to re-calculate the number of Fragment required.
 
955
                TotalPacketLength = skb->data_len;
 
956
                //
 
957
                //means it's a length field, thus an 802.3 packet
 
958
                //And we need to re-calculate the number of Fragment required.
 
959
                //
 
960
                // For TKIP, MIC value is treated as payload, it might be fragmented through
 
961
                // different MPDUs.
 
962
                if (pAdapter->PortCfg.GroupCipher == Ndis802_11Encryption2Enabled)
 
963
                {
 
964
                        TotalPacketLength = skb->data_len + 8;
 
965
                }
 
966
                
 
967
                // Check for payload allowed for each fragment
 
968
                AllowFragSize = (pAdapter->PortCfg.FragmentThreshold) - LENGTH_802_11 - LENGTH_CRC;
 
969
 
 
970
                // Calculate fragments required
 
971
                NumberOfFrag = ((TotalPacketLength - LENGTH_802_3) / AllowFragSize) + 1;
 
972
                // Minus 1 if the size just match to allowable fragment size
 
973
                if (((skb->data_len - LENGTH_802_3) % AllowFragSize) == 0)
 
974
                {
 
975
                        NumberOfFrag--;
 
976
                }
 
977
 
 
978
                
 
979
                if (NumberOfFrag != RTUSB_GET_PACKET_FRAGMENTS(skb))
 
980
                {
 
981
                        DBGPRINT(RT_DEBUG_TRACE, "Original fragment required = %d, new fragment required = %d\n",
 
982
                                                RTUSB_GET_PACKET_FRAGMENTS(skb), NumberOfFrag); 
 
983
                        //
 
984
                        // Update number of Fragment
 
985
                        //
 
986
                        RTUSB_SET_PACKET_FRAGMENTS(skb, NumberOfFrag);
 
987
                        NumberRequired = RTUSB_GET_PACKET_FRAGMENTS(skb) + RTUSB_GET_PACKET_RTS(skb);   
 
988
                }               
 
989
        }
 
990
 
 
991
        //
 
992
    // calcuate the overhead bytes that encryption algorithm may add. This
 
993
    // affects the calculate of "duration" field
 
994
    //
 
995
        if ((CipherSuite == Ndis802_11Encryption1Enabled) && 
 
996
                (pAdapter->PortCfg.SharedKey[pAdapter->PortCfg.DefaultKeyId].KeyLen != 0))
 
997
                EncryptionOverhead = 8;     // WEP: IV + ICV                    
 
998
        else if (CipherSuite == Ndis802_11Encryption2Enabled)
 
999
                EncryptionOverhead = 12;    // TKIP: IV + EIV + ICV, MIC already added to TotalPacketLength
 
1000
        else if (CipherSuite == Ndis802_11Encryption3Enabled)
 
1001
                EncryptionOverhead = 16;    // AES: IV + EIV + Hardware MIC
 
1002
        else
 
1003
            EncryptionOverhead = 0;
 
1004
 
 
1005
        //
 
1006
        // Make RTS frame if required
 
1007
        //
 
1008
        if (RTUSB_GET_PACKET_RTS(skb))
 
1009
        {
 
1010
                PCONTROL_HEADER         pControlHeader;
 
1011
                ULONG                           NextFragSize;
 
1012
                //UINT                          RTSFrameSize; //used only to calculate duration
 
1013
                
 
1014
                DBGPRINT_RAW(RT_DEBUG_INFO, "Making RTS Frame\n");
 
1015
 
 
1016
                pTxContext  = &pAdapter->TxContext[pAdapter->NextTxIndex];
 
1017
                pTxContext->InUse   = TRUE;
 
1018
                pTxContext->LastOne = FALSE;
 
1019
                
 
1020
                // Increase & maintain Tx Ring Index
 
1021
                pAdapter->NextTxIndex++;
 
1022
                if (pAdapter->NextTxIndex >= TX_RING_SIZE)
 
1023
                {
 
1024
                        pAdapter->NextTxIndex = 0;
 
1025
                }
 
1026
 
 
1027
                pTxD  = &(pTxContext->TransferBuffer->TxDesc);
 
1028
                memset(pTxD, 0, sizeof(TXD_STRUC));
 
1029
                pDest = pTxContext->TransferBuffer->WirelessPacket;              
 
1030
                                
 
1031
                pControlHeader = (PCONTROL_HEADER)pDest;
 
1032
                memset(pControlHeader, 0, sizeof(CONTROL_HEADER));
 
1033
 
 
1034
                //FrameControl
 
1035
                pControlHeader->Frame.Type    = BTYPE_CNTL;
 
1036
                if (pAdapter->PortCfg.BGProtectionInUsed == 1)
 
1037
                {
 
1038
                        pControlHeader->Frame.Subtype = SUBTYPE_CTS;
 
1039
                        memcpy(&pControlHeader->Addr1, pAdapter->CurrentAddress, ETH_LENGTH_OF_ADDRESS);
 
1040
                }
 
1041
                else
 
1042
                {
 
1043
                        pControlHeader->Frame.Subtype = SUBTYPE_RTS;
 
1044
                        // RA
 
1045
                        if (INFRA_ON(pAdapter))
 
1046
                        {
 
1047
                                memcpy(&pControlHeader->Addr1, &pAdapter->PortCfg.Bssid, ETH_LENGTH_OF_ADDRESS);
 
1048
                        }
 
1049
                        else
 
1050
                        {
 
1051
                                memcpy(&pControlHeader->Addr1, (PUCHAR) pVirtualAddress, ETH_LENGTH_OF_ADDRESS);
 
1052
                        }
 
1053
                        // TA
 
1054
                        memcpy(&pControlHeader->Addr2, pAdapter->CurrentAddress, ETH_LENGTH_OF_ADDRESS);
 
1055
                }
 
1056
 
 
1057
                // Calculate duration = 2 SIFS + CTS + Data Frame size
 
1058
                if (RTUSB_GET_PACKET_FRAGMENTS(skb) > 1)
 
1059
                {
 
1060
                        // If fragment required, size is maximum fragment size
 
1061
                        NextFragSize = pAdapter->PortCfg.FragmentThreshold;
 
1062
                }
 
1063
                else
 
1064
                {
 
1065
                        // Size should be frame with 802.11 header & CRC
 
1066
                        NextFragSize = skb->data_len + LENGTH_802_11 + LENGTH_CRC - LENGTH_802_3;
 
1067
 
 
1068
                        if (Encapped)
 
1069
                                NextFragSize += LENGTH_802_1_H;
 
1070
                }
 
1071
                pControlHeader->Duration = 2 * (pAdapter->PortCfg.Dsifs)
 
1072
                        + RTUSBCalcDuration(pAdapter, pAdapter->PortCfg.TxRate, NextFragSize + EncryptionOverhead)
 
1073
                        + AckDuration; 
 
1074
 
 
1075
                // Write Tx descriptor
 
1076
                // Don't kick tx start until all frames are prepared
 
1077
                // RTS has to set more fragment bit for fragment burst
 
1078
                // RTS did not encrypt          
 
1079
                if (pAdapter->PortCfg.BGProtectionInUsed == 1)
 
1080
                {
 
1081
                        RTUSBWriteTxDescriptor(pTxD, FALSE, 7, FALSE, FALSE, FALSE, FrameGap, 10, FALSE, 0, CW_MIN_IN_BITS, CW_MAX_IN_BITS, 14, pAdapter->PortCfg.RtsRate, 4, pAdapter->PortCfg.TxPreambleInUsed);
 
1082
                }
 
1083
                else
 
1084
                {
 
1085
                        RTUSBWriteTxDescriptor(pTxD, FALSE, 7, TRUE, FALSE, FALSE, FrameGap, sizeof(CONTROL_HEADER), FALSE, 0, CW_MIN_IN_BITS, CW_MAX_IN_BITS, sizeof(CONTROL_HEADER) + 4, pAdapter->PortCfg.RtsRate, 4, pAdapter->PortCfg.TxPreambleInUsed);
 
1086
//steven:should we need this?                   pTxD->RTS = 1;
 
1087
                }
 
1088
 
 
1089
                TransferBufferLength = sizeof(CONTROL_HEADER) + sizeof(TXD_STRUC);
 
1090
                if ((TransferBufferLength % 2) == 1)
 
1091
                        TransferBufferLength++;
 
1092
 
 
1093
                pTxContext->BulkOutSize = TransferBufferLength;
 
1094
 
 
1095
                NumberRequired--;
 
1096
                //
 
1097
                // Increase BulkOut stanby count.
 
1098
                //
 
1099
                atomic_inc(&pAdapter->TxCount);
 
1100
 
 
1101
                RTUSB_SET_BULK_FLAG(pAdapter, fRTUSB_BULK_OUT_DATA_NORMAL);
 
1102
        }
 
1103
        // Find the WPA key, either Group or Pairwise Key//steven:according to Controlhead.Addr1 (only when AuthMode >= Ndis802_11AuthModeWPA)
 
1104
        if (pAdapter->PortCfg.AuthMode >= Ndis802_11AuthModeWPA)
 
1105
        {
 
1106
                INT     idx;
 
1107
                        
 
1108
                pWpaKey = (PWPA_KEY) NULL;
 
1109
                // First lokup the DA, if it's a group address, use GROUP key
 
1110
                if (Header_802_11.Controlhead.Addr1.Octet[0] & 0x01)
 
1111
                {
 
1112
                        if (pAdapter->PortCfg.GroupKey[pAdapter->PortCfg.DefaultKeyId].KeyLen != 0)
 
1113
                        {
 
1114
                                pWpaKey = (PWPA_KEY) &pAdapter->PortCfg.GroupKey[pAdapter->PortCfg.DefaultKeyId];
 
1115
                                pWpaKey->Type = GROUP_KEY;
 
1116
                                KeyID = pAdapter->PortCfg.DefaultKeyId;//for Tx descriptor
 
1117
                                DBGPRINT(RT_DEBUG_INFO, "Tx Use Group Key\n");
 
1118
                        }
 
1119
                }
 
1120
                // Try to find the Pairwise Key
 
1121
                else
 
1122
                {
 
1123
                        for (idx = 0; idx < PAIRWISE_KEY_NO; idx++)
 
1124
                        {
 
1125
                                if (((memcmp(&Header_802_11.Controlhead.Addr1, pAdapter->PortCfg.PairwiseKey[idx].BssId, 6)== 0)) &&
 
1126
                                        (pAdapter->PortCfg.PairwiseKey[idx].KeyLen != 0))
 
1127
                                {
 
1128
                                        pWpaKey = (PWPA_KEY) &pAdapter->PortCfg.PairwiseKey[idx];
 
1129
                                        pWpaKey->Type = PAIRWISE_KEY;
 
1130
                                        KeyID = (UCHAR)idx;
 
1131
                                        DBGPRINT(RT_DEBUG_INFO, "Tx Use Pairwise Key\n");
 
1132
                                        break;
 
1133
                                }
 
1134
                        }
 
1135
                        // Use default Group Key if there is no Pairwise key present
 
1136
                        if ((pAdapter->PortCfg.GroupKey[pAdapter->PortCfg.DefaultKeyId].KeyLen != 0) && (pWpaKey == NULL))
 
1137
                        {
 
1138
                                pWpaKey = (PWPA_KEY) &pAdapter->PortCfg.GroupKey[pAdapter->PortCfg.DefaultKeyId];
 
1139
                                pWpaKey->Type = GROUP_KEY;
 
1140
                                KeyID = pAdapter->PortCfg.DefaultKeyId;//for Tx descriptor
 
1141
                                DBGPRINT(RT_DEBUG_INFO, "Tx Use Group Key\n");
 
1142
                        }
 
1143
                }
 
1144
        }
 
1145
 
 
1146
                if (pWpaKey != NULL)
 
1147
                {
 
1148
                        INT i;
 
1149
                                
 
1150
                        DBGPRINT(RT_DEBUG_INFO, "RTMPHardEncrypt TKIP Key = ");
 
1151
                        for (i = 0; i < 16; i++)
 
1152
                        {
 
1153
                                DBGPRINT_RAW(RT_DEBUG_INFO, "%02x:", pWpaKey->Key[i]);
 
1154
                        }
 
1155
                        DBGPRINT_RAW(RT_DEBUG_INFO, "\n");                                              
 
1156
                        DBGPRINT(RT_DEBUG_INFO, "RTMPHardEncrypt TKIP TxMic = ");
 
1157
                        for (i = 0; i < 8; i++)
 
1158
                        {
 
1159
                                DBGPRINT_RAW(RT_DEBUG_INFO, "%02x:", pWpaKey->TxMic[i]);
 
1160
                        }
 
1161
                        DBGPRINT_RAW(RT_DEBUG_INFO, "\n");                                              
 
1162
                        DBGPRINT(RT_DEBUG_INFO, "RTMPHardEncrypt TKIP TxTsc = ");
 
1163
                        for (i = 0; i < 6; i++)
 
1164
                        {
 
1165
                                DBGPRINT_RAW(RT_DEBUG_INFO, "%02x:", pWpaKey->TxTsc[i]);
 
1166
                        }
 
1167
                        DBGPRINT_RAW(RT_DEBUG_INFO, "\n");                                              
 
1168
                }
 
1169
 
 
1170
        StartOfFrame = TRUE;
 
1171
        // Start Copy Ndis Packet into Ring buffer.
 
1172
        // For frame required more than one ring buffer (fragment), all ring buffers
 
1173
        // have to be filled before kicking start tx bit.
 
1174
        do
 
1175
        {
 
1176
//              NdisAcquireSpinLock(&pAdapter->TxRingLock);
 
1177
                // Get the Tx Ring descriptor & Dma Buffer address
 
1178
                pTxContext  = &pAdapter->TxContext[pAdapter->NextTxIndex];
 
1179
                pTxContext->InUse   = TRUE;
 
1180
                pTxContext->LastOne = FALSE;
 
1181
                
 
1182
                // Increase & maintain Tx Ring Index
 
1183
                pAdapter->NextTxIndex++;
 
1184
                if (pAdapter->NextTxIndex >= TX_RING_SIZE)
 
1185
                {
 
1186
                        pAdapter->NextTxIndex = 0;
 
1187
                }
 
1188
//              NdisReleaseSpinLock(&pAdapter->TxRingLock);
 
1189
 
 
1190
                pTxD  = &(pTxContext->TransferBuffer->TxDesc);
 
1191
                memset(pTxD, 0, sizeof(TXD_STRUC));
 
1192
                pDest = pTxContext->TransferBuffer->WirelessPacket;              
 
1193
                // Maximum allowable payload with one ring buffer, bound by fragment size
 
1194
                FreeFragSize = pAdapter->PortCfg.FragmentThreshold - LENGTH_CRC;
 
1195
                
 
1196
                // Make fragment number & more fragment bit of 802.11 header
 
1197
                if (StartOfFrame == TRUE)
 
1198
                {
 
1199
                        Header_802_11.Frag = 0;                 // Start of fragment burst / Single Frame
 
1200
                }
 
1201
                else
 
1202
                {
 
1203
                        Header_802_11.Frag++;                   // Rest of fragmented frames.
 
1204
                }
 
1205
                
 
1206
                // Turn on with no frames after this one
 
1207
                if (NumberRequired > 1)
 
1208
                {
 
1209
                    ULONG NextFragSize;
 
1210
//                  ULONG FragSize;
 
1211
                    
 
1212
                        Header_802_11.Controlhead.Frame.MoreFrag = 1;
 
1213
                        MoreFragment = TRUE;
 
1214
                        
 
1215
                        if (NumberRequired == 2)
 
1216
                        NextFragSize = RemainSize - pAdapter->PortCfg.FragmentThreshold + LENGTH_802_11 + LENGTH_802_11 + LENGTH_CRC;
 
1217
                        else
 
1218
                            NextFragSize = pAdapter->PortCfg.FragmentThreshold;
 
1219
                        
 
1220
                        Header_802_11.Controlhead.Duration = 3 * pAdapter->PortCfg.Dsifs
 
1221
                                + 2 * AckDuration
 
1222
                                + RTUSBCalcDuration(pAdapter, pAdapter->PortCfg.TxRate, NextFragSize + EncryptionOverhead);
 
1223
                }
 
1224
                else
 
1225
                {
 
1226
                        Header_802_11.Controlhead.Frame.MoreFrag = 0;
 
1227
                        MoreFragment = FALSE;
 
1228
                        
 
1229
                        if (Header_802_11.Controlhead.Addr1.Octet[0] & 0x01)
 
1230
                        {
 
1231
                                // No ACK expected for multicast frame          
 
1232
                                Header_802_11.Controlhead.Duration = 0;
 
1233
                        }
 
1234
                        else
 
1235
                        {
 
1236
                                // ACK size is 14 include CRC, and its rate is 2Mb
 
1237
                                Header_802_11.Controlhead.Duration = pAdapter->PortCfg.Dsifs + AckDuration;
 
1238
                        }
 
1239
                }
 
1240
 
 
1241
                // Check for WEP enable bit and prepare for software WEP
 
1242
                if ((CipherSuite == Ndis802_11Encryption1Enabled) && (EAPOLFrame == FALSE) &&
 
1243
                        (pAdapter->PortCfg.SharedKey[pAdapter->PortCfg.DefaultKeyId].KeyLen != 0))
 
1244
                {
 
1245
                        Header_802_11.Controlhead.Frame.Wep = 1;
 
1246
                        Cipher = TRUE;
 
1247
                }
 
1248
                else if ((CipherSuite == Ndis802_11Encryption2Enabled) && (pWpaKey != NULL))
 
1249
                {
 
1250
                        Header_802_11.Controlhead.Frame.Wep = 1;
 
1251
                        Cipher = TRUE;
 
1252
                }
 
1253
                else if ((CipherSuite == Ndis802_11Encryption3Enabled) && (pWpaKey != NULL))
 
1254
                {
 
1255
                        Header_802_11.Controlhead.Frame.Wep = 1;
 
1256
                        Cipher = TRUE;
 
1257
                }
 
1258
                else
 
1259
                {
 
1260
                        Header_802_11.Controlhead.Frame.Wep = 0;
 
1261
                        Cipher = FALSE;
 
1262
                }
 
1263
                
 
1264
                // Copy 802.11 header to Tx ring buffer
 
1265
                memcpy(pDest, &Header_802_11, sizeof(Header_802_11));
 
1266
                pDest        += sizeof(Header_802_11);
 
1267
                FreeFragSize -= sizeof(Header_802_11);
 
1268
 
 
1269
                if ((CipherSuite == Ndis802_11Encryption1Enabled) && (EAPOLFrame == FALSE) &&
 
1270
                        (pAdapter->PortCfg.SharedKey[pAdapter->PortCfg.DefaultKeyId].KeyLen != 0))
 
1271
                {
 
1272
                        // Prepare IV, IV offset, Key for Hardware encryption
 
1273
                        RTMPInitWepEngine(
 
1274
                                pAdapter,
 
1275
                                pAdapter->PortCfg.SharedKey[pAdapter->PortCfg.DefaultKeyId].Key,
 
1276
                                pAdapter->PortCfg.DefaultKeyId,
 
1277
                                pAdapter->PortCfg.SharedKey[pAdapter->PortCfg.DefaultKeyId].KeyLen,
 
1278
                                (PUCHAR) &pTxD->Iv);
 
1279
                        KeyID = pAdapter->PortCfg.DefaultKeyId;
 
1280
                        // Set Iv offset in TxD
 
1281
                        pTxD->IvOffset = LENGTH_802_11;
 
1282
 
 
1283
                        memcpy(pDest, &pTxD->Iv, 4);
 
1284
                        pDest += 4;
 
1285
                }
 
1286
                else if ((CipherSuite == Ndis802_11Encryption2Enabled) && (pWpaKey != NULL))
 
1287
                {
 
1288
                        INT             i;
 
1289
 
 
1290
                        i = 0;
 
1291
                        // Prepare IV, EIV, IV offset, Key for hardware encryption
 
1292
                        RTMPInitTkipEngine(
 
1293
                                pAdapter,
 
1294
                                pWpaKey->Key,
 
1295
                                pAdapter->PortCfg.DefaultKeyId,         // This might cause problem when using peer key
 
1296
                                Header_802_11.Controlhead.Addr2.Octet,
 
1297
                                pWpaKey->TxMic,
 
1298
                                pWpaKey->TxTsc,
 
1299
                                0,
 
1300
                                &Iv16,
 
1301
                                &Iv32,
 
1302
                                pDest);
 
1303
 
 
1304
                        // Increase TxTsc value for next transmission
 
1305
                        while (++pWpaKey->TxTsc[i] == 0x0)
 
1306
                        {
 
1307
                                i++;
 
1308
                                if (i == 6)
 
1309
                                        break;
 
1310
                        }
 
1311
                        if (i == 6)
 
1312
                        {
 
1313
                                // TODO: TSC has done one full cycle, do re-keying stuff follow specs
 
1314
                                // Should send a special event microsoft defined to request re-key
 
1315
                        }
 
1316
                        
 
1317
                        // Copy IV
 
1318
                        memcpy(&pTxD->Iv, &Iv16, 4);
 
1319
                        
 
1320
                        // Copy EIV
 
1321
                        memcpy(&pTxD->Eiv, &Iv32, 4);
 
1322
                        
 
1323
                        // Set IV offset
 
1324
                        pTxD->IvOffset = LENGTH_802_11;
 
1325
 
 
1326
                        memcpy(pDest, &Iv16, 4);
 
1327
                        pDest += 4;
 
1328
                        memcpy(pDest, &Iv32, 4);
 
1329
                        pDest += 4;
 
1330
                        
 
1331
                }
 
1332
                else if ((CipherSuite == Ndis802_11Encryption3Enabled) && (pWpaKey != NULL))
 
1333
                {
 
1334
                        INT             i;
 
1335
                        PUCHAR  pTmp;
 
1336
 
 
1337
                        i = 0;
 
1338
                        pTmp = (PUCHAR) &Iv16;
 
1339
                        *pTmp       = pWpaKey->TxTsc[0];
 
1340
                        *(pTmp + 1) = pWpaKey->TxTsc[1];
 
1341
                        *(pTmp + 2) = 0;
 
1342
                        *(pTmp + 3) = (pAdapter->PortCfg.DefaultKeyId << 6) | 0x20;
 
1343
                        
 
1344
                        Iv32 = *(PULONG)(&pWpaKey->TxTsc[2]);
 
1345
                        
 
1346
                        // Increase TxTsc value for next transmission
 
1347
                        while (++pWpaKey->TxTsc[i] == 0x0)
 
1348
                        {
 
1349
                                i++;
 
1350
                                if (i == 6)
 
1351
                                        break;
 
1352
                        }
 
1353
                        if (i == 6)
 
1354
                        {
 
1355
                                // TODO: TSC has done one full cycle, do re-keying stuff follow specs
 
1356
                                // Should send a special event microsoft defined to request re-key
 
1357
                        }
 
1358
                        
 
1359
                        // Copy IV
 
1360
                        memcpy(&pTxD->Iv, &Iv16, 4);
 
1361
                        
 
1362
                        // Copy EIV
 
1363
                        memcpy(&pTxD->Eiv, &Iv32, 4);
 
1364
                        
 
1365
                        // Set IV offset
 
1366
                        pTxD->IvOffset = LENGTH_802_11;
 
1367
 
 
1368
                        memcpy(pDest, &Iv16, 4);
 
1369
                        pDest += 4;
 
1370
                        memcpy(pDest, &Iv32, 4);
 
1371
                        pDest += 4;
 
1372
 
 
1373
                }
 
1374
                
 
1375
                //
 
1376
                // Only the first fragment required LLC-SNAP header !!!
 
1377
                //
 
1378
                if ((StartOfFrame == TRUE) && (Encapped == TRUE))
 
1379
                {
 
1380
                        // For WEP & no encryption required frame, just copy LLC header into buffer,
 
1381
                        // Hardware will do the encryption job.
 
1382
                        // For TKIP, we have to calculate MIC and store it first
 
1383
                        if ((CipherSuite == Ndis802_11Encryption2Enabled) && (pWpaKey != NULL))
 
1384
                        {
 
1385
                                // Calculate MSDU MIC Value
 
1386
                                RTMPCalculateMICValue(pAdapter, skb, pEncap, 6, pWpaKey);
 
1387
                        }
 
1388
                        // For WEP & no encryption required frame, just copy LLC header into buffer,
 
1389
                        // Hardware will do the encryption job.
 
1390
                        // For TKIP, we have to calculate MIC and store it first
 
1391
                        
 
1392
                        // Copy LLC header
 
1393
                        memcpy(pDest, pEncap, 6);
 
1394
                        pDest += 6;
 
1395
 
 
1396
                        // Copy protocol type
 
1397
                        pSrc = (PUCHAR) pVirtualAddress;
 
1398
                        memcpy(pDest, pSrc + 12, 2);
 
1399
                        pDest += 2;
 
1400
                        
 
1401
                        // Exclude 802.3 header size, we will recalculate the size at
 
1402
                        // the end of fragment preparation.
 
1403
                        NdisBufferLength -= LENGTH_802_3;
 
1404
                        pSrc += LENGTH_802_3;
 
1405
                        FreeFragSize -= LENGTH_802_1_H;
 
1406
                }
 
1407
                else if ((StartOfFrame == TRUE) && (Encapped == FALSE))
 
1408
                {
 
1409
                        if ((pAdapter->PortCfg.WepStatus == Ndis802_11Encryption2Enabled) && (pWpaKey != NULL))
 
1410
                        {
 
1411
                                // Calculate MSDU MIC Value
 
1412
                                RTMPCalculateMICValue(pAdapter, skb, pEncap, 0, pWpaKey);
 
1413
                        }
 
1414
                        
 
1415
                        pSrc = (PUCHAR) pVirtualAddress + LENGTH_802_3;
 
1416
                        NdisBufferLength -= LENGTH_802_3;
 
1417
                }
 
1418
                
 
1419
                // Start copying payload
 
1420
                BytesCopied = 0;
 
1421
                do
 
1422
                {
 
1423
                        if (NdisBufferLength >= FreeFragSize)
 
1424
                        {
 
1425
                                // Copy only the free fragment size, and save the pointer
 
1426
                                // of current buffer descriptor for next fragment buffer.
 
1427
                                memcpy(pDest, pSrc, FreeFragSize);
 
1428
                                BytesCopied += FreeFragSize;
 
1429
                                pSrc        += FreeFragSize;
 
1430
                                pDest       += FreeFragSize;
 
1431
                                NdisBufferLength      -= FreeFragSize;
 
1432
                                break;
 
1433
                        }
 
1434
                        else
 
1435
                        {
 
1436
                                // Copy the rest of this buffer descriptor pointed data
 
1437
                                // into ring buffer.
 
1438
                                memcpy(pDest, pSrc, NdisBufferLength);
 
1439
                                BytesCopied  += NdisBufferLength;
 
1440
                                pDest        += NdisBufferLength;
 
1441
                                FreeFragSize -= NdisBufferLength;
 
1442
                        }
 
1443
                                // No more buffer descriptor
 
1444
                                // Add MIC value if needed
 
1445
                                if ((CipherSuite == Ndis802_11Encryption2Enabled) && 
 
1446
                                        (MICFrag == FALSE) &&
 
1447
                                        (pWpaKey != NULL))
 
1448
                                {
 
1449
                                        INT i;
 
1450
 
 
1451
                                        NdisBufferLength = 8;           // Set length to MIC length
 
1452
                                        DBGPRINT(RT_DEBUG_INFO, "Calculated TX MIC value = ");  
 
1453
                                        for (i = 0; i < 8; i++)
 
1454
                                        {
 
1455
                                                DBGPRINT_RAW(RT_DEBUG_INFO, "%02x:", pAdapter->PrivateInfo.Tx.MIC[i]);  
 
1456
                                        }
 
1457
                                        DBGPRINT_RAW(RT_DEBUG_INFO, "\n"); 
 
1458
                                                                
 
1459
                                        if (FreeFragSize >= NdisBufferLength)
 
1460
                                        {
 
1461
                                                memcpy(pDest, pAdapter->PrivateInfo.Tx.MIC, NdisBufferLength);
 
1462
                                                BytesCopied  += NdisBufferLength;
 
1463
                                                pDest            += NdisBufferLength;
 
1464
                                                FreeFragSize -= NdisBufferLength;
 
1465
                                                NdisBufferLength = 0;
 
1466
                                                RemainSize   += 8;      // Need to add MIC as payload
 
1467
                                        }
 
1468
                                        else
 
1469
                                        {
 
1470
                                                memcpy(pDest, pAdapter->PrivateInfo.Tx.MIC, FreeFragSize);
 
1471
                                                BytesCopied  += FreeFragSize;
 
1472
                                                pSrc              = pAdapter->PrivateInfo.Tx.MIC + FreeFragSize;
 
1473
                                                pDest            += FreeFragSize;
 
1474
                                                NdisBufferLength                 -= FreeFragSize;
 
1475
                                                MICFrag           = TRUE;
 
1476
                                                RemainSize   += (8 - FreeFragSize);     // Need to add MIC as payload
 
1477
                                        }
 
1478
                                }
 
1479
                }       while (FALSE);          // End of copying payload
 
1480
                                
 
1481
                // Real packet size, No 802.1H header for fragments except the first one.
 
1482
                if ((StartOfFrame == TRUE) && (Encapped == TRUE))
 
1483
                {
 
1484
                        TxSize = BytesCopied + LENGTH_802_11 + LENGTH_802_1_H;
 
1485
                }
 
1486
                else
 
1487
                {
 
1488
                        TxSize = BytesCopied + LENGTH_802_11;
 
1489
                }
 
1490
 
 
1491
                RemainSize = RemainSize - BytesCopied;
 
1492
                        
 
1493
                if ((CipherSuite == Ndis802_11Encryption1Enabled) && (Header_802_11.Controlhead.Frame.Wep == 1))
 
1494
                {
 
1495
                        // IV + ICV which ASIC added after encryption done
 
1496
                        TxSize += 4;
 
1497
                        PLCPLength = TxSize + 8;
 
1498
                }
 
1499
                else if ((CipherSuite == Ndis802_11Encryption2Enabled) && (pWpaKey != NULL))
 
1500
                {
 
1501
                        // IV + EIV + ICV which ASIC added after encryption done
 
1502
                        TxSize += 8;
 
1503
                        PLCPLength = TxSize + 8;
 
1504
                }
 
1505
                else if ((CipherSuite == Ndis802_11Encryption3Enabled) && (pWpaKey != NULL))
 
1506
                {
 
1507
                        // IV + EIV + HW MIC
 
1508
                        TxSize += 8;
 
1509
                        PLCPLength = TxSize + 12;
 
1510
                }
 
1511
                else
 
1512
                {
 
1513
                        PLCPLength = TxSize + 4;
 
1514
                }
 
1515
                DBGPRINT_RAW(RT_DEBUG_INFO, "TxSize = %d, PLCPLength = %d\n", TxSize, PLCPLength);//steven:for debug
 
1516
                                
 
1517
                // Prepare Tx descriptors before kicking tx.
 
1518
                // The BBP register index in Tx descriptor has to be configured too.
 
1519
                if (Header_802_11.Controlhead.Addr1.Octet[0] & 0x01)
 
1520
                {
 
1521
                        INC_COUNTER(pAdapter->WlanCounters.MulticastTransmittedFrameCount);
 
1522
                        // Multicast, retry bit is off
 
1523
                        if (StartOfFrame == TRUE)
 
1524
                        {
 
1525
                                if (RTUSB_GET_PACKET_RTS(skb) != 1)
 
1526
                                        RTUSBWriteTxDescriptor(pTxD, FALSE, 0, FALSE, FALSE, TRUE, FrameGap, TxSize, Cipher, KeyID, CW_MIN_IN_BITS, CW_MAX_IN_BITS, PLCPLength, pAdapter->PortCfg.TxRate, 4, pAdapter->PortCfg.TxPreambleInUsed);
 
1527
                                else
 
1528
                                        RTUSBWriteTxDescriptor(pTxD, FALSE, 0, FALSE, FALSE, TRUE, FrameGap, TxSize, Cipher, KeyID, 0, 0, PLCPLength, pAdapter->PortCfg.TxRate, 4, pAdapter->PortCfg.TxPreambleInUsed);
 
1529
                        }
 
1530
                        else
 
1531
                                RTUSBWriteTxDescriptor(pTxD, FALSE, 0, FALSE, FALSE, FALSE, FrameGap, TxSize, Cipher, KeyID, 0, 0, PLCPLength, pAdapter->PortCfg.TxRate, 4, pAdapter->PortCfg.TxPreambleInUsed);
 
1532
                }
 
1533
                else
 
1534
                {
 
1535
                        if (StartOfFrame == TRUE)
 
1536
                        {
 
1537
                                if (RTUSB_GET_PACKET_RTS(skb) != 1)
 
1538
                                        RTUSBWriteTxDescriptor(pTxD, MoreFragment, 7, TRUE, FALSE, TRUE, FrameGap, TxSize, Cipher, KeyID, CW_MIN_IN_BITS, CW_MAX_IN_BITS, PLCPLength, pAdapter->PortCfg.TxRate, 4, pAdapter->PortCfg.TxPreambleInUsed);
 
1539
                                else
 
1540
                                        RTUSBWriteTxDescriptor(pTxD, MoreFragment, 7, TRUE, FALSE, TRUE, FrameGap, TxSize, Cipher, KeyID, 0, 0, PLCPLength, pAdapter->PortCfg.TxRate, 4, pAdapter->PortCfg.TxPreambleInUsed);
 
1541
                        }
 
1542
                        else
 
1543
                                RTUSBWriteTxDescriptor(pTxD, MoreFragment, 7, TRUE, FALSE, FALSE, FrameGap, TxSize, Cipher, KeyID, 0, 0, PLCPLength, pAdapter->PortCfg.TxRate, 4, pAdapter->PortCfg.TxPreambleInUsed);
 
1544
                }
 
1545
 
 
1546
                TransferBufferLength = TxSize + sizeof(TXD_STRUC);
 
1547
                if ((TransferBufferLength % 2) == 1)//always bulk out even number of bytes
 
1548
                        TransferBufferLength++;
 
1549
                if ((TransferBufferLength % pAdapter->BulkOutMaxPacketSize) == 0)
 
1550
                        TransferBufferLength += 2;
 
1551
 
 
1552
                pTxContext->BulkOutSize = TransferBufferLength;
 
1553
                RTUSB_SET_BULK_FLAG(pAdapter, fRTUSB_BULK_OUT_DATA_NORMAL);
 
1554
                
 
1555
                // Set frame gap for the rest of fragment burst.
 
1556
                // It won't matter if there is only one fragment (single fragment frame).
 
1557
                StartOfFrame = FALSE;
 
1558
                NumberRequired--;
 
1559
                if (NumberRequired == 0)
 
1560
                {
 
1561
                        pTxContext->LastOne = TRUE;
 
1562
                }
 
1563
                else
 
1564
                {
 
1565
                        pTxContext->LastOne = FALSE;
 
1566
                }
 
1567
//steven:use ASIC counters to derive this count instead         INC_COUNTER(pAdapter->WlanCounters.TransmittedFragmentCount);
 
1568
                //
 
1569
                // Increase BulkOut stanby count.
 
1570
                //              
 
1571
                atomic_inc(&pAdapter->TxCount);
 
1572
        }       while (NumberRequired > 0);
 
1573
 
 
1574
#if 0
 
1575
        // Add duplicate 1mb broadcast frames
 
1576
        do
 
1577
        {
 
1578
                if ((pAdapter->PortCfg.TxRate != RATE_1) && (Bcast_8023 == TRUE) && (SingleFrag == TRUE))
 
1579
                {
 
1580
                        PTX_CONTEXT             pTmpContext;
 
1581
                        PTXD_STRUC              pTmpTxD;
 
1582
                        ULONG                   DataOffset = 0;
 
1583
 
 
1584
                        pSrc = pTxContext->TransferBuffer->WirelessPacket;
 
1585
                        //
 
1586
                        // Check the offset of the original 802.3 data packet
 
1587
                        //
 
1588
                        if (CipherSuite == Ndis802_11EncryptionDisabled)
 
1589
                                DataOffset = 0;
 
1590
                        else if (CipherSuite == Ndis802_11Encryption1Enabled)
 
1591
                                DataOffset += 4; //Add IV
 
1592
                        else if (CipherSuite == Ndis802_11Encryption2Enabled)
 
1593
                                DataOffset += 8; //Add EIV
 
1594
                        else if (CipherSuite == Ndis802_11Encryption3Enabled)   
 
1595
                                DataOffset += 8; //Add EIV
 
1596
                                
 
1597
                        // Check for DHCP & BOOTP protocol
 
1598
                        if ((*(pSrc + 0x35 + DataOffset) != 0x44) || (*(pSrc + 0x37 + DataOffset) != 0x43))
 
1599
                        {
 
1600
                                // 
 
1601
                                // 2054 (hex 0806) for ARP datagrams
 
1602
                                // if this packet is not ARP datagrams, then do nothing
 
1603
                                // ARP datagrams will also be duplicate at 1mb broadcast frames
 
1604
                                //
 
1605
                                if (Protocol != 0x0806 )
 
1606
                                        break;
 
1607
                        }
 
1608
 
 
1609
                        // Get the Tx Ring descriptor & Dma Buffer address
 
1610
                        pTmpContext = &pAdapter->TxContext[pAdapter->NextTxIndex];
 
1611
                        pDest = pTmpContext->TransferBuffer->WirelessPacket;
 
1612
                        
 
1613
                        if (pTmpContext->InUse == TRUE)
 
1614
                                break;  //No available Tx Ring for Send 1mb broadcast frames.
 
1615
 
 
1616
                        // Increase & maintain Tx Ring Index
 
1617
                        pAdapter->NextTxIndex++;
 
1618
                        if (pAdapter->NextTxIndex >= TX_RING_SIZE)
 
1619
                        {
 
1620
                                pAdapter->NextTxIndex = 0;
 
1621
                        }
 
1622
                        
 
1623
                        //
 
1624
                        // Reset LastOne Tx Ring descriptor
 
1625
                        //
 
1626
                        pTmpContext->InUse   = TRUE;
 
1627
                        pTmpContext->LastOne = TRUE;
 
1628
 
 
1629
                        pTmpTxD  = &(pTmpContext->TransferBuffer->TxDesc);
 
1630
                        //
 
1631
                        // Duplicate TxD descriptor, and we will reset the its value later.
 
1632
                        //
 
1633
                        memcpy(pTmpTxD, pTxD, sizeof(TXD_STRUC));
 
1634
                        // Start coping data to new ring 
 
1635
                        memcpy(pDest, pSrc, pTxContext->BulkOutSize);
 
1636
                        pTmpContext->BulkOutSize = pTxContext->BulkOutSize;
 
1637
                        RTUSBWriteTxDescriptor(pTmpTxD, FALSE, 7, TRUE, FALSE, FALSE, FrameGap, TxSize, Cipher, KeyID, 0, 0, PLCPLength, RATE_1, 4, pAdapter->PortCfg.TxPreambleInUsed);        
 
1638
                        //
 
1639
                        // Increase BulkOut stanby count.
 
1640
                        //                      
 
1641
                        atomic_inc(&pAdapter->TxCount);
 
1642
                        DBGPRINT(RT_DEBUG_TRACE, "Send 1M broadcast frame!\n");
 
1643
                }
 
1644
        } while (FALSE);
 
1645
#endif
 
1646
 
 
1647
        // Acknowledge protocol send complete of pending packet.
 
1648
        RTUSBFreeSkbBuffer(skb);
 
1649
        return (NDIS_STATUS_SUCCESS);
 
1650
 
 
1651
}
 
1652
 
 
1653
VOID RTUSBRxPacket(unsigned long data)
 
1654
//VOID RTUSBRxPacket(purbb_t pUrb)
 
1655
{
 
1656
        //PRT2570ADAPTER pAdapter = (PRT2570ADAPTER)data;
 
1657
        purbb_t pUrb = (purbb_t)data;
 
1658
        PRT2570ADAPTER pAdapter;
 
1659
        PRX_CONTEXT pRxContext;
 
1660
        PRXD_STRUC              pRxD;
 
1661
        NDIS_STATUS             Status;
 
1662
        PHEADER_802_11  pHeader;
 
1663
        PUCHAR                  pData;
 
1664
        PUCHAR                  pDestMac, pSrcMac;
 
1665
        UCHAR                   KeyIdx;
 
1666
        ULONG                   i;
 
1667
        UINT                    PacketSize = 0;
 
1668
        PUCHAR                  pEncap;
 
1669
        UCHAR                   LLC_Len[2];
 
1670
        UCHAR                   Header802_3[14];
 
1671
        PWPA_KEY                pWpaKey = NULL;
 
1672
        // To indicate cipher used for this packet
 
1673
        NDIS_802_11_ENCRYPTION_STATUS   Cipher;
 
1674
        struct sk_buff  *skb;
 
1675
        PVOID                   pManage;
 
1676
        wlan_ng_prism2_header   *ph;
 
1677
 
 
1678
        pRxContext= (PRX_CONTEXT)pUrb->context;
 
1679
        pAdapter = pRxContext->pAdapter;
 
1680
        
 
1681
        if( RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_REMOVE_IN_PROGRESS) )
 
1682
                return;
 
1683
 
 
1684
        do
 
1685
        {
 
1686
                if (pRxContext->pUrb->actual_length >= (sizeof(RXD_STRUC) + LENGTH_802_11))//blue
 
1687
                {
 
1688
                        pData = pRxContext->TransferBuffer;
 
1689
                        pManage = (PVOID) pData;
 
1690
 
 
1691
                        pRxD = (PRXD_STRUC)(pData + pRxContext->pUrb->actual_length - sizeof(RXD_STRUC));
 
1692
                        pHeader = (PHEADER_802_11)pData;
 
1693
                        
 
1694
 
 
1695
                        if (pRxD->DataByteCnt < 4)
 
1696
                                Status = NDIS_STATUS_FAILURE;
 
1697
                        else
 
1698
                        {
 
1699
                                pAdapter->PortCfg.Pss = PWR_ACTIVE;
 
1700
 
 
1701
                                // Increase Total receive byte counter after real data received no mater any error or not
 
1702
                                pAdapter->RalinkCounters.ReceivedByteCount += (pRxD->DataByteCnt - 4);
 
1703
 
 
1704
                                // Check for all RxD errors
 
1705
                                Status = RTMPCheckRxDescriptor(pAdapter, pRxD);
 
1706
                        }
 
1707
 
 
1708
                        /* Only recieve valid packets in to monitor mode */
 
1709
                        if (pAdapter->PortCfg.BssType == BSS_MONITOR && Status == NDIS_STATUS_SUCCESS)
 
1710
                        {
 
1711
                                struct sk_buff  *skb;
 
1712
                                if ((skb = __dev_alloc_skb(2048, GFP_DMA|GFP_ATOMIC)) != NULL)
 
1713
                                {
 
1714
                                        if (pAdapter->PortCfg.MallowRFMONTx == TRUE) {
 
1715
                                                if (pAdapter->PortCfg.ForcePrismHeader != 1)
 
1716
                                                        goto rfmontx_80211_receive;
 
1717
                                        } else {
 
1718
                                                if (pAdapter->PortCfg.ForcePrismHeader == 2)
 
1719
                                                        goto rfmontx_80211_receive;
 
1720
                                        }
 
1721
 
 
1722
                                        // setup the wlan-ng prismheader
 
1723
 
 
1724
                                        if (skb_headroom(skb) < sizeof(wlan_ng_prism2_header))
 
1725
                                                pskb_expand_head(skb, sizeof(wlan_ng_prism2_header), 0, GFP_ATOMIC);
 
1726
 
 
1727
                                        ph = (wlan_ng_prism2_header *)
 
1728
                                                skb_push(skb, sizeof(wlan_ng_prism2_header));
 
1729
                                        memset(ph, 0, sizeof(wlan_ng_prism2_header));
 
1730
 
 
1731
                                        ph->msgcode     = DIDmsg_lnxind_wlansniffrm;
 
1732
                                        ph->msglen      = sizeof(wlan_ng_prism2_header);
 
1733
                                        strcpy(ph->devname, pAdapter->net->name);
 
1734
 
 
1735
                                        ph->hosttime.did        = DIDmsg_lnxind_wlansniffrm_hosttime;
 
1736
                                        ph->mactime.did         = DIDmsg_lnxind_wlansniffrm_mactime;
 
1737
                                        ph->channel.did         = DIDmsg_lnxind_wlansniffrm_channel;
 
1738
                                        ph->rssi.did            = DIDmsg_lnxind_wlansniffrm_rssi;
 
1739
                                        ph->signal.did          = DIDmsg_lnxind_wlansniffrm_signal;
 
1740
                                        ph->noise.did           = DIDmsg_lnxind_wlansniffrm_noise;
 
1741
                                        ph->rate.did            = DIDmsg_lnxind_wlansniffrm_rate;
 
1742
                                        ph->istx.did            = DIDmsg_lnxind_wlansniffrm_istx;
 
1743
                                        ph->frmlen.did          = DIDmsg_lnxind_wlansniffrm_frmlen;
 
1744
 
 
1745
                                        ph->hosttime.len        = 4;
 
1746
                                        ph->mactime.len         = 4;
 
1747
                                        ph->channel.len         = 4;
 
1748
                                        ph->rssi.len            = 4;
 
1749
                                        ph->signal.len          = 4;
 
1750
                                        ph->noise.len           = 4;
 
1751
                                        ph->rate.len            = 4;
 
1752
                                        ph->istx.len            = 4;
 
1753
                                        ph->frmlen.len          = 4;
 
1754
                
 
1755
                                        ph->hosttime.data       = jiffies;
 
1756
                                        ph->channel.data        = pAdapter->PortCfg.IbssConfig.Channel;
 
1757
                                        ph->signal.data         = pRxD->BBR1;
 
1758
                                        ph->noise.data          = pAdapter->PortCfg.LastR17Value;
 
1759
                                        ph->rssi.data           = ph->signal.data - ph->noise.data;
 
1760
                                        ph->frmlen.data         = pRxD->DataByteCnt;
 
1761
 
 
1762
                                        if (pRxD->Ofdm == 1)
 
1763
                                        {
 
1764
                                                for (i = 4; i < 12; i++)
 
1765
                                                        if (pRxD->BBR0 == PlcpSignal[i])
 
1766
                                                                ph->rate.data = _11G_RATES[i] * 2;
 
1767
                                        }
 
1768
                                        else
 
1769
                                                ph->rate.data = pRxD->BBR0 / 5;
 
1770
 
 
1771
                                        // end prismheader setup
 
1772
 
 
1773
                                rfmontx_80211_receive:
 
1774
 
 
1775
                                        skb->dev = pAdapter->net;
 
1776
                                        memcpy(skb_put(skb, pRxD->DataByteCnt-4), pData, pRxD->DataByteCnt-4);
 
1777
                                        skb->mac.raw = skb->data;
 
1778
                                        skb->pkt_type = PACKET_OTHERHOST;
 
1779
                                        skb->protocol = htons(ETH_P_802_2);
 
1780
                                        skb->ip_summed = CHECKSUM_NONE;
 
1781
                                        netif_rx(skb);
 
1782
                                }       
 
1783
                        
 
1784
                                if ((!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_RESET_IN_PROGRESS)) &&
 
1785
                                (!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_BULKIN_RESET)) &&
 
1786
                                (!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_RADIO_OFF)) &&
 
1787
                                (!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_HALT_IN_PROGRESS)) &&
 
1788
                                (!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_REMOVE_IN_PROGRESS)))
 
1789
                                RTUSBBulkReceive(pAdapter);
 
1790
                                continue;
 
1791
                        }
 
1792
                        
 
1793
                        if (Status == NDIS_STATUS_SUCCESS)
 
1794
                        {
 
1795
                                // Apply packet filtering rule based on microsoft requirements.
 
1796
                                Status = RTMPApplyPacketFilter(pAdapter, pRxD, pHeader);
 
1797
                        }       
 
1798
                
 
1799
                        // Add receive counters
 
1800
                        if (Status == NDIS_STATUS_SUCCESS)
 
1801
                        {
 
1802
                                // Increase 802.11 counters & general receive counters
 
1803
                                INC_COUNTER(pAdapter->WlanCounters.ReceivedFragmentCount);
 
1804
                        }
 
1805
                        else
 
1806
                        {
 
1807
                                // Increase general counters
 
1808
                                pAdapter->Counters.RxErrors++;
 
1809
                        }
 
1810
                        
 
1811
 
 
1812
                        // Check for retry bit, if this bit is on, search the cache with SA & sequence
 
1813
                        // as index, if matched, discard this frame, otherwise, update cache
 
1814
                        // This check only apply to unicast data & management frames
 
1815
                        if ((pRxD->U2M) && (Status == NDIS_STATUS_SUCCESS) && (pHeader->Controlhead.Frame.Type != BTYPE_CNTL))
 
1816
                        {
 
1817
                                if (pHeader->Controlhead.Frame.Retry)
 
1818
                                {
 
1819
                                        if (RTMPSearchTupleCache(pAdapter, pHeader) == TRUE)
 
1820
                                        {
 
1821
                                                // Found retry frame in tuple cache, Discard this frame / fragment
 
1822
                                                // Increase 802.11 counters
 
1823
                                                INC_COUNTER(pAdapter->WlanCounters.FrameDuplicateCount);
 
1824
                                                DBGPRINT_RAW(RT_DEBUG_INFO, "duplicate frame\n");//steven:for debug
 
1825
                                                Status = NDIS_STATUS_FAILURE;
 
1826
                                        }
 
1827
                                        else
 
1828
                                        {
 
1829
                                                RTMPUpdateTupleCache(pAdapter, pHeader);
 
1830
                                        }
 
1831
                                }
 
1832
                                else    // Update Tuple Cache
 
1833
                                {
 
1834
                                        RTMPUpdateTupleCache(pAdapter, pHeader);
 
1835
                                }
 
1836
                        }
 
1837
                        
 
1838
                        // Check and set the cipher variable
 
1839
                        if (pRxD->U2M)
 
1840
                                Cipher = pAdapter->PortCfg.PairCipher;
 
1841
                        else
 
1842
                                Cipher = pAdapter->PortCfg.GroupCipher;         
 
1843
                                Cipher = pAdapter->PortCfg.WepStatus;
 
1844
 
 
1845
                        //
 
1846
                        // Do RxD release operation     for     all     failure frames
 
1847
                        //
 
1848
                        if (Status == NDIS_STATUS_SUCCESS)
 
1849
                        {
 
1850
                                //
 
1851
                                // Start of     main loop to parse receiving frames.
 
1852
                                // The sequence will be Type first,     then subtype...
 
1853
                                //
 
1854
                                switch (pHeader->Controlhead.Frame.Type)
 
1855
                                        {
 
1856
                                                // Frame with data type
 
1857
                                                case BTYPE_DATA:
 
1858
                                                        // pData : Pointer skip the     first 24 bytes, 802.11 HEADER
 
1859
                                                        pData += LENGTH_802_11;
 
1860
 
 
1861
                                                        PacketSize = pRxD->DataByteCnt - LENGTH_802_11 - 4;      //Minus FCS[4].  default for NoneWep.
 
1862
                                                        // Drop not my BSS frame
 
1863
                                                        if (INFRA_ON(pAdapter))
 
1864
                                                        {
 
1865
                                                                // Infrastructure mode, check address 2 for BSSID
 
1866
                                                                if (memcmp(&pHeader->Controlhead.Addr2, &pAdapter->PortCfg.Bssid, 6) != 0)
 
1867
                                                                        break;  // Receive frame not my BSSID
 
1868
                                                                else
 
1869
                                                                        atomic_inc(&(pAdapter->PortCfg.DataPacketsFromAP));
 
1870
                                                        }
 
1871
                                                        else    // Ad-Hoc mode or Not associated
 
1872
                                                        {
 
1873
                                                                // Ad-Hoc mode, check address 3 for BSSID
 
1874
                                                                if (memcmp(&pHeader->Addr3, &pAdapter->PortCfg.Bssid, 6) != 0)
 
1875
                                                                        break;  // Receive frame not my BSSID
 
1876
 
 
1877
                                                                // Drop frame from AP while we are in Ad-hoc mode or not associated
 
1878
                                                                if (pHeader->Controlhead.Frame.FrDs)
 
1879
                                                                        break;
 
1880
                                                        }
 
1881
 
 
1882
                                                        // Drop Null data frame, or CF with NULL data frame
 
1883
                                                        if ((pHeader->Controlhead.Frame.Subtype == SUBTYPE_NULL_FUNC) ||
 
1884
                                                                (pHeader->Controlhead.Frame.Subtype == SUBTYPE_CFACK)     ||
 
1885
                                                                (pHeader->Controlhead.Frame.Subtype == SUBTYPE_CFPOLL)    ||
 
1886
                                                                (pHeader->Controlhead.Frame.Subtype == SUBTYPE_CFACK_CFPOLL))
 
1887
                                                        {
 
1888
                                                                break;
 
1889
                                                        }
 
1890
 
 
1891
                                                        // Process Broadcast & Multicast data frame
 
1892
                                                        if (pRxD->Bcast || pRxD->Mcast)
 
1893
                                                        {
 
1894
                                                                // Multicast 802.11 Counter
 
1895
                                                                INC_COUNTER(pAdapter->WlanCounters.MulticastReceivedFrameCount);
 
1896
                                                                DBGPRINT(RT_DEBUG_INFO,"Receiving multicast frame\n");
 
1897
                                                                // Drop Mcast / Bcast frame with fragment bit on
 
1898
                                                                if (pHeader->Controlhead.Frame.MoreFrag)
 
1899
                                                                {
 
1900
                                                                        DBGPRINT_RAW(RT_DEBUG_ERROR,"Receiving multicast frame with fragment bit on\n");                                                        
 
1901
                                                                        break;
 
1902
                                                                }       
 
1903
                                                                
 
1904
                                                                // Filter out Bcast frame which AP relayed for us
 
1905
                                                                if (((memcmp(&pHeader->Addr3, pAdapter->CurrentAddress, 6) == 0)) && pHeader->Controlhead.Frame.FrDs)
 
1906
                                                                        break;
 
1907
                                                                
 
1908
                                                                // WEP encrypted frame
 
1909
                                                                if (pHeader->Controlhead.Frame.Wep)
 
1910
                                                                {
 
1911
                                                                        // Check our WEP setting, if no WEP turning on, just drop this frame
 
1912
                                                                        if (Cipher == Ndis802_11Encryption1Enabled)     // WEP
 
1913
                                                                        {
 
1914
                                                                                if (pRxD->CiErr)
 
1915
                                                                                        break;
 
1916
                                                                                else
 
1917
                                                                                {
 
1918
                                                                                        pData = pData + 4;  //Offset skip IV[4]
 
1919
                                                                                        pRxD->DataByteCnt = pRxD->DataByteCnt - 8; //Minus ICV[4] & FCS[4].
 
1920
                                                                                }
 
1921
 
 
1922
                                                                                PacketSize = pRxD->DataByteCnt - LENGTH_802_11 - 4;     //Minus IV[4].
 
1923
                                                                        }
 
1924
                                                                        else if (Cipher == Ndis802_11Encryption2Enabled)        // TKIP
 
1925
                                                                        {
 
1926
                                                                                if (pRxD->CiErr)
 
1927
                                                                                        {
 
1928
                                                                        DBGPRINT_RAW(RT_DEBUG_ERROR,"pRxD->CiErr\n");                                                   
 
1929
                                                                                        break;
 
1930
                                                                                        }
 
1931
                                                                                else
 
1932
                                                                                {
 
1933
                                                                                        pData = pData + 8; //Offset skip IV[8]
 
1934
                                                                                        //
 
1935
                                                                                        // the MIC is stored on the last one no more Fragment.
 
1936
                                                                                        // that is only last MPDU only need to check MIC.
 
1937
                                                                                        //
 
1938
                                                                                        if (pHeader->Controlhead.Frame.MoreFrag == TRUE)
 
1939
                                                                                        {                                                                       
 
1940
                                                                                                // No MIC here.
 
1941
                                                                                                pRxD->DataByteCnt = pRxD->DataByteCnt - 8; //ICV[4] &FCS[4].
 
1942
                                                                                        }
 
1943
                                                                                        else
 
1944
                                                                                        {
 
1945
                                                                                                if (pHeader->Frag != 0)
 
1946
                                                                                                        pRxD->DataByteCnt = pRxD->DataByteCnt - 8; //MIC been frag ICV[4] & FCS[4]
 
1947
                                                                                                else                                                                            
 
1948
                                                                                                        pRxD->DataByteCnt = pRxD->DataByteCnt - 16; //Minus MIC[8] & ICV[4] &FCS[4].
 
1949
                                                                                        }                                                               
 
1950
                                                                                }
 
1951
 
 
1952
                                                                                PacketSize = pRxD->DataByteCnt - LENGTH_802_11 - 8;     //Minus IV+EIV[8].
 
1953
                                                                        }
 
1954
                                                                        else if (Cipher == Ndis802_11Encryption3Enabled)        // AES
 
1955
                                                                        {
 
1956
                                                                                if (pRxD->CiErr)
 
1957
                                                                                        break;
 
1958
                                                                                else
 
1959
                                                                                {
 
1960
                                                                                        pData = pData + 8; //Offset skip RSN[8]
 
1961
                                                                                        pRxD->DataByteCnt = pRxD->DataByteCnt - 12;  //Minus MIC[8] & ICV[4]
 
1962
                                                                                }
 
1963
 
 
1964
                                                                                PacketSize = pRxD->DataByteCnt - LENGTH_802_11 - 8;     //Minus RSN[8]
 
1965
                                                                        }
 
1966
                                                                        else
 
1967
                                                                                break;
 
1968
                                                                }
 
1969
                                                        }//if (pRxD->Bcast || pRxD->Mcast)                              
 
1970
                                                        // Begin process unicast to     me frame
 
1971
                                                        else if (pRxD->U2M)
 
1972
                                                        {                                       
 
1973
                                                                //
 
1974
                                                                // Begin frame processing
 
1975
                                                                //
 
1976
                                                                // DA is always address 1
 
1977
                                                                pDestMac = (PUCHAR)     &(pHeader->Controlhead.Addr1);
 
1978
                                                                // Seclect SA by different mode
 
1979
                                                                if (INFRA_ON(pAdapter))         // For infrastructure, SA is address 3
 
1980
                                                                {
 
1981
                                                                        pSrcMac = (PUCHAR) &(pHeader->Addr3);
 
1982
                                                                }
 
1983
                                                                else                                                                    // For IBSS     mode, SA is     address 2
 
1984
                                                                {
 
1985
                                                                        pSrcMac = (PUCHAR) &(pHeader->Controlhead.Addr2);
 
1986
                                                                }
 
1987
                                                                // WEP encrypted frame
 
1988
                                                                if (Cipher == Ndis802_11Encryption1Enabled)     // WEP
 
1989
                                                                {
 
1990
                                                                        if (pHeader->Controlhead.Frame.Wep)
 
1991
                                                                        {
 
1992
                                                                                if (pRxD->CiErr)
 
1993
                                                                                        break;
 
1994
                                                                                else
 
1995
                                                                                {
 
1996
                                                                                        pData = pData + 4; //Offset skip IV[4]
 
1997
                                                                                        pRxD->DataByteCnt = pRxD->DataByteCnt - 8; //Minus ICV[4] & FCS[4].
 
1998
                                                                                }
 
1999
 
 
2000
                                                                                PacketSize = pRxD->DataByteCnt - LENGTH_802_11 - 4;     //Minus IV[4].
 
2001
                                                                        }
 
2002
                                                                        else if ((pAdapter->PortCfg.PrivacyFilter == Ndis802_11PrivFilter8021xWEP) &&
 
2003
                                                                                         (pHeader->Frag == 0))
 
2004
                                                                        {
 
2005
                                                                                // Check 802.1x frame, if not drop it.
 
2006
                                                                                if (memcmp(EAPOL, pData + 6, 2))
 
2007
                                                                                {
 
2008
                                                                                        // Not 802.1X frames
 
2009
                                                                                        // Add error counter
 
2010
                                                                                        break;
 
2011
                                                                                }                                                       
 
2012
                                                                        }                                               
 
2013
                                                                }
 
2014
                                                                else if (Cipher == Ndis802_11Encryption2Enabled)        // TKIP
 
2015
                                                                {
 
2016
                                                                        if (pHeader->Controlhead.Frame.Wep)
 
2017
                                                                        {
 
2018
                                                                                if (pRxD->CiErr)
 
2019
                                                                                        {                                                                               
 
2020
                                                                DBGPRINT(RT_DEBUG_TEMP,"pRxD->CiErr\n");
 
2021
                                                                                        break;
 
2022
 
 
2023
 
 
2024
                                                                                        }
 
2025
                                                                                else
 
2026
                                                                                {
 
2027
                                                                                        pData = pData + 8;  //Offset skip IV[8]
 
2028
                                                                                        //
 
2029
                                                                                        // the MIC is stored on the last one no more Fragment.
 
2030
                                                                                        // that is only last MPDU only need to check MIC.
 
2031
                                                                                        //
 
2032
                                                                                        if (pHeader->Controlhead.Frame.MoreFrag == TRUE)
 
2033
                                                                                        {
 
2034
                                                                                                //No MIC here.
 
2035
                                                                                                pRxD->DataByteCnt = pRxD->DataByteCnt - 8; //ICV[4] &FCS[4].
 
2036
                                                                                        }
 
2037
                                                                                        else
 
2038
                                                                                        {
 
2039
                                                                                                if (pHeader->Frag != 0)
 
2040
                                                                                                        pRxD->DataByteCnt = pRxD->DataByteCnt - 8; //MIC been frag ICV[4] & FCS[4]
 
2041
                                                                                                else
 
2042
                                                                                                        pRxD->DataByteCnt = pRxD->DataByteCnt - 16; //Minus MIC[8] & ICV[4] & FCS[4].
 
2043
                                                                                        }
 
2044
                                                                                }
 
2045
 
 
2046
                                                                                PacketSize = pRxD->DataByteCnt - LENGTH_802_11 - 8;     //Minus IV+EIV[8].
 
2047
                                                                        }
 
2048
                                                                        else if ((pAdapter->PortCfg.PrivacyFilter == Ndis802_11PrivFilter8021xWEP) &&
 
2049
                                                                                         (pHeader->Frag == 0))
 
2050
                                                                        {
 
2051
                                                                                // Check 802.1x frame, if not drop it.
 
2052
                                                                                if (memcmp(EAPOL, pData + 6, 2) != 0)
 
2053
                                                                                {
 
2054
                                                                DBGPRINT(RT_DEBUG_TEMP,"Not 802.1X frames\n");
 
2055
                                                                                        // Not 802.1X frames
 
2056
                                                                                        // Add error counter
 
2057
                                                                                        break;
 
2058
                                                                                }
 
2059
                                                                                DBGPRINT(RT_DEBUG_TEMP," 802.1X EAPOL frames\n");
 
2060
                                                                        }
 
2061
                                                                }
 
2062
                                                                else if (Cipher == Ndis802_11Encryption3Enabled)        // AES
 
2063
                                                                {
 
2064
                                                                        if (pHeader->Controlhead.Frame.Wep)
 
2065
                                                                        {
 
2066
                                                                                if (pRxD->CiErr)
 
2067
                                                                                        break;
 
2068
                                                                                else
 
2069
                                                                                {
 
2070
                                                                                        pData = pData + 8; //Offset skip IV[8]
 
2071
                                                                                        pRxD->DataByteCnt = pRxD->DataByteCnt - 12;  //Minus MIC[8] & ICV[4]
 
2072
                                                                                }
 
2073
 
 
2074
                                                                                PacketSize = pRxD->DataByteCnt - LENGTH_802_11 - 8;     //Minus RSN[8]
 
2075
                                                                        }
 
2076
                                                                        else if ((pAdapter->PortCfg.PrivacyFilter == Ndis802_11PrivFilter8021xWEP) &&
 
2077
                                                                                         (pHeader->Frag == 0))
 
2078
                                                                        {
 
2079
                                                                                // Check 802.1x frame, if not drop it.
 
2080
                                                                                if (memcmp(EAPOL, pData + 6, 2) != 0)
 
2081
                                                                                {
 
2082
                                                                                        // Not 802.1X frames
 
2083
                                                                                        // Add error counter
 
2084
                                                                                        break;
 
2085
                                                                                }
 
2086
                                                                        }
 
2087
                                                                }
 
2088
                                                                else if (pHeader->Controlhead.Frame.Wep)
 
2089
                                                                {
 
2090
                                                                        // Drop WEP frame when PrivacyInvoked is FALSE
 
2091
                                                                        break;
 
2092
                                                                }                                               
 
2093
                                                        }//else if      (pRxD->U2M)
 
2094
                        
 
2095
                                                        // The total available payload should exclude 24-byte 802.11 Header
 
2096
                                                        //packetSize = pRxD->DataByteCnt - LENGTH_802_11 - 4;                                   
 
2097
 
 
2098
                                                        // Find the WPA key, either Group or Pairwise Key
 
2099
                                                        // Although the data has been decrypted by ASIC,
 
2100
                                                        // driver has to calculate the RxMIC which required the key.
 
2101
                                                        // The failed case should not happen. If it did, drop it.
 
2102
                                                        if ((pAdapter->PortCfg.CipherAlg == CIPHER_TKIP) && (pHeader->Controlhead.Frame.Wep))
 
2103
                                                        {
 
2104
                                                                INT     idx;
 
2105
 
 
2106
                                                                pWpaKey = (PWPA_KEY) NULL;
 
2107
                                                                // First lookup the DA, if it's a group address, use GROUP key
 
2108
                                                                if (pRxD->Bcast || pRxD->Mcast)
 
2109
                                                                {
 
2110
#ifdef BIG_ENDIAN
 
2111
                                                                        idx = (pRxD->Iv & 0xc0000000) >> 30;
 
2112
#else
 
2113
                                                                        idx = (pRxD->Iv & 0x000000c0) >> 6;
 
2114
#endif
 
2115
                                                                        if ((pAdapter->PortCfg.GroupKey[idx].KeyLen != 0) && 
 
2116
                                                                                ((INFRA_ON(pAdapter) && ((memcmp(&pHeader->Controlhead.Addr2, &pAdapter->PortCfg.Bssid, 6) == 0))) ||
 
2117
                                                                                (ADHOC_ON(pAdapter) && ((memcmp(&pHeader->Addr3, &pAdapter->PortCfg.Bssid, 6) == 0)))))
 
2118
                                                                        {
 
2119
                                                                                pWpaKey = (PWPA_KEY) &pAdapter->PortCfg.GroupKey[idx];
 
2120
                                                                                pWpaKey->Type = GROUP_KEY;
 
2121
                                                                                DBGPRINT(RT_DEBUG_INFO, "Rx Use Group Key %d\n", idx);
 
2122
                                                                        }
 
2123
                                                                }
 
2124
                                                                // Try to find the Pairwise Key
 
2125
                                                                else
 
2126
                                                                {
 
2127
                                                                        for (idx = 0; idx < PAIRWISE_KEY_NO; idx++)
 
2128
                                                                        {
 
2129
                                                                                if (((memcmp(&pHeader->Controlhead.Addr2, pAdapter->PortCfg.PairwiseKey[idx].BssId, 6) == 0)) &&
 
2130
                                                                                        (pAdapter->PortCfg.PairwiseKey[idx].KeyLen != 0))
 
2131
                                                                                {
 
2132
                                                                                        pWpaKey = (PWPA_KEY) &pAdapter->PortCfg.PairwiseKey[idx];
 
2133
                                                                                        pWpaKey->Type = PAIRWISE_KEY;
 
2134
                                                                                        DBGPRINT(RT_DEBUG_LOUD, "Rx Use Pairwise Key %d\n",idx);
 
2135
                                                                                        break;
 
2136
                                                                                }
 
2137
                                                                        }
 
2138
                                                                        // Use default Group Key if there is no Pairwise key present
 
2139
                                                                        if ((pWpaKey == NULL) && (pAdapter->PortCfg.GroupKey[pAdapter->PortCfg.DefaultKeyId].KeyLen != 0))
 
2140
                                                                        {
 
2141
                                                                                pWpaKey = (PWPA_KEY) &pAdapter->PortCfg.GroupKey[pAdapter->PortCfg.DefaultKeyId];
 
2142
                                                                                pWpaKey->Type = GROUP_KEY;
 
2143
                                                                                DBGPRINT(RT_DEBUG_INFO, "Rx Use Group Key\n");
 
2144
                                                                        }
 
2145
                                                                }
 
2146
                                                                
 
2147
                                                                if (pWpaKey == NULL)
 
2148
                                                                        break;
 
2149
                                                        }
 
2150
 
 
2151
                                                        // DA is always address 1
 
2152
                                                        pDestMac = (PUCHAR) &(pHeader->Controlhead.Addr1);
 
2153
                                                        // Seclect SA by different mode
 
2154
                                                        if (INFRA_ON(pAdapter))
 
2155
                                                        {
 
2156
                                                                // For infrastructure, SA is address 3
 
2157
                                                                pSrcMac = (PUCHAR) &(pHeader->Addr3);
 
2158
                                                        }
 
2159
                                                        else
 
2160
                                                        {
 
2161
                                                                // For IBSS mode, SA is address 2
 
2162
                                                                pSrcMac = (PUCHAR) &(pHeader->Controlhead.Addr2);
 
2163
                                                        }
 
2164
                        
 
2165
                                                        // Process Broadcast & Multicast data frame
 
2166
                                                        if (pRxD->Bcast || pRxD->Mcast)
 
2167
                                                        {                                                       
 
2168
                                                                // Save encapaturation starting pointer
 
2169
                                                                pEncap = pData;
 
2170
 
 
2171
                                                                // For TKIP frame, calculate the MIC value
 
2172
                                                                if ((pAdapter->PortCfg.CipherAlg == CIPHER_TKIP) && (pHeader->Controlhead.Frame.Wep))
 
2173
                                                                {
 
2174
                                                                        i = 0;
 
2175
 
 
2176
                                                                        if (RTMPTkipCompareMICValue(pAdapter,                                                                                                                   
 
2177
                                                                                                                                pData,
 
2178
                                                                                                                                pDestMac,
 
2179
                                                                                                                                pSrcMac,
 
2180
                                                                                                                                pWpaKey->RxMic,
 
2181
                                                                                                                                PacketSize) == FALSE)
 
2182
                                                                        {
 
2183
                                                                                DBGPRINT_RAW(RT_DEBUG_ERROR,"BroadCast/Multicast Rx MIC Value error\n");
 
2184
                                                                                RTMPReportMicError(pAdapter, pWpaKey);
 
2185
                                                                                Status = NDIS_STATUS_FAILURE;
 
2186
                                                                                break;
 
2187
                                                                        }
 
2188
 
 
2189
                                                                        // Second, increase RxTsc value for next transmission
 
2190
                                                                        while (++pWpaKey->RxTsc[i] == 0x0)
 
2191
                                                                        {
 
2192
                                                                                i++;
 
2193
                                                                                if (i == 6)
 
2194
                                                                                        break;
 
2195
                                                                        }
 
2196
                                                                        // Rx TSC has done one full cycle, since re-key is done by transmitter
 
2197
                                                                        // We did not do anything for Rx path
 
2198
                                                                }
 
2199
                                                                // For WPA2 mixer mode PairCipher = AES, GroupCipher = TKIP
 
2200
                                                                else if ((pAdapter->PortCfg.PairCipher  == Ndis802_11Encryption3Enabled) && 
 
2201
                                                                                 (pAdapter->PortCfg.GroupCipher == Ndis802_11Encryption2Enabled) &&
 
2202
                                                                                 (pHeader->Controlhead.Frame.Wep))
 
2203
                                                                {
 
2204
                                                                        //Use Software to decript TKIP packet.
 
2205
                                                                        if (RTMPSoftDecryptTKIP(pAdapter, pRxContext->TransferBuffer, pRxD->DataByteCnt + 12, pAdapter->PortCfg.GroupKey))
 
2206
                                                                        {
 
2207
                                                                                DBGPRINT(RT_DEBUG_INFO, "WPA2::RTMPSoftDecryptTKIP Complete\n");
 
2208
                                                                                pData = pRxContext->TransferBuffer + LENGTH_802_11;
 
2209
                                                                                PacketSize = pRxD->DataByteCnt - 8 - LENGTH_802_11;  //8 bytes MIC, 4 bytes ICV
 
2210
                                                                                pEncap = pData;                                                         
 
2211
                                                                        }
 
2212
                                                                }
 
2213
                                                        
 
2214
                                                                // Check for encapsulataion other than RFC1042 & Bridge tunnel
 
2215
                                                                if ((memcmp(SNAP_802_1H, pEncap, 6) != 0) && (memcmp(SNAP_BRIDGE_TUNNEL, pEncap, 6) != 0))
 
2216
                                                                {
 
2217
                                                                        LLC_Len[0] = PacketSize / 256;
 
2218
                                                                        LLC_Len[1] = PacketSize % 256;
 
2219
                                                                        MAKE_802_3_HEADER(Header802_3, pDestMac, pSrcMac, ((PUCHAR) LLC_Len));
 
2220
                                                                }
 
2221
                                                                else
 
2222
                                                                {
 
2223
                                                                        // Remove 802.11 H header & reconstruct 802.3 header
 
2224
                                                                        pData += (LENGTH_802_1_H - LENGTH_802_3_TYPE);
 
2225
                                                                        // Patch for WHQl only, which did not turn on Netbios but use IPX within its payload
 
2226
                                                                        if (((memcmp(IPX, pData, 2) == 0) || (memcmp(APPLE_TALK, pData, 2) == 0)) && (memcmp(SNAP_802_1H, pEncap, 6) == 0))
 
2227
                                                                        {
 
2228
                                                                                LLC_Len[0] = PacketSize / 256;
 
2229
                                                                                LLC_Len[1] = PacketSize % 256;
 
2230
                                                                                pData = pData - LENGTH_802_1_H;
 
2231
                                                                                MAKE_802_3_HEADER(Header802_3, pDestMac, pSrcMac, ((PUCHAR) LLC_Len));
 
2232
                                                                        }
 
2233
                                                                        else
 
2234
                                                                        {
 
2235
                                                                                MAKE_802_3_HEADER(Header802_3, pDestMac, pSrcMac, pData);
 
2236
                                                                                // The total available payload should exclude 24-byte 802.11 Header
 
2237
                                                                                // and 8-byte 802.2 LLC
 
2238
                                                                                PacketSize -= LENGTH_802_1_H;
 
2239
                                                                        }
 
2240
                                                                
 
2241
                                                                        // Point to read 802.3 payload
 
2242
                                                                        pData += LENGTH_802_3_TYPE;
 
2243
                                                                }
 
2244
                        
 
2245
                                                                // For miniportTransferData
 
2246
                                                                pAdapter->pRxData = pData;
 
2247
 
 
2248
                                                                pAdapter->PortCfg.LedCntl.fRxActivity = TRUE; // for RX ACTIVITY LED
 
2249
 
 
2250
                                                                // Acknolwdge upper layer the received frame
 
2251
                                                                // Copy header to head of data for compatibility with older protocol
 
2252
                                                                // eariler than W2K
 
2253
                                                                //memcpy(pData - LENGTH_802_3, Header802_3, LENGTH_802_3);
 
2254
                                                                // Acknowledge upper layer the received frame
 
2255
                                                                if ((skb = dev_alloc_skb(PacketSize + LENGTH_802_3 + 2)) != NULL)
 
2256
                                                                {
 
2257
                                                                    skb->dev = pAdapter->net;
 
2258
                                                                    skb_reserve(skb, 2);    // 16 byte align the IP header
 
2259
                                                                    memcpy(skb_put(skb, LENGTH_802_3), Header802_3, LENGTH_802_3);
 
2260
                                                                    memcpy(skb_put(skb, PacketSize), pData, PacketSize);
 
2261
                                                                    skb->protocol = eth_type_trans(skb, pAdapter->net);
 
2262
                                                                    netif_rx(skb);
 
2263
                                                                    pAdapter->net->last_rx = jiffies;
 
2264
                                                                    pAdapter->netstats.rx_packets++;
 
2265
                                                                }
 
2266
                                                                //memset(Header802_3, 0, LENGTH_802_3);
 
2267
                                                                DBGPRINT_RAW(RT_DEBUG_INFO, "!!! Broadcast Ethernet rx Indicated !!!\n");
 
2268
                                                        } //if (pRxD->Bcast || pRxD->Mcast)                                                                                     
 
2269
                                                        // Begin process unicast to me frame
 
2270
                                                        else if (pRxD->U2M) 
 
2271
                                                        {
 
2272
                                                                // Update Rx data rate first.
 
2273
                                                                if (pRxD->Ofdm == 1)
 
2274
                                                                {
 
2275
                                                                        for (i = 4; i < 12; i++)
 
2276
                                                                        {
 
2277
                                                                                if (pRxD->BBR0 == PlcpSignal[i])
 
2278
                                                                                        break;
 
2279
                                                                        }
 
2280
                                                                        if (i < 12)
 
2281
                                                                                pAdapter->LastRxRate = i;
 
2282
                                                                }
 
2283
                                                                else    // receive CCK encoding
 
2284
                                                                {
 
2285
                                                                        if (pRxD->BBR0 == 10)
 
2286
                                                                                pAdapter->LastRxRate = 0;
 
2287
                                                                        else if (pRxD->BBR0 == 20)
 
2288
                                                                                pAdapter->LastRxRate = 1;
 
2289
                                                                        else if (pRxD->BBR0 == 55)
 
2290
                                                                                pAdapter->LastRxRate = 2;
 
2291
                                                                        else if (pRxD->BBR0 == 110)
 
2292
                                                                                pAdapter->LastRxRate = 3;
 
2293
                                                                }
 
2294
 
 
2295
                                                                // Send PS-Poll for AP to send next data frame                                  
 
2296
                                                                if ((pHeader->Controlhead.Frame.MoreData) && INFRA_ON(pAdapter) && (pAdapter->PortCfg.Psm == PWR_SAVE))
 
2297
                                                                {
 
2298
                                                                        //Send PS-Poll frame
 
2299
                                                                        EnqueuePsPoll(pAdapter);
 
2300
                                                                        DBGPRINT(RT_DEBUG_TRACE, "Sending PS-POLL\n");
 
2301
                                                                }
 
2302
 
 
2303
                                                                //
 
2304
                                                                // Begin frame processing
 
2305
                                                                //
 
2306
                                                                if (pHeader->Frag == 0) // First or Only fragment
 
2307
                                                                {
 
2308
                                                                        // For TKIP frame, calculate the MIC value
 
2309
                                                                        if (pHeader->Controlhead.Frame.MoreFrag == FALSE)
 
2310
                                                                        {
 
2311
                                                                                if ((pAdapter->PortCfg.CipherAlg == CIPHER_TKIP) && (pHeader->Controlhead.Frame.Wep))
 
2312
                                                                                {
 
2313
                                                                                        //
 
2314
                                                                                        // Use Software to descrypt if transmition keyID not 0 on ADHOC mode.
 
2315
                                                                                        // Since ASIC allows hardware descrypt only KeyID=0 as their pairwisekey.
 
2316
                                                                                        //
 
2317
                                                                                        // Check U2M and KeyID not pairwise key, used Software decypt 
 
2318
                                                                                        //
 
2319
                                                                                        KeyIdx= *((PUCHAR)(pRxContext->TransferBuffer + LENGTH_802_11 + 3));
 
2320
                                                                                        KeyIdx = KeyIdx >> 6;
 
2321
 
 
2322
                                                                                        if(KeyIdx != 0)
 
2323
                                                                                        {
 
2324
                                                                                                //Use Software to decript TKIP packet.
 
2325
                                                                                                if (RTMPSoftDecryptTKIP(pAdapter, pRxContext->TransferBuffer, pRxD->DataByteCnt + 12, pAdapter->PortCfg.GroupKey))
 
2326
                                                                                                {
 
2327
                                                                                                        DBGPRINT(RT_DEBUG_TEMP, "U2M Use Groupkey RTMPSoftDecryptTKIP Complete\n");
 
2328
                                                                                                        pData = pRxContext->TransferBuffer + LENGTH_802_11;
 
2329
                                                                                                        PacketSize = pRxD->DataByteCnt - 8 - LENGTH_802_11;  //8 bytes MIC, 4 bytes ICV
 
2330
                                                                                                }
 
2331
                                                                                                else
 
2332
                                                                                                {
 
2333
                                                                                                        DBGPRINT(RT_DEBUG_TEMP, "RTMPSoftDecryptTKIP failed\n");
 
2334
                                                                                                        break;
 
2335
                                                                                                }
 
2336
                                                                                        }
 
2337
                                                                                        else
 
2338
                                                                                        {
 
2339
                                                                                                if (RTMPTkipCompareMICValue(pAdapter,                                                                                                                                           
 
2340
                                                                                                                                                        pData,
 
2341
                                                                                                                                                        pDestMac,
 
2342
                                                                                                                                                        pSrcMac,
 
2343
                                                                                                                                                        pWpaKey->RxMic,//steven:where is this from in RT2570
 
2344
                                                                                                                                                        PacketSize) == FALSE)
 
2345
                                                                                                {
 
2346
                                                                                                        DBGPRINT_RAW(RT_DEBUG_ERROR,"U2M Rx MIC Value error1\n");
 
2347
                                                                                                        RTMPReportMicError(pAdapter, pWpaKey);
 
2348
                                                                                                        Status = NDIS_STATUS_FAILURE;
 
2349
                                                                                                        break;
 
2350
                                                                                                }
 
2351
                                                                                        }
 
2352
 
 
2353
                                                                                        // TODO:
 
2354
                                                                                        // Getting RxTSC from Rx descriptor
 
2355
                                                                                }
 
2356
                                                                        }
 
2357
                                                                        
 
2358
                                                                        // Save encapaturation starting pointer
 
2359
                                                                        pEncap = pData;                                                         
 
2360
                                                                        pAdapter->FragFrame.Flags &= 0xFFFFFFFE;
 
2361
                                                                        
 
2362
                                                                        // Check for encapsulataion other than RFC1042 & Bridge tunnel
 
2363
                                                                        if ((memcmp(SNAP_802_1H, pEncap, 6) != 0) && (memcmp(SNAP_BRIDGE_TUNNEL, pEncap, 6) != 0))
 
2364
                                                                        {
 
2365
                                                                                LLC_Len[0] = PacketSize / 256;
 
2366
                                                                                LLC_Len[1] = PacketSize % 256;
 
2367
                                                                                MAKE_802_3_HEADER(Header802_3, pDestMac, pSrcMac, ((PUCHAR) LLC_Len));
 
2368
                                                                        }
 
2369
                                                                        else
 
2370
                                                                        {
 
2371
                                                                                // Remove 802.11 H header & reconstruct 802.3 header
 
2372
                                                                                pData += (LENGTH_802_1_H - LENGTH_802_3_TYPE);
 
2373
                                                                if ((memcmp(EAPOL, pData, 2) == 0))
 
2374
                                                                {
 
2375
                                                                        PacketSize += LENGTH_802_11;
 
2376
                                                        DBGPRINT_RAW(RT_DEBUG_TEMP, "indicated packet EAPOL  PacketSize%d\n", PacketSize);//steven:for debug
 
2377
                                                                        // Enqueue this frame to MLME engine
 
2378
                                                                        MlmeEnqueueForRecv(
 
2379
                                                                                pAdapter,
 
2380
                                                                                &pAdapter->Mlme.Queue,  
 
2381
                                                                                (UCHAR)pRxD->BBR1, 
 
2382
                                                                                PacketSize + LENGTH_802_1_H, 
 
2383
                                                                                pManage);                                       
 
2384
                                                                        break;
 
2385
                                                                }
 
2386
                                                                                // Patch for WHQl only, which did not turn on Netbios but use IPX within its payload
 
2387
                                                                                if ((((memcmp(IPX, pData, 2) == 0) || (memcmp(APPLE_TALK, pData, 2) == 0)) && memcmp(SNAP_802_1H, pEncap, 6) == 0))
 
2388
                                                                                {
 
2389
                                                                                        LLC_Len[0] = PacketSize / 256;
 
2390
                                                                                        LLC_Len[1] = PacketSize % 256;
 
2391
                                                                                        pData = pData - LENGTH_802_1_H;
 
2392
                                                                                        MAKE_802_3_HEADER(Header802_3, pDestMac, pSrcMac, ((PUCHAR) LLC_Len));
 
2393
                                                                                }
 
2394
                                                                                else
 
2395
                                                                                {
 
2396
                                                                                                
 
2397
                                                                                        MAKE_802_3_HEADER(Header802_3, pDestMac, pSrcMac, pData);
 
2398
                                                                                        // The total available payload should exclude 24-byte 802.11 Header
 
2399
                                                                                        // and 8-byte 802.2 LLC
 
2400
                                                                                        PacketSize -= LENGTH_802_1_H;
 
2401
                                                                                        memcpy(pAdapter->FragFrame.Header_LLC, pEncap, 8);
 
2402
                                                                                        pAdapter->FragFrame.Flags |= 0x01;
 
2403
                                                                                }
 
2404
                                                                                
 
2405
                                                                                // Point to read 802.3 payload
 
2406
                                                                                pData += LENGTH_802_3_TYPE;                                                             
 
2407
                                                                        }
 
2408
                                                                                
 
2409
                                                                        // One & The only fragment
 
2410
                                                                        if (pHeader->Controlhead.Frame.MoreFrag == FALSE)
 
2411
                                                                        {
 
2412
                                                                                // For miniportTransferData
 
2413
                                                                                pAdapter->pRxData = pData;
 
2414
 
 
2415
                                                                                pAdapter->PortCfg.LedCntl.fRxActivity = TRUE; // for RX ACTIVITY LED
 
2416
 
 
2417
                                                                                DBGPRINT_RAW(RT_DEBUG_INFO, "indicated packet size = %d\n", PacketSize);//steven:for debug
 
2418
                                                                                // Acknolwdge upper layer the received frame
 
2419
                                                                                //memcpy((PUCHAR) pData - LENGTH_802_3, Header802_3, LENGTH_802_3);
 
2420
 
 
2421
                                                                                if ((skb = dev_alloc_skb(PacketSize + LENGTH_802_3 + 2)) != NULL)
 
2422
                                                                                {
 
2423
                                                                                    skb->dev = pAdapter->net;
 
2424
                                                                                    skb_reserve(skb, 2);    // 16 byte align the IP header
 
2425
                                                                                    memcpy(skb_put(skb, LENGTH_802_3), Header802_3, LENGTH_802_3);
 
2426
                                                                                    memcpy(skb_put(skb, PacketSize), pData, PacketSize);
 
2427
                                                                                    skb->protocol = eth_type_trans(skb, pAdapter->net);
 
2428
                                                                                    netif_rx(skb);
 
2429
                                                                                    pAdapter->net->last_rx = jiffies;
 
2430
                                                                                    pAdapter->netstats.rx_packets++;
 
2431
                                                                                }
 
2432
                                                                                // Increase general counters
 
2433
                                                                                pAdapter->Counters.GoodReceives++;
 
2434
 
 
2435
                                                                                DBGPRINT_RAW(RT_DEBUG_INFO, "!!! Frame without Fragment Indicated       !!!\n");
 
2436
                                                                        }
 
2437
                                                                        // First fragment of fragmented frames
 
2438
                                                                        else
 
2439
                                                                        {
 
2440
                                                                                memcpy(pAdapter->FragFrame.Buffer,      pData, PacketSize);
 
2441
                                                                                memcpy(pAdapter->FragFrame.Header802_3, Header802_3, LENGTH_802_3);
 
2442
                                                                                pAdapter->FragFrame.RxSize       = PacketSize;
 
2443
                                                                                pAdapter->FragFrame.Sequence = pHeader->Sequence;
 
2444
                                                                                pAdapter->FragFrame.LastFrag = pHeader->Frag;           // Should be 0
 
2445
                                                                        }
 
2446
                                                                }
 
2447
                                                                // Middle & End of fragment burst fragments
 
2448
                                                                else
 
2449
                                                                {
 
2450
                                                                        // No LLC-SNAP header in except the first fragment frame
 
2451
 
 
2452
                                                                        if ((pHeader->Sequence != pAdapter->FragFrame.Sequence) ||
 
2453
                                                                                (pHeader->Frag != (pAdapter->FragFrame.LastFrag + 1)))
 
2454
                                                                        {
 
2455
                                                                                // Fragment is not the same sequence or out of fragment number order
 
2456
                                                                                // Clear Fragment frame contents
 
2457
                                                                                memset(&pAdapter->FragFrame, 0, sizeof(FRAGMENT_FRAME));
 
2458
                                                                                Status = NDIS_STATUS_FAILURE;
 
2459
                                                                                break;
 
2460
                                                                        }       
 
2461
                                                                        else if ((pAdapter->FragFrame.RxSize + PacketSize) > MAX_FRAME_SIZE)
 
2462
                                                                        {
 
2463
                                                                                // Fragment frame is too large, it exeeds the maximum frame size.
 
2464
                                                                                // We have to drop it.
 
2465
                                                                                // Clear Fragment frame contents
 
2466
                                                                                memset(&pAdapter->FragFrame, 0, sizeof(FRAGMENT_FRAME));
 
2467
                                                                                Status = NDIS_STATUS_FAILURE;
 
2468
                                                                                break;
 
2469
                                                                        }
 
2470
 
 
2471
                                                                        memcpy(&pAdapter->FragFrame.Buffer[pAdapter->FragFrame.RxSize], pData, PacketSize);
 
2472
                                                                        pAdapter->FragFrame.RxSize      += PacketSize;
 
2473
                                                                        pData += PacketSize;
 
2474
                                                                        pAdapter->FragFrame.LastFrag = pHeader->Frag;           // Update fragment number
 
2475
                                                                                
 
2476
                                                                        // Last fragment
 
2477
                                                                        if (pHeader->Controlhead.Frame.MoreFrag == FALSE)
 
2478
                                                                        {
 
2479
                                                                                // For miniportTransferData
 
2480
                                                                                pAdapter->pRxData = pAdapter->FragFrame.Buffer;
 
2481
 
 
2482
                                                                                pAdapter->PortCfg.LedCntl.fRxActivity = TRUE; // for RX ACTIVITY LED
 
2483
 
 
2484
                                                                                // For TKIP frame, calculate the MIC value
 
2485
                                                                                if ((pAdapter->PortCfg.CipherAlg == CIPHER_TKIP) && (pHeader->Controlhead.Frame.Wep))
 
2486
                                                                                {
 
2487
                                                                                        if (pWpaKey == NULL)
 
2488
                                                                                        {
 
2489
                                                                                                DBGPRINT_RAW(RT_DEBUG_ERROR,"No matched TKIP in decryption done calculate MIC routine!!!\n");                                           
 
2490
                                                                                                Status = NDIS_STATUS_FAILURE;
 
2491
                                                                                                break;
 
2492
                                                                                        }
 
2493
 
 
2494
                                                                                        //
 
2495
                                                                                        // For the last fragment, we also need to copy the MIC 
 
2496
                                                                                        // to the end of pAdapter->FragFrame.Buffer
 
2497
                                                                                        // for RTMPTkipCompareMICValueWithLLC used.
 
2498
                                                                                        //
 
2499
                                                                                        pAdapter->FragFrame.RxSize -= 8; //We need to Minus MIC[8] on Fragment case.
 
2500
                                                                                                
 
2501
                                                                                        if (pAdapter->FragFrame.Flags & 0x00000001)
 
2502
                                                                                        {
 
2503
                                                                                                if (RTMPTkipCompareMICValueWithLLC(pAdapter,                                                                                                                                                            
 
2504
                                                                                                                                                                        pAdapter->FragFrame.Header_LLC,
 
2505
                                                                                                                                                                        pAdapter->FragFrame.Buffer,
 
2506
                                                                                                                                                                        pDestMac,
 
2507
                                                                                                                                                                        pSrcMac,
 
2508
                                                                                                                                                                        pWpaKey->RxMic,
 
2509
                                                                                                                                                                        pAdapter->FragFrame.RxSize) == FALSE)
 
2510
                                                                                                {
 
2511
                                                                                                DBGPRINT_RAW(RT_DEBUG_ERROR,"Rx MIC Value error 2\n");                                                  
 
2512
                                                                                                RTMPReportMicError(pAdapter, pWpaKey);
 
2513
                                                                                                Status = NDIS_STATUS_FAILURE;
 
2514
                                                                                                break;
 
2515
                                                                                                }
 
2516
                                                                                        }
 
2517
                                                                                        else
 
2518
                                                                                        {
 
2519
                                                                                                if (RTMPTkipCompareMICValue(pAdapter,                                                                                                                                           
 
2520
                                                                                                                                                        pAdapter->FragFrame.Buffer,
 
2521
                                                                                                                                                        pDestMac,
 
2522
                                                                                                                                                        pSrcMac,
 
2523
                                                                                                                                                        pWpaKey->RxMic,
 
2524
                                                                                                                                                        pAdapter->FragFrame.RxSize) == FALSE)
 
2525
                                                                                                {
 
2526
                                                                                                        DBGPRINT_RAW(RT_DEBUG_ERROR,"Rx MIC Value error 2\n");                                                  
 
2527
                                                                                                        RTMPReportMicError(pAdapter, pWpaKey);
 
2528
                                                                                                        Status = NDIS_STATUS_FAILURE;
 
2529
                                                                                                        break;
 
2530
                                                                                                }
 
2531
                                                                                        }
 
2532
                                                                                }               
 
2533
 
 
2534
                                                                                // Acknolwdge upper layer the received frame
 
2535
                                        if ((skb = dev_alloc_skb(pAdapter->FragFrame.RxSize + LENGTH_802_3 + 2)) != NULL)
 
2536
                                        {
 
2537
 
 
2538
                                            skb->dev = pAdapter->net;
 
2539
                                            skb_reserve(skb, 2);    /* 16 byte align the IP header */
 
2540
                                            memcpy(skb_put(skb, LENGTH_802_3), (PVOID) pAdapter->FragFrame.Header802_3, LENGTH_802_3);
 
2541
                                            memcpy(skb_put(skb, pAdapter->FragFrame.RxSize), (PVOID) &pAdapter->FragFrame.Buffer[0], pAdapter->FragFrame.RxSize);
 
2542
                                            skb->protocol = eth_type_trans(skb, pAdapter->net);
 
2543
                                            netif_rx(skb);
 
2544
                                            pAdapter->net->last_rx = jiffies;
 
2545
                                            pAdapter->netstats.rx_packets++;
 
2546
                                        }
 
2547
                                                                                // Increase general counters
 
2548
                                                                                pAdapter->Counters.GoodReceives++;
 
2549
 
 
2550
                                                                                // Clear Fragment frame contents
 
2551
                                                                                //memset(&pAdapter->FragFrame, 0, sizeof(FRAGMENT_FRAME));
 
2552
                                                                                DBGPRINT_RAW(RT_DEBUG_INFO, "!!! Frame with Fragment Indicated !!!\n");
 
2553
                                                                        } //Last fragment //if (pHeader->Controlhead.Frame.MoreFrag == FALSE)
 
2554
                                                                } //Middle & End of fragment burst fragments
 
2555
                                                        }//else if (pRxD->U2M)
 
2556
                                                        break;
 
2557
 
 
2558
                                                case BTYPE_MGMT:
 
2559
                                                        // Enqueue this frame to MLME engine
 
2560
                                                        MlmeEnqueueForRecv(pAdapter,
 
2561
                                                                                                &pAdapter->Mlme.Queue,
 
2562
                                                                                                (UCHAR)pRxD->BBR1,
 
2563
                                                                                                pRxD->DataByteCnt - 4,
 
2564
                                                                                                pData);
 
2565
                                                        break;
 
2566
                                
 
2567
                                                case BTYPE_CNTL:
 
2568
                                                        // Ignore ???
 
2569
                                                        break;
 
2570
                                
 
2571
                                                default :
 
2572
                                                        break;
 
2573
                                        }//switch (pHeader->Controlhead.Frame.Type)     
 
2574
                                pAdapter->RalinkCounters.RxCount ++;
 
2575
 
 
2576
                        }
 
2577
                        else if (Status == NDIS_STATUS_RESET)
 
2578
                        {
 
2579
                                RTUSBEnqueueInternalCmd(pAdapter, RT_OID_USB_RESET_BULK_IN);
 
2580
                                return;
 
2581
                        }
 
2582
                }
 
2583
 
 
2584
                pRxContext->InUse = FALSE;
 
2585
 
 
2586
        if ((!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_RESET_IN_PROGRESS)) &&
 
2587
                (!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_BULKIN_RESET)) &&
 
2588
                (!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_RADIO_OFF)) &&
 
2589
                (!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_HALT_IN_PROGRESS)) &&
 
2590
                (!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_REMOVE_IN_PROGRESS)))
 
2591
                        RTUSBBulkReceive(pAdapter);
 
2592
        }while(0);
 
2593
        
 
2594
 
 
2595
}
 
2596
/*
 
2597
        ========================================================================
 
2598
        
 
2599
        Routine Description:
 
2600
 
 
2601
        Arguments:
 
2602
 
 
2603
        Return Value:
 
2604
 
 
2605
        IRQL = 
 
2606
        
 
2607
        Note:
 
2608
        
 
2609
        ========================================================================
 
2610
*/
 
2611
VOID    RTUSBDequeueMLMEPacket(
 
2612
        IN      PRT2570ADAPTER  pAdapter)
 
2613
{
 
2614
        PMGMT_STRUC             pMgmt;
 
2615
 
 
2616
        DBGPRINT(RT_DEBUG_INFO, "RTUSBDequeueMLMEPacket\n");
 
2617
        NdisAcquireSpinLock(&pAdapter->MLMEWaitQueueLock);
 
2618
        while ((pAdapter->PopMgmtIndex != pAdapter->PushMgmtIndex) || (atomic_read(&pAdapter->MgmtQueueSize) > 0))
 
2619
        {
 
2620
                pMgmt = &pAdapter->MgmtRing[pAdapter->PopMgmtIndex];
 
2621
 
 
2622
                if (RTUSBFreeDescriptorRequest(pAdapter, PRIO_RING, 1) == NDIS_STATUS_SUCCESS)
 
2623
                {
 
2624
                        atomic_dec(&pAdapter->MgmtQueueSize);
 
2625
                        pAdapter->PopMgmtIndex = (pAdapter->PopMgmtIndex + 1) % MGMT_RING_SIZE;
 
2626
                        NdisReleaseSpinLock(&pAdapter->MLMEWaitQueueLock);
 
2627
 
 
2628
                        RTUSBMlmeHardTransmit(pAdapter, pMgmt);
 
2629
 
 
2630
                        MlmeFreeMemory(pAdapter, pMgmt->pBuffer);
 
2631
                        pMgmt->pBuffer = NULL;
 
2632
                        pMgmt->Valid = FALSE;
 
2633
 
 
2634
                        NdisAcquireSpinLock(&pAdapter->MLMEWaitQueueLock);
 
2635
                }
 
2636
                else
 
2637
                {
 
2638
                        DBGPRINT(RT_DEBUG_TRACE, "RTUSBDequeueMLMEPacket::PrioRingFirstIndex = %d, PrioRingTxCnt = %d, PopMgmtIndex = %d, PushMgmtIndex = %d, NextMLMEIndex = %d\n", 
 
2639
                        pAdapter->PrioRingFirstIndex, pAdapter->PrioRingTxCnt, 
 
2640
                        pAdapter->PopMgmtIndex, pAdapter->PushMgmtIndex, pAdapter->NextMLMEIndex);      
 
2641
                        break;
 
2642
                }
 
2643
        }
 
2644
        NdisReleaseSpinLock(&pAdapter->MLMEWaitQueueLock);
 
2645
}    
 
2646
 
 
2647
/*
 
2648
        ========================================================================
 
2649
        
 
2650
        Routine Description:
 
2651
 
 
2652
        Arguments:
 
2653
 
 
2654
        Return Value:
 
2655
 
 
2656
        IRQL = 
 
2657
        
 
2658
        Note:
 
2659
        
 
2660
        ========================================================================
 
2661
*/
 
2662
VOID    RTUSBCleanUpMLMEWaitQueue(
 
2663
        IN      PRT2570ADAPTER  pAdapter)
 
2664
{
 
2665
        PMGMT_STRUC             pMgmt;
 
2666
 
 
2667
        DBGPRINT(RT_DEBUG_TRACE, "--->CleanUpMLMEWaitQueue\n");
 
2668
 
 
2669
        NdisAcquireSpinLock(&pAdapter->MLMEWaitQueueLock);
 
2670
        while (pAdapter->PopMgmtIndex != pAdapter->PushMgmtIndex)
 
2671
        {
 
2672
                pMgmt = (PMGMT_STRUC)&pAdapter->MgmtRing[pAdapter->PopMgmtIndex];
 
2673
                MlmeFreeMemory(pAdapter, pMgmt->pBuffer);
 
2674
                pMgmt->pBuffer = NULL;
 
2675
                pMgmt->Valid = FALSE;
 
2676
                atomic_dec(&pAdapter->MgmtQueueSize);
 
2677
 
 
2678
                pAdapter->PopMgmtIndex++;
 
2679
                if (pAdapter->PopMgmtIndex >= MGMT_RING_SIZE)
 
2680
                {
 
2681
                        pAdapter->PopMgmtIndex = 0;
 
2682
                }
 
2683
        }
 
2684
        NdisReleaseSpinLock(&pAdapter->MLMEWaitQueueLock);
 
2685
 
 
2686
        DBGPRINT(RT_DEBUG_TRACE, "<---CleanUpMLMEWaitQueue\n");
 
2687
}
 
2688
 
 
2689
 
 
2690
/*
 
2691
        ========================================================================
 
2692
 
 
2693
        Routine Description:
 
2694
                API for MLME to transmit management frame to AP (BSS Mode)
 
2695
        or station (IBSS Mode)
 
2696
        
 
2697
        Arguments:
 
2698
                pAdapter        Pointer to our adapter
 
2699
                Buffer          Pointer to  memory of outgoing frame
 
2700
                Length          Size of outgoing management frame
 
2701
                
 
2702
        Return Value:
 
2703
                NDIS_STATUS_FAILURE
 
2704
                NDIS_STATUS_PENDING
 
2705
                NDIS_STATUS_SUCCESS
 
2706
 
 
2707
        Note:
 
2708
        
 
2709
        ========================================================================
 
2710
*/
 
2711
VOID    MiniportMMRequest(
 
2712
        IN      PRT2570ADAPTER  pAdapter,
 
2713
        IN      PVOID                   pBuffer,
 
2714
        IN      ULONG                   Length)
 
2715
{
 
2716
 
 
2717
        if (pBuffer)
 
2718
        {
 
2719
                PMGMT_STRUC     pMgmt;
 
2720
 
 
2721
                // Check management ring free avaliability
 
2722
                NdisAcquireSpinLock(&pAdapter->MLMEWaitQueueLock);
 
2723
                pMgmt = (PMGMT_STRUC)&pAdapter->MgmtRing[pAdapter->PushMgmtIndex];
 
2724
                // This management cell has been occupied
 
2725
                if (pMgmt->Valid == TRUE)
 
2726
                {
 
2727
                        NdisReleaseSpinLock(&pAdapter->MLMEWaitQueueLock);
 
2728
                        MlmeFreeMemory(pAdapter, pBuffer);
 
2729
                        pAdapter->RalinkCounters.MgmtRingFullCount++;
 
2730
                        DBGPRINT_RAW(RT_DEBUG_WARN, "MiniportMMRequest (error:: MgmtRing full)\n");
 
2731
                }
 
2732
                // Insert this request into software managemnet ring
 
2733
                else
 
2734
                {
 
2735
                        pMgmt->pBuffer = pBuffer;
 
2736
                        pMgmt->Length  = Length;
 
2737
                        pMgmt->Valid   = TRUE;
 
2738
                        pAdapter->PushMgmtIndex++;
 
2739
                        atomic_inc(&pAdapter->MgmtQueueSize);
 
2740
                        if (pAdapter->PushMgmtIndex >= MGMT_RING_SIZE)
 
2741
                        {
 
2742
                                pAdapter->PushMgmtIndex = 0;
 
2743
                        }
 
2744
                        NdisReleaseSpinLock(&pAdapter->MLMEWaitQueueLock);
 
2745
                }
 
2746
        }
 
2747
        else
 
2748
                DBGPRINT(RT_DEBUG_WARN, "MiniportMMRequest (error:: NULL msg)\n");
 
2749
        
 
2750
        RTUSBDequeueMLMEPacket(pAdapter);
 
2751
        
 
2752
        // If pAdapter->PrioRingTxCnt is larger than 0, this means that prio_ring have something to transmit.
 
2753
        // Then call KickBulkOut to transmit it
 
2754
        if (pAdapter->PrioRingTxCnt > 0)
 
2755
        {
 
2756
                DBGPRINT(RT_DEBUG_INFO, "MiniportMMRequest::PrioRingFirstIndex = %d, PrioRingTxCnt = %d, PopMgmtIndex = %d, PushMgmtIndex = %d, NextMLMEIndex = %d\n", 
 
2757
                pAdapter->PrioRingFirstIndex, pAdapter->PrioRingTxCnt, 
 
2758
                pAdapter->PopMgmtIndex, pAdapter->PushMgmtIndex, pAdapter->NextMLMEIndex);      
 
2759
 
 
2760
                AsicForceWakeup(pAdapter);
 
2761
                RTUSBKickBulkOut(pAdapter);
 
2762
        }
 
2763
        
 
2764
}
 
2765
 
 
2766
/*
 
2767
        ========================================================================
 
2768
 
 
2769
        Routine Description:
 
2770
                Search tuple cache for receive duplicate frame from unicast frames.
 
2771
                
 
2772
        Arguments:
 
2773
                pAdapter                Pointer to our adapter
 
2774
                pHeader                 802.11 header of receiving frame
 
2775
                
 
2776
        Return Value:
 
2777
                TRUE                    found matched tuple cache
 
2778
                FALSE                   no matched found
 
2779
 
 
2780
        Note:
 
2781
        
 
2782
        ========================================================================
 
2783
*/
 
2784
BOOLEAN RTMPSearchTupleCache(
 
2785
        IN      PRT2570ADAPTER  pAdapter,
 
2786
        IN      PHEADER_802_11  pHeader)
 
2787
{
 
2788
        INT     Index;
 
2789
 
 
2790
        for (Index = 0; Index < MAX_CLIENT; Index++)
 
2791
        {
 
2792
                if (pAdapter->TupleCache[Index].Valid == FALSE)
 
2793
                    continue;
 
2794
                
 
2795
                if ((memcmp(&pAdapter->TupleCache[Index].MAC, &pHeader->Controlhead.Addr2, 6)== 0) &&
 
2796
                        (pAdapter->TupleCache[Index].Sequence == pHeader->Sequence) &&
 
2797
                        (pAdapter->TupleCache[Index].Frag == pHeader->Frag))
 
2798
                {
 
2799
                        return (TRUE);
 
2800
                }
 
2801
        }
 
2802
        return (FALSE);
 
2803
}
 
2804
 
 
2805
/*
 
2806
        ========================================================================
 
2807
 
 
2808
        Routine Description:
 
2809
                Update tuple cache for new received unicast frames.
 
2810
                
 
2811
        Arguments:
 
2812
                pAdapter                Pointer to our adapter
 
2813
                pHeader                 802.11 header of receiving frame
 
2814
                
 
2815
        Return Value:
 
2816
                None
 
2817
                
 
2818
        Note:
 
2819
        
 
2820
        ========================================================================
 
2821
*/
 
2822
VOID    RTMPUpdateTupleCache(
 
2823
        IN      PRT2570ADAPTER  pAdapter,
 
2824
        IN      PHEADER_802_11  pHeader)
 
2825
{
 
2826
        UCHAR   Index;
 
2827
 
 
2828
        for (Index = 0; Index < MAX_CLIENT; Index++)
 
2829
        {
 
2830
                if (pAdapter->TupleCache[Index].Valid == FALSE)
 
2831
                {
 
2832
                        // Add new entry
 
2833
                        memcpy(&pAdapter->TupleCache[Index].MAC, &pHeader->Controlhead.Addr2, 6);
 
2834
                        pAdapter->TupleCache[Index].Sequence = pHeader->Sequence;
 
2835
                        pAdapter->TupleCache[Index].Frag     = pHeader->Frag;
 
2836
                        pAdapter->TupleCache[Index].Valid    = TRUE;
 
2837
                        pAdapter->TupleCacheLastUpdateIndex  = Index;
 
2838
                        DBGPRINT(RT_DEBUG_INFO,"DUPCHECK - Add Entry %d, MAC=%02x:%02x:%02x:%02x:%02x:%02x\n", 
 
2839
                            Index, pAdapter->TupleCache[Index].MAC.Octet[0], pAdapter->TupleCache[Index].MAC.Octet[1],
 
2840
                            pAdapter->TupleCache[Index].MAC.Octet[2], pAdapter->TupleCache[Index].MAC.Octet[3],
 
2841
                            pAdapter->TupleCache[Index].MAC.Octet[4], pAdapter->TupleCache[Index].MAC.Octet[5]);
 
2842
                        return;
 
2843
                }
 
2844
                else if ((memcmp(&pAdapter->TupleCache[Index].MAC, &pHeader->Controlhead.Addr2, 6)== 0))
 
2845
                {
 
2846
                        // Update old entry
 
2847
                        pAdapter->TupleCache[Index].Sequence = pHeader->Sequence;
 
2848
                        pAdapter->TupleCache[Index].Frag     = pHeader->Frag;
 
2849
                        return;
 
2850
                }
 
2851
        }
 
2852
 
 
2853
    // tuple cache full, replace the first inserted one (even though it may not be
 
2854
    // least referenced one)
 
2855
        if (Index == MAX_CLIENT)
 
2856
        {
 
2857
            pAdapter->TupleCacheLastUpdateIndex ++;
 
2858
            if (pAdapter->TupleCacheLastUpdateIndex >= MAX_CLIENT)
 
2859
                pAdapter->TupleCacheLastUpdateIndex = 0;
 
2860
            Index = pAdapter->TupleCacheLastUpdateIndex;
 
2861
 
 
2862
                // replace with new entry
 
2863
                memcpy(&pAdapter->TupleCache[Index].MAC, &pHeader->Controlhead.Addr2, 6);
 
2864
                pAdapter->TupleCache[Index].Sequence = pHeader->Sequence;
 
2865
                pAdapter->TupleCache[Index].Frag     = pHeader->Frag;
 
2866
                pAdapter->TupleCache[Index].Valid    = TRUE;
 
2867
                DBGPRINT(RT_DEBUG_INFO,"DUPCHECK - replace Entry %d, MAC=%02x:%02x:%02x:%02x:%02x:%02x\n", 
 
2868
                    Index, pAdapter->TupleCache[Index].MAC.Octet[0], pAdapter->TupleCache[Index].MAC.Octet[1],
 
2869
                    pAdapter->TupleCache[Index].MAC.Octet[2], pAdapter->TupleCache[Index].MAC.Octet[3],
 
2870
                    pAdapter->TupleCache[Index].MAC.Octet[4], pAdapter->TupleCache[Index].MAC.Octet[5]);
 
2871
        }
 
2872
}
 
2873
 
 
2874
/*
 
2875
        ========================================================================
 
2876
 
 
2877
        Routine Description:
 
2878
                Apply packet filter policy, return NDIS_STATUS_FAILURE if this frame
 
2879
                should be dropped.
 
2880
                
 
2881
        Arguments:
 
2882
                pAdapter                Pointer to our adapter
 
2883
                pRxD                    Pointer to the Rx descriptor
 
2884
                pHeader                 Pointer to the 802.11 frame header
 
2885
                
 
2886
        Return Value:
 
2887
                NDIS_STATUS_SUCCESS             Accept frame
 
2888
                NDIS_STATUS_FAILURE             Drop Frame
 
2889
                
 
2890
        Note:
 
2891
                Maganement frame should bypass this filtering rule.
 
2892
        
 
2893
        ========================================================================
 
2894
*/
 
2895
NDIS_STATUS     RTMPApplyPacketFilter(
 
2896
        IN      PRT2570ADAPTER  pAdapter, 
 
2897
        IN      PRXD_STRUC              pRxD, 
 
2898
        IN      PHEADER_802_11  pHeader)
 
2899
{
 
2900
        UCHAR   i;
 
2901
        
 
2902
        // 0. Management frame should bypass all these filtering rules.
 
2903
        if (pHeader->Controlhead.Frame.Type == BTYPE_MGMT)
 
2904
        {
 
2905
                if ((pRxD->U2M) || (pRxD->Bcast) || (pRxD->Mcast))//steven:for ASIC Bug Workaround
 
2906
                return(NDIS_STATUS_SUCCESS);
 
2907
        }
 
2908
        
 
2909
        // 0.1  Drop all Rx frames if MIC countermeasures kicks in
 
2910
        if (pAdapter->PortCfg.MicErrCnt >= 2)
 
2911
        {
 
2912
                return(NDIS_STATUS_FAILURE);
 
2913
        }
 
2914
        
 
2915
        // 1. Drop unicast to me packet if NDIS_PACKET_TYPE_DIRECTED is FALSE
 
2916
        if (pRxD->U2M)
 
2917
        {
 
2918
                if (pAdapter->bAcceptDirect == FALSE)
 
2919
                {
 
2920
                        DBGPRINT_RAW(RT_DEBUG_INFO, "unicast not accepted\n");//steven:for debug
 
2921
                        return(NDIS_STATUS_FAILURE);
 
2922
                }
 
2923
        }
 
2924
                
 
2925
        // 2. Drop broadcast packet if NDIS_PACKET_TYPE_BROADCAST is FALSE
 
2926
        else if (pRxD->Bcast)
 
2927
        {
 
2928
                if (pAdapter->bAcceptBroadcast == FALSE)
 
2929
                {
 
2930
                        DBGPRINT(RT_DEBUG_INFO, "broadcast not accepted\n");//steven:for debug
 
2931
                        return(NDIS_STATUS_FAILURE);
 
2932
                }
 
2933
        }
 
2934
                        
 
2935
        // 3. Drop multicast packet if NDIS_PACKET_TYPE_ALL_MULTICAST is false
 
2936
        //    and NDIS_PACKET_TYPE_MULTICAST is false.
 
2937
        //    If NDIS_PACKET_TYPE_MULTICAST is true, but NDIS_PACKET_TYPE_ALL_MULTICAST is false.
 
2938
        //    We have to deal with multicast table lookup & drop not matched packets.
 
2939
        else if (pRxD->Mcast)
 
2940
        {
 
2941
                if (pAdapter->bAcceptAllMulticast == FALSE)
 
2942
                {
 
2943
                        if (pAdapter->bAcceptMulticast == FALSE)
 
2944
                        {
 
2945
                                DBGPRINT_RAW(RT_DEBUG_INFO, "multicast not accepted\n");//steven:for debug
 
2946
                                return(NDIS_STATUS_FAILURE);
 
2947
                        }
 
2948
                        else
 
2949
                        {
 
2950
                                // Selected accept multicast packet based on multicast table
 
2951
                                for (i = 0; i < pAdapter->NumberOfMcAddresses; i++)
 
2952
                                {
 
2953
                                        if ((memcmp(&pHeader->Controlhead.Addr1, pAdapter->McastTable[i], ETH_LENGTH_OF_ADDRESS)== 0))
 
2954
                                        {
 
2955
                                                break;          // Matched
 
2956
                                        }
 
2957
                                }
 
2958
 
 
2959
                                // Not matched
 
2960
                                if (i == pAdapter->NumberOfMcAddresses)
 
2961
                                {
 
2962
                                        DBGPRINT(RT_DEBUG_INFO,"Drop multicast %02x:%02x:%02x:%02x:%02x:%02x\n",
 
2963
                                                pHeader->Controlhead.Addr1.Octet[0], pHeader->Controlhead.Addr1.Octet[1],
 
2964
                                                pHeader->Controlhead.Addr1.Octet[2], pHeader->Controlhead.Addr1.Octet[3],
 
2965
                                                pHeader->Controlhead.Addr1.Octet[4], pHeader->Controlhead.Addr1.Octet[5]);
 
2966
                                        DBGPRINT(RT_DEBUG_LOUD, "multicast not matched\n");
 
2967
                                        return(NDIS_STATUS_FAILURE);
 
2968
                                }
 
2969
                                else
 
2970
                                {
 
2971
                                        DBGPRINT(RT_DEBUG_INFO,"Accept multicast %02x:%02x:%02x:%02x:%02x:%02x\n",
 
2972
                                                pHeader->Controlhead.Addr1.Octet[0], pHeader->Controlhead.Addr1.Octet[1],
 
2973
                                                pHeader->Controlhead.Addr1.Octet[2], pHeader->Controlhead.Addr1.Octet[3],
 
2974
                                                pHeader->Controlhead.Addr1.Octet[4], pHeader->Controlhead.Addr1.Octet[5]);
 
2975
                                }
 
2976
                        }
 
2977
                }
 
2978
        }
 
2979
 
 
2980
        // 4. Not U2M, not Mcast, not Bcast, must be unicast to other DA.
 
2981
        //    Since we did not implement promiscuous mode, just drop this kind of packet for now.
 
2982
        else
 
2983
        {
 
2984
                DBGPRINT_RAW(RT_DEBUG_TRACE, "not-to-me unicast\n");//steven:for debug
 
2985
                return(NDIS_STATUS_FAILURE);
 
2986
        }
 
2987
        
 
2988
        return(NDIS_STATUS_SUCCESS);    
 
2989
}
 
2990
 
 
2991
/*
 
2992
        ========================================================================
 
2993
 
 
2994
        Routine Description:
 
2995
                Check Rx descriptor, return NDIS_STATUS_FAILURE if any error dound
 
2996
                
 
2997
        Arguments:
 
2998
                pRxD            Pointer to the Rx descriptor
 
2999
                
 
3000
        Return Value:
 
3001
                NDIS_STATUS_SUCCESS             No err
 
3002
                NDIS_STATUS_FAILURE             Error
 
3003
                
 
3004
        Note:
 
3005
        
 
3006
        ========================================================================
 
3007
*/
 
3008
NDIS_STATUS     RTMPCheckRxDescriptor(
 
3009
        IN      PRT2570ADAPTER  pAdapter,
 
3010
        IN      PRXD_STRUC      pRxD)
 
3011
{
 
3012
        // Phy errors
 
3013
        if (pRxD->PhyErr)
 
3014
        {
 
3015
                DBGPRINT_RAW(RT_DEBUG_ERROR, "pRxD->PhyErr 0x%x, 0x%x, 0x%x, 0x%x\n", *(ULONG*)pRxD, *((ULONG*)pRxD+1), *((ULONG*)pRxD+2), *((ULONG*)pRxD+3));
 
3016
                return(NDIS_STATUS_FAILURE);
 
3017
        }
 
3018
        
 
3019
        // CRC errors
 
3020
        if (pRxD->Crc)
 
3021
        {
 
3022
                DBGPRINT_RAW(RT_DEBUG_ERROR, "pRxD->Crc\n");
 
3023
                return(NDIS_STATUS_FAILURE);
 
3024
        }
 
3025
        
 
3026
        // Paul 04-03 for OFDM Rx length issue
 
3027
        if (pRxD->DataByteCnt > 1604)
 
3028
        {
 
3029
                DBGPRINT_RAW(RT_DEBUG_ERROR, "received too long, DataByteCnt = %d\n", pRxD->DataByteCnt);
 
3030
                return NDIS_STATUS_RESET;
 
3031
        }
 
3032
 
 
3033
        return(NDIS_STATUS_SUCCESS);
 
3034
}
 
3035
 
 
3036
/*
 
3037
        ========================================================================
 
3038
 
 
3039
        Routine Description:
 
3040
                Process MIC error indication and record MIC error timer.
 
3041
                
 
3042
        Arguments:
 
3043
                pAdapter                Pointer to our adapter
 
3044
                pWpaKey                 Pointer to the WPA key structure
 
3045
                
 
3046
        Return Value:
 
3047
                None
 
3048
                
 
3049
        Note:
 
3050
        
 
3051
        ========================================================================
 
3052
*/
 
3053
VOID    RTMPReportMicError(
 
3054
        IN      PRT2570ADAPTER  pAdapter, 
 
3055
        IN      PWPA_KEY                pWpaKey)
 
3056
{
 
3057
        ULONG   Now;
 
3058
        struct
 
3059
        {
 
3060
                NDIS_802_11_STATUS_INDICATION           Status;
 
3061
                NDIS_802_11_AUTHENTICATION_REQUEST      Request;
 
3062
        }       Report;
 
3063
 
 
3064
        // 0. Set Status to indicate auth error
 
3065
        Report.Status.StatusType = Ndis802_11StatusType_Authentication;
 
3066
        
 
3067
        // 1. Check for Group or Pairwise MIC error
 
3068
        if (pWpaKey->Type == PAIRWISE_KEY)
 
3069
                Report.Request.Flags = NDIS_802_11_AUTH_REQUEST_PAIRWISE_ERROR;
 
3070
        else
 
3071
                Report.Request.Flags = NDIS_802_11_AUTH_REQUEST_GROUP_ERROR;
 
3072
 
 
3073
        // 2. Copy AP MAC address
 
3074
        memcpy(Report.Request.Bssid, pWpaKey->BssId, 6);
 
3075
 
 
3076
        // 3. Calculate length
 
3077
        Report.Request.Length = sizeof(NDIS_802_11_AUTHENTICATION_REQUEST);
 
3078
 
 
3079
        // 4. Indicate to NDIS
 
3080
        NdisMIndicateStatus(pAdapter->AdapterHandle, NDIS_STATUS_MEDIA_SPECIFIC_INDICATION, (PVOID) &Report, sizeof(Report));
 
3081
        NdisMIndicateStatusComplete(pAdapter->AdapterHandle);
 
3082
 
 
3083
        // 5. Record Last MIC error time and count
 
3084
        Now = jiffies;
 
3085
        if (pAdapter->PortCfg.MicErrCnt == 0)
 
3086
        {
 
3087
                pAdapter->PortCfg.MicErrCnt++;
 
3088
                pAdapter->PortCfg.LastMicErrorTime = Now;
 
3089
        }
 
3090
        else if (pAdapter->PortCfg.MicErrCnt == 1)
 
3091
        {
 
3092
                if (time_after((unsigned long)Now, (unsigned long)(pAdapter->PortCfg.LastMicErrorTime + (60 * 1000))))
 
3093
                {
 
3094
                        // Update Last MIC error time, this did not violate two MIC errors within 60 seconds
 
3095
                        pAdapter->PortCfg.LastMicErrorTime = Now;                       
 
3096
                }
 
3097
                else
 
3098
                {
 
3099
                        pAdapter->PortCfg.LastMicErrorTime = Now;                       
 
3100
                        // Violate MIC error counts, MIC countermeasures kicks in
 
3101
                        pAdapter->PortCfg.MicErrCnt++;                  
 
3102
                        // We shall block all reception
 
3103
                        // We shall clean all Tx ring and disassoicate from AP after next EAPOL frame
 
3104
                        RTUSBRejectPendingPackets(pAdapter);
 
3105
                        RTUSBCleanUpDataBulkOutQueue(pAdapter);
 
3106
                }
 
3107
        }
 
3108
        else
 
3109
        {
 
3110
                // MIC error count >= 2
 
3111
                // This should not happen
 
3112
                ;
 
3113
        }
 
3114
}
 
3115
/*
 
3116
        ========================================================================
 
3117
 
 
3118
        Routine Description:
 
3119
                Copy frame from waiting queue into relative ring buffer and set 
 
3120
        appropriate ASIC register to kick hardware transmit function
 
3121
        
 
3122
        Arguments:
 
3123
                pAdapter        Pointer to our adapter
 
3124
                pBuffer         Pointer to  memory of outgoing frame
 
3125
                Length          Size of outgoing management frame
 
3126
                
 
3127
        Return Value:
 
3128
                NDIS_STATUS_FAILURE
 
3129
                NDIS_STATUS_PENDING
 
3130
                NDIS_STATUS_SUCCESS
 
3131
 
 
3132
        Note:
 
3133
        
 
3134
        ========================================================================
 
3135
*/
 
3136
VOID    RTUSBMlmeHardTransmit(
 
3137
        IN      PRT2570ADAPTER  pAdapter,
 
3138
        IN      PMGMT_STRUC             pMgmt)
 
3139
{
 
3140
        PTX_CONTEXT             pMLMEContext;
 
3141
        PTXD_STRUC              pTxD;
 
3142
        PUCHAR                  pDest;  
 
3143
        PHEADER_802_11  pHeader_802_11;
 
3144
        BOOLEAN         AckRequired, InsertTimestamp;
 
3145
        ULONG                   TransferBufferLength;
 
3146
        PVOID                   pBuffer = pMgmt->pBuffer;
 
3147
        ULONG                   Length = pMgmt->Length;
 
3148
        
 
3149
        DBGPRINT_RAW(RT_DEBUG_INFO, "--->MlmeHardTransmit\n");
 
3150
        
 
3151
        pAdapter->PrioRingTxCnt++;
 
3152
 
 
3153
        pMLMEContext = &pAdapter->MLMEContext[pAdapter->NextMLMEIndex];
 
3154
        pMLMEContext->InUse = TRUE;
 
3155
 
 
3156
        // Increase & maintain Tx Ring Index
 
3157
        pAdapter->NextMLMEIndex++;
 
3158
        if (pAdapter->NextMLMEIndex >= PRIO_RING_SIZE)
 
3159
        {
 
3160
                pAdapter->NextMLMEIndex = 0;
 
3161
        }
 
3162
 
 
3163
        pDest                           = pMLMEContext->TransferBuffer->WirelessPacket;              
 
3164
        pTxD                            = (PTXD_STRUC)(pMLMEContext->TransferBuffer);
 
3165
        memset(pTxD, 0, sizeof(TXD_STRUC));
 
3166
        
 
3167
        pHeader_802_11 = (PHEADER_802_11) pBuffer;
 
3168
        InsertTimestamp = FALSE;
 
3169
        if (pHeader_802_11->Controlhead.Frame.Type == BTYPE_CNTL) // must be PS-POLL
 
3170
        {
 
3171
                AckRequired = FALSE;
 
3172
        }
 
3173
        else // BTYPE_MGMT or BMGMT_DATA(must be NULL frame)
 
3174
        {
 
3175
                pAdapter->Sequence       = ((pAdapter->Sequence) + 1) & (MAX_SEQ_NUMBER);
 
3176
                pHeader_802_11->Sequence = pAdapter->Sequence;
 
3177
 
 
3178
                if (pHeader_802_11->Controlhead.Addr1.Octet[0] & 0x01) // MULTICAST, BROADCAST
 
3179
                {
 
3180
                        INC_COUNTER(pAdapter->WlanCounters.MulticastTransmittedFrameCount);
 
3181
                        AckRequired = FALSE;
 
3182
                        pHeader_802_11->Controlhead.Duration = 0;
 
3183
                }
 
3184
                else
 
3185
                {
 
3186
                        AckRequired = TRUE;
 
3187
                        pHeader_802_11->Controlhead.Duration = RTUSBCalcDuration(pAdapter, pAdapter->PortCfg.MlmeRate, 14);
 
3188
                        if (pHeader_802_11->Controlhead.Frame.Subtype == SUBTYPE_PROBE_RSP)
 
3189
                        {
 
3190
                                InsertTimestamp = TRUE;
 
3191
                        }
 
3192
                }
 
3193
        }
 
3194
        
 
3195
        memcpy(pDest, pBuffer, Length);
 
3196
   
 
3197
        // Initialize Priority Descriptor
 
3198
        // For inter-frame gap, the number is for this frame and next frame
 
3199
        // For MLME rate, we will fix as 2Mb to match other vendor's implement
 
3200
        RTUSBWriteTxDescriptor(pTxD, FALSE, 0, AckRequired, InsertTimestamp,
 
3201
                TRUE, IFS_BACKOFF, Length, FALSE, 0, CW_MIN_IN_BITS, CW_MAX_IN_BITS,
 
3202
                Length + 4, pAdapter->PortCfg.MlmeRate, 4, pAdapter->PortCfg.TxPreambleInUsed);
 
3203
 
 
3204
        // Build our URB for USBD
 
3205
        TransferBufferLength = sizeof(TXD_STRUC) + Length;
 
3206
        if ((TransferBufferLength % 2) == 1)
 
3207
                TransferBufferLength++;
 
3208
        if ((TransferBufferLength % pAdapter->BulkOutMaxPacketSize) == 0)
 
3209
                TransferBufferLength += 2;
 
3210
        
 
3211
        pMLMEContext->BulkOutSize = TransferBufferLength;
 
3212
        RTUSB_SET_BULK_FLAG(pAdapter, fRTUSB_BULK_OUT_MLME);
 
3213
        
 
3214
        DBGPRINT(RT_DEBUG_INFO, "<---MlmeHardTransmit\n");
 
3215
}   
 
3216