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