e419fb1d5ee4f74dd4eed4857c7a3db5983c796f
[pandora-kernel.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 { RTPRIV_IOCTL_STATISTICS,
105   0, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK,
106   "stat"},
107 { RTPRIV_IOCTL_GSITESURVEY,
108   0, IW_PRIV_TYPE_CHAR | 1024,
109   "get_site_survey"},
110
111
112 };
113
114 static __s32 ralinkrate[] =
115         {2,  4,   11,  22, // CCK
116         12, 18,   24,  36, 48, 72, 96, 108, // OFDM
117         13, 26,   39,  52,  78, 104, 117, 130, 26,  52,  78, 104, 156, 208, 234, 260, // 20MHz, 800ns GI, MCS: 0 ~ 15
118         39, 78,  117, 156, 234, 312, 351, 390,                                                                            // 20MHz, 800ns GI, MCS: 16 ~ 23
119         27, 54,   81, 108, 162, 216, 243, 270, 54, 108, 162, 216, 324, 432, 486, 540, // 40MHz, 800ns GI, MCS: 0 ~ 15
120         81, 162, 243, 324, 486, 648, 729, 810,                                                                            // 40MHz, 800ns GI, MCS: 16 ~ 23
121         14, 29,   43,  57,  87, 115, 130, 144, 29, 59,   87, 115, 173, 230, 260, 288, // 20MHz, 400ns GI, MCS: 0 ~ 15
122         43, 87,  130, 173, 260, 317, 390, 433,                                                                            // 20MHz, 400ns GI, MCS: 16 ~ 23
123         30, 60,   90, 120, 180, 240, 270, 300, 60, 120, 180, 240, 360, 480, 540, 600, // 40MHz, 400ns GI, MCS: 0 ~ 15
124         90, 180, 270, 360, 540, 720, 810, 900};
125
126
127
128 INT Set_SSID_Proc(
129     IN  PRTMP_ADAPTER   pAdapter,
130     IN  PSTRING          arg);
131
132 #ifdef WMM_SUPPORT
133 INT     Set_WmmCapable_Proc(
134         IN      PRTMP_ADAPTER   pAd,
135         IN      PSTRING                 arg);
136 #endif
137
138 INT Set_NetworkType_Proc(
139     IN  PRTMP_ADAPTER   pAdapter,
140     IN  PSTRING          arg);
141
142 INT Set_AuthMode_Proc(
143     IN  PRTMP_ADAPTER   pAdapter,
144     IN  PSTRING          arg);
145
146 INT Set_EncrypType_Proc(
147     IN  PRTMP_ADAPTER   pAdapter,
148     IN  PSTRING          arg);
149
150 INT Set_DefaultKeyID_Proc(
151     IN  PRTMP_ADAPTER   pAdapter,
152     IN  PSTRING          arg);
153
154 INT Set_Key1_Proc(
155     IN  PRTMP_ADAPTER   pAdapter,
156     IN  PSTRING          arg);
157
158 INT Set_Key2_Proc(
159     IN  PRTMP_ADAPTER   pAdapter,
160     IN  PSTRING          arg);
161
162 INT Set_Key3_Proc(
163     IN  PRTMP_ADAPTER   pAdapter,
164     IN  PSTRING          arg);
165
166 INT Set_Key4_Proc(
167     IN  PRTMP_ADAPTER   pAdapter,
168     IN  PSTRING          arg);
169
170 INT Set_WPAPSK_Proc(
171     IN  PRTMP_ADAPTER   pAdapter,
172     IN  PSTRING          arg);
173
174
175 INT Set_PSMode_Proc(
176     IN  PRTMP_ADAPTER   pAdapter,
177     IN  PSTRING          arg);
178
179 #ifdef RT3090
180 INT Set_PCIePSLevel_Proc(
181 IN  PRTMP_ADAPTER   pAdapter,
182 IN  PUCHAR          arg);
183 #endif // RT3090 //
184 #ifdef WPA_SUPPLICANT_SUPPORT
185 INT Set_Wpa_Support(
186     IN  PRTMP_ADAPTER   pAd,
187         IN      PSTRING                 arg);
188 #endif // WPA_SUPPLICANT_SUPPORT //
189
190 NDIS_STATUS RTMPWPANoneAddKeyProc(
191     IN  PRTMP_ADAPTER   pAd,
192     IN  PVOID                   pBuf);
193
194 INT Set_FragTest_Proc(
195     IN  PRTMP_ADAPTER   pAdapter,
196     IN  PSTRING          arg);
197
198 #ifdef DOT11_N_SUPPORT
199 INT Set_TGnWifiTest_Proc(
200     IN  PRTMP_ADAPTER   pAd,
201     IN  PSTRING          arg);
202 #endif // DOT11_N_SUPPORT //
203
204 INT Set_LongRetryLimit_Proc(
205         IN      PRTMP_ADAPTER   pAdapter,
206         IN      PSTRING                 arg);
207
208 INT Set_ShortRetryLimit_Proc(
209         IN      PRTMP_ADAPTER   pAdapter,
210         IN      PSTRING                 arg);
211
212 #ifdef EXT_BUILD_CHANNEL_LIST
213 INT Set_Ieee80211dClientMode_Proc(
214     IN  PRTMP_ADAPTER   pAdapter,
215     IN  PSTRING          arg);
216 #endif // EXT_BUILD_CHANNEL_LIST //
217
218 #ifdef CARRIER_DETECTION_SUPPORT
219 INT Set_CarrierDetect_Proc(
220     IN  PRTMP_ADAPTER   pAd,
221     IN  PSTRING          arg);
222 #endif // CARRIER_DETECTION_SUPPORT //
223
224 INT     Show_Adhoc_MacTable_Proc(
225         IN      PRTMP_ADAPTER   pAd,
226         IN      PSTRING                 extra);
227
228 #ifdef RTMP_RF_RW_SUPPORT
229 VOID RTMPIoctlRF(
230         IN      PRTMP_ADAPTER   pAdapter,
231         IN      struct iwreq    *wrq);
232 #endif // RTMP_RF_RW_SUPPORT //
233
234
235 INT Set_BeaconLostTime_Proc(
236     IN  PRTMP_ADAPTER   pAd,
237     IN  PSTRING         arg);
238
239 INT Set_AutoRoaming_Proc(
240     IN  PRTMP_ADAPTER   pAd,
241     IN  PSTRING         arg);
242
243 INT Set_SiteSurvey_Proc(
244         IN      PRTMP_ADAPTER   pAd,
245         IN      PSTRING                 arg);
246
247 INT Set_ForceTxBurst_Proc(
248     IN  PRTMP_ADAPTER   pAd,
249     IN  PSTRING         arg);
250
251 #ifdef ANT_DIVERSITY_SUPPORT
252 INT     Set_Antenna_Proc(
253         IN      PRTMP_ADAPTER   pAd,
254         IN      PUCHAR                  arg);
255 #endif // ANT_DIVERSITY_SUPPORT //
256
257 static struct {
258         PSTRING name;
259         INT (*set_proc)(PRTMP_ADAPTER pAdapter, PSTRING arg);
260 } *PRTMP_PRIVATE_SET_PROC, RTMP_PRIVATE_SUPPORT_PROC[] = {
261         {"DriverVersion",                               Set_DriverVersion_Proc},
262         {"CountryRegion",                               Set_CountryRegion_Proc},
263         {"CountryRegionABand",                  Set_CountryRegionABand_Proc},
264         {"SSID",                                                Set_SSID_Proc},
265         {"WirelessMode",                                Set_WirelessMode_Proc},
266         {"TxBurst",                                     Set_TxBurst_Proc},
267         {"TxPreamble",                          Set_TxPreamble_Proc},
268         {"TxPower",                                     Set_TxPower_Proc},
269         {"Channel",                                     Set_Channel_Proc},
270         {"BGProtection",                                Set_BGProtection_Proc},
271         {"RTSThreshold",                                Set_RTSThreshold_Proc},
272         {"FragThreshold",                               Set_FragThreshold_Proc},
273 #ifdef DOT11_N_SUPPORT
274         {"HtBw",                                Set_HtBw_Proc},
275         {"HtMcs",                               Set_HtMcs_Proc},
276         {"HtGi",                                Set_HtGi_Proc},
277         {"HtOpMode",                        Set_HtOpMode_Proc},
278         {"HtExtcha",                        Set_HtExtcha_Proc},
279         {"HtMpduDensity",                       Set_HtMpduDensity_Proc},
280         {"HtBaWinSize",                         Set_HtBaWinSize_Proc},
281         {"HtRdg",                                       Set_HtRdg_Proc},
282         {"HtAmsdu",                                     Set_HtAmsdu_Proc},
283         {"HtAutoBa",                            Set_HtAutoBa_Proc},
284         {"HtBaDecline",                                 Set_BADecline_Proc},
285         {"HtProtect",                           Set_HtProtect_Proc},
286         {"HtMimoPs",                            Set_HtMimoPs_Proc},
287         {"HtDisallowTKIP",                              Set_HtDisallowTKIP_Proc},
288 #endif // DOT11_N_SUPPORT //
289
290 #ifdef AGGREGATION_SUPPORT
291         {"PktAggregate",                                Set_PktAggregate_Proc},
292 #endif // AGGREGATION_SUPPORT //
293
294 #ifdef WMM_SUPPORT
295         {"WmmCapable",                                  Set_WmmCapable_Proc},
296 #endif
297         {"IEEE80211H",                                  Set_IEEE80211H_Proc},
298     {"NetworkType",                 Set_NetworkType_Proc},
299         {"AuthMode",                                    Set_AuthMode_Proc},
300         {"EncrypType",                                  Set_EncrypType_Proc},
301         {"DefaultKeyID",                                Set_DefaultKeyID_Proc},
302         {"Key1",                                                Set_Key1_Proc},
303         {"Key2",                                                Set_Key2_Proc},
304         {"Key3",                                                Set_Key3_Proc},
305         {"Key4",                                                Set_Key4_Proc},
306         {"WPAPSK",                                              Set_WPAPSK_Proc},
307         {"ResetCounter",                                Set_ResetStatCounter_Proc},
308         {"PSMode",                      Set_PSMode_Proc},
309 #ifdef DBG
310         {"Debug",                                               Set_Debug_Proc},
311 #endif // DBG //
312
313 #ifdef RALINK_ATE
314         {"ATE",                                                 Set_ATE_Proc},
315         {"ATEDA",                                               Set_ATE_DA_Proc},
316         {"ATESA",                                               Set_ATE_SA_Proc},
317         {"ATEBSSID",                                    Set_ATE_BSSID_Proc},
318         {"ATECHANNEL",                                  Set_ATE_CHANNEL_Proc},
319         {"ATETXPOW0",                                   Set_ATE_TX_POWER0_Proc},
320         {"ATETXPOW1",                                   Set_ATE_TX_POWER1_Proc},
321         {"ATETXANT",                                    Set_ATE_TX_Antenna_Proc},
322         {"ATERXANT",                                    Set_ATE_RX_Antenna_Proc},
323         {"ATETXFREQOFFSET",                             Set_ATE_TX_FREQOFFSET_Proc},
324         {"ATETXBW",                                             Set_ATE_TX_BW_Proc},
325         {"ATETXLEN",                                    Set_ATE_TX_LENGTH_Proc},
326         {"ATETXCNT",                                    Set_ATE_TX_COUNT_Proc},
327         {"ATETXMCS",                                    Set_ATE_TX_MCS_Proc},
328         {"ATETXMODE",                                   Set_ATE_TX_MODE_Proc},
329         {"ATETXGI",                                             Set_ATE_TX_GI_Proc},
330         {"ATERXFER",                                    Set_ATE_RX_FER_Proc},
331         {"ATERRF",                                              Set_ATE_Read_RF_Proc},
332         {"ATEWRF1",                                             Set_ATE_Write_RF1_Proc},
333         {"ATEWRF2",                                             Set_ATE_Write_RF2_Proc},
334         {"ATEWRF3",                                             Set_ATE_Write_RF3_Proc},
335         {"ATEWRF4",                                             Set_ATE_Write_RF4_Proc},
336         {"ATELDE2P",                                Set_ATE_Load_E2P_Proc},
337         {"ATERE2P",                                             Set_ATE_Read_E2P_Proc},
338         {"ATESHOW",                                             Set_ATE_Show_Proc},
339         {"ATEHELP",                                             Set_ATE_Help_Proc},
340
341 #ifdef RALINK_28xx_QA
342         {"TxStop",                                              Set_TxStop_Proc},
343         {"RxStop",                                              Set_RxStop_Proc},
344 #endif // RALINK_28xx_QA //
345 #endif // RALINK_ATE //
346
347 #ifdef WPA_SUPPLICANT_SUPPORT
348     {"WpaSupport",                  Set_Wpa_Support},
349 #endif // WPA_SUPPLICANT_SUPPORT //
350
351
352
353
354
355         {"FixedTxMode",                 Set_FixedTxMode_Proc},
356 #ifdef CONFIG_APSTA_MIXED_SUPPORT
357         {"OpMode",                                              Set_OpMode_Proc},
358 #endif // CONFIG_APSTA_MIXED_SUPPORT //
359 #ifdef DOT11_N_SUPPORT
360     {"TGnWifiTest",                 Set_TGnWifiTest_Proc},
361     {"ForceGF",                                 Set_ForceGF_Proc},
362 #endif // DOT11_N_SUPPORT //
363 #ifdef QOS_DLS_SUPPORT
364         {"DlsAddEntry",                                 Set_DlsAddEntry_Proc},
365         {"DlsTearDownEntry",                    Set_DlsTearDownEntry_Proc},
366 #endif // QOS_DLS_SUPPORT //
367         {"LongRetry",                           Set_LongRetryLimit_Proc},
368         {"ShortRetry",                          Set_ShortRetryLimit_Proc},
369 #ifdef EXT_BUILD_CHANNEL_LIST
370         {"11dClientMode",                               Set_Ieee80211dClientMode_Proc},
371 #endif // EXT_BUILD_CHANNEL_LIST //
372 #ifdef CARRIER_DETECTION_SUPPORT
373         {"CarrierDetect",                               Set_CarrierDetect_Proc},
374 #endif // CARRIER_DETECTION_SUPPORT //
375
376
377 //2008/09/11:KH add to support efuse<--
378 #ifdef RT30xx
379 #ifdef RTMP_EFUSE_SUPPORT
380         {"efuseFreeNumber",                             set_eFuseGetFreeBlockCount_Proc},
381         {"efuseDump",                                   set_eFusedump_Proc},
382         {"efuseLoadFromBin",                            set_eFuseLoadFromBin_Proc},
383         {"efuseBufferModeWriteBack",            set_eFuseBufferModeWriteBack_Proc},
384 #endif // RTMP_EFUSE_SUPPORT //
385 #ifdef ANT_DIVERSITY_SUPPORT
386         {"ant",                                 Set_Antenna_Proc},
387 #endif // ANT_DIVERSITY_SUPPORT //
388 #endif // RT30xx //
389 //2008/09/11:KH add to support efuse-->
390
391         {"BeaconLostTime",                              Set_BeaconLostTime_Proc},
392         {"AutoRoaming",                                 Set_AutoRoaming_Proc},
393         {"SiteSurvey",                                  Set_SiteSurvey_Proc},
394         {"ForceTxBurst",                                Set_ForceTxBurst_Proc},
395
396         {NULL,}
397 };
398
399
400 VOID RTMPAddKey(
401         IN      PRTMP_ADAPTER       pAd,
402         IN      PNDIS_802_11_KEY    pKey)
403 {
404         ULONG                           KeyIdx;
405         MAC_TABLE_ENTRY         *pEntry;
406
407     DBGPRINT(RT_DEBUG_TRACE, ("RTMPAddKey ------>\n"));
408
409         if (pAd->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
410         {
411                 if (pKey->KeyIndex & 0x80000000)
412                 {
413                     if (pAd->StaCfg.AuthMode == Ndis802_11AuthModeWPANone)
414             {
415                 NdisZeroMemory(pAd->StaCfg.PMK, 32);
416                 NdisMoveMemory(pAd->StaCfg.PMK, pKey->KeyMaterial, pKey->KeyLength);
417                 goto end;
418             }
419                     // Update PTK
420                     NdisZeroMemory(&pAd->SharedKey[BSS0][0], sizeof(CIPHER_KEY));
421             pAd->SharedKey[BSS0][0].KeyLen = LEN_TKIP_EK;
422             NdisMoveMemory(pAd->SharedKey[BSS0][0].Key, pKey->KeyMaterial, LEN_TKIP_EK);
423 #ifdef WPA_SUPPLICANT_SUPPORT
424             if (pAd->StaCfg.PairCipher == Ndis802_11Encryption2Enabled)
425             {
426                 NdisMoveMemory(pAd->SharedKey[BSS0][0].RxMic, pKey->KeyMaterial + LEN_TKIP_EK, LEN_TKIP_TXMICK);
427                 NdisMoveMemory(pAd->SharedKey[BSS0][0].TxMic, pKey->KeyMaterial + LEN_TKIP_EK + LEN_TKIP_TXMICK, LEN_TKIP_RXMICK);
428             }
429             else
430 #endif // WPA_SUPPLICANT_SUPPORT //
431             {
432                 NdisMoveMemory(pAd->SharedKey[BSS0][0].TxMic, pKey->KeyMaterial + LEN_TKIP_EK, LEN_TKIP_TXMICK);
433                 NdisMoveMemory(pAd->SharedKey[BSS0][0].RxMic, pKey->KeyMaterial + LEN_TKIP_EK + LEN_TKIP_TXMICK, LEN_TKIP_RXMICK);
434             }
435
436             // Decide its ChiperAlg
437                 if (pAd->StaCfg.PairCipher == Ndis802_11Encryption2Enabled)
438                         pAd->SharedKey[BSS0][0].CipherAlg = CIPHER_TKIP;
439                 else if (pAd->StaCfg.PairCipher == Ndis802_11Encryption3Enabled)
440                         pAd->SharedKey[BSS0][0].CipherAlg = CIPHER_AES;
441                 else
442                         pAd->SharedKey[BSS0][0].CipherAlg = CIPHER_NONE;
443
444             // Update these related information to MAC_TABLE_ENTRY
445                 pEntry = &pAd->MacTab.Content[BSSID_WCID];
446             NdisMoveMemory(pEntry->PairwiseKey.Key, pAd->SharedKey[BSS0][0].Key, LEN_TKIP_EK);
447                 NdisMoveMemory(pEntry->PairwiseKey.RxMic, pAd->SharedKey[BSS0][0].RxMic, LEN_TKIP_RXMICK);
448                 NdisMoveMemory(pEntry->PairwiseKey.TxMic, pAd->SharedKey[BSS0][0].TxMic, LEN_TKIP_TXMICK);
449                 pEntry->PairwiseKey.CipherAlg = pAd->SharedKey[BSS0][0].CipherAlg;
450
451                 // Update pairwise key information to ASIC Shared Key Table
452                 AsicAddSharedKeyEntry(pAd,
453                                                           BSS0,
454                                                           0,
455                                                           pAd->SharedKey[BSS0][0].CipherAlg,
456                                                           pAd->SharedKey[BSS0][0].Key,
457                                                           pAd->SharedKey[BSS0][0].TxMic,
458                                                           pAd->SharedKey[BSS0][0].RxMic);
459
460                 // Update ASIC WCID attribute table and IVEIV table
461                 RTMPAddWcidAttributeEntry(pAd,
462                                                                   BSS0,
463                                                                   0,
464                                                                   pAd->SharedKey[BSS0][0].CipherAlg,
465                                                                   pEntry);
466
467             if (pAd->StaCfg.AuthMode >= Ndis802_11AuthModeWPA2)
468             {
469                 // set 802.1x port control
470                     //pAd->StaCfg.PortSecured = WPA_802_1X_PORT_SECURED;
471                                 STA_PORT_SECURED(pAd);
472
473                 // Indicate Connected for GUI
474                 pAd->IndicateMediaState = NdisMediaStateConnected;
475             }
476                 }
477         else
478         {
479             // Update GTK
480             pAd->StaCfg.DefaultKeyId = (pKey->KeyIndex & 0xFF);
481             NdisZeroMemory(&pAd->SharedKey[BSS0][pAd->StaCfg.DefaultKeyId], sizeof(CIPHER_KEY));
482             pAd->SharedKey[BSS0][pAd->StaCfg.DefaultKeyId].KeyLen = LEN_TKIP_EK;
483             NdisMoveMemory(pAd->SharedKey[BSS0][pAd->StaCfg.DefaultKeyId].Key, pKey->KeyMaterial, LEN_TKIP_EK);
484 #ifdef WPA_SUPPLICANT_SUPPORT
485             if (pAd->StaCfg.GroupCipher == Ndis802_11Encryption2Enabled)
486             {
487                 NdisMoveMemory(pAd->SharedKey[BSS0][pAd->StaCfg.DefaultKeyId].RxMic, pKey->KeyMaterial + LEN_TKIP_EK, LEN_TKIP_TXMICK);
488                 NdisMoveMemory(pAd->SharedKey[BSS0][pAd->StaCfg.DefaultKeyId].TxMic, pKey->KeyMaterial + LEN_TKIP_EK + LEN_TKIP_TXMICK, LEN_TKIP_RXMICK);
489             }
490             else
491 #endif // WPA_SUPPLICANT_SUPPORT //
492             {
493                 NdisMoveMemory(pAd->SharedKey[BSS0][pAd->StaCfg.DefaultKeyId].TxMic, pKey->KeyMaterial + LEN_TKIP_EK, LEN_TKIP_TXMICK);
494                 NdisMoveMemory(pAd->SharedKey[BSS0][pAd->StaCfg.DefaultKeyId].RxMic, pKey->KeyMaterial + LEN_TKIP_EK + LEN_TKIP_TXMICK, LEN_TKIP_RXMICK);
495             }
496
497             // Update Shared Key CipherAlg
498                 pAd->SharedKey[BSS0][pAd->StaCfg.DefaultKeyId].CipherAlg = CIPHER_NONE;
499                 if (pAd->StaCfg.GroupCipher == Ndis802_11Encryption2Enabled)
500                         pAd->SharedKey[BSS0][pAd->StaCfg.DefaultKeyId].CipherAlg = CIPHER_TKIP;
501                 else if (pAd->StaCfg.GroupCipher == Ndis802_11Encryption3Enabled)
502                         pAd->SharedKey[BSS0][pAd->StaCfg.DefaultKeyId].CipherAlg = CIPHER_AES;
503
504             // Update group key information to ASIC Shared Key Table
505                 AsicAddSharedKeyEntry(pAd,
506                                                           BSS0,
507                                                           pAd->StaCfg.DefaultKeyId,
508                                                           pAd->SharedKey[BSS0][pAd->StaCfg.DefaultKeyId].CipherAlg,
509                                                           pAd->SharedKey[BSS0][pAd->StaCfg.DefaultKeyId].Key,
510                                                           pAd->SharedKey[BSS0][pAd->StaCfg.DefaultKeyId].TxMic,
511                                                           pAd->SharedKey[BSS0][pAd->StaCfg.DefaultKeyId].RxMic);
512
513                 // Update ASIC WCID attribute table and IVEIV table
514                 RTMPAddWcidAttributeEntry(pAd,
515                                                                   BSS0,
516                                                                   pAd->StaCfg.DefaultKeyId,
517                                                                   pAd->SharedKey[BSS0][pAd->StaCfg.DefaultKeyId].CipherAlg,
518                                                                   NULL);
519
520             // set 802.1x port control
521                 //pAd->StaCfg.PortSecured = WPA_802_1X_PORT_SECURED;
522                         STA_PORT_SECURED(pAd);
523
524             // Indicate Connected for GUI
525             pAd->IndicateMediaState = NdisMediaStateConnected;
526         }
527         }
528         else    // dynamic WEP from wpa_supplicant
529         {
530                 UCHAR   CipherAlg;
531         PUCHAR  Key;
532
533                 if(pKey->KeyLength == 32)
534                         goto end;
535
536                 KeyIdx = pKey->KeyIndex & 0x0fffffff;
537
538                 if (KeyIdx < 4)
539                 {
540                         // it is a default shared key, for Pairwise key setting
541                         if (pKey->KeyIndex & 0x80000000)
542                         {
543                                 pEntry = MacTableLookup(pAd, pKey->BSSID);
544
545                                 if (pEntry)
546                                 {
547                                         DBGPRINT(RT_DEBUG_TRACE, ("RTMPAddKey: Set Pair-wise Key\n"));
548
549                                         // set key material and key length
550                                         pEntry->PairwiseKey.KeyLen = (UCHAR)pKey->KeyLength;
551                                         NdisMoveMemory(pEntry->PairwiseKey.Key, &pKey->KeyMaterial, pKey->KeyLength);
552
553                                         // set Cipher type
554                                         if (pKey->KeyLength == 5)
555                                                 pEntry->PairwiseKey.CipherAlg = CIPHER_WEP64;
556                                         else
557                                                 pEntry->PairwiseKey.CipherAlg = CIPHER_WEP128;
558
559                                         // Add Pair-wise key to Asic
560                                         AsicAddPairwiseKeyEntry(
561                                                 pAd,
562                                                 pEntry->Addr,
563                                                 (UCHAR)pEntry->Aid,
564                                 &pEntry->PairwiseKey);
565
566                                         // update WCID attribute table and IVEIV table for this entry
567                                         RTMPAddWcidAttributeEntry(
568                                                 pAd,
569                                                 BSS0,
570                                                 KeyIdx, // The value may be not zero
571                                                 pEntry->PairwiseKey.CipherAlg,
572                                                 pEntry);
573
574                                 }
575                         }
576                         else
577             {
578                                 // Default key for tx (shared key)
579                                 pAd->StaCfg.DefaultKeyId = (UCHAR) KeyIdx;
580
581                                 // set key material and key length
582                                 pAd->SharedKey[BSS0][KeyIdx].KeyLen = (UCHAR) pKey->KeyLength;
583                                 NdisMoveMemory(pAd->SharedKey[BSS0][KeyIdx].Key, &pKey->KeyMaterial, pKey->KeyLength);
584
585                                 // Set Ciper type
586                                 if (pKey->KeyLength == 5)
587                                         pAd->SharedKey[BSS0][KeyIdx].CipherAlg = CIPHER_WEP64;
588                                 else
589                                         pAd->SharedKey[BSS0][KeyIdx].CipherAlg = CIPHER_WEP128;
590
591                         CipherAlg = pAd->SharedKey[BSS0][KeyIdx].CipherAlg;
592                         Key = pAd->SharedKey[BSS0][KeyIdx].Key;
593
594                                 // Set Group key material to Asic
595                         AsicAddSharedKeyEntry(pAd, BSS0, KeyIdx, CipherAlg, Key, NULL, NULL);
596
597                                 // Update WCID attribute table and IVEIV table for this group key table
598                                 RTMPAddWcidAttributeEntry(pAd, BSS0, KeyIdx, CipherAlg, NULL);
599
600                         }
601                 }
602         }
603 end:
604         return;
605 }
606
607 char * rtstrchr(const char * s, int c)
608 {
609     for(; *s != (char) c; ++s)
610         if (*s == '\0')
611             return NULL;
612     return (char *) s;
613 }
614
615 /*
616 This is required for LinEX2004/kernel2.6.7 to provide iwlist scanning function
617 */
618
619 int
620 rt_ioctl_giwname(struct net_device *dev,
621                    struct iw_request_info *info,
622                    char *name, char *extra)
623 {
624
625 #ifdef RTMP_MAC_PCI
626     strncpy(name, "RT2860 Wireless", IFNAMSIZ);
627 #endif // RTMP_MAC_PCI //
628         return 0;
629 }
630
631 int rt_ioctl_siwfreq(struct net_device *dev,
632                         struct iw_request_info *info,
633                         struct iw_freq *freq, char *extra)
634 {
635         PRTMP_ADAPTER pAdapter = RTMP_OS_NETDEV_GET_PRIV(dev);
636         int     chan = -1;
637
638     //check if the interface is down
639     if(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE))
640     {
641         DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
642         return -ENETDOWN;
643     }
644
645
646         if (freq->e > 1)
647                 return -EINVAL;
648
649         if((freq->e == 0) && (freq->m <= 1000))
650                 chan = freq->m; // Setting by channel number
651         else
652                 MAP_KHZ_TO_CHANNEL_ID( (freq->m /100) , chan); // Setting by frequency - search the table , like 2.412G, 2.422G,
653
654     if (ChannelSanity(pAdapter, chan) == TRUE)
655     {
656         pAdapter->CommonCfg.Channel = chan;
657         DBGPRINT(RT_DEBUG_ERROR, ("==>rt_ioctl_siwfreq::SIOCSIWFREQ[cmd=0x%x] (Channel=%d)\n", SIOCSIWFREQ, pAdapter->CommonCfg.Channel));
658     }
659     else
660         return -EINVAL;
661
662         return 0;
663 }
664
665
666 int rt_ioctl_giwfreq(struct net_device *dev,
667                    struct iw_request_info *info,
668                    struct iw_freq *freq, char *extra)
669 {
670         PRTMP_ADAPTER pAdapter = NULL;
671         UCHAR ch;
672         ULONG   m = 2412000;
673
674         pAdapter = RTMP_OS_NETDEV_GET_PRIV(dev);
675         if (pAdapter == NULL)
676         {
677                 /* if 1st open fail, pAd will be free;
678                    So the net_dev->priv will be NULL in 2rd open */
679                 return -ENETDOWN;
680         }
681
682                 ch = pAdapter->CommonCfg.Channel;
683
684         DBGPRINT(RT_DEBUG_TRACE,("==>rt_ioctl_giwfreq  %d\n", ch));
685
686         MAP_CHANNEL_ID_TO_KHZ(ch, m);
687         freq->m = m * 100;
688         freq->e = 1;
689         return 0;
690 }
691
692
693 int rt_ioctl_siwmode(struct net_device *dev,
694                    struct iw_request_info *info,
695                    __u32 *mode, char *extra)
696 {
697         PRTMP_ADAPTER pAdapter = RTMP_OS_NETDEV_GET_PRIV(dev);
698
699         //check if the interface is down
700     if(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE))
701     {
702         DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
703         return -ENETDOWN;
704     }
705
706         switch (*mode)
707         {
708                 case IW_MODE_ADHOC:
709                         Set_NetworkType_Proc(pAdapter, "Adhoc");
710                         break;
711                 case IW_MODE_INFRA:
712                         Set_NetworkType_Proc(pAdapter, "Infra");
713                         break;
714 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,4,20))
715         case IW_MODE_MONITOR:
716                         Set_NetworkType_Proc(pAdapter, "Monitor");
717                         break;
718 #endif
719                 default:
720                         DBGPRINT(RT_DEBUG_TRACE, ("===>rt_ioctl_siwmode::SIOCSIWMODE (unknown %d)\n", *mode));
721                         return -EINVAL;
722         }
723
724         // Reset Ralink supplicant to not use, it will be set to start when UI set PMK key
725         pAdapter->StaCfg.WpaState = SS_NOTUSE;
726
727         return 0;
728 }
729
730
731 int rt_ioctl_giwmode(struct net_device *dev,
732                    struct iw_request_info *info,
733                    __u32 *mode, char *extra)
734 {
735         PRTMP_ADAPTER pAdapter = RTMP_OS_NETDEV_GET_PRIV(dev);
736
737         if (pAdapter == NULL)
738         {
739                 /* if 1st open fail, pAd will be free;
740                    So the net_dev->priv will be NULL in 2rd open */
741                 return -ENETDOWN;
742         }
743
744         if (ADHOC_ON(pAdapter))
745                 *mode = IW_MODE_ADHOC;
746     else if (INFRA_ON(pAdapter))
747                 *mode = IW_MODE_INFRA;
748 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,4,20))
749     else if (MONITOR_ON(pAdapter))
750     {
751         *mode = IW_MODE_MONITOR;
752     }
753 #endif
754     else
755         *mode = IW_MODE_AUTO;
756
757         DBGPRINT(RT_DEBUG_TRACE, ("==>rt_ioctl_giwmode(mode=%d)\n", *mode));
758         return 0;
759 }
760
761 int rt_ioctl_siwsens(struct net_device *dev,
762                    struct iw_request_info *info,
763                    char *name, char *extra)
764 {
765         PRTMP_ADAPTER pAdapter = RTMP_OS_NETDEV_GET_PRIV(dev);
766
767         //check if the interface is down
768         if(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE))
769         {
770                 DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
771                 return -ENETDOWN;
772         }
773
774         return 0;
775 }
776
777 int rt_ioctl_giwsens(struct net_device *dev,
778                    struct iw_request_info *info,
779                    char *name, char *extra)
780 {
781         return 0;
782 }
783
784 int rt_ioctl_giwrange(struct net_device *dev,
785                    struct iw_request_info *info,
786                    struct iw_point *data, char *extra)
787 {
788         PRTMP_ADAPTER pAdapter = RTMP_OS_NETDEV_GET_PRIV(dev);
789         struct iw_range *range = (struct iw_range *) extra;
790         u16 val;
791         int i;
792
793         if (pAdapter == NULL)
794         {
795                 /* if 1st open fail, pAd will be free;
796                    So the net_dev->priv will be NULL in 2rd open */
797                 return -ENETDOWN;
798         }
799
800         DBGPRINT(RT_DEBUG_TRACE ,("===>rt_ioctl_giwrange\n"));
801         data->length = sizeof(struct iw_range);
802         memset(range, 0, sizeof(struct iw_range));
803
804         range->txpower_capa = IW_TXPOW_DBM;
805
806         if (INFRA_ON(pAdapter)||ADHOC_ON(pAdapter))
807         {
808                 range->min_pmp = 1 * 1024;
809                 range->max_pmp = 65535 * 1024;
810                 range->min_pmt = 1 * 1024;
811                 range->max_pmt = 1000 * 1024;
812                 range->pmp_flags = IW_POWER_PERIOD;
813                 range->pmt_flags = IW_POWER_TIMEOUT;
814                 range->pm_capa = IW_POWER_PERIOD | IW_POWER_TIMEOUT |
815                         IW_POWER_UNICAST_R | IW_POWER_ALL_R;
816         }
817
818         range->we_version_compiled = WIRELESS_EXT;
819         range->we_version_source = 14;
820
821         range->retry_capa = IW_RETRY_LIMIT;
822         range->retry_flags = IW_RETRY_LIMIT;
823         range->min_retry = 0;
824         range->max_retry = 255;
825
826         range->num_channels =  pAdapter->ChannelListNum;
827
828         val = 0;
829         for (i = 1; i <= range->num_channels; i++)
830         {
831                 u32 m = 2412000;
832                 range->freq[val].i = pAdapter->ChannelList[i-1].Channel;
833                 MAP_CHANNEL_ID_TO_KHZ(pAdapter->ChannelList[i-1].Channel, m);
834                 range->freq[val].m = m * 100; /* OS_HZ */
835
836                 range->freq[val].e = 1;
837                 val++;
838                 if (val == IW_MAX_FREQUENCIES)
839                         break;
840         }
841         range->num_frequency = val;
842
843         range->max_qual.qual = 100; /* what is correct max? This was not
844                                         * documented exactly. At least
845                                         * 69 has been observed. */
846         range->max_qual.level = 0; /* dB */
847         range->max_qual.noise = 0; /* dB */
848
849         /* What would be suitable values for "average/typical" qual? */
850         range->avg_qual.qual = 20;
851         range->avg_qual.level = -60;
852         range->avg_qual.noise = -95;
853         range->sensitivity = 3;
854
855         range->max_encoding_tokens = NR_WEP_KEYS;
856         range->num_encoding_sizes = 2;
857         range->encoding_size[0] = 5;
858         range->encoding_size[1] = 13;
859
860         range->min_rts = 0;
861         range->max_rts = 2347;
862         range->min_frag = 256;
863         range->max_frag = 2346;
864
865 #if WIRELESS_EXT > 17
866         /* IW_ENC_CAPA_* bit field */
867         range->enc_capa = IW_ENC_CAPA_WPA | IW_ENC_CAPA_WPA2 |
868                                         IW_ENC_CAPA_CIPHER_TKIP | IW_ENC_CAPA_CIPHER_CCMP;
869 #endif
870
871         return 0;
872 }
873
874 int rt_ioctl_siwap(struct net_device *dev,
875                       struct iw_request_info *info,
876                       struct sockaddr *ap_addr, char *extra)
877 {
878         PRTMP_ADAPTER pAdapter = RTMP_OS_NETDEV_GET_PRIV(dev);
879     NDIS_802_11_MAC_ADDRESS Bssid;
880
881         //check if the interface is down
882         if(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE))
883         {
884         DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
885         return -ENETDOWN;
886     }
887
888         if (pAdapter->Mlme.CntlMachine.CurrState != CNTL_IDLE)
889     {
890         RTMP_MLME_RESET_STATE_MACHINE(pAdapter);
891         DBGPRINT(RT_DEBUG_TRACE, ("!!! MLME busy, reset MLME state machine !!!\n"));
892     }
893
894     // tell CNTL state machine to call NdisMSetInformationComplete() after completing
895     // this request, because this request is initiated by NDIS.
896     pAdapter->MlmeAux.CurrReqIsFromNdis = FALSE;
897         // Prevent to connect AP again in STAMlmePeriodicExec
898         pAdapter->MlmeAux.AutoReconnectSsidLen= 32;
899
900     memset(Bssid, 0, MAC_ADDR_LEN);
901     memcpy(Bssid, ap_addr->sa_data, MAC_ADDR_LEN);
902     MlmeEnqueue(pAdapter,
903                 MLME_CNTL_STATE_MACHINE,
904                 OID_802_11_BSSID,
905                 sizeof(NDIS_802_11_MAC_ADDRESS),
906                 (VOID *)&Bssid);
907
908     DBGPRINT(RT_DEBUG_TRACE, ("IOCTL::SIOCSIWAP %02x:%02x:%02x:%02x:%02x:%02x\n",
909         Bssid[0], Bssid[1], Bssid[2], Bssid[3], Bssid[4], Bssid[5]));
910
911         return 0;
912 }
913
914 int rt_ioctl_giwap(struct net_device *dev,
915                       struct iw_request_info *info,
916                       struct sockaddr *ap_addr, char *extra)
917 {
918         PRTMP_ADAPTER pAdapter = RTMP_OS_NETDEV_GET_PRIV(dev);
919
920         if (pAdapter == NULL)
921         {
922                 /* if 1st open fail, pAd will be free;
923                    So the net_dev->priv will be NULL in 2rd open */
924                 return -ENETDOWN;
925         }
926
927         if (INFRA_ON(pAdapter) || ADHOC_ON(pAdapter))
928         {
929                 ap_addr->sa_family = ARPHRD_ETHER;
930                 memcpy(ap_addr->sa_data, &pAdapter->CommonCfg.Bssid, ETH_ALEN);
931         }
932 #ifdef WPA_SUPPLICANT_SUPPORT
933     // Add for RT2870
934     else if (pAdapter->StaCfg.WpaSupplicantUP != WPA_SUPPLICANT_DISABLE)
935     {
936         ap_addr->sa_family = ARPHRD_ETHER;
937         memcpy(ap_addr->sa_data, &pAdapter->MlmeAux.Bssid, ETH_ALEN);
938     }
939 #endif // WPA_SUPPLICANT_SUPPORT //
940         else
941         {
942                 DBGPRINT(RT_DEBUG_TRACE, ("IOCTL::SIOCGIWAP(=EMPTY)\n"));
943                 return -ENOTCONN;
944         }
945
946         return 0;
947 }
948
949 /*
950  * Units are in db above the noise floor. That means the
951  * rssi values reported in the tx/rx descriptors in the
952  * driver are the SNR expressed in db.
953  *
954  * If you assume that the noise floor is -95, which is an
955  * excellent assumption 99.5 % of the time, then you can
956  * derive the absolute signal level (i.e. -95 + rssi).
957  * There are some other slight factors to take into account
958  * depending on whether the rssi measurement is from 11b,
959  * 11g, or 11a.   These differences are at most 2db and
960  * can be documented.
961  *
962  * NB: various calculations are based on the orinoco/wavelan
963  *     drivers for compatibility
964  */
965 static void set_quality(PRTMP_ADAPTER pAdapter,
966                         struct iw_quality *iq,
967                         signed char rssi)
968 {
969         __u8 ChannelQuality;
970
971         // Normalize Rssi
972         if (rssi >= -50)
973         ChannelQuality = 100;
974         else if (rssi >= -80) // between -50 ~ -80dbm
975                 ChannelQuality = (__u8)(24 + ((rssi + 80) * 26)/10);
976         else if (rssi >= -90)   // between -80 ~ -90dbm
977         ChannelQuality = (__u8)((rssi + 90) * 26)/10;
978         else
979                 ChannelQuality = 0;
980
981     iq->qual = (__u8)ChannelQuality;
982
983     iq->level = (__u8)(rssi);
984     iq->noise = (pAdapter->BbpWriteLatch[66] > pAdapter->BbpTuning.FalseCcaUpperThreshold) ? ((__u8)pAdapter->BbpTuning.FalseCcaUpperThreshold) : ((__u8) pAdapter->BbpWriteLatch[66]);         // noise level (dBm)
985     iq->noise += 256 - 143;
986     iq->updated = pAdapter->iw_stats.qual.updated;
987 }
988
989 int rt_ioctl_iwaplist(struct net_device *dev,
990                         struct iw_request_info *info,
991                         struct iw_point *data, char *extra)
992 {
993         PRTMP_ADAPTER pAdapter = RTMP_OS_NETDEV_GET_PRIV(dev);
994
995         struct sockaddr addr[IW_MAX_AP];
996         struct iw_quality qual[IW_MAX_AP];
997         int i;
998
999         //check if the interface is down
1000     if(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE))
1001     {
1002         DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
1003                 data->length = 0;
1004                 return 0;
1005         //return -ENETDOWN;
1006         }
1007
1008         for (i = 0; i <IW_MAX_AP ; i++)
1009         {
1010                 if (i >=  pAdapter->ScanTab.BssNr)
1011                         break;
1012                 addr[i].sa_family = ARPHRD_ETHER;
1013                         memcpy(addr[i].sa_data, &pAdapter->ScanTab.BssEntry[i].Bssid, MAC_ADDR_LEN);
1014                 set_quality(pAdapter, &qual[i], pAdapter->ScanTab.BssEntry[i].Rssi);
1015         }
1016         data->length = i;
1017         memcpy(extra, &addr, i*sizeof(addr[0]));
1018         data->flags = 1;                /* signal quality present (sort of) */
1019         memcpy(extra + i*sizeof(addr[0]), &qual, i*sizeof(qual[i]));
1020
1021         return 0;
1022 }
1023
1024 #ifdef SIOCGIWSCAN
1025 int rt_ioctl_siwscan(struct net_device *dev,
1026                         struct iw_request_info *info,
1027                         struct iw_point *data, char *extra)
1028 {
1029         PRTMP_ADAPTER pAdapter = RTMP_OS_NETDEV_GET_PRIV(dev);
1030
1031         ULONG                                                           Now;
1032         int Status = NDIS_STATUS_SUCCESS;
1033
1034         //check if the interface is down
1035         if(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE))
1036         {
1037                 DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
1038                 return -ENETDOWN;
1039         }
1040
1041         if (MONITOR_ON(pAdapter))
1042     {
1043         DBGPRINT(RT_DEBUG_TRACE, ("!!! Driver is in Monitor Mode now !!!\n"));
1044         return -EINVAL;
1045     }
1046
1047
1048 #ifdef WPA_SUPPLICANT_SUPPORT
1049         if (pAdapter->StaCfg.WpaSupplicantUP == WPA_SUPPLICANT_ENABLE)
1050         {
1051                 pAdapter->StaCfg.WpaSupplicantScanCount++;
1052         }
1053 #endif // WPA_SUPPLICANT_SUPPORT //
1054
1055     pAdapter->StaCfg.bScanReqIsFromWebUI = TRUE;
1056         if (RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_BSS_SCAN_IN_PROGRESS))
1057                 return NDIS_STATUS_SUCCESS;
1058         do{
1059                 Now = jiffies;
1060
1061 #ifdef WPA_SUPPLICANT_SUPPORT
1062                 if ((pAdapter->StaCfg.WpaSupplicantUP == WPA_SUPPLICANT_ENABLE) &&
1063                         (pAdapter->StaCfg.WpaSupplicantScanCount > 3))
1064                 {
1065                         DBGPRINT(RT_DEBUG_TRACE, ("!!! WpaSupplicantScanCount > 3\n"));
1066                         Status = NDIS_STATUS_SUCCESS;
1067                         break;
1068                 }
1069 #endif // WPA_SUPPLICANT_SUPPORT //
1070
1071                 if ((OPSTATUS_TEST_FLAG(pAdapter, fOP_STATUS_MEDIA_STATE_CONNECTED)) &&
1072                         ((pAdapter->StaCfg.AuthMode == Ndis802_11AuthModeWPA) ||
1073                         (pAdapter->StaCfg.AuthMode == Ndis802_11AuthModeWPAPSK)) &&
1074                         (pAdapter->StaCfg.PortSecured == WPA_802_1X_PORT_NOT_SECURED))
1075                 {
1076                         DBGPRINT(RT_DEBUG_TRACE, ("!!! Link UP, Port Not Secured! ignore this set::OID_802_11_BSSID_LIST_SCAN\n"));
1077                         Status = NDIS_STATUS_SUCCESS;
1078                         break;
1079                 }
1080
1081                 if (pAdapter->Mlme.CntlMachine.CurrState != CNTL_IDLE)
1082                 {
1083                         RTMP_MLME_RESET_STATE_MACHINE(pAdapter);
1084                         DBGPRINT(RT_DEBUG_TRACE, ("!!! MLME busy, reset MLME state machine !!!\n"));
1085                 }
1086
1087                 // tell CNTL state machine to call NdisMSetInformationComplete() after completing
1088                 // this request, because this request is initiated by NDIS.
1089                 pAdapter->MlmeAux.CurrReqIsFromNdis = FALSE;
1090                 // Reset allowed scan retries
1091                 pAdapter->StaCfg.ScanCnt = 0;
1092                 pAdapter->StaCfg.LastScanTime = Now;
1093
1094                 MlmeEnqueue(pAdapter,
1095                         MLME_CNTL_STATE_MACHINE,
1096                         OID_802_11_BSSID_LIST_SCAN,
1097                         0,
1098                         NULL);
1099
1100                 Status = NDIS_STATUS_SUCCESS;
1101                 RTMP_MLME_HANDLER(pAdapter);
1102         }while(0);
1103         return NDIS_STATUS_SUCCESS;
1104 }
1105
1106 int rt_ioctl_giwscan(struct net_device *dev,
1107                         struct iw_request_info *info,
1108                         struct iw_point *data, char *extra)
1109 {
1110
1111         PRTMP_ADAPTER pAdapter = RTMP_OS_NETDEV_GET_PRIV(dev);
1112         int i=0;
1113         PSTRING current_ev = extra, previous_ev = extra;
1114         PSTRING end_buf;
1115         PSTRING current_val;
1116         STRING custom[MAX_CUSTOM_LEN] = {0};
1117 #ifndef IWEVGENIE
1118         unsigned char idx;
1119 #endif // IWEVGENIE //
1120         struct iw_event iwe;
1121
1122         if (RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_BSS_SCAN_IN_PROGRESS))
1123     {
1124                 /*
1125                  * Still scanning, indicate the caller should try again.
1126                  */
1127                 return -EAGAIN;
1128         }
1129
1130
1131 #ifdef WPA_SUPPLICANT_SUPPORT
1132         if (pAdapter->StaCfg.WpaSupplicantUP == WPA_SUPPLICANT_ENABLE)
1133         {
1134                 pAdapter->StaCfg.WpaSupplicantScanCount = 0;
1135         }
1136 #endif // WPA_SUPPLICANT_SUPPORT //
1137
1138         if (pAdapter->ScanTab.BssNr == 0)
1139         {
1140                 data->length = 0;
1141                 return 0;
1142         }
1143
1144 #if WIRELESS_EXT >= 17
1145     if (data->length > 0)
1146         end_buf = extra + data->length;
1147     else
1148         end_buf = extra + IW_SCAN_MAX_DATA;
1149 #else
1150     end_buf = extra + IW_SCAN_MAX_DATA;
1151 #endif
1152
1153         for (i = 0; i < pAdapter->ScanTab.BssNr; i++)
1154         {
1155                 if (current_ev >= end_buf)
1156         {
1157 #if WIRELESS_EXT >= 17
1158             return -E2BIG;
1159 #else
1160                         break;
1161 #endif
1162         }
1163
1164                 //MAC address
1165                 //================================
1166                 memset(&iwe, 0, sizeof(iwe));
1167                 iwe.cmd = SIOCGIWAP;
1168                 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
1169                                 memcpy(iwe.u.ap_addr.sa_data, &pAdapter->ScanTab.BssEntry[i].Bssid, ETH_ALEN);
1170
1171         previous_ev = current_ev;
1172                 current_ev = IWE_STREAM_ADD_EVENT(info, current_ev,end_buf, &iwe, IW_EV_ADDR_LEN);
1173         if (current_ev == previous_ev)
1174 #if WIRELESS_EXT >= 17
1175             return -E2BIG;
1176 #else
1177                         break;
1178 #endif
1179
1180                 /*
1181                 Protocol:
1182                         it will show scanned AP's WirelessMode .
1183                         it might be
1184                                         802.11a
1185                                         802.11a/n
1186                                         802.11g/n
1187                                         802.11b/g/n
1188                                         802.11g
1189                                         802.11b/g
1190                 */
1191                 memset(&iwe, 0, sizeof(iwe));
1192                 iwe.cmd = SIOCGIWNAME;
1193
1194
1195         {
1196                 PBSS_ENTRY pBssEntry=&pAdapter->ScanTab.BssEntry[i];
1197                 BOOLEAN isGonly=FALSE;
1198                 int rateCnt=0;
1199
1200                 if (pBssEntry->Channel>14)
1201                 {
1202                         if (pBssEntry->HtCapabilityLen!=0)
1203                                 strcpy(iwe.u.name,"802.11a/n");
1204                         else
1205                                 strcpy(iwe.u.name,"802.11a");
1206                 }
1207                 else
1208                 {
1209                         /*
1210                                 if one of non B mode rate is set supported rate . it mean G only.
1211                         */
1212                         for (rateCnt=0;rateCnt<pBssEntry->SupRateLen;rateCnt++)
1213                         {
1214                                 /*
1215                                         6Mbps(140) 9Mbps(146) and >=12Mbps(152) are supported rate , it mean G only.
1216                                 */
1217                                 if (pBssEntry->SupRate[rateCnt]==140 || pBssEntry->SupRate[rateCnt]==146 || pBssEntry->SupRate[rateCnt]>=152)
1218                                         isGonly=TRUE;
1219                         }
1220
1221                         for (rateCnt=0;rateCnt<pBssEntry->ExtRateLen;rateCnt++)
1222                         {
1223                                 if (pBssEntry->ExtRate[rateCnt]==140 || pBssEntry->ExtRate[rateCnt]==146 || pBssEntry->ExtRate[rateCnt]>=152)
1224                                         isGonly=TRUE;
1225                         }
1226
1227
1228                         if (pBssEntry->HtCapabilityLen!=0)
1229                         {
1230                                 if (isGonly==TRUE)
1231                                         strcpy(iwe.u.name,"802.11g/n");
1232                                 else
1233                                         strcpy(iwe.u.name,"802.11b/g/n");
1234                         }
1235                         else
1236                         {
1237                                 if (isGonly==TRUE)
1238                                         strcpy(iwe.u.name,"802.11g");
1239                                 else
1240                                 {
1241                                         if (pBssEntry->SupRateLen==4 && pBssEntry->ExtRateLen==0)
1242                                                 strcpy(iwe.u.name,"802.11b");
1243                                         else
1244                                                 strcpy(iwe.u.name,"802.11b/g");
1245                                 }
1246                         }
1247                 }
1248         }
1249
1250                 previous_ev = current_ev;
1251                 current_ev = IWE_STREAM_ADD_EVENT(info, current_ev,end_buf, &iwe, IW_EV_ADDR_LEN);
1252                 if (current_ev == previous_ev)
1253 #if WIRELESS_EXT >= 17
1254                         return -E2BIG;
1255 #else
1256                         break;
1257 #endif
1258
1259                 //ESSID
1260                 //================================
1261                 memset(&iwe, 0, sizeof(iwe));
1262                 iwe.cmd = SIOCGIWESSID;
1263                 iwe.u.data.length = pAdapter->ScanTab.BssEntry[i].SsidLen;
1264                 iwe.u.data.flags = 1;
1265
1266         previous_ev = current_ev;
1267                 current_ev = IWE_STREAM_ADD_POINT(info, current_ev,end_buf, &iwe, (PSTRING) pAdapter->ScanTab.BssEntry[i].Ssid);
1268         if (current_ev == previous_ev)
1269 #if WIRELESS_EXT >= 17
1270             return -E2BIG;
1271 #else
1272                         break;
1273 #endif
1274
1275                 //Network Type
1276                 //================================
1277                 memset(&iwe, 0, sizeof(iwe));
1278                 iwe.cmd = SIOCGIWMODE;
1279                 if (pAdapter->ScanTab.BssEntry[i].BssType == Ndis802_11IBSS)
1280                 {
1281                         iwe.u.mode = IW_MODE_ADHOC;
1282                 }
1283                 else if (pAdapter->ScanTab.BssEntry[i].BssType == Ndis802_11Infrastructure)
1284                 {
1285                         iwe.u.mode = IW_MODE_INFRA;
1286                 }
1287                 else
1288                 {
1289                         iwe.u.mode = IW_MODE_AUTO;
1290                 }
1291                 iwe.len = IW_EV_UINT_LEN;
1292
1293         previous_ev = current_ev;
1294                 current_ev = IWE_STREAM_ADD_EVENT(info, current_ev, end_buf, &iwe,  IW_EV_UINT_LEN);
1295         if (current_ev == previous_ev)
1296 #if WIRELESS_EXT >= 17
1297             return -E2BIG;
1298 #else
1299                         break;
1300 #endif
1301
1302                 //Channel and Frequency
1303                 //================================
1304                 memset(&iwe, 0, sizeof(iwe));
1305                 iwe.cmd = SIOCGIWFREQ;
1306                 if (INFRA_ON(pAdapter) || ADHOC_ON(pAdapter))
1307                         iwe.u.freq.m = pAdapter->ScanTab.BssEntry[i].Channel;
1308                 else
1309                         iwe.u.freq.m = pAdapter->ScanTab.BssEntry[i].Channel;
1310                 iwe.u.freq.e = 0;
1311                 iwe.u.freq.i = 0;
1312
1313                 previous_ev = current_ev;
1314                 current_ev = IWE_STREAM_ADD_EVENT(info, current_ev,end_buf, &iwe, IW_EV_FREQ_LEN);
1315         if (current_ev == previous_ev)
1316 #if WIRELESS_EXT >= 17
1317             return -E2BIG;
1318 #else
1319                         break;
1320 #endif
1321
1322         //Add quality statistics
1323         //================================
1324         memset(&iwe, 0, sizeof(iwe));
1325         iwe.cmd = IWEVQUAL;
1326         iwe.u.qual.level = 0;
1327         iwe.u.qual.noise = 0;
1328         set_quality(pAdapter, &iwe.u.qual, pAdapter->ScanTab.BssEntry[i].Rssi);
1329         current_ev = IWE_STREAM_ADD_EVENT(info, current_ev, end_buf, &iwe, IW_EV_QUAL_LEN);
1330         if (current_ev == previous_ev)
1331 #if WIRELESS_EXT >= 17
1332             return -E2BIG;
1333 #else
1334                         break;
1335 #endif
1336
1337                 //Encyption key
1338                 //================================
1339                 memset(&iwe, 0, sizeof(iwe));
1340                 iwe.cmd = SIOCGIWENCODE;
1341                 if (CAP_IS_PRIVACY_ON (pAdapter->ScanTab.BssEntry[i].CapabilityInfo ))
1342                         iwe.u.data.flags =IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
1343                 else
1344                         iwe.u.data.flags = IW_ENCODE_DISABLED;
1345
1346         previous_ev = current_ev;
1347         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);
1348         if (current_ev == previous_ev)
1349 #if WIRELESS_EXT >= 17
1350             return -E2BIG;
1351 #else
1352                         break;
1353 #endif
1354
1355                 //Bit Rate
1356                 //================================
1357                 if (pAdapter->ScanTab.BssEntry[i].SupRateLen)
1358         {
1359             UCHAR tmpRate = pAdapter->ScanTab.BssEntry[i].SupRate[pAdapter->ScanTab.BssEntry[i].SupRateLen-1];
1360                         memset(&iwe, 0, sizeof(iwe));
1361                         iwe.cmd = SIOCGIWRATE;
1362                 current_val = current_ev + IW_EV_LCP_LEN;
1363             if (tmpRate == 0x82)
1364                 iwe.u.bitrate.value =  1 * 1000000;
1365             else if (tmpRate == 0x84)
1366                 iwe.u.bitrate.value =  2 * 1000000;
1367             else if (tmpRate == 0x8B)
1368                 iwe.u.bitrate.value =  5.5 * 1000000;
1369             else if (tmpRate == 0x96)
1370                 iwe.u.bitrate.value =  11 * 1000000;
1371             else
1372                     iwe.u.bitrate.value =  (tmpRate/2) * 1000000;
1373
1374                         if (tmpRate == 0x6c && pAdapter->ScanTab.BssEntry[i].HtCapabilityLen > 0)
1375                         {
1376                                 int rate_count = sizeof(ralinkrate)/sizeof(__s32);
1377                                 HT_CAP_INFO capInfo = pAdapter->ScanTab.BssEntry[i].HtCapability.HtCapInfo;
1378                                 int shortGI = capInfo.ChannelWidth ? capInfo.ShortGIfor40 : capInfo.ShortGIfor20;
1379                                 int maxMCS = pAdapter->ScanTab.BssEntry[i].HtCapability.MCSSet[1] ?  15 : 7;
1380                                 int rate_index = 12 + ((UCHAR)capInfo.ChannelWidth * 24) + ((UCHAR)shortGI *48) + ((UCHAR)maxMCS);
1381                                 if (rate_index < 0)
1382                                         rate_index = 0;
1383                                 if (rate_index > rate_count)
1384                                         rate_index = rate_count;
1385                                 iwe.u.bitrate.value     =  ralinkrate[rate_index] * 500000;
1386                         }
1387
1388                         iwe.u.bitrate.disabled = 0;
1389                         current_val = IWE_STREAM_ADD_VALUE(info, current_ev,
1390                                 current_val, end_buf, &iwe,
1391                         IW_EV_PARAM_LEN);
1392
1393                 if((current_val-current_ev)>IW_EV_LCP_LEN)
1394                 current_ev = current_val;
1395                 else
1396 #if WIRELESS_EXT >= 17
1397                 return -E2BIG;
1398 #else
1399                             break;
1400 #endif
1401         }
1402
1403 #ifdef IWEVGENIE
1404         //WPA IE
1405                 if (pAdapter->ScanTab.BssEntry[i].WpaIE.IELen > 0)
1406         {
1407                         memset(&iwe, 0, sizeof(iwe));
1408                         memset(&custom[0], 0, MAX_CUSTOM_LEN);
1409                         memcpy(custom, &(pAdapter->ScanTab.BssEntry[i].WpaIE.IE[0]),
1410                                                    pAdapter->ScanTab.BssEntry[i].WpaIE.IELen);
1411                         iwe.cmd = IWEVGENIE;
1412                         iwe.u.data.length = pAdapter->ScanTab.BssEntry[i].WpaIE.IELen;
1413                         current_ev = IWE_STREAM_ADD_POINT(info, current_ev, end_buf, &iwe, custom);
1414                         if (current_ev == previous_ev)
1415 #if WIRELESS_EXT >= 17
1416                 return -E2BIG;
1417 #else
1418                             break;
1419 #endif
1420                 }
1421
1422                 //WPA2 IE
1423         if (pAdapter->ScanTab.BssEntry[i].RsnIE.IELen > 0)
1424         {
1425                 memset(&iwe, 0, sizeof(iwe));
1426                         memset(&custom[0], 0, MAX_CUSTOM_LEN);
1427                         memcpy(custom, &(pAdapter->ScanTab.BssEntry[i].RsnIE.IE[0]),
1428                                                    pAdapter->ScanTab.BssEntry[i].RsnIE.IELen);
1429                         iwe.cmd = IWEVGENIE;
1430                         iwe.u.data.length = pAdapter->ScanTab.BssEntry[i].RsnIE.IELen;
1431                         current_ev = IWE_STREAM_ADD_POINT(info, current_ev, end_buf, &iwe, custom);
1432                         if (current_ev == previous_ev)
1433 #if WIRELESS_EXT >= 17
1434                 return -E2BIG;
1435 #else
1436                             break;
1437 #endif
1438         }
1439 #else
1440         //WPA IE
1441                 //================================
1442         if (pAdapter->ScanTab.BssEntry[i].WpaIE.IELen > 0)
1443         {
1444                 NdisZeroMemory(&iwe, sizeof(iwe));
1445                         memset(&custom[0], 0, MAX_CUSTOM_LEN);
1446                 iwe.cmd = IWEVCUSTOM;
1447             iwe.u.data.length = (pAdapter->ScanTab.BssEntry[i].WpaIE.IELen * 2) + 7;
1448             NdisMoveMemory(custom, "wpa_ie=", 7);
1449             for (idx = 0; idx < pAdapter->ScanTab.BssEntry[i].WpaIE.IELen; idx++)
1450                 sprintf(custom, "%s%02x", custom, pAdapter->ScanTab.BssEntry[i].WpaIE.IE[idx]);
1451             previous_ev = current_ev;
1452                 current_ev = IWE_STREAM_ADD_POINT(info, current_ev, end_buf, &iwe,  custom);
1453             if (current_ev == previous_ev)
1454 #if WIRELESS_EXT >= 17
1455                 return -E2BIG;
1456 #else
1457                             break;
1458 #endif
1459         }
1460
1461         //WPA2 IE
1462         if (pAdapter->ScanTab.BssEntry[i].RsnIE.IELen > 0)
1463         {
1464                 NdisZeroMemory(&iwe, sizeof(iwe));
1465                         memset(&custom[0], 0, MAX_CUSTOM_LEN);
1466                 iwe.cmd = IWEVCUSTOM;
1467             iwe.u.data.length = (pAdapter->ScanTab.BssEntry[i].RsnIE.IELen * 2) + 7;
1468             NdisMoveMemory(custom, "rsn_ie=", 7);
1469                         for (idx = 0; idx < pAdapter->ScanTab.BssEntry[i].RsnIE.IELen; idx++)
1470                 sprintf(custom, "%s%02x", custom, pAdapter->ScanTab.BssEntry[i].RsnIE.IE[idx]);
1471             previous_ev = current_ev;
1472                 current_ev = IWE_STREAM_ADD_POINT(info, current_ev, end_buf, &iwe,  custom);
1473             if (current_ev == previous_ev)
1474 #if WIRELESS_EXT >= 17
1475                 return -E2BIG;
1476 #else
1477                             break;
1478 #endif
1479         }
1480 #endif // IWEVGENIE //
1481         }
1482
1483         data->length = current_ev - extra;
1484     pAdapter->StaCfg.bScanReqIsFromWebUI = FALSE;
1485         DBGPRINT(RT_DEBUG_ERROR ,("===>rt_ioctl_giwscan. %d(%d) BSS returned, data->length = %d\n",i , pAdapter->ScanTab.BssNr, data->length));
1486         return 0;
1487 }
1488 #endif
1489
1490 int rt_ioctl_siwessid(struct net_device *dev,
1491                          struct iw_request_info *info,
1492                          struct iw_point *data, char *essid)
1493 {
1494         PRTMP_ADAPTER pAdapter = RTMP_OS_NETDEV_GET_PRIV(dev);
1495
1496         //check if the interface is down
1497     if(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE))
1498     {
1499         DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
1500         return -ENETDOWN;
1501     }
1502
1503         if (data->flags)
1504         {
1505                 PSTRING pSsidString = NULL;
1506
1507                 // Includes null character.
1508                 if (data->length > (IW_ESSID_MAX_SIZE + 1))
1509                         return -E2BIG;
1510
1511                 pSsidString = kmalloc(MAX_LEN_OF_SSID+1, MEM_ALLOC_FLAG);
1512                 if (pSsidString)
1513         {
1514                         NdisZeroMemory(pSsidString, MAX_LEN_OF_SSID+1);
1515                         NdisMoveMemory(pSsidString, essid, data->length);
1516                         if (Set_SSID_Proc(pAdapter, pSsidString) == FALSE)
1517                                 return -EINVAL;
1518                 }
1519                 else
1520                         return -ENOMEM;
1521                 }
1522         else
1523     {
1524                 // ANY ssid
1525                 if (Set_SSID_Proc(pAdapter, "") == FALSE)
1526                         return -EINVAL;
1527     }
1528         return 0;
1529 }
1530
1531 int rt_ioctl_giwessid(struct net_device *dev,
1532                          struct iw_request_info *info,
1533                          struct iw_point *data, char *essid)
1534 {
1535         PRTMP_ADAPTER pAdapter = RTMP_OS_NETDEV_GET_PRIV(dev);
1536
1537         if (pAdapter == NULL)
1538         {
1539                 /* if 1st open fail, pAd will be free;
1540                    So the net_dev->priv will be NULL in 2rd open */
1541                 return -ENETDOWN;
1542         }
1543
1544         data->flags = 1;
1545     if (MONITOR_ON(pAdapter))
1546     {
1547         data->length  = 0;
1548         return 0;
1549     }
1550
1551         if (OPSTATUS_TEST_FLAG(pAdapter, fOP_STATUS_MEDIA_STATE_CONNECTED))
1552         {
1553                 DBGPRINT(RT_DEBUG_TRACE ,("MediaState is connected\n"));
1554                 data->length = pAdapter->CommonCfg.SsidLen;
1555                 memcpy(essid, pAdapter->CommonCfg.Ssid, pAdapter->CommonCfg.SsidLen);
1556         }
1557         else
1558         {//the ANY ssid was specified
1559                 data->length  = 0;
1560                 DBGPRINT(RT_DEBUG_TRACE ,("MediaState is not connected, ess\n"));
1561         }
1562
1563         return 0;
1564
1565 }
1566
1567 int rt_ioctl_siwnickn(struct net_device *dev,
1568                          struct iw_request_info *info,
1569                          struct iw_point *data, char *nickname)
1570 {
1571         PRTMP_ADAPTER pAdapter = RTMP_OS_NETDEV_GET_PRIV(dev);
1572
1573     //check if the interface is down
1574     if(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE))
1575     {
1576         DBGPRINT(RT_DEBUG_TRACE ,("INFO::Network is down!\n"));
1577         return -ENETDOWN;
1578     }
1579
1580         if (data->length > IW_ESSID_MAX_SIZE)
1581                 return -EINVAL;
1582
1583         memset(pAdapter->nickname, 0, IW_ESSID_MAX_SIZE + 1);
1584         memcpy(pAdapter->nickname, nickname, data->length);
1585
1586
1587         return 0;
1588 }
1589
1590 int rt_ioctl_giwnickn(struct net_device *dev,
1591                          struct iw_request_info *info,
1592                          struct iw_point *data, char *nickname)
1593 {
1594         PRTMP_ADAPTER pAdapter = RTMP_OS_NETDEV_GET_PRIV(dev);
1595
1596         if (pAdapter == NULL)
1597         {
1598                 /* if 1st open fail, pAd will be free;
1599                    So the net_dev->priv will be NULL in 2rd open */
1600                 return -ENETDOWN;
1601         }
1602
1603         if (data->length > strlen((PSTRING) pAdapter->nickname) + 1)
1604                 data->length = strlen((PSTRING) pAdapter->nickname) + 1;
1605         if (data->length > 0) {
1606                 memcpy(nickname, pAdapter->nickname, data->length-1);
1607                 nickname[data->length-1] = '\0';
1608         }
1609         return 0;
1610 }
1611
1612 int rt_ioctl_siwrts(struct net_device *dev,
1613                        struct iw_request_info *info,
1614                        struct iw_param *rts, char *extra)
1615 {
1616         PRTMP_ADAPTER pAdapter = RTMP_OS_NETDEV_GET_PRIV(dev);
1617         u16 val;
1618
1619     //check if the interface is down
1620     if(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE))
1621     {
1622         DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
1623         return -ENETDOWN;
1624     }
1625
1626         if (rts->disabled)
1627                 val = MAX_RTS_THRESHOLD;
1628         else if (rts->value < 0 || rts->value > MAX_RTS_THRESHOLD)
1629                 return -EINVAL;
1630         else if (rts->value == 0)
1631             val = MAX_RTS_THRESHOLD;
1632         else
1633                 val = rts->value;
1634
1635         if (val != pAdapter->CommonCfg.RtsThreshold)
1636                 pAdapter->CommonCfg.RtsThreshold = val;
1637
1638         return 0;
1639 }
1640
1641 int rt_ioctl_giwrts(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
1647         if (pAdapter == NULL)
1648         {
1649                 /* if 1st open fail, pAd will be free;
1650                    So the net_dev->priv will be NULL in 2rd open */
1651                 return -ENETDOWN;
1652         }
1653
1654         //check if the interface is down
1655         if(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE))
1656         {
1657                 DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
1658                 return -ENETDOWN;
1659         }
1660
1661         rts->value = pAdapter->CommonCfg.RtsThreshold;
1662         rts->disabled = (rts->value == MAX_RTS_THRESHOLD);
1663         rts->fixed = 1;
1664
1665         return 0;
1666 }
1667
1668 int rt_ioctl_siwfrag(struct net_device *dev,
1669                         struct iw_request_info *info,
1670                         struct iw_param *frag, char *extra)
1671 {
1672         PRTMP_ADAPTER pAdapter = RTMP_OS_NETDEV_GET_PRIV(dev);
1673         u16 val;
1674
1675         //check if the interface is down
1676         if(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE))
1677         {
1678                 DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
1679                 return -ENETDOWN;
1680         }
1681
1682         if (frag->disabled)
1683                 val = MAX_FRAG_THRESHOLD;
1684         else if (frag->value >= MIN_FRAG_THRESHOLD || frag->value <= MAX_FRAG_THRESHOLD)
1685         val = __cpu_to_le16(frag->value & ~0x1); /* even numbers only */
1686         else if (frag->value == 0)
1687             val = MAX_FRAG_THRESHOLD;
1688         else
1689                 return -EINVAL;
1690
1691         pAdapter->CommonCfg.FragmentThreshold = val;
1692         return 0;
1693 }
1694
1695 int rt_ioctl_giwfrag(struct net_device *dev,
1696                         struct iw_request_info *info,
1697                         struct iw_param *frag, char *extra)
1698 {
1699         PRTMP_ADAPTER pAdapter = RTMP_OS_NETDEV_GET_PRIV(dev);
1700
1701         if (pAdapter == NULL)
1702         {
1703                 /* if 1st open fail, pAd will be free;
1704                    So the net_dev->priv will be NULL in 2rd open */
1705                 return -ENETDOWN;
1706         }
1707
1708         //check if the interface is down
1709         if(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE))
1710         {
1711                 DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
1712                 return -ENETDOWN;
1713         }
1714
1715         frag->value = pAdapter->CommonCfg.FragmentThreshold;
1716         frag->disabled = (frag->value == MAX_FRAG_THRESHOLD);
1717         frag->fixed = 1;
1718
1719         return 0;
1720 }
1721
1722 #define MAX_WEP_KEY_SIZE 13
1723 #define MIN_WEP_KEY_SIZE 5
1724 int rt_ioctl_siwencode(struct net_device *dev,
1725                           struct iw_request_info *info,
1726                           struct iw_point *erq, char *extra)
1727 {
1728         PRTMP_ADAPTER pAdapter = RTMP_OS_NETDEV_GET_PRIV(dev);
1729
1730         //check if the interface is down
1731         if(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE))
1732         {
1733                 DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
1734                 return -ENETDOWN;
1735         }
1736
1737         if ((erq->length == 0) &&
1738         (erq->flags & IW_ENCODE_DISABLED))
1739         {
1740                 pAdapter->StaCfg.PairCipher = Ndis802_11WEPDisabled;
1741                 pAdapter->StaCfg.GroupCipher = Ndis802_11WEPDisabled;
1742                 pAdapter->StaCfg.WepStatus = Ndis802_11WEPDisabled;
1743         pAdapter->StaCfg.OrigWepStatus = pAdapter->StaCfg.WepStatus;
1744         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeOpen;
1745         goto done;
1746         }
1747         else if (erq->flags & IW_ENCODE_RESTRICTED || erq->flags & IW_ENCODE_OPEN)
1748         {
1749             //pAdapter->StaCfg.PortSecured = WPA_802_1X_PORT_SECURED;
1750                 STA_PORT_SECURED(pAdapter);
1751                 pAdapter->StaCfg.PairCipher = Ndis802_11WEPEnabled;
1752                 pAdapter->StaCfg.GroupCipher = Ndis802_11WEPEnabled;
1753                 pAdapter->StaCfg.WepStatus = Ndis802_11WEPEnabled;
1754         pAdapter->StaCfg.OrigWepStatus = pAdapter->StaCfg.WepStatus;
1755                 if (erq->flags & IW_ENCODE_RESTRICTED)
1756                         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeShared;
1757         else
1758                         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeOpen;
1759         }
1760
1761     if (erq->length > 0)
1762         {
1763                 int keyIdx = (erq->flags & IW_ENCODE_INDEX) - 1;
1764                 /* Check the size of the key */
1765                 if (erq->length > MAX_WEP_KEY_SIZE)
1766                 {
1767                         return -EINVAL;
1768                 }
1769                 /* Check key index */
1770                 if ((keyIdx < 0) || (keyIdx >= NR_WEP_KEYS))
1771         {
1772             DBGPRINT(RT_DEBUG_TRACE ,("==>rt_ioctl_siwencode::Wrong keyIdx=%d! Using default key instead (%d)\n",
1773                                         keyIdx, pAdapter->StaCfg.DefaultKeyId));
1774
1775             //Using default key
1776                         keyIdx = pAdapter->StaCfg.DefaultKeyId;
1777         }
1778                 else
1779                         pAdapter->StaCfg.DefaultKeyId = keyIdx;
1780
1781         NdisZeroMemory(pAdapter->SharedKey[BSS0][keyIdx].Key,  16);
1782
1783                 if (erq->length == MAX_WEP_KEY_SIZE)
1784         {
1785                         pAdapter->SharedKey[BSS0][keyIdx].KeyLen = MAX_WEP_KEY_SIZE;
1786             pAdapter->SharedKey[BSS0][keyIdx].CipherAlg = CIPHER_WEP128;
1787                 }
1788                 else if (erq->length == MIN_WEP_KEY_SIZE)
1789         {
1790             pAdapter->SharedKey[BSS0][keyIdx].KeyLen = MIN_WEP_KEY_SIZE;
1791             pAdapter->SharedKey[BSS0][keyIdx].CipherAlg = CIPHER_WEP64;
1792                 }
1793                 else
1794                         /* Disable the key */
1795                         pAdapter->SharedKey[BSS0][keyIdx].KeyLen = 0;
1796
1797                 /* Check if the key is not marked as invalid */
1798                 if(!(erq->flags & IW_ENCODE_NOKEY))
1799                 {
1800                         /* Copy the key in the driver */
1801                         NdisMoveMemory(pAdapter->SharedKey[BSS0][keyIdx].Key, extra, erq->length);
1802         }
1803         }
1804     else
1805                         {
1806                 /* Do we want to just set the transmit key index ? */
1807                 int index = (erq->flags & IW_ENCODE_INDEX) - 1;
1808                 if ((index >= 0) && (index < 4))
1809         {
1810                         pAdapter->StaCfg.DefaultKeyId = index;
1811             }
1812         else
1813                         /* Don't complain if only change the mode */
1814                 if (!(erq->flags & IW_ENCODE_MODE))
1815                         return -EINVAL;
1816         }
1817
1818 done:
1819     DBGPRINT(RT_DEBUG_TRACE ,("==>rt_ioctl_siwencode::erq->flags=%x\n",erq->flags));
1820         DBGPRINT(RT_DEBUG_TRACE ,("==>rt_ioctl_siwencode::AuthMode=%x\n",pAdapter->StaCfg.AuthMode));
1821         DBGPRINT(RT_DEBUG_TRACE ,("==>rt_ioctl_siwencode::DefaultKeyId=%x, KeyLen = %d\n",pAdapter->StaCfg.DefaultKeyId , pAdapter->SharedKey[BSS0][pAdapter->StaCfg.DefaultKeyId].KeyLen));
1822         DBGPRINT(RT_DEBUG_TRACE ,("==>rt_ioctl_siwencode::WepStatus=%x\n",pAdapter->StaCfg.WepStatus));
1823         return 0;
1824 }
1825
1826 int
1827 rt_ioctl_giwencode(struct net_device *dev,
1828                           struct iw_request_info *info,
1829                           struct iw_point *erq, char *key)
1830 {
1831         int kid;
1832         PRTMP_ADAPTER pAdapter = RTMP_OS_NETDEV_GET_PRIV(dev);
1833
1834         if (pAdapter == NULL)
1835         {
1836                 /* if 1st open fail, pAd will be free;
1837                    So the net_dev->priv will be NULL in 2rd open */
1838                 return -ENETDOWN;
1839         }
1840
1841         //check if the interface is down
1842         if(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE))
1843         {
1844                 DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
1845         return -ENETDOWN;
1846         }
1847
1848         kid = erq->flags & IW_ENCODE_INDEX;
1849         DBGPRINT(RT_DEBUG_TRACE, ("===>rt_ioctl_giwencode %d\n", erq->flags & IW_ENCODE_INDEX));
1850
1851         if (pAdapter->StaCfg.WepStatus == Ndis802_11WEPDisabled)
1852         {
1853                 erq->length = 0;
1854                 erq->flags = IW_ENCODE_DISABLED;
1855         }
1856         else if ((kid > 0) && (kid <=4))
1857         {
1858                 // copy wep key
1859                 erq->flags = kid ;                      /* NB: base 1 */
1860                 if (erq->length > pAdapter->SharedKey[BSS0][kid-1].KeyLen)
1861                         erq->length = pAdapter->SharedKey[BSS0][kid-1].KeyLen;
1862                 memcpy(key, pAdapter->SharedKey[BSS0][kid-1].Key, erq->length);
1863                 //if ((kid == pAdapter->PortCfg.DefaultKeyId))
1864                 //erq->flags |= IW_ENCODE_ENABLED;      /* XXX */
1865                 if (pAdapter->StaCfg.AuthMode == Ndis802_11AuthModeShared)
1866                         erq->flags |= IW_ENCODE_RESTRICTED;             /* XXX */
1867                 else
1868                         erq->flags |= IW_ENCODE_OPEN;           /* XXX */
1869
1870         }
1871         else if (kid == 0)
1872         {
1873                 if (pAdapter->StaCfg.AuthMode == Ndis802_11AuthModeShared)
1874                         erq->flags |= IW_ENCODE_RESTRICTED;             /* XXX */
1875                 else
1876                         erq->flags |= IW_ENCODE_OPEN;           /* XXX */
1877                 erq->length = pAdapter->SharedKey[BSS0][pAdapter->StaCfg.DefaultKeyId].KeyLen;
1878                 memcpy(key, pAdapter->SharedKey[BSS0][pAdapter->StaCfg.DefaultKeyId].Key, erq->length);
1879                 // copy default key ID
1880                 if (pAdapter->StaCfg.AuthMode == Ndis802_11AuthModeShared)
1881                         erq->flags |= IW_ENCODE_RESTRICTED;             /* XXX */
1882                 else
1883                         erq->flags |= IW_ENCODE_OPEN;           /* XXX */
1884                 erq->flags = pAdapter->StaCfg.DefaultKeyId + 1;                 /* NB: base 1 */
1885                 erq->flags |= IW_ENCODE_ENABLED;        /* XXX */
1886         }
1887
1888         return 0;
1889
1890 }
1891
1892 static int
1893 rt_ioctl_setparam(struct net_device *dev, struct iw_request_info *info,
1894                          void *w, char *extra)
1895 {
1896         PRTMP_ADAPTER pAdapter;
1897         POS_COOKIE pObj;
1898         PSTRING this_char = extra;
1899         PSTRING value;
1900         int  Status=0;
1901
1902         pAdapter = RTMP_OS_NETDEV_GET_PRIV(dev);
1903         if (pAdapter == NULL)
1904         {
1905                 /* if 1st open fail, pAd will be free;
1906                    So the net_dev->priv will be NULL in 2rd open */
1907                 return -ENETDOWN;
1908         }
1909
1910         pObj = (POS_COOKIE) pAdapter->OS_Cookie;
1911         {
1912                 pObj->ioctl_if_type = INT_MAIN;
1913         pObj->ioctl_if = MAIN_MBSSID;
1914         }
1915
1916         //check if the interface is down
1917         if(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE))
1918         {
1919                 DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
1920                         return -ENETDOWN;
1921         }
1922
1923         if (!*this_char)
1924                 return -EINVAL;
1925
1926         if ((value = rtstrchr(this_char, '=')) != NULL)
1927             *value++ = 0;
1928
1929         if (!value && (strcmp(this_char, "SiteSurvey") != 0))
1930             return -EINVAL;
1931         else
1932                 goto SET_PROC;
1933
1934         // reject setting nothing besides ANY ssid(ssidLen=0)
1935     if (!*value && (strcmp(this_char, "SSID") != 0))
1936         return -EINVAL;
1937
1938 SET_PROC:
1939         for (PRTMP_PRIVATE_SET_PROC = RTMP_PRIVATE_SUPPORT_PROC; PRTMP_PRIVATE_SET_PROC->name; PRTMP_PRIVATE_SET_PROC++)
1940         {
1941             if (strcmp(this_char, PRTMP_PRIVATE_SET_PROC->name) == 0)
1942             {
1943                 if(!PRTMP_PRIVATE_SET_PROC->set_proc(pAdapter, value))
1944                 {       //FALSE:Set private failed then return Invalid argument
1945                             Status = -EINVAL;
1946                 }
1947                     break;      //Exit for loop.
1948             }
1949         }
1950
1951         if(PRTMP_PRIVATE_SET_PROC->name == NULL)
1952         {  //Not found argument
1953             Status = -EINVAL;
1954             DBGPRINT(RT_DEBUG_TRACE, ("===>rt_ioctl_setparam:: (iwpriv) Not Support Set Command [%s=%s]\n", this_char, value));
1955         }
1956
1957     return Status;
1958 }
1959
1960
1961 static int
1962 rt_private_get_statistics(struct net_device *dev, struct iw_request_info *info,
1963                 struct iw_point *wrq, char *extra)
1964 {
1965         INT                             Status = 0;
1966     PRTMP_ADAPTER   pAd = RTMP_OS_NETDEV_GET_PRIV(dev);
1967
1968     if (extra == NULL)
1969     {
1970         wrq->length = 0;
1971         return -EIO;
1972     }
1973
1974     memset(extra, 0x00, IW_PRIV_SIZE_MASK);
1975     sprintf(extra, "\n\n");
1976
1977 #ifdef RALINK_ATE
1978         if (ATE_ON(pAd))
1979         {
1980             sprintf(extra+strlen(extra), "Tx success                      = %ld\n", (ULONG)pAd->ate.TxDoneCount);
1981             //sprintf(extra+strlen(extra), "Tx success without retry        = %ld\n", (ULONG)pAd->ate.TxDoneCount);
1982         }
1983         else
1984 #endif // RALINK_ATE //
1985         {
1986     sprintf(extra+strlen(extra), "Tx success                      = %ld\n", (ULONG)pAd->WlanCounters.TransmittedFragmentCount.QuadPart);
1987     sprintf(extra+strlen(extra), "Tx success without retry        = %ld\n", (ULONG)pAd->WlanCounters.TransmittedFragmentCount.QuadPart - (ULONG)pAd->WlanCounters.RetryCount.QuadPart);
1988         }
1989     sprintf(extra+strlen(extra), "Tx success after retry          = %ld\n", (ULONG)pAd->WlanCounters.RetryCount.QuadPart);
1990     sprintf(extra+strlen(extra), "Tx fail to Rcv ACK after retry  = %ld\n", (ULONG)pAd->WlanCounters.FailedCount.QuadPart);
1991     sprintf(extra+strlen(extra), "RTS Success Rcv CTS             = %ld\n", (ULONG)pAd->WlanCounters.RTSSuccessCount.QuadPart);
1992     sprintf(extra+strlen(extra), "RTS Fail Rcv CTS                = %ld\n", (ULONG)pAd->WlanCounters.RTSFailureCount.QuadPart);
1993
1994     sprintf(extra+strlen(extra), "Rx success                      = %ld\n", (ULONG)pAd->WlanCounters.ReceivedFragmentCount.QuadPart);
1995     sprintf(extra+strlen(extra), "Rx with CRC                     = %ld\n", (ULONG)pAd->WlanCounters.FCSErrorCount.QuadPart);
1996     sprintf(extra+strlen(extra), "Rx drop due to out of resource  = %ld\n", (ULONG)pAd->Counters8023.RxNoBuffer);
1997     sprintf(extra+strlen(extra), "Rx duplicate frame              = %ld\n", (ULONG)pAd->WlanCounters.FrameDuplicateCount.QuadPart);
1998
1999     sprintf(extra+strlen(extra), "False CCA (one second)          = %ld\n", (ULONG)pAd->RalinkCounters.OneSecFalseCCACnt);
2000
2001 #ifdef RALINK_ATE
2002         if (ATE_ON(pAd))
2003         {
2004                 if (pAd->ate.RxAntennaSel == 0)
2005                 {
2006                 sprintf(extra+strlen(extra), "RSSI-A                          = %ld\n", (LONG)(pAd->ate.LastRssi0 - pAd->BbpRssiToDbmDelta));
2007                         sprintf(extra+strlen(extra), "RSSI-B (if available)           = %ld\n", (LONG)(pAd->ate.LastRssi1 - pAd->BbpRssiToDbmDelta));
2008                         sprintf(extra+strlen(extra), "RSSI-C (if available)           = %ld\n\n", (LONG)(pAd->ate.LastRssi2 - pAd->BbpRssiToDbmDelta));
2009                 }
2010                 else
2011                 {
2012                 sprintf(extra+strlen(extra), "RSSI                            = %ld\n", (LONG)(pAd->ate.LastRssi0 - pAd->BbpRssiToDbmDelta));
2013                 }
2014         }
2015         else
2016 #endif // RALINK_ATE //
2017         {
2018         sprintf(extra+strlen(extra), "RSSI-A                          = %ld\n", (LONG)(pAd->StaCfg.RssiSample.LastRssi0 - pAd->BbpRssiToDbmDelta));
2019         sprintf(extra+strlen(extra), "RSSI-B (if available)           = %ld\n", (LONG)(pAd->StaCfg.RssiSample.LastRssi1 - pAd->BbpRssiToDbmDelta));
2020         sprintf(extra+strlen(extra), "RSSI-C (if available)           = %ld\n\n", (LONG)(pAd->StaCfg.RssiSample.LastRssi2 - pAd->BbpRssiToDbmDelta));
2021         }
2022 #ifdef WPA_SUPPLICANT_SUPPORT
2023     sprintf(extra+strlen(extra), "WpaSupplicantUP                 = %d\n\n", pAd->StaCfg.WpaSupplicantUP);
2024 #endif // WPA_SUPPLICANT_SUPPORT //
2025
2026
2027
2028     wrq->length = strlen(extra) + 1; // 1: size of '\0'
2029     DBGPRINT(RT_DEBUG_TRACE, ("<== rt_private_get_statistics, wrq->length = %d\n", wrq->length));
2030
2031     return Status;
2032 }
2033
2034 #ifdef DOT11_N_SUPPORT
2035 void    getBaInfo(
2036         IN      PRTMP_ADAPTER   pAd,
2037         IN      PSTRING                 pOutBuf)
2038 {
2039         INT i, j;
2040         BA_ORI_ENTRY *pOriBAEntry;
2041         BA_REC_ENTRY *pRecBAEntry;
2042
2043         for (i=0; i<MAX_LEN_OF_MAC_TABLE; i++)
2044         {
2045                 PMAC_TABLE_ENTRY pEntry = &pAd->MacTab.Content[i];
2046                 if (((pEntry->ValidAsCLI || pEntry->ValidAsApCli) && (pEntry->Sst == SST_ASSOC))
2047                         || (pEntry->ValidAsWDS) || (pEntry->ValidAsMesh))
2048                 {
2049                         sprintf(pOutBuf, "%s\n%02X:%02X:%02X:%02X:%02X:%02X (Aid = %d) (AP) -\n",
2050                 pOutBuf,
2051                                 pEntry->Addr[0], pEntry->Addr[1], pEntry->Addr[2],
2052                                 pEntry->Addr[3], pEntry->Addr[4], pEntry->Addr[5], pEntry->Aid);
2053
2054                         sprintf(pOutBuf, "%s[Recipient]\n", pOutBuf);
2055                         for (j=0; j < NUM_OF_TID; j++)
2056                         {
2057                                 if (pEntry->BARecWcidArray[j] != 0)
2058                                 {
2059                                         pRecBAEntry =&pAd->BATable.BARecEntry[pEntry->BARecWcidArray[j]];
2060                                         sprintf(pOutBuf, "%sTID=%d, BAWinSize=%d, LastIndSeq=%d, ReorderingPkts=%d\n", pOutBuf, j, pRecBAEntry->BAWinSize, pRecBAEntry->LastIndSeq, pRecBAEntry->list.qlen);
2061                                 }
2062                         }
2063                         sprintf(pOutBuf, "%s\n", pOutBuf);
2064
2065                         sprintf(pOutBuf, "%s[Originator]\n", pOutBuf);
2066                         for (j=0; j < NUM_OF_TID; j++)
2067                         {
2068                                 if (pEntry->BAOriWcidArray[j] != 0)
2069                                 {
2070                                         pOriBAEntry =&pAd->BATable.BAOriEntry[pEntry->BAOriWcidArray[j]];
2071                                         sprintf(pOutBuf, "%sTID=%d, BAWinSize=%d, StartSeq=%d, CurTxSeq=%d\n", pOutBuf, j, pOriBAEntry->BAWinSize, pOriBAEntry->Sequence, pEntry->TxSeq[j]);
2072                                 }
2073                         }
2074                         sprintf(pOutBuf, "%s\n\n", pOutBuf);
2075                 }
2076         if (strlen(pOutBuf) > (IW_PRIV_SIZE_MASK - 30))
2077                 break;
2078         }
2079
2080         return;
2081 }
2082 #endif // DOT11_N_SUPPORT //
2083
2084 static int
2085 rt_private_show(struct net_device *dev, struct iw_request_info *info,
2086                 struct iw_point *wrq, PSTRING extra)
2087 {
2088         INT                             Status = 0;
2089         PRTMP_ADAPTER   pAd;
2090         POS_COOKIE              pObj;
2091         u32             subcmd = wrq->flags;
2092
2093         pAd = RTMP_OS_NETDEV_GET_PRIV(dev);
2094         if (pAd == NULL)
2095         {
2096                 /* if 1st open fail, pAd will be free;
2097                    So the net_dev->priv will be NULL in 2rd open */
2098                 return -ENETDOWN;
2099         }
2100
2101         pObj = (POS_COOKIE) pAd->OS_Cookie;
2102         if (extra == NULL)
2103         {
2104                 wrq->length = 0;
2105                 return -EIO;
2106         }
2107         memset(extra, 0x00, IW_PRIV_SIZE_MASK);
2108
2109         {
2110                 pObj->ioctl_if_type = INT_MAIN;
2111                 pObj->ioctl_if = MAIN_MBSSID;
2112         }
2113
2114     switch(subcmd)
2115     {
2116
2117         case SHOW_CONN_STATUS:
2118             if (MONITOR_ON(pAd))
2119             {
2120 #ifdef DOT11_N_SUPPORT
2121                 if (pAd->CommonCfg.PhyMode >= PHY_11ABGN_MIXED &&
2122                     pAd->CommonCfg.RegTransmitSetting.field.BW)
2123                     sprintf(extra, "Monitor Mode(CentralChannel %d)\n", pAd->CommonCfg.CentralChannel);
2124                 else
2125 #endif // DOT11_N_SUPPORT //
2126                     sprintf(extra, "Monitor Mode(Channel %d)\n", pAd->CommonCfg.Channel);
2127             }
2128             else
2129             {
2130                 if (pAd->IndicateMediaState == NdisMediaStateConnected)
2131                 {
2132                     if (INFRA_ON(pAd))
2133                     {
2134                     sprintf(extra, "Connected(AP: %s[%02X:%02X:%02X:%02X:%02X:%02X])\n",
2135                                     pAd->CommonCfg.Ssid,
2136                                     pAd->CommonCfg.Bssid[0],
2137                                     pAd->CommonCfg.Bssid[1],
2138                                     pAd->CommonCfg.Bssid[2],
2139                                     pAd->CommonCfg.Bssid[3],
2140                                     pAd->CommonCfg.Bssid[4],
2141                                     pAd->CommonCfg.Bssid[5]);
2142                         DBGPRINT(RT_DEBUG_TRACE ,("Ssid=%s ,Ssidlen = %d\n",pAd->CommonCfg.Ssid, pAd->CommonCfg.SsidLen));
2143                 }
2144                     else if (ADHOC_ON(pAd))
2145                         sprintf(extra, "Connected\n");
2146                 }
2147                 else
2148                 {
2149                     sprintf(extra, "Disconnected\n");
2150                         DBGPRINT(RT_DEBUG_TRACE ,("ConnStatus is not connected\n"));
2151                 }
2152             }
2153             wrq->length = strlen(extra) + 1; // 1: size of '\0'
2154             break;
2155         case SHOW_DRVIER_VERION:
2156             sprintf(extra, "Driver version-%s, %s %s\n", STA_DRIVER_VERSION, __DATE__, __TIME__ );
2157             wrq->length = strlen(extra) + 1; // 1: size of '\0'
2158             break;
2159 #ifdef DOT11_N_SUPPORT
2160         case SHOW_BA_INFO:
2161             getBaInfo(pAd, extra);
2162             wrq->length = strlen(extra) + 1; // 1: size of '\0'
2163             break;
2164 #endif // DOT11_N_SUPPORT //
2165                 case SHOW_DESC_INFO:
2166                         {
2167                                 Show_DescInfo_Proc(pAd, NULL);
2168                                 wrq->length = 0; // 1: size of '\0'
2169                         }
2170                         break;
2171         case RAIO_OFF:
2172             if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_BSS_SCAN_IN_PROGRESS))
2173             {
2174                 if (pAd->Mlme.CntlMachine.CurrState != CNTL_IDLE)
2175                         {
2176                             RTMP_MLME_RESET_STATE_MACHINE(pAd);
2177                             DBGPRINT(RT_DEBUG_TRACE, ("!!! MLME busy, reset MLME state machine !!!\n"));
2178                         }
2179             }
2180             pAd->StaCfg.bSwRadio = FALSE;
2181             if (pAd->StaCfg.bRadio != (pAd->StaCfg.bHwRadio && pAd->StaCfg.bSwRadio))
2182             {
2183                 pAd->StaCfg.bRadio = (pAd->StaCfg.bHwRadio && pAd->StaCfg.bSwRadio);
2184                 if (pAd->StaCfg.bRadio == FALSE)
2185                 {
2186                     MlmeRadioOff(pAd);
2187                     // Update extra information
2188                                         pAd->ExtraInfo = SW_RADIO_OFF;
2189                 }
2190             }
2191             sprintf(extra, "Radio Off\n");
2192             wrq->length = strlen(extra) + 1; // 1: size of '\0'
2193             break;
2194         case RAIO_ON:
2195             pAd->StaCfg.bSwRadio = TRUE;
2196             //if (pAd->StaCfg.bRadio != (pAd->StaCfg.bHwRadio && pAd->StaCfg.bSwRadio))
2197             {
2198                 pAd->StaCfg.bRadio = (pAd->StaCfg.bHwRadio && pAd->StaCfg.bSwRadio);
2199                 if (pAd->StaCfg.bRadio == TRUE)
2200                 {
2201                     MlmeRadioOn(pAd);
2202                     // Update extra information
2203                                         pAd->ExtraInfo = EXTRA_INFO_CLEAR;
2204                 }
2205             }
2206             sprintf(extra, "Radio On\n");
2207             wrq->length = strlen(extra) + 1; // 1: size of '\0'
2208             break;
2209
2210
2211 #ifdef QOS_DLS_SUPPORT
2212                 case SHOW_DLS_ENTRY_INFO:
2213                         {
2214                                 Set_DlsEntryInfo_Display_Proc(pAd, NULL);
2215                                 wrq->length = 0; // 1: size of '\0'
2216                         }
2217                         break;
2218 #endif // QOS_DLS_SUPPORT //
2219
2220                 case SHOW_CFG_VALUE:
2221                         {
2222                                 Status = RTMPShowCfgValue(pAd, (PSTRING) wrq->pointer, extra);
2223                                 if (Status == 0)
2224                                         wrq->length = strlen(extra) + 1; // 1: size of '\0'
2225                         }
2226                         break;
2227                 case SHOW_ADHOC_ENTRY_INFO:
2228                         Show_Adhoc_MacTable_Proc(pAd, extra);
2229                         wrq->length = strlen(extra) + 1; // 1: size of '\0'
2230                         break;
2231         default:
2232             DBGPRINT(RT_DEBUG_TRACE, ("%s - unknow subcmd = %d\n", __FUNCTION__, subcmd));
2233             break;
2234     }
2235
2236     return Status;
2237 }
2238
2239 #ifdef SIOCSIWMLME
2240 int rt_ioctl_siwmlme(struct net_device *dev,
2241                            struct iw_request_info *info,
2242                            union iwreq_data *wrqu,
2243                            char *extra)
2244 {
2245         PRTMP_ADAPTER   pAd = RTMP_OS_NETDEV_GET_PRIV(dev);
2246         struct iw_mlme *pMlme = (struct iw_mlme *)wrqu->data.pointer;
2247         MLME_QUEUE_ELEM                         MsgElem;
2248         MLME_DISASSOC_REQ_STRUCT        DisAssocReq;
2249         MLME_DEAUTH_REQ_STRUCT      DeAuthReq;
2250
2251         DBGPRINT(RT_DEBUG_TRACE, ("====> %s\n", __FUNCTION__));
2252
2253         if (pMlme == NULL)
2254                 return -EINVAL;
2255
2256         switch(pMlme->cmd)
2257         {
2258 #ifdef IW_MLME_DEAUTH
2259                 case IW_MLME_DEAUTH:
2260                         DBGPRINT(RT_DEBUG_TRACE, ("====> %s - IW_MLME_DEAUTH\n", __FUNCTION__));
2261                         COPY_MAC_ADDR(DeAuthReq.Addr, pAd->CommonCfg.Bssid);
2262                         DeAuthReq.Reason = pMlme->reason_code;
2263                         MsgElem.MsgLen = sizeof(MLME_DEAUTH_REQ_STRUCT);
2264                         NdisMoveMemory(MsgElem.Msg, &DeAuthReq, sizeof(MLME_DEAUTH_REQ_STRUCT));
2265                         MlmeDeauthReqAction(pAd, &MsgElem);
2266                         if (INFRA_ON(pAd))
2267                         {
2268                             LinkDown(pAd, FALSE);
2269                             pAd->Mlme.AssocMachine.CurrState = ASSOC_IDLE;
2270                         }
2271                         break;
2272 #endif // IW_MLME_DEAUTH //
2273 #ifdef IW_MLME_DISASSOC
2274                 case IW_MLME_DISASSOC:
2275                         DBGPRINT(RT_DEBUG_TRACE, ("====> %s - IW_MLME_DISASSOC\n", __FUNCTION__));
2276                         COPY_MAC_ADDR(DisAssocReq.Addr, pAd->CommonCfg.Bssid);
2277                         DisAssocReq.Reason =  pMlme->reason_code;
2278
2279                         MsgElem.Machine = ASSOC_STATE_MACHINE;
2280                         MsgElem.MsgType = MT2_MLME_DISASSOC_REQ;
2281                         MsgElem.MsgLen = sizeof(MLME_DISASSOC_REQ_STRUCT);
2282                         NdisMoveMemory(MsgElem.Msg, &DisAssocReq, sizeof(MLME_DISASSOC_REQ_STRUCT));
2283
2284                         pAd->Mlme.CntlMachine.CurrState = CNTL_WAIT_OID_DISASSOC;
2285                         MlmeDisassocReqAction(pAd, &MsgElem);
2286                         break;
2287 #endif // IW_MLME_DISASSOC //
2288                 default:
2289                         DBGPRINT(RT_DEBUG_TRACE, ("====> %s - Unknow Command\n", __FUNCTION__));
2290                         break;
2291         }
2292
2293         return 0;
2294 }
2295 #endif // SIOCSIWMLME //
2296
2297 #if WIRELESS_EXT > 17
2298 int rt_ioctl_siwauth(struct net_device *dev,
2299                           struct iw_request_info *info,
2300                           union iwreq_data *wrqu, char *extra)
2301 {
2302         PRTMP_ADAPTER   pAdapter = RTMP_OS_NETDEV_GET_PRIV(dev);
2303         struct iw_param *param = &wrqu->param;
2304
2305     //check if the interface is down
2306         if(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE))
2307         {
2308                 DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
2309         return -ENETDOWN;
2310         }
2311         switch (param->flags & IW_AUTH_INDEX) {
2312         case IW_AUTH_WPA_VERSION:
2313             if (param->value == IW_AUTH_WPA_VERSION_WPA)
2314             {
2315                 pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeWPAPSK;
2316                                 if (pAdapter->StaCfg.BssType == BSS_ADHOC)
2317                                         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeWPANone;
2318             }
2319             else if (param->value == IW_AUTH_WPA_VERSION_WPA2)
2320                 pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeWPA2PSK;
2321
2322             DBGPRINT(RT_DEBUG_TRACE, ("%s::IW_AUTH_WPA_VERSION - param->value = %d!\n", __FUNCTION__, param->value));
2323             break;
2324         case IW_AUTH_CIPHER_PAIRWISE:
2325             if (param->value == IW_AUTH_CIPHER_NONE)
2326             {
2327                 pAdapter->StaCfg.WepStatus = Ndis802_11WEPDisabled;
2328                 pAdapter->StaCfg.OrigWepStatus = pAdapter->StaCfg.WepStatus;
2329                 pAdapter->StaCfg.PairCipher = Ndis802_11WEPDisabled;
2330             }
2331             else if (param->value == IW_AUTH_CIPHER_WEP40 ||
2332                      param->value == IW_AUTH_CIPHER_WEP104)
2333             {
2334                 pAdapter->StaCfg.WepStatus = Ndis802_11WEPEnabled;
2335                 pAdapter->StaCfg.OrigWepStatus = pAdapter->StaCfg.WepStatus;
2336                 pAdapter->StaCfg.PairCipher = Ndis802_11WEPEnabled;
2337 #ifdef WPA_SUPPLICANT_SUPPORT
2338                 pAdapter->StaCfg.IEEE8021X = FALSE;
2339 #endif // WPA_SUPPLICANT_SUPPORT //
2340             }
2341             else if (param->value == IW_AUTH_CIPHER_TKIP)
2342             {
2343                 pAdapter->StaCfg.WepStatus = Ndis802_11Encryption2Enabled;
2344                 pAdapter->StaCfg.OrigWepStatus = pAdapter->StaCfg.WepStatus;
2345                 pAdapter->StaCfg.PairCipher = Ndis802_11Encryption2Enabled;
2346             }
2347             else if (param->value == IW_AUTH_CIPHER_CCMP)
2348             {
2349                 pAdapter->StaCfg.WepStatus = Ndis802_11Encryption3Enabled;
2350                 pAdapter->StaCfg.OrigWepStatus = pAdapter->StaCfg.WepStatus;
2351                 pAdapter->StaCfg.PairCipher = Ndis802_11Encryption3Enabled;
2352             }
2353             DBGPRINT(RT_DEBUG_TRACE, ("%s::IW_AUTH_CIPHER_PAIRWISE - param->value = %d!\n", __FUNCTION__, param->value));
2354             break;
2355         case IW_AUTH_CIPHER_GROUP:
2356             if (param->value == IW_AUTH_CIPHER_NONE)
2357             {
2358                 pAdapter->StaCfg.GroupCipher = Ndis802_11WEPDisabled;
2359             }
2360             else if (param->value == IW_AUTH_CIPHER_WEP40 ||
2361                      param->value == IW_AUTH_CIPHER_WEP104)
2362             {
2363                 pAdapter->StaCfg.GroupCipher = Ndis802_11WEPEnabled;
2364             }
2365             else if (param->value == IW_AUTH_CIPHER_TKIP)
2366             {
2367                 pAdapter->StaCfg.GroupCipher = Ndis802_11Encryption2Enabled;
2368             }
2369             else if (param->value == IW_AUTH_CIPHER_CCMP)
2370             {
2371                 pAdapter->StaCfg.GroupCipher = Ndis802_11Encryption3Enabled;
2372             }
2373             DBGPRINT(RT_DEBUG_TRACE, ("%s::IW_AUTH_CIPHER_GROUP - param->value = %d!\n", __FUNCTION__, param->value));
2374             break;
2375         case IW_AUTH_KEY_MGMT:
2376             if (param->value == IW_AUTH_KEY_MGMT_802_1X)
2377             {
2378                 if (pAdapter->StaCfg.AuthMode == Ndis802_11AuthModeWPAPSK)
2379                 {
2380                     pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeWPA;
2381 #ifdef WPA_SUPPLICANT_SUPPORT
2382                     pAdapter->StaCfg.IEEE8021X = FALSE;
2383 #endif // WPA_SUPPLICANT_SUPPORT //
2384                 }
2385                 else if (pAdapter->StaCfg.AuthMode == Ndis802_11AuthModeWPA2PSK)
2386                 {
2387                     pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeWPA2;
2388 #ifdef WPA_SUPPLICANT_SUPPORT
2389                     pAdapter->StaCfg.IEEE8021X = FALSE;
2390 #endif // WPA_SUPPLICANT_SUPPORT //
2391                 }
2392 #ifdef WPA_SUPPLICANT_SUPPORT
2393                 else
2394                     // WEP 1x
2395                     pAdapter->StaCfg.IEEE8021X = TRUE;
2396 #endif // WPA_SUPPLICANT_SUPPORT //
2397             }
2398             else if (param->value == 0)
2399             {
2400                 //pAdapter->StaCfg.PortSecured = WPA_802_1X_PORT_SECURED;
2401                                 STA_PORT_SECURED(pAdapter);
2402             }
2403             DBGPRINT(RT_DEBUG_TRACE, ("%s::IW_AUTH_KEY_MGMT - param->value = %d!\n", __FUNCTION__, param->value));
2404             break;
2405         case IW_AUTH_RX_UNENCRYPTED_EAPOL:
2406             break;
2407         case IW_AUTH_PRIVACY_INVOKED:
2408             /*if (param->value == 0)
2409                         {
2410                 pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeOpen;
2411                 pAdapter->StaCfg.WepStatus = Ndis802_11WEPDisabled;
2412                 pAdapter->StaCfg.OrigWepStatus = pAdapter->StaCfg.WepStatus;
2413                 pAdapter->StaCfg.PairCipher = Ndis802_11WEPDisabled;
2414                     pAdapter->StaCfg.GroupCipher = Ndis802_11WEPDisabled;
2415             }*/
2416             DBGPRINT(RT_DEBUG_TRACE, ("%s::IW_AUTH_PRIVACY_INVOKED - param->value = %d!\n", __FUNCTION__, param->value));
2417                 break;
2418         case IW_AUTH_DROP_UNENCRYPTED:
2419             if (param->value != 0)
2420                 pAdapter->StaCfg.PortSecured = WPA_802_1X_PORT_NOT_SECURED;
2421                         else
2422                         {
2423                 //pAdapter->StaCfg.PortSecured = WPA_802_1X_PORT_SECURED;
2424                                 STA_PORT_SECURED(pAdapter);
2425                         }
2426             DBGPRINT(RT_DEBUG_TRACE, ("%s::IW_AUTH_WPA_VERSION - param->value = %d!\n", __FUNCTION__, param->value));
2427                 break;
2428         case IW_AUTH_80211_AUTH_ALG:
2429                         if (param->value & IW_AUTH_ALG_SHARED_KEY)
2430             {
2431                                 pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeShared;
2432                         }
2433             else if (param->value & IW_AUTH_ALG_OPEN_SYSTEM)
2434             {
2435                                 pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeOpen;
2436                         }
2437             else
2438                                 return -EINVAL;
2439             DBGPRINT(RT_DEBUG_TRACE, ("%s::IW_AUTH_80211_AUTH_ALG - param->value = %d!\n", __FUNCTION__, param->value));
2440                         break;
2441         case IW_AUTH_WPA_ENABLED:
2442                 DBGPRINT(RT_DEBUG_TRACE, ("%s::IW_AUTH_WPA_ENABLED - Driver supports WPA!(param->value = %d)\n", __FUNCTION__, param->value));
2443                 break;
2444         default:
2445                 return -EOPNOTSUPP;
2446 }
2447
2448         return 0;
2449 }
2450
2451 int rt_ioctl_giwauth(struct net_device *dev,
2452                                struct iw_request_info *info,
2453                                union iwreq_data *wrqu, char *extra)
2454 {
2455         PRTMP_ADAPTER   pAdapter = RTMP_OS_NETDEV_GET_PRIV(dev);
2456         struct iw_param *param = &wrqu->param;
2457
2458     //check if the interface is down
2459         if(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE))
2460     {
2461                 DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
2462         return -ENETDOWN;
2463     }
2464
2465         switch (param->flags & IW_AUTH_INDEX) {
2466         case IW_AUTH_DROP_UNENCRYPTED:
2467         param->value = (pAdapter->StaCfg.WepStatus == Ndis802_11WEPDisabled) ? 0 : 1;
2468                 break;
2469
2470         case IW_AUTH_80211_AUTH_ALG:
2471         param->value = (pAdapter->StaCfg.AuthMode == Ndis802_11AuthModeShared) ? IW_AUTH_ALG_SHARED_KEY : IW_AUTH_ALG_OPEN_SYSTEM;
2472                 break;
2473
2474         case IW_AUTH_WPA_ENABLED:
2475                 param->value = (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA) ? 1 : 0;
2476                 break;
2477
2478         default:
2479                 return -EOPNOTSUPP;
2480         }
2481     DBGPRINT(RT_DEBUG_TRACE, ("rt_ioctl_giwauth::param->value = %d!\n", param->value));
2482         return 0;
2483 }
2484
2485 void fnSetCipherKey(
2486     IN  PRTMP_ADAPTER   pAdapter,
2487     IN  INT             keyIdx,
2488     IN  UCHAR           CipherAlg,
2489     IN  BOOLEAN         bGTK,
2490     IN  struct iw_encode_ext *ext)
2491 {
2492     NdisZeroMemory(&pAdapter->SharedKey[BSS0][keyIdx], sizeof(CIPHER_KEY));
2493     pAdapter->SharedKey[BSS0][keyIdx].KeyLen = LEN_TKIP_EK;
2494     NdisMoveMemory(pAdapter->SharedKey[BSS0][keyIdx].Key, ext->key, LEN_TKIP_EK);
2495     NdisMoveMemory(pAdapter->SharedKey[BSS0][keyIdx].TxMic, ext->key + LEN_TKIP_EK, LEN_TKIP_TXMICK);
2496     NdisMoveMemory(pAdapter->SharedKey[BSS0][keyIdx].RxMic, ext->key + LEN_TKIP_EK + LEN_TKIP_TXMICK, LEN_TKIP_RXMICK);
2497     pAdapter->SharedKey[BSS0][keyIdx].CipherAlg = CipherAlg;
2498
2499     // Update group key information to ASIC Shared Key Table
2500         AsicAddSharedKeyEntry(pAdapter,
2501                                                   BSS0,
2502                                                   keyIdx,
2503                                                   pAdapter->SharedKey[BSS0][keyIdx].CipherAlg,
2504                                                   pAdapter->SharedKey[BSS0][keyIdx].Key,
2505                                                   pAdapter->SharedKey[BSS0][keyIdx].TxMic,
2506                                                   pAdapter->SharedKey[BSS0][keyIdx].RxMic);
2507
2508     if (bGTK)
2509         // Update ASIC WCID attribute table and IVEIV table
2510         RTMPAddWcidAttributeEntry(pAdapter,
2511                                                           BSS0,
2512                                                           keyIdx,
2513                                                           pAdapter->SharedKey[BSS0][keyIdx].CipherAlg,
2514                                                           NULL);
2515     else
2516         // Update ASIC WCID attribute table and IVEIV table
2517         RTMPAddWcidAttributeEntry(pAdapter,
2518                                                           BSS0,
2519                                                           keyIdx,
2520                                                           pAdapter->SharedKey[BSS0][keyIdx].CipherAlg,
2521                                                           &pAdapter->MacTab.Content[BSSID_WCID]);
2522 }
2523
2524 int rt_ioctl_siwencodeext(struct net_device *dev,
2525                            struct iw_request_info *info,
2526                            union iwreq_data *wrqu,
2527                            char *extra)
2528                         {
2529     PRTMP_ADAPTER   pAdapter = RTMP_OS_NETDEV_GET_PRIV(dev);
2530         struct iw_point *encoding = &wrqu->encoding;
2531         struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
2532     int keyIdx, alg = ext->alg;
2533
2534     //check if the interface is down
2535         if(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE))
2536         {
2537                 DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
2538         return -ENETDOWN;
2539         }
2540
2541     if (encoding->flags & IW_ENCODE_DISABLED)
2542         {
2543         keyIdx = (encoding->flags & IW_ENCODE_INDEX) - 1;
2544         // set BSSID wcid entry of the Pair-wise Key table as no-security mode
2545             AsicRemovePairwiseKeyEntry(pAdapter, BSS0, BSSID_WCID);
2546         pAdapter->SharedKey[BSS0][keyIdx].KeyLen = 0;
2547                 pAdapter->SharedKey[BSS0][keyIdx].CipherAlg = CIPHER_NONE;
2548                 AsicRemoveSharedKeyEntry(pAdapter, 0, (UCHAR)keyIdx);
2549         NdisZeroMemory(&pAdapter->SharedKey[BSS0][keyIdx], sizeof(CIPHER_KEY));
2550         DBGPRINT(RT_DEBUG_TRACE, ("%s::Remove all keys!(encoding->flags = %x)\n", __FUNCTION__, encoding->flags));
2551     }
2552                                         else
2553     {
2554         // Get Key Index and convet to our own defined key index
2555         keyIdx = (encoding->flags & IW_ENCODE_INDEX) - 1;
2556         if((keyIdx < 0) || (keyIdx >= NR_WEP_KEYS))
2557                 return -EINVAL;
2558
2559         if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY)
2560         {
2561             pAdapter->StaCfg.DefaultKeyId = keyIdx;
2562             DBGPRINT(RT_DEBUG_TRACE, ("%s::DefaultKeyId = %d\n", __FUNCTION__, pAdapter->StaCfg.DefaultKeyId));
2563         }
2564
2565         switch (alg) {
2566                 case IW_ENCODE_ALG_NONE:
2567                 DBGPRINT(RT_DEBUG_TRACE, ("%s::IW_ENCODE_ALG_NONE\n", __FUNCTION__));
2568                         break;
2569                 case IW_ENCODE_ALG_WEP:
2570                 DBGPRINT(RT_DEBUG_TRACE, ("%s::IW_ENCODE_ALG_WEP - ext->key_len = %d, keyIdx = %d\n", __FUNCTION__, ext->key_len, keyIdx));
2571                         if (ext->key_len == MAX_WEP_KEY_SIZE)
2572                 {
2573                                 pAdapter->SharedKey[BSS0][keyIdx].KeyLen = MAX_WEP_KEY_SIZE;
2574                     pAdapter->SharedKey[BSS0][keyIdx].CipherAlg = CIPHER_WEP128;
2575                                 }
2576                         else if (ext->key_len == MIN_WEP_KEY_SIZE)
2577                 {
2578                     pAdapter->SharedKey[BSS0][keyIdx].KeyLen = MIN_WEP_KEY_SIZE;
2579                     pAdapter->SharedKey[BSS0][keyIdx].CipherAlg = CIPHER_WEP64;
2580                         }
2581                         else
2582                     return -EINVAL;
2583
2584                 NdisZeroMemory(pAdapter->SharedKey[BSS0][keyIdx].Key,  16);
2585                             NdisMoveMemory(pAdapter->SharedKey[BSS0][keyIdx].Key, ext->key, ext->key_len);
2586
2587                                 if (pAdapter->StaCfg.GroupCipher == Ndis802_11GroupWEP40Enabled ||
2588                                         pAdapter->StaCfg.GroupCipher == Ndis802_11GroupWEP104Enabled)
2589                                 {
2590                                         // Set Group key material to Asic
2591                                         AsicAddSharedKeyEntry(pAdapter, BSS0, keyIdx, pAdapter->SharedKey[BSS0][keyIdx].CipherAlg, pAdapter->SharedKey[BSS0][keyIdx].Key, NULL, NULL);
2592                                         // Update WCID attribute table and IVEIV table for this group key table
2593                                         RTMPAddWcidAttributeEntry(pAdapter, BSS0, keyIdx, pAdapter->SharedKey[BSS0][keyIdx].CipherAlg, NULL);
2594                                         STA_PORT_SECURED(pAdapter);
2595                                         // Indicate Connected for GUI
2596                                         pAdapter->IndicateMediaState = NdisMediaStateConnected;
2597                                 }
2598                         break;
2599             case IW_ENCODE_ALG_TKIP:
2600                 DBGPRINT(RT_DEBUG_TRACE, ("%s::IW_ENCODE_ALG_TKIP - keyIdx = %d, ext->key_len = %d\n", __FUNCTION__, keyIdx, ext->key_len));
2601                 if (ext->key_len == 32)
2602                 {
2603                     if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY)
2604                     {
2605                         fnSetCipherKey(pAdapter, keyIdx, CIPHER_TKIP, FALSE, ext);
2606                         if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA2)
2607                         {
2608                             //pAdapter->StaCfg.PortSecured = WPA_802_1X_PORT_SECURED;
2609                             STA_PORT_SECURED(pAdapter);
2610                             pAdapter->IndicateMediaState = NdisMediaStateConnected;
2611                         }
2612                 }
2613                     else if (ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY)
2614                     {
2615                         fnSetCipherKey(pAdapter, keyIdx, CIPHER_TKIP, TRUE, ext);
2616
2617                         // set 802.1x port control
2618                         //pAdapter->StaCfg.PortSecured = WPA_802_1X_PORT_SECURED;
2619                         STA_PORT_SECURED(pAdapter);
2620                         pAdapter->IndicateMediaState = NdisMediaStateConnected;
2621                     }
2622                 }
2623                 else
2624                     return -EINVAL;
2625                 break;
2626             case IW_ENCODE_ALG_CCMP:
2627                 if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY)
2628                 {
2629                     fnSetCipherKey(pAdapter, keyIdx, CIPHER_AES, FALSE, ext);
2630                     if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA2)
2631                         //pAdapter->StaCfg.PortSecured = WPA_802_1X_PORT_SECURED;
2632                         STA_PORT_SECURED(pAdapter);
2633                         pAdapter->IndicateMediaState = NdisMediaStateConnected;
2634                 }
2635                 else if (ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY)
2636                 {
2637                     fnSetCipherKey(pAdapter, keyIdx, CIPHER_AES, TRUE, ext);
2638
2639                     // set 802.1x port control
2640                         //pAdapter->StaCfg.PortSecured = WPA_802_1X_PORT_SECURED;
2641                         STA_PORT_SECURED(pAdapter);
2642                         pAdapter->IndicateMediaState = NdisMediaStateConnected;
2643                 }
2644                 break;
2645                 default:
2646                         return -EINVAL;
2647                 }
2648     }
2649
2650     return 0;
2651 }
2652
2653 int
2654 rt_ioctl_giwencodeext(struct net_device *dev,
2655                           struct iw_request_info *info,
2656                           union iwreq_data *wrqu, char *extra)
2657 {
2658         PRTMP_ADAPTER pAd = RTMP_OS_NETDEV_GET_PRIV(dev);
2659         PCHAR pKey = NULL;
2660         struct iw_point *encoding = &wrqu->encoding;
2661         struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
2662         int idx, max_key_len;
2663
2664         DBGPRINT(RT_DEBUG_TRACE ,("===> rt_ioctl_giwencodeext\n"));
2665
2666         max_key_len = encoding->length - sizeof(*ext);
2667         if (max_key_len < 0)
2668                 return -EINVAL;
2669
2670         idx = encoding->flags & IW_ENCODE_INDEX;
2671         if (idx)
2672         {
2673                 if (idx < 1 || idx > 4)
2674                         return -EINVAL;
2675                 idx--;
2676
2677                 if ((pAd->StaCfg.WepStatus == Ndis802_11Encryption2Enabled) ||
2678                         (pAd->StaCfg.WepStatus == Ndis802_11Encryption3Enabled))
2679                 {
2680                         if (idx != pAd->StaCfg.DefaultKeyId)
2681                         {
2682                                 ext->key_len = 0;
2683                                 return 0;
2684                         }
2685                 }
2686         }
2687         else
2688                 idx = pAd->StaCfg.DefaultKeyId;
2689
2690         encoding->flags = idx + 1;
2691         memset(ext, 0, sizeof(*ext));
2692
2693         ext->key_len = 0;
2694         switch(pAd->StaCfg.WepStatus) {
2695                 case Ndis802_11WEPDisabled:
2696                         ext->alg = IW_ENCODE_ALG_NONE;
2697                         encoding->flags |= IW_ENCODE_DISABLED;
2698                         break;
2699                 case Ndis802_11WEPEnabled:
2700                         ext->alg = IW_ENCODE_ALG_WEP;
2701                         if (pAd->SharedKey[BSS0][idx].KeyLen > max_key_len)
2702                                 return -E2BIG;
2703                         else
2704                         {
2705                                 ext->key_len = pAd->SharedKey[BSS0][idx].KeyLen;
2706                                 pKey = (PCHAR)&(pAd->SharedKey[BSS0][idx].Key[0]);
2707                         }
2708                         break;
2709                 case Ndis802_11Encryption2Enabled:
2710                 case Ndis802_11Encryption3Enabled:
2711                         if (pAd->StaCfg.WepStatus == Ndis802_11Encryption2Enabled)
2712                                 ext->alg = IW_ENCODE_ALG_TKIP;
2713                         else
2714                                 ext->alg = IW_ENCODE_ALG_CCMP;
2715
2716                         if (max_key_len < 32)
2717                                 return -E2BIG;
2718                         else
2719                         {
2720                                 ext->key_len = 32;
2721                                 pKey = (PCHAR)&pAd->StaCfg.PMK[0];
2722                         }
2723                         break;
2724                 default:
2725                         return -EINVAL;
2726         }
2727
2728         if (ext->key_len && pKey)
2729         {
2730                 encoding->flags |= IW_ENCODE_ENABLED;
2731                 memcpy(ext->key, pKey, ext->key_len);
2732         }
2733
2734         return 0;
2735 }
2736
2737 #ifdef SIOCSIWGENIE
2738 int rt_ioctl_siwgenie(struct net_device *dev,
2739                           struct iw_request_info *info,
2740                           union iwreq_data *wrqu, char *extra)
2741 {
2742         PRTMP_ADAPTER   pAd = RTMP_OS_NETDEV_GET_PRIV(dev);
2743
2744         DBGPRINT(RT_DEBUG_TRACE ,("===> rt_ioctl_siwgenie\n"));
2745 #ifdef NATIVE_WPA_SUPPLICANT_SUPPORT
2746         pAd->StaCfg.bRSN_IE_FromWpaSupplicant = FALSE;
2747 #endif // NATIVE_WPA_SUPPLICANT_SUPPORT //
2748         if (wrqu->data.length > MAX_LEN_OF_RSNIE ||
2749             (wrqu->data.length && extra == NULL))
2750                 return -EINVAL;
2751
2752         if (wrqu->data.length)
2753         {
2754                 pAd->StaCfg.RSNIE_Len = wrqu->data.length;
2755                 NdisMoveMemory(&pAd->StaCfg.RSN_IE[0], extra, pAd->StaCfg.RSNIE_Len);
2756 #ifdef NATIVE_WPA_SUPPLICANT_SUPPORT
2757                 pAd->StaCfg.bRSN_IE_FromWpaSupplicant = TRUE;
2758 #endif // NATIVE_WPA_SUPPLICANT_SUPPORT //
2759         }
2760         else
2761         {
2762                 pAd->StaCfg.RSNIE_Len = 0;
2763                 NdisZeroMemory(&pAd->StaCfg.RSN_IE[0], MAX_LEN_OF_RSNIE);
2764         }
2765
2766         return 0;
2767 }
2768 #endif // SIOCSIWGENIE //
2769
2770 int rt_ioctl_giwgenie(struct net_device *dev,
2771                                struct iw_request_info *info,
2772                                union iwreq_data *wrqu, char *extra)
2773 {
2774         PRTMP_ADAPTER   pAd = RTMP_OS_NETDEV_GET_PRIV(dev);
2775
2776         if ((pAd->StaCfg.RSNIE_Len == 0) ||
2777                 (pAd->StaCfg.AuthMode < Ndis802_11AuthModeWPA))
2778         {
2779                 wrqu->data.length = 0;
2780                 return 0;
2781         }
2782
2783 #ifdef NATIVE_WPA_SUPPLICANT_SUPPORT
2784 #ifdef SIOCSIWGENIE
2785         if (pAd->StaCfg.WpaSupplicantUP == WPA_SUPPLICANT_ENABLE)
2786         {
2787         if (wrqu->data.length < pAd->StaCfg.RSNIE_Len)
2788                 return -E2BIG;
2789
2790         wrqu->data.length = pAd->StaCfg.RSNIE_Len;
2791         memcpy(extra, &pAd->StaCfg.RSN_IE[0], pAd->StaCfg.RSNIE_Len);
2792         }
2793         else
2794 #endif // SIOCSIWGENIE //
2795 #endif // NATIVE_WPA_SUPPLICANT_SUPPORT //
2796         {
2797                 UCHAR RSNIe = IE_WPA;
2798
2799                 if (wrqu->data.length < (pAd->StaCfg.RSNIE_Len + 2)) // ID, Len
2800                         return -E2BIG;
2801                 wrqu->data.length = pAd->StaCfg.RSNIE_Len + 2;
2802
2803                 if ((pAd->StaCfg.AuthMode == Ndis802_11AuthModeWPA2PSK) ||
2804             (pAd->StaCfg.AuthMode == Ndis802_11AuthModeWPA2))
2805                         RSNIe = IE_RSN;
2806
2807                 extra[0] = (char)RSNIe;
2808                 extra[1] = pAd->StaCfg.RSNIE_Len;
2809                 memcpy(extra+2, &pAd->StaCfg.RSN_IE[0], pAd->StaCfg.RSNIE_Len);
2810         }
2811
2812         return 0;
2813 }
2814
2815 int rt_ioctl_siwpmksa(struct net_device *dev,
2816                            struct iw_request_info *info,
2817                            union iwreq_data *wrqu,
2818                            char *extra)
2819 {
2820         PRTMP_ADAPTER   pAd = RTMP_OS_NETDEV_GET_PRIV(dev);
2821         struct iw_pmksa *pPmksa = (struct iw_pmksa *)wrqu->data.pointer;
2822         INT     CachedIdx = 0, idx = 0;
2823
2824         if (pPmksa == NULL)
2825                 return -EINVAL;
2826
2827         DBGPRINT(RT_DEBUG_TRACE ,("===> rt_ioctl_siwpmksa\n"));
2828         switch(pPmksa->cmd)
2829         {
2830                 case IW_PMKSA_FLUSH:
2831                         NdisZeroMemory(pAd->StaCfg.SavedPMK, sizeof(BSSID_INFO)*PMKID_NO);
2832                         DBGPRINT(RT_DEBUG_TRACE ,("rt_ioctl_siwpmksa - IW_PMKSA_FLUSH\n"));
2833                         break;
2834                 case IW_PMKSA_REMOVE:
2835                         for (CachedIdx = 0; CachedIdx < pAd->StaCfg.SavedPMKNum; CachedIdx++)
2836                         {
2837                         // compare the BSSID
2838                         if (NdisEqualMemory(pPmksa->bssid.sa_data, pAd->StaCfg.SavedPMK[CachedIdx].BSSID, MAC_ADDR_LEN))
2839                         {
2840                                 NdisZeroMemory(pAd->StaCfg.SavedPMK[CachedIdx].BSSID, MAC_ADDR_LEN);
2841                                         NdisZeroMemory(pAd->StaCfg.SavedPMK[CachedIdx].PMKID, 16);
2842                                         for (idx = CachedIdx; idx < (pAd->StaCfg.SavedPMKNum - 1); idx++)
2843                                         {
2844                                                 NdisMoveMemory(&pAd->StaCfg.SavedPMK[idx].BSSID[0], &pAd->StaCfg.SavedPMK[idx+1].BSSID[0], MAC_ADDR_LEN);
2845                                                 NdisMoveMemory(&pAd->StaCfg.SavedPMK[idx].PMKID[0], &pAd->StaCfg.SavedPMK[idx+1].PMKID[0], 16);
2846                                         }
2847                                         pAd->StaCfg.SavedPMKNum--;
2848                                 break;
2849                         }
2850                 }
2851
2852                         DBGPRINT(RT_DEBUG_TRACE ,("rt_ioctl_siwpmksa - IW_PMKSA_REMOVE\n"));
2853                         break;
2854                 case IW_PMKSA_ADD:
2855                         for (CachedIdx = 0; CachedIdx < pAd->StaCfg.SavedPMKNum; CachedIdx++)
2856                         {
2857                         // compare the BSSID
2858                         if (NdisEqualMemory(pPmksa->bssid.sa_data, pAd->StaCfg.SavedPMK[CachedIdx].BSSID, MAC_ADDR_LEN))
2859                                 break;
2860                 }
2861
2862                 // Found, replace it
2863                 if (CachedIdx < PMKID_NO)
2864                 {
2865                         DBGPRINT(RT_DEBUG_OFF, ("Update PMKID, idx = %d\n", CachedIdx));
2866                         NdisMoveMemory(&pAd->StaCfg.SavedPMK[CachedIdx].BSSID[0], pPmksa->bssid.sa_data, MAC_ADDR_LEN);
2867                                 NdisMoveMemory(&pAd->StaCfg.SavedPMK[CachedIdx].PMKID[0], pPmksa->pmkid, 16);
2868                         pAd->StaCfg.SavedPMKNum++;
2869                 }
2870                 // Not found, replace the last one
2871                 else
2872                 {
2873                         // Randomly replace one
2874                         CachedIdx = (pPmksa->bssid.sa_data[5] % PMKID_NO);
2875                         DBGPRINT(RT_DEBUG_OFF, ("Update PMKID, idx = %d\n", CachedIdx));
2876                         NdisMoveMemory(&pAd->StaCfg.SavedPMK[CachedIdx].BSSID[0], pPmksa->bssid.sa_data, MAC_ADDR_LEN);
2877                                 NdisMoveMemory(&pAd->StaCfg.SavedPMK[CachedIdx].PMKID[0], pPmksa->pmkid, 16);
2878                 }
2879
2880                         DBGPRINT(RT_DEBUG_TRACE ,("rt_ioctl_siwpmksa - IW_PMKSA_ADD\n"));
2881                         break;
2882                 default:
2883                         DBGPRINT(RT_DEBUG_TRACE ,("rt_ioctl_siwpmksa - Unknow Command!!\n"));
2884                         break;
2885         }
2886
2887         return 0;
2888 }
2889 #endif // #if WIRELESS_EXT > 17
2890
2891 int rt_ioctl_siwrate(struct net_device *dev,
2892                         struct iw_request_info *info,
2893                         union iwreq_data *wrqu, char *extra)
2894 {
2895     PRTMP_ADAPTER   pAd = RTMP_OS_NETDEV_GET_PRIV(dev);
2896     UINT32          rate = wrqu->bitrate.value, fixed = wrqu->bitrate.fixed;
2897
2898     //check if the interface is down
2899         if(!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_INTERRUPT_IN_USE))
2900         {
2901                 DBGPRINT(RT_DEBUG_TRACE, ("rt_ioctl_siwrate::Network is down!\n"));
2902         return -ENETDOWN;
2903         }
2904
2905     DBGPRINT(RT_DEBUG_TRACE, ("rt_ioctl_siwrate::(rate = %d, fixed = %d)\n", rate, fixed));
2906     /* rate = -1 => auto rate
2907        rate = X, fixed = 1 => (fixed rate X)
2908     */
2909     if (rate == -1)
2910     {
2911         //Auto Rate
2912         pAd->StaCfg.DesiredTransmitSetting.field.MCS = MCS_AUTO;
2913                 pAd->StaCfg.bAutoTxRateSwitch = TRUE;
2914                 if ((pAd->CommonCfg.PhyMode <= PHY_11G) ||
2915                     (pAd->MacTab.Content[BSSID_WCID].HTPhyMode.field.MODE <= MODE_OFDM))
2916             RTMPSetDesiredRates(pAd, -1);
2917
2918 #ifdef DOT11_N_SUPPORT
2919             SetCommonHT(pAd);
2920 #endif // DOT11_N_SUPPORT //
2921     }
2922     else
2923     {
2924         if (fixed)
2925         {
2926                 pAd->StaCfg.bAutoTxRateSwitch = FALSE;
2927             if ((pAd->CommonCfg.PhyMode <= PHY_11G) ||
2928                 (pAd->MacTab.Content[BSSID_WCID].HTPhyMode.field.MODE <= MODE_OFDM))
2929                 RTMPSetDesiredRates(pAd, rate);
2930             else
2931             {
2932                 pAd->StaCfg.DesiredTransmitSetting.field.MCS = MCS_AUTO;
2933 #ifdef DOT11_N_SUPPORT
2934                 SetCommonHT(pAd);
2935 #endif // DOT11_N_SUPPORT //
2936             }
2937             DBGPRINT(RT_DEBUG_TRACE, ("rt_ioctl_siwrate::(HtMcs=%d)\n",pAd->StaCfg.DesiredTransmitSetting.field.MCS));
2938         }
2939         else
2940         {
2941             // TODO: rate = X, fixed = 0 => (rates <= X)
2942             return -EOPNOTSUPP;
2943         }
2944     }
2945
2946     return 0;
2947 }
2948
2949 int rt_ioctl_giwrate(struct net_device *dev,
2950                                struct iw_request_info *info,
2951                                union iwreq_data *wrqu, char *extra)
2952 {
2953     PRTMP_ADAPTER   pAd = RTMP_OS_NETDEV_GET_PRIV(dev);
2954     int rate_index = 0, rate_count = 0;
2955     HTTRANSMIT_SETTING ht_setting;
2956 /* Remove to global variable
2957     __s32 ralinkrate[] =
2958         {2,  4,   11,  22, // CCK
2959         12, 18,   24,  36, 48, 72, 96, 108, // OFDM
2960         13, 26,   39,  52,  78, 104, 117, 130, 26,  52,  78, 104, 156, 208, 234, 260, // 20MHz, 800ns GI, MCS: 0 ~ 15
2961         39, 78,  117, 156, 234, 312, 351, 390,                                                                            // 20MHz, 800ns GI, MCS: 16 ~ 23
2962         27, 54,   81, 108, 162, 216, 243, 270, 54, 108, 162, 216, 324, 432, 486, 540, // 40MHz, 800ns GI, MCS: 0 ~ 15
2963         81, 162, 243, 324, 486, 648, 729, 810,                                                                            // 40MHz, 800ns GI, MCS: 16 ~ 23
2964         14, 29,   43,  57,  87, 115, 130, 144, 29, 59,   87, 115, 173, 230, 260, 288, // 20MHz, 400ns GI, MCS: 0 ~ 15
2965         43, 87,  130, 173, 260, 317, 390, 433,                                                                            // 20MHz, 400ns GI, MCS: 16 ~ 23
2966         30, 60,   90, 120, 180, 240, 270, 300, 60, 120, 180, 240, 360, 480, 540, 600, // 40MHz, 400ns GI, MCS: 0 ~ 15
2967         90, 180, 270, 360, 540, 720, 810, 900};                                                                           // 40MHz, 400ns GI, MCS: 16 ~ 23
2968 */
2969
2970     rate_count = sizeof(ralinkrate)/sizeof(__s32);
2971     //check if the interface is down
2972         if(!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_INTERRUPT_IN_USE))
2973         {
2974                 DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
2975         return -ENETDOWN;
2976         }
2977
2978     if ((pAd->StaCfg.bAutoTxRateSwitch == FALSE) &&
2979         (INFRA_ON(pAd)) &&
2980         ((pAd->CommonCfg.PhyMode <= PHY_11G) || (pAd->MacTab.Content[BSSID_WCID].HTPhyMode.field.MODE <= MODE_OFDM)))
2981         ht_setting.word = pAd->StaCfg.HTPhyMode.word;
2982     else
2983         ht_setting.word = pAd->MacTab.Content[BSSID_WCID].HTPhyMode.word;
2984
2985 #ifdef DOT11_N_SUPPORT
2986     if (ht_setting.field.MODE >= MODE_HTMIX)
2987     {
2988 //      rate_index = 12 + ((UCHAR)ht_setting.field.BW *16) + ((UCHAR)ht_setting.field.ShortGI *32) + ((UCHAR)ht_setting.field.MCS);
2989         rate_index = 12 + ((UCHAR)ht_setting.field.BW *24) + ((UCHAR)ht_setting.field.ShortGI *48) + ((UCHAR)ht_setting.field.MCS);
2990     }
2991     else
2992 #endif // DOT11_N_SUPPORT //
2993     if (ht_setting.field.MODE == MODE_OFDM)
2994         rate_index = (UCHAR)(ht_setting.field.MCS) + 4;
2995     else if (ht_setting.field.MODE == MODE_CCK)
2996         rate_index = (UCHAR)(ht_setting.field.MCS);
2997
2998     if (rate_index < 0)
2999         rate_index = 0;
3000
3001     if (rate_index > rate_count)
3002         rate_index = rate_count;
3003
3004     wrqu->bitrate.value = ralinkrate[rate_index] * 500000;
3005     wrqu->bitrate.disabled = 0;
3006
3007     return 0;
3008 }
3009
3010 static const iw_handler rt_handler[] =
3011 {
3012         (iw_handler) NULL,                                  /* SIOCSIWCOMMIT */
3013         (iw_handler) rt_ioctl_giwname,                  /* SIOCGIWNAME   */
3014         (iw_handler) NULL,                                  /* SIOCSIWNWID   */
3015         (iw_handler) NULL,                                  /* SIOCGIWNWID   */
3016         (iw_handler) rt_ioctl_siwfreq,              /* SIOCSIWFREQ   */
3017         (iw_handler) rt_ioctl_giwfreq,              /* SIOCGIWFREQ   */
3018         (iw_handler) rt_ioctl_siwmode,              /* SIOCSIWMODE   */
3019         (iw_handler) rt_ioctl_giwmode,              /* SIOCGIWMODE   */
3020         (iw_handler) NULL,                              /* SIOCSIWSENS   */
3021         (iw_handler) NULL,                              /* SIOCGIWSENS   */
3022         (iw_handler) NULL /* not used */,               /* SIOCSIWRANGE  */
3023         (iw_handler) rt_ioctl_giwrange,             /* SIOCGIWRANGE  */
3024         (iw_handler) NULL /* not used */,               /* SIOCSIWPRIV   */
3025         (iw_handler) NULL /* kernel code */,    /* SIOCGIWPRIV   */
3026         (iw_handler) NULL /* not used */,               /* SIOCSIWSTATS  */
3027         (iw_handler) rt28xx_get_wireless_stats /* kernel code */,    /* SIOCGIWSTATS  */
3028         (iw_handler) NULL,                              /* SIOCSIWSPY    */
3029         (iw_handler) NULL,                              /* SIOCGIWSPY    */
3030         (iw_handler) NULL,                                      /* SIOCSIWTHRSPY */
3031         (iw_handler) NULL,                                      /* SIOCGIWTHRSPY */
3032         (iw_handler) rt_ioctl_siwap,            /* SIOCSIWAP     */
3033         (iw_handler) rt_ioctl_giwap,                /* SIOCGIWAP     */
3034 #ifdef SIOCSIWMLME
3035         (iw_handler) rt_ioctl_siwmlme,          /* SIOCSIWMLME   */
3036 #else
3037         (iw_handler) NULL,                                      /* SIOCSIWMLME */
3038 #endif // SIOCSIWMLME //
3039         (iw_handler) rt_ioctl_iwaplist,             /* SIOCGIWAPLIST */
3040 #ifdef SIOCGIWSCAN
3041         (iw_handler) rt_ioctl_siwscan,              /* SIOCSIWSCAN   */
3042         (iw_handler) rt_ioctl_giwscan,              /* SIOCGIWSCAN   */
3043 #else
3044         (iw_handler) NULL,                                      /* SIOCSIWSCAN   */
3045         (iw_handler) NULL,                                      /* SIOCGIWSCAN   */
3046 #endif /* SIOCGIWSCAN */
3047         (iw_handler) rt_ioctl_siwessid,             /* SIOCSIWESSID  */
3048         (iw_handler) rt_ioctl_giwessid,             /* SIOCGIWESSID  */
3049         (iw_handler) rt_ioctl_siwnickn,             /* SIOCSIWNICKN  */
3050         (iw_handler) rt_ioctl_giwnickn,             /* SIOCGIWNICKN  */
3051         (iw_handler) NULL,                                      /* -- hole --    */
3052         (iw_handler) NULL,                                      /* -- hole --    */
3053         (iw_handler) rt_ioctl_siwrate,          /* SIOCSIWRATE   */
3054         (iw_handler) rt_ioctl_giwrate,          /* SIOCGIWRATE   */
3055         (iw_handler) rt_ioctl_siwrts,               /* SIOCSIWRTS    */
3056         (iw_handler) rt_ioctl_giwrts,               /* SIOCGIWRTS    */
3057         (iw_handler) rt_ioctl_siwfrag,              /* SIOCSIWFRAG   */
3058         (iw_handler) rt_ioctl_giwfrag,              /* SIOCGIWFRAG   */
3059         (iw_handler) NULL,                              /* SIOCSIWTXPOW  */
3060         (iw_handler) NULL,                              /* SIOCGIWTXPOW  */
3061         (iw_handler) NULL,                              /* SIOCSIWRETRY  */
3062         (iw_handler) NULL,                              /* SIOCGIWRETRY  */
3063         (iw_handler) rt_ioctl_siwencode,                /* SIOCSIWENCODE */
3064         (iw_handler) rt_ioctl_giwencode,                /* SIOCGIWENCODE */
3065         (iw_handler) NULL,                              /* SIOCSIWPOWER  */
3066         (iw_handler) NULL,                              /* SIOCGIWPOWER  */
3067         (iw_handler) NULL,                                              /* -- hole -- */
3068         (iw_handler) NULL,                                              /* -- hole -- */
3069 #if WIRELESS_EXT > 17
3070     (iw_handler) rt_ioctl_siwgenie,         /* SIOCSIWGENIE  */
3071         (iw_handler) rt_ioctl_giwgenie,         /* SIOCGIWGENIE  */
3072         (iw_handler) rt_ioctl_siwauth,              /* SIOCSIWAUTH   */
3073         (iw_handler) rt_ioctl_giwauth,              /* SIOCGIWAUTH   */
3074         (iw_handler) rt_ioctl_siwencodeext,         /* SIOCSIWENCODEEXT */
3075         (iw_handler) rt_ioctl_giwencodeext,             /* SIOCGIWENCODEEXT */
3076         (iw_handler) rt_ioctl_siwpmksa,         /* SIOCSIWPMKSA  */
3077 #endif
3078 };
3079
3080 static const iw_handler rt_priv_handlers[] = {
3081         (iw_handler) NULL, /* + 0x00 */
3082         (iw_handler) NULL, /* + 0x01 */
3083         (iw_handler) rt_ioctl_setparam, /* + 0x02 */
3084         (iw_handler) NULL, /* + 0x03 */
3085         (iw_handler) NULL, /* + 0x04 */
3086         (iw_handler) NULL, /* + 0x05 */
3087         (iw_handler) NULL, /* + 0x06 */
3088         (iw_handler) NULL, /* + 0x07 */
3089         (iw_handler) NULL, /* + 0x08 */
3090         (iw_handler) rt_private_get_statistics, /* + 0x09 */
3091         (iw_handler) NULL, /* + 0x0A */
3092         (iw_handler) NULL, /* + 0x0B */
3093         (iw_handler) NULL, /* + 0x0C */
3094         (iw_handler) NULL, /* + 0x0D */
3095         (iw_handler) NULL, /* + 0x0E */
3096         (iw_handler) NULL, /* + 0x0F */
3097         (iw_handler) NULL, /* + 0x10 */
3098         (iw_handler) rt_private_show, /* + 0x11 */
3099     (iw_handler) NULL, /* + 0x12 */
3100         (iw_handler) NULL, /* + 0x13 */
3101     (iw_handler) NULL, /* + 0x14 */
3102         (iw_handler) NULL, /* + 0x15 */
3103     (iw_handler) NULL, /* + 0x16 */
3104         (iw_handler) NULL, /* + 0x17 */
3105         (iw_handler) NULL, /* + 0x18 */
3106 };
3107
3108 const struct iw_handler_def rt28xx_iw_handler_def =
3109 {
3110 #define N(a)    (sizeof (a) / sizeof (a[0]))
3111         .standard       = (iw_handler *) rt_handler,
3112         .num_standard   = sizeof(rt_handler) / sizeof(iw_handler),
3113         .private        = (iw_handler *) rt_priv_handlers,
3114         .num_private            = N(rt_priv_handlers),
3115         .private_args   = (struct iw_priv_args *) privtab,
3116         .num_private_args       = N(privtab),
3117 #if IW_HANDLER_VERSION >= 7
3118     .get_wireless_stats = rt28xx_get_wireless_stats,
3119 #endif
3120 };
3121
3122 INT rt28xx_sta_ioctl(
3123         IN      struct net_device       *net_dev,
3124         IN      OUT     struct ifreq    *rq,
3125         IN      INT                                     cmd)
3126 {
3127         POS_COOKIE                      pObj;
3128         RTMP_ADAPTER        *pAd = NULL;
3129         struct iwreq        *wrq = (struct iwreq *) rq;
3130         BOOLEAN                         StateMachineTouched = FALSE;
3131         INT                                     Status = NDIS_STATUS_SUCCESS;
3132
3133         pAd = RTMP_OS_NETDEV_GET_PRIV(net_dev);
3134         if (pAd == NULL)
3135         {
3136                 /* if 1st open fail, pAd will be free;
3137                    So the net_dev->priv will be NULL in 2rd open */
3138                 return -ENETDOWN;
3139         }
3140         pObj = (POS_COOKIE) pAd->OS_Cookie;
3141
3142     //check if the interface is down
3143     if(!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_INTERRUPT_IN_USE))
3144     {
3145 #ifdef CONFIG_APSTA_MIXED_SUPPORT
3146             if (wrq->u.data.pointer == NULL)
3147             {
3148                     return Status;
3149             }
3150
3151             if (strstr(wrq->u.data.pointer, "OpMode") == NULL)
3152 #endif // CONFIG_APSTA_MIXED_SUPPORT //
3153                 {
3154             DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
3155                     return -ENETDOWN;
3156         }
3157     }
3158
3159         {       // determine this ioctl command is comming from which interface.
3160                 pObj->ioctl_if_type = INT_MAIN;
3161                 pObj->ioctl_if = MAIN_MBSSID;
3162         }
3163
3164         switch(cmd)
3165         {
3166 #ifdef RALINK_ATE
3167 #ifdef RALINK_28xx_QA
3168                 case RTPRIV_IOCTL_ATE:
3169                         {
3170                                 RtmpDoAte(pAd, wrq);
3171                         }
3172                         break;
3173 #endif // RALINK_28xx_QA //
3174 #endif // RALINK_ATE //
3175         case SIOCGIFHWADDR:
3176                         DBGPRINT(RT_DEBUG_TRACE, ("IOCTL::SIOCGIFHWADDR\n"));
3177                         memcpy(wrq->u.name, pAd->CurrentAddress, ETH_ALEN);
3178                         break;
3179                 case SIOCGIWNAME:
3180         {
3181                 char *name=&wrq->u.name[0];
3182                 rt_ioctl_giwname(net_dev, NULL, name, NULL);
3183             break;
3184                 }
3185                 case SIOCGIWESSID:  //Get ESSID
3186         {
3187                 struct iw_point *essid=&wrq->u.essid;
3188                 rt_ioctl_giwessid(net_dev, NULL, essid, essid->pointer);
3189             break;
3190                 }
3191                 case SIOCSIWESSID:  //Set ESSID
3192                 {
3193                 struct iw_point *essid=&wrq->u.essid;
3194                 rt_ioctl_siwessid(net_dev, NULL, essid, essid->pointer);
3195             break;
3196                 }
3197                 case SIOCSIWNWID:   // set network id (the cell)
3198                 case SIOCGIWNWID:   // get network id
3199                         Status = -EOPNOTSUPP;
3200                         break;
3201                 case SIOCSIWFREQ:   //set channel/frequency (Hz)
3202                 {
3203                 struct iw_freq *freq=&wrq->u.freq;
3204                 rt_ioctl_siwfreq(net_dev, NULL, freq, NULL);
3205                         break;
3206                 }
3207                 case SIOCGIWFREQ:   // get channel/frequency (Hz)
3208                 {
3209                 struct iw_freq *freq=&wrq->u.freq;
3210                 rt_ioctl_giwfreq(net_dev, NULL, freq, NULL);
3211                         break;
3212                 }
3213                 case SIOCSIWNICKN: //set node name/nickname
3214                 {
3215                 //struct iw_point *data=&wrq->u.data;
3216                 //rt_ioctl_siwnickn(net_dev, NULL, data, NULL);
3217                         break;
3218                         }
3219                 case SIOCGIWNICKN: //get node name/nickname
3220         {
3221                         struct iw_point *erq = NULL;
3222                 erq = &wrq->u.data;
3223             erq->length = strlen((PSTRING) pAd->nickname);
3224             Status = copy_to_user(erq->pointer, pAd->nickname, erq->length);
3225                         break;
3226                 }
3227                 case SIOCGIWRATE:   //get default bit rate (bps)
3228                     rt_ioctl_giwrate(net_dev, NULL, &wrq->u, NULL);
3229             break;
3230             case SIOCSIWRATE:  //set default bit rate (bps)
3231                 rt_ioctl_siwrate(net_dev, NULL, &wrq->u, NULL);
3232             break;
3233         case SIOCGIWRTS:  // get RTS/CTS threshold (bytes)
3234                 {
3235                 struct iw_param *rts=&wrq->u.rts;
3236                 rt_ioctl_giwrts(net_dev, NULL, rts, NULL);
3237             break;
3238                 }
3239         case SIOCSIWRTS:  //set RTS/CTS threshold (bytes)
3240                 {
3241                 struct iw_param *rts=&wrq->u.rts;
3242                 rt_ioctl_siwrts(net_dev, NULL, rts, NULL);
3243             break;
3244                 }
3245         case SIOCGIWFRAG:  //get fragmentation thr (bytes)
3246                 {
3247                 struct iw_param *frag=&wrq->u.frag;
3248                 rt_ioctl_giwfrag(net_dev, NULL, frag, NULL);
3249             break;
3250                 }
3251         case SIOCSIWFRAG:  //set fragmentation thr (bytes)
3252                 {
3253                 struct iw_param *frag=&wrq->u.frag;
3254                 rt_ioctl_siwfrag(net_dev, NULL, frag, NULL);
3255             break;
3256                 }
3257         case SIOCGIWENCODE:  //get encoding token & mode
3258                 {
3259                 struct iw_point *erq=&wrq->u.encoding;
3260                 if(erq)
3261                         rt_ioctl_giwencode(net_dev, NULL, erq, erq->pointer);
3262             break;
3263                 }
3264         case SIOCSIWENCODE:  //set encoding token & mode
3265                 {
3266                 struct iw_point *erq=&wrq->u.encoding;
3267                 if(erq)
3268                         rt_ioctl_siwencode(net_dev, NULL, erq, erq->pointer);
3269             break;
3270                 }
3271                 case SIOCGIWAP:     //get access point MAC addresses
3272                 {
3273                 struct sockaddr *ap_addr=&wrq->u.ap_addr;
3274                 rt_ioctl_giwap(net_dev, NULL, ap_addr, ap_addr->sa_data);
3275                         break;
3276                 }
3277             case SIOCSIWAP:  //set access point MAC addresses
3278                 {
3279                 struct sockaddr *ap_addr=&wrq->u.ap_addr;
3280                 rt_ioctl_siwap(net_dev, NULL, ap_addr, ap_addr->sa_data);
3281             break;
3282                 }
3283                 case SIOCGIWMODE:   //get operation mode
3284                 {
3285                 __u32 *mode=&wrq->u.mode;
3286                 rt_ioctl_giwmode(net_dev, NULL, mode, NULL);
3287             break;
3288                 }
3289                 case SIOCSIWMODE:   //set operation mode
3290                 {
3291                 __u32 *mode=&wrq->u.mode;
3292                 rt_ioctl_siwmode(net_dev, NULL, mode, NULL);
3293             break;
3294                 }
3295                 case SIOCGIWSENS:   //get sensitivity (dBm)
3296                 case SIOCSIWSENS:       //set sensitivity (dBm)
3297                 case SIOCGIWPOWER:  //get Power Management settings
3298                 case SIOCSIWPOWER:  //set Power Management settings
3299                 case SIOCGIWTXPOW:  //get transmit power (dBm)
3300                 case SIOCSIWTXPOW:  //set transmit power (dBm)
3301                 case SIOCGIWRANGE:      //Get range of parameters
3302                 case SIOCGIWRETRY:      //get retry limits and lifetime
3303                 case SIOCSIWRETRY:      //set retry limits and lifetime
3304                 case RT_PRIV_IOCTL:
3305                 case RT_PRIV_IOCTL_EXT:
3306                         Status = -EOPNOTSUPP;
3307                         break;
3308                 case SIOCGIWPRIV:
3309                         if (wrq->u.data.pointer)
3310                         {
3311                                 if ( access_ok(VERIFY_WRITE, wrq->u.data.pointer, sizeof(privtab)) != TRUE)
3312                                         break;
3313                                 wrq->u.data.length = sizeof(privtab) / sizeof(privtab[0]);
3314                                 if (copy_to_user(wrq->u.data.pointer, privtab, sizeof(privtab)))
3315                                         Status = -EFAULT;
3316                         }
3317                         break;
3318                 case RTPRIV_IOCTL_SET:
3319                         if(access_ok(VERIFY_READ, wrq->u.data.pointer, wrq->u.data.length) != TRUE)
3320                                         break;
3321                         rt_ioctl_setparam(net_dev, NULL, NULL, wrq->u.data.pointer);
3322                         break;
3323                 case RTPRIV_IOCTL_GSITESURVEY:
3324                         RTMPIoctlGetSiteSurvey(pAd, wrq);
3325                     break;
3326         case SIOCETHTOOL:
3327                 break;
3328                 default:
3329                         DBGPRINT(RT_DEBUG_ERROR, ("IOCTL::unknown IOCTL's cmd = 0x%08x\n", cmd));
3330                         Status = -EOPNOTSUPP;
3331                         break;
3332         }
3333
3334     if(StateMachineTouched) // Upper layer sent a MLME-related operations
3335         RTMP_MLME_HANDLER(pAd);
3336
3337         return Status;
3338 }
3339
3340 /*
3341     ==========================================================================
3342     Description:
3343         Set SSID
3344     Return:
3345         TRUE if all parameters are OK, FALSE otherwise
3346     ==========================================================================
3347 */
3348 INT Set_SSID_Proc(
3349     IN  PRTMP_ADAPTER   pAdapter,
3350     IN  PSTRING          arg)
3351 {
3352     NDIS_802_11_SSID                    Ssid, *pSsid=NULL;
3353     BOOLEAN                             StateMachineTouched = FALSE;
3354     int                                 success = TRUE;
3355
3356     if( strlen(arg) <= MAX_LEN_OF_SSID)
3357     {
3358         NdisZeroMemory(&Ssid, sizeof(NDIS_802_11_SSID));
3359         if (strlen(arg) != 0)
3360         {
3361             NdisMoveMemory(Ssid.Ssid, arg, strlen(arg));
3362             Ssid.SsidLength = strlen(arg);
3363         }
3364         else   //ANY ssid
3365         {
3366             Ssid.SsidLength = 0;
3367             memcpy(Ssid.Ssid, "", 0);
3368                 pAdapter->StaCfg.BssType = BSS_INFRA;
3369                 pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeOpen;
3370         pAdapter->StaCfg.WepStatus  = Ndis802_11EncryptionDisabled;
3371         }
3372         pSsid = &Ssid;
3373
3374         if (pAdapter->Mlme.CntlMachine.CurrState != CNTL_IDLE)
3375         {
3376             RTMP_MLME_RESET_STATE_MACHINE(pAdapter);
3377             DBGPRINT(RT_DEBUG_TRACE, ("!!! MLME busy, reset MLME state machine !!!\n"));
3378         }
3379
3380                 if ((pAdapter->StaCfg.WpaPassPhraseLen >= 8) &&
3381                         (pAdapter->StaCfg.WpaPassPhraseLen <= 64))
3382                 {
3383                         STRING passphrase_str[65] = {0};
3384                         UCHAR keyMaterial[40];
3385
3386                         RTMPMoveMemory(passphrase_str, pAdapter->StaCfg.WpaPassPhrase, pAdapter->StaCfg.WpaPassPhraseLen);
3387                         RTMPZeroMemory(pAdapter->StaCfg.PMK, 32);
3388                         if (pAdapter->StaCfg.WpaPassPhraseLen == 64)
3389                         {
3390                             AtoH((PSTRING) pAdapter->StaCfg.WpaPassPhrase, pAdapter->StaCfg.PMK, 32);
3391                         }
3392                         else
3393                         {
3394                             PasswordHash((PSTRING) pAdapter->StaCfg.WpaPassPhrase, Ssid.Ssid, Ssid.SsidLength, keyMaterial);
3395                             NdisMoveMemory(pAdapter->StaCfg.PMK, keyMaterial, 32);
3396                         }
3397                 }
3398
3399         pAdapter->MlmeAux.CurrReqIsFromNdis = TRUE;
3400         pAdapter->StaCfg.bScanReqIsFromWebUI = FALSE;
3401                 pAdapter->bConfigChanged = TRUE;
3402
3403         MlmeEnqueue(pAdapter,
3404                     MLME_CNTL_STATE_MACHINE,
3405                     OID_802_11_SSID,
3406                     sizeof(NDIS_802_11_SSID),
3407                     (VOID *)pSsid);
3408
3409         StateMachineTouched = TRUE;
3410         DBGPRINT(RT_DEBUG_TRACE, ("Set_SSID_Proc::(Len=%d,Ssid=%s)\n", Ssid.SsidLength, Ssid.Ssid));
3411     }
3412     else
3413         success = FALSE;
3414
3415     if (StateMachineTouched) // Upper layer sent a MLME-related operations
3416         RTMP_MLME_HANDLER(pAdapter);
3417
3418     return success;
3419 }
3420
3421 #ifdef WMM_SUPPORT
3422 /*
3423     ==========================================================================
3424     Description:
3425         Set WmmCapable Enable or Disable
3426     Return:
3427         TRUE if all parameters are OK, FALSE otherwise
3428     ==========================================================================
3429 */
3430 INT     Set_WmmCapable_Proc(
3431         IN      PRTMP_ADAPTER   pAd,
3432         IN      PSTRING                 arg)
3433 {
3434         BOOLEAN bWmmCapable;
3435
3436         bWmmCapable = simple_strtol(arg, 0, 10);
3437
3438         if ((bWmmCapable == 1)
3439                 )
3440                 pAd->CommonCfg.bWmmCapable = TRUE;
3441         else if (bWmmCapable == 0)
3442                 pAd->CommonCfg.bWmmCapable = FALSE;
3443         else
3444                 return FALSE;  //Invalid argument
3445
3446         DBGPRINT(RT_DEBUG_TRACE, ("Set_WmmCapable_Proc::(bWmmCapable=%d)\n",
3447                 pAd->CommonCfg.bWmmCapable));
3448
3449         return TRUE;
3450 }
3451 #endif // WMM_SUPPORT //
3452
3453 /*
3454     ==========================================================================
3455     Description:
3456         Set Network Type(Infrastructure/Adhoc mode)
3457     Return:
3458         TRUE if all parameters are OK, FALSE otherwise
3459     ==========================================================================
3460 */
3461 INT Set_NetworkType_Proc(
3462     IN  PRTMP_ADAPTER   pAdapter,
3463     IN  PSTRING          arg)
3464 {
3465     UINT32      Value = 0;
3466
3467     if (strcmp(arg, "Adhoc") == 0)
3468         {
3469                 if (pAdapter->StaCfg.BssType != BSS_ADHOC)
3470                 {
3471                         // Config has changed
3472                         pAdapter->bConfigChanged = TRUE;
3473             if (MONITOR_ON(pAdapter))
3474             {
3475                 RTMP_IO_WRITE32(pAdapter, RX_FILTR_CFG, STANORMAL);
3476                 RTMP_IO_READ32(pAdapter, MAC_SYS_CTRL, &Value);
3477                                 Value &= (~0x80);
3478                                 RTMP_IO_WRITE32(pAdapter, MAC_SYS_CTRL, Value);
3479                 OPSTATUS_CLEAR_FLAG(pAdapter, fOP_STATUS_MEDIA_STATE_CONNECTED);
3480                 pAdapter->StaCfg.bAutoReconnect = TRUE;
3481                 LinkDown(pAdapter, FALSE);
3482             }
3483                         if (INFRA_ON(pAdapter))
3484                         {
3485                                 //BOOLEAN Cancelled;
3486                                 // Set the AutoReconnectSsid to prevent it reconnect to old SSID
3487                                 // Since calling this indicate user don't want to connect to that SSID anymore.
3488                                 pAdapter->MlmeAux.AutoReconnectSsidLen= 32;
3489                                 NdisZeroMemory(pAdapter->MlmeAux.AutoReconnectSsid, pAdapter->MlmeAux.AutoReconnectSsidLen);
3490
3491                                 LinkDown(pAdapter, FALSE);
3492
3493                                 DBGPRINT(RT_DEBUG_TRACE, ("NDIS_STATUS_MEDIA_DISCONNECT Event BB!\n"));
3494                         }
3495                 }
3496                 pAdapter->StaCfg.BssType = BSS_ADHOC;
3497         pAdapter->net_dev->type = pAdapter->StaCfg.OriDevType;
3498                 DBGPRINT(RT_DEBUG_TRACE, ("===>Set_NetworkType_Proc::(AD-HOC)\n"));
3499         }
3500     else if (strcmp(arg, "Infra") == 0)
3501         {
3502                 if (pAdapter->StaCfg.BssType != BSS_INFRA)
3503                 {
3504                         // Config has changed
3505                         pAdapter->bConfigChanged = TRUE;
3506             if (MONITOR_ON(pAdapter))
3507             {
3508                 RTMP_IO_WRITE32(pAdapter, RX_FILTR_CFG, STANORMAL);
3509                 RTMP_IO_READ32(pAdapter, MAC_SYS_CTRL, &Value);
3510                                 Value &= (~0x80);
3511                                 RTMP_IO_WRITE32(pAdapter, MAC_SYS_CTRL, Value);
3512                 OPSTATUS_CLEAR_FLAG(pAdapter, fOP_STATUS_MEDIA_STATE_CONNECTED);
3513                 pAdapter->StaCfg.bAutoReconnect = TRUE;
3514                 LinkDown(pAdapter, FALSE);
3515             }
3516                         if (ADHOC_ON(pAdapter))
3517                         {
3518                                 // Set the AutoReconnectSsid to prevent it reconnect to old SSID
3519                                 // Since calling this indicate user don't want to connect to that SSID anymore.
3520                                 pAdapter->MlmeAux.AutoReconnectSsidLen= 32;
3521                                 NdisZeroMemory(pAdapter->MlmeAux.AutoReconnectSsid, pAdapter->MlmeAux.AutoReconnectSsidLen);
3522
3523                                 LinkDown(pAdapter, FALSE);
3524                         }
3525                 }
3526                 pAdapter->StaCfg.BssType = BSS_INFRA;
3527         pAdapter->net_dev->type = pAdapter->StaCfg.OriDevType;
3528                 DBGPRINT(RT_DEBUG_TRACE, ("===>Set_NetworkType_Proc::(INFRA)\n"));
3529         }
3530     else if (strcmp(arg, "Monitor") == 0)
3531     {
3532                         UCHAR   bbpValue = 0;
3533                         BCN_TIME_CFG_STRUC csr;
3534                         OPSTATUS_CLEAR_FLAG(pAdapter, fOP_STATUS_INFRA_ON);
3535             OPSTATUS_CLEAR_FLAG(pAdapter, fOP_STATUS_ADHOC_ON);
3536                         OPSTATUS_SET_FLAG(pAdapter, fOP_STATUS_MEDIA_STATE_CONNECTED);
3537                         // disable all periodic state machine
3538                         pAdapter->StaCfg.bAutoReconnect = FALSE;
3539                         // reset all mlme state machine
3540                         RTMP_MLME_RESET_STATE_MACHINE(pAdapter);
3541                         DBGPRINT(RT_DEBUG_TRACE, ("fOP_STATUS_MEDIA_STATE_CONNECTED \n"));
3542             if (pAdapter->CommonCfg.CentralChannel == 0)
3543             {
3544 #ifdef DOT11_N_SUPPORT
3545                 if (pAdapter->CommonCfg.PhyMode == PHY_11AN_MIXED)
3546                     pAdapter->CommonCfg.CentralChannel = 36;
3547                 else
3548 #endif // DOT11_N_SUPPORT //
3549                     pAdapter->CommonCfg.CentralChannel = 6;
3550             }
3551 #ifdef DOT11_N_SUPPORT
3552             else
3553                 N_ChannelCheck(pAdapter);
3554 #endif // DOT11_N_SUPPORT //
3555
3556 #ifdef DOT11_N_SUPPORT
3557                         if (pAdapter->CommonCfg.PhyMode >= PHY_11ABGN_MIXED &&
3558                 pAdapter->CommonCfg.RegTransmitSetting.field.BW == BW_40 &&
3559                 pAdapter->CommonCfg.RegTransmitSetting.field.EXTCHA == EXTCHA_ABOVE)
3560                         {
3561                                 // 40MHz ,control channel at lower
3562                                 RTMP_BBP_IO_READ8_BY_REG_ID(pAdapter, BBP_R4, &bbpValue);
3563                                 bbpValue &= (~0x18);
3564                                 bbpValue |= 0x10;
3565                                 RTMP_BBP_IO_WRITE8_BY_REG_ID(pAdapter, BBP_R4, bbpValue);
3566                                 pAdapter->CommonCfg.BBPCurrentBW = BW_40;
3567                                 //  RX : control channel at lower
3568                                 RTMP_BBP_IO_READ8_BY_REG_ID(pAdapter, BBP_R3, &bbpValue);
3569                                 bbpValue &= (~0x20);
3570                                 RTMP_BBP_IO_WRITE8_BY_REG_ID(pAdapter, BBP_R3, bbpValue);
3571
3572                                 RTMP_IO_READ32(pAdapter, TX_BAND_CFG, &Value);
3573                                 Value &= 0xfffffffe;
3574                                 RTMP_IO_WRITE32(pAdapter, TX_BAND_CFG, Value);
3575                                 pAdapter->CommonCfg.CentralChannel = pAdapter->CommonCfg.Channel + 2;
3576                 AsicSwitchChannel(pAdapter, pAdapter->CommonCfg.CentralChannel, FALSE);
3577                             AsicLockChannel(pAdapter, pAdapter->CommonCfg.CentralChannel);
3578                 DBGPRINT(RT_DEBUG_TRACE, ("BW_40 ,control_channel(%d), CentralChannel(%d) \n",
3579                                            pAdapter->CommonCfg.Channel,
3580                                            pAdapter->CommonCfg.CentralChannel));
3581                         }
3582                         else if (pAdapter->CommonCfg.PhyMode >= PHY_11ABGN_MIXED &&
3583                      pAdapter->CommonCfg.RegTransmitSetting.field.BW == BW_40 &&
3584                      pAdapter->CommonCfg.RegTransmitSetting.field.EXTCHA == EXTCHA_BELOW)
3585                         {
3586                                 // 40MHz ,control channel at upper
3587                                 RTMP_BBP_IO_READ8_BY_REG_ID(pAdapter, BBP_R4, &bbpValue);
3588                                 bbpValue &= (~0x18);
3589                                 bbpValue |= 0x10;
3590                                 RTMP_BBP_IO_WRITE8_BY_REG_ID(pAdapter, BBP_R4, bbpValue);
3591                                 pAdapter->CommonCfg.BBPCurrentBW = BW_40;
3592                                 RTMP_IO_READ32(pAdapter, TX_BAND_CFG, &Value);
3593                                 Value |= 0x1;
3594                                 RTMP_IO_WRITE32(pAdapter, TX_BAND_CFG, Value);
3595
3596                                 RTMP_BBP_IO_READ8_BY_REG_ID(pAdapter, BBP_R3, &bbpValue);
3597                                 bbpValue |= (0x20);
3598                                 RTMP_BBP_IO_WRITE8_BY_REG_ID(pAdapter, BBP_R3, bbpValue);
3599                                 pAdapter->CommonCfg.CentralChannel = pAdapter->CommonCfg.Channel - 2;
3600                 AsicSwitchChannel(pAdapter, pAdapter->CommonCfg.CentralChannel, FALSE);
3601                             AsicLockChannel(pAdapter, pAdapter->CommonCfg.CentralChannel);
3602                 DBGPRINT(RT_DEBUG_TRACE, ("BW_40 ,control_channel(%d), CentralChannel(%d) \n",
3603                                            pAdapter->CommonCfg.Channel,
3604                                            pAdapter->CommonCfg.CentralChannel));
3605                         }
3606                         else
3607 #endif // DOT11_N_SUPPORT //
3608                         {
3609                                 // 20MHz
3610                                 RTMP_BBP_IO_READ8_BY_REG_ID(pAdapter, BBP_R4, &bbpValue);
3611                                 bbpValue &= (~0x18);
3612                                 RTMP_BBP_IO_WRITE8_BY_REG_ID(pAdapter, BBP_R4, bbpValue);
3613                                 pAdapter->CommonCfg.BBPCurrentBW = BW_20;
3614                 AsicSwitchChannel(pAdapter, pAdapter->CommonCfg.Channel, FALSE);
3615                             AsicLockChannel(pAdapter, pAdapter->CommonCfg.Channel);
3616                                 DBGPRINT(RT_DEBUG_TRACE, ("BW_20, Channel(%d)\n", pAdapter->CommonCfg.Channel));
3617                         }
3618                         // Enable Rx with promiscuous reception
3619                         RTMP_IO_WRITE32(pAdapter, RX_FILTR_CFG, 0x3);
3620                         // ASIC supporsts sniffer function with replacing RSSI with timestamp.
3621                         //RTMP_IO_READ32(pAdapter, MAC_SYS_CTRL, &Value);
3622                         //Value |= (0x80);
3623                         //RTMP_IO_WRITE32(pAdapter, MAC_SYS_CTRL, Value);
3624                         // disable sync
3625                         RTMP_IO_READ32(pAdapter, BCN_TIME_CFG, &csr.word);
3626                         csr.field.bBeaconGen = 0;
3627                         csr.field.bTBTTEnable = 0;
3628                         csr.field.TsfSyncMode = 0;
3629                         RTMP_IO_WRITE32(pAdapter, BCN_TIME_CFG, csr.word);
3630
3631                         pAdapter->StaCfg.BssType = BSS_MONITOR;
3632             pAdapter->net_dev->type = ARPHRD_IEEE80211_PRISM; //ARPHRD_IEEE80211; // IEEE80211
3633                         DBGPRINT(RT_DEBUG_TRACE, ("===>Set_NetworkType_Proc::(MONITOR)\n"));
3634     }
3635
3636     // Reset Ralink supplicant to not use, it will be set to start when UI set PMK key
3637     pAdapter->StaCfg.WpaState = SS_NOTUSE;
3638
3639     DBGPRINT(RT_DEBUG_TRACE, ("Set_NetworkType_Proc::(NetworkType=%d)\n", pAdapter->StaCfg.BssType));
3640
3641     return TRUE;
3642 }
3643
3644 /*
3645     ==========================================================================
3646     Description:
3647         Set Authentication mode
3648     Return:
3649         TRUE if all parameters are OK, FALSE otherwise
3650     ==========================================================================
3651 */
3652 INT Set_AuthMode_Proc(
3653     IN  PRTMP_ADAPTER   pAdapter,
3654     IN  PSTRING          arg)
3655 {
3656     if ((strcmp(arg, "WEPAUTO") == 0) || (strcmp(arg, "wepauto") == 0))
3657         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeAutoSwitch;
3658     else if ((strcmp(arg, "OPEN") == 0) || (strcmp(arg, "open") == 0))
3659         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeOpen;
3660     else if ((strcmp(arg, "SHARED") == 0) || (strcmp(arg, "shared") == 0))
3661         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeShared;
3662     else if ((strcmp(arg, "WPAPSK") == 0) || (strcmp(arg, "wpapsk") == 0))
3663         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeWPAPSK;
3664     else if ((strcmp(arg, "WPANONE") == 0) || (strcmp(arg, "wpanone") == 0))
3665         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeWPANone;
3666     else if ((strcmp(arg, "WPA2PSK") == 0) || (strcmp(arg, "wpa2psk") == 0))
3667         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeWPA2PSK;
3668 #ifdef WPA_SUPPLICANT_SUPPORT
3669     else if ((strcmp(arg, "WPA") == 0) || (strcmp(arg, "wpa") == 0))
3670         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeWPA;
3671     else if ((strcmp(arg, "WPA2") == 0) || (strcmp(arg, "wpa2") == 0))
3672         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeWPA2;
3673 #endif // WPA_SUPPLICANT_SUPPORT //
3674     else
3675         return FALSE;
3676
3677     pAdapter->StaCfg.PortSecured = WPA_802_1X_PORT_NOT_SECURED;
3678
3679     DBGPRINT(RT_DEBUG_TRACE, ("Set_AuthMode_Proc::(AuthMode=%d)\n", pAdapter->StaCfg.AuthMode));
3680
3681     return TRUE;
3682 }
3683
3684 /*
3685     ==========================================================================
3686     Description:
3687         Set Encryption Type
3688     Return:
3689         TRUE if all parameters are OK, FALSE otherwise
3690     ==========================================================================
3691 */
3692 INT Set_EncrypType_Proc(
3693     IN  PRTMP_ADAPTER   pAdapter,
3694     IN  PSTRING          arg)
3695 {
3696     if ((strcmp(arg, "NONE") == 0) || (strcmp(arg, "none") == 0))
3697     {
3698         if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
3699             return TRUE;    // do nothing
3700
3701         pAdapter->StaCfg.WepStatus     = Ndis802_11WEPDisabled;
3702         pAdapter->StaCfg.PairCipher    = Ndis802_11WEPDisabled;
3703             pAdapter->StaCfg.GroupCipher   = Ndis802_11WEPDisabled;
3704     }
3705     else if ((strcmp(arg, "WEP") == 0) || (strcmp(arg, "wep") == 0))
3706     {
3707         if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
3708             return TRUE;    // do nothing
3709
3710         pAdapter->StaCfg.WepStatus     = Ndis802_11WEPEnabled;
3711         pAdapter->StaCfg.PairCipher    = Ndis802_11WEPEnabled;
3712             pAdapter->StaCfg.GroupCipher   = Ndis802_11WEPEnabled;
3713     }
3714     else if ((strcmp(arg, "TKIP") == 0) || (strcmp(arg, "tkip") == 0))
3715     {
3716         if (pAdapter->StaCfg.AuthMode < Ndis802_11AuthModeWPA)
3717             return TRUE;    // do nothing
3718
3719         pAdapter->StaCfg.WepStatus     = Ndis802_11Encryption2Enabled;
3720         pAdapter->StaCfg.PairCipher    = Ndis802_11Encryption2Enabled;
3721             pAdapter->StaCfg.GroupCipher   = Ndis802_11Encryption2Enabled;
3722     }
3723     else if ((strcmp(arg, "AES") == 0) || (strcmp(arg, "aes") == 0))
3724     {
3725         if (pAdapter->StaCfg.AuthMode < Ndis802_11AuthModeWPA)
3726             return TRUE;    // do nothing
3727
3728         pAdapter->StaCfg.WepStatus     = Ndis802_11Encryption3Enabled;
3729         pAdapter->StaCfg.PairCipher    = Ndis802_11Encryption3Enabled;
3730             pAdapter->StaCfg.GroupCipher   = Ndis802_11Encryption3Enabled;
3731     }
3732     else
3733         return FALSE;
3734
3735     pAdapter->StaCfg.OrigWepStatus = pAdapter->StaCfg.WepStatus;
3736
3737     DBGPRINT(RT_DEBUG_TRACE, ("Set_EncrypType_Proc::(EncrypType=%d)\n", pAdapter->StaCfg.WepStatus));
3738
3739     return TRUE;
3740 }
3741
3742 /*
3743     ==========================================================================
3744     Description:
3745         Set Default Key ID
3746     Return:
3747         TRUE if all parameters are OK, FALSE otherwise
3748     ==========================================================================
3749 */
3750 INT Set_DefaultKeyID_Proc(
3751     IN  PRTMP_ADAPTER   pAdapter,
3752     IN  PSTRING          arg)
3753 {
3754     ULONG                               KeyIdx;
3755
3756     KeyIdx = simple_strtol(arg, 0, 10);
3757     if((KeyIdx >= 1 ) && (KeyIdx <= 4))
3758         pAdapter->StaCfg.DefaultKeyId = (UCHAR) (KeyIdx - 1 );
3759     else
3760         return FALSE;  //Invalid argument
3761
3762     DBGPRINT(RT_DEBUG_TRACE, ("Set_DefaultKeyID_Proc::(DefaultKeyID=%d)\n", pAdapter->StaCfg.DefaultKeyId));
3763
3764     return TRUE;
3765 }
3766
3767 /*
3768     ==========================================================================
3769     Description:
3770         Set WEP KEY1
3771     Return:
3772         TRUE if all parameters are OK, FALSE otherwise
3773     ==========================================================================
3774 */
3775 INT Set_Key1_Proc(
3776     IN  PRTMP_ADAPTER   pAdapter,
3777     IN  PSTRING          arg)
3778 {
3779     int                                 KeyLen;
3780     int                                 i;
3781     UCHAR                               CipherAlg=CIPHER_WEP64;
3782
3783     if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
3784         return TRUE;    // do nothing
3785
3786     KeyLen = strlen(arg);
3787
3788     switch (KeyLen)
3789     {
3790         case 5: //wep 40 Ascii type
3791             pAdapter->SharedKey[BSS0][0].KeyLen = KeyLen;
3792             memcpy(pAdapter->SharedKey[BSS0][0].Key, arg, KeyLen);
3793             CipherAlg = CIPHER_WEP64;
3794             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key1_Proc::(Key1=%s and type=%s)\n", arg, "Ascii"));
3795             break;
3796         case 10: //wep 40 Hex type
3797             for(i=0; i < KeyLen; i++)
3798             {
3799                 if( !isxdigit(*(arg+i)) )
3800                     return FALSE;  //Not Hex value;
3801             }
3802             pAdapter->SharedKey[BSS0][0].KeyLen = KeyLen / 2 ;
3803             AtoH(arg, pAdapter->SharedKey[BSS0][0].Key, KeyLen / 2);
3804             CipherAlg = CIPHER_WEP64;
3805             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key1_Proc::(Key1=%s and type=%s)\n", arg, "Hex"));
3806             break;
3807         case 13: //wep 104 Ascii type
3808             pAdapter->SharedKey[BSS0][0].KeyLen = KeyLen;
3809             memcpy(pAdapter->SharedKey[BSS0][0].Key, arg, KeyLen);
3810             CipherAlg = CIPHER_WEP128;
3811             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key1_Proc::(Key1=%s and type=%s)\n", arg, "Ascii"));
3812             break;
3813         case 26: //wep 104 Hex type
3814             for(i=0; i < KeyLen; i++)
3815             {
3816                 if( !isxdigit(*(arg+i)) )
3817                     return FALSE;  //Not Hex value;
3818             }
3819             pAdapter->SharedKey[BSS0][0].KeyLen = KeyLen / 2 ;
3820             AtoH(arg, pAdapter->SharedKey[BSS0][0].Key, KeyLen / 2);
3821             CipherAlg = CIPHER_WEP128;
3822             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key1_Proc::(Key1=%s and type=%s)\n", arg, "Hex"));
3823             break;
3824         default: //Invalid argument
3825             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key1_Proc::Invalid argument (=%s)\n", arg));
3826             return FALSE;
3827     }
3828
3829     pAdapter->SharedKey[BSS0][0].CipherAlg = CipherAlg;
3830
3831     // Set keys (into ASIC)
3832     if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
3833         ;   // not support
3834     else    // Old WEP stuff
3835     {
3836         AsicAddSharedKeyEntry(pAdapter,
3837                               0,
3838                               0,
3839                               pAdapter->SharedKey[BSS0][0].CipherAlg,
3840                               pAdapter->SharedKey[BSS0][0].Key,
3841                               NULL,
3842                               NULL);
3843     }
3844
3845     return TRUE;
3846 }
3847 /*
3848     ==========================================================================
3849
3850     Description:
3851         Set WEP KEY2
3852     Return:
3853         TRUE if all parameters are OK, FALSE otherwise
3854     ==========================================================================
3855 */
3856 INT Set_Key2_Proc(
3857     IN  PRTMP_ADAPTER   pAdapter,
3858     IN  PSTRING          arg)
3859 {
3860     int                                 KeyLen;
3861     int                                 i;
3862     UCHAR                               CipherAlg=CIPHER_WEP64;
3863
3864     if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
3865         return TRUE;    // do nothing
3866
3867     KeyLen = strlen(arg);
3868
3869     switch (KeyLen)
3870     {
3871         case 5: //wep 40 Ascii type
3872             pAdapter->SharedKey[BSS0][1].KeyLen = KeyLen;
3873             memcpy(pAdapter->SharedKey[BSS0][1].Key, arg, KeyLen);
3874             CipherAlg = CIPHER_WEP64;
3875             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key2_Proc::(Key2=%s and type=%s)\n", arg, "Ascii"));
3876             break;
3877         case 10: //wep 40 Hex type
3878             for(i=0; i < KeyLen; i++)
3879             {
3880                 if( !isxdigit(*(arg+i)) )
3881                     return FALSE;  //Not Hex value;
3882             }
3883             pAdapter->SharedKey[BSS0][1].KeyLen = KeyLen / 2 ;
3884             AtoH(arg, pAdapter->SharedKey[BSS0][1].Key, KeyLen / 2);
3885             CipherAlg = CIPHER_WEP64;
3886             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key2_Proc::(Key2=%s and type=%s)\n", arg, "Hex"));
3887             break;
3888         case 13: //wep 104 Ascii type
3889             pAdapter->SharedKey[BSS0][1].KeyLen = KeyLen;
3890             memcpy(pAdapter->SharedKey[BSS0][1].Key, arg, KeyLen);
3891             CipherAlg = CIPHER_WEP128;
3892             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key2_Proc::(Key2=%s and type=%s)\n", arg, "Ascii"));
3893             break;
3894         case 26: //wep 104 Hex type
3895             for(i=0; i < KeyLen; i++)
3896             {
3897                 if( !isxdigit(*(arg+i)) )
3898                     return FALSE;  //Not Hex value;
3899             }
3900             pAdapter->SharedKey[BSS0][1].KeyLen = KeyLen / 2 ;
3901             AtoH(arg, pAdapter->SharedKey[BSS0][1].Key, KeyLen / 2);
3902             CipherAlg = CIPHER_WEP128;
3903             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key2_Proc::(Key2=%s and type=%s)\n", arg, "Hex"));
3904             break;
3905         default: //Invalid argument
3906             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key2_Proc::Invalid argument (=%s)\n", arg));
3907             return FALSE;
3908     }
3909     pAdapter->SharedKey[BSS0][1].CipherAlg = CipherAlg;
3910
3911     // Set keys (into ASIC)
3912     if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
3913         ;   // not support
3914     else    // Old WEP stuff
3915     {
3916         AsicAddSharedKeyEntry(pAdapter,
3917                               0,
3918                               1,
3919                               pAdapter->SharedKey[BSS0][1].CipherAlg,
3920                               pAdapter->SharedKey[BSS0][1].Key,
3921                               NULL,
3922                               NULL);
3923     }
3924
3925     return TRUE;
3926 }
3927 /*
3928     ==========================================================================
3929     Description:
3930         Set WEP KEY3
3931     Return:
3932         TRUE if all parameters are OK, FALSE otherwise
3933     ==========================================================================
3934 */
3935 INT Set_Key3_Proc(
3936     IN  PRTMP_ADAPTER   pAdapter,
3937     IN  PSTRING          arg)
3938 {
3939     int                                 KeyLen;
3940     int                                 i;
3941     UCHAR                               CipherAlg=CIPHER_WEP64;
3942
3943     if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
3944         return TRUE;    // do nothing
3945
3946     KeyLen = strlen(arg);
3947
3948     switch (KeyLen)
3949     {
3950         case 5: //wep 40 Ascii type
3951             pAdapter->SharedKey[BSS0][2].KeyLen = KeyLen;
3952             memcpy(pAdapter->SharedKey[BSS0][2].Key, arg, KeyLen);
3953             CipherAlg = CIPHER_WEP64;
3954             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key3_Proc::(Key3=%s and type=Ascii)\n", arg));
3955             break;
3956         case 10: //wep 40 Hex type
3957             for(i=0; i < KeyLen; i++)
3958             {
3959                 if( !isxdigit(*(arg+i)) )
3960                     return FALSE;  //Not Hex value;
3961             }
3962             pAdapter->SharedKey[BSS0][2].KeyLen = KeyLen / 2 ;
3963             AtoH(arg, pAdapter->SharedKey[BSS0][2].Key, KeyLen / 2);
3964             CipherAlg = CIPHER_WEP64;
3965             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key3_Proc::(Key3=%s and type=Hex)\n", arg));
3966             break;
3967         case 13: //wep 104 Ascii type
3968             pAdapter->SharedKey[BSS0][2].KeyLen = KeyLen;
3969             memcpy(pAdapter->SharedKey[BSS0][2].Key, arg, KeyLen);
3970             CipherAlg = CIPHER_WEP128;
3971             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key3_Proc::(Key3=%s and type=Ascii)\n", arg));
3972             break;
3973         case 26: //wep 104 Hex type
3974             for(i=0; i < KeyLen; i++)
3975             {
3976                 if( !isxdigit(*(arg+i)) )
3977                     return FALSE;  //Not Hex value;
3978             }
3979             pAdapter->SharedKey[BSS0][2].KeyLen = KeyLen / 2 ;
3980             AtoH(arg, pAdapter->SharedKey[BSS0][2].Key, KeyLen / 2);
3981             CipherAlg = CIPHER_WEP128;
3982             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key3_Proc::(Key3=%s and type=Hex)\n", arg));
3983             break;
3984         default: //Invalid argument
3985             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key3_Proc::Invalid argument (=%s)\n", arg));
3986             return FALSE;
3987     }
3988     pAdapter->SharedKey[BSS0][2].CipherAlg = CipherAlg;
3989
3990     // Set keys (into ASIC)
3991     if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
3992         ;   // not support
3993     else    // Old WEP stuff
3994     {
3995         AsicAddSharedKeyEntry(pAdapter,
3996                               0,
3997                               2,
3998                               pAdapter->SharedKey[BSS0][2].CipherAlg,
3999                               pAdapter->SharedKey[BSS0][2].Key,
4000                               NULL,
4001                               NULL);
4002     }
4003
4004     return TRUE;
4005 }
4006 /*
4007     ==========================================================================
4008     Description:
4009         Set WEP KEY4
4010     Return:
4011         TRUE if all parameters are OK, FALSE otherwise
4012     ==========================================================================
4013 */
4014 INT Set_Key4_Proc(
4015     IN  PRTMP_ADAPTER   pAdapter,
4016     IN  PSTRING          arg)
4017 {
4018     int                                 KeyLen;
4019     int                                 i;
4020     UCHAR                               CipherAlg=CIPHER_WEP64;
4021
4022     if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
4023         return TRUE;    // do nothing
4024
4025     KeyLen = strlen(arg);
4026
4027     switch (KeyLen)
4028     {
4029         case 5: //wep 40 Ascii type
4030             pAdapter->SharedKey[BSS0][3].KeyLen = KeyLen;
4031             memcpy(pAdapter->SharedKey[BSS0][3].Key, arg, KeyLen);
4032             CipherAlg = CIPHER_WEP64;
4033             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key4_Proc::(Key4=%s and type=%s)\n", arg, "Ascii"));
4034             break;
4035         case 10: //wep 40 Hex type
4036             for(i=0; i < KeyLen; i++)
4037             {
4038                 if( !isxdigit(*(arg+i)) )
4039                     return FALSE;  //Not Hex value;
4040             }
4041             pAdapter->SharedKey[BSS0][3].KeyLen = KeyLen / 2 ;
4042             AtoH(arg, pAdapter->SharedKey[BSS0][3].Key, KeyLen / 2);
4043             CipherAlg = CIPHER_WEP64;
4044             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key4_Proc::(Key4=%s and type=%s)\n", arg, "Hex"));
4045             break;
4046         case 13: //wep 104 Ascii type
4047             pAdapter->SharedKey[BSS0][3].KeyLen = KeyLen;
4048             memcpy(pAdapter->SharedKey[BSS0][3].Key, arg, KeyLen);
4049             CipherAlg = CIPHER_WEP128;
4050             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key4_Proc::(Key4=%s and type=%s)\n", arg, "Ascii"));
4051             break;
4052         case 26: //wep 104 Hex type
4053             for(i=0; i < KeyLen; i++)
4054             {
4055                 if( !isxdigit(*(arg+i)) )
4056                     return FALSE;  //Not Hex value;
4057             }
4058             pAdapter->SharedKey[BSS0][3].KeyLen = KeyLen / 2 ;
4059             AtoH(arg, pAdapter->SharedKey[BSS0][3].Key, KeyLen / 2);
4060             CipherAlg = CIPHER_WEP128;
4061             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key4_Proc::(Key4=%s and type=%s)\n", arg, "Hex"));
4062             break;
4063         default: //Invalid argument
4064             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key4_Proc::Invalid argument (=%s)\n", arg));
4065             return FALSE;
4066     }
4067     pAdapter->SharedKey[BSS0][3].CipherAlg = CipherAlg;
4068
4069     // Set keys (into ASIC)
4070     if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
4071         ;   // not support
4072     else    // Old WEP stuff
4073     {
4074         AsicAddSharedKeyEntry(pAdapter,
4075                               0,
4076                               3,
4077                               pAdapter->SharedKey[BSS0][3].CipherAlg,
4078                               pAdapter->SharedKey[BSS0][3].Key,
4079                               NULL,
4080                               NULL);
4081     }
4082
4083     return TRUE;
4084 }
4085
4086 /*
4087     ==========================================================================
4088     Description:
4089         Set WPA PSK key
4090     Return:
4091         TRUE if all parameters are OK, FALSE otherwise
4092     ==========================================================================
4093 */
4094 INT Set_WPAPSK_Proc(
4095     IN  PRTMP_ADAPTER   pAd,
4096     IN  PSTRING          arg)
4097 {
4098     int status;
4099
4100     if ((pAd->StaCfg.AuthMode != Ndis802_11AuthModeWPAPSK) &&
4101         (pAd->StaCfg.AuthMode != Ndis802_11AuthModeWPA2PSK) &&
4102             (pAd->StaCfg.AuthMode != Ndis802_11AuthModeWPANone)
4103                 )
4104         return TRUE;    // do nothing
4105
4106     DBGPRINT(RT_DEBUG_TRACE, ("Set_WPAPSK_Proc::(WPAPSK=%s)\n", arg));
4107
4108         status = RT_CfgSetWPAPSKKey(pAd, arg, pAd->MlmeAux.Ssid, pAd->MlmeAux.SsidLen, pAd->StaCfg.PMK);
4109         if (status == FALSE)
4110         {
4111                 DBGPRINT(RT_DEBUG_TRACE, ("Set_WPAPSK_Proc(): Set key failed!\n"));
4112                 return FALSE;
4113         }
4114         NdisZeroMemory(pAd->StaCfg.WpaPassPhrase, 64);
4115     NdisMoveMemory(pAd->StaCfg.WpaPassPhrase, arg, strlen(arg));
4116     pAd->StaCfg.WpaPassPhraseLen = (UINT)strlen(arg);
4117
4118
4119
4120     if(pAd->StaCfg.BssType == BSS_ADHOC &&
4121        pAd->StaCfg.AuthMode == Ndis802_11AuthModeWPANone)
4122     {
4123         pAd->StaCfg.WpaState = SS_NOTUSE;
4124     }
4125     else
4126     {
4127         // Start STA supplicant state machine
4128         pAd->StaCfg.WpaState = SS_START;
4129     }
4130
4131     return TRUE;
4132 }
4133
4134 /*
4135     ==========================================================================
4136     Description:
4137         Set Power Saving mode
4138     Return:
4139         TRUE if all parameters are OK, FALSE otherwise
4140     ==========================================================================
4141 */
4142 INT Set_PSMode_Proc(
4143     IN  PRTMP_ADAPTER   pAdapter,
4144     IN  PSTRING          arg)
4145 {
4146     if (pAdapter->StaCfg.BssType == BSS_INFRA)
4147     {
4148         if ((strcmp(arg, "Max_PSP") == 0) ||
4149                         (strcmp(arg, "max_psp") == 0) ||
4150                         (strcmp(arg, "MAX_PSP") == 0))
4151         {
4152             // do NOT turn on PSM bit here, wait until MlmeCheckPsmChange()
4153             // to exclude certain situations.
4154             if (pAdapter->StaCfg.bWindowsACCAMEnable == FALSE)
4155                 pAdapter->StaCfg.WindowsPowerMode = Ndis802_11PowerModeMAX_PSP;
4156             pAdapter->StaCfg.WindowsBatteryPowerMode = Ndis802_11PowerModeMAX_PSP;
4157             OPSTATUS_SET_FLAG(pAdapter, fOP_STATUS_RECEIVE_DTIM);
4158             pAdapter->StaCfg.DefaultListenCount = 5;
4159
4160         }
4161         else if ((strcmp(arg, "Fast_PSP") == 0) ||
4162                                  (strcmp(arg, "fast_psp") == 0) ||
4163                  (strcmp(arg, "FAST_PSP") == 0))
4164         {
4165             // do NOT turn on PSM bit here, wait until MlmeCheckPsmChange()
4166             // to exclude certain situations.
4167             OPSTATUS_SET_FLAG(pAdapter, fOP_STATUS_RECEIVE_DTIM);
4168             if (pAdapter->StaCfg.bWindowsACCAMEnable == FALSE)
4169                 pAdapter->StaCfg.WindowsPowerMode = Ndis802_11PowerModeFast_PSP;
4170             pAdapter->StaCfg.WindowsBatteryPowerMode = Ndis802_11PowerModeFast_PSP;
4171             pAdapter->StaCfg.DefaultListenCount = 3;
4172         }
4173         else if ((strcmp(arg, "Legacy_PSP") == 0) ||
4174                  (strcmp(arg, "legacy_psp") == 0) ||
4175                  (strcmp(arg, "LEGACY_PSP") == 0))
4176         {
4177             // do NOT turn on PSM bit here, wait until MlmeCheckPsmChange()
4178             // to exclude certain situations.
4179             OPSTATUS_SET_FLAG(pAdapter, fOP_STATUS_RECEIVE_DTIM);
4180             if (pAdapter->StaCfg.bWindowsACCAMEnable == FALSE)
4181                 pAdapter->StaCfg.WindowsPowerMode = Ndis802_11PowerModeLegacy_PSP;
4182             pAdapter->StaCfg.WindowsBatteryPowerMode = Ndis802_11PowerModeLegacy_PSP;
4183             pAdapter->StaCfg.DefaultListenCount = 3;
4184         }
4185         else
4186         {
4187             //Default Ndis802_11PowerModeCAM
4188             // clear PSM bit immediately
4189             RTMP_SET_PSM_BIT(pAdapter, PWR_ACTIVE);
4190             OPSTATUS_SET_FLAG(pAdapter, fOP_STATUS_RECEIVE_DTIM);
4191             if (pAdapter->StaCfg.bWindowsACCAMEnable == FALSE)
4192                 pAdapter->StaCfg.WindowsPowerMode = Ndis802_11PowerModeCAM;
4193             pAdapter->StaCfg.WindowsBatteryPowerMode = Ndis802_11PowerModeCAM;
4194         }
4195
4196         DBGPRINT(RT_DEBUG_TRACE, ("Set_PSMode_Proc::(PSMode=%ld)\n", pAdapter->StaCfg.WindowsPowerMode));
4197     }
4198     else
4199         return FALSE;
4200
4201
4202     return TRUE;
4203 }
4204 //Add to suport the function which clould dynamicallly enable/disable PCIe Power Saving
4205
4206 #ifdef WPA_SUPPLICANT_SUPPORT
4207 /*
4208     ==========================================================================
4209     Description:
4210         Set WpaSupport flag.
4211     Value:
4212         0: Driver ignore wpa_supplicant.
4213         1: wpa_supplicant initiates scanning and AP selection.
4214         2: driver takes care of scanning, AP selection, and IEEE 802.11 association parameters.
4215     Return:
4216         TRUE if all parameters are OK, FALSE otherwise
4217     ==========================================================================
4218 */
4219 INT Set_Wpa_Support(
4220     IN  PRTMP_ADAPTER   pAd,
4221         IN      PSTRING                 arg)
4222 {
4223
4224     if ( simple_strtol(arg, 0, 10) == 0)
4225         pAd->StaCfg.WpaSupplicantUP = WPA_SUPPLICANT_DISABLE;
4226     else if ( simple_strtol(arg, 0, 10) == 1)
4227         pAd->StaCfg.WpaSupplicantUP = WPA_SUPPLICANT_ENABLE;
4228     else if ( simple_strtol(arg, 0, 10) == 2)
4229         pAd->StaCfg.WpaSupplicantUP = WPA_SUPPLICANT_ENABLE_WITH_WEB_UI;
4230     else
4231         pAd->StaCfg.WpaSupplicantUP = WPA_SUPPLICANT_DISABLE;
4232
4233     DBGPRINT(RT_DEBUG_TRACE, ("Set_Wpa_Support::(WpaSupplicantUP=%d)\n", pAd->StaCfg.WpaSupplicantUP));
4234
4235     return TRUE;
4236 }
4237 #endif // WPA_SUPPLICANT_SUPPORT //
4238
4239 INT Set_TGnWifiTest_Proc(
4240     IN  PRTMP_ADAPTER   pAd,
4241     IN  PSTRING          arg)
4242 {
4243     if (simple_strtol(arg, 0, 10) == 0)
4244         pAd->StaCfg.bTGnWifiTest = FALSE;
4245     else
4246         pAd->StaCfg.bTGnWifiTest = TRUE;
4247
4248     DBGPRINT(RT_DEBUG_TRACE, ("IF Set_TGnWifiTest_Proc::(bTGnWifiTest=%d)\n", pAd->StaCfg.bTGnWifiTest));
4249         return TRUE;
4250 }
4251
4252 #ifdef EXT_BUILD_CHANNEL_LIST
4253 INT Set_Ieee80211dClientMode_Proc(
4254     IN  PRTMP_ADAPTER   pAdapter,
4255     IN  PSTRING          arg)
4256 {
4257     if (simple_strtol(arg, 0, 10) == 0)
4258         pAdapter->StaCfg.IEEE80211dClientMode = Rt802_11_D_None;
4259     else if (simple_strtol(arg, 0, 10) == 1)
4260         pAdapter->StaCfg.IEEE80211dClientMode = Rt802_11_D_Flexible;
4261     else if (simple_strtol(arg, 0, 10) == 2)
4262         pAdapter->StaCfg.IEEE80211dClientMode = Rt802_11_D_Strict;
4263     else
4264         return FALSE;
4265
4266     DBGPRINT(RT_DEBUG_TRACE, ("Set_Ieee802dMode_Proc::(IEEEE0211dMode=%d)\n", pAdapter->StaCfg.IEEE80211dClientMode));
4267     return TRUE;
4268 }
4269 #endif // EXT_BUILD_CHANNEL_LIST //
4270
4271 #ifdef CARRIER_DETECTION_SUPPORT
4272 INT Set_CarrierDetect_Proc(
4273     IN  PRTMP_ADAPTER   pAd,
4274     IN  PSTRING         arg)
4275 {
4276     if (simple_strtol(arg, 0, 10) == 0)
4277         pAd->CommonCfg.CarrierDetect.Enable = FALSE;
4278     else
4279         pAd->CommonCfg.CarrierDetect.Enable = TRUE;
4280
4281     DBGPRINT(RT_DEBUG_TRACE, ("IF Set_CarrierDetect_Proc::(CarrierDetect.Enable=%d)\n", pAd->CommonCfg.CarrierDetect.Enable));
4282         return TRUE;
4283 }
4284 #endif // CARRIER_DETECTION_SUPPORT //
4285
4286
4287 INT     Show_Adhoc_MacTable_Proc(
4288         IN      PRTMP_ADAPTER   pAd,
4289         IN      PSTRING                 extra)
4290 {
4291         INT i;
4292
4293         sprintf(extra, "\n");
4294
4295 #ifdef DOT11_N_SUPPORT
4296         sprintf(extra, "%sHT Operating Mode : %d\n", extra, pAd->CommonCfg.AddHTInfo.AddHtInfo2.OperaionMode);
4297 #endif // DOT11_N_SUPPORT //
4298
4299         sprintf(extra, "%s\n%-19s%-4s%-4s%-7s%-7s%-7s%-10s%-6s%-6s%-6s%-6s\n", extra,
4300                         "MAC", "AID", "BSS", "RSSI0", "RSSI1", "RSSI2", "PhMd", "BW", "MCS", "SGI", "STBC");
4301
4302         for (i=1; i<MAX_LEN_OF_MAC_TABLE; i++)
4303         {
4304                 PMAC_TABLE_ENTRY pEntry = &pAd->MacTab.Content[i];
4305
4306                 if (strlen(extra) > (IW_PRIV_SIZE_MASK - 30))
4307                     break;
4308                 if ((pEntry->ValidAsCLI || pEntry->ValidAsApCli) && (pEntry->Sst == SST_ASSOC))
4309                 {
4310                         sprintf(extra, "%s%02X:%02X:%02X:%02X:%02X:%02X  ", extra,
4311                                 pEntry->Addr[0], pEntry->Addr[1], pEntry->Addr[2],
4312                                 pEntry->Addr[3], pEntry->Addr[4], pEntry->Addr[5]);
4313                         sprintf(extra, "%s%-4d", extra, (int)pEntry->Aid);
4314                         sprintf(extra, "%s%-4d", extra, (int)pEntry->apidx);
4315                         sprintf(extra, "%s%-7d", extra, pEntry->RssiSample.AvgRssi0);
4316                         sprintf(extra, "%s%-7d", extra, pEntry->RssiSample.AvgRssi1);
4317                         sprintf(extra, "%s%-7d", extra, pEntry->RssiSample.AvgRssi2);
4318                         sprintf(extra, "%s%-10s", extra, GetPhyMode(pEntry->HTPhyMode.field.MODE));
4319                         sprintf(extra, "%s%-6s", extra, GetBW(pEntry->HTPhyMode.field.BW));
4320                         sprintf(extra, "%s%-6d", extra, pEntry->HTPhyMode.field.MCS);
4321                         sprintf(extra, "%s%-6d", extra, pEntry->HTPhyMode.field.ShortGI);
4322                         sprintf(extra, "%s%-6d", extra, pEntry->HTPhyMode.field.STBC);
4323                         sprintf(extra, "%s%-10d, %d, %d%%\n", extra, pEntry->DebugFIFOCount, pEntry->DebugTxCount,
4324                                                 (pEntry->DebugTxCount) ? ((pEntry->DebugTxCount-pEntry->DebugFIFOCount)*100/pEntry->DebugTxCount) : 0);
4325                         sprintf(extra, "%s\n", extra);
4326                 }
4327         }
4328
4329         return TRUE;
4330 }
4331
4332
4333 INT Set_BeaconLostTime_Proc(
4334     IN  PRTMP_ADAPTER   pAd,
4335     IN  PSTRING         arg)
4336 {
4337         ULONG ltmp = (ULONG)simple_strtol(arg, 0, 10);
4338
4339         if ((ltmp != 0) && (ltmp <= 60))
4340                 pAd->StaCfg.BeaconLostTime = (ltmp * OS_HZ);
4341
4342     DBGPRINT(RT_DEBUG_TRACE, ("IF Set_BeaconLostTime_Proc::(BeaconLostTime=%ld)\n", pAd->StaCfg.BeaconLostTime));
4343         return TRUE;
4344 }
4345
4346 INT Set_AutoRoaming_Proc(
4347     IN  PRTMP_ADAPTER   pAd,
4348     IN  PSTRING         arg)
4349 {
4350     if (simple_strtol(arg, 0, 10) == 0)
4351         pAd->StaCfg.bAutoRoaming = FALSE;
4352     else
4353         pAd->StaCfg.bAutoRoaming = TRUE;
4354
4355     DBGPRINT(RT_DEBUG_TRACE, ("IF Set_AutoRoaming_Proc::(bAutoRoaming=%d)\n", pAd->StaCfg.bAutoRoaming));
4356         return TRUE;
4357 }
4358
4359
4360 /*
4361     ==========================================================================
4362     Description:
4363         Issue a site survey command to driver
4364         Arguments:
4365             pAdapter                    Pointer to our adapter
4366             wrq                         Pointer to the ioctl argument
4367
4368     Return Value:
4369         None
4370
4371     Note:
4372         Usage:
4373                1.) iwpriv ra0 set site_survey
4374     ==========================================================================
4375 */
4376 INT Set_SiteSurvey_Proc(
4377         IN      PRTMP_ADAPTER   pAd,
4378         IN      PSTRING                 arg)
4379 {
4380         NDIS_802_11_SSID Ssid;
4381
4382         //check if the interface is down
4383         if (!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_INTERRUPT_IN_USE))
4384         {
4385                 DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
4386                 return -ENETDOWN;
4387         }
4388
4389         if (MONITOR_ON(pAd))
4390     {
4391         DBGPRINT(RT_DEBUG_TRACE, ("!!! Driver is in Monitor Mode now !!!\n"));
4392         return -EINVAL;
4393     }
4394
4395         RTMPZeroMemory(&Ssid, sizeof(NDIS_802_11_SSID));
4396         Ssid.SsidLength = 0;
4397         if ((arg != NULL) &&
4398                 (strlen(arg) <= MAX_LEN_OF_SSID))
4399     {
4400         RTMPMoveMemory(Ssid.Ssid, arg, strlen(arg));
4401         Ssid.SsidLength = strlen(arg);
4402         }
4403
4404         pAd->StaCfg.bScanReqIsFromWebUI = TRUE;
4405
4406         if (pAd->Mlme.CntlMachine.CurrState != CNTL_IDLE)
4407         {
4408                 RTMP_MLME_RESET_STATE_MACHINE(pAd);
4409                 DBGPRINT(RT_DEBUG_TRACE, ("!!! MLME busy, reset MLME state machine !!!\n"));
4410         }
4411
4412         // tell CNTL state machine to call NdisMSetInformationComplete() after completing
4413         // this request, because this request is initiated by NDIS.
4414         pAd->MlmeAux.CurrReqIsFromNdis = FALSE;
4415         // Reset allowed scan retries
4416         pAd->StaCfg.ScanCnt = 0;
4417         NdisGetSystemUpTime(&pAd->StaCfg.LastScanTime);
4418
4419         MlmeEnqueue(pAd,
4420                 MLME_CNTL_STATE_MACHINE,
4421                 OID_802_11_BSSID_LIST_SCAN,
4422                 Ssid.SsidLength,
4423                 Ssid.Ssid);
4424
4425         RTMP_MLME_HANDLER(pAd);
4426
4427     DBGPRINT(RT_DEBUG_TRACE, ("Set_SiteSurvey_Proc\n"));
4428
4429     return TRUE;
4430 }
4431
4432 INT Set_ForceTxBurst_Proc(
4433     IN  PRTMP_ADAPTER   pAd,
4434     IN  PSTRING         arg)
4435 {
4436     if (simple_strtol(arg, 0, 10) == 0)
4437         pAd->StaCfg.bForceTxBurst = FALSE;
4438     else
4439         pAd->StaCfg.bForceTxBurst = TRUE;
4440
4441     DBGPRINT(RT_DEBUG_TRACE, ("IF Set_ForceTxBurst_Proc::(bForceTxBurst=%d)\n", pAd->StaCfg.bForceTxBurst));
4442         return TRUE;
4443 }
4444
4445 #ifdef ANT_DIVERSITY_SUPPORT
4446 INT     Set_Antenna_Proc(
4447         IN      PRTMP_ADAPTER   pAd,
4448         IN      PUCHAR                  arg)
4449 {
4450     UCHAR UsedAnt;
4451         DBGPRINT(RT_DEBUG_TRACE, ("==> Set_Antenna_Proc *******************\n"));
4452
4453     if(simple_strtol(arg, 0, 10) <= 3)
4454         UsedAnt = simple_strtol(arg, 0, 10);
4455
4456     pAd->CommonCfg.bRxAntDiversity = UsedAnt; // Auto switch
4457     if (UsedAnt == ANT_DIVERSITY_ENABLE)
4458     {
4459             pAd->RxAnt.EvaluateStableCnt = 0;
4460             DBGPRINT(RT_DEBUG_TRACE, ("<== Set_Antenna_Proc(Auto Switch Mode), (%d,%d)\n", pAd->RxAnt.Pair1PrimaryRxAnt, pAd->RxAnt.Pair1SecondaryRxAnt));
4461     }
4462     /* 2: Fix in the PHY Antenna CON1*/
4463     if (UsedAnt == ANT_FIX_ANT1)
4464     {
4465             AsicSetRxAnt(pAd, 0);
4466             DBGPRINT(RT_DEBUG_TRACE, ("<== Set_Antenna_Proc(Fix in Ant CON1), (%d,%d)\n", pAd->RxAnt.Pair1PrimaryRxAnt, pAd->RxAnt.Pair1SecondaryRxAnt));
4467     }
4468     /* 3: Fix in the PHY Antenna CON2*/
4469     if (UsedAnt == ANT_FIX_ANT2)
4470     {
4471             AsicSetRxAnt(pAd, 1);
4472             DBGPRINT(RT_DEBUG_TRACE, ("<== Set_Antenna_Proc(Fix in Ant CON2), (%d,%d)\n", pAd->RxAnt.Pair1PrimaryRxAnt, pAd->RxAnt.Pair1SecondaryRxAnt));
4473     }
4474
4475         return TRUE;
4476 }
4477 #endif // ANT_DIVERSITY_SUPPORT //