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