0f4c8af97e47f5c5b06f4f4b80ebbddcc7ed81b9
[linux-2.6.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 #ifndef RT30xx
703         pObj->MLMEThr_task              = NULL;
704         pObj->RTUSBCmdThr_task  = NULL;
705 #endif
706 #ifdef RT30xx
707         pObj->MLMEThr_pid       = NULL;
708         pObj->RTUSBCmdThr_pid   = NULL;
709 #endif
710         *ppAd = (PVOID)vmalloc(sizeof(RTMP_ADAPTER));
711
712         if (*ppAd)
713         {
714                 NdisZeroMemory(*ppAd, sizeof(RTMP_ADAPTER));
715                 ((PRTMP_ADAPTER)*ppAd)->OS_Cookie = handle;
716                 return (NDIS_STATUS_SUCCESS);
717         }
718         else
719         {
720                 return (NDIS_STATUS_FAILURE);
721         }
722 }
723
724
725 /*
726 ========================================================================
727 Routine Description:
728     Create kernel threads & tasklets.
729
730 Arguments:
731     *net_dev                    Pointer to wireless net device interface
732
733 Return Value:
734         NDIS_STATUS_SUCCESS
735         NDIS_STATUS_FAILURE
736
737 Note:
738 ========================================================================
739 */
740 NDIS_STATUS      CreateThreads(
741         IN      struct net_device *net_dev)
742 {
743         PRTMP_ADAPTER pAd = net_dev->ml_priv;
744         POS_COOKIE pObj = (POS_COOKIE) pAd->OS_Cookie;
745 #ifndef RT30xx
746         struct task_struct *tsk;
747 #endif
748 #ifdef RT30xx
749         pid_t pid_number;
750 #endif
751
752         //init_MUTEX(&(pAd->usbdev_semaphore));
753
754         init_MUTEX_LOCKED(&(pAd->mlme_semaphore));
755         init_completion (&pAd->mlmeComplete);
756
757         init_MUTEX_LOCKED(&(pAd->RTUSBCmd_semaphore));
758         init_completion (&pAd->CmdQComplete);
759
760         init_MUTEX_LOCKED(&(pAd->RTUSBTimer_semaphore));
761         init_completion (&pAd->TimerQComplete);
762
763         // Creat MLME Thread
764 #ifndef RT30xx
765         pObj->MLMEThr_task = NULL;
766         tsk = kthread_run(MlmeThread, pAd, "%s", pAd->net_dev->name);
767
768         if (IS_ERR(tsk)) {
769 #endif
770 #ifdef RT30xx
771         pObj->MLMEThr_pid = NULL;
772         pid_number = kernel_thread(MlmeThread, pAd, CLONE_VM);
773         if (pid_number < 0)
774         {
775 #endif
776                 printk (KERN_WARNING "%s: unable to start Mlme thread\n",pAd->net_dev->name);
777                 return NDIS_STATUS_FAILURE;
778         }
779
780 #ifndef RT30xx
781         pObj->MLMEThr_task = tsk;
782 #endif
783 #ifdef RT30xx
784         pObj->MLMEThr_pid = find_get_pid(pid_number);
785 #endif
786         // Wait for the thread to start
787         wait_for_completion(&(pAd->mlmeComplete));
788
789         // Creat Command Thread
790 #ifndef RT30xx
791         pObj->RTUSBCmdThr_task = NULL;
792         tsk = kthread_run(RTUSBCmdThread, pAd, "%s", pAd->net_dev->name);
793
794         if (IS_ERR(tsk) < 0)
795 #endif
796 #ifdef RT30xx
797         pObj->RTUSBCmdThr_pid = NULL;
798         pid_number = kernel_thread(RTUSBCmdThread, pAd, CLONE_VM);
799         if (pid_number < 0)
800 #endif
801         {
802                 printk (KERN_WARNING "%s: unable to start RTUSBCmd thread\n",pAd->net_dev->name);
803                 return NDIS_STATUS_FAILURE;
804         }
805
806 #ifndef RT30xx
807         pObj->RTUSBCmdThr_task = tsk;
808 #endif
809 #ifdef RT30xx
810         pObj->RTUSBCmdThr_pid = find_get_pid(pid_number);
811 #endif
812         wait_for_completion(&(pAd->CmdQComplete));
813
814 #ifndef RT30xx
815         pObj->TimerQThr_task = NULL;
816         tsk = kthread_run(TimerQThread, pAd, "%s", pAd->net_dev->name);
817         if (IS_ERR(tsk) < 0)
818 #endif
819 #ifdef RT30xx
820         pObj->TimerQThr_pid = NULL;
821         pid_number = kernel_thread(TimerQThread, pAd, CLONE_VM);
822         if (pid_number < 0)
823 #endif
824         {
825                 printk (KERN_WARNING "%s: unable to start TimerQThread\n",pAd->net_dev->name);
826                 return NDIS_STATUS_FAILURE;
827         }
828 #ifndef RT30xx
829         pObj->TimerQThr_task = tsk;
830 #endif
831 #ifdef RT30xx
832         pObj->TimerQThr_pid = find_get_pid(pid_number);
833 #endif
834         // Wait for the thread to start
835         wait_for_completion(&(pAd->TimerQComplete));
836
837         // Create receive tasklet
838         tasklet_init(&pObj->rx_done_task, rx_done_tasklet, (ULONG)pAd);
839         tasklet_init(&pObj->mgmt_dma_done_task, rt2870_mgmt_dma_done_tasklet, (unsigned long)pAd);
840         tasklet_init(&pObj->ac0_dma_done_task, rt2870_ac0_dma_done_tasklet, (unsigned long)pAd);
841         tasklet_init(&pObj->ac1_dma_done_task, rt2870_ac1_dma_done_tasklet, (unsigned long)pAd);
842         tasklet_init(&pObj->ac2_dma_done_task, rt2870_ac2_dma_done_tasklet, (unsigned long)pAd);
843         tasklet_init(&pObj->ac3_dma_done_task, rt2870_ac3_dma_done_tasklet, (unsigned long)pAd);
844         tasklet_init(&pObj->hcca_dma_done_task, rt2870_hcca_dma_done_tasklet, (unsigned long)pAd);
845         tasklet_init(&pObj->tbtt_task, tbtt_tasklet, (unsigned long)pAd);
846         tasklet_init(&pObj->null_frame_complete_task, rt2870_null_frame_complete_tasklet, (unsigned long)pAd);
847         tasklet_init(&pObj->rts_frame_complete_task, rt2870_rts_frame_complete_tasklet, (unsigned long)pAd);
848         tasklet_init(&pObj->pspoll_frame_complete_task, rt2870_pspoll_frame_complete_tasklet, (unsigned long)pAd);
849
850         return NDIS_STATUS_SUCCESS;
851 }
852
853 /*
854 ========================================================================
855 Routine Description:
856         As STA's BSSID is a WC too, it uses shared key table.
857         This function write correct unicast TX key to ASIC WCID.
858         And we still make a copy in our MacTab.Content[BSSID_WCID].PairwiseKey.
859         Caller guarantee TKIP/AES always has keyidx = 0. (pairwise key)
860         Caller guarantee WEP calls this function when set Txkey,  default key index=0~3.
861
862 Arguments:
863         pAd                                     Pointer to our adapter
864         pKey                                    Pointer to the where the key stored
865
866 Return Value:
867         NDIS_SUCCESS                    Add key successfully
868
869 Note:
870 ========================================================================
871 */
872 VOID    RTMPAddBSSIDCipher(
873         IN      PRTMP_ADAPTER           pAd,
874         IN      UCHAR                           Aid,
875         IN      PNDIS_802_11_KEY        pKey,
876         IN  UCHAR                       CipherAlg)
877 {
878         PUCHAR          pTxMic, pRxMic;
879         BOOLEAN         bKeyRSC, bAuthenticator; // indicate the receive SC set by KeyRSC value
880 //      UCHAR           CipherAlg;
881         UCHAR           i;
882         ULONG           WCIDAttri;
883         USHORT          offset;
884         UCHAR           KeyIdx, IVEIV[8];
885         UINT32          Value;
886
887         DBGPRINT(RT_DEBUG_TRACE, ("RTMPAddBSSIDCipher==> Aid = %d\n",Aid));
888
889         // Bit 29 of Add-key KeyRSC
890         bKeyRSC            = (pKey->KeyIndex & 0x20000000) ? TRUE : FALSE;
891
892         // Bit 28 of Add-key Authenticator
893         bAuthenticator = (pKey->KeyIndex & 0x10000000) ? TRUE : FALSE;
894         KeyIdx = (UCHAR)pKey->KeyIndex&0xff;
895
896         if (KeyIdx > 4)
897                 return;
898
899
900         if (pAd->MacTab.Content[Aid].PairwiseKey.CipherAlg == CIPHER_TKIP)
901         {       if (pAd->StaCfg.AuthMode == Ndis802_11AuthModeWPANone)
902                 {
903                         // for WPA-None Tx, Rx MIC is the same
904                         pTxMic = (PUCHAR) (&pKey->KeyMaterial) + 16;
905                         pRxMic = pTxMic;
906                 }
907                 else if (bAuthenticator == TRUE)
908                 {
909                         pTxMic = (PUCHAR) (&pKey->KeyMaterial) + 16;
910                         pRxMic = (PUCHAR) (&pKey->KeyMaterial) + 24;
911                 }
912                 else
913                 {
914                         pRxMic = (PUCHAR) (&pKey->KeyMaterial) + 16;
915                         pTxMic = (PUCHAR) (&pKey->KeyMaterial) + 24;
916                 }
917
918                 offset = PAIRWISE_KEY_TABLE_BASE + (Aid * HW_KEY_ENTRY_SIZE) + 0x10;
919                 for (i=0; i<8; )
920                 {
921                         Value = *(pTxMic+i);
922                         Value += (*(pTxMic+i+1)<<8);
923                         Value += (*(pTxMic+i+2)<<16);
924                         Value += (*(pTxMic+i+3)<<24);
925                         RTUSBWriteMACRegister(pAd, offset+i, Value);
926                         i+=4;
927                 }
928
929                 offset = PAIRWISE_KEY_TABLE_BASE + (Aid * HW_KEY_ENTRY_SIZE) + 0x18;
930                 for (i=0; i<8; )
931                 {
932                         Value = *(pRxMic+i);
933                         Value += (*(pRxMic+i+1)<<8);
934                         Value += (*(pRxMic+i+2)<<16);
935                         Value += (*(pRxMic+i+3)<<24);
936                         RTUSBWriteMACRegister(pAd, offset+i, Value);
937                         i+=4;
938                 }
939
940                 // Only Key lenth equal to TKIP key have these
941                 NdisMoveMemory(pAd->MacTab.Content[Aid].PairwiseKey.RxMic, pRxMic, 8);
942                 NdisMoveMemory(pAd->MacTab.Content[Aid].PairwiseKey.TxMic, pTxMic, 8);
943
944                 DBGPRINT(RT_DEBUG_TRACE,
945                                 ("      TxMIC  = %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x \n",
946                                 pTxMic[0],pTxMic[1],pTxMic[2],pTxMic[3],
947                                 pTxMic[4],pTxMic[5],pTxMic[6],pTxMic[7]));
948                 DBGPRINT(RT_DEBUG_TRACE,
949                                 ("      RxMIC  = %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x \n",
950                                 pRxMic[0],pRxMic[1],pRxMic[2],pRxMic[3],
951                                 pRxMic[4],pRxMic[5],pRxMic[6],pRxMic[7]));
952         }
953
954         // 2. Record Security Key.
955         pAd->MacTab.Content[BSSID_WCID].PairwiseKey.KeyLen= (UCHAR)pKey->KeyLength;
956         NdisMoveMemory(pAd->MacTab.Content[Aid].PairwiseKey.Key, &pKey->KeyMaterial, pKey->KeyLength);
957
958         // 3. Check RxTsc. And used to init to ASIC IV.
959         if (bKeyRSC == TRUE)
960                 NdisMoveMemory(pAd->MacTab.Content[Aid].PairwiseKey.RxTsc, &pKey->KeyRSC, 6);
961         else
962                 NdisZeroMemory(pAd->MacTab.Content[Aid].PairwiseKey.RxTsc, 6);
963
964         // 4. Init TxTsc to one based on WiFi WPA specs
965         pAd->MacTab.Content[Aid].PairwiseKey.TxTsc[0] = 1;
966         pAd->MacTab.Content[Aid].PairwiseKey.TxTsc[1] = 0;
967         pAd->MacTab.Content[Aid].PairwiseKey.TxTsc[2] = 0;
968         pAd->MacTab.Content[Aid].PairwiseKey.TxTsc[3] = 0;
969         pAd->MacTab.Content[Aid].PairwiseKey.TxTsc[4] = 0;
970         pAd->MacTab.Content[Aid].PairwiseKey.TxTsc[5] = 0;
971
972         CipherAlg = pAd->MacTab.Content[Aid].PairwiseKey.CipherAlg;
973
974         offset = PAIRWISE_KEY_TABLE_BASE + (Aid * HW_KEY_ENTRY_SIZE);
975         RTUSBMultiWrite(pAd, (USHORT) offset, pKey->KeyMaterial,
976                                 ((pKey->KeyLength == LEN_TKIP_KEY) ? 16 : (USHORT)pKey->KeyLength));
977
978         offset = SHARED_KEY_TABLE_BASE + (KeyIdx * HW_KEY_ENTRY_SIZE);
979         RTUSBMultiWrite(pAd, (USHORT) offset, pKey->KeyMaterial, (USHORT)pKey->KeyLength);
980
981         offset = PAIRWISE_IVEIV_TABLE_BASE + (Aid * HW_IVEIV_ENTRY_SIZE);
982         NdisZeroMemory(IVEIV, 8);
983
984         // IV/EIV
985         if ((CipherAlg == CIPHER_TKIP) ||
986                 (CipherAlg == CIPHER_TKIP_NO_MIC) ||
987                 (CipherAlg == CIPHER_AES))
988         {
989                 IVEIV[3] = 0x20; // Eiv bit on. keyid always 0 for pairwise key
990         }
991         // default key idx needs to set.
992         // in TKIP/AES KeyIdx = 0 , WEP KeyIdx is default tx key.
993         else
994         {
995                 IVEIV[3] |= (KeyIdx<< 6);
996         }
997         RTUSBMultiWrite(pAd, (USHORT) offset, IVEIV, 8);
998
999         // WCID Attribute UDF:3, BSSIdx:3, Alg:3, Keytable:1=PAIRWISE KEY, BSSIdx is 0
1000         if ((CipherAlg == CIPHER_TKIP) ||
1001                 (CipherAlg == CIPHER_TKIP_NO_MIC) ||
1002                 (CipherAlg == CIPHER_AES))
1003         {
1004                 WCIDAttri = (CipherAlg<<1)|SHAREDKEYTABLE;
1005         }
1006         else
1007                 WCIDAttri = (CipherAlg<<1)|SHAREDKEYTABLE;
1008
1009         offset = MAC_WCID_ATTRIBUTE_BASE + (Aid* HW_WCID_ATTRI_SIZE);
1010         RTUSBWriteMACRegister(pAd, offset, WCIDAttri);
1011         RTUSBReadMACRegister(pAd, offset, &Value);
1012
1013         DBGPRINT(RT_DEBUG_TRACE, ("BSSID_WCID : offset = %x, WCIDAttri = %lx\n",
1014                         offset, WCIDAttri));
1015
1016         // pAddr
1017         // Add Bssid mac address at linkup. not here.  check!
1018         /*offset = MAC_WCID_BASE + (BSSID_WCID * HW_WCID_ENTRY_SIZE);
1019         *for (i=0; i<MAC_ADDR_LEN; i++)
1020         {
1021                 RTMP_IO_WRITE8(pAd, offset+i, pKey->BSSID[i]);
1022         }
1023         */
1024
1025         DBGPRINT(RT_DEBUG_ERROR, ("AddBSSIDasWCIDEntry: Alg=%s, KeyLength = %d\n",
1026                         CipherName[CipherAlg], pKey->KeyLength));
1027         DBGPRINT(RT_DEBUG_TRACE, ("Key [idx=%x] [KeyLen = %d]\n",
1028                         pKey->KeyIndex, pKey->KeyLength));
1029         for(i=0; i<pKey->KeyLength; i++)
1030                 DBGPRINT_RAW(RT_DEBUG_TRACE,(" %x:", pKey->KeyMaterial[i]));
1031         DBGPRINT(RT_DEBUG_TRACE,("       \n"));
1032 }
1033
1034 /*
1035 ========================================================================
1036 Routine Description:
1037     Get a received packet.
1038
1039 Arguments:
1040         pAd                                     device control block
1041         pSaveRxD                        receive descriptor information
1042         *pbReschedule           need reschedule flag
1043         *pRxPending                     pending received packet flag
1044
1045 Return Value:
1046     the recieved packet
1047
1048 Note:
1049 ========================================================================
1050 */
1051 #define RT2870_RXDMALEN_FIELD_SIZE                      4
1052 PNDIS_PACKET GetPacketFromRxRing(
1053         IN              PRTMP_ADAPTER           pAd,
1054         OUT             PRT28XX_RXD_STRUC       pSaveRxD,
1055         OUT             BOOLEAN                         *pbReschedule,
1056         IN OUT  UINT32                          *pRxPending)
1057 {
1058         PRX_CONTEXT             pRxContext;
1059         PNDIS_PACKET    pSkb;
1060         PUCHAR                  pData;
1061         ULONG                   ThisFrameLen;
1062         ULONG                   RxBufferLength;
1063         PRXWI_STRUC             pRxWI;
1064
1065         pRxContext = &pAd->RxContext[pAd->NextRxBulkInReadIndex];
1066         if ((pRxContext->Readable == FALSE) || (pRxContext->InUse == TRUE))
1067                 return NULL;
1068
1069         RxBufferLength = pRxContext->BulkInOffset - pAd->ReadPosition;
1070         if (RxBufferLength < (RT2870_RXDMALEN_FIELD_SIZE + sizeof(RXWI_STRUC) + sizeof(RXINFO_STRUC)))
1071         {
1072                 goto label_null;
1073         }
1074
1075         pData = &pRxContext->TransferBuffer[pAd->ReadPosition]; /* 4KB */
1076         // The RXDMA field is 4 bytes, now just use the first 2 bytes. The Length including the (RXWI + MSDU + Padding)
1077         ThisFrameLen = *pData + (*(pData+1)<<8);
1078     if (ThisFrameLen == 0)
1079         {
1080                 DBGPRINT(RT_DEBUG_TRACE, ("BIRIdx(%d): RXDMALen is zero.[%ld], BulkInBufLen = %ld)\n",
1081                                                                 pAd->NextRxBulkInReadIndex, ThisFrameLen, pRxContext->BulkInOffset));
1082                 goto label_null;
1083         }
1084         if ((ThisFrameLen&0x3) != 0)
1085         {
1086                 DBGPRINT(RT_DEBUG_ERROR, ("BIRIdx(%d): RXDMALen not multiple of 4.[%ld], BulkInBufLen = %ld)\n",
1087                                                                 pAd->NextRxBulkInReadIndex, ThisFrameLen, pRxContext->BulkInOffset));
1088                 goto label_null;
1089         }
1090
1091         if ((ThisFrameLen + 8)> RxBufferLength) // 8 for (RT2870_RXDMALEN_FIELD_SIZE + sizeof(RXINFO_STRUC))
1092         {
1093                 DBGPRINT(RT_DEBUG_TRACE,("BIRIdx(%d):FrameLen(0x%lx) outranges. BulkInLen=0x%lx, remaining RxBufLen=0x%lx, ReadPos=0x%lx\n",
1094                                                 pAd->NextRxBulkInReadIndex, ThisFrameLen, pRxContext->BulkInOffset, RxBufferLength, pAd->ReadPosition));
1095
1096                 // error frame. finish this loop
1097                 goto label_null;
1098         }
1099
1100         // skip USB frame length field
1101         pData += RT2870_RXDMALEN_FIELD_SIZE;
1102         pRxWI = (PRXWI_STRUC)pData;
1103
1104         if (pRxWI->MPDUtotalByteCount > ThisFrameLen)
1105         {
1106                 DBGPRINT(RT_DEBUG_ERROR, ("%s():pRxWIMPDUtotalByteCount(%d) large than RxDMALen(%ld)\n",
1107                                                                         __func__, pRxWI->MPDUtotalByteCount, ThisFrameLen));
1108                 goto label_null;
1109         }
1110
1111         // allocate a rx packet
1112         pSkb = dev_alloc_skb(ThisFrameLen);
1113         if (pSkb == NULL)
1114         {
1115                 DBGPRINT(RT_DEBUG_ERROR,("%s():Cannot Allocate sk buffer for this Bulk-In buffer!\n", __func__));
1116                 goto label_null;
1117         }
1118
1119         // copy the rx packet
1120         memcpy(skb_put(pSkb, ThisFrameLen), pData, ThisFrameLen);
1121         RTPKT_TO_OSPKT(pSkb)->dev = get_netdev_from_bssid(pAd, BSS0);
1122         RTMP_SET_PACKET_SOURCE(OSPKT_TO_RTPKT(pSkb), PKTSRC_NDIS);
1123
1124         // copy RxD
1125         *pSaveRxD = *(PRXINFO_STRUC)(pData + ThisFrameLen);
1126
1127         // update next packet read position.
1128         pAd->ReadPosition += (ThisFrameLen + RT2870_RXDMALEN_FIELD_SIZE + RXINFO_SIZE); // 8 for (RT2870_RXDMALEN_FIELD_SIZE + sizeof(RXINFO_STRUC))
1129
1130         return pSkb;
1131
1132 label_null:
1133
1134         return NULL;
1135 }
1136
1137
1138 /*
1139 ========================================================================
1140 Routine Description:
1141     Handle received packets.
1142
1143 Arguments:
1144         data                            - URB information pointer
1145
1146 Return Value:
1147     None
1148
1149 Note:
1150 ========================================================================
1151 */
1152 static void rx_done_tasklet(unsigned long data)
1153 {
1154         purbb_t                         pUrb;
1155         PRX_CONTEXT                     pRxContext;
1156         PRTMP_ADAPTER           pAd;
1157         NTSTATUS                        Status;
1158         unsigned int            IrqFlags;
1159
1160         pUrb            = (purbb_t)data;
1161         pRxContext      = (PRX_CONTEXT)pUrb->context;
1162         pAd             = pRxContext->pAd;
1163         Status = pUrb->status;
1164
1165
1166         RTMP_IRQ_LOCK(&pAd->BulkInLock, IrqFlags);
1167         pRxContext->InUse = FALSE;
1168         pRxContext->IRPPending = FALSE;
1169         pRxContext->BulkInOffset += pUrb->actual_length;
1170         //NdisInterlockedDecrement(&pAd->PendingRx);
1171         pAd->PendingRx--;
1172
1173         if (Status == USB_ST_NOERROR)
1174         {
1175                 pAd->BulkInComplete++;
1176                 pAd->NextRxBulkInPosition = 0;
1177                 if (pRxContext->BulkInOffset)   // As jan's comment, it may bulk-in success but size is zero.
1178                 {
1179                         pRxContext->Readable = TRUE;
1180                         INC_RING_INDEX(pAd->NextRxBulkInIndex, RX_RING_SIZE);
1181                 }
1182                 RTMP_IRQ_UNLOCK(&pAd->BulkInLock, IrqFlags);
1183         }
1184         else     // STATUS_OTHER
1185         {
1186                 pAd->BulkInCompleteFail++;
1187                 // Still read this packet although it may comtain wrong bytes.
1188                 pRxContext->Readable = FALSE;
1189                 RTMP_IRQ_UNLOCK(&pAd->BulkInLock, IrqFlags);
1190
1191                 // Parsing all packets. because after reset, the index will reset to all zero.
1192                 if ((!RTMP_TEST_FLAG(pAd, (fRTMP_ADAPTER_RESET_IN_PROGRESS |
1193                                                                         fRTMP_ADAPTER_BULKIN_RESET |
1194                                                                         fRTMP_ADAPTER_HALT_IN_PROGRESS |
1195                                                                         fRTMP_ADAPTER_NIC_NOT_EXIST))))
1196                 {
1197
1198                         DBGPRINT_RAW(RT_DEBUG_ERROR, ("Bulk In Failed. Status=%d, BIIdx=0x%x, BIRIdx=0x%x, actual_length= 0x%x\n",
1199                                                         Status, pAd->NextRxBulkInIndex, pAd->NextRxBulkInReadIndex, pRxContext->pUrb->actual_length));
1200
1201                         RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_BULKIN_RESET);
1202                         RTUSBEnqueueInternalCmd(pAd, CMDTHREAD_RESET_BULK_IN, NULL, 0);
1203                 }
1204         }
1205
1206         ASSERT((pRxContext->InUse == pRxContext->IRPPending));
1207
1208         RTUSBBulkReceive(pAd);
1209
1210         return;
1211
1212 }
1213
1214
1215 static void rt2870_mgmt_dma_done_tasklet(unsigned long data)
1216 {
1217         PRTMP_ADAPTER   pAd;
1218         PTX_CONTEXT             pMLMEContext;
1219         int                             index;
1220         PNDIS_PACKET    pPacket;
1221         purbb_t                 pUrb;
1222         NTSTATUS                Status;
1223         unsigned long   IrqFlags;
1224
1225
1226         pUrb                    = (purbb_t)data;
1227         pMLMEContext    = (PTX_CONTEXT)pUrb->context;
1228         pAd                     = pMLMEContext->pAd;
1229         Status                  = pUrb->status;
1230         index                   = pMLMEContext->SelfIdx;
1231
1232         ASSERT((pAd->MgmtRing.TxDmaIdx == index));
1233
1234         RTMP_IRQ_LOCK(&pAd->BulkOutLock[MGMTPIPEIDX], IrqFlags);
1235
1236
1237         if (Status != USB_ST_NOERROR)
1238         {
1239                 //Bulk-Out fail status handle
1240                 if ((!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_RESET_IN_PROGRESS)) &&
1241                         (!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS)) &&
1242                         (!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST)) &&
1243                         (!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_BULKOUT_RESET)))
1244                 {
1245                         DBGPRINT_RAW(RT_DEBUG_ERROR, ("Bulk Out MLME Failed, Status=%d!\n", Status));
1246                         // TODO: How to handle about the MLMEBulkOut failed issue. Need to resend the mgmt pkt?
1247                         RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_BULKOUT_RESET);
1248                         pAd->bulkResetPipeid = (MGMTPIPEIDX | BULKOUT_MGMT_RESET_FLAG);
1249                 }
1250         }
1251
1252         pAd->BulkOutPending[MGMTPIPEIDX] = FALSE;
1253         RTMP_IRQ_UNLOCK(&pAd->BulkOutLock[MGMTPIPEIDX], IrqFlags);
1254
1255         RTMP_IRQ_LOCK(&pAd->MLMEBulkOutLock, IrqFlags);
1256         // Reset MLME context flags
1257         pMLMEContext->IRPPending = FALSE;
1258         pMLMEContext->InUse = FALSE;
1259         pMLMEContext->bWaitingBulkOut = FALSE;
1260         pMLMEContext->BulkOutSize = 0;
1261
1262         pPacket = pAd->MgmtRing.Cell[index].pNdisPacket;
1263         pAd->MgmtRing.Cell[index].pNdisPacket = NULL;
1264
1265         // Increase MgmtRing Index
1266         INC_RING_INDEX(pAd->MgmtRing.TxDmaIdx, MGMT_RING_SIZE);
1267         pAd->MgmtRing.TxSwFreeIdx++;
1268         RTMP_IRQ_UNLOCK(&pAd->MLMEBulkOutLock, IrqFlags);
1269
1270         // No-matter success or fail, we free the mgmt packet.
1271         if (pPacket)
1272                 RTMPFreeNdisPacket(pAd, pPacket);
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                         ((pAd->bulkResetPipeid & BULKOUT_MGMT_RESET_FLAG) == BULKOUT_MGMT_RESET_FLAG))
1284                 {       // For Mgmt Bulk-Out failed, ignore it now.
1285                         RTUSBEnqueueInternalCmd(pAd, CMDTHREAD_RESET_BULK_OUT, NULL, 0);
1286                 }
1287                 else
1288                 {
1289
1290                         // Always call Bulk routine, even reset bulk.
1291                         // The protectioon of rest bulk should be in BulkOut routine
1292                         if (pAd->MgmtRing.TxSwFreeIdx < MGMT_RING_SIZE /* pMLMEContext->bWaitingBulkOut == TRUE */)
1293                         {
1294                                 RTUSB_SET_BULK_FLAG(pAd, fRTUSB_BULK_OUT_MLME);
1295                         }
1296                                 RTUSBKickBulkOut(pAd);
1297                         }
1298                 }
1299
1300 }
1301
1302
1303 static void rt2870_hcca_dma_done_tasklet(unsigned long data)
1304 {
1305         PRTMP_ADAPTER           pAd;
1306         PHT_TX_CONTEXT          pHTTXContext;
1307         UCHAR                           BulkOutPipeId = 4;
1308         purbb_t                         pUrb;
1309
1310 #ifndef RT30xx
1311         DBGPRINT_RAW(RT_DEBUG_ERROR, ("--->hcca_dma_done_tasklet\n"));
1312 #endif
1313
1314         pUrb                    = (purbb_t)data;
1315         pHTTXContext    = (PHT_TX_CONTEXT)pUrb->context;
1316         pAd                             = pHTTXContext->pAd;
1317
1318         rt2870_dataout_complete_tasklet((unsigned long)pUrb);
1319
1320         if ((RTMP_TEST_FLAG(pAd, (fRTMP_ADAPTER_RESET_IN_PROGRESS |
1321                                                                 fRTMP_ADAPTER_HALT_IN_PROGRESS |
1322                                                                 fRTMP_ADAPTER_NIC_NOT_EXIST))))
1323         {
1324                 // do nothing and return directly.
1325         }
1326         else
1327         {
1328                 if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_BULKOUT_RESET))
1329                 {
1330                         RTUSBEnqueueInternalCmd(pAd, CMDTHREAD_RESET_BULK_OUT, NULL, 0);
1331                 }
1332                 else
1333                 {       pHTTXContext = &pAd->TxContext[BulkOutPipeId];
1334                         if ((pAd->TxSwQueue[BulkOutPipeId].Number > 0) &&
1335                                 /*((pHTTXContext->CurWritePosition > (pHTTXContext->NextBulkOutPosition + 0x6000)) || (pHTTXContext->NextBulkOutPosition > pHTTXContext->CurWritePosition + 0x6000)) && */
1336                                 (pAd->DeQueueRunning[BulkOutPipeId] == FALSE) &&
1337                                 (pHTTXContext->bCurWriting == FALSE))
1338                         {
1339                                 RTMPDeQueuePacket(pAd, FALSE, BulkOutPipeId, MAX_TX_PROCESS);
1340                         }
1341
1342 #ifndef RT30xx
1343                         RTUSB_SET_BULK_FLAG(pAd, fRTUSB_BULK_OUT_DATA_NORMAL);
1344 #endif
1345 #ifdef RT30xx
1346                         RTUSB_SET_BULK_FLAG(pAd, fRTUSB_BULK_OUT_DATA_NORMAL<<4);
1347 #endif
1348                         RTUSBKickBulkOut(pAd);
1349                 }
1350         }
1351
1352 #ifndef RT30xx
1353         DBGPRINT_RAW(RT_DEBUG_ERROR, ("<---hcca_dma_done_tasklet\n"));
1354 #endif
1355                 return;
1356 }
1357
1358
1359 static void rt2870_ac3_dma_done_tasklet(unsigned long data)
1360 {
1361         PRTMP_ADAPTER           pAd;
1362         PHT_TX_CONTEXT          pHTTXContext;
1363         UCHAR                           BulkOutPipeId = 3;
1364         purbb_t                         pUrb;
1365
1366
1367         pUrb                    = (purbb_t)data;
1368         pHTTXContext    = (PHT_TX_CONTEXT)pUrb->context;
1369         pAd                             = pHTTXContext->pAd;
1370
1371         rt2870_dataout_complete_tasklet((unsigned long)pUrb);
1372
1373         if ((RTMP_TEST_FLAG(pAd, (fRTMP_ADAPTER_RESET_IN_PROGRESS |
1374                                                                 fRTMP_ADAPTER_HALT_IN_PROGRESS |
1375                                                                 fRTMP_ADAPTER_NIC_NOT_EXIST))))
1376         {
1377                 // do nothing and return directly.
1378         }
1379         else
1380         {
1381                 if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_BULKOUT_RESET))
1382                 {
1383                         RTUSBEnqueueInternalCmd(pAd, CMDTHREAD_RESET_BULK_OUT, NULL, 0);
1384                 }
1385                 else
1386                 {       pHTTXContext = &pAd->TxContext[BulkOutPipeId];
1387                         if ((pAd->TxSwQueue[BulkOutPipeId].Number > 0) &&
1388                                 /*((pHTTXContext->CurWritePosition > (pHTTXContext->NextBulkOutPosition + 0x6000)) || (pHTTXContext->NextBulkOutPosition > pHTTXContext->CurWritePosition + 0x6000)) && */
1389                                 (pAd->DeQueueRunning[BulkOutPipeId] == FALSE) &&
1390                                 (pHTTXContext->bCurWriting == FALSE))
1391                         {
1392                                 RTMPDeQueuePacket(pAd, FALSE, BulkOutPipeId, MAX_TX_PROCESS);
1393                         }
1394
1395                         RTUSB_SET_BULK_FLAG(pAd, fRTUSB_BULK_OUT_DATA_NORMAL<<3);
1396                         RTUSBKickBulkOut(pAd);
1397                 }
1398         }
1399
1400
1401                 return;
1402 }
1403
1404
1405 static void rt2870_ac2_dma_done_tasklet(unsigned long data)
1406 {
1407         PRTMP_ADAPTER           pAd;
1408         PHT_TX_CONTEXT          pHTTXContext;
1409         UCHAR                           BulkOutPipeId = 2;
1410         purbb_t                         pUrb;
1411
1412
1413         pUrb                    = (purbb_t)data;
1414         pHTTXContext    = (PHT_TX_CONTEXT)pUrb->context;
1415         pAd                             = pHTTXContext->pAd;
1416
1417         rt2870_dataout_complete_tasklet((unsigned long)pUrb);
1418
1419         if ((RTMP_TEST_FLAG(pAd, (fRTMP_ADAPTER_RESET_IN_PROGRESS |
1420                                                                 fRTMP_ADAPTER_HALT_IN_PROGRESS |
1421                                                                 fRTMP_ADAPTER_NIC_NOT_EXIST))))
1422         {
1423                 // do nothing and return directly.
1424         }
1425         else
1426         {
1427                 if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_BULKOUT_RESET))
1428                 {
1429                         RTUSBEnqueueInternalCmd(pAd, CMDTHREAD_RESET_BULK_OUT, NULL, 0);
1430                 }
1431                 else
1432                 {       pHTTXContext = &pAd->TxContext[BulkOutPipeId];
1433                         if ((pAd->TxSwQueue[BulkOutPipeId].Number > 0) &&
1434                                 /*((pHTTXContext->CurWritePosition > (pHTTXContext->NextBulkOutPosition + 0x6000)) || (pHTTXContext->NextBulkOutPosition > pHTTXContext->CurWritePosition + 0x6000)) && */
1435                                 (pAd->DeQueueRunning[BulkOutPipeId] == FALSE) &&
1436                                 (pHTTXContext->bCurWriting == FALSE))
1437                         {
1438                                 RTMPDeQueuePacket(pAd, FALSE, BulkOutPipeId, MAX_TX_PROCESS);
1439                         }
1440
1441                         RTUSB_SET_BULK_FLAG(pAd, fRTUSB_BULK_OUT_DATA_NORMAL<<2);
1442                         RTUSBKickBulkOut(pAd);
1443                 }
1444         }
1445
1446                 return;
1447 }
1448
1449
1450 static void rt2870_ac1_dma_done_tasklet(unsigned long data)
1451 {
1452         PRTMP_ADAPTER           pAd;
1453         PHT_TX_CONTEXT          pHTTXContext;
1454         UCHAR                           BulkOutPipeId = 1;
1455         purbb_t                         pUrb;
1456
1457
1458         pUrb                    = (purbb_t)data;
1459         pHTTXContext    = (PHT_TX_CONTEXT)pUrb->context;
1460         pAd                             = pHTTXContext->pAd;
1461
1462         rt2870_dataout_complete_tasklet((unsigned long)pUrb);
1463
1464         if ((RTMP_TEST_FLAG(pAd, (fRTMP_ADAPTER_RESET_IN_PROGRESS |
1465                                                                 fRTMP_ADAPTER_HALT_IN_PROGRESS |
1466                                                                 fRTMP_ADAPTER_NIC_NOT_EXIST))))
1467         {
1468                 // do nothing and return directly.
1469         }
1470         else
1471         {
1472                 if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_BULKOUT_RESET))
1473                 {
1474                         RTUSBEnqueueInternalCmd(pAd, CMDTHREAD_RESET_BULK_OUT, NULL, 0);
1475                 }
1476                 else
1477                 {       pHTTXContext = &pAd->TxContext[BulkOutPipeId];
1478                         if ((pAd->TxSwQueue[BulkOutPipeId].Number > 0) &&
1479                                 /*((pHTTXContext->CurWritePosition > (pHTTXContext->NextBulkOutPosition + 0x6000)) || (pHTTXContext->NextBulkOutPosition > pHTTXContext->CurWritePosition + 0x6000)) && */
1480                                 (pAd->DeQueueRunning[BulkOutPipeId] == FALSE) &&
1481                                 (pHTTXContext->bCurWriting == FALSE))
1482                         {
1483                                 RTMPDeQueuePacket(pAd, FALSE, BulkOutPipeId, MAX_TX_PROCESS);
1484                         }
1485
1486                         RTUSB_SET_BULK_FLAG(pAd, fRTUSB_BULK_OUT_DATA_NORMAL<<1);
1487                         RTUSBKickBulkOut(pAd);
1488                 }
1489         }
1490
1491
1492         return;
1493 }
1494
1495
1496 static void rt2870_ac0_dma_done_tasklet(unsigned long data)
1497 {
1498         PRTMP_ADAPTER           pAd;
1499         PHT_TX_CONTEXT          pHTTXContext;
1500         UCHAR                           BulkOutPipeId = 0;
1501         purbb_t                         pUrb;
1502
1503
1504         pUrb                    = (purbb_t)data;
1505         pHTTXContext    = (PHT_TX_CONTEXT)pUrb->context;
1506         pAd                             = pHTTXContext->pAd;
1507
1508         rt2870_dataout_complete_tasklet((unsigned long)pUrb);
1509
1510         if ((RTMP_TEST_FLAG(pAd, (fRTMP_ADAPTER_RESET_IN_PROGRESS |
1511                                                                 fRTMP_ADAPTER_HALT_IN_PROGRESS |
1512                                                                 fRTMP_ADAPTER_NIC_NOT_EXIST))))
1513         {
1514                 // do nothing and return directly.
1515         }
1516         else
1517         {
1518                 if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_BULKOUT_RESET))
1519                 {
1520                         RTUSBEnqueueInternalCmd(pAd, CMDTHREAD_RESET_BULK_OUT, NULL, 0);
1521                 }
1522                 else
1523                 {       pHTTXContext = &pAd->TxContext[BulkOutPipeId];
1524                         if ((pAd->TxSwQueue[BulkOutPipeId].Number > 0) &&
1525                                 /*  ((pHTTXContext->CurWritePosition > (pHTTXContext->NextBulkOutPosition + 0x6000)) || (pHTTXContext->NextBulkOutPosition > pHTTXContext->CurWritePosition + 0x6000)) && */
1526                                 (pAd->DeQueueRunning[BulkOutPipeId] == FALSE) &&
1527                                 (pHTTXContext->bCurWriting == FALSE))
1528                         {
1529                                 RTMPDeQueuePacket(pAd, FALSE, BulkOutPipeId, MAX_TX_PROCESS);
1530                         }
1531
1532                         RTUSB_SET_BULK_FLAG(pAd, fRTUSB_BULK_OUT_DATA_NORMAL);
1533                         RTUSBKickBulkOut(pAd);
1534                 }
1535         }
1536
1537
1538         return;
1539
1540 }
1541
1542
1543 static void rt2870_null_frame_complete_tasklet(unsigned long data)
1544 {
1545         PRTMP_ADAPTER   pAd;
1546         PTX_CONTEXT             pNullContext;
1547         purbb_t                 pUrb;
1548         NTSTATUS                Status;
1549         unsigned long   irqFlag;
1550
1551
1552         pUrb                    = (purbb_t)data;
1553         pNullContext    = (PTX_CONTEXT)pUrb->context;
1554         pAd                     = pNullContext->pAd;
1555         Status                  = pUrb->status;
1556
1557         // Reset Null frame context flags
1558         RTMP_IRQ_LOCK(&pAd->BulkOutLock[0], irqFlag);
1559         pNullContext->IRPPending        = FALSE;
1560         pNullContext->InUse             = FALSE;
1561         pAd->BulkOutPending[0] = FALSE;
1562         pAd->watchDogTxPendingCnt[0] = 0;
1563
1564         if (Status == USB_ST_NOERROR)
1565         {
1566                 RTMP_IRQ_UNLOCK(&pAd->BulkOutLock[0], irqFlag);
1567
1568                 RTMPDeQueuePacket(pAd, FALSE, NUM_OF_TX_RING, MAX_TX_PROCESS);
1569         }
1570         else    // STATUS_OTHER
1571         {
1572                 if ((!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_RESET_IN_PROGRESS)) &&
1573                         (!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS)) &&
1574                         (!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST)) &&
1575                         (!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_BULKOUT_RESET)))
1576                 {
1577                         DBGPRINT_RAW(RT_DEBUG_ERROR, ("Bulk Out Null Frame Failed, ReasonCode=%d!\n", Status));
1578                         RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_BULKOUT_RESET);
1579                         pAd->bulkResetPipeid = (MGMTPIPEIDX | BULKOUT_MGMT_RESET_FLAG);
1580                         RTMP_IRQ_UNLOCK(&pAd->BulkOutLock[0], irqFlag);
1581                         RTUSBEnqueueInternalCmd(pAd, CMDTHREAD_RESET_BULK_OUT, NULL, 0);
1582                 }
1583                 else
1584                 {
1585                         RTMP_IRQ_UNLOCK(&pAd->BulkOutLock[0], irqFlag);
1586                 }
1587         }
1588
1589         // Always call Bulk routine, even reset bulk.
1590         // The protectioon of rest bulk should be in BulkOut routine
1591         RTUSBKickBulkOut(pAd);
1592
1593 }
1594
1595
1596 static void rt2870_rts_frame_complete_tasklet(unsigned long data)
1597 {
1598         PRTMP_ADAPTER   pAd;
1599         PTX_CONTEXT             pRTSContext;
1600         purbb_t                 pUrb;
1601         NTSTATUS                Status;
1602         unsigned long   irqFlag;
1603
1604
1605         pUrb            = (purbb_t)data;
1606         pRTSContext     = (PTX_CONTEXT)pUrb->context;
1607         pAd                     = pRTSContext->pAd;
1608         Status          = pUrb->status;
1609
1610         // Reset RTS frame context flags
1611         RTMP_IRQ_LOCK(&pAd->BulkOutLock[0], irqFlag);
1612         pRTSContext->IRPPending = FALSE;
1613         pRTSContext->InUse              = FALSE;
1614
1615         if (Status == USB_ST_NOERROR)
1616         {
1617                 RTMP_IRQ_UNLOCK(&pAd->BulkOutLock[0], irqFlag);
1618                 RTMPDeQueuePacket(pAd, FALSE, NUM_OF_TX_RING, MAX_TX_PROCESS);
1619         }
1620         else    // STATUS_OTHER
1621         {
1622                 if ((!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_RESET_IN_PROGRESS)) &&
1623                         (!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS)) &&
1624                         (!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST)) &&
1625                         (!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_BULKOUT_RESET)))
1626                 {
1627                         DBGPRINT_RAW(RT_DEBUG_ERROR, ("Bulk Out RTS Frame Failed\n"));
1628                         RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_BULKOUT_RESET);
1629                         pAd->bulkResetPipeid = (MGMTPIPEIDX | BULKOUT_MGMT_RESET_FLAG);
1630                         RTMP_IRQ_UNLOCK(&pAd->BulkOutLock[0], irqFlag);
1631                         RTUSBEnqueueInternalCmd(pAd, CMDTHREAD_RESET_BULK_OUT, NULL, 0);
1632                 }
1633                 else
1634                 {
1635                         RTMP_IRQ_UNLOCK(&pAd->BulkOutLock[0], irqFlag);
1636                 }
1637         }
1638
1639         RTMP_SEM_LOCK(&pAd->BulkOutLock[pRTSContext->BulkOutPipeId]);
1640         pAd->BulkOutPending[pRTSContext->BulkOutPipeId] = FALSE;
1641         RTMP_SEM_UNLOCK(&pAd->BulkOutLock[pRTSContext->BulkOutPipeId]);
1642
1643         // Always call Bulk routine, even reset bulk.
1644         // The protectioon of rest bulk should be in BulkOut routine
1645         RTUSBKickBulkOut(pAd);
1646
1647 }
1648
1649
1650 static void rt2870_pspoll_frame_complete_tasklet(unsigned long data)
1651 {
1652         PRTMP_ADAPTER   pAd;
1653         PTX_CONTEXT             pPsPollContext;
1654         purbb_t                 pUrb;
1655         NTSTATUS                Status;
1656
1657
1658         pUrb                    = (purbb_t)data;
1659         pPsPollContext  = (PTX_CONTEXT)pUrb->context;
1660         pAd                             = pPsPollContext->pAd;
1661         Status                  = pUrb->status;
1662
1663         // Reset PsPoll context flags
1664         pPsPollContext->IRPPending      = FALSE;
1665         pPsPollContext->InUse           = FALSE;
1666         pAd->watchDogTxPendingCnt[0] = 0;
1667
1668         if (Status == USB_ST_NOERROR)
1669         {
1670                 RTMPDeQueuePacket(pAd, FALSE, NUM_OF_TX_RING, MAX_TX_PROCESS);
1671         }
1672         else // STATUS_OTHER
1673         {
1674                 if ((!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_RESET_IN_PROGRESS)) &&
1675                         (!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS)) &&
1676                         (!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST)) &&
1677                         (!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_BULKOUT_RESET)))
1678                 {
1679                         DBGPRINT_RAW(RT_DEBUG_ERROR, ("Bulk Out PSPoll Failed\n"));
1680                         RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_BULKOUT_RESET);
1681                         pAd->bulkResetPipeid = (MGMTPIPEIDX | BULKOUT_MGMT_RESET_FLAG);
1682                         RTUSBEnqueueInternalCmd(pAd, CMDTHREAD_RESET_BULK_OUT, NULL, 0);
1683                 }
1684         }
1685
1686         RTMP_SEM_LOCK(&pAd->BulkOutLock[0]);
1687         pAd->BulkOutPending[0] = FALSE;
1688         RTMP_SEM_UNLOCK(&pAd->BulkOutLock[0]);
1689
1690         // Always call Bulk routine, even reset bulk.
1691         // The protectioon of rest bulk should be in BulkOut routine
1692         RTUSBKickBulkOut(pAd);
1693
1694 }
1695
1696
1697 static void rt2870_dataout_complete_tasklet(unsigned long data)
1698 {
1699         PRTMP_ADAPTER           pAd;
1700         purbb_t                         pUrb;
1701         POS_COOKIE                      pObj;
1702         PHT_TX_CONTEXT          pHTTXContext;
1703         UCHAR                           BulkOutPipeId;
1704         NTSTATUS                        Status;
1705         unsigned long           IrqFlags;
1706
1707
1708         pUrb                    = (purbb_t)data;
1709         pHTTXContext    = (PHT_TX_CONTEXT)pUrb->context;
1710         pAd                             = pHTTXContext->pAd;
1711         pObj                    = (POS_COOKIE) pAd->OS_Cookie;
1712         Status                  = pUrb->status;
1713
1714         // Store BulkOut PipeId
1715         BulkOutPipeId = pHTTXContext->BulkOutPipeId;
1716         pAd->BulkOutDataOneSecCount++;
1717
1718         //DBGPRINT(RT_DEBUG_LOUD, ("Done-B(%d):I=0x%lx, CWPos=%ld, NBPos=%ld, ENBPos=%ld, bCopy=%d!\n", BulkOutPipeId, in_interrupt(), pHTTXContext->CurWritePosition,
1719         //              pHTTXContext->NextBulkOutPosition, pHTTXContext->ENextBulkOutPosition, pHTTXContext->bCopySavePad));
1720
1721         RTMP_IRQ_LOCK(&pAd->BulkOutLock[BulkOutPipeId], IrqFlags);
1722         pAd->BulkOutPending[BulkOutPipeId] = FALSE;
1723         pHTTXContext->IRPPending = FALSE;
1724         pAd->watchDogTxPendingCnt[BulkOutPipeId] = 0;
1725
1726         if (Status == USB_ST_NOERROR)
1727         {
1728                 pAd->BulkOutComplete++;
1729
1730                 RTMP_IRQ_UNLOCK(&pAd->BulkOutLock[BulkOutPipeId], IrqFlags);
1731
1732                 pAd->Counters8023.GoodTransmits++;
1733                 //RTMP_IRQ_LOCK(&pAd->TxContextQueueLock[BulkOutPipeId], IrqFlags);
1734                 FREE_HTTX_RING(pAd, BulkOutPipeId, pHTTXContext);
1735                 //RTMP_IRQ_UNLOCK(&pAd->TxContextQueueLock[BulkOutPipeId], IrqFlags);
1736
1737
1738         }
1739         else    // STATUS_OTHER
1740         {
1741                 PUCHAR  pBuf;
1742
1743                 pAd->BulkOutCompleteOther++;
1744
1745                 pBuf = &pHTTXContext->TransferBuffer->field.WirelessPacket[pHTTXContext->NextBulkOutPosition];
1746
1747                 if (!RTMP_TEST_FLAG(pAd, (fRTMP_ADAPTER_RESET_IN_PROGRESS |
1748                                                                         fRTMP_ADAPTER_HALT_IN_PROGRESS |
1749                                                                         fRTMP_ADAPTER_NIC_NOT_EXIST |
1750                                                                         fRTMP_ADAPTER_BULKOUT_RESET)))
1751                 {
1752                         RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_BULKOUT_RESET);
1753                         pAd->bulkResetPipeid = BulkOutPipeId;
1754                         pAd->bulkResetReq[BulkOutPipeId] = pAd->BulkOutReq;
1755                 }
1756                 RTMP_IRQ_UNLOCK(&pAd->BulkOutLock[BulkOutPipeId], IrqFlags);
1757
1758                 DBGPRINT_RAW(RT_DEBUG_ERROR, ("BulkOutDataPacket failed: ReasonCode=%d!\n", Status));
1759                 DBGPRINT_RAW(RT_DEBUG_ERROR, ("\t>>BulkOut Req=0x%lx, Complete=0x%lx, Other=0x%lx\n", pAd->BulkOutReq, pAd->BulkOutComplete, pAd->BulkOutCompleteOther));
1760                 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]));
1761                 //DBGPRINT_RAW(RT_DEBUG_ERROR, (">>BulkOutCompleteCancel=0x%x, BulkOutCompleteOther=0x%x\n", pAd->BulkOutCompleteCancel, pAd->BulkOutCompleteOther));
1762
1763         }
1764
1765         //
1766         // bInUse = TRUE, means some process are filling TX data, after that must turn on bWaitingBulkOut
1767         // bWaitingBulkOut = TRUE, means the TX data are waiting for bulk out.
1768         //
1769         //RTMP_IRQ_LOCK(&pAd->TxContextQueueLock[BulkOutPipeId], IrqFlags);
1770         if ((pHTTXContext->ENextBulkOutPosition != pHTTXContext->CurWritePosition) &&
1771                 (pHTTXContext->ENextBulkOutPosition != (pHTTXContext->CurWritePosition+8)) &&
1772                 !RTUSB_TEST_BULK_FLAG(pAd, (fRTUSB_BULK_OUT_DATA_FRAG << BulkOutPipeId)))
1773         {
1774                 // Indicate There is data avaliable
1775                 RTUSB_SET_BULK_FLAG(pAd, (fRTUSB_BULK_OUT_DATA_NORMAL << BulkOutPipeId));
1776         }
1777         //RTMP_IRQ_UNLOCK(&pAd->TxContextQueueLock[BulkOutPipeId], IrqFlags);
1778
1779         // Always call Bulk routine, even reset bulk.
1780         // The protection of rest bulk should be in BulkOut routine
1781         RTUSBKickBulkOut(pAd);
1782 }
1783
1784 /* End of 2870_rtmp_init.c */