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