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