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