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