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__));
238 pObj->MLMEThr_task = NULL;
241 pObj->MLMEThr_pid = NULL;
244 complete_and_exit (&pAd->mlmeComplete, 0);
251 ========================================================================
253 USB command kernel thread.
256 *Context the pAd, driver control block pointer
262 ========================================================================
267 PRTMP_ADAPTER pAd = (PRTMP_ADAPTER)Context;
271 pObj = (POS_COOKIE)pAd->OS_Cookie;
273 rtmp_os_thread_init("rt2870CmdThread", (PVOID)&(pAd->CmdQComplete));
275 NdisAcquireSpinLock(&pAd->CmdQLock);
276 pAd->CmdQ.CmdQState = RT2870_THREAD_RUNNING;
277 NdisReleaseSpinLock(&pAd->CmdQLock);
279 while (pAd->CmdQ.CmdQState == RT2870_THREAD_RUNNING)
281 /* lock the device pointers */
282 //down(&(pAd->RTUSBCmd_semaphore));
283 status = down_interruptible(&(pAd->RTUSBCmd_semaphore));
285 if (pAd->CmdQ.CmdQState == RT2870_THREAD_STOPED)
290 RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS);
293 /* lock the device pointers , need to check if required*/
294 //down(&(pAd->usbdev_semaphore));
296 if (!pAd->PM_FlgSuspend)
299 /* unlock the device pointers */
300 //up(&(pAd->usbdev_semaphore));
303 if (!pAd->PM_FlgSuspend)
304 { // Clear the CmdQElements.
305 CmdQElmt *pCmdQElmt = NULL;
307 NdisAcquireSpinLock(&pAd->CmdQLock);
308 pAd->CmdQ.CmdQState = RT2870_THREAD_STOPED;
309 while(pAd->CmdQ.size)
311 RTUSBDequeueCmd(&pAd->CmdQ, &pCmdQElmt);
314 if (pCmdQElmt->CmdFromNdis == TRUE)
316 if (pCmdQElmt->buffer != NULL)
317 NdisFreeMemory(pCmdQElmt->buffer, pCmdQElmt->bufferlength, 0);
319 NdisFreeMemory(pCmdQElmt, sizeof(CmdQElmt), 0);
323 if ((pCmdQElmt->buffer != NULL) && (pCmdQElmt->bufferlength != 0))
324 NdisFreeMemory(pCmdQElmt->buffer, pCmdQElmt->bufferlength, 0);
326 NdisFreeMemory(pCmdQElmt, sizeof(CmdQElmt), 0);
332 NdisReleaseSpinLock(&pAd->CmdQLock);
334 /* notify the exit routine that we're actually exiting now
336 * complete()/wait_for_completion() is similar to up()/down(),
337 * except that complete() is safe in the case where the structure
338 * is getting deleted in a parallel mode of execution (i.e. just
339 * after the down() -- that's necessary for the thread-shutdown
342 * complete_and_exit() goes even further than this -- it is safe in
343 * the case that the thread of the caller is going away (not just
344 * the structure) -- this is necessary for the module-remove case.
345 * This is important in preemption kernels, which transfer the flow
346 * of execution immediately upon a complete().
348 DBGPRINT(RT_DEBUG_TRACE,( "<---RTUSBCmdThread\n"));
351 pObj->RTUSBCmdThr_task = NULL;
354 pObj->RTUSBCmdThr_pid = NULL;
357 complete_and_exit (&pAd->CmdQComplete, 0);
363 static void RT2870_TimerQ_Handle(RTMP_ADAPTER *pAd)
366 RALINK_TIMER_STRUCT *pTimer;
367 RT2870_TIMER_ENTRY *pEntry;
368 unsigned long irqFlag;
370 while(!pAd->TimerFunc_kill)
372 // printk("waiting for event!\n");
375 status = down_interruptible(&(pAd->RTUSBTimer_semaphore));
377 if (pAd->TimerQ.status == RT2870_THREAD_STOPED)
381 while(pAd->TimerQ.pQHead)
383 RTMP_IRQ_LOCK(&pAd->TimerQLock, irqFlag);
384 pEntry = pAd->TimerQ.pQHead;
387 pTimer = pEntry->pRaTimer;
390 pAd->TimerQ.pQHead = pEntry->pNext;
391 if (pEntry == pAd->TimerQ.pQTail)
392 pAd->TimerQ.pQTail = NULL;
394 // return this queue entry to timerQFreeList.
395 pEntry->pNext = pAd->TimerQ.pQPollFreeList;
396 pAd->TimerQ.pQPollFreeList = pEntry;
398 RTMP_IRQ_UNLOCK(&pAd->TimerQLock, irqFlag);
402 if (pTimer->handle != NULL)
403 if (!pAd->PM_FlgSuspend)
404 pTimer->handle(NULL, (PVOID) pTimer->cookie, NULL, pTimer);
405 if ((pTimer->Repeat) && (pTimer->State == FALSE))
406 RTMP_OS_Add_Timer(&pTimer->TimerObj, pTimer->TimerValue);
412 pAd->TimerQ.status = RT2870_THREAD_STOPED;
413 RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS);
421 IN OUT PVOID Context)
426 pAd = (PRTMP_ADAPTER)Context;
427 pObj = (POS_COOKIE) pAd->OS_Cookie;
429 rtmp_os_thread_init("rt2870TimerQHandle", (PVOID)&(pAd->TimerQComplete));
431 RT2870_TimerQ_Handle(pAd);
433 /* notify the exit routine that we're actually exiting now
435 * complete()/wait_for_completion() is similar to up()/down(),
436 * except that complete() is safe in the case where the structure
437 * is getting deleted in a parallel mode of execution (i.e. just
438 * after the down() -- that's necessary for the thread-shutdown
441 * complete_and_exit() goes even further than this -- it is safe in
442 * the case that the thread of the caller is going away (not just
443 * the structure) -- this is necessary for the module-remove case.
444 * This is important in preemption kernels, which transfer the flow
445 * of execution immediately upon a complete().
447 DBGPRINT(RT_DEBUG_TRACE,( "<---%s\n",__func__));
450 pObj->TimerQThr_task = NULL;
453 pObj->TimerQThr_pid = NULL;
455 complete_and_exit(&pAd->TimerQComplete, 0);
461 RT2870_TIMER_ENTRY *RT2870_TimerQ_Insert(
462 IN RTMP_ADAPTER *pAd,
463 IN RALINK_TIMER_STRUCT *pTimer)
465 RT2870_TIMER_ENTRY *pQNode = NULL, *pQTail;
466 unsigned long irqFlags;
469 RTMP_IRQ_LOCK(&pAd->TimerQLock, irqFlags);
470 if (pAd->TimerQ.status & RT2870_THREAD_CAN_DO_INSERT)
472 if(pAd->TimerQ.pQPollFreeList)
474 pQNode = pAd->TimerQ.pQPollFreeList;
475 pAd->TimerQ.pQPollFreeList = pQNode->pNext;
477 pQNode->pRaTimer = pTimer;
478 pQNode->pNext = NULL;
480 pQTail = pAd->TimerQ.pQTail;
481 if (pAd->TimerQ.pQTail != NULL)
482 pQTail->pNext = pQNode;
483 pAd->TimerQ.pQTail = pQNode;
484 if (pAd->TimerQ.pQHead == NULL)
485 pAd->TimerQ.pQHead = pQNode;
487 RTMP_IRQ_UNLOCK(&pAd->TimerQLock, irqFlags);
490 up(&pAd->RTUSBTimer_semaphore);
491 //wake_up(&timerWaitQ);
495 RTMP_IRQ_UNLOCK(&pAd->TimerQLock, irqFlags);
501 BOOLEAN RT2870_TimerQ_Remove(
502 IN RTMP_ADAPTER *pAd,
503 IN RALINK_TIMER_STRUCT *pTimer)
505 RT2870_TIMER_ENTRY *pNode, *pPrev = NULL;
506 unsigned long irqFlags;
508 RTMP_IRQ_LOCK(&pAd->TimerQLock, irqFlags);
509 if (pAd->TimerQ.status >= RT2870_THREAD_INITED)
511 pNode = pAd->TimerQ.pQHead;
514 if (pNode->pRaTimer == pTimer)
517 pNode = pNode->pNext;
520 // Now move it to freeList queue.
523 if (pNode == pAd->TimerQ.pQHead)
524 pAd->TimerQ.pQHead = pNode->pNext;
525 if (pNode == pAd->TimerQ.pQTail)
526 pAd->TimerQ.pQTail = pPrev;
528 pPrev->pNext = pNode->pNext;
530 // return this queue entry to timerQFreeList.
531 pNode->pNext = pAd->TimerQ.pQPollFreeList;
532 pAd->TimerQ.pQPollFreeList = pNode;
535 RTMP_IRQ_UNLOCK(&pAd->TimerQLock, irqFlags);
541 void RT2870_TimerQ_Exit(RTMP_ADAPTER *pAd)
543 RT2870_TIMER_ENTRY *pTimerQ;
544 unsigned long irqFlags;
546 RTMP_IRQ_LOCK(&pAd->TimerQLock, irqFlags);
547 while (pAd->TimerQ.pQHead)
549 pTimerQ = pAd->TimerQ.pQHead;
550 pAd->TimerQ.pQHead = pTimerQ->pNext;
553 pAd->TimerQ.pQPollFreeList = NULL;
554 os_free_mem(pAd, pAd->TimerQ.pTimerQPoll);
555 pAd->TimerQ.pQTail = NULL;
556 pAd->TimerQ.pQHead = NULL;
557 pAd->TimerQ.status = RT2870_THREAD_STOPED;
558 RTMP_IRQ_UNLOCK(&pAd->TimerQLock, irqFlags);
563 void RT2870_TimerQ_Init(RTMP_ADAPTER *pAd)
566 RT2870_TIMER_ENTRY *pQNode, *pEntry;
567 unsigned long irqFlags;
569 NdisAllocateSpinLock(&pAd->TimerQLock);
571 RTMP_IRQ_LOCK(&pAd->TimerQLock, irqFlags);
572 NdisZeroMemory(&pAd->TimerQ, sizeof(pAd->TimerQ));
573 //InterlockedExchange(&pAd->TimerQ.count, 0);
575 /* Initialise the wait q head */
576 //init_waitqueue_head(&timerWaitQ);
578 os_alloc_mem(pAd, &pAd->TimerQ.pTimerQPoll, sizeof(RT2870_TIMER_ENTRY) * TIMER_QUEUE_SIZE_MAX);
579 if (pAd->TimerQ.pTimerQPoll)
582 pQNode = (RT2870_TIMER_ENTRY *)pAd->TimerQ.pTimerQPoll;
583 for (i = 0 ;i <TIMER_QUEUE_SIZE_MAX; i++)
585 pQNode->pNext = pEntry;
589 pAd->TimerQ.pQPollFreeList = pEntry;
590 pAd->TimerQ.pQHead = NULL;
591 pAd->TimerQ.pQTail = NULL;
592 pAd->TimerQ.status = RT2870_THREAD_INITED;
594 RTMP_IRQ_UNLOCK(&pAd->TimerQLock, irqFlags);
598 VOID RT2870_WatchDog(IN RTMP_ADAPTER *pAd)
600 PHT_TX_CONTEXT pHTTXContext;
604 BOOLEAN needDumpSeq = FALSE;
609 RTMP_IO_READ32(pAd, TXRXQ_PCNT, &MACValue);
610 if ((MACValue & 0xff) !=0 )
612 DBGPRINT(RT_DEBUG_TRACE, ("TX QUEUE 0 Not EMPTY(Value=0x%0x). !!!!!!!!!!!!!!!\n", MACValue));
613 RTMP_IO_WRITE32(pAd, PBF_CFG, 0xf40012);
614 while((MACValue &0xff) != 0 && (idx++ < 10))
616 RTMP_IO_READ32(pAd, TXRXQ_PCNT, &MACValue);
619 RTMP_IO_WRITE32(pAd, PBF_CFG, 0xf40006);
622 //PS packets use HCCA queue when dequeue from PS unicast queue (WiFi WPA2 MA9_DT1 for Marvell B STA)
624 if ((MACValue & 0xff00) !=0 )
626 DBGPRINT(RT_DEBUG_TRACE, ("TX QUEUE 1 Not EMPTY(Value=0x%0x). !!!!!!!!!!!!!!!\n", MACValue));
627 RTMP_IO_WRITE32(pAd, PBF_CFG, 0xf4000a);
628 while((MACValue &0xff00) != 0 && (idx++ < 10))
630 RTMP_IO_READ32(pAd, TXRXQ_PCNT, &MACValue);
633 RTMP_IO_WRITE32(pAd, PBF_CFG, 0xf40006);
636 if (pAd->watchDogRxOverFlowCnt >= 2)
638 DBGPRINT(RT_DEBUG_TRACE, ("Maybe the Rx Bulk-In hanged! Cancel the pending Rx bulks request!\n"));
639 if ((!RTMP_TEST_FLAG(pAd, (fRTMP_ADAPTER_RESET_IN_PROGRESS |
640 fRTMP_ADAPTER_BULKIN_RESET |
641 fRTMP_ADAPTER_HALT_IN_PROGRESS |
642 fRTMP_ADAPTER_NIC_NOT_EXIST))))
644 DBGPRINT(RT_DEBUG_TRACE, ("Call CMDTHREAD_RESET_BULK_IN to cancel the pending Rx Bulk!\n"));
645 RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_BULKIN_RESET);
646 RTUSBEnqueueInternalCmd(pAd, CMDTHREAD_RESET_BULK_IN, NULL, 0);
649 pAd->watchDogRxOverFlowCnt = 0;
653 for (idx = 0; idx < NUM_OF_TX_RING; idx++)
657 RTMP_IRQ_LOCK(&pAd->BulkOutLock[idx], irqFlags);
658 if ((pAd->BulkOutPending[idx] == TRUE) && pAd->watchDogTxPendingCnt)
660 pAd->watchDogTxPendingCnt[idx]++;
662 if ((pAd->watchDogTxPendingCnt[idx] > 2) &&
663 (!RTMP_TEST_FLAG(pAd, (fRTMP_ADAPTER_RESET_IN_PROGRESS | fRTMP_ADAPTER_HALT_IN_PROGRESS | fRTMP_ADAPTER_NIC_NOT_EXIST | fRTMP_ADAPTER_BULKOUT_RESET)))
666 // FIXME: Following code just support single bulk out. If you wanna support multiple bulk out. Modify it!
667 pHTTXContext = (PHT_TX_CONTEXT)(&pAd->TxContext[idx]);
668 if (pHTTXContext->IRPPending)
669 { // Check TxContext.
670 pUrb = pHTTXContext->pUrb;
672 else if (idx == MGMTPIPEIDX)
674 PTX_CONTEXT pMLMEContext, pNULLContext, pPsPollContext;
677 pMLMEContext = (PTX_CONTEXT)(pAd->MgmtRing.Cell[pAd->MgmtRing.TxDmaIdx].AllocVa);
678 pPsPollContext = (PTX_CONTEXT)(&pAd->PsPollContext);
679 pNULLContext = (PTX_CONTEXT)(&pAd->NullContext);
681 if (pMLMEContext->IRPPending)
683 ASSERT(pMLMEContext->IRPPending);
684 pUrb = pMLMEContext->pUrb;
686 else if (pNULLContext->IRPPending)
688 ASSERT(pNULLContext->IRPPending);
689 pUrb = pNULLContext->pUrb;
691 else if (pPsPollContext->IRPPending)
693 ASSERT(pPsPollContext->IRPPending);
694 pUrb = pPsPollContext->pUrb;
698 RTMP_IRQ_UNLOCK(&pAd->BulkOutLock[idx], irqFlags);
700 DBGPRINT(RT_DEBUG_TRACE, ("Maybe the Tx Bulk-Out hanged! Cancel the pending Tx bulks request of idx(%d)!\n", idx));
703 DBGPRINT(RT_DEBUG_TRACE, ("Unlink the pending URB!\n"));
705 RTUSB_UNLINK_URB(pUrb);
706 // Sleep 200 microseconds to give cancellation time to work
712 DBGPRINT(RT_DEBUG_ERROR, ("Unkonw bulkOut URB maybe hanged!!!!!!!!!!!!\n"));
717 RTMP_IRQ_UNLOCK(&pAd->BulkOutLock[idx], irqFlags);
722 RTMP_IRQ_UNLOCK(&pAd->BulkOutLock[idx], irqFlags);
726 // For Sigma debug, dump the ba_reordering sequence.
727 if((needDumpSeq == TRUE) && (pAd->CommonCfg.bDisableReordering == 0))
730 PBA_REC_ENTRY pBAEntry = NULL;
732 struct reordering_mpdu *mpdu_blk;
734 Idx = pAd->MacTab.Content[BSSID_WCID].BARecWcidArray[0];
736 pBAEntry = &pAd->BATable.BARecEntry[Idx];
737 if((pBAEntry->list.qlen > 0) && (pBAEntry->list.next != NULL))
739 DBGPRINT(RT_DEBUG_TRACE, ("NICUpdateRawCounters():The Queueing pkt in reordering buffer:\n"));
740 NdisAcquireSpinLock(&pBAEntry->RxReRingLock);
741 mpdu_blk = pBAEntry->list.next;
744 DBGPRINT(RT_DEBUG_TRACE, ("\t%d:Seq-%d, bAMSDU-%d!\n", count, mpdu_blk->Sequence, mpdu_blk->bAMSDU));
745 mpdu_blk = mpdu_blk->next;
749 DBGPRINT(RT_DEBUG_TRACE, ("\npBAEntry->LastIndSeq=%d!\n", pBAEntry->LastIndSeq));
750 NdisReleaseSpinLock(&pBAEntry->RxReRingLock);
756 ========================================================================
758 Release allocated resources.
761 *dev Point to the PCI or USB device
762 pAd driver control block pointer
768 ========================================================================
770 static void _rtusb_disconnect(struct usb_device *dev, PRTMP_ADAPTER pAd)
772 struct net_device *net_dev = NULL;
775 DBGPRINT(RT_DEBUG_ERROR, ("rtusb_disconnect: unregister usbnet usb-%s-%s\n",
776 dev->bus->bus_name, dev->devpath));
781 printk("rtusb_disconnect: pAd == NULL!\n");
784 RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST);
788 // for debug, wait to show some messages to /proc system
794 net_dev = pAd->net_dev;
795 if (pAd->net_dev != NULL)
797 printk("rtusb_disconnect: unregister_netdev(), dev->name=%s!\n", net_dev->name);
798 unregister_netdev (pAd->net_dev);
801 flush_scheduled_work();
804 // free net_device memory
805 free_netdev(net_dev);
807 // free adapter memory
808 RTMPFreeAdapter(pAd);
810 // release a use of the usb device structure
814 DBGPRINT(RT_DEBUG_ERROR, (" RTUSB disconnect successfully\n"));
819 ========================================================================
821 Probe RT28XX chipset.
824 *dev Point to the PCI or USB device
826 *id_table Point to the PCI or USB device ID
832 ========================================================================
834 static int rtusb_probe (struct usb_interface *intf,
835 const struct usb_device_id *id)
838 return (int)rt28xx_probe((void *)intf, (void *)id, 0, &pAd);
842 static void rtusb_disconnect(struct usb_interface *intf)
844 struct usb_device *dev = interface_to_usbdev(intf);
848 pAd = usb_get_intfdata(intf);
849 usb_set_intfdata(intf, NULL);
851 _rtusb_disconnect(dev, pAd);
856 ========================================================================
858 Close kernel threads.
861 *pAd the raxx interface data pointer
867 ========================================================================
869 VOID RT28xxThreadTerminate(
870 IN RTMP_ADAPTER *pAd)
872 POS_COOKIE pObj = (POS_COOKIE) pAd->OS_Cookie;
876 // Sleep 50 milliseconds so pending io might finish normally
877 RTMPusecDelay(50000);
879 // We want to wait until all pending receives and sends to the
880 // device object. We cancel any
881 // irps. Wait until sends and receives have stopped.
882 RTUSBCancelPendingIRPs(pAd);
886 BUG_ON(pObj->TimerQThr_task == NULL);
887 CHECK_PID_LEGALITY(task_pid(pObj->TimerQThr_task))
889 POS_COOKIE pObj = (POS_COOKIE)pAd->OS_Cookie;
891 printk(KERN_DEBUG "Terminate the TimerQThr pid=%d!\n",
892 pid_nr(task_pid(pObj->TimerQThr_task)));
894 pAd->TimerFunc_kill = 1;
896 kthread_stop(pObj->TimerQThr_task);
897 pObj->TimerQThr_task = NULL;
900 BUG_ON(pObj->MLMEThr_task == NULL);
901 CHECK_PID_LEGALITY(task_pid(pObj->MLMEThr_task))
903 printk(KERN_DEBUG "Terminate the MLMEThr pid=%d!\n",
904 pid_nr(task_pid(pObj->MLMEThr_task)));
907 //RT28XX_MLME_HANDLER(pAd);
909 kthread_stop(pObj->MLMEThr_task);
910 pObj->MLMEThr_task = NULL;
913 BUG_ON(pObj->RTUSBCmdThr_task == NULL);
914 CHECK_PID_LEGALITY(task_pid(pObj->RTUSBCmdThr_task))
916 printk(KERN_DEBUG "Terminate the RTUSBCmdThr pid=%d!\n",
917 pid_nr(task_pid(pObj->RTUSBCmdThr_task)));
919 NdisAcquireSpinLock(&pAd->CmdQLock);
920 pAd->CmdQ.CmdQState = RT2870_THREAD_STOPED;
921 NdisReleaseSpinLock(&pAd->CmdQLock);
924 kthread_stop(pObj->RTUSBCmdThr_task);
925 pObj->RTUSBCmdThr_task = NULL;
929 if (pObj->MLMEThr_pid)
931 printk("Terminate the MLMEThr_pid=%d!\n", pid_nr(pObj->MLMEThr_pid));
934 //RT28XX_MLME_HANDLER(pAd);
936 ret = kill_pid(pObj->MLMEThr_pid, SIGTERM, 1);
939 printk (KERN_WARNING "%s: unable to Mlme thread, pid=%d, ret=%d!\n",
940 pAd->net_dev->name, pid_nr(pObj->MLMEThr_pid), ret);
944 //wait_for_completion (&pAd->notify);
945 wait_for_completion (&pAd->mlmeComplete);
946 pObj->MLMEThr_pid = NULL;
950 if (pObj->RTUSBCmdThr_pid >= 0)
952 printk("Terminate the RTUSBCmdThr_pid=%d!\n", pid_nr(pObj->RTUSBCmdThr_pid));
954 NdisAcquireSpinLock(&pAd->CmdQLock);
955 pAd->CmdQ.CmdQState = RT2870_THREAD_STOPED;
956 NdisReleaseSpinLock(&pAd->CmdQLock);
959 ret = kill_pid(pObj->RTUSBCmdThr_pid, SIGTERM, 1);
962 printk(KERN_WARNING "%s: unable to RTUSBCmd thread, pid=%d, ret=%d!\n",
963 pAd->net_dev->name, pid_nr(pObj->RTUSBCmdThr_pid), ret);
967 //wait_for_completion (&pAd->notify);
968 wait_for_completion (&pAd->CmdQComplete);
969 pObj->RTUSBCmdThr_pid = NULL;
972 if (pObj->TimerQThr_pid >= 0)
974 POS_COOKIE pObj = (POS_COOKIE)pAd->OS_Cookie;
975 printk("Terminate the TimerQThr_pid=%d!\n", pid_nr(pObj->TimerQThr_pid));
977 pAd->TimerFunc_kill = 1;
979 ret = kill_pid(pObj->TimerQThr_pid, SIGTERM, 1);
982 printk(KERN_WARNING "%s: unable to stop TimerQThread, pid=%d, ret=%d!\n",
983 pAd->net_dev->name, pid_nr(pObj->TimerQThr_pid), ret);
987 printk("wait_for_completion TimerQThr\n");
988 wait_for_completion(&pAd->TimerQComplete);
989 pObj->TimerQThr_pid = NULL;
996 pAd->CmdQ.CmdQState = RT2870_THREAD_UNKNOWN;
997 pAd->TimerFunc_kill = 0;
1001 void kill_thread_task(IN PRTMP_ADAPTER pAd)
1005 pObj = (POS_COOKIE) pAd->OS_Cookie;
1007 tasklet_kill(&pObj->rx_done_task);
1008 tasklet_kill(&pObj->mgmt_dma_done_task);
1009 tasklet_kill(&pObj->ac0_dma_done_task);
1010 tasklet_kill(&pObj->ac1_dma_done_task);
1011 tasklet_kill(&pObj->ac2_dma_done_task);
1012 tasklet_kill(&pObj->ac3_dma_done_task);
1013 tasklet_kill(&pObj->hcca_dma_done_task);
1014 tasklet_kill(&pObj->tbtt_task);
1020 ========================================================================
1021 Routine Description:
1022 Check the chipset vendor/product ID.
1025 _dev_p Point to the PCI or USB device
1032 ========================================================================
1034 BOOLEAN RT28XXChipsetCheck(
1037 struct usb_interface *intf = (struct usb_interface *)_dev_p;
1038 struct usb_device *dev_p = interface_to_usbdev(intf);
1042 for(i=0; i<rtusb_usb_id_len; i++)
1044 if (dev_p->descriptor.idVendor == rtusb_usb_id[i].idVendor &&
1045 dev_p->descriptor.idProduct == rtusb_usb_id[i].idProduct)
1048 printk(KERN_DEBUG "rt2870: idVendor = 0x%x, idProduct = 0x%x\n",
1051 printk("rt2870: idVendor = 0x%x, idProduct = 0x%x\n",
1053 dev_p->descriptor.idVendor, dev_p->descriptor.idProduct);
1058 if (i == rtusb_usb_id_len)
1060 printk("rt2870: Error! Device Descriptor not matching!\n");
1069 ========================================================================
1070 Routine Description:
1071 Init net device structure.
1074 _dev_p Point to the PCI or USB device
1075 *net_dev Point to the net device
1076 *pAd the raxx interface data pointer
1083 ========================================================================
1085 BOOLEAN RT28XXNetDevInit(
1087 IN struct net_device *net_dev,
1088 IN RTMP_ADAPTER *pAd)
1090 struct usb_interface *intf = (struct usb_interface *)_dev_p;
1091 struct usb_device *dev_p = interface_to_usbdev(intf);
1094 pAd->config = &dev_p->config->desc;
1100 ========================================================================
1101 Routine Description:
1102 Init net device structure.
1105 _dev_p Point to the PCI or USB device
1106 *pAd the raxx interface data pointer
1113 ========================================================================
1115 BOOLEAN RT28XXProbePostConfig(
1117 IN RTMP_ADAPTER *pAd,
1120 struct usb_interface *intf = (struct usb_interface *)_dev_p;
1121 struct usb_host_interface *iface_desc;
1126 /* get the active interface descriptor */
1127 iface_desc = intf->cur_altsetting;
1129 /* get # of enpoints */
1130 pAd->NumberOfPipes = iface_desc->desc.bNumEndpoints;
1131 DBGPRINT(RT_DEBUG_TRACE,
1132 ("NumEndpoints=%d\n", iface_desc->desc.bNumEndpoints));
1134 /* Configure Pipes */
1137 for(i=0; i<pAd->NumberOfPipes; i++)
1139 if ((iface_desc->endpoint[i].desc.bmAttributes ==
1140 USB_ENDPOINT_XFER_BULK) &&
1141 ((iface_desc->endpoint[i].desc.bEndpointAddress &
1142 USB_ENDPOINT_DIR_MASK) == USB_DIR_IN))
1144 pAd->BulkInEpAddr = iface_desc->endpoint[i].desc.bEndpointAddress;
1145 pAd->BulkInMaxPacketSize = iface_desc->endpoint[i].desc.wMaxPacketSize;
1147 DBGPRINT_RAW(RT_DEBUG_TRACE,
1148 ("BULK IN MaximumPacketSize = %d\n", pAd->BulkInMaxPacketSize));
1149 DBGPRINT_RAW(RT_DEBUG_TRACE,
1150 ("EP address = 0x%2x\n", iface_desc->endpoint[i].desc.bEndpointAddress));
1152 else if ((iface_desc->endpoint[i].desc.bmAttributes ==
1153 USB_ENDPOINT_XFER_BULK) &&
1154 ((iface_desc->endpoint[i].desc.bEndpointAddress &
1155 USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT))
1157 // there are 6 bulk out EP. EP6 highest priority.
1158 // EP1-4 is EDCA. EP5 is HCCA.
1159 pAd->BulkOutEpAddr[BulkOutIdx++] = iface_desc->endpoint[i].desc.bEndpointAddress;
1160 pAd->BulkOutMaxPacketSize = iface_desc->endpoint[i].desc.wMaxPacketSize;
1162 DBGPRINT_RAW(RT_DEBUG_TRACE,
1163 ("BULK OUT MaximumPacketSize = %d\n", pAd->BulkOutMaxPacketSize));
1164 DBGPRINT_RAW(RT_DEBUG_TRACE,
1165 ("EP address = 0x%2x \n", iface_desc->endpoint[i].desc.bEndpointAddress));
1169 if (!(pAd->BulkInEpAddr && pAd->BulkOutEpAddr[0]))
1171 printk("%s: Could not find both bulk-in and bulk-out endpoints\n", __func__);
1180 ========================================================================
1181 Routine Description:
1185 *pAd the raxx interface data pointer
1191 ========================================================================
1193 VOID RT28XXDMADisable(
1194 IN RTMP_ADAPTER *pAd)
1202 ========================================================================
1203 Routine Description:
1207 *pAd the raxx interface data pointer
1213 ========================================================================
1215 VOID RT28XXDMAEnable(
1216 IN RTMP_ADAPTER *pAd)
1218 WPDMA_GLO_CFG_STRUC GloCfg;
1219 USB_DMA_CFG_STRUC UsbCfg;
1223 RTMP_IO_WRITE32(pAd, MAC_SYS_CTRL, 0x4);
1226 RTMP_IO_READ32(pAd, WPDMA_GLO_CFG, &GloCfg.word);
1227 if ((GloCfg.field.TxDMABusy == 0) && (GloCfg.field.RxDMABusy == 0))
1230 DBGPRINT(RT_DEBUG_TRACE, ("==> DMABusy\n"));
1231 RTMPusecDelay(1000);
1237 GloCfg.field.EnTXWriteBackDDONE = 1;
1238 GloCfg.field.EnableRxDMA = 1;
1239 GloCfg.field.EnableTxDMA = 1;
1240 DBGPRINT(RT_DEBUG_TRACE, ("<== WRITE DMA offset 0x208 = 0x%x\n", GloCfg.word));
1241 RTMP_IO_WRITE32(pAd, WPDMA_GLO_CFG, GloCfg.word);
1244 UsbCfg.field.phyclear = 0;
1245 /* usb version is 1.1,do not use bulk in aggregation */
1246 if (pAd->BulkInMaxPacketSize == 512)
1247 UsbCfg.field.RxBulkAggEn = 1;
1248 /* for last packet, PBF might use more than limited, so minus 2 to prevent from error */
1249 UsbCfg.field.RxBulkAggLmt = (MAX_RXBULK_SIZE /1024)-3;
1250 UsbCfg.field.RxBulkAggTOut = 0x80; /* 2006-10-18 */
1251 UsbCfg.field.RxBulkEn = 1;
1252 UsbCfg.field.TxBulkEn = 1;
1254 RTUSBWriteMACRegister(pAd, USB_DMA_CFG, UsbCfg.word);
1259 ========================================================================
1260 Routine Description:
1261 Write Beacon buffer to Asic.
1264 *pAd the raxx interface data pointer
1270 ========================================================================
1272 VOID RT28xx_UpdateBeaconToAsic(
1273 IN RTMP_ADAPTER *pAd,
1278 PUCHAR pBeaconFrame = NULL;
1281 BEACON_SYNC_STRUCT *pBeaconSync = pAd->CommonCfg.pBeaconSync;
1283 BOOLEAN bBcnReq = FALSE;
1287 if (pBeaconFrame == NULL)
1289 DBGPRINT(RT_DEBUG_ERROR,("pBeaconFrame is NULL!\n"));
1293 if (pBeaconSync == NULL)
1295 DBGPRINT(RT_DEBUG_ERROR,("pBeaconSync is NULL!\n"));
1299 //if ((pAd->WdsTab.Mode == WDS_BRIDGE_MODE) ||
1300 // ((pAd->ApCfg.MBSSID[apidx].MSSIDDev == NULL) || !(pAd->ApCfg.MBSSID[apidx].MSSIDDev->flags & IFF_UP))
1302 if (bBcnReq == FALSE)
1304 /* when the ra interface is down, do not send its beacon frame */
1305 /* clear all zero */
1306 for(i=0; i<TXWI_SIZE; i+=4) {
1307 RTMP_IO_WRITE32(pAd, pAd->BeaconOffset[bcn_idx] + i, 0x00);
1309 pBeaconSync->BeaconBitMap &= (~(BEACON_BITMAP_MASK & (1 << bcn_idx)));
1310 NdisZeroMemory(pBeaconSync->BeaconTxWI[bcn_idx], TXWI_SIZE);
1314 ptr = (PUCHAR)&pAd->BeaconTxWI;
1316 if (NdisEqualMemory(pBeaconSync->BeaconTxWI[bcn_idx], &pAd->BeaconTxWI, TXWI_SIZE) == FALSE)
1317 { // If BeaconTxWI changed, we need to rewrite the TxWI for the Beacon frames.
1318 pBeaconSync->BeaconBitMap &= (~(BEACON_BITMAP_MASK & (1 << bcn_idx)));
1319 NdisMoveMemory(pBeaconSync->BeaconTxWI[bcn_idx], &pAd->BeaconTxWI, TXWI_SIZE);
1322 if ((pBeaconSync->BeaconBitMap & (1 << bcn_idx)) != (1 << bcn_idx))
1324 for (i=0; i<TXWI_SIZE; i+=4) // 16-byte TXWI field
1326 longValue = *ptr + (*(ptr+1)<<8) + (*(ptr+2)<<16) + (*(ptr+3)<<24);
1327 RTMP_IO_WRITE32(pAd, pAd->BeaconOffset[bcn_idx] + i, longValue);
1332 ptr = pBeaconSync->BeaconBuf[bcn_idx];
1333 padding = (FrameLen & 0x01);
1334 NdisZeroMemory((PUCHAR)(pBeaconFrame + FrameLen), padding);
1335 FrameLen += padding;
1336 for (i = 0 ; i < FrameLen /*HW_BEACON_OFFSET*/; i += 2)
1338 if (NdisEqualMemory(ptr, pBeaconFrame, 2) == FALSE)
1340 NdisMoveMemory(ptr, pBeaconFrame, 2);
1341 //shortValue = *ptr + (*(ptr+1)<<8);
1342 //RTMP_IO_WRITE8(pAd, pAd->BeaconOffset[bcn_idx] + TXWI_SIZE + i, shortValue);
1343 RTUSBMultiWrite(pAd, pAd->BeaconOffset[bcn_idx] + TXWI_SIZE + i, ptr, 2);
1349 pBeaconSync->BeaconBitMap |= (1 << bcn_idx);
1351 // For AP interface, set the DtimBitOn so that we can send Bcast/Mcast frame out after this beacon frame.
1357 VOID RT2870_BssBeaconStop(
1358 IN RTMP_ADAPTER *pAd)
1360 BEACON_SYNC_STRUCT *pBeaconSync;
1362 BOOLEAN Cancelled = TRUE;
1364 pBeaconSync = pAd->CommonCfg.pBeaconSync;
1365 if (pBeaconSync && pBeaconSync->EnableBeacon)
1369 NumOfBcn = MAX_MESH_NUM;
1371 RTMPCancelTimer(&pAd->CommonCfg.BeaconUpdateTimer, &Cancelled);
1373 for(i=0; i<NumOfBcn; i++)
1375 NdisZeroMemory(pBeaconSync->BeaconBuf[i], HW_BEACON_OFFSET);
1376 NdisZeroMemory(pBeaconSync->BeaconTxWI[i], TXWI_SIZE);
1378 for (offset=0; offset<HW_BEACON_OFFSET; offset+=4)
1379 RTMP_IO_WRITE32(pAd, pAd->BeaconOffset[i] + offset, 0x00);
1381 pBeaconSync->CapabilityInfoLocationInBeacon[i] = 0;
1382 pBeaconSync->TimIELocationInBeacon[i] = 0;
1384 pBeaconSync->BeaconBitMap = 0;
1385 pBeaconSync->DtimBitOn = 0;
1390 VOID RT2870_BssBeaconStart(
1391 IN RTMP_ADAPTER *pAd)
1394 BEACON_SYNC_STRUCT *pBeaconSync;
1395 // LARGE_INTEGER tsfTime, deltaTime;
1397 pBeaconSync = pAd->CommonCfg.pBeaconSync;
1398 if (pBeaconSync && pBeaconSync->EnableBeacon)
1402 NumOfBcn = MAX_MESH_NUM;
1404 for(apidx=0; apidx<NumOfBcn; apidx++)
1406 UCHAR CapabilityInfoLocationInBeacon = 0;
1407 UCHAR TimIELocationInBeacon = 0;
1409 NdisZeroMemory(pBeaconSync->BeaconBuf[apidx], HW_BEACON_OFFSET);
1410 pBeaconSync->CapabilityInfoLocationInBeacon[apidx] = CapabilityInfoLocationInBeacon;
1411 pBeaconSync->TimIELocationInBeacon[apidx] = TimIELocationInBeacon;
1412 NdisZeroMemory(pBeaconSync->BeaconTxWI[apidx], TXWI_SIZE);
1414 pBeaconSync->BeaconBitMap = 0;
1415 pBeaconSync->DtimBitOn = 0;
1416 pAd->CommonCfg.BeaconUpdateTimer.Repeat = TRUE;
1418 pAd->CommonCfg.BeaconAdjust = 0;
1419 pAd->CommonCfg.BeaconFactor = 0xffffffff / (pAd->CommonCfg.BeaconPeriod << 10);
1420 pAd->CommonCfg.BeaconRemain = (0xffffffff % (pAd->CommonCfg.BeaconPeriod << 10)) + 1;
1421 printk("RT2870_BssBeaconStart:BeaconFactor=%d, BeaconRemain=%d!\n", pAd->CommonCfg.BeaconFactor, pAd->CommonCfg.BeaconRemain);
1422 RTMPSetTimer(&pAd->CommonCfg.BeaconUpdateTimer, pAd->CommonCfg.BeaconPeriod);
1428 VOID RT2870_BssBeaconInit(
1429 IN RTMP_ADAPTER *pAd)
1431 BEACON_SYNC_STRUCT *pBeaconSync;
1434 NdisAllocMemory(pAd->CommonCfg.pBeaconSync, sizeof(BEACON_SYNC_STRUCT), MEM_ALLOC_FLAG);
1435 if (pAd->CommonCfg.pBeaconSync)
1437 pBeaconSync = pAd->CommonCfg.pBeaconSync;
1438 NdisZeroMemory(pBeaconSync, sizeof(BEACON_SYNC_STRUCT));
1439 for(i=0; i < HW_BEACON_MAX_COUNT; i++)
1441 NdisZeroMemory(pBeaconSync->BeaconBuf[i], HW_BEACON_OFFSET);
1442 pBeaconSync->CapabilityInfoLocationInBeacon[i] = 0;
1443 pBeaconSync->TimIELocationInBeacon[i] = 0;
1444 NdisZeroMemory(pBeaconSync->BeaconTxWI[i], TXWI_SIZE);
1446 pBeaconSync->BeaconBitMap = 0;
1448 //RTMPInitTimer(pAd, &pAd->CommonCfg.BeaconUpdateTimer, GET_TIMER_FUNCTION(BeaconUpdateExec), pAd, TRUE);
1449 pBeaconSync->EnableBeacon = TRUE;
1454 VOID RT2870_BssBeaconExit(
1455 IN RTMP_ADAPTER *pAd)
1457 BEACON_SYNC_STRUCT *pBeaconSync;
1458 BOOLEAN Cancelled = TRUE;
1461 if (pAd->CommonCfg.pBeaconSync)
1463 pBeaconSync = pAd->CommonCfg.pBeaconSync;
1464 pBeaconSync->EnableBeacon = FALSE;
1465 RTMPCancelTimer(&pAd->CommonCfg.BeaconUpdateTimer, &Cancelled);
1466 pBeaconSync->BeaconBitMap = 0;
1468 for(i=0; i<HW_BEACON_MAX_COUNT; i++)
1470 NdisZeroMemory(pBeaconSync->BeaconBuf[i], HW_BEACON_OFFSET);
1471 pBeaconSync->CapabilityInfoLocationInBeacon[i] = 0;
1472 pBeaconSync->TimIELocationInBeacon[i] = 0;
1473 NdisZeroMemory(pBeaconSync->BeaconTxWI[i], TXWI_SIZE);
1476 NdisFreeMemory(pAd->CommonCfg.pBeaconSync, HW_BEACON_OFFSET * HW_BEACON_MAX_COUNT, 0);
1477 pAd->CommonCfg.pBeaconSync = NULL;
1481 VOID BeaconUpdateExec(
1482 IN PVOID SystemSpecific1,
1483 IN PVOID FunctionContext,
1484 IN PVOID SystemSpecific2,
1485 IN PVOID SystemSpecific3)
1487 PRTMP_ADAPTER pAd = (PRTMP_ADAPTER)FunctionContext;
1488 LARGE_INTEGER tsfTime_a;//, tsfTime_b, deltaTime_exp, deltaTime_ab;
1489 UINT32 delta, remain, remain_low, remain_high;
1490 // BOOLEAN positive;
1492 ReSyncBeaconTime(pAd);
1496 RTMP_IO_READ32(pAd, TSF_TIMER_DW0, &tsfTime_a.u.LowPart);
1497 RTMP_IO_READ32(pAd, TSF_TIMER_DW1, &tsfTime_a.u.HighPart);
1500 //positive=getDeltaTime(tsfTime_a, expectedTime, &deltaTime_exp);
1501 remain_high = pAd->CommonCfg.BeaconRemain * tsfTime_a.u.HighPart;
1502 remain_low = tsfTime_a.u.LowPart % (pAd->CommonCfg.BeaconPeriod << 10);
1503 remain = (remain_high + remain_low)%(pAd->CommonCfg.BeaconPeriod << 10);
1504 delta = (pAd->CommonCfg.BeaconPeriod << 10) - remain;
1506 pAd->CommonCfg.BeaconUpdateTimer.TimerValue = (delta >> 10) + 10;