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