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