dd01c64fbf61fd09432cc6d0452213239d4801eb
[linux-2.6.git] / drivers / staging / rt2870 / 2870_main_dev.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     rtmp_main.c
29
30     Abstract:
31     main initialization routines
32
33     Revision History:
34     Who         When            What
35     --------    ----------      ----------------------------------------------
36     Name        Date            Modification logs
37     Jan Lee             01-10-2005          modified
38         Sample          Jun/01/07               Merge RT2870 and RT2860 drivers.
39 */
40
41 #include "rt_config.h"
42
43
44 // Following information will be show when you run 'modinfo'
45 // *** If you have a solution for the bug in current version of driver, please mail to me.
46 // Otherwise post to forum in ralinktech's web site(www.ralinktech.com) and let all users help you. ***
47 MODULE_AUTHOR("Paul Lin <paul_lin@ralinktech.com>");
48 MODULE_DESCRIPTION("RT2870 Wireless Lan Linux Driver");
49 MODULE_LICENSE("GPL");
50 #ifdef MODULE_VERSION
51 MODULE_VERSION(STA_DRIVER_VERSION);
52 #endif
53
54 /* Kernel thread and vars, which handles packets that are completed. Only
55  * packets that have a "complete" function are sent here. This way, the
56  * completion is run out of kernel context, and doesn't block the rest of
57  * the stack. */
58
59 extern INT __devinit rt28xx_probe(IN void *_dev_p, IN void *_dev_id_p,
60                                                                         IN UINT argc, OUT PRTMP_ADAPTER *ppAd);
61
62
63 /* module table */
64 struct usb_device_id    rtusb_usb_id[] = RT2870_USB_DEVICES;
65 INT const               rtusb_usb_id_len = sizeof(rtusb_usb_id) / sizeof(struct usb_device_id);
66 MODULE_DEVICE_TABLE(usb, rtusb_usb_id);
67
68 #ifndef PF_NOFREEZE
69 #define PF_NOFREEZE  0
70 #endif
71
72
73 #ifdef CONFIG_PM
74 static int rt2870_suspend(struct usb_interface *intf, pm_message_t state);
75 static int rt2870_resume(struct usb_interface *intf);
76 #endif // CONFIG_PM //
77
78 /**************************************************************************/
79 /**************************************************************************/
80 //tested for kernel 2.6series
81 /**************************************************************************/
82 /**************************************************************************/
83 static int rtusb_probe (struct usb_interface *intf,
84                                                 const struct usb_device_id *id);
85 static void rtusb_disconnect(struct usb_interface *intf);
86
87 struct usb_driver rtusb_driver = {
88         .name="rt2870",
89         .probe=rtusb_probe,
90         .disconnect=rtusb_disconnect,
91         .id_table=rtusb_usb_id,
92
93 #ifdef CONFIG_PM
94         suspend:        rt2870_suspend,
95         resume:         rt2870_resume,
96 #endif
97         };
98
99 #ifdef CONFIG_PM
100
101 VOID RT2860RejectPendingPackets(
102         IN      PRTMP_ADAPTER   pAd)
103 {
104         // clear PS packets
105         // clear TxSw packets
106 }
107
108 static int rt2870_suspend(
109         struct usb_interface *intf,
110         pm_message_t state)
111 {
112         struct net_device *net_dev;
113         PRTMP_ADAPTER pAd = usb_get_intfdata(intf);
114
115
116         DBGPRINT(RT_DEBUG_TRACE, ("===> rt2870_suspend()\n"));
117         net_dev = pAd->net_dev;
118         netif_device_detach (net_dev);
119
120         pAd->PM_FlgSuspend = 1;
121         if (netif_running(net_dev)) {
122                 RTUSBCancelPendingBulkInIRP(pAd);
123                 RTUSBCancelPendingBulkOutIRP(pAd);
124         }
125         DBGPRINT(RT_DEBUG_TRACE, ("<=== rt2870_suspend()\n"));
126         return 0;
127 }
128
129 static int rt2870_resume(
130         struct usb_interface *intf)
131 {
132         struct net_device *net_dev;
133         PRTMP_ADAPTER pAd = usb_get_intfdata(intf);
134
135
136         DBGPRINT(RT_DEBUG_TRACE, ("===> rt2870_resume()\n"));
137
138         pAd->PM_FlgSuspend = 0;
139         net_dev = pAd->net_dev;
140         netif_device_attach (net_dev);
141         netif_start_queue(net_dev);
142         netif_carrier_on(net_dev);
143         netif_wake_queue(net_dev);
144
145         DBGPRINT(RT_DEBUG_TRACE, ("<=== rt2870_resume()\n"));
146         return 0;
147 }
148 #endif // CONFIG_PM //
149
150
151 // Init driver module
152 INT __init rtusb_init(void)
153 {
154         printk("rtusb init --->\n");
155         return usb_register(&rtusb_driver);
156 }
157
158 // Deinit driver module
159 VOID __exit rtusb_exit(void)
160 {
161         usb_deregister(&rtusb_driver);
162         printk("<--- rtusb exit\n");
163 }
164
165 module_init(rtusb_init);
166 module_exit(rtusb_exit);
167
168
169
170
171 /*--------------------------------------------------------------------- */
172 /* function declarations                                                                                                */
173 /*--------------------------------------------------------------------- */
174
175 /*
176 ========================================================================
177 Routine Description:
178     MLME kernel thread.
179
180 Arguments:
181         *Context                        the pAd, driver control block pointer
182
183 Return Value:
184     0                                   close the thread
185
186 Note:
187 ========================================================================
188 */
189 INT MlmeThread(
190         IN void *Context)
191 {
192         PRTMP_ADAPTER   pAd = (PRTMP_ADAPTER)Context;
193         POS_COOKIE      pObj;
194         int status;
195
196         pObj = (POS_COOKIE)pAd->OS_Cookie;
197
198         rtmp_os_thread_init("rt2870MlmeThread", (PVOID)&(pAd->mlmeComplete));
199
200         while (pAd->mlme_kill == 0)
201         {
202                 /* lock the device pointers */
203                 //down(&(pAd->mlme_semaphore));
204                 status = down_interruptible(&(pAd->mlme_semaphore));
205
206                 /* lock the device pointers , need to check if required*/
207                 //down(&(pAd->usbdev_semaphore));
208
209                 if (!pAd->PM_FlgSuspend)
210                 MlmeHandler(pAd);
211
212                 /* unlock the device pointers */
213                 //up(&(pAd->usbdev_semaphore));
214                 if (status != 0)
215                 {
216                         RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS);
217                         break;
218                 }
219         }
220
221         /* notify the exit routine that we're actually exiting now
222          *
223          * complete()/wait_for_completion() is similar to up()/down(),
224          * except that complete() is safe in the case where the structure
225          * is getting deleted in a parallel mode of execution (i.e. just
226          * after the down() -- that's necessary for the thread-shutdown
227          * case.
228          *
229          * complete_and_exit() goes even further than this -- it is safe in
230          * the case that the thread of the caller is going away (not just
231          * the structure) -- this is necessary for the module-remove case.
232          * This is important in preemption kernels, which transfer the flow
233          * of execution immediately upon a complete().
234          */
235         DBGPRINT(RT_DEBUG_TRACE,( "<---%s\n",__func__));
236
237 #ifndef RT30xx
238         pObj->MLMEThr_task = NULL;
239 #endif
240 #ifdef RT30xx
241         pObj->MLMEThr_pid = NULL;
242 #endif
243
244         complete_and_exit (&pAd->mlmeComplete, 0);
245         return 0;
246
247 }
248
249
250 /*
251 ========================================================================
252 Routine Description:
253     USB command kernel thread.
254
255 Arguments:
256         *Context                        the pAd, driver control block pointer
257
258 Return Value:
259     0                                   close the thread
260
261 Note:
262 ========================================================================
263 */
264 INT RTUSBCmdThread(
265         IN void * Context)
266 {
267         PRTMP_ADAPTER   pAd = (PRTMP_ADAPTER)Context;
268         POS_COOKIE              pObj;
269         int status;
270
271         pObj = (POS_COOKIE)pAd->OS_Cookie;
272
273         rtmp_os_thread_init("rt2870CmdThread", (PVOID)&(pAd->CmdQComplete));
274
275         NdisAcquireSpinLock(&pAd->CmdQLock);
276         pAd->CmdQ.CmdQState = RT2870_THREAD_RUNNING;
277         NdisReleaseSpinLock(&pAd->CmdQLock);
278
279         while (pAd->CmdQ.CmdQState == RT2870_THREAD_RUNNING)
280         {
281                 /* lock the device pointers */
282                 //down(&(pAd->RTUSBCmd_semaphore));
283                 status = down_interruptible(&(pAd->RTUSBCmd_semaphore));
284
285                 if (pAd->CmdQ.CmdQState == RT2870_THREAD_STOPED)
286                         break;
287
288                 if (status != 0)
289                 {
290                         RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS);
291                         break;
292                 }
293                 /* lock the device pointers , need to check if required*/
294                 //down(&(pAd->usbdev_semaphore));
295
296                 if (!pAd->PM_FlgSuspend)
297                 CMDHandler(pAd);
298
299                 /* unlock the device pointers */
300                 //up(&(pAd->usbdev_semaphore));
301         }
302
303         if (!pAd->PM_FlgSuspend)
304         {       // Clear the CmdQElements.
305                 CmdQElmt        *pCmdQElmt = NULL;
306
307                 NdisAcquireSpinLock(&pAd->CmdQLock);
308                 pAd->CmdQ.CmdQState = RT2870_THREAD_STOPED;
309                 while(pAd->CmdQ.size)
310                 {
311                         RTUSBDequeueCmd(&pAd->CmdQ, &pCmdQElmt);
312                         if (pCmdQElmt)
313                         {
314                                 if (pCmdQElmt->CmdFromNdis == TRUE)
315                                 {
316                                         if (pCmdQElmt->buffer != NULL)
317                                                 NdisFreeMemory(pCmdQElmt->buffer, pCmdQElmt->bufferlength, 0);
318
319                                         NdisFreeMemory(pCmdQElmt, sizeof(CmdQElmt), 0);
320                                 }
321                                 else
322                                 {
323                                         if ((pCmdQElmt->buffer != NULL) && (pCmdQElmt->bufferlength != 0))
324                                                 NdisFreeMemory(pCmdQElmt->buffer, pCmdQElmt->bufferlength, 0);
325                             {
326                                                 NdisFreeMemory(pCmdQElmt, sizeof(CmdQElmt), 0);
327                                         }
328                                 }
329                         }
330                 }
331
332                 NdisReleaseSpinLock(&pAd->CmdQLock);
333         }
334         /* notify the exit routine that we're actually exiting now
335          *
336          * complete()/wait_for_completion() is similar to up()/down(),
337          * except that complete() is safe in the case where the structure
338          * is getting deleted in a parallel mode of execution (i.e. just
339          * after the down() -- that's necessary for the thread-shutdown
340          * case.
341          *
342          * complete_and_exit() goes even further than this -- it is safe in
343          * the case that the thread of the caller is going away (not just
344          * the structure) -- this is necessary for the module-remove case.
345          * This is important in preemption kernels, which transfer the flow
346          * of execution immediately upon a complete().
347          */
348         DBGPRINT(RT_DEBUG_TRACE,( "<---RTUSBCmdThread\n"));
349
350 #ifndef RT30xx
351         pObj->RTUSBCmdThr_task = NULL;
352 #endif
353 #ifdef RT30xx
354         pObj->RTUSBCmdThr_pid = NULL;
355 #endif
356
357         complete_and_exit (&pAd->CmdQComplete, 0);
358         return 0;
359
360 }
361
362
363 static void RT2870_TimerQ_Handle(RTMP_ADAPTER *pAd)
364 {
365         int status;
366         RALINK_TIMER_STRUCT     *pTimer;
367         RT2870_TIMER_ENTRY      *pEntry;
368         unsigned long   irqFlag;
369
370         while(!pAd->TimerFunc_kill)
371         {
372 //              printk("waiting for event!\n");
373                 pTimer = NULL;
374
375                 status = down_interruptible(&(pAd->RTUSBTimer_semaphore));
376
377                 if (pAd->TimerQ.status == RT2870_THREAD_STOPED)
378                         break;
379
380                 // event happened.
381                 while(pAd->TimerQ.pQHead)
382                 {
383                         RTMP_IRQ_LOCK(&pAd->TimerQLock, irqFlag);
384                         pEntry = pAd->TimerQ.pQHead;
385                         if (pEntry)
386                         {
387                                 pTimer = pEntry->pRaTimer;
388
389                                 // update pQHead
390                                 pAd->TimerQ.pQHead = pEntry->pNext;
391                                 if (pEntry == pAd->TimerQ.pQTail)
392                                         pAd->TimerQ.pQTail = NULL;
393
394                                 // return this queue entry to timerQFreeList.
395                                 pEntry->pNext = pAd->TimerQ.pQPollFreeList;
396                                 pAd->TimerQ.pQPollFreeList = pEntry;
397                         }
398                         RTMP_IRQ_UNLOCK(&pAd->TimerQLock, irqFlag);
399
400                         if (pTimer)
401                         {
402                                 if (pTimer->handle != NULL)
403                                 if (!pAd->PM_FlgSuspend)
404                                         pTimer->handle(NULL, (PVOID) pTimer->cookie, NULL, pTimer);
405                                 if ((pTimer->Repeat) && (pTimer->State == FALSE))
406                                         RTMP_OS_Add_Timer(&pTimer->TimerObj, pTimer->TimerValue);
407                         }
408                 }
409
410                 if (status != 0)
411                 {
412                         pAd->TimerQ.status = RT2870_THREAD_STOPED;
413                         RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS);
414                         break;
415                 }
416         }
417 }
418
419
420 INT TimerQThread(
421         IN OUT PVOID Context)
422 {
423         PRTMP_ADAPTER   pAd;
424         POS_COOKIE      pObj;
425
426         pAd = (PRTMP_ADAPTER)Context;
427         pObj = (POS_COOKIE) pAd->OS_Cookie;
428
429         rtmp_os_thread_init("rt2870TimerQHandle", (PVOID)&(pAd->TimerQComplete));
430
431         RT2870_TimerQ_Handle(pAd);
432
433         /* notify the exit routine that we're actually exiting now
434          *
435          * complete()/wait_for_completion() is similar to up()/down(),
436          * except that complete() is safe in the case where the structure
437          * is getting deleted in a parallel mode of execution (i.e. just
438          * after the down() -- that's necessary for the thread-shutdown
439          * case.
440          *
441          * complete_and_exit() goes even further than this -- it is safe in
442          * the case that the thread of the caller is going away (not just
443          * the structure) -- this is necessary for the module-remove case.
444          * This is important in preemption kernels, which transfer the flow
445          * of execution immediately upon a complete().
446          */
447         DBGPRINT(RT_DEBUG_TRACE,( "<---%s\n",__func__));
448
449 #ifndef RT30xx
450         pObj->TimerQThr_task = NULL;
451 #endif
452 #ifdef RT30xx
453         pObj->TimerQThr_pid = NULL;
454 #endif
455         complete_and_exit(&pAd->TimerQComplete, 0);
456         return 0;
457
458 }
459
460
461 RT2870_TIMER_ENTRY *RT2870_TimerQ_Insert(
462         IN RTMP_ADAPTER *pAd,
463         IN RALINK_TIMER_STRUCT *pTimer)
464 {
465         RT2870_TIMER_ENTRY *pQNode = NULL, *pQTail;
466         unsigned long irqFlags;
467
468
469         RTMP_IRQ_LOCK(&pAd->TimerQLock, irqFlags);
470         if (pAd->TimerQ.status & RT2870_THREAD_CAN_DO_INSERT)
471         {
472                 if(pAd->TimerQ.pQPollFreeList)
473                 {
474                         pQNode = pAd->TimerQ.pQPollFreeList;
475                         pAd->TimerQ.pQPollFreeList = pQNode->pNext;
476
477                         pQNode->pRaTimer = pTimer;
478                         pQNode->pNext = NULL;
479
480                         pQTail = pAd->TimerQ.pQTail;
481                         if (pAd->TimerQ.pQTail != NULL)
482                                 pQTail->pNext = pQNode;
483                         pAd->TimerQ.pQTail = pQNode;
484                         if (pAd->TimerQ.pQHead == NULL)
485                                 pAd->TimerQ.pQHead = pQNode;
486                 }
487                 RTMP_IRQ_UNLOCK(&pAd->TimerQLock, irqFlags);
488
489                 if (pQNode)
490                         up(&pAd->RTUSBTimer_semaphore);
491                         //wake_up(&timerWaitQ);
492         }
493         else
494         {
495                 RTMP_IRQ_UNLOCK(&pAd->TimerQLock, irqFlags);
496         }
497         return pQNode;
498 }
499
500
501 BOOLEAN RT2870_TimerQ_Remove(
502         IN RTMP_ADAPTER *pAd,
503         IN RALINK_TIMER_STRUCT *pTimer)
504 {
505         RT2870_TIMER_ENTRY *pNode, *pPrev = NULL;
506         unsigned long irqFlags;
507
508         RTMP_IRQ_LOCK(&pAd->TimerQLock, irqFlags);
509         if (pAd->TimerQ.status >= RT2870_THREAD_INITED)
510         {
511                 pNode = pAd->TimerQ.pQHead;
512                 while (pNode)
513                 {
514                         if (pNode->pRaTimer == pTimer)
515                                 break;
516                         pPrev = pNode;
517                         pNode = pNode->pNext;
518                 }
519
520                 // Now move it to freeList queue.
521                 if (pNode)
522                 {
523                         if (pNode == pAd->TimerQ.pQHead)
524                                 pAd->TimerQ.pQHead = pNode->pNext;
525                         if (pNode == pAd->TimerQ.pQTail)
526                                 pAd->TimerQ.pQTail = pPrev;
527                         if (pPrev != NULL)
528                                 pPrev->pNext = pNode->pNext;
529
530                         // return this queue entry to timerQFreeList.
531                         pNode->pNext = pAd->TimerQ.pQPollFreeList;
532                         pAd->TimerQ.pQPollFreeList = pNode;
533                 }
534         }
535         RTMP_IRQ_UNLOCK(&pAd->TimerQLock, irqFlags);
536
537         return TRUE;
538 }
539
540
541 void RT2870_TimerQ_Exit(RTMP_ADAPTER *pAd)
542 {
543         RT2870_TIMER_ENTRY *pTimerQ;
544         unsigned long irqFlags;
545
546         RTMP_IRQ_LOCK(&pAd->TimerQLock, irqFlags);
547         while (pAd->TimerQ.pQHead)
548         {
549                 pTimerQ = pAd->TimerQ.pQHead;
550                 pAd->TimerQ.pQHead = pTimerQ->pNext;
551                 // remove the timeQ
552         }
553         pAd->TimerQ.pQPollFreeList = NULL;
554         os_free_mem(pAd, pAd->TimerQ.pTimerQPoll);
555         pAd->TimerQ.pQTail = NULL;
556         pAd->TimerQ.pQHead = NULL;
557         pAd->TimerQ.status = RT2870_THREAD_STOPED;
558         RTMP_IRQ_UNLOCK(&pAd->TimerQLock, irqFlags);
559
560 }
561
562
563 void RT2870_TimerQ_Init(RTMP_ADAPTER *pAd)
564 {
565         int     i;
566         RT2870_TIMER_ENTRY *pQNode, *pEntry;
567         unsigned long irqFlags;
568
569         NdisAllocateSpinLock(&pAd->TimerQLock);
570
571         RTMP_IRQ_LOCK(&pAd->TimerQLock, irqFlags);
572         NdisZeroMemory(&pAd->TimerQ, sizeof(pAd->TimerQ));
573         //InterlockedExchange(&pAd->TimerQ.count, 0);
574
575         /* Initialise the wait q head */
576         //init_waitqueue_head(&timerWaitQ);
577
578         os_alloc_mem(pAd, &pAd->TimerQ.pTimerQPoll, sizeof(RT2870_TIMER_ENTRY) * TIMER_QUEUE_SIZE_MAX);
579         if (pAd->TimerQ.pTimerQPoll)
580         {
581                 pEntry = NULL;
582                 pQNode = (RT2870_TIMER_ENTRY *)pAd->TimerQ.pTimerQPoll;
583                 for (i = 0 ;i <TIMER_QUEUE_SIZE_MAX; i++)
584                 {
585                         pQNode->pNext = pEntry;
586                         pEntry = pQNode;
587                         pQNode++;
588                 }
589                 pAd->TimerQ.pQPollFreeList = pEntry;
590                 pAd->TimerQ.pQHead = NULL;
591                 pAd->TimerQ.pQTail = NULL;
592                 pAd->TimerQ.status = RT2870_THREAD_INITED;
593         }
594         RTMP_IRQ_UNLOCK(&pAd->TimerQLock, irqFlags);
595 }
596
597
598 VOID RT2870_WatchDog(IN RTMP_ADAPTER *pAd)
599 {
600         PHT_TX_CONTEXT          pHTTXContext;
601         int                                     idx;
602         ULONG                           irqFlags;
603         PURB                            pUrb;
604         BOOLEAN                         needDumpSeq = FALSE;
605         UINT32                  MACValue;
606
607
608         idx = 0;
609         RTMP_IO_READ32(pAd, TXRXQ_PCNT, &MACValue);
610         if ((MACValue & 0xff) !=0 )
611         {
612                 DBGPRINT(RT_DEBUG_TRACE, ("TX QUEUE 0 Not EMPTY(Value=0x%0x). !!!!!!!!!!!!!!!\n", MACValue));
613                 RTMP_IO_WRITE32(pAd, PBF_CFG, 0xf40012);
614                 while((MACValue &0xff) != 0 && (idx++ < 10))
615                 {
616                         RTMP_IO_READ32(pAd, TXRXQ_PCNT, &MACValue);
617                         NdisMSleep(1);
618                 }
619                 RTMP_IO_WRITE32(pAd, PBF_CFG, 0xf40006);
620         }
621
622 //PS packets use HCCA queue when dequeue from PS unicast queue (WiFi WPA2 MA9_DT1 for Marvell B STA)
623         idx = 0;
624         if ((MACValue & 0xff00) !=0 )
625         {
626                 DBGPRINT(RT_DEBUG_TRACE, ("TX QUEUE 1 Not EMPTY(Value=0x%0x). !!!!!!!!!!!!!!!\n", MACValue));
627                 RTMP_IO_WRITE32(pAd, PBF_CFG, 0xf4000a);
628                 while((MACValue &0xff00) != 0 && (idx++ < 10))
629                 {
630                         RTMP_IO_READ32(pAd, TXRXQ_PCNT, &MACValue);
631                         NdisMSleep(1);
632                 }
633                 RTMP_IO_WRITE32(pAd, PBF_CFG, 0xf40006);
634         }
635
636         if (pAd->watchDogRxOverFlowCnt >= 2)
637         {
638                 DBGPRINT(RT_DEBUG_TRACE, ("Maybe the Rx Bulk-In hanged! Cancel the pending Rx bulks request!\n"));
639                 if ((!RTMP_TEST_FLAG(pAd, (fRTMP_ADAPTER_RESET_IN_PROGRESS |
640                                                                         fRTMP_ADAPTER_BULKIN_RESET |
641                                                                         fRTMP_ADAPTER_HALT_IN_PROGRESS |
642                                                                         fRTMP_ADAPTER_NIC_NOT_EXIST))))
643                 {
644                         DBGPRINT(RT_DEBUG_TRACE, ("Call CMDTHREAD_RESET_BULK_IN to cancel the pending Rx Bulk!\n"));
645                         RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_BULKIN_RESET);
646                         RTUSBEnqueueInternalCmd(pAd, CMDTHREAD_RESET_BULK_IN, NULL, 0);
647                         needDumpSeq = TRUE;
648                 }
649                 pAd->watchDogRxOverFlowCnt = 0;
650         }
651
652
653         for (idx = 0; idx < NUM_OF_TX_RING; idx++)
654         {
655                 pUrb = NULL;
656
657                 RTMP_IRQ_LOCK(&pAd->BulkOutLock[idx], irqFlags);
658                 if ((pAd->BulkOutPending[idx] == TRUE) && pAd->watchDogTxPendingCnt)
659                 {
660                         pAd->watchDogTxPendingCnt[idx]++;
661
662                         if ((pAd->watchDogTxPendingCnt[idx] > 2) &&
663                                  (!RTMP_TEST_FLAG(pAd, (fRTMP_ADAPTER_RESET_IN_PROGRESS | fRTMP_ADAPTER_HALT_IN_PROGRESS | fRTMP_ADAPTER_NIC_NOT_EXIST | fRTMP_ADAPTER_BULKOUT_RESET)))
664                                 )
665                         {
666                                 // FIXME: Following code just support single bulk out. If you wanna support multiple bulk out. Modify it!
667                                 pHTTXContext = (PHT_TX_CONTEXT)(&pAd->TxContext[idx]);
668                                 if (pHTTXContext->IRPPending)
669                                 {       // Check TxContext.
670                                         pUrb = pHTTXContext->pUrb;
671                                 }
672                                 else if (idx == MGMTPIPEIDX)
673                                 {
674                                         PTX_CONTEXT pMLMEContext, pNULLContext, pPsPollContext;
675
676                                         //Check MgmtContext.
677                                         pMLMEContext = (PTX_CONTEXT)(pAd->MgmtRing.Cell[pAd->MgmtRing.TxDmaIdx].AllocVa);
678                                         pPsPollContext = (PTX_CONTEXT)(&pAd->PsPollContext);
679                                         pNULLContext = (PTX_CONTEXT)(&pAd->NullContext);
680
681                                         if (pMLMEContext->IRPPending)
682                                         {
683                                                 ASSERT(pMLMEContext->IRPPending);
684                                                 pUrb = pMLMEContext->pUrb;
685                                         }
686                                         else if (pNULLContext->IRPPending)
687                                         {
688                                                 ASSERT(pNULLContext->IRPPending);
689                                                 pUrb = pNULLContext->pUrb;
690                                         }
691                                         else if (pPsPollContext->IRPPending)
692                                         {
693                                                 ASSERT(pPsPollContext->IRPPending);
694                                                 pUrb = pPsPollContext->pUrb;
695                                         }
696                                 }
697
698                                 RTMP_IRQ_UNLOCK(&pAd->BulkOutLock[idx], irqFlags);
699
700                                 DBGPRINT(RT_DEBUG_TRACE, ("Maybe the Tx Bulk-Out hanged! Cancel the pending Tx bulks request of idx(%d)!\n", idx));
701                                 if (pUrb)
702                                 {
703                                         DBGPRINT(RT_DEBUG_TRACE, ("Unlink the pending URB!\n"));
704                                         // unlink it now
705                                         RTUSB_UNLINK_URB(pUrb);
706                                         // Sleep 200 microseconds to give cancellation time to work
707                                         RTMPusecDelay(200);
708                                         needDumpSeq = TRUE;
709                                 }
710                                 else
711                                 {
712                                         DBGPRINT(RT_DEBUG_ERROR, ("Unkonw bulkOut URB maybe hanged!!!!!!!!!!!!\n"));
713                                 }
714                         }
715                         else
716                         {
717                                 RTMP_IRQ_UNLOCK(&pAd->BulkOutLock[idx], irqFlags);
718                         }
719                 }
720                 else
721                 {
722                         RTMP_IRQ_UNLOCK(&pAd->BulkOutLock[idx], irqFlags);
723                 }
724         }
725
726         // For Sigma debug, dump the ba_reordering sequence.
727         if((needDumpSeq == TRUE) && (pAd->CommonCfg.bDisableReordering == 0))
728         {
729                 USHORT                          Idx;
730                 PBA_REC_ENTRY           pBAEntry = NULL;
731                 UCHAR                           count = 0;
732                 struct reordering_mpdu *mpdu_blk;
733
734                 Idx = pAd->MacTab.Content[BSSID_WCID].BARecWcidArray[0];
735
736                 pBAEntry = &pAd->BATable.BARecEntry[Idx];
737                 if((pBAEntry->list.qlen > 0) && (pBAEntry->list.next != NULL))
738                 {
739                         DBGPRINT(RT_DEBUG_TRACE, ("NICUpdateRawCounters():The Queueing pkt in reordering buffer:\n"));
740                         NdisAcquireSpinLock(&pBAEntry->RxReRingLock);
741                         mpdu_blk = pBAEntry->list.next;
742                         while (mpdu_blk)
743                         {
744                                 DBGPRINT(RT_DEBUG_TRACE, ("\t%d:Seq-%d, bAMSDU-%d!\n", count, mpdu_blk->Sequence, mpdu_blk->bAMSDU));
745                                 mpdu_blk = mpdu_blk->next;
746                                 count++;
747                         }
748
749                         DBGPRINT(RT_DEBUG_TRACE, ("\npBAEntry->LastIndSeq=%d!\n", pBAEntry->LastIndSeq));
750                         NdisReleaseSpinLock(&pBAEntry->RxReRingLock);
751                 }
752         }
753 }
754
755 /*
756 ========================================================================
757 Routine Description:
758     Release allocated resources.
759
760 Arguments:
761     *dev                                Point to the PCI or USB device
762         pAd                                     driver control block pointer
763
764 Return Value:
765     None
766
767 Note:
768 ========================================================================
769 */
770 static void _rtusb_disconnect(struct usb_device *dev, PRTMP_ADAPTER pAd)
771 {
772         struct net_device       *net_dev = NULL;
773
774
775         DBGPRINT(RT_DEBUG_ERROR, ("rtusb_disconnect: unregister usbnet usb-%s-%s\n",
776                                 dev->bus->bus_name, dev->devpath));
777         if (!pAd)
778         {
779                 usb_put_dev(dev);
780
781                 printk("rtusb_disconnect: pAd == NULL!\n");
782                 return;
783         }
784         RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST);
785
786
787
788         // for debug, wait to show some messages to /proc system
789         udelay(1);
790
791
792
793
794         net_dev = pAd->net_dev;
795         if (pAd->net_dev != NULL)
796         {
797                 printk("rtusb_disconnect: unregister_netdev(), dev->name=%s!\n", net_dev->name);
798                 unregister_netdev (pAd->net_dev);
799         }
800         udelay(1);
801         flush_scheduled_work();
802         udelay(1);
803
804         // free net_device memory
805         free_netdev(net_dev);
806
807         // free adapter memory
808         RTMPFreeAdapter(pAd);
809
810         // release a use of the usb device structure
811         usb_put_dev(dev);
812         udelay(1);
813
814         DBGPRINT(RT_DEBUG_ERROR, (" RTUSB disconnect successfully\n"));
815 }
816
817
818 /*
819 ========================================================================
820 Routine Description:
821     Probe RT28XX chipset.
822
823 Arguments:
824     *dev                                Point to the PCI or USB device
825         interface
826         *id_table                       Point to the PCI or USB device ID
827
828 Return Value:
829     None
830
831 Note:
832 ========================================================================
833 */
834 static int rtusb_probe (struct usb_interface *intf,
835                                                 const struct usb_device_id *id)
836 {
837         PRTMP_ADAPTER pAd;
838         return (int)rt28xx_probe((void *)intf, (void *)id, 0, &pAd);
839 }
840
841
842 static void rtusb_disconnect(struct usb_interface *intf)
843 {
844         struct usb_device   *dev = interface_to_usbdev(intf);
845         PRTMP_ADAPTER       pAd;
846
847
848         pAd = usb_get_intfdata(intf);
849         usb_set_intfdata(intf, NULL);
850
851         _rtusb_disconnect(dev, pAd);
852 }
853
854
855 /*
856 ========================================================================
857 Routine Description:
858     Close kernel threads.
859
860 Arguments:
861         *pAd                            the raxx interface data pointer
862
863 Return Value:
864     NONE
865
866 Note:
867 ========================================================================
868 */
869 VOID RT28xxThreadTerminate(
870         IN RTMP_ADAPTER *pAd)
871 {
872         POS_COOKIE      pObj = (POS_COOKIE) pAd->OS_Cookie;
873         INT                     ret;
874
875
876         // Sleep 50 milliseconds so pending io might finish normally
877         RTMPusecDelay(50000);
878
879         // We want to wait until all pending receives and sends to the
880         // device object. We cancel any
881         // irps. Wait until sends and receives have stopped.
882         RTUSBCancelPendingIRPs(pAd);
883
884         // Terminate Threads
885 #ifndef RT30xx
886         BUG_ON(pObj->TimerQThr_task == NULL);
887         CHECK_PID_LEGALITY(task_pid(pObj->TimerQThr_task))
888         {
889                 POS_COOKIE pObj = (POS_COOKIE)pAd->OS_Cookie;
890
891                 printk(KERN_DEBUG "Terminate the TimerQThr pid=%d!\n",
892                         pid_nr(task_pid(pObj->TimerQThr_task)));
893                 mb();
894                 pAd->TimerFunc_kill = 1;
895                 mb();
896                 kthread_stop(pObj->TimerQThr_task);
897                 pObj->TimerQThr_task = NULL;
898         }
899
900         BUG_ON(pObj->MLMEThr_task == NULL);
901         CHECK_PID_LEGALITY(task_pid(pObj->MLMEThr_task))
902         {
903                 printk(KERN_DEBUG "Terminate the MLMEThr pid=%d!\n",
904                         pid_nr(task_pid(pObj->MLMEThr_task)));
905                 mb();
906                 pAd->mlme_kill = 1;
907                 //RT28XX_MLME_HANDLER(pAd);
908                 mb();
909                 kthread_stop(pObj->MLMEThr_task);
910                 pObj->MLMEThr_task = NULL;
911         }
912
913         BUG_ON(pObj->RTUSBCmdThr_task == NULL);
914         CHECK_PID_LEGALITY(task_pid(pObj->RTUSBCmdThr_task))
915         {
916                 printk(KERN_DEBUG "Terminate the RTUSBCmdThr pid=%d!\n",
917                         pid_nr(task_pid(pObj->RTUSBCmdThr_task)));
918                 mb();
919                 NdisAcquireSpinLock(&pAd->CmdQLock);
920                 pAd->CmdQ.CmdQState = RT2870_THREAD_STOPED;
921                 NdisReleaseSpinLock(&pAd->CmdQLock);
922                 mb();
923                 //RTUSBCMDUp(pAd);
924                 kthread_stop(pObj->RTUSBCmdThr_task);
925                 pObj->RTUSBCmdThr_task = NULL;
926         }
927 #endif
928 #ifdef RT30xx
929         if (pObj->MLMEThr_pid)
930         {
931                 printk("Terminate the MLMEThr_pid=%d!\n", pid_nr(pObj->MLMEThr_pid));
932                 mb();
933                 pAd->mlme_kill = 1;
934                 //RT28XX_MLME_HANDLER(pAd);
935                 mb();
936                 ret = kill_pid(pObj->MLMEThr_pid, SIGTERM, 1);
937                 if (ret)
938                 {
939                         printk (KERN_WARNING "%s: unable to Mlme thread, pid=%d, ret=%d!\n",
940                                         pAd->net_dev->name, pid_nr(pObj->MLMEThr_pid), ret);
941                 }
942                 else
943                 {
944                         //wait_for_completion (&pAd->notify);
945                         wait_for_completion (&pAd->mlmeComplete);
946                         pObj->MLMEThr_pid = NULL;
947                 }
948         }
949
950         if (pObj->RTUSBCmdThr_pid >= 0)
951         {
952                 printk("Terminate the RTUSBCmdThr_pid=%d!\n", pid_nr(pObj->RTUSBCmdThr_pid));
953                 mb();
954                 NdisAcquireSpinLock(&pAd->CmdQLock);
955                 pAd->CmdQ.CmdQState = RT2870_THREAD_STOPED;
956                 NdisReleaseSpinLock(&pAd->CmdQLock);
957                 mb();
958                 //RTUSBCMDUp(pAd);
959                 ret = kill_pid(pObj->RTUSBCmdThr_pid, SIGTERM, 1);
960                 if (ret)
961                 {
962                         printk(KERN_WARNING "%s: unable to RTUSBCmd thread, pid=%d, ret=%d!\n",
963                                         pAd->net_dev->name, pid_nr(pObj->RTUSBCmdThr_pid), ret);
964         }
965                 else
966                 {
967                         //wait_for_completion (&pAd->notify);
968                         wait_for_completion (&pAd->CmdQComplete);
969                         pObj->RTUSBCmdThr_pid = NULL;
970                 }
971         }
972         if (pObj->TimerQThr_pid >= 0)
973         {
974                 POS_COOKIE pObj = (POS_COOKIE)pAd->OS_Cookie;
975                 printk("Terminate the TimerQThr_pid=%d!\n", pid_nr(pObj->TimerQThr_pid));
976                 mb();
977                 pAd->TimerFunc_kill = 1;
978                 mb();
979                 ret = kill_pid(pObj->TimerQThr_pid, SIGTERM, 1);
980                 if (ret)
981                 {
982                         printk(KERN_WARNING "%s: unable to stop TimerQThread, pid=%d, ret=%d!\n",
983                                         pAd->net_dev->name, pid_nr(pObj->TimerQThr_pid), ret);
984                 }
985                 else
986                 {
987                         printk("wait_for_completion TimerQThr\n");
988                         wait_for_completion(&pAd->TimerQComplete);
989                         pObj->TimerQThr_pid = NULL;
990                 }
991         }
992 #endif
993
994         // Kill tasklets
995         pAd->mlme_kill = 0;
996         pAd->CmdQ.CmdQState = RT2870_THREAD_UNKNOWN;
997         pAd->TimerFunc_kill = 0;
998 }
999
1000
1001 void kill_thread_task(IN PRTMP_ADAPTER pAd)
1002 {
1003         POS_COOKIE pObj;
1004
1005         pObj = (POS_COOKIE) pAd->OS_Cookie;
1006
1007         tasklet_kill(&pObj->rx_done_task);
1008         tasklet_kill(&pObj->mgmt_dma_done_task);
1009         tasklet_kill(&pObj->ac0_dma_done_task);
1010         tasklet_kill(&pObj->ac1_dma_done_task);
1011         tasklet_kill(&pObj->ac2_dma_done_task);
1012         tasklet_kill(&pObj->ac3_dma_done_task);
1013         tasklet_kill(&pObj->hcca_dma_done_task);
1014         tasklet_kill(&pObj->tbtt_task);
1015
1016 }
1017
1018
1019 /*
1020 ========================================================================
1021 Routine Description:
1022     Check the chipset vendor/product ID.
1023
1024 Arguments:
1025     _dev_p                              Point to the PCI or USB device
1026
1027 Return Value:
1028     TRUE                                Check ok
1029         FALSE                           Check fail
1030
1031 Note:
1032 ========================================================================
1033 */
1034 BOOLEAN RT28XXChipsetCheck(
1035         IN void *_dev_p)
1036 {
1037         struct usb_interface *intf = (struct usb_interface *)_dev_p;
1038         struct usb_device *dev_p = interface_to_usbdev(intf);
1039         UINT32 i;
1040
1041
1042         for(i=0; i<rtusb_usb_id_len; i++)
1043         {
1044                 if (dev_p->descriptor.idVendor == rtusb_usb_id[i].idVendor &&
1045                         dev_p->descriptor.idProduct == rtusb_usb_id[i].idProduct)
1046                 {
1047 #ifndef RT30xx
1048                         printk(KERN_DEBUG "rt2870: idVendor = 0x%x, idProduct = 0x%x\n",
1049 #endif
1050 #ifdef RT30xx
1051                         printk("rt2870: idVendor = 0x%x, idProduct = 0x%x\n",
1052 #endif
1053                                         dev_p->descriptor.idVendor, dev_p->descriptor.idProduct);
1054                         break;
1055                 }
1056         }
1057
1058         if (i == rtusb_usb_id_len)
1059         {
1060                 printk("rt2870: Error! Device Descriptor not matching!\n");
1061                 return FALSE;
1062         }
1063
1064         return TRUE;
1065 }
1066
1067
1068 /*
1069 ========================================================================
1070 Routine Description:
1071     Init net device structure.
1072
1073 Arguments:
1074     _dev_p                              Point to the PCI or USB device
1075     *net_dev                    Point to the net device
1076         *pAd                            the raxx interface data pointer
1077
1078 Return Value:
1079     TRUE                                Init ok
1080         FALSE                           Init fail
1081
1082 Note:
1083 ========================================================================
1084 */
1085 BOOLEAN RT28XXNetDevInit(
1086         IN void                                 *_dev_p,
1087         IN struct  net_device   *net_dev,
1088         IN RTMP_ADAPTER                 *pAd)
1089 {
1090         struct usb_interface *intf = (struct usb_interface *)_dev_p;
1091         struct usb_device *dev_p = interface_to_usbdev(intf);
1092
1093
1094         pAd->config = &dev_p->config->desc;
1095         return TRUE;
1096 }
1097
1098
1099 /*
1100 ========================================================================
1101 Routine Description:
1102     Init net device structure.
1103
1104 Arguments:
1105     _dev_p                              Point to the PCI or USB device
1106         *pAd                            the raxx interface data pointer
1107
1108 Return Value:
1109     TRUE                                Config ok
1110         FALSE                           Config fail
1111
1112 Note:
1113 ========================================================================
1114 */
1115 BOOLEAN RT28XXProbePostConfig(
1116         IN void                                 *_dev_p,
1117         IN RTMP_ADAPTER                 *pAd,
1118         IN INT32                                interface)
1119 {
1120         struct usb_interface *intf = (struct usb_interface *)_dev_p;
1121         struct usb_host_interface *iface_desc;
1122         ULONG BulkOutIdx;
1123         UINT32 i;
1124
1125
1126         /* get the active interface descriptor */
1127         iface_desc = intf->cur_altsetting;
1128
1129         /* get # of enpoints  */
1130         pAd->NumberOfPipes = iface_desc->desc.bNumEndpoints;
1131         DBGPRINT(RT_DEBUG_TRACE,
1132                         ("NumEndpoints=%d\n", iface_desc->desc.bNumEndpoints));
1133
1134         /* Configure Pipes */
1135         BulkOutIdx = 0;
1136
1137         for(i=0; i<pAd->NumberOfPipes; i++)
1138         {
1139                 if ((iface_desc->endpoint[i].desc.bmAttributes ==
1140                                 USB_ENDPOINT_XFER_BULK) &&
1141                         ((iface_desc->endpoint[i].desc.bEndpointAddress &
1142                                 USB_ENDPOINT_DIR_MASK) == USB_DIR_IN))
1143                 {
1144                         pAd->BulkInEpAddr = iface_desc->endpoint[i].desc.bEndpointAddress;
1145                         pAd->BulkInMaxPacketSize = iface_desc->endpoint[i].desc.wMaxPacketSize;
1146
1147                         DBGPRINT_RAW(RT_DEBUG_TRACE,
1148                                 ("BULK IN MaximumPacketSize = %d\n", pAd->BulkInMaxPacketSize));
1149                         DBGPRINT_RAW(RT_DEBUG_TRACE,
1150                                 ("EP address = 0x%2x\n", iface_desc->endpoint[i].desc.bEndpointAddress));
1151                 }
1152                 else if ((iface_desc->endpoint[i].desc.bmAttributes ==
1153                                         USB_ENDPOINT_XFER_BULK) &&
1154                                 ((iface_desc->endpoint[i].desc.bEndpointAddress &
1155                                         USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT))
1156                 {
1157                         // there are 6 bulk out EP. EP6 highest priority.
1158                         // EP1-4 is EDCA.  EP5 is HCCA.
1159                         pAd->BulkOutEpAddr[BulkOutIdx++] = iface_desc->endpoint[i].desc.bEndpointAddress;
1160                         pAd->BulkOutMaxPacketSize = iface_desc->endpoint[i].desc.wMaxPacketSize;
1161
1162                         DBGPRINT_RAW(RT_DEBUG_TRACE,
1163                                 ("BULK OUT MaximumPacketSize = %d\n", pAd->BulkOutMaxPacketSize));
1164                         DBGPRINT_RAW(RT_DEBUG_TRACE,
1165                                 ("EP address = 0x%2x  \n", iface_desc->endpoint[i].desc.bEndpointAddress));
1166                 }
1167         }
1168
1169         if (!(pAd->BulkInEpAddr && pAd->BulkOutEpAddr[0]))
1170         {
1171                 printk("%s: Could not find both bulk-in and bulk-out endpoints\n", __func__);
1172                 return FALSE;
1173         }
1174
1175         return TRUE;
1176 }
1177
1178
1179 /*
1180 ========================================================================
1181 Routine Description:
1182     Disable DMA.
1183
1184 Arguments:
1185         *pAd                            the raxx interface data pointer
1186
1187 Return Value:
1188         None
1189
1190 Note:
1191 ========================================================================
1192 */
1193 VOID RT28XXDMADisable(
1194         IN RTMP_ADAPTER                 *pAd)
1195 {
1196         // no use
1197 }
1198
1199
1200
1201 /*
1202 ========================================================================
1203 Routine Description:
1204     Enable DMA.
1205
1206 Arguments:
1207         *pAd                            the raxx interface data pointer
1208
1209 Return Value:
1210         None
1211
1212 Note:
1213 ========================================================================
1214 */
1215 VOID RT28XXDMAEnable(
1216         IN RTMP_ADAPTER                 *pAd)
1217 {
1218         WPDMA_GLO_CFG_STRUC     GloCfg;
1219         USB_DMA_CFG_STRUC       UsbCfg;
1220         int                                     i = 0;
1221
1222
1223         RTMP_IO_WRITE32(pAd, MAC_SYS_CTRL, 0x4);
1224         do
1225         {
1226                 RTMP_IO_READ32(pAd, WPDMA_GLO_CFG, &GloCfg.word);
1227                 if ((GloCfg.field.TxDMABusy == 0)  && (GloCfg.field.RxDMABusy == 0))
1228                         break;
1229
1230                 DBGPRINT(RT_DEBUG_TRACE, ("==>  DMABusy\n"));
1231                 RTMPusecDelay(1000);
1232                 i++;
1233         }while ( i <200);
1234
1235
1236         RTMPusecDelay(50);
1237         GloCfg.field.EnTXWriteBackDDONE = 1;
1238         GloCfg.field.EnableRxDMA = 1;
1239         GloCfg.field.EnableTxDMA = 1;
1240         DBGPRINT(RT_DEBUG_TRACE, ("<== WRITE DMA offset 0x208 = 0x%x\n", GloCfg.word));
1241         RTMP_IO_WRITE32(pAd, WPDMA_GLO_CFG, GloCfg.word);
1242
1243         UsbCfg.word = 0;
1244         UsbCfg.field.phyclear = 0;
1245         /* usb version is 1.1,do not use bulk in aggregation */
1246         if (pAd->BulkInMaxPacketSize == 512)
1247                         UsbCfg.field.RxBulkAggEn = 1;
1248         /* for last packet, PBF might use more than limited, so minus 2 to prevent from error */
1249         UsbCfg.field.RxBulkAggLmt = (MAX_RXBULK_SIZE /1024)-3;
1250         UsbCfg.field.RxBulkAggTOut = 0x80; /* 2006-10-18 */
1251         UsbCfg.field.RxBulkEn = 1;
1252         UsbCfg.field.TxBulkEn = 1;
1253
1254         RTUSBWriteMACRegister(pAd, USB_DMA_CFG, UsbCfg.word);
1255
1256 }
1257
1258 /*
1259 ========================================================================
1260 Routine Description:
1261     Write Beacon buffer to Asic.
1262
1263 Arguments:
1264         *pAd                            the raxx interface data pointer
1265
1266 Return Value:
1267         None
1268
1269 Note:
1270 ========================================================================
1271 */
1272 VOID RT28xx_UpdateBeaconToAsic(
1273         IN RTMP_ADAPTER         *pAd,
1274         IN INT                          apidx,
1275         IN ULONG                        FrameLen,
1276         IN ULONG                        UpdatePos)
1277 {
1278         PUCHAR          pBeaconFrame = NULL;
1279         UCHAR                   *ptr;
1280         UINT                    i, padding;
1281         BEACON_SYNC_STRUCT      *pBeaconSync = pAd->CommonCfg.pBeaconSync;
1282         UINT32                  longValue;
1283         BOOLEAN                 bBcnReq = FALSE;
1284         UCHAR                   bcn_idx = 0;
1285
1286
1287         if (pBeaconFrame == NULL)
1288         {
1289                 DBGPRINT(RT_DEBUG_ERROR,("pBeaconFrame is NULL!\n"));
1290                 return;
1291         }
1292
1293         if (pBeaconSync == NULL)
1294         {
1295                 DBGPRINT(RT_DEBUG_ERROR,("pBeaconSync is NULL!\n"));
1296                 return;
1297         }
1298
1299         //if ((pAd->WdsTab.Mode == WDS_BRIDGE_MODE) ||
1300         //      ((pAd->ApCfg.MBSSID[apidx].MSSIDDev == NULL) || !(pAd->ApCfg.MBSSID[apidx].MSSIDDev->flags & IFF_UP))
1301         //      )
1302         if (bBcnReq == FALSE)
1303         {
1304                 /* when the ra interface is down, do not send its beacon frame */
1305                 /* clear all zero */
1306                 for(i=0; i<TXWI_SIZE; i+=4) {
1307                         RTMP_IO_WRITE32(pAd, pAd->BeaconOffset[bcn_idx] + i, 0x00);
1308                 }
1309                 pBeaconSync->BeaconBitMap &= (~(BEACON_BITMAP_MASK & (1 << bcn_idx)));
1310                 NdisZeroMemory(pBeaconSync->BeaconTxWI[bcn_idx], TXWI_SIZE);
1311         }
1312         else
1313         {
1314                 ptr = (PUCHAR)&pAd->BeaconTxWI;
1315
1316                 if (NdisEqualMemory(pBeaconSync->BeaconTxWI[bcn_idx], &pAd->BeaconTxWI, TXWI_SIZE) == FALSE)
1317                 {       // If BeaconTxWI changed, we need to rewrite the TxWI for the Beacon frames.
1318                         pBeaconSync->BeaconBitMap &= (~(BEACON_BITMAP_MASK & (1 << bcn_idx)));
1319                         NdisMoveMemory(pBeaconSync->BeaconTxWI[bcn_idx], &pAd->BeaconTxWI, TXWI_SIZE);
1320                 }
1321
1322                 if ((pBeaconSync->BeaconBitMap & (1 << bcn_idx)) != (1 << bcn_idx))
1323                 {
1324                         for (i=0; i<TXWI_SIZE; i+=4)  // 16-byte TXWI field
1325                         {
1326                                 longValue =  *ptr + (*(ptr+1)<<8) + (*(ptr+2)<<16) + (*(ptr+3)<<24);
1327                                 RTMP_IO_WRITE32(pAd, pAd->BeaconOffset[bcn_idx] + i, longValue);
1328                                 ptr += 4;
1329                         }
1330                 }
1331
1332                 ptr = pBeaconSync->BeaconBuf[bcn_idx];
1333                 padding = (FrameLen & 0x01);
1334                 NdisZeroMemory((PUCHAR)(pBeaconFrame + FrameLen), padding);
1335                 FrameLen += padding;
1336                 for (i = 0 ; i < FrameLen /*HW_BEACON_OFFSET*/; i += 2)
1337                 {
1338                         if (NdisEqualMemory(ptr, pBeaconFrame, 2) == FALSE)
1339                         {
1340                                 NdisMoveMemory(ptr, pBeaconFrame, 2);
1341                                 //shortValue = *ptr + (*(ptr+1)<<8);
1342                                 //RTMP_IO_WRITE8(pAd, pAd->BeaconOffset[bcn_idx] + TXWI_SIZE + i, shortValue);
1343                                 RTUSBMultiWrite(pAd, pAd->BeaconOffset[bcn_idx] + TXWI_SIZE + i, ptr, 2);
1344                         }
1345                         ptr +=2;
1346                         pBeaconFrame += 2;
1347                 }
1348
1349                 pBeaconSync->BeaconBitMap |= (1 << bcn_idx);
1350
1351                 // For AP interface, set the DtimBitOn so that we can send Bcast/Mcast frame out after this beacon frame.
1352         }
1353
1354 }
1355
1356
1357 VOID RT2870_BssBeaconStop(
1358         IN RTMP_ADAPTER *pAd)
1359 {
1360         BEACON_SYNC_STRUCT      *pBeaconSync;
1361         int i, offset;
1362         BOOLEAN Cancelled = TRUE;
1363
1364         pBeaconSync = pAd->CommonCfg.pBeaconSync;
1365         if (pBeaconSync && pBeaconSync->EnableBeacon)
1366         {
1367                 INT NumOfBcn;
1368
1369                 NumOfBcn = MAX_MESH_NUM;
1370
1371                 RTMPCancelTimer(&pAd->CommonCfg.BeaconUpdateTimer, &Cancelled);
1372
1373                 for(i=0; i<NumOfBcn; i++)
1374                 {
1375                         NdisZeroMemory(pBeaconSync->BeaconBuf[i], HW_BEACON_OFFSET);
1376                         NdisZeroMemory(pBeaconSync->BeaconTxWI[i], TXWI_SIZE);
1377
1378                         for (offset=0; offset<HW_BEACON_OFFSET; offset+=4)
1379                                 RTMP_IO_WRITE32(pAd, pAd->BeaconOffset[i] + offset, 0x00);
1380
1381                         pBeaconSync->CapabilityInfoLocationInBeacon[i] = 0;
1382                         pBeaconSync->TimIELocationInBeacon[i] = 0;
1383                 }
1384                 pBeaconSync->BeaconBitMap = 0;
1385                 pBeaconSync->DtimBitOn = 0;
1386         }
1387 }
1388
1389
1390 VOID RT2870_BssBeaconStart(
1391         IN RTMP_ADAPTER *pAd)
1392 {
1393         int apidx;
1394         BEACON_SYNC_STRUCT      *pBeaconSync;
1395 //      LARGE_INTEGER   tsfTime, deltaTime;
1396
1397         pBeaconSync = pAd->CommonCfg.pBeaconSync;
1398         if (pBeaconSync && pBeaconSync->EnableBeacon)
1399         {
1400                 INT NumOfBcn;
1401
1402                 NumOfBcn = MAX_MESH_NUM;
1403
1404                 for(apidx=0; apidx<NumOfBcn; apidx++)
1405                 {
1406                         UCHAR CapabilityInfoLocationInBeacon = 0;
1407                         UCHAR TimIELocationInBeacon = 0;
1408
1409                         NdisZeroMemory(pBeaconSync->BeaconBuf[apidx], HW_BEACON_OFFSET);
1410                         pBeaconSync->CapabilityInfoLocationInBeacon[apidx] = CapabilityInfoLocationInBeacon;
1411                         pBeaconSync->TimIELocationInBeacon[apidx] = TimIELocationInBeacon;
1412                         NdisZeroMemory(pBeaconSync->BeaconTxWI[apidx], TXWI_SIZE);
1413                 }
1414                 pBeaconSync->BeaconBitMap = 0;
1415                 pBeaconSync->DtimBitOn = 0;
1416                 pAd->CommonCfg.BeaconUpdateTimer.Repeat = TRUE;
1417
1418                 pAd->CommonCfg.BeaconAdjust = 0;
1419                 pAd->CommonCfg.BeaconFactor = 0xffffffff / (pAd->CommonCfg.BeaconPeriod << 10);
1420                 pAd->CommonCfg.BeaconRemain = (0xffffffff % (pAd->CommonCfg.BeaconPeriod << 10)) + 1;
1421                 printk("RT2870_BssBeaconStart:BeaconFactor=%d, BeaconRemain=%d!\n", pAd->CommonCfg.BeaconFactor, pAd->CommonCfg.BeaconRemain);
1422                 RTMPSetTimer(&pAd->CommonCfg.BeaconUpdateTimer, pAd->CommonCfg.BeaconPeriod);
1423
1424         }
1425 }
1426
1427
1428 VOID RT2870_BssBeaconInit(
1429         IN RTMP_ADAPTER *pAd)
1430 {
1431         BEACON_SYNC_STRUCT      *pBeaconSync;
1432         int i;
1433
1434         NdisAllocMemory(pAd->CommonCfg.pBeaconSync, sizeof(BEACON_SYNC_STRUCT), MEM_ALLOC_FLAG);
1435         if (pAd->CommonCfg.pBeaconSync)
1436         {
1437                 pBeaconSync = pAd->CommonCfg.pBeaconSync;
1438                 NdisZeroMemory(pBeaconSync, sizeof(BEACON_SYNC_STRUCT));
1439                 for(i=0; i < HW_BEACON_MAX_COUNT; i++)
1440                 {
1441                         NdisZeroMemory(pBeaconSync->BeaconBuf[i], HW_BEACON_OFFSET);
1442                         pBeaconSync->CapabilityInfoLocationInBeacon[i] = 0;
1443                         pBeaconSync->TimIELocationInBeacon[i] = 0;
1444                         NdisZeroMemory(pBeaconSync->BeaconTxWI[i], TXWI_SIZE);
1445                 }
1446                 pBeaconSync->BeaconBitMap = 0;
1447
1448                 //RTMPInitTimer(pAd, &pAd->CommonCfg.BeaconUpdateTimer, GET_TIMER_FUNCTION(BeaconUpdateExec), pAd, TRUE);
1449                 pBeaconSync->EnableBeacon = TRUE;
1450         }
1451 }
1452
1453
1454 VOID RT2870_BssBeaconExit(
1455         IN RTMP_ADAPTER *pAd)
1456 {
1457         BEACON_SYNC_STRUCT      *pBeaconSync;
1458         BOOLEAN Cancelled = TRUE;
1459         int i;
1460
1461         if (pAd->CommonCfg.pBeaconSync)
1462         {
1463                 pBeaconSync = pAd->CommonCfg.pBeaconSync;
1464                 pBeaconSync->EnableBeacon = FALSE;
1465                 RTMPCancelTimer(&pAd->CommonCfg.BeaconUpdateTimer, &Cancelled);
1466                 pBeaconSync->BeaconBitMap = 0;
1467
1468                 for(i=0; i<HW_BEACON_MAX_COUNT; i++)
1469                 {
1470                         NdisZeroMemory(pBeaconSync->BeaconBuf[i], HW_BEACON_OFFSET);
1471                         pBeaconSync->CapabilityInfoLocationInBeacon[i] = 0;
1472                         pBeaconSync->TimIELocationInBeacon[i] = 0;
1473                         NdisZeroMemory(pBeaconSync->BeaconTxWI[i], TXWI_SIZE);
1474                 }
1475
1476                 NdisFreeMemory(pAd->CommonCfg.pBeaconSync, HW_BEACON_OFFSET * HW_BEACON_MAX_COUNT, 0);
1477                 pAd->CommonCfg.pBeaconSync = NULL;
1478         }
1479 }
1480
1481 VOID BeaconUpdateExec(
1482     IN PVOID SystemSpecific1,
1483     IN PVOID FunctionContext,
1484     IN PVOID SystemSpecific2,
1485     IN PVOID SystemSpecific3)
1486 {
1487         PRTMP_ADAPTER   pAd = (PRTMP_ADAPTER)FunctionContext;
1488         LARGE_INTEGER   tsfTime_a;//, tsfTime_b, deltaTime_exp, deltaTime_ab;
1489         UINT32                  delta, remain, remain_low, remain_high;
1490 //      BOOLEAN                 positive;
1491
1492         ReSyncBeaconTime(pAd);
1493
1494
1495
1496         RTMP_IO_READ32(pAd, TSF_TIMER_DW0, &tsfTime_a.u.LowPart);
1497         RTMP_IO_READ32(pAd, TSF_TIMER_DW1, &tsfTime_a.u.HighPart);
1498
1499
1500         //positive=getDeltaTime(tsfTime_a, expectedTime, &deltaTime_exp);
1501         remain_high = pAd->CommonCfg.BeaconRemain * tsfTime_a.u.HighPart;
1502         remain_low = tsfTime_a.u.LowPart % (pAd->CommonCfg.BeaconPeriod << 10);
1503         remain = (remain_high + remain_low)%(pAd->CommonCfg.BeaconPeriod << 10);
1504         delta = (pAd->CommonCfg.BeaconPeriod << 10) - remain;
1505
1506         pAd->CommonCfg.BeaconUpdateTimer.TimerValue = (delta >> 10) + 10;
1507
1508 }
1509