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