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