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 #ifdef CONFIG_STA_SUPPORT
50 MODULE_LICENSE("GPL");
52 MODULE_VERSION(STA_DRIVER_VERSION);
54 #endif // CONFIG_STA_SUPPORT //
56 /* Kernel thread and vars, which handles packets that are completed. Only
57 * packets that have a "complete" function are sent here. This way, the
58 * completion is run out of kernel context, and doesn't block the rest of
61 extern INT __devinit rt28xx_probe(IN void *_dev_p, IN void *_dev_id_p,
62 IN UINT argc, OUT PRTMP_ADAPTER *ppAd);
66 struct usb_device_id rtusb_usb_id[] = RT2870_USB_DEVICES;
67 INT const rtusb_usb_id_len = sizeof(rtusb_usb_id) / sizeof(struct usb_device_id);
68 MODULE_DEVICE_TABLE(usb, rtusb_usb_id);
76 static int rt2870_suspend(struct usb_interface *intf, pm_message_t state);
77 static int rt2870_resume(struct usb_interface *intf);
78 #endif // CONFIG_PM //
80 /**************************************************************************/
81 /**************************************************************************/
82 //tested for kernel 2.6series
83 /**************************************************************************/
84 /**************************************************************************/
85 static int rtusb_probe (struct usb_interface *intf,
86 const struct usb_device_id *id);
87 static void rtusb_disconnect(struct usb_interface *intf);
89 struct usb_driver rtusb_driver = {
92 .disconnect=rtusb_disconnect,
93 .id_table=rtusb_usb_id,
96 suspend: rt2870_suspend,
97 resume: rt2870_resume,
103 VOID RT2860RejectPendingPackets(
104 IN PRTMP_ADAPTER pAd)
107 // clear TxSw packets
110 static int rt2870_suspend(
111 struct usb_interface *intf,
114 struct net_device *net_dev;
115 PRTMP_ADAPTER pAd = usb_get_intfdata(intf);
118 DBGPRINT(RT_DEBUG_TRACE, ("===> rt2870_suspend()\n"));
119 net_dev = pAd->net_dev;
120 netif_device_detach (net_dev);
122 pAd->PM_FlgSuspend = 1;
123 if (netif_running(net_dev)) {
124 RTUSBCancelPendingBulkInIRP(pAd);
125 RTUSBCancelPendingBulkOutIRP(pAd);
127 DBGPRINT(RT_DEBUG_TRACE, ("<=== rt2870_suspend()\n"));
131 static int rt2870_resume(
132 struct usb_interface *intf)
134 struct net_device *net_dev;
135 PRTMP_ADAPTER pAd = usb_get_intfdata(intf);
138 DBGPRINT(RT_DEBUG_TRACE, ("===> rt2870_resume()\n"));
140 pAd->PM_FlgSuspend = 0;
141 net_dev = pAd->net_dev;
142 netif_device_attach (net_dev);
143 netif_start_queue(net_dev);
144 netif_carrier_on(net_dev);
145 netif_wake_queue(net_dev);
147 DBGPRINT(RT_DEBUG_TRACE, ("<=== rt2870_resume()\n"));
150 #endif // CONFIG_PM //
153 // Init driver module
154 INT __init rtusb_init(void)
156 printk("rtusb init --->\n");
157 return usb_register(&rtusb_driver);
160 // Deinit driver module
161 VOID __exit rtusb_exit(void)
163 usb_deregister(&rtusb_driver);
164 printk("<--- rtusb exit\n");
167 module_init(rtusb_init);
168 module_exit(rtusb_exit);
173 /*--------------------------------------------------------------------- */
174 /* function declarations */
175 /*--------------------------------------------------------------------- */
178 ========================================================================
183 *Context the pAd, driver control block pointer
189 ========================================================================
194 PRTMP_ADAPTER pAd = (PRTMP_ADAPTER)Context;
198 pObj = (POS_COOKIE)pAd->OS_Cookie;
200 rtmp_os_thread_init("rt2870MlmeThread", (PVOID)&(pAd->mlmeComplete));
202 while (pAd->mlme_kill == 0)
204 /* lock the device pointers */
205 //down(&(pAd->mlme_semaphore));
206 status = down_interruptible(&(pAd->mlme_semaphore));
208 /* lock the device pointers , need to check if required*/
209 //down(&(pAd->usbdev_semaphore));
211 if (!pAd->PM_FlgSuspend)
214 /* unlock the device pointers */
215 //up(&(pAd->usbdev_semaphore));
218 RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS);
223 /* notify the exit routine that we're actually exiting now
225 * complete()/wait_for_completion() is similar to up()/down(),
226 * except that complete() is safe in the case where the structure
227 * is getting deleted in a parallel mode of execution (i.e. just
228 * after the down() -- that's necessary for the thread-shutdown
231 * complete_and_exit() goes even further than this -- it is safe in
232 * the case that the thread of the caller is going away (not just
233 * the structure) -- this is necessary for the module-remove case.
234 * This is important in preemption kernels, which transfer the flow
235 * of execution immediately upon a complete().
237 DBGPRINT(RT_DEBUG_TRACE,( "<---%s\n",__func__));
239 pObj->MLMEThr_task = NULL;
241 complete_and_exit (&pAd->mlmeComplete, 0);
248 ========================================================================
250 USB command kernel thread.
253 *Context the pAd, driver control block pointer
259 ========================================================================
264 PRTMP_ADAPTER pAd = (PRTMP_ADAPTER)Context;
268 pObj = (POS_COOKIE)pAd->OS_Cookie;
270 rtmp_os_thread_init("rt2870CmdThread", (PVOID)&(pAd->CmdQComplete));
272 NdisAcquireSpinLock(&pAd->CmdQLock);
273 pAd->CmdQ.CmdQState = RT2870_THREAD_RUNNING;
274 NdisReleaseSpinLock(&pAd->CmdQLock);
276 while (pAd->CmdQ.CmdQState == RT2870_THREAD_RUNNING)
278 /* lock the device pointers */
279 //down(&(pAd->RTUSBCmd_semaphore));
280 status = down_interruptible(&(pAd->RTUSBCmd_semaphore));
282 if (pAd->CmdQ.CmdQState == RT2870_THREAD_STOPED)
287 RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS);
290 /* lock the device pointers , need to check if required*/
291 //down(&(pAd->usbdev_semaphore));
293 if (!pAd->PM_FlgSuspend)
296 /* unlock the device pointers */
297 //up(&(pAd->usbdev_semaphore));
300 if (!pAd->PM_FlgSuspend)
301 { // Clear the CmdQElements.
302 CmdQElmt *pCmdQElmt = NULL;
304 NdisAcquireSpinLock(&pAd->CmdQLock);
305 pAd->CmdQ.CmdQState = RT2870_THREAD_STOPED;
306 while(pAd->CmdQ.size)
308 RTUSBDequeueCmd(&pAd->CmdQ, &pCmdQElmt);
311 if (pCmdQElmt->CmdFromNdis == TRUE)
313 if (pCmdQElmt->buffer != NULL)
314 NdisFreeMemory(pCmdQElmt->buffer, pCmdQElmt->bufferlength, 0);
316 NdisFreeMemory(pCmdQElmt, sizeof(CmdQElmt), 0);
320 if ((pCmdQElmt->buffer != NULL) && (pCmdQElmt->bufferlength != 0))
321 NdisFreeMemory(pCmdQElmt->buffer, pCmdQElmt->bufferlength, 0);
323 NdisFreeMemory(pCmdQElmt, sizeof(CmdQElmt), 0);
329 NdisReleaseSpinLock(&pAd->CmdQLock);
331 /* notify the exit routine that we're actually exiting now
333 * complete()/wait_for_completion() is similar to up()/down(),
334 * except that complete() is safe in the case where the structure
335 * is getting deleted in a parallel mode of execution (i.e. just
336 * after the down() -- that's necessary for the thread-shutdown
339 * complete_and_exit() goes even further than this -- it is safe in
340 * the case that the thread of the caller is going away (not just
341 * the structure) -- this is necessary for the module-remove case.
342 * This is important in preemption kernels, which transfer the flow
343 * of execution immediately upon a complete().
345 DBGPRINT(RT_DEBUG_TRACE,( "<---RTUSBCmdThread\n"));
347 pObj->RTUSBCmdThr_task = NULL;
349 complete_and_exit (&pAd->CmdQComplete, 0);
355 static void RT2870_TimerQ_Handle(RTMP_ADAPTER *pAd)
358 RALINK_TIMER_STRUCT *pTimer;
359 RT2870_TIMER_ENTRY *pEntry;
360 unsigned long irqFlag;
362 while(!pAd->TimerFunc_kill)
364 // printk("waiting for event!\n");
367 status = down_interruptible(&(pAd->RTUSBTimer_semaphore));
369 if (pAd->TimerQ.status == RT2870_THREAD_STOPED)
373 while(pAd->TimerQ.pQHead)
375 RTMP_IRQ_LOCK(&pAd->TimerQLock, irqFlag);
376 pEntry = pAd->TimerQ.pQHead;
379 pTimer = pEntry->pRaTimer;
382 pAd->TimerQ.pQHead = pEntry->pNext;
383 if (pEntry == pAd->TimerQ.pQTail)
384 pAd->TimerQ.pQTail = NULL;
386 // return this queue entry to timerQFreeList.
387 pEntry->pNext = pAd->TimerQ.pQPollFreeList;
388 pAd->TimerQ.pQPollFreeList = pEntry;
390 RTMP_IRQ_UNLOCK(&pAd->TimerQLock, irqFlag);
394 if (pTimer->handle != NULL)
395 if (!pAd->PM_FlgSuspend)
396 pTimer->handle(NULL, (PVOID) pTimer->cookie, NULL, pTimer);
397 if ((pTimer->Repeat) && (pTimer->State == FALSE))
398 RTMP_OS_Add_Timer(&pTimer->TimerObj, pTimer->TimerValue);
404 pAd->TimerQ.status = RT2870_THREAD_STOPED;
405 RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS);
413 IN OUT PVOID Context)
418 pAd = (PRTMP_ADAPTER)Context;
419 pObj = (POS_COOKIE) pAd->OS_Cookie;
421 rtmp_os_thread_init("rt2870TimerQHandle", (PVOID)&(pAd->TimerQComplete));
423 RT2870_TimerQ_Handle(pAd);
425 /* notify the exit routine that we're actually exiting now
427 * complete()/wait_for_completion() is similar to up()/down(),
428 * except that complete() is safe in the case where the structure
429 * is getting deleted in a parallel mode of execution (i.e. just
430 * after the down() -- that's necessary for the thread-shutdown
433 * complete_and_exit() goes even further than this -- it is safe in
434 * the case that the thread of the caller is going away (not just
435 * the structure) -- this is necessary for the module-remove case.
436 * This is important in preemption kernels, which transfer the flow
437 * of execution immediately upon a complete().
439 DBGPRINT(RT_DEBUG_TRACE,( "<---%s\n",__func__));
441 pObj->TimerQThr_task = NULL;
443 complete_and_exit(&pAd->TimerQComplete, 0);
449 RT2870_TIMER_ENTRY *RT2870_TimerQ_Insert(
450 IN RTMP_ADAPTER *pAd,
451 IN RALINK_TIMER_STRUCT *pTimer)
453 RT2870_TIMER_ENTRY *pQNode = NULL, *pQTail;
454 unsigned long irqFlags;
457 RTMP_IRQ_LOCK(&pAd->TimerQLock, irqFlags);
458 if (pAd->TimerQ.status & RT2870_THREAD_CAN_DO_INSERT)
460 if(pAd->TimerQ.pQPollFreeList)
462 pQNode = pAd->TimerQ.pQPollFreeList;
463 pAd->TimerQ.pQPollFreeList = pQNode->pNext;
465 pQNode->pRaTimer = pTimer;
466 pQNode->pNext = NULL;
468 pQTail = pAd->TimerQ.pQTail;
469 if (pAd->TimerQ.pQTail != NULL)
470 pQTail->pNext = pQNode;
471 pAd->TimerQ.pQTail = pQNode;
472 if (pAd->TimerQ.pQHead == NULL)
473 pAd->TimerQ.pQHead = pQNode;
475 RTMP_IRQ_UNLOCK(&pAd->TimerQLock, irqFlags);
478 up(&pAd->RTUSBTimer_semaphore);
479 //wake_up(&timerWaitQ);
483 RTMP_IRQ_UNLOCK(&pAd->TimerQLock, irqFlags);
489 BOOLEAN RT2870_TimerQ_Remove(
490 IN RTMP_ADAPTER *pAd,
491 IN RALINK_TIMER_STRUCT *pTimer)
493 RT2870_TIMER_ENTRY *pNode, *pPrev = NULL;
494 unsigned long irqFlags;
496 RTMP_IRQ_LOCK(&pAd->TimerQLock, irqFlags);
497 if (pAd->TimerQ.status >= RT2870_THREAD_INITED)
499 pNode = pAd->TimerQ.pQHead;
502 if (pNode->pRaTimer == pTimer)
505 pNode = pNode->pNext;
508 // Now move it to freeList queue.
511 if (pNode == pAd->TimerQ.pQHead)
512 pAd->TimerQ.pQHead = pNode->pNext;
513 if (pNode == pAd->TimerQ.pQTail)
514 pAd->TimerQ.pQTail = pPrev;
516 pPrev->pNext = pNode->pNext;
518 // return this queue entry to timerQFreeList.
519 pNode->pNext = pAd->TimerQ.pQPollFreeList;
520 pAd->TimerQ.pQPollFreeList = pNode;
523 RTMP_IRQ_UNLOCK(&pAd->TimerQLock, irqFlags);
529 void RT2870_TimerQ_Exit(RTMP_ADAPTER *pAd)
531 RT2870_TIMER_ENTRY *pTimerQ;
532 unsigned long irqFlags;
534 RTMP_IRQ_LOCK(&pAd->TimerQLock, irqFlags);
535 while (pAd->TimerQ.pQHead)
537 pTimerQ = pAd->TimerQ.pQHead;
538 pAd->TimerQ.pQHead = pTimerQ->pNext;
541 pAd->TimerQ.pQPollFreeList = NULL;
542 os_free_mem(pAd, pAd->TimerQ.pTimerQPoll);
543 pAd->TimerQ.pQTail = NULL;
544 pAd->TimerQ.pQHead = NULL;
545 pAd->TimerQ.status = RT2870_THREAD_STOPED;
546 RTMP_IRQ_UNLOCK(&pAd->TimerQLock, irqFlags);
551 void RT2870_TimerQ_Init(RTMP_ADAPTER *pAd)
554 RT2870_TIMER_ENTRY *pQNode, *pEntry;
555 unsigned long irqFlags;
557 NdisAllocateSpinLock(&pAd->TimerQLock);
559 RTMP_IRQ_LOCK(&pAd->TimerQLock, irqFlags);
560 NdisZeroMemory(&pAd->TimerQ, sizeof(pAd->TimerQ));
561 //InterlockedExchange(&pAd->TimerQ.count, 0);
563 /* Initialise the wait q head */
564 //init_waitqueue_head(&timerWaitQ);
566 os_alloc_mem(pAd, &pAd->TimerQ.pTimerQPoll, sizeof(RT2870_TIMER_ENTRY) * TIMER_QUEUE_SIZE_MAX);
567 if (pAd->TimerQ.pTimerQPoll)
570 pQNode = (RT2870_TIMER_ENTRY *)pAd->TimerQ.pTimerQPoll;
571 for (i = 0 ;i <TIMER_QUEUE_SIZE_MAX; i++)
573 pQNode->pNext = pEntry;
577 pAd->TimerQ.pQPollFreeList = pEntry;
578 pAd->TimerQ.pQHead = NULL;
579 pAd->TimerQ.pQTail = NULL;
580 pAd->TimerQ.status = RT2870_THREAD_INITED;
582 RTMP_IRQ_UNLOCK(&pAd->TimerQLock, irqFlags);
586 VOID RT2870_WatchDog(IN RTMP_ADAPTER *pAd)
588 PHT_TX_CONTEXT pHTTXContext;
592 BOOLEAN needDumpSeq = FALSE;
597 RTMP_IO_READ32(pAd, TXRXQ_PCNT, &MACValue);
598 if ((MACValue & 0xff) !=0 )
600 DBGPRINT(RT_DEBUG_TRACE, ("TX QUEUE 0 Not EMPTY(Value=0x%0x). !!!!!!!!!!!!!!!\n", MACValue));
601 RTMP_IO_WRITE32(pAd, PBF_CFG, 0xf40012);
602 while((MACValue &0xff) != 0 && (idx++ < 10))
604 RTMP_IO_READ32(pAd, TXRXQ_PCNT, &MACValue);
607 RTMP_IO_WRITE32(pAd, PBF_CFG, 0xf40006);
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 #ifdef DOT11_N_SUPPORT
715 // For Sigma debug, dump the ba_reordering sequence.
716 if((needDumpSeq == TRUE) && (pAd->CommonCfg.bDisableReordering == 0))
719 PBA_REC_ENTRY pBAEntry = NULL;
721 struct reordering_mpdu *mpdu_blk;
723 Idx = pAd->MacTab.Content[BSSID_WCID].BARecWcidArray[0];
725 pBAEntry = &pAd->BATable.BARecEntry[Idx];
726 if((pBAEntry->list.qlen > 0) && (pBAEntry->list.next != NULL))
728 DBGPRINT(RT_DEBUG_TRACE, ("NICUpdateRawCounters():The Queueing pkt in reordering buffer:\n"));
729 NdisAcquireSpinLock(&pBAEntry->RxReRingLock);
730 mpdu_blk = pBAEntry->list.next;
733 DBGPRINT(RT_DEBUG_TRACE, ("\t%d:Seq-%d, bAMSDU-%d!\n", count, mpdu_blk->Sequence, mpdu_blk->bAMSDU));
734 mpdu_blk = mpdu_blk->next;
738 DBGPRINT(RT_DEBUG_TRACE, ("\npBAEntry->LastIndSeq=%d!\n", pBAEntry->LastIndSeq));
739 NdisReleaseSpinLock(&pBAEntry->RxReRingLock);
742 #endif // DOT11_N_SUPPORT //
746 ========================================================================
748 Release allocated resources.
751 *dev Point to the PCI or USB device
752 pAd driver control block pointer
758 ========================================================================
760 static void _rtusb_disconnect(struct usb_device *dev, PRTMP_ADAPTER pAd)
762 struct net_device *net_dev = NULL;
765 DBGPRINT(RT_DEBUG_ERROR, ("rtusb_disconnect: unregister usbnet usb-%s-%s\n",
766 dev->bus->bus_name, dev->devpath));
771 printk("rtusb_disconnect: pAd == NULL!\n");
774 RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST);
778 // for debug, wait to show some messages to /proc system
784 net_dev = pAd->net_dev;
785 if (pAd->net_dev != NULL)
787 printk("rtusb_disconnect: unregister_netdev(), dev->name=%s!\n", net_dev->name);
788 unregister_netdev (pAd->net_dev);
791 flush_scheduled_work();
794 // free net_device memory
795 free_netdev(net_dev);
797 // free adapter memory
798 RTMPFreeAdapter(pAd);
800 // release a use of the usb device structure
804 DBGPRINT(RT_DEBUG_ERROR, (" RTUSB disconnect successfully\n"));
809 ========================================================================
811 Probe RT28XX chipset.
814 *dev Point to the PCI or USB device
816 *id_table Point to the PCI or USB device ID
822 ========================================================================
824 static int rtusb_probe (struct usb_interface *intf,
825 const struct usb_device_id *id)
828 return (int)rt28xx_probe((void *)intf, (void *)id, 0, &pAd);
832 static void rtusb_disconnect(struct usb_interface *intf)
834 struct usb_device *dev = interface_to_usbdev(intf);
838 pAd = usb_get_intfdata(intf);
839 usb_set_intfdata(intf, NULL);
841 _rtusb_disconnect(dev, pAd);
846 ========================================================================
848 Close kernel threads.
851 *pAd the raxx interface data pointer
857 ========================================================================
859 VOID RT28xxThreadTerminate(
860 IN RTMP_ADAPTER *pAd)
862 POS_COOKIE pObj = (POS_COOKIE) pAd->OS_Cookie;
866 // Sleep 50 milliseconds so pending io might finish normally
867 RTMPusecDelay(50000);
869 // We want to wait until all pending receives and sends to the
870 // device object. We cancel any
871 // irps. Wait until sends and receives have stopped.
872 RTUSBCancelPendingIRPs(pAd);
875 BUG_ON(pObj->TimerQThr_task == NULL);
876 CHECK_PID_LEGALITY(task_pid(pObj->TimerQThr_task))
878 POS_COOKIE pObj = (POS_COOKIE)pAd->OS_Cookie;
880 printk(KERN_DEBUG "Terminate the TimerQThr pid=%d!\n",
881 pid_nr(task_pid(pObj->TimerQThr_task)));
883 pAd->TimerFunc_kill = 1;
885 kthread_stop(pObj->TimerQThr_task);
886 pObj->TimerQThr_task = NULL;
889 BUG_ON(pObj->MLMEThr_task == NULL);
890 CHECK_PID_LEGALITY(task_pid(pObj->MLMEThr_task))
892 printk(KERN_DEBUG "Terminate the MLMEThr pid=%d!\n",
893 pid_nr(task_pid(pObj->MLMEThr_task)));
896 //RT28XX_MLME_HANDLER(pAd);
898 kthread_stop(pObj->MLMEThr_task);
899 pObj->MLMEThr_task = NULL;
902 BUG_ON(pObj->RTUSBCmdThr_task == NULL);
903 CHECK_PID_LEGALITY(task_pid(pObj->RTUSBCmdThr_task))
905 printk(KERN_DEBUG "Terminate the RTUSBCmdThr pid=%d!\n",
906 pid_nr(task_pid(pObj->RTUSBCmdThr_task)));
908 NdisAcquireSpinLock(&pAd->CmdQLock);
909 pAd->CmdQ.CmdQState = RT2870_THREAD_STOPED;
910 NdisReleaseSpinLock(&pAd->CmdQLock);
913 kthread_stop(pObj->RTUSBCmdThr_task);
914 pObj->RTUSBCmdThr_task = NULL;
920 pAd->CmdQ.CmdQState = RT2870_THREAD_UNKNOWN;
921 pAd->TimerFunc_kill = 0;
925 void kill_thread_task(IN PRTMP_ADAPTER pAd)
929 pObj = (POS_COOKIE) pAd->OS_Cookie;
931 tasklet_kill(&pObj->rx_done_task);
932 tasklet_kill(&pObj->mgmt_dma_done_task);
933 tasklet_kill(&pObj->ac0_dma_done_task);
934 tasklet_kill(&pObj->ac1_dma_done_task);
935 tasklet_kill(&pObj->ac2_dma_done_task);
936 tasklet_kill(&pObj->ac3_dma_done_task);
937 tasklet_kill(&pObj->hcca_dma_done_task);
938 tasklet_kill(&pObj->tbtt_task);
944 ========================================================================
946 Check the chipset vendor/product ID.
949 _dev_p Point to the PCI or USB device
956 ========================================================================
958 BOOLEAN RT28XXChipsetCheck(
961 struct usb_interface *intf = (struct usb_interface *)_dev_p;
962 struct usb_device *dev_p = interface_to_usbdev(intf);
966 for(i=0; i<rtusb_usb_id_len; i++)
968 if (dev_p->descriptor.idVendor == rtusb_usb_id[i].idVendor &&
969 dev_p->descriptor.idProduct == rtusb_usb_id[i].idProduct)
971 printk(KERN_DEBUG "rt2870: idVendor = 0x%x, idProduct = 0x%x\n",
972 dev_p->descriptor.idVendor, dev_p->descriptor.idProduct);
977 if (i == rtusb_usb_id_len)
979 printk("rt2870: Error! Device Descriptor not matching!\n");
988 ========================================================================
990 Init net device structure.
993 _dev_p Point to the PCI or USB device
994 *net_dev Point to the net device
995 *pAd the raxx interface data pointer
1002 ========================================================================
1004 BOOLEAN RT28XXNetDevInit(
1006 IN struct net_device *net_dev,
1007 IN RTMP_ADAPTER *pAd)
1009 struct usb_interface *intf = (struct usb_interface *)_dev_p;
1010 struct usb_device *dev_p = interface_to_usbdev(intf);
1013 pAd->config = &dev_p->config->desc;
1019 ========================================================================
1020 Routine Description:
1021 Init net device structure.
1024 _dev_p Point to the PCI or USB device
1025 *pAd the raxx interface data pointer
1032 ========================================================================
1034 BOOLEAN RT28XXProbePostConfig(
1036 IN RTMP_ADAPTER *pAd,
1039 struct usb_interface *intf = (struct usb_interface *)_dev_p;
1040 struct usb_host_interface *iface_desc;
1045 /* get the active interface descriptor */
1046 iface_desc = intf->cur_altsetting;
1048 /* get # of enpoints */
1049 pAd->NumberOfPipes = iface_desc->desc.bNumEndpoints;
1050 DBGPRINT(RT_DEBUG_TRACE,
1051 ("NumEndpoints=%d\n", iface_desc->desc.bNumEndpoints));
1053 /* Configure Pipes */
1056 for(i=0; i<pAd->NumberOfPipes; i++)
1058 if ((iface_desc->endpoint[i].desc.bmAttributes ==
1059 USB_ENDPOINT_XFER_BULK) &&
1060 ((iface_desc->endpoint[i].desc.bEndpointAddress &
1061 USB_ENDPOINT_DIR_MASK) == USB_DIR_IN))
1063 pAd->BulkInEpAddr = iface_desc->endpoint[i].desc.bEndpointAddress;
1064 pAd->BulkInMaxPacketSize = iface_desc->endpoint[i].desc.wMaxPacketSize;
1066 DBGPRINT_RAW(RT_DEBUG_TRACE,
1067 ("BULK IN MaximumPacketSize = %d\n", pAd->BulkInMaxPacketSize));
1068 DBGPRINT_RAW(RT_DEBUG_TRACE,
1069 ("EP address = 0x%2x\n", iface_desc->endpoint[i].desc.bEndpointAddress));
1071 else if ((iface_desc->endpoint[i].desc.bmAttributes ==
1072 USB_ENDPOINT_XFER_BULK) &&
1073 ((iface_desc->endpoint[i].desc.bEndpointAddress &
1074 USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT))
1076 // there are 6 bulk out EP. EP6 highest priority.
1077 // EP1-4 is EDCA. EP5 is HCCA.
1078 pAd->BulkOutEpAddr[BulkOutIdx++] = iface_desc->endpoint[i].desc.bEndpointAddress;
1079 pAd->BulkOutMaxPacketSize = iface_desc->endpoint[i].desc.wMaxPacketSize;
1081 DBGPRINT_RAW(RT_DEBUG_TRACE,
1082 ("BULK OUT MaximumPacketSize = %d\n", pAd->BulkOutMaxPacketSize));
1083 DBGPRINT_RAW(RT_DEBUG_TRACE,
1084 ("EP address = 0x%2x \n", iface_desc->endpoint[i].desc.bEndpointAddress));
1088 if (!(pAd->BulkInEpAddr && pAd->BulkOutEpAddr[0]))
1090 printk("%s: Could not find both bulk-in and bulk-out endpoints\n", __func__);
1099 ========================================================================
1100 Routine Description:
1104 *pAd the raxx interface data pointer
1110 ========================================================================
1112 VOID RT28XXDMADisable(
1113 IN RTMP_ADAPTER *pAd)
1121 ========================================================================
1122 Routine Description:
1126 *pAd the raxx interface data pointer
1132 ========================================================================
1134 VOID RT28XXDMAEnable(
1135 IN RTMP_ADAPTER *pAd)
1137 WPDMA_GLO_CFG_STRUC GloCfg;
1138 USB_DMA_CFG_STRUC UsbCfg;
1142 RTMP_IO_WRITE32(pAd, MAC_SYS_CTRL, 0x4);
1145 RTMP_IO_READ32(pAd, WPDMA_GLO_CFG, &GloCfg.word);
1146 if ((GloCfg.field.TxDMABusy == 0) && (GloCfg.field.RxDMABusy == 0))
1149 DBGPRINT(RT_DEBUG_TRACE, ("==> DMABusy\n"));
1150 RTMPusecDelay(1000);
1156 GloCfg.field.EnTXWriteBackDDONE = 1;
1157 GloCfg.field.EnableRxDMA = 1;
1158 GloCfg.field.EnableTxDMA = 1;
1159 DBGPRINT(RT_DEBUG_TRACE, ("<== WRITE DMA offset 0x208 = 0x%x\n", GloCfg.word));
1160 RTMP_IO_WRITE32(pAd, WPDMA_GLO_CFG, GloCfg.word);
1163 UsbCfg.field.phyclear = 0;
1164 /* usb version is 1.1,do not use bulk in aggregation */
1165 if (pAd->BulkInMaxPacketSize == 512)
1166 UsbCfg.field.RxBulkAggEn = 1;
1167 /* for last packet, PBF might use more than limited, so minus 2 to prevent from error */
1168 UsbCfg.field.RxBulkAggLmt = (MAX_RXBULK_SIZE /1024)-3;
1169 UsbCfg.field.RxBulkAggTOut = 0x80; /* 2006-10-18 */
1170 UsbCfg.field.RxBulkEn = 1;
1171 UsbCfg.field.TxBulkEn = 1;
1173 RTUSBWriteMACRegister(pAd, USB_DMA_CFG, UsbCfg.word);
1178 ========================================================================
1179 Routine Description:
1180 Write Beacon buffer to Asic.
1183 *pAd the raxx interface data pointer
1189 ========================================================================
1191 VOID RT28xx_UpdateBeaconToAsic(
1192 IN RTMP_ADAPTER *pAd,
1197 PUCHAR pBeaconFrame = NULL;
1200 BEACON_SYNC_STRUCT *pBeaconSync = pAd->CommonCfg.pBeaconSync;
1202 BOOLEAN bBcnReq = FALSE;
1206 if (pBeaconFrame == NULL)
1208 DBGPRINT(RT_DEBUG_ERROR,("pBeaconFrame is NULL!\n"));
1212 if (pBeaconSync == NULL)
1214 DBGPRINT(RT_DEBUG_ERROR,("pBeaconSync is NULL!\n"));
1218 //if ((pAd->WdsTab.Mode == WDS_BRIDGE_MODE) ||
1219 // ((pAd->ApCfg.MBSSID[apidx].MSSIDDev == NULL) || !(pAd->ApCfg.MBSSID[apidx].MSSIDDev->flags & IFF_UP))
1221 if (bBcnReq == FALSE)
1223 /* when the ra interface is down, do not send its beacon frame */
1224 /* clear all zero */
1225 for(i=0; i<TXWI_SIZE; i+=4) {
1226 RTMP_IO_WRITE32(pAd, pAd->BeaconOffset[bcn_idx] + i, 0x00);
1228 pBeaconSync->BeaconBitMap &= (~(BEACON_BITMAP_MASK & (1 << bcn_idx)));
1229 NdisZeroMemory(pBeaconSync->BeaconTxWI[bcn_idx], TXWI_SIZE);
1233 ptr = (PUCHAR)&pAd->BeaconTxWI;
1234 #ifdef RT_BIG_ENDIAN
1235 RTMPWIEndianChange(ptr, TYPE_TXWI);
1237 if (NdisEqualMemory(pBeaconSync->BeaconTxWI[bcn_idx], &pAd->BeaconTxWI, TXWI_SIZE) == FALSE)
1238 { // If BeaconTxWI changed, we need to rewrite the TxWI for the Beacon frames.
1239 pBeaconSync->BeaconBitMap &= (~(BEACON_BITMAP_MASK & (1 << bcn_idx)));
1240 NdisMoveMemory(pBeaconSync->BeaconTxWI[bcn_idx], &pAd->BeaconTxWI, TXWI_SIZE);
1243 if ((pBeaconSync->BeaconBitMap & (1 << bcn_idx)) != (1 << bcn_idx))
1245 for (i=0; i<TXWI_SIZE; i+=4) // 16-byte TXWI field
1247 longValue = *ptr + (*(ptr+1)<<8) + (*(ptr+2)<<16) + (*(ptr+3)<<24);
1248 RTMP_IO_WRITE32(pAd, pAd->BeaconOffset[bcn_idx] + i, longValue);
1253 ptr = pBeaconSync->BeaconBuf[bcn_idx];
1254 padding = (FrameLen & 0x01);
1255 NdisZeroMemory((PUCHAR)(pBeaconFrame + FrameLen), padding);
1256 FrameLen += padding;
1257 for (i = 0 ; i < FrameLen /*HW_BEACON_OFFSET*/; i += 2)
1259 if (NdisEqualMemory(ptr, pBeaconFrame, 2) == FALSE)
1261 NdisMoveMemory(ptr, pBeaconFrame, 2);
1262 //shortValue = *ptr + (*(ptr+1)<<8);
1263 //RTMP_IO_WRITE8(pAd, pAd->BeaconOffset[bcn_idx] + TXWI_SIZE + i, shortValue);
1264 RTUSBMultiWrite(pAd, pAd->BeaconOffset[bcn_idx] + TXWI_SIZE + i, ptr, 2);
1270 pBeaconSync->BeaconBitMap |= (1 << bcn_idx);
1276 VOID RT2870_BssBeaconStop(
1277 IN RTMP_ADAPTER *pAd)
1279 BEACON_SYNC_STRUCT *pBeaconSync;
1281 BOOLEAN Cancelled = TRUE;
1283 pBeaconSync = pAd->CommonCfg.pBeaconSync;
1284 if (pBeaconSync && pBeaconSync->EnableBeacon)
1289 #ifdef CONFIG_STA_SUPPORT
1290 IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
1292 NumOfBcn = MAX_MESH_NUM;
1294 #endif // CONFIG_STA_SUPPORT //
1296 RTMPCancelTimer(&pAd->CommonCfg.BeaconUpdateTimer, &Cancelled);
1298 for(i=0; i<NumOfBcn; i++)
1300 NdisZeroMemory(pBeaconSync->BeaconBuf[i], HW_BEACON_OFFSET);
1301 NdisZeroMemory(pBeaconSync->BeaconTxWI[i], TXWI_SIZE);
1303 for (offset=0; offset<HW_BEACON_OFFSET; offset+=4)
1304 RTMP_IO_WRITE32(pAd, pAd->BeaconOffset[i] + offset, 0x00);
1306 pBeaconSync->CapabilityInfoLocationInBeacon[i] = 0;
1307 pBeaconSync->TimIELocationInBeacon[i] = 0;
1309 pBeaconSync->BeaconBitMap = 0;
1310 pBeaconSync->DtimBitOn = 0;
1315 VOID RT2870_BssBeaconStart(
1316 IN RTMP_ADAPTER *pAd)
1319 BEACON_SYNC_STRUCT *pBeaconSync;
1320 // LARGE_INTEGER tsfTime, deltaTime;
1322 pBeaconSync = pAd->CommonCfg.pBeaconSync;
1323 if (pBeaconSync && pBeaconSync->EnableBeacon)
1328 #ifdef CONFIG_STA_SUPPORT
1329 IF_DEV_CONFIG_OPMODE_ON_STA(pAd)
1331 NumOfBcn = MAX_MESH_NUM;
1333 #endif // CONFIG_STA_SUPPORT //
1335 for(apidx=0; apidx<NumOfBcn; apidx++)
1337 UCHAR CapabilityInfoLocationInBeacon = 0;
1338 UCHAR TimIELocationInBeacon = 0;
1340 NdisZeroMemory(pBeaconSync->BeaconBuf[apidx], HW_BEACON_OFFSET);
1341 pBeaconSync->CapabilityInfoLocationInBeacon[apidx] = CapabilityInfoLocationInBeacon;
1342 pBeaconSync->TimIELocationInBeacon[apidx] = TimIELocationInBeacon;
1343 NdisZeroMemory(pBeaconSync->BeaconTxWI[apidx], TXWI_SIZE);
1345 pBeaconSync->BeaconBitMap = 0;
1346 pBeaconSync->DtimBitOn = 0;
1347 pAd->CommonCfg.BeaconUpdateTimer.Repeat = TRUE;
1349 pAd->CommonCfg.BeaconAdjust = 0;
1350 pAd->CommonCfg.BeaconFactor = 0xffffffff / (pAd->CommonCfg.BeaconPeriod << 10);
1351 pAd->CommonCfg.BeaconRemain = (0xffffffff % (pAd->CommonCfg.BeaconPeriod << 10)) + 1;
1352 printk("RT2870_BssBeaconStart:BeaconFactor=%d, BeaconRemain=%d!\n", pAd->CommonCfg.BeaconFactor, pAd->CommonCfg.BeaconRemain);
1353 RTMPSetTimer(&pAd->CommonCfg.BeaconUpdateTimer, pAd->CommonCfg.BeaconPeriod);
1359 VOID RT2870_BssBeaconInit(
1360 IN RTMP_ADAPTER *pAd)
1362 BEACON_SYNC_STRUCT *pBeaconSync;
1365 NdisAllocMemory(pAd->CommonCfg.pBeaconSync, sizeof(BEACON_SYNC_STRUCT), MEM_ALLOC_FLAG);
1366 if (pAd->CommonCfg.pBeaconSync)
1368 pBeaconSync = pAd->CommonCfg.pBeaconSync;
1369 NdisZeroMemory(pBeaconSync, sizeof(BEACON_SYNC_STRUCT));
1370 for(i=0; i < HW_BEACON_MAX_COUNT; i++)
1372 NdisZeroMemory(pBeaconSync->BeaconBuf[i], HW_BEACON_OFFSET);
1373 pBeaconSync->CapabilityInfoLocationInBeacon[i] = 0;
1374 pBeaconSync->TimIELocationInBeacon[i] = 0;
1375 NdisZeroMemory(pBeaconSync->BeaconTxWI[i], TXWI_SIZE);
1377 pBeaconSync->BeaconBitMap = 0;
1379 //RTMPInitTimer(pAd, &pAd->CommonCfg.BeaconUpdateTimer, GET_TIMER_FUNCTION(BeaconUpdateExec), pAd, TRUE);
1380 pBeaconSync->EnableBeacon = TRUE;
1385 VOID RT2870_BssBeaconExit(
1386 IN RTMP_ADAPTER *pAd)
1388 BEACON_SYNC_STRUCT *pBeaconSync;
1389 BOOLEAN Cancelled = TRUE;
1392 if (pAd->CommonCfg.pBeaconSync)
1394 pBeaconSync = pAd->CommonCfg.pBeaconSync;
1395 pBeaconSync->EnableBeacon = FALSE;
1396 RTMPCancelTimer(&pAd->CommonCfg.BeaconUpdateTimer, &Cancelled);
1397 pBeaconSync->BeaconBitMap = 0;
1399 for(i=0; i<HW_BEACON_MAX_COUNT; i++)
1401 NdisZeroMemory(pBeaconSync->BeaconBuf[i], HW_BEACON_OFFSET);
1402 pBeaconSync->CapabilityInfoLocationInBeacon[i] = 0;
1403 pBeaconSync->TimIELocationInBeacon[i] = 0;
1404 NdisZeroMemory(pBeaconSync->BeaconTxWI[i], TXWI_SIZE);
1407 NdisFreeMemory(pAd->CommonCfg.pBeaconSync, HW_BEACON_OFFSET * HW_BEACON_MAX_COUNT, 0);
1408 pAd->CommonCfg.pBeaconSync = NULL;
1412 VOID BeaconUpdateExec(
1413 IN PVOID SystemSpecific1,
1414 IN PVOID FunctionContext,
1415 IN PVOID SystemSpecific2,
1416 IN PVOID SystemSpecific3)
1418 PRTMP_ADAPTER pAd = (PRTMP_ADAPTER)FunctionContext;
1419 LARGE_INTEGER tsfTime_a;//, tsfTime_b, deltaTime_exp, deltaTime_ab;
1420 UINT32 delta, remain, remain_low, remain_high;
1421 // BOOLEAN positive;
1423 ReSyncBeaconTime(pAd);
1427 RTMP_IO_READ32(pAd, TSF_TIMER_DW0, &tsfTime_a.u.LowPart);
1428 RTMP_IO_READ32(pAd, TSF_TIMER_DW1, &tsfTime_a.u.HighPart);
1431 //positive=getDeltaTime(tsfTime_a, expectedTime, &deltaTime_exp);
1432 remain_high = pAd->CommonCfg.BeaconRemain * tsfTime_a.u.HighPart;
1433 remain_low = tsfTime_a.u.LowPart % (pAd->CommonCfg.BeaconPeriod << 10);
1434 remain = (remain_high + remain_low)%(pAd->CommonCfg.BeaconPeriod << 10);
1435 delta = (pAd->CommonCfg.BeaconPeriod << 10) - remain;
1437 pAd->CommonCfg.BeaconUpdateTimer.TimerValue = (delta >> 10) + 10;