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