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