Staging: rt2860: remove dead DOT11N_DRAFT3 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 DOT11_N_SUPPORT
3146         MaxPhyMode = PHY_11N_5G;
3147 #endif // DOT11_N_SUPPORT //
3148
3149
3150         DBGPRINT(RT_DEBUG_TRACE, ("-->RTMPSetInformation(),     0x%08x\n", cmd&0x7FFF));
3151     switch(cmd & 0x7FFF) {
3152         case RT_OID_802_11_COUNTRY_REGION:
3153             if (wrq->u.data.length < sizeof(UCHAR))
3154                 Status = -EINVAL;
3155                         // Only avaliable when EEPROM not programming
3156             else if (!(pAdapter->CommonCfg.CountryRegion & 0x80) && !(pAdapter->CommonCfg.CountryRegionForABand & 0x80))
3157             {
3158                 ULONG   Country;
3159                 UCHAR   TmpPhy;
3160
3161                                 Status = copy_from_user(&Country, wrq->u.data.pointer, wrq->u.data.length);
3162                                 pAdapter->CommonCfg.CountryRegion = (UCHAR)(Country & 0x000000FF);
3163                                 pAdapter->CommonCfg.CountryRegionForABand = (UCHAR)((Country >> 8) & 0x000000FF);
3164                 TmpPhy = pAdapter->CommonCfg.PhyMode;
3165                                 pAdapter->CommonCfg.PhyMode = 0xff;
3166                                 // Build all corresponding channel information
3167                                 RTMPSetPhyMode(pAdapter, TmpPhy);
3168 #ifdef DOT11_N_SUPPORT
3169                                 SetCommonHT(pAdapter);
3170 #endif // DOT11_N_SUPPORT //
3171                                 DBGPRINT(RT_DEBUG_TRACE, ("Set::RT_OID_802_11_COUNTRY_REGION (A:%d  B/G:%d)\n", pAdapter->CommonCfg.CountryRegionForABand,
3172                                     pAdapter->CommonCfg.CountryRegion));
3173             }
3174             break;
3175         case OID_802_11_BSSID_LIST_SCAN:
3176             Now = jiffies;
3177                         DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_BSSID_LIST_SCAN, TxCnt = %d \n", pAdapter->RalinkCounters.LastOneSecTotalTxCount));
3178
3179             if (MONITOR_ON(pAdapter))
3180             {
3181                 DBGPRINT(RT_DEBUG_TRACE, ("!!! Driver is in Monitor Mode now !!!\n"));
3182                 break;
3183             }
3184
3185                         //Benson add 20080527, when radio off, sta don't need to scan
3186                         if (RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_RADIO_OFF))
3187                                 break;
3188
3189                         if (RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_BSS_SCAN_IN_PROGRESS))
3190                         {
3191                 DBGPRINT(RT_DEBUG_TRACE, ("!!! Driver is scanning now !!!\n"));
3192                                 pAdapter->StaCfg.bScanReqIsFromWebUI = TRUE;
3193                                 Status = NDIS_STATUS_SUCCESS;
3194                 break;
3195             }
3196
3197                         if (pAdapter->RalinkCounters.LastOneSecTotalTxCount > 100)
3198             {
3199                 DBGPRINT(RT_DEBUG_TRACE, ("!!! Link UP, ignore this set::OID_802_11_BSSID_LIST_SCAN\n"));
3200                                 Status = NDIS_STATUS_SUCCESS;
3201                                 pAdapter->StaCfg.ScanCnt = 99;          // Prevent auto scan triggered by this OID
3202                                 break;
3203             }
3204
3205             if ((OPSTATUS_TEST_FLAG(pAdapter, fOP_STATUS_MEDIA_STATE_CONNECTED)) &&
3206                                 ((pAdapter->StaCfg.AuthMode == Ndis802_11AuthModeWPA) ||
3207                                 (pAdapter->StaCfg.AuthMode == Ndis802_11AuthModeWPAPSK) ||
3208                                 (pAdapter->StaCfg.AuthMode == Ndis802_11AuthModeWPA2) ||
3209                                 (pAdapter->StaCfg.AuthMode == Ndis802_11AuthModeWPA2PSK)) &&
3210                 (pAdapter->StaCfg.PortSecured == WPA_802_1X_PORT_NOT_SECURED))
3211             {
3212                 DBGPRINT(RT_DEBUG_TRACE, ("!!! Link UP, Port Not Secured! ignore this set::OID_802_11_BSSID_LIST_SCAN\n"));
3213                                 Status = NDIS_STATUS_SUCCESS;
3214                                 pAdapter->StaCfg.ScanCnt = 99;          // Prevent auto scan triggered by this OID
3215                                 break;
3216             }
3217
3218
3219             if (pAdapter->Mlme.CntlMachine.CurrState != CNTL_IDLE)
3220             {
3221                 RT28XX_MLME_RESET_STATE_MACHINE(pAdapter);
3222                 DBGPRINT(RT_DEBUG_TRACE, ("!!! MLME busy, reset MLME state machine !!!\n"));
3223             }
3224
3225             // tell CNTL state machine to call NdisMSetInformationComplete() after completing
3226             // this request, because this request is initiated by NDIS.
3227             pAdapter->MlmeAux.CurrReqIsFromNdis = FALSE;
3228             // Reset allowed scan retries
3229             pAdapter->StaCfg.ScanCnt = 0;
3230             pAdapter->StaCfg.LastScanTime = Now;
3231
3232                         pAdapter->StaCfg.bScanReqIsFromWebUI = TRUE;
3233             RTMP_SET_FLAG(pAdapter, fRTMP_ADAPTER_BSS_SCAN_IN_PROGRESS);
3234             MlmeEnqueue(pAdapter,
3235                         MLME_CNTL_STATE_MACHINE,
3236                         OID_802_11_BSSID_LIST_SCAN,
3237                         0,
3238                         NULL);
3239
3240             Status = NDIS_STATUS_SUCCESS;
3241             StateMachineTouched = TRUE;
3242             break;
3243         case OID_802_11_SSID:
3244             if (wrq->u.data.length != sizeof(NDIS_802_11_SSID))
3245                 Status = -EINVAL;
3246             else
3247             {
3248                 PCHAR pSsidString = NULL;
3249                 Status = copy_from_user(&Ssid, wrq->u.data.pointer, wrq->u.data.length);
3250
3251                                 DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_SSID (Len=%d,Ssid=%s)\n", Ssid.SsidLength, Ssid.Ssid));
3252                 if (Ssid.SsidLength > MAX_LEN_OF_SSID)
3253                     Status = -EINVAL;
3254                 else
3255                 {
3256                         if (Ssid.SsidLength == 0)
3257                         {
3258                                 Set_SSID_Proc(pAdapter, "");
3259                         }
3260                                         else
3261                         {
3262                                 pSsidString = (CHAR *) kmalloc(MAX_LEN_OF_SSID+1, MEM_ALLOC_FLAG);
3263                                                 if (pSsidString)
3264                                                 {
3265                                                         NdisZeroMemory(pSsidString, MAX_LEN_OF_SSID+1);
3266                                                         NdisMoveMemory(pSsidString, Ssid.Ssid, Ssid.SsidLength);
3267                                         Set_SSID_Proc(pAdapter, pSsidString);
3268                                                         kfree(pSsidString);
3269                                                 }
3270                                                 else
3271                                                         Status = -ENOMEM;
3272                         }
3273                 }
3274             }
3275             break;
3276         case OID_802_11_BSSID:
3277             if (wrq->u.data.length != sizeof(NDIS_802_11_MAC_ADDRESS))
3278                 Status  = -EINVAL;
3279             else
3280             {
3281                 Status = copy_from_user(&Bssid, wrq->u.data.pointer, wrq->u.data.length);
3282
3283                 // tell CNTL state machine to call NdisMSetInformationComplete() after completing
3284                 // this request, because this request is initiated by NDIS.
3285                 pAdapter->MlmeAux.CurrReqIsFromNdis = FALSE;
3286
3287                                 // Prevent to connect AP again in STAMlmePeriodicExec
3288                                 pAdapter->MlmeAux.AutoReconnectSsidLen= 32;
3289
3290                 // Reset allowed scan retries
3291                                 pAdapter->StaCfg.ScanCnt = 0;
3292
3293                 if (pAdapter->Mlme.CntlMachine.CurrState != CNTL_IDLE)
3294                 {
3295                     RT28XX_MLME_RESET_STATE_MACHINE(pAdapter);
3296                     DBGPRINT(RT_DEBUG_TRACE, ("!!! MLME busy, reset MLME state machine !!!\n"));
3297                 }
3298                 MlmeEnqueue(pAdapter,
3299                             MLME_CNTL_STATE_MACHINE,
3300                             OID_802_11_BSSID,
3301                             sizeof(NDIS_802_11_MAC_ADDRESS),
3302                             (VOID *)&Bssid);
3303                 Status = NDIS_STATUS_SUCCESS;
3304                 StateMachineTouched = TRUE;
3305
3306                 DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_BSSID %02x:%02x:%02x:%02x:%02x:%02x\n",
3307                                         Bssid[0], Bssid[1], Bssid[2], Bssid[3], Bssid[4], Bssid[5]));
3308             }
3309             break;
3310         case RT_OID_802_11_RADIO:
3311             if (wrq->u.data.length != sizeof(BOOLEAN))
3312                 Status  = -EINVAL;
3313             else
3314             {
3315                 Status = copy_from_user(&RadioState, wrq->u.data.pointer, wrq->u.data.length);
3316                 DBGPRINT(RT_DEBUG_TRACE, ("Set::RT_OID_802_11_RADIO (=%d)\n", RadioState));
3317                 if (pAdapter->StaCfg.bSwRadio != RadioState)
3318                 {
3319                     pAdapter->StaCfg.bSwRadio = RadioState;
3320                     if (pAdapter->StaCfg.bRadio != (pAdapter->StaCfg.bHwRadio && pAdapter->StaCfg.bSwRadio))
3321                     {
3322                         pAdapter->StaCfg.bRadio = (pAdapter->StaCfg.bHwRadio && pAdapter->StaCfg.bSwRadio);
3323                         if (pAdapter->StaCfg.bRadio == TRUE)
3324                         {
3325                             MlmeRadioOn(pAdapter);
3326                             // Update extra information
3327                                                         pAdapter->ExtraInfo = EXTRA_INFO_CLEAR;
3328                         }
3329                         else
3330                         {
3331                             MlmeRadioOff(pAdapter);
3332                             // Update extra information
3333                                                         pAdapter->ExtraInfo = SW_RADIO_OFF;
3334                         }
3335                     }
3336                 }
3337             }
3338             break;
3339         case RT_OID_802_11_PHY_MODE:
3340             if (wrq->u.data.length != sizeof(RT_802_11_PHY_MODE))
3341                 Status  = -EINVAL;
3342             else
3343             {
3344                 Status = copy_from_user(&PhyMode, wrq->u.data.pointer, wrq->u.data.length);
3345                                 if (PhyMode <= MaxPhyMode)
3346                                 {
3347                         RTMPSetPhyMode(pAdapter, PhyMode);
3348 #ifdef DOT11_N_SUPPORT
3349                                         SetCommonHT(pAdapter);
3350 #endif // DOT11_N_SUPPORT //
3351                                 }
3352                 DBGPRINT(RT_DEBUG_TRACE, ("Set::RT_OID_802_11_PHY_MODE (=%d)\n", PhyMode));
3353             }
3354             break;
3355         case RT_OID_802_11_STA_CONFIG:
3356             if (wrq->u.data.length != sizeof(RT_802_11_STA_CONFIG))
3357                 Status  = -EINVAL;
3358             else
3359             {
3360                 Status = copy_from_user(&StaConfig, wrq->u.data.pointer, wrq->u.data.length);
3361                 pAdapter->CommonCfg.bEnableTxBurst = StaConfig.EnableTxBurst;
3362                 pAdapter->CommonCfg.UseBGProtection = StaConfig.UseBGProtection;
3363                 pAdapter->CommonCfg.bUseShortSlotTime = 1; // 2003-10-30 always SHORT SLOT capable
3364                 if ((pAdapter->CommonCfg.PhyMode != StaConfig.AdhocMode) &&
3365                                         (StaConfig.AdhocMode <= MaxPhyMode))
3366                 {
3367                     // allow dynamic change of "USE OFDM rate or not" in ADHOC mode
3368                     // if setting changed, need to reset current TX rate as well as BEACON frame format
3369                     if (pAdapter->StaCfg.BssType == BSS_ADHOC)
3370                     {
3371                                                 pAdapter->CommonCfg.PhyMode = StaConfig.AdhocMode;
3372                         RTMPSetPhyMode(pAdapter, PhyMode);
3373                         MlmeUpdateTxRates(pAdapter, FALSE, 0);
3374                         MakeIbssBeacon(pAdapter);           // re-build BEACON frame
3375                         AsicEnableIbssSync(pAdapter);   // copy to on-chip memory
3376                     }
3377                 }
3378                 DBGPRINT(RT_DEBUG_TRACE, ("Set::RT_OID_802_11_SET_STA_CONFIG (Burst=%d, Protection=%ld,ShortSlot=%d\n",
3379                                         pAdapter->CommonCfg.bEnableTxBurst,
3380                                         pAdapter->CommonCfg.UseBGProtection,
3381                                         pAdapter->CommonCfg.bUseShortSlotTime));
3382             }
3383             break;
3384         case OID_802_11_DESIRED_RATES:
3385             if (wrq->u.data.length != sizeof(NDIS_802_11_RATES))
3386                 Status  = -EINVAL;
3387             else
3388             {
3389                 Status = copy_from_user(&aryRates, wrq->u.data.pointer, wrq->u.data.length);
3390                 NdisZeroMemory(pAdapter->CommonCfg.DesireRate, MAX_LEN_OF_SUPPORTED_RATES);
3391                 NdisMoveMemory(pAdapter->CommonCfg.DesireRate, &aryRates, sizeof(NDIS_802_11_RATES));
3392                 DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_DESIRED_RATES (%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x)\n",
3393                     pAdapter->CommonCfg.DesireRate[0],pAdapter->CommonCfg.DesireRate[1],
3394                     pAdapter->CommonCfg.DesireRate[2],pAdapter->CommonCfg.DesireRate[3],
3395                     pAdapter->CommonCfg.DesireRate[4],pAdapter->CommonCfg.DesireRate[5],
3396                     pAdapter->CommonCfg.DesireRate[6],pAdapter->CommonCfg.DesireRate[7] ));
3397                 // Changing DesiredRate may affect the MAX TX rate we used to TX frames out
3398                 MlmeUpdateTxRates(pAdapter, FALSE, 0);
3399             }
3400             break;
3401         case RT_OID_802_11_PREAMBLE:
3402             if (wrq->u.data.length != sizeof(RT_802_11_PREAMBLE))
3403                 Status  = -EINVAL;
3404             else
3405             {
3406                 Status = copy_from_user(&Preamble, wrq->u.data.pointer, wrq->u.data.length);
3407                 if (Preamble == Rt802_11PreambleShort)
3408                 {
3409                     pAdapter->CommonCfg.TxPreamble = Preamble;
3410                     MlmeSetTxPreamble(pAdapter, Rt802_11PreambleShort);
3411                 }
3412                 else if ((Preamble == Rt802_11PreambleLong) || (Preamble == Rt802_11PreambleAuto))
3413                 {
3414                     // if user wants AUTO, initialize to LONG here, then change according to AP's
3415                     // capability upon association.
3416                     pAdapter->CommonCfg.TxPreamble = Preamble;
3417                     MlmeSetTxPreamble(pAdapter, Rt802_11PreambleLong);
3418                 }
3419                 else
3420                 {
3421                     Status = -EINVAL;
3422                     break;
3423                 }
3424                 DBGPRINT(RT_DEBUG_TRACE, ("Set::RT_OID_802_11_PREAMBLE (=%d)\n", Preamble));
3425             }
3426             break;
3427         case OID_802_11_WEP_STATUS:
3428             if (wrq->u.data.length != sizeof(NDIS_802_11_WEP_STATUS))
3429                 Status  = -EINVAL;
3430             else
3431             {
3432                 Status = copy_from_user(&WepStatus, wrq->u.data.pointer, wrq->u.data.length);
3433                 // Since TKIP, AES, WEP are all supported. It should not have any invalid setting
3434                 if (WepStatus <= Ndis802_11Encryption3KeyAbsent)
3435                 {
3436                     if (pAdapter->StaCfg.WepStatus != WepStatus)
3437                     {
3438                         // Config has changed
3439                         pAdapter->bConfigChanged = TRUE;
3440                     }
3441                     pAdapter->StaCfg.WepStatus     = WepStatus;
3442                     pAdapter->StaCfg.OrigWepStatus = WepStatus;
3443                     pAdapter->StaCfg.PairCipher    = WepStatus;
3444                         pAdapter->StaCfg.GroupCipher   = WepStatus;
3445                 }
3446                 else
3447                 {
3448                     Status  = -EINVAL;
3449                     break;
3450                 }
3451                 DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_WEP_STATUS (=%d)\n",WepStatus));
3452             }
3453             break;
3454         case OID_802_11_AUTHENTICATION_MODE:
3455             if (wrq->u.data.length != sizeof(NDIS_802_11_AUTHENTICATION_MODE))
3456                 Status  = -EINVAL;
3457             else
3458             {
3459                 Status = copy_from_user(&AuthMode, wrq->u.data.pointer, wrq->u.data.length);
3460                 if (AuthMode > Ndis802_11AuthModeMax)
3461                 {
3462                     Status  = -EINVAL;
3463                     break;
3464                 }
3465                 else
3466                 {
3467                     if (pAdapter->StaCfg.AuthMode != AuthMode)
3468                     {
3469                         // Config has changed
3470                         pAdapter->bConfigChanged = TRUE;
3471                     }
3472                     pAdapter->StaCfg.AuthMode = AuthMode;
3473                 }
3474                 pAdapter->StaCfg.PortSecured = WPA_802_1X_PORT_NOT_SECURED;
3475                 DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_AUTHENTICATION_MODE (=%d) \n",pAdapter->StaCfg.AuthMode));
3476             }
3477             break;
3478         case OID_802_11_INFRASTRUCTURE_MODE:
3479             if (wrq->u.data.length != sizeof(NDIS_802_11_NETWORK_INFRASTRUCTURE))
3480                 Status  = -EINVAL;
3481             else
3482             {
3483                 Status = copy_from_user(&BssType, wrq->u.data.pointer, wrq->u.data.length);
3484
3485                                 if (BssType == Ndis802_11IBSS)
3486                                         Set_NetworkType_Proc(pAdapter, "Adhoc");
3487                                 else if (BssType == Ndis802_11Infrastructure)
3488                                         Set_NetworkType_Proc(pAdapter, "Infra");
3489                                 else if (BssType == Ndis802_11Monitor)
3490                                         Set_NetworkType_Proc(pAdapter, "Monitor");
3491                                 else
3492                                 {
3493                                         Status  = -EINVAL;
3494                                         DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_INFRASTRUCTURE_MODE (unknown)\n"));
3495                                 }
3496                         }
3497                         break;
3498          case OID_802_11_REMOVE_WEP:
3499             DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_REMOVE_WEP\n"));
3500             if (wrq->u.data.length != sizeof(NDIS_802_11_KEY_INDEX))
3501             {
3502                                 Status = -EINVAL;
3503             }
3504             else
3505             {
3506                                 KeyIdx = *(NDIS_802_11_KEY_INDEX *) wrq->u.data.pointer;
3507
3508                                 if (KeyIdx & 0x80000000)
3509                                 {
3510                                         // Should never set default bit when remove key
3511                                         Status = -EINVAL;
3512                                 }
3513                                 else
3514                                 {
3515                                         KeyIdx = KeyIdx & 0x0fffffff;
3516                                         if (KeyIdx >= 4){
3517                                                 Status = -EINVAL;
3518                                         }
3519                                         else
3520                                         {
3521                                                 pAdapter->SharedKey[BSS0][KeyIdx].KeyLen = 0;
3522                                                 pAdapter->SharedKey[BSS0][KeyIdx].CipherAlg = CIPHER_NONE;
3523                                                 AsicRemoveSharedKeyEntry(pAdapter, 0, (UCHAR)KeyIdx);
3524                                         }
3525                                 }
3526             }
3527             break;
3528         case RT_OID_802_11_RESET_COUNTERS:
3529             NdisZeroMemory(&pAdapter->WlanCounters, sizeof(COUNTER_802_11));
3530             NdisZeroMemory(&pAdapter->Counters8023, sizeof(COUNTER_802_3));
3531             NdisZeroMemory(&pAdapter->RalinkCounters, sizeof(COUNTER_RALINK));
3532             pAdapter->Counters8023.RxNoBuffer   = 0;
3533                         pAdapter->Counters8023.GoodReceives = 0;
3534                         pAdapter->Counters8023.RxNoBuffer   = 0;
3535             DBGPRINT(RT_DEBUG_TRACE, ("Set::RT_OID_802_11_RESET_COUNTERS \n"));
3536             break;
3537         case OID_802_11_RTS_THRESHOLD:
3538             if (wrq->u.data.length != sizeof(NDIS_802_11_RTS_THRESHOLD))
3539                 Status  = -EINVAL;
3540             else
3541             {
3542                 Status = copy_from_user(&RtsThresh, wrq->u.data.pointer, wrq->u.data.length);
3543                 if (RtsThresh > MAX_RTS_THRESHOLD)
3544                     Status  = -EINVAL;
3545                 else
3546                     pAdapter->CommonCfg.RtsThreshold = (USHORT)RtsThresh;
3547             }
3548             DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_RTS_THRESHOLD (=%ld)\n",RtsThresh));
3549             break;
3550         case OID_802_11_FRAGMENTATION_THRESHOLD:
3551             if (wrq->u.data.length != sizeof(NDIS_802_11_FRAGMENTATION_THRESHOLD))
3552                 Status  = -EINVAL;
3553             else
3554             {
3555                 Status = copy_from_user(&FragThresh, wrq->u.data.pointer, wrq->u.data.length);
3556                 pAdapter->CommonCfg.bUseZeroToDisableFragment = FALSE;
3557                 if (FragThresh > MAX_FRAG_THRESHOLD || FragThresh < MIN_FRAG_THRESHOLD)
3558                 {
3559                     if (FragThresh == 0)
3560                     {
3561                         pAdapter->CommonCfg.FragmentThreshold = MAX_FRAG_THRESHOLD;
3562                         pAdapter->CommonCfg.bUseZeroToDisableFragment = TRUE;
3563                     }
3564                     else
3565                         Status  = -EINVAL;
3566                 }
3567                 else
3568                     pAdapter->CommonCfg.FragmentThreshold = (USHORT)FragThresh;
3569             }
3570             DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_FRAGMENTATION_THRESHOLD (=%ld) \n",FragThresh));
3571             break;
3572         case OID_802_11_POWER_MODE:
3573             if (wrq->u.data.length != sizeof(NDIS_802_11_POWER_MODE))
3574                 Status = -EINVAL;
3575             else
3576             {
3577                 Status = copy_from_user(&PowerMode, wrq->u.data.pointer, wrq->u.data.length);
3578                 if (PowerMode == Ndis802_11PowerModeCAM)
3579                         Set_PSMode_Proc(pAdapter, "CAM");
3580                 else if (PowerMode == Ndis802_11PowerModeMAX_PSP)
3581                         Set_PSMode_Proc(pAdapter, "Max_PSP");
3582                 else if (PowerMode == Ndis802_11PowerModeFast_PSP)
3583                                         Set_PSMode_Proc(pAdapter, "Fast_PSP");
3584                 else if (PowerMode == Ndis802_11PowerModeLegacy_PSP)
3585                                         Set_PSMode_Proc(pAdapter, "Legacy_PSP");
3586                 else
3587                     Status = -EINVAL;
3588             }
3589             DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_POWER_MODE (=%d)\n",PowerMode));
3590             break;
3591          case RT_OID_802_11_TX_POWER_LEVEL_1:
3592                         if (wrq->u.data.length  < sizeof(ULONG))
3593                                 Status = -EINVAL;
3594                         else
3595                         {
3596                                 Status = copy_from_user(&PowerTemp, wrq->u.data.pointer, wrq->u.data.length);
3597                                 if (PowerTemp > 100)
3598                                         PowerTemp = 0xffffffff;  // AUTO
3599                                 pAdapter->CommonCfg.TxPowerDefault = PowerTemp; //keep current setting.
3600                                         pAdapter->CommonCfg.TxPowerPercentage = pAdapter->CommonCfg.TxPowerDefault;
3601                 DBGPRINT(RT_DEBUG_TRACE, ("Set::RT_OID_802_11_TX_POWER_LEVEL_1 (=%ld)\n", pAdapter->CommonCfg.TxPowerPercentage));
3602                         }
3603                 break;
3604                 case OID_802_11_NETWORK_TYPE_IN_USE:
3605                         if (wrq->u.data.length != sizeof(NDIS_802_11_NETWORK_TYPE))
3606                                 Status = -EINVAL;
3607                         else
3608                         {
3609                                 Status = copy_from_user(&NetType, wrq->u.data.pointer, wrq->u.data.length);
3610
3611                                 if (NetType == Ndis802_11DS)
3612                                         RTMPSetPhyMode(pAdapter, PHY_11B);
3613                                 else if (NetType == Ndis802_11OFDM24)
3614                                         RTMPSetPhyMode(pAdapter, PHY_11BG_MIXED);
3615                                 else if (NetType == Ndis802_11OFDM5)
3616                                         RTMPSetPhyMode(pAdapter, PHY_11A);
3617                                 else
3618                                         Status = -EINVAL;
3619 #ifdef DOT11_N_SUPPORT
3620                                 if (Status == NDIS_STATUS_SUCCESS)
3621                                         SetCommonHT(pAdapter);
3622 #endif // DOT11_N_SUPPORT //
3623                 DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_NETWORK_TYPE_IN_USE (=%d)\n",NetType));
3624                     }
3625                         break;
3626         // For WPA PSK PMK key
3627         case RT_OID_802_11_ADD_WPA:
3628             pKey = kmalloc(wrq->u.data.length, MEM_ALLOC_FLAG);
3629             if(pKey == NULL)
3630             {
3631                 Status = -ENOMEM;
3632                 break;
3633             }
3634
3635             Status = copy_from_user(pKey, wrq->u.data.pointer, wrq->u.data.length);
3636             if (pKey->Length != wrq->u.data.length)
3637             {
3638                 Status  = -EINVAL;
3639                 DBGPRINT(RT_DEBUG_TRACE, ("Set::RT_OID_802_11_ADD_WPA, Failed!!\n"));
3640             }
3641             else
3642             {
3643                 if ((pAdapter->StaCfg.AuthMode != Ndis802_11AuthModeWPAPSK) &&
3644                                     (pAdapter->StaCfg.AuthMode != Ndis802_11AuthModeWPA2PSK) &&
3645                                     (pAdapter->StaCfg.AuthMode != Ndis802_11AuthModeWPANone) )
3646                 {
3647                     Status = -EOPNOTSUPP;
3648                     DBGPRINT(RT_DEBUG_TRACE, ("Set::RT_OID_802_11_ADD_WPA, Failed!! [AuthMode != WPAPSK/WPA2PSK/WPANONE]\n"));
3649                 }
3650                 else if ((pAdapter->StaCfg.AuthMode == Ndis802_11AuthModeWPAPSK) ||
3651                                                  (pAdapter->StaCfg.AuthMode == Ndis802_11AuthModeWPA2PSK) ||
3652                                                  (pAdapter->StaCfg.AuthMode == Ndis802_11AuthModeWPANone) )     // Only for WPA PSK mode
3653                                 {
3654                     NdisMoveMemory(pAdapter->StaCfg.PMK, &pKey->KeyMaterial, pKey->KeyLength);
3655                     // Use RaConfig as PSK agent.
3656                     // Start STA supplicant state machine
3657                     if (pAdapter->StaCfg.AuthMode != Ndis802_11AuthModeWPANone)
3658                         pAdapter->StaCfg.WpaState = SS_START;
3659
3660                     DBGPRINT(RT_DEBUG_TRACE, ("Set::RT_OID_802_11_ADD_WPA (id=0x%x, Len=%d-byte)\n", pKey->KeyIndex, pKey->KeyLength));
3661                 }
3662                 else
3663                 {
3664                     pAdapter->StaCfg.WpaState = SS_NOTUSE;
3665                     DBGPRINT(RT_DEBUG_TRACE, ("Set::RT_OID_802_11_ADD_WPA (id=0x%x, Len=%d-byte)\n", pKey->KeyIndex, pKey->KeyLength));
3666                 }
3667             }
3668             kfree(pKey);
3669             break;
3670         case OID_802_11_REMOVE_KEY:
3671             pRemoveKey = kmalloc(wrq->u.data.length, MEM_ALLOC_FLAG);
3672             if(pRemoveKey == NULL)
3673             {
3674                 Status = -ENOMEM;
3675                 break;
3676             }
3677
3678             Status = copy_from_user(pRemoveKey, wrq->u.data.pointer, wrq->u.data.length);
3679             if (pRemoveKey->Length != wrq->u.data.length)
3680             {
3681                 Status  = -EINVAL;
3682                 DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_REMOVE_KEY, Failed!!\n"));
3683             }
3684             else
3685             {
3686                 if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
3687                 {
3688                     RTMPWPARemoveKeyProc(pAdapter, pRemoveKey);
3689                     DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_REMOVE_KEY, Remove WPA Key!!\n"));
3690                 }
3691                 else
3692                 {
3693                     KeyIdx = pRemoveKey->KeyIndex;
3694
3695                     if (KeyIdx & 0x80000000)
3696                     {
3697                         // Should never set default bit when remove key
3698                         Status  = -EINVAL;
3699                         DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_REMOVE_KEY, Failed!!(Should never set default bit when remove key)\n"));
3700                     }
3701                     else
3702                     {
3703                         KeyIdx = KeyIdx & 0x0fffffff;
3704                         if (KeyIdx > 3)
3705                         {
3706                             Status  = -EINVAL;
3707                             DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_REMOVE_KEY, Failed!!(KeyId[%d] out of range)\n", KeyIdx));
3708                         }
3709                         else
3710                         {
3711                             pAdapter->SharedKey[BSS0][KeyIdx].KeyLen = 0;
3712                             pAdapter->SharedKey[BSS0][KeyIdx].CipherAlg = CIPHER_NONE;
3713                             AsicRemoveSharedKeyEntry(pAdapter, 0, (UCHAR)KeyIdx);
3714                             DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_REMOVE_KEY (id=0x%x, Len=%d-byte)\n", pRemoveKey->KeyIndex, pRemoveKey->Length));
3715                         }
3716                     }
3717                 }
3718             }
3719             kfree(pRemoveKey);
3720             break;
3721         // New for WPA
3722         case OID_802_11_ADD_KEY:
3723             pKey = kmalloc(wrq->u.data.length, MEM_ALLOC_FLAG);
3724             if(pKey == NULL)
3725             {
3726                 Status = -ENOMEM;
3727                 break;
3728             }
3729             Status = copy_from_user(pKey, wrq->u.data.pointer, wrq->u.data.length);
3730             if (pKey->Length != wrq->u.data.length)
3731             {
3732                 Status  = -EINVAL;
3733                 DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_ADD_KEY, Failed!!\n"));
3734             }
3735             else
3736             {
3737                 RTMPAddKey(pAdapter, pKey);
3738                 DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_ADD_KEY (id=0x%x, Len=%d-byte)\n", pKey->KeyIndex, pKey->KeyLength));
3739             }
3740             kfree(pKey);
3741             break;
3742         case OID_802_11_CONFIGURATION:
3743             if (wrq->u.data.length != sizeof(NDIS_802_11_CONFIGURATION))
3744                 Status  = -EINVAL;
3745             else
3746             {
3747                 Status = copy_from_user(&Config, wrq->u.data.pointer, wrq->u.data.length);
3748                 pConfig = &Config;
3749
3750                 if ((pConfig->BeaconPeriod >= 20) && (pConfig->BeaconPeriod <=400))
3751                      pAdapter->CommonCfg.BeaconPeriod = (USHORT) pConfig->BeaconPeriod;
3752
3753                 pAdapter->StaActive.AtimWin = (USHORT) pConfig->ATIMWindow;
3754                 MAP_KHZ_TO_CHANNEL_ID(pConfig->DSConfig, pAdapter->CommonCfg.Channel);
3755                 //
3756                                 // Save the channel on MlmeAux for CntlOidRTBssidProc used.
3757                                 //
3758                                 pAdapter->MlmeAux.Channel = pAdapter->CommonCfg.Channel;
3759
3760                 DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_CONFIGURATION (BeacnPeriod=%ld,AtimW=%ld,Ch=%d)\n",
3761                     pConfig->BeaconPeriod, pConfig->ATIMWindow, pAdapter->CommonCfg.Channel));
3762                 // Config has changed
3763                 pAdapter->bConfigChanged = TRUE;
3764             }
3765             break;
3766 #ifdef DOT11_N_SUPPORT
3767                 case RT_OID_802_11_SET_HT_PHYMODE:
3768                         if (wrq->u.data.length  != sizeof(OID_SET_HT_PHYMODE))
3769                                 Status = -EINVAL;
3770                         else
3771                         {
3772                             POID_SET_HT_PHYMODE pHTPhyMode = &HT_PhyMode;
3773
3774                                 Status = copy_from_user(&HT_PhyMode, wrq->u.data.pointer, wrq->u.data.length);
3775                                 DBGPRINT(RT_DEBUG_TRACE, ("Set::pHTPhyMode      (PhyMode = %d,TransmitNo = %d, HtMode = %d,     ExtOffset =     %d , MCS = %d, BW =     %d,     STBC = %d, SHORTGI = %d) \n",
3776                                 pHTPhyMode->PhyMode, pHTPhyMode->TransmitNo,pHTPhyMode->HtMode,pHTPhyMode->ExtOffset,
3777                                 pHTPhyMode->MCS, pHTPhyMode->BW, pHTPhyMode->STBC,      pHTPhyMode->SHORTGI));
3778                                 if (pAdapter->CommonCfg.PhyMode >= PHY_11ABGN_MIXED)
3779                                         RTMPSetHT(pAdapter,     pHTPhyMode);
3780                         }
3781                         DBGPRINT(RT_DEBUG_TRACE, ("Set::RT_OID_802_11_SET_HT_PHYMODE(MCS=%d,BW=%d,SGI=%d,STBC=%d)\n",
3782                                 pAdapter->StaCfg.HTPhyMode.field.MCS, pAdapter->StaCfg.HTPhyMode.field.BW, pAdapter->StaCfg.HTPhyMode.field.ShortGI,
3783                                 pAdapter->StaCfg.HTPhyMode.field.STBC));
3784                         break;
3785 #endif // DOT11_N_SUPPORT //
3786                 case RT_OID_802_11_SET_APSD_SETTING:
3787                         if (wrq->u.data.length != sizeof(ULONG))
3788                                 Status = -EINVAL;
3789                         else
3790                         {
3791                                 ULONG apsd ;
3792                                 Status = copy_from_user(&apsd, wrq->u.data.pointer,     wrq->u.data.length);
3793
3794                                 /*-------------------------------------------------------------------
3795                                 |B31~B7 |       B6~B5    |       B4      |       B3      |      B2       |      B1       |         B0           |
3796                                 ---------------------------------------------------------------------
3797                                 | Rsvd  | Max SP Len | AC_VO | AC_VI | AC_BK | AC_BE | APSD     Capable |
3798                                 ---------------------------------------------------------------------*/
3799                                 pAdapter->CommonCfg.bAPSDCapable = (apsd & 0x00000001) ? TRUE : FALSE;
3800                                 pAdapter->CommonCfg.bAPSDAC_BE = ((apsd & 0x00000002) >> 1)     ? TRUE : FALSE;
3801                                 pAdapter->CommonCfg.bAPSDAC_BK = ((apsd & 0x00000004) >> 2)     ? TRUE : FALSE;
3802                                 pAdapter->CommonCfg.bAPSDAC_VI = ((apsd & 0x00000008) >> 3)     ? TRUE : FALSE;
3803                                 pAdapter->CommonCfg.bAPSDAC_VO = ((apsd & 0x00000010) >> 4)     ? TRUE : FALSE;
3804                                 pAdapter->CommonCfg.MaxSPLength = (UCHAR)((apsd & 0x00000060) >> 5);
3805
3806                                 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,
3807                                         pAdapter->CommonCfg.bAPSDAC_BE, pAdapter->CommonCfg.bAPSDAC_BK, pAdapter->CommonCfg.bAPSDAC_VI, pAdapter->CommonCfg.bAPSDAC_VO, pAdapter->CommonCfg.MaxSPLength));
3808                         }
3809                         break;
3810
3811                 case RT_OID_802_11_SET_APSD_PSM:
3812                         if (wrq->u.data.length  != sizeof(ULONG))
3813                                 Status = -EINVAL;
3814                         else
3815                         {
3816                                 // Driver needs to notify AP when PSM changes
3817                                 Status = copy_from_user(&pAdapter->CommonCfg.bAPSDForcePowerSave, wrq->u.data.pointer, wrq->u.data.length);
3818                                 if (pAdapter->CommonCfg.bAPSDForcePowerSave     != pAdapter->StaCfg.Psm)
3819                                 {
3820                                         MlmeSetPsmBit(pAdapter, pAdapter->CommonCfg.bAPSDForcePowerSave);
3821                                         RTMPSendNullFrame(pAdapter,     pAdapter->CommonCfg.TxRate,     TRUE);
3822                                 }
3823                                 DBGPRINT(RT_DEBUG_TRACE, ("Set::RT_OID_802_11_SET_APSD_PSM (bAPSDForcePowerSave:%d)\n", pAdapter->CommonCfg.bAPSDForcePowerSave));
3824                         }
3825                         break;
3826
3827                 case RT_OID_802_11_SET_WMM:
3828                         if (wrq->u.data.length  != sizeof(BOOLEAN))
3829                                 Status = -EINVAL;
3830                         else
3831                         {
3832                                 Status = copy_from_user(&pAdapter->CommonCfg.bWmmCapable, wrq->u.data.pointer, wrq->u.data.length);
3833                                 DBGPRINT(RT_DEBUG_TRACE, ("Set::RT_OID_802_11_SET_WMM (=%d)     \n", pAdapter->CommonCfg.bWmmCapable));
3834                         }
3835                         break;
3836
3837                 case OID_802_11_DISASSOCIATE:
3838                         //
3839                         // Set NdisRadioStateOff to     TRUE, instead of called MlmeRadioOff.
3840                         // Later on, NDIS_802_11_BSSID_LIST_EX->NumberOfItems should be 0
3841                         // when query OID_802_11_BSSID_LIST.
3842                         //
3843                         // TRUE:  NumberOfItems will set to     0.
3844                         // FALSE: NumberOfItems no change.
3845                         //
3846                         pAdapter->CommonCfg.NdisRadioStateOff = TRUE;
3847                         // Set to immediately send the media disconnect event
3848                         pAdapter->MlmeAux.CurrReqIsFromNdis     = TRUE;
3849                         DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_DISASSOCIATE \n"));
3850
3851                         if (INFRA_ON(pAdapter))
3852                         {
3853                                 if (pAdapter->Mlme.CntlMachine.CurrState !=     CNTL_IDLE)
3854                                 {
3855                                         RT28XX_MLME_RESET_STATE_MACHINE(pAdapter);
3856                                         DBGPRINT(RT_DEBUG_TRACE, ("!!! MLME     busy, reset     MLME state machine !!!\n"));
3857                                 }
3858
3859                                 MlmeEnqueue(pAdapter,
3860                                         MLME_CNTL_STATE_MACHINE,
3861                                         OID_802_11_DISASSOCIATE,
3862                                         0,
3863                                         NULL);
3864
3865                                 StateMachineTouched     = TRUE;
3866                         }
3867                         break;
3868
3869 #ifdef DOT11_N_SUPPORT
3870                 case RT_OID_802_11_SET_IMME_BA_CAP:
3871                                 if (wrq->u.data.length != sizeof(OID_BACAP_STRUC))
3872                                         Status = -EINVAL;
3873                                 else
3874                                 {
3875                                         OID_BACAP_STRUC Orde ;
3876                                         Status = copy_from_user(&Orde, wrq->u.data.pointer, wrq->u.data.length);
3877                                         if (Orde.Policy > BA_NOTUSE)
3878                                         {
3879                                                 Status = NDIS_STATUS_INVALID_DATA;
3880                                         }
3881                                         else if (Orde.Policy == BA_NOTUSE)
3882                                         {
3883                                                 pAdapter->CommonCfg.BACapability.field.Policy = BA_NOTUSE;
3884                                                 pAdapter->CommonCfg.BACapability.field.MpduDensity = Orde.MpduDensity;
3885                                                 pAdapter->CommonCfg.DesiredHtPhy.MpduDensity = Orde.MpduDensity;
3886                                                 pAdapter->CommonCfg.DesiredHtPhy.AmsduEnable = Orde.AmsduEnable;
3887                                                 pAdapter->CommonCfg.DesiredHtPhy.AmsduSize= Orde.AmsduSize;
3888                                                 pAdapter->CommonCfg.DesiredHtPhy.MimoPs= Orde.MMPSmode;
3889                                                 pAdapter->CommonCfg.BACapability.field.MMPSmode = Orde.MMPSmode;
3890                                                 // UPdata to HT IE
3891                                                 pAdapter->CommonCfg.HtCapability.HtCapInfo.MimoPs = Orde.MMPSmode;
3892                                                 pAdapter->CommonCfg.HtCapability.HtCapInfo.AMsduSize = Orde.AmsduSize;
3893                                                 pAdapter->CommonCfg.HtCapability.HtCapParm.MpduDensity = Orde.MpduDensity;
3894                                         }
3895                                         else
3896                                         {
3897                         pAdapter->CommonCfg.BACapability.field.AutoBA = Orde.AutoBA;
3898                                                 pAdapter->CommonCfg.BACapability.field.Policy = IMMED_BA; // we only support immediate BA.
3899                                                 pAdapter->CommonCfg.BACapability.field.MpduDensity = Orde.MpduDensity;
3900                                                 pAdapter->CommonCfg.DesiredHtPhy.MpduDensity = Orde.MpduDensity;
3901                                                 pAdapter->CommonCfg.DesiredHtPhy.AmsduEnable = Orde.AmsduEnable;
3902                                                 pAdapter->CommonCfg.DesiredHtPhy.AmsduSize= Orde.AmsduSize;
3903                                                 pAdapter->CommonCfg.DesiredHtPhy.MimoPs = Orde.MMPSmode;
3904                                                 pAdapter->CommonCfg.BACapability.field.MMPSmode = Orde.MMPSmode;
3905
3906                                                 // UPdata to HT IE
3907                                                 pAdapter->CommonCfg.HtCapability.HtCapInfo.MimoPs = Orde.MMPSmode;
3908                                                 pAdapter->CommonCfg.HtCapability.HtCapInfo.AMsduSize = Orde.AmsduSize;
3909                                                 pAdapter->CommonCfg.HtCapability.HtCapParm.MpduDensity = Orde.MpduDensity;
3910
3911                                                 if (pAdapter->CommonCfg.BACapability.field.RxBAWinLimit > MAX_RX_REORDERBUF)
3912                                                         pAdapter->CommonCfg.BACapability.field.RxBAWinLimit = MAX_RX_REORDERBUF;
3913
3914                                         }
3915
3916                                         pAdapter->CommonCfg.REGBACapability.word = pAdapter->CommonCfg.BACapability.word;
3917                                         DBGPRINT(RT_DEBUG_TRACE, ("Set::(Orde.AutoBA = %d) (Policy=%d)(ReBAWinLimit=%d)(TxBAWinLimit=%d)(AutoMode=%d)\n",Orde.AutoBA, pAdapter->CommonCfg.BACapability.field.Policy,
3918                                                 pAdapter->CommonCfg.BACapability.field.RxBAWinLimit,pAdapter->CommonCfg.BACapability.field.TxBAWinLimit, pAdapter->CommonCfg.BACapability.field.AutoBA));
3919                                         DBGPRINT(RT_DEBUG_TRACE, ("Set::(MimoPs = %d)(AmsduEnable = %d) (AmsduSize=%d)(MpduDensity=%d)\n",pAdapter->CommonCfg.DesiredHtPhy.MimoPs, pAdapter->CommonCfg.DesiredHtPhy.AmsduEnable,
3920                                                 pAdapter->CommonCfg.DesiredHtPhy.AmsduSize, pAdapter->CommonCfg.DesiredHtPhy.MpduDensity));
3921                                 }
3922
3923                                 break;
3924                 case RT_OID_802_11_ADD_IMME_BA:
3925                         DBGPRINT(RT_DEBUG_TRACE, (" Set :: RT_OID_802_11_ADD_IMME_BA \n"));
3926                         if (wrq->u.data.length != sizeof(OID_ADD_BA_ENTRY))
3927                                         Status = -EINVAL;
3928                         else
3929                         {
3930                                 UCHAR                   index;
3931                                 OID_ADD_BA_ENTRY    BA;
3932                                 MAC_TABLE_ENTRY     *pEntry;
3933
3934                                 Status = copy_from_user(&BA, wrq->u.data.pointer, wrq->u.data.length);
3935                                 if (BA.TID > 15)
3936                                 {
3937                                         Status = NDIS_STATUS_INVALID_DATA;
3938                                         break;
3939                                 }
3940                                 else
3941                                 {
3942                                         //BATableInsertEntry
3943                                         //As ad-hoc mode, BA pair is not limited to only BSSID. so add via OID.
3944                                         index = BA.TID;
3945                                         // in ad hoc mode, when adding BA pair, we should insert this entry into MACEntry too
3946                                         pEntry = MacTableLookup(pAdapter, BA.MACAddr);
3947                                         if (!pEntry)
3948                                         {
3949                                                 DBGPRINT(RT_DEBUG_TRACE, ("RT_OID_802_11_ADD_IMME_BA. break on no connection.----:%x:%x\n", BA.MACAddr[4], BA.MACAddr[5]));
3950                                                 break;
3951                                         }
3952                                         if (BA.IsRecipient == FALSE)
3953                                         {
3954                                             if (pEntry->bIAmBadAtheros == TRUE)
3955                                                         pAdapter->CommonCfg.BACapability.field.RxBAWinLimit = 0x10;
3956
3957                                                 BAOriSessionSetUp(pAdapter, pEntry, index, 0, 100, TRUE);
3958                                         }
3959                                         else
3960                                         {
3961                                                 //BATableInsertEntry(pAdapter, pEntry->Aid, BA.MACAddr, 0, 0xffff, BA.TID, BA.nMSDU, BA.IsRecipient);
3962                                         }
3963
3964                                         DBGPRINT(RT_DEBUG_TRACE, ("Set::RT_OID_802_11_ADD_IMME_BA. Rec = %d. Mac = %x:%x:%x:%x:%x:%x . \n",
3965                                                 BA.IsRecipient, BA.MACAddr[0], BA.MACAddr[1], BA.MACAddr[2], BA.MACAddr[2]
3966                                                 , BA.MACAddr[4], BA.MACAddr[5]));
3967                                 }
3968                         }
3969                         break;
3970
3971                 case RT_OID_802_11_TEAR_IMME_BA:
3972                         DBGPRINT(RT_DEBUG_TRACE, ("Set :: RT_OID_802_11_TEAR_IMME_BA \n"));
3973                         if (wrq->u.data.length != sizeof(OID_ADD_BA_ENTRY))
3974                                         Status = -EINVAL;
3975                         else
3976                         {
3977                                 POID_ADD_BA_ENTRY       pBA;
3978                                 MAC_TABLE_ENTRY *pEntry;
3979
3980                                 pBA = kmalloc(wrq->u.data.length, MEM_ALLOC_FLAG);
3981
3982                                 if (pBA == NULL)
3983                                 {
3984                                         DBGPRINT(RT_DEBUG_TRACE, ("Set :: RT_OID_802_11_TEAR_IMME_BA kmalloc() can't allocate enough memory\n"));
3985                                         Status = NDIS_STATUS_FAILURE;
3986                                 }
3987                                 else
3988                                 {
3989                                         Status = copy_from_user(pBA, wrq->u.data.pointer, wrq->u.data.length);
3990                                         DBGPRINT(RT_DEBUG_TRACE, ("Set :: RT_OID_802_11_TEAR_IMME_BA(TID=%d, bAllTid=%d)\n", pBA->TID, pBA->bAllTid));
3991
3992                                         if (!pBA->bAllTid && (pBA->TID > NUM_OF_TID))
3993                                         {
3994                                                 Status = NDIS_STATUS_INVALID_DATA;
3995                                                 break;
3996                                         }
3997
3998                                         if (pBA->IsRecipient == FALSE)
3999                                         {
4000                                                 pEntry = MacTableLookup(pAdapter, pBA->MACAddr);
4001                                                 DBGPRINT(RT_DEBUG_TRACE, (" pBA->IsRecipient == FALSE\n"));
4002                                                 if (pEntry)
4003                                                 {
4004                                                         DBGPRINT(RT_DEBUG_TRACE, (" pBA->pEntry\n"));
4005                                                         BAOriSessionTearDown(pAdapter, pEntry->Aid, pBA->TID, FALSE, TRUE);
4006                                                 }
4007                                                 else
4008                                                         DBGPRINT(RT_DEBUG_TRACE, ("Set :: Not found pEntry \n"));
4009                                         }
4010                                         else
4011                                         {
4012                                                 pEntry = MacTableLookup(pAdapter, pBA->MACAddr);
4013                                                 if (pEntry)
4014                                                 {
4015                                                         BARecSessionTearDown( pAdapter, (UCHAR)pEntry->Aid, pBA->TID, TRUE);
4016                                                 }
4017                                                 else
4018                                                         DBGPRINT(RT_DEBUG_TRACE, ("Set :: Not found pEntry \n"));
4019                                         }
4020                                         kfree(pBA);
4021                                 }
4022             }
4023             break;
4024 #endif // DOT11_N_SUPPORT //
4025
4026         // For WPA_SUPPLICANT to set static wep key
4027         case OID_802_11_ADD_WEP:
4028             pWepKey = kmalloc(wrq->u.data.length, MEM_ALLOC_FLAG);
4029
4030             if(pWepKey == NULL)
4031             {
4032                 Status = -ENOMEM;
4033                                 DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_ADD_WEP, Failed!!\n"));
4034                 break;
4035             }
4036             Status = copy_from_user(pWepKey, wrq->u.data.pointer, wrq->u.data.length);
4037             if (Status)
4038             {
4039                 Status  = -EINVAL;
4040                 DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_ADD_WEP, Failed (length mismatch)!!\n"));
4041             }
4042             else
4043             {
4044                         KeyIdx = pWepKey->KeyIndex & 0x0fffffff;
4045                 // KeyIdx must be 0 ~ 3
4046                 if (KeyIdx > 4)
4047                         {
4048                     Status  = -EINVAL;
4049                     DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_ADD_WEP, Failed (KeyIdx must be smaller than 4)!!\n"));
4050                 }
4051                 else
4052                 {
4053                     UCHAR CipherAlg = 0;
4054                     PUCHAR Key;
4055
4056                     // set key material and key length
4057                     NdisZeroMemory(pAdapter->SharedKey[BSS0][KeyIdx].Key, 16);
4058                     pAdapter->SharedKey[BSS0][KeyIdx].KeyLen = (UCHAR) pWepKey->KeyLength;
4059                     NdisMoveMemory(pAdapter->SharedKey[BSS0][KeyIdx].Key, &pWepKey->KeyMaterial, pWepKey->KeyLength);
4060
4061                     switch(pWepKey->KeyLength)
4062                     {
4063                         case 5:
4064                             CipherAlg = CIPHER_WEP64;
4065                             break;
4066                         case 13:
4067                             CipherAlg = CIPHER_WEP128;
4068                             break;
4069                         default:
4070                             DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_ADD_WEP, only support CIPHER_WEP64(len:5) & CIPHER_WEP128(len:13)!!\n"));
4071                             Status = -EINVAL;
4072                             break;
4073                     }
4074                     pAdapter->SharedKey[BSS0][KeyIdx].CipherAlg = CipherAlg;
4075
4076                     // Default key for tx (shared key)
4077                     if (pWepKey->KeyIndex & 0x80000000)
4078                     {
4079 #ifdef WPA_SUPPLICANT_SUPPORT
4080                         // set key material and key length
4081                         NdisZeroMemory(pAdapter->StaCfg.DesireSharedKey[KeyIdx].Key, 16);
4082                         pAdapter->StaCfg.DesireSharedKey[KeyIdx].KeyLen = (UCHAR) pWepKey->KeyLength;
4083                         NdisMoveMemory(pAdapter->StaCfg.DesireSharedKey[KeyIdx].Key, &pWepKey->KeyMaterial, pWepKey->KeyLength);
4084                         pAdapter->StaCfg.DesireSharedKeyId = KeyIdx;
4085                         pAdapter->StaCfg.DesireSharedKey[KeyIdx].CipherAlg = CipherAlg;
4086 #endif // WPA_SUPPLICANT_SUPPORT //
4087                         pAdapter->StaCfg.DefaultKeyId = (UCHAR) KeyIdx;
4088                     }
4089
4090 #ifdef WPA_SUPPLICANT_SUPPORT
4091                                         if ((pAdapter->StaCfg.WpaSupplicantUP != 0) &&
4092                                                 (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA))
4093                                         {
4094                                                 Key = pWepKey->KeyMaterial;
4095
4096                                                 // Set Group key material to Asic
4097                                         AsicAddSharedKeyEntry(pAdapter, BSS0, KeyIdx, CipherAlg, Key, NULL, NULL);
4098
4099                                                 // Update WCID attribute table and IVEIV table for this group key table
4100                                                 RTMPAddWcidAttributeEntry(pAdapter, BSS0, KeyIdx, CipherAlg, NULL);
4101
4102                                                 STA_PORT_SECURED(pAdapter);
4103
4104                                         // Indicate Connected for GUI
4105                                         pAdapter->IndicateMediaState = NdisMediaStateConnected;
4106                                         }
4107                     else if (pAdapter->StaCfg.PortSecured == WPA_802_1X_PORT_SECURED)
4108 #endif // WPA_SUPPLICANT_SUPPORT
4109                     {
4110                         Key = pAdapter->SharedKey[BSS0][KeyIdx].Key;
4111
4112                         // Set key material and cipherAlg to Asic
4113                                         AsicAddSharedKeyEntry(pAdapter, BSS0, KeyIdx, CipherAlg, Key, NULL, NULL);
4114
4115                         if (pWepKey->KeyIndex & 0x80000000)
4116                         {
4117                             PMAC_TABLE_ENTRY pEntry = &pAdapter->MacTab.Content[BSSID_WCID];
4118                             // Assign group key info
4119                                                 RTMPAddWcidAttributeEntry(pAdapter, BSS0, KeyIdx, CipherAlg, NULL);
4120                                                 // Assign pairwise key info
4121                                                 RTMPAddWcidAttributeEntry(pAdapter, BSS0, KeyIdx, CipherAlg, pEntry);
4122                         }
4123                     }
4124                                         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"));
4125                                 }
4126             }
4127             kfree(pWepKey);
4128             break;
4129 #ifdef WPA_SUPPLICANT_SUPPORT
4130             case OID_SET_COUNTERMEASURES:
4131             if (wrq->u.data.length != sizeof(int))
4132                 Status  = -EINVAL;
4133             else
4134             {
4135                 int enabled = 0;
4136                 Status = copy_from_user(&enabled, wrq->u.data.pointer, wrq->u.data.length);
4137                 if (enabled == 1)
4138                     pAdapter->StaCfg.bBlockAssoc = TRUE;
4139                 else
4140                     // WPA MIC error should block association attempt for 60 seconds
4141                     pAdapter->StaCfg.bBlockAssoc = FALSE;
4142                 DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_SET_COUNTERMEASURES bBlockAssoc=%s\n", pAdapter->StaCfg.bBlockAssoc ? "TRUE":"FALSE"));
4143             }
4144                 break;
4145         case RT_OID_WPA_SUPPLICANT_SUPPORT:
4146                         if (wrq->u.data.length != sizeof(UCHAR))
4147                 Status  = -EINVAL;
4148             else
4149             {
4150                 Status = copy_from_user(&wpa_supplicant_enable, wrq->u.data.pointer, wrq->u.data.length);
4151                         pAdapter->StaCfg.WpaSupplicantUP = wpa_supplicant_enable;
4152                         DBGPRINT(RT_DEBUG_TRACE, ("Set::RT_OID_WPA_SUPPLICANT_SUPPORT (=%d)\n", pAdapter->StaCfg.WpaSupplicantUP));
4153                         }
4154             break;
4155         case OID_802_11_DEAUTHENTICATION:
4156             if (wrq->u.data.length != sizeof(MLME_DEAUTH_REQ_STRUCT))
4157                 Status  = -EINVAL;
4158             else
4159             {
4160                 MLME_DEAUTH_REQ_STRUCT      *pInfo;
4161                                 MLME_QUEUE_ELEM *MsgElem = (MLME_QUEUE_ELEM *) kmalloc(sizeof(MLME_QUEUE_ELEM), MEM_ALLOC_FLAG);
4162
4163                 pInfo = (MLME_DEAUTH_REQ_STRUCT *) MsgElem->Msg;
4164                 Status = copy_from_user(pInfo, wrq->u.data.pointer, wrq->u.data.length);
4165                 MlmeDeauthReqAction(pAdapter, MsgElem);
4166                                 kfree(MsgElem);
4167
4168                 if (INFRA_ON(pAdapter))
4169                 {
4170                     LinkDown(pAdapter, FALSE);
4171                     pAdapter->Mlme.AssocMachine.CurrState = ASSOC_IDLE;
4172                 }
4173                 DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_DEAUTHENTICATION (Reason=%d)\n", pInfo->Reason));
4174             }
4175             break;
4176         case OID_802_11_DROP_UNENCRYPTED:
4177             if (wrq->u.data.length != sizeof(int))
4178                 Status  = -EINVAL;
4179             else
4180             {
4181                 int enabled = 0;
4182                 Status = copy_from_user(&enabled, wrq->u.data.pointer, wrq->u.data.length);
4183                 if (enabled == 1)
4184                     pAdapter->StaCfg.PortSecured = WPA_802_1X_PORT_NOT_SECURED;
4185                 else
4186                     pAdapter->StaCfg.PortSecured = WPA_802_1X_PORT_SECURED;
4187                                 NdisAcquireSpinLock(&pAdapter->MacTabLock);
4188                                 pAdapter->MacTab.Content[BSSID_WCID].PortSecured = pAdapter->StaCfg.PortSecured;
4189                                 NdisReleaseSpinLock(&pAdapter->MacTabLock);
4190                 DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_DROP_UNENCRYPTED (=%d)\n", enabled));
4191             }
4192             break;
4193         case OID_802_11_SET_IEEE8021X:
4194             if (wrq->u.data.length != sizeof(BOOLEAN))
4195                 Status  = -EINVAL;
4196             else
4197             {
4198                 Status = copy_from_user(&IEEE8021xState, wrq->u.data.pointer, wrq->u.data.length);
4199                         pAdapter->StaCfg.IEEE8021X = IEEE8021xState;
4200                 DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_SET_IEEE8021X (=%d)\n", IEEE8021xState));
4201             }
4202             break;
4203         case OID_802_11_SET_IEEE8021X_REQUIRE_KEY:
4204                         if (wrq->u.data.length != sizeof(BOOLEAN))
4205                                  Status  = -EINVAL;
4206             else
4207             {
4208                 Status = copy_from_user(&IEEE8021x_required_keys, wrq->u.data.pointer, wrq->u.data.length);
4209                                 pAdapter->StaCfg.IEEE8021x_required_keys = IEEE8021x_required_keys;
4210                                 DBGPRINT(RT_DEBUG_TRACE, ("Set::OID_802_11_SET_IEEE8021X_REQUIRE_KEY (%d)\n", IEEE8021x_required_keys));
4211                         }
4212                         break;
4213         case OID_802_11_PMKID:
4214                 pPmkId = kmalloc(wrq->u.data.length, MEM_ALLOC_FLAG);
4215
4216                 if(pPmkId == NULL) {
4217                 Status = -ENOMEM;
4218                 break;
4219             }
4220             Status = copy_from_user(pPmkId, wrq->u.data.pointer, wrq->u.data.length);
4221
4222                 // check the PMKID information
4223                 if (pPmkId->BSSIDInfoCount == 0)
4224                 NdisZeroMemory(pAdapter->StaCfg.SavedPMK, sizeof(BSSID_INFO)*PMKID_NO);
4225                 else
4226                 {
4227                         PBSSID_INFO     pBssIdInfo;
4228                         UINT            BssIdx;
4229                         UINT            CachedIdx;
4230
4231                         for (BssIdx = 0; BssIdx < pPmkId->BSSIDInfoCount; BssIdx++)
4232                         {
4233                                 // point to the indexed BSSID_INFO structure
4234                                 pBssIdInfo = (PBSSID_INFO) ((PUCHAR) pPmkId + 2 * sizeof(UINT) + BssIdx * sizeof(BSSID_INFO));
4235                                 // Find the entry in the saved data base.
4236                                 for (CachedIdx = 0; CachedIdx < pAdapter->StaCfg.SavedPMKNum; CachedIdx++)
4237                                 {
4238                                         // compare the BSSID
4239                                         if (NdisEqualMemory(pBssIdInfo->BSSID, pAdapter->StaCfg.SavedPMK[CachedIdx].BSSID, sizeof(NDIS_802_11_MAC_ADDRESS)))
4240                                                 break;
4241                                 }
4242
4243                                 // Found, replace it
4244                                 if (CachedIdx < PMKID_NO)
4245                                 {
4246                                         DBGPRINT(RT_DEBUG_OFF, ("Update OID_802_11_PMKID, idx = %d\n", CachedIdx));
4247                                         NdisMoveMemory(&pAdapter->StaCfg.SavedPMK[CachedIdx], pBssIdInfo, sizeof(BSSID_INFO));
4248                                         pAdapter->StaCfg.SavedPMKNum++;
4249                                 }
4250                                 // Not found, replace the last one
4251                                 else
4252                                 {
4253                                         // Randomly replace one
4254                                         CachedIdx = (pBssIdInfo->BSSID[5] % PMKID_NO);
4255                                         DBGPRINT(RT_DEBUG_OFF, ("Update OID_802_11_PMKID, idx = %d\n", CachedIdx));
4256                                         NdisMoveMemory(&pAdapter->StaCfg.SavedPMK[CachedIdx], pBssIdInfo, sizeof(BSSID_INFO));
4257                                 }
4258                         }
4259                         }
4260                         if(pPmkId)
4261                                 kfree(pPmkId);
4262                 break;
4263 #endif // WPA_SUPPLICANT_SUPPORT //
4264
4265         default:
4266             DBGPRINT(RT_DEBUG_TRACE, ("Set::unknown IOCTL's subcmd = 0x%08x\n", cmd));
4267             Status = -EOPNOTSUPP;
4268             break;
4269     }
4270
4271
4272     return Status;
4273 }
4274
4275 INT RTMPQueryInformation(
4276     IN  PRTMP_ADAPTER pAdapter,
4277     IN  OUT struct ifreq    *rq,
4278     IN  INT                 cmd)
4279 {
4280     struct iwreq                        *wrq = (struct iwreq *) rq;
4281     NDIS_802_11_BSSID_LIST_EX           *pBssidList = NULL;
4282     PNDIS_WLAN_BSSID_EX                 pBss;
4283     NDIS_802_11_SSID                    Ssid;
4284     NDIS_802_11_CONFIGURATION           *pConfiguration = NULL;
4285     RT_802_11_LINK_STATUS               *pLinkStatus = NULL;
4286     RT_802_11_STA_CONFIG                *pStaConfig = NULL;
4287     NDIS_802_11_STATISTICS              *pStatistics = NULL;
4288     NDIS_802_11_RTS_THRESHOLD           RtsThresh;
4289     NDIS_802_11_FRAGMENTATION_THRESHOLD FragThresh;
4290     NDIS_802_11_POWER_MODE              PowerMode;
4291     NDIS_802_11_NETWORK_INFRASTRUCTURE  BssType;
4292     RT_802_11_PREAMBLE                  PreamType;
4293     NDIS_802_11_AUTHENTICATION_MODE     AuthMode;
4294     NDIS_802_11_WEP_STATUS              WepStatus;
4295     NDIS_MEDIA_STATE                    MediaState;
4296     ULONG                               BssBufSize, ulInfo=0, NetworkTypeList[4], apsd = 0;
4297     USHORT                              BssLen = 0;
4298     PUCHAR                              pBuf = NULL, pPtr;
4299     INT                                 Status = NDIS_STATUS_SUCCESS;
4300     UINT                                we_version_compiled;
4301     UCHAR                               i, Padding = 0;
4302     BOOLEAN                             RadioState;
4303         UCHAR   driverVersion[8];
4304     OID_SET_HT_PHYMODE                          *pHTPhyMode = NULL;
4305
4306     switch(cmd)
4307     {
4308         case RT_OID_DEVICE_NAME:
4309             wrq->u.data.length = sizeof(STA_NIC_DEVICE_NAME);
4310             Status = copy_to_user(wrq->u.data.pointer, STA_NIC_DEVICE_NAME, wrq->u.data.length);
4311             break;
4312         case RT_OID_VERSION_INFO:
4313                         DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_VERSION_INFO \n"));
4314                         wrq->u.data.length = 8*sizeof(UCHAR);
4315                         sprintf(&driverVersion[0], "%s", STA_DRIVER_VERSION);
4316                         driverVersion[7] = '\0';
4317                         if (copy_to_user(wrq->u.data.pointer, &driverVersion, wrq->u.data.length))
4318             {
4319                                 Status = -EFAULT;
4320             }
4321             break;
4322         case OID_802_11_BSSID_LIST:
4323             if (RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_BSS_SCAN_IN_PROGRESS))
4324             {
4325                 /*
4326                  * Still scanning, indicate the caller should try again.
4327                  */
4328                 DBGPRINT(RT_DEBUG_TRACE, ("Query::OID_802_11_BSSID_LIST (Still scanning)\n"));
4329                                 return -EAGAIN;
4330             }
4331             DBGPRINT(RT_DEBUG_TRACE, ("Query::OID_802_11_BSSID_LIST (%d BSS returned)\n",pAdapter->ScanTab.BssNr));
4332                         pAdapter->StaCfg.bScanReqIsFromWebUI = FALSE;
4333             // Claculate total buffer size required
4334             BssBufSize = sizeof(ULONG);
4335
4336             for (i = 0; i < pAdapter->ScanTab.BssNr; i++)
4337             {
4338                 // Align pointer to 4 bytes boundary.
4339                 //Padding = 4 - (pAdapter->ScanTab.BssEntry[i].VarIELen & 0x0003);
4340                 //if (Padding == 4)
4341                 //    Padding = 0;
4342                 BssBufSize += (sizeof(NDIS_WLAN_BSSID_EX) - 1 + sizeof(NDIS_802_11_FIXED_IEs) + pAdapter->ScanTab.BssEntry[i].VarIELen + Padding);
4343             }
4344
4345             // For safety issue, we add 256 bytes just in case
4346             BssBufSize += 256;
4347             // Allocate the same size as passed from higher layer
4348             pBuf = kmalloc(BssBufSize, MEM_ALLOC_FLAG);
4349             if(pBuf == NULL)
4350             {
4351                 Status = -ENOMEM;
4352                 break;
4353             }
4354             // Init 802_11_BSSID_LIST_EX structure
4355             NdisZeroMemory(pBuf, BssBufSize);
4356             pBssidList = (PNDIS_802_11_BSSID_LIST_EX) pBuf;
4357             pBssidList->NumberOfItems = pAdapter->ScanTab.BssNr;
4358
4359             // Calculate total buffer length
4360             BssLen = 4; // Consist of NumberOfItems
4361             // Point to start of NDIS_WLAN_BSSID_EX
4362             // pPtr = pBuf + sizeof(ULONG);
4363             pPtr = (PUCHAR) &pBssidList->Bssid[0];
4364             for (i = 0; i < pAdapter->ScanTab.BssNr; i++)
4365             {
4366                 pBss = (PNDIS_WLAN_BSSID_EX) pPtr;
4367                 NdisMoveMemory(&pBss->MacAddress, &pAdapter->ScanTab.BssEntry[i].Bssid, MAC_ADDR_LEN);
4368                 if ((pAdapter->ScanTab.BssEntry[i].Hidden == 1) && (pAdapter->StaCfg.bShowHiddenSSID == FALSE))
4369                 {
4370                     //
4371                                         // We must return this SSID during 4way handshaking, otherwise Aegis will failed to parse WPA infomation
4372                                         // and then failed to send EAPOl farame.
4373                                         //
4374                                         if ((pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA) && (pAdapter->StaCfg.PortSecured != WPA_802_1X_PORT_SECURED))
4375                                         {
4376                                                 pBss->Ssid.SsidLength = pAdapter->ScanTab.BssEntry[i].SsidLen;
4377                                                 NdisMoveMemory(pBss->Ssid.Ssid, pAdapter->ScanTab.BssEntry[i].Ssid, pAdapter->ScanTab.BssEntry[i].SsidLen);
4378                                         }
4379                                         else
4380                         pBss->Ssid.SsidLength = 0;
4381                 }
4382                 else
4383                 {
4384                     pBss->Ssid.SsidLength = pAdapter->ScanTab.BssEntry[i].SsidLen;
4385                     NdisMoveMemory(pBss->Ssid.Ssid, pAdapter->ScanTab.BssEntry[i].Ssid, pAdapter->ScanTab.BssEntry[i].SsidLen);
4386                 }
4387                 pBss->Privacy = pAdapter->ScanTab.BssEntry[i].Privacy;
4388                 pBss->Rssi = pAdapter->ScanTab.BssEntry[i].Rssi - pAdapter->BbpRssiToDbmDelta;
4389                 pBss->NetworkTypeInUse = NetworkTypeInUseSanity(&pAdapter->ScanTab.BssEntry[i]);
4390                 pBss->Configuration.Length = sizeof(NDIS_802_11_CONFIGURATION);
4391                 pBss->Configuration.BeaconPeriod = pAdapter->ScanTab.BssEntry[i].BeaconPeriod;
4392                 pBss->Configuration.ATIMWindow = pAdapter->ScanTab.BssEntry[i].AtimWin;
4393
4394                 MAP_CHANNEL_ID_TO_KHZ(pAdapter->ScanTab.BssEntry[i].Channel, pBss->Configuration.DSConfig);
4395
4396                 if (pAdapter->ScanTab.BssEntry[i].BssType == BSS_INFRA)
4397                     pBss->InfrastructureMode = Ndis802_11Infrastructure;
4398                 else
4399                     pBss->InfrastructureMode = Ndis802_11IBSS;
4400
4401                 NdisMoveMemory(pBss->SupportedRates, pAdapter->ScanTab.BssEntry[i].SupRate, pAdapter->ScanTab.BssEntry[i].SupRateLen);
4402                 NdisMoveMemory(pBss->SupportedRates + pAdapter->ScanTab.BssEntry[i].SupRateLen,
4403                                pAdapter->ScanTab.BssEntry[i].ExtRate,
4404                                pAdapter->ScanTab.BssEntry[i].ExtRateLen);
4405
4406                 if (pAdapter->ScanTab.BssEntry[i].VarIELen == 0)
4407                 {
4408                     pBss->IELength = sizeof(NDIS_802_11_FIXED_IEs);
4409                     NdisMoveMemory(pBss->IEs, &pAdapter->ScanTab.BssEntry[i].FixIEs, sizeof(NDIS_802_11_FIXED_IEs));
4410                     pPtr = pPtr + sizeof(NDIS_WLAN_BSSID_EX) - 1 + sizeof(NDIS_802_11_FIXED_IEs);
4411                 }
4412                 else
4413                 {
4414                     pBss->IELength = (ULONG)(sizeof(NDIS_802_11_FIXED_IEs) + pAdapter->ScanTab.BssEntry[i].VarIELen);
4415                     pPtr = pPtr + sizeof(NDIS_WLAN_BSSID_EX) - 1 + sizeof(NDIS_802_11_FIXED_IEs);
4416                     NdisMoveMemory(pBss->IEs, &pAdapter->ScanTab.BssEntry[i].FixIEs, sizeof(NDIS_802_11_FIXED_IEs));
4417                     NdisMoveMemory(pBss->IEs + sizeof(NDIS_802_11_FIXED_IEs), pAdapter->ScanTab.BssEntry[i].VarIEs, pAdapter->ScanTab.BssEntry[i].VarIELen);
4418                     pPtr += pAdapter->ScanTab.BssEntry[i].VarIELen;
4419                 }
4420                 pBss->Length = (ULONG)(sizeof(NDIS_WLAN_BSSID_EX) - 1 + sizeof(NDIS_802_11_FIXED_IEs) + pAdapter->ScanTab.BssEntry[i].VarIELen + Padding);
4421
4422 #if WIRELESS_EXT < 17
4423                 if ((BssLen + pBss->Length) < wrq->u.data.length)
4424                 BssLen += pBss->Length;
4425                 else
4426                 {
4427                     pBssidList->NumberOfItems = i;
4428                     break;
4429                 }
4430 #else
4431                 BssLen += pBss->Length;
4432 #endif
4433             }
4434
4435 #if WIRELESS_EXT < 17
4436             wrq->u.data.length = BssLen;
4437 #else
4438             if (BssLen > wrq->u.data.length)
4439             {
4440                 kfree(pBssidList);
4441                 return -E2BIG;
4442             }
4443             else
4444                 wrq->u.data.length = BssLen;
4445 #endif
4446             Status = copy_to_user(wrq->u.data.pointer, pBssidList, BssLen);
4447             kfree(pBssidList);
4448             break;
4449         case OID_802_3_CURRENT_ADDRESS:
4450             wrq->u.data.length = MAC_ADDR_LEN;
4451             Status = copy_to_user(wrq->u.data.pointer, &pAdapter->CurrentAddress, wrq->u.data.length);
4452             break;
4453         case OID_GEN_MEDIA_CONNECT_STATUS:
4454             if (pAdapter->IndicateMediaState == NdisMediaStateConnected)
4455                 MediaState = NdisMediaStateConnected;
4456             else
4457                 MediaState = NdisMediaStateDisconnected;
4458
4459             wrq->u.data.length = sizeof(NDIS_MEDIA_STATE);
4460             Status = copy_to_user(wrq->u.data.pointer, &MediaState, wrq->u.data.length);
4461             break;
4462         case OID_802_11_BSSID:
4463             if (INFRA_ON(pAdapter) || ADHOC_ON(pAdapter))
4464             {
4465                 Status = copy_to_user(wrq->u.data.pointer, &pAdapter->CommonCfg.Bssid, sizeof(NDIS_802_11_MAC_ADDRESS));
4466
4467             }
4468             else
4469             {
4470                 DBGPRINT(RT_DEBUG_TRACE, ("Query::OID_802_11_BSSID(=EMPTY)\n"));
4471                 Status = -ENOTCONN;
4472             }
4473             break;
4474         case OID_802_11_SSID:
4475                         NdisZeroMemory(&Ssid, sizeof(NDIS_802_11_SSID));
4476                         NdisZeroMemory(Ssid.Ssid, MAX_LEN_OF_SSID);
4477             Ssid.SsidLength = pAdapter->CommonCfg.SsidLen;
4478                         memcpy(Ssid.Ssid, pAdapter->CommonCfg.Ssid,     Ssid.SsidLength);
4479             wrq->u.data.length = sizeof(NDIS_802_11_SSID);
4480             Status = copy_to_user(wrq->u.data.pointer, &Ssid, wrq->u.data.length);
4481             DBGPRINT(RT_DEBUG_TRACE, ("Query::OID_802_11_SSID (Len=%d, ssid=%s)\n", Ssid.SsidLength,Ssid.Ssid));
4482             break;
4483         case RT_OID_802_11_QUERY_LINK_STATUS:
4484             pLinkStatus = (RT_802_11_LINK_STATUS *) kmalloc(sizeof(RT_802_11_LINK_STATUS), MEM_ALLOC_FLAG);
4485             if (pLinkStatus)
4486             {
4487                 pLinkStatus->CurrTxRate = RateIdTo500Kbps[pAdapter->CommonCfg.TxRate];   // unit : 500 kbps
4488                 pLinkStatus->ChannelQuality = pAdapter->Mlme.ChannelQuality;
4489                 pLinkStatus->RxByteCount = pAdapter->RalinkCounters.ReceivedByteCount;
4490                 pLinkStatus->TxByteCount = pAdapter->RalinkCounters.TransmittedByteCount;
4491                         pLinkStatus->CentralChannel = pAdapter->CommonCfg.CentralChannel;
4492                 wrq->u.data.length = sizeof(RT_802_11_LINK_STATUS);
4493                 Status = copy_to_user(wrq->u.data.pointer, pLinkStatus, wrq->u.data.length);
4494                 kfree(pLinkStatus);
4495                 DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_802_11_QUERY_LINK_STATUS\n"));
4496             }
4497             else
4498             {
4499                 DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_802_11_QUERY_LINK_STATUS(kmalloc failed)\n"));
4500                 Status = -EFAULT;
4501             }
4502             break;
4503         case OID_802_11_CONFIGURATION:
4504             pConfiguration = (NDIS_802_11_CONFIGURATION *) kmalloc(sizeof(NDIS_802_11_CONFIGURATION), MEM_ALLOC_FLAG);
4505             if (pConfiguration)
4506             {
4507                 pConfiguration->Length = sizeof(NDIS_802_11_CONFIGURATION);
4508                 pConfiguration->BeaconPeriod = pAdapter->CommonCfg.BeaconPeriod;
4509                 pConfiguration->ATIMWindow = pAdapter->StaActive.AtimWin;
4510                 MAP_CHANNEL_ID_TO_KHZ(pAdapter->CommonCfg.Channel, pConfiguration->DSConfig);
4511                 wrq->u.data.length = sizeof(NDIS_802_11_CONFIGURATION);
4512                 Status = copy_to_user(wrq->u.data.pointer, pConfiguration, wrq->u.data.length);
4513                 DBGPRINT(RT_DEBUG_TRACE, ("Query::OID_802_11_CONFIGURATION(BeaconPeriod=%ld,AtimW=%ld,Channel=%d) \n",
4514                                         pConfiguration->BeaconPeriod, pConfiguration->ATIMWindow, pAdapter->CommonCfg.Channel));
4515                                 kfree(pConfiguration);
4516             }
4517             else
4518             {
4519                 DBGPRINT(RT_DEBUG_TRACE, ("Query::OID_802_11_CONFIGURATION(kmalloc failed)\n"));
4520                 Status = -EFAULT;
4521             }
4522             break;
4523                 case RT_OID_802_11_SNR_0:
4524                         if ((pAdapter->StaCfg.LastSNR0 > 0))
4525                         {
4526                                 ulInfo = ((0xeb - pAdapter->StaCfg.LastSNR0) * 3) /     16 ;
4527                                 wrq->u.data.length = sizeof(ulInfo);
4528                                 Status = copy_to_user(wrq->u.data.pointer, &ulInfo,     wrq->u.data.length);
4529                                 DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_802_11_SNR_0(0x=%lx)\n", ulInfo));
4530                         }
4531             else
4532                             Status = -EFAULT;
4533                         break;
4534                 case RT_OID_802_11_SNR_1:
4535                         if ((pAdapter->Antenna.field.RxPath     > 1) &&
4536                 (pAdapter->StaCfg.LastSNR1 > 0))
4537                         {
4538                                 ulInfo = ((0xeb - pAdapter->StaCfg.LastSNR1) * 3) /     16 ;
4539                                 wrq->u.data.length = sizeof(ulInfo);
4540                                 Status = copy_to_user(wrq->u.data.pointer, &ulInfo,     wrq->u.data.length);
4541                                 DBGPRINT(RT_DEBUG_TRACE,("Query::RT_OID_802_11_SNR_1(0x=%lx)\n",ulInfo));
4542                         }
4543                         else
4544                                 Status = -EFAULT;
4545             DBGPRINT(RT_DEBUG_TRACE,("Query::RT_OID_802_11_SNR_1(pAdapter->StaCfg.LastSNR1=%d)\n",pAdapter->StaCfg.LastSNR1));
4546                         break;
4547         case OID_802_11_RSSI_TRIGGER:
4548             ulInfo = pAdapter->StaCfg.RssiSample.LastRssi0 - pAdapter->BbpRssiToDbmDelta;
4549             wrq->u.data.length = sizeof(ulInfo);
4550             Status = copy_to_user(wrq->u.data.pointer, &ulInfo, wrq->u.data.length);
4551             DBGPRINT(RT_DEBUG_TRACE, ("Query::OID_802_11_RSSI_TRIGGER(=%ld)\n", ulInfo));
4552             break;
4553                 case OID_802_11_RSSI:
4554         case RT_OID_802_11_RSSI:
4555                         ulInfo = pAdapter->StaCfg.RssiSample.LastRssi0;
4556                         wrq->u.data.length = sizeof(ulInfo);
4557                         Status = copy_to_user(wrq->u.data.pointer, &ulInfo,     wrq->u.data.length);
4558                         break;
4559                 case RT_OID_802_11_RSSI_1:
4560             ulInfo = pAdapter->StaCfg.RssiSample.LastRssi1;
4561                         wrq->u.data.length = sizeof(ulInfo);
4562                         Status = copy_to_user(wrq->u.data.pointer, &ulInfo,     wrq->u.data.length);
4563                         break;
4564         case RT_OID_802_11_RSSI_2:
4565             ulInfo = pAdapter->StaCfg.RssiSample.LastRssi2;
4566                         wrq->u.data.length = sizeof(ulInfo);
4567                         Status = copy_to_user(wrq->u.data.pointer, &ulInfo,     wrq->u.data.length);
4568                         break;
4569         case OID_802_11_STATISTICS:
4570             pStatistics = (NDIS_802_11_STATISTICS *) kmalloc(sizeof(NDIS_802_11_STATISTICS), MEM_ALLOC_FLAG);
4571             if (pStatistics)
4572             {
4573                 DBGPRINT(RT_DEBUG_TRACE, ("Query::OID_802_11_STATISTICS \n"));
4574                 // add the most up-to-date h/w raw counters into software counters
4575                             NICUpdateRawCounters(pAdapter);
4576
4577                 // Sanity check for calculation of sucessful count
4578                 if (pAdapter->WlanCounters.TransmittedFragmentCount.QuadPart < pAdapter->WlanCounters.RetryCount.QuadPart)
4579                     pAdapter->WlanCounters.TransmittedFragmentCount.QuadPart = pAdapter->WlanCounters.RetryCount.QuadPart;
4580
4581                 pStatistics->TransmittedFragmentCount.QuadPart = pAdapter->WlanCounters.TransmittedFragmentCount.QuadPart;
4582                 pStatistics->MulticastTransmittedFrameCount.QuadPart = pAdapter->WlanCounters.MulticastTransmittedFrameCount.QuadPart;
4583                 pStatistics->FailedCount.QuadPart = pAdapter->WlanCounters.FailedCount.QuadPart;
4584                 pStatistics->RetryCount.QuadPart = pAdapter->WlanCounters.RetryCount.QuadPart;
4585                 pStatistics->MultipleRetryCount.QuadPart = pAdapter->WlanCounters.MultipleRetryCount.QuadPart;
4586                 pStatistics->RTSSuccessCount.QuadPart = pAdapter->WlanCounters.RTSSuccessCount.QuadPart;
4587                 pStatistics->RTSFailureCount.QuadPart = pAdapter->WlanCounters.RTSFailureCount.QuadPart;
4588                 pStatistics->ACKFailureCount.QuadPart = pAdapter->WlanCounters.ACKFailureCount.QuadPart;
4589                 pStatistics->FrameDuplicateCount.QuadPart = pAdapter->WlanCounters.FrameDuplicateCount.QuadPart;
4590                 pStatistics->ReceivedFragmentCount.QuadPart = pAdapter->WlanCounters.ReceivedFragmentCount.QuadPart;
4591                 pStatistics->MulticastReceivedFrameCount.QuadPart = pAdapter->WlanCounters.MulticastReceivedFrameCount.QuadPart;
4592 #ifdef DBG
4593                 pStatistics->FCSErrorCount = pAdapter->RalinkCounters.RealFcsErrCount;
4594 #else
4595                 pStatistics->FCSErrorCount.QuadPart = pAdapter->WlanCounters.FCSErrorCount.QuadPart;
4596                 pStatistics->FrameDuplicateCount.u.LowPart = pAdapter->WlanCounters.FrameDuplicateCount.u.LowPart / 100;
4597 #endif
4598                 wrq->u.data.length = sizeof(NDIS_802_11_STATISTICS);
4599                 Status = copy_to_user(wrq->u.data.pointer, pStatistics, wrq->u.data.length);
4600                 kfree(pStatistics);
4601             }
4602             else
4603             {
4604                 DBGPRINT(RT_DEBUG_TRACE, ("Query::OID_802_11_STATISTICS(kmalloc failed)\n"));
4605                 Status = -EFAULT;
4606             }
4607             break;
4608         case OID_GEN_RCV_OK:
4609             ulInfo = pAdapter->Counters8023.GoodReceives;
4610             wrq->u.data.length = sizeof(ulInfo);
4611             Status = copy_to_user(wrq->u.data.pointer, &ulInfo, wrq->u.data.length);
4612             break;
4613         case OID_GEN_RCV_NO_BUFFER:
4614             ulInfo = pAdapter->Counters8023.RxNoBuffer;
4615             wrq->u.data.length = sizeof(ulInfo);
4616             Status = copy_to_user(wrq->u.data.pointer, &ulInfo, wrq->u.data.length);
4617             break;
4618         case RT_OID_802_11_PHY_MODE:
4619             ulInfo = (ULONG)pAdapter->CommonCfg.PhyMode;
4620             wrq->u.data.length = sizeof(ulInfo);
4621             Status = copy_to_user(wrq->u.data.pointer, &ulInfo, wrq->u.data.length);
4622             DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_802_11_PHY_MODE (=%ld)\n", ulInfo));
4623             break;
4624         case RT_OID_802_11_STA_CONFIG:
4625             pStaConfig = (RT_802_11_STA_CONFIG *) kmalloc(sizeof(RT_802_11_STA_CONFIG), MEM_ALLOC_FLAG);
4626             if (pStaConfig)
4627             {
4628                 DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_802_11_STA_CONFIG\n"));
4629                 pStaConfig->EnableTxBurst = pAdapter->CommonCfg.bEnableTxBurst;
4630                 pStaConfig->EnableTurboRate = 0;
4631                 pStaConfig->UseBGProtection = pAdapter->CommonCfg.UseBGProtection;
4632                 pStaConfig->UseShortSlotTime = pAdapter->CommonCfg.bUseShortSlotTime;
4633                 //pStaConfig->AdhocMode = pAdapter->StaCfg.AdhocMode;
4634                 pStaConfig->HwRadioStatus = (pAdapter->StaCfg.bHwRadio == TRUE) ? 1 : 0;
4635                 pStaConfig->Rsv1 = 0;
4636                 pStaConfig->SystemErrorBitmap = pAdapter->SystemErrorBitmap;
4637                 wrq->u.data.length = sizeof(RT_802_11_STA_CONFIG);
4638                 Status = copy_to_user(wrq->u.data.pointer, pStaConfig, wrq->u.data.length);
4639                 kfree(pStaConfig);
4640             }
4641             else
4642             {
4643                 DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_802_11_STA_CONFIG(kmalloc failed)\n"));
4644                 Status = -EFAULT;
4645             }
4646             break;
4647         case OID_802_11_RTS_THRESHOLD:
4648             RtsThresh = pAdapter->CommonCfg.RtsThreshold;
4649             wrq->u.data.length = sizeof(RtsThresh);
4650             Status = copy_to_user(wrq->u.data.pointer, &RtsThresh, wrq->u.data.length);
4651             DBGPRINT(RT_DEBUG_TRACE, ("Query::OID_802_11_RTS_THRESHOLD(=%ld)\n", RtsThresh));
4652             break;
4653         case OID_802_11_FRAGMENTATION_THRESHOLD:
4654             FragThresh = pAdapter->CommonCfg.FragmentThreshold;
4655             if (pAdapter->CommonCfg.bUseZeroToDisableFragment == TRUE)
4656                 FragThresh = 0;
4657             wrq->u.data.length = sizeof(FragThresh);
4658             Status = copy_to_user(wrq->u.data.pointer, &FragThresh, wrq->u.data.length);
4659             DBGPRINT(RT_DEBUG_TRACE, ("Query::OID_802_11_FRAGMENTATION_THRESHOLD(=%ld)\n", FragThresh));
4660             break;
4661         case OID_802_11_POWER_MODE:
4662             PowerMode = pAdapter->StaCfg.WindowsPowerMode;
4663             wrq->u.data.length = sizeof(PowerMode);
4664             Status = copy_to_user(wrq->u.data.pointer, &PowerMode, wrq->u.data.length);
4665             DBGPRINT(RT_DEBUG_TRACE, ("Query::OID_802_11_POWER_MODE(=%d)\n", PowerMode));
4666             break;
4667         case RT_OID_802_11_RADIO:
4668             RadioState = (BOOLEAN) pAdapter->StaCfg.bSwRadio;
4669             wrq->u.data.length = sizeof(RadioState);
4670             Status = copy_to_user(wrq->u.data.pointer, &RadioState, wrq->u.data.length);
4671             DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_802_11_QUERY_RADIO (=%d)\n", RadioState));
4672             break;
4673         case OID_802_11_INFRASTRUCTURE_MODE:
4674             if (pAdapter->StaCfg.BssType == BSS_ADHOC)
4675                 BssType = Ndis802_11IBSS;
4676             else if (pAdapter->StaCfg.BssType == BSS_INFRA)
4677                 BssType = Ndis802_11Infrastructure;
4678             else if (pAdapter->StaCfg.BssType == BSS_MONITOR)
4679                 BssType = Ndis802_11Monitor;
4680             else
4681                 BssType = Ndis802_11AutoUnknown;
4682
4683             wrq->u.data.length = sizeof(BssType);
4684             Status = copy_to_user(wrq->u.data.pointer, &BssType, wrq->u.data.length);
4685             DBGPRINT(RT_DEBUG_TRACE, ("Query::OID_802_11_INFRASTRUCTURE_MODE(=%d)\n", BssType));
4686             break;
4687         case RT_OID_802_11_PREAMBLE:
4688             PreamType = pAdapter->CommonCfg.TxPreamble;
4689             wrq->u.data.length = sizeof(PreamType);
4690             Status = copy_to_user(wrq->u.data.pointer, &PreamType, wrq->u.data.length);
4691             DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_802_11_PREAMBLE(=%d)\n", PreamType));
4692             break;
4693         case OID_802_11_AUTHENTICATION_MODE:
4694             AuthMode = pAdapter->StaCfg.AuthMode;
4695             wrq->u.data.length = sizeof(AuthMode);
4696             Status = copy_to_user(wrq->u.data.pointer, &AuthMode, wrq->u.data.length);
4697             DBGPRINT(RT_DEBUG_TRACE, ("Query::OID_802_11_AUTHENTICATION_MODE(=%d)\n", AuthMode));
4698             break;
4699         case OID_802_11_WEP_STATUS:
4700             WepStatus = pAdapter->StaCfg.WepStatus;
4701             wrq->u.data.length = sizeof(WepStatus);
4702             Status = copy_to_user(wrq->u.data.pointer, &WepStatus, wrq->u.data.length);
4703             DBGPRINT(RT_DEBUG_TRACE, ("Query::OID_802_11_WEP_STATUS(=%d)\n", WepStatus));
4704             break;
4705         case OID_802_11_TX_POWER_LEVEL:
4706                         wrq->u.data.length = sizeof(ULONG);
4707                         Status = copy_to_user(wrq->u.data.pointer, &pAdapter->CommonCfg.TxPower, wrq->u.data.length);
4708                         DBGPRINT(RT_DEBUG_TRACE, ("Query::OID_802_11_TX_POWER_LEVEL %x\n",pAdapter->CommonCfg.TxPower));
4709                         break;
4710         case RT_OID_802_11_TX_POWER_LEVEL_1:
4711             wrq->u.data.length = sizeof(ULONG);
4712             Status = copy_to_user(wrq->u.data.pointer, &pAdapter->CommonCfg.TxPowerPercentage, wrq->u.data.length);
4713                         DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_802_11_TX_POWER_LEVEL_1 (=%ld)\n", pAdapter->CommonCfg.TxPowerPercentage));
4714                         break;
4715         case OID_802_11_NETWORK_TYPES_SUPPORTED:
4716                         if ((pAdapter->RfIcType == RFIC_2850) || (pAdapter->RfIcType == RFIC_2750))
4717                         {
4718                                 NetworkTypeList[0] = 3;                 // NumberOfItems = 3
4719                                 NetworkTypeList[1] = Ndis802_11DS;      // NetworkType[1] = 11b
4720                                 NetworkTypeList[2] = Ndis802_11OFDM24;  // NetworkType[2] = 11g
4721                                 NetworkTypeList[3] = Ndis802_11OFDM5;   // NetworkType[3] = 11a
4722                 wrq->u.data.length = 16;
4723                                 Status = copy_to_user(wrq->u.data.pointer, &NetworkTypeList[0], wrq->u.data.length);
4724                         }
4725                         else
4726                         {
4727                                 NetworkTypeList[0] = 2;                 // NumberOfItems = 2
4728                                 NetworkTypeList[1] = Ndis802_11DS;      // NetworkType[1] = 11b
4729                                 NetworkTypeList[2] = Ndis802_11OFDM24;  // NetworkType[2] = 11g
4730                             wrq->u.data.length = 12;
4731                                 Status = copy_to_user(wrq->u.data.pointer, &NetworkTypeList[0], wrq->u.data.length);
4732                         }
4733                         DBGPRINT(RT_DEBUG_TRACE, ("Query::OID_802_11_NETWORK_TYPES_SUPPORTED\n"));
4734                                 break;
4735             case OID_802_11_NETWORK_TYPE_IN_USE:
4736             wrq->u.data.length = sizeof(ULONG);
4737                         if (pAdapter->CommonCfg.PhyMode == PHY_11A)
4738                                 ulInfo = Ndis802_11OFDM5;
4739                         else if ((pAdapter->CommonCfg.PhyMode == PHY_11BG_MIXED) || (pAdapter->CommonCfg.PhyMode == PHY_11G))
4740                                 ulInfo = Ndis802_11OFDM24;
4741                         else
4742                                 ulInfo = Ndis802_11DS;
4743             Status = copy_to_user(wrq->u.data.pointer, &ulInfo, wrq->u.data.length);
4744                         break;
4745         case RT_OID_802_11_QUERY_LAST_RX_RATE:
4746             ulInfo = (ULONG)pAdapter->LastRxRate;
4747             wrq->u.data.length = sizeof(ulInfo);
4748                         Status = copy_to_user(wrq->u.data.pointer, &ulInfo, wrq->u.data.length);
4749                         DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_802_11_QUERY_LAST_RX_RATE (=%ld)\n", ulInfo));
4750                         break;
4751                 case RT_OID_802_11_QUERY_LAST_TX_RATE:
4752                         //ulInfo = (ULONG)pAdapter->LastTxRate;
4753                         ulInfo = (ULONG)pAdapter->MacTab.Content[BSSID_WCID].HTPhyMode.word;
4754                         wrq->u.data.length = sizeof(ulInfo);
4755                         Status = copy_to_user(wrq->u.data.pointer, &ulInfo,     wrq->u.data.length);
4756                         DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_802_11_QUERY_LAST_TX_RATE (=%lx)\n", ulInfo));
4757                         break;
4758         case RT_OID_802_11_QUERY_EEPROM_VERSION:
4759             wrq->u.data.length = sizeof(ULONG);
4760             Status = copy_to_user(wrq->u.data.pointer, &pAdapter->EepromVersion, wrq->u.data.length);
4761             break;
4762         case RT_OID_802_11_QUERY_FIRMWARE_VERSION:
4763             wrq->u.data.length = sizeof(ULONG);
4764             Status = copy_to_user(wrq->u.data.pointer, &pAdapter->FirmwareVersion, wrq->u.data.length);
4765                         break;
4766             case RT_OID_802_11_QUERY_NOISE_LEVEL:
4767                         wrq->u.data.length = sizeof(UCHAR);
4768                         Status = copy_to_user(wrq->u.data.pointer, &pAdapter->BbpWriteLatch[66], wrq->u.data.length);
4769                         DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_802_11_QUERY_NOISE_LEVEL (=%d)\n", pAdapter->BbpWriteLatch[66]));
4770                         break;
4771             case RT_OID_802_11_EXTRA_INFO:
4772                         wrq->u.data.length = sizeof(ULONG);
4773                         Status = copy_to_user(wrq->u.data.pointer, &pAdapter->ExtraInfo, wrq->u.data.length);
4774                 DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_802_11_EXTRA_INFO (=%ld)\n", pAdapter->ExtraInfo));
4775                 break;
4776             case RT_OID_WE_VERSION_COMPILED:
4777                 wrq->u.data.length = sizeof(UINT);
4778                 we_version_compiled = WIRELESS_EXT;
4779                 Status = copy_to_user(wrq->u.data.pointer, &we_version_compiled, wrq->u.data.length);
4780                 break;
4781                 case RT_OID_802_11_QUERY_APSD_SETTING:
4782                         apsd = (pAdapter->CommonCfg.bAPSDCapable | (pAdapter->CommonCfg.bAPSDAC_BE << 1) | (pAdapter->CommonCfg.bAPSDAC_BK << 2)
4783                                 | (pAdapter->CommonCfg.bAPSDAC_VI << 3) | (pAdapter->CommonCfg.bAPSDAC_VO << 4) | (pAdapter->CommonCfg.MaxSPLength << 5));
4784
4785                         wrq->u.data.length = sizeof(ULONG);
4786                         Status = copy_to_user(wrq->u.data.pointer, &apsd, wrq->u.data.length);
4787                         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",
4788                                 apsd,pAdapter->CommonCfg.bAPSDCapable,pAdapter->CommonCfg.bAPSDAC_BE,pAdapter->CommonCfg.bAPSDAC_BK,pAdapter->CommonCfg.bAPSDAC_VI,pAdapter->CommonCfg.bAPSDAC_VO,pAdapter->CommonCfg.MaxSPLength));
4789                         break;
4790                 case RT_OID_802_11_QUERY_APSD_PSM:
4791                         wrq->u.data.length = sizeof(ULONG);
4792                         Status = copy_to_user(wrq->u.data.pointer, &pAdapter->CommonCfg.bAPSDForcePowerSave, wrq->u.data.length);
4793                         DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_802_11_QUERY_APSD_PSM (=%d)\n", pAdapter->CommonCfg.bAPSDForcePowerSave));
4794                         break;
4795                 case RT_OID_802_11_QUERY_WMM:
4796                         wrq->u.data.length = sizeof(BOOLEAN);
4797                         Status = copy_to_user(wrq->u.data.pointer, &pAdapter->CommonCfg.bWmmCapable, wrq->u.data.length);
4798                         DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_802_11_QUERY_WMM (=%d)\n",     pAdapter->CommonCfg.bWmmCapable));
4799                         break;
4800 #ifdef WPA_SUPPLICANT_SUPPORT
4801         case RT_OID_NEW_DRIVER:
4802             {
4803                 UCHAR enabled = 1;
4804                 wrq->u.data.length = sizeof(UCHAR);
4805                 Status = copy_to_user(wrq->u.data.pointer, &enabled, wrq->u.data.length);
4806                 DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_NEW_DRIVER (=%d)\n", enabled));
4807             }
4808                 break;
4809         case RT_OID_WPA_SUPPLICANT_SUPPORT:
4810                 wrq->u.data.length = sizeof(UCHAR);
4811                 Status = copy_to_user(wrq->u.data.pointer, &pAdapter->StaCfg.WpaSupplicantUP, wrq->u.data.length);
4812             DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_WPA_SUPPLICANT_SUPPORT (=%d)\n", pAdapter->StaCfg.WpaSupplicantUP));
4813                 break;
4814 #endif // WPA_SUPPLICANT_SUPPORT //
4815
4816         case RT_OID_DRIVER_DEVICE_NAME:
4817             DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_DRIVER_DEVICE_NAME \n"));
4818                         wrq->u.data.length = 16;
4819                         if (copy_to_user(wrq->u.data.pointer, pAdapter->StaCfg.dev_name, wrq->u.data.length))
4820                         {
4821                                 Status = -EFAULT;
4822                         }
4823             break;
4824         case RT_OID_802_11_QUERY_HT_PHYMODE:
4825             pHTPhyMode = (OID_SET_HT_PHYMODE *) kmalloc(sizeof(OID_SET_HT_PHYMODE), MEM_ALLOC_FLAG);
4826             if (pHTPhyMode)
4827             {
4828                 pHTPhyMode->PhyMode = pAdapter->CommonCfg.PhyMode;
4829                         pHTPhyMode->HtMode = (UCHAR)pAdapter->MacTab.Content[BSSID_WCID].HTPhyMode.field.MODE;
4830                         pHTPhyMode->BW = (UCHAR)pAdapter->MacTab.Content[BSSID_WCID].HTPhyMode.field.BW;
4831                         pHTPhyMode->MCS= (UCHAR)pAdapter->MacTab.Content[BSSID_WCID].HTPhyMode.field.MCS;
4832                         pHTPhyMode->SHORTGI= (UCHAR)pAdapter->MacTab.Content[BSSID_WCID].HTPhyMode.field.ShortGI;
4833                         pHTPhyMode->STBC= (UCHAR)pAdapter->MacTab.Content[BSSID_WCID].HTPhyMode.field.STBC;
4834
4835                         pHTPhyMode->ExtOffset = ((pAdapter->CommonCfg.CentralChannel < pAdapter->CommonCfg.Channel) ? (EXTCHA_BELOW) : (EXTCHA_ABOVE));
4836                 wrq->u.data.length = sizeof(OID_SET_HT_PHYMODE);
4837                 if (copy_to_user(wrq->u.data.pointer, pHTPhyMode, wrq->u.data.length))
4838                         {
4839                                 Status = -EFAULT;
4840                         }
4841                         DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_802_11_QUERY_HT_PHYMODE (PhyMode = %d, MCS =%d, BW = %d, STBC = %d, ExtOffset=%d)\n",
4842                                 pHTPhyMode->HtMode, pHTPhyMode->MCS, pHTPhyMode->BW, pHTPhyMode->STBC, pHTPhyMode->ExtOffset));
4843                         DBGPRINT(RT_DEBUG_TRACE, (" MlmeUpdateTxRates (.word = %x )\n", pAdapter->MacTab.Content[BSSID_WCID].HTPhyMode.word));
4844             }
4845             else
4846             {
4847                 DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_802_11_STA_CONFIG(kmalloc failed)\n"));
4848                 Status = -EFAULT;
4849             }
4850             break;
4851         case RT_OID_802_11_COUNTRY_REGION:
4852             DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_802_11_COUNTRY_REGION \n"));
4853                         wrq->u.data.length = sizeof(ulInfo);
4854             ulInfo = pAdapter->CommonCfg.CountryRegionForABand;
4855             ulInfo = (ulInfo << 8)|(pAdapter->CommonCfg.CountryRegion);
4856                         if (copy_to_user(wrq->u.data.pointer, &ulInfo, wrq->u.data.length))
4857             {
4858                                 Status = -EFAULT;
4859             }
4860             break;
4861         case RT_OID_802_11_QUERY_DAT_HT_PHYMODE:
4862             pHTPhyMode = (OID_SET_HT_PHYMODE *) kmalloc(sizeof(OID_SET_HT_PHYMODE), MEM_ALLOC_FLAG);
4863             if (pHTPhyMode)
4864             {
4865                 pHTPhyMode->PhyMode = pAdapter->CommonCfg.PhyMode;
4866                         pHTPhyMode->HtMode = (UCHAR)pAdapter->CommonCfg.RegTransmitSetting.field.HTMODE;
4867                         pHTPhyMode->BW = (UCHAR)pAdapter->CommonCfg.RegTransmitSetting.field.BW;
4868                         pHTPhyMode->MCS= (UCHAR)pAdapter->StaCfg.DesiredTransmitSetting.field.MCS;
4869                         pHTPhyMode->SHORTGI= (UCHAR)pAdapter->CommonCfg.RegTransmitSetting.field.ShortGI;
4870                         pHTPhyMode->STBC= (UCHAR)pAdapter->CommonCfg.RegTransmitSetting.field.STBC;
4871
4872                 wrq->u.data.length = sizeof(OID_SET_HT_PHYMODE);
4873                 if (copy_to_user(wrq->u.data.pointer, pHTPhyMode, wrq->u.data.length))
4874                         {
4875                                 Status = -EFAULT;
4876                         }
4877                         DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_802_11_QUERY_HT_PHYMODE (PhyMode = %d, MCS =%d, BW = %d, STBC = %d, ExtOffset=%d)\n",
4878                                 pHTPhyMode->HtMode, pHTPhyMode->MCS, pHTPhyMode->BW, pHTPhyMode->STBC, pHTPhyMode->ExtOffset));
4879                         DBGPRINT(RT_DEBUG_TRACE, (" MlmeUpdateTxRates (.word = %x )\n", pAdapter->MacTab.Content[BSSID_WCID].HTPhyMode.word));
4880             }
4881             else
4882             {
4883                 DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_802_11_STA_CONFIG(kmalloc failed)\n"));
4884                 Status = -EFAULT;
4885             }
4886             break;
4887         case RT_OID_QUERY_MULTIPLE_CARD_SUPPORT:
4888                         wrq->u.data.length = sizeof(UCHAR);
4889             i = 0;
4890                         if (copy_to_user(wrq->u.data.pointer, &i, wrq->u.data.length))
4891             {
4892                                 Status = -EFAULT;
4893             }
4894             DBGPRINT(RT_DEBUG_TRACE, ("Query::RT_OID_QUERY_MULTIPLE_CARD_SUPPORT(=%d) \n", i));
4895             break;
4896
4897                 case OID_802_11_BUILD_CHANNEL_EX:
4898                         {
4899                                 UCHAR value;
4900                                 DBGPRINT(RT_DEBUG_TRACE, ("Query::OID_802_11_BUILD_CHANNEL_EX \n"));
4901                                 wrq->u.data.length = sizeof(UCHAR);
4902                                 DBGPRINT(RT_DEBUG_TRACE, ("Doesn't support EXT_BUILD_CHANNEL_LIST.\n"));
4903                                 value = 0;
4904                                 Status = copy_to_user(wrq->u.data.pointer, &value, 1);
4905                                 DBGPRINT(RT_DEBUG_TRACE, ("Status=%d\n", Status));
4906                         }
4907                         break;
4908
4909                 case OID_802_11_GET_CH_LIST:
4910                         {
4911                                 PRT_CHANNEL_LIST_INFO pChListBuf;
4912
4913                                 DBGPRINT(RT_DEBUG_TRACE, ("Query::OID_802_11_GET_CH_LIST \n"));
4914                                 if (pAdapter->ChannelListNum == 0)
4915                                 {
4916                                         wrq->u.data.length = 0;
4917                                         break;
4918                                 }
4919
4920                                 pChListBuf = (RT_CHANNEL_LIST_INFO *) kmalloc(sizeof(RT_CHANNEL_LIST_INFO), MEM_ALLOC_FLAG);
4921                                 if (pChListBuf == NULL)
4922                                 {
4923                                         wrq->u.data.length = 0;
4924                                         break;
4925                                 }
4926
4927                                 pChListBuf->ChannelListNum = pAdapter->ChannelListNum;
4928                                 for (i = 0; i < pChListBuf->ChannelListNum; i++)
4929                                         pChListBuf->ChannelList[i] = pAdapter->ChannelList[i].Channel;
4930
4931                                 wrq->u.data.length = sizeof(RT_CHANNEL_LIST_INFO);
4932                                 Status = copy_to_user(wrq->u.data.pointer, pChListBuf, sizeof(RT_CHANNEL_LIST_INFO));
4933                                 DBGPRINT(RT_DEBUG_TRACE, ("Status=%d\n", Status));
4934
4935                                 if (pChListBuf)
4936                                         kfree(pChListBuf);
4937                         }
4938                         break;
4939
4940                 case OID_802_11_GET_COUNTRY_CODE:
4941                         DBGPRINT(RT_DEBUG_TRACE, ("Query::OID_802_11_GET_COUNTRY_CODE \n"));
4942                         wrq->u.data.length = 2;
4943                         Status = copy_to_user(wrq->u.data.pointer, &pAdapter->CommonCfg.CountryCode, 2);
4944                         DBGPRINT(RT_DEBUG_TRACE, ("Status=%d\n", Status));
4945                         break;
4946
4947                 case OID_802_11_GET_CHANNEL_GEOGRAPHY:
4948                         DBGPRINT(RT_DEBUG_TRACE, ("Query::OID_802_11_GET_CHANNEL_GEOGRAPHY \n"));
4949                         wrq->u.data.length = 1;
4950                         Status = copy_to_user(wrq->u.data.pointer, &pAdapter->CommonCfg.Geography, 1);
4951                         DBGPRINT(RT_DEBUG_TRACE, ("Status=%d\n", Status));
4952                         break;
4953
4954         default:
4955             DBGPRINT(RT_DEBUG_TRACE, ("Query::unknown IOCTL's subcmd = 0x%08x\n", cmd));
4956             Status = -EOPNOTSUPP;
4957             break;
4958     }
4959     return Status;
4960 }
4961
4962 INT rt28xx_sta_ioctl(
4963         IN      struct net_device       *net_dev,
4964         IN      OUT     struct ifreq    *rq,
4965         IN      INT                                     cmd)
4966 {
4967         POS_COOKIE                      pObj;
4968         VIRTUAL_ADAPTER         *pVirtualAd = NULL;
4969         RTMP_ADAPTER        *pAd = NULL;
4970         struct iwreq        *wrq = (struct iwreq *) rq;
4971         BOOLEAN                         StateMachineTouched = FALSE;
4972         INT                                     Status = NDIS_STATUS_SUCCESS;
4973         USHORT                          subcmd;
4974
4975         if (net_dev->priv_flags == INT_MAIN)
4976         {
4977                 pAd = net_dev->ml_priv;
4978         }
4979         else
4980         {
4981                 pVirtualAd = net_dev->ml_priv;
4982                 pAd = pVirtualAd->RtmpDev->ml_priv;
4983         }
4984         pObj = (POS_COOKIE) pAd->OS_Cookie;
4985
4986         if (pAd == NULL)
4987         {
4988                 /* if 1st open fail, pAd will be free;
4989                    So the net_dev->ml_priv will be NULL in 2rd open */
4990                 return -ENETDOWN;
4991         }
4992
4993     //check if the interface is down
4994     if(!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_INTERRUPT_IN_USE))
4995     {
4996         {
4997             DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
4998                     return -ENETDOWN;
4999         }
5000     }
5001
5002         {       // determine this ioctl command is comming from which interface.
5003                 pObj->ioctl_if_type = INT_MAIN;
5004                 pObj->ioctl_if = MAIN_MBSSID;
5005         }
5006
5007         switch(cmd)
5008         {
5009         case SIOCGIFHWADDR:
5010                         DBGPRINT(RT_DEBUG_TRACE, ("IOCTL::SIOCGIFHWADDR\n"));
5011                         memcpy(wrq->u.name, pAd->CurrentAddress, ETH_ALEN);
5012                         break;
5013                 case SIOCGIWNAME:
5014         {
5015                 char *name=&wrq->u.name[0];
5016                 rt_ioctl_giwname(net_dev, NULL, name, NULL);
5017                         break;
5018                 }
5019                 case SIOCGIWESSID:  //Get ESSID
5020         {
5021                 struct iw_point *essid=&wrq->u.essid;
5022                 rt_ioctl_giwessid(net_dev, NULL, essid, essid->pointer);
5023                         break;
5024                 }
5025                 case SIOCSIWESSID:  //Set ESSID
5026         {
5027                 struct iw_point *essid=&wrq->u.essid;
5028                 rt_ioctl_siwessid(net_dev, NULL, essid, essid->pointer);
5029                         break;
5030                 }
5031                 case SIOCSIWNWID:   // set network id (the cell)
5032                 case SIOCGIWNWID:   // get network id
5033                         Status = -EOPNOTSUPP;
5034                         break;
5035                 case SIOCSIWFREQ:   //set channel/frequency (Hz)
5036         {
5037                 struct iw_freq *freq=&wrq->u.freq;
5038                 rt_ioctl_siwfreq(net_dev, NULL, freq, NULL);
5039                         break;
5040                 }
5041                 case SIOCGIWFREQ:   // get channel/frequency (Hz)
5042         {
5043                 struct iw_freq *freq=&wrq->u.freq;
5044                 rt_ioctl_giwfreq(net_dev, NULL, freq, NULL);
5045                         break;
5046                 }
5047                 case SIOCSIWNICKN: //set node name/nickname
5048         {
5049                 struct iw_point *data=&wrq->u.data;
5050                 rt_ioctl_siwnickn(net_dev, NULL, data, NULL);
5051                         break;
5052                 }
5053                 case SIOCGIWNICKN: //get node name/nickname
5054         {
5055                 struct iw_point *data=&wrq->u.data;
5056                 rt_ioctl_giwnickn(net_dev, NULL, data, NULL);
5057                         break;
5058                 }
5059                 case SIOCGIWRATE:   //get default bit rate (bps)
5060                     rt_ioctl_giwrate(net_dev, NULL, &wrq->u, NULL);
5061             break;
5062             case SIOCSIWRATE:  //set default bit rate (bps)
5063                 rt_ioctl_siwrate(net_dev, NULL, &wrq->u, NULL);
5064             break;
5065         case SIOCGIWRTS:  // get RTS/CTS threshold (bytes)
5066         {
5067                 struct iw_param *rts=&wrq->u.rts;
5068                 rt_ioctl_giwrts(net_dev, NULL, rts, NULL);
5069                         break;
5070                 }
5071         case SIOCSIWRTS:  //set RTS/CTS threshold (bytes)
5072         {
5073                 struct iw_param *rts=&wrq->u.rts;
5074                 rt_ioctl_siwrts(net_dev, NULL, rts, NULL);
5075                         break;
5076                 }
5077         case SIOCGIWFRAG:  //get fragmentation thr (bytes)
5078         {
5079                 struct iw_param *frag=&wrq->u.frag;
5080                 rt_ioctl_giwfrag(net_dev, NULL, frag, NULL);
5081                         break;
5082                 }
5083         case SIOCSIWFRAG:  //set fragmentation thr (bytes)
5084         {
5085                 struct iw_param *frag=&wrq->u.frag;
5086                 rt_ioctl_siwfrag(net_dev, NULL, frag, NULL);
5087                         break;
5088                 }
5089         case SIOCGIWENCODE:  //get encoding token & mode
5090         {
5091                 struct iw_point *erq=&wrq->u.encoding;
5092                 if(erq->pointer)
5093                         rt_ioctl_giwencode(net_dev, NULL, erq, erq->pointer);
5094                         break;
5095                 }
5096         case SIOCSIWENCODE:  //set encoding token & mode
5097         {
5098                 struct iw_point *erq=&wrq->u.encoding;
5099                 if(erq->pointer)
5100                         rt_ioctl_siwencode(net_dev, NULL, erq, erq->pointer);
5101                         break;
5102                 }
5103                 case SIOCGIWAP:     //get access point MAC addresses
5104         {
5105                 struct sockaddr *ap_addr=&wrq->u.ap_addr;
5106                 rt_ioctl_giwap(net_dev, NULL, ap_addr, ap_addr->sa_data);
5107                         break;
5108                 }
5109             case SIOCSIWAP:  //set access point MAC addresses
5110         {
5111                 struct sockaddr *ap_addr=&wrq->u.ap_addr;
5112                 rt_ioctl_siwap(net_dev, NULL, ap_addr, ap_addr->sa_data);
5113                         break;
5114                 }
5115                 case SIOCGIWMODE:   //get operation mode
5116         {
5117                 __u32 *mode=&wrq->u.mode;
5118                 rt_ioctl_giwmode(net_dev, NULL, mode, NULL);
5119                         break;
5120                 }
5121                 case SIOCSIWMODE:   //set operation mode
5122         {
5123                 __u32 *mode=&wrq->u.mode;
5124                 rt_ioctl_siwmode(net_dev, NULL, mode, NULL);
5125                         break;
5126                 }
5127                 case SIOCGIWSENS:   //get sensitivity (dBm)
5128                 case SIOCSIWSENS:       //set sensitivity (dBm)
5129                 case SIOCGIWPOWER:  //get Power Management settings
5130                 case SIOCSIWPOWER:  //set Power Management settings
5131                 case SIOCGIWTXPOW:  //get transmit power (dBm)
5132                 case SIOCSIWTXPOW:  //set transmit power (dBm)
5133                 case SIOCGIWRANGE:      //Get range of parameters
5134                 case SIOCGIWRETRY:      //get retry limits and lifetime
5135                 case SIOCSIWRETRY:      //set retry limits and lifetime
5136                         Status = -EOPNOTSUPP;
5137                         break;
5138                 case RT_PRIV_IOCTL:
5139                         subcmd = wrq->u.data.flags;
5140                         if( subcmd & OID_GET_SET_TOGGLE)
5141                                 Status = RTMPSetInformation(pAd, rq, subcmd);
5142                         else
5143                                 Status = RTMPQueryInformation(pAd, rq, subcmd);
5144                         break;
5145                 case SIOCGIWPRIV:
5146                         if (wrq->u.data.pointer)
5147                         {
5148                                 if ( access_ok(VERIFY_WRITE, wrq->u.data.pointer, sizeof(privtab)) != TRUE)
5149                                         break;
5150                                 wrq->u.data.length = sizeof(privtab) / sizeof(privtab[0]);
5151                                 if (copy_to_user(wrq->u.data.pointer, privtab, sizeof(privtab)))
5152                                         Status = -EFAULT;
5153                         }
5154                         break;
5155                 case RTPRIV_IOCTL_SET:
5156                         if(access_ok(VERIFY_READ, wrq->u.data.pointer, wrq->u.data.length) != TRUE)
5157                                 break;
5158                         rt_ioctl_setparam(net_dev, NULL, NULL, wrq->u.data.pointer);
5159                         break;
5160                 case RTPRIV_IOCTL_GSITESURVEY:
5161                         RTMPIoctlGetSiteSurvey(pAd, wrq);
5162                     break;
5163 #ifdef DBG
5164                 case RTPRIV_IOCTL_MAC:
5165                         RTMPIoctlMAC(pAd, wrq);
5166                         break;
5167                 case RTPRIV_IOCTL_E2P:
5168                         RTMPIoctlE2PROM(pAd, wrq);
5169                         break;
5170 #endif // DBG //
5171         case SIOCETHTOOL:
5172                 break;
5173                 default:
5174                         DBGPRINT(RT_DEBUG_ERROR, ("IOCTL::unknown IOCTL's cmd = 0x%08x\n", cmd));
5175                         Status = -EOPNOTSUPP;
5176                         break;
5177         }
5178
5179     if(StateMachineTouched) // Upper layer sent a MLME-related operations
5180         RT28XX_MLME_HANDLER(pAd);
5181
5182         return Status;
5183 }
5184
5185 /*
5186     ==========================================================================
5187     Description:
5188         Set SSID
5189     Return:
5190         TRUE if all parameters are OK, FALSE otherwise
5191     ==========================================================================
5192 */
5193 INT Set_SSID_Proc(
5194     IN  PRTMP_ADAPTER   pAdapter,
5195     IN  PUCHAR          arg)
5196 {
5197     NDIS_802_11_SSID                    Ssid, *pSsid=NULL;
5198     BOOLEAN                             StateMachineTouched = FALSE;
5199     int                                 success = TRUE;
5200
5201     if( strlen(arg) <= MAX_LEN_OF_SSID)
5202     {
5203         NdisZeroMemory(&Ssid, sizeof(NDIS_802_11_SSID));
5204         if (strlen(arg) != 0)
5205         {
5206             NdisMoveMemory(Ssid.Ssid, arg, strlen(arg));
5207             Ssid.SsidLength = strlen(arg);
5208         }
5209         else   //ANY ssid
5210         {
5211             Ssid.SsidLength = 0;
5212                     memcpy(Ssid.Ssid, "", 0);
5213                         pAdapter->StaCfg.BssType = BSS_INFRA;
5214                         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeOpen;
5215                 pAdapter->StaCfg.WepStatus  = Ndis802_11EncryptionDisabled;
5216                 }
5217         pSsid = &Ssid;
5218
5219         if (pAdapter->Mlme.CntlMachine.CurrState != CNTL_IDLE)
5220         {
5221             RT28XX_MLME_RESET_STATE_MACHINE(pAdapter);
5222             DBGPRINT(RT_DEBUG_TRACE, ("!!! MLME busy, reset MLME state machine !!!\n"));
5223         }
5224
5225         pAdapter->MlmeAux.CurrReqIsFromNdis = TRUE;
5226         pAdapter->StaCfg.bScanReqIsFromWebUI = FALSE;
5227                 pAdapter->bConfigChanged = TRUE;
5228
5229         MlmeEnqueue(pAdapter,
5230                     MLME_CNTL_STATE_MACHINE,
5231                     OID_802_11_SSID,
5232                     sizeof(NDIS_802_11_SSID),
5233                     (VOID *)pSsid);
5234
5235         StateMachineTouched = TRUE;
5236         DBGPRINT(RT_DEBUG_TRACE, ("Set_SSID_Proc::(Len=%d,Ssid=%s)\n", Ssid.SsidLength, Ssid.Ssid));
5237     }
5238     else
5239         success = FALSE;
5240
5241     if (StateMachineTouched) // Upper layer sent a MLME-related operations
5242         RT28XX_MLME_HANDLER(pAdapter);
5243
5244     return success;
5245 }
5246
5247 #ifdef WMM_SUPPORT
5248 /*
5249     ==========================================================================
5250     Description:
5251         Set WmmCapable Enable or Disable
5252     Return:
5253         TRUE if all parameters are OK, FALSE otherwise
5254     ==========================================================================
5255 */
5256 INT     Set_WmmCapable_Proc(
5257         IN      PRTMP_ADAPTER   pAd,
5258         IN      PUCHAR                  arg)
5259 {
5260         BOOLEAN bWmmCapable;
5261
5262         bWmmCapable = simple_strtol(arg, 0, 10);
5263
5264         if ((bWmmCapable == 1)
5265                 )
5266                 pAd->CommonCfg.bWmmCapable = TRUE;
5267         else if (bWmmCapable == 0)
5268                 pAd->CommonCfg.bWmmCapable = FALSE;
5269         else
5270                 return FALSE;  //Invalid argument
5271
5272         DBGPRINT(RT_DEBUG_TRACE, ("Set_WmmCapable_Proc::(bWmmCapable=%d)\n",
5273                 pAd->CommonCfg.bWmmCapable));
5274
5275         return TRUE;
5276 }
5277 #endif // WMM_SUPPORT //
5278
5279 /*
5280     ==========================================================================
5281     Description:
5282         Set Network Type(Infrastructure/Adhoc mode)
5283     Return:
5284         TRUE if all parameters are OK, FALSE otherwise
5285     ==========================================================================
5286 */
5287 INT Set_NetworkType_Proc(
5288     IN  PRTMP_ADAPTER   pAdapter,
5289     IN  PUCHAR          arg)
5290 {
5291     UINT32      Value = 0;
5292
5293     if (strcmp(arg, "Adhoc") == 0)
5294         {
5295                 if (pAdapter->StaCfg.BssType != BSS_ADHOC)
5296                 {
5297                         // Config has changed
5298                         pAdapter->bConfigChanged = TRUE;
5299             if (MONITOR_ON(pAdapter))
5300             {
5301                 RTMP_IO_WRITE32(pAdapter, RX_FILTR_CFG, STANORMAL);
5302                 RTMP_IO_READ32(pAdapter, MAC_SYS_CTRL, &Value);
5303                                 Value &= (~0x80);
5304                                 RTMP_IO_WRITE32(pAdapter, MAC_SYS_CTRL, Value);
5305                 OPSTATUS_CLEAR_FLAG(pAdapter, fOP_STATUS_MEDIA_STATE_CONNECTED);
5306                 pAdapter->StaCfg.bAutoReconnect = TRUE;
5307                 LinkDown(pAdapter, FALSE);
5308             }
5309                         if (INFRA_ON(pAdapter))
5310                         {
5311                                 //BOOLEAN Cancelled;
5312                                 // Set the AutoReconnectSsid to prevent it reconnect to old SSID
5313                                 // Since calling this indicate user don't want to connect to that SSID anymore.
5314                                 pAdapter->MlmeAux.AutoReconnectSsidLen= 32;
5315                                 NdisZeroMemory(pAdapter->MlmeAux.AutoReconnectSsid, pAdapter->MlmeAux.AutoReconnectSsidLen);
5316
5317                                 LinkDown(pAdapter, FALSE);
5318
5319                                 DBGPRINT(RT_DEBUG_TRACE, ("NDIS_STATUS_MEDIA_DISCONNECT Event BB!\n"));
5320                         }
5321                 }
5322                 pAdapter->StaCfg.BssType = BSS_ADHOC;
5323         pAdapter->net_dev->type = pAdapter->StaCfg.OriDevType;
5324                 DBGPRINT(RT_DEBUG_TRACE, ("===>Set_NetworkType_Proc::(AD-HOC)\n"));
5325         }
5326     else if (strcmp(arg, "Infra") == 0)
5327         {
5328                 if (pAdapter->StaCfg.BssType != BSS_INFRA)
5329                 {
5330                         // Config has changed
5331                         pAdapter->bConfigChanged = TRUE;
5332             if (MONITOR_ON(pAdapter))
5333             {
5334                 RTMP_IO_WRITE32(pAdapter, RX_FILTR_CFG, STANORMAL);
5335                 RTMP_IO_READ32(pAdapter, MAC_SYS_CTRL, &Value);
5336                                 Value &= (~0x80);
5337                                 RTMP_IO_WRITE32(pAdapter, MAC_SYS_CTRL, Value);
5338                 OPSTATUS_CLEAR_FLAG(pAdapter, fOP_STATUS_MEDIA_STATE_CONNECTED);
5339                 pAdapter->StaCfg.bAutoReconnect = TRUE;
5340                 LinkDown(pAdapter, FALSE);
5341             }
5342                         if (ADHOC_ON(pAdapter))
5343                         {
5344                                 // Set the AutoReconnectSsid to prevent it reconnect to old SSID
5345                                 // Since calling this indicate user don't want to connect to that SSID anymore.
5346                                 pAdapter->MlmeAux.AutoReconnectSsidLen= 32;
5347                                 NdisZeroMemory(pAdapter->MlmeAux.AutoReconnectSsid, pAdapter->MlmeAux.AutoReconnectSsidLen);
5348
5349                                 LinkDown(pAdapter, FALSE);
5350                         }
5351                 }
5352                 pAdapter->StaCfg.BssType = BSS_INFRA;
5353         pAdapter->net_dev->type = pAdapter->StaCfg.OriDevType;
5354                 DBGPRINT(RT_DEBUG_TRACE, ("===>Set_NetworkType_Proc::(INFRA)\n"));
5355
5356         pAdapter->StaCfg.BssType = BSS_INFRA;
5357         }
5358     else if (strcmp(arg, "Monitor") == 0)
5359     {
5360                 UCHAR   bbpValue = 0;
5361                 BCN_TIME_CFG_STRUC csr;
5362                 OPSTATUS_CLEAR_FLAG(pAdapter, fOP_STATUS_INFRA_ON);
5363         OPSTATUS_CLEAR_FLAG(pAdapter, fOP_STATUS_ADHOC_ON);
5364                 OPSTATUS_SET_FLAG(pAdapter, fOP_STATUS_MEDIA_STATE_CONNECTED);
5365                 // disable all periodic state machine
5366                 pAdapter->StaCfg.bAutoReconnect = FALSE;
5367                 // reset all mlme state machine
5368                 RT28XX_MLME_RESET_STATE_MACHINE(pAdapter);
5369                 DBGPRINT(RT_DEBUG_TRACE, ("fOP_STATUS_MEDIA_STATE_CONNECTED \n"));
5370         if (pAdapter->CommonCfg.CentralChannel == 0)
5371         {
5372 #ifdef DOT11_N_SUPPORT
5373             if (pAdapter->CommonCfg.PhyMode == PHY_11AN_MIXED)
5374                 pAdapter->CommonCfg.CentralChannel = 36;
5375             else
5376 #endif // DOT11_N_SUPPORT //
5377                 pAdapter->CommonCfg.CentralChannel = 6;
5378         }
5379 #ifdef DOT11_N_SUPPORT
5380         else
5381             N_ChannelCheck(pAdapter);
5382 #endif // DOT11_N_SUPPORT //
5383
5384 #ifdef DOT11_N_SUPPORT
5385         if (pAdapter->CommonCfg.PhyMode >= PHY_11ABGN_MIXED &&
5386             pAdapter->CommonCfg.RegTransmitSetting.field.BW == BW_40 &&
5387             pAdapter->CommonCfg.RegTransmitSetting.field.EXTCHA == EXTCHA_ABOVE)
5388                 {
5389                         // 40MHz ,control channel at lower
5390                         RTMP_BBP_IO_READ8_BY_REG_ID(pAdapter, BBP_R4, &bbpValue);
5391                         bbpValue &= (~0x18);
5392                         bbpValue |= 0x10;
5393                         RTMP_BBP_IO_WRITE8_BY_REG_ID(pAdapter, BBP_R4, bbpValue);
5394                         pAdapter->CommonCfg.BBPCurrentBW = BW_40;
5395                         //  RX : control channel at lower
5396                         RTMP_BBP_IO_READ8_BY_REG_ID(pAdapter, BBP_R3, &bbpValue);
5397                         bbpValue &= (~0x20);
5398                         RTMP_BBP_IO_WRITE8_BY_REG_ID(pAdapter, BBP_R3, bbpValue);
5399
5400                         RTMP_IO_READ32(pAdapter, TX_BAND_CFG, &Value);
5401                         Value &= 0xfffffffe;
5402                         RTMP_IO_WRITE32(pAdapter, TX_BAND_CFG, Value);
5403                         pAdapter->CommonCfg.CentralChannel = pAdapter->CommonCfg.Channel + 2;
5404             AsicSwitchChannel(pAdapter, pAdapter->CommonCfg.CentralChannel, FALSE);
5405                     AsicLockChannel(pAdapter, pAdapter->CommonCfg.CentralChannel);
5406             DBGPRINT(RT_DEBUG_TRACE, ("BW_40 ,control_channel(%d), CentralChannel(%d) \n",
5407                                        pAdapter->CommonCfg.Channel,
5408                                        pAdapter->CommonCfg.CentralChannel));
5409                 }
5410                 else if (pAdapter->CommonCfg.PhyMode >= PHY_11ABGN_MIXED &&
5411                  pAdapter->CommonCfg.RegTransmitSetting.field.BW == BW_40 &&
5412                  pAdapter->CommonCfg.RegTransmitSetting.field.EXTCHA == EXTCHA_BELOW)
5413                 {
5414                         // 40MHz ,control channel at upper
5415                         RTMP_BBP_IO_READ8_BY_REG_ID(pAdapter, BBP_R4, &bbpValue);
5416                         bbpValue &= (~0x18);
5417                         bbpValue |= 0x10;
5418                         RTMP_BBP_IO_WRITE8_BY_REG_ID(pAdapter, BBP_R4, bbpValue);
5419                         pAdapter->CommonCfg.BBPCurrentBW = BW_40;
5420                         RTMP_IO_READ32(pAdapter, TX_BAND_CFG, &Value);
5421                         Value |= 0x1;
5422                         RTMP_IO_WRITE32(pAdapter, TX_BAND_CFG, Value);
5423
5424                         RTMP_BBP_IO_READ8_BY_REG_ID(pAdapter, BBP_R3, &bbpValue);
5425                         bbpValue |= (0x20);
5426                         RTMP_BBP_IO_WRITE8_BY_REG_ID(pAdapter, BBP_R3, bbpValue);
5427                         pAdapter->CommonCfg.CentralChannel = pAdapter->CommonCfg.Channel - 2;
5428             AsicSwitchChannel(pAdapter, pAdapter->CommonCfg.CentralChannel, FALSE);
5429                     AsicLockChannel(pAdapter, pAdapter->CommonCfg.CentralChannel);
5430             DBGPRINT(RT_DEBUG_TRACE, ("BW_40 ,control_channel(%d), CentralChannel(%d) \n",
5431                                        pAdapter->CommonCfg.Channel,
5432                                        pAdapter->CommonCfg.CentralChannel));
5433                 }
5434                 else
5435 #endif // DOT11_N_SUPPORT //
5436                 {
5437                         // 20MHz
5438                         RTMP_BBP_IO_READ8_BY_REG_ID(pAdapter, BBP_R4, &bbpValue);
5439                         bbpValue &= (~0x18);
5440                         RTMP_BBP_IO_WRITE8_BY_REG_ID(pAdapter, BBP_R4, bbpValue);
5441                         pAdapter->CommonCfg.BBPCurrentBW = BW_20;
5442                         AsicSwitchChannel(pAdapter, pAdapter->CommonCfg.Channel, FALSE);
5443                         AsicLockChannel(pAdapter, pAdapter->CommonCfg.Channel);
5444                         DBGPRINT(RT_DEBUG_TRACE, ("BW_20, Channel(%d)\n", pAdapter->CommonCfg.Channel));
5445                 }
5446                 // Enable Rx with promiscuous reception
5447                 RTMP_IO_WRITE32(pAdapter, RX_FILTR_CFG, 0x3);
5448                 // ASIC supporsts sniffer function with replacing RSSI with timestamp.
5449                 //RTMP_IO_READ32(pAdapter, MAC_SYS_CTRL, &Value);
5450                 //Value |= (0x80);
5451                 //RTMP_IO_WRITE32(pAdapter, MAC_SYS_CTRL, Value);
5452                 // disable sync
5453                 RTMP_IO_READ32(pAdapter, BCN_TIME_CFG, &csr.word);
5454                 csr.field.bBeaconGen = 0;
5455                 csr.field.bTBTTEnable = 0;
5456                 csr.field.TsfSyncMode = 0;
5457                 RTMP_IO_WRITE32(pAdapter, BCN_TIME_CFG, csr.word);
5458
5459                 pAdapter->StaCfg.BssType = BSS_MONITOR;
5460         pAdapter->net_dev->type = ARPHRD_IEEE80211_PRISM; //ARPHRD_IEEE80211; // IEEE80211
5461                 DBGPRINT(RT_DEBUG_TRACE, ("===>Set_NetworkType_Proc::(MONITOR)\n"));
5462     }
5463
5464     // Reset Ralink supplicant to not use, it will be set to start when UI set PMK key
5465     pAdapter->StaCfg.WpaState = SS_NOTUSE;
5466
5467     DBGPRINT(RT_DEBUG_TRACE, ("Set_NetworkType_Proc::(NetworkType=%d)\n", pAdapter->StaCfg.BssType));
5468
5469     return TRUE;
5470 }
5471
5472 /*
5473     ==========================================================================
5474     Description:
5475         Set Authentication mode
5476     Return:
5477         TRUE if all parameters are OK, FALSE otherwise
5478     ==========================================================================
5479 */
5480 INT Set_AuthMode_Proc(
5481     IN  PRTMP_ADAPTER   pAdapter,
5482     IN  PUCHAR          arg)
5483 {
5484     if ((strcmp(arg, "WEPAUTO") == 0) || (strcmp(arg, "wepauto") == 0))
5485         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeAutoSwitch;
5486     else if ((strcmp(arg, "OPEN") == 0) || (strcmp(arg, "open") == 0))
5487         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeOpen;
5488     else if ((strcmp(arg, "SHARED") == 0) || (strcmp(arg, "shared") == 0))
5489         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeShared;
5490     else if ((strcmp(arg, "WPAPSK") == 0) || (strcmp(arg, "wpapsk") == 0))
5491         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeWPAPSK;
5492     else if ((strcmp(arg, "WPANONE") == 0) || (strcmp(arg, "wpanone") == 0))
5493         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeWPANone;
5494     else if ((strcmp(arg, "WPA2PSK") == 0) || (strcmp(arg, "wpa2psk") == 0))
5495         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeWPA2PSK;
5496 #ifdef WPA_SUPPLICANT_SUPPORT
5497     else if ((strcmp(arg, "WPA") == 0) || (strcmp(arg, "wpa") == 0))
5498         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeWPA;
5499     else if ((strcmp(arg, "WPA2") == 0) || (strcmp(arg, "wpa2") == 0))
5500         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeWPA2;
5501 #endif // WPA_SUPPLICANT_SUPPORT //
5502     else
5503         return FALSE;
5504
5505     pAdapter->StaCfg.PortSecured = WPA_802_1X_PORT_NOT_SECURED;
5506
5507     DBGPRINT(RT_DEBUG_TRACE, ("Set_AuthMode_Proc::(AuthMode=%d)\n", pAdapter->StaCfg.AuthMode));
5508
5509     return TRUE;
5510 }
5511
5512 /*
5513     ==========================================================================
5514     Description:
5515         Set Encryption Type
5516     Return:
5517         TRUE if all parameters are OK, FALSE otherwise
5518     ==========================================================================
5519 */
5520 INT Set_EncrypType_Proc(
5521     IN  PRTMP_ADAPTER   pAdapter,
5522     IN  PUCHAR          arg)
5523 {
5524     if ((strcmp(arg, "NONE") == 0) || (strcmp(arg, "none") == 0))
5525     {
5526         if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
5527             return TRUE;    // do nothing
5528
5529         pAdapter->StaCfg.WepStatus     = Ndis802_11WEPDisabled;
5530         pAdapter->StaCfg.PairCipher    = Ndis802_11WEPDisabled;
5531             pAdapter->StaCfg.GroupCipher   = Ndis802_11WEPDisabled;
5532     }
5533     else if ((strcmp(arg, "WEP") == 0) || (strcmp(arg, "wep") == 0))
5534     {
5535         if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
5536             return TRUE;    // do nothing
5537
5538         pAdapter->StaCfg.WepStatus     = Ndis802_11WEPEnabled;
5539         pAdapter->StaCfg.PairCipher    = Ndis802_11WEPEnabled;
5540             pAdapter->StaCfg.GroupCipher   = Ndis802_11WEPEnabled;
5541     }
5542     else if ((strcmp(arg, "TKIP") == 0) || (strcmp(arg, "tkip") == 0))
5543     {
5544         if (pAdapter->StaCfg.AuthMode < Ndis802_11AuthModeWPA)
5545             return TRUE;    // do nothing
5546
5547         pAdapter->StaCfg.WepStatus     = Ndis802_11Encryption2Enabled;
5548         pAdapter->StaCfg.PairCipher    = Ndis802_11Encryption2Enabled;
5549             pAdapter->StaCfg.GroupCipher   = Ndis802_11Encryption2Enabled;
5550     }
5551     else if ((strcmp(arg, "AES") == 0) || (strcmp(arg, "aes") == 0))
5552     {
5553         if (pAdapter->StaCfg.AuthMode < Ndis802_11AuthModeWPA)
5554             return TRUE;    // do nothing
5555
5556         pAdapter->StaCfg.WepStatus     = Ndis802_11Encryption3Enabled;
5557         pAdapter->StaCfg.PairCipher    = Ndis802_11Encryption3Enabled;
5558             pAdapter->StaCfg.GroupCipher   = Ndis802_11Encryption3Enabled;
5559     }
5560     else
5561         return FALSE;
5562
5563     pAdapter->StaCfg.OrigWepStatus = pAdapter->StaCfg.WepStatus;
5564
5565     DBGPRINT(RT_DEBUG_TRACE, ("Set_EncrypType_Proc::(EncrypType=%d)\n", pAdapter->StaCfg.WepStatus));
5566
5567     return TRUE;
5568 }
5569
5570 /*
5571     ==========================================================================
5572     Description:
5573         Set Default Key ID
5574     Return:
5575         TRUE if all parameters are OK, FALSE otherwise
5576     ==========================================================================
5577 */
5578 INT Set_DefaultKeyID_Proc(
5579     IN  PRTMP_ADAPTER   pAdapter,
5580     IN  PUCHAR          arg)
5581 {
5582     ULONG                               KeyIdx;
5583
5584     KeyIdx = simple_strtol(arg, 0, 10);
5585     if((KeyIdx >= 1 ) && (KeyIdx <= 4))
5586         pAdapter->StaCfg.DefaultKeyId = (UCHAR) (KeyIdx - 1 );
5587     else
5588         return FALSE;  //Invalid argument
5589
5590     DBGPRINT(RT_DEBUG_TRACE, ("Set_DefaultKeyID_Proc::(DefaultKeyID=%d)\n", pAdapter->StaCfg.DefaultKeyId));
5591
5592     return TRUE;
5593 }
5594
5595 /*
5596     ==========================================================================
5597     Description:
5598         Set WEP KEY1
5599     Return:
5600         TRUE if all parameters are OK, FALSE otherwise
5601     ==========================================================================
5602 */
5603 INT Set_Key1_Proc(
5604     IN  PRTMP_ADAPTER   pAdapter,
5605     IN  PUCHAR          arg)
5606 {
5607     int                                 KeyLen;
5608     int                                 i;
5609     UCHAR                               CipherAlg=CIPHER_WEP64;
5610
5611     if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
5612         return TRUE;    // do nothing
5613
5614     KeyLen = strlen(arg);
5615
5616     switch (KeyLen)
5617     {
5618         case 5: //wep 40 Ascii type
5619             pAdapter->SharedKey[BSS0][0].KeyLen = KeyLen;
5620             memcpy(pAdapter->SharedKey[BSS0][0].Key, arg, KeyLen);
5621             CipherAlg = CIPHER_WEP64;
5622             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key1_Proc::(Key1=%s and type=%s)\n", arg, "Ascii"));
5623             break;
5624         case 10: //wep 40 Hex type
5625             for(i=0; i < KeyLen; i++)
5626             {
5627                 if( !isxdigit(*(arg+i)) )
5628                     return FALSE;  //Not Hex value;
5629             }
5630             pAdapter->SharedKey[BSS0][0].KeyLen = KeyLen / 2 ;
5631             AtoH(arg, pAdapter->SharedKey[BSS0][0].Key, KeyLen / 2);
5632             CipherAlg = CIPHER_WEP64;
5633             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key1_Proc::(Key1=%s and type=%s)\n", arg, "Hex"));
5634             break;
5635         case 13: //wep 104 Ascii type
5636             pAdapter->SharedKey[BSS0][0].KeyLen = KeyLen;
5637             memcpy(pAdapter->SharedKey[BSS0][0].Key, arg, KeyLen);
5638             CipherAlg = CIPHER_WEP128;
5639             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key1_Proc::(Key1=%s and type=%s)\n", arg, "Ascii"));
5640             break;
5641         case 26: //wep 104 Hex type
5642             for(i=0; i < KeyLen; i++)
5643             {
5644                 if( !isxdigit(*(arg+i)) )
5645                     return FALSE;  //Not Hex value;
5646             }
5647             pAdapter->SharedKey[BSS0][0].KeyLen = KeyLen / 2 ;
5648             AtoH(arg, pAdapter->SharedKey[BSS0][0].Key, KeyLen / 2);
5649             CipherAlg = CIPHER_WEP128;
5650             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key1_Proc::(Key1=%s and type=%s)\n", arg, "Hex"));
5651             break;
5652         default: //Invalid argument
5653             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key1_Proc::Invalid argument (=%s)\n", arg));
5654             return FALSE;
5655     }
5656
5657     pAdapter->SharedKey[BSS0][0].CipherAlg = CipherAlg;
5658
5659     // Set keys (into ASIC)
5660     if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
5661         ;   // not support
5662     else    // Old WEP stuff
5663     {
5664         AsicAddSharedKeyEntry(pAdapter,
5665                               0,
5666                               0,
5667                               pAdapter->SharedKey[BSS0][0].CipherAlg,
5668                               pAdapter->SharedKey[BSS0][0].Key,
5669                               NULL,
5670                               NULL);
5671     }
5672
5673     return TRUE;
5674 }
5675 /*
5676     ==========================================================================
5677
5678     Description:
5679         Set WEP KEY2
5680     Return:
5681         TRUE if all parameters are OK, FALSE otherwise
5682     ==========================================================================
5683 */
5684 INT Set_Key2_Proc(
5685     IN  PRTMP_ADAPTER   pAdapter,
5686     IN  PUCHAR          arg)
5687 {
5688     int                                 KeyLen;
5689     int                                 i;
5690     UCHAR                               CipherAlg=CIPHER_WEP64;
5691
5692     if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
5693         return TRUE;    // do nothing
5694
5695     KeyLen = strlen(arg);
5696
5697     switch (KeyLen)
5698     {
5699         case 5: //wep 40 Ascii type
5700             pAdapter->SharedKey[BSS0][1].KeyLen = KeyLen;
5701             memcpy(pAdapter->SharedKey[BSS0][1].Key, arg, KeyLen);
5702             CipherAlg = CIPHER_WEP64;
5703             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key2_Proc::(Key2=%s and type=%s)\n", arg, "Ascii"));
5704             break;
5705         case 10: //wep 40 Hex type
5706             for(i=0; i < KeyLen; i++)
5707             {
5708                 if( !isxdigit(*(arg+i)) )
5709                     return FALSE;  //Not Hex value;
5710             }
5711             pAdapter->SharedKey[BSS0][1].KeyLen = KeyLen / 2 ;
5712             AtoH(arg, pAdapter->SharedKey[BSS0][1].Key, KeyLen / 2);
5713             CipherAlg = CIPHER_WEP64;
5714             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key2_Proc::(Key2=%s and type=%s)\n", arg, "Hex"));
5715             break;
5716         case 13: //wep 104 Ascii type
5717             pAdapter->SharedKey[BSS0][1].KeyLen = KeyLen;
5718             memcpy(pAdapter->SharedKey[BSS0][1].Key, arg, KeyLen);
5719             CipherAlg = CIPHER_WEP128;
5720             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key2_Proc::(Key2=%s and type=%s)\n", arg, "Ascii"));
5721             break;
5722         case 26: //wep 104 Hex type
5723             for(i=0; i < KeyLen; i++)
5724             {
5725                 if( !isxdigit(*(arg+i)) )
5726                     return FALSE;  //Not Hex value;
5727             }
5728             pAdapter->SharedKey[BSS0][1].KeyLen = KeyLen / 2 ;
5729             AtoH(arg, pAdapter->SharedKey[BSS0][1].Key, KeyLen / 2);
5730             CipherAlg = CIPHER_WEP128;
5731             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key2_Proc::(Key2=%s and type=%s)\n", arg, "Hex"));
5732             break;
5733         default: //Invalid argument
5734             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key2_Proc::Invalid argument (=%s)\n", arg));
5735             return FALSE;
5736     }
5737     pAdapter->SharedKey[BSS0][1].CipherAlg = CipherAlg;
5738
5739     // Set keys (into ASIC)
5740     if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
5741         ;   // not support
5742     else    // Old WEP stuff
5743     {
5744         AsicAddSharedKeyEntry(pAdapter,
5745                               0,
5746                               1,
5747                               pAdapter->SharedKey[BSS0][1].CipherAlg,
5748                               pAdapter->SharedKey[BSS0][1].Key,
5749                               NULL,
5750                               NULL);
5751     }
5752
5753     return TRUE;
5754 }
5755 /*
5756     ==========================================================================
5757     Description:
5758         Set WEP KEY3
5759     Return:
5760         TRUE if all parameters are OK, FALSE otherwise
5761     ==========================================================================
5762 */
5763 INT Set_Key3_Proc(
5764     IN  PRTMP_ADAPTER   pAdapter,
5765     IN  PUCHAR          arg)
5766 {
5767     int                                 KeyLen;
5768     int                                 i;
5769     UCHAR                               CipherAlg=CIPHER_WEP64;
5770
5771     if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
5772         return TRUE;    // do nothing
5773
5774     KeyLen = strlen(arg);
5775
5776     switch (KeyLen)
5777     {
5778         case 5: //wep 40 Ascii type
5779             pAdapter->SharedKey[BSS0][2].KeyLen = KeyLen;
5780             memcpy(pAdapter->SharedKey[BSS0][2].Key, arg, KeyLen);
5781             CipherAlg = CIPHER_WEP64;
5782             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key3_Proc::(Key3=%s and type=Ascii)\n", arg));
5783             break;
5784         case 10: //wep 40 Hex type
5785             for(i=0; i < KeyLen; i++)
5786             {
5787                 if( !isxdigit(*(arg+i)) )
5788                     return FALSE;  //Not Hex value;
5789             }
5790             pAdapter->SharedKey[BSS0][2].KeyLen = KeyLen / 2 ;
5791             AtoH(arg, pAdapter->SharedKey[BSS0][2].Key, KeyLen / 2);
5792             CipherAlg = CIPHER_WEP64;
5793             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key3_Proc::(Key3=%s and type=Hex)\n", arg));
5794             break;
5795         case 13: //wep 104 Ascii type
5796             pAdapter->SharedKey[BSS0][2].KeyLen = KeyLen;
5797             memcpy(pAdapter->SharedKey[BSS0][2].Key, arg, KeyLen);
5798             CipherAlg = CIPHER_WEP128;
5799             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key3_Proc::(Key3=%s and type=Ascii)\n", arg));
5800             break;
5801         case 26: //wep 104 Hex type
5802             for(i=0; i < KeyLen; i++)
5803             {
5804                 if( !isxdigit(*(arg+i)) )
5805                     return FALSE;  //Not Hex value;
5806             }
5807             pAdapter->SharedKey[BSS0][2].KeyLen = KeyLen / 2 ;
5808             AtoH(arg, pAdapter->SharedKey[BSS0][2].Key, KeyLen / 2);
5809             CipherAlg = CIPHER_WEP128;
5810             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key3_Proc::(Key3=%s and type=Hex)\n", arg));
5811             break;
5812         default: //Invalid argument
5813             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key3_Proc::Invalid argument (=%s)\n", arg));
5814             return FALSE;
5815     }
5816     pAdapter->SharedKey[BSS0][2].CipherAlg = CipherAlg;
5817
5818     // Set keys (into ASIC)
5819     if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
5820         ;   // not support
5821     else    // Old WEP stuff
5822     {
5823         AsicAddSharedKeyEntry(pAdapter,
5824                               0,
5825                               2,
5826                               pAdapter->SharedKey[BSS0][2].CipherAlg,
5827                               pAdapter->SharedKey[BSS0][2].Key,
5828                               NULL,
5829                               NULL);
5830     }
5831
5832     return TRUE;
5833 }
5834 /*
5835     ==========================================================================
5836     Description:
5837         Set WEP KEY4
5838     Return:
5839         TRUE if all parameters are OK, FALSE otherwise
5840     ==========================================================================
5841 */
5842 INT Set_Key4_Proc(
5843     IN  PRTMP_ADAPTER   pAdapter,
5844     IN  PUCHAR          arg)
5845 {
5846     int                                 KeyLen;
5847     int                                 i;
5848     UCHAR                               CipherAlg=CIPHER_WEP64;
5849
5850     if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
5851         return TRUE;    // do nothing
5852
5853     KeyLen = strlen(arg);
5854
5855     switch (KeyLen)
5856     {
5857         case 5: //wep 40 Ascii type
5858             pAdapter->SharedKey[BSS0][3].KeyLen = KeyLen;
5859             memcpy(pAdapter->SharedKey[BSS0][3].Key, arg, KeyLen);
5860             CipherAlg = CIPHER_WEP64;
5861             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key4_Proc::(Key4=%s and type=%s)\n", arg, "Ascii"));
5862             break;
5863         case 10: //wep 40 Hex type
5864             for(i=0; i < KeyLen; i++)
5865             {
5866                 if( !isxdigit(*(arg+i)) )
5867                     return FALSE;  //Not Hex value;
5868             }
5869             pAdapter->SharedKey[BSS0][3].KeyLen = KeyLen / 2 ;
5870             AtoH(arg, pAdapter->SharedKey[BSS0][3].Key, KeyLen / 2);
5871             CipherAlg = CIPHER_WEP64;
5872             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key4_Proc::(Key4=%s and type=%s)\n", arg, "Hex"));
5873             break;
5874         case 13: //wep 104 Ascii type
5875             pAdapter->SharedKey[BSS0][3].KeyLen = KeyLen;
5876             memcpy(pAdapter->SharedKey[BSS0][3].Key, arg, KeyLen);
5877             CipherAlg = CIPHER_WEP128;
5878             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key4_Proc::(Key4=%s and type=%s)\n", arg, "Ascii"));
5879             break;
5880         case 26: //wep 104 Hex type
5881             for(i=0; i < KeyLen; i++)
5882             {
5883                 if( !isxdigit(*(arg+i)) )
5884                     return FALSE;  //Not Hex value;
5885             }
5886             pAdapter->SharedKey[BSS0][3].KeyLen = KeyLen / 2 ;
5887             AtoH(arg, pAdapter->SharedKey[BSS0][3].Key, KeyLen / 2);
5888             CipherAlg = CIPHER_WEP128;
5889             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key4_Proc::(Key4=%s and type=%s)\n", arg, "Hex"));
5890             break;
5891         default: //Invalid argument
5892             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key4_Proc::Invalid argument (=%s)\n", arg));
5893             return FALSE;
5894     }
5895     pAdapter->SharedKey[BSS0][3].CipherAlg = CipherAlg;
5896
5897     // Set keys (into ASIC)
5898     if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
5899         ;   // not support
5900     else    // Old WEP stuff
5901     {
5902         AsicAddSharedKeyEntry(pAdapter,
5903                               0,
5904                               3,
5905                               pAdapter->SharedKey[BSS0][3].CipherAlg,
5906                               pAdapter->SharedKey[BSS0][3].Key,
5907                               NULL,
5908                               NULL);
5909     }
5910
5911     return TRUE;
5912 }
5913
5914 /*
5915     ==========================================================================
5916     Description:
5917         Set WPA PSK key
5918     Return:
5919         TRUE if all parameters are OK, FALSE otherwise
5920     ==========================================================================
5921 */
5922 INT Set_WPAPSK_Proc(
5923     IN  PRTMP_ADAPTER   pAdapter,
5924     IN  PUCHAR          arg)
5925 {
5926     UCHAR                   keyMaterial[40];
5927
5928     if ((pAdapter->StaCfg.AuthMode != Ndis802_11AuthModeWPAPSK) &&
5929         (pAdapter->StaCfg.AuthMode != Ndis802_11AuthModeWPA2PSK) &&
5930             (pAdapter->StaCfg.AuthMode != Ndis802_11AuthModeWPANone)
5931                 )
5932         return TRUE;    // do nothing
5933
5934     DBGPRINT(RT_DEBUG_TRACE, ("Set_WPAPSK_Proc::(WPAPSK=%s)\n", arg));
5935
5936     NdisZeroMemory(keyMaterial, 40);
5937
5938     if ((strlen(arg) < 8) || (strlen(arg) > 64))
5939     {
5940         DBGPRINT(RT_DEBUG_TRACE, ("Set failed!!(WPAPSK=%s), WPAPSK key-string required 8 ~ 64 characters \n", arg));
5941         return FALSE;
5942     }
5943
5944     if (strlen(arg) == 64)
5945     {
5946         AtoH(arg, keyMaterial, 32);
5947         NdisMoveMemory(pAdapter->StaCfg.PMK, keyMaterial, 32);
5948
5949     }
5950     else
5951     {
5952         PasswordHash((char *)arg, pAdapter->MlmeAux.Ssid, pAdapter->MlmeAux.SsidLen, keyMaterial);
5953         NdisMoveMemory(pAdapter->StaCfg.PMK, keyMaterial, 32);
5954     }
5955
5956
5957
5958     if(pAdapter->StaCfg.BssType == BSS_ADHOC &&
5959        pAdapter->StaCfg.AuthMode == Ndis802_11AuthModeWPANone)
5960     {
5961          pAdapter->StaCfg.WpaState = SS_NOTUSE;
5962     }
5963     else
5964     {
5965         // Start STA supplicant state machine
5966         pAdapter->StaCfg.WpaState = SS_START;
5967     }
5968
5969     return TRUE;
5970 }
5971
5972 /*
5973     ==========================================================================
5974     Description:
5975         Set Power Saving mode
5976     Return:
5977         TRUE if all parameters are OK, FALSE otherwise
5978     ==========================================================================
5979 */
5980 INT Set_PSMode_Proc(
5981     IN  PRTMP_ADAPTER   pAdapter,
5982     IN  PUCHAR          arg)
5983 {
5984     if (pAdapter->StaCfg.BssType == BSS_INFRA)
5985     {
5986         if ((strcmp(arg, "Max_PSP") == 0) ||
5987                         (strcmp(arg, "max_psp") == 0) ||
5988                         (strcmp(arg, "MAX_PSP") == 0))
5989         {
5990             // do NOT turn on PSM bit here, wait until MlmeCheckForPsmChange()
5991             // to exclude certain situations.
5992             if (pAdapter->StaCfg.bWindowsACCAMEnable == FALSE)
5993                 pAdapter->StaCfg.WindowsPowerMode = Ndis802_11PowerModeMAX_PSP;
5994             pAdapter->StaCfg.WindowsBatteryPowerMode = Ndis802_11PowerModeMAX_PSP;
5995             OPSTATUS_SET_FLAG(pAdapter, fOP_STATUS_RECEIVE_DTIM);
5996             pAdapter->StaCfg.DefaultListenCount = 5;
5997
5998         }
5999         else if ((strcmp(arg, "Fast_PSP") == 0) ||
6000                                  (strcmp(arg, "fast_psp") == 0) ||
6001                  (strcmp(arg, "FAST_PSP") == 0))
6002         {
6003             // do NOT turn on PSM bit here, wait until MlmeCheckForPsmChange()
6004             // to exclude certain situations.
6005             OPSTATUS_SET_FLAG(pAdapter, fOP_STATUS_RECEIVE_DTIM);
6006             if (pAdapter->StaCfg.bWindowsACCAMEnable == FALSE)
6007                 pAdapter->StaCfg.WindowsPowerMode = Ndis802_11PowerModeFast_PSP;
6008             pAdapter->StaCfg.WindowsBatteryPowerMode = Ndis802_11PowerModeFast_PSP;
6009             pAdapter->StaCfg.DefaultListenCount = 3;
6010         }
6011         else if ((strcmp(arg, "Legacy_PSP") == 0) ||
6012                  (strcmp(arg, "legacy_psp") == 0) ||
6013                  (strcmp(arg, "LEGACY_PSP") == 0))
6014         {
6015             // do NOT turn on PSM bit here, wait until MlmeCheckForPsmChange()
6016             // to exclude certain situations.
6017             OPSTATUS_SET_FLAG(pAdapter, fOP_STATUS_RECEIVE_DTIM);
6018             if (pAdapter->StaCfg.bWindowsACCAMEnable == FALSE)
6019                 pAdapter->StaCfg.WindowsPowerMode = Ndis802_11PowerModeLegacy_PSP;
6020             pAdapter->StaCfg.WindowsBatteryPowerMode = Ndis802_11PowerModeLegacy_PSP;
6021             pAdapter->StaCfg.DefaultListenCount = 3;
6022         }
6023         else
6024         {
6025             //Default Ndis802_11PowerModeCAM
6026             // clear PSM bit immediately
6027             MlmeSetPsmBit(pAdapter, PWR_ACTIVE);
6028             OPSTATUS_SET_FLAG(pAdapter, fOP_STATUS_RECEIVE_DTIM);
6029             if (pAdapter->StaCfg.bWindowsACCAMEnable == FALSE)
6030                 pAdapter->StaCfg.WindowsPowerMode = Ndis802_11PowerModeCAM;
6031             pAdapter->StaCfg.WindowsBatteryPowerMode = Ndis802_11PowerModeCAM;
6032         }
6033
6034         DBGPRINT(RT_DEBUG_TRACE, ("Set_PSMode_Proc::(PSMode=%ld)\n", pAdapter->StaCfg.WindowsPowerMode));
6035     }
6036     else
6037         return FALSE;
6038
6039
6040     return TRUE;
6041 }
6042
6043 #ifdef WPA_SUPPLICANT_SUPPORT
6044 /*
6045     ==========================================================================
6046     Description:
6047         Set WpaSupport flag.
6048     Value:
6049         0: Driver ignore wpa_supplicant.
6050         1: wpa_supplicant initiates scanning and AP selection.
6051         2: driver takes care of scanning, AP selection, and IEEE 802.11 association parameters.
6052     Return:
6053         TRUE if all parameters are OK, FALSE otherwise
6054     ==========================================================================
6055 */
6056 INT Set_Wpa_Support(
6057     IN  PRTMP_ADAPTER   pAd,
6058         IN      PUCHAR                  arg)
6059 {
6060
6061     if ( simple_strtol(arg, 0, 10) == 0)
6062         pAd->StaCfg.WpaSupplicantUP = WPA_SUPPLICANT_DISABLE;
6063     else if ( simple_strtol(arg, 0, 10) == 1)
6064         pAd->StaCfg.WpaSupplicantUP = WPA_SUPPLICANT_ENABLE;
6065     else if ( simple_strtol(arg, 0, 10) == 2)
6066         pAd->StaCfg.WpaSupplicantUP = WPA_SUPPLICANT_ENABLE_WITH_WEB_UI;
6067     else
6068         pAd->StaCfg.WpaSupplicantUP = WPA_SUPPLICANT_DISABLE;
6069
6070     DBGPRINT(RT_DEBUG_TRACE, ("Set_Wpa_Support::(WpaSupplicantUP=%d)\n", pAd->StaCfg.WpaSupplicantUP));
6071
6072     return TRUE;
6073 }
6074 #endif // WPA_SUPPLICANT_SUPPORT //
6075
6076 #ifdef DBG
6077 /*
6078     ==========================================================================
6079     Description:
6080         Read / Write MAC
6081     Arguments:
6082         pAdapter                    Pointer to our adapter
6083         wrq                         Pointer to the ioctl argument
6084
6085     Return Value:
6086         None
6087
6088     Note:
6089         Usage:
6090                1.) iwpriv ra0 mac 0        ==> read MAC where Addr=0x0
6091                2.) iwpriv ra0 mac 0=12     ==> write MAC where Addr=0x0, value=12
6092     ==========================================================================
6093 */
6094 VOID RTMPIoctlMAC(
6095         IN      PRTMP_ADAPTER   pAdapter,
6096         IN      struct iwreq    *wrq)
6097 {
6098         CHAR                            *this_char;
6099         CHAR                            *value;
6100         INT                                     j = 0, k = 0;
6101         CHAR                            msg[1024];
6102         CHAR                            arg[255];
6103         ULONG                           macAddr = 0;
6104         UCHAR                           temp[16], temp2[16];
6105         UINT32                          macValue = 0;
6106         INT                                     Status;
6107
6108
6109         memset(msg, 0x00, 1024);
6110         if (wrq->u.data.length > 1) //No parameters.
6111         {
6112             Status = copy_from_user(arg, wrq->u.data.pointer, (wrq->u.data.length > 255) ? 255 : wrq->u.data.length);
6113                 sprintf(msg, "\n");
6114
6115                 //Parsing Read or Write
6116             this_char = arg;
6117                 if (!*this_char)
6118                         goto next;
6119
6120                 if ((value = rtstrchr(this_char, '=')) != NULL)
6121                         *value++ = 0;
6122
6123                 if (!value || !*value)
6124                 { //Read
6125                         // Sanity check
6126                         if(strlen(this_char) > 4)
6127                                 goto next;
6128
6129                         j = strlen(this_char);
6130                         while(j-- > 0)
6131                         {
6132                                 if(this_char[j] > 'f' || this_char[j] < '0')
6133                                         return;
6134                         }
6135
6136                         // Mac Addr
6137                         k = j = strlen(this_char);
6138                         while(j-- > 0)
6139                         {
6140                                 this_char[4-k+j] = this_char[j];
6141                         }
6142
6143                         while(k < 4)
6144                                 this_char[3-k++]='0';
6145                         this_char[4]='\0';
6146
6147                         if(strlen(this_char) == 4)
6148                         {
6149                                 AtoH(this_char, temp, 2);
6150                                 macAddr = *temp*256 + temp[1];
6151                                 if (macAddr < 0xFFFF)
6152                                 {
6153                                         RTMP_IO_READ32(pAdapter, macAddr, &macValue);
6154                                         DBGPRINT(RT_DEBUG_TRACE, ("MacAddr=%lx, MacValue=%x\n", macAddr, macValue));
6155                                         sprintf(msg+strlen(msg), "[0x%08lX]:%08X  ", macAddr , macValue);
6156                                 }
6157                                 else
6158                                 {//Invalid parametes, so default printk all bbp
6159                                         goto next;
6160                                 }
6161                         }
6162                 }
6163                 else
6164                 { //Write
6165                         memcpy(&temp2, value, strlen(value));
6166                         temp2[strlen(value)] = '\0';
6167
6168                         // Sanity check
6169                         if((strlen(this_char) > 4) || strlen(temp2) > 8)
6170                                 goto next;
6171
6172                         j = strlen(this_char);
6173                         while(j-- > 0)
6174                         {
6175                                 if(this_char[j] > 'f' || this_char[j] < '0')
6176                                         return;
6177                         }
6178
6179                         j = strlen(temp2);
6180                         while(j-- > 0)
6181                         {
6182                                 if(temp2[j] > 'f' || temp2[j] < '0')
6183                                         return;
6184                         }
6185
6186                         //MAC Addr
6187                         k = j = strlen(this_char);
6188                         while(j-- > 0)
6189                         {
6190                                 this_char[4-k+j] = this_char[j];
6191                         }
6192
6193                         while(k < 4)
6194                                 this_char[3-k++]='0';
6195                         this_char[4]='\0';
6196
6197                         //MAC value
6198                         k = j = strlen(temp2);
6199                         while(j-- > 0)
6200                         {
6201                                 temp2[8-k+j] = temp2[j];
6202                         }
6203
6204                         while(k < 8)
6205                                 temp2[7-k++]='0';
6206                         temp2[8]='\0';
6207
6208                         {
6209                                 AtoH(this_char, temp, 2);
6210                                 macAddr = *temp*256 + temp[1];
6211
6212                                 AtoH(temp2, temp, 4);
6213                                 macValue = *temp*256*256*256 + temp[1]*256*256 + temp[2]*256 + temp[3];
6214
6215                                 // debug mode
6216                                 if (macAddr == (HW_DEBUG_SETTING_BASE + 4))
6217                                 {
6218                                         // 0x2bf4: byte0 non-zero: enable R17 tuning, 0: disable R17 tuning
6219                     if (macValue & 0x000000ff)
6220                     {
6221                         pAdapter->BbpTuning.bEnable = TRUE;
6222                         DBGPRINT(RT_DEBUG_TRACE,("turn on R17 tuning\n"));
6223                     }
6224                     else
6225                     {
6226                         UCHAR R66;
6227                         pAdapter->BbpTuning.bEnable = FALSE;
6228                         R66 = 0x26 + GET_LNA_GAIN(pAdapter);
6229                                                 RTMP_BBP_IO_WRITE8_BY_REG_ID(pAdapter, BBP_R66, (0x26 + GET_LNA_GAIN(pAdapter)));
6230                         DBGPRINT(RT_DEBUG_TRACE,("turn off R17 tuning, restore to 0x%02x\n", R66));
6231                     }
6232                                         return;
6233                                 }
6234
6235                                 DBGPRINT(RT_DEBUG_TRACE, ("MacAddr=%02lx, MacValue=0x%x\n", macAddr, macValue));
6236
6237                                 RTMP_IO_WRITE32(pAdapter, macAddr, macValue);
6238                                 sprintf(msg+strlen(msg), "[0x%08lX]:%08X  ", macAddr, macValue);
6239                         }
6240                 }
6241         }
6242 next:
6243         if(strlen(msg) == 1)
6244                 sprintf(msg+strlen(msg), "===>Error command format!");
6245
6246         // Copy the information into the user buffer
6247         wrq->u.data.length = strlen(msg);
6248         Status = copy_to_user(wrq->u.data.pointer, msg, wrq->u.data.length);
6249
6250         DBGPRINT(RT_DEBUG_TRACE, ("<==RTMPIoctlMAC\n\n"));
6251 }
6252
6253 /*
6254     ==========================================================================
6255     Description:
6256         Read / Write E2PROM
6257     Arguments:
6258         pAdapter                    Pointer to our adapter
6259         wrq                         Pointer to the ioctl argument
6260
6261     Return Value:
6262         None
6263
6264     Note:
6265         Usage:
6266                1.) iwpriv ra0 e2p 0             ==> read E2PROM where Addr=0x0
6267                2.) iwpriv ra0 e2p 0=1234    ==> write E2PROM where Addr=0x0, value=1234
6268     ==========================================================================
6269 */
6270 VOID RTMPIoctlE2PROM(
6271         IN      PRTMP_ADAPTER   pAdapter,
6272         IN      struct iwreq    *wrq)
6273 {
6274         CHAR                            *this_char;
6275         CHAR                            *value;
6276         INT                                     j = 0, k = 0;
6277         CHAR                            msg[1024];
6278         CHAR                            arg[255];
6279         USHORT                          eepAddr = 0;
6280         UCHAR                           temp[16], temp2[16];
6281         USHORT                          eepValue;
6282         int                                     Status;
6283
6284
6285         memset(msg, 0x00, 1024);
6286         if (wrq->u.data.length > 1) //No parameters.
6287         {
6288             Status = copy_from_user(arg, wrq->u.data.pointer, (wrq->u.data.length > 255) ? 255 : wrq->u.data.length);
6289                 sprintf(msg, "\n");
6290
6291             //Parsing Read or Write
6292                 this_char = arg;
6293
6294
6295                 if (!*this_char)
6296                         goto next;
6297
6298                 if ((value = rtstrchr(this_char, '=')) != NULL)
6299                         *value++ = 0;
6300
6301                 if (!value || !*value)
6302                 { //Read
6303
6304                         // Sanity check
6305                         if(strlen(this_char) > 4)
6306                                 goto next;
6307
6308                         j = strlen(this_char);
6309                         while(j-- > 0)
6310                         {
6311                                 if(this_char[j] > 'f' || this_char[j] < '0')
6312                                         return;
6313                         }
6314
6315                         // E2PROM addr
6316                         k = j = strlen(this_char);
6317                         while(j-- > 0)
6318                         {
6319                                 this_char[4-k+j] = this_char[j];
6320                         }
6321
6322                         while(k < 4)
6323                                 this_char[3-k++]='0';
6324                         this_char[4]='\0';
6325
6326                         if(strlen(this_char) == 4)
6327                         {
6328                                 AtoH(this_char, temp, 2);
6329                                 eepAddr = *temp*256 + temp[1];
6330                                 if (eepAddr < 0xFFFF)
6331                                 {
6332                                         RT28xx_EEPROM_READ16(pAdapter, eepAddr, eepValue);
6333                                         sprintf(msg+strlen(msg), "[0x%04X]:0x%04X  ", eepAddr , eepValue);
6334                                 }
6335                                 else
6336                                 {//Invalid parametes, so default printk all bbp
6337                                         goto next;
6338                                 }
6339                         }
6340                 }
6341                 else
6342                 { //Write
6343                         memcpy(&temp2, value, strlen(value));
6344                         temp2[strlen(value)] = '\0';
6345
6346                         // Sanity check
6347                         if((strlen(this_char) > 4) || strlen(temp2) > 8)
6348                                 goto next;
6349
6350                         j = strlen(this_char);
6351                         while(j-- > 0)
6352                         {
6353                                 if(this_char[j] > 'f' || this_char[j] < '0')
6354                                         return;
6355                         }
6356                         j = strlen(temp2);
6357                         while(j-- > 0)
6358                         {
6359                                 if(temp2[j] > 'f' || temp2[j] < '0')
6360                                         return;
6361                         }
6362
6363                         //MAC Addr
6364                         k = j = strlen(this_char);
6365                         while(j-- > 0)
6366                         {
6367                                 this_char[4-k+j] = this_char[j];
6368                         }
6369
6370                         while(k < 4)
6371                                 this_char[3-k++]='0';
6372                         this_char[4]='\0';
6373
6374                         //MAC value
6375                         k = j = strlen(temp2);
6376                         while(j-- > 0)
6377                         {
6378                                 temp2[4-k+j] = temp2[j];
6379                         }
6380
6381                         while(k < 4)
6382                                 temp2[3-k++]='0';
6383                         temp2[4]='\0';
6384
6385                         AtoH(this_char, temp, 2);
6386                         eepAddr = *temp*256 + temp[1];
6387
6388                         AtoH(temp2, temp, 2);
6389                         eepValue = *temp*256 + temp[1];
6390
6391                         RT28xx_EEPROM_WRITE16(pAdapter, eepAddr, eepValue);
6392                         sprintf(msg+strlen(msg), "[0x%02X]:%02X  ", eepAddr, eepValue);
6393                 }
6394         }
6395 next:
6396         if(strlen(msg) == 1)
6397                 sprintf(msg+strlen(msg), "===>Error command format!");
6398
6399
6400         // Copy the information into the user buffer
6401         wrq->u.data.length = strlen(msg);
6402         Status = copy_to_user(wrq->u.data.pointer, msg, wrq->u.data.length);
6403
6404         DBGPRINT(RT_DEBUG_TRACE, ("<==RTMPIoctlE2PROM\n"));
6405 }
6406 #endif // DBG //
6407
6408
6409
6410
6411 INT Set_TGnWifiTest_Proc(
6412     IN  PRTMP_ADAPTER   pAd,
6413     IN  PUCHAR          arg)
6414 {
6415     if (simple_strtol(arg, 0, 10) == 0)
6416         pAd->StaCfg.bTGnWifiTest = FALSE;
6417     else
6418         pAd->StaCfg.bTGnWifiTest = TRUE;
6419
6420     DBGPRINT(RT_DEBUG_TRACE, ("IF Set_TGnWifiTest_Proc::(bTGnWifiTest=%d)\n", pAd->StaCfg.bTGnWifiTest));
6421         return TRUE;
6422 }
6423
6424 INT Set_LongRetryLimit_Proc(
6425         IN      PRTMP_ADAPTER   pAdapter,
6426         IN      PUCHAR                  arg)
6427 {
6428         TX_RTY_CFG_STRUC        tx_rty_cfg;
6429         UCHAR                           LongRetryLimit = (UCHAR)simple_strtol(arg, 0, 10);
6430
6431         RTMP_IO_READ32(pAdapter, TX_RTY_CFG, &tx_rty_cfg.word);
6432         tx_rty_cfg.field.LongRtyLimit = LongRetryLimit;
6433         RTMP_IO_WRITE32(pAdapter, TX_RTY_CFG, tx_rty_cfg.word);
6434         DBGPRINT(RT_DEBUG_TRACE, ("IF Set_LongRetryLimit_Proc::(tx_rty_cfg=0x%x)\n", tx_rty_cfg.word));
6435         return TRUE;
6436 }
6437
6438 INT Set_ShortRetryLimit_Proc(
6439         IN      PRTMP_ADAPTER   pAdapter,
6440         IN      PUCHAR                  arg)
6441 {
6442         TX_RTY_CFG_STRUC        tx_rty_cfg;
6443         UCHAR                           ShortRetryLimit = (UCHAR)simple_strtol(arg, 0, 10);
6444
6445         RTMP_IO_READ32(pAdapter, TX_RTY_CFG, &tx_rty_cfg.word);
6446         tx_rty_cfg.field.ShortRtyLimit = ShortRetryLimit;
6447         RTMP_IO_WRITE32(pAdapter, TX_RTY_CFG, tx_rty_cfg.word);
6448         DBGPRINT(RT_DEBUG_TRACE, ("IF Set_ShortRetryLimit_Proc::(tx_rty_cfg=0x%x)\n", tx_rty_cfg.word));
6449         return TRUE;
6450 }