Staging: rt3070: remove dead CONFIG_AP_SUPPORT code
[linux-2.6] / drivers / staging / rt3070 / sta_ioctl.c
1 /*
2  *************************************************************************
3  * Ralink Tech Inc.
4  * 5F., No.36, Taiyuan St., Jhubei City,
5  * Hsinchu County 302,
6  * Taiwan, R.O.C.
7  *
8  * (c) Copyright 2002-2007, Ralink Technology, Inc.
9  *
10  * This program is free software; you can redistribute it and/or modify  *
11  * it under the terms of the GNU General Public License as published by  *
12  * the Free Software Foundation; either version 2 of the License, or     *
13  * (at your option) any later version.                                   *
14  *                                                                       *
15  * This program is distributed in the hope that it will be useful,       *
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
18  * GNU General Public License for more details.                          *
19  *                                                                       *
20  * You should have received a copy of the GNU General Public License     *
21  * along with this program; if not, write to the                         *
22  * Free Software Foundation, Inc.,                                       *
23  * 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
24  *                                                                       *
25  *************************************************************************
26
27     Module Name:
28     sta_ioctl.c
29
30     Abstract:
31     IOCTL related subroutines
32
33     Revision History:
34     Who         When          What
35     --------    ----------    ----------------------------------------------
36     Rory Chen   01-03-2003    created
37         Rory Chen   02-14-2005    modify to support RT61
38 */
39
40 #include        "rt_config.h"
41
42 #ifdef DBG
43 extern ULONG    RTDebugLevel;
44 #endif
45
46 #define NR_WEP_KEYS                             4
47 #define WEP_SMALL_KEY_LEN                       (40/8)
48 #define WEP_LARGE_KEY_LEN                       (104/8)
49
50 #define GROUP_KEY_NO                4
51
52 extern UCHAR    CipherWpa2Template[];
53 extern UCHAR    CipherWpaPskTkip[];
54 extern UCHAR    CipherWpaPskTkipLen;
55
56 typedef struct PACKED _RT_VERSION_INFO{
57     UCHAR       DriverVersionW;
58     UCHAR       DriverVersionX;
59     UCHAR       DriverVersionY;
60     UCHAR       DriverVersionZ;
61     UINT        DriverBuildYear;
62     UINT        DriverBuildMonth;
63     UINT        DriverBuildDay;
64 } RT_VERSION_INFO, *PRT_VERSION_INFO;
65
66 struct iw_priv_args privtab[] = {
67 { RTPRIV_IOCTL_SET,
68   IW_PRIV_TYPE_CHAR | 1024, 0,
69   "set"},
70
71 { RTPRIV_IOCTL_SHOW, 0, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK,
72   ""},
73 { RTPRIV_IOCTL_SHOW, IW_PRIV_TYPE_CHAR | 1024, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK,
74   ""},
75 /* --- sub-ioctls definitions --- */
76     { SHOW_CONN_STATUS,
77           0, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "connStatus" },
78         { SHOW_DRVIER_VERION,
79           0, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "driverVer" },
80     { SHOW_BA_INFO,
81           0, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "bainfo" },
82         { SHOW_DESC_INFO,
83           0, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "descinfo" },
84     { RAIO_OFF,
85           0, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "radio_off" },
86         { RAIO_ON,
87           0, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK, "radio_on" },
88         { 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         (iw_handler) rt_ioctl_setparam, /* + 0x02 */
2956 #ifdef DBG
2957         (iw_handler) rt_private_ioctl_bbp, /* + 0x03 */
2958 #else
2959         (iw_handler) NULL, /* + 0x03 */
2960 #endif
2961         (iw_handler) NULL, /* + 0x04 */
2962         (iw_handler) NULL, /* + 0x05 */
2963         (iw_handler) NULL, /* + 0x06 */
2964         (iw_handler) NULL, /* + 0x07 */
2965         (iw_handler) NULL, /* + 0x08 */
2966         (iw_handler) rt_private_get_statistics, /* + 0x09 */
2967         (iw_handler) NULL, /* + 0x0A */
2968         (iw_handler) NULL, /* + 0x0B */
2969         (iw_handler) NULL, /* + 0x0C */
2970         (iw_handler) NULL, /* + 0x0D */
2971         (iw_handler) NULL, /* + 0x0E */
2972         (iw_handler) NULL, /* + 0x0F */
2973         (iw_handler) NULL, /* + 0x10 */
2974         (iw_handler) rt_private_show, /* + 0x11 */
2975     (iw_handler) NULL, /* + 0x12 */
2976         (iw_handler) NULL, /* + 0x13 */
2977         (iw_handler) NULL, /* + 0x15 */
2978         (iw_handler) NULL, /* + 0x17 */
2979         (iw_handler) NULL, /* + 0x18 */
2980 };
2981
2982 const struct iw_handler_def rt28xx_iw_handler_def =
2983 {
2984 #define N(a)    (sizeof (a) / sizeof (a[0]))
2985         .standard       = (iw_handler *) rt_handler,
2986         .num_standard   = sizeof(rt_handler) / sizeof(iw_handler),
2987         .private        = (iw_handler *) rt_priv_handlers,
2988         .num_private            = N(rt_priv_handlers),
2989         .private_args   = (struct iw_priv_args *) privtab,
2990         .num_private_args       = N(privtab),
2991 #if IW_HANDLER_VERSION >= 7
2992     .get_wireless_stats = rt28xx_get_wireless_stats,
2993 #endif
2994 };
2995
2996 INT RTMPSetInformation(
2997     IN  PRTMP_ADAPTER pAdapter,
2998     IN  OUT struct ifreq    *rq,
2999     IN  INT                 cmd)
3000 {
3001     struct iwreq                        *wrq = (struct iwreq *) rq;
3002     NDIS_802_11_SSID                    Ssid;
3003     NDIS_802_11_MAC_ADDRESS             Bssid;
3004     RT_802_11_PHY_MODE                  PhyMode;
3005     RT_802_11_STA_CONFIG                StaConfig;
3006     NDIS_802_11_RATES                   aryRates;
3007     RT_802_11_PREAMBLE                  Preamble;
3008     NDIS_802_11_WEP_STATUS              WepStatus;
3009     NDIS_802_11_AUTHENTICATION_MODE     AuthMode = Ndis802_11AuthModeMax;
3010     NDIS_802_11_NETWORK_INFRASTRUCTURE  BssType;
3011     NDIS_802_11_RTS_THRESHOLD           RtsThresh;
3012     NDIS_802_11_FRAGMENTATION_THRESHOLD FragThresh;
3013     NDIS_802_11_POWER_MODE              PowerMode;
3014     PNDIS_802_11_KEY                    pKey = NULL;
3015     PNDIS_802_11_WEP                            pWepKey =NULL;
3016     PNDIS_802_11_REMOVE_KEY             pRemoveKey = NULL;
3017     NDIS_802_11_CONFIGURATION           Config, *pConfig = NULL;
3018     NDIS_802_11_NETWORK_TYPE            NetType;
3019     ULONG                               Now;
3020     UINT                                KeyIdx = 0;
3021     INT                                 Status = NDIS_STATUS_SUCCESS, MaxPhyMode = PHY_11G;
3022     ULONG                               PowerTemp;
3023     BOOLEAN                             RadioState;
3024     BOOLEAN                             StateMachineTouched = FALSE;
3025         OID_SET_HT_PHYMODE                                      HT_PhyMode;     //11n ,kathy
3026     PNDIS_802_11_PMKID                  pPmkId = NULL;
3027     BOOLEAN                                             IEEE8021xState = FALSE;
3028     BOOLEAN                                             IEEE8021x_required_keys = FALSE;
3029     UCHAR                               wpa_supplicant_enable = 0;
3030
3031         MaxPhyMode = PHY_11N_5G;
3032
3033         DBGPRINT(RT_DEBUG_TRACE, ("-->RTMPSetInformation(),     0x%08x\n", cmd&0x7FFF));
3034     switch(cmd & 0x7FFF) {
3035         case RT_OID_802_11_COUNTRY_REGION:
3036             if (wrq->u.data.length < sizeof(UCHAR))
3037                 Status = -EINVAL;
3038                         // Only avaliable when EEPROM not programming
3039             else if (!(pAdapter->CommonCfg.CountryRegion & 0x80) && !(pAdapter->CommonCfg.CountryRegionForABand & 0x80))
3040             {
3041                 ULONG   Country;
3042                 UCHAR   TmpPhy;
3043
3044                                 Status = copy_from_user(&Country, wrq->u.data.pointer, wrq->u.data.length);
3045                                 pAdapter->CommonCfg.CountryRegion = (UCHAR)(Country & 0x000000FF);
3046                                 pAdapter->CommonCfg.CountryRegionForABand = (UCHAR)((Country >> 8) & 0x000000FF);
3047                 TmpPhy = pAdapter->CommonCfg.PhyMode;
3048                                 pAdapter->CommonCfg.PhyMode = 0xff;
3049                                 // Build all corresponding channel information
3050                                 RTMPSetPhyMode(pAdapter, TmpPhy);
3051                                 SetCommonHT(pAdapter);
3052                                 DBGPRINT(RT_DEBUG_TRACE, ("Set::RT_OID_802_11_COUNTRY_REGION (A:%d  B/G:%d)\n", pAdapter->CommonCfg.CountryRegionForABand,
3053                                     pAdapter->CommonCfg.CountryRegion));
3054             }
3055             break;
3056         case OID_802_11_BSSID_LIST_SCAN:
3057             Now = jiffies;
3058                         DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_BSSID_LIST_SCAN, TxCnt = %d \n", pAdapter->RalinkCounters.LastOneSecTotalTxCount));
3059
3060             if (MONITOR_ON(pAdapter))
3061             {
3062                 DBGPRINT(RT_DEBUG_TRACE, ("!!! Driver is in Monitor Mode now !!!\n"));
3063                 break;
3064             }
3065
3066                         //Benson add 20080527, when radio off, sta don't need to scan
3067                         if (RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_RADIO_OFF))
3068                                 break;
3069
3070                         if (RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_BSS_SCAN_IN_PROGRESS))
3071                         {
3072                 DBGPRINT(RT_DEBUG_TRACE, ("!!! Driver is scanning now !!!\n"));
3073                                 pAdapter->StaCfg.bScanReqIsFromWebUI = TRUE;
3074                                 Status = NDIS_STATUS_SUCCESS;
3075                 break;
3076             }
3077
3078                         if (pAdapter->RalinkCounters.LastOneSecTotalTxCount > 100)
3079             {
3080                 DBGPRINT(RT_DEBUG_TRACE, ("!!! Link UP, ignore this set::OID_802_11_BSSID_LIST_SCAN\n"));
3081                                 Status = NDIS_STATUS_SUCCESS;
3082                                 pAdapter->StaCfg.ScanCnt = 99;          // Prevent auto scan triggered by this OID
3083                                 break;
3084             }
3085
3086             if ((OPSTATUS_TEST_FLAG(pAdapter, fOP_STATUS_MEDIA_STATE_CONNECTED)) &&
3087                                 ((pAdapter->StaCfg.AuthMode == Ndis802_11AuthModeWPA) ||
3088                                 (pAdapter->StaCfg.AuthMode == Ndis802_11AuthModeWPAPSK) ||
3089                                 (pAdapter->StaCfg.AuthMode == Ndis802_11AuthModeWPA2) ||
3090                                 (pAdapter->StaCfg.AuthMode == Ndis802_11AuthModeWPA2PSK)) &&
3091                 (pAdapter->StaCfg.PortSecured == WPA_802_1X_PORT_NOT_SECURED))
3092             {
3093                 DBGPRINT(RT_DEBUG_TRACE, ("!!! Link UP, Port Not Secured! ignore this set::OID_802_11_BSSID_LIST_SCAN\n"));
3094                                 Status = NDIS_STATUS_SUCCESS;
3095                                 pAdapter->StaCfg.ScanCnt = 99;          // Prevent auto scan triggered by this OID
3096                                 break;
3097             }
3098
3099
3100             if (pAdapter->Mlme.CntlMachine.CurrState != CNTL_IDLE)
3101             {
3102                 RT28XX_MLME_RESET_STATE_MACHINE(pAdapter);
3103                 DBGPRINT(RT_DEBUG_TRACE, ("!!! MLME busy, reset MLME state machine !!!\n"));
3104             }
3105
3106             // tell CNTL state machine to call NdisMSetInformationComplete() after completing
3107             // this request, because this request is initiated by NDIS.
3108             pAdapter->MlmeAux.CurrReqIsFromNdis = FALSE;
3109             // Reset allowed scan retries
3110             pAdapter->StaCfg.ScanCnt = 0;
3111             pAdapter->StaCfg.LastScanTime = Now;
3112
3113                         pAdapter->StaCfg.bScanReqIsFromWebUI = TRUE;
3114             RTMP_SET_FLAG(pAdapter, fRTMP_ADAPTER_BSS_SCAN_IN_PROGRESS);
3115             MlmeEnqueue(pAdapter,
3116                         MLME_CNTL_STATE_MACHINE,
3117                         OID_802_11_BSSID_LIST_SCAN,
3118                         0,
3119                         NULL);
3120
3121             Status = NDIS_STATUS_SUCCESS;
3122             StateMachineTouched = TRUE;
3123             break;
3124         case OID_802_11_SSID:
3125             if (wrq->u.data.length != sizeof(NDIS_802_11_SSID))
3126                 Status = -EINVAL;
3127             else
3128             {
3129                 PCHAR pSsidString = NULL;
3130                 Status = copy_from_user(&Ssid, wrq->u.data.pointer, wrq->u.data.length);
3131
3132                                 DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_SSID (Len=%d,Ssid=%s)\n", Ssid.SsidLength, Ssid.Ssid));
3133                 if (Ssid.SsidLength > MAX_LEN_OF_SSID)
3134                     Status = -EINVAL;
3135                 else
3136                 {
3137                         if (Ssid.SsidLength == 0)
3138                         {
3139                                 Set_SSID_Proc(pAdapter, "");
3140                         }
3141                                         else
3142                         {
3143                                 pSsidString = (CHAR *) kmalloc(MAX_LEN_OF_SSID+1, MEM_ALLOC_FLAG);
3144                                                 if (pSsidString)
3145                                                 {
3146                                                         NdisZeroMemory(pSsidString, MAX_LEN_OF_SSID+1);
3147                                                         NdisMoveMemory(pSsidString, Ssid.Ssid, Ssid.SsidLength);
3148                                         Set_SSID_Proc(pAdapter, pSsidString);
3149                                                         kfree(pSsidString);
3150                                                 }
3151                                                 else
3152                                                         Status = -ENOMEM;
3153                         }
3154                 }
3155             }
3156             break;
3157         case OID_802_11_BSSID:
3158             if (wrq->u.data.length != sizeof(NDIS_802_11_MAC_ADDRESS))
3159                 Status  = -EINVAL;
3160             else
3161             {
3162                 Status = copy_from_user(&Bssid, wrq->u.data.pointer, wrq->u.data.length);
3163
3164                 // tell CNTL state machine to call NdisMSetInformationComplete() after completing
3165                 // this request, because this request is initiated by NDIS.
3166                 pAdapter->MlmeAux.CurrReqIsFromNdis = FALSE;
3167
3168                                 // Prevent to connect AP again in STAMlmePeriodicExec
3169                                 pAdapter->MlmeAux.AutoReconnectSsidLen= 32;
3170
3171                 // Reset allowed scan retries
3172                                 pAdapter->StaCfg.ScanCnt = 0;
3173
3174                 if (pAdapter->Mlme.CntlMachine.CurrState != CNTL_IDLE)
3175                 {
3176                     RT28XX_MLME_RESET_STATE_MACHINE(pAdapter);
3177                     DBGPRINT(RT_DEBUG_TRACE, ("!!! MLME busy, reset MLME state machine !!!\n"));
3178                 }
3179                 MlmeEnqueue(pAdapter,
3180                             MLME_CNTL_STATE_MACHINE,
3181                             OID_802_11_BSSID,
3182                             sizeof(NDIS_802_11_MAC_ADDRESS),
3183                             (VOID *)&Bssid);
3184                 Status = NDIS_STATUS_SUCCESS;
3185                 StateMachineTouched = TRUE;
3186
3187                 DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_BSSID %02x:%02x:%02x:%02x:%02x:%02x\n",
3188                                         Bssid[0], Bssid[1], Bssid[2], Bssid[3], Bssid[4], Bssid[5]));
3189             }
3190             break;
3191         case RT_OID_802_11_RADIO:
3192             if (wrq->u.data.length != sizeof(BOOLEAN))
3193                 Status  = -EINVAL;
3194             else
3195             {
3196                 Status = copy_from_user(&RadioState, wrq->u.data.pointer, wrq->u.data.length);
3197                 DBGPRINT(RT_DEBUG_TRACE, ("Set::RT_OID_802_11_RADIO (=%d)\n", RadioState));
3198                 if (pAdapter->StaCfg.bSwRadio != RadioState)
3199                 {
3200                     pAdapter->StaCfg.bSwRadio = RadioState;
3201                     if (pAdapter->StaCfg.bRadio != (pAdapter->StaCfg.bHwRadio && pAdapter->StaCfg.bSwRadio))
3202                     {
3203                         pAdapter->StaCfg.bRadio = (pAdapter->StaCfg.bHwRadio && pAdapter->StaCfg.bSwRadio);
3204                         if (pAdapter->StaCfg.bRadio == TRUE)
3205                         {
3206                             MlmeRadioOn(pAdapter);
3207                             // Update extra information
3208                                                         pAdapter->ExtraInfo = EXTRA_INFO_CLEAR;
3209                         }
3210                         else
3211                         {
3212                             MlmeRadioOff(pAdapter);
3213                             // Update extra information
3214                                                         pAdapter->ExtraInfo = SW_RADIO_OFF;
3215                         }
3216                     }
3217                 }
3218             }
3219             break;
3220         case RT_OID_802_11_PHY_MODE:
3221             if (wrq->u.data.length != sizeof(RT_802_11_PHY_MODE))
3222                 Status  = -EINVAL;
3223             else
3224             {
3225                 Status = copy_from_user(&PhyMode, wrq->u.data.pointer, wrq->u.data.length);
3226                                 if (PhyMode <= MaxPhyMode)
3227                                 {
3228                         RTMPSetPhyMode(pAdapter, PhyMode);
3229                                         SetCommonHT(pAdapter);
3230                                 }
3231                 DBGPRINT(RT_DEBUG_TRACE, ("Set::RT_OID_802_11_PHY_MODE (=%d)\n", PhyMode));
3232             }
3233             break;
3234         case RT_OID_802_11_STA_CONFIG:
3235             if (wrq->u.data.length != sizeof(RT_802_11_STA_CONFIG))
3236                 Status  = -EINVAL;
3237             else
3238             {
3239                 Status = copy_from_user(&StaConfig, wrq->u.data.pointer, wrq->u.data.length);
3240                 pAdapter->CommonCfg.bEnableTxBurst = StaConfig.EnableTxBurst;
3241                 pAdapter->CommonCfg.UseBGProtection = StaConfig.UseBGProtection;
3242                 pAdapter->CommonCfg.bUseShortSlotTime = 1; // 2003-10-30 always SHORT SLOT capable
3243                 if ((pAdapter->CommonCfg.PhyMode != StaConfig.AdhocMode) &&
3244                                         (StaConfig.AdhocMode <= MaxPhyMode))
3245                 {
3246                     // allow dynamic change of "USE OFDM rate or not" in ADHOC mode
3247                     // if setting changed, need to reset current TX rate as well as BEACON frame format
3248                     pAdapter->CommonCfg.PhyMode = StaConfig.AdhocMode;
3249                     if (pAdapter->StaCfg.BssType == BSS_ADHOC)
3250                     {
3251                         RTMPSetPhyMode(pAdapter, PhyMode);
3252                         MlmeUpdateTxRates(pAdapter, FALSE, 0);
3253                         MakeIbssBeacon(pAdapter);           // re-build BEACON frame
3254                         AsicEnableIbssSync(pAdapter);   // copy to on-chip memory
3255                     }
3256                 }
3257                 DBGPRINT(RT_DEBUG_TRACE, ("Set::RT_OID_802_11_SET_STA_CONFIG (Burst=%d, Protection=%ld,ShortSlot=%d\n",
3258                                         pAdapter->CommonCfg.bEnableTxBurst,
3259                                         pAdapter->CommonCfg.UseBGProtection,
3260                                         pAdapter->CommonCfg.bUseShortSlotTime));
3261             }
3262             break;
3263         case OID_802_11_DESIRED_RATES:
3264             if (wrq->u.data.length != sizeof(NDIS_802_11_RATES))
3265                 Status  = -EINVAL;
3266             else
3267             {
3268                 Status = copy_from_user(&aryRates, wrq->u.data.pointer, wrq->u.data.length);
3269                 NdisZeroMemory(pAdapter->CommonCfg.DesireRate, MAX_LEN_OF_SUPPORTED_RATES);
3270                 NdisMoveMemory(pAdapter->CommonCfg.DesireRate, &aryRates, sizeof(NDIS_802_11_RATES));
3271                 DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_DESIRED_RATES (%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x)\n",
3272                     pAdapter->CommonCfg.DesireRate[0],pAdapter->CommonCfg.DesireRate[1],
3273                     pAdapter->CommonCfg.DesireRate[2],pAdapter->CommonCfg.DesireRate[3],
3274                     pAdapter->CommonCfg.DesireRate[4],pAdapter->CommonCfg.DesireRate[5],
3275                     pAdapter->CommonCfg.DesireRate[6],pAdapter->CommonCfg.DesireRate[7] ));
3276                 // Changing DesiredRate may affect the MAX TX rate we used to TX frames out
3277                 MlmeUpdateTxRates(pAdapter, FALSE, 0);
3278             }
3279             break;
3280         case RT_OID_802_11_PREAMBLE:
3281             if (wrq->u.data.length != sizeof(RT_802_11_PREAMBLE))
3282                 Status  = -EINVAL;
3283             else
3284             {
3285                 Status = copy_from_user(&Preamble, wrq->u.data.pointer, wrq->u.data.length);
3286                 if (Preamble == Rt802_11PreambleShort)
3287                 {
3288                     pAdapter->CommonCfg.TxPreamble = Preamble;
3289                     MlmeSetTxPreamble(pAdapter, Rt802_11PreambleShort);
3290                 }
3291                 else if ((Preamble == Rt802_11PreambleLong) || (Preamble == Rt802_11PreambleAuto))
3292                 {
3293                     // if user wants AUTO, initialize to LONG here, then change according to AP's
3294                     // capability upon association.
3295                     pAdapter->CommonCfg.TxPreamble = Preamble;
3296                     MlmeSetTxPreamble(pAdapter, Rt802_11PreambleLong);
3297                 }
3298                 else
3299                 {
3300                     Status = -EINVAL;
3301                     break;
3302                 }
3303                 DBGPRINT(RT_DEBUG_TRACE, ("Set::RT_OID_802_11_PREAMBLE (=%d)\n", Preamble));
3304             }
3305             break;
3306         case OID_802_11_WEP_STATUS:
3307             if (wrq->u.data.length != sizeof(NDIS_802_11_WEP_STATUS))
3308                 Status  = -EINVAL;
3309             else
3310             {
3311                 Status = copy_from_user(&WepStatus, wrq->u.data.pointer, wrq->u.data.length);
3312                 // Since TKIP, AES, WEP are all supported. It should not have any invalid setting
3313                 if (WepStatus <= Ndis802_11Encryption3KeyAbsent)
3314                 {
3315                     if (pAdapter->StaCfg.WepStatus != WepStatus)
3316                     {
3317                         // Config has changed
3318                         pAdapter->bConfigChanged = TRUE;
3319                     }
3320                     pAdapter->StaCfg.WepStatus     = WepStatus;
3321                     pAdapter->StaCfg.OrigWepStatus = WepStatus;
3322                     pAdapter->StaCfg.PairCipher    = WepStatus;
3323                         pAdapter->StaCfg.GroupCipher   = WepStatus;
3324                 }
3325                 else
3326                 {
3327                     Status  = -EINVAL;
3328                     break;
3329                 }
3330                 DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_WEP_STATUS (=%d)\n",WepStatus));
3331             }
3332             break;
3333         case OID_802_11_AUTHENTICATION_MODE:
3334             if (wrq->u.data.length != sizeof(NDIS_802_11_AUTHENTICATION_MODE))
3335                 Status  = -EINVAL;
3336             else
3337             {
3338                 Status = copy_from_user(&AuthMode, wrq->u.data.pointer, wrq->u.data.length);
3339                 if (AuthMode > Ndis802_11AuthModeMax)
3340                 {
3341                     Status  = -EINVAL;
3342                     break;
3343                 }
3344                 else
3345                 {
3346                     if (pAdapter->StaCfg.AuthMode != AuthMode)
3347                     {
3348                         // Config has changed
3349                         pAdapter->bConfigChanged = TRUE;
3350                     }
3351                     pAdapter->StaCfg.AuthMode = AuthMode;
3352                 }
3353                 pAdapter->StaCfg.PortSecured = WPA_802_1X_PORT_NOT_SECURED;
3354                 DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_AUTHENTICATION_MODE (=%d) \n",pAdapter->StaCfg.AuthMode));
3355             }
3356             break;
3357         case OID_802_11_INFRASTRUCTURE_MODE:
3358             if (wrq->u.data.length != sizeof(NDIS_802_11_NETWORK_INFRASTRUCTURE))
3359                 Status  = -EINVAL;
3360             else
3361             {
3362                 Status = copy_from_user(&BssType, wrq->u.data.pointer, wrq->u.data.length);
3363
3364                                 if (BssType == Ndis802_11IBSS)
3365                                         Set_NetworkType_Proc(pAdapter, "Adhoc");
3366                                 else if (BssType == Ndis802_11Infrastructure)
3367                                         Set_NetworkType_Proc(pAdapter, "Infra");
3368                                 else if (BssType == Ndis802_11Monitor)
3369                                         Set_NetworkType_Proc(pAdapter, "Monitor");
3370                                 else
3371                                 {
3372                                         Status  = -EINVAL;
3373                                         DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_INFRASTRUCTURE_MODE (unknown)\n"));
3374                                 }
3375                         }
3376                         break;
3377          case OID_802_11_REMOVE_WEP:
3378             DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_REMOVE_WEP\n"));
3379             if (wrq->u.data.length != sizeof(NDIS_802_11_KEY_INDEX))
3380             {
3381                                 Status = -EINVAL;
3382             }
3383             else
3384             {
3385                                 KeyIdx = *(NDIS_802_11_KEY_INDEX *) wrq->u.data.pointer;
3386
3387                                 if (KeyIdx & 0x80000000)
3388                                 {
3389                                         // Should never set default bit when remove key
3390                                         Status = -EINVAL;
3391                                 }
3392                                 else
3393                                 {
3394                                         KeyIdx = KeyIdx & 0x0fffffff;
3395                                         if (KeyIdx >= 4){
3396                                                 Status = -EINVAL;
3397                                         }
3398                                         else
3399                                         {
3400                                                 pAdapter->SharedKey[BSS0][KeyIdx].KeyLen = 0;
3401                                                 pAdapter->SharedKey[BSS0][KeyIdx].CipherAlg = CIPHER_NONE;
3402                                                 AsicRemoveSharedKeyEntry(pAdapter, 0, (UCHAR)KeyIdx);
3403                                         }
3404                                 }
3405             }
3406             break;
3407         case RT_OID_802_11_RESET_COUNTERS:
3408             NdisZeroMemory(&pAdapter->WlanCounters, sizeof(COUNTER_802_11));
3409             NdisZeroMemory(&pAdapter->Counters8023, sizeof(COUNTER_802_3));
3410             NdisZeroMemory(&pAdapter->RalinkCounters, sizeof(COUNTER_RALINK));
3411             pAdapter->Counters8023.RxNoBuffer   = 0;
3412                         pAdapter->Counters8023.GoodReceives = 0;
3413                         pAdapter->Counters8023.RxNoBuffer   = 0;
3414 #ifdef RT2870
3415                         pAdapter->BulkOutComplete       = 0;
3416                         pAdapter->BulkOutCompleteOther= 0;
3417                         pAdapter->BulkOutCompleteCancel = 0;
3418                         pAdapter->BulkOutReq = 0;
3419                         pAdapter->BulkInReq= 0;
3420                         pAdapter->BulkInComplete = 0;
3421                         pAdapter->BulkInCompleteFail = 0;
3422 #endif // RT2870 //
3423             DBGPRINT(RT_DEBUG_TRACE, ("Set::RT_OID_802_11_RESET_COUNTERS \n"));
3424             break;
3425         case OID_802_11_RTS_THRESHOLD:
3426             if (wrq->u.data.length != sizeof(NDIS_802_11_RTS_THRESHOLD))
3427                 Status  = -EINVAL;
3428             else
3429             {
3430                 Status = copy_from_user(&RtsThresh, wrq->u.data.pointer, wrq->u.data.length);
3431                 if (RtsThresh > MAX_RTS_THRESHOLD)
3432                     Status  = -EINVAL;
3433                 else
3434                     pAdapter->CommonCfg.RtsThreshold = (USHORT)RtsThresh;
3435             }
3436             DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_RTS_THRESHOLD (=%ld)\n",RtsThresh));
3437             break;
3438         case OID_802_11_FRAGMENTATION_THRESHOLD:
3439             if (wrq->u.data.length != sizeof(NDIS_802_11_FRAGMENTATION_THRESHOLD))
3440                 Status  = -EINVAL;
3441             else
3442             {
3443                 Status = copy_from_user(&FragThresh, wrq->u.data.pointer, wrq->u.data.length);
3444                 pAdapter->CommonCfg.bUseZeroToDisableFragment = FALSE;
3445                 if (FragThresh > MAX_FRAG_THRESHOLD || FragThresh < MIN_FRAG_THRESHOLD)
3446                 {
3447                     if (FragThresh == 0)
3448                     {
3449                         pAdapter->CommonCfg.FragmentThreshold = MAX_FRAG_THRESHOLD;
3450                         pAdapter->CommonCfg.bUseZeroToDisableFragment = TRUE;
3451                     }
3452                     else
3453                         Status  = -EINVAL;
3454                 }
3455                 else
3456                     pAdapter->CommonCfg.FragmentThreshold = (USHORT)FragThresh;
3457             }
3458             DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_FRAGMENTATION_THRESHOLD (=%ld) \n",FragThresh));
3459             break;
3460         case OID_802_11_POWER_MODE:
3461             if (wrq->u.data.length != sizeof(NDIS_802_11_POWER_MODE))
3462                 Status = -EINVAL;
3463             else
3464             {
3465                 Status = copy_from_user(&PowerMode, wrq->u.data.pointer, wrq->u.data.length);
3466                 if (PowerMode == Ndis802_11PowerModeCAM)
3467                         Set_PSMode_Proc(pAdapter, "CAM");
3468                 else if (PowerMode == Ndis802_11PowerModeMAX_PSP)
3469                         Set_PSMode_Proc(pAdapter, "Max_PSP");
3470                 else if (PowerMode == Ndis802_11PowerModeFast_PSP)
3471                                         Set_PSMode_Proc(pAdapter, "Fast_PSP");
3472                 else if (PowerMode == Ndis802_11PowerModeLegacy_PSP)
3473                                         Set_PSMode_Proc(pAdapter, "Legacy_PSP");
3474                 else
3475                     Status = -EINVAL;
3476             }
3477             DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_POWER_MODE (=%d)\n",PowerMode));
3478             break;
3479          case RT_OID_802_11_TX_POWER_LEVEL_1:
3480                         if (wrq->u.data.length  < sizeof(ULONG))
3481                                 Status = -EINVAL;
3482                         else
3483                         {
3484                                 Status = copy_from_user(&PowerTemp, wrq->u.data.pointer, wrq->u.data.length);
3485                                 if (PowerTemp > 100)
3486                                         PowerTemp = 0xffffffff;  // AUTO
3487                                 pAdapter->CommonCfg.TxPowerDefault = PowerTemp; //keep current setting.
3488                                         pAdapter->CommonCfg.TxPowerPercentage = pAdapter->CommonCfg.TxPowerDefault;
3489                 DBGPRINT(RT_DEBUG_TRACE, ("Set::RT_OID_802_11_TX_POWER_LEVEL_1 (=%ld)\n", pAdapter->CommonCfg.TxPowerPercentage));
3490                         }
3491                 break;
3492                 case OID_802_11_NETWORK_TYPE_IN_USE:
3493                         if (wrq->u.data.length != sizeof(NDIS_802_11_NETWORK_TYPE))
3494                                 Status = -EINVAL;
3495                         else
3496                         {
3497                                 Status = copy_from_user(&NetType, wrq->u.data.pointer, wrq->u.data.length);
3498
3499                                 if (NetType == Ndis802_11DS)
3500                                         RTMPSetPhyMode(pAdapter, PHY_11B);
3501                                 else if (NetType == Ndis802_11OFDM24)
3502                                         RTMPSetPhyMode(pAdapter, PHY_11BG_MIXED);
3503                                 else if (NetType == Ndis802_11OFDM5)
3504                                         RTMPSetPhyMode(pAdapter, PHY_11A);
3505                                 else
3506                                         Status = -EINVAL;
3507
3508                                 if (Status == NDIS_STATUS_SUCCESS)
3509                                         SetCommonHT(pAdapter);
3510
3511                 DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_NETWORK_TYPE_IN_USE (=%d)\n",NetType));
3512                     }
3513                         break;
3514         // For WPA PSK PMK key
3515         case RT_OID_802_11_ADD_WPA:
3516             pKey = kmalloc(wrq->u.data.length, MEM_ALLOC_FLAG);
3517             if(pKey == NULL)
3518             {
3519                 Status = -ENOMEM;
3520                 break;
3521             }
3522
3523             Status = copy_from_user(pKey, wrq->u.data.pointer, wrq->u.data.length);
3524             if (pKey->Length != wrq->u.data.length)
3525             {
3526                 Status  = -EINVAL;
3527                 DBGPRINT(RT_DEBUG_TRACE, ("Set::RT_OID_802_11_ADD_WPA, Failed!!\n"));
3528             }
3529             else
3530             {
3531                 if ((pAdapter->StaCfg.AuthMode != Ndis802_11AuthModeWPAPSK) &&
3532                                     (pAdapter->StaCfg.AuthMode != Ndis802_11AuthModeWPA2PSK) &&
3533                                     (pAdapter->StaCfg.AuthMode != Ndis802_11AuthModeWPANone) )
3534                 {
3535                     Status = -EOPNOTSUPP;
3536                     DBGPRINT(RT_DEBUG_TRACE, ("Set::RT_OID_802_11_ADD_WPA, Failed!! [AuthMode != WPAPSK/WPA2PSK/WPANONE]\n"));
3537                 }
3538                 else if ((pAdapter->StaCfg.AuthMode == Ndis802_11AuthModeWPAPSK) ||
3539                                                  (pAdapter->StaCfg.AuthMode == Ndis802_11AuthModeWPA2PSK) ||
3540                                                  (pAdapter->StaCfg.AuthMode == Ndis802_11AuthModeWPANone) )     // Only for WPA PSK mode
3541                                 {
3542                     NdisMoveMemory(pAdapter->StaCfg.PMK, &pKey->KeyMaterial, pKey->KeyLength);
3543                     // Use RaConfig as PSK agent.
3544                     // Start STA supplicant state machine
3545                     if (pAdapter->StaCfg.AuthMode != Ndis802_11AuthModeWPANone)
3546                         pAdapter->StaCfg.WpaState = SS_START;
3547
3548                     DBGPRINT(RT_DEBUG_TRACE, ("Set::RT_OID_802_11_ADD_WPA (id=0x%x, Len=%d-byte)\n", pKey->KeyIndex, pKey->KeyLength));
3549                 }
3550                 else
3551                 {
3552                     pAdapter->StaCfg.WpaState = SS_NOTUSE;
3553                     DBGPRINT(RT_DEBUG_TRACE, ("Set::RT_OID_802_11_ADD_WPA (id=0x%x, Len=%d-byte)\n", pKey->KeyIndex, pKey->KeyLength));
3554                 }
3555             }
3556             kfree(pKey);
3557             break;
3558         case OID_802_11_REMOVE_KEY:
3559             pRemoveKey = kmalloc(wrq->u.data.length, MEM_ALLOC_FLAG);
3560             if(pRemoveKey == NULL)
3561             {
3562                 Status = -ENOMEM;
3563                 break;
3564             }
3565
3566             Status = copy_from_user(pRemoveKey, wrq->u.data.pointer, wrq->u.data.length);
3567             if (pRemoveKey->Length != wrq->u.data.length)
3568             {
3569                 Status  = -EINVAL;
3570                 DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_REMOVE_KEY, Failed!!\n"));
3571             }
3572             else
3573             {
3574                 if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
3575                 {
3576                     RTMPWPARemoveKeyProc(pAdapter, pRemoveKey);
3577                     DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_REMOVE_KEY, Remove WPA Key!!\n"));
3578                 }
3579                 else
3580                 {
3581                     KeyIdx = pRemoveKey->KeyIndex;
3582
3583                     if (KeyIdx & 0x80000000)
3584                     {
3585                         // Should never set default bit when remove key
3586                         Status  = -EINVAL;
3587                         DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_REMOVE_KEY, Failed!!(Should never set default bit when remove key)\n"));
3588                     }
3589                     else
3590                     {
3591                         KeyIdx = KeyIdx & 0x0fffffff;
3592                         if (KeyIdx > 3)
3593                         {
3594                             Status  = -EINVAL;
3595                             DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_REMOVE_KEY, Failed!!(KeyId[%d] out of range)\n", KeyIdx));
3596                         }
3597                         else
3598                         {
3599                             pAdapter->SharedKey[BSS0][KeyIdx].KeyLen = 0;
3600                             pAdapter->SharedKey[BSS0][KeyIdx].CipherAlg = CIPHER_NONE;
3601                             AsicRemoveSharedKeyEntry(pAdapter, 0, (UCHAR)KeyIdx);
3602                             DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_REMOVE_KEY (id=0x%x, Len=%d-byte)\n", pRemoveKey->KeyIndex, pRemoveKey->Length));
3603                         }
3604                     }
3605                 }
3606             }
3607             kfree(pRemoveKey);
3608             break;
3609         // New for WPA
3610         case OID_802_11_ADD_KEY:
3611             pKey = kmalloc(wrq->u.data.length, MEM_ALLOC_FLAG);
3612             if(pKey == NULL)
3613             {
3614                 Status = -ENOMEM;
3615                 break;
3616             }
3617             Status = copy_from_user(pKey, wrq->u.data.pointer, wrq->u.data.length);
3618             if (pKey->Length != wrq->u.data.length)
3619             {
3620                 Status  = -EINVAL;
3621                 DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_ADD_KEY, Failed!!\n"));
3622             }
3623             else
3624             {
3625                 RTMPAddKey(pAdapter, pKey);
3626                 DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_ADD_KEY (id=0x%x, Len=%d-byte)\n", pKey->KeyIndex, pKey->KeyLength));
3627             }
3628             kfree(pKey);
3629             break;
3630         case OID_802_11_CONFIGURATION:
3631             if (wrq->u.data.length != sizeof(NDIS_802_11_CONFIGURATION))
3632                 Status  = -EINVAL;
3633             else
3634             {
3635                 Status = copy_from_user(&Config, wrq->u.data.pointer, wrq->u.data.length);
3636                 pConfig = &Config;
3637
3638                 if ((pConfig->BeaconPeriod >= 20) && (pConfig->BeaconPeriod <=400))
3639                      pAdapter->CommonCfg.BeaconPeriod = (USHORT) pConfig->BeaconPeriod;
3640
3641                 pAdapter->StaActive.AtimWin = (USHORT) pConfig->ATIMWindow;
3642                 MAP_KHZ_TO_CHANNEL_ID(pConfig->DSConfig, pAdapter->CommonCfg.Channel);
3643                 //
3644                                 // Save the channel on MlmeAux for CntlOidRTBssidProc used.
3645                                 //
3646                                 pAdapter->MlmeAux.Channel = pAdapter->CommonCfg.Channel;
3647
3648                 DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_CONFIGURATION (BeacnPeriod=%ld,AtimW=%ld,Ch=%d)\n",
3649                     pConfig->BeaconPeriod, pConfig->ATIMWindow, pAdapter->CommonCfg.Channel));
3650                 // Config has changed
3651                 pAdapter->bConfigChanged = TRUE;
3652             }
3653             break;
3654                 case RT_OID_802_11_SET_HT_PHYMODE:
3655                         if (wrq->u.data.length  != sizeof(OID_SET_HT_PHYMODE))
3656                                 Status = -EINVAL;
3657                         else
3658                         {
3659                             POID_SET_HT_PHYMODE pHTPhyMode = &HT_PhyMode;
3660
3661                                 Status = copy_from_user(&HT_PhyMode, wrq->u.data.pointer, wrq->u.data.length);
3662                                 DBGPRINT(RT_DEBUG_TRACE, ("Set::pHTPhyMode      (PhyMode = %d,TransmitNo = %d, HtMode = %d,     ExtOffset =     %d , MCS = %d, BW =     %d,     STBC = %d, SHORTGI = %d) \n",
3663                                 pHTPhyMode->PhyMode, pHTPhyMode->TransmitNo,pHTPhyMode->HtMode,pHTPhyMode->ExtOffset,
3664                                 pHTPhyMode->MCS, pHTPhyMode->BW, pHTPhyMode->STBC,      pHTPhyMode->SHORTGI));
3665                                 if (pAdapter->CommonCfg.PhyMode >= PHY_11ABGN_MIXED)
3666                                         RTMPSetHT(pAdapter,     pHTPhyMode);
3667                         }
3668                         DBGPRINT(RT_DEBUG_TRACE, ("Set::RT_OID_802_11_SET_HT_PHYMODE(MCS=%d,BW=%d,SGI=%d,STBC=%d)\n",
3669                                 pAdapter->StaCfg.HTPhyMode.field.MCS, pAdapter->StaCfg.HTPhyMode.field.BW, pAdapter->StaCfg.HTPhyMode.field.ShortGI,
3670                                 pAdapter->StaCfg.HTPhyMode.field.STBC));
3671                         break;
3672                 case RT_OID_802_11_SET_APSD_SETTING:
3673                         if (wrq->u.data.length != sizeof(ULONG))
3674                                 Status = -EINVAL;
3675                         else
3676                         {
3677                                 ULONG apsd ;
3678                                 Status = copy_from_user(&apsd, wrq->u.data.pointer,     wrq->u.data.length);
3679
3680                                 /*-------------------------------------------------------------------
3681                                 |B31~B7 |       B6~B5    |       B4      |       B3      |      B2       |      B1       |         B0           |
3682                                 ---------------------------------------------------------------------
3683                                 | Rsvd  | Max SP Len | AC_VO | AC_VI | AC_BK | AC_BE | APSD     Capable |
3684                                 ---------------------------------------------------------------------*/
3685                                 pAdapter->CommonCfg.bAPSDCapable = (apsd & 0x00000001) ? TRUE : FALSE;
3686                                 pAdapter->CommonCfg.bAPSDAC_BE = ((apsd & 0x00000002) >> 1)     ? TRUE : FALSE;
3687                                 pAdapter->CommonCfg.bAPSDAC_BK = ((apsd & 0x00000004) >> 2)     ? TRUE : FALSE;
3688                                 pAdapter->CommonCfg.bAPSDAC_VI = ((apsd & 0x00000008) >> 3)     ? TRUE : FALSE;
3689                                 pAdapter->CommonCfg.bAPSDAC_VO = ((apsd & 0x00000010) >> 4)     ? TRUE : FALSE;
3690                                 pAdapter->CommonCfg.MaxSPLength = (UCHAR)((apsd & 0x00000060) >> 5);
3691
3692                                 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,
3693                                         pAdapter->CommonCfg.bAPSDAC_BE, pAdapter->CommonCfg.bAPSDAC_BK, pAdapter->CommonCfg.bAPSDAC_VI, pAdapter->CommonCfg.bAPSDAC_VO, pAdapter->CommonCfg.MaxSPLength));
3694                         }
3695                         break;
3696
3697                 case RT_OID_802_11_SET_APSD_PSM:
3698                         if (wrq->u.data.length  != sizeof(ULONG))
3699                                 Status = -EINVAL;
3700                         else
3701                         {
3702                                 // Driver needs to notify AP when PSM changes
3703                                 Status = copy_from_user(&pAdapter->CommonCfg.bAPSDForcePowerSave, wrq->u.data.pointer, wrq->u.data.length);
3704                                 if (pAdapter->CommonCfg.bAPSDForcePowerSave     != pAdapter->StaCfg.Psm)
3705                                 {
3706                                         MlmeSetPsmBit(pAdapter, pAdapter->CommonCfg.bAPSDForcePowerSave);
3707                                         RTMPSendNullFrame(pAdapter,     pAdapter->CommonCfg.TxRate,     TRUE);
3708                                 }
3709                                 DBGPRINT(RT_DEBUG_TRACE, ("Set::RT_OID_802_11_SET_APSD_PSM (bAPSDForcePowerSave:%d)\n", pAdapter->CommonCfg.bAPSDForcePowerSave));
3710                         }
3711                         break;
3712
3713                 case RT_OID_802_11_SET_WMM:
3714                         if (wrq->u.data.length  != sizeof(BOOLEAN))
3715                                 Status = -EINVAL;
3716                         else
3717                         {
3718                                 Status = copy_from_user(&pAdapter->CommonCfg.bWmmCapable, wrq->u.data.pointer, wrq->u.data.length);
3719                                 DBGPRINT(RT_DEBUG_TRACE, ("Set::RT_OID_802_11_SET_WMM (=%d)     \n", pAdapter->CommonCfg.bWmmCapable));
3720                         }
3721                         break;
3722
3723                 case OID_802_11_DISASSOCIATE:
3724                         //
3725                         // Set NdisRadioStateOff to     TRUE, instead of called MlmeRadioOff.
3726                         // Later on, NDIS_802_11_BSSID_LIST_EX->NumberOfItems should be 0
3727                         // when query OID_802_11_BSSID_LIST.
3728                         //
3729                         // TRUE:  NumberOfItems will set to     0.
3730                         // FALSE: NumberOfItems no change.
3731                         //
3732                         pAdapter->CommonCfg.NdisRadioStateOff = TRUE;
3733                         // Set to immediately send the media disconnect event
3734                         pAdapter->MlmeAux.CurrReqIsFromNdis     = TRUE;
3735                         DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_DISASSOCIATE \n"));
3736
3737                         if (INFRA_ON(pAdapter))
3738                         {
3739                                 if (pAdapter->Mlme.CntlMachine.CurrState !=     CNTL_IDLE)
3740                                 {
3741                                         RT28XX_MLME_RESET_STATE_MACHINE(pAdapter);
3742                                         DBGPRINT(RT_DEBUG_TRACE, ("!!! MLME     busy, reset     MLME state machine !!!\n"));
3743                                 }
3744
3745                                 MlmeEnqueue(pAdapter,
3746                                         MLME_CNTL_STATE_MACHINE,
3747                                         OID_802_11_DISASSOCIATE,
3748                                         0,
3749                                         NULL);
3750
3751                                 StateMachineTouched     = TRUE;
3752                         }
3753                         break;
3754                 case RT_OID_802_11_SET_IMME_BA_CAP:
3755                                 if (wrq->u.data.length != sizeof(OID_BACAP_STRUC))
3756                                         Status = -EINVAL;
3757                                 else
3758                                 {
3759                                         OID_BACAP_STRUC Orde ;
3760                                         Status = copy_from_user(&Orde, wrq->u.data.pointer, wrq->u.data.length);
3761                                         if (Orde.Policy > BA_NOTUSE)
3762                                         {
3763                                                 Status = NDIS_STATUS_INVALID_DATA;
3764                                         }
3765                                         else if (Orde.Policy == BA_NOTUSE)
3766                                         {
3767                                                 pAdapter->CommonCfg.BACapability.field.Policy = BA_NOTUSE;
3768                                                 pAdapter->CommonCfg.BACapability.field.MpduDensity = Orde.MpduDensity;
3769                                                 pAdapter->CommonCfg.DesiredHtPhy.MpduDensity = Orde.MpduDensity;
3770                                                 pAdapter->CommonCfg.DesiredHtPhy.AmsduEnable = Orde.AmsduEnable;
3771                                                 pAdapter->CommonCfg.DesiredHtPhy.AmsduSize= Orde.AmsduSize;
3772                                                 pAdapter->CommonCfg.DesiredHtPhy.MimoPs= Orde.MMPSmode;
3773                                                 pAdapter->CommonCfg.BACapability.field.MMPSmode = Orde.MMPSmode;
3774                                                 // UPdata to HT IE
3775                                                 pAdapter->CommonCfg.HtCapability.HtCapInfo.MimoPs = Orde.MMPSmode;
3776                                                 pAdapter->CommonCfg.HtCapability.HtCapInfo.AMsduSize = Orde.AmsduSize;
3777                                                 pAdapter->CommonCfg.HtCapability.HtCapParm.MpduDensity = Orde.MpduDensity;
3778                                         }
3779                                         else
3780                                         {
3781                         pAdapter->CommonCfg.BACapability.field.AutoBA = Orde.AutoBA;
3782                                                 pAdapter->CommonCfg.BACapability.field.Policy = IMMED_BA; // we only support immediate BA.
3783                                                 pAdapter->CommonCfg.BACapability.field.MpduDensity = Orde.MpduDensity;
3784                                                 pAdapter->CommonCfg.DesiredHtPhy.MpduDensity = Orde.MpduDensity;
3785                                                 pAdapter->CommonCfg.DesiredHtPhy.AmsduEnable = Orde.AmsduEnable;
3786                                                 pAdapter->CommonCfg.DesiredHtPhy.AmsduSize= Orde.AmsduSize;
3787                                                 pAdapter->CommonCfg.DesiredHtPhy.MimoPs = Orde.MMPSmode;
3788                                                 pAdapter->CommonCfg.BACapability.field.MMPSmode = Orde.MMPSmode;
3789
3790                                                 // UPdata to HT IE
3791                                                 pAdapter->CommonCfg.HtCapability.HtCapInfo.MimoPs = Orde.MMPSmode;
3792                                                 pAdapter->CommonCfg.HtCapability.HtCapInfo.AMsduSize = Orde.AmsduSize;
3793                                                 pAdapter->CommonCfg.HtCapability.HtCapParm.MpduDensity = Orde.MpduDensity;
3794
3795                                                 if (pAdapter->CommonCfg.BACapability.field.RxBAWinLimit > MAX_RX_REORDERBUF)
3796                                                         pAdapter->CommonCfg.BACapability.field.RxBAWinLimit = MAX_RX_REORDERBUF;
3797
3798                                         }
3799
3800                                         pAdapter->CommonCfg.REGBACapability.word = pAdapter->CommonCfg.BACapability.word;
3801                                         DBGPRINT(RT_DEBUG_TRACE, ("Set::(Orde.AutoBA = %d) (Policy=%d)(ReBAWinLimit=%d)(TxBAWinLimit=%d)(AutoMode=%d)\n",Orde.AutoBA, pAdapter->CommonCfg.BACapability.field.Policy,
3802                                                 pAdapter->CommonCfg.BACapability.field.RxBAWinLimit,pAdapter->CommonCfg.BACapability.field.TxBAWinLimit, pAdapter->CommonCfg.BACapability.field.AutoBA));
3803                                         DBGPRINT(RT_DEBUG_TRACE, ("Set::(MimoPs = %d)(AmsduEnable = %d) (AmsduSize=%d)(MpduDensity=%d)\n",pAdapter->CommonCfg.DesiredHtPhy.MimoPs, pAdapter->CommonCfg.DesiredHtPhy.AmsduEnable,
3804                                                 pAdapter->CommonCfg.DesiredHtPhy.AmsduSize, pAdapter->CommonCfg.DesiredHtPhy.MpduDensity));
3805                                 }
3806
3807                                 break;
3808                 case RT_OID_802_11_ADD_IMME_BA:
3809                         DBGPRINT(RT_DEBUG_TRACE, (" Set :: RT_OID_802_11_ADD_IMME_BA \n"));
3810                         if (wrq->u.data.length != sizeof(OID_ADD_BA_ENTRY))
3811                                         Status = -EINVAL;
3812                         else
3813                         {
3814                                 UCHAR                   index;
3815                                 OID_ADD_BA_ENTRY    BA;
3816                                 MAC_TABLE_ENTRY     *pEntry;
3817
3818                                 Status = copy_from_user(&BA, wrq->u.data.pointer, wrq->u.data.length);
3819                                 if (BA.TID > 15)
3820                                 {
3821                                         Status = NDIS_STATUS_INVALID_DATA;
3822                                         break;
3823                                 }
3824                                 else
3825                                 {
3826                                         //BATableInsertEntry
3827                                         //As ad-hoc mode, BA pair is not limited to only BSSID. so add via OID.
3828                                         index = BA.TID;
3829                                         // in ad hoc mode, when adding BA pair, we should insert this entry into MACEntry too
3830                                         pEntry = MacTableLookup(pAdapter, BA.MACAddr);
3831                                         if (!pEntry)
3832                                         {
3833                                                 DBGPRINT(RT_DEBUG_TRACE, ("RT_OID_802_11_ADD_IMME_BA. break on no connection.----:%x:%x\n", BA.MACAddr[4], BA.MACAddr[5]));
3834                                                 break;
3835                                         }
3836                                         if (BA.IsRecipient == FALSE)
3837                                         {
3838                                             if (pEntry->bIAmBadAtheros == TRUE)
3839                                                         pAdapter->CommonCfg.BACapability.field.RxBAWinLimit = 0x10;
3840
3841                                                 BAOriSessionSetUp(pAdapter, pEntry, index, 0, 100, TRUE);
3842                                         }
3843                                         else
3844                                         {
3845                                                 //BATableInsertEntry(pAdapter, pEntry->Aid, BA.MACAddr, 0, 0xffff, BA.TID, BA.nMSDU, BA.IsRecipient);
3846                                         }
3847
3848                                         DBGPRINT(RT_DEBUG_TRACE, ("Set::RT_OID_802_11_ADD_IMME_BA. Rec = %d. Mac = %x:%x:%x:%x:%x:%x . \n",
3849                                                 BA.IsRecipient, BA.MACAddr[0], BA.MACAddr[1], BA.MACAddr[2], BA.MACAddr[2]
3850                                                 , BA.MACAddr[4], BA.MACAddr[5]));
3851                                 }
3852                         }
3853                         break;
3854
3855                 case RT_OID_802_11_TEAR_IMME_BA:
3856                         DBGPRINT(RT_DEBUG_TRACE, ("Set :: RT_OID_802_11_TEAR_IMME_BA \n"));
3857                         if (wrq->u.data.length != sizeof(OID_ADD_BA_ENTRY))
3858                                         Status = -EINVAL;
3859                         else
3860                         {
3861                                 POID_ADD_BA_ENTRY       pBA;
3862                                 MAC_TABLE_ENTRY *pEntry;
3863
3864                                 pBA = kmalloc(wrq->u.data.length, MEM_ALLOC_FLAG);
3865
3866                                 if (pBA == NULL)
3867                                 {
3868                                         DBGPRINT(RT_DEBUG_TRACE, ("Set :: RT_OID_802_11_TEAR_IMME_BA kmalloc() can't allocate enough memory\n"));
3869                                         Status = NDIS_STATUS_FAILURE;
3870                                 }
3871                                 else
3872                                 {
3873                                         Status = copy_from_user(pBA, wrq->u.data.pointer, wrq->u.data.length);
3874                                         DBGPRINT(RT_DEBUG_TRACE, ("Set :: RT_OID_802_11_TEAR_IMME_BA(TID=%d, bAllTid=%d)\n", pBA->TID, pBA->bAllTid));
3875
3876                                         if (!pBA->bAllTid && (pBA->TID > NUM_OF_TID))
3877                                         {
3878                                                 Status = NDIS_STATUS_INVALID_DATA;
3879                                                 break;
3880                                         }
3881
3882                                         if (pBA->IsRecipient == FALSE)
3883                                         {
3884                                                 pEntry = MacTableLookup(pAdapter, pBA->MACAddr);
3885                                                 DBGPRINT(RT_DEBUG_TRACE, (" pBA->IsRecipient == FALSE\n"));
3886                                                 if (pEntry)
3887                                                 {
3888                                                         DBGPRINT(RT_DEBUG_TRACE, (" pBA->pEntry\n"));
3889                                                         BAOriSessionTearDown(pAdapter, pEntry->Aid, pBA->TID, FALSE, TRUE);
3890                                                 }
3891                                                 else
3892                                                         DBGPRINT(RT_DEBUG_TRACE, ("Set :: Not found pEntry \n"));
3893                                         }
3894                                         else
3895                                         {
3896                                                 pEntry = MacTableLookup(pAdapter, pBA->MACAddr);
3897                                                 if (pEntry)
3898                                                 {
3899                                                         BARecSessionTearDown( pAdapter, (UCHAR)pEntry->Aid, pBA->TID, TRUE);
3900                                                 }
3901                                                 else
3902                                                         DBGPRINT(RT_DEBUG_TRACE, ("Set :: Not found pEntry \n"));
3903                                         }
3904                                         kfree(pBA);
3905                                 }
3906             }
3907             break;
3908         // For WPA_SUPPLICANT to set static wep key
3909         case OID_802_11_ADD_WEP:
3910             pWepKey = kmalloc(wrq->u.data.length, MEM_ALLOC_FLAG);
3911
3912             if(pWepKey == NULL)
3913             {
3914                 Status = -ENOMEM;
3915                                 DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_ADD_WEP, Failed!!\n"));
3916                 break;
3917             }
3918             Status = copy_from_user(pWepKey, wrq->u.data.pointer, wrq->u.data.length);
3919             if (Status)
3920             {
3921                 Status  = -EINVAL;
3922                 DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_ADD_WEP, Failed (length mismatch)!!\n"));
3923             }
3924             else
3925             {
3926                         KeyIdx = pWepKey->KeyIndex & 0x0fffffff;
3927                 // KeyIdx must be 0 ~ 3
3928                 if (KeyIdx > 4)
3929                         {
3930                     Status  = -EINVAL;
3931                     DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_ADD_WEP, Failed (KeyIdx must be smaller than 4)!!\n"));
3932                 }
3933                 else
3934                 {
3935                     UCHAR CipherAlg = 0;
3936                     PUCHAR Key;
3937
3938                     // set key material and key length
3939                     NdisZeroMemory(pAdapter->SharedKey[BSS0][KeyIdx].Key, 16);
3940                     pAdapter->SharedKey[BSS0][KeyIdx].KeyLen = (UCHAR) pWepKey->KeyLength;
3941                     NdisMoveMemory(pAdapter->SharedKey[BSS0][KeyIdx].Key, &pWepKey->KeyMaterial, pWepKey->KeyLength);
3942
3943                     switch(pWepKey->KeyLength)
3944                     {
3945                         case 5:
3946                             CipherAlg = CIPHER_WEP64;
3947                             break;
3948                         case 13:
3949                             CipherAlg = CIPHER_WEP128;
3950                             break;
3951                         default:
3952                             DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_ADD_WEP, only support CIPHER_WEP64(len:5) & CIPHER_WEP128(len:13)!!\n"));
3953                             Status = -EINVAL;
3954                             break;
3955                     }
3956                     pAdapter->SharedKey[BSS0][KeyIdx].CipherAlg = CipherAlg;
3957
3958                     // Default key for tx (shared key)
3959                     if (pWepKey->KeyIndex & 0x80000000)
3960                     {
3961                         // set key material and key length
3962                         NdisZeroMemory(pAdapter->StaCfg.DesireSharedKey[KeyIdx].Key, 16);
3963                         pAdapter->StaCfg.DesireSharedKey[KeyIdx].KeyLen = (UCHAR) pWepKey->KeyLength;
3964                         NdisMoveMemory(pAdapter->StaCfg.DesireSharedKey[KeyIdx].Key, &pWepKey->KeyMaterial, pWepKey->KeyLength);
3965                         pAdapter->StaCfg.DesireSharedKeyId = KeyIdx;
3966                         pAdapter->StaCfg.DesireSharedKey[KeyIdx].CipherAlg = CipherAlg;
3967                         pAdapter->StaCfg.DefaultKeyId = (UCHAR) KeyIdx;
3968                     }
3969
3970                     if (pAdapter->StaCfg.PortSecured == WPA_802_1X_PORT_SECURED)
3971                     {
3972                         Key = pAdapter->SharedKey[BSS0][KeyIdx].Key;
3973
3974                         // Set key material and cipherAlg to Asic
3975                                         AsicAddSharedKeyEntry(pAdapter, BSS0, KeyIdx, CipherAlg, Key, NULL, NULL);
3976
3977                         if (pWepKey->KeyIndex & 0x80000000)
3978                         {
3979                             PMAC_TABLE_ENTRY pEntry = &pAdapter->MacTab.Content[BSSID_WCID];
3980                             // Assign group key info
3981                                                 RTMPAddWcidAttributeEntry(pAdapter, BSS0, KeyIdx, CipherAlg, NULL);
3982                                                 // Assign pairwise key info
3983                                                 RTMPAddWcidAttributeEntry(pAdapter, BSS0, KeyIdx, CipherAlg, pEntry);
3984                         }
3985                     }
3986                                         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"));
3987                                 }
3988             }
3989             kfree(pWepKey);
3990             break;
3991             case OID_SET_COUNTERMEASURES:
3992             if (wrq->u.data.length != sizeof(int))
3993                 Status  = -EINVAL;
3994             else
3995             {
3996                 int enabled = 0;
3997                 Status = copy_from_user(&enabled, wrq->u.data.pointer, wrq->u.data.length);
3998                 if (enabled == 1)
3999                     pAdapter->StaCfg.bBlockAssoc = TRUE;
4000                 else
4001                     // WPA MIC error should block association attempt for 60 seconds
4002                     pAdapter->StaCfg.bBlockAssoc = FALSE;
4003                 DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_SET_COUNTERMEASURES bBlockAssoc=%s\n", pAdapter->StaCfg.bBlockAssoc ? "TRUE":"FALSE"));
4004             }
4005                 break;
4006         case RT_OID_WPA_SUPPLICANT_SUPPORT:
4007                         if (wrq->u.data.length != sizeof(UCHAR))
4008                 Status  = -EINVAL;
4009             else
4010             {
4011                 Status = copy_from_user(&wpa_supplicant_enable, wrq->u.data.pointer, wrq->u.data.length);
4012                         pAdapter->StaCfg.WpaSupplicantUP = wpa_supplicant_enable;
4013                         DBGPRINT(RT_DEBUG_TRACE, ("Set::RT_OID_WPA_SUPPLICANT_SUPPORT (=%d)\n", pAdapter->StaCfg.WpaSupplicantUP));
4014                         }
4015             break;
4016         case OID_802_11_DEAUTHENTICATION:
4017             if (wrq->u.data.length != sizeof(MLME_DEAUTH_REQ_STRUCT))
4018                 Status  = -EINVAL;
4019             else
4020             {
4021                 MLME_DEAUTH_REQ_STRUCT      *pInfo;
4022                                 MLME_QUEUE_ELEM *MsgElem = (MLME_QUEUE_ELEM *) kmalloc(sizeof(MLME_QUEUE_ELEM), MEM_ALLOC_FLAG);
4023
4024                 pInfo = (MLME_DEAUTH_REQ_STRUCT *) MsgElem->Msg;
4025                 Status = copy_from_user(pInfo, wrq->u.data.pointer, wrq->u.data.length);
4026                 MlmeDeauthReqAction(pAdapter, MsgElem);
4027                                 kfree(MsgElem);
4028
4029                 if (INFRA_ON(pAdapter))
4030                 {
4031                     LinkDown(pAdapter, FALSE);
4032                     pAdapter->Mlme.AssocMachine.CurrState = ASSOC_IDLE;
4033                 }
4034                 DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_DEAUTHENTICATION (Reason=%d)\n", pInfo->Reason));
4035             }
4036             break;
4037         case OID_802_11_DROP_UNENCRYPTED:
4038             if (wrq->u.data.length != sizeof(int))
4039                 Status  = -EINVAL;
4040             else
4041             {
4042                 int enabled = 0;
4043                 Status = copy_from_user(&enabled, wrq->u.data.pointer, wrq->u.data.length);
4044                 if (enabled == 1)
4045                     pAdapter->StaCfg.PortSecured = WPA_802_1X_PORT_NOT_SECURED;
4046                 else
4047                     pAdapter->StaCfg.PortSecured = WPA_802_1X_PORT_SECURED;
4048                                 NdisAcquireSpinLock(&pAdapter->MacTabLock);
4049                                 pAdapter->MacTab.Content[BSSID_WCID].PortSecured = pAdapter->StaCfg.PortSecured;
4050                                 NdisReleaseSpinLock(&pAdapter->MacTabLock);
4051                 DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_DROP_UNENCRYPTED (=%d)\n", enabled));
4052             }
4053             break;
4054         case OID_802_11_SET_IEEE8021X:
4055             if (wrq->u.data.length != sizeof(BOOLEAN))
4056                 Status  = -EINVAL;
4057             else
4058             {
4059                 Status = copy_from_user(&IEEE8021xState, wrq->u.data.pointer, wrq->u.data.length);
4060                         pAdapter->StaCfg.IEEE8021X = IEEE8021xState;
4061                 DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_SET_IEEE8021X (=%d)\n", IEEE8021xState));
4062             }
4063             break;
4064         case OID_802_11_SET_IEEE8021X_REQUIRE_KEY:
4065                         if (wrq->u.data.length != sizeof(BOOLEAN))
4066                                  Status  = -EINVAL;
4067             else
4068             {
4069                 Status = copy_from_user(&IEEE8021x_required_keys, wrq->u.data.pointer, wrq->u.data.length);
4070                                 pAdapter->StaCfg.IEEE8021x_required_keys = IEEE8021x_required_keys;
4071                                 DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_SET_IEEE8021X_REQUIRE_KEY (%d)\n", IEEE8021x_required_keys));
4072                         }
4073                         break;
4074         case OID_802_11_PMKID:
4075                 pPmkId = kmalloc(wrq->u.data.length, MEM_ALLOC_FLAG);
4076
4077                 if(pPmkId == NULL) {
4078                 Status = -ENOMEM;
4079                 break;
4080             }
4081             Status = copy_from_user(pPmkId, wrq->u.data.pointer, wrq->u.data.length);
4082
4083                 // check the PMKID information
4084                 if (pPmkId->BSSIDInfoCount == 0)
4085                 NdisZeroMemory(pAdapter->StaCfg.SavedPMK, sizeof(BSSID_INFO)*PMKID_NO);
4086                 else
4087                 {
4088                         PBSSID_INFO     pBssIdInfo;
4089                         UINT            BssIdx;
4090                         UINT            CachedIdx;
4091
4092                         for (BssIdx = 0; BssIdx < pPmkId->BSSIDInfoCount; BssIdx++)
4093                         {
4094                                 // point to the indexed BSSID_INFO structure
4095                                 pBssIdInfo = (PBSSID_INFO) ((PUCHAR) pPmkId + 2 * sizeof(UINT) + BssIdx * sizeof(BSSID_INFO));
4096                                 // Find the entry in the saved data base.
4097                                 for (CachedIdx = 0; CachedIdx < pAdapter->StaCfg.SavedPMKNum; CachedIdx++)
4098                                 {
4099                                         // compare the BSSID
4100                                         if (NdisEqualMemory(pBssIdInfo->BSSID, pAdapter->StaCfg.SavedPMK[CachedIdx].BSSID, sizeof(NDIS_802_11_MAC_ADDRESS)))
4101                                                 break;
4102                                 }
4103
4104                                 // Found, replace it
4105                                 if (CachedIdx < PMKID_NO)
4106                                 {
4107                                         DBGPRINT(RT_DEBUG_OFF, ("Update OID_802_11_PMKID, idx = %d\n", CachedIdx));
4108                                         NdisMoveMemory(&pAdapter->StaCfg.SavedPMK[CachedIdx], pBssIdInfo, sizeof(BSSID_INFO));
4109                                         pAdapter->StaCfg.SavedPMKNum++;
4110                                 }
4111                                 // Not found, replace the last one
4112                                 else
4113                                 {
4114                                         // Randomly replace one
4115                                         CachedIdx = (pBssIdInfo->BSSID[5] % PMKID_NO);
4116                                         DBGPRINT(RT_DEBUG_OFF, ("Update OID_802_11_PMKID, idx = %d\n", CachedIdx));
4117                                         NdisMoveMemory(&pAdapter->StaCfg.SavedPMK[CachedIdx], pBssIdInfo, sizeof(BSSID_INFO));
4118                                 }
4119                         }
4120                         }
4121                         if(pPmkId)
4122                                 kfree(pPmkId);
4123                 break;
4124         default:
4125             DBGPRINT(RT_DEBUG_TRACE, ("Set::unknown IOCTL's subcmd = 0x%08x\n", cmd));
4126             Status = -EOPNOTSUPP;
4127             break;
4128     }
4129
4130
4131     return Status;
4132 }
4133
4134 INT RTMPQueryInformation(
4135     IN  PRTMP_ADAPTER pAdapter,
4136     IN  OUT struct ifreq    *rq,
4137     IN  INT                 cmd)
4138 {
4139     struct iwreq                        *wrq = (struct iwreq *) rq;
4140     NDIS_802_11_BSSID_LIST_EX           *pBssidList = NULL;
4141     PNDIS_WLAN_BSSID_EX                 pBss;
4142     NDIS_802_11_SSID                    Ssid;
4143     NDIS_802_11_CONFIGURATION           *pConfiguration = NULL;
4144     RT_802_11_LINK_STATUS               *pLinkStatus = NULL;
4145     RT_802_11_STA_CONFIG                *pStaConfig = NULL;
4146     NDIS_802_11_STATISTICS              *pStatistics = NULL;
4147     NDIS_802_11_RTS_THRESHOLD           RtsThresh;
4148     NDIS_802_11_FRAGMENTATION_THRESHOLD FragThresh;
4149     NDIS_802_11_POWER_MODE              PowerMode;
4150     NDIS_802_11_NETWORK_INFRASTRUCTURE  BssType;
4151     RT_802_11_PREAMBLE                  PreamType;
4152     NDIS_802_11_AUTHENTICATION_MODE     AuthMode;
4153     NDIS_802_11_WEP_STATUS              WepStatus;
4154     NDIS_MEDIA_STATE                    MediaState;
4155     ULONG                               BssBufSize, ulInfo=0, NetworkTypeList[4], apsd = 0;
4156     USHORT                              BssLen = 0;
4157     PUCHAR                              pBuf = NULL, pPtr;
4158     INT                                 Status = NDIS_STATUS_SUCCESS;
4159     UINT                                we_version_compiled;
4160     UCHAR                               i, Padding = 0;
4161     BOOLEAN                             RadioState;
4162         UCHAR   driverVersion[8];
4163     OID_SET_HT_PHYMODE                          *pHTPhyMode = NULL;
4164
4165     switch(cmd)
4166     {
4167         case RT_OID_DEVICE_NAME:
4168             wrq->u.data.length = sizeof(STA_NIC_DEVICE_NAME);
4169             Status = copy_to_user(wrq->u.data.pointer, STA_NIC_DEVICE_NAME, wrq->u.data.length);
4170             break;
4171         case RT_OID_VERSION_INFO:
4172                         DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_VERSION_INFO \n"));
4173                         wrq->u.data.length = 8*sizeof(UCHAR);
4174                         sprintf(&driverVersion[0], "%s", STA_DRIVER_VERSION);
4175                         driverVersion[7] = '\0';
4176                         if (copy_to_user(wrq->u.data.pointer, &driverVersion, wrq->u.data.length))
4177             {
4178                                 Status = -EFAULT;
4179             }
4180             break;
4181         case OID_802_11_BSSID_LIST:
4182             if (RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_BSS_SCAN_IN_PROGRESS))
4183             {
4184                 /*
4185                  * Still scanning, indicate the caller should try again.
4186                  */
4187                 DBGPRINT(RT_DEBUG_TRACE, ("Query::OID_802_11_BSSID_LIST (Still scanning)\n"));
4188                                 return -EAGAIN;
4189             }
4190             DBGPRINT(RT_DEBUG_TRACE, ("Query::OID_802_11_BSSID_LIST (%d BSS returned)\n",pAdapter->ScanTab.BssNr));
4191                         pAdapter->StaCfg.bScanReqIsFromWebUI = FALSE;
4192             // Claculate total buffer size required
4193             BssBufSize = sizeof(ULONG);
4194
4195             for (i = 0; i < pAdapter->ScanTab.BssNr; i++)
4196             {
4197                 // Align pointer to 4 bytes boundary.
4198                 //Padding = 4 - (pAdapter->ScanTab.BssEntry[i].VarIELen & 0x0003);
4199                 //if (Padding == 4)
4200                 //    Padding = 0;
4201                 BssBufSize += (sizeof(NDIS_WLAN_BSSID_EX) - 1 + sizeof(NDIS_802_11_FIXED_IEs) + pAdapter->ScanTab.BssEntry[i].VarIELen + Padding);
4202             }
4203
4204             // For safety issue, we add 256 bytes just in case
4205             BssBufSize += 256;
4206             // Allocate the same size as passed from higher layer
4207             pBuf = kmalloc(BssBufSize, MEM_ALLOC_FLAG);
4208             if(pBuf == NULL)
4209             {
4210                 Status = -ENOMEM;
4211                 break;
4212             }
4213             // Init 802_11_BSSID_LIST_EX structure
4214             NdisZeroMemory(pBuf, BssBufSize);
4215             pBssidList = (PNDIS_802_11_BSSID_LIST_EX) pBuf;
4216             pBssidList->NumberOfItems = pAdapter->ScanTab.BssNr;
4217
4218             // Calculate total buffer length
4219             BssLen = 4; // Consist of NumberOfItems
4220             // Point to start of NDIS_WLAN_BSSID_EX
4221             // pPtr = pBuf + sizeof(ULONG);
4222             pPtr = (PUCHAR) &pBssidList->Bssid[0];
4223             for (i = 0; i < pAdapter->ScanTab.BssNr; i++)
4224             {
4225                 pBss = (PNDIS_WLAN_BSSID_EX) pPtr;
4226                 NdisMoveMemory(&pBss->MacAddress, &pAdapter->ScanTab.BssEntry[i].Bssid, MAC_ADDR_LEN);
4227                 if ((pAdapter->ScanTab.BssEntry[i].Hidden == 1) && (pAdapter->StaCfg.bShowHiddenSSID == FALSE))
4228                 {
4229                     //
4230                                         // We must return this SSID during 4way handshaking, otherwise Aegis will failed to parse WPA infomation
4231                                         // and then failed to send EAPOl farame.
4232                                         //
4233                                         if ((pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA) && (pAdapter->StaCfg.PortSecured != WPA_802_1X_PORT_SECURED))
4234                                         {
4235                                                 pBss->Ssid.SsidLength = pAdapter->ScanTab.BssEntry[i].SsidLen;
4236                                                 NdisMoveMemory(pBss->Ssid.Ssid, pAdapter->ScanTab.BssEntry[i].Ssid, pAdapter->ScanTab.BssEntry[i].SsidLen);
4237                                         }
4238                                         else
4239                         pBss->Ssid.SsidLength = 0;
4240                 }
4241                 else
4242                 {
4243                     pBss->Ssid.SsidLength = pAdapter->ScanTab.BssEntry[i].SsidLen;
4244                     NdisMoveMemory(pBss->Ssid.Ssid, pAdapter->ScanTab.BssEntry[i].Ssid, pAdapter->ScanTab.BssEntry[i].SsidLen);
4245                 }
4246                 pBss->Privacy = pAdapter->ScanTab.BssEntry[i].Privacy;
4247                 pBss->Rssi = pAdapter->ScanTab.BssEntry[i].Rssi - pAdapter->BbpRssiToDbmDelta;
4248                 pBss->NetworkTypeInUse = NetworkTypeInUseSanity(&pAdapter->ScanTab.BssEntry[i]);
4249                 pBss->Configuration.Length = sizeof(NDIS_802_11_CONFIGURATION);
4250                 pBss->Configuration.BeaconPeriod = pAdapter->ScanTab.BssEntry[i].BeaconPeriod;
4251                 pBss->Configuration.ATIMWindow = pAdapter->ScanTab.BssEntry[i].AtimWin;
4252
4253                 MAP_CHANNEL_ID_TO_KHZ(pAdapter->ScanTab.BssEntry[i].Channel, pBss->Configuration.DSConfig);
4254
4255                 if (pAdapter->ScanTab.BssEntry[i].BssType == BSS_INFRA)
4256                     pBss->InfrastructureMode = Ndis802_11Infrastructure;
4257                 else
4258                     pBss->InfrastructureMode = Ndis802_11IBSS;
4259
4260                 NdisMoveMemory(pBss->SupportedRates, pAdapter->ScanTab.BssEntry[i].SupRate, pAdapter->ScanTab.BssEntry[i].SupRateLen);
4261                 NdisMoveMemory(pBss->SupportedRates + pAdapter->ScanTab.BssEntry[i].SupRateLen,
4262                                pAdapter->ScanTab.BssEntry[i].ExtRate,
4263                                pAdapter->ScanTab.BssEntry[i].ExtRateLen);
4264
4265                 if (pAdapter->ScanTab.BssEntry[i].VarIELen == 0)
4266                 {
4267                     pBss->IELength = sizeof(NDIS_802_11_FIXED_IEs);
4268                     NdisMoveMemory(pBss->IEs, &pAdapter->ScanTab.BssEntry[i].FixIEs, sizeof(NDIS_802_11_FIXED_IEs));
4269                     pPtr = pPtr + sizeof(NDIS_WLAN_BSSID_EX) - 1 + sizeof(NDIS_802_11_FIXED_IEs);
4270                 }
4271                 else
4272                 {
4273                     pBss->IELength = (ULONG)(sizeof(NDIS_802_11_FIXED_IEs) + pAdapter->ScanTab.BssEntry[i].VarIELen);
4274                     pPtr = pPtr + sizeof(NDIS_WLAN_BSSID_EX) - 1 + sizeof(NDIS_802_11_FIXED_IEs);
4275                     NdisMoveMemory(pBss->IEs, &pAdapter->ScanTab.BssEntry[i].FixIEs, sizeof(NDIS_802_11_FIXED_IEs));
4276                     NdisMoveMemory(pBss->IEs + sizeof(NDIS_802_11_FIXED_IEs), pAdapter->ScanTab.BssEntry[i].VarIEs, pAdapter->ScanTab.BssEntry[i].VarIELen);
4277                     pPtr += pAdapter->ScanTab.BssEntry[i].VarIELen;
4278                 }
4279                 pBss->Length = (ULONG)(sizeof(NDIS_WLAN_BSSID_EX) - 1 + sizeof(NDIS_802_11_FIXED_IEs) + pAdapter->ScanTab.BssEntry[i].VarIELen + Padding);
4280
4281 #if WIRELESS_EXT < 17
4282                 if ((BssLen + pBss->Length) < wrq->u.data.length)
4283                 BssLen += pBss->Length;
4284                 else
4285                 {
4286                     pBssidList->NumberOfItems = i;
4287                     break;
4288                 }
4289 #else
4290                 BssLen += pBss->Length;
4291 #endif
4292             }
4293
4294 #if WIRELESS_EXT < 17
4295             wrq->u.data.length = BssLen;
4296 #else
4297             if (BssLen > wrq->u.data.length)
4298             {
4299                 kfree(pBssidList);
4300                 return -E2BIG;
4301             }
4302             else
4303                 wrq->u.data.length = BssLen;
4304 #endif
4305             Status = copy_to_user(wrq->u.data.pointer, pBssidList, BssLen);
4306             kfree(pBssidList);
4307             break;
4308         case OID_802_3_CURRENT_ADDRESS:
4309             wrq->u.data.length = MAC_ADDR_LEN;
4310             Status = copy_to_user(wrq->u.data.pointer, &pAdapter->CurrentAddress, wrq->u.data.length);
4311             break;
4312         case OID_GEN_MEDIA_CONNECT_STATUS:
4313             if (pAdapter->IndicateMediaState == NdisMediaStateConnected)
4314                 MediaState = NdisMediaStateConnected;
4315             else
4316                 MediaState = NdisMediaStateDisconnected;
4317
4318             wrq->u.data.length = sizeof(NDIS_MEDIA_STATE);
4319             Status = copy_to_user(wrq->u.data.pointer, &MediaState, wrq->u.data.length);
4320             break;
4321         case OID_802_11_BSSID:
4322             if (INFRA_ON(pAdapter) || ADHOC_ON(pAdapter))
4323             {
4324                 Status = copy_to_user(wrq->u.data.pointer, &pAdapter->CommonCfg.Bssid, sizeof(NDIS_802_11_MAC_ADDRESS));
4325
4326             }
4327             else
4328             {
4329                 DBGPRINT(RT_DEBUG_TRACE, ("Query::OID_802_11_BSSID(=EMPTY)\n"));
4330                 Status = -ENOTCONN;
4331             }
4332             break;
4333         case OID_802_11_SSID:
4334                         NdisZeroMemory(&Ssid, sizeof(NDIS_802_11_SSID));
4335                         NdisZeroMemory(Ssid.Ssid, MAX_LEN_OF_SSID);
4336             Ssid.SsidLength = pAdapter->CommonCfg.SsidLen;
4337                         memcpy(Ssid.Ssid, pAdapter->CommonCfg.Ssid,     Ssid.SsidLength);
4338             wrq->u.data.length = sizeof(NDIS_802_11_SSID);
4339             Status = copy_to_user(wrq->u.data.pointer, &Ssid, wrq->u.data.length);
4340             DBGPRINT(RT_DEBUG_TRACE, ("Query::OID_802_11_SSID (Len=%d, ssid=%s)\n", Ssid.SsidLength,Ssid.Ssid));
4341             break;
4342         case RT_OID_802_11_QUERY_LINK_STATUS:
4343             pLinkStatus = (RT_802_11_LINK_STATUS *) kmalloc(sizeof(RT_802_11_LINK_STATUS), MEM_ALLOC_FLAG);
4344             if (pLinkStatus)
4345             {
4346                 pLinkStatus->CurrTxRate = RateIdTo500Kbps[pAdapter->CommonCfg.TxRate];   // unit : 500 kbps
4347                 pLinkStatus->ChannelQuality = pAdapter->Mlme.ChannelQuality;
4348                 pLinkStatus->RxByteCount = pAdapter->RalinkCounters.ReceivedByteCount;
4349                 pLinkStatus->TxByteCount = pAdapter->RalinkCounters.TransmittedByteCount;
4350                         pLinkStatus->CentralChannel = pAdapter->CommonCfg.CentralChannel;
4351                 wrq->u.data.length = sizeof(RT_802_11_LINK_STATUS);
4352                 Status = copy_to_user(wrq->u.data.pointer, pLinkStatus, wrq->u.data.length);
4353                 kfree(pLinkStatus);
4354                 DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_802_11_QUERY_LINK_STATUS\n"));
4355             }
4356             else
4357             {
4358                 DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_802_11_QUERY_LINK_STATUS(kmalloc failed)\n"));
4359                 Status = -EFAULT;
4360             }
4361             break;
4362         case OID_802_11_CONFIGURATION:
4363             pConfiguration = (NDIS_802_11_CONFIGURATION *) kmalloc(sizeof(NDIS_802_11_CONFIGURATION), MEM_ALLOC_FLAG);
4364             if (pConfiguration)
4365             {
4366                 pConfiguration->Length = sizeof(NDIS_802_11_CONFIGURATION);
4367                 pConfiguration->BeaconPeriod = pAdapter->CommonCfg.BeaconPeriod;
4368                 pConfiguration->ATIMWindow = pAdapter->StaActive.AtimWin;
4369                 MAP_CHANNEL_ID_TO_KHZ(pAdapter->CommonCfg.Channel, pConfiguration->DSConfig);
4370                 wrq->u.data.length = sizeof(NDIS_802_11_CONFIGURATION);
4371                 Status = copy_to_user(wrq->u.data.pointer, pConfiguration, wrq->u.data.length);
4372                 DBGPRINT(RT_DEBUG_TRACE, ("Query::OID_802_11_CONFIGURATION(BeaconPeriod=%ld,AtimW=%ld,Channel=%d) \n",
4373                                         pConfiguration->BeaconPeriod, pConfiguration->ATIMWindow, pAdapter->CommonCfg.Channel));
4374                                 kfree(pConfiguration);
4375             }
4376             else
4377             {
4378                 DBGPRINT(RT_DEBUG_TRACE, ("Query::OID_802_11_CONFIGURATION(kmalloc failed)\n"));
4379                 Status = -EFAULT;
4380             }
4381             break;
4382                 case RT_OID_802_11_SNR_0:
4383                         if ((pAdapter->StaCfg.LastSNR0 > 0))
4384                         {
4385                                 ulInfo = ((0xeb - pAdapter->StaCfg.LastSNR0) * 3) /     16 ;
4386                                 wrq->u.data.length = sizeof(ulInfo);
4387                                 Status = copy_to_user(wrq->u.data.pointer, &ulInfo,     wrq->u.data.length);
4388                                 DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_802_11_SNR_0(0x=%lx)\n", ulInfo));
4389                         }
4390             else
4391                             Status = -EFAULT;
4392                         break;
4393                 case RT_OID_802_11_SNR_1:
4394                         if ((pAdapter->Antenna.field.RxPath     > 1) &&
4395                 (pAdapter->StaCfg.LastSNR1 > 0))
4396                         {
4397                                 ulInfo = ((0xeb - pAdapter->StaCfg.LastSNR1) * 3) /     16 ;
4398                                 wrq->u.data.length = sizeof(ulInfo);
4399                                 Status = copy_to_user(wrq->u.data.pointer, &ulInfo,     wrq->u.data.length);
4400                                 DBGPRINT(RT_DEBUG_TRACE,("Query::RT_OID_802_11_SNR_1(0x=%lx)\n",ulInfo));
4401                         }
4402                         else
4403                                 Status = -EFAULT;
4404             DBGPRINT(RT_DEBUG_TRACE,("Query::RT_OID_802_11_SNR_1(pAdapter->StaCfg.LastSNR1=%d)\n",pAdapter->StaCfg.LastSNR1));
4405                         break;
4406         case OID_802_11_RSSI_TRIGGER:
4407             ulInfo = pAdapter->StaCfg.RssiSample.LastRssi0 - pAdapter->BbpRssiToDbmDelta;
4408             wrq->u.data.length = sizeof(ulInfo);
4409             Status = copy_to_user(wrq->u.data.pointer, &ulInfo, wrq->u.data.length);
4410             DBGPRINT(RT_DEBUG_TRACE, ("Query::OID_802_11_RSSI_TRIGGER(=%ld)\n", ulInfo));
4411             break;
4412                 case OID_802_11_RSSI:
4413         case RT_OID_802_11_RSSI:
4414                         ulInfo = pAdapter->StaCfg.RssiSample.LastRssi0;
4415                         wrq->u.data.length = sizeof(ulInfo);
4416                         Status = copy_to_user(wrq->u.data.pointer, &ulInfo,     wrq->u.data.length);
4417                         break;
4418                 case RT_OID_802_11_RSSI_1:
4419             ulInfo = pAdapter->StaCfg.RssiSample.LastRssi1;
4420                         wrq->u.data.length = sizeof(ulInfo);
4421                         Status = copy_to_user(wrq->u.data.pointer, &ulInfo,     wrq->u.data.length);
4422                         break;
4423         case RT_OID_802_11_RSSI_2:
4424             ulInfo = pAdapter->StaCfg.RssiSample.LastRssi2;
4425                         wrq->u.data.length = sizeof(ulInfo);
4426                         Status = copy_to_user(wrq->u.data.pointer, &ulInfo,     wrq->u.data.length);
4427                         break;
4428         case OID_802_11_STATISTICS:
4429             pStatistics = (NDIS_802_11_STATISTICS *) kmalloc(sizeof(NDIS_802_11_STATISTICS), MEM_ALLOC_FLAG);
4430             if (pStatistics)
4431             {
4432                 DBGPRINT(RT_DEBUG_TRACE, ("Query::OID_802_11_STATISTICS \n"));
4433                 // add the most up-to-date h/w raw counters into software counters
4434                             NICUpdateRawCounters(pAdapter);
4435
4436                 // Sanity check for calculation of sucessful count
4437                 if (pAdapter->WlanCounters.TransmittedFragmentCount.QuadPart < pAdapter->WlanCounters.RetryCount.QuadPart)
4438                     pAdapter->WlanCounters.TransmittedFragmentCount.QuadPart = pAdapter->WlanCounters.RetryCount.QuadPart;
4439
4440                 pStatistics->TransmittedFragmentCount.QuadPart = pAdapter->WlanCounters.TransmittedFragmentCount.QuadPart;
4441                 pStatistics->MulticastTransmittedFrameCount.QuadPart = pAdapter->WlanCounters.MulticastTransmittedFrameCount.QuadPart;
4442                 pStatistics->FailedCount.QuadPart = pAdapter->WlanCounters.FailedCount.QuadPart;
4443                 pStatistics->RetryCount.QuadPart = pAdapter->WlanCounters.RetryCount.QuadPart;
4444                 pStatistics->MultipleRetryCount.QuadPart = pAdapter->WlanCounters.MultipleRetryCount.QuadPart;
4445                 pStatistics->RTSSuccessCount.QuadPart = pAdapter->WlanCounters.RTSSuccessCount.QuadPart;
4446                 pStatistics->RTSFailureCount.QuadPart = pAdapter->WlanCounters.RTSFailureCount.QuadPart;
4447                 pStatistics->ACKFailureCount.QuadPart = pAdapter->WlanCounters.ACKFailureCount.QuadPart;
4448                 pStatistics->FrameDuplicateCount.QuadPart = pAdapter->WlanCounters.FrameDuplicateCount.QuadPart;
4449                 pStatistics->ReceivedFragmentCount.QuadPart = pAdapter->WlanCounters.ReceivedFragmentCount.QuadPart;
4450                 pStatistics->MulticastReceivedFrameCount.QuadPart = pAdapter->WlanCounters.MulticastReceivedFrameCount.QuadPart;
4451 #ifdef DBG
4452                 pStatistics->FCSErrorCount = pAdapter->RalinkCounters.RealFcsErrCount;
4453 #else
4454                 pStatistics->FCSErrorCount.QuadPart = pAdapter->WlanCounters.FCSErrorCount.QuadPart;
4455                 pStatistics->FrameDuplicateCount.u.LowPart = pAdapter->WlanCounters.FrameDuplicateCount.u.LowPart / 100;
4456 #endif
4457                 wrq->u.data.length = sizeof(NDIS_802_11_STATISTICS);
4458                 Status = copy_to_user(wrq->u.data.pointer, pStatistics, wrq->u.data.length);
4459                 kfree(pStatistics);
4460             }
4461             else
4462             {
4463                 DBGPRINT(RT_DEBUG_TRACE, ("Query::OID_802_11_STATISTICS(kmalloc failed)\n"));
4464                 Status = -EFAULT;
4465             }
4466             break;
4467         case OID_GEN_RCV_OK:
4468             ulInfo = pAdapter->Counters8023.GoodReceives;
4469             wrq->u.data.length = sizeof(ulInfo);
4470             Status = copy_to_user(wrq->u.data.pointer, &ulInfo, wrq->u.data.length);
4471             break;
4472         case OID_GEN_RCV_NO_BUFFER:
4473             ulInfo = pAdapter->Counters8023.RxNoBuffer;
4474             wrq->u.data.length = sizeof(ulInfo);
4475             Status = copy_to_user(wrq->u.data.pointer, &ulInfo, wrq->u.data.length);
4476             break;
4477         case RT_OID_802_11_PHY_MODE:
4478             ulInfo = (ULONG)pAdapter->CommonCfg.PhyMode;
4479             wrq->u.data.length = sizeof(ulInfo);
4480             Status = copy_to_user(wrq->u.data.pointer, &ulInfo, wrq->u.data.length);
4481             DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_802_11_PHY_MODE (=%ld)\n", ulInfo));
4482             break;
4483         case RT_OID_802_11_STA_CONFIG:
4484             pStaConfig = (RT_802_11_STA_CONFIG *) kmalloc(sizeof(RT_802_11_STA_CONFIG), MEM_ALLOC_FLAG);
4485             if (pStaConfig)
4486             {
4487                 DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_802_11_STA_CONFIG\n"));
4488                 pStaConfig->EnableTxBurst = pAdapter->CommonCfg.bEnableTxBurst;
4489                 pStaConfig->EnableTurboRate = 0;
4490                 pStaConfig->UseBGProtection = pAdapter->CommonCfg.UseBGProtection;
4491                 pStaConfig->UseShortSlotTime = pAdapter->CommonCfg.bUseShortSlotTime;
4492                 //pStaConfig->AdhocMode = pAdapter->StaCfg.AdhocMode;
4493                 pStaConfig->HwRadioStatus = (pAdapter->StaCfg.bHwRadio == TRUE) ? 1 : 0;
4494                 pStaConfig->Rsv1 = 0;
4495                 pStaConfig->SystemErrorBitmap = pAdapter->SystemErrorBitmap;
4496                 wrq->u.data.length = sizeof(RT_802_11_STA_CONFIG);
4497                 Status = copy_to_user(wrq->u.data.pointer, pStaConfig, wrq->u.data.length);
4498                 kfree(pStaConfig);
4499             }
4500             else
4501             {
4502                 DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_802_11_STA_CONFIG(kmalloc failed)\n"));
4503                 Status = -EFAULT;
4504             }
4505             break;
4506         case OID_802_11_RTS_THRESHOLD:
4507             RtsThresh = pAdapter->CommonCfg.RtsThreshold;
4508             wrq->u.data.length = sizeof(RtsThresh);
4509             Status = copy_to_user(wrq->u.data.pointer, &RtsThresh, wrq->u.data.length);
4510             DBGPRINT(RT_DEBUG_TRACE, ("Query::OID_802_11_RTS_THRESHOLD(=%ld)\n", RtsThresh));
4511             break;
4512         case OID_802_11_FRAGMENTATION_THRESHOLD:
4513             FragThresh = pAdapter->CommonCfg.FragmentThreshold;
4514             if (pAdapter->CommonCfg.bUseZeroToDisableFragment == TRUE)
4515                 FragThresh = 0;
4516             wrq->u.data.length = sizeof(FragThresh);
4517             Status = copy_to_user(wrq->u.data.pointer, &FragThresh, wrq->u.data.length);
4518             DBGPRINT(RT_DEBUG_TRACE, ("Query::OID_802_11_FRAGMENTATION_THRESHOLD(=%ld)\n", FragThresh));
4519             break;
4520         case OID_802_11_POWER_MODE:
4521             PowerMode = pAdapter->StaCfg.WindowsPowerMode;
4522             wrq->u.data.length = sizeof(PowerMode);
4523             Status = copy_to_user(wrq->u.data.pointer, &PowerMode, wrq->u.data.length);
4524             DBGPRINT(RT_DEBUG_TRACE, ("Query::OID_802_11_POWER_MODE(=%d)\n", PowerMode));
4525             break;
4526         case RT_OID_802_11_RADIO:
4527             RadioState = (BOOLEAN) pAdapter->StaCfg.bSwRadio;
4528             wrq->u.data.length = sizeof(RadioState);
4529             Status = copy_to_user(wrq->u.data.pointer, &RadioState, wrq->u.data.length);
4530             DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_802_11_QUERY_RADIO (=%d)\n", RadioState));
4531             break;
4532         case OID_802_11_INFRASTRUCTURE_MODE:
4533             if (pAdapter->StaCfg.BssType == BSS_ADHOC)
4534                 BssType = Ndis802_11IBSS;
4535             else if (pAdapter->StaCfg.BssType == BSS_INFRA)
4536                 BssType = Ndis802_11Infrastructure;
4537             else if (pAdapter->StaCfg.BssType == BSS_MONITOR)
4538                 BssType = Ndis802_11Monitor;
4539             else
4540                 BssType = Ndis802_11AutoUnknown;
4541
4542             wrq->u.data.length = sizeof(BssType);
4543             Status = copy_to_user(wrq->u.data.pointer, &BssType, wrq->u.data.length);
4544             DBGPRINT(RT_DEBUG_TRACE, ("Query::OID_802_11_INFRASTRUCTURE_MODE(=%d)\n", BssType));
4545             break;
4546         case RT_OID_802_11_PREAMBLE:
4547             PreamType = pAdapter->CommonCfg.TxPreamble;
4548             wrq->u.data.length = sizeof(PreamType);
4549             Status = copy_to_user(wrq->u.data.pointer, &PreamType, wrq->u.data.length);
4550             DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_802_11_PREAMBLE(=%d)\n", PreamType));
4551             break;
4552         case OID_802_11_AUTHENTICATION_MODE:
4553             AuthMode = pAdapter->StaCfg.AuthMode;
4554             wrq->u.data.length = sizeof(AuthMode);
4555             Status = copy_to_user(wrq->u.data.pointer, &AuthMode, wrq->u.data.length);
4556             DBGPRINT(RT_DEBUG_TRACE, ("Query::OID_802_11_AUTHENTICATION_MODE(=%d)\n", AuthMode));
4557             break;
4558         case OID_802_11_WEP_STATUS:
4559             WepStatus = pAdapter->StaCfg.WepStatus;
4560             wrq->u.data.length = sizeof(WepStatus);
4561             Status = copy_to_user(wrq->u.data.pointer, &WepStatus, wrq->u.data.length);
4562             DBGPRINT(RT_DEBUG_TRACE, ("Query::OID_802_11_WEP_STATUS(=%d)\n", WepStatus));
4563             break;
4564         case OID_802_11_TX_POWER_LEVEL:
4565                         wrq->u.data.length = sizeof(ULONG);
4566                         Status = copy_to_user(wrq->u.data.pointer, &pAdapter->CommonCfg.TxPower, wrq->u.data.length);
4567                         DBGPRINT(RT_DEBUG_TRACE, ("Query::OID_802_11_TX_POWER_LEVEL %x\n",pAdapter->CommonCfg.TxPower));
4568                         break;
4569         case RT_OID_802_11_TX_POWER_LEVEL_1:
4570             wrq->u.data.length = sizeof(ULONG);
4571             Status = copy_to_user(wrq->u.data.pointer, &pAdapter->CommonCfg.TxPowerPercentage, wrq->u.data.length);
4572                         DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_802_11_TX_POWER_LEVEL_1 (=%ld)\n", pAdapter->CommonCfg.TxPowerPercentage));
4573                         break;
4574         case OID_802_11_NETWORK_TYPES_SUPPORTED:
4575                         if ((pAdapter->RfIcType == RFIC_2850) || (pAdapter->RfIcType == RFIC_2750))
4576                         {
4577                                 NetworkTypeList[0] = 3;                 // NumberOfItems = 3
4578                                 NetworkTypeList[1] = Ndis802_11DS;      // NetworkType[1] = 11b
4579                                 NetworkTypeList[2] = Ndis802_11OFDM24;  // NetworkType[2] = 11g
4580                                 NetworkTypeList[3] = Ndis802_11OFDM5;   // NetworkType[3] = 11a
4581                 wrq->u.data.length = 16;
4582                                 Status = copy_to_user(wrq->u.data.pointer, &NetworkTypeList[0], wrq->u.data.length);
4583                         }
4584                         else
4585                         {
4586                                 NetworkTypeList[0] = 2;                 // NumberOfItems = 2
4587                                 NetworkTypeList[1] = Ndis802_11DS;      // NetworkType[1] = 11b
4588                                 NetworkTypeList[2] = Ndis802_11OFDM24;  // NetworkType[2] = 11g
4589                             wrq->u.data.length = 12;
4590                                 Status = copy_to_user(wrq->u.data.pointer, &NetworkTypeList[0], wrq->u.data.length);
4591                         }
4592                         DBGPRINT(RT_DEBUG_TRACE, ("Query::OID_802_11_NETWORK_TYPES_SUPPORTED\n"));
4593                                 break;
4594             case OID_802_11_NETWORK_TYPE_IN_USE:
4595             wrq->u.data.length = sizeof(ULONG);
4596                         if (pAdapter->CommonCfg.PhyMode == PHY_11A)
4597                                 ulInfo = Ndis802_11OFDM5;
4598                         else if ((pAdapter->CommonCfg.PhyMode == PHY_11BG_MIXED) || (pAdapter->CommonCfg.PhyMode == PHY_11G))
4599                                 ulInfo = Ndis802_11OFDM24;
4600                         else
4601                                 ulInfo = Ndis802_11DS;
4602             Status = copy_to_user(wrq->u.data.pointer, &ulInfo, wrq->u.data.length);
4603                         break;
4604         case RT_OID_802_11_QUERY_LAST_RX_RATE:
4605             ulInfo = (ULONG)pAdapter->LastRxRate;
4606             wrq->u.data.length = sizeof(ulInfo);
4607                         Status = copy_to_user(wrq->u.data.pointer, &ulInfo, wrq->u.data.length);
4608                         DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_802_11_QUERY_LAST_RX_RATE (=%ld)\n", ulInfo));
4609                         break;
4610                 case RT_OID_802_11_QUERY_LAST_TX_RATE:
4611                         //ulInfo = (ULONG)pAdapter->LastTxRate;
4612                         ulInfo = (ULONG)pAdapter->MacTab.Content[BSSID_WCID].HTPhyMode.word;
4613                         wrq->u.data.length = sizeof(ulInfo);
4614                         Status = copy_to_user(wrq->u.data.pointer, &ulInfo,     wrq->u.data.length);
4615                         DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_802_11_QUERY_LAST_TX_RATE (=%lx)\n", ulInfo));
4616                         break;
4617         case RT_OID_802_11_QUERY_EEPROM_VERSION:
4618             wrq->u.data.length = sizeof(ULONG);
4619             Status = copy_to_user(wrq->u.data.pointer, &pAdapter->EepromVersion, wrq->u.data.length);
4620             break;
4621         case RT_OID_802_11_QUERY_FIRMWARE_VERSION:
4622             wrq->u.data.length = sizeof(ULONG);
4623             Status = copy_to_user(wrq->u.data.pointer, &pAdapter->FirmwareVersion, wrq->u.data.length);
4624                         break;
4625             case RT_OID_802_11_QUERY_NOISE_LEVEL:
4626                         wrq->u.data.length = sizeof(UCHAR);
4627                         Status = copy_to_user(wrq->u.data.pointer, &pAdapter->BbpWriteLatch[66], wrq->u.data.length);
4628                         DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_802_11_QUERY_NOISE_LEVEL (=%d)\n", pAdapter->BbpWriteLatch[66]));
4629                         break;
4630             case RT_OID_802_11_EXTRA_INFO:
4631                         wrq->u.data.length = sizeof(ULONG);
4632                         Status = copy_to_user(wrq->u.data.pointer, &pAdapter->ExtraInfo, wrq->u.data.length);
4633                 DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_802_11_EXTRA_INFO (=%ld)\n", pAdapter->ExtraInfo));
4634                 break;
4635             case RT_OID_WE_VERSION_COMPILED:
4636                 wrq->u.data.length = sizeof(UINT);
4637                 we_version_compiled = WIRELESS_EXT;
4638                 Status = copy_to_user(wrq->u.data.pointer, &we_version_compiled, wrq->u.data.length);
4639                 break;
4640                 case RT_OID_802_11_QUERY_APSD_SETTING:
4641                         apsd = (pAdapter->CommonCfg.bAPSDCapable | (pAdapter->CommonCfg.bAPSDAC_BE << 1) | (pAdapter->CommonCfg.bAPSDAC_BK << 2)
4642                                 | (pAdapter->CommonCfg.bAPSDAC_VI << 3) | (pAdapter->CommonCfg.bAPSDAC_VO << 4) | (pAdapter->CommonCfg.MaxSPLength << 5));
4643
4644                         wrq->u.data.length = sizeof(ULONG);
4645                         Status = copy_to_user(wrq->u.data.pointer, &apsd, wrq->u.data.length);
4646                         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",
4647                                 apsd,pAdapter->CommonCfg.bAPSDCapable,pAdapter->CommonCfg.bAPSDAC_BE,pAdapter->CommonCfg.bAPSDAC_BK,pAdapter->CommonCfg.bAPSDAC_VI,pAdapter->CommonCfg.bAPSDAC_VO,pAdapter->CommonCfg.MaxSPLength));
4648                         break;
4649                 case RT_OID_802_11_QUERY_APSD_PSM:
4650                         wrq->u.data.length = sizeof(ULONG);
4651                         Status = copy_to_user(wrq->u.data.pointer, &pAdapter->CommonCfg.bAPSDForcePowerSave, wrq->u.data.length);
4652                         DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_802_11_QUERY_APSD_PSM (=%d)\n", pAdapter->CommonCfg.bAPSDForcePowerSave));
4653                         break;
4654                 case RT_OID_802_11_QUERY_WMM:
4655                         wrq->u.data.length = sizeof(BOOLEAN);
4656                         Status = copy_to_user(wrq->u.data.pointer, &pAdapter->CommonCfg.bWmmCapable, wrq->u.data.length);
4657                         DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_802_11_QUERY_WMM (=%d)\n",     pAdapter->CommonCfg.bWmmCapable));
4658                         break;
4659         case RT_OID_NEW_DRIVER:
4660             {
4661                 UCHAR enabled = 1;
4662                 wrq->u.data.length = sizeof(UCHAR);
4663                 Status = copy_to_user(wrq->u.data.pointer, &enabled, wrq->u.data.length);
4664                 DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_NEW_DRIVER (=%d)\n", enabled));
4665             }
4666                 break;
4667         case RT_OID_WPA_SUPPLICANT_SUPPORT:
4668                 wrq->u.data.length = sizeof(UCHAR);
4669                 Status = copy_to_user(wrq->u.data.pointer, &pAdapter->StaCfg.WpaSupplicantUP, wrq->u.data.length);
4670             DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_WPA_SUPPLICANT_SUPPORT (=%d)\n", pAdapter->StaCfg.WpaSupplicantUP));
4671                 break;
4672         case RT_OID_DRIVER_DEVICE_NAME:
4673             DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_DRIVER_DEVICE_NAME \n"));
4674                         wrq->u.data.length = 16;
4675                         if (copy_to_user(wrq->u.data.pointer, pAdapter->StaCfg.dev_name, wrq->u.data.length))
4676                         {
4677                                 Status = -EFAULT;
4678                         }
4679             break;
4680         case RT_OID_802_11_QUERY_HT_PHYMODE:
4681             pHTPhyMode = (OID_SET_HT_PHYMODE *) kmalloc(sizeof(OID_SET_HT_PHYMODE), MEM_ALLOC_FLAG);
4682             if (pHTPhyMode)
4683             {
4684                 pHTPhyMode->PhyMode = pAdapter->CommonCfg.PhyMode;
4685                         pHTPhyMode->HtMode = (UCHAR)pAdapter->MacTab.Content[BSSID_WCID].HTPhyMode.field.MODE;
4686                         pHTPhyMode->BW = (UCHAR)pAdapter->MacTab.Content[BSSID_WCID].HTPhyMode.field.BW;
4687                         pHTPhyMode->MCS= (UCHAR)pAdapter->MacTab.Content[BSSID_WCID].HTPhyMode.field.MCS;
4688                         pHTPhyMode->SHORTGI= (UCHAR)pAdapter->MacTab.Content[BSSID_WCID].HTPhyMode.field.ShortGI;
4689                         pHTPhyMode->STBC= (UCHAR)pAdapter->MacTab.Content[BSSID_WCID].HTPhyMode.field.STBC;
4690
4691                         pHTPhyMode->ExtOffset = ((pAdapter->CommonCfg.CentralChannel < pAdapter->CommonCfg.Channel) ? (EXTCHA_BELOW) : (EXTCHA_ABOVE));
4692                 wrq->u.data.length = sizeof(OID_SET_HT_PHYMODE);
4693                 if (copy_to_user(wrq->u.data.pointer, pHTPhyMode, wrq->u.data.length))
4694                         {
4695                                 Status = -EFAULT;
4696                         }
4697                         DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_802_11_QUERY_HT_PHYMODE (PhyMode = %d, MCS =%d, BW = %d, STBC = %d, ExtOffset=%d)\n",
4698                                 pHTPhyMode->HtMode, pHTPhyMode->MCS, pHTPhyMode->BW, pHTPhyMode->STBC, pHTPhyMode->ExtOffset));
4699                         DBGPRINT(RT_DEBUG_TRACE, (" MlmeUpdateTxRates (.word = %x )\n", pAdapter->MacTab.Content[BSSID_WCID].HTPhyMode.word));
4700             }
4701             else
4702             {
4703                 DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_802_11_STA_CONFIG(kmalloc failed)\n"));
4704                 Status = -EFAULT;
4705             }
4706             break;
4707         case RT_OID_802_11_COUNTRY_REGION:
4708             DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_802_11_COUNTRY_REGION \n"));
4709                         wrq->u.data.length = sizeof(ulInfo);
4710             ulInfo = pAdapter->CommonCfg.CountryRegionForABand;
4711             ulInfo = (ulInfo << 8)|(pAdapter->CommonCfg.CountryRegion);
4712                         if (copy_to_user(wrq->u.data.pointer, &ulInfo, wrq->u.data.length))
4713             {
4714                                 Status = -EFAULT;
4715             }
4716             break;
4717         case RT_OID_802_11_QUERY_DAT_HT_PHYMODE:
4718             pHTPhyMode = (OID_SET_HT_PHYMODE *) kmalloc(sizeof(OID_SET_HT_PHYMODE), MEM_ALLOC_FLAG);
4719             if (pHTPhyMode)
4720             {
4721                 pHTPhyMode->PhyMode = pAdapter->CommonCfg.PhyMode;
4722                         pHTPhyMode->HtMode = (UCHAR)pAdapter->CommonCfg.RegTransmitSetting.field.HTMODE;
4723                         pHTPhyMode->BW = (UCHAR)pAdapter->CommonCfg.RegTransmitSetting.field.BW;
4724                         pHTPhyMode->MCS= (UCHAR)pAdapter->StaCfg.DesiredTransmitSetting.field.MCS;
4725                         pHTPhyMode->SHORTGI= (UCHAR)pAdapter->CommonCfg.RegTransmitSetting.field.ShortGI;
4726                         pHTPhyMode->STBC= (UCHAR)pAdapter->CommonCfg.RegTransmitSetting.field.STBC;
4727
4728                 wrq->u.data.length = sizeof(OID_SET_HT_PHYMODE);
4729                 if (copy_to_user(wrq->u.data.pointer, pHTPhyMode, wrq->u.data.length))
4730                         {
4731                                 Status = -EFAULT;
4732                         }
4733                         DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_802_11_QUERY_HT_PHYMODE (PhyMode = %d, MCS =%d, BW = %d, STBC = %d, ExtOffset=%d)\n",
4734                                 pHTPhyMode->HtMode, pHTPhyMode->MCS, pHTPhyMode->BW, pHTPhyMode->STBC, pHTPhyMode->ExtOffset));
4735                         DBGPRINT(RT_DEBUG_TRACE, (" MlmeUpdateTxRates (.word = %x )\n", pAdapter->MacTab.Content[BSSID_WCID].HTPhyMode.word));
4736             }
4737             else
4738             {
4739                 DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_802_11_STA_CONFIG(kmalloc failed)\n"));
4740                 Status = -EFAULT;
4741             }
4742             break;
4743         case RT_OID_QUERY_MULTIPLE_CARD_SUPPORT:
4744                         wrq->u.data.length = sizeof(UCHAR);
4745             i = 0;
4746                         if (copy_to_user(wrq->u.data.pointer, &i, wrq->u.data.length))
4747             {
4748                                 Status = -EFAULT;
4749             }
4750             DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_QUERY_MULTIPLE_CARD_SUPPORT(=%d) \n", i));
4751             break;
4752
4753                 case OID_802_11_BUILD_CHANNEL_EX:
4754                         {
4755                                 UCHAR value;
4756                                 DBGPRINT(RT_DEBUG_TRACE, ("Query::OID_802_11_BUILD_CHANNEL_EX \n"));
4757                                 wrq->u.data.length = sizeof(UCHAR);
4758                                 DBGPRINT(RT_DEBUG_TRACE, ("Doesn't support EXT_BUILD_CHANNEL_LIST.\n"));
4759                                 value = 0;
4760                                 Status = copy_to_user(wrq->u.data.pointer, &value, 1);
4761                                 DBGPRINT(RT_DEBUG_TRACE, ("Status=%d\n", Status));
4762                         }
4763                         break;
4764
4765                 case OID_802_11_GET_CH_LIST:
4766                         {
4767                                 PRT_CHANNEL_LIST_INFO pChListBuf;
4768
4769                                 DBGPRINT(RT_DEBUG_TRACE, ("Query::OID_802_11_GET_CH_LIST \n"));
4770                                 if (pAdapter->ChannelListNum == 0)
4771                                 {
4772                                         wrq->u.data.length = 0;
4773                                         break;
4774                                 }
4775
4776                                 pChListBuf = (RT_CHANNEL_LIST_INFO *) kmalloc(sizeof(RT_CHANNEL_LIST_INFO), MEM_ALLOC_FLAG);
4777                                 if (pChListBuf == NULL)
4778                                 {
4779                                         wrq->u.data.length = 0;
4780                                         break;
4781                                 }
4782
4783                                 pChListBuf->ChannelListNum = pAdapter->ChannelListNum;
4784                                 for (i = 0; i < pChListBuf->ChannelListNum; i++)
4785                                         pChListBuf->ChannelList[i] = pAdapter->ChannelList[i].Channel;
4786
4787                                 wrq->u.data.length = sizeof(RT_CHANNEL_LIST_INFO);
4788                                 Status = copy_to_user(wrq->u.data.pointer, pChListBuf, sizeof(RT_CHANNEL_LIST_INFO));
4789                                 DBGPRINT(RT_DEBUG_TRACE, ("Status=%d\n", Status));
4790
4791                                 if (pChListBuf)
4792                                         kfree(pChListBuf);
4793                         }
4794                         break;
4795
4796                 case OID_802_11_GET_COUNTRY_CODE:
4797                         DBGPRINT(RT_DEBUG_TRACE, ("Query::OID_802_11_GET_COUNTRY_CODE \n"));
4798                         wrq->u.data.length = 2;
4799                         Status = copy_to_user(wrq->u.data.pointer, &pAdapter->CommonCfg.CountryCode, 2);
4800                         DBGPRINT(RT_DEBUG_TRACE, ("Status=%d\n", Status));
4801                         break;
4802
4803                 case OID_802_11_GET_CHANNEL_GEOGRAPHY:
4804                         DBGPRINT(RT_DEBUG_TRACE, ("Query::OID_802_11_GET_CHANNEL_GEOGRAPHY \n"));
4805                         wrq->u.data.length = 1;
4806                         Status = copy_to_user(wrq->u.data.pointer, &pAdapter->CommonCfg.Geography, 1);
4807                         DBGPRINT(RT_DEBUG_TRACE, ("Status=%d\n", Status));
4808                         break;
4809
4810         default:
4811             DBGPRINT(RT_DEBUG_TRACE, ("Query::unknown IOCTL's subcmd = 0x%08x\n", cmd));
4812             Status = -EOPNOTSUPP;
4813             break;
4814     }
4815     return Status;
4816 }
4817
4818 INT rt28xx_sta_ioctl(
4819         IN      struct net_device       *net_dev,
4820         IN      OUT     struct ifreq    *rq,
4821         IN      INT                                     cmd)
4822 {
4823         POS_COOKIE                      pObj;
4824         VIRTUAL_ADAPTER         *pVirtualAd = NULL;
4825         RTMP_ADAPTER        *pAd = NULL;
4826         struct iwreq        *wrq = (struct iwreq *) rq;
4827         BOOLEAN                         StateMachineTouched = FALSE;
4828         INT                                     Status = NDIS_STATUS_SUCCESS;
4829         USHORT                          subcmd;
4830
4831         if (net_dev->priv_flags == INT_MAIN)
4832         {
4833                 pAd = net_dev->ml_priv;
4834         }
4835         else
4836         {
4837                 pVirtualAd = net_dev->ml_priv;
4838                 pAd = pVirtualAd->RtmpDev->ml_priv;
4839         }
4840         pObj = (POS_COOKIE) pAd->OS_Cookie;
4841
4842         if (pAd == NULL)
4843         {
4844                 /* if 1st open fail, pAd will be free;
4845                    So the net_dev->ml_priv will be NULL in 2rd open */
4846                 return -ENETDOWN;
4847         }
4848
4849     //check if the interface is down
4850     if(!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_INTERRUPT_IN_USE))
4851     {
4852         {
4853             DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
4854                     return -ENETDOWN;
4855         }
4856     }
4857
4858         {       // determine this ioctl command is comming from which interface.
4859                 pObj->ioctl_if_type = INT_MAIN;
4860                 pObj->ioctl_if = MAIN_MBSSID;
4861         }
4862
4863         switch(cmd)
4864         {
4865         case SIOCGIFHWADDR:
4866                         DBGPRINT(RT_DEBUG_TRACE, ("IOCTL::SIOCGIFHWADDR\n"));
4867                         memcpy(wrq->u.name, pAd->CurrentAddress, ETH_ALEN);
4868                         break;
4869                 case SIOCGIWNAME:
4870         {
4871                 char *name=&wrq->u.name[0];
4872                 rt_ioctl_giwname(net_dev, NULL, name, NULL);
4873                         break;
4874                 }
4875                 case SIOCGIWESSID:  //Get ESSID
4876         {
4877                 struct iw_point *essid=&wrq->u.essid;
4878                 rt_ioctl_giwessid(net_dev, NULL, essid, essid->pointer);
4879                         break;
4880                 }
4881                 case SIOCSIWESSID:  //Set ESSID
4882         {
4883                 struct iw_point *essid=&wrq->u.essid;
4884                 rt_ioctl_siwessid(net_dev, NULL, essid, essid->pointer);
4885                         break;
4886                 }
4887                 case SIOCSIWNWID:   // set network id (the cell)
4888                 case SIOCGIWNWID:   // get network id
4889                         Status = -EOPNOTSUPP;
4890                         break;
4891                 case SIOCSIWFREQ:   //set channel/frequency (Hz)
4892         {
4893                 struct iw_freq *freq=&wrq->u.freq;
4894                 rt_ioctl_siwfreq(net_dev, NULL, freq, NULL);
4895                         break;
4896                 }
4897                 case SIOCGIWFREQ:   // get channel/frequency (Hz)
4898         {
4899                 struct iw_freq *freq=&wrq->u.freq;
4900                 rt_ioctl_giwfreq(net_dev, NULL, freq, NULL);
4901                         break;
4902                 }
4903                 case SIOCSIWNICKN: //set node name/nickname
4904         {
4905                 struct iw_point *data=&wrq->u.data;
4906                 rt_ioctl_siwnickn(net_dev, NULL, data, NULL);
4907                         break;
4908                 }
4909                 case SIOCGIWNICKN: //get node name/nickname
4910         {
4911                 struct iw_point *data=&wrq->u.data;
4912                 rt_ioctl_giwnickn(net_dev, NULL, data, NULL);
4913                         break;
4914                 }
4915                 case SIOCGIWRATE:   //get default bit rate (bps)
4916                     rt_ioctl_giwrate(net_dev, NULL, &wrq->u, NULL);
4917             break;
4918             case SIOCSIWRATE:  //set default bit rate (bps)
4919                 rt_ioctl_siwrate(net_dev, NULL, &wrq->u, NULL);
4920             break;
4921         case SIOCGIWRTS:  // get RTS/CTS threshold (bytes)
4922         {
4923                 struct iw_param *rts=&wrq->u.rts;
4924                 rt_ioctl_giwrts(net_dev, NULL, rts, NULL);
4925                         break;
4926                 }
4927         case SIOCSIWRTS:  //set RTS/CTS threshold (bytes)
4928         {
4929                 struct iw_param *rts=&wrq->u.rts;
4930                 rt_ioctl_siwrts(net_dev, NULL, rts, NULL);
4931                         break;
4932                 }
4933         case SIOCGIWFRAG:  //get fragmentation thr (bytes)
4934         {
4935                 struct iw_param *frag=&wrq->u.frag;
4936                 rt_ioctl_giwfrag(net_dev, NULL, frag, NULL);
4937                         break;
4938                 }
4939         case SIOCSIWFRAG:  //set fragmentation thr (bytes)
4940         {
4941                 struct iw_param *frag=&wrq->u.frag;
4942                 rt_ioctl_siwfrag(net_dev, NULL, frag, NULL);
4943                         break;
4944                 }
4945         case SIOCGIWENCODE:  //get encoding token & mode
4946         {
4947                 struct iw_point *erq=&wrq->u.encoding;
4948                 if(erq->pointer)
4949                         rt_ioctl_giwencode(net_dev, NULL, erq, erq->pointer);
4950                         break;
4951                 }
4952         case SIOCSIWENCODE:  //set encoding token & mode
4953         {
4954                 struct iw_point *erq=&wrq->u.encoding;
4955                 if(erq->pointer)
4956                         rt_ioctl_siwencode(net_dev, NULL, erq, erq->pointer);
4957                         break;
4958                 }
4959                 case SIOCGIWAP:     //get access point MAC addresses
4960         {
4961                 struct sockaddr *ap_addr=&wrq->u.ap_addr;
4962                 rt_ioctl_giwap(net_dev, NULL, ap_addr, ap_addr->sa_data);
4963                         break;
4964                 }
4965             case SIOCSIWAP:  //set access point MAC addresses
4966         {
4967                 struct sockaddr *ap_addr=&wrq->u.ap_addr;
4968                 rt_ioctl_siwap(net_dev, NULL, ap_addr, ap_addr->sa_data);
4969                         break;
4970                 }
4971                 case SIOCGIWMODE:   //get operation mode
4972         {
4973                 __u32 *mode=&wrq->u.mode;
4974                 rt_ioctl_giwmode(net_dev, NULL, mode, NULL);
4975                         break;
4976                 }
4977                 case SIOCSIWMODE:   //set operation mode
4978         {
4979                 __u32 *mode=&wrq->u.mode;
4980                 rt_ioctl_siwmode(net_dev, NULL, mode, NULL);
4981                         break;
4982                 }
4983                 case SIOCGIWSENS:   //get sensitivity (dBm)
4984                 case SIOCSIWSENS:       //set sensitivity (dBm)
4985                 case SIOCGIWPOWER:  //get Power Management settings
4986                 case SIOCSIWPOWER:  //set Power Management settings
4987                 case SIOCGIWTXPOW:  //get transmit power (dBm)
4988                 case SIOCSIWTXPOW:  //set transmit power (dBm)
4989                 case SIOCGIWRANGE:      //Get range of parameters
4990                 case SIOCGIWRETRY:      //get retry limits and lifetime
4991                 case SIOCSIWRETRY:      //set retry limits and lifetime
4992                         Status = -EOPNOTSUPP;
4993                         break;
4994                 case RT_PRIV_IOCTL:
4995         case RT_PRIV_IOCTL_EXT:
4996                         subcmd = wrq->u.data.flags;
4997                         if( subcmd & OID_GET_SET_TOGGLE)
4998                                 Status = RTMPSetInformation(pAd, rq, subcmd);
4999                         else
5000                                 Status = RTMPQueryInformation(pAd, rq, subcmd);
5001                         break;
5002                 case SIOCGIWPRIV:
5003                         if (wrq->u.data.pointer)
5004                         {
5005                                 if ( access_ok(VERIFY_WRITE, wrq->u.data.pointer, sizeof(privtab)) != TRUE)
5006                                         break;
5007                                 wrq->u.data.length = sizeof(privtab) / sizeof(privtab[0]);
5008                                 if (copy_to_user(wrq->u.data.pointer, privtab, sizeof(privtab)))
5009                                         Status = -EFAULT;
5010                         }
5011                         break;
5012                 case RTPRIV_IOCTL_SET:
5013                         if(access_ok(VERIFY_READ, wrq->u.data.pointer, wrq->u.data.length) != TRUE)
5014                                 break;
5015                         rt_ioctl_setparam(net_dev, NULL, NULL, wrq->u.data.pointer);
5016                         break;
5017                 case RTPRIV_IOCTL_GSITESURVEY:
5018                         RTMPIoctlGetSiteSurvey(pAd, wrq);
5019                     break;
5020 #ifdef DBG
5021                 case RTPRIV_IOCTL_MAC:
5022                         RTMPIoctlMAC(pAd, wrq);
5023                         break;
5024                 case RTPRIV_IOCTL_E2P:
5025                         RTMPIoctlE2PROM(pAd, wrq);
5026                         break;
5027 #ifdef RT30xx
5028                 case RTPRIV_IOCTL_RF:
5029                         RTMPIoctlRF(pAd, wrq);
5030                         break;
5031 #endif // RT30xx //
5032 #endif // DBG //
5033         case SIOCETHTOOL:
5034                 break;
5035                 default:
5036                         DBGPRINT(RT_DEBUG_ERROR, ("IOCTL::unknown IOCTL's cmd = 0x%08x\n", cmd));
5037                         Status = -EOPNOTSUPP;
5038                         break;
5039         }
5040
5041     if(StateMachineTouched) // Upper layer sent a MLME-related operations
5042         RT28XX_MLME_HANDLER(pAd);
5043
5044         return Status;
5045 }
5046
5047 /*
5048     ==========================================================================
5049     Description:
5050         Set SSID
5051     Return:
5052         TRUE if all parameters are OK, FALSE otherwise
5053     ==========================================================================
5054 */
5055 INT Set_SSID_Proc(
5056     IN  PRTMP_ADAPTER   pAdapter,
5057     IN  PUCHAR          arg)
5058 {
5059     NDIS_802_11_SSID                    Ssid, *pSsid=NULL;
5060     BOOLEAN                             StateMachineTouched = FALSE;
5061     int                                 success = TRUE;
5062
5063     if( strlen(arg) <= MAX_LEN_OF_SSID)
5064     {
5065         NdisZeroMemory(&Ssid, sizeof(NDIS_802_11_SSID));
5066         if (strlen(arg) != 0)
5067         {
5068             NdisMoveMemory(Ssid.Ssid, arg, strlen(arg));
5069             Ssid.SsidLength = strlen(arg);
5070         }
5071         else   //ANY ssid
5072         {
5073             Ssid.SsidLength = 0;
5074                     memcpy(Ssid.Ssid, "", 0);
5075                         pAdapter->StaCfg.BssType = BSS_INFRA;
5076                         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeOpen;
5077                 pAdapter->StaCfg.WepStatus  = Ndis802_11EncryptionDisabled;
5078                 }
5079         pSsid = &Ssid;
5080
5081         if (pAdapter->Mlme.CntlMachine.CurrState != CNTL_IDLE)
5082         {
5083             RT28XX_MLME_RESET_STATE_MACHINE(pAdapter);
5084             DBGPRINT(RT_DEBUG_TRACE, ("!!! MLME busy, reset MLME state machine !!!\n"));
5085         }
5086
5087         pAdapter->MlmeAux.CurrReqIsFromNdis = TRUE;
5088         pAdapter->StaCfg.bScanReqIsFromWebUI = FALSE;
5089                 pAdapter->bConfigChanged = TRUE;
5090
5091         MlmeEnqueue(pAdapter,
5092                     MLME_CNTL_STATE_MACHINE,
5093                     OID_802_11_SSID,
5094                     sizeof(NDIS_802_11_SSID),
5095                     (VOID *)pSsid);
5096
5097         StateMachineTouched = TRUE;
5098         DBGPRINT(RT_DEBUG_TRACE, ("Set_SSID_Proc::(Len=%d,Ssid=%s)\n", Ssid.SsidLength, Ssid.Ssid));
5099     }
5100     else
5101         success = FALSE;
5102
5103     if (StateMachineTouched) // Upper layer sent a MLME-related operations
5104         RT28XX_MLME_HANDLER(pAdapter);
5105
5106     return success;
5107 }
5108
5109 #ifdef WMM_SUPPORT
5110 /*
5111     ==========================================================================
5112     Description:
5113         Set WmmCapable Enable or Disable
5114     Return:
5115         TRUE if all parameters are OK, FALSE otherwise
5116     ==========================================================================
5117 */
5118 INT     Set_WmmCapable_Proc(
5119         IN      PRTMP_ADAPTER   pAd,
5120         IN      PUCHAR                  arg)
5121 {
5122         BOOLEAN bWmmCapable;
5123
5124         bWmmCapable = simple_strtol(arg, 0, 10);
5125
5126         if ((bWmmCapable == 1)
5127 #ifdef RT2870
5128                 && (pAd->NumberOfPipes >= 5)
5129 #endif // RT2870 //
5130                 )
5131                 pAd->CommonCfg.bWmmCapable = TRUE;
5132         else if (bWmmCapable == 0)
5133                 pAd->CommonCfg.bWmmCapable = FALSE;
5134         else
5135                 return FALSE;  //Invalid argument
5136
5137         DBGPRINT(RT_DEBUG_TRACE, ("Set_WmmCapable_Proc::(bWmmCapable=%d)\n",
5138                 pAd->CommonCfg.bWmmCapable));
5139
5140         return TRUE;
5141 }
5142 #endif // WMM_SUPPORT //
5143
5144 /*
5145     ==========================================================================
5146     Description:
5147         Set Network Type(Infrastructure/Adhoc mode)
5148     Return:
5149         TRUE if all parameters are OK, FALSE otherwise
5150     ==========================================================================
5151 */
5152 INT Set_NetworkType_Proc(
5153     IN  PRTMP_ADAPTER   pAdapter,
5154     IN  PUCHAR          arg)
5155 {
5156     UINT32      Value = 0;
5157
5158     if (strcmp(arg, "Adhoc") == 0)
5159         {
5160                 if (pAdapter->StaCfg.BssType != BSS_ADHOC)
5161                 {
5162                         // Config has changed
5163                         pAdapter->bConfigChanged = TRUE;
5164             if (MONITOR_ON(pAdapter))
5165             {
5166                 RTMP_IO_WRITE32(pAdapter, RX_FILTR_CFG, STANORMAL);
5167                 RTMP_IO_READ32(pAdapter, MAC_SYS_CTRL, &Value);
5168                                 Value &= (~0x80);
5169                                 RTMP_IO_WRITE32(pAdapter, MAC_SYS_CTRL, Value);
5170                 OPSTATUS_CLEAR_FLAG(pAdapter, fOP_STATUS_MEDIA_STATE_CONNECTED);
5171                 pAdapter->StaCfg.bAutoReconnect = TRUE;
5172                 LinkDown(pAdapter, FALSE);
5173             }
5174                         if (INFRA_ON(pAdapter))
5175                         {
5176                                 //BOOLEAN Cancelled;
5177                                 // Set the AutoReconnectSsid to prevent it reconnect to old SSID
5178                                 // Since calling this indicate user don't want to connect to that SSID anymore.
5179                                 pAdapter->MlmeAux.AutoReconnectSsidLen= 32;
5180                                 NdisZeroMemory(pAdapter->MlmeAux.AutoReconnectSsid, pAdapter->MlmeAux.AutoReconnectSsidLen);
5181
5182                                 LinkDown(pAdapter, FALSE);
5183
5184                                 DBGPRINT(RT_DEBUG_TRACE, ("NDIS_STATUS_MEDIA_DISCONNECT Event BB!\n"));
5185                         }
5186                 }
5187                 pAdapter->StaCfg.BssType = BSS_ADHOC;
5188         pAdapter->net_dev->type = pAdapter->StaCfg.OriDevType;
5189                 DBGPRINT(RT_DEBUG_TRACE, ("===>Set_NetworkType_Proc::(AD-HOC)\n"));
5190         }
5191     else if (strcmp(arg, "Infra") == 0)
5192         {
5193                 if (pAdapter->StaCfg.BssType != BSS_INFRA)
5194                 {
5195                         // Config has changed
5196                         pAdapter->bConfigChanged = TRUE;
5197             if (MONITOR_ON(pAdapter))
5198             {
5199                 RTMP_IO_WRITE32(pAdapter, RX_FILTR_CFG, STANORMAL);
5200                 RTMP_IO_READ32(pAdapter, MAC_SYS_CTRL, &Value);
5201                                 Value &= (~0x80);
5202                                 RTMP_IO_WRITE32(pAdapter, MAC_SYS_CTRL, Value);
5203                 OPSTATUS_CLEAR_FLAG(pAdapter, fOP_STATUS_MEDIA_STATE_CONNECTED);
5204                 pAdapter->StaCfg.bAutoReconnect = TRUE;
5205                 LinkDown(pAdapter, FALSE);
5206             }
5207                         if (ADHOC_ON(pAdapter))
5208                         {
5209                                 // Set the AutoReconnectSsid to prevent it reconnect to old SSID
5210                                 // Since calling this indicate user don't want to connect to that SSID anymore.
5211                                 pAdapter->MlmeAux.AutoReconnectSsidLen= 32;
5212                                 NdisZeroMemory(pAdapter->MlmeAux.AutoReconnectSsid, pAdapter->MlmeAux.AutoReconnectSsidLen);
5213
5214                                 LinkDown(pAdapter, FALSE);
5215                         }
5216                 }
5217                 pAdapter->StaCfg.BssType = BSS_INFRA;
5218         pAdapter->net_dev->type = pAdapter->StaCfg.OriDevType;
5219                 DBGPRINT(RT_DEBUG_TRACE, ("===>Set_NetworkType_Proc::(INFRA)\n"));
5220
5221         pAdapter->StaCfg.BssType = BSS_INFRA;
5222         }
5223     else if (strcmp(arg, "Monitor") == 0)
5224     {
5225                 UCHAR   bbpValue = 0;
5226                 BCN_TIME_CFG_STRUC csr;
5227                 OPSTATUS_CLEAR_FLAG(pAdapter, fOP_STATUS_INFRA_ON);
5228         OPSTATUS_CLEAR_FLAG(pAdapter, fOP_STATUS_ADHOC_ON);
5229                 OPSTATUS_SET_FLAG(pAdapter, fOP_STATUS_MEDIA_STATE_CONNECTED);
5230                 // disable all periodic state machine
5231                 pAdapter->StaCfg.bAutoReconnect = FALSE;
5232                 // reset all mlme state machine
5233                 RT28XX_MLME_RESET_STATE_MACHINE(pAdapter);
5234                 DBGPRINT(RT_DEBUG_TRACE, ("fOP_STATUS_MEDIA_STATE_CONNECTED \n"));
5235         if (pAdapter->CommonCfg.CentralChannel == 0)
5236         {
5237             if (pAdapter->CommonCfg.PhyMode == PHY_11AN_MIXED)
5238                 pAdapter->CommonCfg.CentralChannel = 36;
5239             else
5240                 pAdapter->CommonCfg.CentralChannel = 6;
5241         }
5242         else
5243             N_ChannelCheck(pAdapter);
5244
5245         if (pAdapter->CommonCfg.PhyMode >= PHY_11ABGN_MIXED &&
5246             pAdapter->CommonCfg.RegTransmitSetting.field.BW == BW_40 &&
5247             pAdapter->CommonCfg.RegTransmitSetting.field.EXTCHA == EXTCHA_ABOVE)
5248                 {
5249                         // 40MHz ,control channel at lower
5250                         RTMP_BBP_IO_READ8_BY_REG_ID(pAdapter, BBP_R4, &bbpValue);
5251                         bbpValue &= (~0x18);
5252                         bbpValue |= 0x10;
5253                         RTMP_BBP_IO_WRITE8_BY_REG_ID(pAdapter, BBP_R4, bbpValue);
5254                         pAdapter->CommonCfg.BBPCurrentBW = BW_40;
5255                         //  RX : control channel at lower
5256                         RTMP_BBP_IO_READ8_BY_REG_ID(pAdapter, BBP_R3, &bbpValue);
5257                         bbpValue &= (~0x20);
5258                         RTMP_BBP_IO_WRITE8_BY_REG_ID(pAdapter, BBP_R3, bbpValue);
5259
5260                         RTMP_IO_READ32(pAdapter, TX_BAND_CFG, &Value);
5261                         Value &= 0xfffffffe;
5262                         RTMP_IO_WRITE32(pAdapter, TX_BAND_CFG, Value);
5263                         pAdapter->CommonCfg.CentralChannel = pAdapter->CommonCfg.Channel + 2;
5264             AsicSwitchChannel(pAdapter, pAdapter->CommonCfg.CentralChannel, FALSE);
5265                     AsicLockChannel(pAdapter, pAdapter->CommonCfg.CentralChannel);
5266             DBGPRINT(RT_DEBUG_TRACE, ("BW_40 ,control_channel(%d), CentralChannel(%d) \n",
5267                                        pAdapter->CommonCfg.Channel,
5268                                        pAdapter->CommonCfg.CentralChannel));
5269                 }
5270                 else if (pAdapter->CommonCfg.PhyMode >= PHY_11ABGN_MIXED &&
5271                  pAdapter->CommonCfg.RegTransmitSetting.field.BW == BW_40 &&
5272                  pAdapter->CommonCfg.RegTransmitSetting.field.EXTCHA == EXTCHA_BELOW)
5273                 {
5274                         // 40MHz ,control channel at upper
5275                         RTMP_BBP_IO_READ8_BY_REG_ID(pAdapter, BBP_R4, &bbpValue);
5276                         bbpValue &= (~0x18);
5277                         bbpValue |= 0x10;
5278                         RTMP_BBP_IO_WRITE8_BY_REG_ID(pAdapter, BBP_R4, bbpValue);
5279                         pAdapter->CommonCfg.BBPCurrentBW = BW_40;
5280                         RTMP_IO_READ32(pAdapter, TX_BAND_CFG, &Value);
5281                         Value |= 0x1;
5282                         RTMP_IO_WRITE32(pAdapter, TX_BAND_CFG, Value);
5283
5284                         RTMP_BBP_IO_READ8_BY_REG_ID(pAdapter, BBP_R3, &bbpValue);
5285                         bbpValue |= (0x20);
5286                         RTMP_BBP_IO_WRITE8_BY_REG_ID(pAdapter, BBP_R3, bbpValue);
5287                         pAdapter->CommonCfg.CentralChannel = pAdapter->CommonCfg.Channel - 2;
5288             AsicSwitchChannel(pAdapter, pAdapter->CommonCfg.CentralChannel, FALSE);
5289                     AsicLockChannel(pAdapter, pAdapter->CommonCfg.CentralChannel);
5290             DBGPRINT(RT_DEBUG_TRACE, ("BW_40 ,control_channel(%d), CentralChannel(%d) \n",
5291                                        pAdapter->CommonCfg.Channel,
5292                                        pAdapter->CommonCfg.CentralChannel));
5293                 }
5294                 else
5295                 {
5296                         // 20MHz
5297                         RTMP_BBP_IO_READ8_BY_REG_ID(pAdapter, BBP_R4, &bbpValue);
5298                         bbpValue &= (~0x18);
5299                         RTMP_BBP_IO_WRITE8_BY_REG_ID(pAdapter, BBP_R4, bbpValue);
5300                         pAdapter->CommonCfg.BBPCurrentBW = BW_20;
5301                         AsicSwitchChannel(pAdapter, pAdapter->CommonCfg.Channel, FALSE);
5302                         AsicLockChannel(pAdapter, pAdapter->CommonCfg.Channel);
5303                         DBGPRINT(RT_DEBUG_TRACE, ("BW_20, Channel(%d)\n", pAdapter->CommonCfg.Channel));
5304                 }
5305                 // Enable Rx with promiscuous reception
5306                 RTMP_IO_WRITE32(pAdapter, RX_FILTR_CFG, 0x3);
5307                 // ASIC supporsts sniffer function with replacing RSSI with timestamp.
5308                 //RTMP_IO_READ32(pAdapter, MAC_SYS_CTRL, &Value);
5309                 //Value |= (0x80);
5310                 //RTMP_IO_WRITE32(pAdapter, MAC_SYS_CTRL, Value);
5311                 // disable sync
5312                 RTMP_IO_READ32(pAdapter, BCN_TIME_CFG, &csr.word);
5313                 csr.field.bBeaconGen = 0;
5314                 csr.field.bTBTTEnable = 0;
5315                 csr.field.TsfSyncMode = 0;
5316                 RTMP_IO_WRITE32(pAdapter, BCN_TIME_CFG, csr.word);
5317
5318                 pAdapter->StaCfg.BssType = BSS_MONITOR;
5319         pAdapter->net_dev->type = ARPHRD_IEEE80211_PRISM; //ARPHRD_IEEE80211; // IEEE80211
5320                 DBGPRINT(RT_DEBUG_TRACE, ("===>Set_NetworkType_Proc::(MONITOR)\n"));
5321     }
5322
5323     // Reset Ralink supplicant to not use, it will be set to start when UI set PMK key
5324     pAdapter->StaCfg.WpaState = SS_NOTUSE;
5325
5326     DBGPRINT(RT_DEBUG_TRACE, ("Set_NetworkType_Proc::(NetworkType=%d)\n", pAdapter->StaCfg.BssType));
5327
5328     return TRUE;
5329 }
5330
5331 /*
5332     ==========================================================================
5333     Description:
5334         Set Authentication mode
5335     Return:
5336         TRUE if all parameters are OK, FALSE otherwise
5337     ==========================================================================
5338 */
5339 INT Set_AuthMode_Proc(
5340     IN  PRTMP_ADAPTER   pAdapter,
5341     IN  PUCHAR          arg)
5342 {
5343     if ((strcmp(arg, "WEPAUTO") == 0) || (strcmp(arg, "wepauto") == 0))
5344         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeAutoSwitch;
5345     else if ((strcmp(arg, "OPEN") == 0) || (strcmp(arg, "open") == 0))
5346         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeOpen;
5347     else if ((strcmp(arg, "SHARED") == 0) || (strcmp(arg, "shared") == 0))
5348         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeShared;
5349     else if ((strcmp(arg, "WPAPSK") == 0) || (strcmp(arg, "wpapsk") == 0))
5350         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeWPAPSK;
5351     else if ((strcmp(arg, "WPANONE") == 0) || (strcmp(arg, "wpanone") == 0))
5352         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeWPANone;
5353     else if ((strcmp(arg, "WPA2PSK") == 0) || (strcmp(arg, "wpa2psk") == 0))
5354         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeWPA2PSK;
5355     else if ((strcmp(arg, "WPA") == 0) || (strcmp(arg, "wpa") == 0))
5356         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeWPA;
5357     else if ((strcmp(arg, "WPA2") == 0) || (strcmp(arg, "wpa2") == 0))
5358         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeWPA2;
5359     else
5360         return FALSE;
5361
5362     pAdapter->StaCfg.PortSecured = WPA_802_1X_PORT_NOT_SECURED;
5363
5364     DBGPRINT(RT_DEBUG_TRACE, ("Set_AuthMode_Proc::(AuthMode=%d)\n", pAdapter->StaCfg.AuthMode));
5365
5366     return TRUE;
5367 }
5368
5369 /*
5370     ==========================================================================
5371     Description:
5372         Set Encryption Type
5373     Return:
5374         TRUE if all parameters are OK, FALSE otherwise
5375     ==========================================================================
5376 */
5377 INT Set_EncrypType_Proc(
5378     IN  PRTMP_ADAPTER   pAdapter,
5379     IN  PUCHAR          arg)
5380 {
5381     if ((strcmp(arg, "NONE") == 0) || (strcmp(arg, "none") == 0))
5382     {
5383         if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
5384             return TRUE;    // do nothing
5385
5386         pAdapter->StaCfg.WepStatus     = Ndis802_11WEPDisabled;
5387         pAdapter->StaCfg.PairCipher    = Ndis802_11WEPDisabled;
5388             pAdapter->StaCfg.GroupCipher   = Ndis802_11WEPDisabled;
5389     }
5390     else if ((strcmp(arg, "WEP") == 0) || (strcmp(arg, "wep") == 0))
5391     {
5392         if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
5393             return TRUE;    // do nothing
5394
5395         pAdapter->StaCfg.WepStatus     = Ndis802_11WEPEnabled;
5396         pAdapter->StaCfg.PairCipher    = Ndis802_11WEPEnabled;
5397             pAdapter->StaCfg.GroupCipher   = Ndis802_11WEPEnabled;
5398     }
5399     else if ((strcmp(arg, "TKIP") == 0) || (strcmp(arg, "tkip") == 0))
5400     {
5401         if (pAdapter->StaCfg.AuthMode < Ndis802_11AuthModeWPA)
5402             return TRUE;    // do nothing
5403
5404         pAdapter->StaCfg.WepStatus     = Ndis802_11Encryption2Enabled;
5405         pAdapter->StaCfg.PairCipher    = Ndis802_11Encryption2Enabled;
5406             pAdapter->StaCfg.GroupCipher   = Ndis802_11Encryption2Enabled;
5407     }
5408     else if ((strcmp(arg, "AES") == 0) || (strcmp(arg, "aes") == 0))
5409     {
5410         if (pAdapter->StaCfg.AuthMode < Ndis802_11AuthModeWPA)
5411             return TRUE;    // do nothing
5412
5413         pAdapter->StaCfg.WepStatus     = Ndis802_11Encryption3Enabled;
5414         pAdapter->StaCfg.PairCipher    = Ndis802_11Encryption3Enabled;
5415             pAdapter->StaCfg.GroupCipher   = Ndis802_11Encryption3Enabled;
5416     }
5417     else
5418         return FALSE;
5419
5420     pAdapter->StaCfg.OrigWepStatus = pAdapter->StaCfg.WepStatus;
5421
5422     DBGPRINT(RT_DEBUG_TRACE, ("Set_EncrypType_Proc::(EncrypType=%d)\n", pAdapter->StaCfg.WepStatus));
5423
5424     return TRUE;
5425 }
5426
5427 /*
5428     ==========================================================================
5429     Description:
5430         Set Default Key ID
5431     Return:
5432         TRUE if all parameters are OK, FALSE otherwise
5433     ==========================================================================
5434 */
5435 INT Set_DefaultKeyID_Proc(
5436     IN  PRTMP_ADAPTER   pAdapter,
5437     IN  PUCHAR          arg)
5438 {
5439     ULONG                               KeyIdx;
5440
5441     KeyIdx = simple_strtol(arg, 0, 10);
5442     if((KeyIdx >= 1 ) && (KeyIdx <= 4))
5443         pAdapter->StaCfg.DefaultKeyId = (UCHAR) (KeyIdx - 1 );
5444     else
5445         return FALSE;  //Invalid argument
5446
5447     DBGPRINT(RT_DEBUG_TRACE, ("Set_DefaultKeyID_Proc::(DefaultKeyID=%d)\n", pAdapter->StaCfg.DefaultKeyId));
5448
5449     return TRUE;
5450 }
5451
5452 /*
5453     ==========================================================================
5454     Description:
5455         Set WEP KEY1
5456     Return:
5457         TRUE if all parameters are OK, FALSE otherwise
5458     ==========================================================================
5459 */
5460 INT Set_Key1_Proc(
5461     IN  PRTMP_ADAPTER   pAdapter,
5462     IN  PUCHAR          arg)
5463 {
5464     int                                 KeyLen;
5465     int                                 i;
5466     UCHAR                               CipherAlg=CIPHER_WEP64;
5467
5468     if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
5469         return TRUE;    // do nothing
5470
5471     KeyLen = strlen(arg);
5472
5473     switch (KeyLen)
5474     {
5475         case 5: //wep 40 Ascii type
5476             pAdapter->SharedKey[BSS0][0].KeyLen = KeyLen;
5477             memcpy(pAdapter->SharedKey[BSS0][0].Key, arg, KeyLen);
5478             CipherAlg = CIPHER_WEP64;
5479             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key1_Proc::(Key1=%s and type=%s)\n", arg, "Ascii"));
5480             break;
5481         case 10: //wep 40 Hex type
5482             for(i=0; i < KeyLen; i++)
5483             {
5484                 if( !isxdigit(*(arg+i)) )
5485                     return FALSE;  //Not Hex value;
5486             }
5487             pAdapter->SharedKey[BSS0][0].KeyLen = KeyLen / 2 ;
5488             AtoH(arg, pAdapter->SharedKey[BSS0][0].Key, KeyLen / 2);
5489             CipherAlg = CIPHER_WEP64;
5490             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key1_Proc::(Key1=%s and type=%s)\n", arg, "Hex"));
5491             break;
5492         case 13: //wep 104 Ascii type
5493             pAdapter->SharedKey[BSS0][0].KeyLen = KeyLen;
5494             memcpy(pAdapter->SharedKey[BSS0][0].Key, arg, KeyLen);
5495             CipherAlg = CIPHER_WEP128;
5496             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key1_Proc::(Key1=%s and type=%s)\n", arg, "Ascii"));
5497             break;
5498         case 26: //wep 104 Hex type
5499             for(i=0; i < KeyLen; i++)
5500             {
5501                 if( !isxdigit(*(arg+i)) )
5502                     return FALSE;  //Not Hex value;
5503             }
5504             pAdapter->SharedKey[BSS0][0].KeyLen = KeyLen / 2 ;
5505             AtoH(arg, pAdapter->SharedKey[BSS0][0].Key, KeyLen / 2);
5506             CipherAlg = CIPHER_WEP128;
5507             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key1_Proc::(Key1=%s and type=%s)\n", arg, "Hex"));
5508             break;
5509         default: //Invalid argument
5510             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key1_Proc::Invalid argument (=%s)\n", arg));
5511             return FALSE;
5512     }
5513
5514     pAdapter->SharedKey[BSS0][0].CipherAlg = CipherAlg;
5515
5516     // Set keys (into ASIC)
5517     if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
5518         ;   // not support
5519     else    // Old WEP stuff
5520     {
5521         AsicAddSharedKeyEntry(pAdapter,
5522                               0,
5523                               0,
5524                               pAdapter->SharedKey[BSS0][0].CipherAlg,
5525                               pAdapter->SharedKey[BSS0][0].Key,
5526                               NULL,
5527                               NULL);
5528     }
5529
5530     return TRUE;
5531 }
5532 /*
5533     ==========================================================================
5534
5535     Description:
5536         Set WEP KEY2
5537     Return:
5538         TRUE if all parameters are OK, FALSE otherwise
5539     ==========================================================================
5540 */
5541 INT Set_Key2_Proc(
5542     IN  PRTMP_ADAPTER   pAdapter,
5543     IN  PUCHAR          arg)
5544 {
5545     int                                 KeyLen;
5546     int                                 i;
5547     UCHAR                               CipherAlg=CIPHER_WEP64;
5548
5549     if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
5550         return TRUE;    // do nothing
5551
5552     KeyLen = strlen(arg);
5553
5554     switch (KeyLen)
5555     {
5556         case 5: //wep 40 Ascii type
5557             pAdapter->SharedKey[BSS0][1].KeyLen = KeyLen;
5558             memcpy(pAdapter->SharedKey[BSS0][1].Key, arg, KeyLen);
5559             CipherAlg = CIPHER_WEP64;
5560             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key2_Proc::(Key2=%s and type=%s)\n", arg, "Ascii"));
5561             break;
5562         case 10: //wep 40 Hex type
5563             for(i=0; i < KeyLen; i++)
5564             {
5565                 if( !isxdigit(*(arg+i)) )
5566                     return FALSE;  //Not Hex value;
5567             }
5568             pAdapter->SharedKey[BSS0][1].KeyLen = KeyLen / 2 ;
5569             AtoH(arg, pAdapter->SharedKey[BSS0][1].Key, KeyLen / 2);
5570             CipherAlg = CIPHER_WEP64;
5571             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key2_Proc::(Key2=%s and type=%s)\n", arg, "Hex"));
5572             break;
5573         case 13: //wep 104 Ascii type
5574             pAdapter->SharedKey[BSS0][1].KeyLen = KeyLen;
5575             memcpy(pAdapter->SharedKey[BSS0][1].Key, arg, KeyLen);
5576             CipherAlg = CIPHER_WEP128;
5577             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key2_Proc::(Key2=%s and type=%s)\n", arg, "Ascii"));
5578             break;
5579         case 26: //wep 104 Hex type
5580             for(i=0; i < KeyLen; i++)
5581             {
5582                 if( !isxdigit(*(arg+i)) )
5583                     return FALSE;  //Not Hex value;
5584             }
5585             pAdapter->SharedKey[BSS0][1].KeyLen = KeyLen / 2 ;
5586             AtoH(arg, pAdapter->SharedKey[BSS0][1].Key, KeyLen / 2);
5587             CipherAlg = CIPHER_WEP128;
5588             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key2_Proc::(Key2=%s and type=%s)\n", arg, "Hex"));
5589             break;
5590         default: //Invalid argument
5591             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key2_Proc::Invalid argument (=%s)\n", arg));
5592             return FALSE;
5593     }
5594     pAdapter->SharedKey[BSS0][1].CipherAlg = CipherAlg;
5595
5596     // Set keys (into ASIC)
5597     if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
5598         ;   // not support
5599     else    // Old WEP stuff
5600     {
5601         AsicAddSharedKeyEntry(pAdapter,
5602                               0,
5603                               1,
5604                               pAdapter->SharedKey[BSS0][1].CipherAlg,
5605                               pAdapter->SharedKey[BSS0][1].Key,
5606                               NULL,
5607                               NULL);
5608     }
5609
5610     return TRUE;
5611 }
5612 /*
5613     ==========================================================================
5614     Description:
5615         Set WEP KEY3
5616     Return:
5617         TRUE if all parameters are OK, FALSE otherwise
5618     ==========================================================================
5619 */
5620 INT Set_Key3_Proc(
5621     IN  PRTMP_ADAPTER   pAdapter,
5622     IN  PUCHAR          arg)
5623 {
5624     int                                 KeyLen;
5625     int                                 i;
5626     UCHAR                               CipherAlg=CIPHER_WEP64;
5627
5628     if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
5629         return TRUE;    // do nothing
5630
5631     KeyLen = strlen(arg);
5632
5633     switch (KeyLen)
5634     {
5635         case 5: //wep 40 Ascii type
5636             pAdapter->SharedKey[BSS0][2].KeyLen = KeyLen;
5637             memcpy(pAdapter->SharedKey[BSS0][2].Key, arg, KeyLen);
5638             CipherAlg = CIPHER_WEP64;
5639             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key3_Proc::(Key3=%s and type=Ascii)\n", arg));
5640             break;
5641         case 10: //wep 40 Hex type
5642             for(i=0; i < KeyLen; i++)
5643             {
5644                 if( !isxdigit(*(arg+i)) )
5645                     return FALSE;  //Not Hex value;
5646             }
5647             pAdapter->SharedKey[BSS0][2].KeyLen = KeyLen / 2 ;
5648             AtoH(arg, pAdapter->SharedKey[BSS0][2].Key, KeyLen / 2);
5649             CipherAlg = CIPHER_WEP64;
5650             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key3_Proc::(Key3=%s and type=Hex)\n", arg));
5651             break;
5652         case 13: //wep 104 Ascii type
5653             pAdapter->SharedKey[BSS0][2].KeyLen = KeyLen;
5654             memcpy(pAdapter->SharedKey[BSS0][2].Key, arg, KeyLen);
5655             CipherAlg = CIPHER_WEP128;
5656             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key3_Proc::(Key3=%s and type=Ascii)\n", arg));
5657             break;
5658         case 26: //wep 104 Hex type
5659             for(i=0; i < KeyLen; i++)
5660             {
5661                 if( !isxdigit(*(arg+i)) )
5662                     return FALSE;  //Not Hex value;
5663             }
5664             pAdapter->SharedKey[BSS0][2].KeyLen = KeyLen / 2 ;
5665             AtoH(arg, pAdapter->SharedKey[BSS0][2].Key, KeyLen / 2);
5666             CipherAlg = CIPHER_WEP128;
5667             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key3_Proc::(Key3=%s and type=Hex)\n", arg));
5668             break;
5669         default: //Invalid argument
5670             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key3_Proc::Invalid argument (=%s)\n", arg));
5671             return FALSE;
5672     }
5673     pAdapter->SharedKey[BSS0][2].CipherAlg = CipherAlg;
5674
5675     // Set keys (into ASIC)
5676     if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
5677         ;   // not support
5678     else    // Old WEP stuff
5679     {
5680         AsicAddSharedKeyEntry(pAdapter,
5681                               0,
5682                               2,
5683                               pAdapter->SharedKey[BSS0][2].CipherAlg,
5684                               pAdapter->SharedKey[BSS0][2].Key,
5685                               NULL,
5686                               NULL);
5687     }
5688
5689     return TRUE;
5690 }
5691 /*
5692     ==========================================================================
5693     Description:
5694         Set WEP KEY4
5695     Return:
5696         TRUE if all parameters are OK, FALSE otherwise
5697     ==========================================================================
5698 */
5699 INT Set_Key4_Proc(
5700     IN  PRTMP_ADAPTER   pAdapter,
5701     IN  PUCHAR          arg)
5702 {
5703     int                                 KeyLen;
5704     int                                 i;
5705     UCHAR                               CipherAlg=CIPHER_WEP64;
5706
5707     if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
5708         return TRUE;    // do nothing
5709
5710     KeyLen = strlen(arg);
5711
5712     switch (KeyLen)
5713     {
5714         case 5: //wep 40 Ascii type
5715             pAdapter->SharedKey[BSS0][3].KeyLen = KeyLen;
5716             memcpy(pAdapter->SharedKey[BSS0][3].Key, arg, KeyLen);
5717             CipherAlg = CIPHER_WEP64;
5718             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key4_Proc::(Key4=%s and type=%s)\n", arg, "Ascii"));
5719             break;
5720         case 10: //wep 40 Hex type
5721             for(i=0; i < KeyLen; i++)
5722             {
5723                 if( !isxdigit(*(arg+i)) )
5724                     return FALSE;  //Not Hex value;
5725             }
5726             pAdapter->SharedKey[BSS0][3].KeyLen = KeyLen / 2 ;
5727             AtoH(arg, pAdapter->SharedKey[BSS0][3].Key, KeyLen / 2);
5728             CipherAlg = CIPHER_WEP64;
5729             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key4_Proc::(Key4=%s and type=%s)\n", arg, "Hex"));
5730             break;
5731         case 13: //wep 104 Ascii type
5732             pAdapter->SharedKey[BSS0][3].KeyLen = KeyLen;
5733             memcpy(pAdapter->SharedKey[BSS0][3].Key, arg, KeyLen);
5734             CipherAlg = CIPHER_WEP128;
5735             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key4_Proc::(Key4=%s and type=%s)\n", arg, "Ascii"));
5736             break;
5737         case 26: //wep 104 Hex type
5738             for(i=0; i < KeyLen; i++)
5739             {
5740                 if( !isxdigit(*(arg+i)) )
5741                     return FALSE;  //Not Hex value;
5742             }
5743             pAdapter->SharedKey[BSS0][3].KeyLen = KeyLen / 2 ;
5744             AtoH(arg, pAdapter->SharedKey[BSS0][3].Key, KeyLen / 2);
5745             CipherAlg = CIPHER_WEP128;
5746             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key4_Proc::(Key4=%s and type=%s)\n", arg, "Hex"));
5747             break;
5748         default: //Invalid argument
5749             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key4_Proc::Invalid argument (=%s)\n", arg));
5750             return FALSE;
5751     }
5752     pAdapter->SharedKey[BSS0][3].CipherAlg = CipherAlg;
5753
5754     // Set keys (into ASIC)
5755     if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
5756         ;   // not support
5757     else    // Old WEP stuff
5758     {
5759         AsicAddSharedKeyEntry(pAdapter,
5760                               0,
5761                               3,
5762                               pAdapter->SharedKey[BSS0][3].CipherAlg,
5763                               pAdapter->SharedKey[BSS0][3].Key,
5764                               NULL,
5765                               NULL);
5766     }
5767
5768     return TRUE;
5769 }
5770
5771 /*
5772     ==========================================================================
5773     Description:
5774         Set WPA PSK key
5775     Return:
5776         TRUE if all parameters are OK, FALSE otherwise
5777     ==========================================================================
5778 */
5779 INT Set_WPAPSK_Proc(
5780     IN  PRTMP_ADAPTER   pAdapter,
5781     IN  PUCHAR          arg)
5782 {
5783     UCHAR                   keyMaterial[40];
5784
5785     if ((pAdapter->StaCfg.AuthMode != Ndis802_11AuthModeWPAPSK) &&
5786         (pAdapter->StaCfg.AuthMode != Ndis802_11AuthModeWPA2PSK) &&
5787             (pAdapter->StaCfg.AuthMode != Ndis802_11AuthModeWPANone)
5788                 )
5789         return TRUE;    // do nothing
5790
5791     DBGPRINT(RT_DEBUG_TRACE, ("Set_WPAPSK_Proc::(WPAPSK=%s)\n", arg));
5792
5793     NdisZeroMemory(keyMaterial, 40);
5794
5795     if ((strlen(arg) < 8) || (strlen(arg) > 64))
5796     {
5797         DBGPRINT(RT_DEBUG_TRACE, ("Set failed!!(WPAPSK=%s), WPAPSK key-string required 8 ~ 64 characters \n", arg));
5798         return FALSE;
5799     }
5800
5801     if (strlen(arg) == 64)
5802     {
5803         AtoH(arg, keyMaterial, 32);
5804         NdisMoveMemory(pAdapter->StaCfg.PMK, keyMaterial, 32);
5805
5806     }
5807     else
5808     {
5809         PasswordHash((char *)arg, pAdapter->MlmeAux.Ssid, pAdapter->MlmeAux.SsidLen, keyMaterial);
5810         NdisMoveMemory(pAdapter->StaCfg.PMK, keyMaterial, 32);
5811     }
5812
5813
5814
5815     if(pAdapter->StaCfg.BssType == BSS_ADHOC &&
5816        pAdapter->StaCfg.AuthMode == Ndis802_11AuthModeWPANone)
5817     {
5818          pAdapter->StaCfg.WpaState = SS_NOTUSE;
5819     }
5820     else
5821     {
5822         // Start STA supplicant state machine
5823         pAdapter->StaCfg.WpaState = SS_START;
5824     }
5825
5826     return TRUE;
5827 }
5828
5829 /*
5830     ==========================================================================
5831     Description:
5832         Set Power Saving mode
5833     Return:
5834         TRUE if all parameters are OK, FALSE otherwise
5835     ==========================================================================
5836 */
5837 INT Set_PSMode_Proc(
5838     IN  PRTMP_ADAPTER   pAdapter,
5839     IN  PUCHAR          arg)
5840 {
5841     if (pAdapter->StaCfg.BssType == BSS_INFRA)
5842     {
5843         if ((strcmp(arg, "Max_PSP") == 0) ||
5844                         (strcmp(arg, "max_psp") == 0) ||
5845                         (strcmp(arg, "MAX_PSP") == 0))
5846         {
5847             // do NOT turn on PSM bit here, wait until MlmeCheckForPsmChange()
5848             // to exclude certain situations.
5849             if (pAdapter->StaCfg.bWindowsACCAMEnable == FALSE)
5850                 pAdapter->StaCfg.WindowsPowerMode = Ndis802_11PowerModeMAX_PSP;
5851             pAdapter->StaCfg.WindowsBatteryPowerMode = Ndis802_11PowerModeMAX_PSP;
5852             OPSTATUS_SET_FLAG(pAdapter, fOP_STATUS_RECEIVE_DTIM);
5853             pAdapter->StaCfg.DefaultListenCount = 5;
5854
5855         }
5856         else if ((strcmp(arg, "Fast_PSP") == 0) ||
5857                                  (strcmp(arg, "fast_psp") == 0) ||
5858                  (strcmp(arg, "FAST_PSP") == 0))
5859         {
5860             // do NOT turn on PSM bit here, wait until MlmeCheckForPsmChange()
5861             // to exclude certain situations.
5862             OPSTATUS_SET_FLAG(pAdapter, fOP_STATUS_RECEIVE_DTIM);
5863             if (pAdapter->StaCfg.bWindowsACCAMEnable == FALSE)
5864                 pAdapter->StaCfg.WindowsPowerMode = Ndis802_11PowerModeFast_PSP;
5865             pAdapter->StaCfg.WindowsBatteryPowerMode = Ndis802_11PowerModeFast_PSP;
5866             pAdapter->StaCfg.DefaultListenCount = 3;
5867         }
5868         else if ((strcmp(arg, "Legacy_PSP") == 0) ||
5869                  (strcmp(arg, "legacy_psp") == 0) ||
5870                  (strcmp(arg, "LEGACY_PSP") == 0))
5871         {
5872             // do NOT turn on PSM bit here, wait until MlmeCheckForPsmChange()
5873             // to exclude certain situations.
5874             OPSTATUS_SET_FLAG(pAdapter, fOP_STATUS_RECEIVE_DTIM);
5875             if (pAdapter->StaCfg.bWindowsACCAMEnable == FALSE)
5876                 pAdapter->StaCfg.WindowsPowerMode = Ndis802_11PowerModeLegacy_PSP;
5877             pAdapter->StaCfg.WindowsBatteryPowerMode = Ndis802_11PowerModeLegacy_PSP;
5878             pAdapter->StaCfg.DefaultListenCount = 3;
5879         }
5880         else
5881         {
5882             //Default Ndis802_11PowerModeCAM
5883             // clear PSM bit immediately
5884             MlmeSetPsmBit(pAdapter, PWR_ACTIVE);
5885             OPSTATUS_SET_FLAG(pAdapter, fOP_STATUS_RECEIVE_DTIM);
5886             if (pAdapter->StaCfg.bWindowsACCAMEnable == FALSE)
5887                 pAdapter->StaCfg.WindowsPowerMode = Ndis802_11PowerModeCAM;
5888             pAdapter->StaCfg.WindowsBatteryPowerMode = Ndis802_11PowerModeCAM;
5889         }
5890
5891         DBGPRINT(RT_DEBUG_TRACE, ("Set_PSMode_Proc::(PSMode=%ld)\n", pAdapter->StaCfg.WindowsPowerMode));
5892     }
5893     else
5894         return FALSE;
5895
5896
5897     return TRUE;
5898 }
5899
5900 /*
5901     ==========================================================================
5902     Description:
5903         Set WpaSupport flag.
5904     Value:
5905         0: Driver ignore wpa_supplicant.
5906         1: wpa_supplicant initiates scanning and AP selection.
5907         2: driver takes care of scanning, AP selection, and IEEE 802.11 association parameters.
5908     Return:
5909         TRUE if all parameters are OK, FALSE otherwise
5910     ==========================================================================
5911 */
5912 INT Set_Wpa_Support(
5913     IN  PRTMP_ADAPTER   pAd,
5914         IN      PUCHAR                  arg)
5915 {
5916
5917     if ( simple_strtol(arg, 0, 10) == 0)
5918         pAd->StaCfg.WpaSupplicantUP = WPA_SUPPLICANT_DISABLE;
5919     else if ( simple_strtol(arg, 0, 10) == 1)
5920         pAd->StaCfg.WpaSupplicantUP = WPA_SUPPLICANT_ENABLE;
5921     else if ( simple_strtol(arg, 0, 10) == 2)
5922         pAd->StaCfg.WpaSupplicantUP = WPA_SUPPLICANT_ENABLE_WITH_WEB_UI;
5923     else
5924         pAd->StaCfg.WpaSupplicantUP = WPA_SUPPLICANT_DISABLE;
5925
5926     DBGPRINT(RT_DEBUG_TRACE, ("Set_Wpa_Support::(WpaSupplicantUP=%d)\n", pAd->StaCfg.WpaSupplicantUP));
5927
5928     return TRUE;
5929 }
5930
5931 #ifdef DBG
5932 /*
5933     ==========================================================================
5934     Description:
5935         Read / Write MAC
5936     Arguments:
5937         pAdapter                    Pointer to our adapter
5938         wrq                         Pointer to the ioctl argument
5939
5940     Return Value:
5941         None
5942
5943     Note:
5944         Usage:
5945                1.) iwpriv ra0 mac 0        ==> read MAC where Addr=0x0
5946                2.) iwpriv ra0 mac 0=12     ==> write MAC where Addr=0x0, value=12
5947     ==========================================================================
5948 */
5949 VOID RTMPIoctlMAC(
5950         IN      PRTMP_ADAPTER   pAdapter,
5951         IN      struct iwreq    *wrq)
5952 {
5953         CHAR                            *this_char;
5954         CHAR                            *value;
5955         INT                                     j = 0, k = 0;
5956         CHAR                            msg[1024];
5957         CHAR                            arg[255];
5958         ULONG                           macAddr = 0;
5959         UCHAR                           temp[16], temp2[16];
5960         UINT32                          macValue = 0;
5961         INT                                     Status;
5962         BOOLEAN                         bIsPrintAllMAC = FALSE;
5963
5964
5965         memset(msg, 0x00, 1024);
5966         if (wrq->u.data.length > 1) //No parameters.
5967         {
5968             Status = copy_from_user(arg, wrq->u.data.pointer, (wrq->u.data.length > 255) ? 255 : wrq->u.data.length);
5969                 sprintf(msg, "\n");
5970
5971                 //Parsing Read or Write
5972             this_char = arg;
5973                 if (!*this_char)
5974                         goto next;
5975
5976                 if ((value = rtstrchr(this_char, '=')) != NULL)
5977                         *value++ = 0;
5978
5979                 if (!value || !*value)
5980                 { //Read
5981                         // Sanity check
5982                         if(strlen(this_char) > 4)
5983                                 goto next;
5984
5985                         j = strlen(this_char);
5986                         while(j-- > 0)
5987                         {
5988                                 if(this_char[j] > 'f' || this_char[j] < '0')
5989                                         return;
5990                         }
5991
5992                         // Mac Addr
5993                         k = j = strlen(this_char);
5994                         while(j-- > 0)
5995                         {
5996                                 this_char[4-k+j] = this_char[j];
5997                         }
5998
5999                         while(k < 4)
6000                                 this_char[3-k++]='0';
6001                         this_char[4]='\0';
6002
6003                         if(strlen(this_char) == 4)
6004                         {
6005                                 AtoH(this_char, temp, 2);
6006                                 macAddr = *temp*256 + temp[1];
6007                                 if (macAddr < 0xFFFF)
6008                                 {
6009                                         RTMP_IO_READ32(pAdapter, macAddr, &macValue);
6010                                         DBGPRINT(RT_DEBUG_TRACE, ("MacAddr=%lx, MacValue=%x\n", macAddr, macValue));
6011                                         sprintf(msg+strlen(msg), "[0x%08lX]:%08X  ", macAddr , macValue);
6012                                 }
6013                                 else
6014                                 {//Invalid parametes, so default printk all mac
6015                                         bIsPrintAllMAC = TRUE;
6016                                         goto next;
6017                                 }
6018                         }
6019                 }
6020                 else
6021                 { //Write
6022                         memcpy(&temp2, value, strlen(value));
6023                         temp2[strlen(value)] = '\0';
6024
6025                         // Sanity check
6026                         if((strlen(this_char) > 4) || strlen(temp2) > 8)
6027                                 goto next;
6028
6029                         j = strlen(this_char);
6030                         while(j-- > 0)
6031                         {
6032                                 if(this_char[j] > 'f' || this_char[j] < '0')
6033                                         return;
6034                         }
6035
6036                         j = strlen(temp2);
6037                         while(j-- > 0)
6038                         {
6039                                 if(temp2[j] > 'f' || temp2[j] < '0')
6040                                         return;
6041                         }
6042
6043                         //MAC Addr
6044                         k = j = strlen(this_char);
6045                         while(j-- > 0)
6046                         {
6047                                 this_char[4-k+j] = this_char[j];
6048                         }
6049
6050                         while(k < 4)
6051                                 this_char[3-k++]='0';
6052                         this_char[4]='\0';
6053
6054                         //MAC value
6055                         k = j = strlen(temp2);
6056                         while(j-- > 0)
6057                         {
6058                                 temp2[8-k+j] = temp2[j];
6059                         }
6060
6061                         while(k < 8)
6062                                 temp2[7-k++]='0';
6063                         temp2[8]='\0';
6064
6065                         {
6066                                 AtoH(this_char, temp, 2);
6067                                 macAddr = *temp*256 + temp[1];
6068
6069                                 AtoH(temp2, temp, 4);
6070                                 macValue = *temp*256*256*256 + temp[1]*256*256 + temp[2]*256 + temp[3];
6071
6072                                 // debug mode
6073                                 if (macAddr == (HW_DEBUG_SETTING_BASE + 4))
6074                                 {
6075                                         // 0x2bf4: byte0 non-zero: enable R17 tuning, 0: disable R17 tuning
6076                     if (macValue & 0x000000ff)
6077                     {
6078                         pAdapter->BbpTuning.bEnable = TRUE;
6079                         DBGPRINT(RT_DEBUG_TRACE,("turn on R17 tuning\n"));
6080                     }
6081                     else
6082                     {
6083                         UCHAR R66;
6084                         pAdapter->BbpTuning.bEnable = FALSE;
6085                         R66 = 0x26 + GET_LNA_GAIN(pAdapter);
6086                                                 RTMP_BBP_IO_WRITE8_BY_REG_ID(pAdapter, BBP_R66, (0x26 + GET_LNA_GAIN(pAdapter)));
6087                         DBGPRINT(RT_DEBUG_TRACE,("turn off R17 tuning, restore to 0x%02x\n", R66));
6088                     }
6089                                         return;
6090                                 }
6091
6092                                 DBGPRINT(RT_DEBUG_TRACE, ("MacAddr=%02lx, MacValue=0x%x\n", macAddr, macValue));
6093
6094                                 RTMP_IO_WRITE32(pAdapter, macAddr, macValue);
6095                                 sprintf(msg+strlen(msg), "[0x%08lX]:%08X  ", macAddr, macValue);
6096                         }
6097                 }
6098         }
6099         else
6100                 bIsPrintAllMAC = TRUE;
6101 next:
6102         if (bIsPrintAllMAC)
6103         {
6104                 struct file             *file_w;
6105                 PCHAR                   fileName = "MacDump.txt";
6106                 mm_segment_t    orig_fs;
6107
6108                 orig_fs = get_fs();
6109                 set_fs(KERNEL_DS);
6110
6111                 // open file
6112                 file_w = filp_open(fileName, O_WRONLY|O_CREAT, 0);
6113                 if (IS_ERR(file_w))
6114                 {
6115                         DBGPRINT(RT_DEBUG_TRACE, ("-->2) %s: Error %ld opening %s\n", __func__, -PTR_ERR(file_w), fileName));
6116                 }
6117                 else
6118                 {
6119                         if (file_w->f_op && file_w->f_op->write)
6120                         {
6121                                 file_w->f_pos = 0;
6122                                 macAddr = 0x1000;
6123
6124                                 while (macAddr <= 0x1800)
6125                                 {
6126                                         RTMP_IO_READ32(pAdapter, macAddr, &macValue);
6127                                         sprintf(msg, "%08lx = %08X\n", macAddr, macValue);
6128
6129                                         // write data to file
6130                                         file_w->f_op->write(file_w, msg, strlen(msg), &file_w->f_pos);
6131
6132                                         printk("%s", msg);
6133                                         macAddr += 4;
6134                                 }
6135                                 sprintf(msg, "\nDump all MAC values to %s\n", fileName);
6136                         }
6137                         filp_close(file_w, NULL);
6138                 }
6139                 set_fs(orig_fs);
6140         }
6141         if(strlen(msg) == 1)
6142                 sprintf(msg+strlen(msg), "===>Error command format!");
6143
6144         // Copy the information into the user buffer
6145         wrq->u.data.length = strlen(msg);
6146         Status = copy_to_user(wrq->u.data.pointer, msg, wrq->u.data.length);
6147
6148         DBGPRINT(RT_DEBUG_TRACE, ("<==RTMPIoctlMAC\n\n"));
6149 }
6150
6151 /*
6152     ==========================================================================
6153     Description:
6154         Read / Write E2PROM
6155     Arguments:
6156         pAdapter                    Pointer to our adapter
6157         wrq                         Pointer to the ioctl argument
6158
6159     Return Value:
6160         None
6161
6162     Note:
6163         Usage:
6164                1.) iwpriv ra0 e2p 0             ==> read E2PROM where Addr=0x0
6165                2.) iwpriv ra0 e2p 0=1234    ==> write E2PROM where Addr=0x0, value=1234
6166     ==========================================================================
6167 */
6168 VOID RTMPIoctlE2PROM(
6169         IN      PRTMP_ADAPTER   pAdapter,
6170         IN      struct iwreq    *wrq)
6171 {
6172         CHAR                            *this_char;
6173         CHAR                            *value;
6174         INT                                     j = 0, k = 0;
6175         CHAR                            msg[1024];
6176         CHAR                            arg[255];
6177         USHORT                          eepAddr = 0;
6178         UCHAR                           temp[16], temp2[16];
6179         USHORT                          eepValue;
6180         int                                     Status;
6181         BOOLEAN                         bIsPrintAllE2P = FALSE;
6182
6183
6184         memset(msg, 0x00, 1024);
6185         if (wrq->u.data.length > 1) //No parameters.
6186         {
6187             Status = copy_from_user(arg, wrq->u.data.pointer, (wrq->u.data.length > 255) ? 255 : wrq->u.data.length);
6188                 sprintf(msg, "\n");
6189
6190             //Parsing Read or Write
6191                 this_char = arg;
6192
6193
6194                 if (!*this_char)
6195                         goto next;
6196
6197                 if ((value = rtstrchr(this_char, '=')) != NULL)
6198                         *value++ = 0;
6199
6200                 if (!value || !*value)
6201                 { //Read
6202
6203                         // Sanity check
6204                         if(strlen(this_char) > 4)
6205                                 goto next;
6206
6207                         j = strlen(this_char);
6208                         while(j-- > 0)
6209                         {
6210                                 if(this_char[j] > 'f' || this_char[j] < '0')
6211                                         return;
6212                         }
6213
6214                         // E2PROM addr
6215                         k = j = strlen(this_char);
6216                         while(j-- > 0)
6217                         {
6218                                 this_char[4-k+j] = this_char[j];
6219                         }
6220
6221                         while(k < 4)
6222                                 this_char[3-k++]='0';
6223                         this_char[4]='\0';
6224
6225                         if(strlen(this_char) == 4)
6226                         {
6227                                 AtoH(this_char, temp, 2);
6228                                 eepAddr = *temp*256 + temp[1];
6229                                 if (eepAddr < 0xFFFF)
6230                                 {
6231                                         RT28xx_EEPROM_READ16(pAdapter, eepAddr, eepValue);
6232                                         sprintf(msg+strlen(msg), "[0x%04X]:0x%04X  ", eepAddr , eepValue);
6233                                 }
6234                                 else
6235                                 {//Invalid parametes, so default printk all bbp
6236                                         bIsPrintAllE2P = TRUE;
6237                                         goto next;
6238                                 }
6239                         }
6240                 }
6241                 else
6242                 { //Write
6243                         memcpy(&temp2, value, strlen(value));
6244                         temp2[strlen(value)] = '\0';
6245
6246                         // Sanity check
6247                         if((strlen(this_char) > 4) || strlen(temp2) > 8)
6248                                 goto next;
6249
6250                         j = strlen(this_char);
6251                         while(j-- > 0)
6252                         {
6253                                 if(this_char[j] > 'f' || this_char[j] < '0')
6254                                         return;
6255                         }
6256                         j = strlen(temp2);
6257                         while(j-- > 0)
6258                         {
6259                                 if(temp2[j] > 'f' || temp2[j] < '0')
6260                                         return;
6261                         }
6262
6263                         //MAC Addr
6264                         k = j = strlen(this_char);
6265                         while(j-- > 0)
6266                         {
6267                                 this_char[4-k+j] = this_char[j];
6268                         }
6269
6270                         while(k < 4)
6271                                 this_char[3-k++]='0';
6272                         this_char[4]='\0';
6273
6274                         //MAC value
6275                         k = j = strlen(temp2);
6276                         while(j-- > 0)
6277                         {
6278                                 temp2[4-k+j] = temp2[j];
6279                         }
6280
6281                         while(k < 4)
6282                                 temp2[3-k++]='0';
6283                         temp2[4]='\0';
6284
6285                         AtoH(this_char, temp, 2);
6286                         eepAddr = *temp*256 + temp[1];
6287
6288                         AtoH(temp2, temp, 2);
6289                         eepValue = *temp*256 + temp[1];
6290
6291                         RT28xx_EEPROM_WRITE16(pAdapter, eepAddr, eepValue);
6292                         sprintf(msg+strlen(msg), "[0x%02X]:%02X  ", eepAddr, eepValue);
6293                 }
6294         }
6295         else
6296                 bIsPrintAllE2P = TRUE;
6297 next:
6298         if (bIsPrintAllE2P)
6299         {
6300                 struct file             *file_w;
6301                 PCHAR                   fileName = "EEPROMDump.txt";
6302                 mm_segment_t    orig_fs;
6303
6304                 orig_fs = get_fs();
6305                 set_fs(KERNEL_DS);
6306
6307                 // open file
6308                 file_w = filp_open(fileName, O_WRONLY|O_CREAT, 0);
6309                 if (IS_ERR(file_w))
6310                 {
6311                         DBGPRINT(RT_DEBUG_TRACE, ("-->2) %s: Error %ld opening %s\n", __func__, -PTR_ERR(file_w), fileName));
6312                 }
6313                 else
6314                 {
6315                         if (file_w->f_op && file_w->f_op->write)
6316                         {
6317                                 file_w->f_pos = 0;
6318                                 eepAddr = 0x00;
6319
6320                                 while (eepAddr <= 0xFE)
6321                                 {
6322                                         RT28xx_EEPROM_READ16(pAdapter, eepAddr, eepValue);
6323                                         sprintf(msg, "%08x = %04x\n", eepAddr , eepValue);
6324
6325                                         // write data to file
6326                                         file_w->f_op->write(file_w, msg, strlen(msg), &file_w->f_pos);
6327
6328                                         printk("%s", msg);
6329                                         eepAddr += 2;
6330                                 }
6331                                 sprintf(msg, "\nDump all EEPROM values to %s\n", fileName);
6332                         }
6333                         filp_close(file_w, NULL);
6334                 }
6335                 set_fs(orig_fs);
6336         }
6337         if(strlen(msg) == 1)
6338                 sprintf(msg+strlen(msg), "===>Error command format!");
6339
6340
6341         // Copy the information into the user buffer
6342         wrq->u.data.length = strlen(msg);
6343         Status = copy_to_user(wrq->u.data.pointer, msg, wrq->u.data.length);
6344
6345         DBGPRINT(RT_DEBUG_TRACE, ("<==RTMPIoctlE2PROM\n"));
6346 }
6347 #ifdef RT30xx
6348 /*
6349     ==========================================================================
6350     Description:
6351         Read / Write RF register
6352 Arguments:
6353     pAdapter                    Pointer to our adapter
6354     wrq                         Pointer to the ioctl argument
6355
6356     Return Value:
6357         None
6358
6359     Note:
6360         Usage:
6361                1.) iwpriv ra0 rf                ==> read all RF registers
6362                2.) iwpriv ra0 rf 1              ==> read RF where RegID=1
6363                3.) iwpriv ra0 rf 1=10               ==> write RF R1=0x10
6364     ==========================================================================
6365 */
6366 VOID RTMPIoctlRF(
6367         IN      PRTMP_ADAPTER   pAdapter,
6368         IN      struct iwreq    *wrq)
6369 {
6370         CHAR                            *this_char;
6371         CHAR                            *value;
6372         UCHAR                           regRF = 0;
6373         CHAR                            msg[2048];
6374         CHAR                            arg[255];
6375         INT                                     rfId;
6376         LONG                            rfValue;
6377         int                                     Status;
6378         BOOLEAN                         bIsPrintAllRF = FALSE;
6379
6380
6381         memset(msg, 0x00, 2048);
6382         if (wrq->u.data.length > 1) //No parameters.
6383         {
6384             Status = copy_from_user(arg, wrq->u.data.pointer, (wrq->u.data.length > 255) ? 255 : wrq->u.data.length);
6385                 sprintf(msg, "\n");
6386
6387             //Parsing Read or Write
6388                 this_char = arg;
6389                 if (!*this_char)
6390                         goto next;
6391
6392                 if ((value = strchr(this_char, '=')) != NULL)
6393                         *value++ = 0;
6394
6395                 if (!value || !*value)
6396                 { //Read
6397                         if (sscanf(this_char, "%d", &(rfId)) == 1)
6398                         {
6399                                 if (rfId <= 31)
6400                                 {
6401                                         // In RT2860 ATE mode, we do not load 8051 firmware.
6402                                             //We must access RF directly.
6403                     // For RT2870 ATE mode, ATE_RF_IO_WRITE8(/READ8)_BY_REG_ID are redefined.
6404                                         // according to Andy, Gary, David require.
6405                                         // the command rf shall read rf register directly for dubug.
6406                                         // BBP_IO_READ8_BY_REG_ID(pAdapter, bbpId, &regBBP);
6407                                         RT30xxReadRFRegister(pAdapter, rfId, &regRF);
6408
6409                                         sprintf(msg+strlen(msg), "R%02d[0x%02x]:%02X  ", rfId, rfId*2, regRF);
6410                                 }
6411                                 else
6412                                 {//Invalid parametes, so default printk all RF
6413                                         bIsPrintAllRF = TRUE;
6414                                         goto next;
6415                                 }
6416                         }
6417                         else
6418                         { //Invalid parametes, so default printk all RF
6419                                 bIsPrintAllRF = TRUE;
6420                                 goto next;
6421                         }
6422                 }
6423                 else
6424                 { //Write
6425                         if ((sscanf(this_char, "%d", &(rfId)) == 1) && (sscanf(value, "%lx", &(rfValue)) == 1))
6426                         {
6427                                 if (rfId <= 31)
6428                                 {
6429                                         // In RT2860 ATE mode, we do not load 8051 firmware.
6430                                         // We should access RF registers directly.
6431                     // For RT2870 ATE mode, ATE_RF_IO_WRITE8/READ8_BY_REG_ID are redefined.
6432                                                 {
6433                                                         // according to Andy, Gary, David require.
6434                                                         // the command RF shall read/write RF register directly for dubug.
6435                                                         //BBP_IO_READ8_BY_REG_ID(pAdapter, bbpId, &regBBP);
6436                                                         //BBP_IO_WRITE8_BY_REG_ID(pAdapter, (UCHAR)bbpId,(UCHAR) bbpValue);
6437                                                         RT30xxReadRFRegister(pAdapter, rfId, &regRF);
6438                                                         RT30xxWriteRFRegister(pAdapter, (UCHAR)rfId,(UCHAR) rfValue);
6439                                                         //Read it back for showing
6440                                                         //BBP_IO_READ8_BY_REG_ID(pAdapter, bbpId, &regBBP);
6441                                                         RT30xxReadRFRegister(pAdapter, rfId, &regRF);
6442                                                         sprintf(msg+strlen(msg), "R%02d[0x%02X]:%02X\n", rfId, rfId*2, regRF);
6443                                                 }
6444                                 }
6445                                 else
6446                                 {//Invalid parametes, so default printk all RF
6447                                         bIsPrintAllRF = TRUE;
6448                                 }
6449                         }
6450                         else
6451                         { //Invalid parametes, so default printk all RF
6452                                 bIsPrintAllRF = TRUE;
6453                         }
6454                 }
6455         }
6456         else
6457                 bIsPrintAllRF = TRUE;
6458 next:
6459         if (bIsPrintAllRF)
6460         {
6461                 memset(msg, 0x00, 2048);
6462                 sprintf(msg, "\n");
6463                 for (rfId = 0; rfId <= 31; rfId++)
6464                 {
6465                         // according to Andy, Gary, David require.
6466                         // the command RF shall read/write RF register directly for dubug.
6467                         RT30xxReadRFRegister(pAdapter, rfId, &regRF);
6468                         sprintf(msg+strlen(msg), "%03d = %02X\n", rfId, regRF);
6469                 }
6470                 // Copy the information into the user buffer
6471                 DBGPRINT(RT_DEBUG_TRACE, ("strlen(msg)=%d\n", (UINT32)strlen(msg)));
6472                 wrq->u.data.length = strlen(msg);
6473                 if (copy_to_user(wrq->u.data.pointer, msg, wrq->u.data.length))
6474                 {
6475                         DBGPRINT(RT_DEBUG_TRACE, ("%s: copy_to_user() fail\n", __func__));
6476                 }
6477         }
6478         else
6479         {
6480                 if(strlen(msg) == 1)
6481                         sprintf(msg+strlen(msg), "===>Error command format!");
6482
6483                 DBGPRINT(RT_DEBUG_TRACE, ("copy to user [msg=%s]\n", msg));
6484                 // Copy the information into the user buffer
6485                 DBGPRINT(RT_DEBUG_TRACE, ("strlen(msg) =%d\n", (UINT32)strlen(msg)));
6486
6487                 // Copy the information into the user buffer
6488                 wrq->u.data.length = strlen(msg);
6489                 Status = copy_to_user(wrq->u.data.pointer, msg, wrq->u.data.length);
6490         }
6491
6492         DBGPRINT(RT_DEBUG_TRACE, ("<==RTMPIoctlRF\n\n"));
6493 }
6494 #endif // RT30xx //
6495 #endif // DBG //
6496
6497
6498
6499
6500 INT Set_TGnWifiTest_Proc(
6501     IN  PRTMP_ADAPTER   pAd,
6502     IN  PUCHAR          arg)
6503 {
6504     if (simple_strtol(arg, 0, 10) == 0)
6505         pAd->StaCfg.bTGnWifiTest = FALSE;
6506     else
6507         pAd->StaCfg.bTGnWifiTest = TRUE;
6508
6509     DBGPRINT(RT_DEBUG_TRACE, ("IF Set_TGnWifiTest_Proc::(bTGnWifiTest=%d)\n", pAd->StaCfg.bTGnWifiTest));
6510         return TRUE;
6511 }
6512
6513 INT Set_LongRetryLimit_Proc(
6514         IN      PRTMP_ADAPTER   pAdapter,
6515         IN      PUCHAR                  arg)
6516 {
6517         TX_RTY_CFG_STRUC        tx_rty_cfg;
6518         UCHAR                           LongRetryLimit = (UCHAR)simple_strtol(arg, 0, 10);
6519
6520         RTMP_IO_READ32(pAdapter, TX_RTY_CFG, &tx_rty_cfg.word);
6521         tx_rty_cfg.field.LongRtyLimit = LongRetryLimit;
6522         RTMP_IO_WRITE32(pAdapter, TX_RTY_CFG, tx_rty_cfg.word);
6523         DBGPRINT(RT_DEBUG_TRACE, ("IF Set_LongRetryLimit_Proc::(tx_rty_cfg=0x%x)\n", tx_rty_cfg.word));
6524         return TRUE;
6525 }
6526
6527 INT Set_ShortRetryLimit_Proc(
6528         IN      PRTMP_ADAPTER   pAdapter,
6529         IN      PUCHAR                  arg)
6530 {
6531         TX_RTY_CFG_STRUC        tx_rty_cfg;
6532         UCHAR                           ShortRetryLimit = (UCHAR)simple_strtol(arg, 0, 10);
6533
6534         RTMP_IO_READ32(pAdapter, TX_RTY_CFG, &tx_rty_cfg.word);
6535         tx_rty_cfg.field.ShortRtyLimit = ShortRetryLimit;
6536         RTMP_IO_WRITE32(pAdapter, TX_RTY_CFG, tx_rty_cfg.word);
6537         DBGPRINT(RT_DEBUG_TRACE, ("IF Set_ShortRetryLimit_Proc::(tx_rty_cfg=0x%x)\n", tx_rty_cfg.word));
6538         return TRUE;
6539 }