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 *************************************************************************
34 -------- ---------- ----------------------------------------------
35 Name Date Modification logs
36 Paul Lin 06-25-2004 created
41 #include "../rt_config.h"
44 ========================================================================
46 Routine Description: NIC initialization complete
56 ========================================================================
59 static int RTUSBFirmwareRun(struct rt_rtmp_adapter *pAd)
63 Status = RTUSB_VendorRequest(pAd,
64 USBD_TRANSFER_DIRECTION_OUT,
65 DEVICE_VENDOR_REQUEST_OUT,
66 0x01, 0x8, 0, NULL, 0);
72 ========================================================================
74 Routine Description: Write Firmware to NIC.
84 ========================================================================
86 int RTUSBFirmwareWrite(struct rt_rtmp_adapter *pAd,
87 const u8 *pFwImage, unsigned long FwLen)
91 /* unsigned long i; */
94 Status = RTUSBReadMACRegister(pAd, MAC_CSR0, &MacReg);
97 RTUSBMultiWrite(pAd, FIRMWARE_IMAGE_BASE, pFwImage, writeLen);
99 Status = RTUSBWriteMACRegister(pAd, 0x7014, 0xffffffff);
100 Status = RTUSBWriteMACRegister(pAd, 0x701c, 0xffffffff);
101 Status = RTUSBFirmwareRun(pAd);
103 /*2008/11/28:KH add to fix the dead rf frequency offset bug<-- */
104 RTMPusecDelay(10000);
105 RTUSBWriteMACRegister(pAd, H2M_MAILBOX_CSR, 0);
106 AsicSendCommandToMcu(pAd, 0x72, 0x00, 0x00, 0x00); /*reset rf by MCU supported by new firmware */
107 /*2008/11/28:KH add to fix the dead rf frequency offset bug--> */
112 int RTUSBVenderReset(struct rt_rtmp_adapter *pAd)
115 DBGPRINT_RAW(RT_DEBUG_ERROR, ("-->RTUSBVenderReset\n"));
116 Status = RTUSB_VendorRequest(pAd,
117 USBD_TRANSFER_DIRECTION_OUT,
118 DEVICE_VENDOR_REQUEST_OUT,
119 0x01, 0x1, 0, NULL, 0);
121 DBGPRINT_RAW(RT_DEBUG_ERROR, ("<--RTUSBVenderReset\n"));
126 ========================================================================
128 Routine Description: Read various length data from RT2573
138 ========================================================================
140 int RTUSBMultiRead(struct rt_rtmp_adapter *pAd,
141 u16 Offset, u8 *pData, u16 length)
145 Status = RTUSB_VendorRequest(pAd,
146 (USBD_TRANSFER_DIRECTION_IN |
147 USBD_SHORT_TRANSFER_OK),
148 DEVICE_VENDOR_REQUEST_IN, 0x7, 0, Offset,
155 ========================================================================
157 Routine Description: Write various length data to RT2573
167 ========================================================================
169 int RTUSBMultiWrite_OneByte(struct rt_rtmp_adapter *pAd,
170 u16 Offset, const u8 *pData)
174 /* TODO: In 2870, use this funciton carefully cause it's not stable. */
175 Status = RTUSB_VendorRequest(pAd,
176 USBD_TRANSFER_DIRECTION_OUT,
177 DEVICE_VENDOR_REQUEST_OUT,
178 0x6, 0, Offset, (u8 *)pData, 1);
183 int RTUSBMultiWrite(struct rt_rtmp_adapter *pAd,
184 u16 Offset, const u8 *pData, u16 length)
188 u16 index = 0, Value;
189 const u8 *pSrc = pData;
195 Value = (u16)(*pSrc | (*(pSrc + 1) << 8));
196 Status = RTUSBSingleWrite(pAd, Offset + index, Value);
200 } while (length > 0);
205 int RTUSBSingleWrite(struct rt_rtmp_adapter *pAd,
206 u16 Offset, u16 Value)
210 Status = RTUSB_VendorRequest(pAd,
211 USBD_TRANSFER_DIRECTION_OUT,
212 DEVICE_VENDOR_REQUEST_OUT,
213 0x2, Value, Offset, NULL, 0);
220 ========================================================================
222 Routine Description: Read 32-bit MAC register
232 ========================================================================
234 int RTUSBReadMACRegister(struct rt_rtmp_adapter *pAd,
235 u16 Offset, u32 *pValue)
240 Status = RTUSB_VendorRequest(pAd,
241 (USBD_TRANSFER_DIRECTION_IN |
242 USBD_SHORT_TRANSFER_OK),
243 DEVICE_VENDOR_REQUEST_IN, 0x7, 0, Offset,
246 *pValue = le2cpu32(localVal);
249 *pValue = 0xffffffff;
255 ========================================================================
257 Routine Description: Write 32-bit MAC register
267 ========================================================================
269 int RTUSBWriteMACRegister(struct rt_rtmp_adapter *pAd,
270 u16 Offset, u32 Value)
277 Status = RTUSBSingleWrite(pAd, Offset, (u16)(localVal & 0xffff));
279 RTUSBSingleWrite(pAd, Offset + 2,
280 (u16)((localVal & 0xffff0000) >> 16));
286 ========================================================================
288 Routine Description: Read 8-bit BBP register
298 ========================================================================
300 int RTUSBReadBBPRegister(struct rt_rtmp_adapter *pAd,
303 BBP_CSR_CFG_STRUC BbpCsr;
307 /* Verify the busy condition */
309 status = RTUSBReadMACRegister(pAd, BBP_CSR_CFG, &BbpCsr.word);
311 if (!(BbpCsr.field.Busy == BUSY))
314 DBGPRINT(RT_DEBUG_TRACE,
315 ("RTUSBReadBBPRegister(BBP_CSR_CFG_1):retry count=%d!\n",
318 } while ((i < RETRY_LIMIT)
319 && (!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST)));
321 if ((i == RETRY_LIMIT)
322 || (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST))) {
324 /* Read failed then Return Default value. */
326 *pValue = pAd->BbpWriteLatch[Id];
328 DBGPRINT_RAW(RT_DEBUG_ERROR,
329 ("Retry count exhausted or device removed!!!\n"));
330 return STATUS_UNSUCCESSFUL;
332 /* Prepare for write material */
334 BbpCsr.field.fRead = 1;
335 BbpCsr.field.Busy = 1;
336 BbpCsr.field.RegNum = Id;
337 RTUSBWriteMACRegister(pAd, BBP_CSR_CFG, BbpCsr.word);
340 /* Verify the busy condition */
342 status = RTUSBReadMACRegister(pAd, BBP_CSR_CFG, &BbpCsr.word);
344 if (!(BbpCsr.field.Busy == BUSY)) {
345 *pValue = (u8)BbpCsr.field.Value;
349 DBGPRINT(RT_DEBUG_TRACE,
350 ("RTUSBReadBBPRegister(BBP_CSR_CFG_2):retry count=%d!\n",
353 } while ((i < RETRY_LIMIT)
354 && (!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST)));
356 if ((i == RETRY_LIMIT)
357 || (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST))) {
359 /* Read failed then Return Default value. */
361 *pValue = pAd->BbpWriteLatch[Id];
363 DBGPRINT_RAW(RT_DEBUG_ERROR,
364 ("Retry count exhausted or device removed!!!\n"));
365 return STATUS_UNSUCCESSFUL;
368 return STATUS_SUCCESS;
372 ========================================================================
374 Routine Description: Write 8-bit BBP register
384 ========================================================================
386 int RTUSBWriteBBPRegister(struct rt_rtmp_adapter *pAd,
389 BBP_CSR_CFG_STRUC BbpCsr;
392 /* Verify the busy condition */
394 status = RTUSBReadMACRegister(pAd, BBP_CSR_CFG, &BbpCsr.word);
396 if (!(BbpCsr.field.Busy == BUSY))
399 DBGPRINT(RT_DEBUG_TRACE,
400 ("RTUSBWriteBBPRegister(BBP_CSR_CFG):retry count=%d!\n",
404 while ((i < RETRY_LIMIT)
405 && (!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST)));
407 if ((i == RETRY_LIMIT)
408 || (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST))) {
409 DBGPRINT_RAW(RT_DEBUG_ERROR,
410 ("Retry count exhausted or device removed!!!\n"));
411 return STATUS_UNSUCCESSFUL;
413 /* Prepare for write material */
415 BbpCsr.field.fRead = 0;
416 BbpCsr.field.Value = Value;
417 BbpCsr.field.Busy = 1;
418 BbpCsr.field.RegNum = Id;
419 RTUSBWriteMACRegister(pAd, BBP_CSR_CFG, BbpCsr.word);
421 pAd->BbpWriteLatch[Id] = Value;
423 return STATUS_SUCCESS;
427 ========================================================================
429 Routine Description: Write RF register through MAC
439 ========================================================================
441 int RTUSBWriteRFRegister(struct rt_rtmp_adapter *pAd, u32 Value)
443 PHY_CSR4_STRUC PhyCsr4;
447 NdisZeroMemory(&PhyCsr4, sizeof(PHY_CSR4_STRUC));
449 status = RTUSBReadMACRegister(pAd, RF_CSR_CFG0, &PhyCsr4.word);
451 if (!(PhyCsr4.field.Busy))
454 DBGPRINT(RT_DEBUG_TRACE,
455 ("RTUSBWriteRFRegister(RF_CSR_CFG0):retry count=%d!\n",
459 while ((i < RETRY_LIMIT)
460 && (!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST)));
462 if ((i == RETRY_LIMIT)
463 || (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST))) {
464 DBGPRINT_RAW(RT_DEBUG_ERROR,
465 ("Retry count exhausted or device removed!!!\n"));
466 return STATUS_UNSUCCESSFUL;
469 RTUSBWriteMACRegister(pAd, RF_CSR_CFG0, Value);
471 return STATUS_SUCCESS;
475 ========================================================================
487 ========================================================================
489 int RTUSBReadEEPROM(struct rt_rtmp_adapter *pAd,
490 u16 Offset, u8 *pData, u16 length)
492 int Status = STATUS_SUCCESS;
494 Status = RTUSB_VendorRequest(pAd,
495 (USBD_TRANSFER_DIRECTION_IN |
496 USBD_SHORT_TRANSFER_OK),
497 DEVICE_VENDOR_REQUEST_IN, 0x9, 0, Offset,
504 ========================================================================
516 ========================================================================
518 int RTUSBWriteEEPROM(struct rt_rtmp_adapter *pAd,
519 u16 Offset, u8 *pData, u16 length)
521 int Status = STATUS_SUCCESS;
523 Status = RTUSB_VendorRequest(pAd,
524 USBD_TRANSFER_DIRECTION_OUT,
525 DEVICE_VENDOR_REQUEST_OUT,
526 0x8, 0, Offset, pData, length);
531 int RTUSBReadEEPROM16(struct rt_rtmp_adapter *pAd,
532 u16 offset, u16 *pData)
537 status = RTUSBReadEEPROM(pAd, offset, (u8 *)(&localData), 2);
538 if (status == STATUS_SUCCESS)
539 *pData = le2cpu16(localData);
546 ========================================================================
558 ========================================================================
560 void RTUSBPutToSleep(struct rt_rtmp_adapter *pAd)
564 /* Timeout 0x40 x 50us */
565 value = (SLEEPCID << 16) + (OWNERMCU << 24) + (0x40 << 8) + 1;
566 RTUSBWriteMACRegister(pAd, 0x7010, value);
567 RTUSBWriteMACRegister(pAd, 0x404, 0x30);
568 /*RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS); */
569 DBGPRINT_RAW(RT_DEBUG_ERROR, ("Sleep Mailbox testvalue %x\n", value));
574 ========================================================================
586 ========================================================================
588 int RTUSBWakeUp(struct rt_rtmp_adapter *pAd)
592 Status = RTUSB_VendorRequest(pAd,
593 USBD_TRANSFER_DIRECTION_OUT,
594 DEVICE_VENDOR_REQUEST_OUT,
595 0x01, 0x09, 0, NULL, 0);
601 ========================================================================
613 ========================================================================
615 void RTUSBInitializeCmdQ(struct rt_cmdq *cmdq)
620 cmdq->CmdQState = RTMP_TASK_STAT_INITED;
624 ========================================================================
636 ========================================================================
638 int RTUSBEnqueueCmdFromNdis(struct rt_rtmp_adapter *pAd,
640 IN BOOLEAN SetInformation,
641 void *pInformationBuffer,
642 u32 InformationBufferLength)
645 struct rt_cmdqelmt *cmdqelmt = NULL;
646 struct rt_rtmp_os_task *pTask = &pAd->cmdQTask;
648 #ifdef KTHREAD_SUPPORT
649 if (pTask->kthread_task == NULL)
651 CHECK_PID_LEGALITY(pTask->taskPID) {
655 return (NDIS_STATUS_RESOURCES);
657 status = os_alloc_mem(pAd, (u8 **) (&cmdqelmt), sizeof(struct rt_cmdqelmt));
658 if ((status != NDIS_STATUS_SUCCESS) || (cmdqelmt == NULL))
659 return (NDIS_STATUS_RESOURCES);
661 cmdqelmt->buffer = NULL;
662 if (pInformationBuffer != NULL) {
664 os_alloc_mem(pAd, (u8 **) & cmdqelmt->buffer,
665 InformationBufferLength);
666 if ((status != NDIS_STATUS_SUCCESS)
667 || (cmdqelmt->buffer == NULL)) {
669 return (NDIS_STATUS_RESOURCES);
671 NdisMoveMemory(cmdqelmt->buffer, pInformationBuffer,
672 InformationBufferLength);
673 cmdqelmt->bufferlength = InformationBufferLength;
676 cmdqelmt->bufferlength = 0;
678 cmdqelmt->command = Oid;
679 cmdqelmt->CmdFromNdis = TRUE;
680 if (SetInformation == TRUE)
681 cmdqelmt->SetOperation = TRUE;
683 cmdqelmt->SetOperation = FALSE;
685 NdisAcquireSpinLock(&pAd->CmdQLock);
686 if (pAd->CmdQ.CmdQState & RTMP_TASK_CAN_DO_INSERT) {
687 EnqueueCmd((&pAd->CmdQ), cmdqelmt);
688 status = NDIS_STATUS_SUCCESS;
690 status = NDIS_STATUS_FAILURE;
692 NdisReleaseSpinLock(&pAd->CmdQLock);
694 if (status == NDIS_STATUS_FAILURE) {
695 if (cmdqelmt->buffer)
696 os_free_mem(pAd, cmdqelmt->buffer);
697 os_free_mem(pAd, cmdqelmt);
701 return (NDIS_STATUS_SUCCESS);
705 ========================================================================
717 ========================================================================
719 int RTUSBEnqueueInternalCmd(struct rt_rtmp_adapter *pAd,
721 void *pInformationBuffer,
722 u32 InformationBufferLength)
725 struct rt_cmdqelmt *cmdqelmt = NULL;
727 status = os_alloc_mem(pAd, (u8 **) & cmdqelmt, sizeof(struct rt_cmdqelmt));
728 if ((status != NDIS_STATUS_SUCCESS) || (cmdqelmt == NULL))
729 return (NDIS_STATUS_RESOURCES);
730 NdisZeroMemory(cmdqelmt, sizeof(struct rt_cmdqelmt));
732 if (InformationBufferLength > 0) {
734 os_alloc_mem(pAd, (u8 **) & cmdqelmt->buffer,
735 InformationBufferLength);
736 if ((status != NDIS_STATUS_SUCCESS)
737 || (cmdqelmt->buffer == NULL)) {
738 os_free_mem(pAd, cmdqelmt);
739 return (NDIS_STATUS_RESOURCES);
741 NdisMoveMemory(cmdqelmt->buffer, pInformationBuffer,
742 InformationBufferLength);
743 cmdqelmt->bufferlength = InformationBufferLength;
746 cmdqelmt->buffer = NULL;
747 cmdqelmt->bufferlength = 0;
750 cmdqelmt->command = Oid;
751 cmdqelmt->CmdFromNdis = FALSE;
753 if (cmdqelmt != NULL) {
754 NdisAcquireSpinLock(&pAd->CmdQLock);
755 if (pAd->CmdQ.CmdQState & RTMP_TASK_CAN_DO_INSERT) {
756 EnqueueCmd((&pAd->CmdQ), cmdqelmt);
757 status = NDIS_STATUS_SUCCESS;
759 status = NDIS_STATUS_FAILURE;
761 NdisReleaseSpinLock(&pAd->CmdQLock);
763 if (status == NDIS_STATUS_FAILURE) {
764 if (cmdqelmt->buffer)
765 os_free_mem(pAd, cmdqelmt->buffer);
766 os_free_mem(pAd, cmdqelmt);
770 return (NDIS_STATUS_SUCCESS);
774 ========================================================================
786 ========================================================================
788 void RTUSBDequeueCmd(struct rt_cmdq *cmdq, struct rt_cmdqelmt * * pcmdqelmt)
790 *pcmdqelmt = cmdq->head;
792 if (*pcmdqelmt != NULL) {
793 cmdq->head = cmdq->head->next;
801 ========================================================================
802 usb_control_msg - Builds a control urb, sends it off and waits for completion
803 @dev: pointer to the usb device to send the message to
804 @pipe: endpoint "pipe" to send the message to
805 @request: USB message request value
806 @requesttype: USB message request type value
807 @value: USB message value
808 @index: USB message index value
809 @data: pointer to the data to send
810 @size: length in bytes of the data to send
811 @timeout: time in jiffies to wait for the message to complete before
812 timing out (if 0 the wait is forever)
813 Context: !in_interrupt ()
815 This function sends a simple control message to a specified endpoint
816 and waits for the message to complete, or timeout.
817 If successful, it returns the number of bytes transferred, otherwise a negative error number.
819 Don't use this function from within an interrupt context, like a
820 bottom half handler. If you need an asynchronous message, or need to send
821 a message from within interrupt context, use usb_submit_urb()
822 If a thread in your driver uses this call, make sure your disconnect()
823 method can wait for it to complete. Since you don't have a handle on
824 the URB used, you can't cancel the request.
834 ========================================================================
836 int RTUSB_VendorRequest(struct rt_rtmp_adapter *pAd,
842 void *TransferBuffer,
843 u32 TransferBufferLength)
846 struct os_cookie *pObj = (struct os_cookie *)pAd->OS_Cookie;
848 if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST)) {
849 DBGPRINT(RT_DEBUG_ERROR, ("device disconnected\n"));
851 } else if (in_interrupt()) {
852 DBGPRINT(RT_DEBUG_ERROR,
853 ("in_interrupt, RTUSB_VendorRequest Request%02x Value%04x Offset%04x\n",
854 Request, Value, Index));
858 #define MAX_RETRY_COUNT 10
861 void *tmpBuf = TransferBuffer;
863 ret = down_interruptible(&(pAd->UsbVendorReq_semaphore));
864 if (pAd->UsbVendorReqBuf) {
865 ASSERT(TransferBufferLength < MAX_PARAM_BUFFER_SIZE);
867 tmpBuf = (void *)pAd->UsbVendorReqBuf;
868 NdisZeroMemory(pAd->UsbVendorReqBuf,
869 TransferBufferLength);
871 if (RequestType == DEVICE_VENDOR_REQUEST_OUT)
872 NdisMoveMemory(tmpBuf, TransferBuffer,
873 TransferBufferLength);
877 if (RequestType == DEVICE_VENDOR_REQUEST_OUT)
879 usb_control_msg(pObj->pUsb_Dev,
880 usb_sndctrlpipe(pObj->
883 RequestType, Value, Index,
885 TransferBufferLength,
886 CONTROL_TIMEOUT_JIFFIES);
887 else if (RequestType == DEVICE_VENDOR_REQUEST_IN)
889 usb_control_msg(pObj->pUsb_Dev,
890 usb_rcvctrlpipe(pObj->
893 RequestType, Value, Index,
895 TransferBufferLength,
896 CONTROL_TIMEOUT_JIFFIES);
898 DBGPRINT(RT_DEBUG_ERROR,
899 ("vendor request direction is failed\n"));
905 DBGPRINT(RT_DEBUG_OFF, ("#\n"));
908 } while ((ret < 0) && (retryCount < MAX_RETRY_COUNT));
910 if ((pAd->UsbVendorReqBuf)
911 && (RequestType == DEVICE_VENDOR_REQUEST_IN))
912 NdisMoveMemory(TransferBuffer, tmpBuf,
913 TransferBufferLength);
914 up(&(pAd->UsbVendorReq_semaphore));
917 DBGPRINT(RT_DEBUG_ERROR,
918 ("RTUSB_VendorRequest failed(%d),TxFlags=0x%x, ReqType=%s, Req=0x%x, Index=0x%x\n",
921 DEVICE_VENDOR_REQUEST_OUT ? "OUT" : "IN"),
924 DBGPRINT(RT_DEBUG_ERROR,
925 ("\tRequest Value=0x%04x!\n", Value));
927 if ((TransferBuffer != NULL)
928 && (TransferBufferLength > 0))
929 hex_dump("Failed TransferBuffer value",
930 TransferBuffer, TransferBufferLength);
936 return STATUS_SUCCESS;
938 return STATUS_UNSUCCESSFUL;
942 ========================================================================
945 Creates an IRP to submite an IOCTL_INTERNAL_USB_RESET_PORT
946 synchronously. Callers of this function must be running at
955 ========================================================================
957 int RTUSB_ResetDevice(struct rt_rtmp_adapter *pAd)
961 DBGPRINT_RAW(RT_DEBUG_TRACE, ("--->USB_ResetDevice\n"));
962 /*RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_RESET_IN_PROGRESS); */
966 void CMDHandler(struct rt_rtmp_adapter *pAd)
968 struct rt_cmdqelmt *cmdqelmt;
970 int NdisStatus = NDIS_STATUS_SUCCESS;
971 /* unsigned long Now = 0; */
973 /* unsigned long IrqFlags; */
975 while (pAd && pAd->CmdQ.size > 0) {
976 NdisStatus = NDIS_STATUS_SUCCESS;
978 NdisAcquireSpinLock(&pAd->CmdQLock);
979 RTUSBDequeueCmd(&pAd->CmdQ, &cmdqelmt);
980 NdisReleaseSpinLock(&pAd->CmdQLock);
982 if (cmdqelmt == NULL)
985 pData = cmdqelmt->buffer;
988 (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST)
989 || RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS))) {
990 switch (cmdqelmt->command) {
991 case CMDTHREAD_CHECK_GPIO:
996 /* Read GPIO pin2 as Hardware controlled radio state */
998 RTUSBReadMACRegister(pAd,
1003 pAd->StaCfg.bHwRadio =
1006 pAd->StaCfg.bHwRadio =
1010 if (pAd->StaCfg.bRadio !=
1011 (pAd->StaCfg.bHwRadio
1012 && pAd->StaCfg.bSwRadio)) {
1013 pAd->StaCfg.bRadio =
1022 ("!!! Radio On !!!\n"));
1026 /* Update extra information */
1032 ("!!! Radio Off !!!\n"));
1036 /* Update extra information */
1045 case CMDTHREAD_QKERIODIC_EXECUT:
1047 StaQuickResponeForRateUpExec(NULL, pAd,
1053 case CMDTHREAD_RESET_BULK_OUT:
1058 struct rt_ht_tx_context *pHTTXContext;
1059 /* struct rt_rtmp_tx_ring *pTxRing; */
1060 unsigned long IrqFlags;
1062 DBGPRINT_RAW(RT_DEBUG_TRACE,
1063 ("CmdThread : CMDTHREAD_RESET_BULK_OUT(ResetPipeid=0x%0x)===>\n",
1064 pAd->bulkResetPipeid));
1065 /* All transfers must be aborted or cancelled before attempting to reset the pipe. */
1066 /*RTUSBCancelPendingBulkOutIRP(pAd); */
1067 /* Wait 10ms to let previous packet that are already in HW FIFO to clear. by MAXLEE 12-25-2007 */
1070 RTUSBReadMACRegister(pAd,
1073 if ((MACValue & 0xf00000
1074 /*0x800000 */ ) == 0)
1077 RTMPusecDelay(10000);
1078 } while (Index < 100);
1080 RTUSBReadMACRegister(pAd, USB_DMA_CFG,
1082 /* To prevent Read Register error, we 2nd check the validity. */
1083 if ((MACValue & 0xc00000) == 0)
1084 RTUSBReadMACRegister(pAd,
1087 /* To prevent Read Register error, we 3rd check the validity. */
1088 if ((MACValue & 0xc00000) == 0)
1089 RTUSBReadMACRegister(pAd,
1092 MACValue |= 0x80000;
1093 RTUSBWriteMACRegister(pAd, USB_DMA_CFG,
1096 /* Wait 1ms to prevent next URB to bulkout before HW reset. by MAXLEE 12-25-2007 */
1097 RTMPusecDelay(1000);
1099 MACValue &= (~0x80000);
1100 RTUSBWriteMACRegister(pAd, USB_DMA_CFG,
1102 DBGPRINT_RAW(RT_DEBUG_TRACE,
1103 ("\tSet 0x2a0 bit19. Clear USB DMA TX path\n"));
1105 /* Wait 5ms to prevent next URB to bulkout before HW reset. by MAXLEE 12-25-2007 */
1106 /*RTMPusecDelay(5000); */
1110 BULKOUT_MGMT_RESET_FLAG) ==
1111 BULKOUT_MGMT_RESET_FLAG) {
1112 RTMP_CLEAR_FLAG(pAd,
1113 fRTMP_ADAPTER_BULKOUT_RESET);
1114 if (pAd->MgmtRing.TxSwFreeIdx <
1116 /* pMLMEContext->bWaitingBulkOut == TRUE */
1118 RTUSB_SET_BULK_FLAG(pAd,
1119 fRTUSB_BULK_OUT_MLME);
1121 RTUSBKickBulkOut(pAd);
1123 DBGPRINT_RAW(RT_DEBUG_TRACE,
1124 ("\tTX MGMT RECOVER Done!\n"));
1130 /*NdisAcquireSpinLock(&pAd->BulkOutLock[pAd->bulkResetPipeid]); */
1131 RTMP_INT_LOCK(&pAd->
1136 BulkOutPending[pAd->
1140 BulkOutPending[pAd->
1146 watchDogTxPendingCnt
1151 /* no matter what, clean the flag */
1152 RTMP_CLEAR_FLAG(pAd,
1153 fRTMP_ADAPTER_BULKOUT_RESET);
1155 /*NdisReleaseSpinLock(&pAd->BulkOutLock[pAd->bulkResetPipeid]); */
1156 RTMP_INT_UNLOCK(&pAd->
1170 RTUSBBulkOutDataPacketComplete);
1194 watchDogTxPendingCnt
1207 ("CmdThread : CMDTHREAD_RESET_BULK_OUT: Submit Tx URB failed %d\n",
1218 ("\tCMDTHREAD_RESET_BULK_OUT: TxContext[%d]:CWPos=%ld, NBPos=%ld, ENBPos=%ld, bCopy=%d, pending=%d!\n",
1224 NextBulkOutPosition,
1226 ENextBulkOutPosition,
1235 ("\t\tBulkOut Req=0x%lx, Complete=0x%lx, Other=0x%lx\n",
1241 BulkOutCompleteOther));
1250 ("\tCMDTHREAD_RESET_BULK_OUT: Submit Tx DATA URB for failed BulkReq(0x%lx) Done, status=%d!\n",
1262 /*NdisReleaseSpinLock(&pAd->BulkOutLock[pAd->bulkResetPipeid]); */
1263 /*RTMP_INT_UNLOCK(&pAd->BulkOutLock[pAd->bulkResetPipeid], IrqFlags); */
1267 ("CmdThread : TX DATA RECOVER FAIL for BulkReq(0x%lx) because BulkOutPending[%d] is TRUE!\n",
1279 struct rt_ht_tx_context *
1282 (struct rt_ht_tx_context *)
1287 struct rt_tx_context *
1290 (struct rt_tx_context *)
1297 struct rt_tx_context *
1300 (struct rt_tx_context *)
1303 struct rt_tx_context *
1306 (struct rt_tx_context *)
1310 if (pHTTXContext->IRPPending)
1338 ("\tTX Occupied by %d!\n",
1341 /* no matter what, clean the flag */
1342 RTMP_CLEAR_FLAG(pAd,
1343 fRTMP_ADAPTER_BULKOUT_RESET);
1345 RTMP_INT_UNLOCK(&pAd->
1351 RTUSB_SET_BULK_FLAG(pAd,
1352 (fRTUSB_BULK_OUT_DATA_NORMAL
1358 RTMPDeQueuePacket(pAd, FALSE,
1361 /*RTUSBKickBulkOut(pAd); */
1366 // Don't cancel BULKIN.
1367 while ((atomic_read(&pAd->PendingRx) > 0) &&
1368 (!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST)))
1370 if (atomic_read(&pAd->PendingRx) > 0)
1372 DBGPRINT_RAW(RT_DEBUG_ERROR, ("BulkIn IRP Pending!!cancel it!\n"));
1373 RTUSBCancelPendingBulkInIRP(pAd);
1375 RTMPusecDelay(100000);
1378 if ((atomic_read(&pAd->PendingRx) == 0) && (!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS)))
1382 pAd->NextRxBulkInReadIndex = 0; // Next Rx Read index
1383 pAd->NextRxBulkInIndex = 0; // Rx Bulk pointer
1384 for (i = 0; i < (RX_RING_SIZE); i++)
1386 struct rt_rx_context *pRxContext = &(pAd->RxContext[i]);
1388 pRxContext->pAd = pAd;
1389 pRxContext->InUse = FALSE;
1390 pRxContext->IRPPending = FALSE;
1391 pRxContext->Readable = FALSE;
1392 pRxContext->ReorderInUse = FALSE;
1395 RTUSBBulkReceive(pAd);
1396 DBGPRINT_RAW(RT_DEBUG_ERROR, ("RTUSBBulkReceive\n"));
1398 DBGPRINT_RAW(RT_DEBUG_TRACE,
1399 ("CmdThread : CMDTHREAD_RESET_BULK_OUT<===\n"));
1402 case CMDTHREAD_RESET_BULK_IN:
1403 DBGPRINT_RAW(RT_DEBUG_TRACE,
1404 ("CmdThread : CMDTHREAD_RESET_BULK_IN === >\n"));
1406 /* All transfers must be aborted or cancelled before attempting to reset the pipe. */
1411 /*while ((atomic_read(&pAd->PendingRx) > 0) && (!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST))) */
1412 if ((pAd->PendingRx > 0)
1416 fRTMP_ADAPTER_NIC_NOT_EXIST)))
1420 ("BulkIn IRP Pending!!!\n"));
1421 RTUSBCancelPendingBulkInIRP
1423 RTMPusecDelay(100000);
1428 /* Wait 10ms before reading register. */
1429 RTMPusecDelay(10000);
1431 RTUSBReadMACRegister(pAd, MAC_CSR0,
1434 if ((NT_SUCCESS(ntStatus) == TRUE) &&
1437 (fRTMP_ADAPTER_RESET_IN_PROGRESS
1438 | fRTMP_ADAPTER_RADIO_OFF |
1439 fRTMP_ADAPTER_HALT_IN_PROGRESS
1441 fRTMP_ADAPTER_NIC_NOT_EXIST)))))
1447 (fRTMP_ADAPTER_RESET_IN_PROGRESS
1448 | fRTMP_ADAPTER_RADIO_OFF
1450 fRTMP_ADAPTER_HALT_IN_PROGRESS
1452 fRTMP_ADAPTER_NIC_NOT_EXIST)))
1454 pAd->NextRxBulkInPosition =
1455 pAd->RxContext[pAd->
1458 DBGPRINT(RT_DEBUG_TRACE,
1459 ("BULK_IN_RESET: NBIIdx=0x%x,NBIRIdx=0x%x, BIRPos=0x%lx. BIReq=x%lx, BIComplete=0x%lx, BICFail0x%lx\n",
1463 NextRxBulkInReadIndex,
1465 NextRxBulkInPosition,
1467 pAd->BulkInComplete,
1469 BulkInCompleteFail));
1470 for (i = 0; i < RX_RING_SIZE;
1472 DBGPRINT(RT_DEBUG_TRACE,
1473 ("\tRxContext[%d]: IRPPending=%d, InUse=%d, Readable=%d!\n",
1487 DBGPRINT_RAW(RT_DEBUG_ERROR, ("==========================================\n"));
1489 pAd->NextRxBulkInReadIndex = 0; // Next Rx Read index
1490 pAd->NextRxBulkInIndex = 0; // Rx Bulk pointer
1491 for (i = 0; i < (RX_RING_SIZE); i++)
1493 struct rt_rx_context *pRxContext = &(pAd->RxContext[i]);
1495 pRxContext->pAd = pAd;
1496 pRxContext->InUse = FALSE;
1497 pRxContext->IRPPending = FALSE;
1498 pRxContext->Readable = FALSE;
1499 pRxContext->ReorderInUse = FALSE;
1502 RTMP_CLEAR_FLAG(pAd,
1503 fRTMP_ADAPTER_BULKIN_RESET);
1507 NumOfBulkInIRP; i++) {
1508 /*RTUSBBulkReceive(pAd); */
1509 struct rt_rx_context *pRxContext;
1512 unsigned long IrqFlags;
1514 RTMP_IRQ_LOCK(&pAd->
1520 NextRxBulkInIndex]);
1521 if ((pAd->PendingRx > 0)
1536 pRxContext->IRPPending =
1540 RTMP_IRQ_UNLOCK(&pAd->
1544 /* Init Rx context descriptor */
1545 RTUSBInitRxDesc(pAd,
1547 pUrb = pRxContext->pUrb;
1548 if ((ret = RTUSB_SUBMIT_URB(pUrb)) != 0) { /* fail */
1570 ("CMDTHREAD_RESET_BULK_IN: Submit Rx URB failed(%d), status=%d\n",
1574 } else { /* success */
1575 /*DBGPRINT(RT_DEBUG_TRACE, ("BIDone, Pend=%d,BIIdx=%d,BIRIdx=%d!\n", */
1576 /* pAd->PendingRx, pAd->NextRxBulkInIndex, pAd->NextRxBulkInReadIndex)); */
1579 ("CMDTHREAD_RESET_BULK_IN: Submit Rx URB Done, status=%d!\n",
1582 ASSERT((pRxContext->InUse == pRxContext->IRPPending));
1587 /* Card must be removed */
1588 if (NT_SUCCESS(ntStatus) !=
1591 fRTMP_ADAPTER_NIC_NOT_EXIST);
1594 ("CMDTHREAD_RESET_BULK_IN: Read Register Failed!Card must be removed!!\n\n"));
1598 ("CMDTHREAD_RESET_BULK_IN: Cannot do bulk in because flags(0x%lx) on !\n",
1603 DBGPRINT_RAW(RT_DEBUG_TRACE,
1604 ("CmdThread : CMDTHREAD_RESET_BULK_IN <===\n"));
1607 case CMDTHREAD_SET_ASIC_WCID:
1609 struct rt_set_asic_wcid SetAsicWcid;
1611 u32 MACValue, MACRValue = 0;
1613 *((struct rt_set_asic_wcid *)(pData));
1615 if (SetAsicWcid.WCID >=
1616 MAX_LEN_OF_MAC_TABLE)
1621 ((u8)SetAsicWcid.WCID) *
1624 DBGPRINT_RAW(RT_DEBUG_TRACE,
1625 ("CmdThread : CMDTHREAD_SET_ASIC_WCID : WCID = %ld, SetTid = %lx, DeleteTid = %lx.\n",
1628 SetAsicWcid.DeleteTid));
1631 Content[SetAsicWcid.WCID].
1634 Content[SetAsicWcid.WCID].
1637 Content[SetAsicWcid.WCID].
1640 Content[SetAsicWcid.WCID].Addr[0]);
1641 DBGPRINT_RAW(RT_DEBUG_TRACE,
1642 ("1-MACValue= %x,\n",
1644 RTUSBWriteMACRegister(pAd, offset,
1647 RTUSBReadMACRegister(pAd, offset + 4,
1649 if (SetAsicWcid.DeleteTid != 0xffffffff)
1651 (~SetAsicWcid.DeleteTid);
1652 if (SetAsicWcid.SetTid != 0xffffffff)
1654 (SetAsicWcid.SetTid);
1655 MACRValue &= 0xffff0000;
1659 Content[SetAsicWcid.WCID].
1661 pAd->MacTab.Content[SetAsicWcid.
1663 MACValue |= MACRValue;
1664 RTUSBWriteMACRegister(pAd, offset + 4,
1667 DBGPRINT_RAW(RT_DEBUG_TRACE,
1668 ("2-MACValue= %x,\n",
1673 case CMDTHREAD_SET_ASIC_WCID_CIPHER:
1675 struct rt_set_asic_wcid_attri SetAsicWcidAttri;
1678 SHAREDKEY_MODE_STRUC csr1;
1680 *((struct rt_set_asic_wcid_attri *)
1683 if (SetAsicWcidAttri.WCID >=
1684 MAX_LEN_OF_MAC_TABLE)
1688 MAC_WCID_ATTRIBUTE_BASE +
1689 ((u8)SetAsicWcidAttri.WCID) *
1692 DBGPRINT_RAW(RT_DEBUG_TRACE,
1693 ("Cmd : CMDTHREAD_SET_ASIC_WCID_CIPHER : WCID = %ld, Cipher = %lx.\n",
1694 SetAsicWcidAttri.WCID,
1695 SetAsicWcidAttri.Cipher));
1697 RTUSBReadMACRegister(pAd, offset,
1701 (((u8)SetAsicWcidAttri.
1704 RTUSBWriteMACRegister(pAd, offset,
1706 DBGPRINT_RAW(RT_DEBUG_TRACE,
1707 ("2-offset = %x , MACValue= %x,\n",
1708 offset, MACRValue));
1711 PAIRWISE_IVEIV_TABLE_BASE +
1712 ((u8)SetAsicWcidAttri.WCID) *
1713 HW_IVEIV_ENTRY_SIZE;
1715 if ((SetAsicWcidAttri.Cipher <=
1719 DefaultKeyId << 30);
1721 MACRValue |= (0x20000000);
1722 RTUSBWriteMACRegister(pAd, offset,
1724 DBGPRINT_RAW(RT_DEBUG_TRACE,
1725 ("2-offset = %x , MACValue= %x,\n",
1726 offset, MACRValue));
1729 /* Update cipher algorithm. WSTA always use BSS0 */
1731 /* for adhoc mode only ,because wep status slow than add key, when use zero config */
1732 if (pAd->StaCfg.BssType == BSS_ADHOC) {
1734 MAC_WCID_ATTRIBUTE_BASE;
1736 RTUSBReadMACRegister(pAd,
1739 MACRValue &= (~0xe);
1741 (((u8)SetAsicWcidAttri.
1744 RTUSBWriteMACRegister(pAd,
1748 /*Update group key cipher,,because wep status slow than add key, when use zero config */
1749 RTUSBReadMACRegister(pAd,
1750 SHARED_KEY_MODE_BASE
1757 csr1.field.Bss0Key0CipherAlg =
1758 SetAsicWcidAttri.Cipher;
1759 csr1.field.Bss0Key1CipherAlg =
1760 SetAsicWcidAttri.Cipher;
1762 RTUSBWriteMACRegister(pAd,
1763 SHARED_KEY_MODE_BASE
1773 /*Benson modified for USB interface, avoid in interrupt when write key, 20080724 --> */
1774 case RT_CMD_SET_KEY_TABLE: /*General call for AsicAddPairwiseKeyEntry() */
1776 struct rt_add_pairwise_key_entry KeyInfo;
1778 *((struct rt_add_pairwise_key_entry *)
1780 AsicAddPairwiseKeyEntry(pAd,
1789 case RT_CMD_SET_RX_WCID_TABLE: /*General call for RTMPAddWcidAttributeEntry() */
1791 struct rt_mac_table_entry *pEntry;
1793 u8 CipherAlg = CIPHER_NONE;
1796 pEntry = (struct rt_mac_table_entry *)(pData);
1798 RTMPAddWcidAttributeEntry(pAd,
1805 /*Benson modified for USB interface, avoid in interrupt when write key, 20080724 <-- */
1807 case CMDTHREAD_SET_CLIENT_MAC_ENTRY:
1809 struct rt_mac_table_entry *pEntry;
1810 pEntry = (struct rt_mac_table_entry *)pData;
1813 AsicRemovePairwiseKeyEntry(pAd,
1819 if ((pEntry->AuthMode <=
1820 Ndis802_11AuthModeAutoSwitch)
1821 && (pEntry->WepStatus ==
1822 Ndis802_11Encryption1Enabled))
1831 AsicUpdateWCIDIVEIV(pAd,
1836 AsicUpdateWCIDAttribute
1844 } else if (pEntry->AuthMode ==
1845 Ndis802_11AuthModeWPANone)
1854 AsicUpdateWCIDIVEIV(pAd,
1859 AsicUpdateWCIDAttribute
1869 /* Other case, disable engine. */
1870 /* Don't worry WPA key, we will add WPA Key after 4-Way handshaking. */
1874 MAC_WCID_ATTRIBUTE_BASE
1877 HW_WCID_ATTRI_SIZE);
1878 /* RX_PKEY_MODE:0 for no security; RX_KEY_TAB:0 for shared key table; BSS_IDX:0 */
1879 RTUSBWriteMACRegister
1884 AsicUpdateRxWCIDTable(pAd, pEntry->Aid,
1886 DBGPRINT(RT_DEBUG_TRACE,
1887 ("UpdateRxWCIDTable(): Aid=%d, Addr=%02x:%02x:%02x:%02x:%02x:%02x!\n",
1888 pEntry->Aid, pEntry->Addr[0],
1897 /* add by johnli, fix "in_interrupt" error when call "MacTableDeleteEntry" in Rx tasklet */
1898 case CMDTHREAD_UPDATE_PROTECT:
1900 AsicUpdateProtect(pAd, 0,
1907 case OID_802_11_ADD_WEP:
1911 struct rt_ndis_802_11_wep *pWepKey;
1913 DBGPRINT(RT_DEBUG_TRACE,
1914 ("CmdThread::OID_802_11_ADD_WEP \n"));
1916 pWepKey = (struct rt_ndis_802_11_wep *)pData;
1917 KeyIdx = pWepKey->KeyIndex & 0x0fffffff;
1919 /* it is a shared key */
1921 || ((pWepKey->KeyLength != 5)
1922 && (pWepKey->KeyLength !=
1925 NDIS_STATUS_INVALID_DATA;
1926 DBGPRINT(RT_DEBUG_ERROR,
1927 ("CmdThread::OID_802_11_ADD_WEP, INVALID_DATA!!\n"));
1930 pAd->SharedKey[BSS0][KeyIdx].
1932 (u8)pWepKey->KeyLength;
1933 NdisMoveMemory(pAd->
1942 SharedKey[BSS0][KeyIdx].
1948 /* Change the WEP cipher to CKIP cipher if CKIP KP on. */
1949 /* Funk UI or Meetinghouse UI will add ckip key from this path. */
1952 if (pAd->OpMode == OPMODE_STA) {
1954 Content[BSSID_WCID].
1961 Content[BSSID_WCID].
1962 PairwiseKey.KeyLen =
1967 pAd->SharedKey[BSS0][KeyIdx].
1968 CipherAlg = CipherAlg;
1970 KeyIndex & 0x80000000) {
1971 /* Default key for tx (shared key) */
1973 u32 WCIDAttri, Value;
1974 u16 offset, offset2;
1975 NdisZeroMemory(IVEIV,
1980 /* Add BSSID to WCTable. because this is Tx wep key. */
1981 /* WCID Attribute UDF:3, BSSIdx:3, Alg:3, Keytable:1=PAIRWISE KEY, BSSIdx is 0 */
1987 MAC_WCID_ATTRIBUTE_BASE
1990 HW_WCID_ATTRI_SIZE);
1991 RTUSBWriteMACRegister
1995 /* Specify key index to find shared key. */
1996 IVEIV[3] = (u8)(KeyIdx << 6); /*WEP Eiv bit off. groupkey index is not 0 */
1998 PAIRWISE_IVEIV_TABLE_BASE
2001 HW_IVEIV_ENTRY_SIZE);
2003 PAIRWISE_IVEIV_TABLE_BASE
2006 HW_IVEIV_ENTRY_SIZE);
2007 for (i = 0; i < 8;) {
2022 RTUSBWriteMACRegister
2026 RTUSBWriteMACRegister
2033 /* 2. WCID Attribute UDF:3, BSSIdx:3, Alg:3, Keytable:use share key, BSSIdx is 0 */
2041 MAC_WCID_ATTRIBUTE_BASE
2044 HW_WCID_ATTRI_SIZE);
2045 DBGPRINT(RT_DEBUG_TRACE,
2046 ("BSS0Mcast_WCID : offset = %x, WCIDAttri = %x\n",
2049 RTUSBWriteMACRegister
2054 AsicAddSharedKeyEntry(pAd, BSS0,
2062 DBGPRINT(RT_DEBUG_TRACE,
2063 ("CmdThread::OID_802_11_ADD_WEP (KeyIdx=%d, Len=%d-byte)\n",
2065 pWepKey->KeyLength));
2070 case CMDTHREAD_802_11_COUNTER_MEASURE:
2073 case CMDTHREAD_SET_GROUP_KEY:
2074 WpaStaGroupKeySetting(pAd);
2077 case CMDTHREAD_SET_PAIRWISE_KEY:
2078 WpaStaPairwiseKeySetting(pAd);
2081 case CMDTHREAD_SET_PSM_BIT:
2083 u16 *pPsm = (u16 *) pData;
2084 MlmeSetPsmBit(pAd, *pPsm);
2087 case CMDTHREAD_FORCE_WAKE_UP:
2088 AsicForceWakeup(pAd, TRUE);
2092 DBGPRINT(RT_DEBUG_ERROR,
2093 ("--> Control Thread !! ERROR !! Unknown(cmdqelmt->command=0x%x) !! \n",
2094 cmdqelmt->command));
2099 if (cmdqelmt->CmdFromNdis == TRUE) {
2100 if (cmdqelmt->buffer != NULL)
2101 os_free_mem(pAd, cmdqelmt->buffer);
2102 os_free_mem(pAd, cmdqelmt);
2104 if ((cmdqelmt->buffer != NULL)
2105 && (cmdqelmt->bufferlength != 0))
2106 os_free_mem(pAd, cmdqelmt->buffer);
2107 os_free_mem(pAd, cmdqelmt);
2109 } /* end of while */
2112 #endif /* RTMP_MAC_USB // */