Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/sparc-2.6
[pandora-kernel.git] / drivers / staging / rt2870 / common / rtusb_io.c
1 /*
2  *************************************************************************
3  * Ralink Tech Inc.
4  * 5F., No.36, Taiyuan St., Jhubei City,
5  * Hsinchu County 302,
6  * Taiwan, R.O.C.
7  *
8  * (c) Copyright 2002-2007, Ralink Technology, Inc.
9  *
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.                                   *
14  *                                                                       *
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.                          *
19  *                                                                       *
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.             *
24  *                                                                       *
25  *************************************************************************
26
27         Module Name:
28         rtusb_io.c
29
30         Abstract:
31
32         Revision History:
33         Who                     When        What
34         --------        ----------  ----------------------------------------------
35         Name            Date        Modification logs
36         Paul Lin    06-25-2004  created
37 */
38
39 #ifdef RTMP_MAC_USB
40
41 #include "../rt_config.h"
42
43 /*
44         ========================================================================
45
46         Routine Description: NIC initialization complete
47
48         Arguments:
49
50         Return Value:
51
52         IRQL =
53
54         Note:
55
56         ========================================================================
57 */
58
59 static int RTUSBFirmwareRun(struct rt_rtmp_adapter *pAd)
60 {
61         int Status;
62
63         Status = RTUSB_VendorRequest(pAd,
64                                      USBD_TRANSFER_DIRECTION_OUT,
65                                      DEVICE_VENDOR_REQUEST_OUT,
66                                      0x01, 0x8, 0, NULL, 0);
67
68         return Status;
69 }
70
71 /*
72         ========================================================================
73
74         Routine Description: Write Firmware to NIC.
75
76         Arguments:
77
78         Return Value:
79
80         IRQL =
81
82         Note:
83
84         ========================================================================
85 */
86 int RTUSBFirmwareWrite(struct rt_rtmp_adapter *pAd,
87                        const u8 *pFwImage, unsigned long FwLen)
88 {
89         u32 MacReg;
90         int Status;
91 /*      unsigned long           i; */
92         u16 writeLen;
93
94         Status = RTUSBReadMACRegister(pAd, MAC_CSR0, &MacReg);
95
96         writeLen = FwLen;
97         RTUSBMultiWrite(pAd, FIRMWARE_IMAGE_BASE, pFwImage, writeLen);
98
99         Status = RTUSBWriteMACRegister(pAd, 0x7014, 0xffffffff);
100         Status = RTUSBWriteMACRegister(pAd, 0x701c, 0xffffffff);
101         Status = RTUSBFirmwareRun(pAd);
102
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--> */
108
109         return Status;
110 }
111
112 int RTUSBVenderReset(struct rt_rtmp_adapter *pAd)
113 {
114         int Status;
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);
120
121         DBGPRINT_RAW(RT_DEBUG_ERROR, ("<--RTUSBVenderReset\n"));
122         return Status;
123 }
124
125 /*
126         ========================================================================
127
128         Routine Description: Read various length data from RT2573
129
130         Arguments:
131
132         Return Value:
133
134         IRQL =
135
136         Note:
137
138         ========================================================================
139 */
140 int RTUSBMultiRead(struct rt_rtmp_adapter *pAd,
141                         u16 Offset, u8 *pData, u16 length)
142 {
143         int Status;
144
145         Status = RTUSB_VendorRequest(pAd,
146                                      (USBD_TRANSFER_DIRECTION_IN |
147                                       USBD_SHORT_TRANSFER_OK),
148                                      DEVICE_VENDOR_REQUEST_IN, 0x7, 0, Offset,
149                                      pData, length);
150
151         return Status;
152 }
153
154 /*
155         ========================================================================
156
157         Routine Description: Write various length data to RT2573
158
159         Arguments:
160
161         Return Value:
162
163         IRQL =
164
165         Note:
166
167         ========================================================================
168 */
169 int RTUSBMultiWrite_OneByte(struct rt_rtmp_adapter *pAd,
170                             u16 Offset, const u8 *pData)
171 {
172         int Status;
173
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);
179
180         return Status;
181 }
182
183 int RTUSBMultiWrite(struct rt_rtmp_adapter *pAd,
184                     u16 Offset, const u8 *pData, u16 length)
185 {
186         int Status;
187
188         u16 index = 0, Value;
189         const u8 *pSrc = pData;
190         u16 resude = 0;
191
192         resude = length % 2;
193         length += resude;
194         do {
195                 Value = (u16)(*pSrc | (*(pSrc + 1) << 8));
196                 Status = RTUSBSingleWrite(pAd, Offset + index, Value);
197                 index += 2;
198                 length -= 2;
199                 pSrc = pSrc + 2;
200         } while (length > 0);
201
202         return Status;
203 }
204
205 int RTUSBSingleWrite(struct rt_rtmp_adapter *pAd,
206                           u16 Offset, u16 Value)
207 {
208         int Status;
209
210         Status = RTUSB_VendorRequest(pAd,
211                                      USBD_TRANSFER_DIRECTION_OUT,
212                                      DEVICE_VENDOR_REQUEST_OUT,
213                                      0x2, Value, Offset, NULL, 0);
214
215         return Status;
216
217 }
218
219 /*
220         ========================================================================
221
222         Routine Description: Read 32-bit MAC register
223
224         Arguments:
225
226         Return Value:
227
228         IRQL =
229
230         Note:
231
232         ========================================================================
233 */
234 int RTUSBReadMACRegister(struct rt_rtmp_adapter *pAd,
235                               u16 Offset, u32 *pValue)
236 {
237         int Status = 0;
238         u32 localVal;
239
240         Status = RTUSB_VendorRequest(pAd,
241                                      (USBD_TRANSFER_DIRECTION_IN |
242                                       USBD_SHORT_TRANSFER_OK),
243                                      DEVICE_VENDOR_REQUEST_IN, 0x7, 0, Offset,
244                                      &localVal, 4);
245
246         *pValue = le2cpu32(localVal);
247
248         if (Status < 0)
249                 *pValue = 0xffffffff;
250
251         return Status;
252 }
253
254 /*
255         ========================================================================
256
257         Routine Description: Write 32-bit MAC register
258
259         Arguments:
260
261         Return Value:
262
263         IRQL =
264
265         Note:
266
267         ========================================================================
268 */
269 int RTUSBWriteMACRegister(struct rt_rtmp_adapter *pAd,
270                                u16 Offset, u32 Value)
271 {
272         int Status;
273         u32 localVal;
274
275         localVal = Value;
276
277         Status = RTUSBSingleWrite(pAd, Offset, (u16)(localVal & 0xffff));
278         Status =
279             RTUSBSingleWrite(pAd, Offset + 2,
280                              (u16)((localVal & 0xffff0000) >> 16));
281
282         return Status;
283 }
284
285 /*
286         ========================================================================
287
288         Routine Description: Read 8-bit BBP register
289
290         Arguments:
291
292         Return Value:
293
294         IRQL =
295
296         Note:
297
298         ========================================================================
299 */
300 int RTUSBReadBBPRegister(struct rt_rtmp_adapter *pAd,
301                               u8 Id, u8 *pValue)
302 {
303         BBP_CSR_CFG_STRUC BbpCsr;
304         u32 i = 0;
305         int status;
306
307         /* Verify the busy condition */
308         do {
309                 status = RTUSBReadMACRegister(pAd, BBP_CSR_CFG, &BbpCsr.word);
310                 if (status >= 0) {
311                         if (!(BbpCsr.field.Busy == BUSY))
312                                 break;
313                 }
314                 DBGPRINT(RT_DEBUG_TRACE,
315                          ("RTUSBReadBBPRegister(BBP_CSR_CFG_1):retry count=%d!\n",
316                           i));
317                 i++;
318         } while ((i < RETRY_LIMIT)
319                  && (!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST)));
320
321         if ((i == RETRY_LIMIT)
322             || (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST))) {
323                 /* */
324                 /* Read failed then Return Default value. */
325                 /* */
326                 *pValue = pAd->BbpWriteLatch[Id];
327
328                 DBGPRINT_RAW(RT_DEBUG_ERROR,
329                              ("Retry count exhausted or device removed!!!\n"));
330                 return STATUS_UNSUCCESSFUL;
331         }
332         /* Prepare for write material */
333         BbpCsr.word = 0;
334         BbpCsr.field.fRead = 1;
335         BbpCsr.field.Busy = 1;
336         BbpCsr.field.RegNum = Id;
337         RTUSBWriteMACRegister(pAd, BBP_CSR_CFG, BbpCsr.word);
338
339         i = 0;
340         /* Verify the busy condition */
341         do {
342                 status = RTUSBReadMACRegister(pAd, BBP_CSR_CFG, &BbpCsr.word);
343                 if (status >= 0) {
344                         if (!(BbpCsr.field.Busy == BUSY)) {
345                                 *pValue = (u8)BbpCsr.field.Value;
346                                 break;
347                         }
348                 }
349                 DBGPRINT(RT_DEBUG_TRACE,
350                          ("RTUSBReadBBPRegister(BBP_CSR_CFG_2):retry count=%d!\n",
351                           i));
352                 i++;
353         } while ((i < RETRY_LIMIT)
354                  && (!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST)));
355
356         if ((i == RETRY_LIMIT)
357             || (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST))) {
358                 /* */
359                 /* Read failed then Return Default value. */
360                 /* */
361                 *pValue = pAd->BbpWriteLatch[Id];
362
363                 DBGPRINT_RAW(RT_DEBUG_ERROR,
364                              ("Retry count exhausted or device removed!!!\n"));
365                 return STATUS_UNSUCCESSFUL;
366         }
367
368         return STATUS_SUCCESS;
369 }
370
371 /*
372         ========================================================================
373
374         Routine Description: Write 8-bit BBP register
375
376         Arguments:
377
378         Return Value:
379
380         IRQL =
381
382         Note:
383
384         ========================================================================
385 */
386 int RTUSBWriteBBPRegister(struct rt_rtmp_adapter *pAd,
387                                u8 Id, u8 Value)
388 {
389         BBP_CSR_CFG_STRUC BbpCsr;
390         u32 i = 0;
391         int status;
392         /* Verify the busy condition */
393         do {
394                 status = RTUSBReadMACRegister(pAd, BBP_CSR_CFG, &BbpCsr.word);
395                 if (status >= 0) {
396                         if (!(BbpCsr.field.Busy == BUSY))
397                                 break;
398                 }
399                 DBGPRINT(RT_DEBUG_TRACE,
400                          ("RTUSBWriteBBPRegister(BBP_CSR_CFG):retry count=%d!\n",
401                           i));
402                 i++;
403         }
404         while ((i < RETRY_LIMIT)
405                && (!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST)));
406
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;
412         }
413         /* Prepare for write material */
414         BbpCsr.word = 0;
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);
420
421         pAd->BbpWriteLatch[Id] = Value;
422
423         return STATUS_SUCCESS;
424 }
425
426 /*
427         ========================================================================
428
429         Routine Description: Write RF register through MAC
430
431         Arguments:
432
433         Return Value:
434
435         IRQL =
436
437         Note:
438
439         ========================================================================
440 */
441 int RTUSBWriteRFRegister(struct rt_rtmp_adapter *pAd, u32 Value)
442 {
443         PHY_CSR4_STRUC PhyCsr4;
444         u32 i = 0;
445         int status;
446
447         NdisZeroMemory(&PhyCsr4, sizeof(PHY_CSR4_STRUC));
448         do {
449                 status = RTUSBReadMACRegister(pAd, RF_CSR_CFG0, &PhyCsr4.word);
450                 if (status >= 0) {
451                         if (!(PhyCsr4.field.Busy))
452                                 break;
453                 }
454                 DBGPRINT(RT_DEBUG_TRACE,
455                          ("RTUSBWriteRFRegister(RF_CSR_CFG0):retry count=%d!\n",
456                           i));
457                 i++;
458         }
459         while ((i < RETRY_LIMIT)
460                && (!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST)));
461
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;
467         }
468
469         RTUSBWriteMACRegister(pAd, RF_CSR_CFG0, Value);
470
471         return STATUS_SUCCESS;
472 }
473
474 /*
475         ========================================================================
476
477         Routine Description:
478
479         Arguments:
480
481         Return Value:
482
483         IRQL =
484
485         Note:
486
487         ========================================================================
488 */
489 int RTUSBReadEEPROM(struct rt_rtmp_adapter *pAd,
490                          u16 Offset, u8 *pData, u16 length)
491 {
492         int Status = STATUS_SUCCESS;
493
494         Status = RTUSB_VendorRequest(pAd,
495                                      (USBD_TRANSFER_DIRECTION_IN |
496                                       USBD_SHORT_TRANSFER_OK),
497                                      DEVICE_VENDOR_REQUEST_IN, 0x9, 0, Offset,
498                                      pData, length);
499
500         return Status;
501 }
502
503 /*
504         ========================================================================
505
506         Routine Description:
507
508         Arguments:
509
510         Return Value:
511
512         IRQL =
513
514         Note:
515
516         ========================================================================
517 */
518 int RTUSBWriteEEPROM(struct rt_rtmp_adapter *pAd,
519                           u16 Offset, u8 *pData, u16 length)
520 {
521         int Status = STATUS_SUCCESS;
522
523         Status = RTUSB_VendorRequest(pAd,
524                                      USBD_TRANSFER_DIRECTION_OUT,
525                                      DEVICE_VENDOR_REQUEST_OUT,
526                                      0x8, 0, Offset, pData, length);
527
528         return Status;
529 }
530
531 int RTUSBReadEEPROM16(struct rt_rtmp_adapter *pAd,
532                            u16 offset, u16 *pData)
533 {
534         int status;
535         u16 localData;
536
537         status = RTUSBReadEEPROM(pAd, offset, (u8 *)(&localData), 2);
538         if (status == STATUS_SUCCESS)
539                 *pData = le2cpu16(localData);
540
541         return status;
542
543 }
544
545 /*
546         ========================================================================
547
548         Routine Description:
549
550         Arguments:
551
552         Return Value:
553
554         IRQL =
555
556         Note:
557
558         ========================================================================
559 */
560 void RTUSBPutToSleep(struct rt_rtmp_adapter *pAd)
561 {
562         u32 value;
563
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));
570
571 }
572
573 /*
574         ========================================================================
575
576         Routine Description:
577
578         Arguments:
579
580         Return Value:
581
582         IRQL =
583
584         Note:
585
586         ========================================================================
587 */
588 int RTUSBWakeUp(struct rt_rtmp_adapter *pAd)
589 {
590         int Status;
591
592         Status = RTUSB_VendorRequest(pAd,
593                                      USBD_TRANSFER_DIRECTION_OUT,
594                                      DEVICE_VENDOR_REQUEST_OUT,
595                                      0x01, 0x09, 0, NULL, 0);
596
597         return Status;
598 }
599
600 /*
601         ========================================================================
602
603         Routine Description:
604
605         Arguments:
606
607         Return Value:
608
609         IRQL =
610
611         Note:
612
613         ========================================================================
614 */
615 void RTUSBInitializeCmdQ(struct rt_cmdq *cmdq)
616 {
617         cmdq->head = NULL;
618         cmdq->tail = NULL;
619         cmdq->size = 0;
620         cmdq->CmdQState = RTMP_TASK_STAT_INITED;
621 }
622
623 /*
624         ========================================================================
625
626         Routine Description:
627
628         Arguments:
629
630         Return Value:
631
632         IRQL =
633
634         Note:
635
636         ========================================================================
637 */
638 int RTUSBEnqueueCmdFromNdis(struct rt_rtmp_adapter *pAd,
639                                     IN NDIS_OID Oid,
640                                     IN BOOLEAN SetInformation,
641                                     void *pInformationBuffer,
642                                     u32 InformationBufferLength)
643 {
644         int status;
645         struct rt_cmdqelmt *cmdqelmt = NULL;
646         struct rt_rtmp_os_task *pTask = &pAd->cmdQTask;
647
648 #ifdef KTHREAD_SUPPORT
649         if (pTask->kthread_task == NULL)
650 #else
651         CHECK_PID_LEGALITY(pTask->taskPID) {
652         }
653         else
654 #endif
655         return (NDIS_STATUS_RESOURCES);
656
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);
660
661         cmdqelmt->buffer = NULL;
662         if (pInformationBuffer != NULL) {
663                 status =
664                     os_alloc_mem(pAd, (u8 **) & cmdqelmt->buffer,
665                                  InformationBufferLength);
666                 if ((status != NDIS_STATUS_SUCCESS)
667                     || (cmdqelmt->buffer == NULL)) {
668                         kfree(cmdqelmt);
669                         return (NDIS_STATUS_RESOURCES);
670                 } else {
671                         NdisMoveMemory(cmdqelmt->buffer, pInformationBuffer,
672                                        InformationBufferLength);
673                         cmdqelmt->bufferlength = InformationBufferLength;
674                 }
675         } else
676                 cmdqelmt->bufferlength = 0;
677
678         cmdqelmt->command = Oid;
679         cmdqelmt->CmdFromNdis = TRUE;
680         if (SetInformation == TRUE)
681                 cmdqelmt->SetOperation = TRUE;
682         else
683                 cmdqelmt->SetOperation = FALSE;
684
685         NdisAcquireSpinLock(&pAd->CmdQLock);
686         if (pAd->CmdQ.CmdQState & RTMP_TASK_CAN_DO_INSERT) {
687                 EnqueueCmd((&pAd->CmdQ), cmdqelmt);
688                 status = NDIS_STATUS_SUCCESS;
689         } else {
690                 status = NDIS_STATUS_FAILURE;
691         }
692         NdisReleaseSpinLock(&pAd->CmdQLock);
693
694         if (status == NDIS_STATUS_FAILURE) {
695                 if (cmdqelmt->buffer)
696                         os_free_mem(pAd, cmdqelmt->buffer);
697                 os_free_mem(pAd, cmdqelmt);
698         } else
699                 RTUSBCMDUp(pAd);
700
701         return (NDIS_STATUS_SUCCESS);
702 }
703
704 /*
705         ========================================================================
706
707         Routine Description:
708
709         Arguments:
710
711         Return Value:
712
713         IRQL =
714
715         Note:
716
717         ========================================================================
718 */
719 int RTUSBEnqueueInternalCmd(struct rt_rtmp_adapter *pAd,
720                                     IN NDIS_OID Oid,
721                                     void *pInformationBuffer,
722                                     u32 InformationBufferLength)
723 {
724         int status;
725         struct rt_cmdqelmt *cmdqelmt = NULL;
726
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));
731
732         if (InformationBufferLength > 0) {
733                 status =
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);
740                 } else {
741                         NdisMoveMemory(cmdqelmt->buffer, pInformationBuffer,
742                                        InformationBufferLength);
743                         cmdqelmt->bufferlength = InformationBufferLength;
744                 }
745         } else {
746                 cmdqelmt->buffer = NULL;
747                 cmdqelmt->bufferlength = 0;
748         }
749
750         cmdqelmt->command = Oid;
751         cmdqelmt->CmdFromNdis = FALSE;
752
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;
758                 } else {
759                         status = NDIS_STATUS_FAILURE;
760                 }
761                 NdisReleaseSpinLock(&pAd->CmdQLock);
762
763                 if (status == NDIS_STATUS_FAILURE) {
764                         if (cmdqelmt->buffer)
765                                 os_free_mem(pAd, cmdqelmt->buffer);
766                         os_free_mem(pAd, cmdqelmt);
767                 } else
768                         RTUSBCMDUp(pAd);
769         }
770         return (NDIS_STATUS_SUCCESS);
771 }
772
773 /*
774         ========================================================================
775
776         Routine Description:
777
778         Arguments:
779
780         Return Value:
781
782         IRQL =
783
784         Note:
785
786         ========================================================================
787 */
788 void RTUSBDequeueCmd(struct rt_cmdq *cmdq, struct rt_cmdqelmt * * pcmdqelmt)
789 {
790         *pcmdqelmt = cmdq->head;
791
792         if (*pcmdqelmt != NULL) {
793                 cmdq->head = cmdq->head->next;
794                 cmdq->size--;
795                 if (cmdq->size == 0)
796                         cmdq->tail = NULL;
797         }
798 }
799
800 /*
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 ()
814
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.
818
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.
825
826         Routine Description:
827
828         Arguments:
829
830         Return Value:
831
832         Note:
833
834         ========================================================================
835 */
836 int RTUSB_VendorRequest(struct rt_rtmp_adapter *pAd,
837                              u32 TransferFlags,
838                              u8 RequestType,
839                              u8 Request,
840                              u16 Value,
841                              u16 Index,
842                              void *TransferBuffer,
843                              u32 TransferBufferLength)
844 {
845         int ret = 0;
846         struct os_cookie *pObj = (struct os_cookie *)pAd->OS_Cookie;
847
848         if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST)) {
849                 DBGPRINT(RT_DEBUG_ERROR, ("device disconnected\n"));
850                 return -1;
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));
855
856                 return -1;
857         } else {
858 #define MAX_RETRY_COUNT  10
859
860                 int retryCount = 0;
861                 void *tmpBuf = TransferBuffer;
862
863                 ret = down_interruptible(&(pAd->UsbVendorReq_semaphore));
864                 if (pAd->UsbVendorReqBuf) {
865                         ASSERT(TransferBufferLength < MAX_PARAM_BUFFER_SIZE);
866
867                         tmpBuf = (void *)pAd->UsbVendorReqBuf;
868                         NdisZeroMemory(pAd->UsbVendorReqBuf,
869                                        TransferBufferLength);
870
871                         if (RequestType == DEVICE_VENDOR_REQUEST_OUT)
872                                 NdisMoveMemory(tmpBuf, TransferBuffer,
873                                                TransferBufferLength);
874                 }
875
876                 do {
877                         if (RequestType == DEVICE_VENDOR_REQUEST_OUT)
878                                 ret =
879                                     usb_control_msg(pObj->pUsb_Dev,
880                                                     usb_sndctrlpipe(pObj->
881                                                                     pUsb_Dev,
882                                                                     0), Request,
883                                                     RequestType, Value, Index,
884                                                     tmpBuf,
885                                                     TransferBufferLength,
886                                                     CONTROL_TIMEOUT_JIFFIES);
887                         else if (RequestType == DEVICE_VENDOR_REQUEST_IN)
888                                 ret =
889                                     usb_control_msg(pObj->pUsb_Dev,
890                                                     usb_rcvctrlpipe(pObj->
891                                                                     pUsb_Dev,
892                                                                     0), Request,
893                                                     RequestType, Value, Index,
894                                                     tmpBuf,
895                                                     TransferBufferLength,
896                                                     CONTROL_TIMEOUT_JIFFIES);
897                         else {
898                                 DBGPRINT(RT_DEBUG_ERROR,
899                                          ("vendor request direction is failed\n"));
900                                 ret = -1;
901                         }
902
903                         retryCount++;
904                         if (ret < 0) {
905                                 DBGPRINT(RT_DEBUG_OFF, ("#\n"));
906                                 RTMPusecDelay(5000);
907                         }
908                 } while ((ret < 0) && (retryCount < MAX_RETRY_COUNT));
909
910                 if ((pAd->UsbVendorReqBuf)
911                     && (RequestType == DEVICE_VENDOR_REQUEST_IN))
912                         NdisMoveMemory(TransferBuffer, tmpBuf,
913                                        TransferBufferLength);
914                 up(&(pAd->UsbVendorReq_semaphore));
915
916                 if (ret < 0) {
917                         DBGPRINT(RT_DEBUG_ERROR,
918                                  ("RTUSB_VendorRequest failed(%d),TxFlags=0x%x, ReqType=%s, Req=0x%x, Index=0x%x\n",
919                                   ret, TransferFlags,
920                                   (RequestType ==
921                                    DEVICE_VENDOR_REQUEST_OUT ? "OUT" : "IN"),
922                                   Request, Index));
923                         if (Request == 0x2)
924                                 DBGPRINT(RT_DEBUG_ERROR,
925                                          ("\tRequest Value=0x%04x!\n", Value));
926
927                         if ((TransferBuffer != NULL)
928                             && (TransferBufferLength > 0))
929                                 hex_dump("Failed TransferBuffer value",
930                                          TransferBuffer, TransferBufferLength);
931                 }
932
933         }
934
935         if (ret != -1)
936                 return STATUS_SUCCESS;
937         else
938                 return STATUS_UNSUCCESSFUL;
939 }
940
941 /*
942         ========================================================================
943
944         Routine Description:
945           Creates an IRP to submite an IOCTL_INTERNAL_USB_RESET_PORT
946           synchronously. Callers of this function must be running at
947           PASSIVE LEVEL.
948
949         Arguments:
950
951         Return Value:
952
953         Note:
954
955         ========================================================================
956 */
957 int RTUSB_ResetDevice(struct rt_rtmp_adapter *pAd)
958 {
959         int Status = TRUE;
960
961         DBGPRINT_RAW(RT_DEBUG_TRACE, ("--->USB_ResetDevice\n"));
962         /*RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_RESET_IN_PROGRESS); */
963         return Status;
964 }
965
966 void CMDHandler(struct rt_rtmp_adapter *pAd)
967 {
968         struct rt_cmdqelmt *cmdqelmt;
969         u8 *pData;
970         int NdisStatus = NDIS_STATUS_SUCCESS;
971 /*      unsigned long                   Now = 0; */
972         int ntStatus;
973 /*      unsigned long   IrqFlags; */
974
975         while (pAd && pAd->CmdQ.size > 0) {
976                 NdisStatus = NDIS_STATUS_SUCCESS;
977
978                 NdisAcquireSpinLock(&pAd->CmdQLock);
979                 RTUSBDequeueCmd(&pAd->CmdQ, &cmdqelmt);
980                 NdisReleaseSpinLock(&pAd->CmdQLock);
981
982                 if (cmdqelmt == NULL)
983                         break;
984
985                 pData = cmdqelmt->buffer;
986
987                 if (!
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:
992                                 {
993                                         u32 data;
994
995                                         {
996                                                 /* Read GPIO pin2 as Hardware controlled radio state */
997
998                                                 RTUSBReadMACRegister(pAd,
999                                                                      GPIO_CTRL_CFG,
1000                                                                      &data);
1001
1002                                                 if (data & 0x04) {
1003                                                         pAd->StaCfg.bHwRadio =
1004                                                             TRUE;
1005                                                 } else {
1006                                                         pAd->StaCfg.bHwRadio =
1007                                                             FALSE;
1008                                                 }
1009
1010                                                 if (pAd->StaCfg.bRadio !=
1011                                                     (pAd->StaCfg.bHwRadio
1012                                                      && pAd->StaCfg.bSwRadio)) {
1013                                                         pAd->StaCfg.bRadio =
1014                                                             (pAd->StaCfg.
1015                                                              bHwRadio
1016                                                              && pAd->StaCfg.
1017                                                              bSwRadio);
1018                                                         if (pAd->StaCfg.
1019                                                             bRadio == TRUE) {
1020                                                                 DBGPRINT_RAW
1021                                                                     (RT_DEBUG_ERROR,
1022                                                                      ("!!! Radio On !!!\n"));
1023
1024                                                                 MlmeRadioOn
1025                                                                     (pAd);
1026                                                                 /* Update extra information */
1027                                                                 pAd->ExtraInfo =
1028                                                                     EXTRA_INFO_CLEAR;
1029                                                         } else {
1030                                                                 DBGPRINT_RAW
1031                                                                     (RT_DEBUG_ERROR,
1032                                                                      ("!!! Radio Off !!!\n"));
1033
1034                                                                 MlmeRadioOff
1035                                                                     (pAd);
1036                                                                 /* Update extra information */
1037                                                                 pAd->ExtraInfo =
1038                                                                     HW_RADIO_OFF;
1039                                                         }
1040                                                 }
1041                                         }
1042                                 }
1043                                 break;
1044
1045                         case CMDTHREAD_QKERIODIC_EXECUT:
1046                                 {
1047                                         StaQuickResponeForRateUpExec(NULL, pAd,
1048                                                                      NULL,
1049                                                                      NULL);
1050                                 }
1051                                 break;
1052
1053                         case CMDTHREAD_RESET_BULK_OUT:
1054                                 {
1055                                         u32 MACValue;
1056                                         u8 Index;
1057                                         int ret = 0;
1058                                         struct rt_ht_tx_context *pHTTXContext;
1059 /*                                              struct rt_rtmp_tx_ring *pTxRing; */
1060                                         unsigned long IrqFlags;
1061
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 */
1068                                         Index = 0;
1069                                         do {
1070                                                 RTUSBReadMACRegister(pAd,
1071                                                                      TXRXQ_PCNT,
1072                                                                      &MACValue);
1073                                                 if ((MACValue & 0xf00000
1074                                                      /*0x800000 */ ) == 0)
1075                                                         break;
1076                                                 Index++;
1077                                                 RTMPusecDelay(10000);
1078                                         } while (Index < 100);
1079                                         MACValue = 0;
1080                                         RTUSBReadMACRegister(pAd, USB_DMA_CFG,
1081                                                              &MACValue);
1082                                         /* To prevent Read Register error, we 2nd check the validity. */
1083                                         if ((MACValue & 0xc00000) == 0)
1084                                                 RTUSBReadMACRegister(pAd,
1085                                                                      USB_DMA_CFG,
1086                                                                      &MACValue);
1087                                         /* To prevent Read Register error, we 3rd check the validity. */
1088                                         if ((MACValue & 0xc00000) == 0)
1089                                                 RTUSBReadMACRegister(pAd,
1090                                                                      USB_DMA_CFG,
1091                                                                      &MACValue);
1092                                         MACValue |= 0x80000;
1093                                         RTUSBWriteMACRegister(pAd, USB_DMA_CFG,
1094                                                               MACValue);
1095
1096                                         /* Wait 1ms to prevent next URB to bulkout before HW reset. by MAXLEE 12-25-2007 */
1097                                         RTMPusecDelay(1000);
1098
1099                                         MACValue &= (~0x80000);
1100                                         RTUSBWriteMACRegister(pAd, USB_DMA_CFG,
1101                                                               MACValue);
1102                                         DBGPRINT_RAW(RT_DEBUG_TRACE,
1103                                                      ("\tSet 0x2a0 bit19. Clear USB DMA TX path\n"));
1104
1105                                         /* Wait 5ms to prevent next URB to bulkout before HW reset. by MAXLEE 12-25-2007 */
1106                                         /*RTMPusecDelay(5000); */
1107
1108                                         if ((pAd->
1109                                              bulkResetPipeid &
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 <
1115                                                     MGMT_RING_SIZE
1116                                                     /* pMLMEContext->bWaitingBulkOut == TRUE */
1117                                                     ) {
1118                                                         RTUSB_SET_BULK_FLAG(pAd,
1119                                                                             fRTUSB_BULK_OUT_MLME);
1120                                                 }
1121                                                 RTUSBKickBulkOut(pAd);
1122
1123                                                 DBGPRINT_RAW(RT_DEBUG_TRACE,
1124                                                              ("\tTX MGMT RECOVER Done!\n"));
1125                                         } else {
1126                                                 pHTTXContext =
1127                                                     &(pAd->
1128                                                       TxContext[pAd->
1129                                                                 bulkResetPipeid]);
1130                                                 /*NdisAcquireSpinLock(&pAd->BulkOutLock[pAd->bulkResetPipeid]); */
1131                                                 RTMP_INT_LOCK(&pAd->
1132                                                               BulkOutLock[pAd->
1133                                                                           bulkResetPipeid],
1134                                                               IrqFlags);
1135                                                 if (pAd->
1136                                                     BulkOutPending[pAd->
1137                                                                    bulkResetPipeid]
1138                                                     == FALSE) {
1139                                                         pAd->
1140                                                             BulkOutPending[pAd->
1141                                                                            bulkResetPipeid]
1142                                                             = TRUE;
1143                                                         pHTTXContext->
1144                                                             IRPPending = TRUE;
1145                                                         pAd->
1146                                                             watchDogTxPendingCnt
1147                                                             [pAd->
1148                                                              bulkResetPipeid] =
1149                                                             1;
1150
1151                                                         /* no matter what, clean the flag */
1152                                                         RTMP_CLEAR_FLAG(pAd,
1153                                                                         fRTMP_ADAPTER_BULKOUT_RESET);
1154
1155                                                         /*NdisReleaseSpinLock(&pAd->BulkOutLock[pAd->bulkResetPipeid]); */
1156                                                         RTMP_INT_UNLOCK(&pAd->
1157                                                                         BulkOutLock
1158                                                                         [pAd->
1159                                                                          bulkResetPipeid],
1160                                                                         IrqFlags);
1161                                                         {
1162                                                                 RTUSBInitHTTxDesc
1163                                                                     (pAd,
1164                                                                      pHTTXContext,
1165                                                                      pAd->
1166                                                                      bulkResetPipeid,
1167                                                                      pHTTXContext->
1168                                                                      BulkOutSize,
1169                                                                      (usb_complete_t)
1170                                                                      RTUSBBulkOutDataPacketComplete);
1171
1172                                                                 if ((ret =
1173                                                                      RTUSB_SUBMIT_URB
1174                                                                      (pHTTXContext->
1175                                                                       pUrb)) !=
1176                                                                     0) {
1177                                                                         RTMP_INT_LOCK
1178                                                                             (&pAd->
1179                                                                              BulkOutLock
1180                                                                              [pAd->
1181                                                                               bulkResetPipeid],
1182                                                                              IrqFlags);
1183                                                                         pAd->
1184                                                                             BulkOutPending
1185                                                                             [pAd->
1186                                                                              bulkResetPipeid]
1187                                                                             =
1188                                                                             FALSE;
1189                                                                         pHTTXContext->
1190                                                                             IRPPending
1191                                                                             =
1192                                                                             FALSE;
1193                                                                         pAd->
1194                                                                             watchDogTxPendingCnt
1195                                                                             [pAd->
1196                                                                              bulkResetPipeid]
1197                                                                             = 0;
1198                                                                         RTMP_INT_UNLOCK
1199                                                                             (&pAd->
1200                                                                              BulkOutLock
1201                                                                              [pAd->
1202                                                                               bulkResetPipeid],
1203                                                                              IrqFlags);
1204
1205                                                                         DBGPRINT
1206                                                                             (RT_DEBUG_ERROR,
1207                                                                              ("CmdThread : CMDTHREAD_RESET_BULK_OUT: Submit Tx URB failed %d\n",
1208                                                                               ret));
1209                                                                 } else {
1210                                                                         RTMP_IRQ_LOCK
1211                                                                             (&pAd->
1212                                                                              BulkOutLock
1213                                                                              [pAd->
1214                                                                               bulkResetPipeid],
1215                                                                              IrqFlags);
1216                                                                         DBGPRINT_RAW
1217                                                                             (RT_DEBUG_TRACE,
1218                                                                              ("\tCMDTHREAD_RESET_BULK_OUT: TxContext[%d]:CWPos=%ld, NBPos=%ld, ENBPos=%ld, bCopy=%d, pending=%d!\n",
1219                                                                               pAd->
1220                                                                               bulkResetPipeid,
1221                                                                               pHTTXContext->
1222                                                                               CurWritePosition,
1223                                                                               pHTTXContext->
1224                                                                               NextBulkOutPosition,
1225                                                                               pHTTXContext->
1226                                                                               ENextBulkOutPosition,
1227                                                                               pHTTXContext->
1228                                                                               bCopySavePad,
1229                                                                               pAd->
1230                                                                               BulkOutPending
1231                                                                               [pAd->
1232                                                                                bulkResetPipeid]));
1233                                                                         DBGPRINT_RAW
1234                                                                             (RT_DEBUG_TRACE,
1235                                                                              ("\t\tBulkOut Req=0x%lx, Complete=0x%lx, Other=0x%lx\n",
1236                                                                               pAd->
1237                                                                               BulkOutReq,
1238                                                                               pAd->
1239                                                                               BulkOutComplete,
1240                                                                               pAd->
1241                                                                               BulkOutCompleteOther));
1242                                                                         RTMP_IRQ_UNLOCK
1243                                                                             (&pAd->
1244                                                                              BulkOutLock
1245                                                                              [pAd->
1246                                                                               bulkResetPipeid],
1247                                                                              IrqFlags);
1248                                                                         DBGPRINT_RAW
1249                                                                             (RT_DEBUG_TRACE,
1250                                                                              ("\tCMDTHREAD_RESET_BULK_OUT: Submit Tx DATA URB for failed BulkReq(0x%lx) Done, status=%d!\n",
1251                                                                               pAd->
1252                                                                               bulkResetReq
1253                                                                               [pAd->
1254                                                                                bulkResetPipeid],
1255                                                                               pHTTXContext->
1256                                                                               pUrb->
1257                                                                               status));
1258
1259                                                                 }
1260                                                         }
1261                                                 } else {
1262                                                         /*NdisReleaseSpinLock(&pAd->BulkOutLock[pAd->bulkResetPipeid]); */
1263                                                         /*RTMP_INT_UNLOCK(&pAd->BulkOutLock[pAd->bulkResetPipeid], IrqFlags); */
1264
1265                                                         DBGPRINT_RAW
1266                                                             (RT_DEBUG_ERROR,
1267                                                              ("CmdThread : TX DATA RECOVER FAIL for BulkReq(0x%lx) because BulkOutPending[%d] is TRUE!\n",
1268                                                               pAd->
1269                                                               bulkResetReq[pAd->
1270                                                                            bulkResetPipeid],
1271                                                               pAd->
1272                                                               bulkResetPipeid));
1273                                                         if (pAd->
1274                                                             bulkResetPipeid ==
1275                                                             0) {
1276                                                                 u8
1277                                                                     pendingContext
1278                                                                     = 0;
1279                                                                 struct rt_ht_tx_context *
1280                                                                     pHTTXContext
1281                                                                     =
1282                                                                     (struct rt_ht_tx_context *)
1283                                                                     (&pAd->
1284                                                                      TxContext
1285                                                                      [pAd->
1286                                                                       bulkResetPipeid]);
1287                                                                 struct rt_tx_context *
1288                                                                     pMLMEContext
1289                                                                     =
1290                                                                     (struct rt_tx_context *)
1291                                                                     (pAd->
1292                                                                      MgmtRing.
1293                                                                      Cell[pAd->
1294                                                                           MgmtRing.
1295                                                                           TxDmaIdx].
1296                                                                      AllocVa);
1297                                                                 struct rt_tx_context *
1298                                                                     pNULLContext
1299                                                                     =
1300                                                                     (struct rt_tx_context *)
1301                                                                     (&pAd->
1302                                                                      PsPollContext);
1303                                                                 struct rt_tx_context *
1304                                                                     pPsPollContext
1305                                                                     =
1306                                                                     (struct rt_tx_context *)
1307                                                                     (&pAd->
1308                                                                      NullContext);
1309
1310                                                                 if (pHTTXContext->IRPPending)
1311                                                                         pendingContext
1312                                                                             |=
1313                                                                             1;
1314                                                                 else if
1315                                                                     (pMLMEContext->
1316                                                                      IRPPending)
1317                                                                         pendingContext
1318                                                                             |=
1319                                                                             2;
1320                                                                 else if
1321                                                                     (pNULLContext->
1322                                                                      IRPPending)
1323                                                                         pendingContext
1324                                                                             |=
1325                                                                             4;
1326                                                                 else if
1327                                                                     (pPsPollContext->
1328                                                                      IRPPending)
1329                                                                         pendingContext
1330                                                                             |=
1331                                                                             8;
1332                                                                 else
1333                                                                         pendingContext
1334                                                                             = 0;
1335
1336                                                                 DBGPRINT_RAW
1337                                                                     (RT_DEBUG_ERROR,
1338                                                                      ("\tTX Occupied by %d!\n",
1339                                                                       pendingContext));
1340                                                         }
1341                                                         /* no matter what, clean the flag */
1342                                                         RTMP_CLEAR_FLAG(pAd,
1343                                                                         fRTMP_ADAPTER_BULKOUT_RESET);
1344
1345                                                         RTMP_INT_UNLOCK(&pAd->
1346                                                                         BulkOutLock
1347                                                                         [pAd->
1348                                                                          bulkResetPipeid],
1349                                                                         IrqFlags);
1350
1351                                                         RTUSB_SET_BULK_FLAG(pAd,
1352                                                                             (fRTUSB_BULK_OUT_DATA_NORMAL
1353                                                                              <<
1354                                                                              pAd->
1355                                                                              bulkResetPipeid));
1356                                                 }
1357
1358                                                 RTMPDeQueuePacket(pAd, FALSE,
1359                                                                   NUM_OF_TX_RING,
1360                                                                   MAX_TX_PROCESS);
1361                                                 /*RTUSBKickBulkOut(pAd); */
1362                                         }
1363
1364                                 }
1365                                 /*
1366                                    // Don't cancel BULKIN.
1367                                    while ((atomic_read(&pAd->PendingRx) > 0) &&
1368                                    (!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST)))
1369                                    {
1370                                    if (atomic_read(&pAd->PendingRx) > 0)
1371                                    {
1372                                    DBGPRINT_RAW(RT_DEBUG_ERROR, ("BulkIn IRP Pending!!cancel it!\n"));
1373                                    RTUSBCancelPendingBulkInIRP(pAd);
1374                                    }
1375                                    RTMPusecDelay(100000);
1376                                    }
1377
1378                                    if ((atomic_read(&pAd->PendingRx) == 0) && (!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS)))
1379                                    {
1380                                    u8        i;
1381                                    RTUSBRxPacket(pAd);
1382                                    pAd->NextRxBulkInReadIndex = 0;      // Next Rx Read index
1383                                    pAd->NextRxBulkInIndex               = 0;    // Rx Bulk pointer
1384                                    for (i = 0; i < (RX_RING_SIZE); i++)
1385                                    {
1386                                    struct rt_rx_context *pRxContext = &(pAd->RxContext[i]);
1387
1388                                    pRxContext->pAd      = pAd;
1389                                    pRxContext->InUse            = FALSE;
1390                                    pRxContext->IRPPending       = FALSE;
1391                                    pRxContext->Readable = FALSE;
1392                                    pRxContext->ReorderInUse = FALSE;
1393
1394                                    }
1395                                    RTUSBBulkReceive(pAd);
1396                                    DBGPRINT_RAW(RT_DEBUG_ERROR, ("RTUSBBulkReceive\n"));
1397                                    } */
1398                                 DBGPRINT_RAW(RT_DEBUG_TRACE,
1399                                              ("CmdThread : CMDTHREAD_RESET_BULK_OUT<===\n"));
1400                                 break;
1401
1402                         case CMDTHREAD_RESET_BULK_IN:
1403                                 DBGPRINT_RAW(RT_DEBUG_TRACE,
1404                                              ("CmdThread : CMDTHREAD_RESET_BULK_IN === >\n"));
1405
1406                                 /* All transfers must be aborted or cancelled before attempting to reset the pipe. */
1407                                 {
1408                                         u32 MACValue;
1409
1410                                         {
1411                                                 /*while ((atomic_read(&pAd->PendingRx) > 0) && (!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST))) */
1412                                                 if ((pAd->PendingRx > 0)
1413                                                     &&
1414                                                     (!RTMP_TEST_FLAG
1415                                                      (pAd,
1416                                                       fRTMP_ADAPTER_NIC_NOT_EXIST)))
1417                                                 {
1418                                                         DBGPRINT_RAW
1419                                                             (RT_DEBUG_ERROR,
1420                                                              ("BulkIn IRP Pending!!!\n"));
1421                                                         RTUSBCancelPendingBulkInIRP
1422                                                             (pAd);
1423                                                         RTMPusecDelay(100000);
1424                                                         pAd->PendingRx = 0;
1425                                                 }
1426                                         }
1427
1428                                         /* Wait 10ms before reading register. */
1429                                         RTMPusecDelay(10000);
1430                                         ntStatus =
1431                                             RTUSBReadMACRegister(pAd, MAC_CSR0,
1432                                                                  &MACValue);
1433
1434                                         if ((NT_SUCCESS(ntStatus) == TRUE) &&
1435                                             (!(RTMP_TEST_FLAG
1436                                                (pAd,
1437                                                 (fRTMP_ADAPTER_RESET_IN_PROGRESS
1438                                                  | fRTMP_ADAPTER_RADIO_OFF |
1439                                                  fRTMP_ADAPTER_HALT_IN_PROGRESS
1440                                                  |
1441                                                  fRTMP_ADAPTER_NIC_NOT_EXIST)))))
1442                                         {
1443                                                 u8 i;
1444
1445                                                 if (RTMP_TEST_FLAG
1446                                                     (pAd,
1447                                                      (fRTMP_ADAPTER_RESET_IN_PROGRESS
1448                                                       | fRTMP_ADAPTER_RADIO_OFF
1449                                                       |
1450                                                       fRTMP_ADAPTER_HALT_IN_PROGRESS
1451                                                       |
1452                                                       fRTMP_ADAPTER_NIC_NOT_EXIST)))
1453                                                         break;
1454                                                 pAd->NextRxBulkInPosition =
1455                                                     pAd->RxContext[pAd->
1456                                                                    NextRxBulkInIndex].
1457                                                     BulkInOffset;
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",
1460                                                           pAd->
1461                                                           NextRxBulkInIndex,
1462                                                           pAd->
1463                                                           NextRxBulkInReadIndex,
1464                                                           pAd->
1465                                                           NextRxBulkInPosition,
1466                                                           pAd->BulkInReq,
1467                                                           pAd->BulkInComplete,
1468                                                           pAd->
1469                                                           BulkInCompleteFail));
1470                                                 for (i = 0; i < RX_RING_SIZE;
1471                                                      i++) {
1472                                                         DBGPRINT(RT_DEBUG_TRACE,
1473                                                                  ("\tRxContext[%d]: IRPPending=%d, InUse=%d, Readable=%d!\n",
1474                                                                   i,
1475                                                                   pAd->
1476                                                                   RxContext[i].
1477                                                                   IRPPending,
1478                                                                   pAd->
1479                                                                   RxContext[i].
1480                                                                   InUse,
1481                                                                   pAd->
1482                                                                   RxContext[i].
1483                                                                   Readable));
1484                                                 }
1485                                                 /*
1486
1487                                                    DBGPRINT_RAW(RT_DEBUG_ERROR, ("==========================================\n"));
1488
1489                                                    pAd->NextRxBulkInReadIndex = 0;      // Next Rx Read index
1490                                                    pAd->NextRxBulkInIndex               = 0;    // Rx Bulk pointer
1491                                                    for (i = 0; i < (RX_RING_SIZE); i++)
1492                                                    {
1493                                                    struct rt_rx_context *pRxContext = &(pAd->RxContext[i]);
1494
1495                                                    pRxContext->pAd      = pAd;
1496                                                    pRxContext->InUse            = FALSE;
1497                                                    pRxContext->IRPPending       = FALSE;
1498                                                    pRxContext->Readable = FALSE;
1499                                                    pRxContext->ReorderInUse = FALSE;
1500
1501                                                    } */
1502                                                 RTMP_CLEAR_FLAG(pAd,
1503                                                                 fRTMP_ADAPTER_BULKIN_RESET);
1504                                                 for (i = 0;
1505                                                      i <
1506                                                      pAd->CommonCfg.
1507                                                      NumOfBulkInIRP; i++) {
1508                                                         /*RTUSBBulkReceive(pAd); */
1509                                                         struct rt_rx_context *pRxContext;
1510                                                         PURB pUrb;
1511                                                         int ret = 0;
1512                                                         unsigned long IrqFlags;
1513
1514                                                         RTMP_IRQ_LOCK(&pAd->
1515                                                                       BulkInLock,
1516                                                                       IrqFlags);
1517                                                         pRxContext =
1518                                                             &(pAd->
1519                                                               RxContext[pAd->
1520                                                                         NextRxBulkInIndex]);
1521                                                         if ((pAd->PendingRx > 0)
1522                                                             || (pRxContext->
1523                                                                 Readable ==
1524                                                                 TRUE)
1525                                                             || (pRxContext->
1526                                                                 InUse ==
1527                                                                 TRUE)) {
1528                                                                 RTMP_IRQ_UNLOCK
1529                                                                     (&pAd->
1530                                                                      BulkInLock,
1531                                                                      IrqFlags);
1532                                                                 break;
1533                                                         }
1534                                                         pRxContext->InUse =
1535                                                             TRUE;
1536                                                         pRxContext->IRPPending =
1537                                                             TRUE;
1538                                                         pAd->PendingRx++;
1539                                                         pAd->BulkInReq++;
1540                                                         RTMP_IRQ_UNLOCK(&pAd->
1541                                                                         BulkInLock,
1542                                                                         IrqFlags);
1543
1544                                                         /* Init Rx context descriptor */
1545                                                         RTUSBInitRxDesc(pAd,
1546                                                                         pRxContext);
1547                                                         pUrb = pRxContext->pUrb;
1548                                                         if ((ret = RTUSB_SUBMIT_URB(pUrb)) != 0) {      /* fail */
1549
1550                                                                 RTMP_IRQ_LOCK
1551                                                                     (&pAd->
1552                                                                      BulkInLock,
1553                                                                      IrqFlags);
1554                                                                 pRxContext->
1555                                                                     InUse =
1556                                                                     FALSE;
1557                                                                 pRxContext->
1558                                                                     IRPPending =
1559                                                                     FALSE;
1560                                                                 pAd->
1561                                                                     PendingRx--;
1562                                                                 pAd->
1563                                                                     BulkInReq--;
1564                                                                 RTMP_IRQ_UNLOCK
1565                                                                     (&pAd->
1566                                                                      BulkInLock,
1567                                                                      IrqFlags);
1568                                                                 DBGPRINT
1569                                                                     (RT_DEBUG_ERROR,
1570                                                                      ("CMDTHREAD_RESET_BULK_IN: Submit Rx URB failed(%d), status=%d\n",
1571                                                                       ret,
1572                                                                       pUrb->
1573                                                                       status));
1574                                                         } else {        /* success */
1575                                                                 /*DBGPRINT(RT_DEBUG_TRACE, ("BIDone, Pend=%d,BIIdx=%d,BIRIdx=%d!\n", */
1576                                                                 /*                                                      pAd->PendingRx, pAd->NextRxBulkInIndex, pAd->NextRxBulkInReadIndex)); */
1577                                                                 DBGPRINT_RAW
1578                                                                     (RT_DEBUG_TRACE,
1579                                                                      ("CMDTHREAD_RESET_BULK_IN: Submit Rx URB Done, status=%d!\n",
1580                                                                       pUrb->
1581                                                                       status));
1582                                                                 ASSERT((pRxContext->InUse == pRxContext->IRPPending));
1583                                                         }
1584                                                 }
1585
1586                                         } else {
1587                                                 /* Card must be removed */
1588                                                 if (NT_SUCCESS(ntStatus) !=
1589                                                     TRUE) {
1590                                                         RTMP_SET_FLAG(pAd,
1591                                                                       fRTMP_ADAPTER_NIC_NOT_EXIST);
1592                                                         DBGPRINT_RAW
1593                                                             (RT_DEBUG_ERROR,
1594                                                              ("CMDTHREAD_RESET_BULK_IN: Read Register Failed!Card must be removed!!\n\n"));
1595                                                 } else {
1596                                                         DBGPRINT_RAW
1597                                                             (RT_DEBUG_ERROR,
1598                                                              ("CMDTHREAD_RESET_BULK_IN: Cannot do bulk in because flags(0x%lx) on !\n",
1599                                                               pAd->Flags));
1600                                                 }
1601                                         }
1602                                 }
1603                                 DBGPRINT_RAW(RT_DEBUG_TRACE,
1604                                              ("CmdThread : CMDTHREAD_RESET_BULK_IN <===\n"));
1605                                 break;
1606
1607                         case CMDTHREAD_SET_ASIC_WCID:
1608                                 {
1609                                         struct rt_set_asic_wcid SetAsicWcid;
1610                                         u16 offset;
1611                                         u32 MACValue, MACRValue = 0;
1612                                         SetAsicWcid =
1613                                             *((struct rt_set_asic_wcid *)(pData));
1614
1615                                         if (SetAsicWcid.WCID >=
1616                                             MAX_LEN_OF_MAC_TABLE)
1617                                                 return;
1618
1619                                         offset =
1620                                             MAC_WCID_BASE +
1621                                             ((u8)SetAsicWcid.WCID) *
1622                                             HW_WCID_ENTRY_SIZE;
1623
1624                                         DBGPRINT_RAW(RT_DEBUG_TRACE,
1625                                                      ("CmdThread : CMDTHREAD_SET_ASIC_WCID : WCID = %ld, SetTid  = %lx, DeleteTid = %lx.\n",
1626                                                       SetAsicWcid.WCID,
1627                                                       SetAsicWcid.SetTid,
1628                                                       SetAsicWcid.DeleteTid));
1629                                         MACValue =
1630                                             (pAd->MacTab.
1631                                              Content[SetAsicWcid.WCID].
1632                                              Addr[3] << 24) +
1633                                             (pAd->MacTab.
1634                                              Content[SetAsicWcid.WCID].
1635                                              Addr[2] << 16) +
1636                                             (pAd->MacTab.
1637                                              Content[SetAsicWcid.WCID].
1638                                              Addr[1] << 8) +
1639                                             (pAd->MacTab.
1640                                              Content[SetAsicWcid.WCID].Addr[0]);
1641                                         DBGPRINT_RAW(RT_DEBUG_TRACE,
1642                                                      ("1-MACValue= %x,\n",
1643                                                       MACValue));
1644                                         RTUSBWriteMACRegister(pAd, offset,
1645                                                               MACValue);
1646                                         /* Read bitmask */
1647                                         RTUSBReadMACRegister(pAd, offset + 4,
1648                                                              &MACRValue);
1649                                         if (SetAsicWcid.DeleteTid != 0xffffffff)
1650                                                 MACRValue &=
1651                                                     (~SetAsicWcid.DeleteTid);
1652                                         if (SetAsicWcid.SetTid != 0xffffffff)
1653                                                 MACRValue |=
1654                                                     (SetAsicWcid.SetTid);
1655                                         MACRValue &= 0xffff0000;
1656
1657                                         MACValue =
1658                                             (pAd->MacTab.
1659                                              Content[SetAsicWcid.WCID].
1660                                              Addr[5] << 8) +
1661                                             pAd->MacTab.Content[SetAsicWcid.
1662                                                                 WCID].Addr[4];
1663                                         MACValue |= MACRValue;
1664                                         RTUSBWriteMACRegister(pAd, offset + 4,
1665                                                               MACValue);
1666
1667                                         DBGPRINT_RAW(RT_DEBUG_TRACE,
1668                                                      ("2-MACValue= %x,\n",
1669                                                       MACValue));
1670                                 }
1671                                 break;
1672
1673                         case CMDTHREAD_SET_ASIC_WCID_CIPHER:
1674                                 {
1675                                         struct rt_set_asic_wcid_attri SetAsicWcidAttri;
1676                                         u16 offset;
1677                                         u32 MACRValue = 0;
1678                                         SHAREDKEY_MODE_STRUC csr1;
1679                                         SetAsicWcidAttri =
1680                                             *((struct rt_set_asic_wcid_attri *)
1681                                               (pData));
1682
1683                                         if (SetAsicWcidAttri.WCID >=
1684                                             MAX_LEN_OF_MAC_TABLE)
1685                                                 return;
1686
1687                                         offset =
1688                                             MAC_WCID_ATTRIBUTE_BASE +
1689                                             ((u8)SetAsicWcidAttri.WCID) *
1690                                             HW_WCID_ATTRI_SIZE;
1691
1692                                         DBGPRINT_RAW(RT_DEBUG_TRACE,
1693                                                      ("Cmd : CMDTHREAD_SET_ASIC_WCID_CIPHER : WCID = %ld, Cipher = %lx.\n",
1694                                                       SetAsicWcidAttri.WCID,
1695                                                       SetAsicWcidAttri.Cipher));
1696                                         /* Read bitmask */
1697                                         RTUSBReadMACRegister(pAd, offset,
1698                                                              &MACRValue);
1699                                         MACRValue = 0;
1700                                         MACRValue |=
1701                                             (((u8)SetAsicWcidAttri.
1702                                               Cipher) << 1);
1703
1704                                         RTUSBWriteMACRegister(pAd, offset,
1705                                                               MACRValue);
1706                                         DBGPRINT_RAW(RT_DEBUG_TRACE,
1707                                                      ("2-offset = %x , MACValue= %x,\n",
1708                                                       offset, MACRValue));
1709
1710                                         offset =
1711                                             PAIRWISE_IVEIV_TABLE_BASE +
1712                                             ((u8)SetAsicWcidAttri.WCID) *
1713                                             HW_IVEIV_ENTRY_SIZE;
1714                                         MACRValue = 0;
1715                                         if ((SetAsicWcidAttri.Cipher <=
1716                                              CIPHER_WEP128))
1717                                                 MACRValue |=
1718                                                     (pAd->StaCfg.
1719                                                      DefaultKeyId << 30);
1720                                         else
1721                                                 MACRValue |= (0x20000000);
1722                                         RTUSBWriteMACRegister(pAd, offset,
1723                                                               MACRValue);
1724                                         DBGPRINT_RAW(RT_DEBUG_TRACE,
1725                                                      ("2-offset = %x , MACValue= %x,\n",
1726                                                       offset, MACRValue));
1727
1728                                         /* */
1729                                         /* Update cipher algorithm. WSTA always use BSS0 */
1730                                         /* */
1731                                         /* for adhoc mode only ,because wep status slow than add key, when use zero config */
1732                                         if (pAd->StaCfg.BssType == BSS_ADHOC) {
1733                                                 offset =
1734                                                     MAC_WCID_ATTRIBUTE_BASE;
1735
1736                                                 RTUSBReadMACRegister(pAd,
1737                                                                      offset,
1738                                                                      &MACRValue);
1739                                                 MACRValue &= (~0xe);
1740                                                 MACRValue |=
1741                                                     (((u8)SetAsicWcidAttri.
1742                                                       Cipher) << 1);
1743
1744                                                 RTUSBWriteMACRegister(pAd,
1745                                                                       offset,
1746                                                                       MACRValue);
1747
1748                                                 /*Update group key cipher,,because wep status slow than add key, when use zero config */
1749                                                 RTUSBReadMACRegister(pAd,
1750                                                                      SHARED_KEY_MODE_BASE
1751                                                                      +
1752                                                                      4 * (0 /
1753                                                                           2),
1754                                                                      &csr1.
1755                                                                      word);
1756
1757                                                 csr1.field.Bss0Key0CipherAlg =
1758                                                     SetAsicWcidAttri.Cipher;
1759                                                 csr1.field.Bss0Key1CipherAlg =
1760                                                     SetAsicWcidAttri.Cipher;
1761
1762                                                 RTUSBWriteMACRegister(pAd,
1763                                                                       SHARED_KEY_MODE_BASE
1764                                                                       +
1765                                                                       4 * (0 /
1766                                                                            2),
1767                                                                       csr1.
1768                                                                       word);
1769                                         }
1770                                 }
1771                                 break;
1772
1773 /*Benson modified for USB interface, avoid in interrupt when write key, 20080724 --> */
1774                         case RT_CMD_SET_KEY_TABLE:      /*General call for AsicAddPairwiseKeyEntry() */
1775                                 {
1776                                         struct rt_add_pairwise_key_entry KeyInfo;
1777                                         KeyInfo =
1778                                             *((struct rt_add_pairwise_key_entry *)
1779                                               (pData));
1780                                         AsicAddPairwiseKeyEntry(pAd,
1781                                                                 KeyInfo.MacAddr,
1782                                                                 (u8)KeyInfo.
1783                                                                 MacTabMatchWCID,
1784                                                                 &KeyInfo.
1785                                                                 CipherKey);
1786                                 }
1787                                 break;
1788
1789                         case RT_CMD_SET_RX_WCID_TABLE:  /*General call for RTMPAddWcidAttributeEntry() */
1790                                 {
1791                                         struct rt_mac_table_entry *pEntry;
1792                                         u8 KeyIdx = 0;
1793                                         u8 CipherAlg = CIPHER_NONE;
1794                                         u8 ApIdx = BSS0;
1795
1796                                         pEntry = (struct rt_mac_table_entry *)(pData);
1797
1798                                         RTMPAddWcidAttributeEntry(pAd,
1799                                                                   ApIdx,
1800                                                                   KeyIdx,
1801                                                                   CipherAlg,
1802                                                                   pEntry);
1803                                 }
1804                                 break;
1805 /*Benson modified for USB interface, avoid in interrupt when write key, 20080724 <-- */
1806
1807                         case CMDTHREAD_SET_CLIENT_MAC_ENTRY:
1808                                 {
1809                                         struct rt_mac_table_entry *pEntry;
1810                                         pEntry = (struct rt_mac_table_entry *)pData;
1811
1812                                         {
1813                                                 AsicRemovePairwiseKeyEntry(pAd,
1814                                                                            pEntry->
1815                                                                            apidx,
1816                                                                            (u8)
1817                                                                            pEntry->
1818                                                                            Aid);
1819                                                 if ((pEntry->AuthMode <=
1820                                                      Ndis802_11AuthModeAutoSwitch)
1821                                                     && (pEntry->WepStatus ==
1822                                                         Ndis802_11Encryption1Enabled))
1823                                                 {
1824                                                         u32 uIV = 1;
1825                                                         u8 *ptr;
1826
1827                                                         ptr = (u8 *)& uIV;
1828                                                         *(ptr + 3) =
1829                                                             (pAd->StaCfg.
1830                                                              DefaultKeyId << 6);
1831                                                         AsicUpdateWCIDIVEIV(pAd,
1832                                                                             pEntry->
1833                                                                             Aid,
1834                                                                             uIV,
1835                                                                             0);
1836                                                         AsicUpdateWCIDAttribute
1837                                                             (pAd, pEntry->Aid,
1838                                                              BSS0,
1839                                                              pAd->
1840                                                              SharedKey[BSS0]
1841                                                              [pAd->StaCfg.
1842                                                               DefaultKeyId].
1843                                                              CipherAlg, FALSE);
1844                                                 } else if (pEntry->AuthMode ==
1845                                                            Ndis802_11AuthModeWPANone)
1846                                                 {
1847                                                         u32 uIV = 1;
1848                                                         u8 *ptr;
1849
1850                                                         ptr = (u8 *)& uIV;
1851                                                         *(ptr + 3) =
1852                                                             (pAd->StaCfg.
1853                                                              DefaultKeyId << 6);
1854                                                         AsicUpdateWCIDIVEIV(pAd,
1855                                                                             pEntry->
1856                                                                             Aid,
1857                                                                             uIV,
1858                                                                             0);
1859                                                         AsicUpdateWCIDAttribute
1860                                                             (pAd, pEntry->Aid,
1861                                                              BSS0,
1862                                                              pAd->
1863                                                              SharedKey[BSS0]
1864                                                              [pAd->StaCfg.
1865                                                               DefaultKeyId].
1866                                                              CipherAlg, FALSE);
1867                                                 } else {
1868                                                         /* */
1869                                                         /* Other case, disable engine. */
1870                                                         /* Don't worry WPA key, we will add WPA Key after 4-Way handshaking. */
1871                                                         /* */
1872                                                         u16 offset;
1873                                                         offset =
1874                                                             MAC_WCID_ATTRIBUTE_BASE
1875                                                             +
1876                                                             (pEntry->Aid *
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
1880                                                             (pAd, offset, 0);
1881                                                 }
1882                                         }
1883
1884                                         AsicUpdateRxWCIDTable(pAd, pEntry->Aid,
1885                                                               pEntry->Addr);
1886                                         DBGPRINT(RT_DEBUG_TRACE,
1887                                                  ("UpdateRxWCIDTable(): Aid=%d, Addr=%02x:%02x:%02x:%02x:%02x:%02x!\n",
1888                                                   pEntry->Aid, pEntry->Addr[0],
1889                                                   pEntry->Addr[1],
1890                                                   pEntry->Addr[2],
1891                                                   pEntry->Addr[3],
1892                                                   pEntry->Addr[4],
1893                                                   pEntry->Addr[5]));
1894                                 }
1895                                 break;
1896
1897 /* add by johnli, fix "in_interrupt" error when call "MacTableDeleteEntry" in Rx tasklet */
1898                         case CMDTHREAD_UPDATE_PROTECT:
1899                                 {
1900                                         AsicUpdateProtect(pAd, 0,
1901                                                           (ALLN_SETPROTECT),
1902                                                           TRUE, 0);
1903                                 }
1904                                 break;
1905 /* end johnli */
1906
1907                         case OID_802_11_ADD_WEP:
1908                                 {
1909                                         u32 i;
1910                                         u32 KeyIdx;
1911                                         struct rt_ndis_802_11_wep *pWepKey;
1912
1913                                         DBGPRINT(RT_DEBUG_TRACE,
1914                                                  ("CmdThread::OID_802_11_ADD_WEP  \n"));
1915
1916                                         pWepKey = (struct rt_ndis_802_11_wep *)pData;
1917                                         KeyIdx = pWepKey->KeyIndex & 0x0fffffff;
1918
1919                                         /* it is a shared key */
1920                                         if ((KeyIdx >= 4)
1921                                             || ((pWepKey->KeyLength != 5)
1922                                                 && (pWepKey->KeyLength !=
1923                                                     13))) {
1924                                                 NdisStatus =
1925                                                     NDIS_STATUS_INVALID_DATA;
1926                                                 DBGPRINT(RT_DEBUG_ERROR,
1927                                                          ("CmdThread::OID_802_11_ADD_WEP, INVALID_DATA!!\n"));
1928                                         } else {
1929                                                 u8 CipherAlg;
1930                                                 pAd->SharedKey[BSS0][KeyIdx].
1931                                                     KeyLen =
1932                                                     (u8)pWepKey->KeyLength;
1933                                                 NdisMoveMemory(pAd->
1934                                                                SharedKey[BSS0]
1935                                                                [KeyIdx].Key,
1936                                                                &pWepKey->
1937                                                                KeyMaterial,
1938                                                                pWepKey->
1939                                                                KeyLength);
1940                                                 CipherAlg =
1941                                                     (pAd->
1942                                                      SharedKey[BSS0][KeyIdx].
1943                                                      KeyLen ==
1944                                                      5) ? CIPHER_WEP64 :
1945                                                     CIPHER_WEP128;
1946
1947                                                 /* */
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. */
1950                                                 /* */
1951
1952                                                 if (pAd->OpMode == OPMODE_STA) {
1953                                                         pAd->MacTab.
1954                                                             Content[BSSID_WCID].
1955                                                             PairwiseKey.
1956                                                             CipherAlg =
1957                                                             pAd->
1958                                                             SharedKey[BSS0]
1959                                                             [KeyIdx].CipherAlg;
1960                                                         pAd->MacTab.
1961                                                             Content[BSSID_WCID].
1962                                                             PairwiseKey.KeyLen =
1963                                                             pAd->
1964                                                             SharedKey[BSS0]
1965                                                             [KeyIdx].KeyLen;
1966                                                 }
1967                                                 pAd->SharedKey[BSS0][KeyIdx].
1968                                                     CipherAlg = CipherAlg;
1969                                                 if (pWepKey->
1970                                                     KeyIndex & 0x80000000) {
1971                                                         /* Default key for tx (shared key) */
1972                                                         u8 IVEIV[8];
1973                                                         u32 WCIDAttri, Value;
1974                                                         u16 offset, offset2;
1975                                                         NdisZeroMemory(IVEIV,
1976                                                                        8);
1977                                                         pAd->StaCfg.
1978                                                             DefaultKeyId =
1979                                                             (u8)KeyIdx;
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 */
1982                                                         WCIDAttri =
1983                                                             (CipherAlg << 1) |
1984                                                             SHAREDKEYTABLE;
1985
1986                                                         offset =
1987                                                             MAC_WCID_ATTRIBUTE_BASE
1988                                                             +
1989                                                             (BSSID_WCID *
1990                                                              HW_WCID_ATTRI_SIZE);
1991                                                         RTUSBWriteMACRegister
1992                                                             (pAd, offset,
1993                                                              WCIDAttri);
1994                                                         /* 1. IV/EIV */
1995                                                         /* Specify key index to find shared key. */
1996                                                         IVEIV[3] = (u8)(KeyIdx << 6);   /*WEP Eiv bit off. groupkey index is not 0 */
1997                                                         offset =
1998                                                             PAIRWISE_IVEIV_TABLE_BASE
1999                                                             +
2000                                                             (BSS0Mcast_WCID *
2001                                                              HW_IVEIV_ENTRY_SIZE);
2002                                                         offset2 =
2003                                                             PAIRWISE_IVEIV_TABLE_BASE
2004                                                             +
2005                                                             (BSSID_WCID *
2006                                                              HW_IVEIV_ENTRY_SIZE);
2007                                                         for (i = 0; i < 8;) {
2008                                                                 Value =
2009                                                                     IVEIV[i];
2010                                                                 Value +=
2011                                                                     (IVEIV
2012                                                                      [i +
2013                                                                       1] << 8);
2014                                                                 Value +=
2015                                                                     (IVEIV
2016                                                                      [i +
2017                                                                       2] << 16);
2018                                                                 Value +=
2019                                                                     (IVEIV
2020                                                                      [i +
2021                                                                       3] << 24);
2022                                                                 RTUSBWriteMACRegister
2023                                                                     (pAd,
2024                                                                      offset + i,
2025                                                                      Value);
2026                                                                 RTUSBWriteMACRegister
2027                                                                     (pAd,
2028                                                                      offset2 +
2029                                                                      i, Value);
2030                                                                 i += 4;
2031                                                         }
2032
2033                                                         /* 2. WCID Attribute UDF:3, BSSIdx:3, Alg:3, Keytable:use share key, BSSIdx is 0 */
2034                                                         WCIDAttri =
2035                                                             (pAd->
2036                                                              SharedKey[BSS0]
2037                                                              [KeyIdx].
2038                                                              CipherAlg << 1) |
2039                                                             SHAREDKEYTABLE;
2040                                                         offset =
2041                                                             MAC_WCID_ATTRIBUTE_BASE
2042                                                             +
2043                                                             (BSS0Mcast_WCID *
2044                                                              HW_WCID_ATTRI_SIZE);
2045                                                         DBGPRINT(RT_DEBUG_TRACE,
2046                                                                  ("BSS0Mcast_WCID : offset = %x, WCIDAttri = %x\n",
2047                                                                   offset,
2048                                                                   WCIDAttri));
2049                                                         RTUSBWriteMACRegister
2050                                                             (pAd, offset,
2051                                                              WCIDAttri);
2052
2053                                                 }
2054                                                 AsicAddSharedKeyEntry(pAd, BSS0,
2055                                                                       (u8)
2056                                                                       KeyIdx,
2057                                                                       CipherAlg,
2058                                                                       pWepKey->
2059                                                                       KeyMaterial,
2060                                                                       NULL,
2061                                                                       NULL);
2062                                                 DBGPRINT(RT_DEBUG_TRACE,
2063                                                          ("CmdThread::OID_802_11_ADD_WEP (KeyIdx=%d, Len=%d-byte)\n",
2064                                                           KeyIdx,
2065                                                           pWepKey->KeyLength));
2066                                         }
2067                                 }
2068                                 break;
2069
2070                         case CMDTHREAD_802_11_COUNTER_MEASURE:
2071                                 break;
2072
2073                         case CMDTHREAD_SET_GROUP_KEY:
2074                                 WpaStaGroupKeySetting(pAd);
2075                                 break;
2076
2077                         case CMDTHREAD_SET_PAIRWISE_KEY:
2078                                 WpaStaPairwiseKeySetting(pAd);
2079                                 break;
2080
2081                         case CMDTHREAD_SET_PSM_BIT:
2082                                 {
2083                                         u16 *pPsm = (u16 *) pData;
2084                                         MlmeSetPsmBit(pAd, *pPsm);
2085                                 }
2086                                 break;
2087                         case CMDTHREAD_FORCE_WAKE_UP:
2088                                 AsicForceWakeup(pAd, TRUE);
2089                                 break;
2090
2091                         default:
2092                                 DBGPRINT(RT_DEBUG_ERROR,
2093                                          ("--> Control Thread !! ERROR !! Unknown(cmdqelmt->command=0x%x) !! \n",
2094                                           cmdqelmt->command));
2095                                 break;
2096                         }
2097                 }
2098
2099                 if (cmdqelmt->CmdFromNdis == TRUE) {
2100                         if (cmdqelmt->buffer != NULL)
2101                                 os_free_mem(pAd, cmdqelmt->buffer);
2102                         os_free_mem(pAd, cmdqelmt);
2103                 } else {
2104                         if ((cmdqelmt->buffer != NULL)
2105                             && (cmdqelmt->bufferlength != 0))
2106                                 os_free_mem(pAd, cmdqelmt->buffer);
2107                         os_free_mem(pAd, cmdqelmt);
2108                 }
2109         }                       /* end of while */
2110 }
2111
2112 #endif /* RTMP_MAC_USB // */