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