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