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