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