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