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