Staging: rtxxx0: debug messages fixes
[firefly-linux-kernel-4.4.55.git] / drivers / staging / rt2870 / common / 2870_rtmp_init.c
1 /*
2  *************************************************************************
3  * Ralink Tech Inc.
4  * 5F., No.36, Taiyuan St., Jhubei City,
5  * Hsinchu County 302,
6  * Taiwan, R.O.C.
7  *
8  * (c) Copyright 2002-2007, Ralink Technology, Inc.
9  *
10  * This program is free software; you can redistribute it and/or modify  *
11  * it under the terms of the GNU General Public License as published by  *
12  * the Free Software Foundation; either version 2 of the License, or     *
13  * (at your option) any later version.                                   *
14  *                                                                       *
15  * This program is distributed in the hope that it will be useful,       *
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
18  * GNU General Public License for more details.                          *
19  *                                                                       *
20  * You should have received a copy of the GNU General Public License     *
21  * along with this program; if not, write to the                         *
22  * Free Software Foundation, Inc.,                                       *
23  * 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
24  *                                                                       *
25  *************************************************************************
26
27         Module Name:
28         2870_rtmp_init.c
29
30         Abstract:
31         Miniport generic portion header file
32
33         Revision History:
34         Who         When          What
35         --------    ----------    ----------------------------------------------
36         Paul Lin    2002-08-01    created
37     John Chang  2004-08-20    RT2561/2661 use scatter-gather scheme
38     Jan Lee     2006-09-15    RT2860. Change for 802.11n , EEPROM, Led, BA, HT.
39         Sample Lin      2007-05-31    Merge RT2860 and RT2870 drivers.
40 */
41
42 #include "../rt_config.h"
43
44
45 static void rx_done_tasklet(unsigned long data);
46 static void rt2870_hcca_dma_done_tasklet(unsigned long data);
47 static void rt2870_ac3_dma_done_tasklet(unsigned long data);
48 static void rt2870_ac2_dma_done_tasklet(unsigned long data);
49 static void rt2870_ac1_dma_done_tasklet(unsigned long data);
50 static void rt2870_ac0_dma_done_tasklet(unsigned long data);
51 static void rt2870_mgmt_dma_done_tasklet(unsigned long data);
52 static void rt2870_null_frame_complete_tasklet(unsigned long data);
53 static void rt2870_rts_frame_complete_tasklet(unsigned long data);
54 static void rt2870_pspoll_frame_complete_tasklet(unsigned long data);
55 static void rt2870_dataout_complete_tasklet(unsigned long data);
56
57
58 /*
59 ========================================================================
60 Routine Description:
61     Initialize receive data structures.
62
63 Arguments:
64     pAd                                 Pointer to our adapter
65
66 Return Value:
67         NDIS_STATUS_SUCCESS
68         NDIS_STATUS_RESOURCES
69
70 Note:
71         Initialize all receive releated private buffer, include those define
72         in RTMP_ADAPTER structure and all private data structures. The mahor
73         work is to allocate buffer for each packet and chain buffer to
74         NDIS packet descriptor.
75 ========================================================================
76 */
77 NDIS_STATUS     NICInitRecv(
78         IN      PRTMP_ADAPTER   pAd)
79 {
80         UCHAR                           i;
81         NDIS_STATUS                     Status = NDIS_STATUS_SUCCESS;
82         POS_COOKIE                      pObj = (POS_COOKIE) pAd->OS_Cookie;
83
84
85         DBGPRINT(RT_DEBUG_TRACE, ("--> NICInitRecv\n"));
86         pObj = pObj;
87
88         //InterlockedExchange(&pAd->PendingRx, 0);
89         pAd->PendingRx = 0;
90         pAd->NextRxBulkInReadIndex      = 0;    // Next Rx Read index
91         pAd->NextRxBulkInIndex          = 0 ; //RX_RING_SIZE -1; // Rx Bulk pointer
92         pAd->NextRxBulkInPosition       = 0;
93
94         for (i = 0; i < (RX_RING_SIZE); i++)
95         {
96                 PRX_CONTEXT  pRxContext = &(pAd->RxContext[i]);
97
98                 //Allocate URB
99                 pRxContext->pUrb = RTUSB_ALLOC_URB(0);
100                 if (pRxContext->pUrb == NULL)
101                 {
102                         Status = NDIS_STATUS_RESOURCES;
103                         goto out1;
104                 }
105
106                 // Allocate transfer buffer
107                 pRxContext->TransferBuffer = RTUSB_URB_ALLOC_BUFFER(pObj->pUsb_Dev, MAX_RXBULK_SIZE, &pRxContext->data_dma);
108                 if (pRxContext->TransferBuffer == NULL)
109                 {
110                         Status = NDIS_STATUS_RESOURCES;
111                         goto out1;
112                 }
113
114                 NdisZeroMemory(pRxContext->TransferBuffer, MAX_RXBULK_SIZE);
115
116                 pRxContext->pAd = pAd;
117                 pRxContext->pIrp = NULL;
118                 pRxContext->InUse               = FALSE;
119                 pRxContext->IRPPending  = FALSE;
120                 pRxContext->Readable    = FALSE;
121                 //pRxContext->ReorderInUse = FALSE;
122                 pRxContext->bRxHandling = FALSE;
123                 pRxContext->BulkInOffset = 0;
124         }
125
126         DBGPRINT(RT_DEBUG_TRACE, ("<-- NICInitRecv\n"));
127         return Status;
128
129 out1:
130         for (i = 0; i < (RX_RING_SIZE); i++)
131         {
132                 PRX_CONTEXT  pRxContext = &(pAd->RxContext[i]);
133
134                 if (NULL != pRxContext->TransferBuffer)
135                 {
136                         RTUSB_URB_FREE_BUFFER(pObj->pUsb_Dev, MAX_RXBULK_SIZE,
137                                                                 pRxContext->TransferBuffer, pRxContext->data_dma);
138                         pRxContext->TransferBuffer = NULL;
139                 }
140
141                 if (NULL != pRxContext->pUrb)
142                 {
143                         RTUSB_UNLINK_URB(pRxContext->pUrb);
144                         RTUSB_FREE_URB(pRxContext->pUrb);
145                         pRxContext->pUrb = NULL;
146                 }
147         }
148
149         return Status;
150 }
151
152
153 /*
154 ========================================================================
155 Routine Description:
156     Initialize transmit data structures.
157
158 Arguments:
159     pAd                                 Pointer to our adapter
160
161 Return Value:
162         NDIS_STATUS_SUCCESS
163         NDIS_STATUS_RESOURCES
164
165 Note:
166 ========================================================================
167 */
168 NDIS_STATUS     NICInitTransmit(
169         IN      PRTMP_ADAPTER   pAd)
170 {
171 #define LM_USB_ALLOC(pObj, Context, TB_Type, BufferSize, Status, msg1, err1, msg2, err2)        \
172         Context->pUrb = RTUSB_ALLOC_URB(0);             \
173         if (Context->pUrb == NULL) {                    \
174                 DBGPRINT(RT_DEBUG_ERROR, msg1);         \
175                 Status = NDIS_STATUS_RESOURCES;         \
176                 goto err1; }                                            \
177                                                                                         \
178         Context->TransferBuffer =                               \
179                 (TB_Type)RTUSB_URB_ALLOC_BUFFER(pObj->pUsb_Dev, BufferSize, &Context->data_dma);        \
180         if (Context->TransferBuffer == NULL) {  \
181                 DBGPRINT(RT_DEBUG_ERROR, msg2);         \
182                 Status = NDIS_STATUS_RESOURCES;         \
183                 goto err2; }
184
185 #define LM_URB_FREE(pObj, Context, BufferSize)                          \
186         if (NULL != Context->pUrb) {                                                    \
187                 RTUSB_UNLINK_URB(Context->pUrb);                                        \
188                 RTUSB_FREE_URB(Context->pUrb);                                          \
189                 Context->pUrb = NULL; }                                                         \
190         if (NULL != Context->TransferBuffer) {                          \
191                 RTUSB_URB_FREE_BUFFER(pObj->pUsb_Dev, BufferSize,       \
192                                                                 Context->TransferBuffer,        \
193                                                                 Context->data_dma);                     \
194                 Context->TransferBuffer = NULL; }
195
196         UCHAR                   i, acidx;
197         NDIS_STATUS     Status = NDIS_STATUS_SUCCESS;
198         PTX_CONTEXT             pNullContext   = &(pAd->NullContext);
199         PTX_CONTEXT             pPsPollContext = &(pAd->PsPollContext);
200         PTX_CONTEXT             pRTSContext    = &(pAd->RTSContext);
201         PTX_CONTEXT             pMLMEContext = NULL;
202 //      PHT_TX_CONTEXT  pHTTXContext = NULL;
203         POS_COOKIE              pObj = (POS_COOKIE) pAd->OS_Cookie;
204         PVOID                   RingBaseVa;
205 //      RTMP_TX_RING    *pTxRing;
206         RTMP_MGMT_RING  *pMgmtRing;
207
208         DBGPRINT(RT_DEBUG_TRACE, ("--> NICInitTransmit\n"));
209         pObj = pObj;
210
211         // Init 4 set of Tx parameters
212         for(acidx = 0; acidx < NUM_OF_TX_RING; acidx++)
213         {
214                 // Initialize all Transmit releated queues
215                 InitializeQueueHeader(&pAd->TxSwQueue[acidx]);
216
217                 // Next Local tx ring pointer waiting for buck out
218                 pAd->NextBulkOutIndex[acidx] = acidx;
219                 pAd->BulkOutPending[acidx] = FALSE; // Buck Out control flag
220                 //pAd->DataBulkDoneIdx[acidx] = 0;
221         }
222
223         //pAd->NextMLMEIndex    = 0;
224         //pAd->PushMgmtIndex    = 0;
225         //pAd->PopMgmtIndex     = 0;
226         //InterlockedExchange(&pAd->MgmtQueueSize, 0);
227         //InterlockedExchange(&pAd->TxCount, 0);
228
229         //pAd->PrioRingFirstIndex       = 0;
230         //pAd->PrioRingTxCnt            = 0;
231
232         do
233         {
234                 //
235                 // TX_RING_SIZE, 4 ACs
236                 //
237                 for(acidx=0; acidx<4; acidx++)
238                 {
239                         PHT_TX_CONTEXT  pHTTXContext = &(pAd->TxContext[acidx]);
240
241                         NdisZeroMemory(pHTTXContext, sizeof(HT_TX_CONTEXT));
242                         //Allocate URB
243                         LM_USB_ALLOC(pObj, pHTTXContext, PHTTX_BUFFER, sizeof(HTTX_BUFFER), Status,
244                                                         ("<-- ERROR in Alloc TX TxContext[%d] urb!! \n", acidx),
245                                                         done,
246                                                         ("<-- ERROR in Alloc TX TxContext[%d] HTTX_BUFFER !! \n", acidx),
247                                                         out1);
248
249                         NdisZeroMemory(pHTTXContext->TransferBuffer->Aggregation, 4);
250                         pHTTXContext->pAd = pAd;
251                         pHTTXContext->pIrp = NULL;
252                         pHTTXContext->IRPPending = FALSE;
253                         pHTTXContext->NextBulkOutPosition = 0;
254                         pHTTXContext->ENextBulkOutPosition = 0;
255                         pHTTXContext->CurWritePosition = 0;
256                         pHTTXContext->CurWriteRealPos = 0;
257                         pHTTXContext->BulkOutSize = 0;
258                         pHTTXContext->BulkOutPipeId = acidx;
259                         pHTTXContext->bRingEmpty = TRUE;
260                         pHTTXContext->bCopySavePad = FALSE;
261
262                         pAd->BulkOutPending[acidx] = FALSE;
263                 }
264
265
266                 //
267                 // MGMT_RING_SIZE
268                 //
269                 // Allocate MGMT ring descriptor's memory
270                 pAd->MgmtDescRing.AllocSize = MGMT_RING_SIZE * sizeof(TX_CONTEXT);
271                 RTMPAllocateMemory(&pAd->MgmtDescRing.AllocVa, pAd->MgmtDescRing.AllocSize);
272                 if (pAd->MgmtDescRing.AllocVa == NULL)
273                 {
274                         DBGPRINT_ERR(("Failed to allocate a big buffer for MgmtDescRing!\n"));
275                         Status = NDIS_STATUS_RESOURCES;
276                         goto out1;
277                 }
278                 NdisZeroMemory(pAd->MgmtDescRing.AllocVa, pAd->MgmtDescRing.AllocSize);
279                 RingBaseVa     = pAd->MgmtDescRing.AllocVa;
280
281                 // Initialize MGMT Ring and associated buffer memory
282                 pMgmtRing = &pAd->MgmtRing;
283                 for (i = 0; i < MGMT_RING_SIZE; i++)
284                 {
285                         // link the pre-allocated Mgmt buffer to MgmtRing.Cell
286                         pMgmtRing->Cell[i].AllocSize = sizeof(TX_CONTEXT);
287                         pMgmtRing->Cell[i].AllocVa = RingBaseVa;
288                         pMgmtRing->Cell[i].pNdisPacket = NULL;
289                         pMgmtRing->Cell[i].pNextNdisPacket = NULL;
290
291                         //Allocate URB for MLMEContext
292                         pMLMEContext = (PTX_CONTEXT) pAd->MgmtRing.Cell[i].AllocVa;
293                         pMLMEContext->pUrb = RTUSB_ALLOC_URB(0);
294                         if (pMLMEContext->pUrb == NULL)
295                         {
296                                 DBGPRINT(RT_DEBUG_ERROR, ("<-- ERROR in Alloc TX MLMEContext[%d] urb!! \n", i));
297                                 Status = NDIS_STATUS_RESOURCES;
298                                 goto out2;
299                         }
300                         pMLMEContext->pAd = pAd;
301                         pMLMEContext->pIrp = NULL;
302                         pMLMEContext->TransferBuffer = NULL;
303                         pMLMEContext->InUse = FALSE;
304                         pMLMEContext->IRPPending = FALSE;
305                         pMLMEContext->bWaitingBulkOut = FALSE;
306                         pMLMEContext->BulkOutSize = 0;
307                         pMLMEContext->SelfIdx = i;
308
309                         // Offset to next ring descriptor address
310                         RingBaseVa = (PUCHAR) RingBaseVa + sizeof(TX_CONTEXT);
311                 }
312                 DBGPRINT(RT_DEBUG_TRACE, ("MGMT Ring: total %d entry allocated\n", i));
313
314                 //pAd->MgmtRing.TxSwFreeIdx = (MGMT_RING_SIZE - 1);
315                 pAd->MgmtRing.TxSwFreeIdx = MGMT_RING_SIZE;
316                 pAd->MgmtRing.TxCpuIdx = 0;
317                 pAd->MgmtRing.TxDmaIdx = 0;
318
319                 //
320                 // BEACON_RING_SIZE
321                 //
322                 for(i=0; i<BEACON_RING_SIZE; i++) // 2
323                 {
324                         PTX_CONTEXT     pBeaconContext = &(pAd->BeaconContext[i]);
325
326
327                         NdisZeroMemory(pBeaconContext, sizeof(TX_CONTEXT));
328
329                         //Allocate URB
330                         LM_USB_ALLOC(pObj, pBeaconContext, PTX_BUFFER, sizeof(TX_BUFFER), Status,
331                                                         ("<-- ERROR in Alloc TX BeaconContext[%d] urb!! \n", i),
332                                                         out2,
333                                                         ("<-- ERROR in Alloc TX BeaconContext[%d] TX_BUFFER !! \n", i),
334                                                         out3);
335
336                         pBeaconContext->pAd = pAd;
337                         pBeaconContext->pIrp = NULL;
338                         pBeaconContext->InUse = FALSE;
339                         pBeaconContext->IRPPending = FALSE;
340                 }
341
342                 //
343                 // NullContext
344                 //
345                 NdisZeroMemory(pNullContext, sizeof(TX_CONTEXT));
346
347                 //Allocate URB
348                 LM_USB_ALLOC(pObj, pNullContext, PTX_BUFFER, sizeof(TX_BUFFER), Status,
349                                                 ("<-- ERROR in Alloc TX NullContext urb!! \n"),
350                                                 out3,
351                                                 ("<-- ERROR in Alloc TX NullContext TX_BUFFER !! \n"),
352                                                 out4);
353
354                 pNullContext->pAd = pAd;
355                 pNullContext->pIrp = NULL;
356                 pNullContext->InUse = FALSE;
357                 pNullContext->IRPPending = FALSE;
358
359                 //
360                 // RTSContext
361                 //
362                 NdisZeroMemory(pRTSContext, sizeof(TX_CONTEXT));
363
364                 //Allocate URB
365                 LM_USB_ALLOC(pObj, pRTSContext, PTX_BUFFER, sizeof(TX_BUFFER), Status,
366                                                 ("<-- ERROR in Alloc TX RTSContext urb!! \n"),
367                                                 out4,
368                                                 ("<-- ERROR in Alloc TX RTSContext TX_BUFFER !! \n"),
369                                                 out5);
370
371                 pRTSContext->pAd = pAd;
372                 pRTSContext->pIrp = NULL;
373                 pRTSContext->InUse = FALSE;
374                 pRTSContext->IRPPending = FALSE;
375
376                 //
377                 // PsPollContext
378                 //
379                 //NdisZeroMemory(pPsPollContext, sizeof(TX_CONTEXT));
380                 //Allocate URB
381                 LM_USB_ALLOC(pObj, pPsPollContext, PTX_BUFFER, sizeof(TX_BUFFER), Status,
382                                                 ("<-- ERROR in Alloc TX PsPollContext urb!! \n"),
383                                                 out5,
384                                                 ("<-- ERROR in Alloc TX PsPollContext TX_BUFFER !! \n"),
385                                                 out6);
386
387                 pPsPollContext->pAd = pAd;
388                 pPsPollContext->pIrp = NULL;
389                 pPsPollContext->InUse = FALSE;
390                 pPsPollContext->IRPPending = FALSE;
391                 pPsPollContext->bAggregatible = FALSE;
392                 pPsPollContext->LastOne = TRUE;
393
394         }   while (FALSE);
395
396
397 done:
398         DBGPRINT(RT_DEBUG_TRACE, ("<-- NICInitTransmit\n"));
399
400         return Status;
401
402         /* --------------------------- ERROR HANDLE --------------------------- */
403 out6:
404         LM_URB_FREE(pObj, pPsPollContext, sizeof(TX_BUFFER));
405
406 out5:
407         LM_URB_FREE(pObj, pRTSContext, sizeof(TX_BUFFER));
408
409 out4:
410         LM_URB_FREE(pObj, pNullContext, sizeof(TX_BUFFER));
411
412 out3:
413         for(i=0; i<BEACON_RING_SIZE; i++)
414         {
415                 PTX_CONTEXT     pBeaconContext = &(pAd->BeaconContext[i]);
416                 if (pBeaconContext)
417                         LM_URB_FREE(pObj, pBeaconContext, sizeof(TX_BUFFER));
418         }
419
420 out2:
421         if (pAd->MgmtDescRing.AllocVa)
422         {
423                 pMgmtRing = &pAd->MgmtRing;
424         for(i=0; i<MGMT_RING_SIZE; i++)
425         {
426                 pMLMEContext = (PTX_CONTEXT) pAd->MgmtRing.Cell[i].AllocVa;
427                 if (pMLMEContext)
428                         LM_URB_FREE(pObj, pMLMEContext, sizeof(TX_BUFFER));
429         }
430                 NdisFreeMemory(pAd->MgmtDescRing.AllocVa, pAd->MgmtDescRing.AllocSize, 0);
431                 pAd->MgmtDescRing.AllocVa = NULL;
432         }
433
434 out1:
435         for (acidx = 0; acidx < 4; acidx++)
436         {
437                 PHT_TX_CONTEXT pTxContext = &(pAd->TxContext[acidx]);
438                 if (pTxContext)
439                         LM_URB_FREE(pObj, pTxContext, sizeof(HTTX_BUFFER));
440         }
441
442         // Here we didn't have any pre-allocated memory need to free.
443
444         return Status;
445 }
446
447
448 /*
449 ========================================================================
450 Routine Description:
451     Allocate DMA memory blocks for send, receive.
452
453 Arguments:
454     pAd                                 Pointer to our adapter
455
456 Return Value:
457         NDIS_STATUS_SUCCESS
458         NDIS_STATUS_FAILURE
459         NDIS_STATUS_RESOURCES
460
461 Note:
462 ========================================================================
463 */
464 NDIS_STATUS     RTMPAllocTxRxRingMemory(
465         IN      PRTMP_ADAPTER   pAd)
466 {
467 //      COUNTER_802_11  pCounter = &pAd->WlanCounters;
468         NDIS_STATUS             Status;
469         INT                             num;
470
471
472         DBGPRINT(RT_DEBUG_TRACE, ("--> RTMPAllocTxRxRingMemory\n"));
473
474
475         do
476         {
477                 // Init the CmdQ and CmdQLock
478                 NdisAllocateSpinLock(&pAd->CmdQLock);
479                 NdisAcquireSpinLock(&pAd->CmdQLock);
480                 RTUSBInitializeCmdQ(&pAd->CmdQ);
481                 NdisReleaseSpinLock(&pAd->CmdQLock);
482
483
484                 NdisAllocateSpinLock(&pAd->MLMEBulkOutLock);
485                 //NdisAllocateSpinLock(&pAd->MLMEWaitQueueLock);
486                 NdisAllocateSpinLock(&pAd->BulkOutLock[0]);
487                 NdisAllocateSpinLock(&pAd->BulkOutLock[1]);
488                 NdisAllocateSpinLock(&pAd->BulkOutLock[2]);
489                 NdisAllocateSpinLock(&pAd->BulkOutLock[3]);
490                 NdisAllocateSpinLock(&pAd->BulkOutLock[4]);
491                 NdisAllocateSpinLock(&pAd->BulkOutLock[5]);
492                 NdisAllocateSpinLock(&pAd->BulkInLock);
493
494                 for (num = 0; num < NUM_OF_TX_RING; num++)
495                 {
496                         NdisAllocateSpinLock(&pAd->TxContextQueueLock[num]);
497                 }
498
499 //              NdisAllocateSpinLock(&pAd->MemLock);    // Not used in RT28XX
500
501 //              NdisAllocateSpinLock(&pAd->MacTabLock); // init it in UserCfgInit()
502 //              NdisAllocateSpinLock(&pAd->BATabLock); // init it in BATableInit()
503
504 //              for(num=0; num<MAX_LEN_OF_BA_REC_TABLE; num++)
505 //              {
506 //                      NdisAllocateSpinLock(&pAd->BATable.BARecEntry[num].RxReRingLock);
507 //              }
508
509                 //
510                 // Init Mac Table
511                 //
512 //              MacTableInitialize(pAd);
513
514                 //
515                 // Init send data structures and related parameters
516                 //
517                 Status = NICInitTransmit(pAd);
518                 if (Status != NDIS_STATUS_SUCCESS)
519                         break;
520
521                 //
522                 // Init receive data structures and related parameters
523                 //
524                 Status = NICInitRecv(pAd);
525                 if (Status != NDIS_STATUS_SUCCESS)
526                         break;
527
528                 pAd->PendingIoCount = 1;
529
530         } while (FALSE);
531
532         NdisZeroMemory(&pAd->FragFrame, sizeof(FRAGMENT_FRAME));
533         pAd->FragFrame.pFragPacket =  RTMP_AllocateFragPacketBuffer(pAd, RX_BUFFER_NORMSIZE);
534
535         if (pAd->FragFrame.pFragPacket == NULL)
536         {
537                 Status = NDIS_STATUS_RESOURCES;
538         }
539
540         DBGPRINT_S(Status, ("<-- RTMPAllocTxRxRingMemory, Status=%x\n", Status));
541         return Status;
542 }
543
544
545 /*
546 ========================================================================
547 Routine Description:
548         Calls USB_InterfaceStop and frees memory allocated for the URBs
549     calls NdisMDeregisterDevice and frees the memory
550     allocated in VNetInitialize for the Adapter Object
551
552 Arguments:
553         *pAd                            the raxx interface data pointer
554
555 Return Value:
556         None
557
558 Note:
559 ========================================================================
560 */
561 VOID    RTMPFreeTxRxRingMemory(
562         IN      PRTMP_ADAPTER   pAd)
563 {
564 #define LM_URB_FREE(pObj, Context, BufferSize)                          \
565         if (NULL != Context->pUrb) {                                                    \
566                 RTUSB_UNLINK_URB(Context->pUrb);                                        \
567                 RTUSB_FREE_URB(Context->pUrb);                                          \
568                 Context->pUrb = NULL; }                                                         \
569         if (NULL != Context->TransferBuffer) {                                  \
570                 RTUSB_URB_FREE_BUFFER(pObj->pUsb_Dev, BufferSize,       \
571                                                                 Context->TransferBuffer,        \
572                                                                 Context->data_dma);                     \
573                 Context->TransferBuffer = NULL; }
574
575
576         UINT                i, acidx;
577         PTX_CONTEXT                     pNullContext   = &pAd->NullContext;
578         PTX_CONTEXT                     pPsPollContext = &pAd->PsPollContext;
579         PTX_CONTEXT                     pRTSContext    = &pAd->RTSContext;
580 //      PHT_TX_CONTEXT          pHTTXContext;
581         //PRTMP_REORDERBUF      pReorderBuf;
582         POS_COOKIE                      pObj = (POS_COOKIE) pAd->OS_Cookie;
583 //      RTMP_TX_RING            *pTxRing;
584
585         DBGPRINT(RT_DEBUG_ERROR, ("---> RTMPFreeTxRxRingMemory\n"));
586         pObj = pObj;
587
588         // Free all resources for the RECEIVE buffer queue.
589         for(i=0; i<(RX_RING_SIZE); i++)
590         {
591                 PRX_CONTEXT  pRxContext = &(pAd->RxContext[i]);
592                 if (pRxContext)
593                         LM_URB_FREE(pObj, pRxContext, MAX_RXBULK_SIZE);
594         }
595
596         // Free PsPoll frame resource
597         LM_URB_FREE(pObj, pPsPollContext, sizeof(TX_BUFFER));
598
599         // Free NULL frame resource
600         LM_URB_FREE(pObj, pNullContext, sizeof(TX_BUFFER));
601
602         // Free RTS frame resource
603         LM_URB_FREE(pObj, pRTSContext, sizeof(TX_BUFFER));
604
605
606         // Free beacon frame resource
607         for(i=0; i<BEACON_RING_SIZE; i++)
608         {
609                 PTX_CONTEXT     pBeaconContext = &(pAd->BeaconContext[i]);
610                 if (pBeaconContext)
611                         LM_URB_FREE(pObj, pBeaconContext, sizeof(TX_BUFFER));
612         }
613
614
615         // Free mgmt frame resource
616         for(i = 0; i < MGMT_RING_SIZE; i++)
617         {
618                 PTX_CONTEXT pMLMEContext = (PTX_CONTEXT)pAd->MgmtRing.Cell[i].AllocVa;
619                 //LM_URB_FREE(pObj, pMLMEContext, sizeof(TX_BUFFER));
620                 if (NULL != pAd->MgmtRing.Cell[i].pNdisPacket)
621                 {
622                         RTMPFreeNdisPacket(pAd, pAd->MgmtRing.Cell[i].pNdisPacket);
623                         pAd->MgmtRing.Cell[i].pNdisPacket = NULL;
624                         pMLMEContext->TransferBuffer = NULL;
625                 }
626
627                 if (pMLMEContext)
628                 {
629                         if (NULL != pMLMEContext->pUrb)
630                         {
631                                 RTUSB_UNLINK_URB(pMLMEContext->pUrb);
632                                 RTUSB_FREE_URB(pMLMEContext->pUrb);
633                                 pMLMEContext->pUrb = NULL;
634                         }
635                 }
636         }
637         if (pAd->MgmtDescRing.AllocVa)
638                 NdisFreeMemory(pAd->MgmtDescRing.AllocVa, pAd->MgmtDescRing.AllocSize, 0);
639
640
641         // Free Tx frame resource
642                 for(acidx=0; acidx<4; acidx++)
643                 {
644                 PHT_TX_CONTEXT pHTTXContext = &(pAd->TxContext[acidx]);
645                         if (pHTTXContext)
646                                 LM_URB_FREE(pObj, pHTTXContext, sizeof(HTTX_BUFFER));
647                 }
648
649         if (pAd->FragFrame.pFragPacket)
650                 RELEASE_NDIS_PACKET(pAd, pAd->FragFrame.pFragPacket, NDIS_STATUS_SUCCESS);
651
652         for(i=0; i<6; i++)
653         {
654                 NdisFreeSpinLock(&pAd->BulkOutLock[i]);
655         }
656
657         NdisFreeSpinLock(&pAd->BulkInLock);
658         NdisFreeSpinLock(&pAd->MLMEBulkOutLock);
659
660         NdisFreeSpinLock(&pAd->CmdQLock);
661
662         // Clear all pending bulk-out request flags.
663         RTUSB_CLEAR_BULK_FLAG(pAd, 0xffffffff);
664
665 //      NdisFreeSpinLock(&pAd->MacTabLock);
666
667 //      for(i=0; i<MAX_LEN_OF_BA_REC_TABLE; i++)
668 //      {
669 //              NdisFreeSpinLock(&pAd->BATable.BARecEntry[i].RxReRingLock);
670 //      }
671
672         DBGPRINT(RT_DEBUG_ERROR, ("<--- ReleaseAdapter\n"));
673 }
674
675
676 /*
677 ========================================================================
678 Routine Description:
679     Allocate memory for adapter control block.
680
681 Arguments:
682     pAd                                 Pointer to our adapter
683
684 Return Value:
685         NDIS_STATUS_SUCCESS
686         NDIS_STATUS_FAILURE
687         NDIS_STATUS_RESOURCES
688
689 Note:
690 ========================================================================
691 */
692 NDIS_STATUS AdapterBlockAllocateMemory(
693         IN PVOID        handle,
694         OUT     PVOID   *ppAd)
695 {
696         PUSB_DEV        usb_dev;
697         POS_COOKIE      pObj = (POS_COOKIE) handle;
698
699
700         usb_dev = pObj->pUsb_Dev;
701
702         pObj->MLMEThr_pid       = NULL;
703         pObj->RTUSBCmdThr_pid   = NULL;
704
705         *ppAd = (PVOID)vmalloc(sizeof(RTMP_ADAPTER));
706
707         if (*ppAd)
708         {
709                 NdisZeroMemory(*ppAd, sizeof(RTMP_ADAPTER));
710                 ((PRTMP_ADAPTER)*ppAd)->OS_Cookie = handle;
711                 return (NDIS_STATUS_SUCCESS);
712         }
713         else
714         {
715                 return (NDIS_STATUS_FAILURE);
716         }
717 }
718
719
720 /*
721 ========================================================================
722 Routine Description:
723     Create kernel threads & tasklets.
724
725 Arguments:
726     *net_dev                    Pointer to wireless net device interface
727
728 Return Value:
729         NDIS_STATUS_SUCCESS
730         NDIS_STATUS_FAILURE
731
732 Note:
733 ========================================================================
734 */
735 NDIS_STATUS      CreateThreads(
736         IN      struct net_device *net_dev)
737 {
738         PRTMP_ADAPTER pAd = net_dev->ml_priv;
739         POS_COOKIE pObj = (POS_COOKIE) pAd->OS_Cookie;
740         pid_t pid_number;
741
742         //init_MUTEX(&(pAd->usbdev_semaphore));
743
744         init_MUTEX_LOCKED(&(pAd->mlme_semaphore));
745         init_completion (&pAd->mlmeComplete);
746
747         init_MUTEX_LOCKED(&(pAd->RTUSBCmd_semaphore));
748         init_completion (&pAd->CmdQComplete);
749
750         init_MUTEX_LOCKED(&(pAd->RTUSBTimer_semaphore));
751         init_completion (&pAd->TimerQComplete);
752
753         // Creat MLME Thread
754         pObj->MLMEThr_pid = NULL;
755         pid_number = kernel_thread(MlmeThread, pAd, CLONE_VM);
756         if (pid_number < 0)
757         {
758                 printk (KERN_WARNING "%s: unable to start Mlme thread\n",pAd->net_dev->name);
759                 return NDIS_STATUS_FAILURE;
760         }
761
762         pObj->MLMEThr_pid = find_get_pid(pid_number);
763
764         // Wait for the thread to start
765         wait_for_completion(&(pAd->mlmeComplete));
766
767         // Creat Command Thread
768         pObj->RTUSBCmdThr_pid = NULL;
769         pid_number = kernel_thread(RTUSBCmdThread, pAd, CLONE_VM);
770         if (pid_number < 0)
771         {
772                 printk (KERN_WARNING "%s: unable to start RTUSBCmd thread\n",pAd->net_dev->name);
773                 return NDIS_STATUS_FAILURE;
774         }
775
776         pObj->RTUSBCmdThr_pid = find_get_pid(pid_number);
777
778         wait_for_completion(&(pAd->CmdQComplete));
779
780         pObj->TimerQThr_pid = NULL;
781         pid_number = kernel_thread(TimerQThread, pAd, CLONE_VM);
782         if (pid_number < 0)
783         {
784                 printk (KERN_WARNING "%s: unable to start TimerQThread\n",pAd->net_dev->name);
785                 return NDIS_STATUS_FAILURE;
786         }
787
788         pObj->TimerQThr_pid = find_get_pid(pid_number);
789
790         // Wait for the thread to start
791         wait_for_completion(&(pAd->TimerQComplete));
792
793         // Create receive tasklet
794         tasklet_init(&pObj->rx_done_task, rx_done_tasklet, (ULONG)pAd);
795         tasklet_init(&pObj->mgmt_dma_done_task, rt2870_mgmt_dma_done_tasklet, (unsigned long)pAd);
796         tasklet_init(&pObj->ac0_dma_done_task, rt2870_ac0_dma_done_tasklet, (unsigned long)pAd);
797         tasklet_init(&pObj->ac1_dma_done_task, rt2870_ac1_dma_done_tasklet, (unsigned long)pAd);
798         tasklet_init(&pObj->ac2_dma_done_task, rt2870_ac2_dma_done_tasklet, (unsigned long)pAd);
799         tasklet_init(&pObj->ac3_dma_done_task, rt2870_ac3_dma_done_tasklet, (unsigned long)pAd);
800         tasklet_init(&pObj->hcca_dma_done_task, rt2870_hcca_dma_done_tasklet, (unsigned long)pAd);
801         tasklet_init(&pObj->tbtt_task, tbtt_tasklet, (unsigned long)pAd);
802         tasklet_init(&pObj->null_frame_complete_task, rt2870_null_frame_complete_tasklet, (unsigned long)pAd);
803         tasklet_init(&pObj->rts_frame_complete_task, rt2870_rts_frame_complete_tasklet, (unsigned long)pAd);
804         tasklet_init(&pObj->pspoll_frame_complete_task, rt2870_pspoll_frame_complete_tasklet, (unsigned long)pAd);
805
806         return NDIS_STATUS_SUCCESS;
807 }
808
809 /*
810 ========================================================================
811 Routine Description:
812         As STA's BSSID is a WC too, it uses shared key table.
813         This function write correct unicast TX key to ASIC WCID.
814         And we still make a copy in our MacTab.Content[BSSID_WCID].PairwiseKey.
815         Caller guarantee TKIP/AES always has keyidx = 0. (pairwise key)
816         Caller guarantee WEP calls this function when set Txkey,  default key index=0~3.
817
818 Arguments:
819         pAd                                     Pointer to our adapter
820         pKey                                    Pointer to the where the key stored
821
822 Return Value:
823         NDIS_SUCCESS                    Add key successfully
824
825 Note:
826 ========================================================================
827 */
828 VOID    RTMPAddBSSIDCipher(
829         IN      PRTMP_ADAPTER           pAd,
830         IN      UCHAR                           Aid,
831         IN      PNDIS_802_11_KEY        pKey,
832         IN  UCHAR                       CipherAlg)
833 {
834         PUCHAR          pTxMic, pRxMic;
835         BOOLEAN         bKeyRSC, bAuthenticator; // indicate the receive SC set by KeyRSC value
836 //      UCHAR           CipherAlg;
837         UCHAR           i;
838         ULONG           WCIDAttri;
839         USHORT          offset;
840         UCHAR           KeyIdx, IVEIV[8];
841         UINT32          Value;
842
843         DBGPRINT(RT_DEBUG_TRACE, ("RTMPAddBSSIDCipher==> Aid = %d\n",Aid));
844
845         // Bit 29 of Add-key KeyRSC
846         bKeyRSC            = (pKey->KeyIndex & 0x20000000) ? TRUE : FALSE;
847
848         // Bit 28 of Add-key Authenticator
849         bAuthenticator = (pKey->KeyIndex & 0x10000000) ? TRUE : FALSE;
850         KeyIdx = (UCHAR)pKey->KeyIndex&0xff;
851
852         if (KeyIdx > 4)
853                 return;
854
855
856         if (pAd->MacTab.Content[Aid].PairwiseKey.CipherAlg == CIPHER_TKIP)
857         {       if (pAd->StaCfg.AuthMode == Ndis802_11AuthModeWPANone)
858                 {
859                         // for WPA-None Tx, Rx MIC is the same
860                         pTxMic = (PUCHAR) (&pKey->KeyMaterial) + 16;
861                         pRxMic = pTxMic;
862                 }
863                 else if (bAuthenticator == TRUE)
864                 {
865                         pTxMic = (PUCHAR) (&pKey->KeyMaterial) + 16;
866                         pRxMic = (PUCHAR) (&pKey->KeyMaterial) + 24;
867                 }
868                 else
869                 {
870                         pRxMic = (PUCHAR) (&pKey->KeyMaterial) + 16;
871                         pTxMic = (PUCHAR) (&pKey->KeyMaterial) + 24;
872                 }
873
874                 offset = PAIRWISE_KEY_TABLE_BASE + (Aid * HW_KEY_ENTRY_SIZE) + 0x10;
875                 for (i=0; i<8; )
876                 {
877                         Value = *(pTxMic+i);
878                         Value += (*(pTxMic+i+1)<<8);
879                         Value += (*(pTxMic+i+2)<<16);
880                         Value += (*(pTxMic+i+3)<<24);
881                         RTUSBWriteMACRegister(pAd, offset+i, Value);
882                         i+=4;
883                 }
884
885                 offset = PAIRWISE_KEY_TABLE_BASE + (Aid * HW_KEY_ENTRY_SIZE) + 0x18;
886                 for (i=0; i<8; )
887                 {
888                         Value = *(pRxMic+i);
889                         Value += (*(pRxMic+i+1)<<8);
890                         Value += (*(pRxMic+i+2)<<16);
891                         Value += (*(pRxMic+i+3)<<24);
892                         RTUSBWriteMACRegister(pAd, offset+i, Value);
893                         i+=4;
894                 }
895
896                 // Only Key lenth equal to TKIP key have these
897                 NdisMoveMemory(pAd->MacTab.Content[Aid].PairwiseKey.RxMic, pRxMic, 8);
898                 NdisMoveMemory(pAd->MacTab.Content[Aid].PairwiseKey.TxMic, pTxMic, 8);
899
900                 DBGPRINT(RT_DEBUG_TRACE,
901                                 ("      TxMIC  = %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x \n",
902                                 pTxMic[0],pTxMic[1],pTxMic[2],pTxMic[3],
903                                 pTxMic[4],pTxMic[5],pTxMic[6],pTxMic[7]));
904                 DBGPRINT(RT_DEBUG_TRACE,
905                                 ("      RxMIC  = %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x \n",
906                                 pRxMic[0],pRxMic[1],pRxMic[2],pRxMic[3],
907                                 pRxMic[4],pRxMic[5],pRxMic[6],pRxMic[7]));
908         }
909
910         // 2. Record Security Key.
911         pAd->MacTab.Content[BSSID_WCID].PairwiseKey.KeyLen= (UCHAR)pKey->KeyLength;
912         NdisMoveMemory(pAd->MacTab.Content[Aid].PairwiseKey.Key, &pKey->KeyMaterial, pKey->KeyLength);
913
914         // 3. Check RxTsc. And used to init to ASIC IV.
915         if (bKeyRSC == TRUE)
916                 NdisMoveMemory(pAd->MacTab.Content[Aid].PairwiseKey.RxTsc, &pKey->KeyRSC, 6);
917         else
918                 NdisZeroMemory(pAd->MacTab.Content[Aid].PairwiseKey.RxTsc, 6);
919
920         // 4. Init TxTsc to one based on WiFi WPA specs
921         pAd->MacTab.Content[Aid].PairwiseKey.TxTsc[0] = 1;
922         pAd->MacTab.Content[Aid].PairwiseKey.TxTsc[1] = 0;
923         pAd->MacTab.Content[Aid].PairwiseKey.TxTsc[2] = 0;
924         pAd->MacTab.Content[Aid].PairwiseKey.TxTsc[3] = 0;
925         pAd->MacTab.Content[Aid].PairwiseKey.TxTsc[4] = 0;
926         pAd->MacTab.Content[Aid].PairwiseKey.TxTsc[5] = 0;
927
928         CipherAlg = pAd->MacTab.Content[Aid].PairwiseKey.CipherAlg;
929
930         offset = PAIRWISE_KEY_TABLE_BASE + (Aid * HW_KEY_ENTRY_SIZE);
931         RTUSBMultiWrite(pAd, (USHORT) offset, pKey->KeyMaterial,
932                                 ((pKey->KeyLength == LEN_TKIP_KEY) ? 16 : (USHORT)pKey->KeyLength));
933
934         offset = SHARED_KEY_TABLE_BASE + (KeyIdx * HW_KEY_ENTRY_SIZE);
935         RTUSBMultiWrite(pAd, (USHORT) offset, pKey->KeyMaterial, (USHORT)pKey->KeyLength);
936
937         offset = PAIRWISE_IVEIV_TABLE_BASE + (Aid * HW_IVEIV_ENTRY_SIZE);
938         NdisZeroMemory(IVEIV, 8);
939
940         // IV/EIV
941         if ((CipherAlg == CIPHER_TKIP) ||
942                 (CipherAlg == CIPHER_TKIP_NO_MIC) ||
943                 (CipherAlg == CIPHER_AES))
944         {
945                 IVEIV[3] = 0x20; // Eiv bit on. keyid always 0 for pairwise key
946         }
947         // default key idx needs to set.
948         // in TKIP/AES KeyIdx = 0 , WEP KeyIdx is default tx key.
949         else
950         {
951                 IVEIV[3] |= (KeyIdx<< 6);
952         }
953         RTUSBMultiWrite(pAd, (USHORT) offset, IVEIV, 8);
954
955         // WCID Attribute UDF:3, BSSIdx:3, Alg:3, Keytable:1=PAIRWISE KEY, BSSIdx is 0
956         if ((CipherAlg == CIPHER_TKIP) ||
957                 (CipherAlg == CIPHER_TKIP_NO_MIC) ||
958                 (CipherAlg == CIPHER_AES))
959         {
960                 WCIDAttri = (CipherAlg<<1)|SHAREDKEYTABLE;
961         }
962         else
963                 WCIDAttri = (CipherAlg<<1)|SHAREDKEYTABLE;
964
965         offset = MAC_WCID_ATTRIBUTE_BASE + (Aid* HW_WCID_ATTRI_SIZE);
966         RTUSBWriteMACRegister(pAd, offset, WCIDAttri);
967         RTUSBReadMACRegister(pAd, offset, &Value);
968
969         DBGPRINT(RT_DEBUG_TRACE, ("BSSID_WCID : offset = %x, WCIDAttri = %lx\n",
970                         offset, WCIDAttri));
971
972         // pAddr
973         // Add Bssid mac address at linkup. not here.  check!
974         /*offset = MAC_WCID_BASE + (BSSID_WCID * HW_WCID_ENTRY_SIZE);
975         *for (i=0; i<MAC_ADDR_LEN; i++)
976         {
977                 RTMP_IO_WRITE8(pAd, offset+i, pKey->BSSID[i]);
978         }
979         */
980
981         DBGPRINT(RT_DEBUG_ERROR, ("AddBSSIDasWCIDEntry: Alg=%s, KeyLength = %d\n",
982                         CipherName[CipherAlg], pKey->KeyLength));
983         DBGPRINT(RT_DEBUG_TRACE, ("Key [idx=%x] [KeyLen = %d]\n",
984                         pKey->KeyIndex, pKey->KeyLength));
985         for(i=0; i<pKey->KeyLength; i++)
986                 DBGPRINT_RAW(RT_DEBUG_TRACE,(" %x:", pKey->KeyMaterial[i]));
987         DBGPRINT(RT_DEBUG_TRACE,("       \n"));
988 }
989
990 /*
991 ========================================================================
992 Routine Description:
993     Get a received packet.
994
995 Arguments:
996         pAd                                     device control block
997         pSaveRxD                        receive descriptor information
998         *pbReschedule           need reschedule flag
999         *pRxPending                     pending received packet flag
1000
1001 Return Value:
1002     the recieved packet
1003
1004 Note:
1005 ========================================================================
1006 */
1007 #define RT2870_RXDMALEN_FIELD_SIZE                      4
1008 PNDIS_PACKET GetPacketFromRxRing(
1009         IN              PRTMP_ADAPTER           pAd,
1010         OUT             PRT28XX_RXD_STRUC       pSaveRxD,
1011         OUT             BOOLEAN                         *pbReschedule,
1012         IN OUT  UINT32                          *pRxPending)
1013 {
1014         PRX_CONTEXT             pRxContext;
1015         PNDIS_PACKET    pSkb;
1016         PUCHAR                  pData;
1017         ULONG                   ThisFrameLen;
1018         ULONG                   RxBufferLength;
1019         PRXWI_STRUC             pRxWI;
1020
1021         pRxContext = &pAd->RxContext[pAd->NextRxBulkInReadIndex];
1022         if ((pRxContext->Readable == FALSE) || (pRxContext->InUse == TRUE))
1023                 return NULL;
1024
1025         RxBufferLength = pRxContext->BulkInOffset - pAd->ReadPosition;
1026         if (RxBufferLength < (RT2870_RXDMALEN_FIELD_SIZE + sizeof(RXWI_STRUC) + sizeof(RXINFO_STRUC)))
1027         {
1028                 goto label_null;
1029         }
1030
1031         pData = &pRxContext->TransferBuffer[pAd->ReadPosition]; /* 4KB */
1032         // The RXDMA field is 4 bytes, now just use the first 2 bytes. The Length including the (RXWI + MSDU + Padding)
1033         ThisFrameLen = *pData + (*(pData+1)<<8);
1034     if (ThisFrameLen == 0)
1035         {
1036                 DBGPRINT(RT_DEBUG_TRACE, ("BIRIdx(%d): RXDMALen is zero.[%ld], BulkInBufLen = %ld)\n",
1037                                                                 pAd->NextRxBulkInReadIndex, ThisFrameLen, pRxContext->BulkInOffset));
1038                 goto label_null;
1039         }
1040         if ((ThisFrameLen&0x3) != 0)
1041         {
1042                 DBGPRINT(RT_DEBUG_ERROR, ("BIRIdx(%d): RXDMALen not multiple of 4.[%ld], BulkInBufLen = %ld)\n",
1043                                                                 pAd->NextRxBulkInReadIndex, ThisFrameLen, pRxContext->BulkInOffset));
1044                 goto label_null;
1045         }
1046
1047         if ((ThisFrameLen + 8)> RxBufferLength) // 8 for (RT2870_RXDMALEN_FIELD_SIZE + sizeof(RXINFO_STRUC))
1048         {
1049                 DBGPRINT(RT_DEBUG_TRACE,("BIRIdx(%d):FrameLen(0x%lx) outranges. BulkInLen=0x%lx, remaining RxBufLen=0x%lx, ReadPos=0x%lx\n",
1050                                                 pAd->NextRxBulkInReadIndex, ThisFrameLen, pRxContext->BulkInOffset, RxBufferLength, pAd->ReadPosition));
1051
1052                 // error frame. finish this loop
1053                 goto label_null;
1054         }
1055
1056         // skip USB frame length field
1057         pData += RT2870_RXDMALEN_FIELD_SIZE;
1058         pRxWI = (PRXWI_STRUC)pData;
1059
1060         if (pRxWI->MPDUtotalByteCount > ThisFrameLen)
1061         {
1062                 DBGPRINT(RT_DEBUG_ERROR, ("%s():pRxWIMPDUtotalByteCount(%d) large than RxDMALen(%ld)\n",
1063                                                                         __func__, pRxWI->MPDUtotalByteCount, ThisFrameLen));
1064                 goto label_null;
1065         }
1066
1067         // allocate a rx packet
1068         pSkb = dev_alloc_skb(ThisFrameLen);
1069         if (pSkb == NULL)
1070         {
1071                 DBGPRINT(RT_DEBUG_ERROR,("%s():Cannot Allocate sk buffer for this Bulk-In buffer!\n", __func__));
1072                 goto label_null;
1073         }
1074
1075         // copy the rx packet
1076         memcpy(skb_put(pSkb, ThisFrameLen), pData, ThisFrameLen);
1077         RTPKT_TO_OSPKT(pSkb)->dev = get_netdev_from_bssid(pAd, BSS0);
1078         RTMP_SET_PACKET_SOURCE(OSPKT_TO_RTPKT(pSkb), PKTSRC_NDIS);
1079
1080         // copy RxD
1081         *pSaveRxD = *(PRXINFO_STRUC)(pData + ThisFrameLen);
1082
1083         // update next packet read position.
1084         pAd->ReadPosition += (ThisFrameLen + RT2870_RXDMALEN_FIELD_SIZE + RXINFO_SIZE); // 8 for (RT2870_RXDMALEN_FIELD_SIZE + sizeof(RXINFO_STRUC))
1085
1086         return pSkb;
1087
1088 label_null:
1089
1090         return NULL;
1091 }
1092
1093
1094 /*
1095 ========================================================================
1096 Routine Description:
1097     Handle received packets.
1098
1099 Arguments:
1100         data                            - URB information pointer
1101
1102 Return Value:
1103     None
1104
1105 Note:
1106 ========================================================================
1107 */
1108 static void rx_done_tasklet(unsigned long data)
1109 {
1110         purbb_t                         pUrb;
1111         PRX_CONTEXT                     pRxContext;
1112         PRTMP_ADAPTER           pAd;
1113         NTSTATUS                        Status;
1114         unsigned int            IrqFlags;
1115
1116         pUrb            = (purbb_t)data;
1117         pRxContext      = (PRX_CONTEXT)pUrb->context;
1118         pAd             = pRxContext->pAd;
1119         Status = pUrb->status;
1120
1121
1122         RTMP_IRQ_LOCK(&pAd->BulkInLock, IrqFlags);
1123         pRxContext->InUse = FALSE;
1124         pRxContext->IRPPending = FALSE;
1125         pRxContext->BulkInOffset += pUrb->actual_length;
1126         //NdisInterlockedDecrement(&pAd->PendingRx);
1127         pAd->PendingRx--;
1128
1129         if (Status == USB_ST_NOERROR)
1130         {
1131                 pAd->BulkInComplete++;
1132                 pAd->NextRxBulkInPosition = 0;
1133                 if (pRxContext->BulkInOffset)   // As jan's comment, it may bulk-in success but size is zero.
1134                 {
1135                         pRxContext->Readable = TRUE;
1136                         INC_RING_INDEX(pAd->NextRxBulkInIndex, RX_RING_SIZE);
1137                 }
1138                 RTMP_IRQ_UNLOCK(&pAd->BulkInLock, IrqFlags);
1139         }
1140         else     // STATUS_OTHER
1141         {
1142                 pAd->BulkInCompleteFail++;
1143                 // Still read this packet although it may comtain wrong bytes.
1144                 pRxContext->Readable = FALSE;
1145                 RTMP_IRQ_UNLOCK(&pAd->BulkInLock, IrqFlags);
1146
1147                 // Parsing all packets. because after reset, the index will reset to all zero.
1148                 if ((!RTMP_TEST_FLAG(pAd, (fRTMP_ADAPTER_RESET_IN_PROGRESS |
1149                                                                         fRTMP_ADAPTER_BULKIN_RESET |
1150                                                                         fRTMP_ADAPTER_HALT_IN_PROGRESS |
1151                                                                         fRTMP_ADAPTER_NIC_NOT_EXIST))))
1152                 {
1153
1154                         DBGPRINT_RAW(RT_DEBUG_ERROR, ("Bulk In Failed. Status=%d, BIIdx=0x%x, BIRIdx=0x%x, actual_length= 0x%x\n",
1155                                                         Status, pAd->NextRxBulkInIndex, pAd->NextRxBulkInReadIndex, pRxContext->pUrb->actual_length));
1156
1157                         RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_BULKIN_RESET);
1158                         RTUSBEnqueueInternalCmd(pAd, CMDTHREAD_RESET_BULK_IN, NULL, 0);
1159                 }
1160         }
1161
1162         ASSERT((pRxContext->InUse == pRxContext->IRPPending));
1163
1164         RTUSBBulkReceive(pAd);
1165
1166         return;
1167
1168 }
1169
1170
1171 static void rt2870_mgmt_dma_done_tasklet(unsigned long data)
1172 {
1173         PRTMP_ADAPTER   pAd;
1174         PTX_CONTEXT             pMLMEContext;
1175         int                             index;
1176         PNDIS_PACKET    pPacket;
1177         purbb_t                 pUrb;
1178         NTSTATUS                Status;
1179         unsigned long   IrqFlags;
1180
1181
1182         pUrb                    = (purbb_t)data;
1183         pMLMEContext    = (PTX_CONTEXT)pUrb->context;
1184         pAd                     = pMLMEContext->pAd;
1185         Status                  = pUrb->status;
1186         index                   = pMLMEContext->SelfIdx;
1187
1188         ASSERT((pAd->MgmtRing.TxDmaIdx == index));
1189
1190         RTMP_IRQ_LOCK(&pAd->BulkOutLock[MGMTPIPEIDX], IrqFlags);
1191
1192
1193         if (Status != USB_ST_NOERROR)
1194         {
1195                 //Bulk-Out fail status handle
1196                 if ((!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_RESET_IN_PROGRESS)) &&
1197                         (!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS)) &&
1198                         (!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST)) &&
1199                         (!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_BULKOUT_RESET)))
1200                 {
1201                         DBGPRINT_RAW(RT_DEBUG_ERROR, ("Bulk Out MLME Failed, Status=%d!\n", Status));
1202                         // TODO: How to handle about the MLMEBulkOut failed issue. Need to resend the mgmt pkt?
1203                         RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_BULKOUT_RESET);
1204                         pAd->bulkResetPipeid = (MGMTPIPEIDX | BULKOUT_MGMT_RESET_FLAG);
1205                 }
1206         }
1207
1208         pAd->BulkOutPending[MGMTPIPEIDX] = FALSE;
1209         RTMP_IRQ_UNLOCK(&pAd->BulkOutLock[MGMTPIPEIDX], IrqFlags);
1210
1211         RTMP_IRQ_LOCK(&pAd->MLMEBulkOutLock, IrqFlags);
1212         // Reset MLME context flags
1213         pMLMEContext->IRPPending = FALSE;
1214         pMLMEContext->InUse = FALSE;
1215         pMLMEContext->bWaitingBulkOut = FALSE;
1216         pMLMEContext->BulkOutSize = 0;
1217
1218         pPacket = pAd->MgmtRing.Cell[index].pNdisPacket;
1219         pAd->MgmtRing.Cell[index].pNdisPacket = NULL;
1220
1221         // Increase MgmtRing Index
1222         INC_RING_INDEX(pAd->MgmtRing.TxDmaIdx, MGMT_RING_SIZE);
1223         pAd->MgmtRing.TxSwFreeIdx++;
1224         RTMP_IRQ_UNLOCK(&pAd->MLMEBulkOutLock, IrqFlags);
1225
1226         // No-matter success or fail, we free the mgmt packet.
1227         if (pPacket)
1228                 RTMPFreeNdisPacket(pAd, pPacket);
1229
1230         if ((RTMP_TEST_FLAG(pAd, (fRTMP_ADAPTER_RESET_IN_PROGRESS |
1231                                                                 fRTMP_ADAPTER_HALT_IN_PROGRESS |
1232                                                                 fRTMP_ADAPTER_NIC_NOT_EXIST))))
1233         {
1234                 // do nothing and return directly.
1235         }
1236         else
1237         {
1238                 if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_BULKOUT_RESET) &&
1239                         ((pAd->bulkResetPipeid & BULKOUT_MGMT_RESET_FLAG) == BULKOUT_MGMT_RESET_FLAG))
1240                 {       // For Mgmt Bulk-Out failed, ignore it now.
1241                         RTUSBEnqueueInternalCmd(pAd, CMDTHREAD_RESET_BULK_OUT, NULL, 0);
1242                 }
1243                 else
1244                 {
1245
1246                         // Always call Bulk routine, even reset bulk.
1247                         // The protectioon of rest bulk should be in BulkOut routine
1248                         if (pAd->MgmtRing.TxSwFreeIdx < MGMT_RING_SIZE /* pMLMEContext->bWaitingBulkOut == TRUE */)
1249                         {
1250                                 RTUSB_SET_BULK_FLAG(pAd, fRTUSB_BULK_OUT_MLME);
1251                         }
1252                                 RTUSBKickBulkOut(pAd);
1253                         }
1254                 }
1255
1256 }
1257
1258
1259 static void rt2870_hcca_dma_done_tasklet(unsigned long data)
1260 {
1261         PRTMP_ADAPTER           pAd;
1262         PHT_TX_CONTEXT          pHTTXContext;
1263         UCHAR                           BulkOutPipeId = 4;
1264         purbb_t                         pUrb;
1265
1266         DBGPRINT_RAW(RT_DEBUG_ERROR, ("--->hcca_dma_done_tasklet\n"));
1267
1268         pUrb                    = (purbb_t)data;
1269         pHTTXContext    = (PHT_TX_CONTEXT)pUrb->context;
1270         pAd                             = pHTTXContext->pAd;
1271
1272         rt2870_dataout_complete_tasklet((unsigned long)pUrb);
1273
1274         if ((RTMP_TEST_FLAG(pAd, (fRTMP_ADAPTER_RESET_IN_PROGRESS |
1275                                                                 fRTMP_ADAPTER_HALT_IN_PROGRESS |
1276                                                                 fRTMP_ADAPTER_NIC_NOT_EXIST))))
1277         {
1278                 // do nothing and return directly.
1279         }
1280         else
1281         {
1282                 if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_BULKOUT_RESET))
1283                 {
1284                         RTUSBEnqueueInternalCmd(pAd, CMDTHREAD_RESET_BULK_OUT, NULL, 0);
1285                 }
1286                 else
1287                 {       pHTTXContext = &pAd->TxContext[BulkOutPipeId];
1288                         if ((pAd->TxSwQueue[BulkOutPipeId].Number > 0) &&
1289                                 /*((pHTTXContext->CurWritePosition > (pHTTXContext->NextBulkOutPosition + 0x6000)) || (pHTTXContext->NextBulkOutPosition > pHTTXContext->CurWritePosition + 0x6000)) && */
1290                                 (pAd->DeQueueRunning[BulkOutPipeId] == FALSE) &&
1291                                 (pHTTXContext->bCurWriting == FALSE))
1292                         {
1293                                 RTMPDeQueuePacket(pAd, FALSE, BulkOutPipeId, MAX_TX_PROCESS);
1294                         }
1295
1296 #ifndef RT30xx
1297                         RTUSB_SET_BULK_FLAG(pAd, fRTUSB_BULK_OUT_DATA_NORMAL);
1298 #endif
1299 #ifdef RT30xx
1300                         RTUSB_SET_BULK_FLAG(pAd, fRTUSB_BULK_OUT_DATA_NORMAL<<4);
1301 #endif
1302                         RTUSBKickBulkOut(pAd);
1303                 }
1304         }
1305
1306         DBGPRINT_RAW(RT_DEBUG_ERROR, ("<---hcca_dma_done_tasklet\n"));
1307 }
1308
1309
1310 static void rt2870_ac3_dma_done_tasklet(unsigned long data)
1311 {
1312         PRTMP_ADAPTER           pAd;
1313         PHT_TX_CONTEXT          pHTTXContext;
1314         UCHAR                           BulkOutPipeId = 3;
1315         purbb_t                         pUrb;
1316
1317
1318         pUrb                    = (purbb_t)data;
1319         pHTTXContext    = (PHT_TX_CONTEXT)pUrb->context;
1320         pAd                             = pHTTXContext->pAd;
1321
1322         rt2870_dataout_complete_tasklet((unsigned long)pUrb);
1323
1324         if ((RTMP_TEST_FLAG(pAd, (fRTMP_ADAPTER_RESET_IN_PROGRESS |
1325                                                                 fRTMP_ADAPTER_HALT_IN_PROGRESS |
1326                                                                 fRTMP_ADAPTER_NIC_NOT_EXIST))))
1327         {
1328                 // do nothing and return directly.
1329         }
1330         else
1331         {
1332                 if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_BULKOUT_RESET))
1333                 {
1334                         RTUSBEnqueueInternalCmd(pAd, CMDTHREAD_RESET_BULK_OUT, NULL, 0);
1335                 }
1336                 else
1337                 {       pHTTXContext = &pAd->TxContext[BulkOutPipeId];
1338                         if ((pAd->TxSwQueue[BulkOutPipeId].Number > 0) &&
1339                                 /*((pHTTXContext->CurWritePosition > (pHTTXContext->NextBulkOutPosition + 0x6000)) || (pHTTXContext->NextBulkOutPosition > pHTTXContext->CurWritePosition + 0x6000)) && */
1340                                 (pAd->DeQueueRunning[BulkOutPipeId] == FALSE) &&
1341                                 (pHTTXContext->bCurWriting == FALSE))
1342                         {
1343                                 RTMPDeQueuePacket(pAd, FALSE, BulkOutPipeId, MAX_TX_PROCESS);
1344                         }
1345
1346                         RTUSB_SET_BULK_FLAG(pAd, fRTUSB_BULK_OUT_DATA_NORMAL<<3);
1347                         RTUSBKickBulkOut(pAd);
1348                 }
1349         }
1350
1351
1352                 return;
1353 }
1354
1355
1356 static void rt2870_ac2_dma_done_tasklet(unsigned long data)
1357 {
1358         PRTMP_ADAPTER           pAd;
1359         PHT_TX_CONTEXT          pHTTXContext;
1360         UCHAR                           BulkOutPipeId = 2;
1361         purbb_t                         pUrb;
1362
1363
1364         pUrb                    = (purbb_t)data;
1365         pHTTXContext    = (PHT_TX_CONTEXT)pUrb->context;
1366         pAd                             = pHTTXContext->pAd;
1367
1368         rt2870_dataout_complete_tasklet((unsigned long)pUrb);
1369
1370         if ((RTMP_TEST_FLAG(pAd, (fRTMP_ADAPTER_RESET_IN_PROGRESS |
1371                                                                 fRTMP_ADAPTER_HALT_IN_PROGRESS |
1372                                                                 fRTMP_ADAPTER_NIC_NOT_EXIST))))
1373         {
1374                 // do nothing and return directly.
1375         }
1376         else
1377         {
1378                 if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_BULKOUT_RESET))
1379                 {
1380                         RTUSBEnqueueInternalCmd(pAd, CMDTHREAD_RESET_BULK_OUT, NULL, 0);
1381                 }
1382                 else
1383                 {       pHTTXContext = &pAd->TxContext[BulkOutPipeId];
1384                         if ((pAd->TxSwQueue[BulkOutPipeId].Number > 0) &&
1385                                 /*((pHTTXContext->CurWritePosition > (pHTTXContext->NextBulkOutPosition + 0x6000)) || (pHTTXContext->NextBulkOutPosition > pHTTXContext->CurWritePosition + 0x6000)) && */
1386                                 (pAd->DeQueueRunning[BulkOutPipeId] == FALSE) &&
1387                                 (pHTTXContext->bCurWriting == FALSE))
1388                         {
1389                                 RTMPDeQueuePacket(pAd, FALSE, BulkOutPipeId, MAX_TX_PROCESS);
1390                         }
1391
1392                         RTUSB_SET_BULK_FLAG(pAd, fRTUSB_BULK_OUT_DATA_NORMAL<<2);
1393                         RTUSBKickBulkOut(pAd);
1394                 }
1395         }
1396
1397                 return;
1398 }
1399
1400
1401 static void rt2870_ac1_dma_done_tasklet(unsigned long data)
1402 {
1403         PRTMP_ADAPTER           pAd;
1404         PHT_TX_CONTEXT          pHTTXContext;
1405         UCHAR                           BulkOutPipeId = 1;
1406         purbb_t                         pUrb;
1407
1408
1409         pUrb                    = (purbb_t)data;
1410         pHTTXContext    = (PHT_TX_CONTEXT)pUrb->context;
1411         pAd                             = pHTTXContext->pAd;
1412
1413         rt2870_dataout_complete_tasklet((unsigned long)pUrb);
1414
1415         if ((RTMP_TEST_FLAG(pAd, (fRTMP_ADAPTER_RESET_IN_PROGRESS |
1416                                                                 fRTMP_ADAPTER_HALT_IN_PROGRESS |
1417                                                                 fRTMP_ADAPTER_NIC_NOT_EXIST))))
1418         {
1419                 // do nothing and return directly.
1420         }
1421         else
1422         {
1423                 if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_BULKOUT_RESET))
1424                 {
1425                         RTUSBEnqueueInternalCmd(pAd, CMDTHREAD_RESET_BULK_OUT, NULL, 0);
1426                 }
1427                 else
1428                 {       pHTTXContext = &pAd->TxContext[BulkOutPipeId];
1429                         if ((pAd->TxSwQueue[BulkOutPipeId].Number > 0) &&
1430                                 /*((pHTTXContext->CurWritePosition > (pHTTXContext->NextBulkOutPosition + 0x6000)) || (pHTTXContext->NextBulkOutPosition > pHTTXContext->CurWritePosition + 0x6000)) && */
1431                                 (pAd->DeQueueRunning[BulkOutPipeId] == FALSE) &&
1432                                 (pHTTXContext->bCurWriting == FALSE))
1433                         {
1434                                 RTMPDeQueuePacket(pAd, FALSE, BulkOutPipeId, MAX_TX_PROCESS);
1435                         }
1436
1437                         RTUSB_SET_BULK_FLAG(pAd, fRTUSB_BULK_OUT_DATA_NORMAL<<1);
1438                         RTUSBKickBulkOut(pAd);
1439                 }
1440         }
1441
1442
1443         return;
1444 }
1445
1446
1447 static void rt2870_ac0_dma_done_tasklet(unsigned long data)
1448 {
1449         PRTMP_ADAPTER           pAd;
1450         PHT_TX_CONTEXT          pHTTXContext;
1451         UCHAR                           BulkOutPipeId = 0;
1452         purbb_t                         pUrb;
1453
1454
1455         pUrb                    = (purbb_t)data;
1456         pHTTXContext    = (PHT_TX_CONTEXT)pUrb->context;
1457         pAd                             = pHTTXContext->pAd;
1458
1459         rt2870_dataout_complete_tasklet((unsigned long)pUrb);
1460
1461         if ((RTMP_TEST_FLAG(pAd, (fRTMP_ADAPTER_RESET_IN_PROGRESS |
1462                                                                 fRTMP_ADAPTER_HALT_IN_PROGRESS |
1463                                                                 fRTMP_ADAPTER_NIC_NOT_EXIST))))
1464         {
1465                 // do nothing and return directly.
1466         }
1467         else
1468         {
1469                 if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_BULKOUT_RESET))
1470                 {
1471                         RTUSBEnqueueInternalCmd(pAd, CMDTHREAD_RESET_BULK_OUT, NULL, 0);
1472                 }
1473                 else
1474                 {       pHTTXContext = &pAd->TxContext[BulkOutPipeId];
1475                         if ((pAd->TxSwQueue[BulkOutPipeId].Number > 0) &&
1476                                 /*  ((pHTTXContext->CurWritePosition > (pHTTXContext->NextBulkOutPosition + 0x6000)) || (pHTTXContext->NextBulkOutPosition > pHTTXContext->CurWritePosition + 0x6000)) && */
1477                                 (pAd->DeQueueRunning[BulkOutPipeId] == FALSE) &&
1478                                 (pHTTXContext->bCurWriting == FALSE))
1479                         {
1480                                 RTMPDeQueuePacket(pAd, FALSE, BulkOutPipeId, MAX_TX_PROCESS);
1481                         }
1482
1483                         RTUSB_SET_BULK_FLAG(pAd, fRTUSB_BULK_OUT_DATA_NORMAL);
1484                         RTUSBKickBulkOut(pAd);
1485                 }
1486         }
1487
1488
1489         return;
1490
1491 }
1492
1493
1494 static void rt2870_null_frame_complete_tasklet(unsigned long data)
1495 {
1496         PRTMP_ADAPTER   pAd;
1497         PTX_CONTEXT             pNullContext;
1498         purbb_t                 pUrb;
1499         NTSTATUS                Status;
1500         unsigned long   irqFlag;
1501
1502
1503         pUrb                    = (purbb_t)data;
1504         pNullContext    = (PTX_CONTEXT)pUrb->context;
1505         pAd                     = pNullContext->pAd;
1506         Status                  = pUrb->status;
1507
1508         // Reset Null frame context flags
1509         RTMP_IRQ_LOCK(&pAd->BulkOutLock[0], irqFlag);
1510         pNullContext->IRPPending        = FALSE;
1511         pNullContext->InUse             = FALSE;
1512         pAd->BulkOutPending[0] = FALSE;
1513         pAd->watchDogTxPendingCnt[0] = 0;
1514
1515         if (Status == USB_ST_NOERROR)
1516         {
1517                 RTMP_IRQ_UNLOCK(&pAd->BulkOutLock[0], irqFlag);
1518
1519                 RTMPDeQueuePacket(pAd, FALSE, NUM_OF_TX_RING, MAX_TX_PROCESS);
1520         }
1521         else    // STATUS_OTHER
1522         {
1523                 if ((!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_RESET_IN_PROGRESS)) &&
1524                         (!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS)) &&
1525                         (!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST)) &&
1526                         (!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_BULKOUT_RESET)))
1527                 {
1528                         DBGPRINT_RAW(RT_DEBUG_ERROR, ("Bulk Out Null Frame Failed, ReasonCode=%d!\n", Status));
1529                         RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_BULKOUT_RESET);
1530                         pAd->bulkResetPipeid = (MGMTPIPEIDX | BULKOUT_MGMT_RESET_FLAG);
1531                         RTMP_IRQ_UNLOCK(&pAd->BulkOutLock[0], irqFlag);
1532                         RTUSBEnqueueInternalCmd(pAd, CMDTHREAD_RESET_BULK_OUT, NULL, 0);
1533                 }
1534                 else
1535                 {
1536                         RTMP_IRQ_UNLOCK(&pAd->BulkOutLock[0], irqFlag);
1537                 }
1538         }
1539
1540         // Always call Bulk routine, even reset bulk.
1541         // The protectioon of rest bulk should be in BulkOut routine
1542         RTUSBKickBulkOut(pAd);
1543
1544 }
1545
1546
1547 static void rt2870_rts_frame_complete_tasklet(unsigned long data)
1548 {
1549         PRTMP_ADAPTER   pAd;
1550         PTX_CONTEXT             pRTSContext;
1551         purbb_t                 pUrb;
1552         NTSTATUS                Status;
1553         unsigned long   irqFlag;
1554
1555
1556         pUrb            = (purbb_t)data;
1557         pRTSContext     = (PTX_CONTEXT)pUrb->context;
1558         pAd                     = pRTSContext->pAd;
1559         Status          = pUrb->status;
1560
1561         // Reset RTS frame context flags
1562         RTMP_IRQ_LOCK(&pAd->BulkOutLock[0], irqFlag);
1563         pRTSContext->IRPPending = FALSE;
1564         pRTSContext->InUse              = FALSE;
1565
1566         if (Status == USB_ST_NOERROR)
1567         {
1568                 RTMP_IRQ_UNLOCK(&pAd->BulkOutLock[0], irqFlag);
1569                 RTMPDeQueuePacket(pAd, FALSE, NUM_OF_TX_RING, MAX_TX_PROCESS);
1570         }
1571         else    // STATUS_OTHER
1572         {
1573                 if ((!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_RESET_IN_PROGRESS)) &&
1574                         (!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS)) &&
1575                         (!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST)) &&
1576                         (!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_BULKOUT_RESET)))
1577                 {
1578                         DBGPRINT_RAW(RT_DEBUG_ERROR, ("Bulk Out RTS Frame Failed\n"));
1579                         RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_BULKOUT_RESET);
1580                         pAd->bulkResetPipeid = (MGMTPIPEIDX | BULKOUT_MGMT_RESET_FLAG);
1581                         RTMP_IRQ_UNLOCK(&pAd->BulkOutLock[0], irqFlag);
1582                         RTUSBEnqueueInternalCmd(pAd, CMDTHREAD_RESET_BULK_OUT, NULL, 0);
1583                 }
1584                 else
1585                 {
1586                         RTMP_IRQ_UNLOCK(&pAd->BulkOutLock[0], irqFlag);
1587                 }
1588         }
1589
1590         RTMP_SEM_LOCK(&pAd->BulkOutLock[pRTSContext->BulkOutPipeId]);
1591         pAd->BulkOutPending[pRTSContext->BulkOutPipeId] = FALSE;
1592         RTMP_SEM_UNLOCK(&pAd->BulkOutLock[pRTSContext->BulkOutPipeId]);
1593
1594         // Always call Bulk routine, even reset bulk.
1595         // The protectioon of rest bulk should be in BulkOut routine
1596         RTUSBKickBulkOut(pAd);
1597
1598 }
1599
1600
1601 static void rt2870_pspoll_frame_complete_tasklet(unsigned long data)
1602 {
1603         PRTMP_ADAPTER   pAd;
1604         PTX_CONTEXT             pPsPollContext;
1605         purbb_t                 pUrb;
1606         NTSTATUS                Status;
1607
1608
1609         pUrb                    = (purbb_t)data;
1610         pPsPollContext  = (PTX_CONTEXT)pUrb->context;
1611         pAd                             = pPsPollContext->pAd;
1612         Status                  = pUrb->status;
1613
1614         // Reset PsPoll context flags
1615         pPsPollContext->IRPPending      = FALSE;
1616         pPsPollContext->InUse           = FALSE;
1617         pAd->watchDogTxPendingCnt[0] = 0;
1618
1619         if (Status == USB_ST_NOERROR)
1620         {
1621                 RTMPDeQueuePacket(pAd, FALSE, NUM_OF_TX_RING, MAX_TX_PROCESS);
1622         }
1623         else // STATUS_OTHER
1624         {
1625                 if ((!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_RESET_IN_PROGRESS)) &&
1626                         (!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS)) &&
1627                         (!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST)) &&
1628                         (!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_BULKOUT_RESET)))
1629                 {
1630                         DBGPRINT_RAW(RT_DEBUG_ERROR, ("Bulk Out PSPoll Failed\n"));
1631                         RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_BULKOUT_RESET);
1632                         pAd->bulkResetPipeid = (MGMTPIPEIDX | BULKOUT_MGMT_RESET_FLAG);
1633                         RTUSBEnqueueInternalCmd(pAd, CMDTHREAD_RESET_BULK_OUT, NULL, 0);
1634                 }
1635         }
1636
1637         RTMP_SEM_LOCK(&pAd->BulkOutLock[0]);
1638         pAd->BulkOutPending[0] = FALSE;
1639         RTMP_SEM_UNLOCK(&pAd->BulkOutLock[0]);
1640
1641         // Always call Bulk routine, even reset bulk.
1642         // The protectioon of rest bulk should be in BulkOut routine
1643         RTUSBKickBulkOut(pAd);
1644
1645 }
1646
1647
1648 static void rt2870_dataout_complete_tasklet(unsigned long data)
1649 {
1650         PRTMP_ADAPTER           pAd;
1651         purbb_t                         pUrb;
1652         POS_COOKIE                      pObj;
1653         PHT_TX_CONTEXT          pHTTXContext;
1654         UCHAR                           BulkOutPipeId;
1655         NTSTATUS                        Status;
1656         unsigned long           IrqFlags;
1657
1658
1659         pUrb                    = (purbb_t)data;
1660         pHTTXContext    = (PHT_TX_CONTEXT)pUrb->context;
1661         pAd                             = pHTTXContext->pAd;
1662         pObj                    = (POS_COOKIE) pAd->OS_Cookie;
1663         Status                  = pUrb->status;
1664
1665         // Store BulkOut PipeId
1666         BulkOutPipeId = pHTTXContext->BulkOutPipeId;
1667         pAd->BulkOutDataOneSecCount++;
1668
1669         //DBGPRINT(RT_DEBUG_LOUD, ("Done-B(%d):I=0x%lx, CWPos=%ld, NBPos=%ld, ENBPos=%ld, bCopy=%d!\n", BulkOutPipeId, in_interrupt(), pHTTXContext->CurWritePosition,
1670         //              pHTTXContext->NextBulkOutPosition, pHTTXContext->ENextBulkOutPosition, pHTTXContext->bCopySavePad));
1671
1672         RTMP_IRQ_LOCK(&pAd->BulkOutLock[BulkOutPipeId], IrqFlags);
1673         pAd->BulkOutPending[BulkOutPipeId] = FALSE;
1674         pHTTXContext->IRPPending = FALSE;
1675         pAd->watchDogTxPendingCnt[BulkOutPipeId] = 0;
1676
1677         if (Status == USB_ST_NOERROR)
1678         {
1679                 pAd->BulkOutComplete++;
1680
1681                 RTMP_IRQ_UNLOCK(&pAd->BulkOutLock[BulkOutPipeId], IrqFlags);
1682
1683                 pAd->Counters8023.GoodTransmits++;
1684                 //RTMP_IRQ_LOCK(&pAd->TxContextQueueLock[BulkOutPipeId], IrqFlags);
1685                 FREE_HTTX_RING(pAd, BulkOutPipeId, pHTTXContext);
1686                 //RTMP_IRQ_UNLOCK(&pAd->TxContextQueueLock[BulkOutPipeId], IrqFlags);
1687
1688
1689         }
1690         else    // STATUS_OTHER
1691         {
1692                 PUCHAR  pBuf;
1693
1694                 pAd->BulkOutCompleteOther++;
1695
1696                 pBuf = &pHTTXContext->TransferBuffer->field.WirelessPacket[pHTTXContext->NextBulkOutPosition];
1697
1698                 if (!RTMP_TEST_FLAG(pAd, (fRTMP_ADAPTER_RESET_IN_PROGRESS |
1699                                                                         fRTMP_ADAPTER_HALT_IN_PROGRESS |
1700                                                                         fRTMP_ADAPTER_NIC_NOT_EXIST |
1701                                                                         fRTMP_ADAPTER_BULKOUT_RESET)))
1702                 {
1703                         RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_BULKOUT_RESET);
1704                         pAd->bulkResetPipeid = BulkOutPipeId;
1705                         pAd->bulkResetReq[BulkOutPipeId] = pAd->BulkOutReq;
1706                 }
1707                 RTMP_IRQ_UNLOCK(&pAd->BulkOutLock[BulkOutPipeId], IrqFlags);
1708
1709                 DBGPRINT_RAW(RT_DEBUG_ERROR, ("BulkOutDataPacket failed: ReasonCode=%d!\n", Status));
1710                 DBGPRINT_RAW(RT_DEBUG_ERROR, ("\t>>BulkOut Req=0x%lx, Complete=0x%lx, Other=0x%lx\n", pAd->BulkOutReq, pAd->BulkOutComplete, pAd->BulkOutCompleteOther));
1711                 DBGPRINT_RAW(RT_DEBUG_ERROR, ("\t>>BulkOut Header:%x %x %x %x %x %x %x %x\n", pBuf[0], pBuf[1], pBuf[2], pBuf[3], pBuf[4], pBuf[5], pBuf[6], pBuf[7]));
1712                 //DBGPRINT_RAW(RT_DEBUG_ERROR, (">>BulkOutCompleteCancel=0x%x, BulkOutCompleteOther=0x%x\n", pAd->BulkOutCompleteCancel, pAd->BulkOutCompleteOther));
1713
1714         }
1715
1716         //
1717         // bInUse = TRUE, means some process are filling TX data, after that must turn on bWaitingBulkOut
1718         // bWaitingBulkOut = TRUE, means the TX data are waiting for bulk out.
1719         //
1720         //RTMP_IRQ_LOCK(&pAd->TxContextQueueLock[BulkOutPipeId], IrqFlags);
1721         if ((pHTTXContext->ENextBulkOutPosition != pHTTXContext->CurWritePosition) &&
1722                 (pHTTXContext->ENextBulkOutPosition != (pHTTXContext->CurWritePosition+8)) &&
1723                 !RTUSB_TEST_BULK_FLAG(pAd, (fRTUSB_BULK_OUT_DATA_FRAG << BulkOutPipeId)))
1724         {
1725                 // Indicate There is data avaliable
1726                 RTUSB_SET_BULK_FLAG(pAd, (fRTUSB_BULK_OUT_DATA_NORMAL << BulkOutPipeId));
1727         }
1728         //RTMP_IRQ_UNLOCK(&pAd->TxContextQueueLock[BulkOutPipeId], IrqFlags);
1729
1730         // Always call Bulk routine, even reset bulk.
1731         // The protection of rest bulk should be in BulkOut routine
1732         RTUSBKickBulkOut(pAd);
1733 }
1734
1735 /* End of 2870_rtmp_init.c */