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