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