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