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