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