Staging: rt3070: remove dead CONFIG_AP_SUPPORT code
[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         {
616                 idx = 0;
617                 if ((MACValue & 0xff00) !=0 )
618                 {
619                         DBGPRINT(RT_DEBUG_TRACE, ("TX QUEUE 1 Not EMPTY(Value=0x%0x). !!!!!!!!!!!!!!!\n", MACValue));
620                         RTMP_IO_WRITE32(pAd, PBF_CFG, 0xf4000a);
621                         while((MACValue &0xff00) != 0 && (idx++ < 10))
622                         {
623                                 RTMP_IO_READ32(pAd, TXRXQ_PCNT, &MACValue);
624                                 NdisMSleep(1);
625                         }
626                         RTMP_IO_WRITE32(pAd, PBF_CFG, 0xf40006);
627                 }
628         }
629
630         if (pAd->watchDogRxOverFlowCnt >= 2)
631         {
632                 DBGPRINT(RT_DEBUG_TRACE, ("Maybe the Rx Bulk-In hanged! Cancel the pending Rx bulks request!\n"));
633                 if ((!RTMP_TEST_FLAG(pAd, (fRTMP_ADAPTER_RESET_IN_PROGRESS |
634                                                                         fRTMP_ADAPTER_BULKIN_RESET |
635                                                                         fRTMP_ADAPTER_HALT_IN_PROGRESS |
636                                                                         fRTMP_ADAPTER_NIC_NOT_EXIST))))
637                 {
638                         DBGPRINT(RT_DEBUG_TRACE, ("Call CMDTHREAD_RESET_BULK_IN to cancel the pending Rx Bulk!\n"));
639                         RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_BULKIN_RESET);
640                         RTUSBEnqueueInternalCmd(pAd, CMDTHREAD_RESET_BULK_IN, NULL, 0);
641                         needDumpSeq = TRUE;
642                 }
643                 pAd->watchDogRxOverFlowCnt = 0;
644         }
645
646
647         for (idx = 0; idx < NUM_OF_TX_RING; idx++)
648         {
649                 pUrb = NULL;
650
651                 RTMP_IRQ_LOCK(&pAd->BulkOutLock[idx], irqFlags);
652                 if ((pAd->BulkOutPending[idx] == TRUE) && pAd->watchDogTxPendingCnt)
653                 {
654                         pAd->watchDogTxPendingCnt[idx]++;
655
656                         if ((pAd->watchDogTxPendingCnt[idx] > 2) &&
657                                  (!RTMP_TEST_FLAG(pAd, (fRTMP_ADAPTER_RESET_IN_PROGRESS | fRTMP_ADAPTER_HALT_IN_PROGRESS | fRTMP_ADAPTER_NIC_NOT_EXIST | fRTMP_ADAPTER_BULKOUT_RESET)))
658                                 )
659                         {
660                                 // FIXME: Following code just support single bulk out. If you wanna support multiple bulk out. Modify it!
661                                 pHTTXContext = (PHT_TX_CONTEXT)(&pAd->TxContext[idx]);
662                                 if (pHTTXContext->IRPPending)
663                                 {       // Check TxContext.
664                                         pUrb = pHTTXContext->pUrb;
665                                 }
666                                 else if (idx == MGMTPIPEIDX)
667                                 {
668                                         PTX_CONTEXT pMLMEContext, pNULLContext, pPsPollContext;
669
670                                         //Check MgmtContext.
671                                         pMLMEContext = (PTX_CONTEXT)(pAd->MgmtRing.Cell[pAd->MgmtRing.TxDmaIdx].AllocVa);
672                                         pPsPollContext = (PTX_CONTEXT)(&pAd->PsPollContext);
673                                         pNULLContext = (PTX_CONTEXT)(&pAd->NullContext);
674
675                                         if (pMLMEContext->IRPPending)
676                                         {
677                                                 ASSERT(pMLMEContext->IRPPending);
678                                                 pUrb = pMLMEContext->pUrb;
679                                         }
680                                         else if (pNULLContext->IRPPending)
681                                         {
682                                                 ASSERT(pNULLContext->IRPPending);
683                                                 pUrb = pNULLContext->pUrb;
684                                         }
685                                         else if (pPsPollContext->IRPPending)
686                                         {
687                                                 ASSERT(pPsPollContext->IRPPending);
688                                                 pUrb = pPsPollContext->pUrb;
689                                         }
690                                 }
691
692                                 RTMP_IRQ_UNLOCK(&pAd->BulkOutLock[idx], irqFlags);
693
694                                 DBGPRINT(RT_DEBUG_TRACE, ("Maybe the Tx Bulk-Out hanged! Cancel the pending Tx bulks request of idx(%d)!\n", idx));
695                                 if (pUrb)
696                                 {
697                                         DBGPRINT(RT_DEBUG_TRACE, ("Unlink the pending URB!\n"));
698                                         // unlink it now
699                                         RTUSB_UNLINK_URB(pUrb);
700                                         // Sleep 200 microseconds to give cancellation time to work
701                                         RTMPusecDelay(200);
702                                         needDumpSeq = TRUE;
703                                 }
704                                 else
705                                 {
706                                         DBGPRINT(RT_DEBUG_ERROR, ("Unkonw bulkOut URB maybe hanged!!!!!!!!!!!!\n"));
707                                 }
708                         }
709                         else
710                         {
711                                 RTMP_IRQ_UNLOCK(&pAd->BulkOutLock[idx], irqFlags);
712                         }
713                 }
714                 else
715                 {
716                         RTMP_IRQ_UNLOCK(&pAd->BulkOutLock[idx], irqFlags);
717                 }
718         }
719
720         // For Sigma debug, dump the ba_reordering sequence.
721         if((needDumpSeq == TRUE) && (pAd->CommonCfg.bDisableReordering == 0))
722         {
723                 USHORT                          Idx;
724                 PBA_REC_ENTRY           pBAEntry = NULL;
725                 UCHAR                           count = 0;
726                 struct reordering_mpdu *mpdu_blk;
727
728                 Idx = pAd->MacTab.Content[BSSID_WCID].BARecWcidArray[0];
729
730                 pBAEntry = &pAd->BATable.BARecEntry[Idx];
731                 if((pBAEntry->list.qlen > 0) && (pBAEntry->list.next != NULL))
732                 {
733                         DBGPRINT(RT_DEBUG_TRACE, ("NICUpdateRawCounters():The Queueing pkt in reordering buffer:\n"));
734                         NdisAcquireSpinLock(&pBAEntry->RxReRingLock);
735                         mpdu_blk = pBAEntry->list.next;
736                         while (mpdu_blk)
737                         {
738                                 DBGPRINT(RT_DEBUG_TRACE, ("\t%d:Seq-%d, bAMSDU-%d!\n", count, mpdu_blk->Sequence, mpdu_blk->bAMSDU));
739                                 mpdu_blk = mpdu_blk->next;
740                                 count++;
741                         }
742
743                         DBGPRINT(RT_DEBUG_TRACE, ("\npBAEntry->LastIndSeq=%d!\n", pBAEntry->LastIndSeq));
744                         NdisReleaseSpinLock(&pBAEntry->RxReRingLock);
745                 }
746         }
747 }
748
749 /*
750 ========================================================================
751 Routine Description:
752     Release allocated resources.
753
754 Arguments:
755     *dev                                Point to the PCI or USB device
756         pAd                                     driver control block pointer
757
758 Return Value:
759     None
760
761 Note:
762 ========================================================================
763 */
764 static void _rtusb_disconnect(struct usb_device *dev, PRTMP_ADAPTER pAd)
765 {
766         struct net_device       *net_dev = NULL;
767
768
769         DBGPRINT(RT_DEBUG_ERROR, ("rtusb_disconnect: unregister usbnet usb-%s-%s\n",
770                                 dev->bus->bus_name, dev->devpath));
771         if (!pAd)
772         {
773                 usb_put_dev(dev);
774
775                 printk("rtusb_disconnect: pAd == NULL!\n");
776                 return;
777         }
778         RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST);
779
780
781
782         // for debug, wait to show some messages to /proc system
783         udelay(1);
784
785
786
787
788         net_dev = pAd->net_dev;
789         if (pAd->net_dev != NULL)
790         {
791                 printk("rtusb_disconnect: unregister_netdev(), dev->name=%s!\n", net_dev->name);
792                 unregister_netdev (pAd->net_dev);
793         }
794         udelay(1);
795         flush_scheduled_work();
796         udelay(1);
797
798         // free net_device memory
799         free_netdev(net_dev);
800
801         // free adapter memory
802         RTMPFreeAdapter(pAd);
803
804         // release a use of the usb device structure
805         usb_put_dev(dev);
806         udelay(1);
807
808         DBGPRINT(RT_DEBUG_ERROR, (" RTUSB disconnect successfully\n"));
809 }
810
811
812 /*
813 ========================================================================
814 Routine Description:
815     Probe RT28XX chipset.
816
817 Arguments:
818     *dev                                Point to the PCI or USB device
819         interface
820         *id_table                       Point to the PCI or USB device ID
821
822 Return Value:
823     None
824
825 Note:
826 ========================================================================
827 */
828 static int rtusb_probe (struct usb_interface *intf,
829                                                 const struct usb_device_id *id)
830 {
831         PRTMP_ADAPTER pAd;
832         return (int)rt28xx_probe((void *)intf, (void *)id, 0, &pAd);
833 }
834
835
836 static void rtusb_disconnect(struct usb_interface *intf)
837 {
838         struct usb_device   *dev = interface_to_usbdev(intf);
839         PRTMP_ADAPTER       pAd;
840
841
842         pAd = usb_get_intfdata(intf);
843         usb_set_intfdata(intf, NULL);
844
845         _rtusb_disconnect(dev, pAd);
846 }
847
848
849 /*
850 ========================================================================
851 Routine Description:
852     Close kernel threads.
853
854 Arguments:
855         *pAd                            the raxx interface data pointer
856
857 Return Value:
858     NONE
859
860 Note:
861 ========================================================================
862 */
863 VOID RT28xxThreadTerminate(
864         IN RTMP_ADAPTER *pAd)
865 {
866         POS_COOKIE      pObj = (POS_COOKIE) pAd->OS_Cookie;
867         INT                     ret;
868
869
870         // Sleep 50 milliseconds so pending io might finish normally
871         RTMPusecDelay(50000);
872
873         // We want to wait until all pending receives and sends to the
874         // device object. We cancel any
875         // irps. Wait until sends and receives have stopped.
876         RTUSBCancelPendingIRPs(pAd);
877
878         // Terminate Threads
879         if (pObj->MLMEThr_pid)
880         {
881                 printk("Terminate the MLMEThr_pid=%d!\n", pid_nr(pObj->MLMEThr_pid));
882                 mb();
883                 pAd->mlme_kill = 1;
884                 //RT28XX_MLME_HANDLER(pAd);
885                 mb();
886                 ret = kill_pid(pObj->MLMEThr_pid, SIGTERM, 1);
887                 if (ret)
888                 {
889                         printk (KERN_WARNING "%s: unable to Mlme thread, pid=%d, ret=%d!\n",
890                                         pAd->net_dev->name, pid_nr(pObj->MLMEThr_pid), ret);
891                 }
892                 else
893                 {
894                         //wait_for_completion (&pAd->notify);
895                         wait_for_completion (&pAd->mlmeComplete);
896                         pObj->MLMEThr_pid = NULL;
897                 }
898         }
899
900         if (pObj->RTUSBCmdThr_pid >= 0)
901         {
902                 printk("Terminate the RTUSBCmdThr_pid=%d!\n", pid_nr(pObj->RTUSBCmdThr_pid));
903                 mb();
904                 NdisAcquireSpinLock(&pAd->CmdQLock);
905                 pAd->CmdQ.CmdQState = RT2870_THREAD_STOPED;
906                 NdisReleaseSpinLock(&pAd->CmdQLock);
907                 mb();
908                 //RTUSBCMDUp(pAd);
909                 ret = kill_pid(pObj->RTUSBCmdThr_pid, SIGTERM, 1);
910                 if (ret)
911                 {
912                         printk(KERN_WARNING "%s: unable to RTUSBCmd thread, pid=%d, ret=%d!\n",
913                                         pAd->net_dev->name, pid_nr(pObj->RTUSBCmdThr_pid), ret);
914                 }
915                 else
916                 {
917                         //wait_for_completion (&pAd->notify);
918                         wait_for_completion (&pAd->CmdQComplete);
919                         pObj->RTUSBCmdThr_pid = NULL;
920                 }
921         }
922         if (pObj->TimerQThr_pid >= 0)
923         {
924                 POS_COOKIE pObj = (POS_COOKIE)pAd->OS_Cookie;
925
926                 printk("Terminate the TimerQThr_pid=%d!\n", pid_nr(pObj->TimerQThr_pid));
927                 mb();
928                 pAd->TimerFunc_kill = 1;
929                 mb();
930                 ret = kill_pid(pObj->TimerQThr_pid, SIGTERM, 1);
931                 if (ret)
932                 {
933                         printk(KERN_WARNING "%s: unable to stop TimerQThread, pid=%d, ret=%d!\n",
934                                         pAd->net_dev->name, pid_nr(pObj->TimerQThr_pid), ret);
935                 }
936                 else
937                 {
938                         printk("wait_for_completion TimerQThr\n");
939                         wait_for_completion(&pAd->TimerQComplete);
940                         pObj->TimerQThr_pid = NULL;
941                 }
942         }
943         // Kill tasklets
944         pAd->mlme_kill = 0;
945         pAd->CmdQ.CmdQState = RT2870_THREAD_UNKNOWN;
946         pAd->TimerFunc_kill = 0;
947 }
948
949
950 void kill_thread_task(IN PRTMP_ADAPTER pAd)
951 {
952         POS_COOKIE pObj;
953
954         pObj = (POS_COOKIE) pAd->OS_Cookie;
955
956         tasklet_kill(&pObj->rx_done_task);
957         tasklet_kill(&pObj->mgmt_dma_done_task);
958         tasklet_kill(&pObj->ac0_dma_done_task);
959         tasklet_kill(&pObj->ac1_dma_done_task);
960         tasklet_kill(&pObj->ac2_dma_done_task);
961         tasklet_kill(&pObj->ac3_dma_done_task);
962         tasklet_kill(&pObj->hcca_dma_done_task);
963         tasklet_kill(&pObj->tbtt_task);
964
965 }
966
967
968 /*
969 ========================================================================
970 Routine Description:
971     Check the chipset vendor/product ID.
972
973 Arguments:
974     _dev_p                              Point to the PCI or USB device
975
976 Return Value:
977     TRUE                                Check ok
978         FALSE                           Check fail
979
980 Note:
981 ========================================================================
982 */
983 BOOLEAN RT28XXChipsetCheck(
984         IN void *_dev_p)
985 {
986         struct usb_interface *intf = (struct usb_interface *)_dev_p;
987         struct usb_device *dev_p = interface_to_usbdev(intf);
988         UINT32 i;
989
990
991         for(i=0; i<rtusb_usb_id_len; i++)
992         {
993                 if (dev_p->descriptor.idVendor == rtusb_usb_id[i].idVendor &&
994                         dev_p->descriptor.idProduct == rtusb_usb_id[i].idProduct)
995                 {
996                         printk("rt2870: idVendor = 0x%x, idProduct = 0x%x\n",
997                                         dev_p->descriptor.idVendor, dev_p->descriptor.idProduct);
998                         break;
999                 }
1000         }
1001
1002         if (i == rtusb_usb_id_len)
1003         {
1004                 printk("rt2870: Error! Device Descriptor not matching!\n");
1005                 return FALSE;
1006         }
1007
1008         return TRUE;
1009 }
1010
1011
1012 /*
1013 ========================================================================
1014 Routine Description:
1015     Init net device structure.
1016
1017 Arguments:
1018     _dev_p                              Point to the PCI or USB device
1019     *net_dev                    Point to the net device
1020         *pAd                            the raxx interface data pointer
1021
1022 Return Value:
1023     TRUE                                Init ok
1024         FALSE                           Init fail
1025
1026 Note:
1027 ========================================================================
1028 */
1029 BOOLEAN RT28XXNetDevInit(
1030         IN void                                 *_dev_p,
1031         IN struct  net_device   *net_dev,
1032         IN RTMP_ADAPTER                 *pAd)
1033 {
1034         struct usb_interface *intf = (struct usb_interface *)_dev_p;
1035         struct usb_device *dev_p = interface_to_usbdev(intf);
1036
1037
1038         pAd->config = &dev_p->config->desc;
1039         return TRUE;
1040 }
1041
1042
1043 /*
1044 ========================================================================
1045 Routine Description:
1046     Init net device structure.
1047
1048 Arguments:
1049     _dev_p                              Point to the PCI or USB device
1050         *pAd                            the raxx interface data pointer
1051
1052 Return Value:
1053     TRUE                                Config ok
1054         FALSE                           Config fail
1055
1056 Note:
1057 ========================================================================
1058 */
1059 BOOLEAN RT28XXProbePostConfig(
1060         IN void                                 *_dev_p,
1061         IN RTMP_ADAPTER                 *pAd,
1062         IN INT32                                interface)
1063 {
1064         struct usb_interface *intf = (struct usb_interface *)_dev_p;
1065         struct usb_host_interface *iface_desc;
1066         ULONG BulkOutIdx;
1067         UINT32 i;
1068
1069
1070         /* get the active interface descriptor */
1071         iface_desc = intf->cur_altsetting;
1072
1073         /* get # of enpoints  */
1074         pAd->NumberOfPipes = iface_desc->desc.bNumEndpoints;
1075         DBGPRINT(RT_DEBUG_TRACE,
1076                         ("NumEndpoints=%d\n", iface_desc->desc.bNumEndpoints));
1077
1078         /* Configure Pipes */
1079         BulkOutIdx = 0;
1080
1081         for(i=0; i<pAd->NumberOfPipes; i++)
1082         {
1083                 if ((iface_desc->endpoint[i].desc.bmAttributes ==
1084                                 USB_ENDPOINT_XFER_BULK) &&
1085                         ((iface_desc->endpoint[i].desc.bEndpointAddress &
1086                                 USB_ENDPOINT_DIR_MASK) == USB_DIR_IN))
1087                 {
1088                         pAd->BulkInEpAddr = iface_desc->endpoint[i].desc.bEndpointAddress;
1089                         pAd->BulkInMaxPacketSize = iface_desc->endpoint[i].desc.wMaxPacketSize;
1090
1091                         DBGPRINT_RAW(RT_DEBUG_TRACE,
1092                                 ("BULK IN MaximumPacketSize = %d\n", pAd->BulkInMaxPacketSize));
1093                         DBGPRINT_RAW(RT_DEBUG_TRACE,
1094                                 ("EP address = 0x%2x\n", iface_desc->endpoint[i].desc.bEndpointAddress));
1095                 }
1096                 else if ((iface_desc->endpoint[i].desc.bmAttributes ==
1097                                         USB_ENDPOINT_XFER_BULK) &&
1098                                 ((iface_desc->endpoint[i].desc.bEndpointAddress &
1099                                         USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT))
1100                 {
1101                         // there are 6 bulk out EP. EP6 highest priority.
1102                         // EP1-4 is EDCA.  EP5 is HCCA.
1103                         pAd->BulkOutEpAddr[BulkOutIdx++] = iface_desc->endpoint[i].desc.bEndpointAddress;
1104                         pAd->BulkOutMaxPacketSize = iface_desc->endpoint[i].desc.wMaxPacketSize;
1105
1106                         DBGPRINT_RAW(RT_DEBUG_TRACE,
1107                                 ("BULK OUT MaximumPacketSize = %d\n", pAd->BulkOutMaxPacketSize));
1108                         DBGPRINT_RAW(RT_DEBUG_TRACE,
1109                                 ("EP address = 0x%2x  \n", iface_desc->endpoint[i].desc.bEndpointAddress));
1110                 }
1111         }
1112
1113         if (!(pAd->BulkInEpAddr && pAd->BulkOutEpAddr[0]))
1114         {
1115                 printk("%s: Could not find both bulk-in and bulk-out endpoints\n", __func__);
1116                 return FALSE;
1117         }
1118
1119         return TRUE;
1120 }
1121
1122
1123 /*
1124 ========================================================================
1125 Routine Description:
1126     Disable DMA.
1127
1128 Arguments:
1129         *pAd                            the raxx interface data pointer
1130
1131 Return Value:
1132         None
1133
1134 Note:
1135 ========================================================================
1136 */
1137 VOID RT28XXDMADisable(
1138         IN RTMP_ADAPTER                 *pAd)
1139 {
1140         // no use
1141 }
1142
1143
1144
1145 /*
1146 ========================================================================
1147 Routine Description:
1148     Enable DMA.
1149
1150 Arguments:
1151         *pAd                            the raxx interface data pointer
1152
1153 Return Value:
1154         None
1155
1156 Note:
1157 ========================================================================
1158 */
1159 VOID RT28XXDMAEnable(
1160         IN RTMP_ADAPTER                 *pAd)
1161 {
1162         WPDMA_GLO_CFG_STRUC     GloCfg;
1163         USB_DMA_CFG_STRUC       UsbCfg;
1164         int                                     i = 0;
1165
1166
1167         RTMP_IO_WRITE32(pAd, MAC_SYS_CTRL, 0x4);
1168         do
1169         {
1170                 RTMP_IO_READ32(pAd, WPDMA_GLO_CFG, &GloCfg.word);
1171                 if ((GloCfg.field.TxDMABusy == 0)  && (GloCfg.field.RxDMABusy == 0))
1172                         break;
1173
1174                 DBGPRINT(RT_DEBUG_TRACE, ("==>  DMABusy\n"));
1175                 RTMPusecDelay(1000);
1176                 i++;
1177         }while ( i <200);
1178
1179
1180         RTMPusecDelay(50);
1181         GloCfg.field.EnTXWriteBackDDONE = 1;
1182         GloCfg.field.EnableRxDMA = 1;
1183         GloCfg.field.EnableTxDMA = 1;
1184         DBGPRINT(RT_DEBUG_TRACE, ("<== WRITE DMA offset 0x208 = 0x%x\n", GloCfg.word));
1185         RTMP_IO_WRITE32(pAd, WPDMA_GLO_CFG, GloCfg.word);
1186
1187         UsbCfg.word = 0;
1188         UsbCfg.field.phyclear = 0;
1189         /* usb version is 1.1,do not use bulk in aggregation */
1190         if (pAd->BulkInMaxPacketSize == 512)
1191                         UsbCfg.field.RxBulkAggEn = 1;
1192         /* for last packet, PBF might use more than limited, so minus 2 to prevent from error */
1193         UsbCfg.field.RxBulkAggLmt = (MAX_RXBULK_SIZE /1024)-3;
1194         UsbCfg.field.RxBulkAggTOut = 0x80; /* 2006-10-18 */
1195         UsbCfg.field.RxBulkEn = 1;
1196         UsbCfg.field.TxBulkEn = 1;
1197
1198         RTUSBWriteMACRegister(pAd, USB_DMA_CFG, UsbCfg.word);
1199
1200 }
1201
1202 /*
1203 ========================================================================
1204 Routine Description:
1205     Write Beacon buffer to Asic.
1206
1207 Arguments:
1208         *pAd                            the raxx interface data pointer
1209
1210 Return Value:
1211         None
1212
1213 Note:
1214 ========================================================================
1215 */
1216 VOID RT28xx_UpdateBeaconToAsic(
1217         IN RTMP_ADAPTER         *pAd,
1218         IN INT                          apidx,
1219         IN ULONG                        FrameLen,
1220         IN ULONG                        UpdatePos)
1221 {
1222         PUCHAR          pBeaconFrame = NULL;
1223         UCHAR                   *ptr;
1224         UINT                    i, padding;
1225         BEACON_SYNC_STRUCT      *pBeaconSync = pAd->CommonCfg.pBeaconSync;
1226         UINT32                  longValue;
1227 //      USHORT                  shortValue;
1228         BOOLEAN                 bBcnReq = FALSE;
1229         UCHAR                   bcn_idx = 0;
1230
1231
1232         if (pBeaconFrame == NULL)
1233         {
1234                 DBGPRINT(RT_DEBUG_ERROR,("pBeaconFrame is NULL!\n"));
1235                 return;
1236         }
1237
1238         if (pBeaconSync == NULL)
1239         {
1240                 DBGPRINT(RT_DEBUG_ERROR,("pBeaconSync is NULL!\n"));
1241                 return;
1242         }
1243
1244         //if ((pAd->WdsTab.Mode == WDS_BRIDGE_MODE) ||
1245         //      ((pAd->ApCfg.MBSSID[apidx].MSSIDDev == NULL) || !(pAd->ApCfg.MBSSID[apidx].MSSIDDev->flags & IFF_UP))
1246         //      )
1247         if (bBcnReq == FALSE)
1248         {
1249                 /* when the ra interface is down, do not send its beacon frame */
1250                 /* clear all zero */
1251                 for(i=0; i<TXWI_SIZE; i+=4) {
1252                         RTMP_IO_WRITE32(pAd, pAd->BeaconOffset[bcn_idx] + i, 0x00);
1253                 }
1254                 pBeaconSync->BeaconBitMap &= (~(BEACON_BITMAP_MASK & (1 << bcn_idx)));
1255                 NdisZeroMemory(pBeaconSync->BeaconTxWI[bcn_idx], TXWI_SIZE);
1256         }
1257         else
1258         {
1259                 ptr = (PUCHAR)&pAd->BeaconTxWI;
1260
1261                 if (NdisEqualMemory(pBeaconSync->BeaconTxWI[bcn_idx], &pAd->BeaconTxWI, TXWI_SIZE) == FALSE)
1262                 {       // If BeaconTxWI changed, we need to rewrite the TxWI for the Beacon frames.
1263                         pBeaconSync->BeaconBitMap &= (~(BEACON_BITMAP_MASK & (1 << bcn_idx)));
1264                         NdisMoveMemory(pBeaconSync->BeaconTxWI[bcn_idx], &pAd->BeaconTxWI, TXWI_SIZE);
1265                 }
1266
1267                 if ((pBeaconSync->BeaconBitMap & (1 << bcn_idx)) != (1 << bcn_idx))
1268                 {
1269                         for (i=0; i<TXWI_SIZE; i+=4)  // 16-byte TXWI field
1270                         {
1271                                 longValue =  *ptr + (*(ptr+1)<<8) + (*(ptr+2)<<16) + (*(ptr+3)<<24);
1272                                 RTMP_IO_WRITE32(pAd, pAd->BeaconOffset[bcn_idx] + i, longValue);
1273                                 ptr += 4;
1274                         }
1275                 }
1276
1277                 ptr = pBeaconSync->BeaconBuf[bcn_idx];
1278                 padding = (FrameLen & 0x01);
1279                 NdisZeroMemory((PUCHAR)(pBeaconFrame + FrameLen), padding);
1280                 FrameLen += padding;
1281                 for (i = 0 ; i < FrameLen /*HW_BEACON_OFFSET*/; i += 2)
1282                 {
1283                         if (NdisEqualMemory(ptr, pBeaconFrame, 2) == FALSE)
1284                         {
1285                                 NdisMoveMemory(ptr, pBeaconFrame, 2);
1286                                 //shortValue = *ptr + (*(ptr+1)<<8);
1287                                 //RTMP_IO_WRITE8(pAd, pAd->BeaconOffset[bcn_idx] + TXWI_SIZE + i, shortValue);
1288                                 RTUSBMultiWrite(pAd, pAd->BeaconOffset[bcn_idx] + TXWI_SIZE + i, ptr, 2);
1289                         }
1290                         ptr +=2;
1291                         pBeaconFrame += 2;
1292                 }
1293
1294                 pBeaconSync->BeaconBitMap |= (1 << bcn_idx);
1295
1296                 // For AP interface, set the DtimBitOn so that we can send Bcast/Mcast frame out after this beacon frame.
1297         }
1298
1299 }
1300
1301
1302 VOID RT2870_BssBeaconStop(
1303         IN RTMP_ADAPTER *pAd)
1304 {
1305         BEACON_SYNC_STRUCT      *pBeaconSync;
1306         int i, offset;
1307         BOOLEAN Cancelled = TRUE;
1308
1309         pBeaconSync = pAd->CommonCfg.pBeaconSync;
1310         if (pBeaconSync && pBeaconSync->EnableBeacon)
1311         {
1312                 INT NumOfBcn;
1313
1314                 NumOfBcn = MAX_MESH_NUM;
1315
1316                 RTMPCancelTimer(&pAd->CommonCfg.BeaconUpdateTimer, &Cancelled);
1317
1318                 for(i=0; i<NumOfBcn; i++)
1319                 {
1320                         NdisZeroMemory(pBeaconSync->BeaconBuf[i], HW_BEACON_OFFSET);
1321                         NdisZeroMemory(pBeaconSync->BeaconTxWI[i], TXWI_SIZE);
1322
1323                         for (offset=0; offset<HW_BEACON_OFFSET; offset+=4)
1324                                 RTMP_IO_WRITE32(pAd, pAd->BeaconOffset[i] + offset, 0x00);
1325
1326                         pBeaconSync->CapabilityInfoLocationInBeacon[i] = 0;
1327                         pBeaconSync->TimIELocationInBeacon[i] = 0;
1328                 }
1329                 pBeaconSync->BeaconBitMap = 0;
1330                 pBeaconSync->DtimBitOn = 0;
1331         }
1332 }
1333
1334
1335 VOID RT2870_BssBeaconStart(
1336         IN RTMP_ADAPTER *pAd)
1337 {
1338         int apidx;
1339         BEACON_SYNC_STRUCT      *pBeaconSync;
1340 //      LARGE_INTEGER   tsfTime, deltaTime;
1341
1342         pBeaconSync = pAd->CommonCfg.pBeaconSync;
1343         if (pBeaconSync && pBeaconSync->EnableBeacon)
1344         {
1345                 INT NumOfBcn;
1346
1347                 NumOfBcn = MAX_MESH_NUM;
1348
1349                 for(apidx=0; apidx<NumOfBcn; apidx++)
1350                 {
1351                         UCHAR CapabilityInfoLocationInBeacon = 0;
1352                         UCHAR TimIELocationInBeacon = 0;
1353
1354                         NdisZeroMemory(pBeaconSync->BeaconBuf[apidx], HW_BEACON_OFFSET);
1355                         pBeaconSync->CapabilityInfoLocationInBeacon[apidx] = CapabilityInfoLocationInBeacon;
1356                         pBeaconSync->TimIELocationInBeacon[apidx] = TimIELocationInBeacon;
1357                         NdisZeroMemory(pBeaconSync->BeaconTxWI[apidx], TXWI_SIZE);
1358                 }
1359                 pBeaconSync->BeaconBitMap = 0;
1360                 pBeaconSync->DtimBitOn = 0;
1361                 pAd->CommonCfg.BeaconUpdateTimer.Repeat = TRUE;
1362
1363                 pAd->CommonCfg.BeaconAdjust = 0;
1364                 pAd->CommonCfg.BeaconFactor = 0xffffffff / (pAd->CommonCfg.BeaconPeriod << 10);
1365                 pAd->CommonCfg.BeaconRemain = (0xffffffff % (pAd->CommonCfg.BeaconPeriod << 10)) + 1;
1366                 printk("RT2870_BssBeaconStart:BeaconFactor=%d, BeaconRemain=%d!\n", pAd->CommonCfg.BeaconFactor, pAd->CommonCfg.BeaconRemain);
1367                 RTMPSetTimer(&pAd->CommonCfg.BeaconUpdateTimer, pAd->CommonCfg.BeaconPeriod);
1368
1369         }
1370 }
1371
1372
1373 VOID RT2870_BssBeaconInit(
1374         IN RTMP_ADAPTER *pAd)
1375 {
1376         BEACON_SYNC_STRUCT      *pBeaconSync;
1377         int i;
1378
1379         NdisAllocMemory(pAd->CommonCfg.pBeaconSync, sizeof(BEACON_SYNC_STRUCT), MEM_ALLOC_FLAG);
1380         if (pAd->CommonCfg.pBeaconSync)
1381         {
1382                 pBeaconSync = pAd->CommonCfg.pBeaconSync;
1383                 NdisZeroMemory(pBeaconSync, sizeof(BEACON_SYNC_STRUCT));
1384                 for(i=0; i < HW_BEACON_MAX_COUNT; i++)
1385                 {
1386                         NdisZeroMemory(pBeaconSync->BeaconBuf[i], HW_BEACON_OFFSET);
1387                         pBeaconSync->CapabilityInfoLocationInBeacon[i] = 0;
1388                         pBeaconSync->TimIELocationInBeacon[i] = 0;
1389                         NdisZeroMemory(pBeaconSync->BeaconTxWI[i], TXWI_SIZE);
1390                 }
1391                 pBeaconSync->BeaconBitMap = 0;
1392
1393                 //RTMPInitTimer(pAd, &pAd->CommonCfg.BeaconUpdateTimer, GET_TIMER_FUNCTION(BeaconUpdateExec), pAd, TRUE);
1394                 pBeaconSync->EnableBeacon = TRUE;
1395         }
1396 }
1397
1398
1399 VOID RT2870_BssBeaconExit(
1400         IN RTMP_ADAPTER *pAd)
1401 {
1402         BEACON_SYNC_STRUCT      *pBeaconSync;
1403         BOOLEAN Cancelled = TRUE;
1404         int i;
1405
1406         if (pAd->CommonCfg.pBeaconSync)
1407         {
1408                 pBeaconSync = pAd->CommonCfg.pBeaconSync;
1409                 pBeaconSync->EnableBeacon = FALSE;
1410                 RTMPCancelTimer(&pAd->CommonCfg.BeaconUpdateTimer, &Cancelled);
1411                 pBeaconSync->BeaconBitMap = 0;
1412
1413                 for(i=0; i<HW_BEACON_MAX_COUNT; i++)
1414                 {
1415                         NdisZeroMemory(pBeaconSync->BeaconBuf[i], HW_BEACON_OFFSET);
1416                         pBeaconSync->CapabilityInfoLocationInBeacon[i] = 0;
1417                         pBeaconSync->TimIELocationInBeacon[i] = 0;
1418                         NdisZeroMemory(pBeaconSync->BeaconTxWI[i], TXWI_SIZE);
1419                 }
1420
1421                 NdisFreeMemory(pAd->CommonCfg.pBeaconSync, HW_BEACON_OFFSET * HW_BEACON_MAX_COUNT, 0);
1422                 pAd->CommonCfg.pBeaconSync = NULL;
1423         }
1424 }
1425
1426 VOID BeaconUpdateExec(
1427     IN PVOID SystemSpecific1,
1428     IN PVOID FunctionContext,
1429     IN PVOID SystemSpecific2,
1430     IN PVOID SystemSpecific3)
1431 {
1432         PRTMP_ADAPTER   pAd = (PRTMP_ADAPTER)FunctionContext;
1433         LARGE_INTEGER   tsfTime_a;//, tsfTime_b, deltaTime_exp, deltaTime_ab;
1434         UINT32                  delta, remain, remain_low, remain_high;
1435 //      BOOLEAN                 positive;
1436
1437         ReSyncBeaconTime(pAd);
1438
1439
1440
1441         RTMP_IO_READ32(pAd, TSF_TIMER_DW0, &tsfTime_a.u.LowPart);
1442         RTMP_IO_READ32(pAd, TSF_TIMER_DW1, &tsfTime_a.u.HighPart);
1443
1444
1445         //positive=getDeltaTime(tsfTime_a, expectedTime, &deltaTime_exp);
1446         remain_high = pAd->CommonCfg.BeaconRemain * tsfTime_a.u.HighPart;
1447         remain_low = tsfTime_a.u.LowPart % (pAd->CommonCfg.BeaconPeriod << 10);
1448         remain = (remain_high + remain_low)%(pAd->CommonCfg.BeaconPeriod << 10);
1449         delta = (pAd->CommonCfg.BeaconPeriod << 10) - remain;
1450
1451         pAd->CommonCfg.BeaconUpdateTimer.TimerValue = (delta >> 10) + 10;
1452
1453 }
1454