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",
403 } while ((i < RETRY_LIMIT)
404 && (!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST)));
406 if ((i == RETRY_LIMIT)
407 || (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST))) {
408 DBGPRINT_RAW(RT_DEBUG_ERROR,
409 ("Retry count exhausted or device removed!!!\n"));
410 return STATUS_UNSUCCESSFUL;
412 /* Prepare for write material */
414 BbpCsr.field.fRead = 0;
415 BbpCsr.field.Value = Value;
416 BbpCsr.field.Busy = 1;
417 BbpCsr.field.RegNum = Id;
418 RTUSBWriteMACRegister(pAd, BBP_CSR_CFG, BbpCsr.word);
420 pAd->BbpWriteLatch[Id] = Value;
422 return STATUS_SUCCESS;
426 ========================================================================
428 Routine Description: Write RF register through MAC
438 ========================================================================
440 int RTUSBWriteRFRegister(struct rt_rtmp_adapter *pAd, u32 Value)
442 PHY_CSR4_STRUC PhyCsr4;
446 NdisZeroMemory(&PhyCsr4, sizeof(PHY_CSR4_STRUC));
448 status = RTUSBReadMACRegister(pAd, RF_CSR_CFG0, &PhyCsr4.word);
450 if (!(PhyCsr4.field.Busy))
453 DBGPRINT(RT_DEBUG_TRACE,
454 ("RTUSBWriteRFRegister(RF_CSR_CFG0):retry count=%d!\n",
457 } while ((i < RETRY_LIMIT)
458 && (!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST)));
460 if ((i == RETRY_LIMIT)
461 || (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST))) {
462 DBGPRINT_RAW(RT_DEBUG_ERROR,
463 ("Retry count exhausted or device removed!!!\n"));
464 return STATUS_UNSUCCESSFUL;
467 RTUSBWriteMACRegister(pAd, RF_CSR_CFG0, Value);
469 return STATUS_SUCCESS;
473 ========================================================================
485 ========================================================================
487 int RTUSBReadEEPROM(struct rt_rtmp_adapter *pAd,
488 u16 Offset, u8 *pData, u16 length)
490 int Status = STATUS_SUCCESS;
492 Status = RTUSB_VendorRequest(pAd,
493 (USBD_TRANSFER_DIRECTION_IN |
494 USBD_SHORT_TRANSFER_OK),
495 DEVICE_VENDOR_REQUEST_IN, 0x9, 0, Offset,
502 ========================================================================
514 ========================================================================
516 int RTUSBWriteEEPROM(struct rt_rtmp_adapter *pAd,
517 u16 Offset, u8 *pData, u16 length)
519 int Status = STATUS_SUCCESS;
521 Status = RTUSB_VendorRequest(pAd,
522 USBD_TRANSFER_DIRECTION_OUT,
523 DEVICE_VENDOR_REQUEST_OUT,
524 0x8, 0, Offset, pData, length);
529 int RTUSBReadEEPROM16(struct rt_rtmp_adapter *pAd,
530 u16 offset, u16 *pData)
535 status = RTUSBReadEEPROM(pAd, offset, (u8 *)(&localData), 2);
536 if (status == STATUS_SUCCESS)
537 *pData = le2cpu16(localData);
544 ========================================================================
556 ========================================================================
558 void RTUSBPutToSleep(struct rt_rtmp_adapter *pAd)
562 /* Timeout 0x40 x 50us */
563 value = (SLEEPCID << 16) + (OWNERMCU << 24) + (0x40 << 8) + 1;
564 RTUSBWriteMACRegister(pAd, 0x7010, value);
565 RTUSBWriteMACRegister(pAd, 0x404, 0x30);
566 /*RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS); */
567 DBGPRINT_RAW(RT_DEBUG_ERROR, ("Sleep Mailbox testvalue %x\n", value));
572 ========================================================================
584 ========================================================================
586 int RTUSBWakeUp(struct rt_rtmp_adapter *pAd)
590 Status = RTUSB_VendorRequest(pAd,
591 USBD_TRANSFER_DIRECTION_OUT,
592 DEVICE_VENDOR_REQUEST_OUT,
593 0x01, 0x09, 0, NULL, 0);
599 ========================================================================
611 ========================================================================
613 void RTUSBInitializeCmdQ(struct rt_cmdq *cmdq)
618 cmdq->CmdQState = RTMP_TASK_STAT_INITED;
622 ========================================================================
634 ========================================================================
636 int RTUSBEnqueueCmdFromNdis(struct rt_rtmp_adapter *pAd,
638 IN BOOLEAN SetInformation,
639 void *pInformationBuffer,
640 u32 InformationBufferLength)
643 struct rt_cmdqelmt *cmdqelmt = NULL;
644 struct rt_rtmp_os_task *pTask = &pAd->cmdQTask;
646 #ifdef KTHREAD_SUPPORT
647 if (pTask->kthread_task == NULL)
649 CHECK_PID_LEGALITY(pTask->taskPID) {
653 return NDIS_STATUS_RESOURCES;
655 status = os_alloc_mem(pAd, (u8 **) (&cmdqelmt), sizeof(struct rt_cmdqelmt));
656 if ((status != NDIS_STATUS_SUCCESS) || (cmdqelmt == NULL))
657 return NDIS_STATUS_RESOURCES;
659 cmdqelmt->buffer = NULL;
660 if (pInformationBuffer != NULL) {
662 os_alloc_mem(pAd, (u8 **) & cmdqelmt->buffer,
663 InformationBufferLength);
664 if ((status != NDIS_STATUS_SUCCESS)
665 || (cmdqelmt->buffer == NULL)) {
667 return NDIS_STATUS_RESOURCES;
669 NdisMoveMemory(cmdqelmt->buffer, pInformationBuffer,
670 InformationBufferLength);
671 cmdqelmt->bufferlength = InformationBufferLength;
674 cmdqelmt->bufferlength = 0;
676 cmdqelmt->command = Oid;
677 cmdqelmt->CmdFromNdis = TRUE;
678 if (SetInformation == TRUE)
679 cmdqelmt->SetOperation = TRUE;
681 cmdqelmt->SetOperation = FALSE;
683 NdisAcquireSpinLock(&pAd->CmdQLock);
684 if (pAd->CmdQ.CmdQState & RTMP_TASK_CAN_DO_INSERT) {
685 EnqueueCmd((&pAd->CmdQ), cmdqelmt);
686 status = NDIS_STATUS_SUCCESS;
688 status = NDIS_STATUS_FAILURE;
690 NdisReleaseSpinLock(&pAd->CmdQLock);
692 if (status == NDIS_STATUS_FAILURE) {
693 if (cmdqelmt->buffer)
694 os_free_mem(pAd, cmdqelmt->buffer);
695 os_free_mem(pAd, cmdqelmt);
699 return NDIS_STATUS_SUCCESS;
703 ========================================================================
715 ========================================================================
717 int RTUSBEnqueueInternalCmd(struct rt_rtmp_adapter *pAd,
719 void *pInformationBuffer,
720 u32 InformationBufferLength)
723 struct rt_cmdqelmt *cmdqelmt = NULL;
725 status = os_alloc_mem(pAd, (u8 **) & cmdqelmt, sizeof(struct rt_cmdqelmt));
726 if ((status != NDIS_STATUS_SUCCESS) || (cmdqelmt == NULL))
727 return NDIS_STATUS_RESOURCES;
728 NdisZeroMemory(cmdqelmt, sizeof(struct rt_cmdqelmt));
730 if (InformationBufferLength > 0) {
732 os_alloc_mem(pAd, (u8 **) & cmdqelmt->buffer,
733 InformationBufferLength);
734 if ((status != NDIS_STATUS_SUCCESS)
735 || (cmdqelmt->buffer == NULL)) {
736 os_free_mem(pAd, cmdqelmt);
737 return NDIS_STATUS_RESOURCES;
739 NdisMoveMemory(cmdqelmt->buffer, pInformationBuffer,
740 InformationBufferLength);
741 cmdqelmt->bufferlength = InformationBufferLength;
744 cmdqelmt->buffer = NULL;
745 cmdqelmt->bufferlength = 0;
748 cmdqelmt->command = Oid;
749 cmdqelmt->CmdFromNdis = FALSE;
751 if (cmdqelmt != NULL) {
752 NdisAcquireSpinLock(&pAd->CmdQLock);
753 if (pAd->CmdQ.CmdQState & RTMP_TASK_CAN_DO_INSERT) {
754 EnqueueCmd((&pAd->CmdQ), cmdqelmt);
755 status = NDIS_STATUS_SUCCESS;
757 status = NDIS_STATUS_FAILURE;
759 NdisReleaseSpinLock(&pAd->CmdQLock);
761 if (status == NDIS_STATUS_FAILURE) {
762 if (cmdqelmt->buffer)
763 os_free_mem(pAd, cmdqelmt->buffer);
764 os_free_mem(pAd, cmdqelmt);
768 return NDIS_STATUS_SUCCESS;
772 ========================================================================
784 ========================================================================
786 void RTUSBDequeueCmd(struct rt_cmdq *cmdq, struct rt_cmdqelmt * * pcmdqelmt)
788 *pcmdqelmt = cmdq->head;
790 if (*pcmdqelmt != NULL) {
791 cmdq->head = cmdq->head->next;
799 ========================================================================
800 usb_control_msg - Builds a control urb, sends it off and waits for completion
801 @dev: pointer to the usb device to send the message to
802 @pipe: endpoint "pipe" to send the message to
803 @request: USB message request value
804 @requesttype: USB message request type value
805 @value: USB message value
806 @index: USB message index value
807 @data: pointer to the data to send
808 @size: length in bytes of the data to send
809 @timeout: time in jiffies to wait for the message to complete before
810 timing out (if 0 the wait is forever)
811 Context: !in_interrupt ()
813 This function sends a simple control message to a specified endpoint
814 and waits for the message to complete, or timeout.
815 If successful, it returns the number of bytes transferred, otherwise a negative error number.
817 Don't use this function from within an interrupt context, like a
818 bottom half handler. If you need an asynchronous message, or need to send
819 a message from within interrupt context, use usb_submit_urb()
820 If a thread in your driver uses this call, make sure your disconnect()
821 method can wait for it to complete. Since you don't have a handle on
822 the URB used, you can't cancel the request.
832 ========================================================================
834 int RTUSB_VendorRequest(struct rt_rtmp_adapter *pAd,
840 void *TransferBuffer,
841 u32 TransferBufferLength)
844 struct os_cookie *pObj = (struct os_cookie *)pAd->OS_Cookie;
846 if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST)) {
847 DBGPRINT(RT_DEBUG_ERROR, ("device disconnected\n"));
849 } else if (in_interrupt()) {
850 DBGPRINT(RT_DEBUG_ERROR,
851 ("in_interrupt, RTUSB_VendorRequest Request%02x Value%04x Offset%04x\n",
852 Request, Value, Index));
856 #define MAX_RETRY_COUNT 10
859 void *tmpBuf = TransferBuffer;
861 ret = down_interruptible(&(pAd->UsbVendorReq_semaphore));
862 if (pAd->UsbVendorReqBuf) {
863 ASSERT(TransferBufferLength < MAX_PARAM_BUFFER_SIZE);
865 tmpBuf = (void *)pAd->UsbVendorReqBuf;
866 NdisZeroMemory(pAd->UsbVendorReqBuf,
867 TransferBufferLength);
869 if (RequestType == DEVICE_VENDOR_REQUEST_OUT)
870 NdisMoveMemory(tmpBuf, TransferBuffer,
871 TransferBufferLength);
875 if (RequestType == DEVICE_VENDOR_REQUEST_OUT)
877 usb_control_msg(pObj->pUsb_Dev,
878 usb_sndctrlpipe(pObj->
881 RequestType, Value, Index,
883 TransferBufferLength,
884 CONTROL_TIMEOUT_JIFFIES);
885 else if (RequestType == DEVICE_VENDOR_REQUEST_IN)
887 usb_control_msg(pObj->pUsb_Dev,
888 usb_rcvctrlpipe(pObj->
891 RequestType, Value, Index,
893 TransferBufferLength,
894 CONTROL_TIMEOUT_JIFFIES);
896 DBGPRINT(RT_DEBUG_ERROR,
897 ("vendor request direction is failed\n"));
903 DBGPRINT(RT_DEBUG_OFF, ("#\n"));
906 } while ((ret < 0) && (retryCount < MAX_RETRY_COUNT));
908 if ((pAd->UsbVendorReqBuf)
909 && (RequestType == DEVICE_VENDOR_REQUEST_IN))
910 NdisMoveMemory(TransferBuffer, tmpBuf,
911 TransferBufferLength);
912 up(&(pAd->UsbVendorReq_semaphore));
915 DBGPRINT(RT_DEBUG_ERROR,
916 ("RTUSB_VendorRequest failed(%d),TxFlags=0x%x, ReqType=%s, Req=0x%x, Index=0x%x\n",
919 DEVICE_VENDOR_REQUEST_OUT ? "OUT" : "IN"),
922 DBGPRINT(RT_DEBUG_ERROR,
923 ("\tRequest Value=0x%04x!\n", Value));
925 if ((TransferBuffer != NULL)
926 && (TransferBufferLength > 0))
927 hex_dump("Failed TransferBuffer value",
928 TransferBuffer, TransferBufferLength);
934 return STATUS_SUCCESS;
936 return STATUS_UNSUCCESSFUL;
940 ========================================================================
943 Creates an IRP to submite an IOCTL_INTERNAL_USB_RESET_PORT
944 synchronously. Callers of this function must be running at
953 ========================================================================
955 int RTUSB_ResetDevice(struct rt_rtmp_adapter *pAd)
959 DBGPRINT_RAW(RT_DEBUG_TRACE, ("--->USB_ResetDevice\n"));
960 /*RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_RESET_IN_PROGRESS); */
964 void CMDHandler(struct rt_rtmp_adapter *pAd)
966 struct rt_cmdqelmt *cmdqelmt;
968 int NdisStatus = NDIS_STATUS_SUCCESS;
969 /* unsigned long Now = 0; */
971 /* unsigned long IrqFlags; */
973 while (pAd && pAd->CmdQ.size > 0) {
974 NdisStatus = NDIS_STATUS_SUCCESS;
976 NdisAcquireSpinLock(&pAd->CmdQLock);
977 RTUSBDequeueCmd(&pAd->CmdQ, &cmdqelmt);
978 NdisReleaseSpinLock(&pAd->CmdQLock);
980 if (cmdqelmt == NULL)
983 pData = cmdqelmt->buffer;
986 (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST)
987 || RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS))) {
988 switch (cmdqelmt->command) {
989 case CMDTHREAD_CHECK_GPIO:
994 /* Read GPIO pin2 as Hardware controlled radio state */
996 RTUSBReadMACRegister(pAd,
1001 pAd->StaCfg.bHwRadio =
1004 pAd->StaCfg.bHwRadio =
1008 if (pAd->StaCfg.bRadio !=
1009 (pAd->StaCfg.bHwRadio
1010 && pAd->StaCfg.bSwRadio)) {
1011 pAd->StaCfg.bRadio =
1020 ("!!! Radio On !!!\n"));
1024 /* Update extra information */
1030 ("!!! Radio Off !!!\n"));
1034 /* Update extra information */
1043 case CMDTHREAD_QKERIODIC_EXECUT:
1045 StaQuickResponeForRateUpExec(NULL, pAd,
1051 case CMDTHREAD_RESET_BULK_OUT:
1056 struct rt_ht_tx_context *pHTTXContext;
1057 /* struct rt_rtmp_tx_ring *pTxRing; */
1058 unsigned long IrqFlags;
1060 DBGPRINT_RAW(RT_DEBUG_TRACE,
1061 ("CmdThread : CMDTHREAD_RESET_BULK_OUT(ResetPipeid=0x%0x)===>\n",
1062 pAd->bulkResetPipeid));
1063 /* All transfers must be aborted or cancelled before attempting to reset the pipe. */
1064 /*RTUSBCancelPendingBulkOutIRP(pAd); */
1065 /* Wait 10ms to let previous packet that are already in HW FIFO to clear. by MAXLEE 12-25-2007 */
1068 RTUSBReadMACRegister(pAd,
1071 if ((MACValue & 0xf00000
1072 /*0x800000 */) == 0)
1075 RTMPusecDelay(10000);
1076 } while (Index < 100);
1078 RTUSBReadMACRegister(pAd, USB_DMA_CFG,
1080 /* To prevent Read Register error, we 2nd check the validity. */
1081 if ((MACValue & 0xc00000) == 0)
1082 RTUSBReadMACRegister(pAd,
1085 /* To prevent Read Register error, we 3rd check the validity. */
1086 if ((MACValue & 0xc00000) == 0)
1087 RTUSBReadMACRegister(pAd,
1090 MACValue |= 0x80000;
1091 RTUSBWriteMACRegister(pAd, USB_DMA_CFG,
1094 /* Wait 1ms to prevent next URB to bulkout before HW reset. by MAXLEE 12-25-2007 */
1095 RTMPusecDelay(1000);
1097 MACValue &= (~0x80000);
1098 RTUSBWriteMACRegister(pAd, USB_DMA_CFG,
1100 DBGPRINT_RAW(RT_DEBUG_TRACE,
1101 ("\tSet 0x2a0 bit19. Clear USB DMA TX path\n"));
1103 /* Wait 5ms to prevent next URB to bulkout before HW reset. by MAXLEE 12-25-2007 */
1104 /*RTMPusecDelay(5000); */
1108 BULKOUT_MGMT_RESET_FLAG) ==
1109 BULKOUT_MGMT_RESET_FLAG) {
1110 RTMP_CLEAR_FLAG(pAd,
1111 fRTMP_ADAPTER_BULKOUT_RESET);
1112 if (pAd->MgmtRing.TxSwFreeIdx <
1114 /* pMLMEContext->bWaitingBulkOut == TRUE */
1116 RTUSB_SET_BULK_FLAG(pAd,
1117 fRTUSB_BULK_OUT_MLME);
1119 RTUSBKickBulkOut(pAd);
1121 DBGPRINT_RAW(RT_DEBUG_TRACE,
1122 ("\tTX MGMT RECOVER Done!\n"));
1128 /*NdisAcquireSpinLock(&pAd->BulkOutLock[pAd->bulkResetPipeid]); */
1129 RTMP_INT_LOCK(&pAd->
1134 BulkOutPending[pAd->
1138 BulkOutPending[pAd->
1144 watchDogTxPendingCnt
1149 /* no matter what, clean the flag */
1150 RTMP_CLEAR_FLAG(pAd,
1151 fRTMP_ADAPTER_BULKOUT_RESET);
1153 /*NdisReleaseSpinLock(&pAd->BulkOutLock[pAd->bulkResetPipeid]); */
1154 RTMP_INT_UNLOCK(&pAd->
1168 RTUSBBulkOutDataPacketComplete);
1170 ret = RTUSB_SUBMIT_URB
1191 watchDogTxPendingCnt
1204 ("CmdThread : CMDTHREAD_RESET_BULK_OUT: Submit Tx URB failed %d\n",
1215 ("\tCMDTHREAD_RESET_BULK_OUT: TxContext[%d]:CWPos=%ld, NBPos=%ld, ENBPos=%ld, bCopy=%d, pending=%d!\n",
1221 NextBulkOutPosition,
1223 ENextBulkOutPosition,
1232 ("\t\tBulkOut Req=0x%lx, Complete=0x%lx, Other=0x%lx\n",
1238 BulkOutCompleteOther));
1247 ("\tCMDTHREAD_RESET_BULK_OUT: Submit Tx DATA URB for failed BulkReq(0x%lx) Done, status=%d!\n",
1259 /*NdisReleaseSpinLock(&pAd->BulkOutLock[pAd->bulkResetPipeid]); */
1260 /*RTMP_INT_UNLOCK(&pAd->BulkOutLock[pAd->bulkResetPipeid], IrqFlags); */
1264 ("CmdThread : TX DATA RECOVER FAIL for BulkReq(0x%lx) because BulkOutPending[%d] is TRUE!\n",
1276 struct rt_ht_tx_context *
1279 (struct rt_ht_tx_context *)
1284 struct rt_tx_context *
1287 (struct rt_tx_context *)
1294 struct rt_tx_context *
1297 (struct rt_tx_context *)
1300 struct rt_tx_context *
1303 (struct rt_tx_context *)
1307 if (pHTTXContext->IRPPending)
1335 ("\tTX Occupied by %d!\n",
1338 /* no matter what, clean the flag */
1339 RTMP_CLEAR_FLAG(pAd,
1340 fRTMP_ADAPTER_BULKOUT_RESET);
1342 RTMP_INT_UNLOCK(&pAd->
1348 RTUSB_SET_BULK_FLAG(pAd,
1349 (fRTUSB_BULK_OUT_DATA_NORMAL
1355 RTMPDeQueuePacket(pAd, FALSE,
1358 /*RTUSBKickBulkOut(pAd); */
1363 // Don't cancel BULKIN.
1364 while ((atomic_read(&pAd->PendingRx) > 0) &&
1365 (!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST)))
1367 if (atomic_read(&pAd->PendingRx) > 0)
1369 DBGPRINT_RAW(RT_DEBUG_ERROR, ("BulkIn IRP Pending!!cancel it!\n"));
1370 RTUSBCancelPendingBulkInIRP(pAd);
1372 RTMPusecDelay(100000);
1375 if ((atomic_read(&pAd->PendingRx) == 0) && (!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS)))
1379 pAd->NextRxBulkInReadIndex = 0; // Next Rx Read index
1380 pAd->NextRxBulkInIndex = 0; // Rx Bulk pointer
1381 for (i = 0; i < (RX_RING_SIZE); i++)
1383 struct rt_rx_context *pRxContext = &(pAd->RxContext[i]);
1385 pRxContext->pAd = pAd;
1386 pRxContext->InUse = FALSE;
1387 pRxContext->IRPPending = FALSE;
1388 pRxContext->Readable = FALSE;
1389 pRxContext->ReorderInUse = FALSE;
1392 RTUSBBulkReceive(pAd);
1393 DBGPRINT_RAW(RT_DEBUG_ERROR, ("RTUSBBulkReceive\n"));
1395 DBGPRINT_RAW(RT_DEBUG_TRACE,
1396 ("CmdThread : CMDTHREAD_RESET_BULK_OUT<===\n"));
1399 case CMDTHREAD_RESET_BULK_IN:
1400 DBGPRINT_RAW(RT_DEBUG_TRACE,
1401 ("CmdThread : CMDTHREAD_RESET_BULK_IN === >\n"));
1403 /* All transfers must be aborted or cancelled before attempting to reset the pipe. */
1407 /*while ((atomic_read(&pAd->PendingRx) > 0) && (!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST))) */
1408 if ((pAd->PendingRx > 0)
1412 fRTMP_ADAPTER_NIC_NOT_EXIST))) {
1415 ("BulkIn IRP Pending!!!\n"));
1416 RTUSBCancelPendingBulkInIRP
1418 RTMPusecDelay(100000);
1422 /* Wait 10ms before reading register. */
1423 RTMPusecDelay(10000);
1425 RTUSBReadMACRegister(pAd, MAC_CSR0,
1428 if ((NT_SUCCESS(ntStatus) == TRUE) &&
1431 (fRTMP_ADAPTER_RESET_IN_PROGRESS
1432 | fRTMP_ADAPTER_RADIO_OFF |
1433 fRTMP_ADAPTER_HALT_IN_PROGRESS
1435 fRTMP_ADAPTER_NIC_NOT_EXIST)))))
1441 (fRTMP_ADAPTER_RESET_IN_PROGRESS
1442 | fRTMP_ADAPTER_RADIO_OFF
1444 fRTMP_ADAPTER_HALT_IN_PROGRESS
1446 fRTMP_ADAPTER_NIC_NOT_EXIST)))
1448 pAd->NextRxBulkInPosition =
1449 pAd->RxContext[pAd->
1452 DBGPRINT(RT_DEBUG_TRACE,
1453 ("BULK_IN_RESET: NBIIdx=0x%x,NBIRIdx=0x%x, BIRPos=0x%lx. BIReq=x%lx, BIComplete=0x%lx, BICFail0x%lx\n",
1457 NextRxBulkInReadIndex,
1459 NextRxBulkInPosition,
1461 pAd->BulkInComplete,
1463 BulkInCompleteFail));
1464 for (i = 0; i < RX_RING_SIZE;
1466 DBGPRINT(RT_DEBUG_TRACE,
1467 ("\tRxContext[%d]: IRPPending=%d, InUse=%d, Readable=%d!\n",
1481 DBGPRINT_RAW(RT_DEBUG_ERROR, ("==========================================\n"));
1483 pAd->NextRxBulkInReadIndex = 0; // Next Rx Read index
1484 pAd->NextRxBulkInIndex = 0; // Rx Bulk pointer
1485 for (i = 0; i < (RX_RING_SIZE); i++)
1487 struct rt_rx_context *pRxContext = &(pAd->RxContext[i]);
1489 pRxContext->pAd = pAd;
1490 pRxContext->InUse = FALSE;
1491 pRxContext->IRPPending = FALSE;
1492 pRxContext->Readable = FALSE;
1493 pRxContext->ReorderInUse = FALSE;
1496 RTMP_CLEAR_FLAG(pAd,
1497 fRTMP_ADAPTER_BULKIN_RESET);
1501 NumOfBulkInIRP; i++) {
1502 /*RTUSBBulkReceive(pAd); */
1503 struct rt_rx_context *pRxContext;
1506 unsigned long IrqFlags;
1508 RTMP_IRQ_LOCK(&pAd->
1514 NextRxBulkInIndex]);
1515 if ((pAd->PendingRx > 0)
1530 pRxContext->IRPPending =
1534 RTMP_IRQ_UNLOCK(&pAd->
1538 /* Init Rx context descriptor */
1539 RTUSBInitRxDesc(pAd,
1541 pUrb = pRxContext->pUrb;
1542 ret = RTUSB_SUBMIT_URB(pUrb);
1543 if (ret != 0) { /* fail */
1565 ("CMDTHREAD_RESET_BULK_IN: Submit Rx URB failed(%d), status=%d\n",
1569 } else { /* success */
1570 /*DBGPRINT(RT_DEBUG_TRACE, ("BIDone, Pend=%d,BIIdx=%d,BIRIdx=%d!\n", */
1571 /* pAd->PendingRx, pAd->NextRxBulkInIndex, pAd->NextRxBulkInReadIndex)); */
1574 ("CMDTHREAD_RESET_BULK_IN: Submit Rx URB Done, status=%d!\n",
1577 ASSERT((pRxContext->InUse == pRxContext->IRPPending));
1582 /* Card must be removed */
1583 if (NT_SUCCESS(ntStatus) !=
1586 fRTMP_ADAPTER_NIC_NOT_EXIST);
1589 ("CMDTHREAD_RESET_BULK_IN: Read Register Failed!Card must be removed!!\n\n"));
1593 ("CMDTHREAD_RESET_BULK_IN: Cannot do bulk in because flags(0x%lx) on !\n",
1598 DBGPRINT_RAW(RT_DEBUG_TRACE,
1599 ("CmdThread : CMDTHREAD_RESET_BULK_IN <===\n"));
1602 case CMDTHREAD_SET_ASIC_WCID:
1604 struct rt_set_asic_wcid SetAsicWcid;
1606 u32 MACValue, MACRValue = 0;
1608 *((struct rt_set_asic_wcid *)(pData));
1610 if (SetAsicWcid.WCID >=
1611 MAX_LEN_OF_MAC_TABLE)
1616 ((u8)SetAsicWcid.WCID) *
1619 DBGPRINT_RAW(RT_DEBUG_TRACE,
1620 ("CmdThread : CMDTHREAD_SET_ASIC_WCID : WCID = %ld, SetTid = %lx, DeleteTid = %lx.\n",
1623 SetAsicWcid.DeleteTid));
1626 Content[SetAsicWcid.WCID].
1629 Content[SetAsicWcid.WCID].
1632 Content[SetAsicWcid.WCID].
1635 Content[SetAsicWcid.WCID].Addr[0]);
1636 DBGPRINT_RAW(RT_DEBUG_TRACE,
1637 ("1-MACValue= %x,\n",
1639 RTUSBWriteMACRegister(pAd, offset,
1642 RTUSBReadMACRegister(pAd, offset + 4,
1644 if (SetAsicWcid.DeleteTid != 0xffffffff)
1646 (~SetAsicWcid.DeleteTid);
1647 if (SetAsicWcid.SetTid != 0xffffffff)
1649 (SetAsicWcid.SetTid);
1650 MACRValue &= 0xffff0000;
1654 Content[SetAsicWcid.WCID].
1656 pAd->MacTab.Content[SetAsicWcid.
1658 MACValue |= MACRValue;
1659 RTUSBWriteMACRegister(pAd, offset + 4,
1662 DBGPRINT_RAW(RT_DEBUG_TRACE,
1663 ("2-MACValue= %x,\n",
1668 case CMDTHREAD_SET_ASIC_WCID_CIPHER:
1670 struct rt_set_asic_wcid_attri SetAsicWcidAttri;
1673 SHAREDKEY_MODE_STRUC csr1;
1675 *((struct rt_set_asic_wcid_attri *)
1678 if (SetAsicWcidAttri.WCID >=
1679 MAX_LEN_OF_MAC_TABLE)
1683 MAC_WCID_ATTRIBUTE_BASE +
1684 ((u8)SetAsicWcidAttri.WCID) *
1687 DBGPRINT_RAW(RT_DEBUG_TRACE,
1688 ("Cmd : CMDTHREAD_SET_ASIC_WCID_CIPHER : WCID = %ld, Cipher = %lx.\n",
1689 SetAsicWcidAttri.WCID,
1690 SetAsicWcidAttri.Cipher));
1692 RTUSBReadMACRegister(pAd, offset,
1696 (((u8)SetAsicWcidAttri.
1699 RTUSBWriteMACRegister(pAd, offset,
1701 DBGPRINT_RAW(RT_DEBUG_TRACE,
1702 ("2-offset = %x , MACValue= %x,\n",
1703 offset, MACRValue));
1706 PAIRWISE_IVEIV_TABLE_BASE +
1707 ((u8)SetAsicWcidAttri.WCID) *
1708 HW_IVEIV_ENTRY_SIZE;
1710 if ((SetAsicWcidAttri.Cipher <=
1714 DefaultKeyId << 30);
1716 MACRValue |= (0x20000000);
1717 RTUSBWriteMACRegister(pAd, offset,
1719 DBGPRINT_RAW(RT_DEBUG_TRACE,
1720 ("2-offset = %x , MACValue= %x,\n",
1721 offset, MACRValue));
1724 /* Update cipher algorithm. WSTA always use BSS0 */
1726 /* for adhoc mode only ,because wep status slow than add key, when use zero config */
1727 if (pAd->StaCfg.BssType == BSS_ADHOC) {
1729 MAC_WCID_ATTRIBUTE_BASE;
1731 RTUSBReadMACRegister(pAd,
1734 MACRValue &= (~0xe);
1736 (((u8)SetAsicWcidAttri.
1739 RTUSBWriteMACRegister(pAd,
1743 /*Update group key cipher,,because wep status slow than add key, when use zero config */
1744 RTUSBReadMACRegister(pAd,
1745 SHARED_KEY_MODE_BASE
1752 csr1.field.Bss0Key0CipherAlg =
1753 SetAsicWcidAttri.Cipher;
1754 csr1.field.Bss0Key1CipherAlg =
1755 SetAsicWcidAttri.Cipher;
1757 RTUSBWriteMACRegister(pAd,
1758 SHARED_KEY_MODE_BASE
1768 /*Benson modified for USB interface, avoid in interrupt when write key, 20080724 --> */
1769 case RT_CMD_SET_KEY_TABLE: /*General call for AsicAddPairwiseKeyEntry() */
1771 struct rt_add_pairwise_key_entry KeyInfo;
1773 *((struct rt_add_pairwise_key_entry *)
1775 AsicAddPairwiseKeyEntry(pAd,
1784 case RT_CMD_SET_RX_WCID_TABLE: /*General call for RTMPAddWcidAttributeEntry() */
1786 struct rt_mac_table_entry *pEntry;
1788 u8 CipherAlg = CIPHER_NONE;
1791 pEntry = (struct rt_mac_table_entry *)(pData);
1793 RTMPAddWcidAttributeEntry(pAd,
1800 /*Benson modified for USB interface, avoid in interrupt when write key, 20080724 <-- */
1802 case CMDTHREAD_SET_CLIENT_MAC_ENTRY:
1804 struct rt_mac_table_entry *pEntry;
1805 pEntry = (struct rt_mac_table_entry *)pData;
1808 AsicRemovePairwiseKeyEntry(pAd,
1814 if ((pEntry->AuthMode <=
1815 Ndis802_11AuthModeAutoSwitch)
1816 && (pEntry->WepStatus ==
1817 Ndis802_11Encryption1Enabled))
1826 AsicUpdateWCIDIVEIV(pAd,
1831 AsicUpdateWCIDAttribute
1839 } else if (pEntry->AuthMode ==
1840 Ndis802_11AuthModeWPANone)
1849 AsicUpdateWCIDIVEIV(pAd,
1854 AsicUpdateWCIDAttribute
1864 /* Other case, disable engine. */
1865 /* Don't worry WPA key, we will add WPA Key after 4-Way handshaking. */
1869 MAC_WCID_ATTRIBUTE_BASE
1872 HW_WCID_ATTRI_SIZE);
1873 /* RX_PKEY_MODE:0 for no security; RX_KEY_TAB:0 for shared key table; BSS_IDX:0 */
1874 RTUSBWriteMACRegister
1879 AsicUpdateRxWCIDTable(pAd, pEntry->Aid,
1881 DBGPRINT(RT_DEBUG_TRACE,
1882 ("UpdateRxWCIDTable(): Aid=%d, "
1889 /* add by johnli, fix "in_interrupt" error when call "MacTableDeleteEntry" in Rx tasklet */
1890 case CMDTHREAD_UPDATE_PROTECT:
1892 AsicUpdateProtect(pAd, 0,
1899 case OID_802_11_ADD_WEP:
1903 struct rt_ndis_802_11_wep *pWepKey;
1905 DBGPRINT(RT_DEBUG_TRACE,
1906 ("CmdThread::OID_802_11_ADD_WEP \n"));
1908 pWepKey = (struct rt_ndis_802_11_wep *)pData;
1909 KeyIdx = pWepKey->KeyIndex & 0x0fffffff;
1911 /* it is a shared key */
1913 || ((pWepKey->KeyLength != 5)
1914 && (pWepKey->KeyLength !=
1917 NDIS_STATUS_INVALID_DATA;
1918 DBGPRINT(RT_DEBUG_ERROR,
1919 ("CmdThread::OID_802_11_ADD_WEP, INVALID_DATA!!\n"));
1922 pAd->SharedKey[BSS0][KeyIdx].
1924 (u8)pWepKey->KeyLength;
1925 NdisMoveMemory(pAd->
1934 SharedKey[BSS0][KeyIdx].
1940 /* Change the WEP cipher to CKIP cipher if CKIP KP on. */
1941 /* Funk UI or Meetinghouse UI will add ckip key from this path. */
1944 if (pAd->OpMode == OPMODE_STA) {
1946 Content[BSSID_WCID].
1953 Content[BSSID_WCID].
1954 PairwiseKey.KeyLen =
1959 pAd->SharedKey[BSS0][KeyIdx].
1960 CipherAlg = CipherAlg;
1962 KeyIndex & 0x80000000) {
1963 /* Default key for tx (shared key) */
1965 u32 WCIDAttri, Value;
1966 u16 offset, offset2;
1967 NdisZeroMemory(IVEIV,
1972 /* Add BSSID to WCTable. because this is Tx wep key. */
1973 /* WCID Attribute UDF:3, BSSIdx:3, Alg:3, Keytable:1=PAIRWISE KEY, BSSIdx is 0 */
1979 MAC_WCID_ATTRIBUTE_BASE
1982 HW_WCID_ATTRI_SIZE);
1983 RTUSBWriteMACRegister
1987 /* Specify key index to find shared key. */
1988 IVEIV[3] = (u8)(KeyIdx << 6); /*WEP Eiv bit off. groupkey index is not 0 */
1990 PAIRWISE_IVEIV_TABLE_BASE
1993 HW_IVEIV_ENTRY_SIZE);
1995 PAIRWISE_IVEIV_TABLE_BASE
1998 HW_IVEIV_ENTRY_SIZE);
1999 for (i = 0; i < 8;) {
2014 RTUSBWriteMACRegister
2018 RTUSBWriteMACRegister
2025 /* 2. WCID Attribute UDF:3, BSSIdx:3, Alg:3, Keytable:use share key, BSSIdx is 0 */
2033 MAC_WCID_ATTRIBUTE_BASE
2036 HW_WCID_ATTRI_SIZE);
2037 DBGPRINT(RT_DEBUG_TRACE,
2038 ("BSS0Mcast_WCID : offset = %x, WCIDAttri = %x\n",
2041 RTUSBWriteMACRegister
2046 AsicAddSharedKeyEntry(pAd, BSS0,
2054 DBGPRINT(RT_DEBUG_TRACE,
2055 ("CmdThread::OID_802_11_ADD_WEP (KeyIdx=%d, Len=%d-byte)\n",
2057 pWepKey->KeyLength));
2062 case CMDTHREAD_802_11_COUNTER_MEASURE:
2065 case CMDTHREAD_SET_GROUP_KEY:
2066 WpaStaGroupKeySetting(pAd);
2069 case CMDTHREAD_SET_PAIRWISE_KEY:
2070 WpaStaPairwiseKeySetting(pAd);
2073 case CMDTHREAD_SET_PSM_BIT:
2075 u16 *pPsm = (u16 *) pData;
2076 MlmeSetPsmBit(pAd, *pPsm);
2079 case CMDTHREAD_FORCE_WAKE_UP:
2080 AsicForceWakeup(pAd, TRUE);
2084 DBGPRINT(RT_DEBUG_ERROR,
2085 ("--> Control Thread !! ERROR !! Unknown(cmdqelmt->command=0x%x) !! \n",
2086 cmdqelmt->command));
2091 if (cmdqelmt->CmdFromNdis == TRUE) {
2092 if (cmdqelmt->buffer != NULL)
2093 os_free_mem(pAd, cmdqelmt->buffer);
2094 os_free_mem(pAd, cmdqelmt);
2096 if ((cmdqelmt->buffer != NULL)
2097 && (cmdqelmt->bufferlength != 0))
2098 os_free_mem(pAd, cmdqelmt->buffer);
2099 os_free_mem(pAd, cmdqelmt);
2101 } /* end of while */
2104 #endif /* RTMP_MAC_USB // */