Staging: rt2860: add RT3090 chipset support
[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                         Status = -EOPNOTSUPP;
2982                         break;
2983                 case SIOCGIWPRIV:
2984                         if (wrq->u.data.pointer)
2985                         {
2986                                 if ( access_ok(VERIFY_WRITE, wrq->u.data.pointer, sizeof(privtab)) != TRUE)
2987                                         break;
2988                                 wrq->u.data.length = sizeof(privtab) / sizeof(privtab[0]);
2989                                 if (copy_to_user(wrq->u.data.pointer, privtab, sizeof(privtab)))
2990                                         Status = -EFAULT;
2991                         }
2992                         break;
2993                 case RTPRIV_IOCTL_SET:
2994                         if(access_ok(VERIFY_READ, wrq->u.data.pointer, wrq->u.data.length) != TRUE)
2995                                 break;
2996                         rt_ioctl_setparam(net_dev, NULL, NULL, wrq->u.data.pointer);
2997                         break;
2998                 case RTPRIV_IOCTL_GSITESURVEY:
2999                         RTMPIoctlGetSiteSurvey(pAd, wrq);
3000                     break;
3001         case SIOCETHTOOL:
3002                 break;
3003                 default:
3004                         DBGPRINT(RT_DEBUG_ERROR, ("IOCTL::unknown IOCTL's cmd = 0x%08x\n", cmd));
3005                         Status = -EOPNOTSUPP;
3006                         break;
3007         }
3008
3009     if(StateMachineTouched) // Upper layer sent a MLME-related operations
3010         RTMP_MLME_HANDLER(pAd);
3011
3012         return Status;
3013 }
3014
3015 /*
3016     ==========================================================================
3017     Description:
3018         Set SSID
3019     Return:
3020         TRUE if all parameters are OK, FALSE otherwise
3021     ==========================================================================
3022 */
3023 INT Set_SSID_Proc(
3024     IN  PRTMP_ADAPTER   pAdapter,
3025     IN  PSTRING          arg)
3026 {
3027     NDIS_802_11_SSID                    Ssid, *pSsid=NULL;
3028     BOOLEAN                             StateMachineTouched = FALSE;
3029     int                                 success = TRUE;
3030
3031     if( strlen(arg) <= MAX_LEN_OF_SSID)
3032     {
3033         NdisZeroMemory(&Ssid, sizeof(NDIS_802_11_SSID));
3034         if (strlen(arg) != 0)
3035         {
3036             NdisMoveMemory(Ssid.Ssid, arg, strlen(arg));
3037             Ssid.SsidLength = strlen(arg);
3038         }
3039         else   //ANY ssid
3040         {
3041             Ssid.SsidLength = 0;
3042                     memcpy(Ssid.Ssid, "", 0);
3043                         pAdapter->StaCfg.BssType = BSS_INFRA;
3044                         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeOpen;
3045                 pAdapter->StaCfg.WepStatus  = Ndis802_11EncryptionDisabled;
3046                 }
3047         pSsid = &Ssid;
3048
3049         if (pAdapter->Mlme.CntlMachine.CurrState != CNTL_IDLE)
3050         {
3051             RTMP_MLME_RESET_STATE_MACHINE(pAdapter);
3052             DBGPRINT(RT_DEBUG_TRACE, ("!!! MLME busy, reset MLME state machine !!!\n"));
3053         }
3054
3055                 if ((pAdapter->StaCfg.WpaPassPhraseLen >= 8) &&
3056                         (pAdapter->StaCfg.WpaPassPhraseLen <= 64))
3057                 {
3058                         STRING passphrase_str[65] = {0};
3059                         UCHAR keyMaterial[40];
3060
3061                         RTMPMoveMemory(passphrase_str, pAdapter->StaCfg.WpaPassPhrase, pAdapter->StaCfg.WpaPassPhraseLen);
3062                         RTMPZeroMemory(pAdapter->StaCfg.PMK, 32);
3063                         if (pAdapter->StaCfg.WpaPassPhraseLen == 64)
3064                         {
3065                             AtoH((PSTRING) pAdapter->StaCfg.WpaPassPhrase, pAdapter->StaCfg.PMK, 32);
3066                         }
3067                         else
3068                         {
3069                             PasswordHash((PSTRING) pAdapter->StaCfg.WpaPassPhrase, Ssid.Ssid, Ssid.SsidLength, keyMaterial);
3070                             NdisMoveMemory(pAdapter->StaCfg.PMK, keyMaterial, 32);
3071                         }
3072                 }
3073
3074         pAdapter->MlmeAux.CurrReqIsFromNdis = TRUE;
3075         pAdapter->StaCfg.bScanReqIsFromWebUI = FALSE;
3076                 pAdapter->bConfigChanged = TRUE;
3077
3078         MlmeEnqueue(pAdapter,
3079                     MLME_CNTL_STATE_MACHINE,
3080                     OID_802_11_SSID,
3081                     sizeof(NDIS_802_11_SSID),
3082                     (VOID *)pSsid);
3083
3084         StateMachineTouched = TRUE;
3085         DBGPRINT(RT_DEBUG_TRACE, ("Set_SSID_Proc::(Len=%d,Ssid=%s)\n", Ssid.SsidLength, Ssid.Ssid));
3086     }
3087     else
3088         success = FALSE;
3089
3090     if (StateMachineTouched) // Upper layer sent a MLME-related operations
3091         RTMP_MLME_HANDLER(pAdapter);
3092
3093     return success;
3094 }
3095
3096 #ifdef WMM_SUPPORT
3097 /*
3098     ==========================================================================
3099     Description:
3100         Set WmmCapable Enable or Disable
3101     Return:
3102         TRUE if all parameters are OK, FALSE otherwise
3103     ==========================================================================
3104 */
3105 INT     Set_WmmCapable_Proc(
3106         IN      PRTMP_ADAPTER   pAd,
3107         IN      PSTRING                 arg)
3108 {
3109         BOOLEAN bWmmCapable;
3110
3111         bWmmCapable = simple_strtol(arg, 0, 10);
3112
3113         if ((bWmmCapable == 1)
3114 #ifdef RTMP_MAC_USB
3115                 && (pAd->NumberOfPipes >= 5)
3116 #endif // RTMP_MAC_USB //
3117                 )
3118                 pAd->CommonCfg.bWmmCapable = TRUE;
3119         else if (bWmmCapable == 0)
3120                 pAd->CommonCfg.bWmmCapable = FALSE;
3121         else
3122                 return FALSE;  //Invalid argument
3123
3124         DBGPRINT(RT_DEBUG_TRACE, ("Set_WmmCapable_Proc::(bWmmCapable=%d)\n",
3125                 pAd->CommonCfg.bWmmCapable));
3126
3127         return TRUE;
3128 }
3129 #endif // WMM_SUPPORT //
3130
3131 /*
3132     ==========================================================================
3133     Description:
3134         Set Network Type(Infrastructure/Adhoc mode)
3135     Return:
3136         TRUE if all parameters are OK, FALSE otherwise
3137     ==========================================================================
3138 */
3139 INT Set_NetworkType_Proc(
3140     IN  PRTMP_ADAPTER   pAdapter,
3141     IN  PSTRING          arg)
3142 {
3143     UINT32      Value = 0;
3144
3145     if (strcmp(arg, "Adhoc") == 0)
3146         {
3147                 if (pAdapter->StaCfg.BssType != BSS_ADHOC)
3148                 {
3149                         // Config has changed
3150                         pAdapter->bConfigChanged = TRUE;
3151             if (MONITOR_ON(pAdapter))
3152             {
3153                 RTMP_IO_WRITE32(pAdapter, RX_FILTR_CFG, STANORMAL);
3154                 RTMP_IO_READ32(pAdapter, MAC_SYS_CTRL, &Value);
3155                                 Value &= (~0x80);
3156                                 RTMP_IO_WRITE32(pAdapter, MAC_SYS_CTRL, Value);
3157                 OPSTATUS_CLEAR_FLAG(pAdapter, fOP_STATUS_MEDIA_STATE_CONNECTED);
3158                 pAdapter->StaCfg.bAutoReconnect = TRUE;
3159                 LinkDown(pAdapter, FALSE);
3160             }
3161                         if (INFRA_ON(pAdapter))
3162                         {
3163                                 //BOOLEAN Cancelled;
3164                                 // Set the AutoReconnectSsid to prevent it reconnect to old SSID
3165                                 // Since calling this indicate user don't want to connect to that SSID anymore.
3166                                 pAdapter->MlmeAux.AutoReconnectSsidLen= 32;
3167                                 NdisZeroMemory(pAdapter->MlmeAux.AutoReconnectSsid, pAdapter->MlmeAux.AutoReconnectSsidLen);
3168
3169                                 LinkDown(pAdapter, FALSE);
3170
3171                                 DBGPRINT(RT_DEBUG_TRACE, ("NDIS_STATUS_MEDIA_DISCONNECT Event BB!\n"));
3172                         }
3173                 }
3174                 pAdapter->StaCfg.BssType = BSS_ADHOC;
3175         pAdapter->net_dev->type = pAdapter->StaCfg.OriDevType;
3176                 DBGPRINT(RT_DEBUG_TRACE, ("===>Set_NetworkType_Proc::(AD-HOC)\n"));
3177         }
3178     else if (strcmp(arg, "Infra") == 0)
3179         {
3180                 if (pAdapter->StaCfg.BssType != BSS_INFRA)
3181                 {
3182                         // Config has changed
3183                         pAdapter->bConfigChanged = TRUE;
3184             if (MONITOR_ON(pAdapter))
3185             {
3186                 RTMP_IO_WRITE32(pAdapter, RX_FILTR_CFG, STANORMAL);
3187                 RTMP_IO_READ32(pAdapter, MAC_SYS_CTRL, &Value);
3188                                 Value &= (~0x80);
3189                                 RTMP_IO_WRITE32(pAdapter, MAC_SYS_CTRL, Value);
3190                 OPSTATUS_CLEAR_FLAG(pAdapter, fOP_STATUS_MEDIA_STATE_CONNECTED);
3191                 pAdapter->StaCfg.bAutoReconnect = TRUE;
3192                 LinkDown(pAdapter, FALSE);
3193             }
3194                         if (ADHOC_ON(pAdapter))
3195                         {
3196                                 // Set the AutoReconnectSsid to prevent it reconnect to old SSID
3197                                 // Since calling this indicate user don't want to connect to that SSID anymore.
3198                                 pAdapter->MlmeAux.AutoReconnectSsidLen= 32;
3199                                 NdisZeroMemory(pAdapter->MlmeAux.AutoReconnectSsid, pAdapter->MlmeAux.AutoReconnectSsidLen);
3200
3201                                 LinkDown(pAdapter, FALSE);
3202                         }
3203                 }
3204                 pAdapter->StaCfg.BssType = BSS_INFRA;
3205         pAdapter->net_dev->type = pAdapter->StaCfg.OriDevType;
3206                 DBGPRINT(RT_DEBUG_TRACE, ("===>Set_NetworkType_Proc::(INFRA)\n"));
3207         }
3208     else if (strcmp(arg, "Monitor") == 0)
3209     {
3210                 UCHAR   bbpValue = 0;
3211                 BCN_TIME_CFG_STRUC csr;
3212                 OPSTATUS_CLEAR_FLAG(pAdapter, fOP_STATUS_INFRA_ON);
3213         OPSTATUS_CLEAR_FLAG(pAdapter, fOP_STATUS_ADHOC_ON);
3214                 OPSTATUS_SET_FLAG(pAdapter, fOP_STATUS_MEDIA_STATE_CONNECTED);
3215                 // disable all periodic state machine
3216                 pAdapter->StaCfg.bAutoReconnect = FALSE;
3217                 // reset all mlme state machine
3218                         RTMP_MLME_RESET_STATE_MACHINE(pAdapter);
3219                 DBGPRINT(RT_DEBUG_TRACE, ("fOP_STATUS_MEDIA_STATE_CONNECTED \n"));
3220         if (pAdapter->CommonCfg.CentralChannel == 0)
3221         {
3222             if (pAdapter->CommonCfg.PhyMode == PHY_11AN_MIXED)
3223                 pAdapter->CommonCfg.CentralChannel = 36;
3224             else
3225                 pAdapter->CommonCfg.CentralChannel = 6;
3226         }
3227         else
3228             N_ChannelCheck(pAdapter);
3229
3230         if (pAdapter->CommonCfg.PhyMode >= PHY_11ABGN_MIXED &&
3231             pAdapter->CommonCfg.RegTransmitSetting.field.BW == BW_40 &&
3232             pAdapter->CommonCfg.RegTransmitSetting.field.EXTCHA == EXTCHA_ABOVE)
3233                 {
3234                         // 40MHz ,control channel at lower
3235                         RTMP_BBP_IO_READ8_BY_REG_ID(pAdapter, BBP_R4, &bbpValue);
3236                         bbpValue &= (~0x18);
3237                         bbpValue |= 0x10;
3238                         RTMP_BBP_IO_WRITE8_BY_REG_ID(pAdapter, BBP_R4, bbpValue);
3239                         pAdapter->CommonCfg.BBPCurrentBW = BW_40;
3240                         //  RX : control channel at lower
3241                         RTMP_BBP_IO_READ8_BY_REG_ID(pAdapter, BBP_R3, &bbpValue);
3242                         bbpValue &= (~0x20);
3243                         RTMP_BBP_IO_WRITE8_BY_REG_ID(pAdapter, BBP_R3, bbpValue);
3244
3245                         RTMP_IO_READ32(pAdapter, TX_BAND_CFG, &Value);
3246                         Value &= 0xfffffffe;
3247                         RTMP_IO_WRITE32(pAdapter, TX_BAND_CFG, Value);
3248                         pAdapter->CommonCfg.CentralChannel = pAdapter->CommonCfg.Channel + 2;
3249             AsicSwitchChannel(pAdapter, pAdapter->CommonCfg.CentralChannel, FALSE);
3250                     AsicLockChannel(pAdapter, pAdapter->CommonCfg.CentralChannel);
3251             DBGPRINT(RT_DEBUG_TRACE, ("BW_40 ,control_channel(%d), CentralChannel(%d) \n",
3252                                        pAdapter->CommonCfg.Channel,
3253                                        pAdapter->CommonCfg.CentralChannel));
3254                 }
3255                 else if (pAdapter->CommonCfg.PhyMode >= PHY_11ABGN_MIXED &&
3256                  pAdapter->CommonCfg.RegTransmitSetting.field.BW == BW_40 &&
3257                  pAdapter->CommonCfg.RegTransmitSetting.field.EXTCHA == EXTCHA_BELOW)
3258                 {
3259                         // 40MHz ,control channel at upper
3260                         RTMP_BBP_IO_READ8_BY_REG_ID(pAdapter, BBP_R4, &bbpValue);
3261                         bbpValue &= (~0x18);
3262                         bbpValue |= 0x10;
3263                         RTMP_BBP_IO_WRITE8_BY_REG_ID(pAdapter, BBP_R4, bbpValue);
3264                         pAdapter->CommonCfg.BBPCurrentBW = BW_40;
3265                         RTMP_IO_READ32(pAdapter, TX_BAND_CFG, &Value);
3266                         Value |= 0x1;
3267                         RTMP_IO_WRITE32(pAdapter, TX_BAND_CFG, Value);
3268
3269                         RTMP_BBP_IO_READ8_BY_REG_ID(pAdapter, BBP_R3, &bbpValue);
3270                         bbpValue |= (0x20);
3271                         RTMP_BBP_IO_WRITE8_BY_REG_ID(pAdapter, BBP_R3, bbpValue);
3272                         pAdapter->CommonCfg.CentralChannel = pAdapter->CommonCfg.Channel - 2;
3273             AsicSwitchChannel(pAdapter, pAdapter->CommonCfg.CentralChannel, FALSE);
3274                     AsicLockChannel(pAdapter, pAdapter->CommonCfg.CentralChannel);
3275             DBGPRINT(RT_DEBUG_TRACE, ("BW_40 ,control_channel(%d), CentralChannel(%d) \n",
3276                                        pAdapter->CommonCfg.Channel,
3277                                        pAdapter->CommonCfg.CentralChannel));
3278                 }
3279                 else
3280                 {
3281                         // 20MHz
3282                         RTMP_BBP_IO_READ8_BY_REG_ID(pAdapter, BBP_R4, &bbpValue);
3283                         bbpValue &= (~0x18);
3284                         RTMP_BBP_IO_WRITE8_BY_REG_ID(pAdapter, BBP_R4, bbpValue);
3285                         pAdapter->CommonCfg.BBPCurrentBW = BW_20;
3286                         AsicSwitchChannel(pAdapter, pAdapter->CommonCfg.Channel, FALSE);
3287                         AsicLockChannel(pAdapter, pAdapter->CommonCfg.Channel);
3288                         DBGPRINT(RT_DEBUG_TRACE, ("BW_20, Channel(%d)\n", pAdapter->CommonCfg.Channel));
3289                 }
3290                 // Enable Rx with promiscuous reception
3291                 RTMP_IO_WRITE32(pAdapter, RX_FILTR_CFG, 0x3);
3292                 // ASIC supporsts sniffer function with replacing RSSI with timestamp.
3293                 //RTMP_IO_READ32(pAdapter, MAC_SYS_CTRL, &Value);
3294                 //Value |= (0x80);
3295                 //RTMP_IO_WRITE32(pAdapter, MAC_SYS_CTRL, Value);
3296                 // disable sync
3297                 RTMP_IO_READ32(pAdapter, BCN_TIME_CFG, &csr.word);
3298                 csr.field.bBeaconGen = 0;
3299                 csr.field.bTBTTEnable = 0;
3300                 csr.field.TsfSyncMode = 0;
3301                 RTMP_IO_WRITE32(pAdapter, BCN_TIME_CFG, csr.word);
3302
3303                 pAdapter->StaCfg.BssType = BSS_MONITOR;
3304         pAdapter->net_dev->type = ARPHRD_IEEE80211_PRISM; //ARPHRD_IEEE80211; // IEEE80211
3305                 DBGPRINT(RT_DEBUG_TRACE, ("===>Set_NetworkType_Proc::(MONITOR)\n"));
3306     }
3307
3308     // Reset Ralink supplicant to not use, it will be set to start when UI set PMK key
3309     pAdapter->StaCfg.WpaState = SS_NOTUSE;
3310
3311     DBGPRINT(RT_DEBUG_TRACE, ("Set_NetworkType_Proc::(NetworkType=%d)\n", pAdapter->StaCfg.BssType));
3312
3313     return TRUE;
3314 }
3315
3316 /*
3317     ==========================================================================
3318     Description:
3319         Set Authentication mode
3320     Return:
3321         TRUE if all parameters are OK, FALSE otherwise
3322     ==========================================================================
3323 */
3324 INT Set_AuthMode_Proc(
3325     IN  PRTMP_ADAPTER   pAdapter,
3326     IN  PSTRING          arg)
3327 {
3328     if ((strcmp(arg, "WEPAUTO") == 0) || (strcmp(arg, "wepauto") == 0))
3329         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeAutoSwitch;
3330     else if ((strcmp(arg, "OPEN") == 0) || (strcmp(arg, "open") == 0))
3331         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeOpen;
3332     else if ((strcmp(arg, "SHARED") == 0) || (strcmp(arg, "shared") == 0))
3333         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeShared;
3334     else if ((strcmp(arg, "WPAPSK") == 0) || (strcmp(arg, "wpapsk") == 0))
3335         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeWPAPSK;
3336     else if ((strcmp(arg, "WPANONE") == 0) || (strcmp(arg, "wpanone") == 0))
3337         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeWPANone;
3338     else if ((strcmp(arg, "WPA2PSK") == 0) || (strcmp(arg, "wpa2psk") == 0))
3339         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeWPA2PSK;
3340     else if ((strcmp(arg, "WPA") == 0) || (strcmp(arg, "wpa") == 0))
3341         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeWPA;
3342     else if ((strcmp(arg, "WPA2") == 0) || (strcmp(arg, "wpa2") == 0))
3343         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeWPA2;
3344     else
3345         return FALSE;
3346
3347     pAdapter->StaCfg.PortSecured = WPA_802_1X_PORT_NOT_SECURED;
3348
3349     DBGPRINT(RT_DEBUG_TRACE, ("Set_AuthMode_Proc::(AuthMode=%d)\n", pAdapter->StaCfg.AuthMode));
3350
3351     return TRUE;
3352 }
3353
3354 /*
3355     ==========================================================================
3356     Description:
3357         Set Encryption Type
3358     Return:
3359         TRUE if all parameters are OK, FALSE otherwise
3360     ==========================================================================
3361 */
3362 INT Set_EncrypType_Proc(
3363     IN  PRTMP_ADAPTER   pAdapter,
3364     IN  PSTRING          arg)
3365 {
3366     if ((strcmp(arg, "NONE") == 0) || (strcmp(arg, "none") == 0))
3367     {
3368         if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
3369             return TRUE;    // do nothing
3370
3371         pAdapter->StaCfg.WepStatus     = Ndis802_11WEPDisabled;
3372         pAdapter->StaCfg.PairCipher    = Ndis802_11WEPDisabled;
3373             pAdapter->StaCfg.GroupCipher   = Ndis802_11WEPDisabled;
3374     }
3375     else if ((strcmp(arg, "WEP") == 0) || (strcmp(arg, "wep") == 0))
3376     {
3377         if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
3378             return TRUE;    // do nothing
3379
3380         pAdapter->StaCfg.WepStatus     = Ndis802_11WEPEnabled;
3381         pAdapter->StaCfg.PairCipher    = Ndis802_11WEPEnabled;
3382             pAdapter->StaCfg.GroupCipher   = Ndis802_11WEPEnabled;
3383     }
3384     else if ((strcmp(arg, "TKIP") == 0) || (strcmp(arg, "tkip") == 0))
3385     {
3386         if (pAdapter->StaCfg.AuthMode < Ndis802_11AuthModeWPA)
3387             return TRUE;    // do nothing
3388
3389         pAdapter->StaCfg.WepStatus     = Ndis802_11Encryption2Enabled;
3390         pAdapter->StaCfg.PairCipher    = Ndis802_11Encryption2Enabled;
3391             pAdapter->StaCfg.GroupCipher   = Ndis802_11Encryption2Enabled;
3392     }
3393     else if ((strcmp(arg, "AES") == 0) || (strcmp(arg, "aes") == 0))
3394     {
3395         if (pAdapter->StaCfg.AuthMode < Ndis802_11AuthModeWPA)
3396             return TRUE;    // do nothing
3397
3398         pAdapter->StaCfg.WepStatus     = Ndis802_11Encryption3Enabled;
3399         pAdapter->StaCfg.PairCipher    = Ndis802_11Encryption3Enabled;
3400             pAdapter->StaCfg.GroupCipher   = Ndis802_11Encryption3Enabled;
3401     }
3402     else
3403         return FALSE;
3404
3405     pAdapter->StaCfg.OrigWepStatus = pAdapter->StaCfg.WepStatus;
3406
3407     DBGPRINT(RT_DEBUG_TRACE, ("Set_EncrypType_Proc::(EncrypType=%d)\n", pAdapter->StaCfg.WepStatus));
3408
3409     return TRUE;
3410 }
3411
3412 /*
3413     ==========================================================================
3414     Description:
3415         Set Default Key ID
3416     Return:
3417         TRUE if all parameters are OK, FALSE otherwise
3418     ==========================================================================
3419 */
3420 INT Set_DefaultKeyID_Proc(
3421     IN  PRTMP_ADAPTER   pAdapter,
3422     IN  PSTRING          arg)
3423 {
3424     ULONG                               KeyIdx;
3425
3426     KeyIdx = simple_strtol(arg, 0, 10);
3427     if((KeyIdx >= 1 ) && (KeyIdx <= 4))
3428         pAdapter->StaCfg.DefaultKeyId = (UCHAR) (KeyIdx - 1 );
3429     else
3430         return FALSE;  //Invalid argument
3431
3432     DBGPRINT(RT_DEBUG_TRACE, ("Set_DefaultKeyID_Proc::(DefaultKeyID=%d)\n", pAdapter->StaCfg.DefaultKeyId));
3433
3434     return TRUE;
3435 }
3436
3437 /*
3438     ==========================================================================
3439     Description:
3440         Set WEP KEY1
3441     Return:
3442         TRUE if all parameters are OK, FALSE otherwise
3443     ==========================================================================
3444 */
3445 INT Set_Key1_Proc(
3446     IN  PRTMP_ADAPTER   pAdapter,
3447     IN  PSTRING          arg)
3448 {
3449     int                                 KeyLen;
3450     int                                 i;
3451     UCHAR                               CipherAlg=CIPHER_WEP64;
3452
3453     if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
3454         return TRUE;    // do nothing
3455
3456     KeyLen = strlen(arg);
3457
3458     switch (KeyLen)
3459     {
3460         case 5: //wep 40 Ascii type
3461             pAdapter->SharedKey[BSS0][0].KeyLen = KeyLen;
3462             memcpy(pAdapter->SharedKey[BSS0][0].Key, arg, KeyLen);
3463             CipherAlg = CIPHER_WEP64;
3464             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key1_Proc::(Key1=%s and type=%s)\n", arg, "Ascii"));
3465             break;
3466         case 10: //wep 40 Hex type
3467             for(i=0; i < KeyLen; i++)
3468             {
3469                 if( !isxdigit(*(arg+i)) )
3470                     return FALSE;  //Not Hex value;
3471             }
3472             pAdapter->SharedKey[BSS0][0].KeyLen = KeyLen / 2 ;
3473             AtoH(arg, pAdapter->SharedKey[BSS0][0].Key, KeyLen / 2);
3474             CipherAlg = CIPHER_WEP64;
3475             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key1_Proc::(Key1=%s and type=%s)\n", arg, "Hex"));
3476             break;
3477         case 13: //wep 104 Ascii type
3478             pAdapter->SharedKey[BSS0][0].KeyLen = KeyLen;
3479             memcpy(pAdapter->SharedKey[BSS0][0].Key, arg, KeyLen);
3480             CipherAlg = CIPHER_WEP128;
3481             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key1_Proc::(Key1=%s and type=%s)\n", arg, "Ascii"));
3482             break;
3483         case 26: //wep 104 Hex type
3484             for(i=0; i < KeyLen; i++)
3485             {
3486                 if( !isxdigit(*(arg+i)) )
3487                     return FALSE;  //Not Hex value;
3488             }
3489             pAdapter->SharedKey[BSS0][0].KeyLen = KeyLen / 2 ;
3490             AtoH(arg, pAdapter->SharedKey[BSS0][0].Key, KeyLen / 2);
3491             CipherAlg = CIPHER_WEP128;
3492             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key1_Proc::(Key1=%s and type=%s)\n", arg, "Hex"));
3493             break;
3494         default: //Invalid argument
3495             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key1_Proc::Invalid argument (=%s)\n", arg));
3496             return FALSE;
3497     }
3498
3499     pAdapter->SharedKey[BSS0][0].CipherAlg = CipherAlg;
3500
3501     // Set keys (into ASIC)
3502     if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
3503         ;   // not support
3504     else    // Old WEP stuff
3505     {
3506         AsicAddSharedKeyEntry(pAdapter,
3507                               0,
3508                               0,
3509                               pAdapter->SharedKey[BSS0][0].CipherAlg,
3510                               pAdapter->SharedKey[BSS0][0].Key,
3511                               NULL,
3512                               NULL);
3513     }
3514
3515     return TRUE;
3516 }
3517 /*
3518     ==========================================================================
3519
3520     Description:
3521         Set WEP KEY2
3522     Return:
3523         TRUE if all parameters are OK, FALSE otherwise
3524     ==========================================================================
3525 */
3526 INT Set_Key2_Proc(
3527     IN  PRTMP_ADAPTER   pAdapter,
3528     IN  PSTRING          arg)
3529 {
3530     int                                 KeyLen;
3531     int                                 i;
3532     UCHAR                               CipherAlg=CIPHER_WEP64;
3533
3534     if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
3535         return TRUE;    // do nothing
3536
3537     KeyLen = strlen(arg);
3538
3539     switch (KeyLen)
3540     {
3541         case 5: //wep 40 Ascii type
3542             pAdapter->SharedKey[BSS0][1].KeyLen = KeyLen;
3543             memcpy(pAdapter->SharedKey[BSS0][1].Key, arg, KeyLen);
3544             CipherAlg = CIPHER_WEP64;
3545             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key2_Proc::(Key2=%s and type=%s)\n", arg, "Ascii"));
3546             break;
3547         case 10: //wep 40 Hex type
3548             for(i=0; i < KeyLen; i++)
3549             {
3550                 if( !isxdigit(*(arg+i)) )
3551                     return FALSE;  //Not Hex value;
3552             }
3553             pAdapter->SharedKey[BSS0][1].KeyLen = KeyLen / 2 ;
3554             AtoH(arg, pAdapter->SharedKey[BSS0][1].Key, KeyLen / 2);
3555             CipherAlg = CIPHER_WEP64;
3556             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key2_Proc::(Key2=%s and type=%s)\n", arg, "Hex"));
3557             break;
3558         case 13: //wep 104 Ascii type
3559             pAdapter->SharedKey[BSS0][1].KeyLen = KeyLen;
3560             memcpy(pAdapter->SharedKey[BSS0][1].Key, arg, KeyLen);
3561             CipherAlg = CIPHER_WEP128;
3562             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key2_Proc::(Key2=%s and type=%s)\n", arg, "Ascii"));
3563             break;
3564         case 26: //wep 104 Hex type
3565             for(i=0; i < KeyLen; i++)
3566             {
3567                 if( !isxdigit(*(arg+i)) )
3568                     return FALSE;  //Not Hex value;
3569             }
3570             pAdapter->SharedKey[BSS0][1].KeyLen = KeyLen / 2 ;
3571             AtoH(arg, pAdapter->SharedKey[BSS0][1].Key, KeyLen / 2);
3572             CipherAlg = CIPHER_WEP128;
3573             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key2_Proc::(Key2=%s and type=%s)\n", arg, "Hex"));
3574             break;
3575         default: //Invalid argument
3576             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key2_Proc::Invalid argument (=%s)\n", arg));
3577             return FALSE;
3578     }
3579     pAdapter->SharedKey[BSS0][1].CipherAlg = CipherAlg;
3580
3581     // Set keys (into ASIC)
3582     if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
3583         ;   // not support
3584     else    // Old WEP stuff
3585     {
3586         AsicAddSharedKeyEntry(pAdapter,
3587                               0,
3588                               1,
3589                               pAdapter->SharedKey[BSS0][1].CipherAlg,
3590                               pAdapter->SharedKey[BSS0][1].Key,
3591                               NULL,
3592                               NULL);
3593     }
3594
3595     return TRUE;
3596 }
3597 /*
3598     ==========================================================================
3599     Description:
3600         Set WEP KEY3
3601     Return:
3602         TRUE if all parameters are OK, FALSE otherwise
3603     ==========================================================================
3604 */
3605 INT Set_Key3_Proc(
3606     IN  PRTMP_ADAPTER   pAdapter,
3607     IN  PSTRING          arg)
3608 {
3609     int                                 KeyLen;
3610     int                                 i;
3611     UCHAR                               CipherAlg=CIPHER_WEP64;
3612
3613     if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
3614         return TRUE;    // do nothing
3615
3616     KeyLen = strlen(arg);
3617
3618     switch (KeyLen)
3619     {
3620         case 5: //wep 40 Ascii type
3621             pAdapter->SharedKey[BSS0][2].KeyLen = KeyLen;
3622             memcpy(pAdapter->SharedKey[BSS0][2].Key, arg, KeyLen);
3623             CipherAlg = CIPHER_WEP64;
3624             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key3_Proc::(Key3=%s and type=Ascii)\n", arg));
3625             break;
3626         case 10: //wep 40 Hex type
3627             for(i=0; i < KeyLen; i++)
3628             {
3629                 if( !isxdigit(*(arg+i)) )
3630                     return FALSE;  //Not Hex value;
3631             }
3632             pAdapter->SharedKey[BSS0][2].KeyLen = KeyLen / 2 ;
3633             AtoH(arg, pAdapter->SharedKey[BSS0][2].Key, KeyLen / 2);
3634             CipherAlg = CIPHER_WEP64;
3635             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key3_Proc::(Key3=%s and type=Hex)\n", arg));
3636             break;
3637         case 13: //wep 104 Ascii type
3638             pAdapter->SharedKey[BSS0][2].KeyLen = KeyLen;
3639             memcpy(pAdapter->SharedKey[BSS0][2].Key, arg, KeyLen);
3640             CipherAlg = CIPHER_WEP128;
3641             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key3_Proc::(Key3=%s and type=Ascii)\n", arg));
3642             break;
3643         case 26: //wep 104 Hex type
3644             for(i=0; i < KeyLen; i++)
3645             {
3646                 if( !isxdigit(*(arg+i)) )
3647                     return FALSE;  //Not Hex value;
3648             }
3649             pAdapter->SharedKey[BSS0][2].KeyLen = KeyLen / 2 ;
3650             AtoH(arg, pAdapter->SharedKey[BSS0][2].Key, KeyLen / 2);
3651             CipherAlg = CIPHER_WEP128;
3652             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key3_Proc::(Key3=%s and type=Hex)\n", arg));
3653             break;
3654         default: //Invalid argument
3655             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key3_Proc::Invalid argument (=%s)\n", arg));
3656             return FALSE;
3657     }
3658     pAdapter->SharedKey[BSS0][2].CipherAlg = CipherAlg;
3659
3660     // Set keys (into ASIC)
3661     if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
3662         ;   // not support
3663     else    // Old WEP stuff
3664     {
3665         AsicAddSharedKeyEntry(pAdapter,
3666                               0,
3667                               2,
3668                               pAdapter->SharedKey[BSS0][2].CipherAlg,
3669                               pAdapter->SharedKey[BSS0][2].Key,
3670                               NULL,
3671                               NULL);
3672     }
3673
3674     return TRUE;
3675 }
3676 /*
3677     ==========================================================================
3678     Description:
3679         Set WEP KEY4
3680     Return:
3681         TRUE if all parameters are OK, FALSE otherwise
3682     ==========================================================================
3683 */
3684 INT Set_Key4_Proc(
3685     IN  PRTMP_ADAPTER   pAdapter,
3686     IN  PSTRING          arg)
3687 {
3688     int                                 KeyLen;
3689     int                                 i;
3690     UCHAR                               CipherAlg=CIPHER_WEP64;
3691
3692     if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
3693         return TRUE;    // do nothing
3694
3695     KeyLen = strlen(arg);
3696
3697     switch (KeyLen)
3698     {
3699         case 5: //wep 40 Ascii type
3700             pAdapter->SharedKey[BSS0][3].KeyLen = KeyLen;
3701             memcpy(pAdapter->SharedKey[BSS0][3].Key, arg, KeyLen);
3702             CipherAlg = CIPHER_WEP64;
3703             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key4_Proc::(Key4=%s and type=%s)\n", arg, "Ascii"));
3704             break;
3705         case 10: //wep 40 Hex type
3706             for(i=0; i < KeyLen; i++)
3707             {
3708                 if( !isxdigit(*(arg+i)) )
3709                     return FALSE;  //Not Hex value;
3710             }
3711             pAdapter->SharedKey[BSS0][3].KeyLen = KeyLen / 2 ;
3712             AtoH(arg, pAdapter->SharedKey[BSS0][3].Key, KeyLen / 2);
3713             CipherAlg = CIPHER_WEP64;
3714             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key4_Proc::(Key4=%s and type=%s)\n", arg, "Hex"));
3715             break;
3716         case 13: //wep 104 Ascii type
3717             pAdapter->SharedKey[BSS0][3].KeyLen = KeyLen;
3718             memcpy(pAdapter->SharedKey[BSS0][3].Key, arg, KeyLen);
3719             CipherAlg = CIPHER_WEP128;
3720             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key4_Proc::(Key4=%s and type=%s)\n", arg, "Ascii"));
3721             break;
3722         case 26: //wep 104 Hex type
3723             for(i=0; i < KeyLen; i++)
3724             {
3725                 if( !isxdigit(*(arg+i)) )
3726                     return FALSE;  //Not Hex value;
3727             }
3728             pAdapter->SharedKey[BSS0][3].KeyLen = KeyLen / 2 ;
3729             AtoH(arg, pAdapter->SharedKey[BSS0][3].Key, KeyLen / 2);
3730             CipherAlg = CIPHER_WEP128;
3731             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key4_Proc::(Key4=%s and type=%s)\n", arg, "Hex"));
3732             break;
3733         default: //Invalid argument
3734             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key4_Proc::Invalid argument (=%s)\n", arg));
3735             return FALSE;
3736     }
3737     pAdapter->SharedKey[BSS0][3].CipherAlg = CipherAlg;
3738
3739     // Set keys (into ASIC)
3740     if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
3741         ;   // not support
3742     else    // Old WEP stuff
3743     {
3744         AsicAddSharedKeyEntry(pAdapter,
3745                               0,
3746                               3,
3747                               pAdapter->SharedKey[BSS0][3].CipherAlg,
3748                               pAdapter->SharedKey[BSS0][3].Key,
3749                               NULL,
3750                               NULL);
3751     }
3752
3753     return TRUE;
3754 }
3755
3756 /*
3757     ==========================================================================
3758     Description:
3759         Set WPA PSK key
3760     Return:
3761         TRUE if all parameters are OK, FALSE otherwise
3762     ==========================================================================
3763 */
3764 INT Set_WPAPSK_Proc(
3765     IN  PRTMP_ADAPTER   pAd,
3766     IN  PSTRING          arg)
3767 {
3768     int status;
3769
3770     if ((pAd->StaCfg.AuthMode != Ndis802_11AuthModeWPAPSK) &&
3771         (pAd->StaCfg.AuthMode != Ndis802_11AuthModeWPA2PSK) &&
3772             (pAd->StaCfg.AuthMode != Ndis802_11AuthModeWPANone)
3773                 )
3774         return TRUE;    // do nothing
3775
3776     DBGPRINT(RT_DEBUG_TRACE, ("Set_WPAPSK_Proc::(WPAPSK=%s)\n", arg));
3777
3778         status = RT_CfgSetWPAPSKKey(pAd, arg, pAd->MlmeAux.Ssid, pAd->MlmeAux.SsidLen, pAd->StaCfg.PMK);
3779         if (status == FALSE)
3780     {
3781                 DBGPRINT(RT_DEBUG_TRACE, ("Set_WPAPSK_Proc(): Set key failed!\n"));
3782         return FALSE;
3783     }
3784         NdisZeroMemory(pAd->StaCfg.WpaPassPhrase, 64);
3785     NdisMoveMemory(pAd->StaCfg.WpaPassPhrase, arg, strlen(arg));
3786     pAd->StaCfg.WpaPassPhraseLen = (UINT)strlen(arg);
3787
3788
3789
3790     if(pAd->StaCfg.BssType == BSS_ADHOC &&
3791        pAd->StaCfg.AuthMode == Ndis802_11AuthModeWPANone)
3792     {
3793         pAd->StaCfg.WpaState = SS_NOTUSE;
3794     }
3795     else
3796     {
3797         // Start STA supplicant state machine
3798         pAd->StaCfg.WpaState = SS_START;
3799     }
3800
3801     return TRUE;
3802 }
3803
3804 /*
3805     ==========================================================================
3806     Description:
3807         Set Power Saving mode
3808     Return:
3809         TRUE if all parameters are OK, FALSE otherwise
3810     ==========================================================================
3811 */
3812 INT Set_PSMode_Proc(
3813     IN  PRTMP_ADAPTER   pAdapter,
3814     IN  PSTRING          arg)
3815 {
3816     if (pAdapter->StaCfg.BssType == BSS_INFRA)
3817     {
3818         if ((strcmp(arg, "Max_PSP") == 0) ||
3819                         (strcmp(arg, "max_psp") == 0) ||
3820                         (strcmp(arg, "MAX_PSP") == 0))
3821         {
3822             // do NOT turn on PSM bit here, wait until MlmeCheckPsmChange()
3823             // to exclude certain situations.
3824             if (pAdapter->StaCfg.bWindowsACCAMEnable == FALSE)
3825                 pAdapter->StaCfg.WindowsPowerMode = Ndis802_11PowerModeMAX_PSP;
3826             pAdapter->StaCfg.WindowsBatteryPowerMode = Ndis802_11PowerModeMAX_PSP;
3827             OPSTATUS_SET_FLAG(pAdapter, fOP_STATUS_RECEIVE_DTIM);
3828             pAdapter->StaCfg.DefaultListenCount = 5;
3829
3830         }
3831         else if ((strcmp(arg, "Fast_PSP") == 0) ||
3832                                  (strcmp(arg, "fast_psp") == 0) ||
3833                  (strcmp(arg, "FAST_PSP") == 0))
3834         {
3835             // do NOT turn on PSM bit here, wait until MlmeCheckPsmChange()
3836             // to exclude certain situations.
3837             OPSTATUS_SET_FLAG(pAdapter, fOP_STATUS_RECEIVE_DTIM);
3838             if (pAdapter->StaCfg.bWindowsACCAMEnable == FALSE)
3839                 pAdapter->StaCfg.WindowsPowerMode = Ndis802_11PowerModeFast_PSP;
3840             pAdapter->StaCfg.WindowsBatteryPowerMode = Ndis802_11PowerModeFast_PSP;
3841             pAdapter->StaCfg.DefaultListenCount = 3;
3842         }
3843         else if ((strcmp(arg, "Legacy_PSP") == 0) ||
3844                  (strcmp(arg, "legacy_psp") == 0) ||
3845                  (strcmp(arg, "LEGACY_PSP") == 0))
3846         {
3847             // do NOT turn on PSM bit here, wait until MlmeCheckPsmChange()
3848             // to exclude certain situations.
3849             OPSTATUS_SET_FLAG(pAdapter, fOP_STATUS_RECEIVE_DTIM);
3850             if (pAdapter->StaCfg.bWindowsACCAMEnable == FALSE)
3851                 pAdapter->StaCfg.WindowsPowerMode = Ndis802_11PowerModeLegacy_PSP;
3852             pAdapter->StaCfg.WindowsBatteryPowerMode = Ndis802_11PowerModeLegacy_PSP;
3853             pAdapter->StaCfg.DefaultListenCount = 3;
3854         }
3855         else
3856         {
3857             //Default Ndis802_11PowerModeCAM
3858             // clear PSM bit immediately
3859             RTMP_SET_PSM_BIT(pAdapter, PWR_ACTIVE);
3860             OPSTATUS_SET_FLAG(pAdapter, fOP_STATUS_RECEIVE_DTIM);
3861             if (pAdapter->StaCfg.bWindowsACCAMEnable == FALSE)
3862                 pAdapter->StaCfg.WindowsPowerMode = Ndis802_11PowerModeCAM;
3863             pAdapter->StaCfg.WindowsBatteryPowerMode = Ndis802_11PowerModeCAM;
3864         }
3865
3866         DBGPRINT(RT_DEBUG_TRACE, ("Set_PSMode_Proc::(PSMode=%ld)\n", pAdapter->StaCfg.WindowsPowerMode));
3867     }
3868     else
3869         return FALSE;
3870
3871
3872     return TRUE;
3873 }
3874
3875 /*
3876     ==========================================================================
3877     Description:
3878         Set WpaSupport flag.
3879     Value:
3880         0: Driver ignore wpa_supplicant.
3881         1: wpa_supplicant initiates scanning and AP selection.
3882         2: driver takes care of scanning, AP selection, and IEEE 802.11 association parameters.
3883     Return:
3884         TRUE if all parameters are OK, FALSE otherwise
3885     ==========================================================================
3886 */
3887 INT Set_Wpa_Support(
3888     IN  PRTMP_ADAPTER   pAd,
3889         IN      PSTRING                 arg)
3890 {
3891
3892     if ( simple_strtol(arg, 0, 10) == 0)
3893         pAd->StaCfg.WpaSupplicantUP = WPA_SUPPLICANT_DISABLE;
3894     else if ( simple_strtol(arg, 0, 10) == 1)
3895         pAd->StaCfg.WpaSupplicantUP = WPA_SUPPLICANT_ENABLE;
3896     else if ( simple_strtol(arg, 0, 10) == 2)
3897         pAd->StaCfg.WpaSupplicantUP = WPA_SUPPLICANT_ENABLE_WITH_WEB_UI;
3898     else
3899         pAd->StaCfg.WpaSupplicantUP = WPA_SUPPLICANT_DISABLE;
3900
3901     DBGPRINT(RT_DEBUG_TRACE, ("Set_Wpa_Support::(WpaSupplicantUP=%d)\n", pAd->StaCfg.WpaSupplicantUP));
3902
3903     return TRUE;
3904 }
3905
3906 INT Set_TGnWifiTest_Proc(
3907     IN  PRTMP_ADAPTER   pAd,
3908     IN  PSTRING          arg)
3909 {
3910     if (simple_strtol(arg, 0, 10) == 0)
3911         pAd->StaCfg.bTGnWifiTest = FALSE;
3912     else
3913         pAd->StaCfg.bTGnWifiTest = TRUE;
3914
3915     DBGPRINT(RT_DEBUG_TRACE, ("IF Set_TGnWifiTest_Proc::(bTGnWifiTest=%d)\n", pAd->StaCfg.bTGnWifiTest));
3916         return TRUE;
3917 }
3918
3919
3920
3921
3922 INT     Show_Adhoc_MacTable_Proc(
3923         IN      PRTMP_ADAPTER   pAd,
3924         IN      PSTRING                 extra)
3925 {
3926         INT i;
3927
3928         sprintf(extra, "\n");
3929
3930         sprintf(extra + strlen(extra), "HT Operating Mode : %d\n", pAd->CommonCfg.AddHTInfo.AddHtInfo2.OperaionMode);
3931
3932         sprintf(extra + strlen(extra), "\n%-19s%-4s%-4s%-7s%-7s%-7s%-10s%-6s%-6s%-6s%-6s\n",
3933                         "MAC", "AID", "BSS", "RSSI0", "RSSI1", "RSSI2", "PhMd", "BW", "MCS", "SGI", "STBC");
3934
3935         for (i=1; i<MAX_LEN_OF_MAC_TABLE; i++)
3936         {
3937                 PMAC_TABLE_ENTRY pEntry = &pAd->MacTab.Content[i];
3938
3939                 if (strlen(extra) > (IW_PRIV_SIZE_MASK - 30))
3940                     break;
3941                 if ((pEntry->ValidAsCLI || pEntry->ValidAsApCli) && (pEntry->Sst == SST_ASSOC))
3942                 {
3943                         sprintf(extra + strlen(extra), "%02X:%02X:%02X:%02X:%02X:%02X  ",
3944                                 pEntry->Addr[0], pEntry->Addr[1], pEntry->Addr[2],
3945                                 pEntry->Addr[3], pEntry->Addr[4], pEntry->Addr[5]);
3946                         sprintf(extra + strlen(extra), "%-4d", (int)pEntry->Aid);
3947                         sprintf(extra + strlen(extra), "%-4d", (int)pEntry->apidx);
3948                         sprintf(extra + strlen(extra), "%-7d", pEntry->RssiSample.AvgRssi0);
3949                         sprintf(extra + strlen(extra), "%-7d", pEntry->RssiSample.AvgRssi1);
3950                         sprintf(extra + strlen(extra), "%-7d", pEntry->RssiSample.AvgRssi2);
3951                         sprintf(extra + strlen(extra), "%-10s", GetPhyMode(pEntry->HTPhyMode.field.MODE));
3952                         sprintf(extra + strlen(extra), "%-6s", GetBW(pEntry->HTPhyMode.field.BW));
3953                         sprintf(extra + strlen(extra), "%-6d", pEntry->HTPhyMode.field.MCS);
3954                         sprintf(extra + strlen(extra), "%-6d", pEntry->HTPhyMode.field.ShortGI);
3955                         sprintf(extra + strlen(extra), "%-6d", pEntry->HTPhyMode.field.STBC);
3956                         sprintf(extra + strlen(extra), "%-10d, %d, %d%%\n", pEntry->DebugFIFOCount, pEntry->DebugTxCount,
3957                                                 (pEntry->DebugTxCount) ? ((pEntry->DebugTxCount-pEntry->DebugFIFOCount)*100/pEntry->DebugTxCount) : 0);
3958                         sprintf(extra, "%s\n", extra);
3959                 }
3960         }
3961
3962         return TRUE;
3963 }
3964
3965
3966 INT Set_BeaconLostTime_Proc(
3967     IN  PRTMP_ADAPTER   pAd,
3968     IN  PSTRING         arg)
3969 {
3970         ULONG ltmp = (ULONG)simple_strtol(arg, 0, 10);
3971
3972         if ((ltmp != 0) && (ltmp <= 60))
3973                 pAd->StaCfg.BeaconLostTime = (ltmp * OS_HZ);
3974
3975     DBGPRINT(RT_DEBUG_TRACE, ("IF Set_BeaconLostTime_Proc::(BeaconLostTime=%ld)\n", pAd->StaCfg.BeaconLostTime));
3976         return TRUE;
3977 }
3978
3979 INT Set_AutoRoaming_Proc(
3980     IN  PRTMP_ADAPTER   pAd,
3981     IN  PSTRING         arg)
3982 {
3983     if (simple_strtol(arg, 0, 10) == 0)
3984         pAd->StaCfg.bAutoRoaming = FALSE;
3985     else
3986         pAd->StaCfg.bAutoRoaming = TRUE;
3987
3988     DBGPRINT(RT_DEBUG_TRACE, ("IF Set_AutoRoaming_Proc::(bAutoRoaming=%d)\n", pAd->StaCfg.bAutoRoaming));
3989         return TRUE;
3990 }
3991
3992
3993 /*
3994     ==========================================================================
3995     Description:
3996         Issue a site survey command to driver
3997         Arguments:
3998             pAdapter                    Pointer to our adapter
3999             wrq                         Pointer to the ioctl argument
4000
4001     Return Value:
4002         None
4003
4004     Note:
4005         Usage:
4006                1.) iwpriv ra0 set site_survey
4007     ==========================================================================
4008 */
4009 INT Set_SiteSurvey_Proc(
4010         IN      PRTMP_ADAPTER   pAd,
4011         IN      PSTRING                 arg)
4012 {
4013         NDIS_802_11_SSID Ssid;
4014
4015         //check if the interface is down
4016         if (!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_INTERRUPT_IN_USE))
4017         {
4018                 DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
4019                 return -ENETDOWN;
4020         }
4021
4022         if (MONITOR_ON(pAd))
4023     {
4024         DBGPRINT(RT_DEBUG_TRACE, ("!!! Driver is in Monitor Mode now !!!\n"));
4025         return -EINVAL;
4026     }
4027
4028         RTMPZeroMemory(&Ssid, sizeof(NDIS_802_11_SSID));
4029         Ssid.SsidLength = 0;
4030         if ((arg != NULL) &&
4031                 (strlen(arg) <= MAX_LEN_OF_SSID))
4032     {
4033         RTMPMoveMemory(Ssid.Ssid, arg, strlen(arg));
4034         Ssid.SsidLength = strlen(arg);
4035         }
4036
4037         pAd->StaCfg.bScanReqIsFromWebUI = TRUE;
4038
4039         if (pAd->Mlme.CntlMachine.CurrState != CNTL_IDLE)
4040         {
4041                 RTMP_MLME_RESET_STATE_MACHINE(pAd);
4042                 DBGPRINT(RT_DEBUG_TRACE, ("!!! MLME busy, reset MLME state machine !!!\n"));
4043         }
4044
4045         // tell CNTL state machine to call NdisMSetInformationComplete() after completing
4046         // this request, because this request is initiated by NDIS.
4047         pAd->MlmeAux.CurrReqIsFromNdis = FALSE;
4048         // Reset allowed scan retries
4049         pAd->StaCfg.ScanCnt = 0;
4050         NdisGetSystemUpTime(&pAd->StaCfg.LastScanTime);
4051
4052         MlmeEnqueue(pAd,
4053                 MLME_CNTL_STATE_MACHINE,
4054                 OID_802_11_BSSID_LIST_SCAN,
4055                 Ssid.SsidLength,
4056                 Ssid.Ssid);
4057
4058         RTMP_MLME_HANDLER(pAd);
4059
4060     DBGPRINT(RT_DEBUG_TRACE, ("Set_SiteSurvey_Proc\n"));
4061
4062     return TRUE;
4063 }
4064
4065 INT Set_ForceTxBurst_Proc(
4066     IN  PRTMP_ADAPTER   pAd,
4067     IN  PSTRING         arg)
4068 {
4069     if (simple_strtol(arg, 0, 10) == 0)
4070         pAd->StaCfg.bForceTxBurst = FALSE;
4071     else
4072         pAd->StaCfg.bForceTxBurst = TRUE;
4073
4074     DBGPRINT(RT_DEBUG_TRACE, ("IF Set_ForceTxBurst_Proc::(bForceTxBurst=%d)\n", pAd->StaCfg.bForceTxBurst));
4075         return TRUE;
4076 }