Staging: rt3070: remove dead UCOS code
[linux-2.6] / drivers / staging / rt3070 / rt_ate.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
28 #include "rt_config.h"
29
30 #define ATE_BBP_REG_NUM 168
31 UCHAR restore_BBP[ATE_BBP_REG_NUM]={0};
32
33 #ifdef RALINK_ATE
34 UCHAR TemplateFrame[24] = {0x08/* Data type */,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x00,0xAA,0xBB,0x12,0x34,0x56,0x00,0x11,0x22,0xAA,0xBB,0xCC,0x00,0x00};     // 802.11 MAC Header, Type:Data, Length:24bytes
35 extern RTMP_RF_REGS RF2850RegTable[];
36 extern UCHAR NUM_OF_2850_CHNL;
37
38 #ifdef RT2870
39 extern UCHAR EpToQueue[];
40 extern VOID     RTUSBRejectPendingPackets(      IN      PRTMP_ADAPTER   pAd);
41 #endif // RT2870 //
42
43 #ifdef RT30xx
44 //2008/07/10:KH adds to support 3070 ATE<--
45 extern FREQUENCY_ITEM FreqItems3020[];
46 extern UCHAR NUM_OF_3020_CHNL;
47 //2008/07/10:KH adds to support 3070 ATE-->
48 #endif // RT30xx //
49
50 static CHAR CCKRateTable[] = {0, 1, 2, 3, 8, 9, 10, 11, -1}; /* CCK Mode. */
51 static CHAR OFDMRateTable[] = {0, 1, 2, 3, 4, 5, 6, 7, -1}; /* OFDM Mode. */
52 static CHAR HTMIXRateTable[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, -1}; /* HT Mix Mode. */
53
54 static INT TxDmaBusy(
55         IN PRTMP_ADAPTER pAd);
56
57 static INT RxDmaBusy(
58         IN PRTMP_ADAPTER pAd);
59
60 static VOID RtmpDmaEnable(
61         IN PRTMP_ADAPTER pAd,
62         IN INT Enable);
63
64 static VOID BbpSoftReset(
65         IN PRTMP_ADAPTER pAd);
66
67 static VOID RtmpRfIoWrite(
68         IN PRTMP_ADAPTER pAd);
69
70 static INT ATESetUpFrame(
71         IN PRTMP_ADAPTER pAd,
72         IN UINT32 TxIdx);
73
74 static INT ATETxPwrHandler(
75         IN PRTMP_ADAPTER pAd,
76         IN char index);
77
78 static INT ATECmdHandler(
79         IN      PRTMP_ADAPTER   pAd,
80         IN      PUCHAR                  arg);
81
82 static int CheckMCSValid(
83         IN UCHAR Mode,
84         IN UCHAR Mcs);
85
86
87 #ifdef RT2870
88 static VOID ATEWriteTxInfo(
89         IN      PRTMP_ADAPTER   pAd,
90         IN      PTXINFO_STRUC   pTxInfo,
91         IN        USHORT                USBDMApktLen,
92         IN        BOOLEAN               bWiv,
93         IN        UCHAR                 QueueSel,
94         IN        UCHAR                 NextValid,
95         IN        UCHAR                 TxBurst);
96
97 static VOID ATEWriteTxWI(
98         IN      PRTMP_ADAPTER   pAd,
99         IN      PTXWI_STRUC     pTxWI,
100         IN      BOOLEAN                 FRAG,
101         IN      BOOLEAN                 InsTimestamp,
102         IN      BOOLEAN                 AMPDU,
103         IN      BOOLEAN                 Ack,
104         IN      BOOLEAN                 NSeq,           // HW new a sequence.
105         IN      UCHAR                   BASize,
106         IN      UCHAR                   WCID,
107         IN      ULONG                   Length,
108         IN      UCHAR                   PID,
109         IN      UCHAR                   MIMOps,
110         IN      UCHAR                   Txopmode,
111         IN      BOOLEAN                 CfAck,
112         IN      HTTRANSMIT_SETTING      Transmit);
113
114 #endif // RT2870 //
115
116 static VOID SetJapanFilter(
117         IN      PRTMP_ADAPTER   pAd);
118
119 /*=========================end of prototype=========================*/
120
121
122 #ifdef RT2870
123 static INT TxDmaBusy(
124         IN PRTMP_ADAPTER pAd)
125 {
126         INT result;
127         USB_DMA_CFG_STRUC UsbCfg;
128
129         RTMP_IO_READ32(pAd, USB_DMA_CFG, &UsbCfg.word); // disable DMA
130         if (UsbCfg.field.TxBusy)
131                 result = 1;
132         else
133                 result = 0;
134
135         return result;
136 }
137
138 static INT RxDmaBusy(
139         IN PRTMP_ADAPTER pAd)
140 {
141         INT result;
142         USB_DMA_CFG_STRUC UsbCfg;
143
144         RTMP_IO_READ32(pAd, USB_DMA_CFG, &UsbCfg.word); // disable DMA
145         if (UsbCfg.field.RxBusy)
146                 result = 1;
147         else
148                 result = 0;
149
150         return result;
151 }
152
153 static VOID RtmpDmaEnable(
154         IN PRTMP_ADAPTER pAd,
155         IN INT Enable)
156 {
157         BOOLEAN value;
158         ULONG WaitCnt;
159         USB_DMA_CFG_STRUC UsbCfg;
160
161         value = Enable > 0 ? 1 : 0;
162
163         // check DMA is in busy mode.
164         WaitCnt = 0;
165         while (TxDmaBusy(pAd) || RxDmaBusy(pAd))
166         {
167                 RTMPusecDelay(10);
168                 if (WaitCnt++ > 100)
169                         break;
170         }
171
172         //Why not to clear USB DMA TX path first ???
173         RTMP_IO_READ32(pAd, USB_DMA_CFG, &UsbCfg.word); // disable DMA
174         UsbCfg.field.TxBulkEn = value;
175         UsbCfg.field.RxBulkEn = value;
176         RTMP_IO_WRITE32(pAd, USB_DMA_CFG, UsbCfg.word); // abort all TX rings
177         RTMPusecDelay(5000);
178
179         return;
180 }
181 #endif // RT2870 //
182
183 static VOID BbpSoftReset(
184         IN PRTMP_ADAPTER pAd)
185 {
186         UCHAR BbpData = 0;
187
188         // Soft reset, set BBP R21 bit0=1->0
189         ATE_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R21, &BbpData);
190         BbpData |= 0x00000001; //set bit0=1
191         ATE_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R21, BbpData);
192
193         ATE_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R21, &BbpData);
194         BbpData &= ~(0x00000001); //set bit0=0
195         ATE_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R21, BbpData);
196
197         return;
198 }
199
200 static VOID RtmpRfIoWrite(
201         IN PRTMP_ADAPTER pAd)
202 {
203         // Set RF value 1's set R3[bit2] = [0]
204         RTMP_RF_IO_WRITE32(pAd, pAd->LatchRfRegs.R1);
205         RTMP_RF_IO_WRITE32(pAd, pAd->LatchRfRegs.R2);
206         RTMP_RF_IO_WRITE32(pAd, (pAd->LatchRfRegs.R3 & (~0x04)));
207         RTMP_RF_IO_WRITE32(pAd, pAd->LatchRfRegs.R4);
208
209         RTMPusecDelay(200);
210
211         // Set RF value 2's set R3[bit2] = [1]
212         RTMP_RF_IO_WRITE32(pAd, pAd->LatchRfRegs.R1);
213         RTMP_RF_IO_WRITE32(pAd, pAd->LatchRfRegs.R2);
214         RTMP_RF_IO_WRITE32(pAd, (pAd->LatchRfRegs.R3 | 0x04));
215         RTMP_RF_IO_WRITE32(pAd, pAd->LatchRfRegs.R4);
216
217         RTMPusecDelay(200);
218
219         // Set RF value 3's set R3[bit2] = [0]
220         RTMP_RF_IO_WRITE32(pAd, pAd->LatchRfRegs.R1);
221         RTMP_RF_IO_WRITE32(pAd, pAd->LatchRfRegs.R2);
222         RTMP_RF_IO_WRITE32(pAd, (pAd->LatchRfRegs.R3 & (~0x04)));
223         RTMP_RF_IO_WRITE32(pAd, pAd->LatchRfRegs.R4);
224
225         return;
226 }
227
228 static int CheckMCSValid(
229         UCHAR Mode,
230         UCHAR Mcs)
231 {
232         int i;
233         PCHAR pRateTab;
234
235         switch(Mode)
236         {
237                 case 0:
238                         pRateTab = CCKRateTable;
239                         break;
240                 case 1:
241                         pRateTab = OFDMRateTable;
242                         break;
243                 case 2:
244                 case 3:
245                         pRateTab = HTMIXRateTable;
246                         break;
247                 default:
248                         ATEDBGPRINT(RT_DEBUG_ERROR, ("unrecognizable Tx Mode %d\n", Mode));
249                         return -1;
250                         break;
251         }
252
253         i = 0;
254         while(pRateTab[i] != -1)
255         {
256                 if (pRateTab[i] == Mcs)
257                         return 0;
258                 i++;
259         }
260
261         return -1;
262 }
263
264 #if 1
265 static INT ATETxPwrHandler(
266         IN PRTMP_ADAPTER pAd,
267         IN char index)
268 {
269         ULONG R;
270         CHAR TxPower;
271         UCHAR Bbp94 = 0;
272         BOOLEAN bPowerReduce = FALSE;
273 #ifdef RT30xx
274         UCHAR RFValue;
275 #endif // RT30xx //
276 #ifdef RALINK_28xx_QA
277         if ((pAd->ate.bQATxStart == TRUE) || (pAd->ate.bQARxStart == TRUE))
278         {
279                 /* When QA is used for Tx, pAd->ate.TxPower0/1 and real tx power
280                 ** are not synchronized.
281                 */
282 /*
283                 pAd->ate.TxPower0 = pAd->LatchRfRegs.xxx;
284                 pAd->ate.TxPower1 = pAd->LatchRfRegs.xxx;
285 */
286                 return 0;
287         }
288         else
289 #endif // RALINK_28xx_QA //
290         {
291                 TxPower = index == 0 ? pAd->ate.TxPower0 : pAd->ate.TxPower1;
292
293                 if (pAd->ate.Channel <= 14)
294                 {
295                         if (TxPower > 31)
296                         {
297                                 //
298                                 // R3, R4 can't large than 31 (0x24), 31 ~ 36 used by BBP 94
299                                 //
300                                 R = 31;
301                                 if (TxPower <= 36)
302                                         Bbp94 = BBPR94_DEFAULT + (UCHAR)(TxPower - 31);
303                         }
304                         else if (TxPower < 0)
305                         {
306                                 //
307                                 // R3, R4 can't less than 0, -1 ~ -6 used by BBP 94
308                                 //
309                                 R = 0;
310                                 if (TxPower >= -6)
311                                         Bbp94 = BBPR94_DEFAULT + TxPower;
312                         }
313                         else
314                         {
315                                 // 0 ~ 31
316                                 R = (ULONG) TxPower;
317                                 Bbp94 = BBPR94_DEFAULT;
318                         }
319
320                         ATEDBGPRINT(RT_DEBUG_TRACE, ("%s (TxPower=%d, R=%ld, BBP_R94=%d)\n", __func__, TxPower, R, Bbp94));
321                 }
322                 else// 5.5 GHz
323                 {
324                         if (TxPower > 15)
325                         {
326                                 //
327                                 // R3, R4 can't large than 15 (0x0F)
328                                 //
329                                 R = 15;
330                         }
331                         else if (TxPower < 0)
332                         {
333                                 //
334                                 // R3, R4 can't less than 0
335                                 //
336                                 // -1 ~ -7
337                                 ASSERT((TxPower >= -7));
338                                 R = (ULONG)(TxPower + 7);
339                                 bPowerReduce = TRUE;
340                         }
341                         else
342                         {
343                                 // 0 ~ 15
344                                 R = (ULONG) TxPower;
345                         }
346
347                         ATEDBGPRINT(RT_DEBUG_TRACE, ("%s (TxPower=%d, R=%lu)\n", __func__, TxPower, R));
348                 }
349 //2008/09/10:KH adds to support 3070 ATE TX Power tunning real time<--
350 #ifdef RT30xx
351                 if(IS_RT30xx(pAd))
352                 {
353                         // Set Tx Power
354
355                                         RT30xxReadRFRegister(pAd, RF_R12, (PUCHAR)&RFValue);
356                                         RFValue = (RFValue & 0xE0) | TxPower;
357                                         RT30xxWriteRFRegister(pAd, RF_R12, (UCHAR)RFValue);
358                                         ATEDBGPRINT(RT_DEBUG_TRACE, ("3070 or 2070:%s (TxPower=%d, RFValue=%x)\n", __func__, TxPower, RFValue));
359
360                 }
361                 else
362 #endif // RT30xx //
363               {
364                 if (pAd->ate.Channel <= 14)
365                 {
366                         if (index == 0)
367                         {
368                                 R = R << 9;             // shift TX power control to correct RF(R3) register bit position
369                                 R |= (pAd->LatchRfRegs.R3 & 0xffffc1ff);
370                                 pAd->LatchRfRegs.R3 = R;
371                         }
372                         else
373                         {
374                                 R = R << 6;             // shift TX power control to correct RF(R4) register bit position
375                                 R |= (pAd->LatchRfRegs.R4 & 0xfffff83f);
376                                 pAd->LatchRfRegs.R4 = R;
377                         }
378                 }
379                 else// 5.5GHz
380                 {
381                         if (bPowerReduce == FALSE)
382                         {
383                                 if (index == 0)
384                                 {
385                                         R = (R << 10) | (1 << 9);               // shift TX power control to correct RF(R3) register bit position
386                                         R |= (pAd->LatchRfRegs.R3 & 0xffffc1ff);
387                                         pAd->LatchRfRegs.R3 = R;
388                                 }
389                                 else
390                                 {
391                                         R = (R << 7) | (1 << 6);                // shift TX power control to correct RF(R4) register bit position
392                                         R |= (pAd->LatchRfRegs.R4 & 0xfffff83f);
393                                         pAd->LatchRfRegs.R4 = R;
394                                 }
395                         }
396                         else
397                         {
398                                 if (index == 0)
399                                 {
400                                         R = (R << 10);          // shift TX power control to correct RF(R3) register bit position
401                                         R |= (pAd->LatchRfRegs.R3 & 0xffffc1ff);
402
403                                         /* Clear bit 9 of R3 to reduce 7dB. */
404                                         pAd->LatchRfRegs.R3 = (R & (~(1 << 9)));
405                                 }
406                                 else
407                                 {
408                                         R = (R << 7);           // shift TX power control to correct RF(R4) register bit position
409                                         R |= (pAd->LatchRfRegs.R4 & 0xfffff83f);
410
411                                         /* Clear bit 6 of R4 to reduce 7dB. */
412                                         pAd->LatchRfRegs.R4 = (R & (~(1 << 6)));
413                                 }
414                         }
415                 }
416                 RtmpRfIoWrite(pAd);
417            }
418 //2008/09/10:KH adds to support 3070 ATE TX Power tunning real time-->
419
420                 return 0;
421         }
422 }
423 #else// 1 //
424 static INT ATETxPwrHandler(
425         IN PRTMP_ADAPTER pAd,
426         IN char index)
427 {
428         ULONG R;
429         CHAR TxPower;
430         UCHAR Bbp94 = 0;
431
432 #ifdef RALINK_28xx_QA
433         if ((pAd->ate.bQATxStart == TRUE) || (pAd->ate.bQARxStart == TRUE))
434         {
435                 // TODO: how to get current TxPower0/1 from pAd->LatchRfRegs ?
436                 /* When QA is used for Tx, pAd->ate.TxPower0/1 and real tx power
437                 ** are not synchronized.
438                 */
439 /*
440                 pAd->ate.TxPower0 = pAd->LatchRfRegs.xxx;
441                 pAd->ate.TxPower1 = pAd->LatchRfRegs.xxx;
442 */
443                 return 0;
444         }
445         else
446 #endif // RALINK_28xx_QA //
447         {
448                 TxPower = index == 0 ? pAd->ate.TxPower0 : pAd->ate.TxPower1;
449
450         if (TxPower > 31)
451         {
452                 //
453                 // R3, R4 can't large than 36 (0x24), 31 ~ 36 used by BBP 94
454                 //
455                 R = 31;
456                 if (TxPower <= 36)
457                         Bbp94 = BBPR94_DEFAULT + (UCHAR)(TxPower - 31);
458         }
459         else if (TxPower < 0)
460         {
461                 //
462                 // R3, R4 can't less than 0, -1 ~ -6 used by BBP 94
463                 //
464                 R = 0;
465                 if (TxPower >= -6)
466                         Bbp94 = BBPR94_DEFAULT + TxPower;
467         }
468         else
469         {
470                 // 0 ~ 31
471                 R = (ULONG) TxPower;
472                 Bbp94 = BBPR94_DEFAULT;
473         }
474
475         ATEDBGPRINT(RT_DEBUG_TRACE, ("%s (TxPower=%d, R3=%ld, BBP_R94=%d)\n", __func__, TxPower, R, Bbp94));
476
477                 if (pAd->ate.Channel <= 14)
478                 {
479         if (index == 0)
480         {
481                 R = R << 9;             // shift TX power control to correct RF(R3) register bit position
482                 R |= (pAd->LatchRfRegs.R3 & 0xffffc1ff);
483                 pAd->LatchRfRegs.R3 = R;
484         }
485         else
486         {
487                 R = R << 6;             // shift TX power control to correct RF(R4) register bit position
488                 R |= (pAd->LatchRfRegs.R4 & 0xfffff83f);
489                 pAd->LatchRfRegs.R4 = R;
490         }
491                 }
492                 else
493                 {
494                         if (index == 0)
495                         {
496                                 R = (R << 10) | (1 << 9);               // shift TX power control to correct RF(R3) register bit position
497                                 R |= (pAd->LatchRfRegs.R3 & 0xffffc1ff);
498                                 pAd->LatchRfRegs.R3 = R;
499                         }
500                         else
501                         {
502                                 R = (R << 7) | (1 << 6);                // shift TX power control to correct RF(R4) register bit position
503                                 R |= (pAd->LatchRfRegs.R4 & 0xfffff83f);
504                                 pAd->LatchRfRegs.R4 = R;
505                         }
506                 }
507
508         RtmpRfIoWrite(pAd);
509
510         return 0;
511         }
512 }
513 #endif // 1 //
514 /*
515     ==========================================================================
516     Description:
517         Set ATE operation mode to
518         0. ATESTART  = Start ATE Mode
519         1. ATESTOP   = Stop ATE Mode
520         2. TXCONT    = Continuous Transmit
521         3. TXCARR    = Transmit Carrier
522         4. TXFRAME   = Transmit Frames
523         5. RXFRAME   = Receive Frames
524 #ifdef RALINK_28xx_QA
525         6. TXSTOP    = Stop Any Type of Transmition
526         7. RXSTOP    = Stop Receiving Frames
527 #endif // RALINK_28xx_QA //
528     Return:
529         TRUE if all parameters are OK, FALSE otherwise
530     ==========================================================================
531 */
532 /*                                                           */
533 /*                                                           */
534 /*=======================End of RT2860=======================*/
535
536
537 /*======================Start of RT2870======================*/
538 /*                                                           */
539 /*                                                           */
540
541 #ifdef RT2870
542 static INT      ATECmdHandler(
543         IN      PRTMP_ADAPTER   pAd,
544         IN      PUCHAR                  arg)
545 {
546         UINT32                  Value;
547         UCHAR                   BbpData;
548         UINT32          MacData;
549         UINT                    i=0, atemode;
550         //NDIS_STATUS           Status = NDIS_STATUS_SUCCESS;
551         //PUCHAR                        pDest;
552         UINT32                  temp;
553         ULONG                   IrqFlags;
554
555         ATEDBGPRINT(RT_DEBUG_TRACE, ("===> ATECmdHandler()\n"));
556         ATEAsicSwitchChannel(pAd);
557         /* AsicLockChannel() is empty function so far in fact */
558         AsicLockChannel(pAd, pAd->ate.Channel);
559
560         RTMPusecDelay(5000);
561
562         // Default value in BBP R22 is 0x0.
563         BbpData = 0;
564
565         /* Enter ATE mode and set Tx/Rx Idle */
566         if (!strcmp(arg, "ATESTART"))
567         {
568 #ifdef CONFIG_STA_SUPPORT
569                 BOOLEAN       Cancelled;
570 #endif // CONFIG_STA_SUPPORT //
571                 ATEDBGPRINT(RT_DEBUG_TRACE, ("ATE: ATESTART\n"));
572
573                 netif_stop_queue(pAd->net_dev);
574
575                 atemode = pAd->ate.Mode;
576                 pAd->ate.Mode = ATE_START;
577 //              pAd->ate.TxDoneCount = pAd->ate.TxCount;
578                 // Disable Rx
579                 RTMP_IO_READ32(pAd, MAC_SYS_CTRL, &Value);
580                 Value &= ~(1 << 3);
581                 RTMP_IO_WRITE32(pAd, MAC_SYS_CTRL, Value);
582
583                 // Disable auto responder
584                 RTMP_IO_READ32(pAd, AUTO_RSP_CFG, &temp);
585                 temp = temp & 0xFFFFFFFE;
586                 RTMP_IO_WRITE32(pAd, AUTO_RSP_CFG, temp);
587
588                 // read MAC_SYS_CTRL and backup MAC_SYS_CTRL value.
589                 RTMP_IO_READ32(pAd, MAC_SYS_CTRL, &MacData);
590                 // clean bit4 to stop continuous Tx production test.
591                 MacData &= 0xFFFFFFEF;
592                 // Stop continuous TX production test.
593                 RTMP_IO_WRITE32(pAd, MAC_SYS_CTRL, MacData);//disable or cancel pending irp first ???
594
595         if (atemode & ATE_TXCARR
596 #ifdef RT30xx
597  || atemode & ATE_TXCONT
598 #endif // RT30xx //
599 )
600                 {
601 #ifdef RT30xx
602                         //Hardware Reset BBP
603                         RTMP_IO_READ32(pAd, MAC_SYS_CTRL, &temp);
604                         temp = temp |0x00000002;
605                         RTMP_IO_WRITE32(pAd, MAC_SYS_CTRL, temp);
606                         RTMP_IO_READ32(pAd, MAC_SYS_CTRL, &temp);
607                         temp = temp & ~(0x00000002);
608                         RTMP_IO_WRITE32(pAd, MAC_SYS_CTRL, temp);
609                         //Restore All BBP Value
610                         for(i=0;i<ATE_BBP_REG_NUM;i++)
611                         ATE_BBP_IO_WRITE8_BY_REG_ID(pAd,i,restore_BBP[i]);
612 #endif // RT30xx //
613
614                         // No Carrier Test set BBP R22 bit7=0, bit6=0, bit[5~0]=0x0
615                         ATE_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R22, &BbpData);
616                         BbpData &= 0xFFFFFF00; //clear bit7, bit6, bit[5~0]
617                     ATE_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R22, BbpData);
618                 }
619                 else if (atemode & ATE_TXCARRSUPP)
620                 {
621 #ifdef RT30xx
622                         //Hardware Reset BBP
623                         RTMP_IO_READ32(pAd, MAC_SYS_CTRL, &temp);
624                         temp = temp |0x00000002;
625                         RTMP_IO_WRITE32(pAd, MAC_SYS_CTRL, temp);
626                         RTMP_IO_READ32(pAd, MAC_SYS_CTRL, &temp);
627                         temp = temp & ~(0x00000002);
628                         RTMP_IO_WRITE32(pAd, MAC_SYS_CTRL, temp);
629                         //Restore All BBP Value
630                         for(i=0;i<ATE_BBP_REG_NUM;i++)
631                         ATE_BBP_IO_WRITE8_BY_REG_ID(pAd,i,restore_BBP[i]);
632 #endif // RT30xx //
633
634                         // No Cont. TX set BBP R22 bit7=0
635                         ATE_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R22, &BbpData);
636                         BbpData &= ~(1 << 7); //set bit7=0
637                         ATE_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R22, BbpData);
638
639                         // No Carrier Suppression set BBP R24 bit0=0
640                         ATE_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R24, &BbpData);
641                         BbpData &= 0xFFFFFFFE; //clear bit0
642                     ATE_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R24, BbpData);
643                 }
644                 // We should free some resource which allocate when ATE_TXFRAME , ATE_STOP, and ATE_TXCONT.
645                 // TODO:Should we free some resource which was allocated when LoopBack and ATE_STOP ?
646                 else if ((atemode & ATE_TXFRAME) || (atemode == ATE_STOP))
647                 {
648                         if (atemode & ATE_TXCONT)
649                         {
650                                 // Not Cont. TX anymore, so set BBP R22 bit7=0
651                                 ATE_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R22, &BbpData);
652                                 BbpData &= ~(1 << 7); //set bit7=0
653                                 ATE_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R22, BbpData);
654                         }
655                         // Abort Tx, Rx DMA.
656                         RtmpDmaEnable(pAd, 0);
657
658                         {
659                                 // It seems nothing to free,
660                                 // because we didn't allocate any resource when we entered ATE_TXFRAME mode latestly.
661                         }
662
663                         // Start Tx, RX DMA
664                         RtmpDmaEnable(pAd, 1);
665                 }
666
667                 RTUSBRejectPendingPackets(pAd);
668                 RTUSBCleanUpDataBulkOutQueue(pAd);
669
670 #ifdef CONFIG_STA_SUPPORT
671                 //
672                 // It will be called in MlmeSuspend().
673                 //
674                 // Cancel pending timers
675                 RTMPCancelTimer(&pAd->MlmeAux.AssocTimer,     &Cancelled);
676                 RTMPCancelTimer(&pAd->MlmeAux.ReassocTimer,   &Cancelled);
677                 RTMPCancelTimer(&pAd->MlmeAux.DisassocTimer,   &Cancelled);
678                 RTMPCancelTimer(&pAd->MlmeAux.AuthTimer,       &Cancelled);
679                 RTMPCancelTimer(&pAd->MlmeAux.BeaconTimer,     &Cancelled);
680                 RTMPCancelTimer(&pAd->MlmeAux.ScanTimer,       &Cancelled);
681 #endif // CONFIG_STA_SUPPORT //
682
683                 //RTUSBCleanUpMLMEWaitQueue(pAd);       /* not used in RT28xx */
684                 RTUSBCleanUpMLMEBulkOutQueue(pAd);
685
686                 // Sometimes kernel will hang on, so we avoid calling MlmeSuspend().
687 //              MlmeSuspend(pAd, TRUE);
688                 //RTMPCancelTimer(&pAd->Mlme.PeriodicTimer, &Cancelled);
689
690                 // Disable Rx
691                 RTMP_IO_READ32(pAd, MAC_SYS_CTRL, &Value);
692                 Value &= ~(1 << 3);
693                 RTMP_IO_WRITE32(pAd, MAC_SYS_CTRL, Value);
694
695                 // Abort Tx, RX DMA.
696                 RtmpDmaEnable(pAd, 0);
697
698                 // Disable Tx
699                 RTMP_IO_READ32(pAd, MAC_SYS_CTRL, &Value);
700                 Value &= ~(1 << 2);
701                 RTMP_IO_WRITE32(pAd, MAC_SYS_CTRL, Value);
702
703                 // Make sure there are no pending bulk in/out IRPs before we go on.
704 /*=========================================================================*/
705                 /* pAd->PendingRx is not of type atomic_t anymore in 28xx */
706 //              while ((atomic_read(&pAd->PendingRx) > 0))      //pAd->BulkFlags != 0 wait bulk out finish
707                 while ((pAd->PendingRx > 0))    //pAd->BulkFlags != 0 wait bulk out finish
708                 {
709 #if 1
710                         ATE_RTUSBCancelPendingBulkInIRP(pAd);
711 #else
712                         NdisInterlockedDecrement(&pAd->PendingRx);
713 #endif
714                         /* delay 0.5 seconds */
715                         RTMPusecDelay(500000);
716                         pAd->PendingRx = 0;
717                 }
718                 /* peter : why don't we have to get BulkOutLock first ? */
719                 while (((pAd->BulkOutPending[0] == TRUE) ||
720                                 (pAd->BulkOutPending[1] == TRUE) ||
721                                 (pAd->BulkOutPending[2] == TRUE) ||
722                                 (pAd->BulkOutPending[3] == TRUE)) && (pAd->BulkFlags != 0))     //pAd->BulkFlags != 0 wait bulk out finish
723                 {
724                         do
725                         {
726                                 /* pAd->BulkOutPending[y] will be set to FALSE in RTUSBCancelPendingBulkOutIRP(pAd) */
727                                 RTUSBCancelPendingBulkOutIRP(pAd);
728                         } while (FALSE);
729
730                         /* we have enough time delay in RTUSBCancelPendingBulkOutIRP(pAd)
731                         ** so this is not necessary
732                         */
733 //                      RTMPusecDelay(500000);
734                 }
735
736                 /* pAd->PendingRx is not of type atomic_t anymore in 28xx */
737 //              ASSERT(atomic_read(&pAd->PendingRx) == 0);
738                 ASSERT(pAd->PendingRx == 0);
739 /*=========================================================================*/
740
741                 // reset Rx statistics.
742                 pAd->ate.LastSNR0 = 0;
743                 pAd->ate.LastSNR1 = 0;
744                 pAd->ate.LastRssi0 = 0;
745                 pAd->ate.LastRssi1 = 0;
746                 pAd->ate.LastRssi2 = 0;
747                 pAd->ate.AvgRssi0 = 0;
748                 pAd->ate.AvgRssi1 = 0;
749                 pAd->ate.AvgRssi2 = 0;
750                 pAd->ate.AvgRssi0X8 = 0;
751                 pAd->ate.AvgRssi1X8 = 0;
752                 pAd->ate.AvgRssi2X8 = 0;
753                 pAd->ate.NumOfAvgRssiSample = 0;
754
755 #ifdef RALINK_28xx_QA
756                 // Tx frame
757                 pAd->ate.bQATxStart = FALSE;
758                 pAd->ate.bQARxStart = FALSE;
759                 pAd->ate.seq = 0;
760
761                 // counters
762                 pAd->ate.U2M = 0;
763                 pAd->ate.OtherData = 0;
764                 pAd->ate.Beacon = 0;
765                 pAd->ate.OtherCount = 0;
766                 pAd->ate.TxAc0 = 0;
767                 pAd->ate.TxAc1 = 0;
768                 pAd->ate.TxAc2 = 0;
769                 pAd->ate.TxAc3 = 0;
770                 pAd->ate.TxHCCA = 0;
771                 pAd->ate.TxMgmt = 0;
772                 pAd->ate.RSSI0 = 0;
773                 pAd->ate.RSSI1 = 0;
774                 pAd->ate.RSSI2 = 0;
775                 pAd->ate.SNR0 = 0;
776                 pAd->ate.SNR1 = 0;
777
778                 // control
779                 pAd->ate.TxDoneCount = 0;
780                 pAd->ate.TxStatus = 0; // task Tx status // 0 --> task is idle, 1 --> task is running
781 #endif // RALINK_28xx_QA //
782
783                 // Soft reset BBP.
784                 BbpSoftReset(pAd);
785
786
787 #ifdef CONFIG_STA_SUPPORT
788                 AsicDisableSync(pAd);
789
790                 /*
791                 ** If we skip "LinkDown()", we should disable protection
792                 ** to prevent from sending out RTS or CTS-to-self.
793                 */
794                 ATEDisableAsicProtect(pAd);
795                 RTMPStationStop(pAd);
796 #endif // CONFIG_STA_SUPPORT //
797
798                 // Default value in BBP R22 is 0x0.
799                 BbpData = 0;
800                 RTMP_IO_READ32(pAd, MAC_SYS_CTRL, &MacData);
801
802                 // Clean bit4 to stop continuous Tx production test.
803                 MacData &= 0xFFFFFFEF;
804                 ATE_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R22, BbpData);
805                 RTMP_IO_WRITE32(pAd, MAC_SYS_CTRL, MacData);
806                 //Clean ATE Bulk in/out counter and continue setup
807                 InterlockedExchange(&pAd->BulkOutRemained, 0);
808
809                 /* NdisAcquireSpinLock()/NdisReleaseSpinLock() need only one argument in RT28xx */
810                 NdisAcquireSpinLock(&pAd->GenericLock);
811                 pAd->ContinBulkOut = FALSE;
812                 pAd->ContinBulkIn = FALSE;
813                 NdisReleaseSpinLock(&pAd->GenericLock);
814
815                 RTUSB_CLEAR_BULK_FLAG(pAd, fRTUSB_BULK_OUT_DATA_ATE);
816         }
817         else if (!strcmp(arg, "ATESTOP"))
818         {
819                 ATEDBGPRINT(RT_DEBUG_TRACE, ("ATE : ATESTOP ===>\n"));
820
821                 // Default value in BBP R22 is 0x0.
822                 BbpData = 0;
823                 RTMP_IO_READ32(pAd, MAC_SYS_CTRL, &MacData);//0820
824                 // Clean bit4 to stop continuous Tx production test.
825                 MacData &= 0xFFFFFFEF;
826                 ATE_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R22, BbpData);
827                 RTMP_IO_WRITE32(pAd, MAC_SYS_CTRL, MacData); // recover the MAC_SYS_CTRL register back.
828
829                 // Disable Rx
830                 RTMP_IO_READ32(pAd, MAC_SYS_CTRL, &Value);
831                 Value &= ~(1 << 3);
832                 RTMP_IO_WRITE32(pAd, MAC_SYS_CTRL, Value);
833
834                 /*
835                 ** Abort Tx, RX DMA.
836                 ** Q   : How to do the following I/O if Tx, Rx DMA is aborted ?
837                 ** Ans : Bulk endpoints are aborted, while the control endpoint is not.
838                 */
839                 RtmpDmaEnable(pAd, 0);
840
841                 // Disable Tx
842                 RTMP_IO_READ32(pAd, MAC_SYS_CTRL, &Value);
843                 Value &= ~(1 << 2);
844                 RTMP_IO_WRITE32(pAd, MAC_SYS_CTRL, Value);
845
846                 /* Make sure there are no pending bulk in/out IRPs before we go on. */
847 /*=========================================================================*/
848 //              while ((atomic_read(&pAd->PendingRx) > 0))      //pAd->BulkFlags != 0 wait bulk out finish
849                 while (pAd->PendingRx > 0)
850                 {
851 #if 1
852                         ATE_RTUSBCancelPendingBulkInIRP(pAd);
853 #else
854 //                      NdisInterlockedDecrement(&pAd->PendingRx);
855                         pAd->PendingRx--;
856 #endif
857                         RTMPusecDelay(500000);
858                 }
859
860                 while (((pAd->BulkOutPending[0] == TRUE) ||
861                                 (pAd->BulkOutPending[1] == TRUE) ||
862                                 (pAd->BulkOutPending[2] == TRUE) ||
863                                 (pAd->BulkOutPending[3] == TRUE)) && (pAd->BulkFlags != 0))     //pAd->BulkFlags != 0 wait bulk out finish
864                 {
865                         do
866                         {
867                                 RTUSBCancelPendingBulkOutIRP(pAd);
868                         } while (FALSE);
869
870                         RTMPusecDelay(500000);
871                 }
872
873 //              ASSERT(atomic_read(&pAd->PendingRx) == 0);
874                 ASSERT(pAd->PendingRx == 0);
875 /*=========================================================================*/
876 /*      Reset Rx RING                                                      */
877 /*=========================================================================*/
878 //              InterlockedExchange(&pAd->PendingRx, 0);
879                 pAd->PendingRx = 0;
880                 pAd->NextRxBulkInReadIndex = 0; // Next Rx Read index
881                 pAd->NextRxBulkInIndex = RX_RING_SIZE - 1;      // Rx Bulk pointer
882                 pAd->NextRxBulkInPosition = 0;
883                 for (i = 0; i < (RX_RING_SIZE); i++)
884                 {
885                         PRX_CONTEXT  pRxContext = &(pAd->RxContext[i]);
886                         NdisZeroMemory(pRxContext->TransferBuffer, MAX_RXBULK_SIZE);
887                         /* peter : why don't we have to get BulkInLock first ? */
888                         pRxContext->pAd = pAd;
889                         pRxContext->pIrp = NULL;
890             /* peter debug ++ */
891                         pRxContext->BulkInOffset = 0;
892                         pRxContext->bRxHandling = FALSE;
893             /* peter debug -- */
894                         pRxContext->InUse               = FALSE;
895                         pRxContext->IRPPending  = FALSE;
896                         pRxContext->Readable    = FALSE;
897 //                      pRxContext->ReorderInUse = FALSE;
898 //                      pRxContext->ReadPosOffset = 0;
899                 }
900
901 /*=========================================================================*/
902 /*      Reset Tx RING                                                      */
903 /*=========================================================================*/
904                 do
905                 {
906                         RTUSBCancelPendingBulkOutIRP(pAd);
907                 } while (FALSE);
908
909 /*=========================================================================*/
910                 // Enable auto responder.
911                 RTMP_IO_READ32(pAd, AUTO_RSP_CFG, &temp);
912                 temp = temp | (0x01);
913                 RTMP_IO_WRITE32(pAd, AUTO_RSP_CFG, temp);
914
915 /*================================================*/
916                 AsicEnableBssSync(pAd);
917
918                 /* Soft reset BBP.*/
919                 /* In 2870 chipset, ATE_BBP_IO_READ8_BY_REG_ID() == RTMP_BBP_IO_READ8_BY_REG_ID() */
920                 /* Both rt2870ap and rt2870sta use BbpSoftReset(pAd) to do BBP soft reset */
921                 BbpSoftReset(pAd);
922 /*================================================*/
923                 {
924 #ifdef CONFIG_STA_SUPPORT
925                         // Set all state machines back IDLE
926                         pAd->Mlme.CntlMachine.CurrState    = CNTL_IDLE;
927                         pAd->Mlme.AssocMachine.CurrState   = ASSOC_IDLE;
928                         pAd->Mlme.AuthMachine.CurrState    = AUTH_REQ_IDLE;
929                         pAd->Mlme.AuthRspMachine.CurrState = AUTH_RSP_IDLE;
930                         pAd->Mlme.SyncMachine.CurrState    = SYNC_IDLE;
931                         pAd->Mlme.ActMachine.CurrState    = ACT_IDLE;
932 #endif // CONFIG_STA_SUPPORT //
933
934                         //
935                         // ===> refer to MlmeRestartStateMachine().
936                         // When we entered ATE_START mode, PeriodicTimer was not cancelled.
937                         // So we don't have to set it here.
938                         //
939                         //RTMPSetTimer(pAd, &pAd->Mlme.PeriodicTimer, MLME_TASK_EXEC_INTV);
940
941                         ASSERT(pAd->CommonCfg.Channel != 0);
942
943                         AsicSwitchChannel(pAd, pAd->CommonCfg.Channel, FALSE);
944                         AsicLockChannel(pAd, pAd->CommonCfg.Channel);
945
946
947 #ifdef CONFIG_STA_SUPPORT
948                     RTMPStationStart(pAd);
949 #endif // CONFIG_STA_SUPPORT //
950                 }
951 //
952 // These two steps have been done when entering ATE_STOP mode.
953 //
954                 // Clean ATE Bulk in/out counter and continue setup.
955                 InterlockedExchange(&pAd->BulkOutRemained, 0);
956                 NdisAcquireSpinLock(&pAd->GenericLock);
957                 pAd->ContinBulkOut = FALSE;
958                 pAd->ContinBulkIn = FALSE;
959                 NdisReleaseSpinLock(&pAd->GenericLock);
960
961                 /* Wait 50ms to prevent next URB to bulkout during HW reset. */
962                 /* todo : remove this if not necessary */
963                 NdisMSleep(50000);
964
965                 pAd->ate.Mode = ATE_STOP;
966
967                 // Enable Tx
968                 RTMP_IO_READ32(pAd, MAC_SYS_CTRL, &Value);
969                 Value |= (1 << 2);
970                 RTMP_IO_WRITE32(pAd, MAC_SYS_CTRL, Value);
971
972 /*=========================================================================*/
973                 /* restore RX_FILTR_CFG */
974 #ifdef CONFIG_STA_SUPPORT
975                 /* restore RX_FILTR_CFG in order that QA maybe set it to 0x3 */
976                 RTMP_IO_WRITE32(pAd, RX_FILTR_CFG, STANORMAL);
977 #endif // CONFIG_STA_SUPPORT //
978 /*=========================================================================*/
979
980                 // Enable Tx, RX DMA.
981                 RtmpDmaEnable(pAd, 1);
982
983                 // Enable Rx
984                 RTMP_IO_READ32(pAd, MAC_SYS_CTRL, &Value);
985                 Value |= (1 << 3);
986                 RTMP_IO_WRITE32(pAd, MAC_SYS_CTRL, Value);
987
988                 // Wait 10ms to wait all of the bulk-in URBs to complete.
989                 /* todo : remove this if not necessary */
990                 NdisMSleep(10000);
991
992                 // Everything is ready to start normal Tx/Rx.
993                 RTUSBBulkReceive(pAd);
994                 netif_start_queue(pAd->net_dev);
995
996                 ATEDBGPRINT(RT_DEBUG_TRACE, ("<=== ATE : ATESTOP \n"));
997         }
998         else if (!strcmp(arg, "TXCARR"))        // Tx Carrier
999         {
1000                 ATEDBGPRINT(RT_DEBUG_TRACE, ("ATE: TXCARR\n"));
1001                 pAd->ate.Mode |= ATE_TXCARR;
1002
1003 #ifdef RT30xx
1004                         for(i=0;i<ATE_BBP_REG_NUM;i++)
1005                                 restore_BBP[i]=0;
1006                         //Record All BBP Value
1007                         for(i=0;i<ATE_BBP_REG_NUM;i++)
1008                         ATE_BBP_IO_READ8_BY_REG_ID(pAd,i,&restore_BBP[i]);
1009 #endif // RT30xx //
1010
1011                 // Disable Rx
1012                 // May be we need not to do this, because these have been done in ATE_START mode ???
1013                 RTMP_IO_READ32(pAd, MAC_SYS_CTRL, &Value);
1014                 Value &= ~(1 << 3);
1015                 RTMP_IO_WRITE32(pAd, MAC_SYS_CTRL, Value);
1016
1017                 // QA has done the following steps if it is used.
1018                 if (pAd->ate.bQATxStart == FALSE)
1019                 {
1020                         // Soft reset BBP.
1021                         BbpSoftReset(pAd);
1022
1023                         // Carrier Test set BBP R22 bit7=1, bit6=1, bit[5~0]=0x01
1024                         ATE_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R22, &BbpData);
1025                         BbpData &= 0xFFFFFF00; //clear bit7, bit6, bit[5~0]
1026                         BbpData |= 0x000000C1; //set bit7=1, bit6=1, bit[5~0]=0x01
1027                         ATE_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R22, BbpData);
1028
1029                         // set MAC_SYS_CTRL(0x1004) Continuous Tx Production Test (bit4) = 1
1030                         RTMP_IO_READ32(pAd, MAC_SYS_CTRL, &Value);
1031                         Value = Value | 0x00000010;
1032                         RTMP_IO_WRITE32(pAd, MAC_SYS_CTRL, Value);
1033                 }
1034         }
1035         else if (!strcmp(arg, "TXCONT"))        // Tx Continue
1036         {
1037                 if (pAd->ate.bQATxStart == TRUE)
1038                 {
1039                         /* set MAC_SYS_CTRL(0x1004) bit4(Continuous Tx Production Test)
1040                            and bit2(MAC TX enable) back to zero. */
1041                         RTMP_IO_READ32(pAd, MAC_SYS_CTRL, &MacData);
1042                         MacData &= 0xFFFFFFEB;
1043                         RTMP_IO_WRITE32(pAd, MAC_SYS_CTRL, MacData);
1044
1045                         // set BBP R22 bit7=0
1046                         ATE_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R22, &BbpData);
1047                         BbpData &= 0xFFFFFF7F; //set bit7=0
1048                         ATE_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R22, BbpData);
1049                 }
1050
1051                 /* for TxCont mode.
1052                 ** Step 1: Send 50 packets first then wait for a moment.
1053                 ** Step 2: Send more 50 packet then start continue mode.
1054                 */
1055                 ATEDBGPRINT(RT_DEBUG_TRACE, ("ATE: TXCONT\n"));
1056
1057 #ifdef RT30xx
1058                                 for(i=0;i<ATE_BBP_REG_NUM;i++)
1059                                 restore_BBP[i]=0;
1060                         //Record All BBP Value
1061                         for(i=0;i<ATE_BBP_REG_NUM;i++)
1062                         ATE_BBP_IO_READ8_BY_REG_ID(pAd,i,&restore_BBP[i]);
1063 #endif // RT30xx //
1064
1065                 // Step 1: send 50 packets first.
1066                 pAd->ate.Mode |= ATE_TXCONT;
1067                 pAd->ate.TxCount = 50;
1068                 pAd->ate.TxDoneCount = 0;
1069
1070                 // Soft reset BBP.
1071                 BbpSoftReset(pAd);
1072
1073                 // Abort Tx, RX DMA.
1074                 RtmpDmaEnable(pAd, 0);
1075
1076
1077                 /* Only needed if we have to send some normal frames. */
1078                 SetJapanFilter(pAd);
1079
1080                 // Setup frame format.
1081                 ATESetUpFrame(pAd, 0);
1082
1083                 // Enable Tx
1084                 RTMP_IO_READ32(pAd, MAC_SYS_CTRL, &Value);
1085                 Value |= (1 << 2);
1086                 RTMP_IO_WRITE32(pAd, MAC_SYS_CTRL, Value);
1087
1088                 // Disable Rx
1089                 RTMP_IO_READ32(pAd, MAC_SYS_CTRL, &Value);
1090                 Value &= ~(1 << 3);
1091                 RTMP_IO_WRITE32(pAd, MAC_SYS_CTRL, Value);
1092
1093                 // Start Tx, RX DMA.
1094                 RtmpDmaEnable(pAd, 1);
1095
1096                 InterlockedExchange(&pAd->BulkOutRemained, pAd->ate.TxCount);
1097
1098 #ifdef  RALINK_28xx_QA
1099                 if (pAd->ate.bQATxStart == TRUE)
1100                 {
1101                         pAd->ate.TxStatus = 1;
1102                         //pAd->ate.Repeat = 0;
1103                 }
1104 #endif  // RALINK_28xx_QA //
1105
1106                 NdisAcquireSpinLock(&pAd->GenericLock);//0820
1107                 pAd->ContinBulkOut = FALSE;
1108                 NdisReleaseSpinLock(&pAd->GenericLock);
1109
1110                 RTUSB_SET_BULK_FLAG(pAd, fRTUSB_BULK_OUT_DATA_ATE);
1111
1112                 // Kick bulk out
1113                 RTUSBKickBulkOut(pAd);
1114
1115                 /* To make sure all the 50 frames have been bulk out before executing step 2 */
1116                 while (atomic_read(&pAd->BulkOutRemained) > 0)
1117                 {
1118                         RTMPusecDelay(5000);
1119                 }
1120
1121                 // Step 2: send more 50 packets then start continue mode.
1122                 // Abort Tx, RX DMA.
1123                 RtmpDmaEnable(pAd, 0);
1124
1125                 // Cont. TX set BBP R22 bit7=1
1126                 ATE_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R22, &BbpData);
1127                 BbpData |= 0x00000080; //set bit7=1
1128                 ATE_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R22, BbpData);
1129
1130                 pAd->ate.TxCount = 50;
1131                 pAd->ate.TxDoneCount = 0;
1132
1133                 SetJapanFilter(pAd);
1134
1135                 // Setup frame format.
1136                 ATESetUpFrame(pAd, 0);
1137
1138                 // Enable Tx
1139                 RTMP_IO_READ32(pAd, MAC_SYS_CTRL, &Value);
1140                 Value |= (1 << 2);
1141                 RTMP_IO_WRITE32(pAd, MAC_SYS_CTRL, Value);
1142
1143                 // Disable Rx
1144                 RTMP_IO_READ32(pAd, MAC_SYS_CTRL, &Value);
1145                 Value &= ~(1 << 3);
1146
1147                 // Start Tx, RX DMA.
1148                 RtmpDmaEnable(pAd, 1);
1149
1150                 InterlockedExchange(&pAd->BulkOutRemained, pAd->ate.TxCount);
1151
1152 #ifdef  RALINK_28xx_QA
1153                 if (pAd->ate.bQATxStart == TRUE)
1154                 {
1155                         pAd->ate.TxStatus = 1;
1156                         //pAd->ate.Repeat = 0;
1157                 }
1158 #endif  // RALINK_28xx_QA //
1159
1160                 NdisAcquireSpinLock(&pAd->GenericLock);//0820
1161                 pAd->ContinBulkOut = FALSE;
1162                 NdisReleaseSpinLock(&pAd->GenericLock);
1163
1164                 RTUSB_SET_BULK_FLAG(pAd, fRTUSB_BULK_OUT_DATA_ATE);
1165                 // Kick bulk out
1166                 RTUSBKickBulkOut(pAd);
1167
1168 #if 1
1169                 RTMPusecDelay(500);
1170 #else
1171                 while (atomic_read(&pAd->BulkOutRemained) > 0)
1172                 {
1173                         RTMPusecDelay(5000);
1174                 }
1175 #endif // 1 //
1176
1177                 // Set MAC_SYS_CTRL(0x1004) Continuous Tx Production Test (bit4) = 1.
1178                 RTMP_IO_READ32(pAd, MAC_SYS_CTRL, &MacData);
1179                 MacData |= 0x00000010;
1180                 RTMP_IO_WRITE32(pAd, MAC_SYS_CTRL, MacData);
1181         }
1182         else if (!strcmp(arg, "TXFRAME"))       // Tx Frames
1183         {
1184                 ATEDBGPRINT(RT_DEBUG_TRACE, ("ATE: TXFRAME(Count=0x%08x)\n", pAd->ate.TxCount));
1185                 pAd->ate.Mode |= ATE_TXFRAME;
1186
1187                 // Soft reset BBP.
1188                 BbpSoftReset(pAd);
1189
1190                 // Default value in BBP R22 is 0x0.
1191                 BbpData = 0;
1192
1193                 RTMP_IO_READ32(pAd, MAC_SYS_CTRL, &MacData);
1194
1195                 // Clean bit4 to stop continuous Tx production test.
1196                 MacData &= 0xFFFFFFEF;
1197
1198                 ATE_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R22, BbpData);
1199                 RTMP_IO_WRITE32(pAd, MAC_SYS_CTRL, MacData);
1200
1201 #ifdef RALINK_28xx_QA
1202                 // add this for LoopBack mode
1203                 if (pAd->ate.bQARxStart == FALSE)
1204                 {
1205                         // Disable Rx
1206                         RTMP_IO_READ32(pAd, MAC_SYS_CTRL, &Value);
1207                         Value &= ~(1 << 3);
1208                         RTMP_IO_WRITE32(pAd, MAC_SYS_CTRL, Value);
1209                 }
1210
1211                 if (pAd->ate.bQATxStart == TRUE)
1212                 {
1213                         pAd->ate.TxStatus = 1;
1214                         //pAd->ate.Repeat = 0;
1215                 }
1216 #else
1217                 // Disable Rx
1218                 RTMP_IO_READ32(pAd, MAC_SYS_CTRL, &Value);
1219                 Value &= ~(1 << 3);
1220                 RTMP_IO_WRITE32(pAd, MAC_SYS_CTRL, Value);
1221 #endif // RALINK_28xx_QA //
1222
1223                 // Enable Tx
1224                 RTMP_IO_READ32(pAd, MAC_SYS_CTRL, &Value);
1225                 Value |= (1 << 2);
1226                 RTMP_IO_WRITE32(pAd, MAC_SYS_CTRL, Value);
1227
1228         SetJapanFilter(pAd);
1229
1230                 // Abort Tx, RX DMA.
1231                 RtmpDmaEnable(pAd, 0);
1232
1233                 pAd->ate.TxDoneCount = 0;
1234
1235         // Setup frame format
1236                 ATESetUpFrame(pAd, 0);
1237
1238                 // Start Tx, RX DMA.
1239                 RtmpDmaEnable(pAd, 1);
1240
1241                 // Check count is continuous or not yet.
1242                 //
1243                 // Due to the type mismatch between "pAd->BulkOutRemained"(atomic_t) and "pAd->ate.TxCount"(UINT32)
1244                 //
1245                 if (pAd->ate.TxCount == 0)
1246                 {
1247                         InterlockedExchange(&pAd->BulkOutRemained, 0);
1248                 }
1249                 else
1250                 {
1251                         InterlockedExchange(&pAd->BulkOutRemained, pAd->ate.TxCount);
1252                 }
1253                 ATEDBGPRINT(RT_DEBUG_TRACE, ("bulk out count = %d\n", atomic_read(&pAd->BulkOutRemained)));
1254                 ASSERT((atomic_read(&pAd->BulkOutRemained) >= 0));
1255
1256                 if (atomic_read(&pAd->BulkOutRemained) == 0)
1257                 {
1258                         ATEDBGPRINT(RT_DEBUG_TRACE, ("Send packet countinuously\n"));
1259
1260                         /* In 28xx, NdisAcquireSpinLock() == spin_lock_bh() */
1261                         /* NdisAcquireSpinLock only need one argument in 28xx. */
1262                         NdisAcquireSpinLock(&pAd->GenericLock);
1263                         pAd->ContinBulkOut = TRUE;
1264                         NdisReleaseSpinLock(&pAd->GenericLock);
1265
1266                         /* In 28xx, BULK_OUT_LOCK() == spin_lock_irqsave() */
1267                         BULK_OUT_LOCK(&pAd->BulkOutLock[0], IrqFlags);// peter : NdisAcquireSpinLock ==> BULK_OUT_LOCK
1268                         pAd->BulkOutPending[0] = FALSE;
1269                         BULK_OUT_UNLOCK(&pAd->BulkOutLock[0], IrqFlags);// peter : NdisAcquireSpinLock ==> BULK_OUT_LOCK
1270                 }
1271                 else
1272                 {
1273                         ATEDBGPRINT(RT_DEBUG_TRACE, ("Send packets depend on counter\n"));
1274
1275                         NdisAcquireSpinLock(&pAd->GenericLock);
1276                         pAd->ContinBulkOut = FALSE;
1277                         NdisReleaseSpinLock(&pAd->GenericLock);
1278
1279                         BULK_OUT_LOCK(&pAd->BulkOutLock[0], IrqFlags);
1280                         pAd->BulkOutPending[0] = FALSE;
1281                         BULK_OUT_UNLOCK(&pAd->BulkOutLock[0], IrqFlags);
1282                 }
1283
1284                 RTUSB_SET_BULK_FLAG(pAd, fRTUSB_BULK_OUT_DATA_ATE);
1285
1286                 // Kick bulk out
1287                 RTUSBKickBulkOut(pAd);
1288         }
1289 #ifdef RALINK_28xx_QA
1290         else if (!strcmp(arg, "TXSTOP"))                //Enter ATE mode and set Tx/Rx Idle
1291         {
1292                 ATEDBGPRINT(RT_DEBUG_TRACE, ("ATE: TXSTOP\n"));
1293
1294                 atemode = pAd->ate.Mode;
1295                 pAd->ate.Mode &= ATE_TXSTOP;
1296                 pAd->ate.bQATxStart = FALSE;
1297 //              pAd->ate.TxDoneCount = pAd->ate.TxCount;
1298
1299 /*=========================================================================*/
1300                 if (atemode & ATE_TXCARR)
1301                 {
1302                         // No Carrier Test set BBP R22 bit7=0, bit6=0, bit[5~0]=0x0
1303                         ATE_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R22, &BbpData);
1304                         BbpData &= 0xFFFFFF00; //clear bit7, bit6, bit[5~0]
1305                     ATE_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R22, BbpData);
1306                 }
1307                 else if (atemode & ATE_TXCARRSUPP)
1308                 {
1309                         // No Cont. TX set BBP R22 bit7=0
1310                         ATE_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R22, &BbpData);
1311                         BbpData &= ~(1 << 7); //set bit7=0
1312                         ATE_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R22, BbpData);
1313
1314                         // No Carrier Suppression set BBP R24 bit0=0
1315                         ATE_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R24, &BbpData);
1316                         BbpData &= 0xFFFFFFFE; //clear bit0
1317                     ATE_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R24, BbpData);
1318                 }
1319                 else if ((atemode & ATE_TXFRAME) || (atemode == ATE_STOP))
1320                 {
1321                         if (atemode & ATE_TXCONT)
1322                         {
1323                                 // No Cont. TX set BBP R22 bit7=0
1324                                 ATE_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R22, &BbpData);
1325                                 BbpData &= ~(1 << 7); //set bit7=0
1326                                 ATE_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R22, BbpData);
1327                         }
1328                 }
1329
1330 /*=========================================================================*/
1331                 RTUSBRejectPendingPackets(pAd);
1332                 RTUSBCleanUpDataBulkOutQueue(pAd);
1333
1334                 /* not used in RT28xx */
1335                 //RTUSBCleanUpMLMEWaitQueue(pAd);
1336                 /* empty function so far */
1337                 RTUSBCleanUpMLMEBulkOutQueue(pAd);
1338 /*=========================================================================*/
1339                 // Abort Tx, RX DMA.
1340                 RtmpDmaEnable(pAd, 0);
1341 /*=========================================================================*/
1342
1343                 /* In 28xx, pAd->PendingRx is not of type atomic_t anymore */
1344 //              while ((atomic_read(&pAd->PendingRx) > 0))      //pAd->BulkFlags != 0 wait bulk out finish
1345                 /* peter todo : BulkInLock */
1346                 while (pAd->PendingRx > 0)
1347                 {
1348 #if 1
1349                         ATE_RTUSBCancelPendingBulkInIRP(pAd);
1350 #else
1351 //                      NdisInterlockedDecrement(&pAd->PendingRx);
1352                         pAd->PendingRx--;
1353 #endif
1354                         RTMPusecDelay(500000);
1355                 }
1356
1357                 while (((pAd->BulkOutPending[0] == TRUE) ||
1358                                 (pAd->BulkOutPending[1] == TRUE) ||
1359                                 (pAd->BulkOutPending[2] == TRUE) ||
1360                                 (pAd->BulkOutPending[3] == TRUE)) && (pAd->BulkFlags != 0))     //pAd->BulkFlags != 0 wait bulk out finish
1361                 {
1362                         do
1363                         {
1364                                 RTUSBCancelPendingBulkOutIRP(pAd);
1365                         } while (FALSE);
1366
1367                         RTMPusecDelay(500000);
1368                 }
1369
1370                 ASSERT(pAd->PendingRx == 0);
1371 /*=========================================================================*/
1372                 // Enable Tx, Rx DMA.
1373                 RtmpDmaEnable(pAd, 1);
1374
1375                 /* task Tx status : 0 --> task is idle, 1 --> task is running */
1376                 pAd->ate.TxStatus = 0;
1377
1378                 // Soft reset BBP.
1379                 BbpSoftReset(pAd);
1380
1381                 // Disable Tx
1382                 RTMP_IO_READ32(pAd, MAC_SYS_CTRL, &MacData);
1383                 MacData &= (0xfffffffb);
1384                 RTMP_IO_WRITE32(pAd, MAC_SYS_CTRL, MacData);
1385
1386                 //Clean ATE Bulk in/out counter and continue setup
1387                 InterlockedExchange(&pAd->BulkOutRemained, 0);
1388
1389                 pAd->ContinBulkOut = FALSE;
1390         }
1391         else if (!strcmp(arg, "RXSTOP"))
1392         {
1393                 ATEDBGPRINT(RT_DEBUG_TRACE, ("ATE: RXSTOP\n"));
1394                 atemode = pAd->ate.Mode;
1395
1396                 // Disable Rx
1397                 RTMP_IO_READ32(pAd, MAC_SYS_CTRL, &Value);
1398                 Value &= ~(1 << 3);
1399                 RTMP_IO_WRITE32(pAd, MAC_SYS_CTRL, Value);
1400
1401                 pAd->ate.Mode &= ATE_RXSTOP;
1402                 pAd->ate.bQARxStart = FALSE;
1403 //              pAd->ate.TxDoneCount = pAd->ate.TxCount;
1404
1405 /*=========================================================================*/
1406                 RTUSBRejectPendingPackets(pAd);
1407                 RTUSBCleanUpDataBulkOutQueue(pAd);
1408
1409                 /* not used in RT28xx */
1410                 //RTUSBCleanUpMLMEWaitQueue(pAd);
1411                 RTUSBCleanUpMLMEBulkOutQueue(pAd);
1412 /*=========================================================================*/
1413
1414                 // Abort Tx, RX DMA.
1415                 RtmpDmaEnable(pAd, 0);
1416 /*=========================================================================*/
1417 //              while ((atomic_read(&pAd->PendingRx) > 0))
1418                 while (pAd->PendingRx > 0)
1419                 {
1420 #if 1
1421                         ATE_RTUSBCancelPendingBulkInIRP(pAd);
1422 #else
1423 //                      NdisInterlockedDecrement(&pAd->PendingRx);
1424                         pAd->PendingRx--;
1425 #endif
1426                         RTMPusecDelay(500000);
1427                 }
1428
1429                 while (((pAd->BulkOutPending[0] == TRUE) ||
1430                                 (pAd->BulkOutPending[1] == TRUE) ||
1431                                 (pAd->BulkOutPending[2] == TRUE) ||
1432                                 (pAd->BulkOutPending[3] == TRUE)) && (pAd->BulkFlags != 0))     //pAd->BulkFlags != 0 wait bulk out finish
1433                 {
1434                         do
1435                         {
1436                                 RTUSBCancelPendingBulkOutIRP(pAd);
1437                         } while (FALSE);
1438
1439                         RTMPusecDelay(500000);
1440                 }
1441
1442                 ASSERT(pAd->PendingRx == 0);
1443 /*=========================================================================*/
1444
1445                 // Soft reset BBP.
1446                 BbpSoftReset(pAd);
1447                 pAd->ContinBulkIn = FALSE;
1448         }
1449 #endif // RALINK_28xx_QA //
1450         else if (!strcmp(arg, "RXFRAME")) // Rx Frames
1451         {
1452                 ATEDBGPRINT(RT_DEBUG_TRACE, ("ATE: RXFRAME\n"));
1453
1454                 // Disable Rx of MAC block
1455                 RTMP_IO_READ32(pAd, MAC_SYS_CTRL, &Value);
1456                 Value &= ~(1 << 3);
1457                 RTMP_IO_WRITE32(pAd, MAC_SYS_CTRL, Value);
1458
1459                 // Default value in BBP R22 is 0x0.
1460                 BbpData = 0;
1461
1462                 RTMP_IO_READ32(pAd, MAC_SYS_CTRL, &MacData);
1463                 // Clean bit4 to stop continuous Tx production test.
1464                 MacData &= 0xFFFFFFEF;
1465
1466                 ATE_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R22, BbpData);
1467                 RTMP_IO_WRITE32(pAd, MAC_SYS_CTRL, MacData);
1468
1469                 pAd->ate.Mode |= ATE_RXFRAME;
1470
1471                 // Abort Tx, RX DMA.
1472                 RtmpDmaEnable(pAd, 0);
1473
1474                 // Disable TX of MAC block
1475                 RTMP_IO_READ32(pAd, MAC_SYS_CTRL, &Value);
1476                 Value &= ~(1 << 2);
1477                 RTMP_IO_WRITE32(pAd, MAC_SYS_CTRL, Value);
1478
1479         // Reset Rx RING.
1480                 for ( i = 0; i < (RX_RING_SIZE); i++)
1481                 {
1482                         PRX_CONTEXT  pRxContext = &(pAd->RxContext[i]);
1483
1484                         pRxContext->InUse = FALSE;
1485                         pRxContext->IRPPending = FALSE;
1486                         pRxContext->Readable = FALSE;
1487
1488                         //
1489                         // Get the urb from kernel back to driver.
1490                         //
1491                         RTUSB_UNLINK_URB(pRxContext->pUrb);
1492
1493                         /* Sleep 200 microsecs to give cancellation time to work. */
1494                         NdisMSleep(200);
1495                         pAd->BulkInReq = 0;
1496
1497 //                      InterlockedExchange(&pAd->PendingRx, 0);
1498                         pAd->PendingRx = 0;
1499                         pAd->NextRxBulkInReadIndex = 0; // Next Rx Read index
1500                         pAd->NextRxBulkInIndex          = RX_RING_SIZE - 1;     // Rx Bulk pointer
1501                         pAd->NextRxBulkInPosition = 0;
1502                 }
1503
1504                 // read to clear counters
1505                 RTUSBReadMACRegister(pAd, RX_STA_CNT0, &temp); //RX PHY & RX CRC count
1506                 RTUSBReadMACRegister(pAd, RX_STA_CNT1, &temp); //RX PLCP error count & CCA false alarm count
1507                 RTUSBReadMACRegister(pAd, RX_STA_CNT2, &temp); //RX FIFO overflow frame count & RX duplicated filtered frame count
1508
1509                 pAd->ContinBulkIn = TRUE;
1510
1511                 // Enable Tx, RX DMA.
1512                 RtmpDmaEnable(pAd, 1);
1513
1514                 // Enable RX of MAC block
1515                 RTMP_IO_READ32(pAd, MAC_SYS_CTRL, &Value);
1516                 Value |= (1 << 3);
1517                 RTMP_IO_WRITE32(pAd, MAC_SYS_CTRL, Value);
1518
1519                 // Kick bulk in
1520                 RTUSBBulkReceive(pAd);
1521         }
1522         else
1523         {
1524                 ATEDBGPRINT(RT_DEBUG_TRACE, ("ATE: Invalid arg!\n"));
1525                 return FALSE;
1526         }
1527         RTMPusecDelay(5000);
1528
1529         ATEDBGPRINT(RT_DEBUG_TRACE, ("<=== ATECmdHandler()\n"));
1530
1531         return TRUE;
1532 }
1533 #endif // RT2870 //
1534
1535 INT     Set_ATE_Proc(
1536         IN      PRTMP_ADAPTER   pAd,
1537         IN      PUCHAR                  arg)
1538 {
1539         if (ATECmdHandler(pAd, arg))
1540         {
1541                 ATEDBGPRINT(RT_DEBUG_TRACE, ("Ralink: Set_ATE_Proc Success\n"));
1542
1543
1544                 return TRUE;
1545         }
1546         else
1547         {
1548                 ATEDBGPRINT(RT_DEBUG_TRACE, ("Ralink: Set_ATE_Proc Failed\n"));
1549                 return FALSE;
1550         }
1551 }
1552
1553 /*
1554     ==========================================================================
1555     Description:
1556         Set ATE ADDR1=DA for TxFrame(AP  : To DS = 0 ; From DS = 1)
1557         or
1558         Set ATE ADDR3=DA for TxFrame(STA : To DS = 1 ; From DS = 0)
1559
1560     Return:
1561         TRUE if all parameters are OK, FALSE otherwise
1562     ==========================================================================
1563 */
1564 INT     Set_ATE_DA_Proc(
1565         IN      PRTMP_ADAPTER   pAd,
1566         IN      PUCHAR                  arg)
1567 {
1568         CHAR                            *value;
1569         INT                                     i;
1570
1571         if(strlen(arg) != 17)  //Mac address acceptable format 01:02:03:04:05:06 length 17
1572                 return FALSE;
1573
1574     for (i=0, value = rstrtok(arg, ":"); value; value = rstrtok(NULL, ":"))
1575         {
1576                 if((strlen(value) != 2) || (!isxdigit(*value)) || (!isxdigit(*(value+1))) )
1577                         return FALSE;  //Invalid
1578
1579
1580 #ifdef CONFIG_STA_SUPPORT
1581                 AtoH(value, &pAd->ate.Addr3[i++], 1);
1582 #endif // CONFIG_STA_SUPPORT //
1583         }
1584
1585         if(i != 6)
1586                 return FALSE;  //Invalid
1587
1588
1589 #ifdef CONFIG_STA_SUPPORT
1590         ATEDBGPRINT(RT_DEBUG_TRACE, ("Set_ATE_DA_Proc (DA = %2X:%2X:%2X:%2X:%2X:%2X)\n", pAd->ate.Addr3[0],
1591                 pAd->ate.Addr3[1], pAd->ate.Addr3[2], pAd->ate.Addr3[3], pAd->ate.Addr3[4], pAd->ate.Addr3[5]));
1592 #endif // CONFIG_STA_SUPPORT //
1593
1594         ATEDBGPRINT(RT_DEBUG_TRACE, ("Ralink: Set_ATE_DA_Proc Success\n"));
1595
1596         return TRUE;
1597 }
1598
1599 /*
1600     ==========================================================================
1601     Description:
1602         Set ATE ADDR3=SA for TxFrame(AP  : To DS = 0 ; From DS = 1)
1603         or
1604         Set ATE ADDR2=SA for TxFrame(STA : To DS = 1 ; From DS = 0)
1605
1606     Return:
1607         TRUE if all parameters are OK, FALSE otherwise
1608     ==========================================================================
1609 */
1610 INT     Set_ATE_SA_Proc(
1611         IN      PRTMP_ADAPTER   pAd,
1612         IN      PUCHAR                  arg)
1613 {
1614         CHAR                            *value;
1615         INT                                     i;
1616
1617         if(strlen(arg) != 17)  //Mac address acceptable format 01:02:03:04:05:06 length 17
1618                 return FALSE;
1619
1620     for (i=0, value = rstrtok(arg, ":"); value; value = rstrtok(NULL, ":"))
1621         {
1622                 if((strlen(value) != 2) || (!isxdigit(*value)) || (!isxdigit(*(value+1))) )
1623                         return FALSE;  //Invalid
1624
1625
1626 #ifdef CONFIG_STA_SUPPORT
1627                 AtoH(value, &pAd->ate.Addr2[i++], 1);
1628 #endif // CONFIG_STA_SUPPORT //
1629         }
1630
1631         if(i != 6)
1632                 return FALSE;  //Invalid
1633
1634
1635 #ifdef CONFIG_STA_SUPPORT
1636         ATEDBGPRINT(RT_DEBUG_TRACE, ("Set_ATE_SA_Proc (SA = %2X:%2X:%2X:%2X:%2X:%2X)\n", pAd->ate.Addr2[0],
1637                 pAd->ate.Addr2[1], pAd->ate.Addr2[2], pAd->ate.Addr2[3], pAd->ate.Addr2[4], pAd->ate.Addr2[5]));
1638 #endif // CONFIG_STA_SUPPORT //
1639
1640         ATEDBGPRINT(RT_DEBUG_TRACE, ("Ralink: Set_ATE_SA_Proc Success\n"));
1641
1642         return TRUE;
1643 }
1644
1645 /*
1646     ==========================================================================
1647     Description:
1648         Set ATE ADDR2=BSSID for TxFrame(AP  : To DS = 0 ; From DS = 1)
1649         or
1650         Set ATE ADDR1=BSSID for TxFrame(STA : To DS = 1 ; From DS = 0)
1651
1652     Return:
1653         TRUE if all parameters are OK, FALSE otherwise
1654     ==========================================================================
1655 */
1656 INT     Set_ATE_BSSID_Proc(
1657         IN      PRTMP_ADAPTER   pAd,
1658         IN      PUCHAR                  arg)
1659 {
1660         CHAR                            *value;
1661         INT                                     i;
1662
1663         if(strlen(arg) != 17)  //Mac address acceptable format 01:02:03:04:05:06 length 17
1664                 return FALSE;
1665
1666     for (i=0, value = rstrtok(arg, ":"); value; value = rstrtok(NULL, ":"))
1667         {
1668                 if((strlen(value) != 2) || (!isxdigit(*value)) || (!isxdigit(*(value+1))) )
1669                         return FALSE;  //Invalid
1670
1671
1672 #ifdef CONFIG_STA_SUPPORT
1673                 AtoH(value, &pAd->ate.Addr1[i++], 1);
1674 #endif // CONFIG_STA_SUPPORT //
1675         }
1676
1677         if(i != 6)
1678                 return FALSE;  //Invalid
1679
1680
1681 #ifdef CONFIG_STA_SUPPORT
1682         ATEDBGPRINT(RT_DEBUG_TRACE, ("Set_ATE_BSSID_Proc (BSSID = %2X:%2X:%2X:%2X:%2X:%2X)\n",  pAd->ate.Addr1[0],
1683                 pAd->ate.Addr1[1], pAd->ate.Addr1[2], pAd->ate.Addr1[3], pAd->ate.Addr1[4], pAd->ate.Addr1[5]));
1684 #endif // CONFIG_STA_SUPPORT //
1685
1686         ATEDBGPRINT(RT_DEBUG_TRACE, ("Ralink: Set_ATE_BSSID_Proc Success\n"));
1687
1688         return TRUE;
1689 }
1690
1691 /*
1692     ==========================================================================
1693     Description:
1694         Set ATE Tx Channel
1695
1696     Return:
1697         TRUE if all parameters are OK, FALSE otherwise
1698     ==========================================================================
1699 */
1700 INT     Set_ATE_CHANNEL_Proc(
1701         IN      PRTMP_ADAPTER   pAd,
1702         IN      PUCHAR                  arg)
1703 {
1704         UCHAR channel;
1705
1706         channel = simple_strtol(arg, 0, 10);
1707
1708         if ((channel < 1) || (channel > 216))// to allow A band channel : ((channel < 1) || (channel > 14))
1709         {
1710                 ATEDBGPRINT(RT_DEBUG_ERROR, ("Set_ATE_CHANNEL_Proc::Out of range, it should be in range of 1~14.\n"));
1711                 return FALSE;
1712         }
1713         pAd->ate.Channel = channel;
1714
1715         ATEDBGPRINT(RT_DEBUG_TRACE, ("Set_ATE_CHANNEL_Proc (ATE Channel = %d)\n", pAd->ate.Channel));
1716         ATEDBGPRINT(RT_DEBUG_TRACE, ("Ralink: Set_ATE_CHANNEL_Proc Success\n"));
1717
1718
1719         return TRUE;
1720 }
1721
1722 /*
1723     ==========================================================================
1724     Description:
1725         Set ATE Tx Power0
1726
1727     Return:
1728         TRUE if all parameters are OK, FALSE otherwise
1729     ==========================================================================
1730 */
1731 INT     Set_ATE_TX_POWER0_Proc(
1732         IN      PRTMP_ADAPTER   pAd,
1733         IN      PUCHAR                  arg)
1734 {
1735         CHAR TxPower;
1736
1737         TxPower = simple_strtol(arg, 0, 10);
1738
1739         if (pAd->ate.Channel <= 14)
1740         {
1741                 if ((TxPower > 31) || (TxPower < 0))
1742                 {
1743                         ATEDBGPRINT(RT_DEBUG_ERROR, ("Set_ATE_TX_POWER0_Proc::Out of range (Value=%d)\n", TxPower));
1744                         return FALSE;
1745                 }
1746         }
1747         else// 5.5GHz
1748         {
1749                 if ((TxPower > 15) || (TxPower < -7))
1750                 {
1751                         ATEDBGPRINT(RT_DEBUG_ERROR, ("Set_ATE_TX_POWER0_Proc::Out of range (Value=%d)\n", TxPower));
1752                         return FALSE;
1753                 }
1754         }
1755
1756         pAd->ate.TxPower0 = TxPower;
1757         ATETxPwrHandler(pAd, 0);
1758         ATEDBGPRINT(RT_DEBUG_TRACE, ("Ralink: Set_ATE_TX_POWER0_Proc Success\n"));
1759
1760
1761         return TRUE;
1762 }
1763
1764 /*
1765     ==========================================================================
1766     Description:
1767         Set ATE Tx Power1
1768
1769     Return:
1770         TRUE if all parameters are OK, FALSE otherwise
1771     ==========================================================================
1772 */
1773 INT     Set_ATE_TX_POWER1_Proc(
1774         IN      PRTMP_ADAPTER   pAd,
1775         IN      PUCHAR                  arg)
1776 {
1777         CHAR TxPower;
1778
1779         TxPower = simple_strtol(arg, 0, 10);
1780
1781         if (pAd->ate.Channel <= 14)
1782         {
1783         if ((TxPower > 31) || (TxPower < 0))
1784         {
1785                 ATEDBGPRINT(RT_DEBUG_ERROR, ("Set_ATE_TX_POWER1_Proc::Out of range (Value=%d)\n", TxPower));
1786                 return FALSE;
1787         }
1788         }
1789         else
1790         {
1791                 if ((TxPower > 15) || (TxPower < -7))
1792                 {
1793                         ATEDBGPRINT(RT_DEBUG_ERROR, ("Set_ATE_TX_POWER1_Proc::Out of range (Value=%d)\n", TxPower));
1794                         return FALSE;
1795                 }
1796         }
1797
1798         pAd->ate.TxPower1 = TxPower;
1799         ATETxPwrHandler(pAd, 1);
1800         ATEDBGPRINT(RT_DEBUG_TRACE, ("Ralink: Set_ATE_TX_POWER1_Proc Success\n"));
1801
1802
1803         return TRUE;
1804 }
1805
1806 /*
1807     ==========================================================================
1808     Description:
1809         Set ATE Tx Antenna
1810
1811     Return:
1812         TRUE if all parameters are OK, FALSE otherwise
1813     ==========================================================================
1814 */
1815 INT     Set_ATE_TX_Antenna_Proc(
1816         IN      PRTMP_ADAPTER   pAd,
1817         IN      PUCHAR                  arg)
1818 {
1819         CHAR value;
1820
1821         value = simple_strtol(arg, 0, 10);
1822
1823         if ((value > 2) || (value < 0))
1824         {
1825                 ATEDBGPRINT(RT_DEBUG_ERROR, ("Set_ATE_TX_Antenna_Proc::Out of range (Value=%d)\n", value));
1826                 return FALSE;
1827         }
1828
1829         pAd->ate.TxAntennaSel = value;
1830
1831         ATEDBGPRINT(RT_DEBUG_TRACE, ("Set_ATE_TX_Antenna_Proc (Antenna = %d)\n", pAd->ate.TxAntennaSel));
1832         ATEDBGPRINT(RT_DEBUG_TRACE,("Ralink: Set_ATE_TX_Antenna_Proc Success\n"));
1833
1834
1835         return TRUE;
1836 }
1837
1838 /*
1839     ==========================================================================
1840     Description:
1841         Set ATE Rx Antenna
1842
1843     Return:
1844         TRUE if all parameters are OK, FALSE otherwise
1845     ==========================================================================
1846 */
1847 INT     Set_ATE_RX_Antenna_Proc(
1848         IN      PRTMP_ADAPTER   pAd,
1849         IN      PUCHAR                  arg)
1850 {
1851         CHAR value;
1852
1853         value = simple_strtol(arg, 0, 10);
1854
1855         if ((value > 3) || (value < 0))
1856         {
1857                 ATEDBGPRINT(RT_DEBUG_ERROR, ("Set_ATE_RX_Antenna_Proc::Out of range (Value=%d)\n", value));
1858                 return FALSE;
1859         }
1860
1861         pAd->ate.RxAntennaSel = value;
1862
1863         ATEDBGPRINT(RT_DEBUG_TRACE, ("Set_ATE_RX_Antenna_Proc (Antenna = %d)\n", pAd->ate.RxAntennaSel));
1864         ATEDBGPRINT(RT_DEBUG_TRACE, ("Ralink: Set_ATE_RX_Antenna_Proc Success\n"));
1865
1866
1867         return TRUE;
1868 }
1869
1870 /*
1871     ==========================================================================
1872     Description:
1873         Set ATE RF frequence offset
1874
1875     Return:
1876         TRUE if all parameters are OK, FALSE otherwise
1877     ==========================================================================
1878 */
1879 INT     Set_ATE_TX_FREQOFFSET_Proc(
1880         IN      PRTMP_ADAPTER   pAd,
1881         IN      PUCHAR                  arg)
1882 {
1883         UCHAR RFFreqOffset;
1884         ULONG R4;
1885
1886         RFFreqOffset = simple_strtol(arg, 0, 10);
1887 #ifndef RT30xx
1888         if(RFFreqOffset >= 64)
1889 #endif // RT30xx //
1890 #ifdef RT30xx
1891 //2008/08/06: KH modified the limit of offset value from 65 to 95(0x5F)
1892         if(RFFreqOffset >= 95)
1893 #endif // RT30xx //
1894         {
1895                 ATEDBGPRINT(RT_DEBUG_ERROR, ("Set_ATE_TX_FREQOFFSET_Proc::Out of range, it should be in range of 0~63.\n"));
1896                 return FALSE;
1897         }
1898
1899         pAd->ate.RFFreqOffset = RFFreqOffset;
1900 #ifdef RT30xx
1901         if(IS_RT30xx(pAd))
1902         {
1903                 // Set RF offset
1904                 UCHAR RFValue;
1905                 RT30xxReadRFRegister(pAd, RF_R23, (PUCHAR)&RFValue);
1906                                 //2008/08/06: KH modified "pAd->RFFreqOffset" to "pAd->ate.RFFreqOffset"
1907                                 RFValue = (RFValue & 0x80) | pAd->ate.RFFreqOffset;
1908                 RT30xxWriteRFRegister(pAd, RF_R23, (UCHAR)RFValue);
1909         }
1910         else
1911 #endif // RT30xx //
1912         {
1913
1914                 R4 = pAd->ate.RFFreqOffset << 15;               // shift TX power control to correct RF register bit position
1915                 R4 |= (pAd->LatchRfRegs.R4 & ((~0x001f8000)));
1916                 pAd->LatchRfRegs.R4 = R4;
1917
1918                 RtmpRfIoWrite(pAd);
1919         }
1920         ATEDBGPRINT(RT_DEBUG_TRACE, ("Set_ATE_TX_FREQOFFSET_Proc (RFFreqOffset = %d)\n", pAd->ate.RFFreqOffset));
1921         ATEDBGPRINT(RT_DEBUG_TRACE, ("Ralink: Set_ATE_TX_FREQOFFSET_Proc Success\n"));
1922
1923
1924         return TRUE;
1925 }
1926
1927 /*
1928     ==========================================================================
1929     Description:
1930         Set ATE RF BW
1931
1932     Return:
1933         TRUE if all parameters are OK, FALSE otherwise
1934     ==========================================================================
1935 */
1936 INT     Set_ATE_TX_BW_Proc(
1937         IN      PRTMP_ADAPTER   pAd,
1938         IN      PUCHAR                  arg)
1939 {
1940         int i;
1941         UCHAR value = 0;
1942         UCHAR BBPCurrentBW;
1943
1944         BBPCurrentBW = simple_strtol(arg, 0, 10);
1945
1946         if(BBPCurrentBW == 0)
1947                 pAd->ate.TxWI.BW = BW_20;
1948         else
1949                 pAd->ate.TxWI.BW = BW_40;
1950
1951         if(pAd->ate.TxWI.BW == BW_20)
1952         {
1953                 if(pAd->ate.Channel <= 14)
1954                 {
1955                 for (i=0; i<5; i++)
1956                 {
1957                                 if (pAd->Tx20MPwrCfgGBand[i] != 0xffffffff)
1958                                 {
1959                                         RTMP_IO_WRITE32(pAd, TX_PWR_CFG_0 + i*4, pAd->Tx20MPwrCfgGBand[i]);
1960                                         RTMPusecDelay(5000);
1961                                 }
1962                         }
1963                 }
1964                 else
1965                 {
1966                         for (i=0; i<5; i++)
1967                         {
1968                                 if (pAd->Tx20MPwrCfgABand[i] != 0xffffffff)
1969                         {
1970                                         RTMP_IO_WRITE32(pAd, TX_PWR_CFG_0 + i*4, pAd->Tx20MPwrCfgABand[i]);
1971                                 RTMPusecDelay(5000);
1972                         }
1973                 }
1974                 }
1975
1976                 //Set BBP R4 bit[4:3]=0:0
1977                 ATE_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R4, &value);
1978                 value &= (~0x18);
1979                 ATE_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R4, value);
1980
1981                 //Set BBP R66=0x3C
1982                 value = 0x3C;
1983                 ATE_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R66, value);
1984                 //Set BBP R68=0x0B
1985                 //to improve Rx sensitivity.
1986                 value = 0x0B;
1987                 ATE_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R68, value);
1988                 //Set BBP R69=0x16
1989                 value = 0x16;
1990                 ATE_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R69, value);
1991                 //Set BBP R70=0x08
1992                 value = 0x08;
1993                 ATE_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R70, value);
1994                 //Set BBP R73=0x11
1995                 value = 0x11;
1996                 ATE_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R73, value);
1997
1998             // If Channel=14, Bandwidth=20M and Mode=CCK, Set BBP R4 bit5=1
1999             // (Japan filter coefficients)
2000             // This segment of code will only works when ATETXMODE and ATECHANNEL
2001             // were set to MODE_CCK and 14 respectively before ATETXBW is set to 0.
2002             //=====================================================================
2003                 if (pAd->ate.Channel == 14)
2004                 {
2005                         int TxMode = pAd->ate.TxWI.PHYMODE;
2006                         if (TxMode == MODE_CCK)
2007                         {
2008                                 // when Channel==14 && Mode==CCK && BandWidth==20M, BBP R4 bit5=1
2009                                 ATE_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R4, &value);
2010                                 value |= 0x20; //set bit5=1
2011                                 ATE_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R4, value);
2012                         }
2013                 }
2014
2015             //=====================================================================
2016                 // If bandwidth != 40M, RF Reg4 bit 21 = 0.
2017 #ifdef RT30xx
2018         // Set BW
2019                 if(IS_RT30xx(pAd))
2020                         RT30xxWriteRFRegister(pAd, RF_R24, (UCHAR) pAd->Mlme.CaliBW20RfR24);
2021                 else
2022 #endif // RT30xx //
2023                 {
2024                 pAd->LatchRfRegs.R4 &= ~0x00200000;
2025                 RtmpRfIoWrite(pAd);
2026         }
2027
2028         }
2029         else if(pAd->ate.TxWI.BW == BW_40)
2030         {
2031                 if(pAd->ate.Channel <= 14)
2032                 {
2033                         for (i=0; i<5; i++)
2034                         {
2035                                 if (pAd->Tx40MPwrCfgGBand[i] != 0xffffffff)
2036                                 {
2037                                         RTMP_IO_WRITE32(pAd, TX_PWR_CFG_0 + i*4, pAd->Tx40MPwrCfgGBand[i]);
2038                                         RTMPusecDelay(5000);
2039                                 }
2040                         }
2041                 }
2042                 else
2043                 {
2044                         for (i=0; i<5; i++)
2045                         {
2046                                 if (pAd->Tx40MPwrCfgABand[i] != 0xffffffff)
2047                                 {
2048                                         RTMP_IO_WRITE32(pAd, TX_PWR_CFG_0 + i*4, pAd->Tx40MPwrCfgABand[i]);
2049                                         RTMPusecDelay(5000);
2050                                 }
2051                         }
2052 #ifdef DOT11_N_SUPPORT
2053                         if ((pAd->ate.TxWI.PHYMODE >= MODE_HTMIX) && (pAd->ate.TxWI.MCS == 7))
2054                         {
2055                         value = 0x28;
2056                         ATE_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R67, value);
2057                         }
2058 #endif // DOT11_N_SUPPORT //
2059                 }
2060
2061                 //Set BBP R4 bit[4:3]=1:0
2062                 ATE_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R4, &value);
2063                 value &= (~0x18);
2064                 value |= 0x10;
2065                 ATE_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R4, value);
2066
2067                 //Set BBP R66=0x3C
2068                 value = 0x3C;
2069                 ATE_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R66, value);
2070                 //Set BBP R68=0x0C
2071                 //to improve Rx sensitivity.
2072                 value = 0x0C;
2073                 ATE_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R68, value);
2074                 //Set BBP R69=0x1A
2075                 value = 0x1A;
2076                 ATE_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R69, value);
2077                 //Set BBP R70=0x0A
2078                 value = 0x0A;
2079                 ATE_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R70, value);
2080                 //Set BBP R73=0x16
2081                 value = 0x16;
2082                 ATE_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R73, value);
2083
2084                 // If bandwidth = 40M, set RF Reg4 bit 21 = 1.
2085 #ifdef RT30xx
2086         // Set BW
2087                 if(IS_RT30xx(pAd))
2088                         RT30xxWriteRFRegister(pAd, RF_R24, (UCHAR) pAd->Mlme.CaliBW40RfR24);
2089                 else
2090 #endif // RT30xx //
2091                 {
2092                         pAd->LatchRfRegs.R4 |= 0x00200000;
2093                         RtmpRfIoWrite(pAd);
2094                 }
2095         }
2096
2097         ATEDBGPRINT(RT_DEBUG_TRACE, ("Set_ATE_TX_BW_Proc (BBPCurrentBW = %d)\n", pAd->ate.TxWI.BW));
2098         ATEDBGPRINT(RT_DEBUG_TRACE, ("Ralink: Set_ATE_TX_BW_Proc Success\n"));
2099
2100
2101         return TRUE;
2102 }
2103
2104 /*
2105     ==========================================================================
2106     Description:
2107         Set ATE Tx frame length
2108
2109     Return:
2110         TRUE if all parameters are OK, FALSE otherwise
2111     ==========================================================================
2112 */
2113 INT     Set_ATE_TX_LENGTH_Proc(
2114         IN      PRTMP_ADAPTER   pAd,
2115         IN      PUCHAR                  arg)
2116 {
2117         pAd->ate.TxLength = simple_strtol(arg, 0, 10);
2118
2119         if((pAd->ate.TxLength < 24) || (pAd->ate.TxLength > (MAX_FRAME_SIZE - 34/* == 2312 */)))
2120         {
2121                 pAd->ate.TxLength = (MAX_FRAME_SIZE - 34/* == 2312 */);
2122                 ATEDBGPRINT(RT_DEBUG_ERROR, ("Set_ATE_TX_LENGTH_Proc::Out of range, it should be in range of 24~%d.\n", (MAX_FRAME_SIZE - 34/* == 2312 */)));
2123                 return FALSE;
2124         }
2125
2126         ATEDBGPRINT(RT_DEBUG_TRACE, ("Set_ATE_TX_LENGTH_Proc (TxLength = %d)\n", pAd->ate.TxLength));
2127         ATEDBGPRINT(RT_DEBUG_TRACE, ("Ralink: Set_ATE_TX_LENGTH_Proc Success\n"));
2128
2129
2130         return TRUE;
2131 }
2132
2133 /*
2134     ==========================================================================
2135     Description:
2136         Set ATE Tx frame count
2137
2138     Return:
2139         TRUE if all parameters are OK, FALSE otherwise
2140     ==========================================================================
2141 */
2142 INT     Set_ATE_TX_COUNT_Proc(
2143         IN      PRTMP_ADAPTER   pAd,
2144         IN      PUCHAR                  arg)
2145 {
2146         pAd->ate.TxCount = simple_strtol(arg, 0, 10);
2147
2148         ATEDBGPRINT(RT_DEBUG_TRACE, ("Set_ATE_TX_COUNT_Proc (TxCount = %d)\n", pAd->ate.TxCount));
2149         ATEDBGPRINT(RT_DEBUG_TRACE, ("Ralink: Set_ATE_TX_COUNT_Proc Success\n"));
2150
2151
2152         return TRUE;
2153 }
2154
2155 /*
2156     ==========================================================================
2157     Description:
2158         Set ATE Tx frame MCS
2159
2160         Return:
2161                 TRUE if all parameters are OK, FALSE otherwise
2162     ==========================================================================
2163 */
2164 INT     Set_ATE_TX_MCS_Proc(
2165         IN      PRTMP_ADAPTER   pAd,
2166         IN      PUCHAR                  arg)
2167 {
2168         UCHAR MCS;
2169         int result;
2170
2171         MCS = simple_strtol(arg, 0, 10);
2172         result = CheckMCSValid(pAd->ate.TxWI.PHYMODE, MCS);
2173
2174         if (result != -1)
2175         {
2176                 pAd->ate.TxWI.MCS = (UCHAR)MCS;
2177         }
2178         else
2179         {
2180                 ATEDBGPRINT(RT_DEBUG_ERROR, ("Set_ATE_TX_MCS_Proc::Out of range, refer to rate table.\n"));
2181                 return FALSE;
2182         }
2183
2184         ATEDBGPRINT(RT_DEBUG_TRACE, ("Set_ATE_TX_MCS_Proc (MCS = %d)\n", pAd->ate.TxWI.MCS));
2185         ATEDBGPRINT(RT_DEBUG_TRACE, ("Ralink: Set_ATE_TX_MCS_Proc Success\n"));
2186
2187
2188         return TRUE;
2189 }
2190
2191 /*
2192     ==========================================================================
2193     Description:
2194         Set ATE Tx frame Mode
2195         0: MODE_CCK
2196         1: MODE_OFDM
2197         2: MODE_HTMIX
2198         3: MODE_HTGREENFIELD
2199
2200         Return:
2201                 TRUE if all parameters are OK, FALSE otherwise
2202     ==========================================================================
2203 */
2204 INT     Set_ATE_TX_MODE_Proc(
2205         IN      PRTMP_ADAPTER   pAd,
2206         IN      PUCHAR                  arg)
2207 {
2208         pAd->ate.TxWI.PHYMODE = simple_strtol(arg, 0, 10);
2209
2210         if(pAd->ate.TxWI.PHYMODE > 3)
2211         {
2212                 pAd->ate.TxWI.PHYMODE = 0;
2213                 ATEDBGPRINT(RT_DEBUG_ERROR, ("Set_ATE_TX_MODE_Proc::Out of range. it should be in range of 0~3\n"));
2214                 ATEDBGPRINT(RT_DEBUG_ERROR, ("0: CCK, 1: OFDM, 2: HT_MIX, 3: HT_GREEN_FIELD.\n"));
2215                 return FALSE;
2216         }
2217
2218         ATEDBGPRINT(RT_DEBUG_TRACE, ("Set_ATE_TX_MODE_Proc (TxMode = %d)\n", pAd->ate.TxWI.PHYMODE));
2219         ATEDBGPRINT(RT_DEBUG_TRACE, ("Ralink: Set_ATE_TX_MODE_Proc Success\n"));
2220
2221
2222         return TRUE;
2223 }
2224
2225 /*
2226     ==========================================================================
2227     Description:
2228         Set ATE Tx frame GI
2229
2230         Return:
2231                 TRUE if all parameters are OK, FALSE otherwise
2232     ==========================================================================
2233 */
2234 INT     Set_ATE_TX_GI_Proc(
2235         IN      PRTMP_ADAPTER   pAd,
2236         IN      PUCHAR                  arg)
2237 {
2238         pAd->ate.TxWI.ShortGI = simple_strtol(arg, 0, 10);
2239
2240         if(pAd->ate.TxWI.ShortGI > 1)
2241         {
2242                 pAd->ate.TxWI.ShortGI = 0;
2243                 ATEDBGPRINT(RT_DEBUG_ERROR, ("Set_ATE_TX_GI_Proc::Out of range\n"));
2244                 return FALSE;
2245         }
2246
2247         ATEDBGPRINT(RT_DEBUG_TRACE, ("Set_ATE_TX_GI_Proc (GI = %d)\n", pAd->ate.TxWI.ShortGI));
2248         ATEDBGPRINT(RT_DEBUG_TRACE, ("Ralink: Set_ATE_TX_GI_Proc Success\n"));
2249
2250
2251         return TRUE;
2252 }
2253
2254 /*
2255     ==========================================================================
2256     Description:
2257     ==========================================================================
2258  */
2259 INT     Set_ATE_RX_FER_Proc(
2260         IN      PRTMP_ADAPTER   pAd,
2261         IN      PUCHAR                  arg)
2262 {
2263         pAd->ate.bRxFer = simple_strtol(arg, 0, 10);
2264
2265         if (pAd->ate.bRxFer == 1)
2266         {
2267                 pAd->ate.RxCntPerSec = 0;
2268                 pAd->ate.RxTotalCnt = 0;
2269         }
2270
2271         ATEDBGPRINT(RT_DEBUG_TRACE, ("Set_ATE_RX_FER_Proc (bRxFer = %d)\n", pAd->ate.bRxFer));
2272         ATEDBGPRINT(RT_DEBUG_TRACE, ("Ralink: Set_ATE_RX_FER_Proc Success\n"));
2273
2274
2275         return TRUE;
2276 }
2277
2278 INT Set_ATE_Read_RF_Proc(
2279         IN      PRTMP_ADAPTER   pAd,
2280         IN      PUCHAR                  arg)
2281 {
2282 #ifdef RT30xx
2283 //2008/07/10:KH add to support RT30xx ATE<--
2284         if(IS_RT30xx(pAd))
2285         {
2286                         /* modify by WY for Read RF Reg. error */
2287                 UCHAR RFValue;
2288                 INT index=0;
2289                 for (index = 0; index < 32; index++)
2290                 {
2291                         RT30xxReadRFRegister(pAd, index, (PUCHAR)&RFValue);
2292                         printk("R%d=%d\n",index,RFValue);
2293                 }
2294         }
2295         else
2296 //2008/07/10:KH add to support RT30xx ATE-->
2297 #endif // RT30xx //
2298         {
2299                 ate_print(KERN_EMERG "R1 = %lx\n", pAd->LatchRfRegs.R1);
2300                 ate_print(KERN_EMERG "R2 = %lx\n", pAd->LatchRfRegs.R2);
2301                 ate_print(KERN_EMERG "R3 = %lx\n", pAd->LatchRfRegs.R3);
2302                 ate_print(KERN_EMERG "R4 = %lx\n", pAd->LatchRfRegs.R4);
2303         }
2304         return TRUE;
2305 }
2306
2307 INT Set_ATE_Write_RF1_Proc(
2308         IN      PRTMP_ADAPTER   pAd,
2309         IN      PUCHAR                  arg)
2310 {
2311 #ifdef RT30xx
2312 //2008/07/10:KH add to support 3070 ATE<--
2313         if(IS_RT30xx(pAd))
2314         {
2315                 printk("Warning!! RT30xx Don't Support\n");
2316                 return FALSE;
2317
2318         }
2319         else
2320 //2008/07/10:KH add to support 3070 ATE-->
2321 #endif // RT30xx //
2322         {
2323                 UINT32 value = simple_strtol(arg, 0, 16);
2324
2325                 pAd->LatchRfRegs.R1 = value;
2326                 RtmpRfIoWrite(pAd);
2327         }
2328         return TRUE;
2329
2330 }
2331
2332 INT Set_ATE_Write_RF2_Proc(
2333         IN      PRTMP_ADAPTER   pAd,
2334         IN      PUCHAR                  arg)
2335 {
2336 #ifdef RT30xx
2337 //2008/07/10:KH add to support 3070 ATE<--
2338         if(IS_RT30xx(pAd))
2339         {
2340                 printk("Warning!! RT30xx Don't Support\n");
2341                 return FALSE;
2342
2343         }
2344         else
2345 //2008/07/10:KH add to support 3070 ATE-->
2346 #endif // RT30xx //
2347         {
2348                 UINT32 value = simple_strtol(arg, 0, 16);
2349
2350                 pAd->LatchRfRegs.R2 = value;
2351                 RtmpRfIoWrite(pAd);
2352         }
2353         return TRUE;
2354 }
2355
2356 INT Set_ATE_Write_RF3_Proc(
2357         IN      PRTMP_ADAPTER   pAd,
2358         IN      PUCHAR                  arg)
2359 {
2360 #ifdef RT30xx
2361 //2008/07/10:KH add to support 3070 ATE<--
2362         if(IS_RT30xx(pAd))
2363         {
2364                 printk("Warning!! RT30xx Don't Support\n");
2365                 return FALSE;
2366
2367         }
2368         else
2369 //2008/07/10:KH add to support 3070 ATE-->
2370 #endif // RT30xx //
2371         {
2372                 UINT32 value = simple_strtol(arg, 0, 16);
2373
2374                 pAd->LatchRfRegs.R3 = value;
2375                 RtmpRfIoWrite(pAd);
2376         }
2377         return TRUE;
2378 }
2379
2380 INT Set_ATE_Write_RF4_Proc(
2381         IN      PRTMP_ADAPTER   pAd,
2382         IN      PUCHAR                  arg)
2383 {
2384 #ifdef RT30xx
2385 //2008/07/10:KH add to support 3070 ATE<--
2386         if(IS_RT30xx(pAd))
2387         {
2388                 printk("Warning!! RT30xx Don't Support\n");
2389                 return FALSE;
2390
2391         }
2392         else
2393 //2008/07/10:KH add to support 3070 ATE-->
2394 #endif // RT30xx //
2395         {
2396                 UINT32 value = simple_strtol(arg, 0, 16);
2397
2398                 pAd->LatchRfRegs.R4 = value;
2399                 RtmpRfIoWrite(pAd);
2400         }
2401         return TRUE;
2402 }
2403 #ifdef RT30xx
2404 //2008/07/10:KH add to support 3070 ATE<--
2405 INT     SET_ATE_3070RF_Proc(
2406         IN      PRTMP_ADAPTER   pAd,
2407         IN      PUCHAR                  arg)
2408 {
2409         CHAR *this_char;
2410         CHAR *value;
2411         UINT32 Reg,RFValue;
2412         if(IS_RT30xx(pAd))
2413         {
2414                 printk("SET_ATE_3070RF_Proc=%s\n",arg);
2415                 this_char =arg;
2416                 if ((value = strchr(this_char, ':')) != NULL)
2417                         *value++ = 0;
2418                 Reg= simple_strtol(this_char, 0, 16);
2419                 RFValue= simple_strtol(value, 0, 16);
2420                 printk("RF Reg[%d]=%d\n",Reg,RFValue);
2421                 RT30xxWriteRFRegister(pAd, Reg,RFValue);
2422         }
2423         else
2424                 printk("Warning!! Only 3070 Support\n");
2425         return TRUE;
2426 }
2427 //2008/07/10:KH add to support 3070 ATE-->
2428 #endif // RT30xx //
2429 /*
2430     ==========================================================================
2431     Description:
2432         Load and Write EEPROM from a binary file prepared in advance.
2433
2434         Return:
2435                 TRUE if all parameters are OK, FALSE otherwise
2436     ==========================================================================
2437 */
2438 INT Set_ATE_Load_E2P_Proc(
2439         IN      PRTMP_ADAPTER   pAd,
2440         IN      PUCHAR                  arg)
2441 {
2442         BOOLEAN             ret = FALSE;
2443         PUCHAR                  src = EEPROM_BIN_FILE_NAME;
2444         struct file             *srcf;
2445         INT32                   retval, orgfsuid, orgfsgid;
2446         mm_segment_t    orgfs;
2447         USHORT                  WriteEEPROM[(EEPROM_SIZE/2)];
2448         UINT32                  FileLength = 0;
2449         UINT32                  value = simple_strtol(arg, 0, 10);
2450
2451         ATEDBGPRINT(RT_DEBUG_ERROR, ("===> %s (value=%d)\n\n", __func__, value));
2452
2453         if (value > 0)
2454         {
2455                 /* zero the e2p buffer */
2456                 NdisZeroMemory((PUCHAR)WriteEEPROM, EEPROM_SIZE);
2457
2458                 /* save uid and gid used for filesystem access.
2459             ** set user and group to 0 (root)
2460             */
2461                 orgfsuid = current->fsuid;
2462                 orgfsgid = current->fsgid;
2463                 /* as root */
2464                 current->fsuid = current->fsgid = 0;
2465         orgfs = get_fs();
2466         set_fs(KERNEL_DS);
2467
2468                 do
2469                 {
2470                         /* open the bin file */
2471                         srcf = filp_open(src, O_RDONLY, 0);
2472
2473                         if (IS_ERR(srcf))
2474                         {
2475                                 ate_print("%s - Error %ld opening %s\n", __func__, -PTR_ERR(srcf), src);
2476                                 break;
2477                         }
2478
2479                         /* the object must have a read method */
2480                         if ((srcf->f_op == NULL) || (srcf->f_op->read == NULL))
2481                         {
2482                                 ate_print("%s - %s does not have a read method\n", __func__, src);
2483                                 break;
2484                         }
2485
2486                         /* read the firmware from the file *.bin */
2487                         FileLength = srcf->f_op->read(srcf,
2488                                                                                   (PUCHAR)WriteEEPROM,
2489                                                                                   EEPROM_SIZE,
2490                                                                                   &srcf->f_pos);
2491
2492                         if (FileLength != EEPROM_SIZE)
2493                         {
2494                                 ate_print("%s: error file length (=%d) in e2p.bin\n",
2495                                            __func__, FileLength);
2496                                 break;
2497                         }
2498                         else
2499                         {
2500                                 /* write the content of .bin file to EEPROM */
2501                                 rt_ee_write_all(pAd, WriteEEPROM);
2502                                 ret = TRUE;
2503                         }
2504                         break;
2505                 } while(TRUE);
2506
2507                 /* close firmware file */
2508                 if (IS_ERR(srcf))
2509                 {
2510                                 ;
2511                 }
2512                 else
2513                 {
2514                         retval = filp_close(srcf, NULL);
2515                         if (retval)
2516                         {
2517                                 ATEDBGPRINT(RT_DEBUG_ERROR, ("--> Error %d closing %s\n", -retval, src));
2518
2519                         }
2520                 }
2521
2522                 /* restore */
2523                 set_fs(orgfs);
2524                 current->fsuid = orgfsuid;
2525                 current->fsgid = orgfsgid;
2526         }
2527     ATEDBGPRINT(RT_DEBUG_ERROR, ("<=== %s (ret=%d)\n", __func__, ret));
2528
2529     return ret;
2530
2531 }
2532
2533 INT Set_ATE_Read_E2P_Proc(
2534         IN      PRTMP_ADAPTER   pAd,
2535         IN      PUCHAR                  arg)
2536 {
2537         USHORT buffer[EEPROM_SIZE/2];
2538         USHORT *p;
2539         int i;
2540
2541         rt_ee_read_all(pAd, (USHORT *)buffer);
2542         p = buffer;
2543         for (i = 0; i < (EEPROM_SIZE/2); i++)
2544         {
2545                 ate_print("%4.4x ", *p);
2546                 if (((i+1) % 16) == 0)
2547                         ate_print("\n");
2548                 p++;
2549         }
2550         return TRUE;
2551 }
2552
2553 INT     Set_ATE_Show_Proc(
2554         IN      PRTMP_ADAPTER   pAd,
2555         IN      PUCHAR                  arg)
2556 {
2557         ate_print("Mode=%d\n", pAd->ate.Mode);
2558         ate_print("TxPower0=%d\n", pAd->ate.TxPower0);
2559         ate_print("TxPower1=%d\n", pAd->ate.TxPower1);
2560         ate_print("TxAntennaSel=%d\n", pAd->ate.TxAntennaSel);
2561         ate_print("RxAntennaSel=%d\n", pAd->ate.RxAntennaSel);
2562         ate_print("BBPCurrentBW=%d\n", pAd->ate.TxWI.BW);
2563         ate_print("GI=%d\n", pAd->ate.TxWI.ShortGI);
2564         ate_print("MCS=%d\n", pAd->ate.TxWI.MCS);
2565         ate_print("TxMode=%d\n", pAd->ate.TxWI.PHYMODE);
2566         ate_print("Addr1=%02x:%02x:%02x:%02x:%02x:%02x\n",
2567                 pAd->ate.Addr1[0], pAd->ate.Addr1[1], pAd->ate.Addr1[2], pAd->ate.Addr1[3], pAd->ate.Addr1[4], pAd->ate.Addr1[5]);
2568         ate_print("Addr2=%02x:%02x:%02x:%02x:%02x:%02x\n",
2569                 pAd->ate.Addr2[0], pAd->ate.Addr2[1], pAd->ate.Addr2[2], pAd->ate.Addr2[3], pAd->ate.Addr2[4], pAd->ate.Addr2[5]);
2570         ate_print("Addr3=%02x:%02x:%02x:%02x:%02x:%02x\n",
2571                 pAd->ate.Addr3[0], pAd->ate.Addr3[1], pAd->ate.Addr3[2], pAd->ate.Addr3[3], pAd->ate.Addr3[4], pAd->ate.Addr3[5]);
2572         ate_print("Channel=%d\n", pAd->ate.Channel);
2573         ate_print("TxLength=%d\n", pAd->ate.TxLength);
2574         ate_print("TxCount=%u\n", pAd->ate.TxCount);
2575         ate_print("RFFreqOffset=%d\n", pAd->ate.RFFreqOffset);
2576         ate_print(KERN_EMERG "Set_ATE_Show_Proc Success\n");
2577         return TRUE;
2578 }
2579
2580 INT     Set_ATE_Help_Proc(
2581         IN      PRTMP_ADAPTER   pAd,
2582         IN      PUCHAR                  arg)
2583 {
2584         ate_print("ATE=ATESTART, ATESTOP, TXCONT, TXCARR, TXFRAME, RXFRAME\n");
2585         ate_print("ATEDA\n");
2586         ate_print("ATESA\n");
2587         ate_print("ATEBSSID\n");
2588         ate_print("ATECHANNEL, range:0~14(unless A band !)\n");
2589         ate_print("ATETXPOW0, set power level of antenna 1.\n");
2590         ate_print("ATETXPOW1, set power level of antenna 2.\n");
2591         ate_print("ATETXANT, set TX antenna. 0:all, 1:antenna one, 2:antenna two.\n");
2592         ate_print("ATERXANT, set RX antenna.0:all, 1:antenna one, 2:antenna two, 3:antenna three.\n");
2593         ate_print("ATETXFREQOFFSET, set frequency offset, range 0~63\n");
2594         ate_print("ATETXBW, set BandWidth, 0:20MHz, 1:40MHz.\n");
2595         ate_print("ATETXLEN, set Frame length, range 24~%d\n", (MAX_FRAME_SIZE - 34/* == 2312 */));
2596         ate_print("ATETXCNT, set how many frame going to transmit.\n");
2597         ate_print("ATETXMCS, set MCS, reference to rate table.\n");
2598         ate_print("ATETXMODE, set Mode 0:CCK, 1:OFDM, 2:HT-Mix, 3:GreenField, reference to rate table.\n");
2599         ate_print("ATETXGI, set GI interval, 0:Long, 1:Short\n");
2600         ate_print("ATERXFER, 0:disable Rx Frame error rate. 1:enable Rx Frame error rate.\n");
2601         ate_print("ATERRF, show all RF registers.\n");
2602         ate_print("ATEWRF1, set RF1 register.\n");
2603         ate_print("ATEWRF2, set RF2 register.\n");
2604         ate_print("ATEWRF3, set RF3 register.\n");
2605         ate_print("ATEWRF4, set RF4 register.\n");
2606         ate_print("ATELDE2P, load EEPROM from .bin file.\n");
2607         ate_print("ATERE2P, display all EEPROM content.\n");
2608         ate_print("ATESHOW, display all parameters of ATE.\n");
2609         ate_print("ATEHELP, online help.\n");
2610
2611         return TRUE;
2612 }
2613
2614 /*
2615     ==========================================================================
2616     Description:
2617
2618         AsicSwitchChannel() dedicated for ATE.
2619
2620     ==========================================================================
2621 */
2622 VOID ATEAsicSwitchChannel(
2623     IN PRTMP_ADAPTER pAd)
2624 {
2625         UINT32 R2 = 0, R3 = DEFAULT_RF_TX_POWER, R4 = 0, Value = 0;
2626         CHAR TxPwer = 0, TxPwer2 = 0;
2627         UCHAR index, BbpValue = 0, R66 = 0x30;
2628         RTMP_RF_REGS *RFRegTable;
2629         UCHAR Channel;
2630
2631 #ifdef RALINK_28xx_QA
2632         if ((pAd->ate.bQATxStart == TRUE) || (pAd->ate.bQARxStart == TRUE))
2633         {
2634                 if (pAd->ate.Channel != pAd->LatchRfRegs.Channel)
2635                 {
2636                         pAd->ate.Channel = pAd->LatchRfRegs.Channel;
2637                 }
2638                 return;
2639         }
2640         else
2641 #endif // RALINK_28xx_QA //
2642         Channel = pAd->ate.Channel;
2643
2644         // Select antenna
2645         AsicAntennaSelect(pAd, Channel);
2646
2647         // fill Tx power value
2648         TxPwer = pAd->ate.TxPower0;
2649         TxPwer2 = pAd->ate.TxPower1;
2650 #ifdef RT30xx
2651 //2008/07/10:KH add to support 3070 ATE<--
2652
2653         // The RF programming sequence is difference between 3xxx and 2xxx
2654         // The 3070 is 1T1R. Therefore, we don't need to set the number of Tx/Rx path and the only job is to set the parameters of channels.
2655         if (IS_RT30xx(pAd) && ((pAd->RfIcType == RFIC_3020) ||
2656 (pAd->RfIcType == RFIC_3021) || (pAd->RfIcType == RFIC_3022) ||
2657 (pAd->RfIcType == RFIC_2020)))
2658         {
2659                 /* modify by WY for Read RF Reg. error */
2660                 UCHAR RFValue;
2661
2662                 for (index = 0; index < NUM_OF_3020_CHNL; index++)
2663                 {
2664                         if (Channel == FreqItems3020[index].Channel)
2665                         {
2666                                 // Programming channel parameters
2667                                 RT30xxWriteRFRegister(pAd, RF_R02, FreqItems3020[index].N);
2668                                 RT30xxWriteRFRegister(pAd, RF_R03, FreqItems3020[index].K);
2669
2670                                 RT30xxReadRFRegister(pAd, RF_R06, (PUCHAR)&RFValue);
2671                                 RFValue = (RFValue & 0xFC) | FreqItems3020[index].R;
2672                                 RT30xxWriteRFRegister(pAd, RF_R06, (UCHAR)RFValue);
2673
2674                                 // Set Tx Power
2675                                 RT30xxReadRFRegister(pAd, RF_R12, (PUCHAR)&RFValue);
2676                                 RFValue = (RFValue & 0xE0) | TxPwer;
2677                                 RT30xxWriteRFRegister(pAd, RF_R12, (UCHAR)RFValue);
2678
2679                                 // Set RF offset
2680                                 RT30xxReadRFRegister(pAd, RF_R23, (PUCHAR)&RFValue);
2681                                 //2008/08/06: KH modified "pAd->RFFreqOffset" to "pAd->ate.RFFreqOffset"
2682                                 RFValue = (RFValue & 0x80) | pAd->ate.RFFreqOffset;
2683                                 RT30xxWriteRFRegister(pAd, RF_R23, (UCHAR)RFValue);
2684
2685                                 // Set BW
2686                                 if (pAd->ate.TxWI.BW == BW_40)
2687                                 {
2688                                         RFValue = pAd->Mlme.CaliBW40RfR24;
2689                                         //DISABLE_11N_CHECK(pAd);
2690                                 }
2691                                 else
2692                                 {
2693                                         RFValue = pAd->Mlme.CaliBW20RfR24;
2694                                 }
2695                                 RT30xxWriteRFRegister(pAd, RF_R24, (UCHAR)RFValue);
2696
2697                                 // Enable RF tuning
2698                                 RT30xxReadRFRegister(pAd, RF_R07, (PUCHAR)&RFValue);
2699                                 RFValue = RFValue | 0x1;
2700                                 RT30xxWriteRFRegister(pAd, RF_R07, (UCHAR)RFValue);
2701
2702                                 // latch channel for future usage.
2703                                 pAd->LatchRfRegs.Channel = Channel;
2704
2705                                 break;
2706                         }
2707                 }
2708
2709                 DBGPRINT(RT_DEBUG_TRACE, ("SwitchChannel#%d(RF=%d, Pwr0=%d, Pwr1=%d, %dT), N=0x%02X, K=0x%02X, R=0x%02X\n",
2710                         Channel,
2711                         pAd->RfIcType,
2712                         TxPwer,
2713                         TxPwer2,
2714                         pAd->Antenna.field.TxPath,
2715                         FreqItems3020[index].N,
2716                         FreqItems3020[index].K,
2717                         FreqItems3020[index].R));
2718         }
2719         else
2720 //2008/07/10:KH add to support 3070 ATE-->
2721 #endif // RT30xx //
2722 {
2723         RFRegTable = RF2850RegTable;
2724
2725         switch (pAd->RfIcType)
2726         {
2727                 /* But only 2850 and 2750 support 5.5GHz band... */
2728                 case RFIC_2820:
2729                 case RFIC_2850:
2730                 case RFIC_2720:
2731                 case RFIC_2750:
2732
2733                         for (index = 0; index < NUM_OF_2850_CHNL; index++)
2734                         {
2735                                 if (Channel == RFRegTable[index].Channel)
2736                                 {
2737                                         R2 = RFRegTable[index].R2;
2738                                         if (pAd->Antenna.field.TxPath == 1)
2739                                         {
2740                                                 R2 |= 0x4000;   // If TXpath is 1, bit 14 = 1;
2741                                         }
2742
2743                                         if (pAd->Antenna.field.RxPath == 2)
2744                                         {
2745                                                 switch (pAd->ate.RxAntennaSel)
2746                                                 {
2747                                                         case 1:
2748                                                                 R2 |= 0x20040;
2749                                                                 ATE_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R3, &BbpValue);
2750                                                                 BbpValue &= 0xE4;
2751                                                                 BbpValue |= 0x00;
2752                                                                 ATE_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R3, BbpValue);
2753                                                                 break;
2754                                                         case 2:
2755                                                                 R2 |= 0x10040;
2756                                                                 ATE_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R3, &BbpValue);
2757                                                                 BbpValue &= 0xE4;
2758                                                                 BbpValue |= 0x01;
2759                                                                 ATE_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R3, BbpValue);
2760                                                                 break;
2761                                                         default:
2762                                                                 R2 |= 0x40;
2763                                                                 ATE_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R3, &BbpValue);
2764                                                                 BbpValue &= 0xE4;
2765                                                                 /* Only enable two Antenna to receive. */
2766                                                                 BbpValue |= 0x08;
2767                                                                 ATE_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R3, BbpValue);
2768                                                                 break;
2769                                                 }
2770                                         }
2771                                         else if (pAd->Antenna.field.RxPath == 1)
2772                                         {
2773                                                 R2 |= 0x20040;  // write 1 to off RxPath
2774                                         }
2775
2776                                         if (pAd->Antenna.field.TxPath == 2)
2777                                         {
2778                                                 if (pAd->ate.TxAntennaSel == 1)
2779                                                 {
2780                                                         R2 |= 0x4000;   // If TX Antenna select is 1 , bit 14 = 1; Disable Ant 2
2781                                                         ATE_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R1, &BbpValue);
2782                                                         BbpValue &= 0xE7;               //11100111B
2783                                                         ATE_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R1, BbpValue);
2784                                                 }
2785                                                 else if (pAd->ate.TxAntennaSel == 2)
2786                                                 {
2787                                                         R2 |= 0x8000;   // If TX Antenna select is 2 , bit 15 = 1; Disable Ant 1
2788                                                         ATE_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R1, &BbpValue);
2789                                                         BbpValue &= 0xE7;
2790                                                         BbpValue |= 0x08;
2791                                                         ATE_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R1, BbpValue);
2792                                                 }
2793                                                 else
2794                                                 {
2795                                                         ATE_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R1, &BbpValue);
2796                                                         BbpValue &= 0xE7;
2797                                                         BbpValue |= 0x10;
2798                                                         ATE_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R1, BbpValue);
2799                                                 }
2800                                         }
2801                                         if (pAd->Antenna.field.RxPath == 3)
2802                                         {
2803                                                 switch (pAd->ate.RxAntennaSel)
2804                                                 {
2805                                                         case 1:
2806                                                                 R2 |= 0x20040;
2807                                                                 ATE_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R3, &BbpValue);
2808                                                                 BbpValue &= 0xE4;
2809                                                                 BbpValue |= 0x00;
2810                                                                 ATE_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R3, BbpValue);
2811                                                                 break;
2812                                                         case 2:
2813                                                                 R2 |= 0x10040;
2814                                                                 ATE_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R3, &BbpValue);
2815                                                                 BbpValue &= 0xE4;
2816                                                                 BbpValue |= 0x01;
2817                                                                 ATE_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R3, BbpValue);
2818                                                                 break;
2819                                                         case 3:
2820                                                                 R2 |= 0x30000;
2821                                                                 ATE_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R3, &BbpValue);
2822                                                                 BbpValue &= 0xE4;
2823                                                                 BbpValue |= 0x02;
2824                                                                 ATE_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R3, BbpValue);
2825                                                                 break;
2826                                                         default:
2827                                                                 ATE_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R3, &BbpValue);
2828                                                                 BbpValue &= 0xE4;
2829                                                                 BbpValue |= 0x10;
2830                                                                 ATE_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R3, BbpValue);
2831                                                                 break;
2832                                                 }
2833                                         }
2834
2835                                         if (Channel > 14)
2836                                         {
2837                                                 // initialize R3, R4
2838                                                 R3 = (RFRegTable[index].R3 & 0xffffc1ff);
2839                                                 R4 = (RFRegTable[index].R4 & (~0x001f87c0)) | (pAd->ate.RFFreqOffset << 15);
2840
2841                         // According the Rory's suggestion to solve the middle range issue.
2842                                                 // 5.5G band power range: 0xF9~0X0F, TX0 Reg3 bit9/TX1 Reg4 bit6="0" means the TX power reduce 7dB
2843                                                 // R3
2844                                                 if ((TxPwer >= -7) && (TxPwer < 0))
2845                                                 {
2846                                                         TxPwer = (7+TxPwer);
2847                                                         TxPwer = (TxPwer > 0xF) ? (0xF) : (TxPwer);
2848                                                         R3 |= (TxPwer << 10);
2849                                                         ATEDBGPRINT(RT_DEBUG_TRACE, ("ATEAsicSwitchChannel: TxPwer=%d \n", TxPwer));
2850                                                 }
2851                                                 else
2852                                                 {
2853                                                         TxPwer = (TxPwer > 0xF) ? (0xF) : (TxPwer);
2854                                                         R3 |= (TxPwer << 10) | (1 << 9);
2855                                                 }
2856
2857                                                 // R4
2858                                                 if ((TxPwer2 >= -7) && (TxPwer2 < 0))
2859                                                 {
2860                                                         TxPwer2 = (7+TxPwer2);
2861                                                         TxPwer2 = (TxPwer2 > 0xF) ? (0xF) : (TxPwer2);
2862                                                         R4 |= (TxPwer2 << 7);
2863                                                         ATEDBGPRINT(RT_DEBUG_TRACE, ("ATEAsicSwitchChannel: TxPwer2=%d \n", TxPwer2));
2864                                                 }
2865                                                 else
2866                                                 {
2867                                                         TxPwer2 = (TxPwer2 > 0xF) ? (0xF) : (TxPwer2);
2868                                                         R4 |= (TxPwer2 << 7) | (1 << 6);
2869                                                 }
2870                                         }
2871                                         else
2872                                         {
2873                                                 R3 = (RFRegTable[index].R3 & 0xffffc1ff) | (TxPwer << 9); // set TX power0
2874                                                 R4 = (RFRegTable[index].R4 & (~0x001f87c0)) | (pAd->ate.RFFreqOffset << 15) | (TxPwer2 <<6);// Set freq offset & TxPwr1
2875                                         }
2876
2877                                         // Based on BBP current mode before changing RF channel.
2878                                         if (pAd->ate.TxWI.BW == BW_40)
2879                                         {
2880                                                 R4 |=0x200000;
2881                                         }
2882
2883                                         // Update variables
2884                                         pAd->LatchRfRegs.Channel = Channel;
2885                                         pAd->LatchRfRegs.R1 = RFRegTable[index].R1;
2886                                         pAd->LatchRfRegs.R2 = R2;
2887                                         pAd->LatchRfRegs.R3 = R3;
2888                                         pAd->LatchRfRegs.R4 = R4;
2889
2890                                         RtmpRfIoWrite(pAd);
2891
2892                                         break;
2893                                 }
2894                         }
2895                         break;
2896
2897                 default:
2898                         break;
2899         }
2900 }
2901         // Change BBP setting during switch from a->g, g->a
2902         if (Channel <= 14)
2903         {
2904             ULONG       TxPinCfg = 0x00050F0A;// 2007.10.09 by Brian : 0x0005050A ==> 0x00050F0A
2905
2906                 ATE_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R62, (0x37 - GET_LNA_GAIN(pAd)));
2907                 ATE_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R63, (0x37 - GET_LNA_GAIN(pAd)));
2908                 ATE_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R64, (0x37 - GET_LNA_GAIN(pAd)));
2909
2910                 /* For 1T/2R chip only... */
2911             if (pAd->NicConfig2.field.ExternalLNAForG)
2912             {
2913                 ATE_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R82, 0x62);
2914             }
2915             else
2916             {
2917                 ATE_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R82, 0x84);
2918             }
2919
2920         // According the Rory's suggestion to solve the middle range issue.
2921                 ATE_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R86, &BbpValue);
2922                 ASSERT((BbpValue == 0x00));
2923                 if ((BbpValue != 0x00))
2924                 {
2925                         ATE_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R86, 0x00);
2926                 }
2927
2928                 // 5.5GHz band selection PIN, bit1 and bit2 are complement
2929                 RTMP_IO_READ32(pAd, TX_BAND_CFG, &Value);
2930                 Value &= (~0x6);
2931                 Value |= (0x04);
2932                 RTMP_IO_WRITE32(pAd, TX_BAND_CFG, Value);
2933
2934         // Turn off unused PA or LNA when only 1T or 1R.
2935                 if (pAd->Antenna.field.TxPath == 1)
2936                 {
2937                         TxPinCfg &= 0xFFFFFFF3;
2938                 }
2939                 if (pAd->Antenna.field.RxPath == 1)
2940                 {
2941                         TxPinCfg &= 0xFFFFF3FF;
2942                 }
2943
2944                 RTMP_IO_WRITE32(pAd, TX_PIN_CFG, TxPinCfg);
2945         }
2946         else
2947         {
2948             ULONG       TxPinCfg = 0x00050F05;//2007.10.09 by Brian : 0x00050505 ==> 0x00050F05
2949
2950                 ATE_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R62, (0x37 - GET_LNA_GAIN(pAd)));
2951                 ATE_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R63, (0x37 - GET_LNA_GAIN(pAd)));
2952                 ATE_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R64, (0x37 - GET_LNA_GAIN(pAd)));
2953                 ATE_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R82, 0xF2);
2954
2955         // According the Rory's suggestion to solve the middle range issue.
2956                 ATE_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R86, &BbpValue);
2957                 ASSERT((BbpValue == 0x00));
2958                 if ((BbpValue != 0x00))
2959                 {
2960                         ATE_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R86, 0x00);
2961                 }
2962                 ATE_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R91, &BbpValue);
2963                 ASSERT((BbpValue == 0x04));
2964
2965                 ATE_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R92, &BbpValue);
2966                 ASSERT((BbpValue == 0x00));
2967
2968                 // 5.5GHz band selection PIN, bit1 and bit2 are complement
2969                 RTMP_IO_READ32(pAd, TX_BAND_CFG, &Value);
2970                 Value &= (~0x6);
2971                 Value |= (0x02);
2972                 RTMP_IO_WRITE32(pAd, TX_BAND_CFG, Value);
2973
2974         // Turn off unused PA or LNA when only 1T or 1R.
2975                 if (pAd->Antenna.field.TxPath == 1)
2976                 {
2977                         TxPinCfg &= 0xFFFFFFF3;
2978             }
2979                 if (pAd->Antenna.field.RxPath == 1)
2980                 {
2981                         TxPinCfg &= 0xFFFFF3FF;
2982                 }
2983
2984                 RTMP_IO_WRITE32(pAd, TX_PIN_CFG, TxPinCfg);
2985         }
2986
2987     // R66 should be set according to Channel and use 20MHz when scanning
2988         if (Channel <= 14)
2989         {
2990                 // BG band
2991                 R66 = 0x2E + GET_LNA_GAIN(pAd);
2992                 ATE_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R66, R66);
2993         }
2994         else
2995         {
2996                 // 5.5 GHz band
2997                 if (pAd->ate.TxWI.BW == BW_20)
2998                 {
2999                         R66 = (UCHAR)(0x32 + (GET_LNA_GAIN(pAd)*5)/3);
3000                 ATE_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R66, R66);
3001                 }
3002                 else
3003                 {
3004                         R66 = (UCHAR)(0x3A + (GET_LNA_GAIN(pAd)*5)/3);
3005                         ATE_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R66, R66);
3006                 }
3007         }
3008
3009         //
3010         // On 11A, We should delay and wait RF/BBP to be stable
3011         // and the appropriate time should be 1000 micro seconds
3012         // 2005/06/05 - On 11G, We also need this delay time. Otherwise it's difficult to pass the WHQL.
3013         //
3014         RTMPusecDelay(1000);
3015
3016         if (Channel > 14)
3017         {
3018                 // When 5.5GHz band the LSB of TxPwr will be used to reduced 7dB or not.
3019                 ATEDBGPRINT(RT_DEBUG_TRACE, ("SwitchChannel#%d(RF=%d, %dT) to , R1=0x%08lx, R2=0x%08lx, R3=0x%08lx, R4=0x%08lx\n",
3020                                                                   Channel,
3021                                                                   pAd->RfIcType,
3022                                                                   pAd->Antenna.field.TxPath,
3023                                                                   pAd->LatchRfRegs.R1,
3024                                                                   pAd->LatchRfRegs.R2,
3025                                                                   pAd->LatchRfRegs.R3,
3026                                                                   pAd->LatchRfRegs.R4));
3027         }
3028         else
3029         {
3030                 ATEDBGPRINT(RT_DEBUG_TRACE, ("SwitchChannel#%d(RF=%d, Pwr0=%u, Pwr1=%u, %dT) to , R1=0x%08lx, R2=0x%08lx, R3=0x%08lx, R4=0x%08lx\n",
3031                                                                   Channel,
3032                                                                   pAd->RfIcType,
3033                                                                   (R3 & 0x00003e00) >> 9,
3034                                                                   (R4 & 0x000007c0) >> 6,
3035                                                                   pAd->Antenna.field.TxPath,
3036                                                                   pAd->LatchRfRegs.R1,
3037                                                                   pAd->LatchRfRegs.R2,
3038                                                                   pAd->LatchRfRegs.R3,
3039                                                                   pAd->LatchRfRegs.R4));
3040     }
3041 }
3042
3043 //
3044 // In fact, no one will call this routine so far !
3045 //
3046 /*
3047         ==========================================================================
3048         Description:
3049                 Gives CCK TX rate 2 more dB TX power.
3050                 This routine works only in ATE mode.
3051
3052                 calculate desired Tx power in RF R3.Tx0~5,      should consider -
3053                 0. if current radio is a noisy environment (pAd->DrsCounters.fNoisyEnvironment)
3054                 1. TxPowerPercentage
3055                 2. auto calibration based on TSSI feedback
3056                 3. extra 2 db for CCK
3057                 4. -10 db upon very-short distance (AvgRSSI >= -40db) to AP
3058
3059         NOTE: Since this routine requires the value of (pAd->DrsCounters.fNoisyEnvironment),
3060                 it should be called AFTER MlmeDynamicTxRateSwitching()
3061         ==========================================================================
3062  */
3063 VOID ATEAsicAdjustTxPower(
3064         IN PRTMP_ADAPTER pAd)
3065 {
3066         INT                     i, j;
3067         CHAR            DeltaPwr = 0;
3068         BOOLEAN         bAutoTxAgc = FALSE;
3069         UCHAR           TssiRef, *pTssiMinusBoundary, *pTssiPlusBoundary, TxAgcStep;
3070         UCHAR           BbpR49 = 0, idx;
3071         PCHAR           pTxAgcCompensate;
3072         ULONG           TxPwr[5];
3073         CHAR            Value;
3074
3075         /* no one calls this procedure so far */
3076         if (pAd->ate.TxWI.BW == BW_40)
3077         {
3078                 if (pAd->ate.Channel > 14)
3079                 {
3080                         TxPwr[0] = pAd->Tx40MPwrCfgABand[0];
3081                         TxPwr[1] = pAd->Tx40MPwrCfgABand[1];
3082                         TxPwr[2] = pAd->Tx40MPwrCfgABand[2];
3083                         TxPwr[3] = pAd->Tx40MPwrCfgABand[3];
3084                         TxPwr[4] = pAd->Tx40MPwrCfgABand[4];
3085                 }
3086                 else
3087                 {
3088                         TxPwr[0] = pAd->Tx40MPwrCfgGBand[0];
3089                         TxPwr[1] = pAd->Tx40MPwrCfgGBand[1];
3090                         TxPwr[2] = pAd->Tx40MPwrCfgGBand[2];
3091                         TxPwr[3] = pAd->Tx40MPwrCfgGBand[3];
3092                         TxPwr[4] = pAd->Tx40MPwrCfgGBand[4];
3093                 }
3094         }
3095         else
3096         {
3097                 if (pAd->ate.Channel > 14)
3098                 {
3099                         TxPwr[0] = pAd->Tx20MPwrCfgABand[0];
3100                         TxPwr[1] = pAd->Tx20MPwrCfgABand[1];
3101                         TxPwr[2] = pAd->Tx20MPwrCfgABand[2];
3102                         TxPwr[3] = pAd->Tx20MPwrCfgABand[3];
3103                         TxPwr[4] = pAd->Tx20MPwrCfgABand[4];
3104                 }
3105                 else
3106                 {
3107                         TxPwr[0] = pAd->Tx20MPwrCfgGBand[0];
3108                         TxPwr[1] = pAd->Tx20MPwrCfgGBand[1];
3109                         TxPwr[2] = pAd->Tx20MPwrCfgGBand[2];
3110                         TxPwr[3] = pAd->Tx20MPwrCfgGBand[3];
3111                         TxPwr[4] = pAd->Tx20MPwrCfgGBand[4];
3112                 }
3113         }
3114
3115         // TX power compensation for temperature variation based on TSSI.
3116         // Do it per 4 seconds.
3117         if (pAd->Mlme.OneSecPeriodicRound % 4 == 0)
3118         {
3119                 if (pAd->ate.Channel <= 14)
3120                 {
3121                         /* bg channel */
3122                         bAutoTxAgc         = pAd->bAutoTxAgcG;
3123                         TssiRef            = pAd->TssiRefG;
3124                         pTssiMinusBoundary = &pAd->TssiMinusBoundaryG[0];
3125                         pTssiPlusBoundary  = &pAd->TssiPlusBoundaryG[0];
3126                         TxAgcStep          = pAd->TxAgcStepG;
3127                         pTxAgcCompensate   = &pAd->TxAgcCompensateG;
3128                 }
3129                 else
3130                 {
3131                         /* a channel */
3132                         bAutoTxAgc         = pAd->bAutoTxAgcA;
3133                         TssiRef            = pAd->TssiRefA;
3134                         pTssiMinusBoundary = &pAd->TssiMinusBoundaryA[0];
3135                         pTssiPlusBoundary  = &pAd->TssiPlusBoundaryA[0];
3136                         TxAgcStep          = pAd->TxAgcStepA;
3137                         pTxAgcCompensate   = &pAd->TxAgcCompensateA;
3138                 }
3139
3140                 if (bAutoTxAgc)
3141                 {
3142                         /* BbpR49 is unsigned char */
3143                         ATE_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R49, &BbpR49);
3144
3145                         /* (p) TssiPlusBoundaryG[0] = 0 = (m) TssiMinusBoundaryG[0] */
3146                         /* compensate: +4     +3   +2   +1    0   -1   -2   -3   -4 * steps */
3147                         /* step value is defined in pAd->TxAgcStepG for tx power value */
3148
3149                         /* [4]+1+[4]   p4     p3   p2   p1   o1   m1   m2   m3   m4 */
3150                         /* ex:         0x00 0x15 0x25 0x45 0x88 0xA0 0xB5 0xD0 0xF0
3151                            above value are examined in mass factory production */
3152                         /*             [4]    [3]  [2]  [1]  [0]  [1]  [2]  [3]  [4] */
3153
3154                         /* plus is 0x10 ~ 0x40, minus is 0x60 ~ 0x90 */
3155                         /* if value is between p1 ~ o1 or o1 ~ s1, no need to adjust tx power */
3156                         /* if value is 0x65, tx power will be -= TxAgcStep*(2-1) */
3157
3158                         if (BbpR49 > pTssiMinusBoundary[1])
3159                         {
3160                                 // Reading is larger than the reference value.
3161                                 // Check for how large we need to decrease the Tx power.
3162                                 for (idx = 1; idx < 5; idx++)
3163                                 {
3164                                         if (BbpR49 <= pTssiMinusBoundary[idx])  // Found the range
3165                                                 break;
3166                                 }
3167                                 // The index is the step we should decrease, idx = 0 means there is nothing to compensate
3168 //                              if (R3 > (ULONG) (TxAgcStep * (idx-1)))
3169                                         *pTxAgcCompensate = -(TxAgcStep * (idx-1));
3170 //                              else
3171 //                                      *pTxAgcCompensate = -((UCHAR)R3);
3172
3173                                 DeltaPwr += (*pTxAgcCompensate);
3174                                 ATEDBGPRINT(RT_DEBUG_TRACE, ("-- Tx Power, BBP R1=%x, TssiRef=%x, TxAgcStep=%x, step = -%d\n",
3175                                         BbpR49, TssiRef, TxAgcStep, idx-1));
3176                         }
3177                         else if (BbpR49 < pTssiPlusBoundary[1])
3178                         {
3179                                 // Reading is smaller than the reference value
3180                                 // check for how large we need to increase the Tx power
3181                                 for (idx = 1; idx < 5; idx++)
3182                                 {
3183                                         if (BbpR49 >= pTssiPlusBoundary[idx])   // Found the range
3184                                                 break;
3185                                 }
3186                                 // The index is the step we should increase, idx = 0 means there is nothing to compensate
3187                                 *pTxAgcCompensate = TxAgcStep * (idx-1);
3188                                 DeltaPwr += (*pTxAgcCompensate);
3189                                 ATEDBGPRINT(RT_DEBUG_TRACE, ("++ Tx Power, BBP R1=%x, TssiRef=%x, TxAgcStep=%x, step = +%d\n",
3190                                         BbpR49, TssiRef, TxAgcStep, idx-1));
3191                         }
3192                         else
3193                         {
3194                                 *pTxAgcCompensate = 0;
3195                                 ATEDBGPRINT(RT_DEBUG_TRACE, ("   Tx Power, BBP R1=%x, TssiRef=%x, TxAgcStep=%x, step = +%d\n",
3196                                         BbpR49, TssiRef, TxAgcStep, 0));
3197                         }
3198                 }
3199         }
3200         else
3201         {
3202                 if (pAd->ate.Channel <= 14)
3203                 {
3204                         bAutoTxAgc         = pAd->bAutoTxAgcG;
3205                         pTxAgcCompensate   = &pAd->TxAgcCompensateG;
3206                 }
3207                 else
3208                 {
3209                         bAutoTxAgc         = pAd->bAutoTxAgcA;
3210                         pTxAgcCompensate   = &pAd->TxAgcCompensateA;
3211                 }
3212
3213                 if (bAutoTxAgc)
3214                         DeltaPwr += (*pTxAgcCompensate);
3215         }
3216
3217         /* calculate delta power based on the percentage specified from UI */
3218         // E2PROM setting is calibrated for maximum TX power (i.e. 100%)
3219         // We lower TX power here according to the percentage specified from UI
3220         if (pAd->CommonCfg.TxPowerPercentage == 0xffffffff)       // AUTO TX POWER control
3221                 ;
3222         else if (pAd->CommonCfg.TxPowerPercentage > 90)  // 91 ~ 100% & AUTO, treat as 100% in terms of mW
3223                 ;
3224         else if (pAd->CommonCfg.TxPowerPercentage > 60)  // 61 ~ 90%, treat as 75% in terms of mW
3225         {
3226                 DeltaPwr -= 1;
3227         }
3228         else if (pAd->CommonCfg.TxPowerPercentage > 30)  // 31 ~ 60%, treat as 50% in terms of mW
3229         {
3230                 DeltaPwr -= 3;
3231         }
3232         else if (pAd->CommonCfg.TxPowerPercentage > 15)  // 16 ~ 30%, treat as 25% in terms of mW
3233         {
3234                 DeltaPwr -= 6;
3235         }
3236         else if (pAd->CommonCfg.TxPowerPercentage > 9)   // 10 ~ 15%, treat as 12.5% in terms of mW
3237         {
3238                 DeltaPwr -= 9;
3239         }
3240         else                                           // 0 ~ 9 %, treat as MIN(~3%) in terms of mW
3241         {
3242                 DeltaPwr -= 12;
3243         }
3244
3245         /* reset different new tx power for different TX rate */
3246         for(i=0; i<5; i++)
3247         {
3248                 if (TxPwr[i] != 0xffffffff)
3249                 {
3250                         for (j=0; j<8; j++)
3251                         {
3252                                 Value = (CHAR)((TxPwr[i] >> j*4) & 0x0F); /* 0 ~ 15 */
3253
3254                                 if ((Value + DeltaPwr) < 0)
3255                                 {
3256                                         Value = 0; /* min */
3257                                 }
3258                                 else if ((Value + DeltaPwr) > 0xF)
3259                                 {
3260                                         Value = 0xF; /* max */
3261                                 }
3262                                 else
3263                                 {
3264                                         Value += DeltaPwr; /* temperature compensation */
3265                                 }
3266
3267                                 /* fill new value to CSR offset */
3268                                 TxPwr[i] = (TxPwr[i] & ~(0x0000000F << j*4)) | (Value << j*4);
3269                         }
3270
3271                         /* write tx power value to CSR */
3272                         /* TX_PWR_CFG_0 (8 tx rate) for TX power for OFDM 12M/18M
3273                                                                                         TX power for OFDM 6M/9M
3274                                                                                         TX power for CCK5.5M/11M
3275                                                                                         TX power for CCK1M/2M */
3276                         /* TX_PWR_CFG_1 ~ TX_PWR_CFG_4 */
3277                         RTMP_IO_WRITE32(pAd, TX_PWR_CFG_0 + i*4, TxPwr[i]);
3278
3279
3280                 }
3281         }
3282
3283 }
3284
3285 /*
3286         ========================================================================
3287         Routine Description:
3288                 Write TxWI for ATE mode.
3289
3290         Return Value:
3291                 None
3292         ========================================================================
3293 */
3294
3295 #ifdef RT2870
3296 static VOID ATEWriteTxWI(
3297         IN      PRTMP_ADAPTER   pAd,
3298         IN      PTXWI_STRUC     pTxWI,
3299         IN      BOOLEAN                 FRAG,
3300         IN      BOOLEAN                 InsTimestamp,
3301         IN      BOOLEAN                 AMPDU,
3302         IN      BOOLEAN                 Ack,
3303         IN      BOOLEAN                 NSeq,           // HW new a sequence.
3304         IN      UCHAR                   BASize,
3305         IN      UCHAR                   WCID,
3306         IN      ULONG                   Length,
3307         IN      UCHAR                   PID,
3308         IN      UCHAR                   MIMOps,
3309         IN      UCHAR                   Txopmode,
3310         IN      BOOLEAN                 CfAck,
3311         IN      HTTRANSMIT_SETTING      Transmit)
3312 {
3313         //
3314         // Always use Long preamble before verifiation short preamble functionality works well.
3315         // Todo: remove the following line if short preamble functionality works
3316         //
3317         OPSTATUS_CLEAR_FLAG(pAd, fOP_STATUS_SHORT_PREAMBLE_INUSED);
3318         pTxWI->FRAG= FRAG;
3319         pTxWI->TS= InsTimestamp;
3320         pTxWI->AMPDU = AMPDU;
3321
3322         pTxWI->MIMOps = PWR_ACTIVE;
3323         pTxWI->MpduDensity = 4;
3324         pTxWI->ACK = Ack;
3325         pTxWI->txop = Txopmode;
3326         pTxWI->NSEQ = NSeq;
3327         pTxWI->BAWinSize = BASize;
3328
3329         pTxWI->WirelessCliID = WCID;
3330         pTxWI->MPDUtotalByteCount = Length;
3331         pTxWI->PacketId = PID;
3332
3333         pTxWI->BW = Transmit.field.BW;
3334         pTxWI->ShortGI = Transmit.field.ShortGI;
3335         pTxWI->STBC= Transmit.field.STBC;
3336
3337         pTxWI->MCS = Transmit.field.MCS;
3338         pTxWI->PHYMODE= Transmit.field.MODE;
3339
3340 #ifdef DOT11_N_SUPPORT
3341         //
3342         // MMPS is 802.11n features. Because TxWI->MCS > 7 must be HT mode,
3343         // so need not check if it's HT rate.
3344         //
3345         if ((MIMOps == MMPS_STATIC) && (pTxWI->MCS > 7))
3346                 pTxWI->MCS = 7;
3347
3348         if ((MIMOps == MMPS_DYNAMIC) && (pTxWI->MCS > 7)) // SMPS protect 2 spatial.
3349                 pTxWI->MIMOps = 1;
3350 #endif // DOT11_N_SUPPORT //
3351
3352         pTxWI->CFACK = CfAck;
3353
3354         return;
3355 }
3356 #endif // RT2870 //
3357 /*
3358         ========================================================================
3359
3360         Routine Description:
3361                 Disable protection for ATE.
3362         ========================================================================
3363 */
3364 VOID ATEDisableAsicProtect(
3365         IN              PRTMP_ADAPTER   pAd)
3366 {
3367         PROT_CFG_STRUC  ProtCfg, ProtCfg4;
3368         UINT32 Protect[6];
3369         USHORT                  offset;
3370         UCHAR                   i;
3371         UINT32 MacReg = 0;
3372
3373         // Config ASIC RTS threshold register
3374         RTMP_IO_READ32(pAd, TX_RTS_CFG, &MacReg);
3375         MacReg &= 0xFF0000FF;
3376         MacReg |= (pAd->CommonCfg.RtsThreshold << 8);
3377         RTMP_IO_WRITE32(pAd, TX_RTS_CFG, MacReg);
3378
3379         // Initial common protection settings
3380         RTMPZeroMemory(Protect, sizeof(Protect));
3381         ProtCfg4.word = 0;
3382         ProtCfg.word = 0;
3383         ProtCfg.field.TxopAllowGF40 = 1;
3384         ProtCfg.field.TxopAllowGF20 = 1;
3385         ProtCfg.field.TxopAllowMM40 = 1;
3386         ProtCfg.field.TxopAllowMM20 = 1;
3387         ProtCfg.field.TxopAllowOfdm = 1;
3388         ProtCfg.field.TxopAllowCck = 1;
3389         ProtCfg.field.RTSThEn = 1;
3390         ProtCfg.field.ProtectNav = ASIC_SHORTNAV;
3391
3392         // Handle legacy(B/G) protection
3393         ProtCfg.field.ProtectRate = pAd->CommonCfg.RtsRate;
3394         ProtCfg.field.ProtectCtrl = 0;
3395         Protect[0] = ProtCfg.word;
3396         Protect[1] = ProtCfg.word;
3397
3398         // NO PROTECT
3399         // 1.All STAs in the BSS are 20/40 MHz HT
3400         // 2. in ai 20/40MHz BSS
3401         // 3. all STAs are 20MHz in a 20MHz BSS
3402         // Pure HT. no protection.
3403
3404         // MM20_PROT_CFG
3405         //      Reserved (31:27)
3406         //      PROT_TXOP(25:20) -- 010111
3407         //      PROT_NAV(19:18)  -- 01 (Short NAV protection)
3408         //  PROT_CTRL(17:16) -- 00 (None)
3409         //      PROT_RATE(15:0)  -- 0x4004 (OFDM 24M)
3410         Protect[2] = 0x01744004;
3411
3412         // MM40_PROT_CFG
3413         //      Reserved (31:27)
3414         //      PROT_TXOP(25:20) -- 111111
3415         //      PROT_NAV(19:18)  -- 01 (Short NAV protection)
3416         //  PROT_CTRL(17:16) -- 00 (None)
3417         //      PROT_RATE(15:0)  -- 0x4084 (duplicate OFDM 24M)
3418         Protect[3] = 0x03f44084;
3419
3420         // CF20_PROT_CFG
3421         //      Reserved (31:27)
3422         //      PROT_TXOP(25:20) -- 010111
3423         //      PROT_NAV(19:18)  -- 01 (Short NAV protection)
3424         //  PROT_CTRL(17:16) -- 00 (None)
3425         //      PROT_RATE(15:0)  -- 0x4004 (OFDM 24M)
3426         Protect[4] = 0x01744004;
3427
3428         // CF40_PROT_CFG
3429         //      Reserved (31:27)
3430         //      PROT_TXOP(25:20) -- 111111
3431         //      PROT_NAV(19:18)  -- 01 (Short NAV protection)
3432         //  PROT_CTRL(17:16) -- 00 (None)
3433         //      PROT_RATE(15:0)  -- 0x4084 (duplicate OFDM 24M)
3434         Protect[5] = 0x03f44084;
3435
3436         pAd->CommonCfg.IOTestParm.bRTSLongProtOn = FALSE;
3437
3438         offset = CCK_PROT_CFG;
3439         for (i = 0;i < 6;i++)
3440                 RTMP_IO_WRITE32(pAd, offset + i*4, Protect[i]);
3441
3442 }
3443
3444 #ifdef RT2870
3445 /*
3446         ========================================================================
3447         Routine Description:
3448                 Write TxInfo for ATE mode.
3449
3450         Return Value:
3451                 None
3452         ========================================================================
3453 */
3454 static VOID ATEWriteTxInfo(
3455         IN      PRTMP_ADAPTER   pAd,
3456         IN      PTXINFO_STRUC   pTxInfo,
3457         IN      USHORT          USBDMApktLen,
3458         IN      BOOLEAN         bWiv,
3459         IN      UCHAR                   QueueSel,
3460         IN      UCHAR                   NextValid,
3461         IN      UCHAR                   TxBurst)
3462 {
3463         pTxInfo->USBDMATxPktLen = USBDMApktLen;
3464         pTxInfo->QSEL = QueueSel;
3465
3466         if (QueueSel != FIFO_EDCA)
3467                 ATEDBGPRINT(RT_DEBUG_TRACE, ("=======> QueueSel != FIFO_EDCA<=======\n"));
3468
3469         pTxInfo->USBDMANextVLD = NextValid;
3470         pTxInfo->USBDMATxburst = TxBurst;
3471         pTxInfo->WIV = bWiv;
3472         pTxInfo->SwUseLastRound = 0;
3473         pTxInfo->rsv = 0;
3474         pTxInfo->rsv2 = 0;
3475
3476         return;
3477 }
3478 #endif // RT2870 //
3479
3480 /* There are two ways to convert Rssi */
3481 #if 1
3482 //
3483 // The way used with GET_LNA_GAIN().
3484 //
3485 CHAR ATEConvertToRssi(
3486         IN PRTMP_ADAPTER pAd,
3487         IN      CHAR    Rssi,
3488         IN  UCHAR   RssiNumber)
3489 {
3490         UCHAR   RssiOffset, LNAGain;
3491
3492         // Rssi equals to zero should be an invalid value
3493         if (Rssi == 0)
3494                 return -99;
3495
3496         LNAGain = GET_LNA_GAIN(pAd);
3497         if (pAd->LatchRfRegs.Channel > 14)
3498         {
3499                 if (RssiNumber == 0)
3500                         RssiOffset = pAd->ARssiOffset0;
3501                 else if (RssiNumber == 1)
3502                         RssiOffset = pAd->ARssiOffset1;
3503                 else
3504                         RssiOffset = pAd->ARssiOffset2;
3505         }
3506         else
3507         {
3508                 if (RssiNumber == 0)
3509                         RssiOffset = pAd->BGRssiOffset0;
3510                 else if (RssiNumber == 1)
3511                         RssiOffset = pAd->BGRssiOffset1;
3512                 else
3513                         RssiOffset = pAd->BGRssiOffset2;
3514         }
3515
3516         return (-12 - RssiOffset - LNAGain - Rssi);
3517 }
3518 #else
3519 //
3520 // The way originally used in ATE of rt2860ap.
3521 //
3522 CHAR ATEConvertToRssi(
3523         IN PRTMP_ADAPTER pAd,
3524         IN      CHAR                    Rssi,
3525         IN  UCHAR   RssiNumber)
3526 {
3527         UCHAR   RssiOffset, LNAGain;
3528
3529         // Rssi equals to zero should be an invalid value
3530         if (Rssi == 0)
3531                 return -99;
3532
3533     if (pAd->LatchRfRegs.Channel > 14)
3534     {
3535         LNAGain = pAd->ALNAGain;
3536         if (RssiNumber == 0)
3537                         RssiOffset = pAd->ARssiOffset0;
3538                 else if (RssiNumber == 1)
3539                         RssiOffset = pAd->ARssiOffset1;
3540                 else
3541                         RssiOffset = pAd->ARssiOffset2;
3542     }
3543     else
3544     {
3545         LNAGain = pAd->BLNAGain;
3546         if (RssiNumber == 0)
3547                         RssiOffset = pAd->BGRssiOffset0;
3548                 else if (RssiNumber == 1)
3549                         RssiOffset = pAd->BGRssiOffset1;
3550                 else
3551                         RssiOffset = pAd->BGRssiOffset2;
3552     }
3553
3554     return (-32 - RssiOffset + LNAGain - Rssi);
3555 }
3556 #endif /* end of #if 1 */
3557
3558 /*
3559         ========================================================================
3560
3561         Routine Description:
3562                 Set Japan filter coefficients if needed.
3563         Note:
3564                 This routine should only be called when
3565                 entering TXFRAME mode or TXCONT mode.
3566
3567         ========================================================================
3568 */
3569 static VOID SetJapanFilter(
3570         IN              PRTMP_ADAPTER   pAd)
3571 {
3572         UCHAR                   BbpData = 0;
3573
3574         //
3575         // If Channel=14 and Bandwidth=20M and Mode=CCK, set BBP R4 bit5=1
3576         // (Japan Tx filter coefficients)when (TXFRAME or TXCONT).
3577         //
3578         ATE_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R4, &BbpData);
3579
3580     if ((pAd->ate.TxWI.PHYMODE == MODE_CCK) && (pAd->ate.Channel == 14) && (pAd->ate.TxWI.BW == BW_20))
3581     {
3582         BbpData |= 0x20;    // turn on
3583         ATEDBGPRINT(RT_DEBUG_TRACE, ("SetJapanFilter!!!\n"));
3584     }
3585     else
3586     {
3587                 BbpData &= 0xdf;    // turn off
3588                 ATEDBGPRINT(RT_DEBUG_TRACE, ("ClearJapanFilter!!!\n"));
3589     }
3590
3591         ATE_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R4, BbpData);
3592 }
3593
3594 VOID ATESampleRssi(
3595         IN PRTMP_ADAPTER        pAd,
3596         IN PRXWI_STRUC          pRxWI)
3597 {
3598         /* There are two ways to collect RSSI. */
3599 #if 1
3600         //pAd->LastRxRate = (USHORT)((pRxWI->MCS) + (pRxWI->BW <<7) + (pRxWI->ShortGI <<8)+ (pRxWI->PHYMODE <<14)) ;
3601         if (pRxWI->RSSI0 != 0)
3602         {
3603                 pAd->ate.LastRssi0      = ATEConvertToRssi(pAd, (CHAR) pRxWI->RSSI0, RSSI_0);
3604                 pAd->ate.AvgRssi0X8     = (pAd->ate.AvgRssi0X8 - pAd->ate.AvgRssi0) + pAd->ate.LastRssi0;
3605                 pAd->ate.AvgRssi0       = pAd->ate.AvgRssi0X8 >> 3;
3606         }
3607         if (pRxWI->RSSI1 != 0)
3608         {
3609                 pAd->ate.LastRssi1      = ATEConvertToRssi(pAd, (CHAR) pRxWI->RSSI1, RSSI_1);
3610                 pAd->ate.AvgRssi1X8     = (pAd->ate.AvgRssi1X8 - pAd->ate.AvgRssi1) + pAd->ate.LastRssi1;
3611                 pAd->ate.AvgRssi1       = pAd->ate.AvgRssi1X8 >> 3;
3612         }
3613         if (pRxWI->RSSI2 != 0)
3614         {
3615                 pAd->ate.LastRssi2      = ATEConvertToRssi(pAd, (CHAR) pRxWI->RSSI2, RSSI_2);
3616                 pAd->ate.AvgRssi2X8     = (pAd->ate.AvgRssi2X8 - pAd->ate.AvgRssi2) + pAd->ate.LastRssi2;
3617                 pAd->ate.AvgRssi2       = pAd->ate.AvgRssi2X8 >> 3;
3618         }
3619
3620         pAd->ate.LastSNR0 = (CHAR)(pRxWI->SNR0);// CHAR ==> UCHAR ?
3621         pAd->ate.LastSNR1 = (CHAR)(pRxWI->SNR1);// CHAR ==> UCHAR ?
3622
3623         pAd->ate.NumOfAvgRssiSample ++;
3624 #else
3625         pAd->ate.LastSNR0 = (CHAR)(pRxWI->SNR0);
3626         pAd->ate.LastSNR1 = (CHAR)(pRxWI->SNR1);
3627         pAd->ate.RxCntPerSec++;
3628         pAd->ate.LastRssi0 = ATEConvertToRssi(pAd, (CHAR) pRxWI->RSSI0, RSSI_0);
3629         pAd->ate.LastRssi1 = ATEConvertToRssi(pAd, (CHAR) pRxWI->RSSI1, RSSI_1);
3630         pAd->ate.LastRssi2 = ATEConvertToRssi(pAd, (CHAR) pRxWI->RSSI2, RSSI_2);
3631         pAd->ate.AvgRssi0X8 = (pAd->ate.AvgRssi0X8 - pAd->ate.AvgRssi0) + pAd->ate.LastRssi0;
3632         pAd->ate.AvgRssi0 = pAd->ate.AvgRssi0X8 >> 3;
3633         pAd->ate.AvgRssi1X8 = (pAd->ate.AvgRssi1X8 - pAd->ate.AvgRssi1) + pAd->ate.LastRssi1;
3634         pAd->ate.AvgRssi1 = pAd->ate.AvgRssi1X8 >> 3;
3635         pAd->ate.AvgRssi2X8 = (pAd->ate.AvgRssi2X8 - pAd->ate.AvgRssi2) + pAd->ate.LastRssi2;
3636         pAd->ate.AvgRssi2 = pAd->ate.AvgRssi2X8 >> 3;
3637         pAd->ate.NumOfAvgRssiSample ++;
3638 #endif
3639 }
3640
3641 #ifdef CONFIG_STA_SUPPORT
3642 VOID RTMPStationStop(
3643     IN  PRTMP_ADAPTER   pAd)
3644 {
3645 //      BOOLEAN       Cancelled;
3646
3647     ATEDBGPRINT(RT_DEBUG_TRACE, ("==> RTMPStationStop\n"));
3648
3649         // For rx statistics, we need to keep this timer running.
3650 //      RTMPCancelTimer(&pAd->Mlme.PeriodicTimer,      &Cancelled);
3651
3652     ATEDBGPRINT(RT_DEBUG_TRACE, ("<== RTMPStationStop\n"));
3653 }
3654
3655 VOID RTMPStationStart(
3656     IN  PRTMP_ADAPTER   pAd)
3657 {
3658     ATEDBGPRINT(RT_DEBUG_TRACE, ("==> RTMPStationStart\n"));
3659         ATEDBGPRINT(RT_DEBUG_TRACE, ("<== RTMPStationStart\n"));
3660 }
3661 #endif // CONFIG_STA_SUPPORT //
3662
3663 /*
3664         ==========================================================================
3665         Description:
3666                 Setup Frame format.
3667         NOTE:
3668                 This routine should only be used in ATE mode.
3669         ==========================================================================
3670  */
3671
3672 #ifdef RT2870
3673 /*======================Start of RT2870======================*/
3674 /*                                                           */
3675 /*                                                           */
3676 static INT ATESetUpFrame(
3677         IN PRTMP_ADAPTER pAd,
3678         IN UINT32 TxIdx)
3679 {
3680         UINT j;
3681         PTX_CONTEXT     pNullContext;
3682         PUCHAR                  pDest;
3683         HTTRANSMIT_SETTING      TxHTPhyMode;
3684         PTXWI_STRUC             pTxWI;
3685         PTXINFO_STRUC           pTxInfo;
3686         UINT32                  TransferBufferLength, OrgBufferLength = 0;
3687         UCHAR                   padLen = 0;
3688 #ifdef RALINK_28xx_QA
3689         PHEADER_802_11  pHeader80211 = NULL;
3690 #endif // RALINK_28xx_QA //
3691
3692         if ((RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_RESET_IN_PROGRESS)) ||
3693                 (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_BULKOUT_RESET)) ||
3694                 (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS)) ||
3695                 (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST)))
3696         {
3697                 return -1;
3698         }
3699
3700         /* We always use QID_AC_BE and FIFO_EDCA in ATE mode. */
3701
3702         pNullContext = &(pAd->NullContext);
3703         ASSERT(pNullContext != NULL);
3704
3705         if (pNullContext->InUse == FALSE)
3706         {
3707                 // Set the in use bit
3708                 pNullContext->InUse = TRUE;
3709                 NdisZeroMemory(&(pAd->NullFrame), sizeof(HEADER_802_11));
3710
3711                 // Fill 802.11 header.
3712 #ifdef RALINK_28xx_QA
3713                 if (pAd->ate.bQATxStart == TRUE)
3714                 {
3715                         pHeader80211 = NdisMoveMemory(&(pAd->NullFrame), pAd->ate.Header, pAd->ate.HLen);
3716 //                      pDest = NdisMoveMemory(&(pAd->NullFrame), pAd->ate.Header, pAd->ate.HLen);
3717 //                      pHeader80211 = (PHEADER_802_11)pDest;
3718                 }
3719                 else
3720 #endif // RALINK_28xx_QA //
3721                 {
3722                         // Fill 802.11 header.
3723                         NdisMoveMemory(&(pAd->NullFrame), TemplateFrame, sizeof(HEADER_802_11));
3724                 }
3725 #ifdef RT_BIG_ENDIAN
3726                 RTMPFrameEndianChange(pAd, (PUCHAR)&(pAd->NullFrame), DIR_READ, FALSE);
3727 #endif // RT_BIG_ENDIAN //
3728
3729 #ifdef RALINK_28xx_QA
3730                 if (pAd->ate.bQATxStart == TRUE)
3731                 {
3732                         /* modify sequence number.... */
3733                         if (pAd->ate.TxDoneCount == 0)
3734                         {
3735                                 pAd->ate.seq = pHeader80211->Sequence;
3736                         }
3737                         else
3738                         {
3739                                 pHeader80211->Sequence = ++pAd->ate.seq;
3740                         }
3741                         /* We already got all the addr. fields from QA GUI. */
3742                 }
3743                 else
3744 #endif // RALINK_28xx_QA //
3745                 {
3746                         COPY_MAC_ADDR(pAd->NullFrame.Addr1, pAd->ate.Addr1);
3747                         COPY_MAC_ADDR(pAd->NullFrame.Addr2, pAd->ate.Addr2);
3748                         COPY_MAC_ADDR(pAd->NullFrame.Addr3, pAd->ate.Addr3);
3749                 }
3750
3751                 RTMPZeroMemory(&pAd->NullContext.TransferBuffer->field.WirelessPacket[0], TX_BUFFER_NORMSIZE);//???
3752                 pTxInfo = (PTXINFO_STRUC)&pAd->NullContext.TransferBuffer->field.WirelessPacket[0];
3753
3754 #ifdef RALINK_28xx_QA
3755                 if (pAd->ate.bQATxStart == TRUE)
3756                 {
3757                         // Avoid to exceed the range of WirelessPacket[].
3758                         ASSERT(pAd->ate.TxInfo.USBDMATxPktLen <= (MAX_FRAME_SIZE - 34/* == 2312 */));
3759                         NdisMoveMemory(pTxInfo, &(pAd->ate.TxInfo), sizeof(pAd->ate.TxInfo));
3760                 }
3761                 else
3762 #endif // RALINK_28xx_QA //
3763                 {
3764                         // Avoid to exceed the range of WirelessPacket[].
3765                         ASSERT(pAd->ate.TxLength <= (MAX_FRAME_SIZE - 34/* == 2312 */));
3766
3767                         // pTxInfo->USBDMATxPktLen will be updated to include padding later.
3768                         ATEWriteTxInfo(pAd, pTxInfo, (USHORT)(TXWI_SIZE + pAd->ate.TxLength), TRUE, EpToQueue[MGMTPIPEIDX], FALSE,  FALSE);
3769                         pTxInfo->QSEL = FIFO_EDCA;
3770                 }
3771
3772                 pTxWI = (PTXWI_STRUC)&pAd->NullContext.TransferBuffer->field.WirelessPacket[TXINFO_SIZE];
3773
3774                 // Fill TxWI.
3775                 if (pAd->ate.bQATxStart == TRUE)
3776                 {
3777                         TxHTPhyMode.field.BW = pAd->ate.TxWI.BW;
3778                         TxHTPhyMode.field.ShortGI = pAd->ate.TxWI.ShortGI;
3779                         TxHTPhyMode.field.STBC = pAd->ate.TxWI.STBC;
3780                         TxHTPhyMode.field.MCS = pAd->ate.TxWI.MCS;
3781                         TxHTPhyMode.field.MODE = pAd->ate.TxWI.PHYMODE;
3782                         ATEWriteTxWI(pAd, pTxWI, pAd->ate.TxWI.FRAG, pAd->ate.TxWI.TS, pAd->ate.TxWI.AMPDU, pAd->ate.TxWI.ACK, pAd->ate.TxWI.NSEQ,
3783                                 pAd->ate.TxWI.BAWinSize, BSSID_WCID, pAd->ate.TxWI.MPDUtotalByteCount/* include 802.11 header */, pAd->ate.TxWI.PacketId, 0, pAd->ate.TxWI.txop/*IFS_HTTXOP*/, pAd->ate.TxWI.CFACK/*FALSE*/, TxHTPhyMode);
3784                 }
3785                 else
3786                 {
3787                         TxHTPhyMode.field.BW = pAd->ate.TxWI.BW;
3788                         TxHTPhyMode.field.ShortGI = pAd->ate.TxWI.ShortGI;
3789                         TxHTPhyMode.field.STBC = 0;
3790                         TxHTPhyMode.field.MCS = pAd->ate.TxWI.MCS;
3791                         TxHTPhyMode.field.MODE = pAd->ate.TxWI.PHYMODE;
3792
3793                         ATEWriteTxWI(pAd, pTxWI,  FALSE, FALSE, FALSE, FALSE/* No ack required. */, FALSE, 0, BSSID_WCID, pAd->ate.TxLength,
3794                                 0, 0, IFS_HTTXOP, FALSE, TxHTPhyMode);// "MMPS_STATIC" instead of "MMPS_DYNAMIC" ???
3795                 }
3796
3797                 RTMPMoveMemory(&pAd->NullContext.TransferBuffer->field.WirelessPacket[TXINFO_SIZE+TXWI_SIZE], &pAd->NullFrame, sizeof(HEADER_802_11));
3798
3799                 pDest = &(pAd->NullContext.TransferBuffer->field.WirelessPacket[TXINFO_SIZE+TXWI_SIZE+sizeof(HEADER_802_11)]);
3800
3801                 // Prepare frame payload
3802 #ifdef RALINK_28xx_QA
3803                 if (pAd->ate.bQATxStart == TRUE)
3804                 {
3805                         // copy pattern
3806                         if ((pAd->ate.PLen != 0))
3807                         {
3808                                 for (j = 0; j < pAd->ate.DLen; j+=pAd->ate.PLen)
3809                                 {
3810                                         RTMPMoveMemory(pDest, pAd->ate.Pattern, pAd->ate.PLen);
3811                                         pDest += pAd->ate.PLen;
3812                                 }
3813                         }
3814                         TransferBufferLength = TXINFO_SIZE + TXWI_SIZE + pAd->ate.TxWI.MPDUtotalByteCount;
3815                 }
3816                 else
3817 #endif // RALINK_28xx_QA //
3818                 {
3819                     for (j = 0; j < (pAd->ate.TxLength - sizeof(HEADER_802_11)); j++)
3820                     {
3821                                 *pDest = 0xA5;
3822                                 pDest += 1;
3823                     }
3824                         TransferBufferLength = TXINFO_SIZE + TXWI_SIZE + pAd->ate.TxLength;
3825                 }
3826
3827 #if 1
3828                 OrgBufferLength = TransferBufferLength;
3829                 TransferBufferLength = (TransferBufferLength + 3) & (~3);
3830
3831                 // Always add 4 extra bytes at every packet.
3832                 padLen = TransferBufferLength - OrgBufferLength + 4;/* 4 == last packet padding */
3833                 ASSERT((padLen <= (RTMP_PKT_TAIL_PADDING - 4/* 4 == MaxBulkOutsize alignment padding */)));
3834
3835                 /* Now memzero all extra padding bytes. */
3836                 NdisZeroMemory(pDest, padLen);
3837                 pDest += padLen;
3838 #else
3839                 if ((TransferBufferLength % 4) == 1)
3840                 {
3841                         NdisZeroMemory(pDest, 7);
3842                         pDest += 7;
3843                         TransferBufferLength  += 3;
3844                 }
3845                 else if ((TransferBufferLength % 4) == 2)
3846                 {
3847                         NdisZeroMemory(pDest, 6);
3848                         pDest += 6;
3849                         TransferBufferLength  += 2;
3850                 }
3851                 else if ((TransferBufferLength % 4) == 3)
3852                 {
3853                         NdisZeroMemory(pDest, 5);
3854                         pDest += 5;
3855                         TransferBufferLength  += 1;
3856                 }
3857 #endif // 1 //
3858
3859                 // Update pTxInfo->USBDMATxPktLen to include padding.
3860                 pTxInfo->USBDMATxPktLen = TransferBufferLength - TXINFO_SIZE;
3861
3862                 TransferBufferLength += 4;
3863
3864                 // If TransferBufferLength is multiple of 64, add extra 4 bytes again.
3865                 if ((TransferBufferLength % pAd->BulkOutMaxPacketSize) == 0)
3866                 {
3867                         NdisZeroMemory(pDest, 4);
3868                         TransferBufferLength += 4;
3869                 }
3870
3871                 // Fill out frame length information for global Bulk out arbitor
3872                 pAd->NullContext.BulkOutSize = TransferBufferLength;
3873         }
3874 #ifdef RT_BIG_ENDIAN
3875         RTMPWIEndianChange((PUCHAR)pTxWI, TYPE_TXWI);
3876         RTMPFrameEndianChange(pAd, (((PUCHAR)pTxInfo)+TXWI_SIZE+TXINFO_SIZE), DIR_WRITE, FALSE);
3877     RTMPDescriptorEndianChange((PUCHAR)pTxInfo, TYPE_TXINFO);
3878 #endif // RT_BIG_ENDIAN //
3879         return 0;
3880 }
3881
3882 VOID ATE_RTUSBBulkOutDataPacketComplete(purbb_t pUrb, struct pt_regs *pt_regs)
3883 {
3884         PRTMP_ADAPTER           pAd;
3885         PTX_CONTEXT                 pNullContext;
3886         UCHAR                           BulkOutPipeId;
3887         NTSTATUS                        Status;
3888         unsigned long           IrqFlags;
3889         ULONG                       OldValue;
3890
3891         pNullContext = (PTX_CONTEXT)pUrb->context;
3892         pAd = pNullContext->pAd;
3893
3894
3895         // Reset Null frame context flags
3896         pNullContext->IRPPending = FALSE;
3897         pNullContext->InUse = FALSE;
3898         Status = pUrb->status;
3899
3900         // Store BulkOut PipeId
3901         BulkOutPipeId = pNullContext->BulkOutPipeId;
3902         pAd->BulkOutDataOneSecCount++;
3903
3904         if (Status == USB_ST_NOERROR)
3905         {
3906 #ifdef RALINK_28xx_QA
3907                 if ((ATE_ON(pAd)) && (pAd->ate.bQATxStart == TRUE))
3908                 {
3909                         if (pAd->ate.QID == BulkOutPipeId)
3910                         {
3911                                 // Let Rx can have a chance to break in during Tx process,
3912                                 // especially for loopback mode in QA ATE.
3913                                 // To trade off between tx performance and loopback mode integrity.
3914                                 /* Q   : Now Rx is handled by tasklet, do we still need this delay ? */
3915                                 /* Ans : Even tasklet is used, Rx/Tx < 1 if we do not delay for a while right here. */
3916                                 RTMPusecDelay(500);
3917                                 pAd->ate.TxDoneCount++;
3918                                 pAd->RalinkCounters.KickTxCount++;
3919                                 ASSERT(pAd->ate.QID == 0);
3920                                 pAd->ate.TxAc0++;
3921                         }
3922                 }
3923 #endif // RALINK_28xx_QA //
3924                 pAd->BulkOutComplete++;
3925
3926                 pAd->Counters8023.GoodTransmits++;
3927
3928                 /* Don't worry about the queue is empty or not. This function will check itself. */
3929                 RTMPDeQueuePacket(pAd, TRUE, BulkOutPipeId, MAX_TX_PROCESS);
3930
3931                 /* In 28xx, SendTxWaitQueue ==> TxSwQueue  */
3932 /*
3933                 if (pAd->SendTxWaitQueue[BulkOutPipeId].Number > 0)
3934                 {
3935                         RTMPDeQueuePacket(pAd, BulkOutPipeId);
3936                 }
3937 */
3938         }
3939         else    // STATUS_OTHER
3940         {
3941                 pAd->BulkOutCompleteOther++;
3942
3943                 ATEDBGPRINT(RT_DEBUG_ERROR, ("BulkOutDataPacket Failed STATUS_OTHER = 0x%x . \n", Status));
3944                 ATEDBGPRINT(RT_DEBUG_ERROR, (">>BulkOutReq=0x%lx, BulkOutComplete=0x%lx\n", pAd->BulkOutReq, pAd->BulkOutComplete));
3945
3946                 if ((!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_RESET_IN_PROGRESS)) &&
3947                         (!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS)) &&
3948                         (!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_NIC_NOT_EXIST)) &&
3949                         (!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_BULKOUT_RESET)))
3950                 {
3951                         RTMP_SET_FLAG(pAd, fRTMP_ADAPTER_BULKOUT_RESET);
3952                         /* In 28xx, RT_OID_USB_RESET_BULK_OUT ==> CMDTHREAD_RESET_BULK_OUT */
3953                         RTUSBEnqueueInternalCmd(pAd, CMDTHREAD_RESET_BULK_OUT, NULL, 0);
3954                         // Check
3955                         BULK_OUT_LOCK(&pAd->BulkOutLock[BulkOutPipeId], IrqFlags);
3956                         pAd->BulkOutPending[BulkOutPipeId] = FALSE;
3957                         pAd->bulkResetPipeid = BulkOutPipeId;
3958                         BULK_OUT_UNLOCK(&pAd->BulkOutLock[BulkOutPipeId], IrqFlags);
3959                         return;
3960                 }
3961         }
3962
3963
3964
3965         if (atomic_read(&pAd->BulkOutRemained) > 0)
3966         {
3967                 atomic_dec(&pAd->BulkOutRemained);
3968         }
3969
3970         // 1st - Transmit Success
3971         OldValue = pAd->WlanCounters.TransmittedFragmentCount.u.LowPart;
3972         pAd->WlanCounters.TransmittedFragmentCount.u.LowPart++;
3973
3974         if (pAd->WlanCounters.TransmittedFragmentCount.u.LowPart < OldValue)
3975         {
3976                 pAd->WlanCounters.TransmittedFragmentCount.u.HighPart++;
3977         }
3978
3979         if(((pAd->ContinBulkOut == TRUE ) ||(atomic_read(&pAd->BulkOutRemained) > 0)) && (pAd->ate.Mode & ATE_TXFRAME))
3980         {
3981                 RTUSB_SET_BULK_FLAG(pAd, fRTUSB_BULK_OUT_DATA_ATE);
3982         }
3983         else
3984         {
3985                 RTUSB_CLEAR_BULK_FLAG(pAd, fRTUSB_BULK_OUT_DATA_ATE);
3986 #ifdef RALINK_28xx_QA
3987                 pAd->ate.TxStatus = 0;
3988 #endif // RALINK_28xx_QA //
3989         }
3990
3991         BULK_OUT_LOCK(&pAd->BulkOutLock[BulkOutPipeId], IrqFlags);
3992         pAd->BulkOutPending[BulkOutPipeId] = FALSE;
3993         BULK_OUT_UNLOCK(&pAd->BulkOutLock[BulkOutPipeId], IrqFlags);
3994
3995         // Always call Bulk routine, even reset bulk.
3996         // The protection of rest bulk should be in BulkOut routine.
3997         RTUSBKickBulkOut(pAd);
3998 }
3999
4000 /*
4001         ========================================================================
4002
4003         Routine Description:
4004
4005         Arguments:
4006
4007         Return Value:
4008
4009         Note:
4010
4011         ========================================================================
4012 */
4013 VOID    ATE_RTUSBBulkOutDataPacket(
4014         IN      PRTMP_ADAPTER   pAd,
4015         IN      UCHAR                   BulkOutPipeId)
4016 {
4017         PTX_CONTEXT             pNullContext = &(pAd->NullContext);
4018         PURB                    pUrb;
4019         int                             ret = 0;
4020         unsigned long   IrqFlags;
4021
4022
4023         ASSERT(BulkOutPipeId == 0);
4024
4025         /* Build up the frame first. */
4026 //      ATESetUpFrame(pAd, 0);
4027
4028         BULK_OUT_LOCK(&pAd->BulkOutLock[BulkOutPipeId], IrqFlags);
4029
4030         if (pAd->BulkOutPending[BulkOutPipeId] == TRUE)
4031         {
4032                 BULK_OUT_UNLOCK(&pAd->BulkOutLock[BulkOutPipeId], IrqFlags);
4033                 return;
4034         }
4035
4036         pAd->BulkOutPending[BulkOutPipeId] = TRUE;
4037         BULK_OUT_UNLOCK(&pAd->BulkOutLock[BulkOutPipeId], IrqFlags);
4038
4039         // Increase Total transmit byte counter
4040         pAd->RalinkCounters.OneSecTransmittedByteCount +=  pNullContext->BulkOutSize;
4041         pAd->RalinkCounters.TransmittedByteCount +=  pNullContext->BulkOutSize;
4042
4043         // Clear ATE frame bulk out flag
4044         RTUSB_CLEAR_BULK_FLAG(pAd, fRTUSB_BULK_OUT_DATA_ATE);
4045
4046         // Init Tx context descriptor
4047         pNullContext->IRPPending = TRUE;
4048         RTUSBInitTxDesc(pAd, pNullContext, BulkOutPipeId, (usb_complete_t)ATE_RTUSBBulkOutDataPacketComplete);
4049         pUrb = pNullContext->pUrb;
4050
4051         if((ret = RTUSB_SUBMIT_URB(pUrb))!=0)
4052         {
4053                 ATEDBGPRINT(RT_DEBUG_ERROR, ("ATE_RTUSBBulkOutDataPacket: Submit Tx URB failed %d\n", ret));
4054                 return;
4055         }
4056
4057         pAd->BulkOutReq++;
4058         return;
4059
4060 }
4061
4062 /*
4063         ========================================================================
4064
4065         Routine Description:
4066
4067         Arguments:
4068
4069         Return Value:
4070
4071         Note:
4072
4073         ========================================================================
4074 */
4075 VOID    ATE_RTUSBCancelPendingBulkInIRP(
4076         IN      PRTMP_ADAPTER   pAd)
4077 {
4078         PRX_CONTEXT             pRxContext;
4079         UINT                    i;
4080
4081         ATEDBGPRINT(RT_DEBUG_TRACE, ("--->ATE_RTUSBCancelPendingBulkInIRP\n"));
4082 #if 1
4083         for ( i = 0; i < (RX_RING_SIZE); i++)
4084         {
4085                 pRxContext = &(pAd->RxContext[i]);
4086                 if(pRxContext->IRPPending == TRUE)
4087                 {
4088                         RTUSB_UNLINK_URB(pRxContext->pUrb);
4089                         pRxContext->IRPPending = FALSE;
4090                         pRxContext->InUse = FALSE;
4091                         //NdisInterlockedDecrement(&pAd->PendingRx);
4092                         //pAd->PendingRx--;
4093                 }
4094         }
4095 #else
4096         for ( i = 0; i < (RX_RING_SIZE); i++)
4097         {
4098                 pRxContext = &(pAd->RxContext[i]);
4099                 if(atomic_read(&pRxContext->IrpLock) == IRPLOCK_CANCELABLE)
4100                 {
4101                         RTUSB_UNLINK_URB(pRxContext->pUrb);
4102                 }
4103                 InterlockedExchange(&pRxContext->IrpLock, IRPLOCK_CANCE_START);
4104         }
4105 #endif // 1 //
4106         ATEDBGPRINT(RT_DEBUG_TRACE, ("<---ATE_RTUSBCancelPendingBulkInIRP\n"));
4107         return;
4108 }
4109 #endif // RT2870 //
4110
4111 VOID rt_ee_read_all(PRTMP_ADAPTER pAd, USHORT *Data)
4112 {
4113         USHORT i;
4114         USHORT value;
4115
4116         for (i = 0 ; i < EEPROM_SIZE/2 ; )
4117         {
4118                 /* "value" is expecially for some compilers... */
4119                 RT28xx_EEPROM_READ16(pAd, i*2, value);
4120                 Data[i] = value;
4121                 i++;
4122         }
4123 }
4124
4125 VOID rt_ee_write_all(PRTMP_ADAPTER pAd, USHORT *Data)
4126 {
4127         USHORT i;
4128         USHORT value;
4129
4130         for (i = 0 ; i < EEPROM_SIZE/2 ; )
4131         {
4132                 /* "value" is expecially for some compilers... */
4133                 value = Data[i];
4134                 RT28xx_EEPROM_WRITE16(pAd, i*2, value);
4135                 i ++;
4136         }
4137 }
4138 #ifdef RALINK_28xx_QA
4139 VOID ATE_QA_Statistics(
4140         IN PRTMP_ADAPTER                        pAd,
4141         IN PRXWI_STRUC                          pRxWI,
4142         IN PRT28XX_RXD_STRUC            pRxD,
4143         IN PHEADER_802_11                       pHeader)
4144 {
4145         // update counter first
4146         if (pHeader != NULL)
4147         {
4148                 if (pHeader->FC.Type == BTYPE_DATA)
4149                 {
4150                         if (pRxD->U2M)
4151                                 pAd->ate.U2M++;
4152                         else
4153                                 pAd->ate.OtherData++;
4154                 }
4155                 else if (pHeader->FC.Type == BTYPE_MGMT)
4156                 {
4157                         if (pHeader->FC.SubType == SUBTYPE_BEACON)
4158                                 pAd->ate.Beacon++;
4159                         else
4160                                 pAd->ate.OtherCount++;
4161                 }
4162                 else if (pHeader->FC.Type == BTYPE_CNTL)
4163                 {
4164                         pAd->ate.OtherCount++;
4165                 }
4166         }
4167         pAd->ate.RSSI0 = pRxWI->RSSI0;
4168         pAd->ate.RSSI1 = pRxWI->RSSI1;
4169         pAd->ate.RSSI2 = pRxWI->RSSI2;
4170         pAd->ate.SNR0 = pRxWI->SNR0;
4171         pAd->ate.SNR1 = pRxWI->SNR1;
4172 }
4173
4174 /* command id with Cmd Type == 0x0008(for 28xx)/0x0005(for iNIC) */
4175 #define RACFG_CMD_RF_WRITE_ALL          0x0000
4176 #define RACFG_CMD_E2PROM_READ16         0x0001
4177 #define RACFG_CMD_E2PROM_WRITE16        0x0002
4178 #define RACFG_CMD_E2PROM_READ_ALL       0x0003
4179 #define RACFG_CMD_E2PROM_WRITE_ALL      0x0004
4180 #define RACFG_CMD_IO_READ                       0x0005
4181 #define RACFG_CMD_IO_WRITE                      0x0006
4182 #define RACFG_CMD_IO_READ_BULK          0x0007
4183 #define RACFG_CMD_BBP_READ8                     0x0008
4184 #define RACFG_CMD_BBP_WRITE8            0x0009
4185 #define RACFG_CMD_BBP_READ_ALL          0x000a
4186 #define RACFG_CMD_GET_COUNTER           0x000b
4187 #define RACFG_CMD_CLEAR_COUNTER         0x000c
4188
4189 #define RACFG_CMD_RSV1                          0x000d
4190 #define RACFG_CMD_RSV2                          0x000e
4191 #define RACFG_CMD_RSV3                          0x000f
4192
4193 #define RACFG_CMD_TX_START                      0x0010
4194 #define RACFG_CMD_GET_TX_STATUS         0x0011
4195 #define RACFG_CMD_TX_STOP                       0x0012
4196 #define RACFG_CMD_RX_START                      0x0013
4197 #define RACFG_CMD_RX_STOP                       0x0014
4198 #define RACFG_CMD_GET_NOISE_LEVEL       0x0015
4199
4200 #define RACFG_CMD_ATE_START                     0x0080
4201 #define RACFG_CMD_ATE_STOP                      0x0081
4202
4203 #define RACFG_CMD_ATE_START_TX_CARRIER          0x0100
4204 #define RACFG_CMD_ATE_START_TX_CONT                     0x0101
4205 #define RACFG_CMD_ATE_START_TX_FRAME            0x0102
4206 #define RACFG_CMD_ATE_SET_BW                0x0103
4207 #define RACFG_CMD_ATE_SET_TX_POWER0             0x0104
4208 #define RACFG_CMD_ATE_SET_TX_POWER1                     0x0105
4209 #define RACFG_CMD_ATE_SET_FREQ_OFFSET           0x0106
4210 #define RACFG_CMD_ATE_GET_STATISTICS            0x0107
4211 #define RACFG_CMD_ATE_RESET_COUNTER                     0x0108
4212 #define RACFG_CMD_ATE_SEL_TX_ANTENNA            0x0109
4213 #define RACFG_CMD_ATE_SEL_RX_ANTENNA            0x010a
4214 #define RACFG_CMD_ATE_SET_PREAMBLE                      0x010b
4215 #define RACFG_CMD_ATE_SET_CHANNEL                       0x010c
4216 #define RACFG_CMD_ATE_SET_ADDR1                         0x010d
4217 #define RACFG_CMD_ATE_SET_ADDR2                         0x010e
4218 #define RACFG_CMD_ATE_SET_ADDR3                         0x010f
4219 #define RACFG_CMD_ATE_SET_RATE                          0x0110
4220 #define RACFG_CMD_ATE_SET_TX_FRAME_LEN          0x0111
4221 #define RACFG_CMD_ATE_SET_TX_FRAME_COUNT        0x0112
4222 #define RACFG_CMD_ATE_START_RX_FRAME            0x0113
4223 #define RACFG_CMD_ATE_E2PROM_READ_BULK  0x0114
4224 #define RACFG_CMD_ATE_E2PROM_WRITE_BULK 0x0115
4225 #define RACFG_CMD_ATE_IO_WRITE_BULK             0x0116
4226 #define RACFG_CMD_ATE_BBP_READ_BULK             0x0117
4227 #define RACFG_CMD_ATE_BBP_WRITE_BULK    0x0118
4228 #define RACFG_CMD_ATE_RF_READ_BULK              0x0119
4229 #define RACFG_CMD_ATE_RF_WRITE_BULK             0x011a
4230
4231
4232
4233 #define A2Hex(_X, _p)                           \
4234 {                                                                       \
4235         UCHAR *p;                                               \
4236         _X = 0;                                                 \
4237         p = _p;                                                 \
4238         while (((*p >= 'a') && (*p <= 'f')) || ((*p >= 'A') && (*p <= 'F')) || ((*p >= '0') && (*p <= '9')))            \
4239         {                                                                                               \
4240                 if ((*p >= 'a') && (*p <= 'f'))                         \
4241                         _X = _X * 16 + *p - 87;                                 \
4242                 else if ((*p >= 'A') && (*p <= 'F'))            \
4243                         _X = _X * 16 + *p - 55;                                 \
4244                 else if ((*p >= '0') && (*p <= '9'))            \
4245                         _X = _X * 16 + *p - 48;                                 \
4246                 p++;                                                                            \
4247         }                                                                                               \
4248 }
4249
4250
4251 static VOID memcpy_exl(PRTMP_ADAPTER pAd, UCHAR *dst, UCHAR *src, ULONG len);
4252 static VOID memcpy_exs(PRTMP_ADAPTER pAd, UCHAR *dst, UCHAR *src, ULONG len);
4253 static VOID RTMP_IO_READ_BULK(PRTMP_ADAPTER pAd, UCHAR *dst, UCHAR *src, UINT32 len);
4254
4255 #define LEN_OF_ARG 16
4256
4257 VOID RtmpDoAte(
4258         IN      PRTMP_ADAPTER   pAdapter,
4259         IN      struct iwreq    *wrq)
4260 {
4261         unsigned short Command_Id;
4262         struct ate_racfghdr *pRaCfg;
4263         INT     Status = NDIS_STATUS_SUCCESS;
4264
4265
4266
4267         if((pRaCfg = kmalloc(sizeof(struct ate_racfghdr), GFP_KERNEL)) == NULL)
4268         {
4269                 Status = -EINVAL;
4270                 return;
4271         }
4272
4273         NdisZeroMemory(pRaCfg, sizeof(struct ate_racfghdr));
4274
4275     if (copy_from_user((PUCHAR)pRaCfg, wrq->u.data.pointer, wrq->u.data.length))
4276         {
4277                 Status = -EFAULT;
4278                 kfree(pRaCfg);
4279                 return;
4280         }
4281
4282
4283         Command_Id = ntohs(pRaCfg->command_id);
4284
4285         ATEDBGPRINT(RT_DEBUG_TRACE,("\n%s: Command_Id = 0x%04x !\n", __func__, Command_Id));
4286
4287         switch (Command_Id)
4288         {
4289                 // We will get this command when QA starts.
4290                 case RACFG_CMD_ATE_START:
4291                         {
4292                                 ATEDBGPRINT(RT_DEBUG_TRACE,("RACFG_CMD_ATE_START\n"));
4293
4294                                 // prepare feedback as soon as we can to avoid QA timeout.
4295                                 pRaCfg->length = htons(2);
4296                                 pRaCfg->status = htons(0);
4297
4298                                 wrq->u.data.length = sizeof(pRaCfg->magic_no) + sizeof(pRaCfg->command_type)
4299                                                                         + sizeof(pRaCfg->command_id) + sizeof(pRaCfg->length)
4300                                                                         + sizeof(pRaCfg->sequence) + ntohs(pRaCfg->length);
4301
4302                                 ATEDBGPRINT(RT_DEBUG_TRACE, ("wrq->u.data.length = %d\n", wrq->u.data.length));
4303
4304                 if (copy_to_user(wrq->u.data.pointer, pRaCfg, wrq->u.data.length))
4305                 {
4306                         ATEDBGPRINT(RT_DEBUG_TRACE, ("copy_to_user() fail in case RACFG_CMD_ATE_START\n"));
4307                     Status = -EFAULT;
4308                 }
4309                                 else
4310                                 {
4311                         ATEDBGPRINT(RT_DEBUG_TRACE, ("RACFG_CMD_ATE_START is done !\n"));
4312                                 }
4313                                 Set_ATE_Proc(pAdapter, "ATESTART");
4314                         }
4315                         break;
4316
4317                 // We will get this command either QA is closed or ated is killed by user.
4318                 case RACFG_CMD_ATE_STOP:
4319                         {
4320                                 INT32 ret;
4321
4322                                 ATEDBGPRINT(RT_DEBUG_TRACE,("RACFG_CMD_ATE_STOP\n"));
4323
4324                                 // Distinguish this command came from QA(via ated)
4325                                 // or ate daemon according to the existence of pid in payload.
4326                                 // No need to prepare feedback if this cmd came directly from ate daemon.
4327                                 pRaCfg->length = ntohs(pRaCfg->length);
4328
4329                                 if (pRaCfg->length == sizeof(pAdapter->ate.AtePid))
4330                                 {
4331                                         // This command came from QA.
4332                                         // Get the pid of ATE daemon.
4333                                         memcpy((UCHAR *)&pAdapter->ate.AtePid,
4334                                                 (&pRaCfg->data[0]) - 2/* == &(pRaCfg->status) */,
4335                                                 sizeof(pAdapter->ate.AtePid));
4336
4337                                         // prepare feedback as soon as we can to avoid QA timeout.
4338                                         pRaCfg->length = htons(2);
4339                                         pRaCfg->status = htons(0);
4340
4341                                         wrq->u.data.length = sizeof(pRaCfg->magic_no) + sizeof(pRaCfg->command_type)
4342                                                                                 + sizeof(pRaCfg->command_id) + sizeof(pRaCfg->length)
4343                                                                                 + sizeof(pRaCfg->sequence) + ntohs(pRaCfg->length);
4344
4345                                         ATEDBGPRINT(RT_DEBUG_TRACE, ("wrq->u.data.length = %d\n", wrq->u.data.length));
4346
4347                         if (copy_to_user(wrq->u.data.pointer, pRaCfg, wrq->u.data.length))
4348                         {
4349                                 ATEDBGPRINT(RT_DEBUG_ERROR, ("copy_to_user() fail in case RACFG_CMD_ATE_STOP\n"));
4350                             Status = -EFAULT;
4351                         }
4352
4353                                         //
4354                                         // kill ATE daemon when leaving ATE mode.
4355                                         // We must kill ATE daemon first before setting ATESTOP,
4356                                         // or Microsoft will report sth. wrong.
4357                                         ret = kill_proc(pAdapter->ate.AtePid, SIGTERM, 1);
4358                                         if (ret)
4359                                         {
4360                                                 ATEDBGPRINT(RT_DEBUG_ERROR, ("%s: unable to signal thread\n", pAdapter->net_dev->name));
4361                                         }
4362                                 }
4363
4364                                 // AP might have in ATE_STOP mode due to cmd from QA.
4365                                 if (ATE_ON(pAdapter))
4366                                 {
4367                                         // Someone has killed ate daemon while QA GUI is still open.
4368                                         Set_ATE_Proc(pAdapter, "ATESTOP");
4369                                         ATEDBGPRINT(RT_DEBUG_TRACE, ("RACFG_CMD_AP_START is done !\n"));
4370                                 }
4371                         }
4372                         break;
4373
4374                 case RACFG_CMD_RF_WRITE_ALL:
4375                         {
4376                                 UINT32 R1, R2, R3, R4;
4377                                 USHORT channel;
4378
4379                                 memcpy(&R1, pRaCfg->data-2, 4);
4380                                 memcpy(&R2, pRaCfg->data+2, 4);
4381                                 memcpy(&R3, pRaCfg->data+6, 4);
4382                                 memcpy(&R4, pRaCfg->data+10, 4);
4383                                 memcpy(&channel, pRaCfg->data+14, 2);
4384
4385                                 pAdapter->LatchRfRegs.R1 = ntohl(R1);
4386                                 pAdapter->LatchRfRegs.R2 = ntohl(R2);
4387                                 pAdapter->LatchRfRegs.R3 = ntohl(R3);
4388                                 pAdapter->LatchRfRegs.R4 = ntohl(R4);
4389                                 pAdapter->LatchRfRegs.Channel = ntohs(channel);
4390
4391                                 RTMP_RF_IO_WRITE32(pAdapter, pAdapter->LatchRfRegs.R1);
4392                                 RTMP_RF_IO_WRITE32(pAdapter, pAdapter->LatchRfRegs.R2);
4393                                 RTMP_RF_IO_WRITE32(pAdapter, pAdapter->LatchRfRegs.R3);
4394                                 RTMP_RF_IO_WRITE32(pAdapter, pAdapter->LatchRfRegs.R4);
4395
4396                                 // prepare feedback
4397                                 pRaCfg->length = htons(2);
4398                                 pRaCfg->status = htons(0);
4399
4400                                 wrq->u.data.length = sizeof(pRaCfg->magic_no) + sizeof(pRaCfg->command_type)
4401                                                                         + sizeof(pRaCfg->command_id) + sizeof(pRaCfg->length)
4402                                                                         + sizeof(pRaCfg->sequence) + ntohs(pRaCfg->length);
4403
4404                                 ATEDBGPRINT(RT_DEBUG_TRACE, ("wrq->u.data.length = %d\n", wrq->u.data.length));
4405                 if (copy_to_user(wrq->u.data.pointer, pRaCfg, wrq->u.data.length))
4406                 {
4407                         ATEDBGPRINT(RT_DEBUG_ERROR, ("copy_to_user() fail in case RACFG_CMD_RF_WRITE_ALL\n"));
4408                     Status = -EFAULT;
4409                 }
4410                                 else
4411                                 {
4412                         ATEDBGPRINT(RT_DEBUG_TRACE, ("RACFG_CMD_RF_WRITE_ALL is done !\n"));
4413                                 }
4414                         }
4415             break;
4416
4417                 case RACFG_CMD_E2PROM_READ16:
4418                         {
4419                                 USHORT  offset, value, tmp;
4420
4421                                 offset = ntohs(pRaCfg->status);
4422                                 /* "tmp" is expecially for some compilers... */
4423                                 RT28xx_EEPROM_READ16(pAdapter, offset, tmp);
4424                                 value = tmp;
4425                                 value = htons(value);
4426
4427                                 ATEDBGPRINT(RT_DEBUG_TRACE,("EEPROM Read offset = 0x%04x, value = 0x%04x\n", offset, value));
4428
4429                                 // prepare feedback
4430                                 pRaCfg->length = htons(4);
4431                                 pRaCfg->status = htons(0);
4432                                 memcpy(pRaCfg->data, &value, 2);
4433
4434                                 wrq->u.data.length = sizeof(pRaCfg->magic_no) + sizeof(pRaCfg->command_type)
4435                                                                         + sizeof(pRaCfg->command_id) + sizeof(pRaCfg->length)
4436                                                                         + sizeof(pRaCfg->sequence) + ntohs(pRaCfg->length);
4437
4438                                 ATEDBGPRINT(RT_DEBUG_TRACE, ("sizeof(struct ate_racfghdr) = %d\n", sizeof(struct ate_racfghdr)));
4439                                 ATEDBGPRINT(RT_DEBUG_TRACE, ("wrq->u.data.length = %d\n", wrq->u.data.length));
4440
4441                 if (copy_to_user(wrq->u.data.pointer, pRaCfg, wrq->u.data.length))
4442                 {
4443                         ATEDBGPRINT(RT_DEBUG_ERROR, ("copy_to_user() fail in case RACFG_CMD_E2PROM_READ16\n"));
4444                     Status = -EFAULT;
4445                 }
4446                                 else
4447                                 {
4448                         ATEDBGPRINT(RT_DEBUG_TRACE, ("RACFG_CMD_E2PROM_READ16 is done !\n"));
4449                                 }
4450                 }
4451                         break;
4452
4453                 case RACFG_CMD_E2PROM_WRITE16:
4454                         {
4455                                 USHORT  offset, value;
4456
4457                                 offset = ntohs(pRaCfg->status);
4458                                 memcpy(&value, pRaCfg->data, 2);
4459                                 value = ntohs(value);
4460                                 RT28xx_EEPROM_WRITE16(pAdapter, offset, value);
4461
4462                                 // prepare feedback
4463                                 pRaCfg->length = htons(2);
4464                                 pRaCfg->status = htons(0);
4465                                 wrq->u.data.length = sizeof(pRaCfg->magic_no) + sizeof(pRaCfg->command_type)
4466                                                                         + sizeof(pRaCfg->command_id) + sizeof(pRaCfg->length)
4467                                                                         + sizeof(pRaCfg->sequence) + ntohs(pRaCfg->length);
4468
4469                 if (copy_to_user(wrq->u.data.pointer, pRaCfg, wrq->u.data.length))
4470                 {
4471                         ATEDBGPRINT(RT_DEBUG_ERROR, ("copy_to_user() fail in case RACFG_CMD_E2PROM_WRITE16\n"));
4472                     Status = -EFAULT;
4473                 }
4474                                 else
4475                                 {
4476                         ATEDBGPRINT(RT_DEBUG_TRACE, ("RACFG_CMD_E2PROM_WRITE16 is done !\n"));
4477                                 }
4478                         }
4479                         break;
4480
4481                 case RACFG_CMD_E2PROM_READ_ALL:
4482                         {
4483                                 USHORT buffer[EEPROM_SIZE/2];
4484
4485                                 rt_ee_read_all(pAdapter,(USHORT *)buffer);
4486                                 memcpy_exs(pAdapter, pRaCfg->data, (UCHAR *)buffer, EEPROM_SIZE);
4487
4488                                 // prepare feedback
4489                                 pRaCfg->length = htons(2+EEPROM_SIZE);
4490                                 pRaCfg->status = htons(0);
4491                                 wrq->u.data.length = sizeof(pRaCfg->magic_no) + sizeof(pRaCfg->command_type)
4492                                                                         + sizeof(pRaCfg->command_id) + sizeof(pRaCfg->length)
4493                                                                         + sizeof(pRaCfg->sequence) + ntohs(pRaCfg->length);
4494
4495                 if (copy_to_user(wrq->u.data.pointer, pRaCfg, wrq->u.data.length))
4496                 {
4497                         ATEDBGPRINT(RT_DEBUG_ERROR, ("copy_to_user() fail in case RACFG_CMD_E2PROM_READ_ALL\n"));
4498                     Status = -EFAULT;
4499                 }
4500                                 else
4501                                 {
4502                         ATEDBGPRINT(RT_DEBUG_TRACE, ("RACFG_CMD_E2PROM_READ_ALL is done !\n"));
4503                                 }
4504                         }
4505                         break;
4506
4507                 case RACFG_CMD_E2PROM_WRITE_ALL:
4508                         {
4509                                 USHORT buffer[EEPROM_SIZE/2];
4510
4511                                 NdisZeroMemory((UCHAR *)buffer, EEPROM_SIZE);
4512                                 memcpy_exs(pAdapter, (UCHAR *)buffer, (UCHAR *)&pRaCfg->status, EEPROM_SIZE);
4513                                 rt_ee_write_all(pAdapter,(USHORT *)buffer);
4514
4515                                 // prepare feedback
4516                                 pRaCfg->length = htons(2);
4517                                 pRaCfg->status = htons(0);
4518                                 wrq->u.data.length = sizeof(pRaCfg->magic_no) + sizeof(pRaCfg->command_type)
4519                                                                         + sizeof(pRaCfg->command_id) + sizeof(pRaCfg->length)
4520                                                                         + sizeof(pRaCfg->sequence) + ntohs(pRaCfg->length);
4521
4522                 if (copy_to_user(wrq->u.data.pointer, pRaCfg, wrq->u.data.length))
4523                 {
4524                         ATEDBGPRINT(RT_DEBUG_ERROR, ("copy_to_user() fail in case RACFG_CMD_E2PROM_WRITE_ALL\n"));
4525                     Status = -EFAULT;
4526                 }
4527                                 else
4528                                 {
4529                         ATEDBGPRINT(RT_DEBUG_ERROR, ("RACFG_CMD_E2PROM_WRITE_ALL is done !\n"));
4530                                 }
4531
4532                         }
4533                         break;
4534
4535                 case RACFG_CMD_IO_READ:
4536                         {
4537                                 UINT32  offset;
4538                                 UINT32  value;
4539
4540                                 memcpy(&offset, &pRaCfg->status, 4);
4541                                 offset = ntohl(offset);
4542
4543                                 // We do not need the base address.
4544                                 // So just extract the offset out.
4545                                 offset &= 0x0000FFFF;
4546                                 RTMP_IO_READ32(pAdapter, offset, &value);
4547                                 value = htonl(value);
4548
4549                                 // prepare feedback
4550                                 pRaCfg->length = htons(6);
4551                                 pRaCfg->status = htons(0);
4552                                 memcpy(pRaCfg->data, &value, 4);
4553
4554                                 wrq->u.data.length = sizeof(pRaCfg->magic_no) + sizeof(pRaCfg->command_type)
4555                                                                         + sizeof(pRaCfg->command_id) + sizeof(pRaCfg->length)
4556                                                                         + sizeof(pRaCfg->sequence) + ntohs(pRaCfg->length);
4557
4558                 if (copy_to_user(wrq->u.data.pointer, pRaCfg, wrq->u.data.length))
4559                 {
4560                         ATEDBGPRINT(RT_DEBUG_ERROR, ("copy_to_user() fail in case RACFG_CMD_IO_READ\n"));
4561                     Status = -EFAULT;
4562                 }
4563                                 else
4564                                 {
4565                         ATEDBGPRINT(RT_DEBUG_TRACE, ("RACFG_CMD_IO_READ is done !\n"));
4566                                 }
4567                         }
4568                         break;
4569
4570                 case RACFG_CMD_IO_WRITE:
4571                         {
4572                                 UINT32  offset, value;
4573
4574                                 memcpy(&offset, pRaCfg->data-2, 4);
4575                                 memcpy(&value, pRaCfg->data+2, 4);
4576
4577                                 offset = ntohl(offset);
4578
4579                                 // We do not need the base address.
4580                                 // So just extract out the offset.
4581                                 offset &= 0x0000FFFF;
4582                                 value = ntohl(value);
4583                                 ATEDBGPRINT(RT_DEBUG_TRACE,("RACFG_CMD_IO_WRITE: offset = %x, value = %x\n", offset, value));
4584                                 RTMP_IO_WRITE32(pAdapter, offset, value);
4585
4586                                 // prepare feedback
4587                                 pRaCfg->length = htons(2);
4588                                 pRaCfg->status = htons(0);
4589                                 wrq->u.data.length = sizeof(pRaCfg->magic_no) + sizeof(pRaCfg->command_type)
4590                                                                         + sizeof(pRaCfg->command_id) + sizeof(pRaCfg->length)
4591                                                                         + sizeof(pRaCfg->sequence) + ntohs(pRaCfg->length);
4592
4593                 if (copy_to_user(wrq->u.data.pointer, pRaCfg, wrq->u.data.length))
4594                 {
4595                         ATEDBGPRINT(RT_DEBUG_ERROR, ("copy_to_user() fail in case RACFG_CMD_IO_WRITE\n"));
4596                     Status = -EFAULT;
4597                 }
4598                                 else
4599                                 {
4600                         ATEDBGPRINT(RT_DEBUG_TRACE, ("RACFG_CMD_IO_WRITE is done !\n"));
4601                                 }
4602                         }
4603                         break;
4604
4605                 case RACFG_CMD_IO_READ_BULK:
4606                         {
4607                                 UINT32  offset;
4608                                 USHORT  len;
4609
4610                                 memcpy(&offset, &pRaCfg->status, 4);
4611                                 offset = ntohl(offset);
4612
4613                                 // We do not need the base address.
4614                                 // So just extract the offset.
4615                                 offset &= 0x0000FFFF;
4616                                 memcpy(&len, pRaCfg->data+2, 2);
4617                                 len = ntohs(len);
4618
4619                                 if (len > 371)
4620                                 {
4621                                         ATEDBGPRINT(RT_DEBUG_TRACE,("len is too large, make it smaller\n"));
4622                                         pRaCfg->length = htons(2);
4623                                         pRaCfg->status = htons(1);
4624                                         break;
4625                                 }
4626
4627                                 RTMP_IO_READ_BULK(pAdapter, pRaCfg->data, (UCHAR *)offset, len*4);// unit in four bytes
4628
4629                                 // prepare feedback
4630                                 pRaCfg->length = htons(2+len*4);// unit in four bytes
4631                                 pRaCfg->status = htons(0);
4632                                 wrq->u.data.length = sizeof(pRaCfg->magic_no) + sizeof(pRaCfg->command_type)
4633                                                                         + sizeof(pRaCfg->command_id) + sizeof(pRaCfg->length)
4634                                                                         + sizeof(pRaCfg->sequence) + ntohs(pRaCfg->length);
4635
4636                 if (copy_to_user(wrq->u.data.pointer, pRaCfg, wrq->u.data.length))
4637                 {
4638                         ATEDBGPRINT(RT_DEBUG_ERROR, ("copy_to_user() fail in case RACFG_CMD_IO_READ_BULK\n"));
4639                     Status = -EFAULT;
4640                 }
4641                                 else
4642                                 {
4643                         ATEDBGPRINT(RT_DEBUG_TRACE, ("RACFG_CMD_IO_READ_BULK is done !\n"));
4644                                 }
4645                         }
4646                         break;
4647
4648                 case RACFG_CMD_BBP_READ8:
4649                         {
4650                                 USHORT  offset;
4651                                 UCHAR   value;
4652
4653                                 value = 0;
4654                                 offset = ntohs(pRaCfg->status);
4655
4656                                 if (ATE_ON(pAdapter))
4657                                 {
4658                                         ATE_BBP_IO_READ8_BY_REG_ID(pAdapter, offset,  &value);
4659                                 }
4660                                 else
4661                                 {
4662                                         RTMP_BBP_IO_READ8_BY_REG_ID(pAdapter, offset,  &value);
4663                                 }
4664                                 // prepare feedback
4665                                 pRaCfg->length = htons(3);
4666                                 pRaCfg->status = htons(0);
4667                                 pRaCfg->data[0] = value;
4668
4669                                 ATEDBGPRINT(RT_DEBUG_TRACE,("BBP value = %x\n", value));
4670
4671                                 wrq->u.data.length = sizeof(pRaCfg->magic_no) + sizeof(pRaCfg->command_type)
4672                                                                         + sizeof(pRaCfg->command_id) + sizeof(pRaCfg->length)
4673                                                                         + sizeof(pRaCfg->sequence) + ntohs(pRaCfg->length);
4674
4675                 if (copy_to_user(wrq->u.data.pointer, pRaCfg, wrq->u.data.length))
4676                 {
4677                         ATEDBGPRINT(RT_DEBUG_ERROR, ("copy_to_user() fail in case RACFG_CMD_BBP_READ8\n"));
4678                     Status = -EFAULT;
4679                 }
4680                                 else
4681                                 {
4682                         ATEDBGPRINT(RT_DEBUG_TRACE, ("RACFG_CMD_BBP_READ8 is done !\n"));
4683                                 }
4684                         }
4685                         break;
4686                 case RACFG_CMD_BBP_WRITE8:
4687                         {
4688                                 USHORT  offset;
4689                                 UCHAR   value;
4690
4691                                 offset = ntohs(pRaCfg->status);
4692                                 memcpy(&value, pRaCfg->data, 1);
4693
4694                                 if (ATE_ON(pAdapter))
4695                                 {
4696                                         ATE_BBP_IO_WRITE8_BY_REG_ID(pAdapter, offset,  value);
4697                                 }
4698                                 else
4699                                 {
4700                                         RTMP_BBP_IO_WRITE8_BY_REG_ID(pAdapter, offset,  value);
4701                                 }
4702
4703                                 if ((offset == BBP_R1) || (offset == BBP_R3))
4704                                 {
4705                                         SyncTxRxConfig(pAdapter, offset, value);
4706                                 }
4707
4708                                 // prepare feedback
4709                                 pRaCfg->length = htons(2);
4710                                 pRaCfg->status = htons(0);
4711                                 wrq->u.data.length = sizeof(pRaCfg->magic_no) + sizeof(pRaCfg->command_type)
4712                                                                         + sizeof(pRaCfg->command_id) + sizeof(pRaCfg->length)
4713                                                                         + sizeof(pRaCfg->sequence) + ntohs(pRaCfg->length);
4714
4715                 if (copy_to_user(wrq->u.data.pointer, pRaCfg, wrq->u.data.length))
4716                 {
4717                         ATEDBGPRINT(RT_DEBUG_ERROR, ("copy_to_user() fail in case RACFG_CMD_BBP_WRITE8\n"));
4718                     Status = -EFAULT;
4719                 }
4720                                 else
4721                                 {
4722                         ATEDBGPRINT(RT_DEBUG_TRACE, ("RACFG_CMD_BBP_WRITE8 is done !\n"));
4723                                 }
4724                         }
4725                         break;
4726
4727                 case RACFG_CMD_BBP_READ_ALL:
4728                         {
4729                                 USHORT j;
4730
4731                                 for (j = 0; j < 137; j++)
4732                                 {
4733                                         pRaCfg->data[j] = 0;
4734
4735                                         if (ATE_ON(pAdapter))
4736                                         {
4737                                                 ATE_BBP_IO_READ8_BY_REG_ID(pAdapter, j,  &pRaCfg->data[j]);
4738                                         }
4739                                         else
4740                                         {
4741                                                 RTMP_BBP_IO_READ8_BY_REG_ID(pAdapter, j,  &pRaCfg->data[j]);
4742                                         }
4743                                 }
4744
4745                                 // prepare feedback
4746                                 pRaCfg->length = htons(2+137);
4747                                 pRaCfg->status = htons(0);
4748
4749                                 wrq->u.data.length = sizeof(pRaCfg->magic_no) + sizeof(pRaCfg->command_type)
4750                                                                         + sizeof(pRaCfg->command_id) + sizeof(pRaCfg->length)
4751                                                                         + sizeof(pRaCfg->sequence) + ntohs(pRaCfg->length);
4752
4753                 if (copy_to_user(wrq->u.data.pointer, pRaCfg, wrq->u.data.length))
4754                 {
4755                         ATEDBGPRINT(RT_DEBUG_ERROR, ("copy_to_user() fail in case RACFG_CMD_BBP_READ_ALL\n"));
4756                     Status = -EFAULT;
4757                 }
4758                                 else
4759                                 {
4760                         ATEDBGPRINT(RT_DEBUG_TRACE, ("RACFG_CMD_BBP_READ_ALL is done !\n"));
4761                                 }
4762                         }
4763
4764                         break;
4765
4766                 case RACFG_CMD_ATE_E2PROM_READ_BULK:
4767                 {
4768                         USHORT offset;
4769                         USHORT len;
4770                         USHORT buffer[EEPROM_SIZE/2];
4771
4772                         offset = ntohs(pRaCfg->status);
4773                         memcpy(&len, pRaCfg->data, 2);
4774                         len = ntohs(len);
4775
4776                         rt_ee_read_all(pAdapter,(USHORT *)buffer);
4777                         if (offset + len <= EEPROM_SIZE)
4778                                 memcpy_exs(pAdapter, pRaCfg->data, (UCHAR *)buffer+offset, len);
4779                         else
4780                                 ATEDBGPRINT(RT_DEBUG_ERROR, ("exceed EEPROM size\n"));
4781
4782                         // prepare feedback
4783                         pRaCfg->length = htons(2+len);
4784                         pRaCfg->status = htons(0);
4785                         wrq->u.data.length = sizeof(pRaCfg->magic_no) + sizeof(pRaCfg->command_type)
4786                                                                         + sizeof(pRaCfg->command_id) + sizeof(pRaCfg->length)
4787                                                                         + sizeof(pRaCfg->sequence) + ntohs(pRaCfg->length);
4788
4789             if (copy_to_user(wrq->u.data.pointer, pRaCfg, wrq->u.data.length))
4790             {
4791                 ATEDBGPRINT(RT_DEBUG_ERROR, ("copy_to_user() fail in case RACFG_CMD_ATE_E2PROM_READ_BULK\n"));
4792                 Status = -EFAULT;
4793             }
4794                         else
4795                         {
4796                 ATEDBGPRINT(RT_DEBUG_TRACE, ("RACFG_CMD_ATE_E2PROM_READ_BULK is done !\n"));
4797                         }
4798
4799                 }
4800                         break;
4801
4802                 case RACFG_CMD_ATE_E2PROM_WRITE_BULK:
4803                 {
4804                         USHORT offset;
4805                         USHORT len;
4806                         USHORT buffer[EEPROM_SIZE/2];
4807
4808                         offset = ntohs(pRaCfg->status);
4809                         memcpy(&len, pRaCfg->data, 2);
4810                         len = ntohs(len);
4811
4812                         rt_ee_read_all(pAdapter,(USHORT *)buffer);
4813                         memcpy_exs(pAdapter, (UCHAR *)buffer + offset, (UCHAR *)pRaCfg->data + 2, len);
4814                         rt_ee_write_all(pAdapter,(USHORT *)buffer);
4815
4816                         // prepare feedback
4817                         pRaCfg->length = htons(2);
4818                         pRaCfg->status = htons(0);
4819                         wrq->u.data.length = sizeof(pRaCfg->magic_no) + sizeof(pRaCfg->command_type)
4820                                                                 + sizeof(pRaCfg->command_id) + sizeof(pRaCfg->length)
4821                                                                 + sizeof(pRaCfg->sequence) + ntohs(pRaCfg->length);
4822             if (copy_to_user(wrq->u.data.pointer, pRaCfg, wrq->u.data.length))
4823             {
4824                 ATEDBGPRINT(RT_DEBUG_ERROR, ("copy_to_user() fail in case RACFG_CMD_ATE_E2PROM_WRITE_BULK\n"));
4825                    Status = -EFAULT;
4826             }
4827                         else
4828                         {
4829                 ATEDBGPRINT(RT_DEBUG_ERROR, ("RACFG_CMD_ATE_E2PROM_WRITE_BULK is done !\n"));
4830                         }
4831
4832                 }
4833                         break;
4834
4835                 case RACFG_CMD_ATE_IO_WRITE_BULK:
4836                 {
4837                         UINT32 offset, i, value;
4838                         USHORT len;
4839
4840                         memcpy(&offset, &pRaCfg->status, 4);
4841                         offset = ntohl(offset);
4842                         memcpy(&len, pRaCfg->data+2, 2);
4843                         len = ntohs(len);
4844
4845                         for (i = 0; i < len; i += 4)
4846                         {
4847                                 memcpy_exl(pAdapter, (UCHAR *)&value, pRaCfg->data+4+i, 4);
4848                                 printk("Write %x %x\n", offset + i, value);
4849                                 RTMP_IO_WRITE32(pAdapter, (offset +i) & 0xffff, value);
4850                         }
4851
4852                         // prepare feedback
4853                         pRaCfg->length = htons(2);
4854                         pRaCfg->status = htons(0);
4855                         wrq->u.data.length = sizeof(pRaCfg->magic_no) + sizeof(pRaCfg->command_type)
4856                                                                 + sizeof(pRaCfg->command_id) + sizeof(pRaCfg->length)
4857                                                                 + sizeof(pRaCfg->sequence) + ntohs(pRaCfg->length);
4858             if (copy_to_user(wrq->u.data.pointer, pRaCfg, wrq->u.data.length))
4859             {
4860                 ATEDBGPRINT(RT_DEBUG_ERROR, ("copy_to_user() fail in case RACFG_CMD_ATE_IO_WRITE_BULK\n"));
4861                    Status = -EFAULT;
4862             }
4863                         else
4864                         {
4865                 ATEDBGPRINT(RT_DEBUG_ERROR, ("RACFG_CMD_ATE_IO_WRITE_BULK is done !\n"));
4866                         }
4867
4868                 }
4869                         break;
4870
4871                 case RACFG_CMD_ATE_BBP_READ_BULK:
4872                 {
4873                         USHORT offset;
4874                         USHORT len;
4875                         USHORT j;
4876
4877                         offset = ntohs(pRaCfg->status);
4878                         memcpy(&len, pRaCfg->data, 2);
4879                         len = ntohs(len);
4880
4881
4882                         for (j = offset; j < (offset+len); j++)
4883                         {
4884                                 pRaCfg->data[j - offset] = 0;
4885
4886                                 if (pAdapter->ate.Mode == ATE_STOP)
4887                                 {
4888                                         RTMP_BBP_IO_READ8_BY_REG_ID(pAdapter, j,  &pRaCfg->data[j - offset]);
4889                                 }
4890                                 else
4891                                 {
4892                                         ATE_BBP_IO_READ8_BY_REG_ID(pAdapter, j,  &pRaCfg->data[j - offset]);
4893                                 }
4894                         }
4895
4896                         // prepare feedback
4897                         pRaCfg->length = htons(2+len);
4898                         pRaCfg->status = htons(0);
4899                         wrq->u.data.length = sizeof(pRaCfg->magic_no) + sizeof(pRaCfg->command_type)
4900                                                                 + sizeof(pRaCfg->command_id) + sizeof(pRaCfg->length)
4901                                                                 + sizeof(pRaCfg->sequence) + ntohs(pRaCfg->length);
4902
4903             if (copy_to_user(wrq->u.data.pointer, pRaCfg, wrq->u.data.length))
4904             {
4905                 ATEDBGPRINT(RT_DEBUG_ERROR, ("copy_to_user() fail in case RACFG_CMD_ATE_BBP_READ_BULK\n"));
4906                    Status = -EFAULT;
4907             }
4908                         else
4909                         {
4910                 ATEDBGPRINT(RT_DEBUG_TRACE, ("RACFG_CMD_ATE_BBP_READ_BULK is done !\n"));
4911                         }
4912
4913                 }
4914                         break;
4915
4916                 case RACFG_CMD_ATE_BBP_WRITE_BULK:
4917                 {
4918                         USHORT offset;
4919                         USHORT len;
4920                         USHORT j;
4921                         UCHAR *value;
4922
4923                         offset = ntohs(pRaCfg->status);
4924                         memcpy(&len, pRaCfg->data, 2);
4925                         len = ntohs(len);
4926
4927                         for (j = offset; j < (offset+len); j++)
4928                         {
4929                                 value = pRaCfg->data + 2 + (j - offset);
4930                                 if (pAdapter->ate.Mode == ATE_STOP)
4931                                 {
4932                                         RTMP_BBP_IO_WRITE8_BY_REG_ID(pAdapter, j,  *value);
4933                                 }
4934                                 else
4935                                 {
4936                                         ATE_BBP_IO_WRITE8_BY_REG_ID(pAdapter, j,  *value);
4937                                 }
4938                         }
4939
4940                         // prepare feedback
4941                         pRaCfg->length = htons(2);
4942                         pRaCfg->status = htons(0);
4943                         wrq->u.data.length = sizeof(pRaCfg->magic_no) + sizeof(pRaCfg->command_type)
4944                                                                 + sizeof(pRaCfg->command_id) + sizeof(pRaCfg->length)
4945                                                                 + sizeof(pRaCfg->sequence) + ntohs(pRaCfg->length);
4946
4947             if (copy_to_user(wrq->u.data.pointer, pRaCfg, wrq->u.data.length))
4948             {
4949                 ATEDBGPRINT(RT_DEBUG_ERROR, ("copy_to_user() fail in case RACFG_CMD_ATE_BBP_WRITE_BULK\n"));
4950                    Status = -EFAULT;
4951             }
4952                         else
4953                         {
4954                 ATEDBGPRINT(RT_DEBUG_TRACE, ("RACFG_CMD_ATE_BBP_WRITE_BULK is done !\n"));
4955                         }
4956                 }
4957                         break;
4958
4959 #ifdef CONFIG_RALINK_RT3052
4960                 case RACFG_CMD_ATE_RF_READ_BULK:
4961                 {
4962                         USHORT offset;
4963                         USHORT len;
4964                         USHORT j;
4965
4966                         offset = ntohs(pRaCfg->status);
4967                         memcpy(&len, pRaCfg->data, 2);
4968                         len = ntohs(len);
4969
4970                         for (j = offset; j < (offset+len); j++)
4971                         {
4972                                 pRaCfg->data[j - offset] = 0;
4973                                 RT30xxReadRFRegister(pAdapter, j,  &pRaCfg->data[j - offset]);
4974                         }
4975
4976                         // prepare feedback
4977                         pRaCfg->length = htons(2+len);
4978                         pRaCfg->status = htons(0);
4979                         wrq->u.data.length = sizeof(pRaCfg->magic_no) + sizeof(pRaCfg->command_type)
4980                                                                 + sizeof(pRaCfg->command_id) + sizeof(pRaCfg->length)
4981                                                                 + sizeof(pRaCfg->sequence) + ntohs(pRaCfg->length);
4982
4983             if (copy_to_user(wrq->u.data.pointer, pRaCfg, wrq->u.data.length))
4984             {
4985                 ATEDBGPRINT(RT_DEBUG_ERROR, ("copy_to_user() fail in case RACFG_CMD_ATE_RF_READ_BULK\n"));
4986                    Status = -EFAULT;
4987             }
4988                         else
4989                         {
4990                 ATEDBGPRINT(RT_DEBUG_TRACE, ("RACFG_CMD_ATE_RF_READ_BULK is done !\n"));
4991                         }
4992
4993                 }
4994                         break;
4995
4996                 case RACFG_CMD_ATE_RF_WRITE_BULK:
4997                 {
4998                         USHORT offset;
4999                         USHORT len;
5000                         USHORT j;
5001                         UCHAR *value;
5002
5003                         offset = ntohs(pRaCfg->status);
5004                         memcpy(&len, pRaCfg->data, 2);
5005                         len = ntohs(len);
5006
5007                         for (j = offset; j < (offset+len); j++)
5008                         {
5009                                 value = pRaCfg->data + 2 + (j - offset);
5010                                 RT30xxWriteRFRegister(pAdapter, j,  *value);
5011                         }
5012
5013                         // prepare feedback
5014                         pRaCfg->length = htons(2);
5015                         pRaCfg->status = htons(0);
5016                         wrq->u.data.length = sizeof(pRaCfg->magic_no) + sizeof(pRaCfg->command_type)
5017                                                                 + sizeof(pRaCfg->command_id) + sizeof(pRaCfg->length)
5018                                                                 + sizeof(pRaCfg->sequence) + ntohs(pRaCfg->length);
5019
5020             if (copy_to_user(wrq->u.data.pointer, pRaCfg, wrq->u.data.length))
5021             {
5022                 ATEDBGPRINT(RT_DEBUG_ERROR, ("copy_to_user() fail in case RACFG_CMD_ATE_RF_WRITE_BULK\n"));
5023                    Status = -EFAULT;
5024             }
5025                         else
5026                         {
5027                 ATEDBGPRINT(RT_DEBUG_TRACE, ("RACFG_CMD_ATE_RF_WRITE_BULK is done !\n"));
5028                         }
5029
5030                 }
5031                         break;
5032 #endif
5033
5034
5035                 case RACFG_CMD_GET_NOISE_LEVEL:
5036                         {
5037                                 UCHAR   channel;
5038                                 INT32   buffer[3][10];/* 3 : RxPath ; 10 : no. of per rssi samples */
5039
5040                                 channel = (ntohs(pRaCfg->status) & 0x00FF);
5041                                 CalNoiseLevel(pAdapter, channel, buffer);
5042                                 memcpy_exl(pAdapter, (UCHAR *)pRaCfg->data, (UCHAR *)&(buffer[0][0]), (sizeof(INT32)*3*10));
5043
5044                                 // prepare feedback
5045                                 pRaCfg->length = htons(2 + (sizeof(INT32)*3*10));
5046                                 pRaCfg->status = htons(0);
5047                                 wrq->u.data.length = sizeof(pRaCfg->magic_no) + sizeof(pRaCfg->command_type)
5048                                                                         + sizeof(pRaCfg->command_id) + sizeof(pRaCfg->length)
5049                                                                         + sizeof(pRaCfg->sequence) + ntohs(pRaCfg->length);
5050
5051                 if (copy_to_user(wrq->u.data.pointer, pRaCfg, wrq->u.data.length))
5052                 {
5053                         ATEDBGPRINT(RT_DEBUG_ERROR, ("copy_to_user() fail in case RACFG_CMD_GET_NOISE_LEVEL\n"));
5054                     Status = -EFAULT;
5055                 }
5056                                 else
5057                                 {
5058                         ATEDBGPRINT(RT_DEBUG_TRACE, ("RACFG_CMD_GET_NOISE_LEVEL is done !\n"));
5059                                 }
5060                         }
5061                         break;
5062
5063                 case RACFG_CMD_GET_COUNTER:
5064                         {
5065                                 memcpy_exl(pAdapter, &pRaCfg->data[0], (UCHAR *)&pAdapter->ate.U2M, 4);
5066                                 memcpy_exl(pAdapter, &pRaCfg->data[4], (UCHAR *)&pAdapter->ate.OtherData, 4);
5067                                 memcpy_exl(pAdapter, &pRaCfg->data[8], (UCHAR *)&pAdapter->ate.Beacon, 4);
5068                                 memcpy_exl(pAdapter, &pRaCfg->data[12], (UCHAR *)&pAdapter->ate.OtherCount, 4);
5069                                 memcpy_exl(pAdapter, &pRaCfg->data[16], (UCHAR *)&pAdapter->ate.TxAc0, 4);
5070                                 memcpy_exl(pAdapter, &pRaCfg->data[20], (UCHAR *)&pAdapter->ate.TxAc1, 4);
5071                                 memcpy_exl(pAdapter, &pRaCfg->data[24], (UCHAR *)&pAdapter->ate.TxAc2, 4);
5072                                 memcpy_exl(pAdapter, &pRaCfg->data[28], (UCHAR *)&pAdapter->ate.TxAc3, 4);
5073                                 memcpy_exl(pAdapter, &pRaCfg->data[32], (UCHAR *)&pAdapter->ate.TxHCCA, 4);
5074                                 memcpy_exl(pAdapter, &pRaCfg->data[36], (UCHAR *)&pAdapter->ate.TxMgmt, 4);
5075                                 memcpy_exl(pAdapter, &pRaCfg->data[40], (UCHAR *)&pAdapter->ate.RSSI0, 4);
5076                                 memcpy_exl(pAdapter, &pRaCfg->data[44], (UCHAR *)&pAdapter->ate.RSSI1, 4);
5077                                 memcpy_exl(pAdapter, &pRaCfg->data[48], (UCHAR *)&pAdapter->ate.RSSI2, 4);
5078                                 memcpy_exl(pAdapter, &pRaCfg->data[52], (UCHAR *)&pAdapter->ate.SNR0, 4);
5079                                 memcpy_exl(pAdapter, &pRaCfg->data[56], (UCHAR *)&pAdapter->ate.SNR1, 4);
5080
5081                                 pRaCfg->length = htons(2+60);
5082                                 pRaCfg->status = htons(0);
5083                                 wrq->u.data.length = sizeof(pRaCfg->magic_no) + sizeof(pRaCfg->command_type)
5084                                                                         + sizeof(pRaCfg->command_id) + sizeof(pRaCfg->length)
5085                                                                         + sizeof(pRaCfg->sequence) + ntohs(pRaCfg->length);
5086
5087                 if (copy_to_user(wrq->u.data.pointer, pRaCfg, wrq->u.data.length))
5088                 {
5089                         ATEDBGPRINT(RT_DEBUG_ERROR, ("copy_to_user() fail in case RACFG_CMD_GET_COUNTER\n"));
5090                     Status = -EFAULT;
5091                 }
5092                                 else
5093                                 {
5094                         ATEDBGPRINT(RT_DEBUG_TRACE, ("RACFG_CMD_GET_COUNTER is done !\n"));
5095                                 }
5096                         }
5097                         break;
5098
5099                 case RACFG_CMD_CLEAR_COUNTER:
5100                         {
5101                                 pAdapter->ate.U2M = 0;
5102                                 pAdapter->ate.OtherData = 0;
5103                                 pAdapter->ate.Beacon = 0;
5104                                 pAdapter->ate.OtherCount = 0;
5105                                 pAdapter->ate.TxAc0 = 0;
5106                                 pAdapter->ate.TxAc1 = 0;
5107                                 pAdapter->ate.TxAc2 = 0;
5108                                 pAdapter->ate.TxAc3 = 0;
5109                                 pAdapter->ate.TxHCCA = 0;
5110                                 pAdapter->ate.TxMgmt = 0;
5111                                 pAdapter->ate.TxDoneCount = 0;
5112
5113                                 pRaCfg->length = htons(2);
5114                                 pRaCfg->status = htons(0);
5115
5116                                 wrq->u.data.length = sizeof(pRaCfg->magic_no) + sizeof(pRaCfg->command_type)
5117                                                                         + sizeof(pRaCfg->command_id) + sizeof(pRaCfg->length)
5118                                                                         + sizeof(pRaCfg->sequence) + ntohs(pRaCfg->length);
5119
5120                 if (copy_to_user(wrq->u.data.pointer, pRaCfg, wrq->u.data.length))
5121                 {
5122                         ATEDBGPRINT(RT_DEBUG_ERROR, ("copy_to_user() fail in case RACFG_CMD_CLEAR_COUNTER\n"));
5123                     Status = -EFAULT;
5124                 }
5125                                 else
5126                                 {
5127                         ATEDBGPRINT(RT_DEBUG_TRACE, ("RACFG_CMD_CLEAR_COUNTER is done !\n"));
5128                                 }
5129                         }
5130
5131                         break;
5132
5133                 case RACFG_CMD_TX_START:
5134                         {
5135                                 USHORT *p;
5136                                 USHORT  err = 1;
5137                                 UCHAR   Bbp22Value = 0, Bbp24Value = 0;
5138
5139                                 if ((pAdapter->ate.TxStatus != 0) && (pAdapter->ate.Mode & ATE_TXFRAME))
5140                                 {
5141                                         ATEDBGPRINT(RT_DEBUG_TRACE,("Ate Tx is already running, to run next Tx, you must stop it first\n"));
5142                                         err = 2;
5143                                         goto TX_START_ERROR;
5144                                 }
5145                                 else if ((pAdapter->ate.TxStatus != 0) && !(pAdapter->ate.Mode & ATE_TXFRAME))
5146                                 {
5147                                         int i = 0;
5148
5149                                         while ((i++ < 10) && (pAdapter->ate.TxStatus != 0))
5150                                         {
5151                                                 RTMPusecDelay(5000);
5152                                         }
5153
5154                                         // force it to stop
5155                                         pAdapter->ate.TxStatus = 0;
5156                                         pAdapter->ate.TxDoneCount = 0;
5157                                         //pAdapter->ate.Repeat = 0;
5158                                         pAdapter->ate.bQATxStart = FALSE;
5159                                 }
5160
5161                                 // If pRaCfg->length == 0, this "RACFG_CMD_TX_START" is for Carrier test or Carrier Suppression.
5162                                 if (ntohs(pRaCfg->length) != 0)
5163                                 {
5164                                         // Get frame info
5165 #ifdef RT2870
5166                                         NdisMoveMemory(&pAdapter->ate.TxInfo, pRaCfg->data - 2, 4);
5167 #ifdef RT_BIG_ENDIAN
5168                                         RTMPDescriptorEndianChange((PUCHAR) &pAdapter->ate.TxInfo, TYPE_TXINFO);
5169 #endif // RT_BIG_ENDIAN //
5170 #endif // RT2870 //
5171
5172                                         NdisMoveMemory(&pAdapter->ate.TxWI, pRaCfg->data + 2, 16);
5173 #ifdef RT_BIG_ENDIAN
5174                                         RTMPWIEndianChange((PUCHAR)&pAdapter->ate.TxWI, TYPE_TXWI);
5175 #endif // RT_BIG_ENDIAN //
5176
5177                                         NdisMoveMemory(&pAdapter->ate.TxCount, pRaCfg->data + 18, 4);
5178                                         pAdapter->ate.TxCount = ntohl(pAdapter->ate.TxCount);
5179
5180                                         p = (USHORT *)(&pRaCfg->data[22]);
5181                                         //p = pRaCfg->data + 22;
5182                                         // always use QID_AC_BE
5183                                         pAdapter->ate.QID = 0;
5184                                         p = (USHORT *)(&pRaCfg->data[24]);
5185                                         //p = pRaCfg->data + 24;
5186                                         pAdapter->ate.HLen = ntohs(*p);
5187
5188                                         if (pAdapter->ate.HLen > 32)
5189                                         {
5190                                                 ATEDBGPRINT(RT_DEBUG_ERROR,("pAdapter->ate.HLen > 32\n"));
5191                                                 err = 3;
5192                                                 goto TX_START_ERROR;
5193                                         }
5194
5195                                         NdisMoveMemory(&pAdapter->ate.Header, pRaCfg->data + 26, pAdapter->ate.HLen);
5196
5197
5198                                         pAdapter->ate.PLen = ntohs(pRaCfg->length) - (pAdapter->ate.HLen + 28);
5199
5200                                         if (pAdapter->ate.PLen > 32)
5201                                         {
5202                                                 ATEDBGPRINT(RT_DEBUG_ERROR,("pAdapter->ate.PLen > 32\n"));
5203                                                 err = 4;
5204                                                 goto TX_START_ERROR;
5205                                         }
5206
5207                                         NdisMoveMemory(&pAdapter->ate.Pattern, pRaCfg->data + 26 + pAdapter->ate.HLen, pAdapter->ate.PLen);
5208                                         pAdapter->ate.DLen = pAdapter->ate.TxWI.MPDUtotalByteCount - pAdapter->ate.HLen;
5209                                 }
5210
5211                                 ATE_BBP_IO_READ8_BY_REG_ID(pAdapter, BBP_R22, &Bbp22Value);
5212
5213                                 switch (Bbp22Value)
5214                                 {
5215                                         case BBP22_TXFRAME:
5216                                                 {
5217                                                         if (pAdapter->ate.TxCount == 0)
5218                                                         {
5219                                                         }
5220                                                         ATEDBGPRINT(RT_DEBUG_TRACE,("START TXFRAME\n"));
5221                                                         pAdapter->ate.bQATxStart = TRUE;
5222                                                         Set_ATE_Proc(pAdapter, "TXFRAME");
5223                                                 }
5224                                                 break;
5225
5226                                         case BBP22_TXCONT_OR_CARRSUPP:
5227                                                 {
5228                                                         ATEDBGPRINT(RT_DEBUG_TRACE,("BBP22_TXCONT_OR_CARRSUPP\n"));
5229                                                         ATE_BBP_IO_READ8_BY_REG_ID(pAdapter, 24, &Bbp24Value);
5230
5231                                                         switch (Bbp24Value)
5232                                                         {
5233                                                                 case BBP24_TXCONT:
5234                                                                         {
5235                                                                                 ATEDBGPRINT(RT_DEBUG_TRACE,("START TXCONT\n"));
5236                                                                                 pAdapter->ate.bQATxStart = TRUE;
5237                                                                                 Set_ATE_Proc(pAdapter, "TXCONT");
5238                                                                         }
5239                                                                         break;
5240
5241                                                                 case BBP24_CARRSUPP:
5242                                                                         {
5243                                                                                 ATEDBGPRINT(RT_DEBUG_TRACE,("START TXCARRSUPP\n"));
5244                                                                                 pAdapter->ate.bQATxStart = TRUE;
5245                                                                                 pAdapter->ate.Mode |= ATE_TXCARRSUPP;
5246                                                                         }
5247                                                                         break;
5248
5249                                                                 default:
5250                                                                         {
5251                                                                                 ATEDBGPRINT(RT_DEBUG_ERROR,("Unknown Start TX subtype !"));
5252                                                                         }
5253                                                                         break;
5254                                                         }
5255                                                 }
5256                                                 break;
5257
5258                                         case BBP22_TXCARR:
5259                                                 {
5260                                                         ATEDBGPRINT(RT_DEBUG_TRACE,("START TXCARR\n"));
5261                                                         pAdapter->ate.bQATxStart = TRUE;
5262                                                         Set_ATE_Proc(pAdapter, "TXCARR");
5263                                                 }
5264                                                 break;
5265
5266                                         default:
5267                                                 {
5268                                                         ATEDBGPRINT(RT_DEBUG_ERROR,("Unknown Start TX subtype !"));
5269                                                 }
5270                                                 break;
5271                                 }
5272
5273                                 if (pAdapter->ate.bQATxStart == TRUE)
5274                                 {
5275                                         // prepare feedback
5276                                         pRaCfg->length = htons(2);
5277                                         pRaCfg->status = htons(0);
5278
5279                                         wrq->u.data.length = sizeof(pRaCfg->magic_no) + sizeof(pRaCfg->command_type)
5280                                                                                 + sizeof(pRaCfg->command_id) + sizeof(pRaCfg->length)
5281                                                                                 + sizeof(pRaCfg->sequence) + ntohs(pRaCfg->length);
5282
5283                         if (copy_to_user(wrq->u.data.pointer, pRaCfg, wrq->u.data.length))
5284                         {
5285                                 ATEDBGPRINT(RT_DEBUG_ERROR, ("copy_to_user() was failed in case RACFG_CMD_TX_START\n"));
5286                             Status = -EFAULT;
5287                         }
5288                                         else
5289                                         {
5290                                 ATEDBGPRINT(RT_DEBUG_TRACE, ("RACFG_CMD_TX_START is done !\n"));
5291                                         }
5292                                         break;
5293                                 }
5294
5295 TX_START_ERROR:
5296                                 // prepare feedback
5297                                 pRaCfg->length = htons(2);
5298                                 pRaCfg->status = htons(err);
5299
5300                                 wrq->u.data.length = sizeof(pRaCfg->magic_no) + sizeof(pRaCfg->command_type)
5301                                                                         + sizeof(pRaCfg->command_id) + sizeof(pRaCfg->length)
5302                                                                         + sizeof(pRaCfg->sequence) + ntohs(pRaCfg->length);
5303                 if (copy_to_user(wrq->u.data.pointer, pRaCfg, wrq->u.data.length))
5304                 {
5305                         ATEDBGPRINT(RT_DEBUG_ERROR, ("copy_to_user() fail in case RACFG_CMD_TX_START\n"));
5306                     Status = -EFAULT;
5307                 }
5308                                 else
5309                                 {
5310                         ATEDBGPRINT(RT_DEBUG_TRACE, ("feedback of TX_START_ERROR is done !\n"));
5311                                 }
5312                         }
5313                         break;
5314
5315                 case RACFG_CMD_GET_TX_STATUS:
5316                         {
5317                                 UINT32 count;
5318
5319                                 // prepare feedback
5320                                 pRaCfg->length = htons(6);
5321                                 pRaCfg->status = htons(0);
5322                                 count = htonl(pAdapter->ate.TxDoneCount);
5323                                 NdisMoveMemory(pRaCfg->data, &count, 4);
5324                                 wrq->u.data.length = sizeof(pRaCfg->magic_no) + sizeof(pRaCfg->command_type)
5325                                                                         + sizeof(pRaCfg->command_id) + sizeof(pRaCfg->length)
5326                                                                         + sizeof(pRaCfg->sequence) + ntohs(pRaCfg->length);
5327
5328                 if (copy_to_user(wrq->u.data.pointer, pRaCfg, wrq->u.data.length))
5329                 {
5330                         ATEDBGPRINT(RT_DEBUG_ERROR, ("copy_to_user() fail in case RACFG_CMD_GET_TX_STATUS\n"));
5331                     Status = -EFAULT;
5332                 }
5333                                 else
5334                                 {
5335                         ATEDBGPRINT(RT_DEBUG_TRACE, ("RACFG_CMD_GET_TX_STATUS is done !\n"));
5336                                 }
5337                         }
5338                         break;
5339
5340                 case RACFG_CMD_TX_STOP:
5341                         {
5342                                 ATEDBGPRINT(RT_DEBUG_TRACE,("RACFG_CMD_TX_STOP\n"));
5343
5344                                 Set_ATE_Proc(pAdapter, "TXSTOP");
5345
5346                                 // prepare feedback
5347                                 pRaCfg->length = htons(2);
5348                                 pRaCfg->status = htons(0);
5349                                 wrq->u.data.length = sizeof(pRaCfg->magic_no) + sizeof(pRaCfg->command_type)
5350                                                                         + sizeof(pRaCfg->command_id) + sizeof(pRaCfg->length)
5351                                                                         + sizeof(pRaCfg->sequence) + ntohs(pRaCfg->length);
5352
5353                 if (copy_to_user(wrq->u.data.pointer, pRaCfg, wrq->u.data.length))
5354                 {
5355                         ATEDBGPRINT(RT_DEBUG_TRACE, ("copy_to_user() fail in case RACFG_CMD_TX_STOP\n"));
5356                     Status = -EFAULT;
5357                 }
5358                                 else
5359                                 {
5360                         ATEDBGPRINT(RT_DEBUG_TRACE, ("RACFG_CMD_TX_STOP is done !\n"));
5361                                 }
5362                         }
5363                         break;
5364
5365                 case RACFG_CMD_RX_START:
5366                         {
5367                                 ATEDBGPRINT(RT_DEBUG_TRACE,("RACFG_CMD_RX_START\n"));
5368
5369                                 pAdapter->ate.bQARxStart = TRUE;
5370                                 Set_ATE_Proc(pAdapter, "RXFRAME");
5371
5372                                 // prepare feedback
5373                                 pRaCfg->length = htons(2);
5374                                 pRaCfg->status = htons(0);
5375                                 wrq->u.data.length = sizeof(pRaCfg->magic_no) + sizeof(pRaCfg->command_type)
5376                                                                         + sizeof(pRaCfg->command_id) + sizeof(pRaCfg->length)
5377                                                                         + sizeof(pRaCfg->sequence) + ntohs(pRaCfg->length);
5378
5379                 if (copy_to_user(wrq->u.data.pointer, pRaCfg, wrq->u.data.length))
5380                 {
5381                         ATEDBGPRINT(RT_DEBUG_ERROR, ("copy_to_user() fail in case RACFG_CMD_RX_START\n"));
5382                     Status = -EFAULT;
5383                 }
5384                                 else
5385                                 {
5386                         ATEDBGPRINT(RT_DEBUG_TRACE, ("RACFG_CMD_RX_START is done !\n"));
5387                                 }
5388                         }
5389                         break;
5390
5391                 case RACFG_CMD_RX_STOP:
5392                         {
5393                                 ATEDBGPRINT(RT_DEBUG_TRACE,("RACFG_CMD_RX_STOP\n"));
5394
5395                                 Set_ATE_Proc(pAdapter, "RXSTOP");
5396
5397                                 // prepare feedback
5398                                 pRaCfg->length = htons(2);
5399                                 pRaCfg->status = htons(0);
5400                                 wrq->u.data.length = sizeof(pRaCfg->magic_no) + sizeof(pRaCfg->command_type)
5401                                                                         + sizeof(pRaCfg->command_id) + sizeof(pRaCfg->length)
5402                                                                         + sizeof(pRaCfg->sequence) + ntohs(pRaCfg->length);
5403
5404                 if (copy_to_user(wrq->u.data.pointer, pRaCfg, wrq->u.data.length))
5405                 {
5406                         ATEDBGPRINT(RT_DEBUG_ERROR, ("copy_to_user() fail in case RACFG_CMD_RX_STOP\n"));
5407                     Status = -EFAULT;
5408                 }
5409                                 else
5410                                 {
5411                         ATEDBGPRINT(RT_DEBUG_TRACE, ("RACFG_CMD_RX_STOP is done !\n"));
5412                                 }
5413                         }
5414                         break;
5415
5416                 /* The following cases are for new ATE GUI(not QA). */
5417                 /*==================================================*/
5418                 case RACFG_CMD_ATE_START_TX_CARRIER:
5419                         {
5420                                 ATEDBGPRINT(RT_DEBUG_TRACE,("RACFG_CMD_ATE_START_TX_CARRIER\n"));
5421
5422                                 Set_ATE_Proc(pAdapter, "TXCARR");
5423
5424                                 pRaCfg->length = htons(2);
5425                                 pRaCfg->status = htons(0);
5426
5427                                 wrq->u.data.length = sizeof(pRaCfg->magic_no) + sizeof(pRaCfg->command_type)
5428                                                                         + sizeof(pRaCfg->command_id) + sizeof(pRaCfg->length)
5429                                                                         + sizeof(pRaCfg->sequence) + ntohs(pRaCfg->length);
5430
5431                                 ATEDBGPRINT(RT_DEBUG_TRACE, ("wrq->u.data.length = %d\n", wrq->u.data.length));
5432
5433                 if (copy_to_user(wrq->u.data.pointer, pRaCfg, wrq->u.data.length))
5434                 {
5435                         ATEDBGPRINT(RT_DEBUG_ERROR, ("copy_to_user() fail in case RACFG_CMD_ATE_START_TX_CARRIER\n"));
5436                     Status = -EFAULT;
5437                 }
5438                                 else
5439                                 {
5440                         ATEDBGPRINT(RT_DEBUG_TRACE, ("RACFG_CMD_ATE_START_TX_CARRIER is done !\n"));
5441                                 }
5442                         }
5443                         break;
5444
5445                 case RACFG_CMD_ATE_START_TX_CONT:
5446                         {
5447                                 ATEDBGPRINT(RT_DEBUG_TRACE,("RACFG_CMD_ATE_START_TX_CONT\n"));
5448
5449                                 Set_ATE_Proc(pAdapter, "TXCONT");
5450
5451                                 pRaCfg->length = htons(2);
5452                                 pRaCfg->status = htons(0);
5453
5454                                 wrq->u.data.length = sizeof(pRaCfg->magic_no) + sizeof(pRaCfg->command_type)
5455                                                                         + sizeof(pRaCfg->command_id) + sizeof(pRaCfg->length)
5456                                                                         + sizeof(pRaCfg->sequence) + ntohs(pRaCfg->length);
5457
5458                                 ATEDBGPRINT(RT_DEBUG_TRACE, ("wrq->u.data.length = %d\n", wrq->u.data.length));
5459
5460                 if (copy_to_user(wrq->u.data.pointer, pRaCfg, wrq->u.data.length))
5461                 {
5462                         ATEDBGPRINT(RT_DEBUG_ERROR, ("copy_to_user() fail in case RACFG_CMD_ATE_START_TX_CONT\n"));
5463                     Status = -EFAULT;
5464                 }
5465                                 else
5466                                 {
5467                         ATEDBGPRINT(RT_DEBUG_TRACE, ("RACFG_CMD_ATE_START_TX_CONT is done !\n"));
5468                                 }
5469                         }
5470                         break;
5471
5472                 case RACFG_CMD_ATE_START_TX_FRAME:
5473                         {
5474                                 ATEDBGPRINT(RT_DEBUG_TRACE,("RACFG_CMD_ATE_START_TX_FRAME\n"));
5475
5476                                 Set_ATE_Proc(pAdapter, "TXFRAME");
5477
5478                                 pRaCfg->length = htons(2);
5479                                 pRaCfg->status = htons(0);
5480
5481                                 wrq->u.data.length = sizeof(pRaCfg->magic_no) + sizeof(pRaCfg->command_type)
5482                                                                         + sizeof(pRaCfg->command_id) + sizeof(pRaCfg->length)
5483                                                                         + sizeof(pRaCfg->sequence) + ntohs(pRaCfg->length);
5484
5485                                 ATEDBGPRINT(RT_DEBUG_TRACE, ("wrq->u.data.length = %d\n", wrq->u.data.length));
5486
5487                 if (copy_to_user(wrq->u.data.pointer, pRaCfg, wrq->u.data.length))
5488                 {
5489                         ATEDBGPRINT(RT_DEBUG_ERROR, ("copy_to_user() fail in case RACFG_CMD_ATE_START_TX_FRAME\n"));
5490                     Status = -EFAULT;
5491                 }
5492                                 else
5493                                 {
5494                         ATEDBGPRINT(RT_DEBUG_TRACE, ("RACFG_CMD_ATE_START_TX_FRAME is done !\n"));
5495                                 }
5496                         }
5497                         break;
5498
5499                 case RACFG_CMD_ATE_SET_BW:
5500                         {
5501                                 SHORT    value = 0;
5502                                 UCHAR    str[LEN_OF_ARG];
5503
5504                                 NdisZeroMemory(str, LEN_OF_ARG);
5505
5506                                 ATEDBGPRINT(RT_DEBUG_TRACE,("RACFG_CMD_ATE_SET_BW\n"));
5507
5508                                 memcpy((PUCHAR)&value, (PUCHAR)&(pRaCfg->status), 2);
5509                                 value = ntohs(value);
5510                                 sprintf((PCHAR)str, "%d", value);
5511
5512                                 Set_ATE_TX_BW_Proc(pAdapter, str);
5513
5514                                 // prepare feedback
5515                                 pRaCfg->length = htons(2);
5516                                 pRaCfg->status = htons(0);
5517                                 wrq->u.data.length = sizeof(pRaCfg->magic_no) + sizeof(pRaCfg->command_type)
5518                                                                         + sizeof(pRaCfg->command_id) + sizeof(pRaCfg->length)
5519                                                                         + sizeof(pRaCfg->sequence) + ntohs(pRaCfg->length);
5520
5521                 if (copy_to_user(wrq->u.data.pointer, pRaCfg, wrq->u.data.length))
5522                 {
5523                         ATEDBGPRINT(RT_DEBUG_ERROR, ("copy_to_user() fail in case RACFG_CMD_ATE_SET_BW\n"));
5524                     Status = -EFAULT;
5525                 }
5526                                 else
5527                                 {
5528                         ATEDBGPRINT(RT_DEBUG_TRACE, ("RACFG_CMD_ATE_SET_BW is done !\n"));
5529                                 }
5530                         }
5531                         break;
5532
5533                 case RACFG_CMD_ATE_SET_TX_POWER0:
5534                         {
5535                                 SHORT    value = 0;
5536                                 UCHAR    str[LEN_OF_ARG];
5537
5538                                 NdisZeroMemory(str, LEN_OF_ARG);
5539
5540                                 ATEDBGPRINT(RT_DEBUG_TRACE,("RACFG_CMD_ATE_SET_TX_POWER0\n"));
5541
5542                                 memcpy((PUCHAR)&value, (PUCHAR)&(pRaCfg->status), 2);
5543                                 value = ntohs(value);
5544                                 sprintf((PCHAR)str, "%d", value);
5545                                 Set_ATE_TX_POWER0_Proc(pAdapter, str);
5546
5547                                 // prepare feedback
5548                                 pRaCfg->length = htons(2);
5549                                 pRaCfg->status = htons(0);
5550                                 wrq->u.data.length = sizeof(pRaCfg->magic_no) + sizeof(pRaCfg->command_type)
5551                                                                         + sizeof(pRaCfg->command_id) + sizeof(pRaCfg->length)
5552                                                                         + sizeof(pRaCfg->sequence) + ntohs(pRaCfg->length);
5553
5554                 if (copy_to_user(wrq->u.data.pointer, pRaCfg, wrq->u.data.length))
5555                 {
5556                         ATEDBGPRINT(RT_DEBUG_ERROR, ("copy_to_user() fail in case RACFG_CMD_ATE_SET_TX_POWER0\n"));
5557                     Status = -EFAULT;
5558                 }
5559                                 else
5560                                 {
5561                         ATEDBGPRINT(RT_DEBUG_TRACE, ("RACFG_CMD_ATE_SET_TX_POWER0 is done !\n"));
5562                                 }
5563                         }
5564                         break;
5565
5566                 case RACFG_CMD_ATE_SET_TX_POWER1:
5567                         {
5568                                 SHORT    value = 0;
5569                                 UCHAR    str[LEN_OF_ARG];
5570
5571                                 NdisZeroMemory(str, LEN_OF_ARG);
5572
5573                                 ATEDBGPRINT(RT_DEBUG_TRACE,("RACFG_CMD_ATE_SET_TX_POWER1\n"));
5574
5575                                 memcpy((PUCHAR)&value, (PUCHAR)&(pRaCfg->status), 2);
5576                                 value = ntohs(value);
5577                                 sprintf((PCHAR)str, "%d", value);
5578                                 Set_ATE_TX_POWER1_Proc(pAdapter, str);
5579
5580                                 // prepare feedback
5581                                 pRaCfg->length = htons(2);
5582                                 pRaCfg->status = htons(0);
5583                                 wrq->u.data.length = sizeof(pRaCfg->magic_no) + sizeof(pRaCfg->command_type)
5584                                                                         + sizeof(pRaCfg->command_id) + sizeof(pRaCfg->length)
5585                                                                         + sizeof(pRaCfg->sequence) + ntohs(pRaCfg->length);
5586
5587                 if (copy_to_user(wrq->u.data.pointer, pRaCfg, wrq->u.data.length))
5588                 {
5589                         ATEDBGPRINT(RT_DEBUG_ERROR, ("copy_to_user() fail in case RACFG_CMD_ATE_SET_TX_POWER1\n"));
5590                     Status = -EFAULT;
5591                 }
5592                                 else
5593                                 {
5594                         ATEDBGPRINT(RT_DEBUG_TRACE, ("RACFG_CMD_ATE_SET_TX_POWER1 is done !\n"));
5595                                 }
5596                         }
5597                         break;
5598
5599                 case RACFG_CMD_ATE_SET_FREQ_OFFSET:
5600                         {
5601                                 SHORT    value = 0;
5602                                 UCHAR    str[LEN_OF_ARG];
5603
5604                                 NdisZeroMemory(str, LEN_OF_ARG);
5605
5606                                 ATEDBGPRINT(RT_DEBUG_TRACE,("RACFG_CMD_ATE_SET_FREQ_OFFSET\n"));
5607
5608                                 memcpy((PUCHAR)&value, (PUCHAR)&(pRaCfg->status), 2);
5609                                 value = ntohs(value);
5610                                 sprintf((PCHAR)str, "%d", value);
5611                                 Set_ATE_TX_FREQOFFSET_Proc(pAdapter, str);
5612
5613                                 // prepare feedback
5614                                 pRaCfg->length = htons(2);
5615                                 pRaCfg->status = htons(0);
5616                                 wrq->u.data.length = sizeof(pRaCfg->magic_no) + sizeof(pRaCfg->command_type)
5617                                                                         + sizeof(pRaCfg->command_id) + sizeof(pRaCfg->length)
5618                                                                         + sizeof(pRaCfg->sequence) + ntohs(pRaCfg->length);
5619
5620                 if (copy_to_user(wrq->u.data.pointer, pRaCfg, wrq->u.data.length))
5621                 {
5622                         ATEDBGPRINT(RT_DEBUG_ERROR, ("copy_to_user() fail in case RACFG_CMD_ATE_SET_FREQ_OFFSET\n"));
5623                     Status = -EFAULT;
5624                 }
5625                                 else
5626                                 {
5627                         ATEDBGPRINT(RT_DEBUG_TRACE, ("RACFG_CMD_ATE_SET_FREQ_OFFSET is done !\n"));
5628                                 }
5629                         }
5630                         break;
5631
5632                 case RACFG_CMD_ATE_GET_STATISTICS:
5633                         {
5634                                 ATEDBGPRINT(RT_DEBUG_TRACE,("RACFG_CMD_ATE_GET_STATISTICS\n"));
5635
5636                                 memcpy_exl(pAdapter, &pRaCfg->data[0], (UCHAR *)&pAdapter->ate.TxDoneCount, 4);
5637                                 memcpy_exl(pAdapter, &pRaCfg->data[4], (UCHAR *)&pAdapter->WlanCounters.RetryCount.u.LowPart, 4);
5638                                 memcpy_exl(pAdapter, &pRaCfg->data[8], (UCHAR *)&pAdapter->WlanCounters.FailedCount.u.LowPart, 4);
5639                                 memcpy_exl(pAdapter, &pRaCfg->data[12], (UCHAR *)&pAdapter->WlanCounters.RTSSuccessCount.u.LowPart, 4);
5640                                 memcpy_exl(pAdapter, &pRaCfg->data[16], (UCHAR *)&pAdapter->WlanCounters.RTSFailureCount.u.LowPart, 4);
5641                                 memcpy_exl(pAdapter, &pRaCfg->data[20], (UCHAR *)&pAdapter->WlanCounters.ReceivedFragmentCount.QuadPart, 4);
5642                                 memcpy_exl(pAdapter, &pRaCfg->data[24], (UCHAR *)&pAdapter->WlanCounters.FCSErrorCount.u.LowPart, 4);
5643                                 memcpy_exl(pAdapter, &pRaCfg->data[28], (UCHAR *)&pAdapter->Counters8023.RxNoBuffer, 4);
5644                                 memcpy_exl(pAdapter, &pRaCfg->data[32], (UCHAR *)&pAdapter->WlanCounters.FrameDuplicateCount.u.LowPart, 4);
5645                                 memcpy_exl(pAdapter, &pRaCfg->data[36], (UCHAR *)&pAdapter->RalinkCounters.OneSecFalseCCACnt, 4);
5646
5647                                 if (pAdapter->ate.RxAntennaSel == 0)
5648                                 {
5649                                         INT32 RSSI0 = 0;
5650                                         INT32 RSSI1 = 0;
5651                                         INT32 RSSI2 = 0;
5652
5653                                         RSSI0 = (INT32)(pAdapter->ate.LastRssi0 - pAdapter->BbpRssiToDbmDelta);
5654                                         RSSI1 = (INT32)(pAdapter->ate.LastRssi1 - pAdapter->BbpRssiToDbmDelta);
5655                                         RSSI2 = (INT32)(pAdapter->ate.LastRssi2 - pAdapter->BbpRssiToDbmDelta);
5656                                         memcpy_exl(pAdapter, &pRaCfg->data[40], (UCHAR *)&RSSI0, 4);
5657                                         memcpy_exl(pAdapter, &pRaCfg->data[44], (UCHAR *)&RSSI1, 4);
5658                                         memcpy_exl(pAdapter, &pRaCfg->data[48], (UCHAR *)&RSSI2, 4);
5659                                         pRaCfg->length = htons(2+52);
5660                                 }
5661                                 else
5662                                 {
5663                                         INT32 RSSI0 = 0;
5664
5665                                         RSSI0 = (INT32)(pAdapter->ate.LastRssi0 - pAdapter->BbpRssiToDbmDelta);
5666                                         memcpy_exl(pAdapter, &pRaCfg->data[40], (UCHAR *)&RSSI0, 4);
5667                                         pRaCfg->length = htons(2+44);
5668                                 }
5669                                 pRaCfg->status = htons(0);
5670                                 wrq->u.data.length = sizeof(pRaCfg->magic_no) + sizeof(pRaCfg->command_type)
5671                                                                         + sizeof(pRaCfg->command_id) + sizeof(pRaCfg->length)
5672                                                                         + sizeof(pRaCfg->sequence) + ntohs(pRaCfg->length);
5673
5674                 if (copy_to_user(wrq->u.data.pointer, pRaCfg, wrq->u.data.length))
5675                 {
5676                         ATEDBGPRINT(RT_DEBUG_ERROR, ("copy_to_user() fail in case RACFG_CMD_ATE_GET_STATISTICS\n"));
5677                     Status = -EFAULT;
5678                 }
5679                                 else
5680                                 {
5681                         ATEDBGPRINT(RT_DEBUG_TRACE, ("RACFG_CMD_ATE_GET_STATISTICS is done !\n"));
5682                                 }
5683                         }
5684                         break;
5685
5686                 case RACFG_CMD_ATE_RESET_COUNTER:
5687                         {
5688                                 SHORT    value = 1;
5689                                 UCHAR    str[LEN_OF_ARG];
5690
5691                                 NdisZeroMemory(str, LEN_OF_ARG);
5692
5693                                 ATEDBGPRINT(RT_DEBUG_TRACE,("RACFG_CMD_ATE_RESET_COUNTER\n"));
5694
5695                                 sprintf((PCHAR)str, "%d", value);
5696                                 Set_ResetStatCounter_Proc(pAdapter, str);
5697
5698                                 pAdapter->ate.TxDoneCount = 0;
5699
5700                                 pRaCfg->length = htons(2);
5701                                 pRaCfg->status = htons(0);
5702
5703                                 wrq->u.data.length = sizeof(pRaCfg->magic_no) + sizeof(pRaCfg->command_type)
5704                                                                         + sizeof(pRaCfg->command_id) + sizeof(pRaCfg->length)
5705                                                                         + sizeof(pRaCfg->sequence) + ntohs(pRaCfg->length);
5706
5707                 if (copy_to_user(wrq->u.data.pointer, pRaCfg, wrq->u.data.length))
5708                 {
5709                         ATEDBGPRINT(RT_DEBUG_ERROR, ("copy_to_user() fail in case RACFG_CMD_ATE_RESET_COUNTER\n"));
5710                     Status = -EFAULT;
5711                 }
5712                                 else
5713                                 {
5714                         ATEDBGPRINT(RT_DEBUG_TRACE, ("RACFG_CMD_ATE_RESET_COUNTER is done !\n"));
5715                                 }
5716                         }
5717
5718                         break;
5719
5720                 case RACFG_CMD_ATE_SEL_TX_ANTENNA:
5721                         {
5722                                 SHORT    value = 0;
5723                                 UCHAR    str[LEN_OF_ARG];
5724
5725                                 NdisZeroMemory(str, LEN_OF_ARG);
5726
5727                                 ATEDBGPRINT(RT_DEBUG_TRACE,("RACFG_CMD_ATE_SEL_TX_ANTENNA\n"));
5728
5729                                 memcpy((PUCHAR)&value, (PUCHAR)&(pRaCfg->status), 2);
5730                                 value = ntohs(value);
5731                                 sprintf((PCHAR)str, "%d", value);
5732                                 Set_ATE_TX_Antenna_Proc(pAdapter, str);
5733
5734                                 // prepare feedback
5735                                 pRaCfg->length = htons(2);
5736                                 pRaCfg->status = htons(0);
5737                                 wrq->u.data.length = sizeof(pRaCfg->magic_no) + sizeof(pRaCfg->command_type)
5738                                                                         + sizeof(pRaCfg->command_id) + sizeof(pRaCfg->length)
5739                                                                         + sizeof(pRaCfg->sequence) + ntohs(pRaCfg->length);
5740
5741                 if (copy_to_user(wrq->u.data.pointer, pRaCfg, wrq->u.data.length))
5742                 {
5743                         ATEDBGPRINT(RT_DEBUG_ERROR, ("copy_to_user() fail in case RACFG_CMD_ATE_SEL_TX_ANTENNA\n"));
5744                     Status = -EFAULT;
5745                 }
5746                                 else
5747                                 {
5748                         ATEDBGPRINT(RT_DEBUG_TRACE, ("RACFG_CMD_ATE_SEL_TX_ANTENNA is done !\n"));
5749                                 }
5750                         }
5751                         break;
5752
5753                 case RACFG_CMD_ATE_SEL_RX_ANTENNA:
5754                         {
5755                                 SHORT    value = 0;
5756                                 UCHAR    str[LEN_OF_ARG];
5757
5758                                 NdisZeroMemory(str, LEN_OF_ARG);
5759
5760                                 ATEDBGPRINT(RT_DEBUG_TRACE,("RACFG_CMD_ATE_SEL_RX_ANTENNA\n"));
5761
5762                                 memcpy((PUCHAR)&value, (PUCHAR)&(pRaCfg->status), 2);
5763                                 value = ntohs(value);
5764                                 sprintf((PCHAR)str, "%d", value);
5765                                 Set_ATE_RX_Antenna_Proc(pAdapter, str);
5766
5767                                 // prepare feedback
5768                                 pRaCfg->length = htons(2);
5769                                 pRaCfg->status = htons(0);
5770                                 wrq->u.data.length = sizeof(pRaCfg->magic_no) + sizeof(pRaCfg->command_type)
5771                                                                         + sizeof(pRaCfg->command_id) + sizeof(pRaCfg->length)
5772                                                                         + sizeof(pRaCfg->sequence) + ntohs(pRaCfg->length);
5773
5774                 if (copy_to_user(wrq->u.data.pointer, pRaCfg, wrq->u.data.length))
5775                 {
5776                         ATEDBGPRINT(RT_DEBUG_ERROR, ("copy_to_user() fail in case RACFG_CMD_ATE_SEL_RX_ANTENNA\n"));
5777                     Status = -EFAULT;
5778                 }
5779                                 else
5780                                 {
5781                         ATEDBGPRINT(RT_DEBUG_TRACE, ("RACFG_CMD_ATE_SEL_RX_ANTENNA is done !\n"));
5782                                 }
5783                         }
5784                         break;
5785
5786                 case RACFG_CMD_ATE_SET_PREAMBLE:
5787                         {
5788                                 SHORT    value = 0;
5789                                 UCHAR    str[LEN_OF_ARG];
5790
5791                                 NdisZeroMemory(str, LEN_OF_ARG);
5792
5793                                 ATEDBGPRINT(RT_DEBUG_TRACE,("RACFG_CMD_ATE_SET_PREAMBLE\n"));
5794
5795                                 memcpy((PUCHAR)&value, (PUCHAR)&(pRaCfg->status), 2);
5796                                 value = ntohs(value);
5797                                 sprintf((PCHAR)str, "%d", value);
5798                                 Set_ATE_TX_MODE_Proc(pAdapter, str);
5799
5800                                 // prepare feedback
5801                                 pRaCfg->length = htons(2);
5802                                 pRaCfg->status = htons(0);
5803                                 wrq->u.data.length = sizeof(pRaCfg->magic_no) + sizeof(pRaCfg->command_type)
5804                                                                         + sizeof(pRaCfg->command_id) + sizeof(pRaCfg->length)
5805                                                                         + sizeof(pRaCfg->sequence) + ntohs(pRaCfg->length);
5806
5807                 if (copy_to_user(wrq->u.data.pointer, pRaCfg, wrq->u.data.length))
5808                 {
5809                         ATEDBGPRINT(RT_DEBUG_ERROR, ("copy_to_user() fail in case RACFG_CMD_ATE_SET_PREAMBLE\n"));
5810                     Status = -EFAULT;
5811                 }
5812                                 else
5813                                 {
5814                         ATEDBGPRINT(RT_DEBUG_TRACE, ("RACFG_CMD_ATE_SET_PREAMBLE is done !\n"));
5815                                 }
5816                         }
5817                         break;
5818
5819                 case RACFG_CMD_ATE_SET_CHANNEL:
5820                         {
5821                                 SHORT    value = 0;
5822                                 UCHAR    str[LEN_OF_ARG];
5823
5824                                 NdisZeroMemory(str, LEN_OF_ARG);
5825
5826                                 ATEDBGPRINT(RT_DEBUG_TRACE,("RACFG_CMD_ATE_SET_CHANNEL\n"));
5827
5828                                 memcpy((PUCHAR)&value, (PUCHAR)&(pRaCfg->status), 2);
5829                                 value = ntohs(value);
5830                                 sprintf((PCHAR)str, "%d", value);
5831                                 Set_ATE_CHANNEL_Proc(pAdapter, str);
5832
5833                                 // prepare feedback
5834                                 pRaCfg->length = htons(2);
5835                                 pRaCfg->status = htons(0);
5836                                 wrq->u.data.length = sizeof(pRaCfg->magic_no) + sizeof(pRaCfg->command_type)
5837                                                                         + sizeof(pRaCfg->command_id) + sizeof(pRaCfg->length)
5838                                                                         + sizeof(pRaCfg->sequence) + ntohs(pRaCfg->length);
5839
5840                 if (copy_to_user(wrq->u.data.pointer, pRaCfg, wrq->u.data.length))
5841                 {
5842                         ATEDBGPRINT(RT_DEBUG_ERROR, ("copy_to_user() fail in case RACFG_CMD_ATE_SET_CHANNEL\n"));
5843                     Status = -EFAULT;
5844                 }
5845                                 else
5846                                 {
5847                         ATEDBGPRINT(RT_DEBUG_TRACE, ("RACFG_CMD_ATE_SET_CHANNEL is done !\n"));
5848                                 }
5849                         }
5850                         break;
5851
5852                 case RACFG_CMD_ATE_SET_ADDR1:
5853                         {
5854                                 ATEDBGPRINT(RT_DEBUG_TRACE,("RACFG_CMD_ATE_SET_ADDR1\n"));
5855
5856                                 // Addr is an array of UCHAR,
5857                                 // so no need to perform endian swap.
5858                                 memcpy(pAdapter->ate.Addr1, (PUCHAR)(pRaCfg->data - 2), MAC_ADDR_LEN);
5859
5860                                 // prepare feedback
5861                                 pRaCfg->length = htons(2);
5862                                 pRaCfg->status = htons(0);
5863                                 wrq->u.data.length = sizeof(pRaCfg->magic_no) + sizeof(pRaCfg->command_type)
5864                                                                         + sizeof(pRaCfg->command_id) + sizeof(pRaCfg->length)
5865                                                                         + sizeof(pRaCfg->sequence) + ntohs(pRaCfg->length);
5866
5867                 if (copy_to_user(wrq->u.data.pointer, pRaCfg, wrq->u.data.length))
5868                 {
5869                         ATEDBGPRINT(RT_DEBUG_ERROR, ("copy_to_user() fail in case RACFG_CMD_ATE_SET_ADDR1\n"));
5870                     Status = -EFAULT;
5871                 }
5872                                 else
5873                                 {
5874                                         ATEDBGPRINT(RT_DEBUG_TRACE, ("RACFG_CMD_ATE_SET_ADDR1 is done !\n (ADDR1 = %2X:%2X:%2X:%2X:%2X:%2X)\n", pAdapter->ate.Addr1[0],
5875                                                 pAdapter->ate.Addr1[1], pAdapter->ate.Addr1[2], pAdapter->ate.Addr1[3], pAdapter->ate.Addr1[4], pAdapter->ate.Addr1[5]));
5876                                 }
5877                         }
5878                         break;
5879
5880                 case RACFG_CMD_ATE_SET_ADDR2:
5881                         {
5882                                 ATEDBGPRINT(RT_DEBUG_TRACE,("RACFG_CMD_ATE_SET_ADDR2\n"));
5883
5884                                 // Addr is an array of UCHAR,
5885                                 // so no need to perform endian swap.
5886                                 memcpy(pAdapter->ate.Addr2, (PUCHAR)(pRaCfg->data - 2), MAC_ADDR_LEN);
5887
5888                                 // prepare feedback
5889                                 pRaCfg->length = htons(2);
5890                                 pRaCfg->status = htons(0);
5891                                 wrq->u.data.length = sizeof(pRaCfg->magic_no) + sizeof(pRaCfg->command_type)
5892                                                                         + sizeof(pRaCfg->command_id) + sizeof(pRaCfg->length)
5893                                                                         + sizeof(pRaCfg->sequence) + ntohs(pRaCfg->length);
5894
5895                 if (copy_to_user(wrq->u.data.pointer, pRaCfg, wrq->u.data.length))
5896                 {
5897                         ATEDBGPRINT(RT_DEBUG_ERROR, ("copy_to_user() fail in case RACFG_CMD_ATE_SET_ADDR2\n"));
5898                     Status = -EFAULT;
5899                 }
5900                                 else
5901                                 {
5902                                         ATEDBGPRINT(RT_DEBUG_TRACE, ("RACFG_CMD_ATE_SET_ADDR2 is done !\n (ADDR2 = %2X:%2X:%2X:%2X:%2X:%2X)\n", pAdapter->ate.Addr2[0],
5903                                                 pAdapter->ate.Addr2[1], pAdapter->ate.Addr2[2], pAdapter->ate.Addr2[3], pAdapter->ate.Addr2[4], pAdapter->ate.Addr2[5]));
5904                                 }
5905                         }
5906                         break;
5907
5908                 case RACFG_CMD_ATE_SET_ADDR3:
5909                         {
5910                                 ATEDBGPRINT(RT_DEBUG_TRACE,("RACFG_CMD_ATE_SET_ADDR3\n"));
5911
5912                                 // Addr is an array of UCHAR,
5913                                 // so no need to perform endian swap.
5914                                 memcpy(pAdapter->ate.Addr3, (PUCHAR)(pRaCfg->data - 2), MAC_ADDR_LEN);
5915
5916                                 // prepare feedback
5917                                 pRaCfg->length = htons(2);
5918                                 pRaCfg->status = htons(0);
5919                                 wrq->u.data.length = sizeof(pRaCfg->magic_no) + sizeof(pRaCfg->command_type)
5920                                                                         + sizeof(pRaCfg->command_id) + sizeof(pRaCfg->length)
5921                                                                         + sizeof(pRaCfg->sequence) + ntohs(pRaCfg->length);
5922
5923                 if (copy_to_user(wrq->u.data.pointer, pRaCfg, wrq->u.data.length))
5924                 {
5925                         ATEDBGPRINT(RT_DEBUG_ERROR, ("copy_to_user() fail in case RACFG_CMD_ATE_SET_ADDR3\n"));
5926                     Status = -EFAULT;
5927                 }
5928                                 else
5929                                 {
5930                                         ATEDBGPRINT(RT_DEBUG_TRACE, ("RACFG_CMD_ATE_SET_ADDR3 is done !\n (ADDR3 = %2X:%2X:%2X:%2X:%2X:%2X)\n", pAdapter->ate.Addr3[0],
5931                                                 pAdapter->ate.Addr3[1], pAdapter->ate.Addr3[2], pAdapter->ate.Addr3[3], pAdapter->ate.Addr3[4], pAdapter->ate.Addr3[5]));
5932                                 }
5933                         }
5934                         break;
5935
5936                 case RACFG_CMD_ATE_SET_RATE:
5937                         {
5938                                 SHORT    value = 0;
5939                                 UCHAR    str[LEN_OF_ARG];
5940
5941                                 NdisZeroMemory(str, LEN_OF_ARG);
5942
5943                                 ATEDBGPRINT(RT_DEBUG_TRACE,("RACFG_CMD_ATE_SET_RATE\n"));
5944
5945                                 memcpy((PUCHAR)&value, (PUCHAR)&(pRaCfg->status), 2);
5946                                 value = ntohs(value);
5947                                 sprintf((PCHAR)str, "%d", value);
5948                                 Set_ATE_TX_MCS_Proc(pAdapter, str);
5949
5950                                 // prepare feedback
5951                                 pRaCfg->length = htons(2);
5952                                 pRaCfg->status = htons(0);
5953                                 wrq->u.data.length = sizeof(pRaCfg->magic_no) + sizeof(pRaCfg->command_type)
5954                                                                         + sizeof(pRaCfg->command_id) + sizeof(pRaCfg->length)
5955                                                                         + sizeof(pRaCfg->sequence) + ntohs(pRaCfg->length);
5956
5957                 if (copy_to_user(wrq->u.data.pointer, pRaCfg, wrq->u.data.length))
5958                 {
5959                         ATEDBGPRINT(RT_DEBUG_ERROR, ("copy_to_user() fail in case RACFG_CMD_ATE_SET_RATE\n"));
5960                     Status = -EFAULT;
5961                 }
5962                                 else
5963                                 {
5964                         ATEDBGPRINT(RT_DEBUG_TRACE, ("RACFG_CMD_ATE_SET_RATE is done !\n"));
5965                                 }
5966                         }
5967                         break;
5968
5969                 case RACFG_CMD_ATE_SET_TX_FRAME_LEN:
5970                         {
5971                                 SHORT    value = 0;
5972                                 UCHAR    str[LEN_OF_ARG];
5973
5974                                 NdisZeroMemory(str, LEN_OF_ARG);
5975
5976                                 ATEDBGPRINT(RT_DEBUG_TRACE,("RACFG_CMD_ATE_SET_TX_FRAME_LEN\n"));
5977
5978                                 memcpy((PUCHAR)&value, (PUCHAR)&(pRaCfg->status), 2);
5979                                 value = ntohs(value);
5980                                 sprintf((PCHAR)str, "%d", value);
5981                                 Set_ATE_TX_LENGTH_Proc(pAdapter, str);
5982
5983                                 // prepare feedback
5984                                 pRaCfg->length = htons(2);
5985                                 pRaCfg->status = htons(0);
5986                                 wrq->u.data.length = sizeof(pRaCfg->magic_no) + sizeof(pRaCfg->command_type)
5987                                                                         + sizeof(pRaCfg->command_id) + sizeof(pRaCfg->length)
5988                                                                         + sizeof(pRaCfg->sequence) + ntohs(pRaCfg->length);
5989
5990                 if (copy_to_user(wrq->u.data.pointer, pRaCfg, wrq->u.data.length))
5991                 {
5992                         ATEDBGPRINT(RT_DEBUG_ERROR, ("copy_to_user() fail in case RACFG_CMD_ATE_SET_TX_FRAME_LEN\n"));
5993                     Status = -EFAULT;
5994                 }
5995                                 else
5996                                 {
5997                         ATEDBGPRINT(RT_DEBUG_TRACE, ("RACFG_CMD_ATE_SET_TX_FRAME_LEN is done !\n"));
5998                                 }
5999                         }
6000                         break;
6001
6002                 case RACFG_CMD_ATE_SET_TX_FRAME_COUNT:
6003                         {
6004                                 USHORT    value = 0;
6005                                 UCHAR    str[LEN_OF_ARG];
6006
6007                                 NdisZeroMemory(str, LEN_OF_ARG);
6008
6009                                 ATEDBGPRINT(RT_DEBUG_TRACE,("RACFG_CMD_ATE_SET_TX_FRAME_COUNT\n"));
6010
6011                                 memcpy((PUCHAR)&value, (PUCHAR)&(pRaCfg->status), 2);
6012                                 value = ntohs(value);
6013                                 {
6014                                         sprintf((PCHAR)str, "%d", value);
6015                                         Set_ATE_TX_COUNT_Proc(pAdapter, str);
6016                                 }
6017
6018                                 // prepare feedback
6019                                 pRaCfg->length = htons(2);
6020                                 pRaCfg->status = htons(0);
6021                                 wrq->u.data.length = sizeof(pRaCfg->magic_no) + sizeof(pRaCfg->command_type)
6022                                                                         + sizeof(pRaCfg->command_id) + sizeof(pRaCfg->length)
6023                                                                         + sizeof(pRaCfg->sequence) + ntohs(pRaCfg->length);
6024
6025                 if (copy_to_user(wrq->u.data.pointer, pRaCfg, wrq->u.data.length))
6026                 {
6027                         ATEDBGPRINT(RT_DEBUG_ERROR, ("copy_to_user() fail in case RACFG_CMD_ATE_SET_TX_FRAME_COUNT\n"));
6028                     Status = -EFAULT;
6029                 }
6030                                 else
6031                                 {
6032                         ATEDBGPRINT(RT_DEBUG_TRACE, ("RACFG_CMD_ATE_SET_TX_FRAME_COUNT is done !\n"));
6033                                 }
6034                         }
6035                         break;
6036
6037                 case RACFG_CMD_ATE_START_RX_FRAME:
6038                         {
6039                                 ATEDBGPRINT(RT_DEBUG_TRACE,("RACFG_CMD_RX_START\n"));
6040
6041                                 Set_ATE_Proc(pAdapter, "RXFRAME");
6042
6043                                 // prepare feedback
6044                                 pRaCfg->length = htons(2);
6045                                 pRaCfg->status = htons(0);
6046                                 wrq->u.data.length = sizeof(pRaCfg->magic_no) + sizeof(pRaCfg->command_type)
6047                                                                         + sizeof(pRaCfg->command_id) + sizeof(pRaCfg->length)
6048                                                                         + sizeof(pRaCfg->sequence) + ntohs(pRaCfg->length);
6049
6050                 if (copy_to_user(wrq->u.data.pointer, pRaCfg, wrq->u.data.length))
6051                 {
6052                         ATEDBGPRINT(RT_DEBUG_ERROR, ("copy_to_user() fail in case RACFG_CMD_RX_START\n"));
6053                     Status = -EFAULT;
6054                 }
6055                                 else
6056                                 {
6057                         ATEDBGPRINT(RT_DEBUG_TRACE, ("RACFG_CMD_RX_START is done !\n"));
6058                                 }
6059                         }
6060                         break;
6061                 default:
6062                         break;
6063         }
6064     ASSERT(pRaCfg != NULL);
6065     if (pRaCfg != NULL)
6066     {
6067     kfree(pRaCfg);
6068     }
6069         return;
6070 }
6071
6072 VOID BubbleSort(INT32 n, INT32 a[])
6073 {
6074         INT32 k, j, temp;
6075
6076         for (k = n-1;  k>0;  k--)
6077         {
6078                 for (j = 0; j<k; j++)
6079                 {
6080                         if(a[j] > a[j+1])
6081                         {
6082                                 temp = a[j];
6083                                 a[j]=a[j+1];
6084                                 a[j+1]=temp;
6085                         }
6086                 }
6087         }
6088 }
6089
6090 VOID CalNoiseLevel(PRTMP_ADAPTER pAd, UCHAR channel, INT32 RSSI[3][10])
6091 {
6092         INT32           RSSI0, RSSI1, RSSI2;
6093         CHAR            Rssi0Offset, Rssi1Offset, Rssi2Offset;
6094         UCHAR           BbpR50Rssi0 = 0, BbpR51Rssi1 = 0, BbpR52Rssi2 = 0;
6095         UCHAR           Org_BBP66value = 0, Org_BBP69value = 0, Org_BBP70value = 0, data = 0;
6096         USHORT          LNA_Gain = 0;
6097         INT32       j = 0;
6098         UCHAR           Org_Channel = pAd->ate.Channel;
6099         USHORT      GainValue = 0, OffsetValue = 0;
6100
6101         ATE_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R66, &Org_BBP66value);
6102         ATE_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R69, &Org_BBP69value);
6103         ATE_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R70, &Org_BBP70value);
6104
6105         //**********************************************************************
6106         // Read the value of LNA gain and Rssi offset
6107         //**********************************************************************
6108         RT28xx_EEPROM_READ16(pAd, EEPROM_LNA_OFFSET, GainValue);
6109
6110         // for Noise Level
6111         if (channel <= 14)
6112         {
6113                 LNA_Gain = GainValue & 0x00FF;
6114
6115                 RT28xx_EEPROM_READ16(pAd, EEPROM_RSSI_BG_OFFSET, OffsetValue);
6116                 Rssi0Offset = OffsetValue & 0x00FF;
6117                 Rssi1Offset = (OffsetValue & 0xFF00) >> 8;
6118                 RT28xx_EEPROM_READ16(pAd, (EEPROM_RSSI_BG_OFFSET + 2)/* 0x48 */, OffsetValue);
6119                 Rssi2Offset = OffsetValue & 0x00FF;
6120         }
6121         else
6122         {
6123                 LNA_Gain = (GainValue & 0xFF00) >> 8;
6124
6125                 RT28xx_EEPROM_READ16(pAd, EEPROM_RSSI_A_OFFSET, OffsetValue);
6126                 Rssi0Offset = OffsetValue & 0x00FF;
6127                 Rssi1Offset = (OffsetValue & 0xFF00) >> 8;
6128                 RT28xx_EEPROM_READ16(pAd, (EEPROM_RSSI_A_OFFSET + 2)/* 0x4C */, OffsetValue);
6129                 Rssi2Offset = OffsetValue & 0x00FF;
6130         }
6131         //**********************************************************************
6132         {
6133                 pAd->ate.Channel = channel;
6134                 ATEAsicSwitchChannel(pAd);
6135                 mdelay(5);
6136
6137                 data = 0x10;
6138                 ATE_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R66, data);
6139                 data = 0x40;
6140                 ATE_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R69, data);
6141                 data = 0x40;
6142                 ATE_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R70, data);
6143                 mdelay(5);
6144
6145                 // Start Rx
6146                 pAd->ate.bQARxStart = TRUE;
6147                 Set_ATE_Proc(pAd, "RXFRAME");
6148
6149                 mdelay(5);
6150
6151                 for (j = 0; j < 10; j++)
6152                 {
6153                         ATE_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R50, &BbpR50Rssi0);
6154                         ATE_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R51, &BbpR51Rssi1);
6155                         ATE_BBP_IO_READ8_BY_REG_ID(pAd, BBP_R52, &BbpR52Rssi2);
6156
6157                         mdelay(10);
6158
6159                         // Calculate RSSI 0
6160                         if (BbpR50Rssi0 == 0)
6161                         {
6162                                 RSSI0 = -100;
6163                         }
6164                         else
6165                         {
6166                                 RSSI0 = (INT32)(-12 - BbpR50Rssi0 - LNA_Gain - Rssi0Offset);
6167                         }
6168                         RSSI[0][j] = RSSI0;
6169
6170                         if ( pAd->Antenna.field.RxPath >= 2 ) // 2R
6171                         {
6172                                 // Calculate RSSI 1
6173                                 if (BbpR51Rssi1 == 0)
6174                                 {
6175                                         RSSI1 = -100;
6176                                 }
6177                                 else
6178                                 {
6179                                         RSSI1 = (INT32)(-12 - BbpR51Rssi1 - LNA_Gain - Rssi1Offset);
6180                                 }
6181                                 RSSI[1][j] = RSSI1;
6182                         }
6183
6184                         if ( pAd->Antenna.field.RxPath >= 3 ) // 3R
6185                         {
6186                                 // Calculate RSSI 2
6187                                 if (BbpR52Rssi2 == 0)
6188                                         RSSI2 = -100;
6189                                 else
6190                                         RSSI2 = (INT32)(-12 - BbpR52Rssi2 - LNA_Gain - Rssi2Offset);
6191
6192                                 RSSI[2][j] = RSSI2;
6193                         }
6194                 }
6195
6196                 // Stop Rx
6197                 Set_ATE_Proc(pAd, "RXSTOP");
6198
6199                 mdelay(5);
6200
6201                 BubbleSort(10, RSSI[0]);        // 1R
6202
6203                 if ( pAd->Antenna.field.RxPath >= 2 ) // 2R
6204                 {
6205                         BubbleSort(10, RSSI[1]);
6206                 }
6207
6208                 if ( pAd->Antenna.field.RxPath >= 3 ) // 3R
6209                 {
6210                         BubbleSort(10, RSSI[2]);
6211                 }
6212
6213         }
6214
6215         pAd->ate.Channel = Org_Channel;
6216         ATEAsicSwitchChannel(pAd);
6217
6218         // Restore original value
6219     ATE_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R66, Org_BBP66value);
6220     ATE_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R69, Org_BBP69value);
6221     ATE_BBP_IO_WRITE8_BY_REG_ID(pAd, BBP_R70, Org_BBP70value);
6222
6223         return;
6224 }
6225
6226 BOOLEAN SyncTxRxConfig(PRTMP_ADAPTER pAd, USHORT offset, UCHAR value)
6227 {
6228         UCHAR tmp = 0, bbp_data = 0;
6229
6230         if (ATE_ON(pAd))
6231         {
6232                 ATE_BBP_IO_READ8_BY_REG_ID(pAd, offset, &bbp_data);
6233         }
6234         else
6235         {
6236                 RTMP_BBP_IO_READ8_BY_REG_ID(pAd, offset, &bbp_data);
6237         }
6238
6239         /* confirm again */
6240         ASSERT(bbp_data == value);
6241
6242         switch(offset)
6243         {
6244                 case BBP_R1:
6245                         /* Need to sync. tx configuration with legacy ATE. */
6246                         tmp = (bbp_data & ((1 << 4) | (1 << 3))/* 0x18 */) >> 3;
6247                     switch(tmp)
6248                     {
6249                                 /* The BBP R1 bit[4:3] = 2 :: Both DACs will be used by QA. */
6250                         case 2:
6251                                         /* All */
6252                                         pAd->ate.TxAntennaSel = 0;
6253                             break;
6254                                 /* The BBP R1 bit[4:3] = 0 :: DAC 0 will be used by QA. */
6255                         case 0:
6256                                         /* Antenna one */
6257                                         pAd->ate.TxAntennaSel = 1;
6258                             break;
6259                                 /* The BBP R1 bit[4:3] = 1 :: DAC 1 will be used by QA. */
6260                         case 1:
6261                                         /* Antenna two */
6262                                         pAd->ate.TxAntennaSel = 2;
6263                             break;
6264                         default:
6265                             DBGPRINT(RT_DEBUG_TRACE, ("%s -- Sth. wrong!  : return FALSE; \n", __func__));
6266                             return FALSE;
6267                     }
6268                         break;/* case BBP_R1 */
6269
6270                 case BBP_R3:
6271                         /* Need to sync. rx configuration with legacy ATE. */
6272                         tmp = (bbp_data & ((1 << 1) | (1 << 0))/* 0x03 */);
6273                     switch(tmp)
6274                     {
6275                                 /* The BBP R3 bit[1:0] = 3 :: All ADCs will be used by QA. */
6276                         case 3:
6277                                         /* All */
6278                                         pAd->ate.RxAntennaSel = 0;
6279                             break;
6280                                 /* The BBP R3 bit[1:0] = 0 :: ADC 0 will be used by QA, */
6281                                 /* unless the BBP R3 bit[4:3] = 2 */
6282                         case 0:
6283                                         /* Antenna one */
6284                                         pAd->ate.RxAntennaSel = 1;
6285                                         tmp = ((bbp_data & ((1 << 4) | (1 << 3))/* 0x03 */) >> 3);
6286                                         if (tmp == 2)// 3R
6287                                         {
6288                                                 /* Default : All ADCs will be used by QA */
6289                                                 pAd->ate.RxAntennaSel = 0;
6290                                         }
6291                             break;
6292                                 /* The BBP R3 bit[1:0] = 1 :: ADC 1 will be used by QA. */
6293                         case 1:
6294                                         /* Antenna two */
6295                                         pAd->ate.RxAntennaSel = 2;
6296                             break;
6297                                 /* The BBP R3 bit[1:0] = 2 :: ADC 2 will be used by QA. */
6298                         case 2:
6299                                         /* Antenna three */
6300                                         pAd->ate.RxAntennaSel = 3;
6301                             break;
6302                         default:
6303                             DBGPRINT(RT_DEBUG_ERROR, ("%s -- Impossible!  : return FALSE; \n", __func__));
6304                             return FALSE;
6305                     }
6306                         break;/* case BBP_R3 */
6307
6308         default:
6309             DBGPRINT(RT_DEBUG_ERROR, ("%s -- Sth. wrong!  : return FALSE; \n", __func__));
6310             return FALSE;
6311
6312         }
6313         return TRUE;
6314 }
6315
6316 static VOID memcpy_exl(PRTMP_ADAPTER pAd, UCHAR *dst, UCHAR *src, ULONG len)
6317 {
6318         ULONG i, Value = 0;
6319         ULONG *pDst, *pSrc;
6320         UCHAR *p8;
6321
6322         p8 = src;
6323         pDst = (ULONG *) dst;
6324         pSrc = (ULONG *) src;
6325
6326         for (i = 0 ; i < (len/4); i++)
6327         {
6328                 /* For alignment issue, we need a variable "Value". */
6329                 memmove(&Value, pSrc, 4);
6330                 Value = htonl(Value);
6331                 memmove(pDst, &Value, 4);
6332                 pDst++;
6333                 pSrc++;
6334         }
6335         if ((len % 4) != 0)
6336         {
6337                 /* wish that it will never reach here */
6338                 memmove(&Value, pSrc, (len % 4));
6339                 Value = htonl(Value);
6340                 memmove(pDst, &Value, (len % 4));
6341         }
6342 }
6343
6344 static VOID memcpy_exs(PRTMP_ADAPTER pAd, UCHAR *dst, UCHAR *src, ULONG len)
6345 {
6346         ULONG i;
6347         UCHAR *pDst, *pSrc;
6348
6349         pDst = dst;
6350         pSrc = src;
6351
6352         for (i = 0; i < (len/2); i++)
6353         {
6354                 memmove(pDst, pSrc, 2);
6355                 *((USHORT *)pDst) = htons(*((USHORT *)pDst));
6356                 pDst+=2;
6357                 pSrc+=2;
6358         }
6359
6360         if ((len % 2) != 0)
6361         {
6362                 memmove(pDst, pSrc, 1);
6363         }
6364 }
6365
6366 static VOID RTMP_IO_READ_BULK(PRTMP_ADAPTER pAd, UCHAR *dst, UCHAR *src, UINT32 len)
6367 {
6368         UINT32 i, Value;
6369         UINT32 *pDst, *pSrc;
6370
6371         pDst = (UINT32 *) dst;
6372         pSrc = (UINT32 *) src;
6373
6374         for (i = 0 ; i < (len/4); i++)
6375         {
6376                 RTMP_IO_READ32(pAd, (ULONG)pSrc, &Value);
6377                 Value = htonl(Value);
6378                 memmove(pDst, &Value, 4);
6379                 pDst++;
6380                 pSrc++;
6381         }
6382         return;
6383 }
6384
6385 INT Set_TxStop_Proc(
6386         IN      PRTMP_ADAPTER   pAd,
6387         IN      PUCHAR                  arg)
6388 {
6389         ATEDBGPRINT(RT_DEBUG_TRACE,("Set_TxStop_Proc\n"));
6390
6391         if (Set_ATE_Proc(pAd, "TXSTOP"))
6392         {
6393         return TRUE;
6394 }
6395         else
6396         {
6397                 return FALSE;
6398         }
6399 }
6400
6401 INT Set_RxStop_Proc(
6402         IN      PRTMP_ADAPTER   pAd,
6403         IN      PUCHAR                  arg)
6404 {
6405         ATEDBGPRINT(RT_DEBUG_TRACE,("Set_RxStop_Proc\n"));
6406
6407         if (Set_ATE_Proc(pAd, "RXSTOP"))
6408         {
6409         return TRUE;
6410 }
6411         else
6412         {
6413                 return FALSE;
6414         }
6415 }
6416 #endif  // RALINK_28xx_QA //
6417 #endif  // RALINK_ATE //
6418