2 *************************************************************************
4 * 5F., No.36, Taiyuan St., Jhubei City,
8 * (c) Copyright 2002-2007, Ralink Technology, Inc.
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. *
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. *
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. *
25 *************************************************************************
31 main initialization routines
35 -------- ---------- ----------------------------------------------
36 Name Date Modification logs
37 Jan Lee 01-10-2005 modified
38 Sample Jun/01/07 Merge RT2870 and RT2860 drivers.
41 #include "rt_config.h"
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");
51 MODULE_VERSION(STA_DRIVER_VERSION);
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
59 extern INT __devinit rt28xx_probe(IN void *_dev_p, IN void *_dev_id_p,
60 IN UINT argc, OUT PRTMP_ADAPTER *ppAd);
64 struct usb_device_id rtusb_usb_id[] = RT2870_USB_DEVICES;
65 INT const rtusb_usb_id_len = sizeof(rtusb_usb_id) / sizeof(struct usb_device_id);
66 MODULE_DEVICE_TABLE(usb, rtusb_usb_id);
74 static int rt2870_suspend(struct usb_interface *intf, pm_message_t state);
75 static int rt2870_resume(struct usb_interface *intf);
76 #endif // CONFIG_PM //
78 /**************************************************************************/
79 /**************************************************************************/
80 //tested for kernel 2.6series
81 /**************************************************************************/
82 /**************************************************************************/
83 static int rtusb_probe (struct usb_interface *intf,
84 const struct usb_device_id *id);
85 static void rtusb_disconnect(struct usb_interface *intf);
87 struct usb_driver rtusb_driver = {
90 .disconnect=rtusb_disconnect,
91 .id_table=rtusb_usb_id,
94 suspend: rt2870_suspend,
95 resume: rt2870_resume,
101 VOID RT2860RejectPendingPackets(
102 IN PRTMP_ADAPTER pAd)
105 // clear TxSw packets
108 static int rt2870_suspend(
109 struct usb_interface *intf,
112 struct net_device *net_dev;
113 PRTMP_ADAPTER pAd = usb_get_intfdata(intf);
116 DBGPRINT(RT_DEBUG_TRACE, ("===> rt2870_suspend()\n"));
117 net_dev = pAd->net_dev;
118 netif_device_detach(net_dev);
120 pAd->PM_FlgSuspend = 1;
121 if (netif_running(net_dev)) {
122 RTUSBCancelPendingBulkInIRP(pAd);
123 RTUSBCancelPendingBulkOutIRP(pAd);
125 DBGPRINT(RT_DEBUG_TRACE, ("<=== rt2870_suspend()\n"));
129 static int rt2870_resume(
130 struct usb_interface *intf)
132 struct net_device *net_dev;
133 PRTMP_ADAPTER pAd = usb_get_intfdata(intf);
136 DBGPRINT(RT_DEBUG_TRACE, ("===> rt2870_resume()\n"));
138 pAd->PM_FlgSuspend = 0;
139 net_dev = pAd->net_dev;
140 netif_device_attach(net_dev);
141 netif_start_queue(net_dev);
142 netif_carrier_on(net_dev);
143 netif_wake_queue(net_dev);
145 DBGPRINT(RT_DEBUG_TRACE, ("<=== rt2870_resume()\n"));
148 #endif // CONFIG_PM //
151 // Init driver module
152 INT __init rtusb_init(void)
154 printk("rtusb init --->\n");
155 return usb_register(&rtusb_driver);
158 // Deinit driver module
159 VOID __exit rtusb_exit(void)
161 usb_deregister(&rtusb_driver);
162 printk("<--- rtusb exit\n");
165 module_init(rtusb_init);
166 module_exit(rtusb_exit);
171 /*--------------------------------------------------------------------- */
172 /* function declarations */
173 /*--------------------------------------------------------------------- */
176 ========================================================================
181 *Context the pAd, driver control block pointer
187 ========================================================================
192 PRTMP_ADAPTER pAd = (PRTMP_ADAPTER)Context;
196 pObj = (POS_COOKIE)pAd->OS_Cookie;
198 rtmp_os_thread_init("rt2870MlmeThread", (PVOID)&(pAd->mlmeComplete));
200 while (pAd->mlme_kill == 0)
202 /* lock the device pointers */
203 //down(&(pAd->mlme_semaphore));
204 status = down_interruptible(&(pAd->mlme_semaphore));
206 /* lock the device pointers , need to check if required*/
207 //down(&(pAd->usbdev_semaphore));
209 if (!pAd->PM_FlgSuspend)
212 /* unlock the device pointers */
213 //up(&(pAd->usbdev_semaphore));
216 RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS);
221 /* notify the exit routine that we're actually exiting now
223 * complete()/wait_for_completion() is similar to up()/down(),
224 * except that complete() is safe in the case where the structure
225 * is getting deleted in a parallel mode of execution (i.e. just
226 * after the down() -- that's necessary for the thread-shutdown
229 * complete_and_exit() goes even further than this -- it is safe in
230 * the case that the thread of the caller is going away (not just
231 * the structure) -- this is necessary for the module-remove case.
232 * This is important in preemption kernels, which transfer the flow
233 * of execution immediately upon a complete().
235 DBGPRINT(RT_DEBUG_TRACE,( "<---%s\n",__func__));
237 pObj->MLMEThr_pid = NULL;
239 complete_and_exit (&pAd->mlmeComplete, 0);
246 ========================================================================
248 USB command kernel thread.
251 *Context the pAd, driver control block pointer
257 ========================================================================
262 PRTMP_ADAPTER pAd = (PRTMP_ADAPTER)Context;
266 pObj = (POS_COOKIE)pAd->OS_Cookie;
268 rtmp_os_thread_init("rt2870CmdThread", (PVOID)&(pAd->CmdQComplete));
270 NdisAcquireSpinLock(&pAd->CmdQLock);
271 pAd->CmdQ.CmdQState = RT2870_THREAD_RUNNING;
272 NdisReleaseSpinLock(&pAd->CmdQLock);
274 while (pAd->CmdQ.CmdQState == RT2870_THREAD_RUNNING)
276 /* lock the device pointers */
277 //down(&(pAd->RTUSBCmd_semaphore));
278 status = down_interruptible(&(pAd->RTUSBCmd_semaphore));
280 if (pAd->CmdQ.CmdQState == RT2870_THREAD_STOPED)
285 RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS);
288 /* lock the device pointers , need to check if required*/
289 //down(&(pAd->usbdev_semaphore));
291 if (!pAd->PM_FlgSuspend)
294 /* unlock the device pointers */
295 //up(&(pAd->usbdev_semaphore));
298 if (!pAd->PM_FlgSuspend)
299 { // Clear the CmdQElements.
300 CmdQElmt *pCmdQElmt = NULL;
302 NdisAcquireSpinLock(&pAd->CmdQLock);
303 pAd->CmdQ.CmdQState = RT2870_THREAD_STOPED;
304 while(pAd->CmdQ.size)
306 RTUSBDequeueCmd(&pAd->CmdQ, &pCmdQElmt);
309 if (pCmdQElmt->CmdFromNdis == TRUE)
311 if (pCmdQElmt->buffer != NULL)
312 NdisFreeMemory(pCmdQElmt->buffer, pCmdQElmt->bufferlength, 0);
314 NdisFreeMemory(pCmdQElmt, sizeof(CmdQElmt), 0);
318 if ((pCmdQElmt->buffer != NULL) && (pCmdQElmt->bufferlength != 0))
319 NdisFreeMemory(pCmdQElmt->buffer, pCmdQElmt->bufferlength, 0);
321 NdisFreeMemory(pCmdQElmt, sizeof(CmdQElmt), 0);
327 NdisReleaseSpinLock(&pAd->CmdQLock);
329 /* notify the exit routine that we're actually exiting now
331 * complete()/wait_for_completion() is similar to up()/down(),
332 * except that complete() is safe in the case where the structure
333 * is getting deleted in a parallel mode of execution (i.e. just
334 * after the down() -- that's necessary for the thread-shutdown
337 * complete_and_exit() goes even further than this -- it is safe in
338 * the case that the thread of the caller is going away (not just
339 * the structure) -- this is necessary for the module-remove case.
340 * This is important in preemption kernels, which transfer the flow
341 * of execution immediately upon a complete().
343 DBGPRINT(RT_DEBUG_TRACE,( "<---RTUSBCmdThread\n"));
345 pObj->RTUSBCmdThr_pid = NULL;
347 complete_and_exit (&pAd->CmdQComplete, 0);
353 static void RT2870_TimerQ_Handle(RTMP_ADAPTER *pAd)
356 RALINK_TIMER_STRUCT *pTimer;
357 RT2870_TIMER_ENTRY *pEntry;
358 unsigned long irqFlag;
360 while(!pAd->TimerFunc_kill)
362 // printk("waiting for event!\n");
365 status = down_interruptible(&(pAd->RTUSBTimer_semaphore));
367 if (pAd->TimerQ.status == RT2870_THREAD_STOPED)
371 while(pAd->TimerQ.pQHead)
373 RTMP_IRQ_LOCK(&pAd->TimerQLock, irqFlag);
374 pEntry = pAd->TimerQ.pQHead;
377 pTimer = pEntry->pRaTimer;
380 pAd->TimerQ.pQHead = pEntry->pNext;
381 if (pEntry == pAd->TimerQ.pQTail)
382 pAd->TimerQ.pQTail = NULL;
384 // return this queue entry to timerQFreeList.
385 pEntry->pNext = pAd->TimerQ.pQPollFreeList;
386 pAd->TimerQ.pQPollFreeList = pEntry;
388 RTMP_IRQ_UNLOCK(&pAd->TimerQLock, irqFlag);
392 if (pTimer->handle != NULL)
393 if (!pAd->PM_FlgSuspend)
394 pTimer->handle(NULL, (PVOID) pTimer->cookie, NULL, pTimer);
395 if ((pTimer->Repeat) && (pTimer->State == FALSE))
396 RTMP_OS_Add_Timer(&pTimer->TimerObj, pTimer->TimerValue);
402 pAd->TimerQ.status = RT2870_THREAD_STOPED;
403 RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS);
411 IN OUT PVOID Context)
416 pAd = (PRTMP_ADAPTER)Context;
417 pObj = (POS_COOKIE) pAd->OS_Cookie;
419 rtmp_os_thread_init("rt2870TimerQHandle", (PVOID)&(pAd->TimerQComplete));
421 RT2870_TimerQ_Handle(pAd);
423 /* notify the exit routine that we're actually exiting now
425 * complete()/wait_for_completion() is similar to up()/down(),
426 * except that complete() is safe in the case where the structure
427 * is getting deleted in a parallel mode of execution (i.e. just
428 * after the down() -- that's necessary for the thread-shutdown
431 * complete_and_exit() goes even further than this -- it is safe in
432 * the case that the thread of the caller is going away (not just
433 * the structure) -- this is necessary for the module-remove case.
434 * This is important in preemption kernels, which transfer the flow
435 * of execution immediately upon a complete().
437 DBGPRINT(RT_DEBUG_TRACE,( "<---%s\n",__func__));
439 pObj->TimerQThr_pid = NULL;
441 complete_and_exit(&pAd->TimerQComplete, 0);
447 RT2870_TIMER_ENTRY *RT2870_TimerQ_Insert(
448 IN RTMP_ADAPTER *pAd,
449 IN RALINK_TIMER_STRUCT *pTimer)
451 RT2870_TIMER_ENTRY *pQNode = NULL, *pQTail;
452 unsigned long irqFlags;
455 RTMP_IRQ_LOCK(&pAd->TimerQLock, irqFlags);
456 if (pAd->TimerQ.status & RT2870_THREAD_CAN_DO_INSERT)
458 if(pAd->TimerQ.pQPollFreeList)
460 pQNode = pAd->TimerQ.pQPollFreeList;
461 pAd->TimerQ.pQPollFreeList = pQNode->pNext;
463 pQNode->pRaTimer = pTimer;
464 pQNode->pNext = NULL;
466 pQTail = pAd->TimerQ.pQTail;
467 if (pAd->TimerQ.pQTail != NULL)
468 pQTail->pNext = pQNode;
469 pAd->TimerQ.pQTail = pQNode;
470 if (pAd->TimerQ.pQHead == NULL)
471 pAd->TimerQ.pQHead = pQNode;
473 RTMP_IRQ_UNLOCK(&pAd->TimerQLock, irqFlags);
476 up(&pAd->RTUSBTimer_semaphore);
477 //wake_up(&timerWaitQ);
481 RTMP_IRQ_UNLOCK(&pAd->TimerQLock, irqFlags);
487 BOOLEAN RT2870_TimerQ_Remove(
488 IN RTMP_ADAPTER *pAd,
489 IN RALINK_TIMER_STRUCT *pTimer)
491 RT2870_TIMER_ENTRY *pNode, *pPrev = NULL;
492 unsigned long irqFlags;
494 RTMP_IRQ_LOCK(&pAd->TimerQLock, irqFlags);
495 if (pAd->TimerQ.status >= RT2870_THREAD_INITED)
497 pNode = pAd->TimerQ.pQHead;
500 if (pNode->pRaTimer == pTimer)
503 pNode = pNode->pNext;
506 // Now move it to freeList queue.
509 if (pNode == pAd->TimerQ.pQHead)
510 pAd->TimerQ.pQHead = pNode->pNext;
511 if (pNode == pAd->TimerQ.pQTail)
512 pAd->TimerQ.pQTail = pPrev;
514 pPrev->pNext = pNode->pNext;
516 // return this queue entry to timerQFreeList.
517 pNode->pNext = pAd->TimerQ.pQPollFreeList;
518 pAd->TimerQ.pQPollFreeList = pNode;
521 RTMP_IRQ_UNLOCK(&pAd->TimerQLock, irqFlags);
527 void RT2870_TimerQ_Exit(RTMP_ADAPTER *pAd)
529 RT2870_TIMER_ENTRY *pTimerQ;
530 unsigned long irqFlags;
532 RTMP_IRQ_LOCK(&pAd->TimerQLock, irqFlags);
533 while (pAd->TimerQ.pQHead)
535 pTimerQ = pAd->TimerQ.pQHead;
536 pAd->TimerQ.pQHead = pTimerQ->pNext;
539 pAd->TimerQ.pQPollFreeList = NULL;
540 os_free_mem(pAd, pAd->TimerQ.pTimerQPoll);
541 pAd->TimerQ.pQTail = NULL;
542 pAd->TimerQ.pQHead = NULL;
543 pAd->TimerQ.status = RT2870_THREAD_STOPED;
544 RTMP_IRQ_UNLOCK(&pAd->TimerQLock, irqFlags);
549 void RT2870_TimerQ_Init(RTMP_ADAPTER *pAd)
552 RT2870_TIMER_ENTRY *pQNode, *pEntry;
553 unsigned long irqFlags;
555 NdisAllocateSpinLock(&pAd->TimerQLock);
557 RTMP_IRQ_LOCK(&pAd->TimerQLock, irqFlags);
558 NdisZeroMemory(&pAd->TimerQ, sizeof(pAd->TimerQ));
559 //InterlockedExchange(&pAd->TimerQ.count, 0);
561 /* Initialise the wait q head */
562 //init_waitqueue_head(&timerWaitQ);
564 os_alloc_mem(pAd, &pAd->TimerQ.pTimerQPoll, sizeof(RT2870_TIMER_ENTRY) * TIMER_QUEUE_SIZE_MAX);
565 if (pAd->TimerQ.pTimerQPoll)
568 pQNode = (RT2870_TIMER_ENTRY *)pAd->TimerQ.pTimerQPoll;
569 for (i = 0 ;i <TIMER_QUEUE_SIZE_MAX; i++)
571 pQNode->pNext = pEntry;
575 pAd->TimerQ.pQPollFreeList = pEntry;
576 pAd->TimerQ.pQHead = NULL;
577 pAd->TimerQ.pQTail = NULL;
578 pAd->TimerQ.status = RT2870_THREAD_INITED;
580 RTMP_IRQ_UNLOCK(&pAd->TimerQLock, irqFlags);
584 VOID RT2870_WatchDog(IN RTMP_ADAPTER *pAd)
586 PHT_TX_CONTEXT pHTTXContext;
590 BOOLEAN needDumpSeq = FALSE;
595 RTMP_IO_READ32(pAd, TXRXQ_PCNT, &MACValue);
596 if ((MACValue & 0xff) !=0 )
598 DBGPRINT(RT_DEBUG_TRACE, ("TX QUEUE 0 Not EMPTY(Value=0x%0x). !!!!!!!!!!!!!!!\n", MACValue));
599 RTMP_IO_WRITE32(pAd, PBF_CFG, 0xf40012);
600 while((MACValue &0xff) != 0 && (idx++ < 10))
602 RTMP_IO_READ32(pAd, TXRXQ_PCNT, &MACValue);
605 RTMP_IO_WRITE32(pAd, PBF_CFG, 0xf40006);
608 //PS packets use HCCA queue when dequeue from PS unicast queue (WiFi WPA2 MA9_DT1 for Marvell B STA)
611 if ((MACValue & 0xff00) !=0 )
613 DBGPRINT(RT_DEBUG_TRACE, ("TX QUEUE 1 Not EMPTY(Value=0x%0x). !!!!!!!!!!!!!!!\n", MACValue));
614 RTMP_IO_WRITE32(pAd, PBF_CFG, 0xf4000a);
615 while((MACValue &0xff00) != 0 && (idx++ < 10))
617 RTMP_IO_READ32(pAd, TXRXQ_PCNT, &MACValue);
620 RTMP_IO_WRITE32(pAd, PBF_CFG, 0xf40006);
624 if (pAd->watchDogRxOverFlowCnt >= 2)
626 DBGPRINT(RT_DEBUG_TRACE, ("Maybe the Rx Bulk-In hanged! Cancel the pending Rx bulks request!\n"));
627 if ((!RTMP_TEST_FLAG(pAd, (fRTMP_ADAPTER_RESET_IN_PROGRESS |
628 fRTMP_ADAPTER_BULKIN_RESET |
629 fRTMP_ADAPTER_HALT_IN_PROGRESS |
630 fRTMP_ADAPTER_NIC_NOT_EXIST))))
632 DBGPRINT(RT_DEBUG_TRACE, ("Call CMDTHREAD_RESET_BULK_IN to cancel the pending Rx Bulk!\n"));
633 RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_BULKIN_RESET);
634 RTUSBEnqueueInternalCmd(pAd, CMDTHREAD_RESET_BULK_IN, NULL, 0);
637 pAd->watchDogRxOverFlowCnt = 0;
641 for (idx = 0; idx < NUM_OF_TX_RING; idx++)
645 RTMP_IRQ_LOCK(&pAd->BulkOutLock[idx], irqFlags);
646 if ((pAd->BulkOutPending[idx] == TRUE) && pAd->watchDogTxPendingCnt)
648 pAd->watchDogTxPendingCnt[idx]++;
650 if ((pAd->watchDogTxPendingCnt[idx] > 2) &&
651 (!RTMP_TEST_FLAG(pAd, (fRTMP_ADAPTER_RESET_IN_PROGRESS | fRTMP_ADAPTER_HALT_IN_PROGRESS | fRTMP_ADAPTER_NIC_NOT_EXIST | fRTMP_ADAPTER_BULKOUT_RESET)))
654 // FIXME: Following code just support single bulk out. If you wanna support multiple bulk out. Modify it!
655 pHTTXContext = (PHT_TX_CONTEXT)(&pAd->TxContext[idx]);
656 if (pHTTXContext->IRPPending)
657 { // Check TxContext.
658 pUrb = pHTTXContext->pUrb;
660 else if (idx == MGMTPIPEIDX)
662 PTX_CONTEXT pMLMEContext, pNULLContext, pPsPollContext;
665 pMLMEContext = (PTX_CONTEXT)(pAd->MgmtRing.Cell[pAd->MgmtRing.TxDmaIdx].AllocVa);
666 pPsPollContext = (PTX_CONTEXT)(&pAd->PsPollContext);
667 pNULLContext = (PTX_CONTEXT)(&pAd->NullContext);
669 if (pMLMEContext->IRPPending)
671 ASSERT(pMLMEContext->IRPPending);
672 pUrb = pMLMEContext->pUrb;
674 else if (pNULLContext->IRPPending)
676 ASSERT(pNULLContext->IRPPending);
677 pUrb = pNULLContext->pUrb;
679 else if (pPsPollContext->IRPPending)
681 ASSERT(pPsPollContext->IRPPending);
682 pUrb = pPsPollContext->pUrb;
686 RTMP_IRQ_UNLOCK(&pAd->BulkOutLock[idx], irqFlags);
688 DBGPRINT(RT_DEBUG_TRACE, ("Maybe the Tx Bulk-Out hanged! Cancel the pending Tx bulks request of idx(%d)!\n", idx));
691 DBGPRINT(RT_DEBUG_TRACE, ("Unlink the pending URB!\n"));
693 RTUSB_UNLINK_URB(pUrb);
694 // Sleep 200 microseconds to give cancellation time to work
700 DBGPRINT(RT_DEBUG_ERROR, ("Unkonw bulkOut URB maybe hanged!!!!!!!!!!!!\n"));
705 RTMP_IRQ_UNLOCK(&pAd->BulkOutLock[idx], irqFlags);
710 RTMP_IRQ_UNLOCK(&pAd->BulkOutLock[idx], irqFlags);
714 // For Sigma debug, dump the ba_reordering sequence.
715 if((needDumpSeq == TRUE) && (pAd->CommonCfg.bDisableReordering == 0))
718 PBA_REC_ENTRY pBAEntry = NULL;
720 struct reordering_mpdu *mpdu_blk;
722 Idx = pAd->MacTab.Content[BSSID_WCID].BARecWcidArray[0];
724 pBAEntry = &pAd->BATable.BARecEntry[Idx];
725 if((pBAEntry->list.qlen > 0) && (pBAEntry->list.next != NULL))
727 DBGPRINT(RT_DEBUG_TRACE, ("NICUpdateRawCounters():The Queueing pkt in reordering buffer:\n"));
728 NdisAcquireSpinLock(&pBAEntry->RxReRingLock);
729 mpdu_blk = pBAEntry->list.next;
732 DBGPRINT(RT_DEBUG_TRACE, ("\t%d:Seq-%d, bAMSDU-%d!\n", count, mpdu_blk->Sequence, mpdu_blk->bAMSDU));
733 mpdu_blk = mpdu_blk->next;
737 DBGPRINT(RT_DEBUG_TRACE, ("\npBAEntry->LastIndSeq=%d!\n", pBAEntry->LastIndSeq));
738 NdisReleaseSpinLock(&pBAEntry->RxReRingLock);
744 ========================================================================
746 Release allocated resources.
749 *dev Point to the PCI or USB device
750 pAd driver control block pointer
756 ========================================================================
758 static void _rtusb_disconnect(struct usb_device *dev, PRTMP_ADAPTER pAd)
760 struct net_device *net_dev = NULL;
763 DBGPRINT(RT_DEBUG_ERROR, ("rtusb_disconnect: unregister usbnet usb-%s-%s\n",
764 dev->bus->bus_name, dev->devpath));
769 printk("rtusb_disconnect: pAd == NULL!\n");
772 RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST);
776 // for debug, wait to show some messages to /proc system
782 net_dev = pAd->net_dev;
783 if (pAd->net_dev != NULL)
785 printk("rtusb_disconnect: unregister_netdev(), dev->name=%s!\n", net_dev->name);
786 unregister_netdev (pAd->net_dev);
789 flush_scheduled_work();
792 // free net_device memory
793 free_netdev(net_dev);
795 // free adapter memory
796 RTMPFreeAdapter(pAd);
798 // release a use of the usb device structure
802 DBGPRINT(RT_DEBUG_ERROR, (" RTUSB disconnect successfully\n"));
807 ========================================================================
809 Probe RT28XX chipset.
812 *dev Point to the PCI or USB device
814 *id_table Point to the PCI or USB device ID
820 ========================================================================
822 static int rtusb_probe (struct usb_interface *intf,
823 const struct usb_device_id *id)
826 return (int)rt28xx_probe((void *)intf, (void *)id, 0, &pAd);
830 static void rtusb_disconnect(struct usb_interface *intf)
832 struct usb_device *dev = interface_to_usbdev(intf);
836 pAd = usb_get_intfdata(intf);
837 usb_set_intfdata(intf, NULL);
839 _rtusb_disconnect(dev, pAd);
844 ========================================================================
846 Close kernel threads.
849 *pAd the raxx interface data pointer
855 ========================================================================
857 VOID RT28xxThreadTerminate(
858 IN RTMP_ADAPTER *pAd)
860 POS_COOKIE pObj = (POS_COOKIE) pAd->OS_Cookie;
864 // Sleep 50 milliseconds so pending io might finish normally
865 RTMPusecDelay(50000);
867 // We want to wait until all pending receives and sends to the
868 // device object. We cancel any
869 // irps. Wait until sends and receives have stopped.
870 RTUSBCancelPendingIRPs(pAd);
873 if (pObj->MLMEThr_pid)
875 printk("Terminate the MLMEThr_pid=%d!\n", pid_nr(pObj->MLMEThr_pid));
878 //RT28XX_MLME_HANDLER(pAd);
880 ret = kill_pid(pObj->MLMEThr_pid, SIGTERM, 1);
883 printk (KERN_WARNING "%s: unable to Mlme thread, pid=%d, ret=%d!\n",
884 pAd->net_dev->name, pid_nr(pObj->MLMEThr_pid), ret);
888 //wait_for_completion (&pAd->notify);
889 wait_for_completion (&pAd->mlmeComplete);
890 pObj->MLMEThr_pid = NULL;
894 if (pObj->RTUSBCmdThr_pid >= 0)
896 printk("Terminate the RTUSBCmdThr_pid=%d!\n", pid_nr(pObj->RTUSBCmdThr_pid));
898 NdisAcquireSpinLock(&pAd->CmdQLock);
899 pAd->CmdQ.CmdQState = RT2870_THREAD_STOPED;
900 NdisReleaseSpinLock(&pAd->CmdQLock);
903 ret = kill_pid(pObj->RTUSBCmdThr_pid, SIGTERM, 1);
906 printk(KERN_WARNING "%s: unable to RTUSBCmd thread, pid=%d, ret=%d!\n",
907 pAd->net_dev->name, pid_nr(pObj->RTUSBCmdThr_pid), ret);
911 //wait_for_completion (&pAd->notify);
912 wait_for_completion (&pAd->CmdQComplete);
913 pObj->RTUSBCmdThr_pid = NULL;
916 if (pObj->TimerQThr_pid >= 0)
918 POS_COOKIE pObj = (POS_COOKIE)pAd->OS_Cookie;
920 printk("Terminate the TimerQThr_pid=%d!\n", pid_nr(pObj->TimerQThr_pid));
922 pAd->TimerFunc_kill = 1;
924 ret = kill_pid(pObj->TimerQThr_pid, SIGTERM, 1);
927 printk(KERN_WARNING "%s: unable to stop TimerQThread, pid=%d, ret=%d!\n",
928 pAd->net_dev->name, pid_nr(pObj->TimerQThr_pid), ret);
932 printk("wait_for_completion TimerQThr\n");
933 wait_for_completion(&pAd->TimerQComplete);
934 pObj->TimerQThr_pid = NULL;
939 pAd->CmdQ.CmdQState = RT2870_THREAD_UNKNOWN;
940 pAd->TimerFunc_kill = 0;
944 void kill_thread_task(IN PRTMP_ADAPTER pAd)
948 pObj = (POS_COOKIE) pAd->OS_Cookie;
950 tasklet_kill(&pObj->rx_done_task);
951 tasklet_kill(&pObj->mgmt_dma_done_task);
952 tasklet_kill(&pObj->ac0_dma_done_task);
953 tasklet_kill(&pObj->ac1_dma_done_task);
954 tasklet_kill(&pObj->ac2_dma_done_task);
955 tasklet_kill(&pObj->ac3_dma_done_task);
956 tasklet_kill(&pObj->hcca_dma_done_task);
957 tasklet_kill(&pObj->tbtt_task);
963 ========================================================================
965 Check the chipset vendor/product ID.
968 _dev_p Point to the PCI or USB device
975 ========================================================================
977 BOOLEAN RT28XXChipsetCheck(
980 struct usb_interface *intf = (struct usb_interface *)_dev_p;
981 struct usb_device *dev_p = interface_to_usbdev(intf);
985 for(i=0; i<rtusb_usb_id_len; i++)
987 if (dev_p->descriptor.idVendor == rtusb_usb_id[i].idVendor &&
988 dev_p->descriptor.idProduct == rtusb_usb_id[i].idProduct)
990 printk("rt2870: idVendor = 0x%x, idProduct = 0x%x\n",
991 dev_p->descriptor.idVendor, dev_p->descriptor.idProduct);
996 if (i == rtusb_usb_id_len)
998 printk("rt2870: Error! Device Descriptor not matching!\n");
1007 ========================================================================
1008 Routine Description:
1009 Init net device structure.
1012 _dev_p Point to the PCI or USB device
1013 *net_dev Point to the net device
1014 *pAd the raxx interface data pointer
1021 ========================================================================
1023 BOOLEAN RT28XXNetDevInit(
1025 IN struct net_device *net_dev,
1026 IN RTMP_ADAPTER *pAd)
1028 struct usb_interface *intf = (struct usb_interface *)_dev_p;
1029 struct usb_device *dev_p = interface_to_usbdev(intf);
1032 pAd->config = &dev_p->config->desc;
1038 ========================================================================
1039 Routine Description:
1040 Init net device structure.
1043 _dev_p Point to the PCI or USB device
1044 *pAd the raxx interface data pointer
1051 ========================================================================
1053 BOOLEAN RT28XXProbePostConfig(
1055 IN RTMP_ADAPTER *pAd,
1058 struct usb_interface *intf = (struct usb_interface *)_dev_p;
1059 struct usb_host_interface *iface_desc;
1064 /* get the active interface descriptor */
1065 iface_desc = intf->cur_altsetting;
1067 /* get # of enpoints */
1068 pAd->NumberOfPipes = iface_desc->desc.bNumEndpoints;
1069 DBGPRINT(RT_DEBUG_TRACE,
1070 ("NumEndpoints=%d\n", iface_desc->desc.bNumEndpoints));
1072 /* Configure Pipes */
1075 for(i=0; i<pAd->NumberOfPipes; i++)
1077 if ((iface_desc->endpoint[i].desc.bmAttributes ==
1078 USB_ENDPOINT_XFER_BULK) &&
1079 ((iface_desc->endpoint[i].desc.bEndpointAddress &
1080 USB_ENDPOINT_DIR_MASK) == USB_DIR_IN))
1082 pAd->BulkInEpAddr = iface_desc->endpoint[i].desc.bEndpointAddress;
1083 pAd->BulkInMaxPacketSize = iface_desc->endpoint[i].desc.wMaxPacketSize;
1085 DBGPRINT_RAW(RT_DEBUG_TRACE,
1086 ("BULK IN MaximumPacketSize = %d\n", pAd->BulkInMaxPacketSize));
1087 DBGPRINT_RAW(RT_DEBUG_TRACE,
1088 ("EP address = 0x%2x\n", iface_desc->endpoint[i].desc.bEndpointAddress));
1090 else if ((iface_desc->endpoint[i].desc.bmAttributes ==
1091 USB_ENDPOINT_XFER_BULK) &&
1092 ((iface_desc->endpoint[i].desc.bEndpointAddress &
1093 USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT))
1095 // there are 6 bulk out EP. EP6 highest priority.
1096 // EP1-4 is EDCA. EP5 is HCCA.
1097 pAd->BulkOutEpAddr[BulkOutIdx++] = iface_desc->endpoint[i].desc.bEndpointAddress;
1098 pAd->BulkOutMaxPacketSize = iface_desc->endpoint[i].desc.wMaxPacketSize;
1100 DBGPRINT_RAW(RT_DEBUG_TRACE,
1101 ("BULK OUT MaximumPacketSize = %d\n", pAd->BulkOutMaxPacketSize));
1102 DBGPRINT_RAW(RT_DEBUG_TRACE,
1103 ("EP address = 0x%2x \n", iface_desc->endpoint[i].desc.bEndpointAddress));
1107 if (!(pAd->BulkInEpAddr && pAd->BulkOutEpAddr[0]))
1109 printk("%s: Could not find both bulk-in and bulk-out endpoints\n", __func__);
1118 ========================================================================
1119 Routine Description:
1123 *pAd the raxx interface data pointer
1129 ========================================================================
1131 VOID RT28XXDMADisable(
1132 IN RTMP_ADAPTER *pAd)
1140 ========================================================================
1141 Routine Description:
1145 *pAd the raxx interface data pointer
1151 ========================================================================
1153 VOID RT28XXDMAEnable(
1154 IN RTMP_ADAPTER *pAd)
1156 WPDMA_GLO_CFG_STRUC GloCfg;
1157 USB_DMA_CFG_STRUC UsbCfg;
1161 RTMP_IO_WRITE32(pAd, MAC_SYS_CTRL, 0x4);
1164 RTMP_IO_READ32(pAd, WPDMA_GLO_CFG, &GloCfg.word);
1165 if ((GloCfg.field.TxDMABusy == 0) && (GloCfg.field.RxDMABusy == 0))
1168 DBGPRINT(RT_DEBUG_TRACE, ("==> DMABusy\n"));
1169 RTMPusecDelay(1000);
1175 GloCfg.field.EnTXWriteBackDDONE = 1;
1176 GloCfg.field.EnableRxDMA = 1;
1177 GloCfg.field.EnableTxDMA = 1;
1178 DBGPRINT(RT_DEBUG_TRACE, ("<== WRITE DMA offset 0x208 = 0x%x\n", GloCfg.word));
1179 RTMP_IO_WRITE32(pAd, WPDMA_GLO_CFG, GloCfg.word);
1182 UsbCfg.field.phyclear = 0;
1183 /* usb version is 1.1,do not use bulk in aggregation */
1184 if (pAd->BulkInMaxPacketSize == 512)
1185 UsbCfg.field.RxBulkAggEn = 1;
1186 /* for last packet, PBF might use more than limited, so minus 2 to prevent from error */
1187 UsbCfg.field.RxBulkAggLmt = (MAX_RXBULK_SIZE /1024)-3;
1188 UsbCfg.field.RxBulkAggTOut = 0x80; /* 2006-10-18 */
1189 UsbCfg.field.RxBulkEn = 1;
1190 UsbCfg.field.TxBulkEn = 1;
1192 RTUSBWriteMACRegister(pAd, USB_DMA_CFG, UsbCfg.word);
1197 ========================================================================
1198 Routine Description:
1199 Write Beacon buffer to Asic.
1202 *pAd the raxx interface data pointer
1208 ========================================================================
1210 VOID RT28xx_UpdateBeaconToAsic(
1211 IN RTMP_ADAPTER *pAd,
1216 PUCHAR pBeaconFrame = NULL;
1219 BEACON_SYNC_STRUCT *pBeaconSync = pAd->CommonCfg.pBeaconSync;
1221 BOOLEAN bBcnReq = FALSE;
1225 if (pBeaconFrame == NULL)
1227 DBGPRINT(RT_DEBUG_ERROR,("pBeaconFrame is NULL!\n"));
1231 if (pBeaconSync == NULL)
1233 DBGPRINT(RT_DEBUG_ERROR,("pBeaconSync is NULL!\n"));
1237 //if ((pAd->WdsTab.Mode == WDS_BRIDGE_MODE) ||
1238 // ((pAd->ApCfg.MBSSID[apidx].MSSIDDev == NULL) || !(pAd->ApCfg.MBSSID[apidx].MSSIDDev->flags & IFF_UP))
1240 if (bBcnReq == FALSE)
1242 /* when the ra interface is down, do not send its beacon frame */
1243 /* clear all zero */
1244 for(i=0; i<TXWI_SIZE; i+=4) {
1245 RTMP_IO_WRITE32(pAd, pAd->BeaconOffset[bcn_idx] + i, 0x00);
1247 pBeaconSync->BeaconBitMap &= (~(BEACON_BITMAP_MASK & (1 << bcn_idx)));
1248 NdisZeroMemory(pBeaconSync->BeaconTxWI[bcn_idx], TXWI_SIZE);
1252 ptr = (PUCHAR)&pAd->BeaconTxWI;
1254 if (NdisEqualMemory(pBeaconSync->BeaconTxWI[bcn_idx], &pAd->BeaconTxWI, TXWI_SIZE) == FALSE)
1255 { // If BeaconTxWI changed, we need to rewrite the TxWI for the Beacon frames.
1256 pBeaconSync->BeaconBitMap &= (~(BEACON_BITMAP_MASK & (1 << bcn_idx)));
1257 NdisMoveMemory(pBeaconSync->BeaconTxWI[bcn_idx], &pAd->BeaconTxWI, TXWI_SIZE);
1260 if ((pBeaconSync->BeaconBitMap & (1 << bcn_idx)) != (1 << bcn_idx))
1262 for (i=0; i<TXWI_SIZE; i+=4) // 16-byte TXWI field
1264 longValue = *ptr + (*(ptr+1)<<8) + (*(ptr+2)<<16) + (*(ptr+3)<<24);
1265 RTMP_IO_WRITE32(pAd, pAd->BeaconOffset[bcn_idx] + i, longValue);
1270 ptr = pBeaconSync->BeaconBuf[bcn_idx];
1271 padding = (FrameLen & 0x01);
1272 NdisZeroMemory((PUCHAR)(pBeaconFrame + FrameLen), padding);
1273 FrameLen += padding;
1274 for (i = 0 ; i < FrameLen /*HW_BEACON_OFFSET*/; i += 2)
1276 if (NdisEqualMemory(ptr, pBeaconFrame, 2) == FALSE)
1278 NdisMoveMemory(ptr, pBeaconFrame, 2);
1279 //shortValue = *ptr + (*(ptr+1)<<8);
1280 //RTMP_IO_WRITE8(pAd, pAd->BeaconOffset[bcn_idx] + TXWI_SIZE + i, shortValue);
1281 RTUSBMultiWrite(pAd, pAd->BeaconOffset[bcn_idx] + TXWI_SIZE + i, ptr, 2);
1287 pBeaconSync->BeaconBitMap |= (1 << bcn_idx);
1289 // For AP interface, set the DtimBitOn so that we can send Bcast/Mcast frame out after this beacon frame.
1295 VOID RT2870_BssBeaconStop(
1296 IN RTMP_ADAPTER *pAd)
1298 BEACON_SYNC_STRUCT *pBeaconSync;
1300 BOOLEAN Cancelled = TRUE;
1302 pBeaconSync = pAd->CommonCfg.pBeaconSync;
1303 if (pBeaconSync && pBeaconSync->EnableBeacon)
1307 NumOfBcn = MAX_MESH_NUM;
1309 RTMPCancelTimer(&pAd->CommonCfg.BeaconUpdateTimer, &Cancelled);
1311 for(i=0; i<NumOfBcn; i++)
1313 NdisZeroMemory(pBeaconSync->BeaconBuf[i], HW_BEACON_OFFSET);
1314 NdisZeroMemory(pBeaconSync->BeaconTxWI[i], TXWI_SIZE);
1316 for (offset=0; offset<HW_BEACON_OFFSET; offset+=4)
1317 RTMP_IO_WRITE32(pAd, pAd->BeaconOffset[i] + offset, 0x00);
1319 pBeaconSync->CapabilityInfoLocationInBeacon[i] = 0;
1320 pBeaconSync->TimIELocationInBeacon[i] = 0;
1322 pBeaconSync->BeaconBitMap = 0;
1323 pBeaconSync->DtimBitOn = 0;
1328 VOID RT2870_BssBeaconStart(
1329 IN RTMP_ADAPTER *pAd)
1332 BEACON_SYNC_STRUCT *pBeaconSync;
1333 // LARGE_INTEGER tsfTime, deltaTime;
1335 pBeaconSync = pAd->CommonCfg.pBeaconSync;
1336 if (pBeaconSync && pBeaconSync->EnableBeacon)
1340 NumOfBcn = MAX_MESH_NUM;
1342 for(apidx=0; apidx<NumOfBcn; apidx++)
1344 UCHAR CapabilityInfoLocationInBeacon = 0;
1345 UCHAR TimIELocationInBeacon = 0;
1347 NdisZeroMemory(pBeaconSync->BeaconBuf[apidx], HW_BEACON_OFFSET);
1348 pBeaconSync->CapabilityInfoLocationInBeacon[apidx] = CapabilityInfoLocationInBeacon;
1349 pBeaconSync->TimIELocationInBeacon[apidx] = TimIELocationInBeacon;
1350 NdisZeroMemory(pBeaconSync->BeaconTxWI[apidx], TXWI_SIZE);
1352 pBeaconSync->BeaconBitMap = 0;
1353 pBeaconSync->DtimBitOn = 0;
1354 pAd->CommonCfg.BeaconUpdateTimer.Repeat = TRUE;
1356 pAd->CommonCfg.BeaconAdjust = 0;
1357 pAd->CommonCfg.BeaconFactor = 0xffffffff / (pAd->CommonCfg.BeaconPeriod << 10);
1358 pAd->CommonCfg.BeaconRemain = (0xffffffff % (pAd->CommonCfg.BeaconPeriod << 10)) + 1;
1359 printk("RT2870_BssBeaconStart:BeaconFactor=%d, BeaconRemain=%d!\n", pAd->CommonCfg.BeaconFactor, pAd->CommonCfg.BeaconRemain);
1360 RTMPSetTimer(&pAd->CommonCfg.BeaconUpdateTimer, pAd->CommonCfg.BeaconPeriod);
1366 VOID RT2870_BssBeaconInit(
1367 IN RTMP_ADAPTER *pAd)
1369 BEACON_SYNC_STRUCT *pBeaconSync;
1372 NdisAllocMemory(pAd->CommonCfg.pBeaconSync, sizeof(BEACON_SYNC_STRUCT), MEM_ALLOC_FLAG);
1373 if (pAd->CommonCfg.pBeaconSync)
1375 pBeaconSync = pAd->CommonCfg.pBeaconSync;
1376 NdisZeroMemory(pBeaconSync, sizeof(BEACON_SYNC_STRUCT));
1377 for(i=0; i < HW_BEACON_MAX_COUNT; i++)
1379 NdisZeroMemory(pBeaconSync->BeaconBuf[i], HW_BEACON_OFFSET);
1380 pBeaconSync->CapabilityInfoLocationInBeacon[i] = 0;
1381 pBeaconSync->TimIELocationInBeacon[i] = 0;
1382 NdisZeroMemory(pBeaconSync->BeaconTxWI[i], TXWI_SIZE);
1384 pBeaconSync->BeaconBitMap = 0;
1386 //RTMPInitTimer(pAd, &pAd->CommonCfg.BeaconUpdateTimer, GET_TIMER_FUNCTION(BeaconUpdateExec), pAd, TRUE);
1387 pBeaconSync->EnableBeacon = TRUE;
1392 VOID RT2870_BssBeaconExit(
1393 IN RTMP_ADAPTER *pAd)
1395 BEACON_SYNC_STRUCT *pBeaconSync;
1396 BOOLEAN Cancelled = TRUE;
1399 if (pAd->CommonCfg.pBeaconSync)
1401 pBeaconSync = pAd->CommonCfg.pBeaconSync;
1402 pBeaconSync->EnableBeacon = FALSE;
1403 RTMPCancelTimer(&pAd->CommonCfg.BeaconUpdateTimer, &Cancelled);
1404 pBeaconSync->BeaconBitMap = 0;
1406 for(i=0; i<HW_BEACON_MAX_COUNT; i++)
1408 NdisZeroMemory(pBeaconSync->BeaconBuf[i], HW_BEACON_OFFSET);
1409 pBeaconSync->CapabilityInfoLocationInBeacon[i] = 0;
1410 pBeaconSync->TimIELocationInBeacon[i] = 0;
1411 NdisZeroMemory(pBeaconSync->BeaconTxWI[i], TXWI_SIZE);
1414 NdisFreeMemory(pAd->CommonCfg.pBeaconSync, HW_BEACON_OFFSET * HW_BEACON_MAX_COUNT, 0);
1415 pAd->CommonCfg.pBeaconSync = NULL;
1419 VOID BeaconUpdateExec(
1420 IN PVOID SystemSpecific1,
1421 IN PVOID FunctionContext,
1422 IN PVOID SystemSpecific2,
1423 IN PVOID SystemSpecific3)
1425 PRTMP_ADAPTER pAd = (PRTMP_ADAPTER)FunctionContext;
1426 LARGE_INTEGER tsfTime_a;//, tsfTime_b, deltaTime_exp, deltaTime_ab;
1427 UINT32 delta, remain, remain_low, remain_high;
1428 // BOOLEAN positive;
1430 ReSyncBeaconTime(pAd);
1434 RTMP_IO_READ32(pAd, TSF_TIMER_DW0, &tsfTime_a.u.LowPart);
1435 RTMP_IO_READ32(pAd, TSF_TIMER_DW1, &tsfTime_a.u.HighPart);
1438 //positive=getDeltaTime(tsfTime_a, expectedTime, &deltaTime_exp);
1439 remain_high = pAd->CommonCfg.BeaconRemain * tsfTime_a.u.HighPart;
1440 remain_low = tsfTime_a.u.LowPart % (pAd->CommonCfg.BeaconPeriod << 10);
1441 remain = (remain_high + remain_low)%(pAd->CommonCfg.BeaconPeriod << 10);
1442 delta = (pAd->CommonCfg.BeaconPeriod << 10) - remain;
1444 pAd->CommonCfg.BeaconUpdateTimer.TimerValue = (delta >> 10) + 10;