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