Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/arnd/asm...
[linux-2.6] / drivers / staging / rt2860 / sta_ioctl.c
1 /*
2  *************************************************************************
3  * Ralink Tech Inc.
4  * 5F., No.36, Taiyuan St., Jhubei City,
5  * Hsinchu County 302,
6  * Taiwan, R.O.C.
7  *
8  * (c) Copyright 2002-2007, Ralink Technology, Inc.
9  *
10  * This program is free software; you can redistribute it and/or modify  *
11  * it under the terms of the GNU General Public License as published by  *
12  * the Free Software Foundation; either version 2 of the License, or     *
13  * (at your option) any later version.                                   *
14  *                                                                       *
15  * This program is distributed in the hope that it will be useful,       *
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
18  * GNU General Public License for more details.                          *
19  *                                                                       *
20  * You should have received a copy of the GNU General Public License     *
21  * along with this program; if not, write to the                         *
22  * Free Software Foundation, Inc.,                                       *
23  * 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
24  *                                                                       *
25  *************************************************************************
26
27     Module Name:
28     sta_ioctl.c
29
30     Abstract:
31     IOCTL related subroutines
32
33     Revision History:
34     Who         When          What
35     --------    ----------    ----------------------------------------------
36     Rory Chen   01-03-2003    created
37         Rory Chen   02-14-2005    modify to support RT61
38 */
39
40 #include        "rt_config.h"
41
42 #ifdef DBG
43 extern ULONG    RTDebugLevel;
44 #endif
45
46 #define NR_WEP_KEYS                             4
47 #define WEP_SMALL_KEY_LEN                       (40/8)
48 #define WEP_LARGE_KEY_LEN                       (104/8)
49
50 #define GROUP_KEY_NO                4
51
52 extern UCHAR    CipherWpa2Template[];
53 extern UCHAR    CipherWpaPskTkip[];
54 extern UCHAR    CipherWpaPskTkipLen;
55
56 typedef struct PACKED _RT_VERSION_INFO{
57     UCHAR       DriverVersionW;
58     UCHAR       DriverVersionX;
59     UCHAR       DriverVersionY;
60     UCHAR       DriverVersionZ;
61     UINT        DriverBuildYear;
62     UINT        DriverBuildMonth;
63     UINT        DriverBuildDay;
64 } RT_VERSION_INFO, *PRT_VERSION_INFO;
65
66 struct iw_priv_args privtab[] = {
67 { RTPRIV_IOCTL_SET,
68   IW_PRIV_TYPE_CHAR | 1024, 0,
69   "set"},
70
71 { RTPRIV_IOCTL_SHOW, 0, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK,
72   ""},
73 { RTPRIV_IOCTL_SHOW, IW_PRIV_TYPE_CHAR | 1024, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK,
74   ""},
75 /* --- sub-ioctls definitions --- */
76     { SHOW_CONN_STATUS,
77           0, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "connStatus" },
78         { SHOW_DRVIER_VERION,
79           0, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "driverVer" },
80     { SHOW_BA_INFO,
81           0, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "bainfo" },
82         { SHOW_DESC_INFO,
83           0, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "descinfo" },
84     { RAIO_OFF,
85           0, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "radio_off" },
86         { RAIO_ON,
87           0, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "radio_on" },
88         { SHOW_CFG_VALUE,
89           IW_PRIV_TYPE_CHAR | 1024, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "show" },
90 #if !defined(RT2860) && !defined(RT30xx)
91         { SHOW_ADHOC_ENTRY_INFO,
92           0, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "adhocEntry" },
93 #endif
94 /* --- sub-ioctls relations --- */
95
96 #ifdef DBG
97 { RTPRIV_IOCTL_BBP,
98   IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK,
99   "bbp"},
100 { RTPRIV_IOCTL_MAC,
101   IW_PRIV_TYPE_CHAR | 1024, IW_PRIV_TYPE_CHAR | 1024,
102   "mac"},
103 #ifdef RT30xx
104 { RTPRIV_IOCTL_RF,
105   IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK,
106   "rf"},
107 #endif // RT30xx //
108 { RTPRIV_IOCTL_E2P,
109   IW_PRIV_TYPE_CHAR | 1024, IW_PRIV_TYPE_CHAR | 1024,
110   "e2p"},
111 #endif  /* DBG */
112
113 { RTPRIV_IOCTL_STATISTICS,
114   0, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK,
115   "stat"},
116 { RTPRIV_IOCTL_GSITESURVEY,
117   0, IW_PRIV_TYPE_CHAR | 1024,
118   "get_site_survey"},
119 };
120
121 INT Set_SSID_Proc(
122     IN  PRTMP_ADAPTER   pAdapter,
123     IN  PUCHAR          arg);
124
125 #ifdef WMM_SUPPORT
126 INT     Set_WmmCapable_Proc(
127         IN      PRTMP_ADAPTER   pAd,
128         IN      PUCHAR                  arg);
129 #endif
130
131 INT Set_NetworkType_Proc(
132     IN  PRTMP_ADAPTER   pAdapter,
133     IN  PUCHAR          arg);
134
135 INT Set_AuthMode_Proc(
136     IN  PRTMP_ADAPTER   pAdapter,
137     IN  PUCHAR          arg);
138
139 INT Set_EncrypType_Proc(
140     IN  PRTMP_ADAPTER   pAdapter,
141     IN  PUCHAR          arg);
142
143 INT Set_DefaultKeyID_Proc(
144     IN  PRTMP_ADAPTER   pAdapter,
145     IN  PUCHAR          arg);
146
147 INT Set_Key1_Proc(
148     IN  PRTMP_ADAPTER   pAdapter,
149     IN  PUCHAR          arg);
150
151 INT Set_Key2_Proc(
152     IN  PRTMP_ADAPTER   pAdapter,
153     IN  PUCHAR          arg);
154
155 INT Set_Key3_Proc(
156     IN  PRTMP_ADAPTER   pAdapter,
157     IN  PUCHAR          arg);
158
159 INT Set_Key4_Proc(
160     IN  PRTMP_ADAPTER   pAdapter,
161     IN  PUCHAR          arg);
162
163 INT Set_WPAPSK_Proc(
164     IN  PRTMP_ADAPTER   pAdapter,
165     IN  PUCHAR          arg);
166
167
168 INT Set_PSMode_Proc(
169     IN  PRTMP_ADAPTER   pAdapter,
170     IN  PUCHAR          arg);
171
172 INT Set_Wpa_Support(
173     IN  PRTMP_ADAPTER   pAd,
174         IN      PUCHAR                  arg);
175
176 #ifdef DBG
177 #if !defined(RT2860) && !defined(RT30xx)
178 VOID RTMPIoctlBBP(
179         IN      PRTMP_ADAPTER   pAdapter,
180         IN      struct iwreq    *wrq);
181 #endif
182
183 VOID RTMPIoctlMAC(
184         IN      PRTMP_ADAPTER   pAdapter,
185         IN      struct iwreq    *wrq);
186
187 VOID RTMPIoctlE2PROM(
188     IN  PRTMP_ADAPTER   pAdapter,
189     IN  struct iwreq    *wrq);
190
191 #ifdef RT30xx
192 VOID RTMPIoctlRF(
193     IN  PRTMP_ADAPTER   pAdapter,
194     IN  struct iwreq    *wrq);
195 #endif // RT30xx //
196 #endif // DBG //
197
198
199 NDIS_STATUS RTMPWPANoneAddKeyProc(
200     IN  PRTMP_ADAPTER   pAd,
201     IN  PVOID                   pBuf);
202
203 INT Set_FragTest_Proc(
204     IN  PRTMP_ADAPTER   pAdapter,
205     IN  PUCHAR          arg);
206
207 INT Set_TGnWifiTest_Proc(
208     IN  PRTMP_ADAPTER   pAd,
209     IN  PUCHAR          arg);
210
211 INT Set_LongRetryLimit_Proc(
212         IN      PRTMP_ADAPTER   pAdapter,
213         IN      PUCHAR                  arg);
214
215 INT Set_ShortRetryLimit_Proc(
216         IN      PRTMP_ADAPTER   pAdapter,
217         IN      PUCHAR                  arg);
218
219 #if !defined(RT2860) && !defined(RT30xx)
220 INT     Show_Adhoc_MacTable_Proc(
221         IN      PRTMP_ADAPTER   pAd,
222         IN      PCHAR                   extra);
223 #endif
224
225 static struct {
226         CHAR *name;
227         INT (*set_proc)(PRTMP_ADAPTER pAdapter, PUCHAR arg);
228 } *PRTMP_PRIVATE_SET_PROC, RTMP_PRIVATE_SUPPORT_PROC[] = {
229         {"DriverVersion",                               Set_DriverVersion_Proc},
230         {"CountryRegion",                               Set_CountryRegion_Proc},
231         {"CountryRegionABand",                  Set_CountryRegionABand_Proc},
232         {"SSID",                                                Set_SSID_Proc},
233         {"WirelessMode",                                Set_WirelessMode_Proc},
234         {"TxBurst",                                     Set_TxBurst_Proc},
235         {"TxPreamble",                          Set_TxPreamble_Proc},
236         {"TxPower",                                     Set_TxPower_Proc},
237         {"Channel",                                     Set_Channel_Proc},
238         {"BGProtection",                                Set_BGProtection_Proc},
239         {"RTSThreshold",                                Set_RTSThreshold_Proc},
240         {"FragThreshold",                               Set_FragThreshold_Proc},
241         {"HtBw",                                Set_HtBw_Proc},
242         {"HtMcs",                               Set_HtMcs_Proc},
243         {"HtGi",                                Set_HtGi_Proc},
244         {"HtOpMode",                        Set_HtOpMode_Proc},
245         {"HtExtcha",                        Set_HtExtcha_Proc},
246         {"HtMpduDensity",                       Set_HtMpduDensity_Proc},
247         {"HtBaWinSize",                         Set_HtBaWinSize_Proc},
248         {"HtRdg",                                       Set_HtRdg_Proc},
249         {"HtAmsdu",                                     Set_HtAmsdu_Proc},
250         {"HtAutoBa",                            Set_HtAutoBa_Proc},
251         {"HtBaDecline",                                 Set_BADecline_Proc},
252         {"HtProtect",                           Set_HtProtect_Proc},
253         {"HtMimoPs",                            Set_HtMimoPs_Proc},
254 #ifdef AGGREGATION_SUPPORT
255         {"PktAggregate",                                Set_PktAggregate_Proc},
256 #endif
257
258 #ifdef WMM_SUPPORT
259         {"WmmCapable",                                  Set_WmmCapable_Proc},
260 #endif
261         {"IEEE80211H",                                  Set_IEEE80211H_Proc},
262     {"NetworkType",                 Set_NetworkType_Proc},
263         {"AuthMode",                                    Set_AuthMode_Proc},
264         {"EncrypType",                                  Set_EncrypType_Proc},
265         {"DefaultKeyID",                                Set_DefaultKeyID_Proc},
266         {"Key1",                                                Set_Key1_Proc},
267         {"Key2",                                                Set_Key2_Proc},
268         {"Key3",                                                Set_Key3_Proc},
269         {"Key4",                                                Set_Key4_Proc},
270         {"WPAPSK",                                              Set_WPAPSK_Proc},
271         {"ResetCounter",                                Set_ResetStatCounter_Proc},
272         {"PSMode",                      Set_PSMode_Proc},
273 #ifdef DBG
274         {"Debug",                                               Set_Debug_Proc},
275 #endif
276     {"WpaSupport",                  Set_Wpa_Support},
277         {"FixedTxMode",                 Set_FixedTxMode_Proc},
278     {"TGnWifiTest",                 Set_TGnWifiTest_Proc},
279     {"ForceGF",                                 Set_ForceGF_Proc},
280         {"LongRetry",                           Set_LongRetryLimit_Proc},
281         {"ShortRetry",                          Set_ShortRetryLimit_Proc},
282 //2008/09/11:KH add to support efuse<--
283 #ifdef RT30xx
284         {"efuseFreeNumber",                             set_eFuseGetFreeBlockCount_Proc},
285         {"efuseDump",                                   set_eFusedump_Proc},
286         {"efuseLoadFromBin",                            set_eFuseLoadFromBin_Proc},
287 #endif // RT30xx //
288 //2008/09/11:KH add to support efuse-->
289         {NULL,}
290 };
291
292
293 VOID RTMPAddKey(
294         IN      PRTMP_ADAPTER       pAd,
295         IN      PNDIS_802_11_KEY    pKey)
296 {
297         ULONG                           KeyIdx;
298         MAC_TABLE_ENTRY         *pEntry;
299
300     DBGPRINT(RT_DEBUG_TRACE, ("RTMPAddKey ------>\n"));
301
302 #ifdef RT2860
303         RTMP_CLEAR_PSFLAG(pAd, fRTMP_PS_CAN_GO_SLEEP);
304         if (RTMP_TEST_PSFLAG(pAd, fRTMP_PS_SET_PCI_CLK_OFF_COMMAND))
305         {
306                 if (pAd->StaCfg.bRadio == FALSE)
307                 {
308                         RTMP_SET_PSFLAG(pAd, fRTMP_PS_CAN_GO_SLEEP);
309                         return;
310                 }
311                 DBGPRINT(RT_DEBUG_TRACE,("RTMPWPAAddKeyProc1==>\n"));
312                 RTMPPCIeLinkCtrlValueRestore(pAd, RESTORE_HALT);
313                 RTMPusecDelay(6000);
314                 pAd->bPCIclkOff = FALSE;
315         }
316 #endif
317
318         if (pAd->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
319         {
320                 if (pKey->KeyIndex & 0x80000000)
321                 {
322                     if (pAd->StaCfg.AuthMode == Ndis802_11AuthModeWPANone)
323             {
324                 NdisZeroMemory(pAd->StaCfg.PMK, 32);
325                 NdisMoveMemory(pAd->StaCfg.PMK, pKey->KeyMaterial, pKey->KeyLength);
326                 goto end;
327             }
328                     // Update PTK
329                     NdisZeroMemory(&pAd->SharedKey[BSS0][0], sizeof(CIPHER_KEY));
330             pAd->SharedKey[BSS0][0].KeyLen = LEN_TKIP_EK;
331             NdisMoveMemory(pAd->SharedKey[BSS0][0].Key, pKey->KeyMaterial, LEN_TKIP_EK);
332
333             if (pAd->StaCfg.PairCipher == Ndis802_11Encryption2Enabled)
334             {
335                 NdisMoveMemory(pAd->SharedKey[BSS0][0].RxMic, pKey->KeyMaterial + LEN_TKIP_EK, LEN_TKIP_TXMICK);
336                 NdisMoveMemory(pAd->SharedKey[BSS0][0].TxMic, pKey->KeyMaterial + LEN_TKIP_EK + LEN_TKIP_TXMICK, LEN_TKIP_RXMICK);
337             }
338             else
339             {
340                 NdisMoveMemory(pAd->SharedKey[BSS0][0].TxMic, pKey->KeyMaterial + LEN_TKIP_EK, LEN_TKIP_TXMICK);
341                 NdisMoveMemory(pAd->SharedKey[BSS0][0].RxMic, pKey->KeyMaterial + LEN_TKIP_EK + LEN_TKIP_TXMICK, LEN_TKIP_RXMICK);
342             }
343
344             // Decide its ChiperAlg
345                 if (pAd->StaCfg.PairCipher == Ndis802_11Encryption2Enabled)
346                         pAd->SharedKey[BSS0][0].CipherAlg = CIPHER_TKIP;
347                 else if (pAd->StaCfg.PairCipher == Ndis802_11Encryption3Enabled)
348                         pAd->SharedKey[BSS0][0].CipherAlg = CIPHER_AES;
349                 else
350                         pAd->SharedKey[BSS0][0].CipherAlg = CIPHER_NONE;
351
352             // Update these related information to MAC_TABLE_ENTRY
353                 pEntry = &pAd->MacTab.Content[BSSID_WCID];
354             NdisMoveMemory(pEntry->PairwiseKey.Key, pAd->SharedKey[BSS0][0].Key, LEN_TKIP_EK);
355                 NdisMoveMemory(pEntry->PairwiseKey.RxMic, pAd->SharedKey[BSS0][0].RxMic, LEN_TKIP_RXMICK);
356                 NdisMoveMemory(pEntry->PairwiseKey.TxMic, pAd->SharedKey[BSS0][0].TxMic, LEN_TKIP_TXMICK);
357                 pEntry->PairwiseKey.CipherAlg = pAd->SharedKey[BSS0][0].CipherAlg;
358
359                 // Update pairwise key information to ASIC Shared Key Table
360                 AsicAddSharedKeyEntry(pAd,
361                                                           BSS0,
362                                                           0,
363                                                           pAd->SharedKey[BSS0][0].CipherAlg,
364                                                           pAd->SharedKey[BSS0][0].Key,
365                                                           pAd->SharedKey[BSS0][0].TxMic,
366                                                           pAd->SharedKey[BSS0][0].RxMic);
367
368                 // Update ASIC WCID attribute table and IVEIV table
369                 RTMPAddWcidAttributeEntry(pAd,
370                                                                   BSS0,
371                                                                   0,
372                                                                   pAd->SharedKey[BSS0][0].CipherAlg,
373                                                                   pEntry);
374
375             if (pAd->StaCfg.AuthMode >= Ndis802_11AuthModeWPA2)
376             {
377                 // set 802.1x port control
378                                 STA_PORT_SECURED(pAd);
379
380                 // Indicate Connected for GUI
381                 pAd->IndicateMediaState = NdisMediaStateConnected;
382             }
383                 }
384         else
385         {
386             // Update GTK
387             pAd->StaCfg.DefaultKeyId = (pKey->KeyIndex & 0xFF);
388             NdisZeroMemory(&pAd->SharedKey[BSS0][pAd->StaCfg.DefaultKeyId], sizeof(CIPHER_KEY));
389             pAd->SharedKey[BSS0][pAd->StaCfg.DefaultKeyId].KeyLen = LEN_TKIP_EK;
390             NdisMoveMemory(pAd->SharedKey[BSS0][pAd->StaCfg.DefaultKeyId].Key, pKey->KeyMaterial, LEN_TKIP_EK);
391
392             if (pAd->StaCfg.GroupCipher == Ndis802_11Encryption2Enabled)
393             {
394                 NdisMoveMemory(pAd->SharedKey[BSS0][pAd->StaCfg.DefaultKeyId].RxMic, pKey->KeyMaterial + LEN_TKIP_EK, LEN_TKIP_TXMICK);
395                 NdisMoveMemory(pAd->SharedKey[BSS0][pAd->StaCfg.DefaultKeyId].TxMic, pKey->KeyMaterial + LEN_TKIP_EK + LEN_TKIP_TXMICK, LEN_TKIP_RXMICK);
396             }
397             else
398             {
399                 NdisMoveMemory(pAd->SharedKey[BSS0][pAd->StaCfg.DefaultKeyId].TxMic, pKey->KeyMaterial + LEN_TKIP_EK, LEN_TKIP_TXMICK);
400                 NdisMoveMemory(pAd->SharedKey[BSS0][pAd->StaCfg.DefaultKeyId].RxMic, pKey->KeyMaterial + LEN_TKIP_EK + LEN_TKIP_TXMICK, LEN_TKIP_RXMICK);
401             }
402
403             // Update Shared Key CipherAlg
404                 pAd->SharedKey[BSS0][pAd->StaCfg.DefaultKeyId].CipherAlg = CIPHER_NONE;
405                 if (pAd->StaCfg.GroupCipher == Ndis802_11Encryption2Enabled)
406                         pAd->SharedKey[BSS0][pAd->StaCfg.DefaultKeyId].CipherAlg = CIPHER_TKIP;
407                 else if (pAd->StaCfg.GroupCipher == Ndis802_11Encryption3Enabled)
408                         pAd->SharedKey[BSS0][pAd->StaCfg.DefaultKeyId].CipherAlg = CIPHER_AES;
409
410             // Update group key information to ASIC Shared Key Table
411                 AsicAddSharedKeyEntry(pAd,
412                                                           BSS0,
413                                                           pAd->StaCfg.DefaultKeyId,
414                                                           pAd->SharedKey[BSS0][pAd->StaCfg.DefaultKeyId].CipherAlg,
415                                                           pAd->SharedKey[BSS0][pAd->StaCfg.DefaultKeyId].Key,
416                                                           pAd->SharedKey[BSS0][pAd->StaCfg.DefaultKeyId].TxMic,
417                                                           pAd->SharedKey[BSS0][pAd->StaCfg.DefaultKeyId].RxMic);
418
419                 // Update ASIC WCID attribute table and IVEIV table
420                 RTMPAddWcidAttributeEntry(pAd,
421                                                                   BSS0,
422                                                                   pAd->StaCfg.DefaultKeyId,
423                                                                   pAd->SharedKey[BSS0][pAd->StaCfg.DefaultKeyId].CipherAlg,
424                                                                   NULL);
425
426             // set 802.1x port control
427                         STA_PORT_SECURED(pAd);
428
429             // Indicate Connected for GUI
430             pAd->IndicateMediaState = NdisMediaStateConnected;
431         }
432         }
433         else    // dynamic WEP from wpa_supplicant
434         {
435                 UCHAR   CipherAlg;
436         PUCHAR  Key;
437
438                 if(pKey->KeyLength == 32)
439                         goto end;
440
441                 KeyIdx = pKey->KeyIndex & 0x0fffffff;
442
443                 if (KeyIdx < 4)
444                 {
445                         // it is a default shared key, for Pairwise key setting
446                         if (pKey->KeyIndex & 0x80000000)
447                         {
448                                 pEntry = MacTableLookup(pAd, pKey->BSSID);
449
450                                 if (pEntry)
451                                 {
452                                         DBGPRINT(RT_DEBUG_TRACE, ("RTMPAddKey: Set Pair-wise Key\n"));
453
454                                         // set key material and key length
455                                         pEntry->PairwiseKey.KeyLen = (UCHAR)pKey->KeyLength;
456                                         NdisMoveMemory(pEntry->PairwiseKey.Key, &pKey->KeyMaterial, pKey->KeyLength);
457
458                                         // set Cipher type
459                                         if (pKey->KeyLength == 5)
460                                                 pEntry->PairwiseKey.CipherAlg = CIPHER_WEP64;
461                                         else
462                                                 pEntry->PairwiseKey.CipherAlg = CIPHER_WEP128;
463
464                                         // Add Pair-wise key to Asic
465                                         AsicAddPairwiseKeyEntry(
466                                                 pAd,
467                                                 pEntry->Addr,
468                                                 (UCHAR)pEntry->Aid,
469                                 &pEntry->PairwiseKey);
470
471                                         // update WCID attribute table and IVEIV table for this entry
472                                         RTMPAddWcidAttributeEntry(
473                                                 pAd,
474                                                 BSS0,
475                                                 KeyIdx, // The value may be not zero
476                                                 pEntry->PairwiseKey.CipherAlg,
477                                                 pEntry);
478
479                                 }
480                         }
481                         else
482             {
483                                 // Default key for tx (shared key)
484                                 pAd->StaCfg.DefaultKeyId = (UCHAR) KeyIdx;
485
486                                 // set key material and key length
487                                 pAd->SharedKey[BSS0][KeyIdx].KeyLen = (UCHAR) pKey->KeyLength;
488                                 NdisMoveMemory(pAd->SharedKey[BSS0][KeyIdx].Key, &pKey->KeyMaterial, pKey->KeyLength);
489
490                                 // Set Ciper type
491                                 if (pKey->KeyLength == 5)
492                                         pAd->SharedKey[BSS0][KeyIdx].CipherAlg = CIPHER_WEP64;
493                                 else
494                                         pAd->SharedKey[BSS0][KeyIdx].CipherAlg = CIPHER_WEP128;
495
496                         CipherAlg = pAd->SharedKey[BSS0][KeyIdx].CipherAlg;
497                         Key = pAd->SharedKey[BSS0][KeyIdx].Key;
498
499                                 // Set Group key material to Asic
500                         AsicAddSharedKeyEntry(pAd, BSS0, KeyIdx, CipherAlg, Key, NULL, NULL);
501
502                                 // Update WCID attribute table and IVEIV table for this group key table
503                                 RTMPAddWcidAttributeEntry(pAd, BSS0, KeyIdx, CipherAlg, NULL);
504
505                         }
506                 }
507         }
508 end:
509 #ifdef RT2860
510         RTMP_SET_PSFLAG(pAd, fRTMP_PS_CAN_GO_SLEEP);
511     DBGPRINT(RT_DEBUG_INFO, ("<------ RTMPAddKey\n"));
512 #endif
513         return;
514 }
515
516 char * rtstrchr(const char * s, int c)
517 {
518     for(; *s != (char) c; ++s)
519         if (*s == '\0')
520             return NULL;
521     return (char *) s;
522 }
523
524 /*
525 This is required for LinEX2004/kernel2.6.7 to provide iwlist scanning function
526 */
527
528 int
529 rt_ioctl_giwname(struct net_device *dev,
530                    struct iw_request_info *info,
531                    char *name, char *extra)
532 {
533 //      PRTMP_ADAPTER pAdapter = dev->ml_priv;
534 #ifdef RT2860
535     strncpy(name, "RT2860 Wireless", IFNAMSIZ);
536 #endif
537 #ifdef RT2870
538         strncpy(name, "RT2870 Wireless", IFNAMSIZ);
539 #endif // RT2870 //
540         return 0;
541 }
542
543 int rt_ioctl_siwfreq(struct net_device *dev,
544                         struct iw_request_info *info,
545                         struct iw_freq *freq, char *extra)
546 {
547         PRTMP_ADAPTER pAdapter = dev->ml_priv;
548         int     chan = -1;
549
550     //check if the interface is down
551     if(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE))
552     {
553         DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
554         return -ENETDOWN;
555     }
556
557
558         if (freq->e > 1)
559                 return -EINVAL;
560
561         if((freq->e == 0) && (freq->m <= 1000))
562                 chan = freq->m; // Setting by channel number
563         else
564                 MAP_KHZ_TO_CHANNEL_ID( (freq->m /100) , chan); // Setting by frequency - search the table , like 2.412G, 2.422G,
565
566     if (ChannelSanity(pAdapter, chan) == TRUE)
567     {
568         pAdapter->CommonCfg.Channel = chan;
569         DBGPRINT(RT_DEBUG_ERROR, ("==>rt_ioctl_siwfreq::SIOCSIWFREQ[cmd=0x%x] (Channel=%d)\n", SIOCSIWFREQ, pAdapter->CommonCfg.Channel));
570     }
571     else
572         return -EINVAL;
573
574         return 0;
575 }
576 int rt_ioctl_giwfreq(struct net_device *dev,
577                    struct iw_request_info *info,
578                    struct iw_freq *freq, char *extra)
579 {
580     VIRTUAL_ADAPTER *pVirtualAd = NULL;
581 #ifndef RT30xx
582         PRTMP_ADAPTER pAdapter = NULL;
583 #endif
584 #ifdef RT30xx
585         PRTMP_ADAPTER pAdapter;
586 #endif
587         UCHAR ch;
588         ULONG   m;
589
590         if (dev->priv_flags == INT_MAIN)
591         {
592                 pAdapter = dev->ml_priv;
593         }
594         else
595         {
596                 pVirtualAd = dev->ml_priv;
597 #ifndef RT30xx
598                 if (pVirtualAd && pVirtualAd->RtmpDev)
599 #endif
600                         pAdapter = pVirtualAd->RtmpDev->ml_priv;
601         }
602
603         if (pAdapter == NULL)
604         {
605                 /* if 1st open fail, pAd will be free;
606                    So the net_dev->ml_priv will be NULL in 2rd open */
607                 return -ENETDOWN;
608         }
609
610                 ch = pAdapter->CommonCfg.Channel;
611
612         DBGPRINT(RT_DEBUG_TRACE,("==>rt_ioctl_giwfreq  %d\n", ch));
613
614     MAP_CHANNEL_ID_TO_KHZ(ch, m);
615         freq->m = m * 100;
616         freq->e = 1;
617         return 0;
618 }
619
620 int rt_ioctl_siwmode(struct net_device *dev,
621                    struct iw_request_info *info,
622                    __u32 *mode, char *extra)
623 {
624         PRTMP_ADAPTER pAdapter = dev->ml_priv;
625
626         //check if the interface is down
627     if(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE))
628     {
629         DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
630         return -ENETDOWN;
631     }
632
633         switch (*mode)
634         {
635                 case IW_MODE_ADHOC:
636                         Set_NetworkType_Proc(pAdapter, "Adhoc");
637                         break;
638                 case IW_MODE_INFRA:
639                         Set_NetworkType_Proc(pAdapter, "Infra");
640                         break;
641         case IW_MODE_MONITOR:
642                         Set_NetworkType_Proc(pAdapter, "Monitor");
643                         break;
644                 default:
645                         DBGPRINT(RT_DEBUG_TRACE, ("===>rt_ioctl_siwmode::SIOCSIWMODE (unknown %d)\n", *mode));
646                         return -EINVAL;
647         }
648
649         // Reset Ralink supplicant to not use, it will be set to start when UI set PMK key
650         pAdapter->StaCfg.WpaState = SS_NOTUSE;
651
652         return 0;
653 }
654
655 int rt_ioctl_giwmode(struct net_device *dev,
656                    struct iw_request_info *info,
657                    __u32 *mode, char *extra)
658 {
659 #ifndef RT30xx
660         PRTMP_ADAPTER   pAdapter = NULL;
661         VIRTUAL_ADAPTER *pVirtualAd = NULL;
662
663         if (dev->priv_flags == INT_MAIN)
664         {
665                 pAdapter = dev->ml_priv;
666         }
667         else
668         {
669                 pVirtualAd = dev->ml_priv;
670                 if (pVirtualAd && pVirtualAd->RtmpDev)
671                         pAdapter = pVirtualAd->RtmpDev->ml_priv;
672         }
673
674         if (pAdapter == NULL)
675         {
676                 /* if 1st open fail, pAd will be free;
677                    So the net_dev->ml_priv will be NULL in 2rd open */
678                 return -ENETDOWN;
679         }
680 #endif
681 #ifdef RT30xx
682         PRTMP_ADAPTER pAdapter = dev->ml_priv;
683 #endif
684
685         if (ADHOC_ON(pAdapter))
686                 *mode = IW_MODE_ADHOC;
687     else if (INFRA_ON(pAdapter))
688                 *mode = IW_MODE_INFRA;
689     else if (MONITOR_ON(pAdapter))
690     {
691         *mode = IW_MODE_MONITOR;
692     }
693     else
694         *mode = IW_MODE_AUTO;
695
696         DBGPRINT(RT_DEBUG_TRACE, ("==>rt_ioctl_giwmode(mode=%d)\n", *mode));
697         return 0;
698 }
699
700 int rt_ioctl_siwsens(struct net_device *dev,
701                    struct iw_request_info *info,
702                    char *name, char *extra)
703 {
704         PRTMP_ADAPTER pAdapter = dev->ml_priv;
705
706         //check if the interface is down
707         if(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE))
708         {
709                 DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
710                 return -ENETDOWN;
711         }
712
713         return 0;
714 }
715
716 int rt_ioctl_giwsens(struct net_device *dev,
717                    struct iw_request_info *info,
718                    char *name, char *extra)
719 {
720         return 0;
721 }
722
723 int rt_ioctl_giwrange(struct net_device *dev,
724                    struct iw_request_info *info,
725                    struct iw_point *data, char *extra)
726 {
727 #ifndef RT30xx
728         PRTMP_ADAPTER   pAdapter = NULL;
729         VIRTUAL_ADAPTER *pVirtualAd = NULL;
730 #endif
731 #ifdef RT30xx
732         PRTMP_ADAPTER pAdapter = dev->ml_priv;
733 #endif
734         struct iw_range *range = (struct iw_range *) extra;
735         u16 val;
736         int i;
737
738 #ifndef RT30xx
739         if (dev->priv_flags == INT_MAIN)
740         {
741                 pAdapter = dev->ml_priv;
742         }
743         else
744         {
745                 pVirtualAd = dev->ml_priv;
746                 if (pVirtualAd && pVirtualAd->RtmpDev)
747                         pAdapter = pVirtualAd->RtmpDev->ml_priv;
748         }
749
750         if (pAdapter == NULL)
751         {
752                 /* if 1st open fail, pAd will be free;
753                    So the net_dev->ml_priv will be NULL in 2rd open */
754                 return -ENETDOWN;
755         }
756 #endif
757
758         DBGPRINT(RT_DEBUG_TRACE ,("===>rt_ioctl_giwrange\n"));
759         data->length = sizeof(struct iw_range);
760         memset(range, 0, sizeof(struct iw_range));
761
762         range->txpower_capa = IW_TXPOW_DBM;
763
764         if (INFRA_ON(pAdapter)||ADHOC_ON(pAdapter))
765         {
766                 range->min_pmp = 1 * 1024;
767                 range->max_pmp = 65535 * 1024;
768                 range->min_pmt = 1 * 1024;
769                 range->max_pmt = 1000 * 1024;
770                 range->pmp_flags = IW_POWER_PERIOD;
771                 range->pmt_flags = IW_POWER_TIMEOUT;
772                 range->pm_capa = IW_POWER_PERIOD | IW_POWER_TIMEOUT |
773                         IW_POWER_UNICAST_R | IW_POWER_ALL_R;
774         }
775
776         range->we_version_compiled = WIRELESS_EXT;
777         range->we_version_source = 14;
778
779         range->retry_capa = IW_RETRY_LIMIT;
780         range->retry_flags = IW_RETRY_LIMIT;
781         range->min_retry = 0;
782         range->max_retry = 255;
783
784         range->num_channels =  pAdapter->ChannelListNum;
785
786         val = 0;
787         for (i = 1; i <= range->num_channels; i++)
788         {
789                 u32 m;
790                 range->freq[val].i = pAdapter->ChannelList[i-1].Channel;
791                 MAP_CHANNEL_ID_TO_KHZ(pAdapter->ChannelList[i-1].Channel, m);
792                 range->freq[val].m = m * 100; /* HZ */
793
794                 range->freq[val].e = 1;
795                 val++;
796                 if (val == IW_MAX_FREQUENCIES)
797                         break;
798         }
799         range->num_frequency = val;
800
801         range->max_qual.qual = 100; /* what is correct max? This was not
802                                         * documented exactly. At least
803                                         * 69 has been observed. */
804         range->max_qual.level = 0; /* dB */
805         range->max_qual.noise = 0; /* dB */
806
807         /* What would be suitable values for "average/typical" qual? */
808         range->avg_qual.qual = 20;
809         range->avg_qual.level = -60;
810         range->avg_qual.noise = -95;
811         range->sensitivity = 3;
812
813         range->max_encoding_tokens = NR_WEP_KEYS;
814         range->num_encoding_sizes = 2;
815         range->encoding_size[0] = 5;
816         range->encoding_size[1] = 13;
817
818         range->min_rts = 0;
819         range->max_rts = 2347;
820         range->min_frag = 256;
821         range->max_frag = 2346;
822
823 #if WIRELESS_EXT > 17
824         /* IW_ENC_CAPA_* bit field */
825         range->enc_capa = IW_ENC_CAPA_WPA | IW_ENC_CAPA_WPA2 |
826                                         IW_ENC_CAPA_CIPHER_TKIP | IW_ENC_CAPA_CIPHER_CCMP;
827 #endif
828
829         return 0;
830 }
831
832 int rt_ioctl_siwap(struct net_device *dev,
833                       struct iw_request_info *info,
834                       struct sockaddr *ap_addr, char *extra)
835 {
836         PRTMP_ADAPTER pAdapter = dev->ml_priv;
837     NDIS_802_11_MAC_ADDRESS Bssid;
838
839         //check if the interface is down
840         if(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE))
841         {
842         DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
843         return -ENETDOWN;
844     }
845
846         if (pAdapter->Mlme.CntlMachine.CurrState != CNTL_IDLE)
847     {
848         RT28XX_MLME_RESET_STATE_MACHINE(pAdapter);
849         DBGPRINT(RT_DEBUG_TRACE, ("!!! MLME busy, reset MLME state machine !!!\n"));
850     }
851
852     // tell CNTL state machine to call NdisMSetInformationComplete() after completing
853     // this request, because this request is initiated by NDIS.
854     pAdapter->MlmeAux.CurrReqIsFromNdis = FALSE;
855         // Prevent to connect AP again in STAMlmePeriodicExec
856         pAdapter->MlmeAux.AutoReconnectSsidLen= 32;
857
858     memset(Bssid, 0, MAC_ADDR_LEN);
859     memcpy(Bssid, ap_addr->sa_data, MAC_ADDR_LEN);
860     MlmeEnqueue(pAdapter,
861                 MLME_CNTL_STATE_MACHINE,
862                 OID_802_11_BSSID,
863                 sizeof(NDIS_802_11_MAC_ADDRESS),
864                 (VOID *)&Bssid);
865
866     DBGPRINT(RT_DEBUG_TRACE, ("IOCTL::SIOCSIWAP %02x:%02x:%02x:%02x:%02x:%02x\n",
867         Bssid[0], Bssid[1], Bssid[2], Bssid[3], Bssid[4], Bssid[5]));
868
869         return 0;
870 }
871
872 int rt_ioctl_giwap(struct net_device *dev,
873                       struct iw_request_info *info,
874                       struct sockaddr *ap_addr, char *extra)
875 {
876 #ifndef RT30xx
877         PRTMP_ADAPTER   pAdapter = NULL;
878         VIRTUAL_ADAPTER *pVirtualAd = NULL;
879
880         if (dev->priv_flags == INT_MAIN)
881         {
882                 pAdapter = dev->ml_priv;
883         }
884         else
885         {
886                 pVirtualAd = dev->ml_priv;
887                 if (pVirtualAd && pVirtualAd->RtmpDev)
888                         pAdapter = pVirtualAd->RtmpDev->ml_priv;
889         }
890
891         if (pAdapter == NULL)
892         {
893                 /* if 1st open fail, pAd will be free;
894                    So the net_dev->ml_priv will be NULL in 2rd open */
895                 return -ENETDOWN;
896         }
897 #endif
898 #ifdef RT30xx
899         PRTMP_ADAPTER pAdapter = dev->ml_priv;
900 #endif
901
902         if (INFRA_ON(pAdapter) || ADHOC_ON(pAdapter))
903         {
904                 ap_addr->sa_family = ARPHRD_ETHER;
905                 memcpy(ap_addr->sa_data, &pAdapter->CommonCfg.Bssid, ETH_ALEN);
906         }
907     // Add for RT2870
908     else if (pAdapter->StaCfg.WpaSupplicantUP != WPA_SUPPLICANT_DISABLE)
909     {
910         ap_addr->sa_family = ARPHRD_ETHER;
911         memcpy(ap_addr->sa_data, &pAdapter->MlmeAux.Bssid, ETH_ALEN);
912     }
913         else
914         {
915                 DBGPRINT(RT_DEBUG_TRACE, ("IOCTL::SIOCGIWAP(=EMPTY)\n"));
916                 return -ENOTCONN;
917         }
918
919         return 0;
920 }
921
922 /*
923  * Units are in db above the noise floor. That means the
924  * rssi values reported in the tx/rx descriptors in the
925  * driver are the SNR expressed in db.
926  *
927  * If you assume that the noise floor is -95, which is an
928  * excellent assumption 99.5 % of the time, then you can
929  * derive the absolute signal level (i.e. -95 + rssi).
930  * There are some other slight factors to take into account
931  * depending on whether the rssi measurement is from 11b,
932  * 11g, or 11a.   These differences are at most 2db and
933  * can be documented.
934  *
935  * NB: various calculations are based on the orinoco/wavelan
936  *     drivers for compatibility
937  */
938 static void set_quality(PRTMP_ADAPTER pAdapter,
939                         struct iw_quality *iq,
940                         signed char rssi)
941 {
942         __u8 ChannelQuality;
943
944         // Normalize Rssi
945         if (rssi >= -50)
946                 ChannelQuality = 100;
947         else if (rssi >= -80) // between -50 ~ -80dbm
948                 ChannelQuality = (__u8)(24 + ((rssi + 80) * 26)/10);
949         else if (rssi >= -90)   // between -80 ~ -90dbm
950         ChannelQuality = (__u8)((rssi + 90) * 26)/10;
951         else
952                 ChannelQuality = 0;
953
954     iq->qual = (__u8)ChannelQuality;
955
956     iq->level = (__u8)(rssi);
957     iq->noise = (pAdapter->BbpWriteLatch[66] > pAdapter->BbpTuning.FalseCcaUpperThreshold) ? ((__u8)pAdapter->BbpTuning.FalseCcaUpperThreshold) : ((__u8) pAdapter->BbpWriteLatch[66]);         // noise level (dBm)
958     iq->noise += 256 - 143;
959     iq->updated = pAdapter->iw_stats.qual.updated;
960 }
961
962 int rt_ioctl_iwaplist(struct net_device *dev,
963                         struct iw_request_info *info,
964                         struct iw_point *data, char *extra)
965 {
966         PRTMP_ADAPTER pAdapter = dev->ml_priv;
967
968         struct sockaddr addr[IW_MAX_AP];
969         struct iw_quality qual[IW_MAX_AP];
970         int i;
971
972         //check if the interface is down
973     if(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE))
974     {
975         DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
976                 data->length = 0;
977                 return 0;
978         //return -ENETDOWN;
979         }
980
981         for (i = 0; i <IW_MAX_AP ; i++)
982         {
983                 if (i >=  pAdapter->ScanTab.BssNr)
984                         break;
985                 addr[i].sa_family = ARPHRD_ETHER;
986                         memcpy(addr[i].sa_data, &pAdapter->ScanTab.BssEntry[i].Bssid, MAC_ADDR_LEN);
987                 set_quality(pAdapter, &qual[i], pAdapter->ScanTab.BssEntry[i].Rssi);
988         }
989         data->length = i;
990         memcpy(extra, &addr, i*sizeof(addr[0]));
991         data->flags = 1;                /* signal quality present (sort of) */
992         memcpy(extra + i*sizeof(addr[0]), &qual, i*sizeof(qual[i]));
993
994         return 0;
995 }
996
997 #ifdef SIOCGIWSCAN
998 int rt_ioctl_siwscan(struct net_device *dev,
999                         struct iw_request_info *info,
1000                         struct iw_point *data, char *extra)
1001 {
1002         PRTMP_ADAPTER pAdapter = dev->ml_priv;
1003
1004         ULONG                                                           Now;
1005         int Status = NDIS_STATUS_SUCCESS;
1006
1007         //check if the interface is down
1008         if(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE))
1009         {
1010                 DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
1011                 return -ENETDOWN;
1012         }
1013
1014         if (MONITOR_ON(pAdapter))
1015     {
1016         DBGPRINT(RT_DEBUG_TRACE, ("!!! Driver is in Monitor Mode now !!!\n"));
1017         return -EINVAL;
1018     }
1019 #ifdef RT2860
1020         if ((pAdapter->OpMode == OPMODE_STA) && (IDLE_ON(pAdapter))
1021                 && (pAdapter->StaCfg.bRadio == TRUE)
1022                 && (RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_IDLE_RADIO_OFF)))
1023         {
1024                 RT28xxPciAsicRadioOn(pAdapter, GUI_IDLE_POWER_SAVE);
1025         }
1026         // Check if still radio off.
1027         else if (pAdapter->bPCIclkOff == TRUE)
1028                 return 0;
1029 #endif
1030         if (pAdapter->StaCfg.WpaSupplicantUP == WPA_SUPPLICANT_ENABLE)
1031         {
1032                 pAdapter->StaCfg.WpaSupplicantScanCount++;
1033         }
1034
1035     pAdapter->StaCfg.bScanReqIsFromWebUI = TRUE;
1036         if (RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_BSS_SCAN_IN_PROGRESS))
1037                 return 0;
1038         do{
1039                 Now = jiffies;
1040
1041                 if ((pAdapter->StaCfg.WpaSupplicantUP == WPA_SUPPLICANT_ENABLE) &&
1042                         (pAdapter->StaCfg.WpaSupplicantScanCount > 3))
1043                 {
1044                         DBGPRINT(RT_DEBUG_TRACE, ("!!! WpaSupplicantScanCount > 3\n"));
1045                         Status = NDIS_STATUS_SUCCESS;
1046                         break;
1047                 }
1048
1049                 if ((OPSTATUS_TEST_FLAG(pAdapter, fOP_STATUS_MEDIA_STATE_CONNECTED)) &&
1050                         ((pAdapter->StaCfg.AuthMode == Ndis802_11AuthModeWPA) ||
1051                         (pAdapter->StaCfg.AuthMode == Ndis802_11AuthModeWPAPSK)) &&
1052                         (pAdapter->StaCfg.PortSecured == WPA_802_1X_PORT_NOT_SECURED))
1053                 {
1054                         DBGPRINT(RT_DEBUG_TRACE, ("!!! Link UP, Port Not Secured! ignore this set::OID_802_11_BSSID_LIST_SCAN\n"));
1055                         Status = NDIS_STATUS_SUCCESS;
1056                         break;
1057                 }
1058
1059                 if (pAdapter->Mlme.CntlMachine.CurrState != CNTL_IDLE)
1060                 {
1061                         RT28XX_MLME_RESET_STATE_MACHINE(pAdapter);
1062                         DBGPRINT(RT_DEBUG_TRACE, ("!!! MLME busy, reset MLME state machine !!!\n"));
1063                 }
1064
1065                 // tell CNTL state machine to call NdisMSetInformationComplete() after completing
1066                 // this request, because this request is initiated by NDIS.
1067                 pAdapter->MlmeAux.CurrReqIsFromNdis = FALSE;
1068                 // Reset allowed scan retries
1069                 pAdapter->StaCfg.ScanCnt = 0;
1070                 pAdapter->StaCfg.LastScanTime = Now;
1071
1072                 MlmeEnqueue(pAdapter,
1073                         MLME_CNTL_STATE_MACHINE,
1074                         OID_802_11_BSSID_LIST_SCAN,
1075                         0,
1076                         NULL);
1077
1078                 Status = NDIS_STATUS_SUCCESS;
1079                 RT28XX_MLME_HANDLER(pAdapter);
1080         }while(0);
1081         return 0;
1082 }
1083
1084 int rt_ioctl_giwscan(struct net_device *dev,
1085                         struct iw_request_info *info,
1086                         struct iw_point *data, char *extra)
1087 {
1088
1089         PRTMP_ADAPTER pAdapter = dev->ml_priv;
1090         int i=0;
1091         char *current_ev = extra, *previous_ev = extra;
1092         char *end_buf;
1093         char *current_val, custom[MAX_CUSTOM_LEN] = {0};
1094 #ifndef IWEVGENIE
1095         char idx;
1096 #endif // IWEVGENIE //
1097         struct iw_event iwe;
1098
1099         if (RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_BSS_SCAN_IN_PROGRESS))
1100     {
1101                 /*
1102                  * Still scanning, indicate the caller should try again.
1103                  */
1104                 return -EAGAIN;
1105         }
1106
1107         if (pAdapter->StaCfg.WpaSupplicantUP == WPA_SUPPLICANT_ENABLE)
1108         {
1109                 pAdapter->StaCfg.WpaSupplicantScanCount = 0;
1110         }
1111
1112         if (pAdapter->ScanTab.BssNr == 0)
1113         {
1114                 data->length = 0;
1115                 return 0;
1116         }
1117
1118 #if WIRELESS_EXT >= 17
1119     if (data->length > 0)
1120         end_buf = extra + data->length;
1121     else
1122         end_buf = extra + IW_SCAN_MAX_DATA;
1123 #else
1124     end_buf = extra + IW_SCAN_MAX_DATA;
1125 #endif
1126
1127         for (i = 0; i < pAdapter->ScanTab.BssNr; i++)
1128         {
1129                 if (current_ev >= end_buf)
1130         {
1131 #if WIRELESS_EXT >= 17
1132             return -E2BIG;
1133 #else
1134                         break;
1135 #endif
1136         }
1137
1138                 //MAC address
1139                 //================================
1140                 memset(&iwe, 0, sizeof(iwe));
1141                 iwe.cmd = SIOCGIWAP;
1142                 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
1143                 memcpy(iwe.u.ap_addr.sa_data, &pAdapter->ScanTab.BssEntry[i].Bssid, ETH_ALEN);
1144
1145         previous_ev = current_ev;
1146                 current_ev = iwe_stream_add_event(info, current_ev,end_buf, &iwe, IW_EV_ADDR_LEN);
1147 #ifdef RT30xx
1148         if (current_ev == previous_ev)
1149 #if WIRELESS_EXT >= 17
1150             return -E2BIG;
1151 #else
1152                         break;
1153 #endif
1154
1155                 /*
1156                 Protocol:
1157                         it will show scanned AP's WirelessMode .
1158                         it might be
1159                                         802.11a
1160                                         802.11a/n
1161                                         802.11g/n
1162                                         802.11b/g/n
1163                                         802.11g
1164                                         802.11b/g
1165                 */
1166                 memset(&iwe, 0, sizeof(iwe));
1167                 iwe.cmd = SIOCGIWNAME;
1168
1169
1170         {
1171                 PBSS_ENTRY pBssEntry=&pAdapter->ScanTab.BssEntry[i];
1172                 BOOLEAN isGonly=FALSE;
1173                 int rateCnt=0;
1174
1175                 if (pBssEntry->Channel>14)
1176                 {
1177                         if (pBssEntry->HtCapabilityLen!=0)
1178                                 strcpy(iwe.u.name,"802.11a/n");
1179                         else
1180                                 strcpy(iwe.u.name,"802.11a");
1181                 }
1182                 else
1183                 {
1184                         /*
1185                                 if one of non B mode rate is set supported rate . it mean G only.
1186                         */
1187                         for (rateCnt=0;rateCnt<pBssEntry->SupRateLen;rateCnt++)
1188                         {
1189                                 /*
1190                                         6Mbps(140) 9Mbps(146) and >=12Mbps(152) are supported rate , it mean G only.
1191                                 */
1192                                 if (pBssEntry->SupRate[rateCnt]==140 || pBssEntry->SupRate[rateCnt]==146 || pBssEntry->SupRate[rateCnt]>=152)
1193                                         isGonly=TRUE;
1194                         }
1195
1196                         for (rateCnt=0;rateCnt<pBssEntry->ExtRateLen;rateCnt++)
1197                         {
1198                                 if (pBssEntry->ExtRate[rateCnt]==140 || pBssEntry->ExtRate[rateCnt]==146 || pBssEntry->ExtRate[rateCnt]>=152)
1199                                         isGonly=TRUE;
1200                         }
1201
1202
1203                         if (pBssEntry->HtCapabilityLen!=0)
1204                         {
1205                                 if (isGonly==TRUE)
1206                                         strcpy(iwe.u.name,"802.11g/n");
1207                                 else
1208                                         strcpy(iwe.u.name,"802.11b/g/n");
1209                         }
1210                         else
1211                         {
1212                                 if (isGonly==TRUE)
1213                                         strcpy(iwe.u.name,"802.11g");
1214                                 else
1215                                 {
1216                                         if (pBssEntry->SupRateLen==4 && pBssEntry->ExtRateLen==0)
1217                                                 strcpy(iwe.u.name,"802.11b");
1218                                         else
1219                                                 strcpy(iwe.u.name,"802.11b/g");
1220                                 }
1221                         }
1222                 }
1223         }
1224
1225                 previous_ev = current_ev;
1226                 current_ev       = iwe_stream_add_event(info, current_ev, end_buf, &iwe, IW_EV_ADDR_LEN);
1227 #endif /* RT30xx */
1228         if (current_ev == previous_ev)
1229 #if WIRELESS_EXT >= 17
1230             return -E2BIG;
1231 #else
1232                         break;
1233 #endif
1234
1235                 //ESSID
1236                 //================================
1237                 memset(&iwe, 0, sizeof(iwe));
1238                 iwe.cmd = SIOCGIWESSID;
1239                 iwe.u.data.length = pAdapter->ScanTab.BssEntry[i].SsidLen;
1240                 iwe.u.data.flags = 1;
1241
1242         previous_ev = current_ev;
1243                 current_ev = iwe_stream_add_point(info, current_ev,end_buf, &iwe, pAdapter->ScanTab.BssEntry[i].Ssid);
1244         if (current_ev == previous_ev)
1245 #if WIRELESS_EXT >= 17
1246             return -E2BIG;
1247 #else
1248                         break;
1249 #endif
1250
1251                 //Network Type
1252                 //================================
1253                 memset(&iwe, 0, sizeof(iwe));
1254                 iwe.cmd = SIOCGIWMODE;
1255                 if (pAdapter->ScanTab.BssEntry[i].BssType == Ndis802_11IBSS)
1256                 {
1257                         iwe.u.mode = IW_MODE_ADHOC;
1258                 }
1259                 else if (pAdapter->ScanTab.BssEntry[i].BssType == Ndis802_11Infrastructure)
1260                 {
1261                         iwe.u.mode = IW_MODE_INFRA;
1262                 }
1263                 else
1264                 {
1265                         iwe.u.mode = IW_MODE_AUTO;
1266                 }
1267                 iwe.len = IW_EV_UINT_LEN;
1268
1269         previous_ev = current_ev;
1270                 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,  IW_EV_UINT_LEN);
1271         if (current_ev == previous_ev)
1272 #if WIRELESS_EXT >= 17
1273             return -E2BIG;
1274 #else
1275                         break;
1276 #endif
1277
1278                 //Channel and Frequency
1279                 //================================
1280                 memset(&iwe, 0, sizeof(iwe));
1281                 iwe.cmd = SIOCGIWFREQ;
1282                 if (INFRA_ON(pAdapter) || ADHOC_ON(pAdapter))
1283                         iwe.u.freq.m = pAdapter->ScanTab.BssEntry[i].Channel;
1284                 else
1285                         iwe.u.freq.m = pAdapter->ScanTab.BssEntry[i].Channel;
1286                 iwe.u.freq.e = 0;
1287                 iwe.u.freq.i = 0;
1288
1289                 previous_ev = current_ev;
1290                 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe, IW_EV_FREQ_LEN);
1291         if (current_ev == previous_ev)
1292 #if WIRELESS_EXT >= 17
1293             return -E2BIG;
1294 #else
1295                         break;
1296 #endif
1297
1298         //Add quality statistics
1299         //================================
1300         memset(&iwe, 0, sizeof(iwe));
1301         iwe.cmd = IWEVQUAL;
1302         iwe.u.qual.level = 0;
1303         iwe.u.qual.noise = 0;
1304         set_quality(pAdapter, &iwe.u.qual, pAdapter->ScanTab.BssEntry[i].Rssi);
1305         current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe, IW_EV_QUAL_LEN);
1306         if (current_ev == previous_ev)
1307 #if WIRELESS_EXT >= 17
1308             return -E2BIG;
1309 #else
1310                         break;
1311 #endif
1312
1313                 //Encyption key
1314                 //================================
1315                 memset(&iwe, 0, sizeof(iwe));
1316                 iwe.cmd = SIOCGIWENCODE;
1317                 if (CAP_IS_PRIVACY_ON (pAdapter->ScanTab.BssEntry[i].CapabilityInfo ))
1318                         iwe.u.data.flags =IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
1319                 else
1320                         iwe.u.data.flags = IW_ENCODE_DISABLED;
1321
1322         previous_ev = current_ev;
1323         current_ev = iwe_stream_add_point(info, current_ev, end_buf,&iwe, (char *)pAdapter->SharedKey[BSS0][(iwe.u.data.flags & IW_ENCODE_INDEX)-1].Key);
1324         if (current_ev == previous_ev)
1325 #if WIRELESS_EXT >= 17
1326             return -E2BIG;
1327 #else
1328                         break;
1329 #endif
1330
1331                 //Bit Rate
1332                 //================================
1333                 if (pAdapter->ScanTab.BssEntry[i].SupRateLen)
1334         {
1335             UCHAR tmpRate = pAdapter->ScanTab.BssEntry[i].SupRate[pAdapter->ScanTab.BssEntry[i].SupRateLen-1];
1336                         memset(&iwe, 0, sizeof(iwe));
1337                         iwe.cmd = SIOCGIWRATE;
1338                 current_val = current_ev + IW_EV_LCP_LEN;
1339             if (tmpRate == 0x82)
1340                 iwe.u.bitrate.value =  1 * 1000000;
1341             else if (tmpRate == 0x84)
1342                 iwe.u.bitrate.value =  2 * 1000000;
1343             else if (tmpRate == 0x8B)
1344                 iwe.u.bitrate.value =  5.5 * 1000000;
1345             else if (tmpRate == 0x96)
1346                 iwe.u.bitrate.value =  11 * 1000000;
1347             else
1348                     iwe.u.bitrate.value =  (tmpRate/2) * 1000000;
1349
1350                         iwe.u.bitrate.disabled = 0;
1351                         current_val = iwe_stream_add_value(info, current_ev,
1352                                 current_val, end_buf, &iwe,
1353                         IW_EV_PARAM_LEN);
1354
1355                 if((current_val-current_ev)>IW_EV_LCP_LEN)
1356                 current_ev = current_val;
1357                 else
1358 #if WIRELESS_EXT >= 17
1359                 return -E2BIG;
1360 #else
1361                             break;
1362 #endif
1363         }
1364
1365 #ifdef IWEVGENIE
1366                 //WPA IE
1367                 if (pAdapter->ScanTab.BssEntry[i].WpaIE.IELen > 0)
1368                 {
1369                         memset(&iwe, 0, sizeof(iwe));
1370                         memset(&custom[0], 0, MAX_CUSTOM_LEN);
1371                         memcpy(custom, &(pAdapter->ScanTab.BssEntry[i].WpaIE.IE[0]),
1372                                                    pAdapter->ScanTab.BssEntry[i].WpaIE.IELen);
1373                         iwe.cmd = IWEVGENIE;
1374                         iwe.u.data.length = pAdapter->ScanTab.BssEntry[i].WpaIE.IELen;
1375                         current_ev = iwe_stream_add_point(info, current_ev, end_buf, &iwe, custom);
1376                         if (current_ev == previous_ev)
1377 #if WIRELESS_EXT >= 17
1378                 return -E2BIG;
1379 #else
1380                             break;
1381 #endif
1382                 }
1383
1384                 //WPA2 IE
1385         if (pAdapter->ScanTab.BssEntry[i].RsnIE.IELen > 0)
1386         {
1387                 memset(&iwe, 0, sizeof(iwe));
1388                         memset(&custom[0], 0, MAX_CUSTOM_LEN);
1389                         memcpy(custom, &(pAdapter->ScanTab.BssEntry[i].RsnIE.IE[0]),
1390                                                    pAdapter->ScanTab.BssEntry[i].RsnIE.IELen);
1391                         iwe.cmd = IWEVGENIE;
1392                         iwe.u.data.length = pAdapter->ScanTab.BssEntry[i].RsnIE.IELen;
1393                         current_ev = iwe_stream_add_point(info, current_ev, end_buf, &iwe, custom);
1394                         if (current_ev == previous_ev)
1395 #if WIRELESS_EXT >= 17
1396                 return -E2BIG;
1397 #else
1398                             break;
1399 #endif
1400         }
1401 #else
1402         //WPA IE
1403                 //================================
1404         if (pAdapter->ScanTab.BssEntry[i].WpaIE.IELen > 0)
1405         {
1406                 NdisZeroMemory(&iwe, sizeof(iwe));
1407                         memset(&custom[0], 0, MAX_CUSTOM_LEN);
1408                 iwe.cmd = IWEVCUSTOM;
1409             iwe.u.data.length = (pAdapter->ScanTab.BssEntry[i].WpaIE.IELen * 2) + 7;
1410             NdisMoveMemory(custom, "wpa_ie=", 7);
1411             for (idx = 0; idx < pAdapter->ScanTab.BssEntry[i].WpaIE.IELen; idx++)
1412                 sprintf(custom + strlen(custom), "%02x", pAdapter->ScanTab.BssEntry[i].WpaIE.IE[idx]);
1413             previous_ev = current_ev;
1414                 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,  custom);
1415             if (current_ev == previous_ev)
1416 #if WIRELESS_EXT >= 17
1417                 return -E2BIG;
1418 #else
1419                             break;
1420 #endif
1421         }
1422
1423         //WPA2 IE
1424         if (pAdapter->ScanTab.BssEntry[i].RsnIE.IELen > 0)
1425         {
1426                 NdisZeroMemory(&iwe, sizeof(iwe));
1427                         memset(&custom[0], 0, MAX_CUSTOM_LEN);
1428                 iwe.cmd = IWEVCUSTOM;
1429             iwe.u.data.length = (pAdapter->ScanTab.BssEntry[i].RsnIE.IELen * 2) + 7;
1430             NdisMoveMemory(custom, "rsn_ie=", 7);
1431                         for (idx = 0; idx < pAdapter->ScanTab.BssEntry[i].RsnIE.IELen; idx++)
1432                 sprintf(custom + strlen(custom), "%02x", pAdapter->ScanTab.BssEntry[i].RsnIE.IE[idx]);
1433             previous_ev = current_ev;
1434                 current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,  custom);
1435             if (current_ev == previous_ev)
1436 #if WIRELESS_EXT >= 17
1437                 return -E2BIG;
1438 #else
1439                             break;
1440 #endif
1441         }
1442 #endif // IWEVGENIE //
1443         }
1444
1445         data->length = current_ev - extra;
1446     pAdapter->StaCfg.bScanReqIsFromWebUI = FALSE;
1447         DBGPRINT(RT_DEBUG_ERROR ,("===>rt_ioctl_giwscan. %d(%d) BSS returned, data->length = %d\n",i , pAdapter->ScanTab.BssNr, data->length));
1448         return 0;
1449 }
1450 #endif
1451
1452 int rt_ioctl_siwessid(struct net_device *dev,
1453                          struct iw_request_info *info,
1454                          struct iw_point *data, char *essid)
1455 {
1456         PRTMP_ADAPTER pAdapter = dev->ml_priv;
1457
1458         //check if the interface is down
1459     if(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE))
1460     {
1461         DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
1462         return -ENETDOWN;
1463     }
1464
1465         if (data->flags)
1466         {
1467                 PCHAR   pSsidString = NULL;
1468
1469                 // Includes null character.
1470                 if (data->length > (IW_ESSID_MAX_SIZE + 1))
1471                         return -E2BIG;
1472
1473                 pSsidString = (CHAR *) kmalloc(MAX_LEN_OF_SSID+1, MEM_ALLOC_FLAG);
1474                 if (pSsidString)
1475                 {
1476                         NdisZeroMemory(pSsidString, MAX_LEN_OF_SSID+1);
1477                         NdisMoveMemory(pSsidString, essid, data->length);
1478                         if (Set_SSID_Proc(pAdapter, pSsidString) == FALSE)
1479                                 return -EINVAL;
1480                 }
1481                 else
1482                         return -ENOMEM;
1483         }
1484         else
1485         {
1486                 // ANY ssid
1487                 if (Set_SSID_Proc(pAdapter, "") == FALSE)
1488                         return -EINVAL;
1489     }
1490         return 0;
1491 }
1492
1493 int rt_ioctl_giwessid(struct net_device *dev,
1494                          struct iw_request_info *info,
1495                          struct iw_point *data, char *essid)
1496 {
1497 #ifndef RT30xx
1498         PRTMP_ADAPTER   pAdapter = NULL;
1499         VIRTUAL_ADAPTER *pVirtualAd = NULL;
1500
1501         if (dev->priv_flags == INT_MAIN)
1502         {
1503                 pAdapter = dev->ml_priv;
1504         }
1505         else
1506         {
1507                 pVirtualAd = dev->ml_priv;
1508                 if (pVirtualAd && pVirtualAd->RtmpDev)
1509                         pAdapter = pVirtualAd->RtmpDev->ml_priv;
1510         }
1511
1512         if (pAdapter == NULL)
1513         {
1514                 /* if 1st open fail, pAd will be free;
1515                    So the net_dev->ml_priv will be NULL in 2rd open */
1516                 return -ENETDOWN;
1517         }
1518 #endif
1519 #ifdef RT30xx
1520         PRTMP_ADAPTER pAdapter = dev->ml_priv;
1521 #endif
1522
1523         data->flags = 1;
1524     if (MONITOR_ON(pAdapter))
1525     {
1526         data->length  = 0;
1527         return 0;
1528     }
1529
1530         if (OPSTATUS_TEST_FLAG(pAdapter, fOP_STATUS_MEDIA_STATE_CONNECTED))
1531         {
1532                 DBGPRINT(RT_DEBUG_TRACE ,("MediaState is connected\n"));
1533                 data->length = pAdapter->CommonCfg.SsidLen;
1534                 memcpy(essid, pAdapter->CommonCfg.Ssid, pAdapter->CommonCfg.SsidLen);
1535         }
1536 #ifdef RT2870
1537     // Add for RT2870
1538     else if (pAdapter->StaCfg.WpaSupplicantUP != WPA_SUPPLICANT_DISABLE)
1539     {
1540         data->length = pAdapter->CommonCfg.SsidLen;
1541                 memcpy(essid, pAdapter->CommonCfg.Ssid, pAdapter->CommonCfg.SsidLen);
1542         }
1543 #endif // RT2870 //
1544         else
1545         {//the ANY ssid was specified
1546                 data->length  = 0;
1547                 DBGPRINT(RT_DEBUG_TRACE ,("MediaState is not connected, ess\n"));
1548         }
1549
1550         return 0;
1551
1552 }
1553
1554 int rt_ioctl_siwnickn(struct net_device *dev,
1555                          struct iw_request_info *info,
1556                          struct iw_point *data, char *nickname)
1557 {
1558         PRTMP_ADAPTER pAdapter = dev->ml_priv;
1559
1560     //check if the interface is down
1561     if(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE))
1562     {
1563         DBGPRINT(RT_DEBUG_TRACE ,("INFO::Network is down!\n"));
1564         return -ENETDOWN;
1565     }
1566
1567         if (data->length > IW_ESSID_MAX_SIZE)
1568                 return -EINVAL;
1569
1570         memset(pAdapter->nickname, 0, IW_ESSID_MAX_SIZE + 1);
1571         memcpy(pAdapter->nickname, nickname, data->length);
1572
1573
1574         return 0;
1575 }
1576
1577 int rt_ioctl_giwnickn(struct net_device *dev,
1578                          struct iw_request_info *info,
1579                          struct iw_point *data, char *nickname)
1580 {
1581 #ifndef RT30xx
1582         PRTMP_ADAPTER   pAdapter = NULL;
1583         VIRTUAL_ADAPTER *pVirtualAd = NULL;
1584
1585         if (dev->priv_flags == INT_MAIN)
1586         {
1587                 pAdapter = dev->ml_priv;
1588         }
1589         else
1590         {
1591                 pVirtualAd = dev->ml_priv;
1592                 if (pVirtualAd && pVirtualAd->RtmpDev)
1593                         pAdapter = pVirtualAd->RtmpDev->ml_priv;
1594         }
1595
1596         if (pAdapter == NULL)
1597         {
1598                 /* if 1st open fail, pAd will be free;
1599                    So the net_dev->ml_priv will be NULL in 2rd open */
1600                 return -ENETDOWN;
1601         }
1602 #endif
1603 #ifdef RT30xx
1604         PRTMP_ADAPTER pAdapter = dev->ml_priv;
1605 #endif
1606
1607         if (data->length > strlen(pAdapter->nickname) + 1)
1608                 data->length = strlen(pAdapter->nickname) + 1;
1609         if (data->length > 0) {
1610                 memcpy(nickname, pAdapter->nickname, data->length-1);
1611                 nickname[data->length-1] = '\0';
1612         }
1613         return 0;
1614 }
1615
1616 int rt_ioctl_siwrts(struct net_device *dev,
1617                        struct iw_request_info *info,
1618                        struct iw_param *rts, char *extra)
1619 {
1620         PRTMP_ADAPTER pAdapter = dev->ml_priv;
1621         u16 val;
1622
1623     //check if the interface is down
1624     if(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE))
1625     {
1626         DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
1627         return -ENETDOWN;
1628     }
1629
1630         if (rts->disabled)
1631                 val = MAX_RTS_THRESHOLD;
1632         else if (rts->value < 0 || rts->value > MAX_RTS_THRESHOLD)
1633                 return -EINVAL;
1634         else if (rts->value == 0)
1635             val = MAX_RTS_THRESHOLD;
1636         else
1637                 val = rts->value;
1638
1639         if (val != pAdapter->CommonCfg.RtsThreshold)
1640                 pAdapter->CommonCfg.RtsThreshold = val;
1641
1642         return 0;
1643 }
1644
1645 int rt_ioctl_giwrts(struct net_device *dev,
1646                        struct iw_request_info *info,
1647                        struct iw_param *rts, char *extra)
1648 {
1649 #ifndef RT30xx
1650         PRTMP_ADAPTER   pAdapter = NULL;
1651         VIRTUAL_ADAPTER *pVirtualAd = NULL;
1652
1653         if (dev->priv_flags == INT_MAIN)
1654         {
1655                 pAdapter = dev->ml_priv;
1656         }
1657         else
1658         {
1659                 pVirtualAd = dev->ml_priv;
1660                 if (pVirtualAd && pVirtualAd->RtmpDev)
1661                         pAdapter = pVirtualAd->RtmpDev->ml_priv;
1662         }
1663
1664         if (pAdapter == NULL)
1665         {
1666                 /* if 1st open fail, pAd will be free;
1667                    So the net_dev->ml_priv will be NULL in 2rd open */
1668                 return -ENETDOWN;
1669         }
1670 #endif
1671 #ifdef RT30xx
1672         PRTMP_ADAPTER pAdapter = dev->ml_priv;
1673 #endif
1674
1675         //check if the interface is down
1676         if(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE))
1677         {
1678                 DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
1679                 return -ENETDOWN;
1680         }
1681
1682         rts->value = pAdapter->CommonCfg.RtsThreshold;
1683         rts->disabled = (rts->value == MAX_RTS_THRESHOLD);
1684         rts->fixed = 1;
1685
1686         return 0;
1687 }
1688
1689 int rt_ioctl_siwfrag(struct net_device *dev,
1690                         struct iw_request_info *info,
1691                         struct iw_param *frag, char *extra)
1692 {
1693         PRTMP_ADAPTER pAdapter = dev->ml_priv;
1694         u16 val;
1695
1696         //check if the interface is down
1697         if(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE))
1698         {
1699                 DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
1700                 return -ENETDOWN;
1701         }
1702
1703         if (frag->disabled)
1704                 val = MAX_FRAG_THRESHOLD;
1705         else if (frag->value >= MIN_FRAG_THRESHOLD || frag->value <= MAX_FRAG_THRESHOLD)
1706         val = __cpu_to_le16(frag->value & ~0x1); /* even numbers only */
1707         else if (frag->value == 0)
1708             val = MAX_FRAG_THRESHOLD;
1709         else
1710                 return -EINVAL;
1711
1712         pAdapter->CommonCfg.FragmentThreshold = val;
1713         return 0;
1714 }
1715
1716 int rt_ioctl_giwfrag(struct net_device *dev,
1717                         struct iw_request_info *info,
1718                         struct iw_param *frag, char *extra)
1719 {
1720 #ifndef RT30xx
1721         PRTMP_ADAPTER   pAdapter = NULL;
1722         VIRTUAL_ADAPTER *pVirtualAd = NULL;
1723
1724         if (dev->priv_flags == INT_MAIN)
1725         {
1726                 pAdapter = dev->ml_priv;
1727         }
1728         else
1729         {
1730                 pVirtualAd = dev->ml_priv;
1731                 if (pVirtualAd && pVirtualAd->RtmpDev)
1732                         pAdapter = pVirtualAd->RtmpDev->ml_priv;
1733         }
1734
1735         if (pAdapter == NULL)
1736         {
1737                 /* if 1st open fail, pAd will be free;
1738                    So the net_dev->ml_priv will be NULL in 2rd open */
1739                 return -ENETDOWN;
1740         }
1741 #endif
1742 #ifdef RT30xx
1743         PRTMP_ADAPTER pAdapter = dev->ml_priv;
1744 #endif
1745
1746         //check if the interface is down
1747         if(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE))
1748         {
1749                 DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
1750                 return -ENETDOWN;
1751         }
1752
1753         frag->value = pAdapter->CommonCfg.FragmentThreshold;
1754         frag->disabled = (frag->value == MAX_FRAG_THRESHOLD);
1755         frag->fixed = 1;
1756
1757         return 0;
1758 }
1759
1760 #define MAX_WEP_KEY_SIZE 13
1761 #define MIN_WEP_KEY_SIZE 5
1762 int rt_ioctl_siwencode(struct net_device *dev,
1763                           struct iw_request_info *info,
1764                           struct iw_point *erq, char *extra)
1765 {
1766         PRTMP_ADAPTER pAdapter = dev->ml_priv;
1767
1768         //check if the interface is down
1769         if(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE))
1770         {
1771                 DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
1772                 return -ENETDOWN;
1773         }
1774
1775         if ((erq->length == 0) &&
1776         (erq->flags & IW_ENCODE_DISABLED))
1777         {
1778                 pAdapter->StaCfg.PairCipher = Ndis802_11WEPDisabled;
1779                 pAdapter->StaCfg.GroupCipher = Ndis802_11WEPDisabled;
1780                 pAdapter->StaCfg.WepStatus = Ndis802_11WEPDisabled;
1781         pAdapter->StaCfg.OrigWepStatus = pAdapter->StaCfg.WepStatus;
1782         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeOpen;
1783         goto done;
1784         }
1785 #ifndef RT30xx
1786         else if ((erq->length == 0) &&
1787              (erq->flags & IW_ENCODE_RESTRICTED || erq->flags & IW_ENCODE_OPEN))
1788 #endif
1789 #ifdef RT30xx
1790         else if (erq->flags & IW_ENCODE_RESTRICTED || erq->flags & IW_ENCODE_OPEN)
1791 #endif
1792         {
1793                 STA_PORT_SECURED(pAdapter);
1794                 pAdapter->StaCfg.PairCipher = Ndis802_11WEPEnabled;
1795                 pAdapter->StaCfg.GroupCipher = Ndis802_11WEPEnabled;
1796                 pAdapter->StaCfg.WepStatus = Ndis802_11WEPEnabled;
1797         pAdapter->StaCfg.OrigWepStatus = pAdapter->StaCfg.WepStatus;
1798                 if (erq->flags & IW_ENCODE_RESTRICTED)
1799                         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeShared;
1800         else
1801                         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeOpen;
1802 #ifndef RT30xx
1803         goto done;
1804 #endif
1805         }
1806
1807     if (erq->length > 0)
1808         {
1809                 int keyIdx = (erq->flags & IW_ENCODE_INDEX) - 1;
1810                 /* Check the size of the key */
1811                 if (erq->length > MAX_WEP_KEY_SIZE) {
1812                         return -EINVAL;
1813                 }
1814                 /* Check key index */
1815                 if ((keyIdx < 0) || (keyIdx >= NR_WEP_KEYS))
1816         {
1817             DBGPRINT(RT_DEBUG_TRACE ,("==>rt_ioctl_siwencode::Wrong keyIdx=%d! Using default key instead (%d)\n",
1818                                         keyIdx, pAdapter->StaCfg.DefaultKeyId));
1819
1820             //Using default key
1821                         keyIdx = pAdapter->StaCfg.DefaultKeyId;
1822         }
1823 #ifdef RT30xx
1824                 else
1825                 {
1826                         pAdapter->StaCfg.DefaultKeyId=keyIdx;
1827                 }
1828 #endif
1829
1830         NdisZeroMemory(pAdapter->SharedKey[BSS0][keyIdx].Key,  16);
1831
1832                 if (erq->length == MAX_WEP_KEY_SIZE)
1833         {
1834                         pAdapter->SharedKey[BSS0][keyIdx].KeyLen = MAX_WEP_KEY_SIZE;
1835             pAdapter->SharedKey[BSS0][keyIdx].CipherAlg = CIPHER_WEP128;
1836                 }
1837                 else if (erq->length == MIN_WEP_KEY_SIZE)
1838         {
1839             pAdapter->SharedKey[BSS0][keyIdx].KeyLen = MIN_WEP_KEY_SIZE;
1840             pAdapter->SharedKey[BSS0][keyIdx].CipherAlg = CIPHER_WEP64;
1841                 }
1842                 else
1843                         /* Disable the key */
1844                         pAdapter->SharedKey[BSS0][keyIdx].KeyLen = 0;
1845
1846                 /* Check if the key is not marked as invalid */
1847                 if(!(erq->flags & IW_ENCODE_NOKEY)) {
1848                         /* Copy the key in the driver */
1849                         NdisMoveMemory(pAdapter->SharedKey[BSS0][keyIdx].Key, extra, erq->length);
1850         }
1851         }
1852     else
1853                         {
1854                 /* Do we want to just set the transmit key index ? */
1855                 int index = (erq->flags & IW_ENCODE_INDEX) - 1;
1856                 if ((index >= 0) && (index < 4))
1857         {
1858                         pAdapter->StaCfg.DefaultKeyId = index;
1859             }
1860         else
1861                         /* Don't complain if only change the mode */
1862                         if (!(erq->flags & IW_ENCODE_MODE)) {
1863                                 return -EINVAL;
1864                 }
1865         }
1866
1867 done:
1868     DBGPRINT(RT_DEBUG_TRACE ,("==>rt_ioctl_siwencode::erq->flags=%x\n",erq->flags));
1869         DBGPRINT(RT_DEBUG_TRACE ,("==>rt_ioctl_siwencode::AuthMode=%x\n",pAdapter->StaCfg.AuthMode));
1870         DBGPRINT(RT_DEBUG_TRACE ,("==>rt_ioctl_siwencode::DefaultKeyId=%x, KeyLen = %d\n",pAdapter->StaCfg.DefaultKeyId , pAdapter->SharedKey[BSS0][pAdapter->StaCfg.DefaultKeyId].KeyLen));
1871         DBGPRINT(RT_DEBUG_TRACE ,("==>rt_ioctl_siwencode::WepStatus=%x\n",pAdapter->StaCfg.WepStatus));
1872         return 0;
1873 }
1874
1875 int
1876 rt_ioctl_giwencode(struct net_device *dev,
1877                           struct iw_request_info *info,
1878                           struct iw_point *erq, char *key)
1879 {
1880 #ifdef RT30xx
1881         PRTMP_ADAPTER pAdapter = dev->ml_priv;
1882 #endif
1883         int kid;
1884 #ifndef RT30xx
1885         PRTMP_ADAPTER   pAdapter = NULL;
1886         VIRTUAL_ADAPTER *pVirtualAd = NULL;
1887
1888         if (dev->priv_flags == INT_MAIN)
1889         {
1890                 pAdapter = dev->ml_priv;
1891         }
1892         else
1893         {
1894                 pVirtualAd = dev->ml_priv;
1895                 if (pVirtualAd && pVirtualAd->RtmpDev)
1896                         pAdapter = pVirtualAd->RtmpDev->ml_priv;
1897         }
1898
1899         if (pAdapter == NULL)
1900         {
1901                 /* if 1st open fail, pAd will be free;
1902                    So the net_dev->ml_priv will be NULL in 2rd open */
1903                 return -ENETDOWN;
1904         }
1905 #endif
1906
1907         //check if the interface is down
1908         if(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE))
1909         {
1910                 DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
1911         return -ENETDOWN;
1912         }
1913
1914         kid = erq->flags & IW_ENCODE_INDEX;
1915         DBGPRINT(RT_DEBUG_TRACE, ("===>rt_ioctl_giwencode %d\n", erq->flags & IW_ENCODE_INDEX));
1916
1917         if (pAdapter->StaCfg.WepStatus == Ndis802_11WEPDisabled)
1918         {
1919                 erq->length = 0;
1920                 erq->flags = IW_ENCODE_DISABLED;
1921         }
1922         else if ((kid > 0) && (kid <=4))
1923         {
1924                 // copy wep key
1925                 erq->flags = kid ;                      /* NB: base 1 */
1926                 if (erq->length > pAdapter->SharedKey[BSS0][kid-1].KeyLen)
1927                         erq->length = pAdapter->SharedKey[BSS0][kid-1].KeyLen;
1928                 memcpy(key, pAdapter->SharedKey[BSS0][kid-1].Key, erq->length);
1929                 //if ((kid == pAdapter->PortCfg.DefaultKeyId))
1930                 //erq->flags |= IW_ENCODE_ENABLED;      /* XXX */
1931                 if (pAdapter->StaCfg.AuthMode == Ndis802_11AuthModeShared)
1932                         erq->flags |= IW_ENCODE_RESTRICTED;             /* XXX */
1933                 else
1934                         erq->flags |= IW_ENCODE_OPEN;           /* XXX */
1935
1936         }
1937         else if (kid == 0)
1938         {
1939                 if (pAdapter->StaCfg.AuthMode == Ndis802_11AuthModeShared)
1940                         erq->flags |= IW_ENCODE_RESTRICTED;             /* XXX */
1941                 else
1942                         erq->flags |= IW_ENCODE_OPEN;           /* XXX */
1943                 erq->length = pAdapter->SharedKey[BSS0][pAdapter->StaCfg.DefaultKeyId].KeyLen;
1944                 memcpy(key, pAdapter->SharedKey[BSS0][pAdapter->StaCfg.DefaultKeyId].Key, erq->length);
1945                 // copy default key ID
1946                 if (pAdapter->StaCfg.AuthMode == Ndis802_11AuthModeShared)
1947                         erq->flags |= IW_ENCODE_RESTRICTED;             /* XXX */
1948                 else
1949                         erq->flags |= IW_ENCODE_OPEN;           /* XXX */
1950                 erq->flags = pAdapter->StaCfg.DefaultKeyId + 1;                 /* NB: base 1 */
1951                 erq->flags |= IW_ENCODE_ENABLED;        /* XXX */
1952         }
1953
1954         return 0;
1955
1956 }
1957
1958 static int
1959 rt_ioctl_setparam(struct net_device *dev, struct iw_request_info *info,
1960                          void *w, char *extra)
1961 {
1962     VIRTUAL_ADAPTER     *pVirtualAd = NULL;
1963         PRTMP_ADAPTER pAdapter;
1964         POS_COOKIE pObj;
1965         char *this_char = extra;
1966         char *value;
1967         int  Status=0;
1968
1969         if (dev->priv_flags == INT_MAIN)
1970         {
1971                 pAdapter = dev->ml_priv;
1972         }
1973         else
1974         {
1975                 pVirtualAd = dev->ml_priv;
1976                 pAdapter = pVirtualAd->RtmpDev->ml_priv;
1977         }
1978         pObj = (POS_COOKIE) pAdapter->OS_Cookie;
1979
1980         if (pAdapter == NULL)
1981         {
1982                 /* if 1st open fail, pAd will be free;
1983                    So the net_dev->ml_priv will be NULL in 2rd open */
1984                 return -ENETDOWN;
1985         }
1986
1987         {
1988                 pObj->ioctl_if_type = INT_MAIN;
1989         pObj->ioctl_if = MAIN_MBSSID;
1990         }
1991
1992         //check if the interface is down
1993         if(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE))
1994         {
1995                 DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
1996                         return -ENETDOWN;
1997         }
1998
1999         if (!*this_char)
2000                 return -EINVAL;
2001
2002         if ((value = rtstrchr(this_char, '=')) != NULL)
2003             *value++ = 0;
2004
2005         if (!value)
2006             return -EINVAL;
2007
2008         // reject setting nothing besides ANY ssid(ssidLen=0)
2009     if (!*value && (strcmp(this_char, "SSID") != 0))
2010         return -EINVAL;
2011
2012         for (PRTMP_PRIVATE_SET_PROC = RTMP_PRIVATE_SUPPORT_PROC; PRTMP_PRIVATE_SET_PROC->name; PRTMP_PRIVATE_SET_PROC++)
2013         {
2014             if (strcmp(this_char, PRTMP_PRIVATE_SET_PROC->name) == 0)
2015             {
2016                 if(!PRTMP_PRIVATE_SET_PROC->set_proc(pAdapter, value))
2017                 {       //FALSE:Set private failed then return Invalid argument
2018                             Status = -EINVAL;
2019                 }
2020                     break;      //Exit for loop.
2021             }
2022         }
2023
2024         if(PRTMP_PRIVATE_SET_PROC->name == NULL)
2025         {  //Not found argument
2026             Status = -EINVAL;
2027             DBGPRINT(RT_DEBUG_TRACE, ("===>rt_ioctl_setparam:: (iwpriv) Not Support Set Command [%s=%s]\n", this_char, value));
2028         }
2029
2030     return Status;
2031 }
2032
2033
2034 static int
2035 rt_private_get_statistics(struct net_device *dev, struct iw_request_info *info,
2036                 struct iw_point *wrq, char *extra)
2037 {
2038         INT                             Status = 0;
2039     PRTMP_ADAPTER   pAd = dev->ml_priv;
2040
2041     if (extra == NULL)
2042     {
2043         wrq->length = 0;
2044         return -EIO;
2045     }
2046
2047     memset(extra, 0x00, IW_PRIV_SIZE_MASK);
2048     sprintf(extra, "\n\n");
2049
2050         {
2051     sprintf(extra+strlen(extra), "Tx success                      = %ld\n", (ULONG)pAd->WlanCounters.TransmittedFragmentCount.QuadPart);
2052     sprintf(extra+strlen(extra), "Tx success without retry        = %ld\n", (ULONG)pAd->WlanCounters.TransmittedFragmentCount.QuadPart - (ULONG)pAd->WlanCounters.RetryCount.QuadPart);
2053         }
2054     sprintf(extra+strlen(extra), "Tx success after retry          = %ld\n", (ULONG)pAd->WlanCounters.RetryCount.QuadPart);
2055     sprintf(extra+strlen(extra), "Tx fail to Rcv ACK after retry  = %ld\n", (ULONG)pAd->WlanCounters.FailedCount.QuadPart);
2056     sprintf(extra+strlen(extra), "RTS Success Rcv CTS             = %ld\n", (ULONG)pAd->WlanCounters.RTSSuccessCount.QuadPart);
2057     sprintf(extra+strlen(extra), "RTS Fail Rcv CTS                = %ld\n", (ULONG)pAd->WlanCounters.RTSFailureCount.QuadPart);
2058
2059     sprintf(extra+strlen(extra), "Rx success                      = %ld\n", (ULONG)pAd->WlanCounters.ReceivedFragmentCount.QuadPart);
2060     sprintf(extra+strlen(extra), "Rx with CRC                     = %ld\n", (ULONG)pAd->WlanCounters.FCSErrorCount.QuadPart);
2061     sprintf(extra+strlen(extra), "Rx drop due to out of resource  = %ld\n", (ULONG)pAd->Counters8023.RxNoBuffer);
2062     sprintf(extra+strlen(extra), "Rx duplicate frame              = %ld\n", (ULONG)pAd->WlanCounters.FrameDuplicateCount.QuadPart);
2063
2064     sprintf(extra+strlen(extra), "False CCA (one second)          = %ld\n", (ULONG)pAd->RalinkCounters.OneSecFalseCCACnt);
2065         {
2066         sprintf(extra+strlen(extra), "RSSI-A                          = %ld\n", (LONG)(pAd->StaCfg.RssiSample.LastRssi0 - pAd->BbpRssiToDbmDelta));
2067         sprintf(extra+strlen(extra), "RSSI-B (if available)           = %ld\n", (LONG)(pAd->StaCfg.RssiSample.LastRssi1 - pAd->BbpRssiToDbmDelta));
2068         sprintf(extra+strlen(extra), "RSSI-C (if available)           = %ld\n\n", (LONG)(pAd->StaCfg.RssiSample.LastRssi2 - pAd->BbpRssiToDbmDelta));
2069         }
2070     sprintf(extra+strlen(extra), "WpaSupplicantUP                 = %d\n\n", pAd->StaCfg.WpaSupplicantUP);
2071
2072     wrq->length = strlen(extra) + 1; // 1: size of '\0'
2073     DBGPRINT(RT_DEBUG_TRACE, ("<== rt_private_get_statistics, wrq->length = %d\n", wrq->length));
2074
2075     return Status;
2076 }
2077
2078 void    getBaInfo(
2079         IN      PRTMP_ADAPTER   pAd,
2080         IN      PUCHAR                  pOutBuf)
2081 {
2082         INT i, j;
2083         BA_ORI_ENTRY *pOriBAEntry;
2084         BA_REC_ENTRY *pRecBAEntry;
2085
2086         for (i=0; i<MAX_LEN_OF_MAC_TABLE; i++)
2087         {
2088                 PMAC_TABLE_ENTRY pEntry = &pAd->MacTab.Content[i];
2089                 if (((pEntry->ValidAsCLI || pEntry->ValidAsApCli) && (pEntry->Sst == SST_ASSOC))
2090                         || (pEntry->ValidAsWDS) || (pEntry->ValidAsMesh))
2091                 {
2092                         sprintf(pOutBuf + strlen(pOutBuf), "\n%02X:%02X:%02X:%02X:%02X:%02X (Aid = %d) (AP) -\n",
2093                                 pEntry->Addr[0], pEntry->Addr[1], pEntry->Addr[2],
2094                                 pEntry->Addr[3], pEntry->Addr[4], pEntry->Addr[5], pEntry->Aid);
2095
2096                         sprintf(pOutBuf, "%s[Recipient]\n", pOutBuf);
2097                         for (j=0; j < NUM_OF_TID; j++)
2098                         {
2099                                 if (pEntry->BARecWcidArray[j] != 0)
2100                                 {
2101                                         pRecBAEntry =&pAd->BATable.BARecEntry[pEntry->BARecWcidArray[j]];
2102                                         sprintf(pOutBuf + strlen(pOutBuf), "TID=%d, BAWinSize=%d, LastIndSeq=%d, ReorderingPkts=%d\n", j, pRecBAEntry->BAWinSize, pRecBAEntry->LastIndSeq, pRecBAEntry->list.qlen);
2103                                 }
2104                         }
2105                         sprintf(pOutBuf, "%s\n", pOutBuf);
2106
2107                         sprintf(pOutBuf, "%s[Originator]\n", pOutBuf);
2108                         for (j=0; j < NUM_OF_TID; j++)
2109                         {
2110                                 if (pEntry->BAOriWcidArray[j] != 0)
2111                                 {
2112                                         pOriBAEntry =&pAd->BATable.BAOriEntry[pEntry->BAOriWcidArray[j]];
2113                                         sprintf(pOutBuf + strlen(pOutBuf), "TID=%d, BAWinSize=%d, StartSeq=%d, CurTxSeq=%d\n", j, pOriBAEntry->BAWinSize, pOriBAEntry->Sequence, pEntry->TxSeq[j]);
2114                                 }
2115                         }
2116                         sprintf(pOutBuf, "%s\n\n", pOutBuf);
2117                 }
2118         if (strlen(pOutBuf) > (IW_PRIV_SIZE_MASK - 30))
2119                 break;
2120         }
2121
2122         return;
2123 }
2124
2125 static int
2126 rt_private_show(struct net_device *dev, struct iw_request_info *info,
2127                 struct iw_point *wrq, char *extra)
2128 {
2129     INT                         Status = 0;
2130     VIRTUAL_ADAPTER     *pVirtualAd = NULL;
2131     PRTMP_ADAPTER   pAd;
2132         POS_COOKIE              pObj;
2133     u32             subcmd = wrq->flags;
2134
2135         if (dev->priv_flags == INT_MAIN)
2136                 pAd = dev->ml_priv;
2137         else
2138         {
2139                 pVirtualAd = dev->ml_priv;
2140                 pAd = pVirtualAd->RtmpDev->ml_priv;
2141         }
2142         pObj = (POS_COOKIE) pAd->OS_Cookie;
2143
2144         if (pAd == NULL)
2145         {
2146                 /* if 1st open fail, pAd will be free;
2147                    So the net_dev->ml_priv will be NULL in 2rd open */
2148                 return -ENETDOWN;
2149         }
2150
2151     if (extra == NULL)
2152     {
2153         wrq->length = 0;
2154         return -EIO;
2155     }
2156     memset(extra, 0x00, IW_PRIV_SIZE_MASK);
2157
2158         {
2159                 pObj->ioctl_if_type = INT_MAIN;
2160         pObj->ioctl_if = MAIN_MBSSID;
2161         }
2162
2163     switch(subcmd)
2164     {
2165
2166         case SHOW_CONN_STATUS:
2167             if (MONITOR_ON(pAd))
2168             {
2169                 if (pAd->CommonCfg.PhyMode >= PHY_11ABGN_MIXED &&
2170                     pAd->CommonCfg.RegTransmitSetting.field.BW)
2171                     sprintf(extra, "Monitor Mode(CentralChannel %d)\n", pAd->CommonCfg.CentralChannel);
2172                 else
2173                     sprintf(extra, "Monitor Mode(Channel %d)\n", pAd->CommonCfg.Channel);
2174             }
2175             else
2176             {
2177                 if (pAd->IndicateMediaState == NdisMediaStateConnected)
2178                 {
2179                     if (INFRA_ON(pAd))
2180                     {
2181                     sprintf(extra, "Connected(AP: %s[%02X:%02X:%02X:%02X:%02X:%02X])\n",
2182                                     pAd->CommonCfg.Ssid,
2183                                     pAd->CommonCfg.Bssid[0],
2184                                     pAd->CommonCfg.Bssid[1],
2185                                     pAd->CommonCfg.Bssid[2],
2186                                     pAd->CommonCfg.Bssid[3],
2187                                     pAd->CommonCfg.Bssid[4],
2188                                     pAd->CommonCfg.Bssid[5]);
2189                         DBGPRINT(RT_DEBUG_TRACE ,("Ssid=%s ,Ssidlen = %d\n",pAd->CommonCfg.Ssid, pAd->CommonCfg.SsidLen));
2190                 }
2191                     else if (ADHOC_ON(pAd))
2192                         sprintf(extra, "Connected\n");
2193                 }
2194                 else
2195                 {
2196                     sprintf(extra, "Disconnected\n");
2197                         DBGPRINT(RT_DEBUG_TRACE ,("ConnStatus is not connected\n"));
2198                 }
2199             }
2200             wrq->length = strlen(extra) + 1; // 1: size of '\0'
2201             break;
2202         case SHOW_DRVIER_VERION:
2203             sprintf(extra, "Driver version-%s, %s %s\n", STA_DRIVER_VERSION, __DATE__, __TIME__ );
2204             wrq->length = strlen(extra) + 1; // 1: size of '\0'
2205             break;
2206         case SHOW_BA_INFO:
2207             getBaInfo(pAd, extra);
2208             wrq->length = strlen(extra) + 1; // 1: size of '\0'
2209             break;
2210                 case SHOW_DESC_INFO:
2211                         {
2212                                 Show_DescInfo_Proc(pAd, NULL);
2213                                 wrq->length = 0; // 1: size of '\0'
2214                         }
2215                         break;
2216         case RAIO_OFF:
2217             if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_BSS_SCAN_IN_PROGRESS))
2218             {
2219                 sprintf(extra, "Scanning\n");
2220                 wrq->length = strlen(extra) + 1; // 1: size of '\0'
2221                 break;
2222             }
2223             pAd->StaCfg.bSwRadio = FALSE;
2224             if (pAd->StaCfg.bRadio != (pAd->StaCfg.bHwRadio && pAd->StaCfg.bSwRadio))
2225             {
2226                 pAd->StaCfg.bRadio = (pAd->StaCfg.bHwRadio && pAd->StaCfg.bSwRadio);
2227                 if (pAd->StaCfg.bRadio == FALSE)
2228                 {
2229                     MlmeRadioOff(pAd);
2230                     // Update extra information
2231                                         pAd->ExtraInfo = SW_RADIO_OFF;
2232                 }
2233             }
2234             sprintf(extra, "Radio Off\n");
2235             wrq->length = strlen(extra) + 1; // 1: size of '\0'
2236             break;
2237         case RAIO_ON:
2238 #ifdef RT2870
2239             if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_BSS_SCAN_IN_PROGRESS))
2240             {
2241                 sprintf(extra, "Scanning\n");
2242                 wrq->length = strlen(extra) + 1; // 1: size of '\0'
2243                 break;
2244             }
2245 #endif
2246             pAd->StaCfg.bSwRadio = TRUE;
2247             //if (pAd->StaCfg.bRadio != (pAd->StaCfg.bHwRadio && pAd->StaCfg.bSwRadio))
2248             {
2249                 pAd->StaCfg.bRadio = (pAd->StaCfg.bHwRadio && pAd->StaCfg.bSwRadio);
2250                 if (pAd->StaCfg.bRadio == TRUE)
2251                 {
2252                     MlmeRadioOn(pAd);
2253                     // Update extra information
2254                                         pAd->ExtraInfo = EXTRA_INFO_CLEAR;
2255                 }
2256             }
2257             sprintf(extra, "Radio On\n");
2258             wrq->length = strlen(extra) + 1; // 1: size of '\0'
2259             break;
2260
2261                 case SHOW_CFG_VALUE:
2262                         {
2263                                 Status = RTMPShowCfgValue(pAd, wrq->pointer, extra);
2264                                 if (Status == 0)
2265                                         wrq->length = strlen(extra) + 1; // 1: size of '\0'
2266                         }
2267                         break;
2268 #if !defined(RT2860) && !defined(RT30xx)
2269                 case SHOW_ADHOC_ENTRY_INFO:
2270                         Show_Adhoc_MacTable_Proc(pAd, extra);
2271                         wrq->length = strlen(extra) + 1; // 1: size of '\0'
2272                         break;
2273 #endif
2274         default:
2275             DBGPRINT(RT_DEBUG_TRACE, ("%s - unknow subcmd = %d\n", __func__, subcmd));
2276             break;
2277     }
2278
2279     return Status;
2280 }
2281
2282 #ifdef SIOCSIWMLME
2283 int rt_ioctl_siwmlme(struct net_device *dev,
2284                            struct iw_request_info *info,
2285                            union iwreq_data *wrqu,
2286                            char *extra)
2287 {
2288         PRTMP_ADAPTER   pAd = dev->ml_priv;
2289         struct iw_mlme *pMlme = (struct iw_mlme *)wrqu->data.pointer;
2290         MLME_QUEUE_ELEM                         MsgElem;
2291         MLME_DISASSOC_REQ_STRUCT        DisAssocReq;
2292         MLME_DEAUTH_REQ_STRUCT      DeAuthReq;
2293
2294         DBGPRINT(RT_DEBUG_TRACE, ("====> %s\n", __func__));
2295
2296         if (pMlme == NULL)
2297                 return -EINVAL;
2298
2299         switch(pMlme->cmd)
2300         {
2301 #ifdef IW_MLME_DEAUTH
2302                 case IW_MLME_DEAUTH:
2303                         DBGPRINT(RT_DEBUG_TRACE, ("====> %s - IW_MLME_DEAUTH\n", __func__));
2304                         COPY_MAC_ADDR(DeAuthReq.Addr, pAd->CommonCfg.Bssid);
2305                         DeAuthReq.Reason = pMlme->reason_code;
2306                         MsgElem.MsgLen = sizeof(MLME_DEAUTH_REQ_STRUCT);
2307                         NdisMoveMemory(MsgElem.Msg, &DeAuthReq, sizeof(MLME_DEAUTH_REQ_STRUCT));
2308                         MlmeDeauthReqAction(pAd, &MsgElem);
2309                         if (INFRA_ON(pAd))
2310                         {
2311                             LinkDown(pAd, FALSE);
2312                             pAd->Mlme.AssocMachine.CurrState = ASSOC_IDLE;
2313                         }
2314                         break;
2315 #endif // IW_MLME_DEAUTH //
2316 #ifdef IW_MLME_DISASSOC
2317                 case IW_MLME_DISASSOC:
2318                         DBGPRINT(RT_DEBUG_TRACE, ("====> %s - IW_MLME_DISASSOC\n", __func__));
2319                         COPY_MAC_ADDR(DisAssocReq.Addr, pAd->CommonCfg.Bssid);
2320                         DisAssocReq.Reason =  pMlme->reason_code;
2321
2322                         MsgElem.Machine = ASSOC_STATE_MACHINE;
2323                         MsgElem.MsgType = MT2_MLME_DISASSOC_REQ;
2324                         MsgElem.MsgLen = sizeof(MLME_DISASSOC_REQ_STRUCT);
2325                         NdisMoveMemory(MsgElem.Msg, &DisAssocReq, sizeof(MLME_DISASSOC_REQ_STRUCT));
2326
2327                         pAd->Mlme.CntlMachine.CurrState = CNTL_WAIT_OID_DISASSOC;
2328                         MlmeDisassocReqAction(pAd, &MsgElem);
2329                         break;
2330 #endif // IW_MLME_DISASSOC //
2331                 default:
2332                         DBGPRINT(RT_DEBUG_TRACE, ("====> %s - Unknow Command\n", __func__));
2333                         break;
2334         }
2335
2336         return 0;
2337 }
2338 #endif // SIOCSIWMLME //
2339
2340 #if WIRELESS_EXT > 17
2341 int rt_ioctl_siwauth(struct net_device *dev,
2342                           struct iw_request_info *info,
2343                           union iwreq_data *wrqu, char *extra)
2344 {
2345         PRTMP_ADAPTER   pAdapter = dev->ml_priv;
2346         struct iw_param *param = &wrqu->param;
2347
2348     //check if the interface is down
2349         if(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE))
2350         {
2351                 DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
2352         return -ENETDOWN;
2353         }
2354         switch (param->flags & IW_AUTH_INDEX) {
2355         case IW_AUTH_WPA_VERSION:
2356             if (param->value == IW_AUTH_WPA_VERSION_WPA)
2357             {
2358                 pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeWPAPSK;
2359                                 if (pAdapter->StaCfg.BssType == BSS_ADHOC)
2360                                         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeWPANone;
2361             }
2362             else if (param->value == IW_AUTH_WPA_VERSION_WPA2)
2363                 pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeWPA2PSK;
2364
2365             DBGPRINT(RT_DEBUG_TRACE, ("%s::IW_AUTH_WPA_VERSION - param->value = %d!\n", __func__, param->value));
2366             break;
2367         case IW_AUTH_CIPHER_PAIRWISE:
2368             if (param->value == IW_AUTH_CIPHER_NONE)
2369             {
2370                 pAdapter->StaCfg.WepStatus = Ndis802_11WEPDisabled;
2371                 pAdapter->StaCfg.OrigWepStatus = pAdapter->StaCfg.WepStatus;
2372                 pAdapter->StaCfg.PairCipher = Ndis802_11WEPDisabled;
2373             }
2374             else if (param->value == IW_AUTH_CIPHER_WEP40 ||
2375                      param->value == IW_AUTH_CIPHER_WEP104)
2376             {
2377                 pAdapter->StaCfg.WepStatus = Ndis802_11WEPEnabled;
2378                 pAdapter->StaCfg.OrigWepStatus = pAdapter->StaCfg.WepStatus;
2379                 pAdapter->StaCfg.PairCipher = Ndis802_11WEPEnabled;
2380                 pAdapter->StaCfg.IEEE8021X = FALSE;
2381             }
2382             else if (param->value == IW_AUTH_CIPHER_TKIP)
2383             {
2384                 pAdapter->StaCfg.WepStatus = Ndis802_11Encryption2Enabled;
2385                 pAdapter->StaCfg.OrigWepStatus = pAdapter->StaCfg.WepStatus;
2386                 pAdapter->StaCfg.PairCipher = Ndis802_11Encryption2Enabled;
2387             }
2388             else if (param->value == IW_AUTH_CIPHER_CCMP)
2389             {
2390                 pAdapter->StaCfg.WepStatus = Ndis802_11Encryption3Enabled;
2391                 pAdapter->StaCfg.OrigWepStatus = pAdapter->StaCfg.WepStatus;
2392                 pAdapter->StaCfg.PairCipher = Ndis802_11Encryption3Enabled;
2393             }
2394             DBGPRINT(RT_DEBUG_TRACE, ("%s::IW_AUTH_CIPHER_PAIRWISE - param->value = %d!\n", __func__, param->value));
2395             break;
2396         case IW_AUTH_CIPHER_GROUP:
2397             if (param->value == IW_AUTH_CIPHER_NONE)
2398             {
2399                 pAdapter->StaCfg.GroupCipher = Ndis802_11WEPDisabled;
2400             }
2401             else if (param->value == IW_AUTH_CIPHER_WEP40 ||
2402                      param->value == IW_AUTH_CIPHER_WEP104)
2403             {
2404                 pAdapter->StaCfg.GroupCipher = Ndis802_11WEPEnabled;
2405             }
2406             else if (param->value == IW_AUTH_CIPHER_TKIP)
2407             {
2408                 pAdapter->StaCfg.GroupCipher = Ndis802_11Encryption2Enabled;
2409             }
2410             else if (param->value == IW_AUTH_CIPHER_CCMP)
2411             {
2412                 pAdapter->StaCfg.GroupCipher = Ndis802_11Encryption3Enabled;
2413             }
2414             DBGPRINT(RT_DEBUG_TRACE, ("%s::IW_AUTH_CIPHER_GROUP - param->value = %d!\n", __func__, param->value));
2415             break;
2416         case IW_AUTH_KEY_MGMT:
2417             if (param->value == IW_AUTH_KEY_MGMT_802_1X)
2418             {
2419                 if (pAdapter->StaCfg.AuthMode == Ndis802_11AuthModeWPAPSK)
2420                 {
2421                     pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeWPA;
2422                     pAdapter->StaCfg.IEEE8021X = FALSE;
2423                 }
2424                 else if (pAdapter->StaCfg.AuthMode == Ndis802_11AuthModeWPA2PSK)
2425                 {
2426                     pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeWPA2;
2427                     pAdapter->StaCfg.IEEE8021X = FALSE;
2428                 }
2429                 else
2430                     // WEP 1x
2431                     pAdapter->StaCfg.IEEE8021X = TRUE;
2432             }
2433             else if (param->value == 0)
2434             {
2435                                 STA_PORT_SECURED(pAdapter);
2436             }
2437             DBGPRINT(RT_DEBUG_TRACE, ("%s::IW_AUTH_KEY_MGMT - param->value = %d!\n", __func__, param->value));
2438             break;
2439         case IW_AUTH_RX_UNENCRYPTED_EAPOL:
2440             break;
2441         case IW_AUTH_PRIVACY_INVOKED:
2442             DBGPRINT(RT_DEBUG_TRACE, ("%s::IW_AUTH_PRIVACY_INVOKED - param->value = %d!\n", __func__, param->value));
2443                 break;
2444         case IW_AUTH_DROP_UNENCRYPTED:
2445             if (param->value != 0)
2446                 pAdapter->StaCfg.PortSecured = WPA_802_1X_PORT_NOT_SECURED;
2447                         else
2448                         {
2449                                 STA_PORT_SECURED(pAdapter);
2450                         }
2451             DBGPRINT(RT_DEBUG_TRACE, ("%s::IW_AUTH_WPA_VERSION - param->value = %d!\n", __func__, param->value));
2452                 break;
2453         case IW_AUTH_80211_AUTH_ALG:
2454                         if (param->value & IW_AUTH_ALG_SHARED_KEY)
2455             {
2456                                 pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeShared;
2457                         }
2458             else if (param->value & IW_AUTH_ALG_OPEN_SYSTEM)
2459             {
2460                                 pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeOpen;
2461                         }
2462             else
2463                                 return -EINVAL;
2464             DBGPRINT(RT_DEBUG_TRACE, ("%s::IW_AUTH_80211_AUTH_ALG - param->value = %d!\n", __func__, param->value));
2465                         break;
2466         case IW_AUTH_WPA_ENABLED:
2467                 DBGPRINT(RT_DEBUG_TRACE, ("%s::IW_AUTH_WPA_ENABLED - Driver supports WPA!(param->value = %d)\n", __func__, param->value));
2468                 break;
2469         default:
2470                 return -EOPNOTSUPP;
2471 }
2472
2473         return 0;
2474 }
2475
2476 int rt_ioctl_giwauth(struct net_device *dev,
2477                                struct iw_request_info *info,
2478                                union iwreq_data *wrqu, char *extra)
2479 {
2480         PRTMP_ADAPTER   pAdapter = dev->ml_priv;
2481         struct iw_param *param = &wrqu->param;
2482
2483     //check if the interface is down
2484         if(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE))
2485     {
2486                 DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
2487         return -ENETDOWN;
2488     }
2489
2490         switch (param->flags & IW_AUTH_INDEX) {
2491         case IW_AUTH_DROP_UNENCRYPTED:
2492         param->value = (pAdapter->StaCfg.WepStatus == Ndis802_11WEPDisabled) ? 0 : 1;
2493                 break;
2494
2495         case IW_AUTH_80211_AUTH_ALG:
2496         param->value = (pAdapter->StaCfg.AuthMode == Ndis802_11AuthModeShared) ? IW_AUTH_ALG_SHARED_KEY : IW_AUTH_ALG_OPEN_SYSTEM;
2497                 break;
2498
2499         case IW_AUTH_WPA_ENABLED:
2500                 param->value = (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA) ? 1 : 0;
2501                 break;
2502
2503         default:
2504                 return -EOPNOTSUPP;
2505         }
2506     DBGPRINT(RT_DEBUG_TRACE, ("rt_ioctl_giwauth::param->value = %d!\n", param->value));
2507         return 0;
2508 }
2509
2510 void fnSetCipherKey(
2511     IN  PRTMP_ADAPTER   pAdapter,
2512     IN  INT             keyIdx,
2513     IN  UCHAR           CipherAlg,
2514     IN  BOOLEAN         bGTK,
2515     IN  struct iw_encode_ext *ext)
2516 {
2517 #ifdef RT2860
2518         RTMP_CLEAR_PSFLAG(pAdapter, fRTMP_PS_CAN_GO_SLEEP);
2519         if (RTMP_TEST_PSFLAG(pAdapter, fRTMP_PS_SET_PCI_CLK_OFF_COMMAND))
2520         {
2521                 if (pAdapter->StaCfg.bRadio == FALSE)
2522                 {
2523                         RTMP_SET_PSFLAG(pAdapter, fRTMP_PS_CAN_GO_SLEEP);
2524                         return;
2525                 }
2526                 DBGPRINT(RT_DEBUG_TRACE,("RTMPWPAAddKeyProc1==>\n"));
2527                 RTMPPCIeLinkCtrlValueRestore(pAdapter, RESTORE_HALT);
2528                 RTMPusecDelay(6000);
2529                 pAdapter->bPCIclkOff = FALSE;
2530         }
2531 #endif
2532     NdisZeroMemory(&pAdapter->SharedKey[BSS0][keyIdx], sizeof(CIPHER_KEY));
2533     pAdapter->SharedKey[BSS0][keyIdx].KeyLen = LEN_TKIP_EK;
2534     NdisMoveMemory(pAdapter->SharedKey[BSS0][keyIdx].Key, ext->key, LEN_TKIP_EK);
2535     NdisMoveMemory(pAdapter->SharedKey[BSS0][keyIdx].TxMic, ext->key + LEN_TKIP_EK, LEN_TKIP_TXMICK);
2536     NdisMoveMemory(pAdapter->SharedKey[BSS0][keyIdx].RxMic, ext->key + LEN_TKIP_EK + LEN_TKIP_TXMICK, LEN_TKIP_RXMICK);
2537     pAdapter->SharedKey[BSS0][keyIdx].CipherAlg = CipherAlg;
2538
2539     // Update group key information to ASIC Shared Key Table
2540         AsicAddSharedKeyEntry(pAdapter,
2541                                                   BSS0,
2542                                                   keyIdx,
2543                                                   pAdapter->SharedKey[BSS0][keyIdx].CipherAlg,
2544                                                   pAdapter->SharedKey[BSS0][keyIdx].Key,
2545                                                   pAdapter->SharedKey[BSS0][keyIdx].TxMic,
2546                                                   pAdapter->SharedKey[BSS0][keyIdx].RxMic);
2547
2548     if (bGTK)
2549         // Update ASIC WCID attribute table and IVEIV table
2550         RTMPAddWcidAttributeEntry(pAdapter,
2551                                                           BSS0,
2552                                                           keyIdx,
2553                                                           pAdapter->SharedKey[BSS0][keyIdx].CipherAlg,
2554                                                           NULL);
2555     else
2556         // Update ASIC WCID attribute table and IVEIV table
2557         RTMPAddWcidAttributeEntry(pAdapter,
2558                                                           BSS0,
2559                                                           keyIdx,
2560                                                           pAdapter->SharedKey[BSS0][keyIdx].CipherAlg,
2561                                                           &pAdapter->MacTab.Content[BSSID_WCID]);
2562 #ifdef RT2860
2563         RTMP_SET_PSFLAG(pAdapter, fRTMP_PS_CAN_GO_SLEEP);
2564 #endif
2565 }
2566
2567 int rt_ioctl_siwencodeext(struct net_device *dev,
2568                            struct iw_request_info *info,
2569                            union iwreq_data *wrqu,
2570                            char *extra)
2571                         {
2572     PRTMP_ADAPTER   pAdapter = dev->ml_priv;
2573         struct iw_point *encoding = &wrqu->encoding;
2574         struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
2575     int keyIdx, alg = ext->alg;
2576
2577     //check if the interface is down
2578         if(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE))
2579         {
2580                 DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
2581         return -ENETDOWN;
2582         }
2583
2584     if (encoding->flags & IW_ENCODE_DISABLED)
2585         {
2586         keyIdx = (encoding->flags & IW_ENCODE_INDEX) - 1;
2587         // set BSSID wcid entry of the Pair-wise Key table as no-security mode
2588             AsicRemovePairwiseKeyEntry(pAdapter, BSS0, BSSID_WCID);
2589         pAdapter->SharedKey[BSS0][keyIdx].KeyLen = 0;
2590                 pAdapter->SharedKey[BSS0][keyIdx].CipherAlg = CIPHER_NONE;
2591                 AsicRemoveSharedKeyEntry(pAdapter, 0, (UCHAR)keyIdx);
2592         NdisZeroMemory(&pAdapter->SharedKey[BSS0][keyIdx], sizeof(CIPHER_KEY));
2593         DBGPRINT(RT_DEBUG_TRACE, ("%s::Remove all keys!(encoding->flags = %x)\n", __func__, encoding->flags));
2594     }
2595                                         else
2596     {
2597         // Get Key Index and convet to our own defined key index
2598         keyIdx = (encoding->flags & IW_ENCODE_INDEX) - 1;
2599         if((keyIdx < 0) || (keyIdx >= NR_WEP_KEYS))
2600                 return -EINVAL;
2601
2602         if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY)
2603         {
2604             pAdapter->StaCfg.DefaultKeyId = keyIdx;
2605             DBGPRINT(RT_DEBUG_TRACE, ("%s::DefaultKeyId = %d\n", __func__, pAdapter->StaCfg.DefaultKeyId));
2606         }
2607
2608         switch (alg) {
2609                 case IW_ENCODE_ALG_NONE:
2610                 DBGPRINT(RT_DEBUG_TRACE, ("%s::IW_ENCODE_ALG_NONE\n", __func__));
2611                         break;
2612                 case IW_ENCODE_ALG_WEP:
2613                 DBGPRINT(RT_DEBUG_TRACE, ("%s::IW_ENCODE_ALG_WEP - ext->key_len = %d, keyIdx = %d\n", __func__, ext->key_len, keyIdx));
2614                         if (ext->key_len == MAX_WEP_KEY_SIZE)
2615                 {
2616                                 pAdapter->SharedKey[BSS0][keyIdx].KeyLen = MAX_WEP_KEY_SIZE;
2617                     pAdapter->SharedKey[BSS0][keyIdx].CipherAlg = CIPHER_WEP128;
2618                                 }
2619                         else if (ext->key_len == MIN_WEP_KEY_SIZE)
2620                 {
2621                     pAdapter->SharedKey[BSS0][keyIdx].KeyLen = MIN_WEP_KEY_SIZE;
2622                     pAdapter->SharedKey[BSS0][keyIdx].CipherAlg = CIPHER_WEP64;
2623                         }
2624                         else
2625                     return -EINVAL;
2626
2627                 NdisZeroMemory(pAdapter->SharedKey[BSS0][keyIdx].Key,  16);
2628                             NdisMoveMemory(pAdapter->SharedKey[BSS0][keyIdx].Key, ext->key, ext->key_len);
2629 #ifndef RT30xx
2630                                 if (pAdapter->StaCfg.GroupCipher == Ndis802_11GroupWEP40Enabled ||
2631                                         pAdapter->StaCfg.GroupCipher == Ndis802_11GroupWEP104Enabled)
2632                                 {
2633                                         // Set Group key material to Asic
2634                                         AsicAddSharedKeyEntry(pAdapter, BSS0, keyIdx, pAdapter->SharedKey[BSS0][keyIdx].CipherAlg, pAdapter->SharedKey[BSS0][keyIdx].Key, NULL, NULL);
2635
2636                                         // Update WCID attribute table and IVEIV table for this group key table
2637                                         RTMPAddWcidAttributeEntry(pAdapter, BSS0, keyIdx, pAdapter->SharedKey[BSS0][keyIdx].CipherAlg, NULL);
2638
2639                                         STA_PORT_SECURED(pAdapter);
2640
2641                                 // Indicate Connected for GUI
2642                                 pAdapter->IndicateMediaState = NdisMediaStateConnected;
2643                                 }
2644 #endif
2645                         break;
2646             case IW_ENCODE_ALG_TKIP:
2647                 DBGPRINT(RT_DEBUG_TRACE, ("%s::IW_ENCODE_ALG_TKIP - keyIdx = %d, ext->key_len = %d\n", __func__, keyIdx, ext->key_len));
2648                 if (ext->key_len == 32)
2649                 {
2650                     if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY)
2651                     {
2652                         fnSetCipherKey(pAdapter, keyIdx, CIPHER_TKIP, FALSE, ext);
2653                         if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA2)
2654                         {
2655                             STA_PORT_SECURED(pAdapter);
2656                         }
2657                 }
2658                     else if (ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY)
2659                     {
2660                         fnSetCipherKey(pAdapter, keyIdx, CIPHER_TKIP, TRUE, ext);
2661
2662                         // set 802.1x port control
2663                         STA_PORT_SECURED(pAdapter);
2664                     }
2665                 }
2666                 else
2667                     return -EINVAL;
2668                 break;
2669             case IW_ENCODE_ALG_CCMP:
2670                 if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY)
2671                 {
2672                     fnSetCipherKey(pAdapter, keyIdx, CIPHER_AES, FALSE, ext);
2673                     if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA2)
2674                         STA_PORT_SECURED(pAdapter);
2675                 }
2676                 else if (ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY)
2677                 {
2678                     fnSetCipherKey(pAdapter, keyIdx, CIPHER_AES, TRUE, ext);
2679
2680                     // set 802.1x port control
2681                         STA_PORT_SECURED(pAdapter);
2682                 }
2683                 break;
2684                 default:
2685                         return -EINVAL;
2686                 }
2687     }
2688
2689     return 0;
2690 }
2691
2692 int
2693 rt_ioctl_giwencodeext(struct net_device *dev,
2694                           struct iw_request_info *info,
2695                           union iwreq_data *wrqu, char *extra)
2696 {
2697         PRTMP_ADAPTER pAd = dev->ml_priv;
2698         PCHAR pKey = NULL;
2699         struct iw_point *encoding = &wrqu->encoding;
2700         struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
2701         int idx, max_key_len;
2702
2703         DBGPRINT(RT_DEBUG_TRACE ,("===> rt_ioctl_giwencodeext\n"));
2704
2705         max_key_len = encoding->length - sizeof(*ext);
2706         if (max_key_len < 0)
2707                 return -EINVAL;
2708
2709         idx = encoding->flags & IW_ENCODE_INDEX;
2710         if (idx)
2711         {
2712                 if (idx < 1 || idx > 4)
2713                         return -EINVAL;
2714                 idx--;
2715
2716                 if ((pAd->StaCfg.WepStatus == Ndis802_11Encryption2Enabled) ||
2717                         (pAd->StaCfg.WepStatus == Ndis802_11Encryption3Enabled))
2718                 {
2719                         if (idx != pAd->StaCfg.DefaultKeyId)
2720                         {
2721                                 ext->key_len = 0;
2722                                 return 0;
2723                         }
2724                 }
2725         }
2726         else
2727                 idx = pAd->StaCfg.DefaultKeyId;
2728
2729         encoding->flags = idx + 1;
2730         memset(ext, 0, sizeof(*ext));
2731
2732         ext->key_len = 0;
2733         switch(pAd->StaCfg.WepStatus) {
2734                 case Ndis802_11WEPDisabled:
2735                         ext->alg = IW_ENCODE_ALG_NONE;
2736                         encoding->flags |= IW_ENCODE_DISABLED;
2737                         break;
2738                 case Ndis802_11WEPEnabled:
2739                         ext->alg = IW_ENCODE_ALG_WEP;
2740                         if (pAd->SharedKey[BSS0][idx].KeyLen > max_key_len)
2741                                 return -E2BIG;
2742                         else
2743                         {
2744                                 ext->key_len = pAd->SharedKey[BSS0][idx].KeyLen;
2745                                 pKey = &(pAd->SharedKey[BSS0][idx].Key[0]);
2746                         }
2747                         break;
2748                 case Ndis802_11Encryption2Enabled:
2749                 case Ndis802_11Encryption3Enabled:
2750                         if (pAd->StaCfg.WepStatus == Ndis802_11Encryption2Enabled)
2751                                 ext->alg = IW_ENCODE_ALG_TKIP;
2752                         else
2753                                 ext->alg = IW_ENCODE_ALG_CCMP;
2754
2755                         if (max_key_len < 32)
2756                                 return -E2BIG;
2757                         else
2758                         {
2759                                 ext->key_len = 32;
2760                                 pKey = &pAd->StaCfg.PMK[0];
2761                         }
2762                         break;
2763                 default:
2764                         return -EINVAL;
2765         }
2766
2767         if (ext->key_len && pKey)
2768         {
2769                 encoding->flags |= IW_ENCODE_ENABLED;
2770                 memcpy(ext->key, pKey, ext->key_len);
2771         }
2772
2773         return 0;
2774 }
2775
2776 #ifdef SIOCSIWGENIE
2777 int rt_ioctl_siwgenie(struct net_device *dev,
2778                           struct iw_request_info *info,
2779                           union iwreq_data *wrqu, char *extra)
2780 {
2781         PRTMP_ADAPTER   pAd = dev->ml_priv;
2782
2783         if (wrqu->data.length > MAX_LEN_OF_RSNIE ||
2784             (wrqu->data.length && extra == NULL))
2785                 return -EINVAL;
2786
2787         if (wrqu->data.length)
2788         {
2789                 pAd->StaCfg.RSNIE_Len = wrqu->data.length;
2790                 NdisMoveMemory(&pAd->StaCfg.RSN_IE[0], extra, pAd->StaCfg.RSNIE_Len);
2791         }
2792         else
2793         {
2794                 pAd->StaCfg.RSNIE_Len = 0;
2795                 NdisZeroMemory(&pAd->StaCfg.RSN_IE[0], MAX_LEN_OF_RSNIE);
2796         }
2797
2798         return 0;
2799 }
2800 #endif // SIOCSIWGENIE //
2801
2802 int rt_ioctl_giwgenie(struct net_device *dev,
2803                                struct iw_request_info *info,
2804                                union iwreq_data *wrqu, char *extra)
2805 {
2806         PRTMP_ADAPTER   pAd = dev->ml_priv;
2807
2808         if ((pAd->StaCfg.RSNIE_Len == 0) ||
2809                 (pAd->StaCfg.AuthMode < Ndis802_11AuthModeWPA))
2810         {
2811                 wrqu->data.length = 0;
2812                 return 0;
2813         }
2814
2815 #ifdef SIOCSIWGENIE
2816         if (pAd->StaCfg.WpaSupplicantUP == WPA_SUPPLICANT_ENABLE)
2817         {
2818         if (wrqu->data.length < pAd->StaCfg.RSNIE_Len)
2819                 return -E2BIG;
2820
2821         wrqu->data.length = pAd->StaCfg.RSNIE_Len;
2822         memcpy(extra, &pAd->StaCfg.RSN_IE[0], pAd->StaCfg.RSNIE_Len);
2823         }
2824         else
2825 #endif // SIOCSIWGENIE //
2826         {
2827                 UCHAR RSNIe = IE_WPA;
2828
2829                 if (wrqu->data.length < (pAd->StaCfg.RSNIE_Len + 2)) // ID, Len
2830                         return -E2BIG;
2831                 wrqu->data.length = pAd->StaCfg.RSNIE_Len + 2;
2832
2833                 if ((pAd->StaCfg.AuthMode == Ndis802_11AuthModeWPA2PSK) ||
2834             (pAd->StaCfg.AuthMode == Ndis802_11AuthModeWPA2))
2835                         RSNIe = IE_RSN;
2836
2837                 extra[0] = (char)RSNIe;
2838                 extra[1] = pAd->StaCfg.RSNIE_Len;
2839                 memcpy(extra+2, &pAd->StaCfg.RSN_IE[0], pAd->StaCfg.RSNIE_Len);
2840         }
2841
2842         return 0;
2843 }
2844
2845 int rt_ioctl_siwpmksa(struct net_device *dev,
2846                            struct iw_request_info *info,
2847                            union iwreq_data *wrqu,
2848                            char *extra)
2849 {
2850         PRTMP_ADAPTER   pAd = dev->ml_priv;
2851         struct iw_pmksa *pPmksa = (struct iw_pmksa *)wrqu->data.pointer;
2852         INT     CachedIdx = 0, idx = 0;
2853
2854         if (pPmksa == NULL)
2855                 return -EINVAL;
2856
2857         DBGPRINT(RT_DEBUG_TRACE ,("===> rt_ioctl_siwpmksa\n"));
2858         switch(pPmksa->cmd)
2859         {
2860                 case IW_PMKSA_FLUSH:
2861                         NdisZeroMemory(pAd->StaCfg.SavedPMK, sizeof(BSSID_INFO)*PMKID_NO);
2862                         DBGPRINT(RT_DEBUG_TRACE ,("rt_ioctl_siwpmksa - IW_PMKSA_FLUSH\n"));
2863                         break;
2864                 case IW_PMKSA_REMOVE:
2865                         for (CachedIdx = 0; CachedIdx < pAd->StaCfg.SavedPMKNum; CachedIdx++)
2866                         {
2867                         // compare the BSSID
2868                         if (NdisEqualMemory(pPmksa->bssid.sa_data, pAd->StaCfg.SavedPMK[CachedIdx].BSSID, MAC_ADDR_LEN))
2869                         {
2870                                 NdisZeroMemory(pAd->StaCfg.SavedPMK[CachedIdx].BSSID, MAC_ADDR_LEN);
2871                                         NdisZeroMemory(pAd->StaCfg.SavedPMK[CachedIdx].PMKID, 16);
2872                                         for (idx = CachedIdx; idx < (pAd->StaCfg.SavedPMKNum - 1); idx++)
2873                                         {
2874                                                 NdisMoveMemory(&pAd->StaCfg.SavedPMK[idx].BSSID[0], &pAd->StaCfg.SavedPMK[idx+1].BSSID[0], MAC_ADDR_LEN);
2875                                                 NdisMoveMemory(&pAd->StaCfg.SavedPMK[idx].PMKID[0], &pAd->StaCfg.SavedPMK[idx+1].PMKID[0], 16);
2876                                         }
2877                                         pAd->StaCfg.SavedPMKNum--;
2878                                 break;
2879                         }
2880                 }
2881
2882                         DBGPRINT(RT_DEBUG_TRACE ,("rt_ioctl_siwpmksa - IW_PMKSA_REMOVE\n"));
2883                         break;
2884                 case IW_PMKSA_ADD:
2885                         for (CachedIdx = 0; CachedIdx < pAd->StaCfg.SavedPMKNum; CachedIdx++)
2886                         {
2887                         // compare the BSSID
2888                         if (NdisEqualMemory(pPmksa->bssid.sa_data, pAd->StaCfg.SavedPMK[CachedIdx].BSSID, MAC_ADDR_LEN))
2889                                 break;
2890                 }
2891
2892                 // Found, replace it
2893                 if (CachedIdx < PMKID_NO)
2894                 {
2895                         DBGPRINT(RT_DEBUG_OFF, ("Update PMKID, idx = %d\n", CachedIdx));
2896                         NdisMoveMemory(&pAd->StaCfg.SavedPMK[CachedIdx].BSSID[0], pPmksa->bssid.sa_data, MAC_ADDR_LEN);
2897                                 NdisMoveMemory(&pAd->StaCfg.SavedPMK[CachedIdx].PMKID[0], pPmksa->pmkid, 16);
2898                         pAd->StaCfg.SavedPMKNum++;
2899                 }
2900                 // Not found, replace the last one
2901                 else
2902                 {
2903                         // Randomly replace one
2904                         CachedIdx = (pPmksa->bssid.sa_data[5] % PMKID_NO);
2905                         DBGPRINT(RT_DEBUG_OFF, ("Update PMKID, idx = %d\n", CachedIdx));
2906                         NdisMoveMemory(&pAd->StaCfg.SavedPMK[CachedIdx].BSSID[0], pPmksa->bssid.sa_data, MAC_ADDR_LEN);
2907                                 NdisMoveMemory(&pAd->StaCfg.SavedPMK[CachedIdx].PMKID[0], pPmksa->pmkid, 16);
2908                 }
2909
2910                         DBGPRINT(RT_DEBUG_TRACE ,("rt_ioctl_siwpmksa - IW_PMKSA_ADD\n"));
2911                         break;
2912                 default:
2913                         DBGPRINT(RT_DEBUG_TRACE ,("rt_ioctl_siwpmksa - Unknow Command!!\n"));
2914                         break;
2915         }
2916
2917         return 0;
2918 }
2919 #endif // #if WIRELESS_EXT > 17
2920
2921 #ifdef DBG
2922 static int
2923 rt_private_ioctl_bbp(struct net_device *dev, struct iw_request_info *info,
2924                 struct iw_point *wrq, char *extra)
2925                         {
2926         CHAR                            *this_char;
2927         CHAR                            *value = NULL;
2928         UCHAR                           regBBP = 0;
2929         UINT32                          bbpId;
2930         UINT32                          bbpValue;
2931         BOOLEAN                         bIsPrintAllBBP = FALSE;
2932         INT                                     Status = 0;
2933     PRTMP_ADAPTER       pAdapter = dev->ml_priv;
2934
2935
2936         memset(extra, 0x00, IW_PRIV_SIZE_MASK);
2937
2938         if (wrq->length > 1) //No parameters.
2939                                 {
2940                 sprintf(extra, "\n");
2941
2942                 //Parsing Read or Write
2943                 this_char = wrq->pointer;
2944                 DBGPRINT(RT_DEBUG_TRACE, ("this_char=%s\n", this_char));
2945                 if (!*this_char)
2946                         goto next;
2947
2948                 if ((value = rtstrchr(this_char, '=')) != NULL)
2949                         *value++ = 0;
2950
2951                 if (!value || !*value)
2952                 { //Read
2953                         DBGPRINT(RT_DEBUG_TRACE, ("this_char=%s, value=%s\n", this_char, value));
2954                         if (sscanf(this_char, "%d", &(bbpId)) == 1)
2955                         {
2956 #ifndef RT30xx
2957                                 if (bbpId <= 136)
2958 #endif // RT30xx //
2959 #ifdef RT30xx
2960                                 if (bbpId <= 138)  // edit by johnli, RF power sequence setup, add BBP R138 for ADC dynamic on/off control
2961 #endif // RT30xx //
2962                                 {
2963                                         {
2964                                         RTMP_BBP_IO_READ8_BY_REG_ID(pAdapter, bbpId, &regBBP);
2965                                         }
2966                                         sprintf(extra+strlen(extra), "R%02d[0x%02X]:%02X\n", bbpId, bbpId*2, regBBP);
2967                     wrq->length = strlen(extra) + 1; // 1: size of '\0'
2968                                         DBGPRINT(RT_DEBUG_TRACE, ("msg=%s\n", extra));
2969                                 }
2970                                 else
2971                                 {//Invalid parametes, so default printk all bbp
2972                                         bIsPrintAllBBP = TRUE;
2973                                         goto next;
2974                                 }
2975                         }
2976                         else
2977                         { //Invalid parametes, so default printk all bbp
2978                                 bIsPrintAllBBP = TRUE;
2979                                 goto next;
2980                         }
2981                 }
2982                 else
2983                 { //Write
2984                         if ((sscanf(this_char, "%d", &(bbpId)) == 1) && (sscanf(value, "%x", &(bbpValue)) == 1))
2985                         {
2986 #ifndef RT30xx
2987                                 if (bbpId <= 136)
2988 #endif // RT30xx //
2989 #ifdef RT30xx
2990                                 if (bbpId <= 138)  // edit by johnli, RF power sequence setup, add BBP R138 for ADC dynamic on/off control
2991 #endif // RT30xx //
2992                                 {
2993                                         {
2994                                             RTMP_BBP_IO_WRITE8_BY_REG_ID(pAdapter, bbpId, bbpValue);
2995                                         //Read it back for showing
2996                                         RTMP_BBP_IO_READ8_BY_REG_ID(pAdapter, bbpId, &regBBP);
2997                         }
2998                                         sprintf(extra+strlen(extra), "R%02d[0x%02X]:%02X\n", bbpId, bbpId*2, regBBP);
2999                     wrq->length = strlen(extra) + 1; // 1: size of '\0'
3000                                         DBGPRINT(RT_DEBUG_TRACE, ("msg=%s\n", extra));
3001                                 }
3002                                 else
3003                                 {//Invalid parametes, so default printk all bbp
3004                                         bIsPrintAllBBP = TRUE;
3005                                         goto next;
3006                                 }
3007                         }
3008                         else
3009                         { //Invalid parametes, so default printk all bbp
3010                                 bIsPrintAllBBP = TRUE;
3011                                 goto next;
3012                         }
3013                 }
3014                 }
3015         else
3016                 bIsPrintAllBBP = TRUE;
3017
3018 next:
3019         if (bIsPrintAllBBP)
3020         {
3021                 memset(extra, 0x00, IW_PRIV_SIZE_MASK);
3022                 sprintf(extra, "\n");
3023 #ifndef RT30xx
3024                 for (bbpId = 0; bbpId <= 136; bbpId++)
3025 #endif // RT30xx //
3026 #ifdef RT30xx
3027                 for (bbpId = 0; bbpId <= 138; bbpId++)  // edit by johnli, RF power sequence setup, add BBP R138 for ADC dynamic on/off control
3028 #endif // RT30xx //
3029                 {
3030                     if (strlen(extra) >= (IW_PRIV_SIZE_MASK - 10))
3031                 break;
3032                         RTMP_BBP_IO_READ8_BY_REG_ID(pAdapter, bbpId, &regBBP);
3033 #ifndef RT30xx
3034                         sprintf(extra+strlen(extra), "R%02d[0x%02X]:%02X    ", bbpId, bbpId*2, regBBP);
3035                         if (bbpId%5 == 4)
3036                                 sprintf(extra+strlen(extra), "\n");
3037 #endif
3038 #ifdef RT30xx
3039                         sprintf(extra+strlen(extra), "%03d = %02X\n", bbpId, regBBP);  // edit by johnli, change display format
3040 #endif
3041                 }
3042
3043         wrq->length = strlen(extra) + 1; // 1: size of '\0'
3044         DBGPRINT(RT_DEBUG_TRACE, ("wrq->length = %d\n", wrq->length));
3045         }
3046
3047         DBGPRINT(RT_DEBUG_TRACE, ("<==rt_private_ioctl_bbp\n\n"));
3048
3049     return Status;
3050 }
3051 #endif // DBG //
3052
3053 int rt_ioctl_siwrate(struct net_device *dev,
3054                         struct iw_request_info *info,
3055                         union iwreq_data *wrqu, char *extra)
3056 {
3057     PRTMP_ADAPTER   pAd = dev->ml_priv;
3058     UINT32          rate = wrqu->bitrate.value, fixed = wrqu->bitrate.fixed;
3059
3060     //check if the interface is down
3061         if(!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_INTERRUPT_IN_USE))
3062         {
3063                 DBGPRINT(RT_DEBUG_TRACE, ("rt_ioctl_siwrate::Network is down!\n"));
3064         return -ENETDOWN;
3065         }
3066
3067     DBGPRINT(RT_DEBUG_TRACE, ("rt_ioctl_siwrate::(rate = %d, fixed = %d)\n", rate, fixed));
3068     /* rate = -1 => auto rate
3069        rate = X, fixed = 1 => (fixed rate X)
3070     */
3071     if (rate == -1)
3072     {
3073                 //Auto Rate
3074                 pAd->StaCfg.DesiredTransmitSetting.field.MCS = MCS_AUTO;
3075                 pAd->StaCfg.bAutoTxRateSwitch = TRUE;
3076                 if ((pAd->CommonCfg.PhyMode <= PHY_11G) ||
3077                     (pAd->MacTab.Content[BSSID_WCID].HTPhyMode.field.MODE <= MODE_OFDM))
3078                         RTMPSetDesiredRates(pAd, -1);
3079
3080                 SetCommonHT(pAd);
3081     }
3082     else
3083     {
3084         if (fixed)
3085         {
3086                 pAd->StaCfg.bAutoTxRateSwitch = FALSE;
3087             if ((pAd->CommonCfg.PhyMode <= PHY_11G) ||
3088                 (pAd->MacTab.Content[BSSID_WCID].HTPhyMode.field.MODE <= MODE_OFDM))
3089                 RTMPSetDesiredRates(pAd, rate);
3090             else
3091             {
3092                 pAd->StaCfg.DesiredTransmitSetting.field.MCS = MCS_AUTO;
3093                 SetCommonHT(pAd);
3094             }
3095             DBGPRINT(RT_DEBUG_TRACE, ("rt_ioctl_siwrate::(HtMcs=%d)\n",pAd->StaCfg.DesiredTransmitSetting.field.MCS));
3096         }
3097         else
3098         {
3099             // TODO: rate = X, fixed = 0 => (rates <= X)
3100             return -EOPNOTSUPP;
3101         }
3102     }
3103
3104     return 0;
3105 }
3106
3107 int rt_ioctl_giwrate(struct net_device *dev,
3108                                struct iw_request_info *info,
3109                                union iwreq_data *wrqu, char *extra)
3110 {
3111     PRTMP_ADAPTER   pAd = dev->ml_priv;
3112     int rate_index = 0, rate_count = 0;
3113     HTTRANSMIT_SETTING ht_setting;
3114     __s32 ralinkrate[] =
3115         {2,  4,   11,  22, // CCK
3116         12, 18,   24,  36, 48, 72, 96, 108, // OFDM
3117         13, 26,   39,  52,  78, 104, 117, 130, 26,  52,  78, 104, 156, 208, 234, 260, // 20MHz, 800ns GI, MCS: 0 ~ 15
3118         39, 78,  117, 156, 234, 312, 351, 390,                                                                            // 20MHz, 800ns GI, MCS: 16 ~ 23
3119         27, 54,   81, 108, 162, 216, 243, 270, 54, 108, 162, 216, 324, 432, 486, 540, // 40MHz, 800ns GI, MCS: 0 ~ 15
3120         81, 162, 243, 324, 486, 648, 729, 810,                                                                            // 40MHz, 800ns GI, MCS: 16 ~ 23
3121         14, 29,   43,  57,  87, 115, 130, 144, 29, 59,   87, 115, 173, 230, 260, 288, // 20MHz, 400ns GI, MCS: 0 ~ 15
3122         43, 87,  130, 173, 260, 317, 390, 433,                                                                            // 20MHz, 400ns GI, MCS: 16 ~ 23
3123         30, 60,   90, 120, 180, 240, 270, 300, 60, 120, 180, 240, 360, 480, 540, 600, // 40MHz, 400ns GI, MCS: 0 ~ 15
3124         90, 180, 270, 360, 540, 720, 810, 900};                                                                           // 40MHz, 400ns GI, MCS: 16 ~ 23
3125
3126     rate_count = sizeof(ralinkrate)/sizeof(__s32);
3127     //check if the interface is down
3128         if(!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_INTERRUPT_IN_USE))
3129         {
3130                 DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
3131         return -ENETDOWN;
3132         }
3133
3134     if ((pAd->StaCfg.bAutoTxRateSwitch == FALSE) &&
3135         (INFRA_ON(pAd)) &&
3136         ((pAd->CommonCfg.PhyMode <= PHY_11G) || (pAd->MacTab.Content[BSSID_WCID].HTPhyMode.field.MODE <= MODE_OFDM)))
3137         ht_setting.word = pAd->StaCfg.HTPhyMode.word;
3138     else
3139         ht_setting.word = pAd->MacTab.Content[BSSID_WCID].HTPhyMode.word;
3140
3141     if (ht_setting.field.MODE >= MODE_HTMIX)
3142     {
3143         rate_index = 12 + ((UCHAR)ht_setting.field.BW *24) + ((UCHAR)ht_setting.field.ShortGI *48) + ((UCHAR)ht_setting.field.MCS);
3144     }
3145     else
3146     if (ht_setting.field.MODE == MODE_OFDM)
3147         rate_index = (UCHAR)(ht_setting.field.MCS) + 4;
3148     else if (ht_setting.field.MODE == MODE_CCK)
3149         rate_index = (UCHAR)(ht_setting.field.MCS);
3150
3151     if (rate_index < 0)
3152         rate_index = 0;
3153
3154     if (rate_index > rate_count)
3155         rate_index = rate_count;
3156
3157     wrqu->bitrate.value = ralinkrate[rate_index] * 500000;
3158     wrqu->bitrate.disabled = 0;
3159
3160     return 0;
3161 }
3162
3163 static const iw_handler rt_handler[] =
3164 {
3165         (iw_handler) NULL,                                  /* SIOCSIWCOMMIT */
3166         (iw_handler) rt_ioctl_giwname,                  /* SIOCGIWNAME   */
3167         (iw_handler) NULL,                                  /* SIOCSIWNWID   */
3168         (iw_handler) NULL,                                  /* SIOCGIWNWID   */
3169         (iw_handler) rt_ioctl_siwfreq,              /* SIOCSIWFREQ   */
3170         (iw_handler) rt_ioctl_giwfreq,              /* SIOCGIWFREQ   */
3171         (iw_handler) rt_ioctl_siwmode,              /* SIOCSIWMODE   */
3172         (iw_handler) rt_ioctl_giwmode,              /* SIOCGIWMODE   */
3173         (iw_handler) NULL,                              /* SIOCSIWSENS   */
3174         (iw_handler) NULL,                              /* SIOCGIWSENS   */
3175         (iw_handler) NULL /* not used */,               /* SIOCSIWRANGE  */
3176         (iw_handler) rt_ioctl_giwrange,             /* SIOCGIWRANGE  */
3177         (iw_handler) NULL /* not used */,               /* SIOCSIWPRIV   */
3178         (iw_handler) NULL /* kernel code */,    /* SIOCGIWPRIV   */
3179         (iw_handler) NULL /* not used */,               /* SIOCSIWSTATS  */
3180         (iw_handler) rt28xx_get_wireless_stats /* kernel code */,    /* SIOCGIWSTATS  */
3181         (iw_handler) NULL,                              /* SIOCSIWSPY    */
3182         (iw_handler) NULL,                              /* SIOCGIWSPY    */
3183         (iw_handler) NULL,                                      /* SIOCSIWTHRSPY */
3184         (iw_handler) NULL,                                      /* SIOCGIWTHRSPY */
3185         (iw_handler) rt_ioctl_siwap,            /* SIOCSIWAP     */
3186         (iw_handler) rt_ioctl_giwap,                /* SIOCGIWAP     */
3187 #ifdef SIOCSIWMLME
3188         (iw_handler) rt_ioctl_siwmlme,          /* SIOCSIWMLME   */
3189 #else
3190         (iw_handler) NULL,                                      /* SIOCSIWMLME */
3191 #endif // SIOCSIWMLME //
3192         (iw_handler) rt_ioctl_iwaplist,             /* SIOCGIWAPLIST */
3193 #ifdef SIOCGIWSCAN
3194         (iw_handler) rt_ioctl_siwscan,              /* SIOCSIWSCAN   */
3195         (iw_handler) rt_ioctl_giwscan,              /* SIOCGIWSCAN   */
3196 #else
3197         (iw_handler) NULL,                                      /* SIOCSIWSCAN   */
3198         (iw_handler) NULL,                                      /* SIOCGIWSCAN   */
3199 #endif /* SIOCGIWSCAN */
3200         (iw_handler) rt_ioctl_siwessid,             /* SIOCSIWESSID  */
3201         (iw_handler) rt_ioctl_giwessid,             /* SIOCGIWESSID  */
3202         (iw_handler) rt_ioctl_siwnickn,             /* SIOCSIWNICKN  */
3203         (iw_handler) rt_ioctl_giwnickn,             /* SIOCGIWNICKN  */
3204         (iw_handler) NULL,                                      /* -- hole --    */
3205         (iw_handler) NULL,                                      /* -- hole --    */
3206         (iw_handler) rt_ioctl_siwrate,          /* SIOCSIWRATE   */
3207         (iw_handler) rt_ioctl_giwrate,          /* SIOCGIWRATE   */
3208         (iw_handler) rt_ioctl_siwrts,               /* SIOCSIWRTS    */
3209         (iw_handler) rt_ioctl_giwrts,               /* SIOCGIWRTS    */
3210         (iw_handler) rt_ioctl_siwfrag,              /* SIOCSIWFRAG   */
3211         (iw_handler) rt_ioctl_giwfrag,              /* SIOCGIWFRAG   */
3212         (iw_handler) NULL,                              /* SIOCSIWTXPOW  */
3213         (iw_handler) NULL,                              /* SIOCGIWTXPOW  */
3214         (iw_handler) NULL,                              /* SIOCSIWRETRY  */
3215         (iw_handler) NULL,                              /* SIOCGIWRETRY  */
3216         (iw_handler) rt_ioctl_siwencode,                /* SIOCSIWENCODE */
3217         (iw_handler) rt_ioctl_giwencode,                /* SIOCGIWENCODE */
3218         (iw_handler) NULL,                              /* SIOCSIWPOWER  */
3219         (iw_handler) NULL,                              /* SIOCGIWPOWER  */
3220         (iw_handler) NULL,                                              /* -- hole -- */
3221         (iw_handler) NULL,                                              /* -- hole -- */
3222 #if WIRELESS_EXT > 17
3223     (iw_handler) rt_ioctl_siwgenie,         /* SIOCSIWGENIE  */
3224         (iw_handler) rt_ioctl_giwgenie,         /* SIOCGIWGENIE  */
3225         (iw_handler) rt_ioctl_siwauth,              /* SIOCSIWAUTH   */
3226         (iw_handler) rt_ioctl_giwauth,              /* SIOCGIWAUTH   */
3227         (iw_handler) rt_ioctl_siwencodeext,         /* SIOCSIWENCODEEXT */
3228         (iw_handler) rt_ioctl_giwencodeext,             /* SIOCGIWENCODEEXT */
3229         (iw_handler) rt_ioctl_siwpmksa,         /* SIOCSIWPMKSA  */
3230 #endif
3231 };
3232
3233 static const iw_handler rt_priv_handlers[] = {
3234         (iw_handler) NULL, /* + 0x00 */
3235         (iw_handler) NULL, /* + 0x01 */
3236         (iw_handler) rt_ioctl_setparam, /* + 0x02 */
3237 #ifdef DBG
3238         (iw_handler) rt_private_ioctl_bbp, /* + 0x03 */
3239 #else
3240         (iw_handler) NULL, /* + 0x03 */
3241 #endif
3242         (iw_handler) NULL, /* + 0x04 */
3243         (iw_handler) NULL, /* + 0x05 */
3244         (iw_handler) NULL, /* + 0x06 */
3245         (iw_handler) NULL, /* + 0x07 */
3246         (iw_handler) NULL, /* + 0x08 */
3247         (iw_handler) rt_private_get_statistics, /* + 0x09 */
3248         (iw_handler) NULL, /* + 0x0A */
3249         (iw_handler) NULL, /* + 0x0B */
3250         (iw_handler) NULL, /* + 0x0C */
3251         (iw_handler) NULL, /* + 0x0D */
3252         (iw_handler) NULL, /* + 0x0E */
3253         (iw_handler) NULL, /* + 0x0F */
3254         (iw_handler) NULL, /* + 0x10 */
3255         (iw_handler) rt_private_show, /* + 0x11 */
3256     (iw_handler) NULL, /* + 0x12 */
3257         (iw_handler) NULL, /* + 0x13 */
3258         (iw_handler) NULL, /* + 0x15 */
3259         (iw_handler) NULL, /* + 0x17 */
3260         (iw_handler) NULL, /* + 0x18 */
3261 };
3262
3263 const struct iw_handler_def rt28xx_iw_handler_def =
3264 {
3265 #define N(a)    (sizeof (a) / sizeof (a[0]))
3266         .standard       = (iw_handler *) rt_handler,
3267         .num_standard   = sizeof(rt_handler) / sizeof(iw_handler),
3268         .private        = (iw_handler *) rt_priv_handlers,
3269         .num_private            = N(rt_priv_handlers),
3270         .private_args   = (struct iw_priv_args *) privtab,
3271         .num_private_args       = N(privtab),
3272 #if IW_HANDLER_VERSION >= 7
3273     .get_wireless_stats = rt28xx_get_wireless_stats,
3274 #endif
3275 };
3276
3277 INT RTMPSetInformation(
3278     IN  PRTMP_ADAPTER pAdapter,
3279     IN  OUT struct ifreq    *rq,
3280     IN  INT                 cmd)
3281 {
3282     struct iwreq                        *wrq = (struct iwreq *) rq;
3283     NDIS_802_11_SSID                    Ssid;
3284     NDIS_802_11_MAC_ADDRESS             Bssid;
3285     RT_802_11_PHY_MODE                  PhyMode;
3286     RT_802_11_STA_CONFIG                StaConfig;
3287     NDIS_802_11_RATES                   aryRates;
3288     RT_802_11_PREAMBLE                  Preamble;
3289     NDIS_802_11_WEP_STATUS              WepStatus;
3290     NDIS_802_11_AUTHENTICATION_MODE     AuthMode = Ndis802_11AuthModeMax;
3291     NDIS_802_11_NETWORK_INFRASTRUCTURE  BssType;
3292     NDIS_802_11_RTS_THRESHOLD           RtsThresh;
3293     NDIS_802_11_FRAGMENTATION_THRESHOLD FragThresh;
3294     NDIS_802_11_POWER_MODE              PowerMode;
3295     PNDIS_802_11_KEY                    pKey = NULL;
3296     PNDIS_802_11_WEP                            pWepKey =NULL;
3297     PNDIS_802_11_REMOVE_KEY             pRemoveKey = NULL;
3298     NDIS_802_11_CONFIGURATION           Config, *pConfig = NULL;
3299     NDIS_802_11_NETWORK_TYPE            NetType;
3300     ULONG                               Now;
3301     UINT                                KeyIdx = 0;
3302     INT                                 Status = NDIS_STATUS_SUCCESS, MaxPhyMode = PHY_11G;
3303     ULONG                               PowerTemp;
3304     BOOLEAN                             RadioState;
3305     BOOLEAN                             StateMachineTouched = FALSE;
3306         OID_SET_HT_PHYMODE                                      HT_PhyMode;     //11n ,kathy
3307     PNDIS_802_11_PMKID                  pPmkId = NULL;
3308     BOOLEAN                                             IEEE8021xState = FALSE;
3309     BOOLEAN                                             IEEE8021x_required_keys = FALSE;
3310     UCHAR                               wpa_supplicant_enable = 0;
3311
3312         MaxPhyMode = PHY_11N_5G;
3313
3314         DBGPRINT(RT_DEBUG_TRACE, ("-->RTMPSetInformation(),     0x%08x\n", cmd&0x7FFF));
3315     switch(cmd & 0x7FFF) {
3316         case RT_OID_802_11_COUNTRY_REGION:
3317             if (wrq->u.data.length < sizeof(UCHAR))
3318                 Status = -EINVAL;
3319                         // Only avaliable when EEPROM not programming
3320             else if (!(pAdapter->CommonCfg.CountryRegion & 0x80) && !(pAdapter->CommonCfg.CountryRegionForABand & 0x80))
3321             {
3322                 ULONG   Country;
3323                 UCHAR   TmpPhy;
3324
3325                                 Status = copy_from_user(&Country, wrq->u.data.pointer, wrq->u.data.length);
3326                                 pAdapter->CommonCfg.CountryRegion = (UCHAR)(Country & 0x000000FF);
3327                                 pAdapter->CommonCfg.CountryRegionForABand = (UCHAR)((Country >> 8) & 0x000000FF);
3328                 TmpPhy = pAdapter->CommonCfg.PhyMode;
3329                                 pAdapter->CommonCfg.PhyMode = 0xff;
3330                                 // Build all corresponding channel information
3331                                 RTMPSetPhyMode(pAdapter, TmpPhy);
3332                                 SetCommonHT(pAdapter);
3333                                 DBGPRINT(RT_DEBUG_TRACE, ("Set::RT_OID_802_11_COUNTRY_REGION (A:%d  B/G:%d)\n", pAdapter->CommonCfg.CountryRegionForABand,
3334                                     pAdapter->CommonCfg.CountryRegion));
3335             }
3336             break;
3337         case OID_802_11_BSSID_LIST_SCAN:
3338             Now = jiffies;
3339                         DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_BSSID_LIST_SCAN, TxCnt = %d \n", pAdapter->RalinkCounters.LastOneSecTotalTxCount));
3340
3341             if (MONITOR_ON(pAdapter))
3342             {
3343                 DBGPRINT(RT_DEBUG_TRACE, ("!!! Driver is in Monitor Mode now !!!\n"));
3344                 break;
3345             }
3346
3347                         //Benson add 20080527, when radio off, sta don't need to scan
3348                         if (RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_RADIO_OFF))
3349                                 break;
3350
3351                         if (RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_BSS_SCAN_IN_PROGRESS))
3352                         {
3353                 DBGPRINT(RT_DEBUG_TRACE, ("!!! Driver is scanning now !!!\n"));
3354                                 pAdapter->StaCfg.bScanReqIsFromWebUI = TRUE;
3355                                 Status = NDIS_STATUS_SUCCESS;
3356                 break;
3357             }
3358
3359                         if (pAdapter->RalinkCounters.LastOneSecTotalTxCount > 100)
3360             {
3361                 DBGPRINT(RT_DEBUG_TRACE, ("!!! Link UP, ignore this set::OID_802_11_BSSID_LIST_SCAN\n"));
3362                                 Status = NDIS_STATUS_SUCCESS;
3363                                 pAdapter->StaCfg.ScanCnt = 99;          // Prevent auto scan triggered by this OID
3364                                 break;
3365             }
3366
3367             if ((OPSTATUS_TEST_FLAG(pAdapter, fOP_STATUS_MEDIA_STATE_CONNECTED)) &&
3368                                 ((pAdapter->StaCfg.AuthMode == Ndis802_11AuthModeWPA) ||
3369                                 (pAdapter->StaCfg.AuthMode == Ndis802_11AuthModeWPAPSK) ||
3370                                 (pAdapter->StaCfg.AuthMode == Ndis802_11AuthModeWPA2) ||
3371                                 (pAdapter->StaCfg.AuthMode == Ndis802_11AuthModeWPA2PSK)) &&
3372                 (pAdapter->StaCfg.PortSecured == WPA_802_1X_PORT_NOT_SECURED))
3373             {
3374                 DBGPRINT(RT_DEBUG_TRACE, ("!!! Link UP, Port Not Secured! ignore this set::OID_802_11_BSSID_LIST_SCAN\n"));
3375                                 Status = NDIS_STATUS_SUCCESS;
3376                                 pAdapter->StaCfg.ScanCnt = 99;          // Prevent auto scan triggered by this OID
3377                                 break;
3378             }
3379
3380
3381             if (pAdapter->Mlme.CntlMachine.CurrState != CNTL_IDLE)
3382             {
3383                 RT28XX_MLME_RESET_STATE_MACHINE(pAdapter);
3384                 DBGPRINT(RT_DEBUG_TRACE, ("!!! MLME busy, reset MLME state machine !!!\n"));
3385             }
3386
3387             // tell CNTL state machine to call NdisMSetInformationComplete() after completing
3388             // this request, because this request is initiated by NDIS.
3389             pAdapter->MlmeAux.CurrReqIsFromNdis = FALSE;
3390             // Reset allowed scan retries
3391             pAdapter->StaCfg.ScanCnt = 0;
3392             pAdapter->StaCfg.LastScanTime = Now;
3393
3394                         pAdapter->StaCfg.bScanReqIsFromWebUI = TRUE;
3395             RTMP_SET_FLAG(pAdapter, fRTMP_ADAPTER_BSS_SCAN_IN_PROGRESS);
3396             MlmeEnqueue(pAdapter,
3397                         MLME_CNTL_STATE_MACHINE,
3398                         OID_802_11_BSSID_LIST_SCAN,
3399                         0,
3400                         NULL);
3401
3402             Status = NDIS_STATUS_SUCCESS;
3403             StateMachineTouched = TRUE;
3404             break;
3405         case OID_802_11_SSID:
3406             if (wrq->u.data.length != sizeof(NDIS_802_11_SSID))
3407                 Status = -EINVAL;
3408             else
3409             {
3410                 PCHAR pSsidString = NULL;
3411                 Status = copy_from_user(&Ssid, wrq->u.data.pointer, wrq->u.data.length);
3412
3413                                 DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_SSID (Len=%d,Ssid=%s)\n", Ssid.SsidLength, Ssid.Ssid));
3414                 if (Ssid.SsidLength > MAX_LEN_OF_SSID)
3415                     Status = -EINVAL;
3416                 else
3417                 {
3418                         if (Ssid.SsidLength == 0)
3419                         {
3420                                 Set_SSID_Proc(pAdapter, "");
3421                         }
3422                                         else
3423                         {
3424                                 pSsidString = (CHAR *) kmalloc(MAX_LEN_OF_SSID+1, MEM_ALLOC_FLAG);
3425                                                 if (pSsidString)
3426                                                 {
3427                                                         NdisZeroMemory(pSsidString, MAX_LEN_OF_SSID+1);
3428                                                         NdisMoveMemory(pSsidString, Ssid.Ssid, Ssid.SsidLength);
3429                                         Set_SSID_Proc(pAdapter, pSsidString);
3430                                                         kfree(pSsidString);
3431                                                 }
3432                                                 else
3433                                                         Status = -ENOMEM;
3434                         }
3435                 }
3436             }
3437             break;
3438         case OID_802_11_BSSID:
3439             if (wrq->u.data.length != sizeof(NDIS_802_11_MAC_ADDRESS))
3440                 Status  = -EINVAL;
3441             else
3442             {
3443                 Status = copy_from_user(&Bssid, wrq->u.data.pointer, wrq->u.data.length);
3444
3445                 // tell CNTL state machine to call NdisMSetInformationComplete() after completing
3446                 // this request, because this request is initiated by NDIS.
3447                 pAdapter->MlmeAux.CurrReqIsFromNdis = FALSE;
3448
3449                                 // Prevent to connect AP again in STAMlmePeriodicExec
3450                                 pAdapter->MlmeAux.AutoReconnectSsidLen= 32;
3451
3452                 // Reset allowed scan retries
3453                                 pAdapter->StaCfg.ScanCnt = 0;
3454
3455                 if (pAdapter->Mlme.CntlMachine.CurrState != CNTL_IDLE)
3456                 {
3457                     RT28XX_MLME_RESET_STATE_MACHINE(pAdapter);
3458                     DBGPRINT(RT_DEBUG_TRACE, ("!!! MLME busy, reset MLME state machine !!!\n"));
3459                 }
3460                 MlmeEnqueue(pAdapter,
3461                             MLME_CNTL_STATE_MACHINE,
3462                             OID_802_11_BSSID,
3463                             sizeof(NDIS_802_11_MAC_ADDRESS),
3464                             (VOID *)&Bssid);
3465                 Status = NDIS_STATUS_SUCCESS;
3466                 StateMachineTouched = TRUE;
3467
3468                 DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_BSSID %02x:%02x:%02x:%02x:%02x:%02x\n",
3469                                         Bssid[0], Bssid[1], Bssid[2], Bssid[3], Bssid[4], Bssid[5]));
3470             }
3471             break;
3472         case RT_OID_802_11_RADIO:
3473             if (wrq->u.data.length != sizeof(BOOLEAN))
3474                 Status  = -EINVAL;
3475             else
3476             {
3477                 Status = copy_from_user(&RadioState, wrq->u.data.pointer, wrq->u.data.length);
3478                 DBGPRINT(RT_DEBUG_TRACE, ("Set::RT_OID_802_11_RADIO (=%d)\n", RadioState));
3479                 if (pAdapter->StaCfg.bSwRadio != RadioState)
3480                 {
3481                     pAdapter->StaCfg.bSwRadio = RadioState;
3482                     if (pAdapter->StaCfg.bRadio != (pAdapter->StaCfg.bHwRadio && pAdapter->StaCfg.bSwRadio))
3483                     {
3484                         pAdapter->StaCfg.bRadio = (pAdapter->StaCfg.bHwRadio && pAdapter->StaCfg.bSwRadio);
3485                         if (pAdapter->StaCfg.bRadio == TRUE)
3486                         {
3487                             MlmeRadioOn(pAdapter);
3488                             // Update extra information
3489                                                         pAdapter->ExtraInfo = EXTRA_INFO_CLEAR;
3490                         }
3491                         else
3492                         {
3493                             MlmeRadioOff(pAdapter);
3494                             // Update extra information
3495                                                         pAdapter->ExtraInfo = SW_RADIO_OFF;
3496                         }
3497                     }
3498                 }
3499             }
3500             break;
3501         case RT_OID_802_11_PHY_MODE:
3502             if (wrq->u.data.length != sizeof(RT_802_11_PHY_MODE))
3503                 Status  = -EINVAL;
3504             else
3505             {
3506                 Status = copy_from_user(&PhyMode, wrq->u.data.pointer, wrq->u.data.length);
3507                                 if (PhyMode <= MaxPhyMode)
3508                                 {
3509                         RTMPSetPhyMode(pAdapter, PhyMode);
3510                                         SetCommonHT(pAdapter);
3511                                 }
3512                 DBGPRINT(RT_DEBUG_TRACE, ("Set::RT_OID_802_11_PHY_MODE (=%d)\n", PhyMode));
3513             }
3514             break;
3515         case RT_OID_802_11_STA_CONFIG:
3516             if (wrq->u.data.length != sizeof(RT_802_11_STA_CONFIG))
3517                 Status  = -EINVAL;
3518             else
3519             {
3520                 Status = copy_from_user(&StaConfig, wrq->u.data.pointer, wrq->u.data.length);
3521                 pAdapter->CommonCfg.bEnableTxBurst = StaConfig.EnableTxBurst;
3522                 pAdapter->CommonCfg.UseBGProtection = StaConfig.UseBGProtection;
3523                 pAdapter->CommonCfg.bUseShortSlotTime = 1; // 2003-10-30 always SHORT SLOT capable
3524                 if ((pAdapter->CommonCfg.PhyMode != StaConfig.AdhocMode) &&
3525                                         (StaConfig.AdhocMode <= MaxPhyMode))
3526                 {
3527                     // allow dynamic change of "USE OFDM rate or not" in ADHOC mode
3528                     // if setting changed, need to reset current TX rate as well as BEACON frame format
3529 #ifdef RT30xx
3530                     pAdapter->CommonCfg.PhyMode = StaConfig.AdhocMode;
3531 #endif
3532                     if (pAdapter->StaCfg.BssType == BSS_ADHOC)
3533                     {
3534 #ifndef RT30xx
3535                                                 pAdapter->CommonCfg.PhyMode = StaConfig.AdhocMode;
3536 #endif
3537                         RTMPSetPhyMode(pAdapter, PhyMode);
3538                         MlmeUpdateTxRates(pAdapter, FALSE, 0);
3539                         MakeIbssBeacon(pAdapter);           // re-build BEACON frame
3540                         AsicEnableIbssSync(pAdapter);   // copy to on-chip memory
3541                     }
3542                 }
3543                 DBGPRINT(RT_DEBUG_TRACE, ("Set::RT_OID_802_11_SET_STA_CONFIG (Burst=%d, Protection=%ld,ShortSlot=%d\n",
3544                                         pAdapter->CommonCfg.bEnableTxBurst,
3545                                         pAdapter->CommonCfg.UseBGProtection,
3546                                         pAdapter->CommonCfg.bUseShortSlotTime));
3547             }
3548             break;
3549         case OID_802_11_DESIRED_RATES:
3550             if (wrq->u.data.length != sizeof(NDIS_802_11_RATES))
3551                 Status  = -EINVAL;
3552             else
3553             {
3554                 Status = copy_from_user(&aryRates, wrq->u.data.pointer, wrq->u.data.length);
3555                 NdisZeroMemory(pAdapter->CommonCfg.DesireRate, MAX_LEN_OF_SUPPORTED_RATES);
3556                 NdisMoveMemory(pAdapter->CommonCfg.DesireRate, &aryRates, sizeof(NDIS_802_11_RATES));
3557                 DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_DESIRED_RATES (%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x)\n",
3558                     pAdapter->CommonCfg.DesireRate[0],pAdapter->CommonCfg.DesireRate[1],
3559                     pAdapter->CommonCfg.DesireRate[2],pAdapter->CommonCfg.DesireRate[3],
3560                     pAdapter->CommonCfg.DesireRate[4],pAdapter->CommonCfg.DesireRate[5],
3561                     pAdapter->CommonCfg.DesireRate[6],pAdapter->CommonCfg.DesireRate[7] ));
3562                 // Changing DesiredRate may affect the MAX TX rate we used to TX frames out
3563                 MlmeUpdateTxRates(pAdapter, FALSE, 0);
3564             }
3565             break;
3566         case RT_OID_802_11_PREAMBLE:
3567             if (wrq->u.data.length != sizeof(RT_802_11_PREAMBLE))
3568                 Status  = -EINVAL;
3569             else
3570             {
3571                 Status = copy_from_user(&Preamble, wrq->u.data.pointer, wrq->u.data.length);
3572                 if (Preamble == Rt802_11PreambleShort)
3573                 {
3574                     pAdapter->CommonCfg.TxPreamble = Preamble;
3575                     MlmeSetTxPreamble(pAdapter, Rt802_11PreambleShort);
3576                 }
3577                 else if ((Preamble == Rt802_11PreambleLong) || (Preamble == Rt802_11PreambleAuto))
3578                 {
3579                     // if user wants AUTO, initialize to LONG here, then change according to AP's
3580                     // capability upon association.
3581                     pAdapter->CommonCfg.TxPreamble = Preamble;
3582                     MlmeSetTxPreamble(pAdapter, Rt802_11PreambleLong);
3583                 }
3584                 else
3585                 {
3586                     Status = -EINVAL;
3587                     break;
3588                 }
3589                 DBGPRINT(RT_DEBUG_TRACE, ("Set::RT_OID_802_11_PREAMBLE (=%d)\n", Preamble));
3590             }
3591             break;
3592         case OID_802_11_WEP_STATUS:
3593             if (wrq->u.data.length != sizeof(NDIS_802_11_WEP_STATUS))
3594                 Status  = -EINVAL;
3595             else
3596             {
3597                 Status = copy_from_user(&WepStatus, wrq->u.data.pointer, wrq->u.data.length);
3598                 // Since TKIP, AES, WEP are all supported. It should not have any invalid setting
3599                 if (WepStatus <= Ndis802_11Encryption3KeyAbsent)
3600                 {
3601                     if (pAdapter->StaCfg.WepStatus != WepStatus)
3602                     {
3603                         // Config has changed
3604                         pAdapter->bConfigChanged = TRUE;
3605                     }
3606                     pAdapter->StaCfg.WepStatus     = WepStatus;
3607                     pAdapter->StaCfg.OrigWepStatus = WepStatus;
3608                     pAdapter->StaCfg.PairCipher    = WepStatus;
3609                         pAdapter->StaCfg.GroupCipher   = WepStatus;
3610                 }
3611                 else
3612                 {
3613                     Status  = -EINVAL;
3614                     break;
3615                 }
3616                 DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_WEP_STATUS (=%d)\n",WepStatus));
3617             }
3618             break;
3619         case OID_802_11_AUTHENTICATION_MODE:
3620             if (wrq->u.data.length != sizeof(NDIS_802_11_AUTHENTICATION_MODE))
3621                 Status  = -EINVAL;
3622             else
3623             {
3624                 Status = copy_from_user(&AuthMode, wrq->u.data.pointer, wrq->u.data.length);
3625                 if (AuthMode > Ndis802_11AuthModeMax)
3626                 {
3627                     Status  = -EINVAL;
3628                     break;
3629                 }
3630                 else
3631                 {
3632                     if (pAdapter->StaCfg.AuthMode != AuthMode)
3633                     {
3634                         // Config has changed
3635                         pAdapter->bConfigChanged = TRUE;
3636                     }
3637                     pAdapter->StaCfg.AuthMode = AuthMode;
3638                 }
3639                 pAdapter->StaCfg.PortSecured = WPA_802_1X_PORT_NOT_SECURED;
3640                 DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_AUTHENTICATION_MODE (=%d) \n",pAdapter->StaCfg.AuthMode));
3641             }
3642             break;
3643         case OID_802_11_INFRASTRUCTURE_MODE:
3644             if (wrq->u.data.length != sizeof(NDIS_802_11_NETWORK_INFRASTRUCTURE))
3645                 Status  = -EINVAL;
3646             else
3647             {
3648                 Status = copy_from_user(&BssType, wrq->u.data.pointer, wrq->u.data.length);
3649
3650                                 if (BssType == Ndis802_11IBSS)
3651                                         Set_NetworkType_Proc(pAdapter, "Adhoc");
3652                                 else if (BssType == Ndis802_11Infrastructure)
3653                                         Set_NetworkType_Proc(pAdapter, "Infra");
3654                                 else if (BssType == Ndis802_11Monitor)
3655                                         Set_NetworkType_Proc(pAdapter, "Monitor");
3656                                 else
3657                                 {
3658                                         Status  = -EINVAL;
3659                                         DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_INFRASTRUCTURE_MODE (unknown)\n"));
3660                                 }
3661                         }
3662                         break;
3663          case OID_802_11_REMOVE_WEP:
3664             DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_REMOVE_WEP\n"));
3665             if (wrq->u.data.length != sizeof(NDIS_802_11_KEY_INDEX))
3666             {
3667                                 Status = -EINVAL;
3668             }
3669             else
3670             {
3671                                 KeyIdx = *(NDIS_802_11_KEY_INDEX *) wrq->u.data.pointer;
3672
3673                                 if (KeyIdx & 0x80000000)
3674                                 {
3675                                         // Should never set default bit when remove key
3676                                         Status = -EINVAL;
3677                                 }
3678                                 else
3679                                 {
3680                                         KeyIdx = KeyIdx & 0x0fffffff;
3681                                         if (KeyIdx >= 4){
3682                                                 Status = -EINVAL;
3683                                         }
3684                                         else
3685                                         {
3686                                                 pAdapter->SharedKey[BSS0][KeyIdx].KeyLen = 0;
3687                                                 pAdapter->SharedKey[BSS0][KeyIdx].CipherAlg = CIPHER_NONE;
3688                                                 AsicRemoveSharedKeyEntry(pAdapter, 0, (UCHAR)KeyIdx);
3689                                         }
3690                                 }
3691             }
3692             break;
3693         case RT_OID_802_11_RESET_COUNTERS:
3694             NdisZeroMemory(&pAdapter->WlanCounters, sizeof(COUNTER_802_11));
3695             NdisZeroMemory(&pAdapter->Counters8023, sizeof(COUNTER_802_3));
3696             NdisZeroMemory(&pAdapter->RalinkCounters, sizeof(COUNTER_RALINK));
3697             pAdapter->Counters8023.RxNoBuffer   = 0;
3698                         pAdapter->Counters8023.GoodReceives = 0;
3699                         pAdapter->Counters8023.RxNoBuffer   = 0;
3700 #ifdef RT2870
3701                         pAdapter->BulkOutComplete       = 0;
3702                         pAdapter->BulkOutCompleteOther= 0;
3703                         pAdapter->BulkOutCompleteCancel = 0;
3704                         pAdapter->BulkOutReq = 0;
3705                         pAdapter->BulkInReq= 0;
3706                         pAdapter->BulkInComplete = 0;
3707                         pAdapter->BulkInCompleteFail = 0;
3708 #endif // RT2870 //
3709             DBGPRINT(RT_DEBUG_TRACE, ("Set::RT_OID_802_11_RESET_COUNTERS \n"));
3710             break;
3711         case OID_802_11_RTS_THRESHOLD:
3712             if (wrq->u.data.length != sizeof(NDIS_802_11_RTS_THRESHOLD))
3713                 Status  = -EINVAL;
3714             else
3715             {
3716                 Status = copy_from_user(&RtsThresh, wrq->u.data.pointer, wrq->u.data.length);
3717                 if (RtsThresh > MAX_RTS_THRESHOLD)
3718                     Status  = -EINVAL;
3719                 else
3720                     pAdapter->CommonCfg.RtsThreshold = (USHORT)RtsThresh;
3721             }
3722             DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_RTS_THRESHOLD (=%ld)\n",RtsThresh));
3723             break;
3724         case OID_802_11_FRAGMENTATION_THRESHOLD:
3725             if (wrq->u.data.length != sizeof(NDIS_802_11_FRAGMENTATION_THRESHOLD))
3726                 Status  = -EINVAL;
3727             else
3728             {
3729                 Status = copy_from_user(&FragThresh, wrq->u.data.pointer, wrq->u.data.length);
3730                 pAdapter->CommonCfg.bUseZeroToDisableFragment = FALSE;
3731                 if (FragThresh > MAX_FRAG_THRESHOLD || FragThresh < MIN_FRAG_THRESHOLD)
3732                 {
3733                     if (FragThresh == 0)
3734                     {
3735                         pAdapter->CommonCfg.FragmentThreshold = MAX_FRAG_THRESHOLD;
3736                         pAdapter->CommonCfg.bUseZeroToDisableFragment = TRUE;
3737                     }
3738                     else
3739                         Status  = -EINVAL;
3740                 }
3741                 else
3742                     pAdapter->CommonCfg.FragmentThreshold = (USHORT)FragThresh;
3743             }
3744             DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_FRAGMENTATION_THRESHOLD (=%ld) \n",FragThresh));
3745             break;
3746         case OID_802_11_POWER_MODE:
3747             if (wrq->u.data.length != sizeof(NDIS_802_11_POWER_MODE))
3748                 Status = -EINVAL;
3749             else
3750             {
3751                 Status = copy_from_user(&PowerMode, wrq->u.data.pointer, wrq->u.data.length);
3752                 if (PowerMode == Ndis802_11PowerModeCAM)
3753                         Set_PSMode_Proc(pAdapter, "CAM");
3754                 else if (PowerMode == Ndis802_11PowerModeMAX_PSP)
3755                         Set_PSMode_Proc(pAdapter, "Max_PSP");
3756                 else if (PowerMode == Ndis802_11PowerModeFast_PSP)
3757                                         Set_PSMode_Proc(pAdapter, "Fast_PSP");
3758                 else if (PowerMode == Ndis802_11PowerModeLegacy_PSP)
3759                                         Set_PSMode_Proc(pAdapter, "Legacy_PSP");
3760                 else
3761                     Status = -EINVAL;
3762             }
3763             DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_POWER_MODE (=%d)\n",PowerMode));
3764             break;
3765          case RT_OID_802_11_TX_POWER_LEVEL_1:
3766                         if (wrq->u.data.length  < sizeof(ULONG))
3767                                 Status = -EINVAL;
3768                         else
3769                         {
3770                                 Status = copy_from_user(&PowerTemp, wrq->u.data.pointer, wrq->u.data.length);
3771                                 if (PowerTemp > 100)
3772                                         PowerTemp = 0xffffffff;  // AUTO
3773                                 pAdapter->CommonCfg.TxPowerDefault = PowerTemp; //keep current setting.
3774                                         pAdapter->CommonCfg.TxPowerPercentage = pAdapter->CommonCfg.TxPowerDefault;
3775                 DBGPRINT(RT_DEBUG_TRACE, ("Set::RT_OID_802_11_TX_POWER_LEVEL_1 (=%ld)\n", pAdapter->CommonCfg.TxPowerPercentage));
3776                         }
3777                 break;
3778                 case OID_802_11_NETWORK_TYPE_IN_USE:
3779                         if (wrq->u.data.length != sizeof(NDIS_802_11_NETWORK_TYPE))
3780                                 Status = -EINVAL;
3781                         else
3782                         {
3783                                 Status = copy_from_user(&NetType, wrq->u.data.pointer, wrq->u.data.length);
3784
3785                                 if (NetType == Ndis802_11DS)
3786                                         RTMPSetPhyMode(pAdapter, PHY_11B);
3787                                 else if (NetType == Ndis802_11OFDM24)
3788                                         RTMPSetPhyMode(pAdapter, PHY_11BG_MIXED);
3789                                 else if (NetType == Ndis802_11OFDM5)
3790                                         RTMPSetPhyMode(pAdapter, PHY_11A);
3791                                 else
3792                                         Status = -EINVAL;
3793
3794                                 if (Status == NDIS_STATUS_SUCCESS)
3795                                         SetCommonHT(pAdapter);
3796
3797                 DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_NETWORK_TYPE_IN_USE (=%d)\n",NetType));
3798                     }
3799                         break;
3800         // For WPA PSK PMK key
3801         case RT_OID_802_11_ADD_WPA:
3802             pKey = kmalloc(wrq->u.data.length, MEM_ALLOC_FLAG);
3803             if(pKey == NULL)
3804             {
3805                 Status = -ENOMEM;
3806                 break;
3807             }
3808
3809             Status = copy_from_user(pKey, wrq->u.data.pointer, wrq->u.data.length);
3810             if (pKey->Length != wrq->u.data.length)
3811             {
3812                 Status  = -EINVAL;
3813                 DBGPRINT(RT_DEBUG_TRACE, ("Set::RT_OID_802_11_ADD_WPA, Failed!!\n"));
3814             }
3815             else
3816             {
3817                 if ((pAdapter->StaCfg.AuthMode != Ndis802_11AuthModeWPAPSK) &&
3818                                     (pAdapter->StaCfg.AuthMode != Ndis802_11AuthModeWPA2PSK) &&
3819                                     (pAdapter->StaCfg.AuthMode != Ndis802_11AuthModeWPANone) )
3820                 {
3821                     Status = -EOPNOTSUPP;
3822                     DBGPRINT(RT_DEBUG_TRACE, ("Set::RT_OID_802_11_ADD_WPA, Failed!! [AuthMode != WPAPSK/WPA2PSK/WPANONE]\n"));
3823                 }
3824                 else if ((pAdapter->StaCfg.AuthMode == Ndis802_11AuthModeWPAPSK) ||
3825                                                  (pAdapter->StaCfg.AuthMode == Ndis802_11AuthModeWPA2PSK) ||
3826                                                  (pAdapter->StaCfg.AuthMode == Ndis802_11AuthModeWPANone) )     // Only for WPA PSK mode
3827                                 {
3828                     NdisMoveMemory(pAdapter->StaCfg.PMK, &pKey->KeyMaterial, pKey->KeyLength);
3829                     // Use RaConfig as PSK agent.
3830                     // Start STA supplicant state machine
3831                     if (pAdapter->StaCfg.AuthMode != Ndis802_11AuthModeWPANone)
3832                         pAdapter->StaCfg.WpaState = SS_START;
3833
3834                     DBGPRINT(RT_DEBUG_TRACE, ("Set::RT_OID_802_11_ADD_WPA (id=0x%x, Len=%d-byte)\n", pKey->KeyIndex, pKey->KeyLength));
3835                 }
3836                 else
3837                 {
3838                     pAdapter->StaCfg.WpaState = SS_NOTUSE;
3839                     DBGPRINT(RT_DEBUG_TRACE, ("Set::RT_OID_802_11_ADD_WPA (id=0x%x, Len=%d-byte)\n", pKey->KeyIndex, pKey->KeyLength));
3840                 }
3841             }
3842             kfree(pKey);
3843             break;
3844         case OID_802_11_REMOVE_KEY:
3845             pRemoveKey = kmalloc(wrq->u.data.length, MEM_ALLOC_FLAG);
3846             if(pRemoveKey == NULL)
3847             {
3848                 Status = -ENOMEM;
3849                 break;
3850             }
3851
3852             Status = copy_from_user(pRemoveKey, wrq->u.data.pointer, wrq->u.data.length);
3853             if (pRemoveKey->Length != wrq->u.data.length)
3854             {
3855                 Status  = -EINVAL;
3856                 DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_REMOVE_KEY, Failed!!\n"));
3857             }
3858             else
3859             {
3860                 if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
3861                 {
3862                     RTMPWPARemoveKeyProc(pAdapter, pRemoveKey);
3863                     DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_REMOVE_KEY, Remove WPA Key!!\n"));
3864                 }
3865                 else
3866                 {
3867                     KeyIdx = pRemoveKey->KeyIndex;
3868
3869                     if (KeyIdx & 0x80000000)
3870                     {
3871                         // Should never set default bit when remove key
3872                         Status  = -EINVAL;
3873                         DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_REMOVE_KEY, Failed!!(Should never set default bit when remove key)\n"));
3874                     }
3875                     else
3876                     {
3877                         KeyIdx = KeyIdx & 0x0fffffff;
3878                         if (KeyIdx > 3)
3879                         {
3880                             Status  = -EINVAL;
3881                             DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_REMOVE_KEY, Failed!!(KeyId[%d] out of range)\n", KeyIdx));
3882                         }
3883                         else
3884                         {
3885                             pAdapter->SharedKey[BSS0][KeyIdx].KeyLen = 0;
3886                             pAdapter->SharedKey[BSS0][KeyIdx].CipherAlg = CIPHER_NONE;
3887                             AsicRemoveSharedKeyEntry(pAdapter, 0, (UCHAR)KeyIdx);
3888                             DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_REMOVE_KEY (id=0x%x, Len=%d-byte)\n", pRemoveKey->KeyIndex, pRemoveKey->Length));
3889                         }
3890                     }
3891                 }
3892             }
3893             kfree(pRemoveKey);
3894             break;
3895         // New for WPA
3896         case OID_802_11_ADD_KEY:
3897             pKey = kmalloc(wrq->u.data.length, MEM_ALLOC_FLAG);
3898             if(pKey == NULL)
3899             {
3900                 Status = -ENOMEM;
3901                 break;
3902             }
3903             Status = copy_from_user(pKey, wrq->u.data.pointer, wrq->u.data.length);
3904             if (pKey->Length != wrq->u.data.length)
3905             {
3906                 Status  = -EINVAL;
3907                 DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_ADD_KEY, Failed!!\n"));
3908             }
3909             else
3910             {
3911                 RTMPAddKey(pAdapter, pKey);
3912                 DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_ADD_KEY (id=0x%x, Len=%d-byte)\n", pKey->KeyIndex, pKey->KeyLength));
3913             }
3914             kfree(pKey);
3915             break;
3916         case OID_802_11_CONFIGURATION:
3917             if (wrq->u.data.length != sizeof(NDIS_802_11_CONFIGURATION))
3918                 Status  = -EINVAL;
3919             else
3920             {
3921                 Status = copy_from_user(&Config, wrq->u.data.pointer, wrq->u.data.length);
3922                 pConfig = &Config;
3923
3924                 if ((pConfig->BeaconPeriod >= 20) && (pConfig->BeaconPeriod <=400))
3925                      pAdapter->CommonCfg.BeaconPeriod = (USHORT) pConfig->BeaconPeriod;
3926
3927                 pAdapter->StaActive.AtimWin = (USHORT) pConfig->ATIMWindow;
3928                 MAP_KHZ_TO_CHANNEL_ID(pConfig->DSConfig, pAdapter->CommonCfg.Channel);
3929                 //
3930                                 // Save the channel on MlmeAux for CntlOidRTBssidProc used.
3931                                 //
3932                                 pAdapter->MlmeAux.Channel = pAdapter->CommonCfg.Channel;
3933
3934                 DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_CONFIGURATION (BeacnPeriod=%ld,AtimW=%ld,Ch=%d)\n",
3935                     pConfig->BeaconPeriod, pConfig->ATIMWindow, pAdapter->CommonCfg.Channel));
3936                 // Config has changed
3937                 pAdapter->bConfigChanged = TRUE;
3938             }
3939             break;
3940                 case RT_OID_802_11_SET_HT_PHYMODE:
3941                         if (wrq->u.data.length  != sizeof(OID_SET_HT_PHYMODE))
3942                                 Status = -EINVAL;
3943                         else
3944                         {
3945                             POID_SET_HT_PHYMODE pHTPhyMode = &HT_PhyMode;
3946
3947                                 Status = copy_from_user(&HT_PhyMode, wrq->u.data.pointer, wrq->u.data.length);
3948                                 DBGPRINT(RT_DEBUG_TRACE, ("Set::pHTPhyMode      (PhyMode = %d,TransmitNo = %d, HtMode = %d,     ExtOffset =     %d , MCS = %d, BW =     %d,     STBC = %d, SHORTGI = %d) \n",
3949                                 pHTPhyMode->PhyMode, pHTPhyMode->TransmitNo,pHTPhyMode->HtMode,pHTPhyMode->ExtOffset,
3950                                 pHTPhyMode->MCS, pHTPhyMode->BW, pHTPhyMode->STBC,      pHTPhyMode->SHORTGI));
3951                                 if (pAdapter->CommonCfg.PhyMode >= PHY_11ABGN_MIXED)
3952                                         RTMPSetHT(pAdapter,     pHTPhyMode);
3953                         }
3954                         DBGPRINT(RT_DEBUG_TRACE, ("Set::RT_OID_802_11_SET_HT_PHYMODE(MCS=%d,BW=%d,SGI=%d,STBC=%d)\n",
3955                                 pAdapter->StaCfg.HTPhyMode.field.MCS, pAdapter->StaCfg.HTPhyMode.field.BW, pAdapter->StaCfg.HTPhyMode.field.ShortGI,
3956                                 pAdapter->StaCfg.HTPhyMode.field.STBC));
3957                         break;
3958                 case RT_OID_802_11_SET_APSD_SETTING:
3959                         if (wrq->u.data.length != sizeof(ULONG))
3960                                 Status = -EINVAL;
3961                         else
3962                         {
3963                                 ULONG apsd ;
3964                                 Status = copy_from_user(&apsd, wrq->u.data.pointer,     wrq->u.data.length);
3965
3966                                 /*-------------------------------------------------------------------
3967                                 |B31~B7 |       B6~B5    |       B4      |       B3      |      B2       |      B1       |         B0           |
3968                                 ---------------------------------------------------------------------
3969                                 | Rsvd  | Max SP Len | AC_VO | AC_VI | AC_BK | AC_BE | APSD     Capable |
3970                                 ---------------------------------------------------------------------*/
3971                                 pAdapter->CommonCfg.bAPSDCapable = (apsd & 0x00000001) ? TRUE : FALSE;
3972                                 pAdapter->CommonCfg.bAPSDAC_BE = ((apsd & 0x00000002) >> 1)     ? TRUE : FALSE;
3973                                 pAdapter->CommonCfg.bAPSDAC_BK = ((apsd & 0x00000004) >> 2)     ? TRUE : FALSE;
3974                                 pAdapter->CommonCfg.bAPSDAC_VI = ((apsd & 0x00000008) >> 3)     ? TRUE : FALSE;
3975                                 pAdapter->CommonCfg.bAPSDAC_VO = ((apsd & 0x00000010) >> 4)     ? TRUE : FALSE;
3976                                 pAdapter->CommonCfg.MaxSPLength = (UCHAR)((apsd & 0x00000060) >> 5);
3977
3978                                 DBGPRINT(RT_DEBUG_TRACE, ("Set::RT_OID_802_11_SET_APSD_SETTING (apsd=0x%lx, APSDCap=%d, [BE,BK,VI,VO]=[%d/%d/%d/%d],    MaxSPLen=%d)\n", apsd, pAdapter->CommonCfg.bAPSDCapable,
3979                                         pAdapter->CommonCfg.bAPSDAC_BE, pAdapter->CommonCfg.bAPSDAC_BK, pAdapter->CommonCfg.bAPSDAC_VI, pAdapter->CommonCfg.bAPSDAC_VO, pAdapter->CommonCfg.MaxSPLength));
3980                         }
3981                         break;
3982
3983                 case RT_OID_802_11_SET_APSD_PSM:
3984                         if (wrq->u.data.length  != sizeof(ULONG))
3985                                 Status = -EINVAL;
3986                         else
3987                         {
3988                                 // Driver needs to notify AP when PSM changes
3989                                 Status = copy_from_user(&pAdapter->CommonCfg.bAPSDForcePowerSave, wrq->u.data.pointer, wrq->u.data.length);
3990                                 if (pAdapter->CommonCfg.bAPSDForcePowerSave     != pAdapter->StaCfg.Psm)
3991                                 {
3992                                         MlmeSetPsmBit(pAdapter, pAdapter->CommonCfg.bAPSDForcePowerSave);
3993                                         RTMPSendNullFrame(pAdapter,     pAdapter->CommonCfg.TxRate,     TRUE);
3994                                 }
3995                                 DBGPRINT(RT_DEBUG_TRACE, ("Set::RT_OID_802_11_SET_APSD_PSM (bAPSDForcePowerSave:%d)\n", pAdapter->CommonCfg.bAPSDForcePowerSave));
3996                         }
3997                         break;
3998
3999                 case RT_OID_802_11_SET_WMM:
4000                         if (wrq->u.data.length  != sizeof(BOOLEAN))
4001                                 Status = -EINVAL;
4002                         else
4003                         {
4004                                 Status = copy_from_user(&pAdapter->CommonCfg.bWmmCapable, wrq->u.data.pointer, wrq->u.data.length);
4005                                 DBGPRINT(RT_DEBUG_TRACE, ("Set::RT_OID_802_11_SET_WMM (=%d)     \n", pAdapter->CommonCfg.bWmmCapable));
4006                         }
4007                         break;
4008
4009                 case OID_802_11_DISASSOCIATE:
4010                         //
4011                         // Set NdisRadioStateOff to     TRUE, instead of called MlmeRadioOff.
4012                         // Later on, NDIS_802_11_BSSID_LIST_EX->NumberOfItems should be 0
4013                         // when query OID_802_11_BSSID_LIST.
4014                         //
4015                         // TRUE:  NumberOfItems will set to     0.
4016                         // FALSE: NumberOfItems no change.
4017                         //
4018                         pAdapter->CommonCfg.NdisRadioStateOff = TRUE;
4019                         // Set to immediately send the media disconnect event
4020                         pAdapter->MlmeAux.CurrReqIsFromNdis     = TRUE;
4021                         DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_DISASSOCIATE \n"));
4022
4023                         if (INFRA_ON(pAdapter))
4024                         {
4025                                 if (pAdapter->Mlme.CntlMachine.CurrState !=     CNTL_IDLE)
4026                                 {
4027                                         RT28XX_MLME_RESET_STATE_MACHINE(pAdapter);
4028                                         DBGPRINT(RT_DEBUG_TRACE, ("!!! MLME     busy, reset     MLME state machine !!!\n"));
4029                                 }
4030
4031                                 MlmeEnqueue(pAdapter,
4032                                         MLME_CNTL_STATE_MACHINE,
4033                                         OID_802_11_DISASSOCIATE,
4034                                         0,
4035                                         NULL);
4036
4037                                 StateMachineTouched     = TRUE;
4038                         }
4039                         break;
4040                 case RT_OID_802_11_SET_IMME_BA_CAP:
4041                                 if (wrq->u.data.length != sizeof(OID_BACAP_STRUC))
4042                                         Status = -EINVAL;
4043                                 else
4044                                 {
4045                                         OID_BACAP_STRUC Orde ;
4046                                         Status = copy_from_user(&Orde, wrq->u.data.pointer, wrq->u.data.length);
4047                                         if (Orde.Policy > BA_NOTUSE)
4048                                         {
4049                                                 Status = NDIS_STATUS_INVALID_DATA;
4050                                         }
4051                                         else if (Orde.Policy == BA_NOTUSE)
4052                                         {
4053                                                 pAdapter->CommonCfg.BACapability.field.Policy = BA_NOTUSE;
4054                                                 pAdapter->CommonCfg.BACapability.field.MpduDensity = Orde.MpduDensity;
4055                                                 pAdapter->CommonCfg.DesiredHtPhy.MpduDensity = Orde.MpduDensity;
4056                                                 pAdapter->CommonCfg.DesiredHtPhy.AmsduEnable = Orde.AmsduEnable;
4057                                                 pAdapter->CommonCfg.DesiredHtPhy.AmsduSize= Orde.AmsduSize;
4058                                                 pAdapter->CommonCfg.DesiredHtPhy.MimoPs= Orde.MMPSmode;
4059                                                 pAdapter->CommonCfg.BACapability.field.MMPSmode = Orde.MMPSmode;
4060                                                 // UPdata to HT IE
4061                                                 pAdapter->CommonCfg.HtCapability.HtCapInfo.MimoPs = Orde.MMPSmode;
4062                                                 pAdapter->CommonCfg.HtCapability.HtCapInfo.AMsduSize = Orde.AmsduSize;
4063                                                 pAdapter->CommonCfg.HtCapability.HtCapParm.MpduDensity = Orde.MpduDensity;
4064                                         }
4065                                         else
4066                                         {
4067                         pAdapter->CommonCfg.BACapability.field.AutoBA = Orde.AutoBA;
4068                                                 pAdapter->CommonCfg.BACapability.field.Policy = IMMED_BA; // we only support immediate BA.
4069                                                 pAdapter->CommonCfg.BACapability.field.MpduDensity = Orde.MpduDensity;
4070                                                 pAdapter->CommonCfg.DesiredHtPhy.MpduDensity = Orde.MpduDensity;
4071                                                 pAdapter->CommonCfg.DesiredHtPhy.AmsduEnable = Orde.AmsduEnable;
4072                                                 pAdapter->CommonCfg.DesiredHtPhy.AmsduSize= Orde.AmsduSize;
4073                                                 pAdapter->CommonCfg.DesiredHtPhy.MimoPs = Orde.MMPSmode;
4074                                                 pAdapter->CommonCfg.BACapability.field.MMPSmode = Orde.MMPSmode;
4075
4076                                                 // UPdata to HT IE
4077                                                 pAdapter->CommonCfg.HtCapability.HtCapInfo.MimoPs = Orde.MMPSmode;
4078                                                 pAdapter->CommonCfg.HtCapability.HtCapInfo.AMsduSize = Orde.AmsduSize;
4079                                                 pAdapter->CommonCfg.HtCapability.HtCapParm.MpduDensity = Orde.MpduDensity;
4080
4081                                                 if (pAdapter->CommonCfg.BACapability.field.RxBAWinLimit > MAX_RX_REORDERBUF)
4082                                                         pAdapter->CommonCfg.BACapability.field.RxBAWinLimit = MAX_RX_REORDERBUF;
4083
4084                                         }
4085
4086                                         pAdapter->CommonCfg.REGBACapability.word = pAdapter->CommonCfg.BACapability.word;
4087                                         DBGPRINT(RT_DEBUG_TRACE, ("Set::(Orde.AutoBA = %d) (Policy=%d)(ReBAWinLimit=%d)(TxBAWinLimit=%d)(AutoMode=%d)\n",Orde.AutoBA, pAdapter->CommonCfg.BACapability.field.Policy,
4088                                                 pAdapter->CommonCfg.BACapability.field.RxBAWinLimit,pAdapter->CommonCfg.BACapability.field.TxBAWinLimit, pAdapter->CommonCfg.BACapability.field.AutoBA));
4089                                         DBGPRINT(RT_DEBUG_TRACE, ("Set::(MimoPs = %d)(AmsduEnable = %d) (AmsduSize=%d)(MpduDensity=%d)\n",pAdapter->CommonCfg.DesiredHtPhy.MimoPs, pAdapter->CommonCfg.DesiredHtPhy.AmsduEnable,
4090                                                 pAdapter->CommonCfg.DesiredHtPhy.AmsduSize, pAdapter->CommonCfg.DesiredHtPhy.MpduDensity));
4091                                 }
4092
4093                                 break;
4094                 case RT_OID_802_11_ADD_IMME_BA:
4095                         DBGPRINT(RT_DEBUG_TRACE, (" Set :: RT_OID_802_11_ADD_IMME_BA \n"));
4096                         if (wrq->u.data.length != sizeof(OID_ADD_BA_ENTRY))
4097                                         Status = -EINVAL;
4098                         else
4099                         {
4100                                 UCHAR                   index;
4101                                 OID_ADD_BA_ENTRY    BA;
4102                                 MAC_TABLE_ENTRY     *pEntry;
4103
4104                                 Status = copy_from_user(&BA, wrq->u.data.pointer, wrq->u.data.length);
4105                                 if (BA.TID > 15)
4106                                 {
4107                                         Status = NDIS_STATUS_INVALID_DATA;
4108                                         break;
4109                                 }
4110                                 else
4111                                 {
4112                                         //BATableInsertEntry
4113                                         //As ad-hoc mode, BA pair is not limited to only BSSID. so add via OID.
4114                                         index = BA.TID;
4115                                         // in ad hoc mode, when adding BA pair, we should insert this entry into MACEntry too
4116                                         pEntry = MacTableLookup(pAdapter, BA.MACAddr);
4117                                         if (!pEntry)
4118                                         {
4119                                                 DBGPRINT(RT_DEBUG_TRACE, ("RT_OID_802_11_ADD_IMME_BA. break on no connection.----:%x:%x\n", BA.MACAddr[4], BA.MACAddr[5]));
4120                                                 break;
4121                                         }
4122                                         if (BA.IsRecipient == FALSE)
4123                                         {
4124                                             if (pEntry->bIAmBadAtheros == TRUE)
4125                                                         pAdapter->CommonCfg.BACapability.field.RxBAWinLimit = 0x10;
4126
4127                                                 BAOriSessionSetUp(pAdapter, pEntry, index, 0, 100, TRUE);
4128                                         }
4129                                         else
4130                                         {
4131                                                 //BATableInsertEntry(pAdapter, pEntry->Aid, BA.MACAddr, 0, 0xffff, BA.TID, BA.nMSDU, BA.IsRecipient);
4132                                         }
4133
4134                                         DBGPRINT(RT_DEBUG_TRACE, ("Set::RT_OID_802_11_ADD_IMME_BA. Rec = %d. Mac = %x:%x:%x:%x:%x:%x . \n",
4135                                                 BA.IsRecipient, BA.MACAddr[0], BA.MACAddr[1], BA.MACAddr[2], BA.MACAddr[2]
4136                                                 , BA.MACAddr[4], BA.MACAddr[5]));
4137                                 }
4138                         }
4139                         break;
4140
4141                 case RT_OID_802_11_TEAR_IMME_BA:
4142                         DBGPRINT(RT_DEBUG_TRACE, ("Set :: RT_OID_802_11_TEAR_IMME_BA \n"));
4143                         if (wrq->u.data.length != sizeof(OID_ADD_BA_ENTRY))
4144                                         Status = -EINVAL;
4145                         else
4146                         {
4147                                 POID_ADD_BA_ENTRY       pBA;
4148                                 MAC_TABLE_ENTRY *pEntry;
4149
4150                                 pBA = kmalloc(wrq->u.data.length, MEM_ALLOC_FLAG);
4151
4152                                 if (pBA == NULL)
4153                                 {
4154                                         DBGPRINT(RT_DEBUG_TRACE, ("Set :: RT_OID_802_11_TEAR_IMME_BA kmalloc() can't allocate enough memory\n"));
4155                                         Status = NDIS_STATUS_FAILURE;
4156                                 }
4157                                 else
4158                                 {
4159                                         Status = copy_from_user(pBA, wrq->u.data.pointer, wrq->u.data.length);
4160                                         DBGPRINT(RT_DEBUG_TRACE, ("Set :: RT_OID_802_11_TEAR_IMME_BA(TID=%d, bAllTid=%d)\n", pBA->TID, pBA->bAllTid));
4161
4162                                         if (!pBA->bAllTid && (pBA->TID > NUM_OF_TID))
4163                                         {
4164                                                 Status = NDIS_STATUS_INVALID_DATA;
4165                                                 break;
4166                                         }
4167
4168                                         if (pBA->IsRecipient == FALSE)
4169                                         {
4170                                                 pEntry = MacTableLookup(pAdapter, pBA->MACAddr);
4171                                                 DBGPRINT(RT_DEBUG_TRACE, (" pBA->IsRecipient == FALSE\n"));
4172                                                 if (pEntry)
4173                                                 {
4174                                                         DBGPRINT(RT_DEBUG_TRACE, (" pBA->pEntry\n"));
4175                                                         BAOriSessionTearDown(pAdapter, pEntry->Aid, pBA->TID, FALSE, TRUE);
4176                                                 }
4177                                                 else
4178                                                         DBGPRINT(RT_DEBUG_TRACE, ("Set :: Not found pEntry \n"));
4179                                         }
4180                                         else
4181                                         {
4182                                                 pEntry = MacTableLookup(pAdapter, pBA->MACAddr);
4183                                                 if (pEntry)
4184                                                 {
4185                                                         BARecSessionTearDown( pAdapter, (UCHAR)pEntry->Aid, pBA->TID, TRUE);
4186                                                 }
4187                                                 else
4188                                                         DBGPRINT(RT_DEBUG_TRACE, ("Set :: Not found pEntry \n"));
4189                                         }
4190                                         kfree(pBA);
4191                                 }
4192             }
4193             break;
4194         // For WPA_SUPPLICANT to set static wep key
4195         case OID_802_11_ADD_WEP:
4196             pWepKey = kmalloc(wrq->u.data.length, MEM_ALLOC_FLAG);
4197
4198             if(pWepKey == NULL)
4199             {
4200                 Status = -ENOMEM;
4201                                 DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_ADD_WEP, Failed!!\n"));
4202                 break;
4203             }
4204             Status = copy_from_user(pWepKey, wrq->u.data.pointer, wrq->u.data.length);
4205             if (Status)
4206             {
4207                 Status  = -EINVAL;
4208                 DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_ADD_WEP, Failed (length mismatch)!!\n"));
4209             }
4210             else
4211             {
4212                         KeyIdx = pWepKey->KeyIndex & 0x0fffffff;
4213                 // KeyIdx must be 0 ~ 3
4214                 if (KeyIdx > 4)
4215                         {
4216                     Status  = -EINVAL;
4217                     DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_ADD_WEP, Failed (KeyIdx must be smaller than 4)!!\n"));
4218                 }
4219                 else
4220                 {
4221                     UCHAR CipherAlg = 0;
4222                     PUCHAR Key;
4223
4224                     // set key material and key length
4225                     NdisZeroMemory(pAdapter->SharedKey[BSS0][KeyIdx].Key, 16);
4226                     pAdapter->SharedKey[BSS0][KeyIdx].KeyLen = (UCHAR) pWepKey->KeyLength;
4227                     NdisMoveMemory(pAdapter->SharedKey[BSS0][KeyIdx].Key, &pWepKey->KeyMaterial, pWepKey->KeyLength);
4228
4229                     switch(pWepKey->KeyLength)
4230                     {
4231                         case 5:
4232                             CipherAlg = CIPHER_WEP64;
4233                             break;
4234                         case 13:
4235                             CipherAlg = CIPHER_WEP128;
4236                             break;
4237                         default:
4238                             DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_ADD_WEP, only support CIPHER_WEP64(len:5) & CIPHER_WEP128(len:13)!!\n"));
4239                             Status = -EINVAL;
4240                             break;
4241                     }
4242                     pAdapter->SharedKey[BSS0][KeyIdx].CipherAlg = CipherAlg;
4243
4244                     // Default key for tx (shared key)
4245                     if (pWepKey->KeyIndex & 0x80000000)
4246                     {
4247                         // set key material and key length
4248                         NdisZeroMemory(pAdapter->StaCfg.DesireSharedKey[KeyIdx].Key, 16);
4249                         pAdapter->StaCfg.DesireSharedKey[KeyIdx].KeyLen = (UCHAR) pWepKey->KeyLength;
4250                         NdisMoveMemory(pAdapter->StaCfg.DesireSharedKey[KeyIdx].Key, &pWepKey->KeyMaterial, pWepKey->KeyLength);
4251                         pAdapter->StaCfg.DesireSharedKeyId = KeyIdx;
4252                         pAdapter->StaCfg.DesireSharedKey[KeyIdx].CipherAlg = CipherAlg;
4253                         pAdapter->StaCfg.DefaultKeyId = (UCHAR) KeyIdx;
4254                     }
4255 #ifndef RT30xx
4256 #ifdef RT2860
4257                                         if ((pAdapter->StaCfg.WpaSupplicantUP != 0) &&
4258 #endif
4259 #ifdef RT2870
4260                                         if ((pAdapter->StaCfg.WpaSupplicantUP != WPA_SUPPLICANT_DISABLE) &&
4261 #endif
4262                                                 (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA))
4263                                         {
4264                                                 Key = pWepKey->KeyMaterial;
4265
4266                                                 // Set Group key material to Asic
4267                                         AsicAddSharedKeyEntry(pAdapter, BSS0, KeyIdx, CipherAlg, Key, NULL, NULL);
4268
4269                                                 // Update WCID attribute table and IVEIV table for this group key table
4270                                                 RTMPAddWcidAttributeEntry(pAdapter, BSS0, KeyIdx, CipherAlg, NULL);
4271
4272                                                 STA_PORT_SECURED(pAdapter);
4273
4274                                         // Indicate Connected for GUI
4275                                         pAdapter->IndicateMediaState = NdisMediaStateConnected;
4276                                         }
4277                     else if (pAdapter->StaCfg.PortSecured == WPA_802_1X_PORT_SECURED)
4278 #endif
4279 #ifdef RT30xx
4280                     if (pAdapter->StaCfg.PortSecured == WPA_802_1X_PORT_SECURED)
4281 #endif
4282                     {
4283                         Key = pAdapter->SharedKey[BSS0][KeyIdx].Key;
4284
4285                         // Set key material and cipherAlg to Asic
4286                                         AsicAddSharedKeyEntry(pAdapter, BSS0, KeyIdx, CipherAlg, Key, NULL, NULL);
4287
4288                         if (pWepKey->KeyIndex & 0x80000000)
4289                         {
4290                             PMAC_TABLE_ENTRY pEntry = &pAdapter->MacTab.Content[BSSID_WCID];
4291                             // Assign group key info
4292                                                 RTMPAddWcidAttributeEntry(pAdapter, BSS0, KeyIdx, CipherAlg, NULL);
4293                                                 // Assign pairwise key info
4294                                                 RTMPAddWcidAttributeEntry(pAdapter, BSS0, KeyIdx, CipherAlg, pEntry);
4295                         }
4296                     }
4297                                         DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_ADD_WEP (id=0x%x, Len=%d-byte), %s\n", pWepKey->KeyIndex, pWepKey->KeyLength, (pAdapter->StaCfg.PortSecured == WPA_802_1X_PORT_SECURED) ? "Port Secured":"Port NOT Secured"));
4298                                 }
4299             }
4300             kfree(pWepKey);
4301             break;
4302             case OID_SET_COUNTERMEASURES:
4303             if (wrq->u.data.length != sizeof(int))
4304                 Status  = -EINVAL;
4305             else
4306             {
4307                 int enabled = 0;
4308                 Status = copy_from_user(&enabled, wrq->u.data.pointer, wrq->u.data.length);
4309                 if (enabled == 1)
4310                     pAdapter->StaCfg.bBlockAssoc = TRUE;
4311                 else
4312                     // WPA MIC error should block association attempt for 60 seconds
4313                     pAdapter->StaCfg.bBlockAssoc = FALSE;
4314                 DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_SET_COUNTERMEASURES bBlockAssoc=%s\n", pAdapter->StaCfg.bBlockAssoc ? "TRUE":"FALSE"));
4315             }
4316                 break;
4317         case RT_OID_WPA_SUPPLICANT_SUPPORT:
4318                         if (wrq->u.data.length != sizeof(UCHAR))
4319                 Status  = -EINVAL;
4320             else
4321             {
4322                 Status = copy_from_user(&wpa_supplicant_enable, wrq->u.data.pointer, wrq->u.data.length);
4323                         pAdapter->StaCfg.WpaSupplicantUP = wpa_supplicant_enable;
4324                         DBGPRINT(RT_DEBUG_TRACE, ("Set::RT_OID_WPA_SUPPLICANT_SUPPORT (=%d)\n", pAdapter->StaCfg.WpaSupplicantUP));
4325                         }
4326             break;
4327         case OID_802_11_DEAUTHENTICATION:
4328             if (wrq->u.data.length != sizeof(MLME_DEAUTH_REQ_STRUCT))
4329                 Status  = -EINVAL;
4330             else
4331             {
4332                 MLME_DEAUTH_REQ_STRUCT      *pInfo;
4333                                 MLME_QUEUE_ELEM *MsgElem = (MLME_QUEUE_ELEM *) kmalloc(sizeof(MLME_QUEUE_ELEM), MEM_ALLOC_FLAG);
4334
4335                 pInfo = (MLME_DEAUTH_REQ_STRUCT *) MsgElem->Msg;
4336                 Status = copy_from_user(pInfo, wrq->u.data.pointer, wrq->u.data.length);
4337                 MlmeDeauthReqAction(pAdapter, MsgElem);
4338                                 kfree(MsgElem);
4339
4340                 if (INFRA_ON(pAdapter))
4341                 {
4342                     LinkDown(pAdapter, FALSE);
4343                     pAdapter->Mlme.AssocMachine.CurrState = ASSOC_IDLE;
4344                 }
4345                 DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_DEAUTHENTICATION (Reason=%d)\n", pInfo->Reason));
4346             }
4347             break;
4348         case OID_802_11_DROP_UNENCRYPTED:
4349             if (wrq->u.data.length != sizeof(int))
4350                 Status  = -EINVAL;
4351             else
4352             {
4353                 int enabled = 0;
4354                 Status = copy_from_user(&enabled, wrq->u.data.pointer, wrq->u.data.length);
4355                 if (enabled == 1)
4356                     pAdapter->StaCfg.PortSecured = WPA_802_1X_PORT_NOT_SECURED;
4357                 else
4358                     pAdapter->StaCfg.PortSecured = WPA_802_1X_PORT_SECURED;
4359                                 NdisAcquireSpinLock(&pAdapter->MacTabLock);
4360                                 pAdapter->MacTab.Content[BSSID_WCID].PortSecured = pAdapter->StaCfg.PortSecured;
4361                                 NdisReleaseSpinLock(&pAdapter->MacTabLock);
4362                 DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_DROP_UNENCRYPTED (=%d)\n", enabled));
4363             }
4364             break;
4365         case OID_802_11_SET_IEEE8021X:
4366             if (wrq->u.data.length != sizeof(BOOLEAN))
4367                 Status  = -EINVAL;
4368             else
4369             {
4370                 Status = copy_from_user(&IEEE8021xState, wrq->u.data.pointer, wrq->u.data.length);
4371                         pAdapter->StaCfg.IEEE8021X = IEEE8021xState;
4372                 DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_SET_IEEE8021X (=%d)\n", IEEE8021xState));
4373             }
4374             break;
4375         case OID_802_11_SET_IEEE8021X_REQUIRE_KEY:
4376                         if (wrq->u.data.length != sizeof(BOOLEAN))
4377                                  Status  = -EINVAL;
4378             else
4379             {
4380                 Status = copy_from_user(&IEEE8021x_required_keys, wrq->u.data.pointer, wrq->u.data.length);
4381                                 pAdapter->StaCfg.IEEE8021x_required_keys = IEEE8021x_required_keys;
4382                                 DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_SET_IEEE8021X_REQUIRE_KEY (%d)\n", IEEE8021x_required_keys));
4383                         }
4384                         break;
4385         case OID_802_11_PMKID:
4386                 pPmkId = kmalloc(wrq->u.data.length, MEM_ALLOC_FLAG);
4387
4388                 if(pPmkId == NULL) {
4389                 Status = -ENOMEM;
4390                 break;
4391             }
4392             Status = copy_from_user(pPmkId, wrq->u.data.pointer, wrq->u.data.length);
4393
4394                 // check the PMKID information
4395                 if (pPmkId->BSSIDInfoCount == 0)
4396                 NdisZeroMemory(pAdapter->StaCfg.SavedPMK, sizeof(BSSID_INFO)*PMKID_NO);
4397                 else
4398                 {
4399                         PBSSID_INFO     pBssIdInfo;
4400                         UINT            BssIdx;
4401                         UINT            CachedIdx;
4402
4403                         for (BssIdx = 0; BssIdx < pPmkId->BSSIDInfoCount; BssIdx++)
4404                         {
4405                                 // point to the indexed BSSID_INFO structure
4406                                 pBssIdInfo = (PBSSID_INFO) ((PUCHAR) pPmkId + 2 * sizeof(UINT) + BssIdx * sizeof(BSSID_INFO));
4407                                 // Find the entry in the saved data base.
4408                                 for (CachedIdx = 0; CachedIdx < pAdapter->StaCfg.SavedPMKNum; CachedIdx++)
4409                                 {
4410                                         // compare the BSSID
4411                                         if (NdisEqualMemory(pBssIdInfo->BSSID, pAdapter->StaCfg.SavedPMK[CachedIdx].BSSID, sizeof(NDIS_802_11_MAC_ADDRESS)))
4412                                                 break;
4413                                 }
4414
4415                                 // Found, replace it
4416                                 if (CachedIdx < PMKID_NO)
4417                                 {
4418                                         DBGPRINT(RT_DEBUG_OFF, ("Update OID_802_11_PMKID, idx = %d\n", CachedIdx));
4419                                         NdisMoveMemory(&pAdapter->StaCfg.SavedPMK[CachedIdx], pBssIdInfo, sizeof(BSSID_INFO));
4420                                         pAdapter->StaCfg.SavedPMKNum++;
4421                                 }
4422                                 // Not found, replace the last one
4423                                 else
4424                                 {
4425                                         // Randomly replace one
4426                                         CachedIdx = (pBssIdInfo->BSSID[5] % PMKID_NO);
4427                                         DBGPRINT(RT_DEBUG_OFF, ("Update OID_802_11_PMKID, idx = %d\n", CachedIdx));
4428                                         NdisMoveMemory(&pAdapter->StaCfg.SavedPMK[CachedIdx], pBssIdInfo, sizeof(BSSID_INFO));
4429                                 }
4430                         }
4431                         }
4432                         if(pPmkId)
4433                                 kfree(pPmkId);
4434                 break;
4435         default:
4436             DBGPRINT(RT_DEBUG_TRACE, ("Set::unknown IOCTL's subcmd = 0x%08x\n", cmd));
4437             Status = -EOPNOTSUPP;
4438             break;
4439     }
4440
4441
4442     return Status;
4443 }
4444
4445 INT RTMPQueryInformation(
4446     IN  PRTMP_ADAPTER pAdapter,
4447     IN  OUT struct ifreq    *rq,
4448     IN  INT                 cmd)
4449 {
4450     struct iwreq                        *wrq = (struct iwreq *) rq;
4451     NDIS_802_11_BSSID_LIST_EX           *pBssidList = NULL;
4452     PNDIS_WLAN_BSSID_EX                 pBss;
4453     NDIS_802_11_SSID                    Ssid;
4454     NDIS_802_11_CONFIGURATION           *pConfiguration = NULL;
4455     RT_802_11_LINK_STATUS               *pLinkStatus = NULL;
4456     RT_802_11_STA_CONFIG                *pStaConfig = NULL;
4457     NDIS_802_11_STATISTICS              *pStatistics = NULL;
4458     NDIS_802_11_RTS_THRESHOLD           RtsThresh;
4459     NDIS_802_11_FRAGMENTATION_THRESHOLD FragThresh;
4460     NDIS_802_11_POWER_MODE              PowerMode;
4461     NDIS_802_11_NETWORK_INFRASTRUCTURE  BssType;
4462     RT_802_11_PREAMBLE                  PreamType;
4463     NDIS_802_11_AUTHENTICATION_MODE     AuthMode;
4464     NDIS_802_11_WEP_STATUS              WepStatus;
4465     NDIS_MEDIA_STATE                    MediaState;
4466     ULONG                               BssBufSize, ulInfo=0, NetworkTypeList[4], apsd = 0;
4467     USHORT                              BssLen = 0;
4468     PUCHAR                              pBuf = NULL, pPtr;
4469     INT                                 Status = NDIS_STATUS_SUCCESS;
4470     UINT                                we_version_compiled;
4471     UCHAR                               i, Padding = 0;
4472     BOOLEAN                             RadioState;
4473         UCHAR   driverVersion[8];
4474     OID_SET_HT_PHYMODE                          *pHTPhyMode = NULL;
4475
4476     switch(cmd)
4477     {
4478         case RT_OID_DEVICE_NAME:
4479             wrq->u.data.length = sizeof(STA_NIC_DEVICE_NAME);
4480             Status = copy_to_user(wrq->u.data.pointer, STA_NIC_DEVICE_NAME, wrq->u.data.length);
4481             break;
4482         case RT_OID_VERSION_INFO:
4483                         DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_VERSION_INFO \n"));
4484                         wrq->u.data.length = 8*sizeof(UCHAR);
4485                         sprintf(&driverVersion[0], "%s", STA_DRIVER_VERSION);
4486                         driverVersion[7] = '\0';
4487                         if (copy_to_user(wrq->u.data.pointer, &driverVersion, wrq->u.data.length))
4488             {
4489                                 Status = -EFAULT;
4490             }
4491             break;
4492         case OID_802_11_BSSID_LIST:
4493             if (RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_BSS_SCAN_IN_PROGRESS))
4494             {
4495                 /*
4496                  * Still scanning, indicate the caller should try again.
4497                  */
4498                 DBGPRINT(RT_DEBUG_TRACE, ("Query::OID_802_11_BSSID_LIST (Still scanning)\n"));
4499                                 return -EAGAIN;
4500             }
4501             DBGPRINT(RT_DEBUG_TRACE, ("Query::OID_802_11_BSSID_LIST (%d BSS returned)\n",pAdapter->ScanTab.BssNr));
4502                         pAdapter->StaCfg.bScanReqIsFromWebUI = FALSE;
4503             // Claculate total buffer size required
4504             BssBufSize = sizeof(ULONG);
4505
4506             for (i = 0; i < pAdapter->ScanTab.BssNr; i++)
4507             {
4508                 // Align pointer to 4 bytes boundary.
4509                 //Padding = 4 - (pAdapter->ScanTab.BssEntry[i].VarIELen & 0x0003);
4510                 //if (Padding == 4)
4511                 //    Padding = 0;
4512                 BssBufSize += (sizeof(NDIS_WLAN_BSSID_EX) - 1 + sizeof(NDIS_802_11_FIXED_IEs) + pAdapter->ScanTab.BssEntry[i].VarIELen + Padding);
4513             }
4514
4515             // For safety issue, we add 256 bytes just in case
4516             BssBufSize += 256;
4517             // Allocate the same size as passed from higher layer
4518             pBuf = kmalloc(BssBufSize, MEM_ALLOC_FLAG);
4519             if(pBuf == NULL)
4520             {
4521                 Status = -ENOMEM;
4522                 break;
4523             }
4524             // Init 802_11_BSSID_LIST_EX structure
4525             NdisZeroMemory(pBuf, BssBufSize);
4526             pBssidList = (PNDIS_802_11_BSSID_LIST_EX) pBuf;
4527             pBssidList->NumberOfItems = pAdapter->ScanTab.BssNr;
4528
4529             // Calculate total buffer length
4530             BssLen = 4; // Consist of NumberOfItems
4531             // Point to start of NDIS_WLAN_BSSID_EX
4532             // pPtr = pBuf + sizeof(ULONG);
4533             pPtr = (PUCHAR) &pBssidList->Bssid[0];
4534             for (i = 0; i < pAdapter->ScanTab.BssNr; i++)
4535             {
4536                 pBss = (PNDIS_WLAN_BSSID_EX) pPtr;
4537                 NdisMoveMemory(&pBss->MacAddress, &pAdapter->ScanTab.BssEntry[i].Bssid, MAC_ADDR_LEN);
4538                 if ((pAdapter->ScanTab.BssEntry[i].Hidden == 1) && (pAdapter->StaCfg.bShowHiddenSSID == FALSE))
4539                 {
4540                     //
4541                                         // We must return this SSID during 4way handshaking, otherwise Aegis will failed to parse WPA infomation
4542                                         // and then failed to send EAPOl farame.
4543                                         //
4544                                         if ((pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA) && (pAdapter->StaCfg.PortSecured != WPA_802_1X_PORT_SECURED))
4545                                         {
4546                                                 pBss->Ssid.SsidLength = pAdapter->ScanTab.BssEntry[i].SsidLen;
4547                                                 NdisMoveMemory(pBss->Ssid.Ssid, pAdapter->ScanTab.BssEntry[i].Ssid, pAdapter->ScanTab.BssEntry[i].SsidLen);
4548                                         }
4549                                         else
4550                         pBss->Ssid.SsidLength = 0;
4551                 }
4552                 else
4553                 {
4554                     pBss->Ssid.SsidLength = pAdapter->ScanTab.BssEntry[i].SsidLen;
4555                     NdisMoveMemory(pBss->Ssid.Ssid, pAdapter->ScanTab.BssEntry[i].Ssid, pAdapter->ScanTab.BssEntry[i].SsidLen);
4556                 }
4557                 pBss->Privacy = pAdapter->ScanTab.BssEntry[i].Privacy;
4558                 pBss->Rssi = pAdapter->ScanTab.BssEntry[i].Rssi - pAdapter->BbpRssiToDbmDelta;
4559                 pBss->NetworkTypeInUse = NetworkTypeInUseSanity(&pAdapter->ScanTab.BssEntry[i]);
4560                 pBss->Configuration.Length = sizeof(NDIS_802_11_CONFIGURATION);
4561                 pBss->Configuration.BeaconPeriod = pAdapter->ScanTab.BssEntry[i].BeaconPeriod;
4562                 pBss->Configuration.ATIMWindow = pAdapter->ScanTab.BssEntry[i].AtimWin;
4563
4564                 MAP_CHANNEL_ID_TO_KHZ(pAdapter->ScanTab.BssEntry[i].Channel, pBss->Configuration.DSConfig);
4565
4566                 if (pAdapter->ScanTab.BssEntry[i].BssType == BSS_INFRA)
4567                     pBss->InfrastructureMode = Ndis802_11Infrastructure;
4568                 else
4569                     pBss->InfrastructureMode = Ndis802_11IBSS;
4570
4571                 NdisMoveMemory(pBss->SupportedRates, pAdapter->ScanTab.BssEntry[i].SupRate, pAdapter->ScanTab.BssEntry[i].SupRateLen);
4572                 NdisMoveMemory(pBss->SupportedRates + pAdapter->ScanTab.BssEntry[i].SupRateLen,
4573                                pAdapter->ScanTab.BssEntry[i].ExtRate,
4574                                pAdapter->ScanTab.BssEntry[i].ExtRateLen);
4575
4576                 if (pAdapter->ScanTab.BssEntry[i].VarIELen == 0)
4577                 {
4578                     pBss->IELength = sizeof(NDIS_802_11_FIXED_IEs);
4579                     NdisMoveMemory(pBss->IEs, &pAdapter->ScanTab.BssEntry[i].FixIEs, sizeof(NDIS_802_11_FIXED_IEs));
4580                     pPtr = pPtr + sizeof(NDIS_WLAN_BSSID_EX) - 1 + sizeof(NDIS_802_11_FIXED_IEs);
4581                 }
4582                 else
4583                 {
4584                     pBss->IELength = (ULONG)(sizeof(NDIS_802_11_FIXED_IEs) + pAdapter->ScanTab.BssEntry[i].VarIELen);
4585                     pPtr = pPtr + sizeof(NDIS_WLAN_BSSID_EX) - 1 + sizeof(NDIS_802_11_FIXED_IEs);
4586                     NdisMoveMemory(pBss->IEs, &pAdapter->ScanTab.BssEntry[i].FixIEs, sizeof(NDIS_802_11_FIXED_IEs));
4587                     NdisMoveMemory(pBss->IEs + sizeof(NDIS_802_11_FIXED_IEs), pAdapter->ScanTab.BssEntry[i].VarIEs, pAdapter->ScanTab.BssEntry[i].VarIELen);
4588                     pPtr += pAdapter->ScanTab.BssEntry[i].VarIELen;
4589                 }
4590                 pBss->Length = (ULONG)(sizeof(NDIS_WLAN_BSSID_EX) - 1 + sizeof(NDIS_802_11_FIXED_IEs) + pAdapter->ScanTab.BssEntry[i].VarIELen + Padding);
4591
4592 #if WIRELESS_EXT < 17
4593                 if ((BssLen + pBss->Length) < wrq->u.data.length)
4594                 BssLen += pBss->Length;
4595                 else
4596                 {
4597                     pBssidList->NumberOfItems = i;
4598                     break;
4599                 }
4600 #else
4601                 BssLen += pBss->Length;
4602 #endif
4603             }
4604
4605 #if WIRELESS_EXT < 17
4606             wrq->u.data.length = BssLen;
4607 #else
4608             if (BssLen > wrq->u.data.length)
4609             {
4610                 kfree(pBssidList);
4611                 return -E2BIG;
4612             }
4613             else
4614                 wrq->u.data.length = BssLen;
4615 #endif
4616             Status = copy_to_user(wrq->u.data.pointer, pBssidList, BssLen);
4617             kfree(pBssidList);
4618             break;
4619         case OID_802_3_CURRENT_ADDRESS:
4620             wrq->u.data.length = MAC_ADDR_LEN;
4621             Status = copy_to_user(wrq->u.data.pointer, &pAdapter->CurrentAddress, wrq->u.data.length);
4622             break;
4623         case OID_GEN_MEDIA_CONNECT_STATUS:
4624             if (pAdapter->IndicateMediaState == NdisMediaStateConnected)
4625                 MediaState = NdisMediaStateConnected;
4626             else
4627                 MediaState = NdisMediaStateDisconnected;
4628
4629             wrq->u.data.length = sizeof(NDIS_MEDIA_STATE);
4630             Status = copy_to_user(wrq->u.data.pointer, &MediaState, wrq->u.data.length);
4631             break;
4632         case OID_802_11_BSSID:
4633             if (INFRA_ON(pAdapter) || ADHOC_ON(pAdapter))
4634             {
4635                 Status = copy_to_user(wrq->u.data.pointer, &pAdapter->CommonCfg.Bssid, sizeof(NDIS_802_11_MAC_ADDRESS));
4636
4637             }
4638             else
4639             {
4640                 DBGPRINT(RT_DEBUG_TRACE, ("Query::OID_802_11_BSSID(=EMPTY)\n"));
4641                 Status = -ENOTCONN;
4642             }
4643             break;
4644         case OID_802_11_SSID:
4645                         NdisZeroMemory(&Ssid, sizeof(NDIS_802_11_SSID));
4646                         NdisZeroMemory(Ssid.Ssid, MAX_LEN_OF_SSID);
4647             Ssid.SsidLength = pAdapter->CommonCfg.SsidLen;
4648                         memcpy(Ssid.Ssid, pAdapter->CommonCfg.Ssid,     Ssid.SsidLength);
4649             wrq->u.data.length = sizeof(NDIS_802_11_SSID);
4650             Status = copy_to_user(wrq->u.data.pointer, &Ssid, wrq->u.data.length);
4651             DBGPRINT(RT_DEBUG_TRACE, ("Query::OID_802_11_SSID (Len=%d, ssid=%s)\n", Ssid.SsidLength,Ssid.Ssid));
4652             break;
4653         case RT_OID_802_11_QUERY_LINK_STATUS:
4654             pLinkStatus = (RT_802_11_LINK_STATUS *) kmalloc(sizeof(RT_802_11_LINK_STATUS), MEM_ALLOC_FLAG);
4655             if (pLinkStatus)
4656             {
4657                 pLinkStatus->CurrTxRate = RateIdTo500Kbps[pAdapter->CommonCfg.TxRate];   // unit : 500 kbps
4658                 pLinkStatus->ChannelQuality = pAdapter->Mlme.ChannelQuality;
4659                 pLinkStatus->RxByteCount = pAdapter->RalinkCounters.ReceivedByteCount;
4660                 pLinkStatus->TxByteCount = pAdapter->RalinkCounters.TransmittedByteCount;
4661                         pLinkStatus->CentralChannel = pAdapter->CommonCfg.CentralChannel;
4662                 wrq->u.data.length = sizeof(RT_802_11_LINK_STATUS);
4663                 Status = copy_to_user(wrq->u.data.pointer, pLinkStatus, wrq->u.data.length);
4664                 kfree(pLinkStatus);
4665                 DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_802_11_QUERY_LINK_STATUS\n"));
4666             }
4667             else
4668             {
4669                 DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_802_11_QUERY_LINK_STATUS(kmalloc failed)\n"));
4670                 Status = -EFAULT;
4671             }
4672             break;
4673         case OID_802_11_CONFIGURATION:
4674             pConfiguration = (NDIS_802_11_CONFIGURATION *) kmalloc(sizeof(NDIS_802_11_CONFIGURATION), MEM_ALLOC_FLAG);
4675             if (pConfiguration)
4676             {
4677                 pConfiguration->Length = sizeof(NDIS_802_11_CONFIGURATION);
4678                 pConfiguration->BeaconPeriod = pAdapter->CommonCfg.BeaconPeriod;
4679                 pConfiguration->ATIMWindow = pAdapter->StaActive.AtimWin;
4680                 MAP_CHANNEL_ID_TO_KHZ(pAdapter->CommonCfg.Channel, pConfiguration->DSConfig);
4681                 wrq->u.data.length = sizeof(NDIS_802_11_CONFIGURATION);
4682                 Status = copy_to_user(wrq->u.data.pointer, pConfiguration, wrq->u.data.length);
4683                 DBGPRINT(RT_DEBUG_TRACE, ("Query::OID_802_11_CONFIGURATION(BeaconPeriod=%ld,AtimW=%ld,Channel=%d) \n",
4684                                         pConfiguration->BeaconPeriod, pConfiguration->ATIMWindow, pAdapter->CommonCfg.Channel));
4685                                 kfree(pConfiguration);
4686             }
4687             else
4688             {
4689                 DBGPRINT(RT_DEBUG_TRACE, ("Query::OID_802_11_CONFIGURATION(kmalloc failed)\n"));
4690                 Status = -EFAULT;
4691             }
4692             break;
4693                 case RT_OID_802_11_SNR_0:
4694                         if ((pAdapter->StaCfg.LastSNR0 > 0))
4695                         {
4696                                 ulInfo = ((0xeb - pAdapter->StaCfg.LastSNR0) * 3) /     16 ;
4697                                 wrq->u.data.length = sizeof(ulInfo);
4698                                 Status = copy_to_user(wrq->u.data.pointer, &ulInfo,     wrq->u.data.length);
4699                                 DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_802_11_SNR_0(0x=%lx)\n", ulInfo));
4700                         }
4701             else
4702                             Status = -EFAULT;
4703                         break;
4704                 case RT_OID_802_11_SNR_1:
4705                         if ((pAdapter->Antenna.field.RxPath     > 1) &&
4706                 (pAdapter->StaCfg.LastSNR1 > 0))
4707                         {
4708                                 ulInfo = ((0xeb - pAdapter->StaCfg.LastSNR1) * 3) /     16 ;
4709                                 wrq->u.data.length = sizeof(ulInfo);
4710                                 Status = copy_to_user(wrq->u.data.pointer, &ulInfo,     wrq->u.data.length);
4711                                 DBGPRINT(RT_DEBUG_TRACE,("Query::RT_OID_802_11_SNR_1(0x=%lx)\n",ulInfo));
4712                         }
4713                         else
4714                                 Status = -EFAULT;
4715             DBGPRINT(RT_DEBUG_TRACE,("Query::RT_OID_802_11_SNR_1(pAdapter->StaCfg.LastSNR1=%d)\n",pAdapter->StaCfg.LastSNR1));
4716                         break;
4717         case OID_802_11_RSSI_TRIGGER:
4718             ulInfo = pAdapter->StaCfg.RssiSample.LastRssi0 - pAdapter->BbpRssiToDbmDelta;
4719             wrq->u.data.length = sizeof(ulInfo);
4720             Status = copy_to_user(wrq->u.data.pointer, &ulInfo, wrq->u.data.length);
4721             DBGPRINT(RT_DEBUG_TRACE, ("Query::OID_802_11_RSSI_TRIGGER(=%ld)\n", ulInfo));
4722             break;
4723                 case OID_802_11_RSSI:
4724         case RT_OID_802_11_RSSI:
4725                         ulInfo = pAdapter->StaCfg.RssiSample.LastRssi0;
4726                         wrq->u.data.length = sizeof(ulInfo);
4727                         Status = copy_to_user(wrq->u.data.pointer, &ulInfo,     wrq->u.data.length);
4728                         break;
4729                 case RT_OID_802_11_RSSI_1:
4730             ulInfo = pAdapter->StaCfg.RssiSample.LastRssi1;
4731                         wrq->u.data.length = sizeof(ulInfo);
4732                         Status = copy_to_user(wrq->u.data.pointer, &ulInfo,     wrq->u.data.length);
4733                         break;
4734         case RT_OID_802_11_RSSI_2:
4735             ulInfo = pAdapter->StaCfg.RssiSample.LastRssi2;
4736                         wrq->u.data.length = sizeof(ulInfo);
4737                         Status = copy_to_user(wrq->u.data.pointer, &ulInfo,     wrq->u.data.length);
4738                         break;
4739         case OID_802_11_STATISTICS:
4740             pStatistics = (NDIS_802_11_STATISTICS *) kmalloc(sizeof(NDIS_802_11_STATISTICS), MEM_ALLOC_FLAG);
4741             if (pStatistics)
4742             {
4743                 DBGPRINT(RT_DEBUG_TRACE, ("Query::OID_802_11_STATISTICS \n"));
4744                 // add the most up-to-date h/w raw counters into software counters
4745                             NICUpdateRawCounters(pAdapter);
4746
4747                 // Sanity check for calculation of sucessful count
4748                 if (pAdapter->WlanCounters.TransmittedFragmentCount.QuadPart < pAdapter->WlanCounters.RetryCount.QuadPart)
4749                     pAdapter->WlanCounters.TransmittedFragmentCount.QuadPart = pAdapter->WlanCounters.RetryCount.QuadPart;
4750
4751                 pStatistics->TransmittedFragmentCount.QuadPart = pAdapter->WlanCounters.TransmittedFragmentCount.QuadPart;
4752                 pStatistics->MulticastTransmittedFrameCount.QuadPart = pAdapter->WlanCounters.MulticastTransmittedFrameCount.QuadPart;
4753                 pStatistics->FailedCount.QuadPart = pAdapter->WlanCounters.FailedCount.QuadPart;
4754                 pStatistics->RetryCount.QuadPart = pAdapter->WlanCounters.RetryCount.QuadPart;
4755                 pStatistics->MultipleRetryCount.QuadPart = pAdapter->WlanCounters.MultipleRetryCount.QuadPart;
4756                 pStatistics->RTSSuccessCount.QuadPart = pAdapter->WlanCounters.RTSSuccessCount.QuadPart;
4757                 pStatistics->RTSFailureCount.QuadPart = pAdapter->WlanCounters.RTSFailureCount.QuadPart;
4758                 pStatistics->ACKFailureCount.QuadPart = pAdapter->WlanCounters.ACKFailureCount.QuadPart;
4759                 pStatistics->FrameDuplicateCount.QuadPart = pAdapter->WlanCounters.FrameDuplicateCount.QuadPart;
4760                 pStatistics->ReceivedFragmentCount.QuadPart = pAdapter->WlanCounters.ReceivedFragmentCount.QuadPart;
4761                 pStatistics->MulticastReceivedFrameCount.QuadPart = pAdapter->WlanCounters.MulticastReceivedFrameCount.QuadPart;
4762 #ifdef DBG
4763                 pStatistics->FCSErrorCount = pAdapter->RalinkCounters.RealFcsErrCount;
4764 #else
4765                 pStatistics->FCSErrorCount.QuadPart = pAdapter->WlanCounters.FCSErrorCount.QuadPart;
4766                 pStatistics->FrameDuplicateCount.u.LowPart = pAdapter->WlanCounters.FrameDuplicateCount.u.LowPart / 100;
4767 #endif
4768                 wrq->u.data.length = sizeof(NDIS_802_11_STATISTICS);
4769                 Status = copy_to_user(wrq->u.data.pointer, pStatistics, wrq->u.data.length);
4770                 kfree(pStatistics);
4771             }
4772             else
4773             {
4774                 DBGPRINT(RT_DEBUG_TRACE, ("Query::OID_802_11_STATISTICS(kmalloc failed)\n"));
4775                 Status = -EFAULT;
4776             }
4777             break;
4778         case OID_GEN_RCV_OK:
4779             ulInfo = pAdapter->Counters8023.GoodReceives;
4780             wrq->u.data.length = sizeof(ulInfo);
4781             Status = copy_to_user(wrq->u.data.pointer, &ulInfo, wrq->u.data.length);
4782             break;
4783         case OID_GEN_RCV_NO_BUFFER:
4784             ulInfo = pAdapter->Counters8023.RxNoBuffer;
4785             wrq->u.data.length = sizeof(ulInfo);
4786             Status = copy_to_user(wrq->u.data.pointer, &ulInfo, wrq->u.data.length);
4787             break;
4788         case RT_OID_802_11_PHY_MODE:
4789             ulInfo = (ULONG)pAdapter->CommonCfg.PhyMode;
4790             wrq->u.data.length = sizeof(ulInfo);
4791             Status = copy_to_user(wrq->u.data.pointer, &ulInfo, wrq->u.data.length);
4792             DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_802_11_PHY_MODE (=%ld)\n", ulInfo));
4793             break;
4794         case RT_OID_802_11_STA_CONFIG:
4795             pStaConfig = (RT_802_11_STA_CONFIG *) kmalloc(sizeof(RT_802_11_STA_CONFIG), MEM_ALLOC_FLAG);
4796             if (pStaConfig)
4797             {
4798                 DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_802_11_STA_CONFIG\n"));
4799                 pStaConfig->EnableTxBurst = pAdapter->CommonCfg.bEnableTxBurst;
4800                 pStaConfig->EnableTurboRate = 0;
4801                 pStaConfig->UseBGProtection = pAdapter->CommonCfg.UseBGProtection;
4802                 pStaConfig->UseShortSlotTime = pAdapter->CommonCfg.bUseShortSlotTime;
4803                 //pStaConfig->AdhocMode = pAdapter->StaCfg.AdhocMode;
4804                 pStaConfig->HwRadioStatus = (pAdapter->StaCfg.bHwRadio == TRUE) ? 1 : 0;
4805                 pStaConfig->Rsv1 = 0;
4806                 pStaConfig->SystemErrorBitmap = pAdapter->SystemErrorBitmap;
4807                 wrq->u.data.length = sizeof(RT_802_11_STA_CONFIG);
4808                 Status = copy_to_user(wrq->u.data.pointer, pStaConfig, wrq->u.data.length);
4809                 kfree(pStaConfig);
4810             }
4811             else
4812             {
4813                 DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_802_11_STA_CONFIG(kmalloc failed)\n"));
4814                 Status = -EFAULT;
4815             }
4816             break;
4817         case OID_802_11_RTS_THRESHOLD:
4818             RtsThresh = pAdapter->CommonCfg.RtsThreshold;
4819             wrq->u.data.length = sizeof(RtsThresh);
4820             Status = copy_to_user(wrq->u.data.pointer, &RtsThresh, wrq->u.data.length);
4821             DBGPRINT(RT_DEBUG_TRACE, ("Query::OID_802_11_RTS_THRESHOLD(=%ld)\n", RtsThresh));
4822             break;
4823         case OID_802_11_FRAGMENTATION_THRESHOLD:
4824             FragThresh = pAdapter->CommonCfg.FragmentThreshold;
4825             if (pAdapter->CommonCfg.bUseZeroToDisableFragment == TRUE)
4826                 FragThresh = 0;
4827             wrq->u.data.length = sizeof(FragThresh);
4828             Status = copy_to_user(wrq->u.data.pointer, &FragThresh, wrq->u.data.length);
4829             DBGPRINT(RT_DEBUG_TRACE, ("Query::OID_802_11_FRAGMENTATION_THRESHOLD(=%ld)\n", FragThresh));
4830             break;
4831         case OID_802_11_POWER_MODE:
4832             PowerMode = pAdapter->StaCfg.WindowsPowerMode;
4833             wrq->u.data.length = sizeof(PowerMode);
4834             Status = copy_to_user(wrq->u.data.pointer, &PowerMode, wrq->u.data.length);
4835             DBGPRINT(RT_DEBUG_TRACE, ("Query::OID_802_11_POWER_MODE(=%d)\n", PowerMode));
4836             break;
4837         case RT_OID_802_11_RADIO:
4838             RadioState = (BOOLEAN) pAdapter->StaCfg.bSwRadio;
4839             wrq->u.data.length = sizeof(RadioState);
4840             Status = copy_to_user(wrq->u.data.pointer, &RadioState, wrq->u.data.length);
4841             DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_802_11_QUERY_RADIO (=%d)\n", RadioState));
4842             break;
4843         case OID_802_11_INFRASTRUCTURE_MODE:
4844             if (pAdapter->StaCfg.BssType == BSS_ADHOC)
4845                 BssType = Ndis802_11IBSS;
4846             else if (pAdapter->StaCfg.BssType == BSS_INFRA)
4847                 BssType = Ndis802_11Infrastructure;
4848             else if (pAdapter->StaCfg.BssType == BSS_MONITOR)
4849                 BssType = Ndis802_11Monitor;
4850             else
4851                 BssType = Ndis802_11AutoUnknown;
4852
4853             wrq->u.data.length = sizeof(BssType);
4854             Status = copy_to_user(wrq->u.data.pointer, &BssType, wrq->u.data.length);
4855             DBGPRINT(RT_DEBUG_TRACE, ("Query::OID_802_11_INFRASTRUCTURE_MODE(=%d)\n", BssType));
4856             break;
4857         case RT_OID_802_11_PREAMBLE:
4858             PreamType = pAdapter->CommonCfg.TxPreamble;
4859             wrq->u.data.length = sizeof(PreamType);
4860             Status = copy_to_user(wrq->u.data.pointer, &PreamType, wrq->u.data.length);
4861             DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_802_11_PREAMBLE(=%d)\n", PreamType));
4862             break;
4863         case OID_802_11_AUTHENTICATION_MODE:
4864             AuthMode = pAdapter->StaCfg.AuthMode;
4865             wrq->u.data.length = sizeof(AuthMode);
4866             Status = copy_to_user(wrq->u.data.pointer, &AuthMode, wrq->u.data.length);
4867             DBGPRINT(RT_DEBUG_TRACE, ("Query::OID_802_11_AUTHENTICATION_MODE(=%d)\n", AuthMode));
4868             break;
4869         case OID_802_11_WEP_STATUS:
4870             WepStatus = pAdapter->StaCfg.WepStatus;
4871             wrq->u.data.length = sizeof(WepStatus);
4872             Status = copy_to_user(wrq->u.data.pointer, &WepStatus, wrq->u.data.length);
4873             DBGPRINT(RT_DEBUG_TRACE, ("Query::OID_802_11_WEP_STATUS(=%d)\n", WepStatus));
4874             break;
4875         case OID_802_11_TX_POWER_LEVEL:
4876                         wrq->u.data.length = sizeof(ULONG);
4877                         Status = copy_to_user(wrq->u.data.pointer, &pAdapter->CommonCfg.TxPower, wrq->u.data.length);
4878                         DBGPRINT(RT_DEBUG_TRACE, ("Query::OID_802_11_TX_POWER_LEVEL %x\n",pAdapter->CommonCfg.TxPower));
4879                         break;
4880         case RT_OID_802_11_TX_POWER_LEVEL_1:
4881             wrq->u.data.length = sizeof(ULONG);
4882             Status = copy_to_user(wrq->u.data.pointer, &pAdapter->CommonCfg.TxPowerPercentage, wrq->u.data.length);
4883                         DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_802_11_TX_POWER_LEVEL_1 (=%ld)\n", pAdapter->CommonCfg.TxPowerPercentage));
4884                         break;
4885         case OID_802_11_NETWORK_TYPES_SUPPORTED:
4886                         if ((pAdapter->RfIcType == RFIC_2850) || (pAdapter->RfIcType == RFIC_2750))
4887                         {
4888                                 NetworkTypeList[0] = 3;                 // NumberOfItems = 3
4889                                 NetworkTypeList[1] = Ndis802_11DS;      // NetworkType[1] = 11b
4890                                 NetworkTypeList[2] = Ndis802_11OFDM24;  // NetworkType[2] = 11g
4891                                 NetworkTypeList[3] = Ndis802_11OFDM5;   // NetworkType[3] = 11a
4892                 wrq->u.data.length = 16;
4893                                 Status = copy_to_user(wrq->u.data.pointer, &NetworkTypeList[0], wrq->u.data.length);
4894                         }
4895                         else
4896                         {
4897                                 NetworkTypeList[0] = 2;                 // NumberOfItems = 2
4898                                 NetworkTypeList[1] = Ndis802_11DS;      // NetworkType[1] = 11b
4899                                 NetworkTypeList[2] = Ndis802_11OFDM24;  // NetworkType[2] = 11g
4900                             wrq->u.data.length = 12;
4901                                 Status = copy_to_user(wrq->u.data.pointer, &NetworkTypeList[0], wrq->u.data.length);
4902                         }
4903                         DBGPRINT(RT_DEBUG_TRACE, ("Query::OID_802_11_NETWORK_TYPES_SUPPORTED\n"));
4904                                 break;
4905             case OID_802_11_NETWORK_TYPE_IN_USE:
4906             wrq->u.data.length = sizeof(ULONG);
4907                         if (pAdapter->CommonCfg.PhyMode == PHY_11A)
4908                                 ulInfo = Ndis802_11OFDM5;
4909                         else if ((pAdapter->CommonCfg.PhyMode == PHY_11BG_MIXED) || (pAdapter->CommonCfg.PhyMode == PHY_11G))
4910                                 ulInfo = Ndis802_11OFDM24;
4911                         else
4912                                 ulInfo = Ndis802_11DS;
4913             Status = copy_to_user(wrq->u.data.pointer, &ulInfo, wrq->u.data.length);
4914                         break;
4915         case RT_OID_802_11_QUERY_LAST_RX_RATE:
4916             ulInfo = (ULONG)pAdapter->LastRxRate;
4917             wrq->u.data.length = sizeof(ulInfo);
4918                         Status = copy_to_user(wrq->u.data.pointer, &ulInfo, wrq->u.data.length);
4919                         DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_802_11_QUERY_LAST_RX_RATE (=%ld)\n", ulInfo));
4920                         break;
4921                 case RT_OID_802_11_QUERY_LAST_TX_RATE:
4922                         //ulInfo = (ULONG)pAdapter->LastTxRate;
4923                         ulInfo = (ULONG)pAdapter->MacTab.Content[BSSID_WCID].HTPhyMode.word;
4924                         wrq->u.data.length = sizeof(ulInfo);
4925                         Status = copy_to_user(wrq->u.data.pointer, &ulInfo,     wrq->u.data.length);
4926                         DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_802_11_QUERY_LAST_TX_RATE (=%lx)\n", ulInfo));
4927                         break;
4928         case RT_OID_802_11_QUERY_EEPROM_VERSION:
4929             wrq->u.data.length = sizeof(ULONG);
4930             Status = copy_to_user(wrq->u.data.pointer, &pAdapter->EepromVersion, wrq->u.data.length);
4931             break;
4932         case RT_OID_802_11_QUERY_FIRMWARE_VERSION:
4933             wrq->u.data.length = sizeof(ULONG);
4934             Status = copy_to_user(wrq->u.data.pointer, &pAdapter->FirmwareVersion, wrq->u.data.length);
4935                         break;
4936             case RT_OID_802_11_QUERY_NOISE_LEVEL:
4937                         wrq->u.data.length = sizeof(UCHAR);
4938                         Status = copy_to_user(wrq->u.data.pointer, &pAdapter->BbpWriteLatch[66], wrq->u.data.length);
4939                         DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_802_11_QUERY_NOISE_LEVEL (=%d)\n", pAdapter->BbpWriteLatch[66]));
4940                         break;
4941             case RT_OID_802_11_EXTRA_INFO:
4942                         wrq->u.data.length = sizeof(ULONG);
4943                         Status = copy_to_user(wrq->u.data.pointer, &pAdapter->ExtraInfo, wrq->u.data.length);
4944                 DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_802_11_EXTRA_INFO (=%ld)\n", pAdapter->ExtraInfo));
4945                 break;
4946             case RT_OID_WE_VERSION_COMPILED:
4947                 wrq->u.data.length = sizeof(UINT);
4948                 we_version_compiled = WIRELESS_EXT;
4949                 Status = copy_to_user(wrq->u.data.pointer, &we_version_compiled, wrq->u.data.length);
4950                 break;
4951                 case RT_OID_802_11_QUERY_APSD_SETTING:
4952                         apsd = (pAdapter->CommonCfg.bAPSDCapable | (pAdapter->CommonCfg.bAPSDAC_BE << 1) | (pAdapter->CommonCfg.bAPSDAC_BK << 2)
4953                                 | (pAdapter->CommonCfg.bAPSDAC_VI << 3) | (pAdapter->CommonCfg.bAPSDAC_VO << 4) | (pAdapter->CommonCfg.MaxSPLength << 5));
4954
4955                         wrq->u.data.length = sizeof(ULONG);
4956                         Status = copy_to_user(wrq->u.data.pointer, &apsd, wrq->u.data.length);
4957                         DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_802_11_QUERY_APSD_SETTING (=0x%lx,APSDCap=%d,AC_BE=%d,AC_BK=%d,AC_VI=%d,AC_VO=%d,MAXSPLen=%d)\n",
4958                                 apsd,pAdapter->CommonCfg.bAPSDCapable,pAdapter->CommonCfg.bAPSDAC_BE,pAdapter->CommonCfg.bAPSDAC_BK,pAdapter->CommonCfg.bAPSDAC_VI,pAdapter->CommonCfg.bAPSDAC_VO,pAdapter->CommonCfg.MaxSPLength));
4959                         break;
4960                 case RT_OID_802_11_QUERY_APSD_PSM:
4961                         wrq->u.data.length = sizeof(ULONG);
4962                         Status = copy_to_user(wrq->u.data.pointer, &pAdapter->CommonCfg.bAPSDForcePowerSave, wrq->u.data.length);
4963                         DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_802_11_QUERY_APSD_PSM (=%d)\n", pAdapter->CommonCfg.bAPSDForcePowerSave));
4964                         break;
4965                 case RT_OID_802_11_QUERY_WMM:
4966                         wrq->u.data.length = sizeof(BOOLEAN);
4967                         Status = copy_to_user(wrq->u.data.pointer, &pAdapter->CommonCfg.bWmmCapable, wrq->u.data.length);
4968                         DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_802_11_QUERY_WMM (=%d)\n",     pAdapter->CommonCfg.bWmmCapable));
4969                         break;
4970         case RT_OID_NEW_DRIVER:
4971             {
4972                 UCHAR enabled = 1;
4973                 wrq->u.data.length = sizeof(UCHAR);
4974                 Status = copy_to_user(wrq->u.data.pointer, &enabled, wrq->u.data.length);
4975                 DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_NEW_DRIVER (=%d)\n", enabled));
4976             }
4977                 break;
4978         case RT_OID_WPA_SUPPLICANT_SUPPORT:
4979                 wrq->u.data.length = sizeof(UCHAR);
4980                 Status = copy_to_user(wrq->u.data.pointer, &pAdapter->StaCfg.WpaSupplicantUP, wrq->u.data.length);
4981             DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_WPA_SUPPLICANT_SUPPORT (=%d)\n", pAdapter->StaCfg.WpaSupplicantUP));
4982                 break;
4983         case RT_OID_DRIVER_DEVICE_NAME:
4984             DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_DRIVER_DEVICE_NAME \n"));
4985                         wrq->u.data.length = 16;
4986                         if (copy_to_user(wrq->u.data.pointer, pAdapter->StaCfg.dev_name, wrq->u.data.length))
4987                         {
4988                                 Status = -EFAULT;
4989                         }
4990             break;
4991         case RT_OID_802_11_QUERY_HT_PHYMODE:
4992             pHTPhyMode = (OID_SET_HT_PHYMODE *) kmalloc(sizeof(OID_SET_HT_PHYMODE), MEM_ALLOC_FLAG);
4993             if (pHTPhyMode)
4994             {
4995                 pHTPhyMode->PhyMode = pAdapter->CommonCfg.PhyMode;
4996                         pHTPhyMode->HtMode = (UCHAR)pAdapter->MacTab.Content[BSSID_WCID].HTPhyMode.field.MODE;
4997                         pHTPhyMode->BW = (UCHAR)pAdapter->MacTab.Content[BSSID_WCID].HTPhyMode.field.BW;
4998                         pHTPhyMode->MCS= (UCHAR)pAdapter->MacTab.Content[BSSID_WCID].HTPhyMode.field.MCS;
4999                         pHTPhyMode->SHORTGI= (UCHAR)pAdapter->MacTab.Content[BSSID_WCID].HTPhyMode.field.ShortGI;
5000                         pHTPhyMode->STBC= (UCHAR)pAdapter->MacTab.Content[BSSID_WCID].HTPhyMode.field.STBC;
5001
5002                         pHTPhyMode->ExtOffset = ((pAdapter->CommonCfg.CentralChannel < pAdapter->CommonCfg.Channel) ? (EXTCHA_BELOW) : (EXTCHA_ABOVE));
5003                 wrq->u.data.length = sizeof(OID_SET_HT_PHYMODE);
5004                 if (copy_to_user(wrq->u.data.pointer, pHTPhyMode, wrq->u.data.length))
5005                         {
5006                                 Status = -EFAULT;
5007                         }
5008                         DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_802_11_QUERY_HT_PHYMODE (PhyMode = %d, MCS =%d, BW = %d, STBC = %d, ExtOffset=%d)\n",
5009                                 pHTPhyMode->HtMode, pHTPhyMode->MCS, pHTPhyMode->BW, pHTPhyMode->STBC, pHTPhyMode->ExtOffset));
5010                         DBGPRINT(RT_DEBUG_TRACE, (" MlmeUpdateTxRates (.word = %x )\n", pAdapter->MacTab.Content[BSSID_WCID].HTPhyMode.word));
5011             }
5012             else
5013             {
5014                 DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_802_11_STA_CONFIG(kmalloc failed)\n"));
5015                 Status = -EFAULT;
5016             }
5017             break;
5018         case RT_OID_802_11_COUNTRY_REGION:
5019             DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_802_11_COUNTRY_REGION \n"));
5020                         wrq->u.data.length = sizeof(ulInfo);
5021             ulInfo = pAdapter->CommonCfg.CountryRegionForABand;
5022             ulInfo = (ulInfo << 8)|(pAdapter->CommonCfg.CountryRegion);
5023                         if (copy_to_user(wrq->u.data.pointer, &ulInfo, wrq->u.data.length))
5024             {
5025                                 Status = -EFAULT;
5026             }
5027             break;
5028         case RT_OID_802_11_QUERY_DAT_HT_PHYMODE:
5029             pHTPhyMode = (OID_SET_HT_PHYMODE *) kmalloc(sizeof(OID_SET_HT_PHYMODE), MEM_ALLOC_FLAG);
5030             if (pHTPhyMode)
5031             {
5032                 pHTPhyMode->PhyMode = pAdapter->CommonCfg.PhyMode;
5033                         pHTPhyMode->HtMode = (UCHAR)pAdapter->CommonCfg.RegTransmitSetting.field.HTMODE;
5034                         pHTPhyMode->BW = (UCHAR)pAdapter->CommonCfg.RegTransmitSetting.field.BW;
5035                         pHTPhyMode->MCS= (UCHAR)pAdapter->StaCfg.DesiredTransmitSetting.field.MCS;
5036                         pHTPhyMode->SHORTGI= (UCHAR)pAdapter->CommonCfg.RegTransmitSetting.field.ShortGI;
5037                         pHTPhyMode->STBC= (UCHAR)pAdapter->CommonCfg.RegTransmitSetting.field.STBC;
5038
5039                 wrq->u.data.length = sizeof(OID_SET_HT_PHYMODE);
5040                 if (copy_to_user(wrq->u.data.pointer, pHTPhyMode, wrq->u.data.length))
5041                         {
5042                                 Status = -EFAULT;
5043                         }
5044                         DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_802_11_QUERY_HT_PHYMODE (PhyMode = %d, MCS =%d, BW = %d, STBC = %d, ExtOffset=%d)\n",
5045                                 pHTPhyMode->HtMode, pHTPhyMode->MCS, pHTPhyMode->BW, pHTPhyMode->STBC, pHTPhyMode->ExtOffset));
5046                         DBGPRINT(RT_DEBUG_TRACE, (" MlmeUpdateTxRates (.word = %x )\n", pAdapter->MacTab.Content[BSSID_WCID].HTPhyMode.word));
5047             }
5048             else
5049             {
5050                 DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_802_11_STA_CONFIG(kmalloc failed)\n"));
5051                 Status = -EFAULT;
5052             }
5053             break;
5054         case RT_OID_QUERY_MULTIPLE_CARD_SUPPORT:
5055                         wrq->u.data.length = sizeof(UCHAR);
5056             i = 0;
5057                         if (copy_to_user(wrq->u.data.pointer, &i, wrq->u.data.length))
5058             {
5059                                 Status = -EFAULT;
5060             }
5061             DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_QUERY_MULTIPLE_CARD_SUPPORT(=%d) \n", i));
5062             break;
5063
5064                 case OID_802_11_BUILD_CHANNEL_EX:
5065                         {
5066                                 UCHAR value;
5067                                 DBGPRINT(RT_DEBUG_TRACE, ("Query::OID_802_11_BUILD_CHANNEL_EX \n"));
5068                                 wrq->u.data.length = sizeof(UCHAR);
5069                                 DBGPRINT(RT_DEBUG_TRACE, ("Doesn't support EXT_BUILD_CHANNEL_LIST.\n"));
5070                                 value = 0;
5071                                 Status = copy_to_user(wrq->u.data.pointer, &value, 1);
5072                                 DBGPRINT(RT_DEBUG_TRACE, ("Status=%d\n", Status));
5073                         }
5074                         break;
5075
5076                 case OID_802_11_GET_CH_LIST:
5077                         {
5078                                 PRT_CHANNEL_LIST_INFO pChListBuf;
5079
5080                                 DBGPRINT(RT_DEBUG_TRACE, ("Query::OID_802_11_GET_CH_LIST \n"));
5081                                 if (pAdapter->ChannelListNum == 0)
5082                                 {
5083                                         wrq->u.data.length = 0;
5084                                         break;
5085                                 }
5086
5087                                 pChListBuf = (RT_CHANNEL_LIST_INFO *) kmalloc(sizeof(RT_CHANNEL_LIST_INFO), MEM_ALLOC_FLAG);
5088                                 if (pChListBuf == NULL)
5089                                 {
5090                                         wrq->u.data.length = 0;
5091                                         break;
5092                                 }
5093
5094                                 pChListBuf->ChannelListNum = pAdapter->ChannelListNum;
5095                                 for (i = 0; i < pChListBuf->ChannelListNum; i++)
5096                                         pChListBuf->ChannelList[i] = pAdapter->ChannelList[i].Channel;
5097
5098                                 wrq->u.data.length = sizeof(RT_CHANNEL_LIST_INFO);
5099                                 Status = copy_to_user(wrq->u.data.pointer, pChListBuf, sizeof(RT_CHANNEL_LIST_INFO));
5100                                 DBGPRINT(RT_DEBUG_TRACE, ("Status=%d\n", Status));
5101
5102                                 if (pChListBuf)
5103                                         kfree(pChListBuf);
5104                         }
5105                         break;
5106
5107                 case OID_802_11_GET_COUNTRY_CODE:
5108                         DBGPRINT(RT_DEBUG_TRACE, ("Query::OID_802_11_GET_COUNTRY_CODE \n"));
5109                         wrq->u.data.length = 2;
5110                         Status = copy_to_user(wrq->u.data.pointer, &pAdapter->CommonCfg.CountryCode, 2);
5111                         DBGPRINT(RT_DEBUG_TRACE, ("Status=%d\n", Status));
5112                         break;
5113
5114                 case OID_802_11_GET_CHANNEL_GEOGRAPHY:
5115                         DBGPRINT(RT_DEBUG_TRACE, ("Query::OID_802_11_GET_CHANNEL_GEOGRAPHY \n"));
5116                         wrq->u.data.length = 1;
5117                         Status = copy_to_user(wrq->u.data.pointer, &pAdapter->CommonCfg.Geography, 1);
5118                         DBGPRINT(RT_DEBUG_TRACE, ("Status=%d\n", Status));
5119                         break;
5120
5121         default:
5122             DBGPRINT(RT_DEBUG_TRACE, ("Query::unknown IOCTL's subcmd = 0x%08x\n", cmd));
5123             Status = -EOPNOTSUPP;
5124             break;
5125     }
5126     return Status;
5127 }
5128
5129 INT rt28xx_sta_ioctl(
5130         IN      struct net_device       *net_dev,
5131         IN      OUT     struct ifreq    *rq,
5132         IN      INT                                     cmd)
5133 {
5134         POS_COOKIE                      pObj;
5135         VIRTUAL_ADAPTER         *pVirtualAd = NULL;
5136         RTMP_ADAPTER        *pAd = NULL;
5137         struct iwreq        *wrq = (struct iwreq *) rq;
5138         BOOLEAN                         StateMachineTouched = FALSE;
5139         INT                                     Status = NDIS_STATUS_SUCCESS;
5140         USHORT                          subcmd;
5141
5142         if (net_dev->priv_flags == INT_MAIN)
5143         {
5144                 pAd = net_dev->ml_priv;
5145         }
5146         else
5147         {
5148                 pVirtualAd = net_dev->ml_priv;
5149                 pAd = pVirtualAd->RtmpDev->ml_priv;
5150         }
5151         pObj = (POS_COOKIE) pAd->OS_Cookie;
5152
5153         if (pAd == NULL)
5154         {
5155                 /* if 1st open fail, pAd will be free;
5156                    So the net_dev->ml_priv will be NULL in 2rd open */
5157                 return -ENETDOWN;
5158         }
5159
5160     //check if the interface is down
5161     if(!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_INTERRUPT_IN_USE))
5162     {
5163         {
5164             DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
5165                     return -ENETDOWN;
5166         }
5167     }
5168
5169         {       // determine this ioctl command is comming from which interface.
5170                 pObj->ioctl_if_type = INT_MAIN;
5171                 pObj->ioctl_if = MAIN_MBSSID;
5172         }
5173
5174         switch(cmd)
5175         {
5176         case SIOCGIFHWADDR:
5177                         DBGPRINT(RT_DEBUG_TRACE, ("IOCTL::SIOCGIFHWADDR\n"));
5178                         memcpy(wrq->u.name, pAd->CurrentAddress, ETH_ALEN);
5179                         break;
5180                 case SIOCGIWNAME:
5181         {
5182                 char *name=&wrq->u.name[0];
5183                 rt_ioctl_giwname(net_dev, NULL, name, NULL);
5184                         break;
5185                 }
5186                 case SIOCGIWESSID:  //Get ESSID
5187         {
5188                 struct iw_point *essid=&wrq->u.essid;
5189                 rt_ioctl_giwessid(net_dev, NULL, essid, essid->pointer);
5190                         break;
5191                 }
5192                 case SIOCSIWESSID:  //Set ESSID
5193         {
5194                 struct iw_point *essid=&wrq->u.essid;
5195                 rt_ioctl_siwessid(net_dev, NULL, essid, essid->pointer);
5196                         break;
5197                 }
5198                 case SIOCSIWNWID:   // set network id (the cell)
5199                 case SIOCGIWNWID:   // get network id
5200                         Status = -EOPNOTSUPP;
5201                         break;
5202                 case SIOCSIWFREQ:   //set channel/frequency (Hz)
5203         {
5204                 struct iw_freq *freq=&wrq->u.freq;
5205                 rt_ioctl_siwfreq(net_dev, NULL, freq, NULL);
5206                         break;
5207                 }
5208                 case SIOCGIWFREQ:   // get channel/frequency (Hz)
5209         {
5210                 struct iw_freq *freq=&wrq->u.freq;
5211                 rt_ioctl_giwfreq(net_dev, NULL, freq, NULL);
5212                         break;
5213                 }
5214                 case SIOCSIWNICKN: //set node name/nickname
5215         {
5216                 struct iw_point *data=&wrq->u.data;
5217                 rt_ioctl_siwnickn(net_dev, NULL, data, NULL);
5218                         break;
5219                 }
5220                 case SIOCGIWNICKN: //get node name/nickname
5221         {
5222                 struct iw_point *data=&wrq->u.data;
5223                 rt_ioctl_giwnickn(net_dev, NULL, data, NULL);
5224                         break;
5225                 }
5226                 case SIOCGIWRATE:   //get default bit rate (bps)
5227                     rt_ioctl_giwrate(net_dev, NULL, &wrq->u, NULL);
5228             break;
5229             case SIOCSIWRATE:  //set default bit rate (bps)
5230                 rt_ioctl_siwrate(net_dev, NULL, &wrq->u, NULL);
5231             break;
5232         case SIOCGIWRTS:  // get RTS/CTS threshold (bytes)
5233         {
5234                 struct iw_param *rts=&wrq->u.rts;
5235                 rt_ioctl_giwrts(net_dev, NULL, rts, NULL);
5236                         break;
5237                 }
5238         case SIOCSIWRTS:  //set RTS/CTS threshold (bytes)
5239         {
5240                 struct iw_param *rts=&wrq->u.rts;
5241                 rt_ioctl_siwrts(net_dev, NULL, rts, NULL);
5242                         break;
5243                 }
5244         case SIOCGIWFRAG:  //get fragmentation thr (bytes)
5245         {
5246                 struct iw_param *frag=&wrq->u.frag;
5247                 rt_ioctl_giwfrag(net_dev, NULL, frag, NULL);
5248                         break;
5249                 }
5250         case SIOCSIWFRAG:  //set fragmentation thr (bytes)
5251         {
5252                 struct iw_param *frag=&wrq->u.frag;
5253                 rt_ioctl_siwfrag(net_dev, NULL, frag, NULL);
5254                         break;
5255                 }
5256         case SIOCGIWENCODE:  //get encoding token & mode
5257         {
5258                 struct iw_point *erq=&wrq->u.encoding;
5259                 if(erq->pointer)
5260                         rt_ioctl_giwencode(net_dev, NULL, erq, erq->pointer);
5261                         break;
5262                 }
5263         case SIOCSIWENCODE:  //set encoding token & mode
5264         {
5265                 struct iw_point *erq=&wrq->u.encoding;
5266                 if(erq->pointer)
5267                         rt_ioctl_siwencode(net_dev, NULL, erq, erq->pointer);
5268                         break;
5269                 }
5270                 case SIOCGIWAP:     //get access point MAC addresses
5271         {
5272                 struct sockaddr *ap_addr=&wrq->u.ap_addr;
5273                 rt_ioctl_giwap(net_dev, NULL, ap_addr, ap_addr->sa_data);
5274                         break;
5275                 }
5276             case SIOCSIWAP:  //set access point MAC addresses
5277         {
5278                 struct sockaddr *ap_addr=&wrq->u.ap_addr;
5279                 rt_ioctl_siwap(net_dev, NULL, ap_addr, ap_addr->sa_data);
5280                         break;
5281                 }
5282                 case SIOCGIWMODE:   //get operation mode
5283         {
5284                 __u32 *mode=&wrq->u.mode;
5285                 rt_ioctl_giwmode(net_dev, NULL, mode, NULL);
5286                         break;
5287                 }
5288                 case SIOCSIWMODE:   //set operation mode
5289         {
5290                 __u32 *mode=&wrq->u.mode;
5291                 rt_ioctl_siwmode(net_dev, NULL, mode, NULL);
5292                         break;
5293                 }
5294                 case SIOCGIWSENS:   //get sensitivity (dBm)
5295                 case SIOCSIWSENS:       //set sensitivity (dBm)
5296                 case SIOCGIWPOWER:  //get Power Management settings
5297                 case SIOCSIWPOWER:  //set Power Management settings
5298                 case SIOCGIWTXPOW:  //get transmit power (dBm)
5299                 case SIOCSIWTXPOW:  //set transmit power (dBm)
5300                 case SIOCGIWRANGE:      //Get range of parameters
5301                 case SIOCGIWRETRY:      //get retry limits and lifetime
5302                 case SIOCSIWRETRY:      //set retry limits and lifetime
5303                         Status = -EOPNOTSUPP;
5304                         break;
5305                 case RT_PRIV_IOCTL:
5306 #ifdef RT30xx
5307         case RT_PRIV_IOCTL_EXT:
5308 #endif
5309                         subcmd = wrq->u.data.flags;
5310                         if( subcmd & OID_GET_SET_TOGGLE)
5311                                 Status = RTMPSetInformation(pAd, rq, subcmd);
5312                         else
5313                                 Status = RTMPQueryInformation(pAd, rq, subcmd);
5314                         break;
5315                 case SIOCGIWPRIV:
5316                         if (wrq->u.data.pointer)
5317                         {
5318                                 if ( access_ok(VERIFY_WRITE, wrq->u.data.pointer, sizeof(privtab)) != TRUE)
5319                                         break;
5320                                 wrq->u.data.length = sizeof(privtab) / sizeof(privtab[0]);
5321                                 if (copy_to_user(wrq->u.data.pointer, privtab, sizeof(privtab)))
5322                                         Status = -EFAULT;
5323                         }
5324                         break;
5325                 case RTPRIV_IOCTL_SET:
5326                         if(access_ok(VERIFY_READ, wrq->u.data.pointer, wrq->u.data.length) != TRUE)
5327                                 break;
5328                         rt_ioctl_setparam(net_dev, NULL, NULL, wrq->u.data.pointer);
5329                         break;
5330                 case RTPRIV_IOCTL_GSITESURVEY:
5331                         RTMPIoctlGetSiteSurvey(pAd, wrq);
5332                     break;
5333 #ifdef DBG
5334                 case RTPRIV_IOCTL_MAC:
5335                         RTMPIoctlMAC(pAd, wrq);
5336                         break;
5337                 case RTPRIV_IOCTL_E2P:
5338                         RTMPIoctlE2PROM(pAd, wrq);
5339                         break;
5340 #ifdef RT30xx
5341                 case RTPRIV_IOCTL_RF:
5342                         RTMPIoctlRF(pAd, wrq);
5343                         break;
5344 #endif // RT30xx //
5345 #endif // DBG //
5346         case SIOCETHTOOL:
5347                 break;
5348                 default:
5349                         DBGPRINT(RT_DEBUG_ERROR, ("IOCTL::unknown IOCTL's cmd = 0x%08x\n", cmd));
5350                         Status = -EOPNOTSUPP;
5351                         break;
5352         }
5353
5354     if(StateMachineTouched) // Upper layer sent a MLME-related operations
5355         RT28XX_MLME_HANDLER(pAd);
5356
5357         return Status;
5358 }
5359
5360 /*
5361     ==========================================================================
5362     Description:
5363         Set SSID
5364     Return:
5365         TRUE if all parameters are OK, FALSE otherwise
5366     ==========================================================================
5367 */
5368 INT Set_SSID_Proc(
5369     IN  PRTMP_ADAPTER   pAdapter,
5370     IN  PUCHAR          arg)
5371 {
5372     NDIS_802_11_SSID                    Ssid, *pSsid=NULL;
5373     BOOLEAN                             StateMachineTouched = FALSE;
5374     int                                 success = TRUE;
5375
5376     if( strlen(arg) <= MAX_LEN_OF_SSID)
5377     {
5378         NdisZeroMemory(&Ssid, sizeof(NDIS_802_11_SSID));
5379         if (strlen(arg) != 0)
5380         {
5381             NdisMoveMemory(Ssid.Ssid, arg, strlen(arg));
5382             Ssid.SsidLength = strlen(arg);
5383         }
5384         else   //ANY ssid
5385         {
5386             Ssid.SsidLength = 0;
5387                     memcpy(Ssid.Ssid, "", 0);
5388                         pAdapter->StaCfg.BssType = BSS_INFRA;
5389                         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeOpen;
5390                 pAdapter->StaCfg.WepStatus  = Ndis802_11EncryptionDisabled;
5391                 }
5392         pSsid = &Ssid;
5393
5394         if (pAdapter->Mlme.CntlMachine.CurrState != CNTL_IDLE)
5395         {
5396             RT28XX_MLME_RESET_STATE_MACHINE(pAdapter);
5397             DBGPRINT(RT_DEBUG_TRACE, ("!!! MLME busy, reset MLME state machine !!!\n"));
5398         }
5399
5400         pAdapter->MlmeAux.CurrReqIsFromNdis = TRUE;
5401         pAdapter->StaCfg.bScanReqIsFromWebUI = FALSE;
5402                 pAdapter->bConfigChanged = TRUE;
5403
5404         MlmeEnqueue(pAdapter,
5405                     MLME_CNTL_STATE_MACHINE,
5406                     OID_802_11_SSID,
5407                     sizeof(NDIS_802_11_SSID),
5408                     (VOID *)pSsid);
5409
5410         StateMachineTouched = TRUE;
5411         DBGPRINT(RT_DEBUG_TRACE, ("Set_SSID_Proc::(Len=%d,Ssid=%s)\n", Ssid.SsidLength, Ssid.Ssid));
5412     }
5413     else
5414         success = FALSE;
5415
5416     if (StateMachineTouched) // Upper layer sent a MLME-related operations
5417         RT28XX_MLME_HANDLER(pAdapter);
5418
5419     return success;
5420 }
5421
5422 #ifdef WMM_SUPPORT
5423 /*
5424     ==========================================================================
5425     Description:
5426         Set WmmCapable Enable or Disable
5427     Return:
5428         TRUE if all parameters are OK, FALSE otherwise
5429     ==========================================================================
5430 */
5431 INT     Set_WmmCapable_Proc(
5432         IN      PRTMP_ADAPTER   pAd,
5433         IN      PUCHAR                  arg)
5434 {
5435         BOOLEAN bWmmCapable;
5436
5437         bWmmCapable = simple_strtol(arg, 0, 10);
5438
5439         if ((bWmmCapable == 1)
5440 #ifdef RT2870
5441                 && (pAd->NumberOfPipes >= 5)
5442 #endif // RT2870 //
5443                 )
5444                 pAd->CommonCfg.bWmmCapable = TRUE;
5445         else if (bWmmCapable == 0)
5446                 pAd->CommonCfg.bWmmCapable = FALSE;
5447         else
5448                 return FALSE;  //Invalid argument
5449
5450         DBGPRINT(RT_DEBUG_TRACE, ("Set_WmmCapable_Proc::(bWmmCapable=%d)\n",
5451                 pAd->CommonCfg.bWmmCapable));
5452
5453         return TRUE;
5454 }
5455 #endif // WMM_SUPPORT //
5456
5457 /*
5458     ==========================================================================
5459     Description:
5460         Set Network Type(Infrastructure/Adhoc mode)
5461     Return:
5462         TRUE if all parameters are OK, FALSE otherwise
5463     ==========================================================================
5464 */
5465 INT Set_NetworkType_Proc(
5466     IN  PRTMP_ADAPTER   pAdapter,
5467     IN  PUCHAR          arg)
5468 {
5469     UINT32      Value = 0;
5470
5471     if (strcmp(arg, "Adhoc") == 0)
5472         {
5473                 if (pAdapter->StaCfg.BssType != BSS_ADHOC)
5474                 {
5475                         // Config has changed
5476                         pAdapter->bConfigChanged = TRUE;
5477             if (MONITOR_ON(pAdapter))
5478             {
5479                 RTMP_IO_WRITE32(pAdapter, RX_FILTR_CFG, STANORMAL);
5480                 RTMP_IO_READ32(pAdapter, MAC_SYS_CTRL, &Value);
5481                                 Value &= (~0x80);
5482                                 RTMP_IO_WRITE32(pAdapter, MAC_SYS_CTRL, Value);
5483                 OPSTATUS_CLEAR_FLAG(pAdapter, fOP_STATUS_MEDIA_STATE_CONNECTED);
5484                 pAdapter->StaCfg.bAutoReconnect = TRUE;
5485                 LinkDown(pAdapter, FALSE);
5486             }
5487                         if (INFRA_ON(pAdapter))
5488                         {
5489                                 //BOOLEAN Cancelled;
5490                                 // Set the AutoReconnectSsid to prevent it reconnect to old SSID
5491                                 // Since calling this indicate user don't want to connect to that SSID anymore.
5492                                 pAdapter->MlmeAux.AutoReconnectSsidLen= 32;
5493                                 NdisZeroMemory(pAdapter->MlmeAux.AutoReconnectSsid, pAdapter->MlmeAux.AutoReconnectSsidLen);
5494
5495                                 LinkDown(pAdapter, FALSE);
5496
5497                                 DBGPRINT(RT_DEBUG_TRACE, ("NDIS_STATUS_MEDIA_DISCONNECT Event BB!\n"));
5498                         }
5499                 }
5500                 pAdapter->StaCfg.BssType = BSS_ADHOC;
5501         pAdapter->net_dev->type = pAdapter->StaCfg.OriDevType;
5502                 DBGPRINT(RT_DEBUG_TRACE, ("===>Set_NetworkType_Proc::(AD-HOC)\n"));
5503         }
5504     else if (strcmp(arg, "Infra") == 0)
5505         {
5506                 if (pAdapter->StaCfg.BssType != BSS_INFRA)
5507                 {
5508                         // Config has changed
5509                         pAdapter->bConfigChanged = TRUE;
5510             if (MONITOR_ON(pAdapter))
5511             {
5512                 RTMP_IO_WRITE32(pAdapter, RX_FILTR_CFG, STANORMAL);
5513                 RTMP_IO_READ32(pAdapter, MAC_SYS_CTRL, &Value);
5514                                 Value &= (~0x80);
5515                                 RTMP_IO_WRITE32(pAdapter, MAC_SYS_CTRL, Value);
5516                 OPSTATUS_CLEAR_FLAG(pAdapter, fOP_STATUS_MEDIA_STATE_CONNECTED);
5517                 pAdapter->StaCfg.bAutoReconnect = TRUE;
5518                 LinkDown(pAdapter, FALSE);
5519             }
5520                         if (ADHOC_ON(pAdapter))
5521                         {
5522                                 // Set the AutoReconnectSsid to prevent it reconnect to old SSID
5523                                 // Since calling this indicate user don't want to connect to that SSID anymore.
5524                                 pAdapter->MlmeAux.AutoReconnectSsidLen= 32;
5525                                 NdisZeroMemory(pAdapter->MlmeAux.AutoReconnectSsid, pAdapter->MlmeAux.AutoReconnectSsidLen);
5526
5527                                 LinkDown(pAdapter, FALSE);
5528                         }
5529                 }
5530                 pAdapter->StaCfg.BssType = BSS_INFRA;
5531         pAdapter->net_dev->type = pAdapter->StaCfg.OriDevType;
5532                 DBGPRINT(RT_DEBUG_TRACE, ("===>Set_NetworkType_Proc::(INFRA)\n"));
5533
5534         pAdapter->StaCfg.BssType = BSS_INFRA;
5535         }
5536     else if (strcmp(arg, "Monitor") == 0)
5537     {
5538                 UCHAR   bbpValue = 0;
5539                 BCN_TIME_CFG_STRUC csr;
5540                 OPSTATUS_CLEAR_FLAG(pAdapter, fOP_STATUS_INFRA_ON);
5541         OPSTATUS_CLEAR_FLAG(pAdapter, fOP_STATUS_ADHOC_ON);
5542                 OPSTATUS_SET_FLAG(pAdapter, fOP_STATUS_MEDIA_STATE_CONNECTED);
5543                 // disable all periodic state machine
5544                 pAdapter->StaCfg.bAutoReconnect = FALSE;
5545                 // reset all mlme state machine
5546                 RT28XX_MLME_RESET_STATE_MACHINE(pAdapter);
5547                 DBGPRINT(RT_DEBUG_TRACE, ("fOP_STATUS_MEDIA_STATE_CONNECTED \n"));
5548         if (pAdapter->CommonCfg.CentralChannel == 0)
5549         {
5550             if (pAdapter->CommonCfg.PhyMode == PHY_11AN_MIXED)
5551                 pAdapter->CommonCfg.CentralChannel = 36;
5552             else
5553                 pAdapter->CommonCfg.CentralChannel = 6;
5554         }
5555         else
5556             N_ChannelCheck(pAdapter);
5557
5558         if (pAdapter->CommonCfg.PhyMode >= PHY_11ABGN_MIXED &&
5559             pAdapter->CommonCfg.RegTransmitSetting.field.BW == BW_40 &&
5560             pAdapter->CommonCfg.RegTransmitSetting.field.EXTCHA == EXTCHA_ABOVE)
5561                 {
5562                         // 40MHz ,control channel at lower
5563                         RTMP_BBP_IO_READ8_BY_REG_ID(pAdapter, BBP_R4, &bbpValue);
5564                         bbpValue &= (~0x18);
5565                         bbpValue |= 0x10;
5566                         RTMP_BBP_IO_WRITE8_BY_REG_ID(pAdapter, BBP_R4, bbpValue);
5567                         pAdapter->CommonCfg.BBPCurrentBW = BW_40;
5568                         //  RX : control channel at lower
5569                         RTMP_BBP_IO_READ8_BY_REG_ID(pAdapter, BBP_R3, &bbpValue);
5570                         bbpValue &= (~0x20);
5571                         RTMP_BBP_IO_WRITE8_BY_REG_ID(pAdapter, BBP_R3, bbpValue);
5572
5573                         RTMP_IO_READ32(pAdapter, TX_BAND_CFG, &Value);
5574                         Value &= 0xfffffffe;
5575                         RTMP_IO_WRITE32(pAdapter, TX_BAND_CFG, Value);
5576                         pAdapter->CommonCfg.CentralChannel = pAdapter->CommonCfg.Channel + 2;
5577             AsicSwitchChannel(pAdapter, pAdapter->CommonCfg.CentralChannel, FALSE);
5578                     AsicLockChannel(pAdapter, pAdapter->CommonCfg.CentralChannel);
5579             DBGPRINT(RT_DEBUG_TRACE, ("BW_40 ,control_channel(%d), CentralChannel(%d) \n",
5580                                        pAdapter->CommonCfg.Channel,
5581                                        pAdapter->CommonCfg.CentralChannel));
5582                 }
5583                 else if (pAdapter->CommonCfg.PhyMode >= PHY_11ABGN_MIXED &&
5584                  pAdapter->CommonCfg.RegTransmitSetting.field.BW == BW_40 &&
5585                  pAdapter->CommonCfg.RegTransmitSetting.field.EXTCHA == EXTCHA_BELOW)
5586                 {
5587                         // 40MHz ,control channel at upper
5588                         RTMP_BBP_IO_READ8_BY_REG_ID(pAdapter, BBP_R4, &bbpValue);
5589                         bbpValue &= (~0x18);
5590                         bbpValue |= 0x10;
5591                         RTMP_BBP_IO_WRITE8_BY_REG_ID(pAdapter, BBP_R4, bbpValue);
5592                         pAdapter->CommonCfg.BBPCurrentBW = BW_40;
5593                         RTMP_IO_READ32(pAdapter, TX_BAND_CFG, &Value);
5594                         Value |= 0x1;
5595                         RTMP_IO_WRITE32(pAdapter, TX_BAND_CFG, Value);
5596
5597                         RTMP_BBP_IO_READ8_BY_REG_ID(pAdapter, BBP_R3, &bbpValue);
5598                         bbpValue |= (0x20);
5599                         RTMP_BBP_IO_WRITE8_BY_REG_ID(pAdapter, BBP_R3, bbpValue);
5600                         pAdapter->CommonCfg.CentralChannel = pAdapter->CommonCfg.Channel - 2;
5601             AsicSwitchChannel(pAdapter, pAdapter->CommonCfg.CentralChannel, FALSE);
5602                     AsicLockChannel(pAdapter, pAdapter->CommonCfg.CentralChannel);
5603             DBGPRINT(RT_DEBUG_TRACE, ("BW_40 ,control_channel(%d), CentralChannel(%d) \n",
5604                                        pAdapter->CommonCfg.Channel,
5605                                        pAdapter->CommonCfg.CentralChannel));
5606                 }
5607                 else
5608                 {
5609                         // 20MHz
5610                         RTMP_BBP_IO_READ8_BY_REG_ID(pAdapter, BBP_R4, &bbpValue);
5611                         bbpValue &= (~0x18);
5612                         RTMP_BBP_IO_WRITE8_BY_REG_ID(pAdapter, BBP_R4, bbpValue);
5613                         pAdapter->CommonCfg.BBPCurrentBW = BW_20;
5614                         AsicSwitchChannel(pAdapter, pAdapter->CommonCfg.Channel, FALSE);
5615                         AsicLockChannel(pAdapter, pAdapter->CommonCfg.Channel);
5616                         DBGPRINT(RT_DEBUG_TRACE, ("BW_20, Channel(%d)\n", pAdapter->CommonCfg.Channel));
5617                 }
5618                 // Enable Rx with promiscuous reception
5619                 RTMP_IO_WRITE32(pAdapter, RX_FILTR_CFG, 0x3);
5620                 // ASIC supporsts sniffer function with replacing RSSI with timestamp.
5621                 //RTMP_IO_READ32(pAdapter, MAC_SYS_CTRL, &Value);
5622                 //Value |= (0x80);
5623                 //RTMP_IO_WRITE32(pAdapter, MAC_SYS_CTRL, Value);
5624                 // disable sync
5625                 RTMP_IO_READ32(pAdapter, BCN_TIME_CFG, &csr.word);
5626                 csr.field.bBeaconGen = 0;
5627                 csr.field.bTBTTEnable = 0;
5628                 csr.field.TsfSyncMode = 0;
5629                 RTMP_IO_WRITE32(pAdapter, BCN_TIME_CFG, csr.word);
5630
5631                 pAdapter->StaCfg.BssType = BSS_MONITOR;
5632         pAdapter->net_dev->type = ARPHRD_IEEE80211_PRISM; //ARPHRD_IEEE80211; // IEEE80211
5633                 DBGPRINT(RT_DEBUG_TRACE, ("===>Set_NetworkType_Proc::(MONITOR)\n"));
5634     }
5635
5636     // Reset Ralink supplicant to not use, it will be set to start when UI set PMK key
5637     pAdapter->StaCfg.WpaState = SS_NOTUSE;
5638
5639     DBGPRINT(RT_DEBUG_TRACE, ("Set_NetworkType_Proc::(NetworkType=%d)\n", pAdapter->StaCfg.BssType));
5640
5641     return TRUE;
5642 }
5643
5644 /*
5645     ==========================================================================
5646     Description:
5647         Set Authentication mode
5648     Return:
5649         TRUE if all parameters are OK, FALSE otherwise
5650     ==========================================================================
5651 */
5652 INT Set_AuthMode_Proc(
5653     IN  PRTMP_ADAPTER   pAdapter,
5654     IN  PUCHAR          arg)
5655 {
5656     if ((strcmp(arg, "WEPAUTO") == 0) || (strcmp(arg, "wepauto") == 0))
5657         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeAutoSwitch;
5658     else if ((strcmp(arg, "OPEN") == 0) || (strcmp(arg, "open") == 0))
5659         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeOpen;
5660     else if ((strcmp(arg, "SHARED") == 0) || (strcmp(arg, "shared") == 0))
5661         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeShared;
5662     else if ((strcmp(arg, "WPAPSK") == 0) || (strcmp(arg, "wpapsk") == 0))
5663         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeWPAPSK;
5664     else if ((strcmp(arg, "WPANONE") == 0) || (strcmp(arg, "wpanone") == 0))
5665         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeWPANone;
5666     else if ((strcmp(arg, "WPA2PSK") == 0) || (strcmp(arg, "wpa2psk") == 0))
5667         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeWPA2PSK;
5668     else if ((strcmp(arg, "WPA") == 0) || (strcmp(arg, "wpa") == 0))
5669         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeWPA;
5670     else if ((strcmp(arg, "WPA2") == 0) || (strcmp(arg, "wpa2") == 0))
5671         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeWPA2;
5672     else
5673         return FALSE;
5674
5675     pAdapter->StaCfg.PortSecured = WPA_802_1X_PORT_NOT_SECURED;
5676
5677     DBGPRINT(RT_DEBUG_TRACE, ("Set_AuthMode_Proc::(AuthMode=%d)\n", pAdapter->StaCfg.AuthMode));
5678
5679     return TRUE;
5680 }
5681
5682 /*
5683     ==========================================================================
5684     Description:
5685         Set Encryption Type
5686     Return:
5687         TRUE if all parameters are OK, FALSE otherwise
5688     ==========================================================================
5689 */
5690 INT Set_EncrypType_Proc(
5691     IN  PRTMP_ADAPTER   pAdapter,
5692     IN  PUCHAR          arg)
5693 {
5694     if ((strcmp(arg, "NONE") == 0) || (strcmp(arg, "none") == 0))
5695     {
5696         if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
5697             return TRUE;    // do nothing
5698
5699         pAdapter->StaCfg.WepStatus     = Ndis802_11WEPDisabled;
5700         pAdapter->StaCfg.PairCipher    = Ndis802_11WEPDisabled;
5701             pAdapter->StaCfg.GroupCipher   = Ndis802_11WEPDisabled;
5702     }
5703     else if ((strcmp(arg, "WEP") == 0) || (strcmp(arg, "wep") == 0))
5704     {
5705         if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
5706             return TRUE;    // do nothing
5707
5708         pAdapter->StaCfg.WepStatus     = Ndis802_11WEPEnabled;
5709         pAdapter->StaCfg.PairCipher    = Ndis802_11WEPEnabled;
5710             pAdapter->StaCfg.GroupCipher   = Ndis802_11WEPEnabled;
5711     }
5712     else if ((strcmp(arg, "TKIP") == 0) || (strcmp(arg, "tkip") == 0))
5713     {
5714         if (pAdapter->StaCfg.AuthMode < Ndis802_11AuthModeWPA)
5715             return TRUE;    // do nothing
5716
5717         pAdapter->StaCfg.WepStatus     = Ndis802_11Encryption2Enabled;
5718         pAdapter->StaCfg.PairCipher    = Ndis802_11Encryption2Enabled;
5719             pAdapter->StaCfg.GroupCipher   = Ndis802_11Encryption2Enabled;
5720     }
5721     else if ((strcmp(arg, "AES") == 0) || (strcmp(arg, "aes") == 0))
5722     {
5723         if (pAdapter->StaCfg.AuthMode < Ndis802_11AuthModeWPA)
5724             return TRUE;    // do nothing
5725
5726         pAdapter->StaCfg.WepStatus     = Ndis802_11Encryption3Enabled;
5727         pAdapter->StaCfg.PairCipher    = Ndis802_11Encryption3Enabled;
5728             pAdapter->StaCfg.GroupCipher   = Ndis802_11Encryption3Enabled;
5729     }
5730     else
5731         return FALSE;
5732
5733     pAdapter->StaCfg.OrigWepStatus = pAdapter->StaCfg.WepStatus;
5734
5735     DBGPRINT(RT_DEBUG_TRACE, ("Set_EncrypType_Proc::(EncrypType=%d)\n", pAdapter->StaCfg.WepStatus));
5736
5737     return TRUE;
5738 }
5739
5740 /*
5741     ==========================================================================
5742     Description:
5743         Set Default Key ID
5744     Return:
5745         TRUE if all parameters are OK, FALSE otherwise
5746     ==========================================================================
5747 */
5748 INT Set_DefaultKeyID_Proc(
5749     IN  PRTMP_ADAPTER   pAdapter,
5750     IN  PUCHAR          arg)
5751 {
5752     ULONG                               KeyIdx;
5753
5754     KeyIdx = simple_strtol(arg, 0, 10);
5755     if((KeyIdx >= 1 ) && (KeyIdx <= 4))
5756         pAdapter->StaCfg.DefaultKeyId = (UCHAR) (KeyIdx - 1 );
5757     else
5758         return FALSE;  //Invalid argument
5759
5760     DBGPRINT(RT_DEBUG_TRACE, ("Set_DefaultKeyID_Proc::(DefaultKeyID=%d)\n", pAdapter->StaCfg.DefaultKeyId));
5761
5762     return TRUE;
5763 }
5764
5765 /*
5766     ==========================================================================
5767     Description:
5768         Set WEP KEY1
5769     Return:
5770         TRUE if all parameters are OK, FALSE otherwise
5771     ==========================================================================
5772 */
5773 INT Set_Key1_Proc(
5774     IN  PRTMP_ADAPTER   pAdapter,
5775     IN  PUCHAR          arg)
5776 {
5777     int                                 KeyLen;
5778     int                                 i;
5779     UCHAR                               CipherAlg=CIPHER_WEP64;
5780
5781     if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
5782         return TRUE;    // do nothing
5783
5784     KeyLen = strlen(arg);
5785
5786     switch (KeyLen)
5787     {
5788         case 5: //wep 40 Ascii type
5789             pAdapter->SharedKey[BSS0][0].KeyLen = KeyLen;
5790             memcpy(pAdapter->SharedKey[BSS0][0].Key, arg, KeyLen);
5791             CipherAlg = CIPHER_WEP64;
5792             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key1_Proc::(Key1=%s and type=%s)\n", arg, "Ascii"));
5793             break;
5794         case 10: //wep 40 Hex type
5795             for(i=0; i < KeyLen; i++)
5796             {
5797                 if( !isxdigit(*(arg+i)) )
5798                     return FALSE;  //Not Hex value;
5799             }
5800             pAdapter->SharedKey[BSS0][0].KeyLen = KeyLen / 2 ;
5801             AtoH(arg, pAdapter->SharedKey[BSS0][0].Key, KeyLen / 2);
5802             CipherAlg = CIPHER_WEP64;
5803             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key1_Proc::(Key1=%s and type=%s)\n", arg, "Hex"));
5804             break;
5805         case 13: //wep 104 Ascii type
5806             pAdapter->SharedKey[BSS0][0].KeyLen = KeyLen;
5807             memcpy(pAdapter->SharedKey[BSS0][0].Key, arg, KeyLen);
5808             CipherAlg = CIPHER_WEP128;
5809             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key1_Proc::(Key1=%s and type=%s)\n", arg, "Ascii"));
5810             break;
5811         case 26: //wep 104 Hex type
5812             for(i=0; i < KeyLen; i++)
5813             {
5814                 if( !isxdigit(*(arg+i)) )
5815                     return FALSE;  //Not Hex value;
5816             }
5817             pAdapter->SharedKey[BSS0][0].KeyLen = KeyLen / 2 ;
5818             AtoH(arg, pAdapter->SharedKey[BSS0][0].Key, KeyLen / 2);
5819             CipherAlg = CIPHER_WEP128;
5820             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key1_Proc::(Key1=%s and type=%s)\n", arg, "Hex"));
5821             break;
5822         default: //Invalid argument
5823             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key1_Proc::Invalid argument (=%s)\n", arg));
5824             return FALSE;
5825     }
5826
5827     pAdapter->SharedKey[BSS0][0].CipherAlg = CipherAlg;
5828
5829     // Set keys (into ASIC)
5830     if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
5831         ;   // not support
5832     else    // Old WEP stuff
5833     {
5834         AsicAddSharedKeyEntry(pAdapter,
5835                               0,
5836                               0,
5837                               pAdapter->SharedKey[BSS0][0].CipherAlg,
5838                               pAdapter->SharedKey[BSS0][0].Key,
5839                               NULL,
5840                               NULL);
5841     }
5842
5843     return TRUE;
5844 }
5845 /*
5846     ==========================================================================
5847
5848     Description:
5849         Set WEP KEY2
5850     Return:
5851         TRUE if all parameters are OK, FALSE otherwise
5852     ==========================================================================
5853 */
5854 INT Set_Key2_Proc(
5855     IN  PRTMP_ADAPTER   pAdapter,
5856     IN  PUCHAR          arg)
5857 {
5858     int                                 KeyLen;
5859     int                                 i;
5860     UCHAR                               CipherAlg=CIPHER_WEP64;
5861
5862     if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
5863         return TRUE;    // do nothing
5864
5865     KeyLen = strlen(arg);
5866
5867     switch (KeyLen)
5868     {
5869         case 5: //wep 40 Ascii type
5870             pAdapter->SharedKey[BSS0][1].KeyLen = KeyLen;
5871             memcpy(pAdapter->SharedKey[BSS0][1].Key, arg, KeyLen);
5872             CipherAlg = CIPHER_WEP64;
5873             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key2_Proc::(Key2=%s and type=%s)\n", arg, "Ascii"));
5874             break;
5875         case 10: //wep 40 Hex type
5876             for(i=0; i < KeyLen; i++)
5877             {
5878                 if( !isxdigit(*(arg+i)) )
5879                     return FALSE;  //Not Hex value;
5880             }
5881             pAdapter->SharedKey[BSS0][1].KeyLen = KeyLen / 2 ;
5882             AtoH(arg, pAdapter->SharedKey[BSS0][1].Key, KeyLen / 2);
5883             CipherAlg = CIPHER_WEP64;
5884             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key2_Proc::(Key2=%s and type=%s)\n", arg, "Hex"));
5885             break;
5886         case 13: //wep 104 Ascii type
5887             pAdapter->SharedKey[BSS0][1].KeyLen = KeyLen;
5888             memcpy(pAdapter->SharedKey[BSS0][1].Key, arg, KeyLen);
5889             CipherAlg = CIPHER_WEP128;
5890             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key2_Proc::(Key2=%s and type=%s)\n", arg, "Ascii"));
5891             break;
5892         case 26: //wep 104 Hex type
5893             for(i=0; i < KeyLen; i++)
5894             {
5895                 if( !isxdigit(*(arg+i)) )
5896                     return FALSE;  //Not Hex value;
5897             }
5898             pAdapter->SharedKey[BSS0][1].KeyLen = KeyLen / 2 ;
5899             AtoH(arg, pAdapter->SharedKey[BSS0][1].Key, KeyLen / 2);
5900             CipherAlg = CIPHER_WEP128;
5901             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key2_Proc::(Key2=%s and type=%s)\n", arg, "Hex"));
5902             break;
5903         default: //Invalid argument
5904             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key2_Proc::Invalid argument (=%s)\n", arg));
5905             return FALSE;
5906     }
5907     pAdapter->SharedKey[BSS0][1].CipherAlg = CipherAlg;
5908
5909     // Set keys (into ASIC)
5910     if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
5911         ;   // not support
5912     else    // Old WEP stuff
5913     {
5914         AsicAddSharedKeyEntry(pAdapter,
5915                               0,
5916                               1,
5917                               pAdapter->SharedKey[BSS0][1].CipherAlg,
5918                               pAdapter->SharedKey[BSS0][1].Key,
5919                               NULL,
5920                               NULL);
5921     }
5922
5923     return TRUE;
5924 }
5925 /*
5926     ==========================================================================
5927     Description:
5928         Set WEP KEY3
5929     Return:
5930         TRUE if all parameters are OK, FALSE otherwise
5931     ==========================================================================
5932 */
5933 INT Set_Key3_Proc(
5934     IN  PRTMP_ADAPTER   pAdapter,
5935     IN  PUCHAR          arg)
5936 {
5937     int                                 KeyLen;
5938     int                                 i;
5939     UCHAR                               CipherAlg=CIPHER_WEP64;
5940
5941     if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
5942         return TRUE;    // do nothing
5943
5944     KeyLen = strlen(arg);
5945
5946     switch (KeyLen)
5947     {
5948         case 5: //wep 40 Ascii type
5949             pAdapter->SharedKey[BSS0][2].KeyLen = KeyLen;
5950             memcpy(pAdapter->SharedKey[BSS0][2].Key, arg, KeyLen);
5951             CipherAlg = CIPHER_WEP64;
5952             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key3_Proc::(Key3=%s and type=Ascii)\n", arg));
5953             break;
5954         case 10: //wep 40 Hex type
5955             for(i=0; i < KeyLen; i++)
5956             {
5957                 if( !isxdigit(*(arg+i)) )
5958                     return FALSE;  //Not Hex value;
5959             }
5960             pAdapter->SharedKey[BSS0][2].KeyLen = KeyLen / 2 ;
5961             AtoH(arg, pAdapter->SharedKey[BSS0][2].Key, KeyLen / 2);
5962             CipherAlg = CIPHER_WEP64;
5963             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key3_Proc::(Key3=%s and type=Hex)\n", arg));
5964             break;
5965         case 13: //wep 104 Ascii type
5966             pAdapter->SharedKey[BSS0][2].KeyLen = KeyLen;
5967             memcpy(pAdapter->SharedKey[BSS0][2].Key, arg, KeyLen);
5968             CipherAlg = CIPHER_WEP128;
5969             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key3_Proc::(Key3=%s and type=Ascii)\n", arg));
5970             break;
5971         case 26: //wep 104 Hex type
5972             for(i=0; i < KeyLen; i++)
5973             {
5974                 if( !isxdigit(*(arg+i)) )
5975                     return FALSE;  //Not Hex value;
5976             }
5977             pAdapter->SharedKey[BSS0][2].KeyLen = KeyLen / 2 ;
5978             AtoH(arg, pAdapter->SharedKey[BSS0][2].Key, KeyLen / 2);
5979             CipherAlg = CIPHER_WEP128;
5980             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key3_Proc::(Key3=%s and type=Hex)\n", arg));
5981             break;
5982         default: //Invalid argument
5983             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key3_Proc::Invalid argument (=%s)\n", arg));
5984             return FALSE;
5985     }
5986     pAdapter->SharedKey[BSS0][2].CipherAlg = CipherAlg;
5987
5988     // Set keys (into ASIC)
5989     if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
5990         ;   // not support
5991     else    // Old WEP stuff
5992     {
5993         AsicAddSharedKeyEntry(pAdapter,
5994                               0,
5995                               2,
5996                               pAdapter->SharedKey[BSS0][2].CipherAlg,
5997                               pAdapter->SharedKey[BSS0][2].Key,
5998                               NULL,
5999                               NULL);
6000     }
6001
6002     return TRUE;
6003 }
6004 /*
6005     ==========================================================================
6006     Description:
6007         Set WEP KEY4
6008     Return:
6009         TRUE if all parameters are OK, FALSE otherwise
6010     ==========================================================================
6011 */
6012 INT Set_Key4_Proc(
6013     IN  PRTMP_ADAPTER   pAdapter,
6014     IN  PUCHAR          arg)
6015 {
6016     int                                 KeyLen;
6017     int                                 i;
6018     UCHAR                               CipherAlg=CIPHER_WEP64;
6019
6020     if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
6021         return TRUE;    // do nothing
6022
6023     KeyLen = strlen(arg);
6024
6025     switch (KeyLen)
6026     {
6027         case 5: //wep 40 Ascii type
6028             pAdapter->SharedKey[BSS0][3].KeyLen = KeyLen;
6029             memcpy(pAdapter->SharedKey[BSS0][3].Key, arg, KeyLen);
6030             CipherAlg = CIPHER_WEP64;
6031             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key4_Proc::(Key4=%s and type=%s)\n", arg, "Ascii"));
6032             break;
6033         case 10: //wep 40 Hex type
6034             for(i=0; i < KeyLen; i++)
6035             {
6036                 if( !isxdigit(*(arg+i)) )
6037                     return FALSE;  //Not Hex value;
6038             }
6039             pAdapter->SharedKey[BSS0][3].KeyLen = KeyLen / 2 ;
6040             AtoH(arg, pAdapter->SharedKey[BSS0][3].Key, KeyLen / 2);
6041             CipherAlg = CIPHER_WEP64;
6042             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key4_Proc::(Key4=%s and type=%s)\n", arg, "Hex"));
6043             break;
6044         case 13: //wep 104 Ascii type
6045             pAdapter->SharedKey[BSS0][3].KeyLen = KeyLen;
6046             memcpy(pAdapter->SharedKey[BSS0][3].Key, arg, KeyLen);
6047             CipherAlg = CIPHER_WEP128;
6048             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key4_Proc::(Key4=%s and type=%s)\n", arg, "Ascii"));
6049             break;
6050         case 26: //wep 104 Hex type
6051             for(i=0; i < KeyLen; i++)
6052             {
6053                 if( !isxdigit(*(arg+i)) )
6054                     return FALSE;  //Not Hex value;
6055             }
6056             pAdapter->SharedKey[BSS0][3].KeyLen = KeyLen / 2 ;
6057             AtoH(arg, pAdapter->SharedKey[BSS0][3].Key, KeyLen / 2);
6058             CipherAlg = CIPHER_WEP128;
6059             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key4_Proc::(Key4=%s and type=%s)\n", arg, "Hex"));
6060             break;
6061         default: //Invalid argument
6062             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key4_Proc::Invalid argument (=%s)\n", arg));
6063             return FALSE;
6064     }
6065     pAdapter->SharedKey[BSS0][3].CipherAlg = CipherAlg;
6066
6067     // Set keys (into ASIC)
6068     if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
6069         ;   // not support
6070     else    // Old WEP stuff
6071     {
6072         AsicAddSharedKeyEntry(pAdapter,
6073                               0,
6074                               3,
6075                               pAdapter->SharedKey[BSS0][3].CipherAlg,
6076                               pAdapter->SharedKey[BSS0][3].Key,
6077                               NULL,
6078                               NULL);
6079     }
6080
6081     return TRUE;
6082 }
6083
6084 /*
6085     ==========================================================================
6086     Description:
6087         Set WPA PSK key
6088     Return:
6089         TRUE if all parameters are OK, FALSE otherwise
6090     ==========================================================================
6091 */
6092 INT Set_WPAPSK_Proc(
6093     IN  PRTMP_ADAPTER   pAdapter,
6094     IN  PUCHAR          arg)
6095 {
6096     UCHAR                   keyMaterial[40];
6097
6098     if ((pAdapter->StaCfg.AuthMode != Ndis802_11AuthModeWPAPSK) &&
6099         (pAdapter->StaCfg.AuthMode != Ndis802_11AuthModeWPA2PSK) &&
6100             (pAdapter->StaCfg.AuthMode != Ndis802_11AuthModeWPANone)
6101                 )
6102         return TRUE;    // do nothing
6103
6104     DBGPRINT(RT_DEBUG_TRACE, ("Set_WPAPSK_Proc::(WPAPSK=%s)\n", arg));
6105
6106     NdisZeroMemory(keyMaterial, 40);
6107
6108     if ((strlen(arg) < 8) || (strlen(arg) > 64))
6109     {
6110         DBGPRINT(RT_DEBUG_TRACE, ("Set failed!!(WPAPSK=%s), WPAPSK key-string required 8 ~ 64 characters \n", arg));
6111         return FALSE;
6112     }
6113
6114     if (strlen(arg) == 64)
6115     {
6116         AtoH(arg, keyMaterial, 32);
6117         NdisMoveMemory(pAdapter->StaCfg.PMK, keyMaterial, 32);
6118
6119     }
6120     else
6121     {
6122         PasswordHash((char *)arg, pAdapter->MlmeAux.Ssid, pAdapter->MlmeAux.SsidLen, keyMaterial);
6123         NdisMoveMemory(pAdapter->StaCfg.PMK, keyMaterial, 32);
6124     }
6125
6126
6127
6128     if(pAdapter->StaCfg.BssType == BSS_ADHOC &&
6129        pAdapter->StaCfg.AuthMode == Ndis802_11AuthModeWPANone)
6130     {
6131          pAdapter->StaCfg.WpaState = SS_NOTUSE;
6132     }
6133     else
6134     {
6135         // Start STA supplicant state machine
6136         pAdapter->StaCfg.WpaState = SS_START;
6137     }
6138
6139     return TRUE;
6140 }
6141
6142 /*
6143     ==========================================================================
6144     Description:
6145         Set Power Saving mode
6146     Return:
6147         TRUE if all parameters are OK, FALSE otherwise
6148     ==========================================================================
6149 */
6150 INT Set_PSMode_Proc(
6151     IN  PRTMP_ADAPTER   pAdapter,
6152     IN  PUCHAR          arg)
6153 {
6154     if (pAdapter->StaCfg.BssType == BSS_INFRA)
6155     {
6156         if ((strcmp(arg, "Max_PSP") == 0) ||
6157                         (strcmp(arg, "max_psp") == 0) ||
6158                         (strcmp(arg, "MAX_PSP") == 0))
6159         {
6160             // do NOT turn on PSM bit here, wait until MlmeCheckForPsmChange()
6161             // to exclude certain situations.
6162             if (pAdapter->StaCfg.bWindowsACCAMEnable == FALSE)
6163                 pAdapter->StaCfg.WindowsPowerMode = Ndis802_11PowerModeMAX_PSP;
6164             pAdapter->StaCfg.WindowsBatteryPowerMode = Ndis802_11PowerModeMAX_PSP;
6165             OPSTATUS_SET_FLAG(pAdapter, fOP_STATUS_RECEIVE_DTIM);
6166             pAdapter->StaCfg.DefaultListenCount = 5;
6167
6168         }
6169         else if ((strcmp(arg, "Fast_PSP") == 0) ||
6170                                  (strcmp(arg, "fast_psp") == 0) ||
6171                  (strcmp(arg, "FAST_PSP") == 0))
6172         {
6173             // do NOT turn on PSM bit here, wait until MlmeCheckForPsmChange()
6174             // to exclude certain situations.
6175             OPSTATUS_SET_FLAG(pAdapter, fOP_STATUS_RECEIVE_DTIM);
6176             if (pAdapter->StaCfg.bWindowsACCAMEnable == FALSE)
6177                 pAdapter->StaCfg.WindowsPowerMode = Ndis802_11PowerModeFast_PSP;
6178             pAdapter->StaCfg.WindowsBatteryPowerMode = Ndis802_11PowerModeFast_PSP;
6179             pAdapter->StaCfg.DefaultListenCount = 3;
6180         }
6181         else if ((strcmp(arg, "Legacy_PSP") == 0) ||
6182                  (strcmp(arg, "legacy_psp") == 0) ||
6183                  (strcmp(arg, "LEGACY_PSP") == 0))
6184         {
6185             // do NOT turn on PSM bit here, wait until MlmeCheckForPsmChange()
6186             // to exclude certain situations.
6187             OPSTATUS_SET_FLAG(pAdapter, fOP_STATUS_RECEIVE_DTIM);
6188             if (pAdapter->StaCfg.bWindowsACCAMEnable == FALSE)
6189                 pAdapter->StaCfg.WindowsPowerMode = Ndis802_11PowerModeLegacy_PSP;
6190             pAdapter->StaCfg.WindowsBatteryPowerMode = Ndis802_11PowerModeLegacy_PSP;
6191             pAdapter->StaCfg.DefaultListenCount = 3;
6192         }
6193         else
6194         {
6195             //Default Ndis802_11PowerModeCAM
6196             // clear PSM bit immediately
6197             MlmeSetPsmBit(pAdapter, PWR_ACTIVE);
6198             OPSTATUS_SET_FLAG(pAdapter, fOP_STATUS_RECEIVE_DTIM);
6199             if (pAdapter->StaCfg.bWindowsACCAMEnable == FALSE)
6200                 pAdapter->StaCfg.WindowsPowerMode = Ndis802_11PowerModeCAM;
6201             pAdapter->StaCfg.WindowsBatteryPowerMode = Ndis802_11PowerModeCAM;
6202         }
6203
6204         DBGPRINT(RT_DEBUG_TRACE, ("Set_PSMode_Proc::(PSMode=%ld)\n", pAdapter->StaCfg.WindowsPowerMode));
6205     }
6206     else
6207         return FALSE;
6208
6209
6210     return TRUE;
6211 }
6212
6213 /*
6214     ==========================================================================
6215     Description:
6216         Set WpaSupport flag.
6217     Value:
6218         0: Driver ignore wpa_supplicant.
6219         1: wpa_supplicant initiates scanning and AP selection.
6220         2: driver takes care of scanning, AP selection, and IEEE 802.11 association parameters.
6221     Return:
6222         TRUE if all parameters are OK, FALSE otherwise
6223     ==========================================================================
6224 */
6225 INT Set_Wpa_Support(
6226     IN  PRTMP_ADAPTER   pAd,
6227         IN      PUCHAR                  arg)
6228 {
6229
6230     if ( simple_strtol(arg, 0, 10) == 0)
6231         pAd->StaCfg.WpaSupplicantUP = WPA_SUPPLICANT_DISABLE;
6232     else if ( simple_strtol(arg, 0, 10) == 1)
6233         pAd->StaCfg.WpaSupplicantUP = WPA_SUPPLICANT_ENABLE;
6234     else if ( simple_strtol(arg, 0, 10) == 2)
6235         pAd->StaCfg.WpaSupplicantUP = WPA_SUPPLICANT_ENABLE_WITH_WEB_UI;
6236     else
6237         pAd->StaCfg.WpaSupplicantUP = WPA_SUPPLICANT_DISABLE;
6238
6239     DBGPRINT(RT_DEBUG_TRACE, ("Set_Wpa_Support::(WpaSupplicantUP=%d)\n", pAd->StaCfg.WpaSupplicantUP));
6240
6241     return TRUE;
6242 }
6243
6244 #ifdef DBG
6245 /*
6246     ==========================================================================
6247     Description:
6248         Read / Write MAC
6249     Arguments:
6250         pAdapter                    Pointer to our adapter
6251         wrq                         Pointer to the ioctl argument
6252
6253     Return Value:
6254         None
6255
6256     Note:
6257         Usage:
6258                1.) iwpriv ra0 mac 0        ==> read MAC where Addr=0x0
6259                2.) iwpriv ra0 mac 0=12     ==> write MAC where Addr=0x0, value=12
6260     ==========================================================================
6261 */
6262 VOID RTMPIoctlMAC(
6263         IN      PRTMP_ADAPTER   pAdapter,
6264         IN      struct iwreq    *wrq)
6265 {
6266         CHAR                            *this_char;
6267         CHAR                            *value;
6268         INT                                     j = 0, k = 0;
6269         CHAR                            msg[1024];
6270         CHAR                            arg[255];
6271         ULONG                           macAddr = 0;
6272         UCHAR                           temp[16], temp2[16];
6273         UINT32                          macValue = 0;
6274         INT                                     Status;
6275 #ifdef RT30xx
6276         BOOLEAN                         bIsPrintAllMAC = FALSE;
6277 #endif
6278
6279         memset(msg, 0x00, 1024);
6280         if (wrq->u.data.length > 1) //No parameters.
6281         {
6282             Status = copy_from_user(arg, wrq->u.data.pointer, (wrq->u.data.length > 255) ? 255 : wrq->u.data.length);
6283                 sprintf(msg, "\n");
6284
6285                 //Parsing Read or Write
6286             this_char = arg;
6287                 if (!*this_char)
6288                         goto next;
6289
6290                 if ((value = rtstrchr(this_char, '=')) != NULL)
6291                         *value++ = 0;
6292
6293                 if (!value || !*value)
6294                 { //Read
6295                         // Sanity check
6296                         if(strlen(this_char) > 4)
6297                                 goto next;
6298
6299                         j = strlen(this_char);
6300                         while(j-- > 0)
6301                         {
6302                                 if(this_char[j] > 'f' || this_char[j] < '0')
6303                                         return;
6304                         }
6305
6306                         // Mac Addr
6307                         k = j = strlen(this_char);
6308                         while(j-- > 0)
6309                         {
6310                                 this_char[4-k+j] = this_char[j];
6311                         }
6312
6313                         while(k < 4)
6314                                 this_char[3-k++]='0';
6315                         this_char[4]='\0';
6316
6317                         if(strlen(this_char) == 4)
6318                         {
6319                                 AtoH(this_char, temp, 2);
6320                                 macAddr = *temp*256 + temp[1];
6321                                 if (macAddr < 0xFFFF)
6322                                 {
6323                                         RTMP_IO_READ32(pAdapter, macAddr, &macValue);
6324                                         DBGPRINT(RT_DEBUG_TRACE, ("MacAddr=%lx, MacValue=%x\n", macAddr, macValue));
6325                                         sprintf(msg+strlen(msg), "[0x%08lX]:%08X  ", macAddr , macValue);
6326                                 }
6327                                 else
6328 #ifndef RT30xx
6329                                 {//Invalid parametes, so default printk all bbp
6330 #endif
6331 #ifdef RT30xx
6332                                 {//Invalid parametes, so default printk all mac
6333                                         bIsPrintAllMAC = TRUE;
6334 #endif
6335                                         goto next;
6336                                 }
6337                         }
6338                 }
6339                 else
6340                 { //Write
6341                         memcpy(&temp2, value, strlen(value));
6342                         temp2[strlen(value)] = '\0';
6343
6344                         // Sanity check
6345                         if((strlen(this_char) > 4) || strlen(temp2) > 8)
6346                                 goto next;
6347
6348                         j = strlen(this_char);
6349                         while(j-- > 0)
6350                         {
6351                                 if(this_char[j] > 'f' || this_char[j] < '0')
6352                                         return;
6353                         }
6354
6355                         j = strlen(temp2);
6356                         while(j-- > 0)
6357                         {
6358                                 if(temp2[j] > 'f' || temp2[j] < '0')
6359                                         return;
6360                         }
6361
6362                         //MAC Addr
6363                         k = j = strlen(this_char);
6364                         while(j-- > 0)
6365                         {
6366                                 this_char[4-k+j] = this_char[j];
6367                         }
6368
6369                         while(k < 4)
6370                                 this_char[3-k++]='0';
6371                         this_char[4]='\0';
6372
6373                         //MAC value
6374                         k = j = strlen(temp2);
6375                         while(j-- > 0)
6376                         {
6377                                 temp2[8-k+j] = temp2[j];
6378                         }
6379
6380                         while(k < 8)
6381                                 temp2[7-k++]='0';
6382                         temp2[8]='\0';
6383
6384                         {
6385                                 AtoH(this_char, temp, 2);
6386                                 macAddr = *temp*256 + temp[1];
6387
6388                                 AtoH(temp2, temp, 4);
6389                                 macValue = *temp*256*256*256 + temp[1]*256*256 + temp[2]*256 + temp[3];
6390
6391                                 // debug mode
6392                                 if (macAddr == (HW_DEBUG_SETTING_BASE + 4))
6393                                 {
6394                                         // 0x2bf4: byte0 non-zero: enable R17 tuning, 0: disable R17 tuning
6395                     if (macValue & 0x000000ff)
6396                     {
6397                         pAdapter->BbpTuning.bEnable = TRUE;
6398                         DBGPRINT(RT_DEBUG_TRACE,("turn on R17 tuning\n"));
6399                     }
6400                     else
6401                     {
6402                         UCHAR R66;
6403                         pAdapter->BbpTuning.bEnable = FALSE;
6404                         R66 = 0x26 + GET_LNA_GAIN(pAdapter);
6405                                                 RTMP_BBP_IO_WRITE8_BY_REG_ID(pAdapter, BBP_R66, (0x26 + GET_LNA_GAIN(pAdapter)));
6406                         DBGPRINT(RT_DEBUG_TRACE,("turn off R17 tuning, restore to 0x%02x\n", R66));
6407                     }
6408                                         return;
6409                                 }
6410
6411                                 DBGPRINT(RT_DEBUG_TRACE, ("MacAddr=%02lx, MacValue=0x%x\n", macAddr, macValue));
6412
6413                                 RTMP_IO_WRITE32(pAdapter, macAddr, macValue);
6414                                 sprintf(msg+strlen(msg), "[0x%08lX]:%08X  ", macAddr, macValue);
6415                         }
6416                 }
6417         }
6418 #ifdef RT30xx
6419         else
6420                 bIsPrintAllMAC = TRUE;
6421 #endif
6422 next:
6423 #ifdef RT30xx
6424         if (bIsPrintAllMAC)
6425         {
6426                 struct file             *file_w;
6427                 PCHAR                   fileName = "MacDump.txt";
6428                 mm_segment_t    orig_fs;
6429
6430                 orig_fs = get_fs();
6431                 set_fs(KERNEL_DS);
6432
6433                 // open file
6434                 file_w = filp_open(fileName, O_WRONLY|O_CREAT, 0);
6435                 if (IS_ERR(file_w))
6436                 {
6437                         DBGPRINT(RT_DEBUG_TRACE, ("-->2) %s: Error %ld opening %s\n", __func__, -PTR_ERR(file_w), fileName));
6438                 }
6439                 else
6440                 {
6441                         if (file_w->f_op && file_w->f_op->write)
6442                         {
6443                                 file_w->f_pos = 0;
6444                                 macAddr = 0x1000;
6445
6446                                 while (macAddr <= 0x1800)
6447                                 {
6448                                         RTMP_IO_READ32(pAdapter, macAddr, &macValue);
6449                                         sprintf(msg, "%08lx = %08X\n", macAddr, macValue);
6450
6451                                         // write data to file
6452                                         file_w->f_op->write(file_w, msg, strlen(msg), &file_w->f_pos);
6453
6454                                         printk("%s", msg);
6455                                         macAddr += 4;
6456                                 }
6457                                 sprintf(msg, "\nDump all MAC values to %s\n", fileName);
6458                         }
6459                         filp_close(file_w, NULL);
6460                 }
6461                 set_fs(orig_fs);
6462         }
6463 #endif /* RT30xx */
6464         if(strlen(msg) == 1)
6465                 sprintf(msg+strlen(msg), "===>Error command format!");
6466
6467         // Copy the information into the user buffer
6468         wrq->u.data.length = strlen(msg);
6469         Status = copy_to_user(wrq->u.data.pointer, msg, wrq->u.data.length);
6470
6471         DBGPRINT(RT_DEBUG_TRACE, ("<==RTMPIoctlMAC\n\n"));
6472 }
6473
6474 /*
6475     ==========================================================================
6476     Description:
6477         Read / Write E2PROM
6478     Arguments:
6479         pAdapter                    Pointer to our adapter
6480         wrq                         Pointer to the ioctl argument
6481
6482     Return Value:
6483         None
6484
6485     Note:
6486         Usage:
6487                1.) iwpriv ra0 e2p 0             ==> read E2PROM where Addr=0x0
6488                2.) iwpriv ra0 e2p 0=1234    ==> write E2PROM where Addr=0x0, value=1234
6489     ==========================================================================
6490 */
6491 VOID RTMPIoctlE2PROM(
6492         IN      PRTMP_ADAPTER   pAdapter,
6493         IN      struct iwreq    *wrq)
6494 {
6495         CHAR                            *this_char;
6496         CHAR                            *value;
6497         INT                                     j = 0, k = 0;
6498         CHAR                            msg[1024];
6499         CHAR                            arg[255];
6500         USHORT                          eepAddr = 0;
6501         UCHAR                           temp[16], temp2[16];
6502         USHORT                          eepValue;
6503         int                                     Status;
6504 #ifdef RT30xx
6505         BOOLEAN                         bIsPrintAllE2P = FALSE;
6506 #endif
6507
6508         memset(msg, 0x00, 1024);
6509         if (wrq->u.data.length > 1) //No parameters.
6510         {
6511             Status = copy_from_user(arg, wrq->u.data.pointer, (wrq->u.data.length > 255) ? 255 : wrq->u.data.length);
6512                 sprintf(msg, "\n");
6513
6514             //Parsing Read or Write
6515                 this_char = arg;
6516
6517
6518                 if (!*this_char)
6519                         goto next;
6520
6521                 if ((value = rtstrchr(this_char, '=')) != NULL)
6522                         *value++ = 0;
6523
6524                 if (!value || !*value)
6525                 { //Read
6526
6527                         // Sanity check
6528                         if(strlen(this_char) > 4)
6529                                 goto next;
6530
6531                         j = strlen(this_char);
6532                         while(j-- > 0)
6533                         {
6534                                 if(this_char[j] > 'f' || this_char[j] < '0')
6535                                         return;
6536                         }
6537
6538                         // E2PROM addr
6539                         k = j = strlen(this_char);
6540                         while(j-- > 0)
6541                         {
6542                                 this_char[4-k+j] = this_char[j];
6543                         }
6544
6545                         while(k < 4)
6546                                 this_char[3-k++]='0';
6547                         this_char[4]='\0';
6548
6549                         if(strlen(this_char) == 4)
6550                         {
6551                                 AtoH(this_char, temp, 2);
6552                                 eepAddr = *temp*256 + temp[1];
6553                                 if (eepAddr < 0xFFFF)
6554                                 {
6555                                         RT28xx_EEPROM_READ16(pAdapter, eepAddr, eepValue);
6556                                         sprintf(msg+strlen(msg), "[0x%04X]:0x%04X  ", eepAddr , eepValue);
6557                                 }
6558                                 else
6559                                 {//Invalid parametes, so default printk all bbp
6560 #ifdef RT30xx
6561                                         bIsPrintAllE2P = TRUE;
6562 #endif
6563                                         goto next;
6564                                 }
6565                         }
6566                 }
6567                 else
6568                 { //Write
6569                         memcpy(&temp2, value, strlen(value));
6570                         temp2[strlen(value)] = '\0';
6571
6572                         // Sanity check
6573                         if((strlen(this_char) > 4) || strlen(temp2) > 8)
6574                                 goto next;
6575
6576                         j = strlen(this_char);
6577                         while(j-- > 0)
6578                         {
6579                                 if(this_char[j] > 'f' || this_char[j] < '0')
6580                                         return;
6581                         }
6582                         j = strlen(temp2);
6583                         while(j-- > 0)
6584                         {
6585                                 if(temp2[j] > 'f' || temp2[j] < '0')
6586                                         return;
6587                         }
6588
6589                         //MAC Addr
6590                         k = j = strlen(this_char);
6591                         while(j-- > 0)
6592                         {
6593                                 this_char[4-k+j] = this_char[j];
6594                         }
6595
6596                         while(k < 4)
6597                                 this_char[3-k++]='0';
6598                         this_char[4]='\0';
6599
6600                         //MAC value
6601                         k = j = strlen(temp2);
6602                         while(j-- > 0)
6603                         {
6604                                 temp2[4-k+j] = temp2[j];
6605                         }
6606
6607                         while(k < 4)
6608                                 temp2[3-k++]='0';
6609                         temp2[4]='\0';
6610
6611                         AtoH(this_char, temp, 2);
6612                         eepAddr = *temp*256 + temp[1];
6613
6614                         AtoH(temp2, temp, 2);
6615                         eepValue = *temp*256 + temp[1];
6616
6617                         RT28xx_EEPROM_WRITE16(pAdapter, eepAddr, eepValue);
6618                         sprintf(msg+strlen(msg), "[0x%02X]:%02X  ", eepAddr, eepValue);
6619                 }
6620         }
6621 #ifdef RT30xx
6622         else
6623                 bIsPrintAllE2P = TRUE;
6624 #endif
6625 next:
6626 #ifdef RT30xx
6627         if (bIsPrintAllE2P)
6628         {
6629                 struct file             *file_w;
6630                 PCHAR                   fileName = "EEPROMDump.txt";
6631                 mm_segment_t    orig_fs;
6632
6633                 orig_fs = get_fs();
6634                 set_fs(KERNEL_DS);
6635
6636                 // open file
6637                 file_w = filp_open(fileName, O_WRONLY|O_CREAT, 0);
6638                 if (IS_ERR(file_w))
6639                 {
6640                         DBGPRINT(RT_DEBUG_TRACE, ("-->2) %s: Error %ld opening %s\n", __func__, -PTR_ERR(file_w), fileName));
6641                 }
6642                 else
6643                 {
6644                         if (file_w->f_op && file_w->f_op->write)
6645                         {
6646                                 file_w->f_pos = 0;
6647                                 eepAddr = 0x00;
6648
6649                                 while (eepAddr <= 0xFE)
6650                                 {
6651                                         RT28xx_EEPROM_READ16(pAdapter, eepAddr, eepValue);
6652                                         sprintf(msg, "%08x = %04x\n", eepAddr , eepValue);
6653
6654                                         // write data to file
6655                                         file_w->f_op->write(file_w, msg, strlen(msg), &file_w->f_pos);
6656
6657                                         printk("%s", msg);
6658                                         eepAddr += 2;
6659                                 }
6660                                 sprintf(msg, "\nDump all EEPROM values to %s\n", fileName);
6661                         }
6662                         filp_close(file_w, NULL);
6663                 }
6664                 set_fs(orig_fs);
6665         }
6666 #endif /* RT30xx */
6667         if(strlen(msg) == 1)
6668                 sprintf(msg+strlen(msg), "===>Error command format!");
6669
6670
6671         // Copy the information into the user buffer
6672         wrq->u.data.length = strlen(msg);
6673         Status = copy_to_user(wrq->u.data.pointer, msg, wrq->u.data.length);
6674
6675         DBGPRINT(RT_DEBUG_TRACE, ("<==RTMPIoctlE2PROM\n"));
6676 }
6677 #ifdef RT30xx
6678 /*
6679     ==========================================================================
6680     Description:
6681         Read / Write RF register
6682 Arguments:
6683     pAdapter                    Pointer to our adapter
6684     wrq                         Pointer to the ioctl argument
6685
6686     Return Value:
6687         None
6688
6689     Note:
6690         Usage:
6691                1.) iwpriv ra0 rf                ==> read all RF registers
6692                2.) iwpriv ra0 rf 1              ==> read RF where RegID=1
6693                3.) iwpriv ra0 rf 1=10               ==> write RF R1=0x10
6694     ==========================================================================
6695 */
6696 VOID RTMPIoctlRF(
6697         IN      PRTMP_ADAPTER   pAdapter,
6698         IN      struct iwreq    *wrq)
6699 {
6700         CHAR                            *this_char;
6701         CHAR                            *value;
6702         UCHAR                           regRF = 0;
6703         CHAR                            msg[2048];
6704         CHAR                            arg[255];
6705         INT                                     rfId;
6706         LONG                            rfValue;
6707         int                                     Status;
6708         BOOLEAN                         bIsPrintAllRF = FALSE;
6709
6710
6711         memset(msg, 0x00, 2048);
6712         if (wrq->u.data.length > 1) //No parameters.
6713         {
6714             Status = copy_from_user(arg, wrq->u.data.pointer, (wrq->u.data.length > 255) ? 255 : wrq->u.data.length);
6715                 sprintf(msg, "\n");
6716
6717             //Parsing Read or Write
6718                 this_char = arg;
6719                 if (!*this_char)
6720                         goto next;
6721
6722                 if ((value = strchr(this_char, '=')) != NULL)
6723                         *value++ = 0;
6724
6725                 if (!value || !*value)
6726                 { //Read
6727                         if (sscanf(this_char, "%d", &(rfId)) == 1)
6728                         {
6729                                 if (rfId <= 31)
6730                                 {
6731                                         // In RT2860 ATE mode, we do not load 8051 firmware.
6732                                             //We must access RF directly.
6733                     // For RT2870 ATE mode, ATE_RF_IO_WRITE8(/READ8)_BY_REG_ID are redefined.
6734                                         // according to Andy, Gary, David require.
6735                                         // the command rf shall read rf register directly for dubug.
6736                                         // BBP_IO_READ8_BY_REG_ID(pAdapter, bbpId, &regBBP);
6737                                         RT30xxReadRFRegister(pAdapter, rfId, &regRF);
6738
6739                                         sprintf(msg+strlen(msg), "R%02d[0x%02x]:%02X  ", rfId, rfId*2, regRF);
6740                                 }
6741                                 else
6742                                 {//Invalid parametes, so default printk all RF
6743                                         bIsPrintAllRF = TRUE;
6744                                         goto next;
6745                                 }
6746                         }
6747                         else
6748                         { //Invalid parametes, so default printk all RF
6749                                 bIsPrintAllRF = TRUE;
6750                                 goto next;
6751                         }
6752                 }
6753                 else
6754                 { //Write
6755                         if ((sscanf(this_char, "%d", &(rfId)) == 1) && (sscanf(value, "%lx", &(rfValue)) == 1))
6756                         {
6757                                 if (rfId <= 31)
6758                                 {
6759                                         // In RT2860 ATE mode, we do not load 8051 firmware.
6760                                         // We should access RF registers directly.
6761                     // For RT2870 ATE mode, ATE_RF_IO_WRITE8/READ8_BY_REG_ID are redefined.
6762                                                 {
6763                                                         // according to Andy, Gary, David require.
6764                                                         // the command RF shall read/write RF register directly for dubug.
6765                                                         //BBP_IO_READ8_BY_REG_ID(pAdapter, bbpId, &regBBP);
6766                                                         //BBP_IO_WRITE8_BY_REG_ID(pAdapter, (UCHAR)bbpId,(UCHAR) bbpValue);
6767                                                         RT30xxReadRFRegister(pAdapter, rfId, &regRF);
6768                                                         RT30xxWriteRFRegister(pAdapter, (UCHAR)rfId,(UCHAR) rfValue);
6769                                                         //Read it back for showing
6770                                                         //BBP_IO_READ8_BY_REG_ID(pAdapter, bbpId, &regBBP);
6771                                                         RT30xxReadRFRegister(pAdapter, rfId, &regRF);
6772                                                         sprintf(msg+strlen(msg), "R%02d[0x%02X]:%02X\n", rfId, rfId*2, regRF);
6773                                                 }
6774                                 }
6775                                 else
6776                                 {//Invalid parametes, so default printk all RF
6777                                         bIsPrintAllRF = TRUE;
6778                                 }
6779                         }
6780                         else
6781                         { //Invalid parametes, so default printk all RF
6782                                 bIsPrintAllRF = TRUE;
6783                         }
6784                 }
6785         }
6786         else
6787                 bIsPrintAllRF = TRUE;
6788 next:
6789         if (bIsPrintAllRF)
6790         {
6791                 memset(msg, 0x00, 2048);
6792                 sprintf(msg, "\n");
6793                 for (rfId = 0; rfId <= 31; rfId++)
6794                 {
6795                         // according to Andy, Gary, David require.
6796                         // the command RF shall read/write RF register directly for dubug.
6797                         RT30xxReadRFRegister(pAdapter, rfId, &regRF);
6798                         sprintf(msg+strlen(msg), "%03d = %02X\n", rfId, regRF);
6799                 }
6800                 // Copy the information into the user buffer
6801                 DBGPRINT(RT_DEBUG_TRACE, ("strlen(msg)=%d\n", (UINT32)strlen(msg)));
6802                 wrq->u.data.length = strlen(msg);
6803                 if (copy_to_user(wrq->u.data.pointer, msg, wrq->u.data.length))
6804                 {
6805                         DBGPRINT(RT_DEBUG_TRACE, ("%s: copy_to_user() fail\n", __func__));
6806                 }
6807         }
6808         else
6809         {
6810                 if(strlen(msg) == 1)
6811                         sprintf(msg+strlen(msg), "===>Error command format!");
6812
6813                 DBGPRINT(RT_DEBUG_TRACE, ("copy to user [msg=%s]\n", msg));
6814                 // Copy the information into the user buffer
6815                 DBGPRINT(RT_DEBUG_TRACE, ("strlen(msg) =%d\n", (UINT32)strlen(msg)));
6816
6817                 // Copy the information into the user buffer
6818                 wrq->u.data.length = strlen(msg);
6819                 Status = copy_to_user(wrq->u.data.pointer, msg, wrq->u.data.length);
6820         }
6821
6822         DBGPRINT(RT_DEBUG_TRACE, ("<==RTMPIoctlRF\n\n"));
6823 }
6824 #endif // RT30xx //
6825 #endif // DBG //
6826
6827
6828
6829
6830 INT Set_TGnWifiTest_Proc(
6831     IN  PRTMP_ADAPTER   pAd,
6832     IN  PUCHAR          arg)
6833 {
6834     if (simple_strtol(arg, 0, 10) == 0)
6835         pAd->StaCfg.bTGnWifiTest = FALSE;
6836     else
6837         pAd->StaCfg.bTGnWifiTest = TRUE;
6838
6839     DBGPRINT(RT_DEBUG_TRACE, ("IF Set_TGnWifiTest_Proc::(bTGnWifiTest=%d)\n", pAd->StaCfg.bTGnWifiTest));
6840         return TRUE;
6841 }
6842
6843 INT Set_LongRetryLimit_Proc(
6844         IN      PRTMP_ADAPTER   pAdapter,
6845         IN      PUCHAR                  arg)
6846 {
6847         TX_RTY_CFG_STRUC        tx_rty_cfg;
6848         UCHAR                           LongRetryLimit = (UCHAR)simple_strtol(arg, 0, 10);
6849
6850         RTMP_IO_READ32(pAdapter, TX_RTY_CFG, &tx_rty_cfg.word);
6851         tx_rty_cfg.field.LongRtyLimit = LongRetryLimit;
6852         RTMP_IO_WRITE32(pAdapter, TX_RTY_CFG, tx_rty_cfg.word);
6853         DBGPRINT(RT_DEBUG_TRACE, ("IF Set_LongRetryLimit_Proc::(tx_rty_cfg=0x%x)\n", tx_rty_cfg.word));
6854         return TRUE;
6855 }
6856
6857 INT Set_ShortRetryLimit_Proc(
6858         IN      PRTMP_ADAPTER   pAdapter,
6859         IN      PUCHAR                  arg)
6860 {
6861         TX_RTY_CFG_STRUC        tx_rty_cfg;
6862         UCHAR                           ShortRetryLimit = (UCHAR)simple_strtol(arg, 0, 10);
6863
6864         RTMP_IO_READ32(pAdapter, TX_RTY_CFG, &tx_rty_cfg.word);
6865         tx_rty_cfg.field.ShortRtyLimit = ShortRetryLimit;
6866         RTMP_IO_WRITE32(pAdapter, TX_RTY_CFG, tx_rty_cfg.word);
6867         DBGPRINT(RT_DEBUG_TRACE, ("IF Set_ShortRetryLimit_Proc::(tx_rty_cfg=0x%x)\n", tx_rty_cfg.word));
6868         return TRUE;
6869 }
6870
6871 #if !defined(RT2860) && !defined(RT30xx)
6872 INT     Show_Adhoc_MacTable_Proc(
6873         IN      PRTMP_ADAPTER   pAd,
6874         IN      PCHAR                   extra)
6875 {
6876         INT i;
6877
6878         sprintf(extra, "\n");
6879
6880         sprintf(extra + strlen(extra), "HT Operating Mode : %d\n", pAd->CommonCfg.AddHTInfo.AddHtInfo2.OperaionMode);
6881
6882         sprintf(extra + strlen(extra), "\n%-19s%-4s%-4s%-7s%-7s%-7s%-10s%-6s%-6s%-6s%-6s\n",
6883                         "MAC", "AID", "BSS", "RSSI0", "RSSI1", "RSSI2", "PhMd", "BW", "MCS", "SGI", "STBC");
6884
6885         for (i=1; i<MAX_LEN_OF_MAC_TABLE; i++)
6886         {
6887                 PMAC_TABLE_ENTRY pEntry = &pAd->MacTab.Content[i];
6888
6889                 if (strlen(extra) > (IW_PRIV_SIZE_MASK - 30))
6890                     break;
6891                 if ((pEntry->ValidAsCLI || pEntry->ValidAsApCli) && (pEntry->Sst == SST_ASSOC))
6892                 {
6893                         sprintf(extra + strlen(extra), "%02X:%02X:%02X:%02X:%02X:%02X  ",
6894                                 pEntry->Addr[0], pEntry->Addr[1], pEntry->Addr[2],
6895                                 pEntry->Addr[3], pEntry->Addr[4], pEntry->Addr[5]);
6896                         sprintf(extra + strlen(extra), "%-4d", (int)pEntry->Aid);
6897                         sprintf(extra + strlen(extra), "%-4d", (int)pEntry->apidx);
6898                         sprintf(extra + strlen(extra), "%-7d", pEntry->RssiSample.AvgRssi0);
6899                         sprintf(extra + strlen(extra), "%-7d", pEntry->RssiSample.AvgRssi1);
6900                         sprintf(extra + strlen(extra), "%-7d", pEntry->RssiSample.AvgRssi2);
6901                         sprintf(extra + strlen(extra), "%-10s", GetPhyMode(pEntry->HTPhyMode.field.MODE));
6902                         sprintf(extra + strlen(extra), "%-6s", GetBW(pEntry->HTPhyMode.field.BW));
6903                         sprintf(extra + strlen(extra), "%-6d", pEntry->HTPhyMode.field.MCS);
6904                         sprintf(extra + strlen(extra), "%-6d", pEntry->HTPhyMode.field.ShortGI);
6905                         sprintf(extra + strlen(extra), "%-6d", pEntry->HTPhyMode.field.STBC);
6906                         sprintf(extra + strlen(extra), "%-10d, %d, %d%%\n", pEntry->DebugFIFOCount, pEntry->DebugTxCount,
6907                                                 (pEntry->DebugTxCount) ? ((pEntry->DebugTxCount-pEntry->DebugFIFOCount)*100/pEntry->DebugTxCount) : 0);
6908                         sprintf(extra, "%s\n", extra);
6909                 }
6910         }
6911
6912         return TRUE;
6913 }
6914 #endif /* RT2870 */