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