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
58 //static int mlme_kill = 0; // Mlme kernel thread
59 //static int RTUSBCmd_kill = 0; // Command kernel thread
60 //static int TimerFunc_kill = 0; // TimerQ kernel thread
62 //static wait_queue_head_t timerWaitQ;
63 //static wait_queue_t waitQ;
65 extern INT __devinit rt28xx_probe(IN void *_dev_p, IN void *_dev_id_p,
66 IN UINT argc, OUT PRTMP_ADAPTER *ppAd);
70 struct usb_device_id rtusb_usb_id[] = RT2870_USB_DEVICES;
71 INT const rtusb_usb_id_len = sizeof(rtusb_usb_id) / sizeof(struct usb_device_id);
72 MODULE_DEVICE_TABLE(usb, rtusb_usb_id);
80 static int rt2870_suspend(struct usb_interface *intf, pm_message_t state);
81 static int rt2870_resume(struct usb_interface *intf);
82 #endif // CONFIG_PM //
84 /**************************************************************************/
85 /**************************************************************************/
86 //tested for kernel 2.6series
87 /**************************************************************************/
88 /**************************************************************************/
89 static int rtusb_probe (struct usb_interface *intf,
90 const struct usb_device_id *id);
91 static void rtusb_disconnect(struct usb_interface *intf);
93 struct usb_driver rtusb_driver = {
96 .disconnect=rtusb_disconnect,
97 .id_table=rtusb_usb_id,
100 suspend: rt2870_suspend,
101 resume: rt2870_resume,
107 VOID RT2860RejectPendingPackets(
108 IN PRTMP_ADAPTER pAd)
111 // clear TxSw packets
114 static int rt2870_suspend(
115 struct usb_interface *intf,
118 struct net_device *net_dev;
119 PRTMP_ADAPTER pAd = usb_get_intfdata(intf);
122 DBGPRINT(RT_DEBUG_TRACE, ("===> rt2870_suspend()\n"));
123 net_dev = pAd->net_dev;
124 netif_device_detach(net_dev);
126 pAd->PM_FlgSuspend = 1;
127 if (netif_running(net_dev)) {
128 RTUSBCancelPendingBulkInIRP(pAd);
129 RTUSBCancelPendingBulkOutIRP(pAd);
131 DBGPRINT(RT_DEBUG_TRACE, ("<=== rt2870_suspend()\n"));
135 static int rt2870_resume(
136 struct usb_interface *intf)
138 struct net_device *net_dev;
139 PRTMP_ADAPTER pAd = usb_get_intfdata(intf);
142 DBGPRINT(RT_DEBUG_TRACE, ("===> rt2870_resume()\n"));
144 pAd->PM_FlgSuspend = 0;
145 net_dev = pAd->net_dev;
146 netif_device_attach(net_dev);
147 netif_start_queue(net_dev);
148 netif_carrier_on(net_dev);
149 netif_wake_queue(net_dev);
151 DBGPRINT(RT_DEBUG_TRACE, ("<=== rt2870_resume()\n"));
154 #endif // CONFIG_PM //
157 // Init driver module
158 INT __init rtusb_init(void)
160 printk("rtusb init --->\n");
161 return usb_register(&rtusb_driver);
164 // Deinit driver module
165 VOID __exit rtusb_exit(void)
167 usb_deregister(&rtusb_driver);
168 printk("<--- rtusb exit\n");
171 module_init(rtusb_init);
172 module_exit(rtusb_exit);
177 /*--------------------------------------------------------------------- */
178 /* function declarations */
179 /*--------------------------------------------------------------------- */
182 ========================================================================
187 *Context the pAd, driver control block pointer
193 ========================================================================
198 PRTMP_ADAPTER pAd = (PRTMP_ADAPTER)Context;
202 pObj = (POS_COOKIE)pAd->OS_Cookie;
204 rtmp_os_thread_init("rt2870MlmeThread", (PVOID)&(pAd->mlmeComplete));
206 while (pAd->mlme_kill == 0)
208 /* lock the device pointers */
209 //down(&(pAd->mlme_semaphore));
210 status = down_interruptible(&(pAd->mlme_semaphore));
212 /* lock the device pointers , need to check if required*/
213 //down(&(pAd->usbdev_semaphore));
215 if (!pAd->PM_FlgSuspend)
218 /* unlock the device pointers */
219 //up(&(pAd->usbdev_semaphore));
222 RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS);
227 /* notify the exit routine that we're actually exiting now
229 * complete()/wait_for_completion() is similar to up()/down(),
230 * except that complete() is safe in the case where the structure
231 * is getting deleted in a parallel mode of execution (i.e. just
232 * after the down() -- that's necessary for the thread-shutdown
235 * complete_and_exit() goes even further than this -- it is safe in
236 * the case that the thread of the caller is going away (not just
237 * the structure) -- this is necessary for the module-remove case.
238 * This is important in preemption kernels, which transfer the flow
239 * of execution immediately upon a complete().
241 DBGPRINT(RT_DEBUG_TRACE,( "<---%s\n",__func__));
243 pObj->MLMEThr_pid = NULL;
245 complete_and_exit (&pAd->mlmeComplete, 0);
252 ========================================================================
254 USB command kernel thread.
257 *Context the pAd, driver control block pointer
263 ========================================================================
268 PRTMP_ADAPTER pAd = (PRTMP_ADAPTER)Context;
272 pObj = (POS_COOKIE)pAd->OS_Cookie;
274 rtmp_os_thread_init("rt2870CmdThread", (PVOID)&(pAd->CmdQComplete));
276 NdisAcquireSpinLock(&pAd->CmdQLock);
277 pAd->CmdQ.CmdQState = RT2870_THREAD_RUNNING;
278 NdisReleaseSpinLock(&pAd->CmdQLock);
280 while (pAd->CmdQ.CmdQState == RT2870_THREAD_RUNNING)
282 /* lock the device pointers */
283 //down(&(pAd->RTUSBCmd_semaphore));
284 status = down_interruptible(&(pAd->RTUSBCmd_semaphore));
286 if (pAd->CmdQ.CmdQState == RT2870_THREAD_STOPED)
291 RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS);
294 /* lock the device pointers , need to check if required*/
295 //down(&(pAd->usbdev_semaphore));
297 if (!pAd->PM_FlgSuspend)
300 /* unlock the device pointers */
301 //up(&(pAd->usbdev_semaphore));
304 if (!pAd->PM_FlgSuspend)
305 { // Clear the CmdQElements.
306 CmdQElmt *pCmdQElmt = NULL;
308 NdisAcquireSpinLock(&pAd->CmdQLock);
309 pAd->CmdQ.CmdQState = RT2870_THREAD_STOPED;
310 while(pAd->CmdQ.size)
312 RTUSBDequeueCmd(&pAd->CmdQ, &pCmdQElmt);
315 if (pCmdQElmt->CmdFromNdis == TRUE)
317 if (pCmdQElmt->buffer != NULL)
318 NdisFreeMemory(pCmdQElmt->buffer, pCmdQElmt->bufferlength, 0);
320 NdisFreeMemory(pCmdQElmt, sizeof(CmdQElmt), 0);
324 if ((pCmdQElmt->buffer != NULL) && (pCmdQElmt->bufferlength != 0))
325 NdisFreeMemory(pCmdQElmt->buffer, pCmdQElmt->bufferlength, 0);
327 NdisFreeMemory(pCmdQElmt, sizeof(CmdQElmt), 0);
333 NdisReleaseSpinLock(&pAd->CmdQLock);
335 /* notify the exit routine that we're actually exiting now
337 * complete()/wait_for_completion() is similar to up()/down(),
338 * except that complete() is safe in the case where the structure
339 * is getting deleted in a parallel mode of execution (i.e. just
340 * after the down() -- that's necessary for the thread-shutdown
343 * complete_and_exit() goes even further than this -- it is safe in
344 * the case that the thread of the caller is going away (not just
345 * the structure) -- this is necessary for the module-remove case.
346 * This is important in preemption kernels, which transfer the flow
347 * of execution immediately upon a complete().
349 DBGPRINT(RT_DEBUG_TRACE,( "<---RTUSBCmdThread\n"));
351 pObj->RTUSBCmdThr_pid = NULL;
353 complete_and_exit (&pAd->CmdQComplete, 0);
359 static void RT2870_TimerQ_Handle(RTMP_ADAPTER *pAd)
362 RALINK_TIMER_STRUCT *pTimer;
363 RT2870_TIMER_ENTRY *pEntry;
364 unsigned long irqFlag;
366 while(!pAd->TimerFunc_kill)
368 // printk("waiting for event!\n");
371 status = down_interruptible(&(pAd->RTUSBTimer_semaphore));
373 if (pAd->TimerQ.status == RT2870_THREAD_STOPED)
377 while(pAd->TimerQ.pQHead)
379 RTMP_IRQ_LOCK(&pAd->TimerQLock, irqFlag);
380 pEntry = pAd->TimerQ.pQHead;
383 pTimer = pEntry->pRaTimer;
386 pAd->TimerQ.pQHead = pEntry->pNext;
387 if (pEntry == pAd->TimerQ.pQTail)
388 pAd->TimerQ.pQTail = NULL;
390 // return this queue entry to timerQFreeList.
391 pEntry->pNext = pAd->TimerQ.pQPollFreeList;
392 pAd->TimerQ.pQPollFreeList = pEntry;
394 RTMP_IRQ_UNLOCK(&pAd->TimerQLock, irqFlag);
398 if (pTimer->handle != NULL)
399 if (!pAd->PM_FlgSuspend)
400 pTimer->handle(NULL, (PVOID) pTimer->cookie, NULL, pTimer);
401 if ((pTimer->Repeat) && (pTimer->State == FALSE))
402 RTMP_OS_Add_Timer(&pTimer->TimerObj, pTimer->TimerValue);
408 pAd->TimerQ.status = RT2870_THREAD_STOPED;
409 RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS);
417 IN OUT PVOID Context)
422 pAd = (PRTMP_ADAPTER)Context;
423 pObj = (POS_COOKIE) pAd->OS_Cookie;
425 rtmp_os_thread_init("rt2870TimerQHandle", (PVOID)&(pAd->TimerQComplete));
427 RT2870_TimerQ_Handle(pAd);
429 /* notify the exit routine that we're actually exiting now
431 * complete()/wait_for_completion() is similar to up()/down(),
432 * except that complete() is safe in the case where the structure
433 * is getting deleted in a parallel mode of execution (i.e. just
434 * after the down() -- that's necessary for the thread-shutdown
437 * complete_and_exit() goes even further than this -- it is safe in
438 * the case that the thread of the caller is going away (not just
439 * the structure) -- this is necessary for the module-remove case.
440 * This is important in preemption kernels, which transfer the flow
441 * of execution immediately upon a complete().
443 DBGPRINT(RT_DEBUG_TRACE,( "<---%s\n",__func__));
445 pObj->TimerQThr_pid = NULL;
447 complete_and_exit(&pAd->TimerQComplete, 0);
453 RT2870_TIMER_ENTRY *RT2870_TimerQ_Insert(
454 IN RTMP_ADAPTER *pAd,
455 IN RALINK_TIMER_STRUCT *pTimer)
457 RT2870_TIMER_ENTRY *pQNode = NULL, *pQTail;
458 unsigned long irqFlags;
461 RTMP_IRQ_LOCK(&pAd->TimerQLock, irqFlags);
462 if (pAd->TimerQ.status & RT2870_THREAD_CAN_DO_INSERT)
464 if(pAd->TimerQ.pQPollFreeList)
466 pQNode = pAd->TimerQ.pQPollFreeList;
467 pAd->TimerQ.pQPollFreeList = pQNode->pNext;
469 pQNode->pRaTimer = pTimer;
470 pQNode->pNext = NULL;
472 pQTail = pAd->TimerQ.pQTail;
473 if (pAd->TimerQ.pQTail != NULL)
474 pQTail->pNext = pQNode;
475 pAd->TimerQ.pQTail = pQNode;
476 if (pAd->TimerQ.pQHead == NULL)
477 pAd->TimerQ.pQHead = pQNode;
479 RTMP_IRQ_UNLOCK(&pAd->TimerQLock, irqFlags);
482 up(&pAd->RTUSBTimer_semaphore);
483 //wake_up(&timerWaitQ);
487 RTMP_IRQ_UNLOCK(&pAd->TimerQLock, irqFlags);
493 BOOLEAN RT2870_TimerQ_Remove(
494 IN RTMP_ADAPTER *pAd,
495 IN RALINK_TIMER_STRUCT *pTimer)
497 RT2870_TIMER_ENTRY *pNode, *pPrev = NULL;
498 unsigned long irqFlags;
500 RTMP_IRQ_LOCK(&pAd->TimerQLock, irqFlags);
501 if (pAd->TimerQ.status >= RT2870_THREAD_INITED)
503 pNode = pAd->TimerQ.pQHead;
506 if (pNode->pRaTimer == pTimer)
509 pNode = pNode->pNext;
512 // Now move it to freeList queue.
515 if (pNode == pAd->TimerQ.pQHead)
516 pAd->TimerQ.pQHead = pNode->pNext;
517 if (pNode == pAd->TimerQ.pQTail)
518 pAd->TimerQ.pQTail = pPrev;
520 pPrev->pNext = pNode->pNext;
522 // return this queue entry to timerQFreeList.
523 pNode->pNext = pAd->TimerQ.pQPollFreeList;
524 pAd->TimerQ.pQPollFreeList = pNode;
527 RTMP_IRQ_UNLOCK(&pAd->TimerQLock, irqFlags);
533 void RT2870_TimerQ_Exit(RTMP_ADAPTER *pAd)
535 RT2870_TIMER_ENTRY *pTimerQ;
536 unsigned long irqFlags;
538 RTMP_IRQ_LOCK(&pAd->TimerQLock, irqFlags);
539 while (pAd->TimerQ.pQHead)
541 pTimerQ = pAd->TimerQ.pQHead;
542 pAd->TimerQ.pQHead = pTimerQ->pNext;
545 pAd->TimerQ.pQPollFreeList = NULL;
546 os_free_mem(pAd, pAd->TimerQ.pTimerQPoll);
547 pAd->TimerQ.pQTail = NULL;
548 pAd->TimerQ.pQHead = NULL;
549 pAd->TimerQ.status = RT2870_THREAD_STOPED;
550 RTMP_IRQ_UNLOCK(&pAd->TimerQLock, irqFlags);
555 void RT2870_TimerQ_Init(RTMP_ADAPTER *pAd)
558 RT2870_TIMER_ENTRY *pQNode, *pEntry;
559 unsigned long irqFlags;
561 NdisAllocateSpinLock(&pAd->TimerQLock);
563 RTMP_IRQ_LOCK(&pAd->TimerQLock, irqFlags);
564 NdisZeroMemory(&pAd->TimerQ, sizeof(pAd->TimerQ));
565 //InterlockedExchange(&pAd->TimerQ.count, 0);
567 /* Initialise the wait q head */
568 //init_waitqueue_head(&timerWaitQ);
570 os_alloc_mem(pAd, &pAd->TimerQ.pTimerQPoll, sizeof(RT2870_TIMER_ENTRY) * TIMER_QUEUE_SIZE_MAX);
571 if (pAd->TimerQ.pTimerQPoll)
574 pQNode = (RT2870_TIMER_ENTRY *)pAd->TimerQ.pTimerQPoll;
575 for (i = 0 ;i <TIMER_QUEUE_SIZE_MAX; i++)
577 pQNode->pNext = pEntry;
581 pAd->TimerQ.pQPollFreeList = pEntry;
582 pAd->TimerQ.pQHead = NULL;
583 pAd->TimerQ.pQTail = NULL;
584 pAd->TimerQ.status = RT2870_THREAD_INITED;
586 RTMP_IRQ_UNLOCK(&pAd->TimerQLock, irqFlags);
590 VOID RT2870_WatchDog(IN RTMP_ADAPTER *pAd)
592 PHT_TX_CONTEXT pHTTXContext;
596 BOOLEAN needDumpSeq = FALSE;
601 RTMP_IO_READ32(pAd, TXRXQ_PCNT, &MACValue);
602 if ((MACValue & 0xff) !=0 )
604 DBGPRINT(RT_DEBUG_TRACE, ("TX QUEUE 0 Not EMPTY(Value=0x%0x). !!!!!!!!!!!!!!!\n", MACValue));
605 RTMP_IO_WRITE32(pAd, PBF_CFG, 0xf40012);
606 while((MACValue &0xff) != 0 && (idx++ < 10))
608 RTMP_IO_READ32(pAd, TXRXQ_PCNT, &MACValue);
611 RTMP_IO_WRITE32(pAd, PBF_CFG, 0xf40006);
614 //PS packets use HCCA queue when dequeue from PS unicast queue (WiFi WPA2 MA9_DT1 for Marvell B STA)
617 if ((MACValue & 0xff00) !=0 )
619 DBGPRINT(RT_DEBUG_TRACE, ("TX QUEUE 1 Not EMPTY(Value=0x%0x). !!!!!!!!!!!!!!!\n", MACValue));
620 RTMP_IO_WRITE32(pAd, PBF_CFG, 0xf4000a);
621 while((MACValue &0xff00) != 0 && (idx++ < 10))
623 RTMP_IO_READ32(pAd, TXRXQ_PCNT, &MACValue);
626 RTMP_IO_WRITE32(pAd, PBF_CFG, 0xf40006);
630 if (pAd->watchDogRxOverFlowCnt >= 2)
632 DBGPRINT(RT_DEBUG_TRACE, ("Maybe the Rx Bulk-In hanged! Cancel the pending Rx bulks request!\n"));
633 if ((!RTMP_TEST_FLAG(pAd, (fRTMP_ADAPTER_RESET_IN_PROGRESS |
634 fRTMP_ADAPTER_BULKIN_RESET |
635 fRTMP_ADAPTER_HALT_IN_PROGRESS |
636 fRTMP_ADAPTER_NIC_NOT_EXIST))))
638 DBGPRINT(RT_DEBUG_TRACE, ("Call CMDTHREAD_RESET_BULK_IN to cancel the pending Rx Bulk!\n"));
639 RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_BULKIN_RESET);
640 RTUSBEnqueueInternalCmd(pAd, CMDTHREAD_RESET_BULK_IN, NULL, 0);
643 pAd->watchDogRxOverFlowCnt = 0;
647 for (idx = 0; idx < NUM_OF_TX_RING; idx++)
651 RTMP_IRQ_LOCK(&pAd->BulkOutLock[idx], irqFlags);
652 if ((pAd->BulkOutPending[idx] == TRUE) && pAd->watchDogTxPendingCnt)
654 pAd->watchDogTxPendingCnt[idx]++;
656 if ((pAd->watchDogTxPendingCnt[idx] > 2) &&
657 (!RTMP_TEST_FLAG(pAd, (fRTMP_ADAPTER_RESET_IN_PROGRESS | fRTMP_ADAPTER_HALT_IN_PROGRESS | fRTMP_ADAPTER_NIC_NOT_EXIST | fRTMP_ADAPTER_BULKOUT_RESET)))
660 // FIXME: Following code just support single bulk out. If you wanna support multiple bulk out. Modify it!
661 pHTTXContext = (PHT_TX_CONTEXT)(&pAd->TxContext[idx]);
662 if (pHTTXContext->IRPPending)
663 { // Check TxContext.
664 pUrb = pHTTXContext->pUrb;
666 else if (idx == MGMTPIPEIDX)
668 PTX_CONTEXT pMLMEContext, pNULLContext, pPsPollContext;
671 pMLMEContext = (PTX_CONTEXT)(pAd->MgmtRing.Cell[pAd->MgmtRing.TxDmaIdx].AllocVa);
672 pPsPollContext = (PTX_CONTEXT)(&pAd->PsPollContext);
673 pNULLContext = (PTX_CONTEXT)(&pAd->NullContext);
675 if (pMLMEContext->IRPPending)
677 ASSERT(pMLMEContext->IRPPending);
678 pUrb = pMLMEContext->pUrb;
680 else if (pNULLContext->IRPPending)
682 ASSERT(pNULLContext->IRPPending);
683 pUrb = pNULLContext->pUrb;
685 else if (pPsPollContext->IRPPending)
687 ASSERT(pPsPollContext->IRPPending);
688 pUrb = pPsPollContext->pUrb;
692 RTMP_IRQ_UNLOCK(&pAd->BulkOutLock[idx], irqFlags);
694 DBGPRINT(RT_DEBUG_TRACE, ("Maybe the Tx Bulk-Out hanged! Cancel the pending Tx bulks request of idx(%d)!\n", idx));
697 DBGPRINT(RT_DEBUG_TRACE, ("Unlink the pending URB!\n"));
699 RTUSB_UNLINK_URB(pUrb);
700 // Sleep 200 microseconds to give cancellation time to work
706 DBGPRINT(RT_DEBUG_ERROR, ("Unkonw bulkOut URB maybe hanged!!!!!!!!!!!!\n"));
711 RTMP_IRQ_UNLOCK(&pAd->BulkOutLock[idx], irqFlags);
716 RTMP_IRQ_UNLOCK(&pAd->BulkOutLock[idx], irqFlags);
720 // For Sigma debug, dump the ba_reordering sequence.
721 if((needDumpSeq == TRUE) && (pAd->CommonCfg.bDisableReordering == 0))
724 PBA_REC_ENTRY pBAEntry = NULL;
726 struct reordering_mpdu *mpdu_blk;
728 Idx = pAd->MacTab.Content[BSSID_WCID].BARecWcidArray[0];
730 pBAEntry = &pAd->BATable.BARecEntry[Idx];
731 if((pBAEntry->list.qlen > 0) && (pBAEntry->list.next != NULL))
733 DBGPRINT(RT_DEBUG_TRACE, ("NICUpdateRawCounters():The Queueing pkt in reordering buffer:\n"));
734 NdisAcquireSpinLock(&pBAEntry->RxReRingLock);
735 mpdu_blk = pBAEntry->list.next;
738 DBGPRINT(RT_DEBUG_TRACE, ("\t%d:Seq-%d, bAMSDU-%d!\n", count, mpdu_blk->Sequence, mpdu_blk->bAMSDU));
739 mpdu_blk = mpdu_blk->next;
743 DBGPRINT(RT_DEBUG_TRACE, ("\npBAEntry->LastIndSeq=%d!\n", pBAEntry->LastIndSeq));
744 NdisReleaseSpinLock(&pBAEntry->RxReRingLock);
750 ========================================================================
752 Release allocated resources.
755 *dev Point to the PCI or USB device
756 pAd driver control block pointer
762 ========================================================================
764 static void _rtusb_disconnect(struct usb_device *dev, PRTMP_ADAPTER pAd)
766 struct net_device *net_dev = NULL;
769 DBGPRINT(RT_DEBUG_ERROR, ("rtusb_disconnect: unregister usbnet usb-%s-%s\n",
770 dev->bus->bus_name, dev->devpath));
775 printk("rtusb_disconnect: pAd == NULL!\n");
778 RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST);
782 // for debug, wait to show some messages to /proc system
788 net_dev = pAd->net_dev;
789 if (pAd->net_dev != NULL)
791 printk("rtusb_disconnect: unregister_netdev(), dev->name=%s!\n", net_dev->name);
792 unregister_netdev (pAd->net_dev);
795 flush_scheduled_work();
798 // free net_device memory
799 free_netdev(net_dev);
801 // free adapter memory
802 RTMPFreeAdapter(pAd);
804 // release a use of the usb device structure
808 DBGPRINT(RT_DEBUG_ERROR, (" RTUSB disconnect successfully\n"));
813 ========================================================================
815 Probe RT28XX chipset.
818 *dev Point to the PCI or USB device
820 *id_table Point to the PCI or USB device ID
826 ========================================================================
828 static int rtusb_probe (struct usb_interface *intf,
829 const struct usb_device_id *id)
832 return (int)rt28xx_probe((void *)intf, (void *)id, 0, &pAd);
836 static void rtusb_disconnect(struct usb_interface *intf)
838 struct usb_device *dev = interface_to_usbdev(intf);
842 pAd = usb_get_intfdata(intf);
843 usb_set_intfdata(intf, NULL);
845 _rtusb_disconnect(dev, pAd);
850 ========================================================================
852 Close kernel threads.
855 *pAd the raxx interface data pointer
861 ========================================================================
863 VOID RT28xxThreadTerminate(
864 IN RTMP_ADAPTER *pAd)
866 POS_COOKIE pObj = (POS_COOKIE) pAd->OS_Cookie;
870 // Sleep 50 milliseconds so pending io might finish normally
871 RTMPusecDelay(50000);
873 // We want to wait until all pending receives and sends to the
874 // device object. We cancel any
875 // irps. Wait until sends and receives have stopped.
876 RTUSBCancelPendingIRPs(pAd);
879 if (pObj->MLMEThr_pid)
881 printk("Terminate the MLMEThr_pid=%d!\n", pid_nr(pObj->MLMEThr_pid));
884 //RT28XX_MLME_HANDLER(pAd);
886 ret = kill_pid(pObj->MLMEThr_pid, SIGTERM, 1);
889 printk (KERN_WARNING "%s: unable to Mlme thread, pid=%d, ret=%d!\n",
890 pAd->net_dev->name, pid_nr(pObj->MLMEThr_pid), ret);
894 //wait_for_completion (&pAd->notify);
895 wait_for_completion (&pAd->mlmeComplete);
896 pObj->MLMEThr_pid = NULL;
900 if (pObj->RTUSBCmdThr_pid >= 0)
902 printk("Terminate the RTUSBCmdThr_pid=%d!\n", pid_nr(pObj->RTUSBCmdThr_pid));
904 NdisAcquireSpinLock(&pAd->CmdQLock);
905 pAd->CmdQ.CmdQState = RT2870_THREAD_STOPED;
906 NdisReleaseSpinLock(&pAd->CmdQLock);
909 ret = kill_pid(pObj->RTUSBCmdThr_pid, SIGTERM, 1);
912 printk(KERN_WARNING "%s: unable to RTUSBCmd thread, pid=%d, ret=%d!\n",
913 pAd->net_dev->name, pid_nr(pObj->RTUSBCmdThr_pid), ret);
917 //wait_for_completion (&pAd->notify);
918 wait_for_completion (&pAd->CmdQComplete);
919 pObj->RTUSBCmdThr_pid = NULL;
922 if (pObj->TimerQThr_pid >= 0)
924 POS_COOKIE pObj = (POS_COOKIE)pAd->OS_Cookie;
926 printk("Terminate the TimerQThr_pid=%d!\n", pid_nr(pObj->TimerQThr_pid));
928 pAd->TimerFunc_kill = 1;
930 ret = kill_pid(pObj->TimerQThr_pid, SIGTERM, 1);
933 printk(KERN_WARNING "%s: unable to stop TimerQThread, pid=%d, ret=%d!\n",
934 pAd->net_dev->name, pid_nr(pObj->TimerQThr_pid), ret);
938 printk("wait_for_completion TimerQThr\n");
939 wait_for_completion(&pAd->TimerQComplete);
940 pObj->TimerQThr_pid = NULL;
945 pAd->CmdQ.CmdQState = RT2870_THREAD_UNKNOWN;
946 pAd->TimerFunc_kill = 0;
950 void kill_thread_task(IN PRTMP_ADAPTER pAd)
954 pObj = (POS_COOKIE) pAd->OS_Cookie;
956 tasklet_kill(&pObj->rx_done_task);
957 tasklet_kill(&pObj->mgmt_dma_done_task);
958 tasklet_kill(&pObj->ac0_dma_done_task);
959 tasklet_kill(&pObj->ac1_dma_done_task);
960 tasklet_kill(&pObj->ac2_dma_done_task);
961 tasklet_kill(&pObj->ac3_dma_done_task);
962 tasklet_kill(&pObj->hcca_dma_done_task);
963 tasklet_kill(&pObj->tbtt_task);
969 ========================================================================
971 Check the chipset vendor/product ID.
974 _dev_p Point to the PCI or USB device
981 ========================================================================
983 BOOLEAN RT28XXChipsetCheck(
986 struct usb_interface *intf = (struct usb_interface *)_dev_p;
987 struct usb_device *dev_p = interface_to_usbdev(intf);
991 for(i=0; i<rtusb_usb_id_len; i++)
993 if (dev_p->descriptor.idVendor == rtusb_usb_id[i].idVendor &&
994 dev_p->descriptor.idProduct == rtusb_usb_id[i].idProduct)
996 printk("rt2870: idVendor = 0x%x, idProduct = 0x%x\n",
997 dev_p->descriptor.idVendor, dev_p->descriptor.idProduct);
1002 if (i == rtusb_usb_id_len)
1004 printk("rt2870: Error! Device Descriptor not matching!\n");
1013 ========================================================================
1014 Routine Description:
1015 Init net device structure.
1018 _dev_p Point to the PCI or USB device
1019 *net_dev Point to the net device
1020 *pAd the raxx interface data pointer
1027 ========================================================================
1029 BOOLEAN RT28XXNetDevInit(
1031 IN struct net_device *net_dev,
1032 IN RTMP_ADAPTER *pAd)
1034 struct usb_interface *intf = (struct usb_interface *)_dev_p;
1035 struct usb_device *dev_p = interface_to_usbdev(intf);
1038 pAd->config = &dev_p->config->desc;
1044 ========================================================================
1045 Routine Description:
1046 Init net device structure.
1049 _dev_p Point to the PCI or USB device
1050 *pAd the raxx interface data pointer
1057 ========================================================================
1059 BOOLEAN RT28XXProbePostConfig(
1061 IN RTMP_ADAPTER *pAd,
1064 struct usb_interface *intf = (struct usb_interface *)_dev_p;
1065 struct usb_host_interface *iface_desc;
1070 /* get the active interface descriptor */
1071 iface_desc = intf->cur_altsetting;
1073 /* get # of enpoints */
1074 pAd->NumberOfPipes = iface_desc->desc.bNumEndpoints;
1075 DBGPRINT(RT_DEBUG_TRACE,
1076 ("NumEndpoints=%d\n", iface_desc->desc.bNumEndpoints));
1078 /* Configure Pipes */
1081 for(i=0; i<pAd->NumberOfPipes; i++)
1083 if ((iface_desc->endpoint[i].desc.bmAttributes ==
1084 USB_ENDPOINT_XFER_BULK) &&
1085 ((iface_desc->endpoint[i].desc.bEndpointAddress &
1086 USB_ENDPOINT_DIR_MASK) == USB_DIR_IN))
1088 pAd->BulkInEpAddr = iface_desc->endpoint[i].desc.bEndpointAddress;
1089 pAd->BulkInMaxPacketSize = iface_desc->endpoint[i].desc.wMaxPacketSize;
1091 DBGPRINT_RAW(RT_DEBUG_TRACE,
1092 ("BULK IN MaximumPacketSize = %d\n", pAd->BulkInMaxPacketSize));
1093 DBGPRINT_RAW(RT_DEBUG_TRACE,
1094 ("EP address = 0x%2x\n", iface_desc->endpoint[i].desc.bEndpointAddress));
1096 else if ((iface_desc->endpoint[i].desc.bmAttributes ==
1097 USB_ENDPOINT_XFER_BULK) &&
1098 ((iface_desc->endpoint[i].desc.bEndpointAddress &
1099 USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT))
1101 // there are 6 bulk out EP. EP6 highest priority.
1102 // EP1-4 is EDCA. EP5 is HCCA.
1103 pAd->BulkOutEpAddr[BulkOutIdx++] = iface_desc->endpoint[i].desc.bEndpointAddress;
1104 pAd->BulkOutMaxPacketSize = iface_desc->endpoint[i].desc.wMaxPacketSize;
1106 DBGPRINT_RAW(RT_DEBUG_TRACE,
1107 ("BULK OUT MaximumPacketSize = %d\n", pAd->BulkOutMaxPacketSize));
1108 DBGPRINT_RAW(RT_DEBUG_TRACE,
1109 ("EP address = 0x%2x \n", iface_desc->endpoint[i].desc.bEndpointAddress));
1113 if (!(pAd->BulkInEpAddr && pAd->BulkOutEpAddr[0]))
1115 printk("%s: Could not find both bulk-in and bulk-out endpoints\n", __func__);
1124 ========================================================================
1125 Routine Description:
1129 *pAd the raxx interface data pointer
1135 ========================================================================
1137 VOID RT28XXDMADisable(
1138 IN RTMP_ADAPTER *pAd)
1146 ========================================================================
1147 Routine Description:
1151 *pAd the raxx interface data pointer
1157 ========================================================================
1159 VOID RT28XXDMAEnable(
1160 IN RTMP_ADAPTER *pAd)
1162 WPDMA_GLO_CFG_STRUC GloCfg;
1163 USB_DMA_CFG_STRUC UsbCfg;
1167 RTMP_IO_WRITE32(pAd, MAC_SYS_CTRL, 0x4);
1170 RTMP_IO_READ32(pAd, WPDMA_GLO_CFG, &GloCfg.word);
1171 if ((GloCfg.field.TxDMABusy == 0) && (GloCfg.field.RxDMABusy == 0))
1174 DBGPRINT(RT_DEBUG_TRACE, ("==> DMABusy\n"));
1175 RTMPusecDelay(1000);
1181 GloCfg.field.EnTXWriteBackDDONE = 1;
1182 GloCfg.field.EnableRxDMA = 1;
1183 GloCfg.field.EnableTxDMA = 1;
1184 DBGPRINT(RT_DEBUG_TRACE, ("<== WRITE DMA offset 0x208 = 0x%x\n", GloCfg.word));
1185 RTMP_IO_WRITE32(pAd, WPDMA_GLO_CFG, GloCfg.word);
1188 UsbCfg.field.phyclear = 0;
1189 /* usb version is 1.1,do not use bulk in aggregation */
1190 if (pAd->BulkInMaxPacketSize == 512)
1191 UsbCfg.field.RxBulkAggEn = 1;
1192 /* for last packet, PBF might use more than limited, so minus 2 to prevent from error */
1193 UsbCfg.field.RxBulkAggLmt = (MAX_RXBULK_SIZE /1024)-3;
1194 UsbCfg.field.RxBulkAggTOut = 0x80; /* 2006-10-18 */
1195 UsbCfg.field.RxBulkEn = 1;
1196 UsbCfg.field.TxBulkEn = 1;
1198 RTUSBWriteMACRegister(pAd, USB_DMA_CFG, UsbCfg.word);
1203 ========================================================================
1204 Routine Description:
1205 Write Beacon buffer to Asic.
1208 *pAd the raxx interface data pointer
1214 ========================================================================
1216 VOID RT28xx_UpdateBeaconToAsic(
1217 IN RTMP_ADAPTER *pAd,
1222 PUCHAR pBeaconFrame = NULL;
1225 BEACON_SYNC_STRUCT *pBeaconSync = pAd->CommonCfg.pBeaconSync;
1227 // USHORT shortValue;
1228 BOOLEAN bBcnReq = FALSE;
1232 if (pBeaconFrame == NULL)
1234 DBGPRINT(RT_DEBUG_ERROR,("pBeaconFrame is NULL!\n"));
1238 if (pBeaconSync == NULL)
1240 DBGPRINT(RT_DEBUG_ERROR,("pBeaconSync is NULL!\n"));
1244 //if ((pAd->WdsTab.Mode == WDS_BRIDGE_MODE) ||
1245 // ((pAd->ApCfg.MBSSID[apidx].MSSIDDev == NULL) || !(pAd->ApCfg.MBSSID[apidx].MSSIDDev->flags & IFF_UP))
1247 if (bBcnReq == FALSE)
1249 /* when the ra interface is down, do not send its beacon frame */
1250 /* clear all zero */
1251 for(i=0; i<TXWI_SIZE; i+=4) {
1252 RTMP_IO_WRITE32(pAd, pAd->BeaconOffset[bcn_idx] + i, 0x00);
1254 pBeaconSync->BeaconBitMap &= (~(BEACON_BITMAP_MASK & (1 << bcn_idx)));
1255 NdisZeroMemory(pBeaconSync->BeaconTxWI[bcn_idx], TXWI_SIZE);
1259 ptr = (PUCHAR)&pAd->BeaconTxWI;
1261 if (NdisEqualMemory(pBeaconSync->BeaconTxWI[bcn_idx], &pAd->BeaconTxWI, TXWI_SIZE) == FALSE)
1262 { // If BeaconTxWI changed, we need to rewrite the TxWI for the Beacon frames.
1263 pBeaconSync->BeaconBitMap &= (~(BEACON_BITMAP_MASK & (1 << bcn_idx)));
1264 NdisMoveMemory(pBeaconSync->BeaconTxWI[bcn_idx], &pAd->BeaconTxWI, TXWI_SIZE);
1267 if ((pBeaconSync->BeaconBitMap & (1 << bcn_idx)) != (1 << bcn_idx))
1269 for (i=0; i<TXWI_SIZE; i+=4) // 16-byte TXWI field
1271 longValue = *ptr + (*(ptr+1)<<8) + (*(ptr+2)<<16) + (*(ptr+3)<<24);
1272 RTMP_IO_WRITE32(pAd, pAd->BeaconOffset[bcn_idx] + i, longValue);
1277 ptr = pBeaconSync->BeaconBuf[bcn_idx];
1278 padding = (FrameLen & 0x01);
1279 NdisZeroMemory((PUCHAR)(pBeaconFrame + FrameLen), padding);
1280 FrameLen += padding;
1281 for (i = 0 ; i < FrameLen /*HW_BEACON_OFFSET*/; i += 2)
1283 if (NdisEqualMemory(ptr, pBeaconFrame, 2) == FALSE)
1285 NdisMoveMemory(ptr, pBeaconFrame, 2);
1286 //shortValue = *ptr + (*(ptr+1)<<8);
1287 //RTMP_IO_WRITE8(pAd, pAd->BeaconOffset[bcn_idx] + TXWI_SIZE + i, shortValue);
1288 RTUSBMultiWrite(pAd, pAd->BeaconOffset[bcn_idx] + TXWI_SIZE + i, ptr, 2);
1294 pBeaconSync->BeaconBitMap |= (1 << bcn_idx);
1296 // For AP interface, set the DtimBitOn so that we can send Bcast/Mcast frame out after this beacon frame.
1302 VOID RT2870_BssBeaconStop(
1303 IN RTMP_ADAPTER *pAd)
1305 BEACON_SYNC_STRUCT *pBeaconSync;
1307 BOOLEAN Cancelled = TRUE;
1309 pBeaconSync = pAd->CommonCfg.pBeaconSync;
1310 if (pBeaconSync && pBeaconSync->EnableBeacon)
1314 NumOfBcn = MAX_MESH_NUM;
1316 RTMPCancelTimer(&pAd->CommonCfg.BeaconUpdateTimer, &Cancelled);
1318 for(i=0; i<NumOfBcn; i++)
1320 NdisZeroMemory(pBeaconSync->BeaconBuf[i], HW_BEACON_OFFSET);
1321 NdisZeroMemory(pBeaconSync->BeaconTxWI[i], TXWI_SIZE);
1323 for (offset=0; offset<HW_BEACON_OFFSET; offset+=4)
1324 RTMP_IO_WRITE32(pAd, pAd->BeaconOffset[i] + offset, 0x00);
1326 pBeaconSync->CapabilityInfoLocationInBeacon[i] = 0;
1327 pBeaconSync->TimIELocationInBeacon[i] = 0;
1329 pBeaconSync->BeaconBitMap = 0;
1330 pBeaconSync->DtimBitOn = 0;
1335 VOID RT2870_BssBeaconStart(
1336 IN RTMP_ADAPTER *pAd)
1339 BEACON_SYNC_STRUCT *pBeaconSync;
1340 // LARGE_INTEGER tsfTime, deltaTime;
1342 pBeaconSync = pAd->CommonCfg.pBeaconSync;
1343 if (pBeaconSync && pBeaconSync->EnableBeacon)
1347 NumOfBcn = MAX_MESH_NUM;
1349 for(apidx=0; apidx<NumOfBcn; apidx++)
1351 UCHAR CapabilityInfoLocationInBeacon = 0;
1352 UCHAR TimIELocationInBeacon = 0;
1354 NdisZeroMemory(pBeaconSync->BeaconBuf[apidx], HW_BEACON_OFFSET);
1355 pBeaconSync->CapabilityInfoLocationInBeacon[apidx] = CapabilityInfoLocationInBeacon;
1356 pBeaconSync->TimIELocationInBeacon[apidx] = TimIELocationInBeacon;
1357 NdisZeroMemory(pBeaconSync->BeaconTxWI[apidx], TXWI_SIZE);
1359 pBeaconSync->BeaconBitMap = 0;
1360 pBeaconSync->DtimBitOn = 0;
1361 pAd->CommonCfg.BeaconUpdateTimer.Repeat = TRUE;
1363 pAd->CommonCfg.BeaconAdjust = 0;
1364 pAd->CommonCfg.BeaconFactor = 0xffffffff / (pAd->CommonCfg.BeaconPeriod << 10);
1365 pAd->CommonCfg.BeaconRemain = (0xffffffff % (pAd->CommonCfg.BeaconPeriod << 10)) + 1;
1366 printk("RT2870_BssBeaconStart:BeaconFactor=%d, BeaconRemain=%d!\n", pAd->CommonCfg.BeaconFactor, pAd->CommonCfg.BeaconRemain);
1367 RTMPSetTimer(&pAd->CommonCfg.BeaconUpdateTimer, pAd->CommonCfg.BeaconPeriod);
1373 VOID RT2870_BssBeaconInit(
1374 IN RTMP_ADAPTER *pAd)
1376 BEACON_SYNC_STRUCT *pBeaconSync;
1379 NdisAllocMemory(pAd->CommonCfg.pBeaconSync, sizeof(BEACON_SYNC_STRUCT), MEM_ALLOC_FLAG);
1380 if (pAd->CommonCfg.pBeaconSync)
1382 pBeaconSync = pAd->CommonCfg.pBeaconSync;
1383 NdisZeroMemory(pBeaconSync, sizeof(BEACON_SYNC_STRUCT));
1384 for(i=0; i < HW_BEACON_MAX_COUNT; i++)
1386 NdisZeroMemory(pBeaconSync->BeaconBuf[i], HW_BEACON_OFFSET);
1387 pBeaconSync->CapabilityInfoLocationInBeacon[i] = 0;
1388 pBeaconSync->TimIELocationInBeacon[i] = 0;
1389 NdisZeroMemory(pBeaconSync->BeaconTxWI[i], TXWI_SIZE);
1391 pBeaconSync->BeaconBitMap = 0;
1393 //RTMPInitTimer(pAd, &pAd->CommonCfg.BeaconUpdateTimer, GET_TIMER_FUNCTION(BeaconUpdateExec), pAd, TRUE);
1394 pBeaconSync->EnableBeacon = TRUE;
1399 VOID RT2870_BssBeaconExit(
1400 IN RTMP_ADAPTER *pAd)
1402 BEACON_SYNC_STRUCT *pBeaconSync;
1403 BOOLEAN Cancelled = TRUE;
1406 if (pAd->CommonCfg.pBeaconSync)
1408 pBeaconSync = pAd->CommonCfg.pBeaconSync;
1409 pBeaconSync->EnableBeacon = FALSE;
1410 RTMPCancelTimer(&pAd->CommonCfg.BeaconUpdateTimer, &Cancelled);
1411 pBeaconSync->BeaconBitMap = 0;
1413 for(i=0; i<HW_BEACON_MAX_COUNT; i++)
1415 NdisZeroMemory(pBeaconSync->BeaconBuf[i], HW_BEACON_OFFSET);
1416 pBeaconSync->CapabilityInfoLocationInBeacon[i] = 0;
1417 pBeaconSync->TimIELocationInBeacon[i] = 0;
1418 NdisZeroMemory(pBeaconSync->BeaconTxWI[i], TXWI_SIZE);
1421 NdisFreeMemory(pAd->CommonCfg.pBeaconSync, HW_BEACON_OFFSET * HW_BEACON_MAX_COUNT, 0);
1422 pAd->CommonCfg.pBeaconSync = NULL;
1426 VOID BeaconUpdateExec(
1427 IN PVOID SystemSpecific1,
1428 IN PVOID FunctionContext,
1429 IN PVOID SystemSpecific2,
1430 IN PVOID SystemSpecific3)
1432 PRTMP_ADAPTER pAd = (PRTMP_ADAPTER)FunctionContext;
1433 LARGE_INTEGER tsfTime_a;//, tsfTime_b, deltaTime_exp, deltaTime_ab;
1434 UINT32 delta, remain, remain_low, remain_high;
1435 // BOOLEAN positive;
1437 ReSyncBeaconTime(pAd);
1441 RTMP_IO_READ32(pAd, TSF_TIMER_DW0, &tsfTime_a.u.LowPart);
1442 RTMP_IO_READ32(pAd, TSF_TIMER_DW1, &tsfTime_a.u.HighPart);
1445 //positive=getDeltaTime(tsfTime_a, expectedTime, &deltaTime_exp);
1446 remain_high = pAd->CommonCfg.BeaconRemain * tsfTime_a.u.HighPart;
1447 remain_low = tsfTime_a.u.LowPart % (pAd->CommonCfg.BeaconPeriod << 10);
1448 remain = (remain_high + remain_low)%(pAd->CommonCfg.BeaconPeriod << 10);
1449 delta = (pAd->CommonCfg.BeaconPeriod << 10) - remain;
1451 pAd->CommonCfg.BeaconUpdateTimer.TimerValue = (delta >> 10) + 10;