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