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