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