1556bf32e504120e08f3310a62fa7bb13e12ddd1
[safe/jmp/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 { RTPRIV_IOCTL_STATISTICS,
93   0, IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_MASK,
94   "stat"},
95 { RTPRIV_IOCTL_GSITESURVEY,
96   0, IW_PRIV_TYPE_CHAR | 1024,
97   "get_site_survey"},
98 };
99
100 INT Set_SSID_Proc(
101     IN  PRTMP_ADAPTER   pAdapter,
102     IN  PUCHAR          arg);
103
104 #ifdef WMM_SUPPORT
105 INT     Set_WmmCapable_Proc(
106         IN      PRTMP_ADAPTER   pAd,
107         IN      PUCHAR                  arg);
108 #endif
109
110 INT Set_NetworkType_Proc(
111     IN  PRTMP_ADAPTER   pAdapter,
112     IN  PUCHAR          arg);
113
114 INT Set_AuthMode_Proc(
115     IN  PRTMP_ADAPTER   pAdapter,
116     IN  PUCHAR          arg);
117
118 INT Set_EncrypType_Proc(
119     IN  PRTMP_ADAPTER   pAdapter,
120     IN  PUCHAR          arg);
121
122 INT Set_DefaultKeyID_Proc(
123     IN  PRTMP_ADAPTER   pAdapter,
124     IN  PUCHAR          arg);
125
126 INT Set_Key1_Proc(
127     IN  PRTMP_ADAPTER   pAdapter,
128     IN  PUCHAR          arg);
129
130 INT Set_Key2_Proc(
131     IN  PRTMP_ADAPTER   pAdapter,
132     IN  PUCHAR          arg);
133
134 INT Set_Key3_Proc(
135     IN  PRTMP_ADAPTER   pAdapter,
136     IN  PUCHAR          arg);
137
138 INT Set_Key4_Proc(
139     IN  PRTMP_ADAPTER   pAdapter,
140     IN  PUCHAR          arg);
141
142 INT Set_WPAPSK_Proc(
143     IN  PRTMP_ADAPTER   pAdapter,
144     IN  PUCHAR          arg);
145
146
147 INT Set_PSMode_Proc(
148     IN  PRTMP_ADAPTER   pAdapter,
149     IN  PUCHAR          arg);
150
151 INT Set_Wpa_Support(
152     IN  PRTMP_ADAPTER   pAd,
153         IN      PUCHAR                  arg);
154
155 NDIS_STATUS RTMPWPANoneAddKeyProc(
156     IN  PRTMP_ADAPTER   pAd,
157     IN  PVOID                   pBuf);
158
159 INT Set_FragTest_Proc(
160     IN  PRTMP_ADAPTER   pAdapter,
161     IN  PUCHAR          arg);
162
163 INT Set_TGnWifiTest_Proc(
164     IN  PRTMP_ADAPTER   pAd,
165     IN  PUCHAR          arg);
166
167 INT Set_LongRetryLimit_Proc(
168         IN      PRTMP_ADAPTER   pAdapter,
169         IN      PUCHAR                  arg);
170
171 INT Set_ShortRetryLimit_Proc(
172         IN      PRTMP_ADAPTER   pAdapter,
173         IN      PUCHAR                  arg);
174
175 static struct {
176         CHAR *name;
177         INT (*set_proc)(PRTMP_ADAPTER pAdapter, PUCHAR arg);
178 } *PRTMP_PRIVATE_SET_PROC, RTMP_PRIVATE_SUPPORT_PROC[] = {
179         {"DriverVersion",                               Set_DriverVersion_Proc},
180         {"CountryRegion",                               Set_CountryRegion_Proc},
181         {"CountryRegionABand",                  Set_CountryRegionABand_Proc},
182         {"SSID",                                                Set_SSID_Proc},
183         {"WirelessMode",                                Set_WirelessMode_Proc},
184         {"TxBurst",                                     Set_TxBurst_Proc},
185         {"TxPreamble",                          Set_TxPreamble_Proc},
186         {"TxPower",                                     Set_TxPower_Proc},
187         {"Channel",                                     Set_Channel_Proc},
188         {"BGProtection",                                Set_BGProtection_Proc},
189         {"RTSThreshold",                                Set_RTSThreshold_Proc},
190         {"FragThreshold",                               Set_FragThreshold_Proc},
191         {"HtBw",                                Set_HtBw_Proc},
192         {"HtMcs",                               Set_HtMcs_Proc},
193         {"HtGi",                                Set_HtGi_Proc},
194         {"HtOpMode",                        Set_HtOpMode_Proc},
195         {"HtExtcha",                        Set_HtExtcha_Proc},
196         {"HtMpduDensity",                       Set_HtMpduDensity_Proc},
197         {"HtBaWinSize",                         Set_HtBaWinSize_Proc},
198         {"HtRdg",                                       Set_HtRdg_Proc},
199         {"HtAmsdu",                                     Set_HtAmsdu_Proc},
200         {"HtAutoBa",                            Set_HtAutoBa_Proc},
201         {"HtBaDecline",                                 Set_BADecline_Proc},
202         {"HtProtect",                           Set_HtProtect_Proc},
203         {"HtMimoPs",                            Set_HtMimoPs_Proc},
204 #ifdef AGGREGATION_SUPPORT
205         {"PktAggregate",                                Set_PktAggregate_Proc},
206 #endif
207
208 #ifdef WMM_SUPPORT
209         {"WmmCapable",                                  Set_WmmCapable_Proc},
210 #endif
211         {"IEEE80211H",                                  Set_IEEE80211H_Proc},
212     {"NetworkType",                 Set_NetworkType_Proc},
213         {"AuthMode",                                    Set_AuthMode_Proc},
214         {"EncrypType",                                  Set_EncrypType_Proc},
215         {"DefaultKeyID",                                Set_DefaultKeyID_Proc},
216         {"Key1",                                                Set_Key1_Proc},
217         {"Key2",                                                Set_Key2_Proc},
218         {"Key3",                                                Set_Key3_Proc},
219         {"Key4",                                                Set_Key4_Proc},
220         {"WPAPSK",                                              Set_WPAPSK_Proc},
221         {"ResetCounter",                                Set_ResetStatCounter_Proc},
222         {"PSMode",                      Set_PSMode_Proc},
223 #ifdef DBG
224         {"Debug",                                               Set_Debug_Proc},
225 #endif
226     {"WpaSupport",                  Set_Wpa_Support},
227         {"FixedTxMode",                 Set_FixedTxMode_Proc},
228     {"TGnWifiTest",                 Set_TGnWifiTest_Proc},
229     {"ForceGF",                                 Set_ForceGF_Proc},
230         {"LongRetry",                           Set_LongRetryLimit_Proc},
231         {"ShortRetry",                          Set_ShortRetryLimit_Proc},
232 //2008/09/11:KH add to support efuse<--
233 #ifdef RT30xx
234         {"efuseFreeNumber",                             set_eFuseGetFreeBlockCount_Proc},
235         {"efuseDump",                                   set_eFusedump_Proc},
236         {"efuseLoadFromBin",                            set_eFuseLoadFromBin_Proc},
237 #endif // RT30xx //
238 //2008/09/11:KH add to support efuse-->
239         {NULL,}
240 };
241
242
243 VOID RTMPAddKey(
244         IN      PRTMP_ADAPTER       pAd,
245         IN      PNDIS_802_11_KEY    pKey)
246 {
247         ULONG                           KeyIdx;
248         MAC_TABLE_ENTRY         *pEntry;
249
250     DBGPRINT(RT_DEBUG_TRACE, ("RTMPAddKey ------>\n"));
251
252 #ifdef RT2860
253         RTMP_CLEAR_PSFLAG(pAd, fRTMP_PS_CAN_GO_SLEEP);
254         if (RTMP_TEST_PSFLAG(pAd, fRTMP_PS_SET_PCI_CLK_OFF_COMMAND))
255         {
256                 if (pAd->StaCfg.bRadio == FALSE)
257                 {
258                         RTMP_SET_PSFLAG(pAd, fRTMP_PS_CAN_GO_SLEEP);
259                         return;
260                 }
261                 DBGPRINT(RT_DEBUG_TRACE,("RTMPWPAAddKeyProc1==>\n"));
262                 RTMPPCIeLinkCtrlValueRestore(pAd, RESTORE_HALT);
263                 RTMPusecDelay(6000);
264                 pAd->bPCIclkOff = FALSE;
265         }
266 #endif
267
268         if (pAd->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
269         {
270                 if (pKey->KeyIndex & 0x80000000)
271                 {
272                     if (pAd->StaCfg.AuthMode == Ndis802_11AuthModeWPANone)
273             {
274                 NdisZeroMemory(pAd->StaCfg.PMK, 32);
275                 NdisMoveMemory(pAd->StaCfg.PMK, pKey->KeyMaterial, pKey->KeyLength);
276                 goto end;
277             }
278                     // Update PTK
279                     NdisZeroMemory(&pAd->SharedKey[BSS0][0], sizeof(CIPHER_KEY));
280             pAd->SharedKey[BSS0][0].KeyLen = LEN_TKIP_EK;
281             NdisMoveMemory(pAd->SharedKey[BSS0][0].Key, pKey->KeyMaterial, LEN_TKIP_EK);
282
283             if (pAd->StaCfg.PairCipher == Ndis802_11Encryption2Enabled)
284             {
285                 NdisMoveMemory(pAd->SharedKey[BSS0][0].RxMic, pKey->KeyMaterial + LEN_TKIP_EK, LEN_TKIP_TXMICK);
286                 NdisMoveMemory(pAd->SharedKey[BSS0][0].TxMic, pKey->KeyMaterial + LEN_TKIP_EK + LEN_TKIP_TXMICK, LEN_TKIP_RXMICK);
287             }
288             else
289             {
290                 NdisMoveMemory(pAd->SharedKey[BSS0][0].TxMic, pKey->KeyMaterial + LEN_TKIP_EK, LEN_TKIP_TXMICK);
291                 NdisMoveMemory(pAd->SharedKey[BSS0][0].RxMic, pKey->KeyMaterial + LEN_TKIP_EK + LEN_TKIP_TXMICK, LEN_TKIP_RXMICK);
292             }
293
294             // Decide its ChiperAlg
295                 if (pAd->StaCfg.PairCipher == Ndis802_11Encryption2Enabled)
296                         pAd->SharedKey[BSS0][0].CipherAlg = CIPHER_TKIP;
297                 else if (pAd->StaCfg.PairCipher == Ndis802_11Encryption3Enabled)
298                         pAd->SharedKey[BSS0][0].CipherAlg = CIPHER_AES;
299                 else
300                         pAd->SharedKey[BSS0][0].CipherAlg = CIPHER_NONE;
301
302             // Update these related information to MAC_TABLE_ENTRY
303                 pEntry = &pAd->MacTab.Content[BSSID_WCID];
304             NdisMoveMemory(pEntry->PairwiseKey.Key, pAd->SharedKey[BSS0][0].Key, LEN_TKIP_EK);
305                 NdisMoveMemory(pEntry->PairwiseKey.RxMic, pAd->SharedKey[BSS0][0].RxMic, LEN_TKIP_RXMICK);
306                 NdisMoveMemory(pEntry->PairwiseKey.TxMic, pAd->SharedKey[BSS0][0].TxMic, LEN_TKIP_TXMICK);
307                 pEntry->PairwiseKey.CipherAlg = pAd->SharedKey[BSS0][0].CipherAlg;
308
309                 // Update pairwise key information to ASIC Shared Key Table
310                 AsicAddSharedKeyEntry(pAd,
311                                                           BSS0,
312                                                           0,
313                                                           pAd->SharedKey[BSS0][0].CipherAlg,
314                                                           pAd->SharedKey[BSS0][0].Key,
315                                                           pAd->SharedKey[BSS0][0].TxMic,
316                                                           pAd->SharedKey[BSS0][0].RxMic);
317
318                 // Update ASIC WCID attribute table and IVEIV table
319                 RTMPAddWcidAttributeEntry(pAd,
320                                                                   BSS0,
321                                                                   0,
322                                                                   pAd->SharedKey[BSS0][0].CipherAlg,
323                                                                   pEntry);
324
325             if (pAd->StaCfg.AuthMode >= Ndis802_11AuthModeWPA2)
326             {
327                 // set 802.1x port control
328                                 STA_PORT_SECURED(pAd);
329
330                 // Indicate Connected for GUI
331                 pAd->IndicateMediaState = NdisMediaStateConnected;
332             }
333                 }
334         else
335         {
336             // Update GTK
337             pAd->StaCfg.DefaultKeyId = (pKey->KeyIndex & 0xFF);
338             NdisZeroMemory(&pAd->SharedKey[BSS0][pAd->StaCfg.DefaultKeyId], sizeof(CIPHER_KEY));
339             pAd->SharedKey[BSS0][pAd->StaCfg.DefaultKeyId].KeyLen = LEN_TKIP_EK;
340             NdisMoveMemory(pAd->SharedKey[BSS0][pAd->StaCfg.DefaultKeyId].Key, pKey->KeyMaterial, LEN_TKIP_EK);
341
342             if (pAd->StaCfg.GroupCipher == Ndis802_11Encryption2Enabled)
343             {
344                 NdisMoveMemory(pAd->SharedKey[BSS0][pAd->StaCfg.DefaultKeyId].RxMic, pKey->KeyMaterial + LEN_TKIP_EK, LEN_TKIP_TXMICK);
345                 NdisMoveMemory(pAd->SharedKey[BSS0][pAd->StaCfg.DefaultKeyId].TxMic, pKey->KeyMaterial + LEN_TKIP_EK + LEN_TKIP_TXMICK, LEN_TKIP_RXMICK);
346             }
347             else
348             {
349                 NdisMoveMemory(pAd->SharedKey[BSS0][pAd->StaCfg.DefaultKeyId].TxMic, pKey->KeyMaterial + LEN_TKIP_EK, LEN_TKIP_TXMICK);
350                 NdisMoveMemory(pAd->SharedKey[BSS0][pAd->StaCfg.DefaultKeyId].RxMic, pKey->KeyMaterial + LEN_TKIP_EK + LEN_TKIP_TXMICK, LEN_TKIP_RXMICK);
351             }
352
353             // Update Shared Key CipherAlg
354                 pAd->SharedKey[BSS0][pAd->StaCfg.DefaultKeyId].CipherAlg = CIPHER_NONE;
355                 if (pAd->StaCfg.GroupCipher == Ndis802_11Encryption2Enabled)
356                         pAd->SharedKey[BSS0][pAd->StaCfg.DefaultKeyId].CipherAlg = CIPHER_TKIP;
357                 else if (pAd->StaCfg.GroupCipher == Ndis802_11Encryption3Enabled)
358                         pAd->SharedKey[BSS0][pAd->StaCfg.DefaultKeyId].CipherAlg = CIPHER_AES;
359
360             // Update group key information to ASIC Shared Key Table
361                 AsicAddSharedKeyEntry(pAd,
362                                                           BSS0,
363                                                           pAd->StaCfg.DefaultKeyId,
364                                                           pAd->SharedKey[BSS0][pAd->StaCfg.DefaultKeyId].CipherAlg,
365                                                           pAd->SharedKey[BSS0][pAd->StaCfg.DefaultKeyId].Key,
366                                                           pAd->SharedKey[BSS0][pAd->StaCfg.DefaultKeyId].TxMic,
367                                                           pAd->SharedKey[BSS0][pAd->StaCfg.DefaultKeyId].RxMic);
368
369                 // Update ASIC WCID attribute table and IVEIV table
370                 RTMPAddWcidAttributeEntry(pAd,
371                                                                   BSS0,
372                                                                   pAd->StaCfg.DefaultKeyId,
373                                                                   pAd->SharedKey[BSS0][pAd->StaCfg.DefaultKeyId].CipherAlg,
374                                                                   NULL);
375
376             // set 802.1x port control
377                         STA_PORT_SECURED(pAd);
378
379             // Indicate Connected for GUI
380             pAd->IndicateMediaState = NdisMediaStateConnected;
381         }
382         }
383         else    // dynamic WEP from wpa_supplicant
384         {
385                 UCHAR   CipherAlg;
386         PUCHAR  Key;
387
388                 if(pKey->KeyLength == 32)
389                         goto end;
390
391                 KeyIdx = pKey->KeyIndex & 0x0fffffff;
392
393                 if (KeyIdx < 4)
394                 {
395                         // it is a default shared key, for Pairwise key setting
396                         if (pKey->KeyIndex & 0x80000000)
397                         {
398                                 pEntry = MacTableLookup(pAd, pKey->BSSID);
399
400                                 if (pEntry)
401                                 {
402                                         DBGPRINT(RT_DEBUG_TRACE, ("RTMPAddKey: Set Pair-wise Key\n"));
403
404                                         // set key material and key length
405                                         pEntry->PairwiseKey.KeyLen = (UCHAR)pKey->KeyLength;
406                                         NdisMoveMemory(pEntry->PairwiseKey.Key, &pKey->KeyMaterial, pKey->KeyLength);
407
408                                         // set Cipher type
409                                         if (pKey->KeyLength == 5)
410                                                 pEntry->PairwiseKey.CipherAlg = CIPHER_WEP64;
411                                         else
412                                                 pEntry->PairwiseKey.CipherAlg = CIPHER_WEP128;
413
414                                         // Add Pair-wise key to Asic
415                                         AsicAddPairwiseKeyEntry(
416                                                 pAd,
417                                                 pEntry->Addr,
418                                                 (UCHAR)pEntry->Aid,
419                                 &pEntry->PairwiseKey);
420
421                                         // update WCID attribute table and IVEIV table for this entry
422                                         RTMPAddWcidAttributeEntry(
423                                                 pAd,
424                                                 BSS0,
425                                                 KeyIdx, // The value may be not zero
426                                                 pEntry->PairwiseKey.CipherAlg,
427                                                 pEntry);
428
429                                 }
430                         }
431                         else
432             {
433                                 // Default key for tx (shared key)
434                                 pAd->StaCfg.DefaultKeyId = (UCHAR) KeyIdx;
435
436                                 // set key material and key length
437                                 pAd->SharedKey[BSS0][KeyIdx].KeyLen = (UCHAR) pKey->KeyLength;
438                                 NdisMoveMemory(pAd->SharedKey[BSS0][KeyIdx].Key, &pKey->KeyMaterial, pKey->KeyLength);
439
440                                 // Set Ciper type
441                                 if (pKey->KeyLength == 5)
442                                         pAd->SharedKey[BSS0][KeyIdx].CipherAlg = CIPHER_WEP64;
443                                 else
444                                         pAd->SharedKey[BSS0][KeyIdx].CipherAlg = CIPHER_WEP128;
445
446                         CipherAlg = pAd->SharedKey[BSS0][KeyIdx].CipherAlg;
447                         Key = pAd->SharedKey[BSS0][KeyIdx].Key;
448
449                                 // Set Group key material to Asic
450                         AsicAddSharedKeyEntry(pAd, BSS0, KeyIdx, CipherAlg, Key, NULL, NULL);
451
452                                 // Update WCID attribute table and IVEIV table for this group key table
453                                 RTMPAddWcidAttributeEntry(pAd, BSS0, KeyIdx, CipherAlg, NULL);
454
455                         }
456                 }
457         }
458 end:
459 #ifdef RT2860
460         RTMP_SET_PSFLAG(pAd, fRTMP_PS_CAN_GO_SLEEP);
461     DBGPRINT(RT_DEBUG_INFO, ("<------ RTMPAddKey\n"));
462 #endif
463         return;
464 }
465
466 char * rtstrchr(const char * s, int c)
467 {
468     for(; *s != (char) c; ++s)
469         if (*s == '\0')
470             return NULL;
471     return (char *) s;
472 }
473
474 /*
475 This is required for LinEX2004/kernel2.6.7 to provide iwlist scanning function
476 */
477
478 int
479 rt_ioctl_giwname(struct net_device *dev,
480                    struct iw_request_info *info,
481                    char *name, char *extra)
482 {
483 //      PRTMP_ADAPTER pAdapter = dev->ml_priv;
484         strncpy(name, RT28xx_CHIP_NAME " Wireless", IFNAMSIZ);
485         return 0;
486 }
487
488 int rt_ioctl_siwfreq(struct net_device *dev,
489                         struct iw_request_info *info,
490                         struct iw_freq *freq, char *extra)
491 {
492         PRTMP_ADAPTER pAdapter = dev->ml_priv;
493         int     chan = -1;
494
495     //check if the interface is down
496     if(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE))
497     {
498         DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
499         return -ENETDOWN;
500     }
501
502
503         if (freq->e > 1)
504                 return -EINVAL;
505
506         if((freq->e == 0) && (freq->m <= 1000))
507                 chan = freq->m; // Setting by channel number
508         else
509                 MAP_KHZ_TO_CHANNEL_ID( (freq->m /100) , chan); // Setting by frequency - search the table , like 2.412G, 2.422G,
510
511     if (ChannelSanity(pAdapter, chan) == TRUE)
512     {
513         pAdapter->CommonCfg.Channel = chan;
514         DBGPRINT(RT_DEBUG_ERROR, ("==>rt_ioctl_siwfreq::SIOCSIWFREQ[cmd=0x%x] (Channel=%d)\n", SIOCSIWFREQ, pAdapter->CommonCfg.Channel));
515     }
516     else
517         return -EINVAL;
518
519         return 0;
520 }
521 int rt_ioctl_giwfreq(struct net_device *dev,
522                    struct iw_request_info *info,
523                    struct iw_freq *freq, char *extra)
524 {
525         PRTMP_ADAPTER pAdapter = dev->ml_priv;
526         UCHAR ch = pAdapter->CommonCfg.Channel;
527         ULONG   m;
528
529         DBGPRINT(RT_DEBUG_TRACE,("==>rt_ioctl_giwfreq  %d\n", ch));
530
531     MAP_CHANNEL_ID_TO_KHZ(ch, m);
532         freq->m = m * 100;
533         freq->e = 1;
534         return 0;
535 }
536
537 int rt_ioctl_siwmode(struct net_device *dev,
538                    struct iw_request_info *info,
539                    __u32 *mode, char *extra)
540 {
541         PRTMP_ADAPTER pAdapter = dev->ml_priv;
542
543         //check if the interface is down
544     if(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE))
545     {
546         DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
547         return -ENETDOWN;
548     }
549
550         switch (*mode)
551         {
552                 case IW_MODE_ADHOC:
553                         Set_NetworkType_Proc(pAdapter, "Adhoc");
554                         break;
555                 case IW_MODE_INFRA:
556                         Set_NetworkType_Proc(pAdapter, "Infra");
557                         break;
558         case IW_MODE_MONITOR:
559                         Set_NetworkType_Proc(pAdapter, "Monitor");
560                         break;
561                 default:
562                         DBGPRINT(RT_DEBUG_TRACE, ("===>rt_ioctl_siwmode::SIOCSIWMODE (unknown %d)\n", *mode));
563                         return -EINVAL;
564         }
565
566         // Reset Ralink supplicant to not use, it will be set to start when UI set PMK key
567         pAdapter->StaCfg.WpaState = SS_NOTUSE;
568
569         return 0;
570 }
571
572 int rt_ioctl_giwmode(struct net_device *dev,
573                    struct iw_request_info *info,
574                    __u32 *mode, char *extra)
575 {
576         PRTMP_ADAPTER pAdapter = dev->ml_priv;
577
578         if (ADHOC_ON(pAdapter))
579                 *mode = IW_MODE_ADHOC;
580     else if (INFRA_ON(pAdapter))
581                 *mode = IW_MODE_INFRA;
582     else if (MONITOR_ON(pAdapter))
583     {
584         *mode = IW_MODE_MONITOR;
585     }
586     else
587         *mode = IW_MODE_AUTO;
588
589         DBGPRINT(RT_DEBUG_TRACE, ("==>rt_ioctl_giwmode(mode=%d)\n", *mode));
590         return 0;
591 }
592
593 int rt_ioctl_siwsens(struct net_device *dev,
594                    struct iw_request_info *info,
595                    char *name, char *extra)
596 {
597         PRTMP_ADAPTER pAdapter = dev->ml_priv;
598
599         //check if the interface is down
600         if(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE))
601         {
602                 DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
603                 return -ENETDOWN;
604         }
605
606         return 0;
607 }
608
609 int rt_ioctl_giwsens(struct net_device *dev,
610                    struct iw_request_info *info,
611                    char *name, char *extra)
612 {
613         return 0;
614 }
615
616 int rt_ioctl_giwrange(struct net_device *dev,
617                    struct iw_request_info *info,
618                    struct iw_point *data, char *extra)
619 {
620         PRTMP_ADAPTER pAdapter = dev->ml_priv;
621         struct iw_range *range = (struct iw_range *) extra;
622         u16 val;
623         int i;
624
625         DBGPRINT(RT_DEBUG_TRACE ,("===>rt_ioctl_giwrange\n"));
626         data->length = sizeof(struct iw_range);
627         memset(range, 0, sizeof(struct iw_range));
628
629         range->txpower_capa = IW_TXPOW_DBM;
630
631         if (INFRA_ON(pAdapter)||ADHOC_ON(pAdapter))
632         {
633                 range->min_pmp = 1 * 1024;
634                 range->max_pmp = 65535 * 1024;
635                 range->min_pmt = 1 * 1024;
636                 range->max_pmt = 1000 * 1024;
637                 range->pmp_flags = IW_POWER_PERIOD;
638                 range->pmt_flags = IW_POWER_TIMEOUT;
639                 range->pm_capa = IW_POWER_PERIOD | IW_POWER_TIMEOUT |
640                         IW_POWER_UNICAST_R | IW_POWER_ALL_R;
641         }
642
643         range->we_version_compiled = WIRELESS_EXT;
644         range->we_version_source = 14;
645
646         range->retry_capa = IW_RETRY_LIMIT;
647         range->retry_flags = IW_RETRY_LIMIT;
648         range->min_retry = 0;
649         range->max_retry = 255;
650
651         range->num_channels =  pAdapter->ChannelListNum;
652
653         val = 0;
654         for (i = 1; i <= range->num_channels; i++)
655         {
656                 u32 m;
657                 range->freq[val].i = pAdapter->ChannelList[i-1].Channel;
658                 MAP_CHANNEL_ID_TO_KHZ(pAdapter->ChannelList[i-1].Channel, m);
659                 range->freq[val].m = m * 100; /* HZ */
660
661                 range->freq[val].e = 1;
662                 val++;
663                 if (val == IW_MAX_FREQUENCIES)
664                         break;
665         }
666         range->num_frequency = val;
667
668         range->max_qual.qual = 100; /* what is correct max? This was not
669                                         * documented exactly. At least
670                                         * 69 has been observed. */
671         range->max_qual.level = 0; /* dB */
672         range->max_qual.noise = 0; /* dB */
673
674         /* What would be suitable values for "average/typical" qual? */
675         range->avg_qual.qual = 20;
676         range->avg_qual.level = -60;
677         range->avg_qual.noise = -95;
678         range->sensitivity = 3;
679
680         range->max_encoding_tokens = NR_WEP_KEYS;
681         range->num_encoding_sizes = 2;
682         range->encoding_size[0] = 5;
683         range->encoding_size[1] = 13;
684
685         range->min_rts = 0;
686         range->max_rts = 2347;
687         range->min_frag = 256;
688         range->max_frag = 2346;
689
690         /* IW_ENC_CAPA_* bit field */
691         range->enc_capa = IW_ENC_CAPA_WPA | IW_ENC_CAPA_WPA2 |
692                                         IW_ENC_CAPA_CIPHER_TKIP | IW_ENC_CAPA_CIPHER_CCMP;
693
694         return 0;
695 }
696
697 int rt_ioctl_siwap(struct net_device *dev,
698                       struct iw_request_info *info,
699                       struct sockaddr *ap_addr, char *extra)
700 {
701         PRTMP_ADAPTER pAdapter = dev->ml_priv;
702     NDIS_802_11_MAC_ADDRESS Bssid;
703
704         //check if the interface is down
705         if(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE))
706         {
707         DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
708         return -ENETDOWN;
709     }
710
711         if (pAdapter->Mlme.CntlMachine.CurrState != CNTL_IDLE)
712     {
713         RT28XX_MLME_RESET_STATE_MACHINE(pAdapter);
714         DBGPRINT(RT_DEBUG_TRACE, ("!!! MLME busy, reset MLME state machine !!!\n"));
715     }
716
717     // tell CNTL state machine to call NdisMSetInformationComplete() after completing
718     // this request, because this request is initiated by NDIS.
719     pAdapter->MlmeAux.CurrReqIsFromNdis = FALSE;
720         // Prevent to connect AP again in STAMlmePeriodicExec
721         pAdapter->MlmeAux.AutoReconnectSsidLen= 32;
722
723     memset(Bssid, 0, MAC_ADDR_LEN);
724     memcpy(Bssid, ap_addr->sa_data, MAC_ADDR_LEN);
725     MlmeEnqueue(pAdapter,
726                 MLME_CNTL_STATE_MACHINE,
727                 OID_802_11_BSSID,
728                 sizeof(NDIS_802_11_MAC_ADDRESS),
729                 (VOID *)&Bssid);
730
731     DBGPRINT(RT_DEBUG_TRACE, ("IOCTL::SIOCSIWAP %02x:%02x:%02x:%02x:%02x:%02x\n",
732         Bssid[0], Bssid[1], Bssid[2], Bssid[3], Bssid[4], Bssid[5]));
733
734         return 0;
735 }
736
737 int rt_ioctl_giwap(struct net_device *dev,
738                       struct iw_request_info *info,
739                       struct sockaddr *ap_addr, char *extra)
740 {
741         PRTMP_ADAPTER pAdapter = dev->ml_priv;
742
743         if (INFRA_ON(pAdapter) || ADHOC_ON(pAdapter))
744         {
745                 ap_addr->sa_family = ARPHRD_ETHER;
746                 memcpy(ap_addr->sa_data, &pAdapter->CommonCfg.Bssid, ETH_ALEN);
747         }
748     // Add for RT2870
749     else if (pAdapter->StaCfg.WpaSupplicantUP != WPA_SUPPLICANT_DISABLE)
750     {
751         ap_addr->sa_family = ARPHRD_ETHER;
752         memcpy(ap_addr->sa_data, &pAdapter->MlmeAux.Bssid, ETH_ALEN);
753     }
754         else
755         {
756                 DBGPRINT(RT_DEBUG_TRACE, ("IOCTL::SIOCGIWAP(=EMPTY)\n"));
757                 return -ENOTCONN;
758         }
759
760         return 0;
761 }
762
763 /*
764  * Units are in db above the noise floor. That means the
765  * rssi values reported in the tx/rx descriptors in the
766  * driver are the SNR expressed in db.
767  *
768  * If you assume that the noise floor is -95, which is an
769  * excellent assumption 99.5 % of the time, then you can
770  * derive the absolute signal level (i.e. -95 + rssi).
771  * There are some other slight factors to take into account
772  * depending on whether the rssi measurement is from 11b,
773  * 11g, or 11a.   These differences are at most 2db and
774  * can be documented.
775  *
776  * NB: various calculations are based on the orinoco/wavelan
777  *     drivers for compatibility
778  */
779 static void set_quality(PRTMP_ADAPTER pAdapter,
780                         struct iw_quality *iq,
781                         signed char rssi)
782 {
783         __u8 ChannelQuality;
784
785         // Normalize Rssi
786         if (rssi >= -50)
787                 ChannelQuality = 100;
788         else if (rssi >= -80) // between -50 ~ -80dbm
789                 ChannelQuality = (__u8)(24 + ((rssi + 80) * 26)/10);
790         else if (rssi >= -90)   // between -80 ~ -90dbm
791         ChannelQuality = (__u8)((rssi + 90) * 26)/10;
792         else
793                 ChannelQuality = 0;
794
795     iq->qual = (__u8)ChannelQuality;
796
797     iq->level = (__u8)(rssi);
798     iq->noise = (pAdapter->BbpWriteLatch[66] > pAdapter->BbpTuning.FalseCcaUpperThreshold) ? ((__u8)pAdapter->BbpTuning.FalseCcaUpperThreshold) : ((__u8) pAdapter->BbpWriteLatch[66]);         // noise level (dBm)
799     iq->noise += 256 - 143;
800     iq->updated = pAdapter->iw_stats.qual.updated;
801 }
802
803 int rt_ioctl_iwaplist(struct net_device *dev,
804                         struct iw_request_info *info,
805                         struct iw_point *data, char *extra)
806 {
807         PRTMP_ADAPTER pAdapter = dev->ml_priv;
808
809         struct sockaddr addr[IW_MAX_AP];
810         struct iw_quality qual[IW_MAX_AP];
811         int i;
812
813         //check if the interface is down
814     if(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE))
815     {
816         DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
817                 data->length = 0;
818                 return 0;
819         //return -ENETDOWN;
820         }
821
822         for (i = 0; i <IW_MAX_AP ; i++)
823         {
824                 if (i >=  pAdapter->ScanTab.BssNr)
825                         break;
826                 addr[i].sa_family = ARPHRD_ETHER;
827                         memcpy(addr[i].sa_data, &pAdapter->ScanTab.BssEntry[i].Bssid, MAC_ADDR_LEN);
828                 set_quality(pAdapter, &qual[i], pAdapter->ScanTab.BssEntry[i].Rssi);
829         }
830         data->length = i;
831         memcpy(extra, &addr, i*sizeof(addr[0]));
832         data->flags = 1;                /* signal quality present (sort of) */
833         memcpy(extra + i*sizeof(addr[0]), &qual, i*sizeof(qual[i]));
834
835         return 0;
836 }
837
838 int rt_ioctl_siwscan(struct net_device *dev,
839                         struct iw_request_info *info,
840                         struct iw_point *data, char *extra)
841 {
842         PRTMP_ADAPTER pAdapter = dev->ml_priv;
843
844         ULONG                                                           Now;
845         int Status = NDIS_STATUS_SUCCESS;
846
847         //check if the interface is down
848         if(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE))
849         {
850                 DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
851                 return -ENETDOWN;
852         }
853
854         if (MONITOR_ON(pAdapter))
855     {
856         DBGPRINT(RT_DEBUG_TRACE, ("!!! Driver is in Monitor Mode now !!!\n"));
857         return -EINVAL;
858     }
859 #ifdef RT2860
860         if ((pAdapter->OpMode == OPMODE_STA) && (IDLE_ON(pAdapter))
861                 && (pAdapter->StaCfg.bRadio == TRUE)
862                 && (RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_IDLE_RADIO_OFF)))
863         {
864                 RT28xxPciAsicRadioOn(pAdapter, GUI_IDLE_POWER_SAVE);
865         }
866         // Check if still radio off.
867         else if (pAdapter->bPCIclkOff == TRUE)
868                 return 0;
869 #endif
870         if (pAdapter->StaCfg.WpaSupplicantUP == WPA_SUPPLICANT_ENABLE)
871         {
872                 pAdapter->StaCfg.WpaSupplicantScanCount++;
873         }
874
875     pAdapter->StaCfg.bScanReqIsFromWebUI = TRUE;
876         if (RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_BSS_SCAN_IN_PROGRESS))
877                 return 0;
878         do{
879                 Now = jiffies;
880
881                 if ((pAdapter->StaCfg.WpaSupplicantUP == WPA_SUPPLICANT_ENABLE) &&
882                         (pAdapter->StaCfg.WpaSupplicantScanCount > 3))
883                 {
884                         DBGPRINT(RT_DEBUG_TRACE, ("!!! WpaSupplicantScanCount > 3\n"));
885                         Status = NDIS_STATUS_SUCCESS;
886                         break;
887                 }
888
889                 if ((OPSTATUS_TEST_FLAG(pAdapter, fOP_STATUS_MEDIA_STATE_CONNECTED)) &&
890                         ((pAdapter->StaCfg.AuthMode == Ndis802_11AuthModeWPA) ||
891                         (pAdapter->StaCfg.AuthMode == Ndis802_11AuthModeWPAPSK)) &&
892                         (pAdapter->StaCfg.PortSecured == WPA_802_1X_PORT_NOT_SECURED))
893                 {
894                         DBGPRINT(RT_DEBUG_TRACE, ("!!! Link UP, Port Not Secured! ignore this set::OID_802_11_BSSID_LIST_SCAN\n"));
895                         Status = NDIS_STATUS_SUCCESS;
896                         break;
897                 }
898
899                 if (pAdapter->Mlme.CntlMachine.CurrState != CNTL_IDLE)
900                 {
901                         RT28XX_MLME_RESET_STATE_MACHINE(pAdapter);
902                         DBGPRINT(RT_DEBUG_TRACE, ("!!! MLME busy, reset MLME state machine !!!\n"));
903                 }
904
905                 // tell CNTL state machine to call NdisMSetInformationComplete() after completing
906                 // this request, because this request is initiated by NDIS.
907                 pAdapter->MlmeAux.CurrReqIsFromNdis = FALSE;
908                 // Reset allowed scan retries
909                 pAdapter->StaCfg.ScanCnt = 0;
910                 pAdapter->StaCfg.LastScanTime = Now;
911
912                 MlmeEnqueue(pAdapter,
913                         MLME_CNTL_STATE_MACHINE,
914                         OID_802_11_BSSID_LIST_SCAN,
915                         0,
916                         NULL);
917
918                 Status = NDIS_STATUS_SUCCESS;
919                 RT28XX_MLME_HANDLER(pAdapter);
920         }while(0);
921         return 0;
922 }
923
924 int rt_ioctl_giwscan(struct net_device *dev,
925                         struct iw_request_info *info,
926                         struct iw_point *data, char *extra)
927 {
928
929         PRTMP_ADAPTER pAdapter = dev->ml_priv;
930         int i=0;
931         char *current_ev = extra, *previous_ev = extra;
932         char *end_buf;
933         char *current_val, custom[MAX_CUSTOM_LEN] = {0};
934         struct iw_event iwe;
935
936         if (RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_BSS_SCAN_IN_PROGRESS))
937     {
938                 /*
939                  * Still scanning, indicate the caller should try again.
940                  */
941                 return -EAGAIN;
942         }
943
944         if (pAdapter->StaCfg.WpaSupplicantUP == WPA_SUPPLICANT_ENABLE)
945         {
946                 pAdapter->StaCfg.WpaSupplicantScanCount = 0;
947         }
948
949         if (pAdapter->ScanTab.BssNr == 0)
950         {
951                 data->length = 0;
952                 return 0;
953         }
954
955     if (data->length > 0)
956         end_buf = extra + data->length;
957     else
958         end_buf = extra + IW_SCAN_MAX_DATA;
959
960         for (i = 0; i < pAdapter->ScanTab.BssNr; i++)
961         {
962                 if (current_ev >= end_buf)
963                         return -E2BIG;
964
965                 //MAC address
966                 //================================
967                 memset(&iwe, 0, sizeof(iwe));
968                 iwe.cmd = SIOCGIWAP;
969                 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
970                 memcpy(iwe.u.ap_addr.sa_data, &pAdapter->ScanTab.BssEntry[i].Bssid, ETH_ALEN);
971
972         previous_ev = current_ev;
973                 current_ev = iwe_stream_add_event(info, current_ev,end_buf, &iwe, IW_EV_ADDR_LEN);
974         if (current_ev == previous_ev)
975                 return -E2BIG;
976
977                 /*
978                 Protocol:
979                         it will show scanned AP's WirelessMode .
980                         it might be
981                                         802.11a
982                                         802.11a/n
983                                         802.11g/n
984                                         802.11b/g/n
985                                         802.11g
986                                         802.11b/g
987                 */
988                 memset(&iwe, 0, sizeof(iwe));
989                 iwe.cmd = SIOCGIWNAME;
990
991
992         {
993                 PBSS_ENTRY pBssEntry=&pAdapter->ScanTab.BssEntry[i];
994                 BOOLEAN isGonly=FALSE;
995                 int rateCnt=0;
996
997                 if (pBssEntry->Channel>14)
998                 {
999                         if (pBssEntry->HtCapabilityLen!=0)
1000                                 strcpy(iwe.u.name,"802.11a/n");
1001                         else
1002                                 strcpy(iwe.u.name,"802.11a");
1003                 }
1004                 else
1005                 {
1006                         /*
1007                                 if one of non B mode rate is set supported rate . it mean G only.
1008                         */
1009                         for (rateCnt=0;rateCnt<pBssEntry->SupRateLen;rateCnt++)
1010                         {
1011                                 /*
1012                                         6Mbps(140) 9Mbps(146) and >=12Mbps(152) are supported rate , it mean G only.
1013                                 */
1014                                 if (pBssEntry->SupRate[rateCnt]==140 || pBssEntry->SupRate[rateCnt]==146 || pBssEntry->SupRate[rateCnt]>=152)
1015                                         isGonly=TRUE;
1016                         }
1017
1018                         for (rateCnt=0;rateCnt<pBssEntry->ExtRateLen;rateCnt++)
1019                         {
1020                                 if (pBssEntry->ExtRate[rateCnt]==140 || pBssEntry->ExtRate[rateCnt]==146 || pBssEntry->ExtRate[rateCnt]>=152)
1021                                         isGonly=TRUE;
1022                         }
1023
1024
1025                         if (pBssEntry->HtCapabilityLen!=0)
1026                         {
1027                                 if (isGonly==TRUE)
1028                                         strcpy(iwe.u.name,"802.11g/n");
1029                                 else
1030                                         strcpy(iwe.u.name,"802.11b/g/n");
1031                         }
1032                         else
1033                         {
1034                                 if (isGonly==TRUE)
1035                                         strcpy(iwe.u.name,"802.11g");
1036                                 else
1037                                 {
1038                                         if (pBssEntry->SupRateLen==4 && pBssEntry->ExtRateLen==0)
1039                                                 strcpy(iwe.u.name,"802.11b");
1040                                         else
1041                                                 strcpy(iwe.u.name,"802.11b/g");
1042                                 }
1043                         }
1044                 }
1045         }
1046
1047                 previous_ev = current_ev;
1048                 current_ev       = iwe_stream_add_event(info, current_ev, end_buf, &iwe, IW_EV_ADDR_LEN);
1049         if (current_ev == previous_ev)
1050                 return -E2BIG;
1051
1052                 //ESSID
1053                 //================================
1054                 memset(&iwe, 0, sizeof(iwe));
1055                 iwe.cmd = SIOCGIWESSID;
1056                 iwe.u.data.length = pAdapter->ScanTab.BssEntry[i].SsidLen;
1057                 iwe.u.data.flags = 1;
1058
1059         previous_ev = current_ev;
1060                 current_ev = iwe_stream_add_point(info, current_ev,end_buf, &iwe, pAdapter->ScanTab.BssEntry[i].Ssid);
1061         if (current_ev == previous_ev)
1062                 return -E2BIG;
1063
1064                 //Network Type
1065                 //================================
1066                 memset(&iwe, 0, sizeof(iwe));
1067                 iwe.cmd = SIOCGIWMODE;
1068                 if (pAdapter->ScanTab.BssEntry[i].BssType == Ndis802_11IBSS)
1069                 {
1070                         iwe.u.mode = IW_MODE_ADHOC;
1071                 }
1072                 else if (pAdapter->ScanTab.BssEntry[i].BssType == Ndis802_11Infrastructure)
1073                 {
1074                         iwe.u.mode = IW_MODE_INFRA;
1075                 }
1076                 else
1077                 {
1078                         iwe.u.mode = IW_MODE_AUTO;
1079                 }
1080                 iwe.len = IW_EV_UINT_LEN;
1081
1082         previous_ev = current_ev;
1083                 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,  IW_EV_UINT_LEN);
1084         if (current_ev == previous_ev)
1085                 return -E2BIG;
1086
1087                 //Channel and Frequency
1088                 //================================
1089                 memset(&iwe, 0, sizeof(iwe));
1090                 iwe.cmd = SIOCGIWFREQ;
1091                 if (INFRA_ON(pAdapter) || ADHOC_ON(pAdapter))
1092                         iwe.u.freq.m = pAdapter->ScanTab.BssEntry[i].Channel;
1093                 else
1094                         iwe.u.freq.m = pAdapter->ScanTab.BssEntry[i].Channel;
1095                 iwe.u.freq.e = 0;
1096                 iwe.u.freq.i = 0;
1097
1098                 previous_ev = current_ev;
1099                 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe, IW_EV_FREQ_LEN);
1100         if (current_ev == previous_ev)
1101                 return -E2BIG;
1102
1103         //Add quality statistics
1104         //================================
1105         memset(&iwe, 0, sizeof(iwe));
1106         iwe.cmd = IWEVQUAL;
1107         iwe.u.qual.level = 0;
1108         iwe.u.qual.noise = 0;
1109         set_quality(pAdapter, &iwe.u.qual, pAdapter->ScanTab.BssEntry[i].Rssi);
1110         current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe, IW_EV_QUAL_LEN);
1111         if (current_ev == previous_ev)
1112                 return -E2BIG;
1113
1114                 //Encyption key
1115                 //================================
1116                 memset(&iwe, 0, sizeof(iwe));
1117                 iwe.cmd = SIOCGIWENCODE;
1118                 if (CAP_IS_PRIVACY_ON (pAdapter->ScanTab.BssEntry[i].CapabilityInfo ))
1119                         iwe.u.data.flags =IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
1120                 else
1121                         iwe.u.data.flags = IW_ENCODE_DISABLED;
1122
1123         previous_ev = current_ev;
1124         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);
1125         if (current_ev == previous_ev)
1126                 return -E2BIG;
1127
1128                 //Bit Rate
1129                 //================================
1130                 if (pAdapter->ScanTab.BssEntry[i].SupRateLen)
1131         {
1132             UCHAR tmpRate = pAdapter->ScanTab.BssEntry[i].SupRate[pAdapter->ScanTab.BssEntry[i].SupRateLen-1];
1133                         memset(&iwe, 0, sizeof(iwe));
1134                         iwe.cmd = SIOCGIWRATE;
1135                 current_val = current_ev + IW_EV_LCP_LEN;
1136             if (tmpRate == 0x82)
1137                 iwe.u.bitrate.value =  1 * 1000000;
1138             else if (tmpRate == 0x84)
1139                 iwe.u.bitrate.value =  2 * 1000000;
1140             else if (tmpRate == 0x8B)
1141                 iwe.u.bitrate.value =  5.5 * 1000000;
1142             else if (tmpRate == 0x96)
1143                 iwe.u.bitrate.value =  11 * 1000000;
1144             else
1145                     iwe.u.bitrate.value =  (tmpRate/2) * 1000000;
1146
1147                         iwe.u.bitrate.disabled = 0;
1148                         current_val = iwe_stream_add_value(info, current_ev,
1149                                 current_val, end_buf, &iwe,
1150                         IW_EV_PARAM_LEN);
1151
1152                 if((current_val-current_ev)>IW_EV_LCP_LEN)
1153                 current_ev = current_val;
1154                 else
1155                         return -E2BIG;
1156         }
1157
1158                 //WPA IE
1159                 if (pAdapter->ScanTab.BssEntry[i].WpaIE.IELen > 0)
1160                 {
1161                         memset(&iwe, 0, sizeof(iwe));
1162                         memset(&custom[0], 0, MAX_CUSTOM_LEN);
1163                         memcpy(custom, &(pAdapter->ScanTab.BssEntry[i].WpaIE.IE[0]),
1164                                                    pAdapter->ScanTab.BssEntry[i].WpaIE.IELen);
1165                         iwe.cmd = IWEVGENIE;
1166                         iwe.u.data.length = pAdapter->ScanTab.BssEntry[i].WpaIE.IELen;
1167                         current_ev = iwe_stream_add_point(info, current_ev, end_buf, &iwe, custom);
1168                         if (current_ev == previous_ev)
1169                                 return -E2BIG;
1170                 }
1171
1172                 //WPA2 IE
1173         if (pAdapter->ScanTab.BssEntry[i].RsnIE.IELen > 0)
1174         {
1175                 memset(&iwe, 0, sizeof(iwe));
1176                         memset(&custom[0], 0, MAX_CUSTOM_LEN);
1177                         memcpy(custom, &(pAdapter->ScanTab.BssEntry[i].RsnIE.IE[0]),
1178                                                    pAdapter->ScanTab.BssEntry[i].RsnIE.IELen);
1179                         iwe.cmd = IWEVGENIE;
1180                         iwe.u.data.length = pAdapter->ScanTab.BssEntry[i].RsnIE.IELen;
1181                         current_ev = iwe_stream_add_point(info, current_ev, end_buf, &iwe, custom);
1182                         if (current_ev == previous_ev)
1183                                 return -E2BIG;
1184         }
1185         }
1186
1187         data->length = current_ev - extra;
1188     pAdapter->StaCfg.bScanReqIsFromWebUI = FALSE;
1189         DBGPRINT(RT_DEBUG_ERROR ,("===>rt_ioctl_giwscan. %d(%d) BSS returned, data->length = %d\n",i , pAdapter->ScanTab.BssNr, data->length));
1190         return 0;
1191 }
1192
1193 int rt_ioctl_siwessid(struct net_device *dev,
1194                          struct iw_request_info *info,
1195                          struct iw_point *data, char *essid)
1196 {
1197         PRTMP_ADAPTER pAdapter = dev->ml_priv;
1198
1199         //check if the interface is down
1200     if(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE))
1201     {
1202         DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
1203         return -ENETDOWN;
1204     }
1205
1206         if (data->flags)
1207         {
1208                 PCHAR   pSsidString = NULL;
1209
1210                 // Includes null character.
1211                 if (data->length > (IW_ESSID_MAX_SIZE + 1))
1212                         return -E2BIG;
1213
1214                 pSsidString = (CHAR *) kmalloc(MAX_LEN_OF_SSID+1, MEM_ALLOC_FLAG);
1215                 if (pSsidString)
1216                 {
1217                         NdisZeroMemory(pSsidString, MAX_LEN_OF_SSID+1);
1218                         NdisMoveMemory(pSsidString, essid, data->length);
1219                         if (Set_SSID_Proc(pAdapter, pSsidString) == FALSE)
1220                                 return -EINVAL;
1221                 }
1222                 else
1223                         return -ENOMEM;
1224         }
1225         else
1226         {
1227                 // ANY ssid
1228                 if (Set_SSID_Proc(pAdapter, "") == FALSE)
1229                         return -EINVAL;
1230     }
1231         return 0;
1232 }
1233
1234 int rt_ioctl_giwessid(struct net_device *dev,
1235                          struct iw_request_info *info,
1236                          struct iw_point *data, char *essid)
1237 {
1238         PRTMP_ADAPTER pAdapter = dev->ml_priv;
1239
1240         data->flags = 1;
1241     if (MONITOR_ON(pAdapter))
1242     {
1243         data->length  = 0;
1244         return 0;
1245     }
1246
1247         if (OPSTATUS_TEST_FLAG(pAdapter, fOP_STATUS_MEDIA_STATE_CONNECTED))
1248         {
1249                 DBGPRINT(RT_DEBUG_TRACE ,("MediaState is connected\n"));
1250                 data->length = pAdapter->CommonCfg.SsidLen;
1251                 memcpy(essid, pAdapter->CommonCfg.Ssid, pAdapter->CommonCfg.SsidLen);
1252         }
1253 #ifdef RT2870
1254     // Add for RT2870
1255     else if (pAdapter->StaCfg.WpaSupplicantUP != WPA_SUPPLICANT_DISABLE)
1256     {
1257         data->length = pAdapter->CommonCfg.SsidLen;
1258                 memcpy(essid, pAdapter->CommonCfg.Ssid, pAdapter->CommonCfg.SsidLen);
1259         }
1260 #endif // RT2870 //
1261         else
1262         {//the ANY ssid was specified
1263                 data->length  = 0;
1264                 DBGPRINT(RT_DEBUG_TRACE ,("MediaState is not connected, ess\n"));
1265         }
1266
1267         return 0;
1268
1269 }
1270
1271 int rt_ioctl_siwnickn(struct net_device *dev,
1272                          struct iw_request_info *info,
1273                          struct iw_point *data, char *nickname)
1274 {
1275         PRTMP_ADAPTER pAdapter = dev->ml_priv;
1276
1277     //check if the interface is down
1278     if(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE))
1279     {
1280         DBGPRINT(RT_DEBUG_TRACE ,("INFO::Network is down!\n"));
1281         return -ENETDOWN;
1282     }
1283
1284         if (data->length > IW_ESSID_MAX_SIZE)
1285                 return -EINVAL;
1286
1287         memset(pAdapter->nickname, 0, IW_ESSID_MAX_SIZE + 1);
1288         memcpy(pAdapter->nickname, nickname, data->length);
1289
1290
1291         return 0;
1292 }
1293
1294 int rt_ioctl_giwnickn(struct net_device *dev,
1295                          struct iw_request_info *info,
1296                          struct iw_point *data, char *nickname)
1297 {
1298         PRTMP_ADAPTER pAdapter = dev->ml_priv;
1299
1300         if (data->length > strlen(pAdapter->nickname) + 1)
1301                 data->length = strlen(pAdapter->nickname) + 1;
1302         if (data->length > 0) {
1303                 memcpy(nickname, pAdapter->nickname, data->length-1);
1304                 nickname[data->length-1] = '\0';
1305         }
1306         return 0;
1307 }
1308
1309 int rt_ioctl_siwrts(struct net_device *dev,
1310                        struct iw_request_info *info,
1311                        struct iw_param *rts, char *extra)
1312 {
1313         PRTMP_ADAPTER pAdapter = dev->ml_priv;
1314         u16 val;
1315
1316     //check if the interface is down
1317     if(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE))
1318     {
1319         DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
1320         return -ENETDOWN;
1321     }
1322
1323         if (rts->disabled)
1324                 val = MAX_RTS_THRESHOLD;
1325         else if (rts->value < 0 || rts->value > MAX_RTS_THRESHOLD)
1326                 return -EINVAL;
1327         else if (rts->value == 0)
1328             val = MAX_RTS_THRESHOLD;
1329         else
1330                 val = rts->value;
1331
1332         if (val != pAdapter->CommonCfg.RtsThreshold)
1333                 pAdapter->CommonCfg.RtsThreshold = val;
1334
1335         return 0;
1336 }
1337
1338 int rt_ioctl_giwrts(struct net_device *dev,
1339                        struct iw_request_info *info,
1340                        struct iw_param *rts, char *extra)
1341 {
1342         PRTMP_ADAPTER pAdapter = dev->ml_priv;
1343
1344         //check if the interface is down
1345         if(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE))
1346         {
1347                 DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
1348                 return -ENETDOWN;
1349         }
1350
1351         rts->value = pAdapter->CommonCfg.RtsThreshold;
1352         rts->disabled = (rts->value == MAX_RTS_THRESHOLD);
1353         rts->fixed = 1;
1354
1355         return 0;
1356 }
1357
1358 int rt_ioctl_siwfrag(struct net_device *dev,
1359                         struct iw_request_info *info,
1360                         struct iw_param *frag, char *extra)
1361 {
1362         PRTMP_ADAPTER pAdapter = dev->ml_priv;
1363         u16 val;
1364
1365         //check if the interface is down
1366         if(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE))
1367         {
1368                 DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
1369                 return -ENETDOWN;
1370         }
1371
1372         if (frag->disabled)
1373                 val = MAX_FRAG_THRESHOLD;
1374         else if (frag->value >= MIN_FRAG_THRESHOLD || frag->value <= MAX_FRAG_THRESHOLD)
1375         val = __cpu_to_le16(frag->value & ~0x1); /* even numbers only */
1376         else if (frag->value == 0)
1377             val = MAX_FRAG_THRESHOLD;
1378         else
1379                 return -EINVAL;
1380
1381         pAdapter->CommonCfg.FragmentThreshold = val;
1382         return 0;
1383 }
1384
1385 int rt_ioctl_giwfrag(struct net_device *dev,
1386                         struct iw_request_info *info,
1387                         struct iw_param *frag, char *extra)
1388 {
1389         PRTMP_ADAPTER pAdapter = dev->ml_priv;
1390
1391         //check if the interface is down
1392         if(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE))
1393         {
1394                 DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
1395                 return -ENETDOWN;
1396         }
1397
1398         frag->value = pAdapter->CommonCfg.FragmentThreshold;
1399         frag->disabled = (frag->value == MAX_FRAG_THRESHOLD);
1400         frag->fixed = 1;
1401
1402         return 0;
1403 }
1404
1405 #define MAX_WEP_KEY_SIZE 13
1406 #define MIN_WEP_KEY_SIZE 5
1407 int rt_ioctl_siwencode(struct net_device *dev,
1408                           struct iw_request_info *info,
1409                           struct iw_point *erq, char *extra)
1410 {
1411         PRTMP_ADAPTER pAdapter = dev->ml_priv;
1412
1413         //check if the interface is down
1414         if(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE))
1415         {
1416                 DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
1417                 return -ENETDOWN;
1418         }
1419
1420         if ((erq->length == 0) &&
1421         (erq->flags & IW_ENCODE_DISABLED))
1422         {
1423                 pAdapter->StaCfg.PairCipher = Ndis802_11WEPDisabled;
1424                 pAdapter->StaCfg.GroupCipher = Ndis802_11WEPDisabled;
1425                 pAdapter->StaCfg.WepStatus = Ndis802_11WEPDisabled;
1426         pAdapter->StaCfg.OrigWepStatus = pAdapter->StaCfg.WepStatus;
1427         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeOpen;
1428         goto done;
1429         } else if (
1430                  (erq->flags & IW_ENCODE_RESTRICTED || erq->flags & IW_ENCODE_OPEN)) {
1431                 STA_PORT_SECURED(pAdapter);
1432                 pAdapter->StaCfg.PairCipher = Ndis802_11WEPEnabled;
1433                 pAdapter->StaCfg.GroupCipher = Ndis802_11WEPEnabled;
1434                 pAdapter->StaCfg.WepStatus = Ndis802_11WEPEnabled;
1435         pAdapter->StaCfg.OrigWepStatus = pAdapter->StaCfg.WepStatus;
1436                 if (erq->flags & IW_ENCODE_RESTRICTED)
1437                         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeShared;
1438         else
1439                         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeOpen;
1440         }
1441
1442     if (erq->length > 0)
1443         {
1444                 int keyIdx = (erq->flags & IW_ENCODE_INDEX) - 1;
1445                 /* Check the size of the key */
1446                 if (erq->length > MAX_WEP_KEY_SIZE) {
1447                         return -EINVAL;
1448                 }
1449                 /* Check key index */
1450                 if ((keyIdx < 0) || (keyIdx >= NR_WEP_KEYS))
1451         {
1452             DBGPRINT(RT_DEBUG_TRACE ,("==>rt_ioctl_siwencode::Wrong keyIdx=%d! Using default key instead (%d)\n",
1453                                         keyIdx, pAdapter->StaCfg.DefaultKeyId));
1454
1455             //Using default key
1456                         keyIdx = pAdapter->StaCfg.DefaultKeyId;
1457         }
1458                 else
1459                         pAdapter->StaCfg.DefaultKeyId=keyIdx;
1460
1461         NdisZeroMemory(pAdapter->SharedKey[BSS0][keyIdx].Key,  16);
1462
1463                 if (erq->length == MAX_WEP_KEY_SIZE)
1464         {
1465                         pAdapter->SharedKey[BSS0][keyIdx].KeyLen = MAX_WEP_KEY_SIZE;
1466             pAdapter->SharedKey[BSS0][keyIdx].CipherAlg = CIPHER_WEP128;
1467                 }
1468                 else if (erq->length == MIN_WEP_KEY_SIZE)
1469         {
1470             pAdapter->SharedKey[BSS0][keyIdx].KeyLen = MIN_WEP_KEY_SIZE;
1471             pAdapter->SharedKey[BSS0][keyIdx].CipherAlg = CIPHER_WEP64;
1472                 }
1473                 else
1474                         /* Disable the key */
1475                         pAdapter->SharedKey[BSS0][keyIdx].KeyLen = 0;
1476
1477                 /* Check if the key is not marked as invalid */
1478                 if(!(erq->flags & IW_ENCODE_NOKEY)) {
1479                         /* Copy the key in the driver */
1480                         NdisMoveMemory(pAdapter->SharedKey[BSS0][keyIdx].Key, extra, erq->length);
1481         }
1482         }
1483     else
1484                         {
1485                 /* Do we want to just set the transmit key index ? */
1486                 int index = (erq->flags & IW_ENCODE_INDEX) - 1;
1487                 if ((index >= 0) && (index < 4))
1488         {
1489                         pAdapter->StaCfg.DefaultKeyId = index;
1490             }
1491         else
1492                         /* Don't complain if only change the mode */
1493                         if (!(erq->flags & IW_ENCODE_MODE)) {
1494                                 return -EINVAL;
1495                 }
1496         }
1497
1498 done:
1499     DBGPRINT(RT_DEBUG_TRACE ,("==>rt_ioctl_siwencode::erq->flags=%x\n",erq->flags));
1500         DBGPRINT(RT_DEBUG_TRACE ,("==>rt_ioctl_siwencode::AuthMode=%x\n",pAdapter->StaCfg.AuthMode));
1501         DBGPRINT(RT_DEBUG_TRACE ,("==>rt_ioctl_siwencode::DefaultKeyId=%x, KeyLen = %d\n",pAdapter->StaCfg.DefaultKeyId , pAdapter->SharedKey[BSS0][pAdapter->StaCfg.DefaultKeyId].KeyLen));
1502         DBGPRINT(RT_DEBUG_TRACE ,("==>rt_ioctl_siwencode::WepStatus=%x\n",pAdapter->StaCfg.WepStatus));
1503         return 0;
1504 }
1505
1506 int
1507 rt_ioctl_giwencode(struct net_device *dev,
1508                           struct iw_request_info *info,
1509                           struct iw_point *erq, char *key)
1510 {
1511         PRTMP_ADAPTER pAdapter = dev->ml_priv;
1512         int kid;
1513
1514         //check if the interface is down
1515         if(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE))
1516         {
1517                 DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
1518         return -ENETDOWN;
1519         }
1520
1521         kid = erq->flags & IW_ENCODE_INDEX;
1522         DBGPRINT(RT_DEBUG_TRACE, ("===>rt_ioctl_giwencode %d\n", erq->flags & IW_ENCODE_INDEX));
1523
1524         if (pAdapter->StaCfg.WepStatus == Ndis802_11WEPDisabled)
1525         {
1526                 erq->length = 0;
1527                 erq->flags = IW_ENCODE_DISABLED;
1528         }
1529         else if ((kid > 0) && (kid <=4))
1530         {
1531                 // copy wep key
1532                 erq->flags = kid ;                      /* NB: base 1 */
1533                 if (erq->length > pAdapter->SharedKey[BSS0][kid-1].KeyLen)
1534                         erq->length = pAdapter->SharedKey[BSS0][kid-1].KeyLen;
1535                 memcpy(key, pAdapter->SharedKey[BSS0][kid-1].Key, erq->length);
1536                 //if ((kid == pAdapter->PortCfg.DefaultKeyId))
1537                 //erq->flags |= IW_ENCODE_ENABLED;      /* XXX */
1538                 if (pAdapter->StaCfg.AuthMode == Ndis802_11AuthModeShared)
1539                         erq->flags |= IW_ENCODE_RESTRICTED;             /* XXX */
1540                 else
1541                         erq->flags |= IW_ENCODE_OPEN;           /* XXX */
1542
1543         }
1544         else if (kid == 0)
1545         {
1546                 if (pAdapter->StaCfg.AuthMode == Ndis802_11AuthModeShared)
1547                         erq->flags |= IW_ENCODE_RESTRICTED;             /* XXX */
1548                 else
1549                         erq->flags |= IW_ENCODE_OPEN;           /* XXX */
1550                 erq->length = pAdapter->SharedKey[BSS0][pAdapter->StaCfg.DefaultKeyId].KeyLen;
1551                 memcpy(key, pAdapter->SharedKey[BSS0][pAdapter->StaCfg.DefaultKeyId].Key, erq->length);
1552                 // copy default key ID
1553                 if (pAdapter->StaCfg.AuthMode == Ndis802_11AuthModeShared)
1554                         erq->flags |= IW_ENCODE_RESTRICTED;             /* XXX */
1555                 else
1556                         erq->flags |= IW_ENCODE_OPEN;           /* XXX */
1557                 erq->flags = pAdapter->StaCfg.DefaultKeyId + 1;                 /* NB: base 1 */
1558                 erq->flags |= IW_ENCODE_ENABLED;        /* XXX */
1559         }
1560
1561         return 0;
1562
1563 }
1564
1565 static int
1566 rt_ioctl_setparam(struct net_device *dev, struct iw_request_info *info,
1567                          void *w, char *extra)
1568 {
1569         PRTMP_ADAPTER pAdapter = dev->ml_priv;
1570         POS_COOKIE pObj = (POS_COOKIE)pAdapter->OS_Cookie;
1571         char *this_char = extra;
1572         char *value;
1573         int  Status=0;
1574
1575         {
1576                 pObj->ioctl_if_type = INT_MAIN;
1577         pObj->ioctl_if = MAIN_MBSSID;
1578         }
1579
1580         //check if the interface is down
1581         if(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE))
1582         {
1583                 DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
1584                         return -ENETDOWN;
1585         }
1586
1587         if (!*this_char)
1588                 return -EINVAL;
1589
1590         if ((value = rtstrchr(this_char, '=')) != NULL)
1591             *value++ = 0;
1592
1593         if (!value)
1594             return -EINVAL;
1595
1596         // reject setting nothing besides ANY ssid(ssidLen=0)
1597     if (!*value && (strcmp(this_char, "SSID") != 0))
1598         return -EINVAL;
1599
1600         for (PRTMP_PRIVATE_SET_PROC = RTMP_PRIVATE_SUPPORT_PROC; PRTMP_PRIVATE_SET_PROC->name; PRTMP_PRIVATE_SET_PROC++)
1601         {
1602             if (strcmp(this_char, PRTMP_PRIVATE_SET_PROC->name) == 0)
1603             {
1604                 if(!PRTMP_PRIVATE_SET_PROC->set_proc(pAdapter, value))
1605                 {       //FALSE:Set private failed then return Invalid argument
1606                             Status = -EINVAL;
1607                 }
1608                     break;      //Exit for loop.
1609             }
1610         }
1611
1612         if(PRTMP_PRIVATE_SET_PROC->name == NULL)
1613         {  //Not found argument
1614             Status = -EINVAL;
1615             DBGPRINT(RT_DEBUG_TRACE, ("===>rt_ioctl_setparam:: (iwpriv) Not Support Set Command [%s=%s]\n", this_char, value));
1616         }
1617
1618     return Status;
1619 }
1620
1621
1622 static int
1623 rt_private_get_statistics(struct net_device *dev, struct iw_request_info *info,
1624                 struct iw_point *wrq, char *extra)
1625 {
1626         INT                             Status = 0;
1627     PRTMP_ADAPTER   pAd = dev->ml_priv;
1628
1629     if (extra == NULL)
1630     {
1631         wrq->length = 0;
1632         return -EIO;
1633     }
1634
1635     memset(extra, 0x00, IW_PRIV_SIZE_MASK);
1636     sprintf(extra, "\n\n");
1637
1638         {
1639     sprintf(extra+strlen(extra), "Tx success                      = %ld\n", (ULONG)pAd->WlanCounters.TransmittedFragmentCount.QuadPart);
1640     sprintf(extra+strlen(extra), "Tx success without retry        = %ld\n", (ULONG)pAd->WlanCounters.TransmittedFragmentCount.QuadPart - (ULONG)pAd->WlanCounters.RetryCount.QuadPart);
1641         }
1642     sprintf(extra+strlen(extra), "Tx success after retry          = %ld\n", (ULONG)pAd->WlanCounters.RetryCount.QuadPart);
1643     sprintf(extra+strlen(extra), "Tx fail to Rcv ACK after retry  = %ld\n", (ULONG)pAd->WlanCounters.FailedCount.QuadPart);
1644     sprintf(extra+strlen(extra), "RTS Success Rcv CTS             = %ld\n", (ULONG)pAd->WlanCounters.RTSSuccessCount.QuadPart);
1645     sprintf(extra+strlen(extra), "RTS Fail Rcv CTS                = %ld\n", (ULONG)pAd->WlanCounters.RTSFailureCount.QuadPart);
1646
1647     sprintf(extra+strlen(extra), "Rx success                      = %ld\n", (ULONG)pAd->WlanCounters.ReceivedFragmentCount.QuadPart);
1648     sprintf(extra+strlen(extra), "Rx with CRC                     = %ld\n", (ULONG)pAd->WlanCounters.FCSErrorCount.QuadPart);
1649     sprintf(extra+strlen(extra), "Rx drop due to out of resource  = %ld\n", (ULONG)pAd->Counters8023.RxNoBuffer);
1650     sprintf(extra+strlen(extra), "Rx duplicate frame              = %ld\n", (ULONG)pAd->WlanCounters.FrameDuplicateCount.QuadPart);
1651
1652     sprintf(extra+strlen(extra), "False CCA (one second)          = %ld\n", (ULONG)pAd->RalinkCounters.OneSecFalseCCACnt);
1653         {
1654         sprintf(extra+strlen(extra), "RSSI-A                          = %ld\n", (LONG)(pAd->StaCfg.RssiSample.LastRssi0 - pAd->BbpRssiToDbmDelta));
1655         sprintf(extra+strlen(extra), "RSSI-B (if available)           = %ld\n", (LONG)(pAd->StaCfg.RssiSample.LastRssi1 - pAd->BbpRssiToDbmDelta));
1656         sprintf(extra+strlen(extra), "RSSI-C (if available)           = %ld\n\n", (LONG)(pAd->StaCfg.RssiSample.LastRssi2 - pAd->BbpRssiToDbmDelta));
1657         }
1658     sprintf(extra+strlen(extra), "WpaSupplicantUP                 = %d\n\n", pAd->StaCfg.WpaSupplicantUP);
1659
1660     wrq->length = strlen(extra) + 1; // 1: size of '\0'
1661     DBGPRINT(RT_DEBUG_TRACE, ("<== rt_private_get_statistics, wrq->length = %d\n", wrq->length));
1662
1663     return Status;
1664 }
1665
1666 void    getBaInfo(
1667         IN      PRTMP_ADAPTER   pAd,
1668         IN      PUCHAR                  pOutBuf)
1669 {
1670         INT i, j;
1671         BA_ORI_ENTRY *pOriBAEntry;
1672         BA_REC_ENTRY *pRecBAEntry;
1673
1674         for (i=0; i<MAX_LEN_OF_MAC_TABLE; i++)
1675         {
1676                 PMAC_TABLE_ENTRY pEntry = &pAd->MacTab.Content[i];
1677                 if (((pEntry->ValidAsCLI || pEntry->ValidAsApCli) && (pEntry->Sst == SST_ASSOC))
1678                         || (pEntry->ValidAsWDS) || (pEntry->ValidAsMesh))
1679                 {
1680                         sprintf(pOutBuf + strlen(pOutBuf), "\n%02X:%02X:%02X:%02X:%02X:%02X (Aid = %d) (AP) -\n",
1681                                 pEntry->Addr[0], pEntry->Addr[1], pEntry->Addr[2],
1682                                 pEntry->Addr[3], pEntry->Addr[4], pEntry->Addr[5], pEntry->Aid);
1683
1684                         sprintf(pOutBuf, "%s[Recipient]\n", pOutBuf);
1685                         for (j=0; j < NUM_OF_TID; j++)
1686                         {
1687                                 if (pEntry->BARecWcidArray[j] != 0)
1688                                 {
1689                                         pRecBAEntry =&pAd->BATable.BARecEntry[pEntry->BARecWcidArray[j]];
1690                                         sprintf(pOutBuf + strlen(pOutBuf), "TID=%d, BAWinSize=%d, LastIndSeq=%d, ReorderingPkts=%d\n", j, pRecBAEntry->BAWinSize, pRecBAEntry->LastIndSeq, pRecBAEntry->list.qlen);
1691                                 }
1692                         }
1693                         sprintf(pOutBuf, "%s\n", pOutBuf);
1694
1695                         sprintf(pOutBuf, "%s[Originator]\n", pOutBuf);
1696                         for (j=0; j < NUM_OF_TID; j++)
1697                         {
1698                                 if (pEntry->BAOriWcidArray[j] != 0)
1699                                 {
1700                                         pOriBAEntry =&pAd->BATable.BAOriEntry[pEntry->BAOriWcidArray[j]];
1701                                         sprintf(pOutBuf + strlen(pOutBuf), "TID=%d, BAWinSize=%d, StartSeq=%d, CurTxSeq=%d\n", j, pOriBAEntry->BAWinSize, pOriBAEntry->Sequence, pEntry->TxSeq[j]);
1702                                 }
1703                         }
1704                         sprintf(pOutBuf, "%s\n\n", pOutBuf);
1705                 }
1706         if (strlen(pOutBuf) > (IW_PRIV_SIZE_MASK - 30))
1707                 break;
1708         }
1709
1710         return;
1711 }
1712
1713 static int
1714 rt_private_show(struct net_device *dev, struct iw_request_info *info,
1715                 struct iw_point *wrq, char *extra)
1716 {
1717     INT                         Status = 0;
1718     PRTMP_ADAPTER pAd = dev->ml_priv;
1719     POS_COOKIE pObj = (POS_COOKIE) pAd->OS_Cookie;
1720     u32             subcmd = wrq->flags;
1721
1722     if (extra == NULL)
1723     {
1724         wrq->length = 0;
1725         return -EIO;
1726     }
1727     memset(extra, 0x00, IW_PRIV_SIZE_MASK);
1728
1729         {
1730                 pObj->ioctl_if_type = INT_MAIN;
1731         pObj->ioctl_if = MAIN_MBSSID;
1732         }
1733
1734     switch(subcmd)
1735     {
1736
1737         case SHOW_CONN_STATUS:
1738             if (MONITOR_ON(pAd))
1739             {
1740                 if (pAd->CommonCfg.PhyMode >= PHY_11ABGN_MIXED &&
1741                     pAd->CommonCfg.RegTransmitSetting.field.BW)
1742                     sprintf(extra, "Monitor Mode(CentralChannel %d)\n", pAd->CommonCfg.CentralChannel);
1743                 else
1744                     sprintf(extra, "Monitor Mode(Channel %d)\n", pAd->CommonCfg.Channel);
1745             }
1746             else
1747             {
1748                 if (pAd->IndicateMediaState == NdisMediaStateConnected)
1749                 {
1750                     if (INFRA_ON(pAd))
1751                     {
1752                     sprintf(extra, "Connected(AP: %s[%02X:%02X:%02X:%02X:%02X:%02X])\n",
1753                                     pAd->CommonCfg.Ssid,
1754                                     pAd->CommonCfg.Bssid[0],
1755                                     pAd->CommonCfg.Bssid[1],
1756                                     pAd->CommonCfg.Bssid[2],
1757                                     pAd->CommonCfg.Bssid[3],
1758                                     pAd->CommonCfg.Bssid[4],
1759                                     pAd->CommonCfg.Bssid[5]);
1760                         DBGPRINT(RT_DEBUG_TRACE ,("Ssid=%s ,Ssidlen = %d\n",pAd->CommonCfg.Ssid, pAd->CommonCfg.SsidLen));
1761                 }
1762                     else if (ADHOC_ON(pAd))
1763                         sprintf(extra, "Connected\n");
1764                 }
1765                 else
1766                 {
1767                     sprintf(extra, "Disconnected\n");
1768                         DBGPRINT(RT_DEBUG_TRACE ,("ConnStatus is not connected\n"));
1769                 }
1770             }
1771             wrq->length = strlen(extra) + 1; // 1: size of '\0'
1772             break;
1773         case SHOW_DRVIER_VERION:
1774             sprintf(extra, "Driver version-%s, %s %s\n", STA_DRIVER_VERSION, __DATE__, __TIME__ );
1775             wrq->length = strlen(extra) + 1; // 1: size of '\0'
1776             break;
1777         case SHOW_BA_INFO:
1778             getBaInfo(pAd, extra);
1779             wrq->length = strlen(extra) + 1; // 1: size of '\0'
1780             break;
1781                 case SHOW_DESC_INFO:
1782                         {
1783                                 Show_DescInfo_Proc(pAd, NULL);
1784                                 wrq->length = 0; // 1: size of '\0'
1785                         }
1786                         break;
1787         case RAIO_OFF:
1788             if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_BSS_SCAN_IN_PROGRESS))
1789             {
1790                 sprintf(extra, "Scanning\n");
1791                 wrq->length = strlen(extra) + 1; // 1: size of '\0'
1792                 break;
1793             }
1794             pAd->StaCfg.bSwRadio = FALSE;
1795             if (pAd->StaCfg.bRadio != (pAd->StaCfg.bHwRadio && pAd->StaCfg.bSwRadio))
1796             {
1797                 pAd->StaCfg.bRadio = (pAd->StaCfg.bHwRadio && pAd->StaCfg.bSwRadio);
1798                 if (pAd->StaCfg.bRadio == FALSE)
1799                 {
1800                     MlmeRadioOff(pAd);
1801                     // Update extra information
1802                                         pAd->ExtraInfo = SW_RADIO_OFF;
1803                 }
1804             }
1805             sprintf(extra, "Radio Off\n");
1806             wrq->length = strlen(extra) + 1; // 1: size of '\0'
1807             break;
1808         case RAIO_ON:
1809 #ifdef RT2870
1810             if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_BSS_SCAN_IN_PROGRESS))
1811             {
1812                 sprintf(extra, "Scanning\n");
1813                 wrq->length = strlen(extra) + 1; // 1: size of '\0'
1814                 break;
1815             }
1816 #endif
1817             pAd->StaCfg.bSwRadio = TRUE;
1818             //if (pAd->StaCfg.bRadio != (pAd->StaCfg.bHwRadio && pAd->StaCfg.bSwRadio))
1819             {
1820                 pAd->StaCfg.bRadio = (pAd->StaCfg.bHwRadio && pAd->StaCfg.bSwRadio);
1821                 if (pAd->StaCfg.bRadio == TRUE)
1822                 {
1823                     MlmeRadioOn(pAd);
1824                     // Update extra information
1825                                         pAd->ExtraInfo = EXTRA_INFO_CLEAR;
1826                 }
1827             }
1828             sprintf(extra, "Radio On\n");
1829             wrq->length = strlen(extra) + 1; // 1: size of '\0'
1830             break;
1831
1832                 case SHOW_CFG_VALUE:
1833                         {
1834                                 Status = RTMPShowCfgValue(pAd, wrq->pointer, extra);
1835                                 if (Status == 0)
1836                                         wrq->length = strlen(extra) + 1; // 1: size of '\0'
1837                         }
1838                         break;
1839         default:
1840             DBGPRINT(RT_DEBUG_TRACE, ("%s - unknow subcmd = %d\n", __func__, subcmd));
1841             break;
1842     }
1843
1844     return Status;
1845 }
1846
1847 int rt_ioctl_siwmlme(struct net_device *dev,
1848                            struct iw_request_info *info,
1849                            union iwreq_data *wrqu,
1850                            char *extra)
1851 {
1852         PRTMP_ADAPTER   pAd = dev->ml_priv;
1853         struct iw_mlme *pMlme = (struct iw_mlme *)wrqu->data.pointer;
1854         MLME_QUEUE_ELEM                         MsgElem;
1855         MLME_DISASSOC_REQ_STRUCT        DisAssocReq;
1856         MLME_DEAUTH_REQ_STRUCT      DeAuthReq;
1857
1858         DBGPRINT(RT_DEBUG_TRACE, ("====> %s\n", __func__));
1859
1860         if (pMlme == NULL)
1861                 return -EINVAL;
1862
1863         switch(pMlme->cmd)
1864         {
1865 #ifdef IW_MLME_DEAUTH
1866                 case IW_MLME_DEAUTH:
1867                         DBGPRINT(RT_DEBUG_TRACE, ("====> %s - IW_MLME_DEAUTH\n", __func__));
1868                         COPY_MAC_ADDR(DeAuthReq.Addr, pAd->CommonCfg.Bssid);
1869                         DeAuthReq.Reason = pMlme->reason_code;
1870                         MsgElem.MsgLen = sizeof(MLME_DEAUTH_REQ_STRUCT);
1871                         NdisMoveMemory(MsgElem.Msg, &DeAuthReq, sizeof(MLME_DEAUTH_REQ_STRUCT));
1872                         MlmeDeauthReqAction(pAd, &MsgElem);
1873                         if (INFRA_ON(pAd))
1874                         {
1875                             LinkDown(pAd, FALSE);
1876                             pAd->Mlme.AssocMachine.CurrState = ASSOC_IDLE;
1877                         }
1878                         break;
1879 #endif // IW_MLME_DEAUTH //
1880 #ifdef IW_MLME_DISASSOC
1881                 case IW_MLME_DISASSOC:
1882                         DBGPRINT(RT_DEBUG_TRACE, ("====> %s - IW_MLME_DISASSOC\n", __func__));
1883                         COPY_MAC_ADDR(DisAssocReq.Addr, pAd->CommonCfg.Bssid);
1884                         DisAssocReq.Reason =  pMlme->reason_code;
1885
1886                         MsgElem.Machine = ASSOC_STATE_MACHINE;
1887                         MsgElem.MsgType = MT2_MLME_DISASSOC_REQ;
1888                         MsgElem.MsgLen = sizeof(MLME_DISASSOC_REQ_STRUCT);
1889                         NdisMoveMemory(MsgElem.Msg, &DisAssocReq, sizeof(MLME_DISASSOC_REQ_STRUCT));
1890
1891                         pAd->Mlme.CntlMachine.CurrState = CNTL_WAIT_OID_DISASSOC;
1892                         MlmeDisassocReqAction(pAd, &MsgElem);
1893                         break;
1894 #endif // IW_MLME_DISASSOC //
1895                 default:
1896                         DBGPRINT(RT_DEBUG_TRACE, ("====> %s - Unknow Command\n", __func__));
1897                         break;
1898         }
1899
1900         return 0;
1901 }
1902
1903 int rt_ioctl_siwauth(struct net_device *dev,
1904                           struct iw_request_info *info,
1905                           union iwreq_data *wrqu, char *extra)
1906 {
1907         PRTMP_ADAPTER   pAdapter = dev->ml_priv;
1908         struct iw_param *param = &wrqu->param;
1909
1910     //check if the interface is down
1911         if(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE))
1912         {
1913                 DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
1914         return -ENETDOWN;
1915         }
1916         switch (param->flags & IW_AUTH_INDEX) {
1917         case IW_AUTH_WPA_VERSION:
1918             if (param->value == IW_AUTH_WPA_VERSION_WPA)
1919             {
1920                 pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeWPAPSK;
1921                                 if (pAdapter->StaCfg.BssType == BSS_ADHOC)
1922                                         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeWPANone;
1923             }
1924             else if (param->value == IW_AUTH_WPA_VERSION_WPA2)
1925                 pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeWPA2PSK;
1926
1927             DBGPRINT(RT_DEBUG_TRACE, ("%s::IW_AUTH_WPA_VERSION - param->value = %d!\n", __func__, param->value));
1928             break;
1929         case IW_AUTH_CIPHER_PAIRWISE:
1930             if (param->value == IW_AUTH_CIPHER_NONE)
1931             {
1932                 pAdapter->StaCfg.WepStatus = Ndis802_11WEPDisabled;
1933                 pAdapter->StaCfg.OrigWepStatus = pAdapter->StaCfg.WepStatus;
1934                 pAdapter->StaCfg.PairCipher = Ndis802_11WEPDisabled;
1935             }
1936             else if (param->value == IW_AUTH_CIPHER_WEP40 ||
1937                      param->value == IW_AUTH_CIPHER_WEP104)
1938             {
1939                 pAdapter->StaCfg.WepStatus = Ndis802_11WEPEnabled;
1940                 pAdapter->StaCfg.OrigWepStatus = pAdapter->StaCfg.WepStatus;
1941                 pAdapter->StaCfg.PairCipher = Ndis802_11WEPEnabled;
1942                 pAdapter->StaCfg.IEEE8021X = FALSE;
1943             }
1944             else if (param->value == IW_AUTH_CIPHER_TKIP)
1945             {
1946                 pAdapter->StaCfg.WepStatus = Ndis802_11Encryption2Enabled;
1947                 pAdapter->StaCfg.OrigWepStatus = pAdapter->StaCfg.WepStatus;
1948                 pAdapter->StaCfg.PairCipher = Ndis802_11Encryption2Enabled;
1949             }
1950             else if (param->value == IW_AUTH_CIPHER_CCMP)
1951             {
1952                 pAdapter->StaCfg.WepStatus = Ndis802_11Encryption3Enabled;
1953                 pAdapter->StaCfg.OrigWepStatus = pAdapter->StaCfg.WepStatus;
1954                 pAdapter->StaCfg.PairCipher = Ndis802_11Encryption3Enabled;
1955             }
1956             DBGPRINT(RT_DEBUG_TRACE, ("%s::IW_AUTH_CIPHER_PAIRWISE - param->value = %d!\n", __func__, param->value));
1957             break;
1958         case IW_AUTH_CIPHER_GROUP:
1959             if (param->value == IW_AUTH_CIPHER_NONE)
1960             {
1961                 pAdapter->StaCfg.GroupCipher = Ndis802_11WEPDisabled;
1962             }
1963             else if (param->value == IW_AUTH_CIPHER_WEP40 ||
1964                      param->value == IW_AUTH_CIPHER_WEP104)
1965             {
1966                 pAdapter->StaCfg.GroupCipher = Ndis802_11WEPEnabled;
1967             }
1968             else if (param->value == IW_AUTH_CIPHER_TKIP)
1969             {
1970                 pAdapter->StaCfg.GroupCipher = Ndis802_11Encryption2Enabled;
1971             }
1972             else if (param->value == IW_AUTH_CIPHER_CCMP)
1973             {
1974                 pAdapter->StaCfg.GroupCipher = Ndis802_11Encryption3Enabled;
1975             }
1976             DBGPRINT(RT_DEBUG_TRACE, ("%s::IW_AUTH_CIPHER_GROUP - param->value = %d!\n", __func__, param->value));
1977             break;
1978         case IW_AUTH_KEY_MGMT:
1979             if (param->value == IW_AUTH_KEY_MGMT_802_1X)
1980             {
1981                 if (pAdapter->StaCfg.AuthMode == Ndis802_11AuthModeWPAPSK)
1982                 {
1983                     pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeWPA;
1984                     pAdapter->StaCfg.IEEE8021X = FALSE;
1985                 }
1986                 else if (pAdapter->StaCfg.AuthMode == Ndis802_11AuthModeWPA2PSK)
1987                 {
1988                     pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeWPA2;
1989                     pAdapter->StaCfg.IEEE8021X = FALSE;
1990                 }
1991                 else
1992                     // WEP 1x
1993                     pAdapter->StaCfg.IEEE8021X = TRUE;
1994             }
1995             else if (param->value == 0)
1996             {
1997                                 STA_PORT_SECURED(pAdapter);
1998             }
1999             DBGPRINT(RT_DEBUG_TRACE, ("%s::IW_AUTH_KEY_MGMT - param->value = %d!\n", __func__, param->value));
2000             break;
2001         case IW_AUTH_RX_UNENCRYPTED_EAPOL:
2002             break;
2003         case IW_AUTH_PRIVACY_INVOKED:
2004             DBGPRINT(RT_DEBUG_TRACE, ("%s::IW_AUTH_PRIVACY_INVOKED - param->value = %d!\n", __func__, param->value));
2005                 break;
2006         case IW_AUTH_DROP_UNENCRYPTED:
2007             if (param->value != 0)
2008                 pAdapter->StaCfg.PortSecured = WPA_802_1X_PORT_NOT_SECURED;
2009                         else
2010                         {
2011                                 STA_PORT_SECURED(pAdapter);
2012                         }
2013             DBGPRINT(RT_DEBUG_TRACE, ("%s::IW_AUTH_WPA_VERSION - param->value = %d!\n", __func__, param->value));
2014                 break;
2015         case IW_AUTH_80211_AUTH_ALG:
2016                         if (param->value & IW_AUTH_ALG_SHARED_KEY)
2017             {
2018                                 pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeShared;
2019                         }
2020             else if (param->value & IW_AUTH_ALG_OPEN_SYSTEM)
2021             {
2022                                 pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeOpen;
2023                         }
2024             else
2025                                 return -EINVAL;
2026             DBGPRINT(RT_DEBUG_TRACE, ("%s::IW_AUTH_80211_AUTH_ALG - param->value = %d!\n", __func__, param->value));
2027                         break;
2028         case IW_AUTH_WPA_ENABLED:
2029                 DBGPRINT(RT_DEBUG_TRACE, ("%s::IW_AUTH_WPA_ENABLED - Driver supports WPA!(param->value = %d)\n", __func__, param->value));
2030                 break;
2031         default:
2032                 return -EOPNOTSUPP;
2033 }
2034
2035         return 0;
2036 }
2037
2038 int rt_ioctl_giwauth(struct net_device *dev,
2039                                struct iw_request_info *info,
2040                                union iwreq_data *wrqu, char *extra)
2041 {
2042         PRTMP_ADAPTER   pAdapter = dev->ml_priv;
2043         struct iw_param *param = &wrqu->param;
2044
2045     //check if the interface is down
2046         if(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE))
2047     {
2048                 DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
2049         return -ENETDOWN;
2050     }
2051
2052         switch (param->flags & IW_AUTH_INDEX) {
2053         case IW_AUTH_DROP_UNENCRYPTED:
2054         param->value = (pAdapter->StaCfg.WepStatus == Ndis802_11WEPDisabled) ? 0 : 1;
2055                 break;
2056
2057         case IW_AUTH_80211_AUTH_ALG:
2058         param->value = (pAdapter->StaCfg.AuthMode == Ndis802_11AuthModeShared) ? IW_AUTH_ALG_SHARED_KEY : IW_AUTH_ALG_OPEN_SYSTEM;
2059                 break;
2060
2061         case IW_AUTH_WPA_ENABLED:
2062                 param->value = (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA) ? 1 : 0;
2063                 break;
2064
2065         default:
2066                 return -EOPNOTSUPP;
2067         }
2068     DBGPRINT(RT_DEBUG_TRACE, ("rt_ioctl_giwauth::param->value = %d!\n", param->value));
2069         return 0;
2070 }
2071
2072 void fnSetCipherKey(
2073     IN  PRTMP_ADAPTER   pAdapter,
2074     IN  INT             keyIdx,
2075     IN  UCHAR           CipherAlg,
2076     IN  BOOLEAN         bGTK,
2077     IN  struct iw_encode_ext *ext)
2078 {
2079 #ifdef RT2860
2080         RTMP_CLEAR_PSFLAG(pAdapter, fRTMP_PS_CAN_GO_SLEEP);
2081         if (RTMP_TEST_PSFLAG(pAdapter, fRTMP_PS_SET_PCI_CLK_OFF_COMMAND))
2082         {
2083                 if (pAdapter->StaCfg.bRadio == FALSE)
2084                 {
2085                         RTMP_SET_PSFLAG(pAdapter, fRTMP_PS_CAN_GO_SLEEP);
2086                         return;
2087                 }
2088                 DBGPRINT(RT_DEBUG_TRACE,("RTMPWPAAddKeyProc1==>\n"));
2089                 RTMPPCIeLinkCtrlValueRestore(pAdapter, RESTORE_HALT);
2090                 RTMPusecDelay(6000);
2091                 pAdapter->bPCIclkOff = FALSE;
2092         }
2093 #endif
2094     NdisZeroMemory(&pAdapter->SharedKey[BSS0][keyIdx], sizeof(CIPHER_KEY));
2095     pAdapter->SharedKey[BSS0][keyIdx].KeyLen = LEN_TKIP_EK;
2096     NdisMoveMemory(pAdapter->SharedKey[BSS0][keyIdx].Key, ext->key, LEN_TKIP_EK);
2097     NdisMoveMemory(pAdapter->SharedKey[BSS0][keyIdx].TxMic, ext->key + LEN_TKIP_EK, LEN_TKIP_TXMICK);
2098     NdisMoveMemory(pAdapter->SharedKey[BSS0][keyIdx].RxMic, ext->key + LEN_TKIP_EK + LEN_TKIP_TXMICK, LEN_TKIP_RXMICK);
2099     pAdapter->SharedKey[BSS0][keyIdx].CipherAlg = CipherAlg;
2100
2101     // Update group key information to ASIC Shared Key Table
2102         AsicAddSharedKeyEntry(pAdapter,
2103                                                   BSS0,
2104                                                   keyIdx,
2105                                                   pAdapter->SharedKey[BSS0][keyIdx].CipherAlg,
2106                                                   pAdapter->SharedKey[BSS0][keyIdx].Key,
2107                                                   pAdapter->SharedKey[BSS0][keyIdx].TxMic,
2108                                                   pAdapter->SharedKey[BSS0][keyIdx].RxMic);
2109
2110     if (bGTK)
2111         // Update ASIC WCID attribute table and IVEIV table
2112         RTMPAddWcidAttributeEntry(pAdapter,
2113                                                           BSS0,
2114                                                           keyIdx,
2115                                                           pAdapter->SharedKey[BSS0][keyIdx].CipherAlg,
2116                                                           NULL);
2117     else
2118         // Update ASIC WCID attribute table and IVEIV table
2119         RTMPAddWcidAttributeEntry(pAdapter,
2120                                                           BSS0,
2121                                                           keyIdx,
2122                                                           pAdapter->SharedKey[BSS0][keyIdx].CipherAlg,
2123                                                           &pAdapter->MacTab.Content[BSSID_WCID]);
2124 #ifdef RT2860
2125         RTMP_SET_PSFLAG(pAdapter, fRTMP_PS_CAN_GO_SLEEP);
2126 #endif
2127 }
2128
2129 int rt_ioctl_siwencodeext(struct net_device *dev,
2130                            struct iw_request_info *info,
2131                            union iwreq_data *wrqu,
2132                            char *extra)
2133                         {
2134     PRTMP_ADAPTER   pAdapter = dev->ml_priv;
2135         struct iw_point *encoding = &wrqu->encoding;
2136         struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
2137     int keyIdx, alg = ext->alg;
2138
2139     //check if the interface is down
2140         if(!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE))
2141         {
2142                 DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
2143         return -ENETDOWN;
2144         }
2145
2146     if (encoding->flags & IW_ENCODE_DISABLED)
2147         {
2148         keyIdx = (encoding->flags & IW_ENCODE_INDEX) - 1;
2149         // set BSSID wcid entry of the Pair-wise Key table as no-security mode
2150             AsicRemovePairwiseKeyEntry(pAdapter, BSS0, BSSID_WCID);
2151         pAdapter->SharedKey[BSS0][keyIdx].KeyLen = 0;
2152                 pAdapter->SharedKey[BSS0][keyIdx].CipherAlg = CIPHER_NONE;
2153                 AsicRemoveSharedKeyEntry(pAdapter, 0, (UCHAR)keyIdx);
2154         NdisZeroMemory(&pAdapter->SharedKey[BSS0][keyIdx], sizeof(CIPHER_KEY));
2155         DBGPRINT(RT_DEBUG_TRACE, ("%s::Remove all keys!(encoding->flags = %x)\n", __func__, encoding->flags));
2156     }
2157                                         else
2158     {
2159         // Get Key Index and convet to our own defined key index
2160         keyIdx = (encoding->flags & IW_ENCODE_INDEX) - 1;
2161         if((keyIdx < 0) || (keyIdx >= NR_WEP_KEYS))
2162                 return -EINVAL;
2163
2164         if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY)
2165         {
2166             pAdapter->StaCfg.DefaultKeyId = keyIdx;
2167             DBGPRINT(RT_DEBUG_TRACE, ("%s::DefaultKeyId = %d\n", __func__, pAdapter->StaCfg.DefaultKeyId));
2168         }
2169
2170         switch (alg) {
2171                 case IW_ENCODE_ALG_NONE:
2172                 DBGPRINT(RT_DEBUG_TRACE, ("%s::IW_ENCODE_ALG_NONE\n", __func__));
2173                         break;
2174                 case IW_ENCODE_ALG_WEP:
2175                 DBGPRINT(RT_DEBUG_TRACE, ("%s::IW_ENCODE_ALG_WEP - ext->key_len = %d, keyIdx = %d\n", __func__, ext->key_len, keyIdx));
2176                         if (ext->key_len == MAX_WEP_KEY_SIZE)
2177                 {
2178                                 pAdapter->SharedKey[BSS0][keyIdx].KeyLen = MAX_WEP_KEY_SIZE;
2179                     pAdapter->SharedKey[BSS0][keyIdx].CipherAlg = CIPHER_WEP128;
2180                                 }
2181                         else if (ext->key_len == MIN_WEP_KEY_SIZE)
2182                 {
2183                     pAdapter->SharedKey[BSS0][keyIdx].KeyLen = MIN_WEP_KEY_SIZE;
2184                     pAdapter->SharedKey[BSS0][keyIdx].CipherAlg = CIPHER_WEP64;
2185                         }
2186                         else
2187                     return -EINVAL;
2188
2189                 NdisZeroMemory(pAdapter->SharedKey[BSS0][keyIdx].Key,  16);
2190                             NdisMoveMemory(pAdapter->SharedKey[BSS0][keyIdx].Key, ext->key, ext->key_len);
2191                                 if (pAdapter->StaCfg.GroupCipher == Ndis802_11GroupWEP40Enabled ||
2192                                         pAdapter->StaCfg.GroupCipher == Ndis802_11GroupWEP104Enabled)
2193                                 {
2194                                         // Set Group key material to Asic
2195                                         AsicAddSharedKeyEntry(pAdapter, BSS0, keyIdx, pAdapter->SharedKey[BSS0][keyIdx].CipherAlg, pAdapter->SharedKey[BSS0][keyIdx].Key, NULL, NULL);
2196
2197                                         // Update WCID attribute table and IVEIV table for this group key table
2198                                         RTMPAddWcidAttributeEntry(pAdapter, BSS0, keyIdx, pAdapter->SharedKey[BSS0][keyIdx].CipherAlg, NULL);
2199
2200                                         STA_PORT_SECURED(pAdapter);
2201
2202                                 // Indicate Connected for GUI
2203                                 pAdapter->IndicateMediaState = NdisMediaStateConnected;
2204                                 }
2205                         break;
2206             case IW_ENCODE_ALG_TKIP:
2207                 DBGPRINT(RT_DEBUG_TRACE, ("%s::IW_ENCODE_ALG_TKIP - keyIdx = %d, ext->key_len = %d\n", __func__, keyIdx, ext->key_len));
2208                 if (ext->key_len == 32)
2209                 {
2210                     if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY)
2211                     {
2212                         fnSetCipherKey(pAdapter, keyIdx, CIPHER_TKIP, FALSE, ext);
2213                         if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA2)
2214                         {
2215                             STA_PORT_SECURED(pAdapter);
2216                         }
2217                 }
2218                     else if (ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY)
2219                     {
2220                         fnSetCipherKey(pAdapter, keyIdx, CIPHER_TKIP, TRUE, ext);
2221
2222                         // set 802.1x port control
2223                         STA_PORT_SECURED(pAdapter);
2224                     }
2225                 }
2226                 else
2227                     return -EINVAL;
2228                 break;
2229             case IW_ENCODE_ALG_CCMP:
2230                 if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY)
2231                 {
2232                     fnSetCipherKey(pAdapter, keyIdx, CIPHER_AES, FALSE, ext);
2233                     if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA2)
2234                         STA_PORT_SECURED(pAdapter);
2235                 }
2236                 else if (ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY)
2237                 {
2238                     fnSetCipherKey(pAdapter, keyIdx, CIPHER_AES, TRUE, ext);
2239
2240                     // set 802.1x port control
2241                         STA_PORT_SECURED(pAdapter);
2242                 }
2243                 break;
2244                 default:
2245                         return -EINVAL;
2246                 }
2247     }
2248
2249     return 0;
2250 }
2251
2252 int
2253 rt_ioctl_giwencodeext(struct net_device *dev,
2254                           struct iw_request_info *info,
2255                           union iwreq_data *wrqu, char *extra)
2256 {
2257         PRTMP_ADAPTER pAd = dev->ml_priv;
2258         PCHAR pKey = NULL;
2259         struct iw_point *encoding = &wrqu->encoding;
2260         struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
2261         int idx, max_key_len;
2262
2263         DBGPRINT(RT_DEBUG_TRACE ,("===> rt_ioctl_giwencodeext\n"));
2264
2265         max_key_len = encoding->length - sizeof(*ext);
2266         if (max_key_len < 0)
2267                 return -EINVAL;
2268
2269         idx = encoding->flags & IW_ENCODE_INDEX;
2270         if (idx)
2271         {
2272                 if (idx < 1 || idx > 4)
2273                         return -EINVAL;
2274                 idx--;
2275
2276                 if ((pAd->StaCfg.WepStatus == Ndis802_11Encryption2Enabled) ||
2277                         (pAd->StaCfg.WepStatus == Ndis802_11Encryption3Enabled))
2278                 {
2279                         if (idx != pAd->StaCfg.DefaultKeyId)
2280                         {
2281                                 ext->key_len = 0;
2282                                 return 0;
2283                         }
2284                 }
2285         }
2286         else
2287                 idx = pAd->StaCfg.DefaultKeyId;
2288
2289         encoding->flags = idx + 1;
2290         memset(ext, 0, sizeof(*ext));
2291
2292         ext->key_len = 0;
2293         switch(pAd->StaCfg.WepStatus) {
2294                 case Ndis802_11WEPDisabled:
2295                         ext->alg = IW_ENCODE_ALG_NONE;
2296                         encoding->flags |= IW_ENCODE_DISABLED;
2297                         break;
2298                 case Ndis802_11WEPEnabled:
2299                         ext->alg = IW_ENCODE_ALG_WEP;
2300                         if (pAd->SharedKey[BSS0][idx].KeyLen > max_key_len)
2301                                 return -E2BIG;
2302                         else
2303                         {
2304                                 ext->key_len = pAd->SharedKey[BSS0][idx].KeyLen;
2305                                 pKey = &(pAd->SharedKey[BSS0][idx].Key[0]);
2306                         }
2307                         break;
2308                 case Ndis802_11Encryption2Enabled:
2309                 case Ndis802_11Encryption3Enabled:
2310                         if (pAd->StaCfg.WepStatus == Ndis802_11Encryption2Enabled)
2311                                 ext->alg = IW_ENCODE_ALG_TKIP;
2312                         else
2313                                 ext->alg = IW_ENCODE_ALG_CCMP;
2314
2315                         if (max_key_len < 32)
2316                                 return -E2BIG;
2317                         else
2318                         {
2319                                 ext->key_len = 32;
2320                                 pKey = &pAd->StaCfg.PMK[0];
2321                         }
2322                         break;
2323                 default:
2324                         return -EINVAL;
2325         }
2326
2327         if (ext->key_len && pKey)
2328         {
2329                 encoding->flags |= IW_ENCODE_ENABLED;
2330                 memcpy(ext->key, pKey, ext->key_len);
2331         }
2332
2333         return 0;
2334 }
2335
2336 int rt_ioctl_siwgenie(struct net_device *dev,
2337                           struct iw_request_info *info,
2338                           union iwreq_data *wrqu, char *extra)
2339 {
2340         PRTMP_ADAPTER   pAd = dev->ml_priv;
2341
2342         if (wrqu->data.length > MAX_LEN_OF_RSNIE ||
2343             (wrqu->data.length && extra == NULL))
2344                 return -EINVAL;
2345
2346         if (wrqu->data.length)
2347         {
2348                 pAd->StaCfg.RSNIE_Len = wrqu->data.length;
2349                 NdisMoveMemory(&pAd->StaCfg.RSN_IE[0], extra, pAd->StaCfg.RSNIE_Len);
2350         }
2351         else
2352         {
2353                 pAd->StaCfg.RSNIE_Len = 0;
2354                 NdisZeroMemory(&pAd->StaCfg.RSN_IE[0], MAX_LEN_OF_RSNIE);
2355         }
2356
2357         return 0;
2358 }
2359
2360 int rt_ioctl_giwgenie(struct net_device *dev,
2361                                struct iw_request_info *info,
2362                                union iwreq_data *wrqu, char *extra)
2363 {
2364         PRTMP_ADAPTER   pAd = dev->ml_priv;
2365
2366         if ((pAd->StaCfg.RSNIE_Len == 0) ||
2367                 (pAd->StaCfg.AuthMode < Ndis802_11AuthModeWPA))
2368         {
2369                 wrqu->data.length = 0;
2370                 return 0;
2371         }
2372
2373         if (pAd->StaCfg.WpaSupplicantUP == WPA_SUPPLICANT_ENABLE)
2374         {
2375         if (wrqu->data.length < pAd->StaCfg.RSNIE_Len)
2376                 return -E2BIG;
2377
2378         wrqu->data.length = pAd->StaCfg.RSNIE_Len;
2379         memcpy(extra, &pAd->StaCfg.RSN_IE[0], pAd->StaCfg.RSNIE_Len);
2380         }
2381         else
2382         {
2383                 UCHAR RSNIe = IE_WPA;
2384
2385                 if (wrqu->data.length < (pAd->StaCfg.RSNIE_Len + 2)) // ID, Len
2386                         return -E2BIG;
2387                 wrqu->data.length = pAd->StaCfg.RSNIE_Len + 2;
2388
2389                 if ((pAd->StaCfg.AuthMode == Ndis802_11AuthModeWPA2PSK) ||
2390             (pAd->StaCfg.AuthMode == Ndis802_11AuthModeWPA2))
2391                         RSNIe = IE_RSN;
2392
2393                 extra[0] = (char)RSNIe;
2394                 extra[1] = pAd->StaCfg.RSNIE_Len;
2395                 memcpy(extra+2, &pAd->StaCfg.RSN_IE[0], pAd->StaCfg.RSNIE_Len);
2396         }
2397
2398         return 0;
2399 }
2400
2401 int rt_ioctl_siwpmksa(struct net_device *dev,
2402                            struct iw_request_info *info,
2403                            union iwreq_data *wrqu,
2404                            char *extra)
2405 {
2406         PRTMP_ADAPTER   pAd = dev->ml_priv;
2407         struct iw_pmksa *pPmksa = (struct iw_pmksa *)wrqu->data.pointer;
2408         INT     CachedIdx = 0, idx = 0;
2409
2410         if (pPmksa == NULL)
2411                 return -EINVAL;
2412
2413         DBGPRINT(RT_DEBUG_TRACE ,("===> rt_ioctl_siwpmksa\n"));
2414         switch(pPmksa->cmd)
2415         {
2416                 case IW_PMKSA_FLUSH:
2417                         NdisZeroMemory(pAd->StaCfg.SavedPMK, sizeof(BSSID_INFO)*PMKID_NO);
2418                         DBGPRINT(RT_DEBUG_TRACE ,("rt_ioctl_siwpmksa - IW_PMKSA_FLUSH\n"));
2419                         break;
2420                 case IW_PMKSA_REMOVE:
2421                         for (CachedIdx = 0; CachedIdx < pAd->StaCfg.SavedPMKNum; CachedIdx++)
2422                         {
2423                         // compare the BSSID
2424                         if (NdisEqualMemory(pPmksa->bssid.sa_data, pAd->StaCfg.SavedPMK[CachedIdx].BSSID, MAC_ADDR_LEN))
2425                         {
2426                                 NdisZeroMemory(pAd->StaCfg.SavedPMK[CachedIdx].BSSID, MAC_ADDR_LEN);
2427                                         NdisZeroMemory(pAd->StaCfg.SavedPMK[CachedIdx].PMKID, 16);
2428                                         for (idx = CachedIdx; idx < (pAd->StaCfg.SavedPMKNum - 1); idx++)
2429                                         {
2430                                                 NdisMoveMemory(&pAd->StaCfg.SavedPMK[idx].BSSID[0], &pAd->StaCfg.SavedPMK[idx+1].BSSID[0], MAC_ADDR_LEN);
2431                                                 NdisMoveMemory(&pAd->StaCfg.SavedPMK[idx].PMKID[0], &pAd->StaCfg.SavedPMK[idx+1].PMKID[0], 16);
2432                                         }
2433                                         pAd->StaCfg.SavedPMKNum--;
2434                                 break;
2435                         }
2436                 }
2437
2438                         DBGPRINT(RT_DEBUG_TRACE ,("rt_ioctl_siwpmksa - IW_PMKSA_REMOVE\n"));
2439                         break;
2440                 case IW_PMKSA_ADD:
2441                         for (CachedIdx = 0; CachedIdx < pAd->StaCfg.SavedPMKNum; CachedIdx++)
2442                         {
2443                         // compare the BSSID
2444                         if (NdisEqualMemory(pPmksa->bssid.sa_data, pAd->StaCfg.SavedPMK[CachedIdx].BSSID, MAC_ADDR_LEN))
2445                                 break;
2446                 }
2447
2448                 // Found, replace it
2449                 if (CachedIdx < PMKID_NO)
2450                 {
2451                         DBGPRINT(RT_DEBUG_OFF, ("Update PMKID, idx = %d\n", CachedIdx));
2452                         NdisMoveMemory(&pAd->StaCfg.SavedPMK[CachedIdx].BSSID[0], pPmksa->bssid.sa_data, MAC_ADDR_LEN);
2453                                 NdisMoveMemory(&pAd->StaCfg.SavedPMK[CachedIdx].PMKID[0], pPmksa->pmkid, 16);
2454                         pAd->StaCfg.SavedPMKNum++;
2455                 }
2456                 // Not found, replace the last one
2457                 else
2458                 {
2459                         // Randomly replace one
2460                         CachedIdx = (pPmksa->bssid.sa_data[5] % PMKID_NO);
2461                         DBGPRINT(RT_DEBUG_OFF, ("Update PMKID, idx = %d\n", CachedIdx));
2462                         NdisMoveMemory(&pAd->StaCfg.SavedPMK[CachedIdx].BSSID[0], pPmksa->bssid.sa_data, MAC_ADDR_LEN);
2463                                 NdisMoveMemory(&pAd->StaCfg.SavedPMK[CachedIdx].PMKID[0], pPmksa->pmkid, 16);
2464                 }
2465
2466                         DBGPRINT(RT_DEBUG_TRACE ,("rt_ioctl_siwpmksa - IW_PMKSA_ADD\n"));
2467                         break;
2468                 default:
2469                         DBGPRINT(RT_DEBUG_TRACE ,("rt_ioctl_siwpmksa - Unknow Command!!\n"));
2470                         break;
2471         }
2472
2473         return 0;
2474 }
2475
2476 int rt_ioctl_siwrate(struct net_device *dev,
2477                         struct iw_request_info *info,
2478                         union iwreq_data *wrqu, char *extra)
2479 {
2480     PRTMP_ADAPTER   pAd = dev->ml_priv;
2481     UINT32          rate = wrqu->bitrate.value, fixed = wrqu->bitrate.fixed;
2482
2483     //check if the interface is down
2484         if(!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_INTERRUPT_IN_USE))
2485         {
2486                 DBGPRINT(RT_DEBUG_TRACE, ("rt_ioctl_siwrate::Network is down!\n"));
2487         return -ENETDOWN;
2488         }
2489
2490     DBGPRINT(RT_DEBUG_TRACE, ("rt_ioctl_siwrate::(rate = %d, fixed = %d)\n", rate, fixed));
2491     /* rate = -1 => auto rate
2492        rate = X, fixed = 1 => (fixed rate X)
2493     */
2494     if (rate == -1)
2495     {
2496                 //Auto Rate
2497                 pAd->StaCfg.DesiredTransmitSetting.field.MCS = MCS_AUTO;
2498                 pAd->StaCfg.bAutoTxRateSwitch = TRUE;
2499                 if ((pAd->CommonCfg.PhyMode <= PHY_11G) ||
2500                     (pAd->MacTab.Content[BSSID_WCID].HTPhyMode.field.MODE <= MODE_OFDM))
2501                         RTMPSetDesiredRates(pAd, -1);
2502
2503                 SetCommonHT(pAd);
2504     }
2505     else
2506     {
2507         if (fixed)
2508         {
2509                 pAd->StaCfg.bAutoTxRateSwitch = FALSE;
2510             if ((pAd->CommonCfg.PhyMode <= PHY_11G) ||
2511                 (pAd->MacTab.Content[BSSID_WCID].HTPhyMode.field.MODE <= MODE_OFDM))
2512                 RTMPSetDesiredRates(pAd, rate);
2513             else
2514             {
2515                 pAd->StaCfg.DesiredTransmitSetting.field.MCS = MCS_AUTO;
2516                 SetCommonHT(pAd);
2517             }
2518             DBGPRINT(RT_DEBUG_TRACE, ("rt_ioctl_siwrate::(HtMcs=%d)\n",pAd->StaCfg.DesiredTransmitSetting.field.MCS));
2519         }
2520         else
2521         {
2522             // TODO: rate = X, fixed = 0 => (rates <= X)
2523             return -EOPNOTSUPP;
2524         }
2525     }
2526
2527     return 0;
2528 }
2529
2530 int rt_ioctl_giwrate(struct net_device *dev,
2531                                struct iw_request_info *info,
2532                                union iwreq_data *wrqu, char *extra)
2533 {
2534     PRTMP_ADAPTER   pAd = dev->ml_priv;
2535     int rate_index = 0, rate_count = 0;
2536     HTTRANSMIT_SETTING ht_setting;
2537     __s32 ralinkrate[] =
2538         {2,  4,   11,  22, // CCK
2539         12, 18,   24,  36, 48, 72, 96, 108, // OFDM
2540         13, 26,   39,  52,  78, 104, 117, 130, 26,  52,  78, 104, 156, 208, 234, 260, // 20MHz, 800ns GI, MCS: 0 ~ 15
2541         39, 78,  117, 156, 234, 312, 351, 390,                                                                            // 20MHz, 800ns GI, MCS: 16 ~ 23
2542         27, 54,   81, 108, 162, 216, 243, 270, 54, 108, 162, 216, 324, 432, 486, 540, // 40MHz, 800ns GI, MCS: 0 ~ 15
2543         81, 162, 243, 324, 486, 648, 729, 810,                                                                            // 40MHz, 800ns GI, MCS: 16 ~ 23
2544         14, 29,   43,  57,  87, 115, 130, 144, 29, 59,   87, 115, 173, 230, 260, 288, // 20MHz, 400ns GI, MCS: 0 ~ 15
2545         43, 87,  130, 173, 260, 317, 390, 433,                                                                            // 20MHz, 400ns GI, MCS: 16 ~ 23
2546         30, 60,   90, 120, 180, 240, 270, 300, 60, 120, 180, 240, 360, 480, 540, 600, // 40MHz, 400ns GI, MCS: 0 ~ 15
2547         90, 180, 270, 360, 540, 720, 810, 900};                                                                           // 40MHz, 400ns GI, MCS: 16 ~ 23
2548
2549     rate_count = sizeof(ralinkrate)/sizeof(__s32);
2550     //check if the interface is down
2551         if(!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_INTERRUPT_IN_USE))
2552         {
2553                 DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
2554         return -ENETDOWN;
2555         }
2556
2557     if ((pAd->StaCfg.bAutoTxRateSwitch == FALSE) &&
2558         (INFRA_ON(pAd)) &&
2559         ((pAd->CommonCfg.PhyMode <= PHY_11G) || (pAd->MacTab.Content[BSSID_WCID].HTPhyMode.field.MODE <= MODE_OFDM)))
2560         ht_setting.word = pAd->StaCfg.HTPhyMode.word;
2561     else
2562         ht_setting.word = pAd->MacTab.Content[BSSID_WCID].HTPhyMode.word;
2563
2564     if (ht_setting.field.MODE >= MODE_HTMIX)
2565     {
2566         rate_index = 12 + ((UCHAR)ht_setting.field.BW *24) + ((UCHAR)ht_setting.field.ShortGI *48) + ((UCHAR)ht_setting.field.MCS);
2567     }
2568     else
2569     if (ht_setting.field.MODE == MODE_OFDM)
2570         rate_index = (UCHAR)(ht_setting.field.MCS) + 4;
2571     else if (ht_setting.field.MODE == MODE_CCK)
2572         rate_index = (UCHAR)(ht_setting.field.MCS);
2573
2574     if (rate_index < 0)
2575         rate_index = 0;
2576
2577     if (rate_index > rate_count)
2578         rate_index = rate_count;
2579
2580     wrqu->bitrate.value = ralinkrate[rate_index] * 500000;
2581     wrqu->bitrate.disabled = 0;
2582
2583     return 0;
2584 }
2585
2586 static const iw_handler rt_handler[] =
2587 {
2588         (iw_handler) NULL,                                  /* SIOCSIWCOMMIT */
2589         (iw_handler) rt_ioctl_giwname,                  /* SIOCGIWNAME   */
2590         (iw_handler) NULL,                                  /* SIOCSIWNWID   */
2591         (iw_handler) NULL,                                  /* SIOCGIWNWID   */
2592         (iw_handler) rt_ioctl_siwfreq,              /* SIOCSIWFREQ   */
2593         (iw_handler) rt_ioctl_giwfreq,              /* SIOCGIWFREQ   */
2594         (iw_handler) rt_ioctl_siwmode,              /* SIOCSIWMODE   */
2595         (iw_handler) rt_ioctl_giwmode,              /* SIOCGIWMODE   */
2596         (iw_handler) NULL,                              /* SIOCSIWSENS   */
2597         (iw_handler) NULL,                              /* SIOCGIWSENS   */
2598         (iw_handler) NULL /* not used */,               /* SIOCSIWRANGE  */
2599         (iw_handler) rt_ioctl_giwrange,             /* SIOCGIWRANGE  */
2600         (iw_handler) NULL /* not used */,               /* SIOCSIWPRIV   */
2601         (iw_handler) NULL /* kernel code */,    /* SIOCGIWPRIV   */
2602         (iw_handler) NULL /* not used */,               /* SIOCSIWSTATS  */
2603         (iw_handler) rt28xx_get_wireless_stats /* kernel code */,    /* SIOCGIWSTATS  */
2604         (iw_handler) NULL,                              /* SIOCSIWSPY    */
2605         (iw_handler) NULL,                              /* SIOCGIWSPY    */
2606         (iw_handler) NULL,                                      /* SIOCSIWTHRSPY */
2607         (iw_handler) NULL,                                      /* SIOCGIWTHRSPY */
2608         (iw_handler) rt_ioctl_siwap,            /* SIOCSIWAP     */
2609         (iw_handler) rt_ioctl_giwap,                /* SIOCGIWAP     */
2610         (iw_handler) rt_ioctl_siwmlme,          /* SIOCSIWMLME   */
2611         (iw_handler) rt_ioctl_iwaplist,             /* SIOCGIWAPLIST */
2612         (iw_handler) rt_ioctl_siwscan,              /* SIOCSIWSCAN   */
2613         (iw_handler) rt_ioctl_giwscan,              /* SIOCGIWSCAN   */
2614         (iw_handler) rt_ioctl_siwessid,             /* SIOCSIWESSID  */
2615         (iw_handler) rt_ioctl_giwessid,             /* SIOCGIWESSID  */
2616         (iw_handler) rt_ioctl_siwnickn,             /* SIOCSIWNICKN  */
2617         (iw_handler) rt_ioctl_giwnickn,             /* SIOCGIWNICKN  */
2618         (iw_handler) NULL,                                      /* -- hole --    */
2619         (iw_handler) NULL,                                      /* -- hole --    */
2620         (iw_handler) rt_ioctl_siwrate,          /* SIOCSIWRATE   */
2621         (iw_handler) rt_ioctl_giwrate,          /* SIOCGIWRATE   */
2622         (iw_handler) rt_ioctl_siwrts,               /* SIOCSIWRTS    */
2623         (iw_handler) rt_ioctl_giwrts,               /* SIOCGIWRTS    */
2624         (iw_handler) rt_ioctl_siwfrag,              /* SIOCSIWFRAG   */
2625         (iw_handler) rt_ioctl_giwfrag,              /* SIOCGIWFRAG   */
2626         (iw_handler) NULL,                              /* SIOCSIWTXPOW  */
2627         (iw_handler) NULL,                              /* SIOCGIWTXPOW  */
2628         (iw_handler) NULL,                              /* SIOCSIWRETRY  */
2629         (iw_handler) NULL,                              /* SIOCGIWRETRY  */
2630         (iw_handler) rt_ioctl_siwencode,                /* SIOCSIWENCODE */
2631         (iw_handler) rt_ioctl_giwencode,                /* SIOCGIWENCODE */
2632         (iw_handler) NULL,                              /* SIOCSIWPOWER  */
2633         (iw_handler) NULL,                              /* SIOCGIWPOWER  */
2634         (iw_handler) NULL,                                              /* -- hole -- */
2635         (iw_handler) NULL,                                              /* -- hole -- */
2636     (iw_handler) rt_ioctl_siwgenie,         /* SIOCSIWGENIE  */
2637         (iw_handler) rt_ioctl_giwgenie,         /* SIOCGIWGENIE  */
2638         (iw_handler) rt_ioctl_siwauth,              /* SIOCSIWAUTH   */
2639         (iw_handler) rt_ioctl_giwauth,              /* SIOCGIWAUTH   */
2640         (iw_handler) rt_ioctl_siwencodeext,         /* SIOCSIWENCODEEXT */
2641         (iw_handler) rt_ioctl_giwencodeext,             /* SIOCGIWENCODEEXT */
2642         (iw_handler) rt_ioctl_siwpmksa,         /* SIOCSIWPMKSA  */
2643 };
2644
2645 static const iw_handler rt_priv_handlers[] = {
2646         (iw_handler) NULL, /* + 0x00 */
2647         (iw_handler) NULL, /* + 0x01 */
2648         (iw_handler) rt_ioctl_setparam, /* + 0x02 */
2649         (iw_handler) NULL, /* + 0x03 */
2650         (iw_handler) NULL, /* + 0x04 */
2651         (iw_handler) NULL, /* + 0x05 */
2652         (iw_handler) NULL, /* + 0x06 */
2653         (iw_handler) NULL, /* + 0x07 */
2654         (iw_handler) NULL, /* + 0x08 */
2655         (iw_handler) rt_private_get_statistics, /* + 0x09 */
2656         (iw_handler) NULL, /* + 0x0A */
2657         (iw_handler) NULL, /* + 0x0B */
2658         (iw_handler) NULL, /* + 0x0C */
2659         (iw_handler) NULL, /* + 0x0D */
2660         (iw_handler) NULL, /* + 0x0E */
2661         (iw_handler) NULL, /* + 0x0F */
2662         (iw_handler) NULL, /* + 0x10 */
2663         (iw_handler) rt_private_show, /* + 0x11 */
2664     (iw_handler) NULL, /* + 0x12 */
2665         (iw_handler) NULL, /* + 0x13 */
2666         (iw_handler) NULL, /* + 0x15 */
2667         (iw_handler) NULL, /* + 0x17 */
2668         (iw_handler) NULL, /* + 0x18 */
2669 };
2670
2671 const struct iw_handler_def rt28xx_iw_handler_def =
2672 {
2673 #define N(a)    (sizeof (a) / sizeof (a[0]))
2674         .standard       = (iw_handler *) rt_handler,
2675         .num_standard   = sizeof(rt_handler) / sizeof(iw_handler),
2676         .private        = (iw_handler *) rt_priv_handlers,
2677         .num_private            = N(rt_priv_handlers),
2678         .private_args   = (struct iw_priv_args *) privtab,
2679         .num_private_args       = N(privtab),
2680 #if IW_HANDLER_VERSION >= 7
2681     .get_wireless_stats = rt28xx_get_wireless_stats,
2682 #endif
2683 };
2684
2685 INT rt28xx_sta_ioctl(
2686         IN      struct net_device       *net_dev,
2687         IN      OUT     struct ifreq    *rq,
2688         IN      INT                                     cmd)
2689 {
2690         RTMP_ADAPTER *pAd = net_dev->ml_priv;
2691         POS_COOKIE pObj = (POS_COOKIE)pAd->OS_Cookie;
2692         struct iwreq        *wrq = (struct iwreq *) rq;
2693         BOOLEAN                         StateMachineTouched = FALSE;
2694         INT                                     Status = NDIS_STATUS_SUCCESS;
2695
2696     //check if the interface is down
2697     if(!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_INTERRUPT_IN_USE))
2698     {
2699         {
2700             DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
2701                     return -ENETDOWN;
2702         }
2703     }
2704
2705         {       // determine this ioctl command is comming from which interface.
2706                 pObj->ioctl_if_type = INT_MAIN;
2707                 pObj->ioctl_if = MAIN_MBSSID;
2708         }
2709
2710         switch(cmd)
2711         {
2712         case SIOCGIFHWADDR:
2713                         DBGPRINT(RT_DEBUG_TRACE, ("IOCTL::SIOCGIFHWADDR\n"));
2714                         memcpy(wrq->u.name, pAd->CurrentAddress, ETH_ALEN);
2715                         break;
2716                 case SIOCGIWNAME:
2717         {
2718                 char *name=&wrq->u.name[0];
2719                 rt_ioctl_giwname(net_dev, NULL, name, NULL);
2720                         break;
2721                 }
2722                 case SIOCGIWESSID:  //Get ESSID
2723         {
2724                 struct iw_point *essid=&wrq->u.essid;
2725                 rt_ioctl_giwessid(net_dev, NULL, essid, essid->pointer);
2726                         break;
2727                 }
2728                 case SIOCSIWESSID:  //Set ESSID
2729         {
2730                 struct iw_point *essid=&wrq->u.essid;
2731                 rt_ioctl_siwessid(net_dev, NULL, essid, essid->pointer);
2732                         break;
2733                 }
2734                 case SIOCSIWNWID:   // set network id (the cell)
2735                 case SIOCGIWNWID:   // get network id
2736                         Status = -EOPNOTSUPP;
2737                         break;
2738                 case SIOCSIWFREQ:   //set channel/frequency (Hz)
2739         {
2740                 struct iw_freq *freq=&wrq->u.freq;
2741                 rt_ioctl_siwfreq(net_dev, NULL, freq, NULL);
2742                         break;
2743                 }
2744                 case SIOCGIWFREQ:   // get channel/frequency (Hz)
2745         {
2746                 struct iw_freq *freq=&wrq->u.freq;
2747                 rt_ioctl_giwfreq(net_dev, NULL, freq, NULL);
2748                         break;
2749                 }
2750                 case SIOCSIWNICKN: //set node name/nickname
2751         {
2752                 struct iw_point *data=&wrq->u.data;
2753                 rt_ioctl_siwnickn(net_dev, NULL, data, NULL);
2754                         break;
2755                 }
2756                 case SIOCGIWNICKN: //get node name/nickname
2757         {
2758                 struct iw_point *data=&wrq->u.data;
2759                 rt_ioctl_giwnickn(net_dev, NULL, data, NULL);
2760                         break;
2761                 }
2762                 case SIOCGIWRATE:   //get default bit rate (bps)
2763                     rt_ioctl_giwrate(net_dev, NULL, &wrq->u, NULL);
2764             break;
2765             case SIOCSIWRATE:  //set default bit rate (bps)
2766                 rt_ioctl_siwrate(net_dev, NULL, &wrq->u, NULL);
2767             break;
2768         case SIOCGIWRTS:  // get RTS/CTS threshold (bytes)
2769         {
2770                 struct iw_param *rts=&wrq->u.rts;
2771                 rt_ioctl_giwrts(net_dev, NULL, rts, NULL);
2772                         break;
2773                 }
2774         case SIOCSIWRTS:  //set RTS/CTS threshold (bytes)
2775         {
2776                 struct iw_param *rts=&wrq->u.rts;
2777                 rt_ioctl_siwrts(net_dev, NULL, rts, NULL);
2778                         break;
2779                 }
2780         case SIOCGIWFRAG:  //get fragmentation thr (bytes)
2781         {
2782                 struct iw_param *frag=&wrq->u.frag;
2783                 rt_ioctl_giwfrag(net_dev, NULL, frag, NULL);
2784                         break;
2785                 }
2786         case SIOCSIWFRAG:  //set fragmentation thr (bytes)
2787         {
2788                 struct iw_param *frag=&wrq->u.frag;
2789                 rt_ioctl_siwfrag(net_dev, NULL, frag, NULL);
2790                         break;
2791                 }
2792         case SIOCGIWENCODE:  //get encoding token & mode
2793         {
2794                 struct iw_point *erq=&wrq->u.encoding;
2795                 if(erq->pointer)
2796                         rt_ioctl_giwencode(net_dev, NULL, erq, erq->pointer);
2797                         break;
2798                 }
2799         case SIOCSIWENCODE:  //set encoding token & mode
2800         {
2801                 struct iw_point *erq=&wrq->u.encoding;
2802                 if(erq->pointer)
2803                         rt_ioctl_siwencode(net_dev, NULL, erq, erq->pointer);
2804                         break;
2805                 }
2806                 case SIOCGIWAP:     //get access point MAC addresses
2807         {
2808                 struct sockaddr *ap_addr=&wrq->u.ap_addr;
2809                 rt_ioctl_giwap(net_dev, NULL, ap_addr, ap_addr->sa_data);
2810                         break;
2811                 }
2812             case SIOCSIWAP:  //set access point MAC addresses
2813         {
2814                 struct sockaddr *ap_addr=&wrq->u.ap_addr;
2815                 rt_ioctl_siwap(net_dev, NULL, ap_addr, ap_addr->sa_data);
2816                         break;
2817                 }
2818                 case SIOCGIWMODE:   //get operation mode
2819         {
2820                 __u32 *mode=&wrq->u.mode;
2821                 rt_ioctl_giwmode(net_dev, NULL, mode, NULL);
2822                         break;
2823                 }
2824                 case SIOCSIWMODE:   //set operation mode
2825         {
2826                 __u32 *mode=&wrq->u.mode;
2827                 rt_ioctl_siwmode(net_dev, NULL, mode, NULL);
2828                         break;
2829                 }
2830                 case SIOCGIWSENS:   //get sensitivity (dBm)
2831                 case SIOCSIWSENS:       //set sensitivity (dBm)
2832                 case SIOCGIWPOWER:  //get Power Management settings
2833                 case SIOCSIWPOWER:  //set Power Management settings
2834                 case SIOCGIWTXPOW:  //get transmit power (dBm)
2835                 case SIOCSIWTXPOW:  //set transmit power (dBm)
2836                 case SIOCGIWRANGE:      //Get range of parameters
2837                 case SIOCGIWRETRY:      //get retry limits and lifetime
2838                 case SIOCSIWRETRY:      //set retry limits and lifetime
2839                 case RT_PRIV_IOCTL:
2840                 case RT_PRIV_IOCTL_EXT:
2841                         Status = -EOPNOTSUPP;
2842                         break;
2843                 case SIOCGIWPRIV:
2844                         if (wrq->u.data.pointer)
2845                         {
2846                                 if ( access_ok(VERIFY_WRITE, wrq->u.data.pointer, sizeof(privtab)) != TRUE)
2847                                         break;
2848                                 wrq->u.data.length = sizeof(privtab) / sizeof(privtab[0]);
2849                                 if (copy_to_user(wrq->u.data.pointer, privtab, sizeof(privtab)))
2850                                         Status = -EFAULT;
2851                         }
2852                         break;
2853                 case RTPRIV_IOCTL_SET:
2854                         if(access_ok(VERIFY_READ, wrq->u.data.pointer, wrq->u.data.length) != TRUE)
2855                                 break;
2856                         rt_ioctl_setparam(net_dev, NULL, NULL, wrq->u.data.pointer);
2857                         break;
2858                 case RTPRIV_IOCTL_GSITESURVEY:
2859                         RTMPIoctlGetSiteSurvey(pAd, wrq);
2860                     break;
2861         case SIOCETHTOOL:
2862                 break;
2863                 default:
2864                         DBGPRINT(RT_DEBUG_ERROR, ("IOCTL::unknown IOCTL's cmd = 0x%08x\n", cmd));
2865                         Status = -EOPNOTSUPP;
2866                         break;
2867         }
2868
2869     if(StateMachineTouched) // Upper layer sent a MLME-related operations
2870         RT28XX_MLME_HANDLER(pAd);
2871
2872         return Status;
2873 }
2874
2875 /*
2876     ==========================================================================
2877     Description:
2878         Set SSID
2879     Return:
2880         TRUE if all parameters are OK, FALSE otherwise
2881     ==========================================================================
2882 */
2883 INT Set_SSID_Proc(
2884     IN  PRTMP_ADAPTER   pAdapter,
2885     IN  PUCHAR          arg)
2886 {
2887     NDIS_802_11_SSID                    Ssid, *pSsid=NULL;
2888     BOOLEAN                             StateMachineTouched = FALSE;
2889     int                                 success = TRUE;
2890
2891     if( strlen(arg) <= MAX_LEN_OF_SSID)
2892     {
2893         NdisZeroMemory(&Ssid, sizeof(NDIS_802_11_SSID));
2894         if (strlen(arg) != 0)
2895         {
2896             NdisMoveMemory(Ssid.Ssid, arg, strlen(arg));
2897             Ssid.SsidLength = strlen(arg);
2898         }
2899         else   //ANY ssid
2900         {
2901             Ssid.SsidLength = 0;
2902                     memcpy(Ssid.Ssid, "", 0);
2903                         pAdapter->StaCfg.BssType = BSS_INFRA;
2904                         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeOpen;
2905                 pAdapter->StaCfg.WepStatus  = Ndis802_11EncryptionDisabled;
2906                 }
2907         pSsid = &Ssid;
2908
2909         if (pAdapter->Mlme.CntlMachine.CurrState != CNTL_IDLE)
2910         {
2911             RT28XX_MLME_RESET_STATE_MACHINE(pAdapter);
2912             DBGPRINT(RT_DEBUG_TRACE, ("!!! MLME busy, reset MLME state machine !!!\n"));
2913         }
2914
2915         pAdapter->MlmeAux.CurrReqIsFromNdis = TRUE;
2916         pAdapter->StaCfg.bScanReqIsFromWebUI = FALSE;
2917                 pAdapter->bConfigChanged = TRUE;
2918
2919         MlmeEnqueue(pAdapter,
2920                     MLME_CNTL_STATE_MACHINE,
2921                     OID_802_11_SSID,
2922                     sizeof(NDIS_802_11_SSID),
2923                     (VOID *)pSsid);
2924
2925         StateMachineTouched = TRUE;
2926         DBGPRINT(RT_DEBUG_TRACE, ("Set_SSID_Proc::(Len=%d,Ssid=%s)\n", Ssid.SsidLength, Ssid.Ssid));
2927     }
2928     else
2929         success = FALSE;
2930
2931     if (StateMachineTouched) // Upper layer sent a MLME-related operations
2932         RT28XX_MLME_HANDLER(pAdapter);
2933
2934     return success;
2935 }
2936
2937 #ifdef WMM_SUPPORT
2938 /*
2939     ==========================================================================
2940     Description:
2941         Set WmmCapable Enable or Disable
2942     Return:
2943         TRUE if all parameters are OK, FALSE otherwise
2944     ==========================================================================
2945 */
2946 INT     Set_WmmCapable_Proc(
2947         IN      PRTMP_ADAPTER   pAd,
2948         IN      PUCHAR                  arg)
2949 {
2950         BOOLEAN bWmmCapable;
2951
2952         bWmmCapable = simple_strtol(arg, 0, 10);
2953
2954         if ((bWmmCapable == 1)
2955 #ifdef RT2870
2956                 && (pAd->NumberOfPipes >= 5)
2957 #endif // RT2870 //
2958                 )
2959                 pAd->CommonCfg.bWmmCapable = TRUE;
2960         else if (bWmmCapable == 0)
2961                 pAd->CommonCfg.bWmmCapable = FALSE;
2962         else
2963                 return FALSE;  //Invalid argument
2964
2965         DBGPRINT(RT_DEBUG_TRACE, ("Set_WmmCapable_Proc::(bWmmCapable=%d)\n",
2966                 pAd->CommonCfg.bWmmCapable));
2967
2968         return TRUE;
2969 }
2970 #endif // WMM_SUPPORT //
2971
2972 /*
2973     ==========================================================================
2974     Description:
2975         Set Network Type(Infrastructure/Adhoc mode)
2976     Return:
2977         TRUE if all parameters are OK, FALSE otherwise
2978     ==========================================================================
2979 */
2980 INT Set_NetworkType_Proc(
2981     IN  PRTMP_ADAPTER   pAdapter,
2982     IN  PUCHAR          arg)
2983 {
2984     UINT32      Value = 0;
2985
2986     if (strcmp(arg, "Adhoc") == 0)
2987         {
2988                 if (pAdapter->StaCfg.BssType != BSS_ADHOC)
2989                 {
2990                         // Config has changed
2991                         pAdapter->bConfigChanged = TRUE;
2992             if (MONITOR_ON(pAdapter))
2993             {
2994                 RTMP_IO_WRITE32(pAdapter, RX_FILTR_CFG, STANORMAL);
2995                 RTMP_IO_READ32(pAdapter, MAC_SYS_CTRL, &Value);
2996                                 Value &= (~0x80);
2997                                 RTMP_IO_WRITE32(pAdapter, MAC_SYS_CTRL, Value);
2998                 OPSTATUS_CLEAR_FLAG(pAdapter, fOP_STATUS_MEDIA_STATE_CONNECTED);
2999                 pAdapter->StaCfg.bAutoReconnect = TRUE;
3000                 LinkDown(pAdapter, FALSE);
3001             }
3002                         if (INFRA_ON(pAdapter))
3003                         {
3004                                 //BOOLEAN Cancelled;
3005                                 // Set the AutoReconnectSsid to prevent it reconnect to old SSID
3006                                 // Since calling this indicate user don't want to connect to that SSID anymore.
3007                                 pAdapter->MlmeAux.AutoReconnectSsidLen= 32;
3008                                 NdisZeroMemory(pAdapter->MlmeAux.AutoReconnectSsid, pAdapter->MlmeAux.AutoReconnectSsidLen);
3009
3010                                 LinkDown(pAdapter, FALSE);
3011
3012                                 DBGPRINT(RT_DEBUG_TRACE, ("NDIS_STATUS_MEDIA_DISCONNECT Event BB!\n"));
3013                         }
3014                 }
3015                 pAdapter->StaCfg.BssType = BSS_ADHOC;
3016         pAdapter->net_dev->type = pAdapter->StaCfg.OriDevType;
3017                 DBGPRINT(RT_DEBUG_TRACE, ("===>Set_NetworkType_Proc::(AD-HOC)\n"));
3018         }
3019     else if (strcmp(arg, "Infra") == 0)
3020         {
3021                 if (pAdapter->StaCfg.BssType != BSS_INFRA)
3022                 {
3023                         // Config has changed
3024                         pAdapter->bConfigChanged = TRUE;
3025             if (MONITOR_ON(pAdapter))
3026             {
3027                 RTMP_IO_WRITE32(pAdapter, RX_FILTR_CFG, STANORMAL);
3028                 RTMP_IO_READ32(pAdapter, MAC_SYS_CTRL, &Value);
3029                                 Value &= (~0x80);
3030                                 RTMP_IO_WRITE32(pAdapter, MAC_SYS_CTRL, Value);
3031                 OPSTATUS_CLEAR_FLAG(pAdapter, fOP_STATUS_MEDIA_STATE_CONNECTED);
3032                 pAdapter->StaCfg.bAutoReconnect = TRUE;
3033                 LinkDown(pAdapter, FALSE);
3034             }
3035                         if (ADHOC_ON(pAdapter))
3036                         {
3037                                 // Set the AutoReconnectSsid to prevent it reconnect to old SSID
3038                                 // Since calling this indicate user don't want to connect to that SSID anymore.
3039                                 pAdapter->MlmeAux.AutoReconnectSsidLen= 32;
3040                                 NdisZeroMemory(pAdapter->MlmeAux.AutoReconnectSsid, pAdapter->MlmeAux.AutoReconnectSsidLen);
3041
3042                                 LinkDown(pAdapter, FALSE);
3043                         }
3044                 }
3045                 pAdapter->StaCfg.BssType = BSS_INFRA;
3046         pAdapter->net_dev->type = pAdapter->StaCfg.OriDevType;
3047                 DBGPRINT(RT_DEBUG_TRACE, ("===>Set_NetworkType_Proc::(INFRA)\n"));
3048
3049         pAdapter->StaCfg.BssType = BSS_INFRA;
3050         }
3051     else if (strcmp(arg, "Monitor") == 0)
3052     {
3053                 UCHAR   bbpValue = 0;
3054                 BCN_TIME_CFG_STRUC csr;
3055                 OPSTATUS_CLEAR_FLAG(pAdapter, fOP_STATUS_INFRA_ON);
3056         OPSTATUS_CLEAR_FLAG(pAdapter, fOP_STATUS_ADHOC_ON);
3057                 OPSTATUS_SET_FLAG(pAdapter, fOP_STATUS_MEDIA_STATE_CONNECTED);
3058                 // disable all periodic state machine
3059                 pAdapter->StaCfg.bAutoReconnect = FALSE;
3060                 // reset all mlme state machine
3061                 RT28XX_MLME_RESET_STATE_MACHINE(pAdapter);
3062                 DBGPRINT(RT_DEBUG_TRACE, ("fOP_STATUS_MEDIA_STATE_CONNECTED \n"));
3063         if (pAdapter->CommonCfg.CentralChannel == 0)
3064         {
3065             if (pAdapter->CommonCfg.PhyMode == PHY_11AN_MIXED)
3066                 pAdapter->CommonCfg.CentralChannel = 36;
3067             else
3068                 pAdapter->CommonCfg.CentralChannel = 6;
3069         }
3070         else
3071             N_ChannelCheck(pAdapter);
3072
3073         if (pAdapter->CommonCfg.PhyMode >= PHY_11ABGN_MIXED &&
3074             pAdapter->CommonCfg.RegTransmitSetting.field.BW == BW_40 &&
3075             pAdapter->CommonCfg.RegTransmitSetting.field.EXTCHA == EXTCHA_ABOVE)
3076                 {
3077                         // 40MHz ,control channel at lower
3078                         RTMP_BBP_IO_READ8_BY_REG_ID(pAdapter, BBP_R4, &bbpValue);
3079                         bbpValue &= (~0x18);
3080                         bbpValue |= 0x10;
3081                         RTMP_BBP_IO_WRITE8_BY_REG_ID(pAdapter, BBP_R4, bbpValue);
3082                         pAdapter->CommonCfg.BBPCurrentBW = BW_40;
3083                         //  RX : control channel at lower
3084                         RTMP_BBP_IO_READ8_BY_REG_ID(pAdapter, BBP_R3, &bbpValue);
3085                         bbpValue &= (~0x20);
3086                         RTMP_BBP_IO_WRITE8_BY_REG_ID(pAdapter, BBP_R3, bbpValue);
3087
3088                         RTMP_IO_READ32(pAdapter, TX_BAND_CFG, &Value);
3089                         Value &= 0xfffffffe;
3090                         RTMP_IO_WRITE32(pAdapter, TX_BAND_CFG, Value);
3091                         pAdapter->CommonCfg.CentralChannel = pAdapter->CommonCfg.Channel + 2;
3092             AsicSwitchChannel(pAdapter, pAdapter->CommonCfg.CentralChannel, FALSE);
3093                     AsicLockChannel(pAdapter, pAdapter->CommonCfg.CentralChannel);
3094             DBGPRINT(RT_DEBUG_TRACE, ("BW_40 ,control_channel(%d), CentralChannel(%d) \n",
3095                                        pAdapter->CommonCfg.Channel,
3096                                        pAdapter->CommonCfg.CentralChannel));
3097                 }
3098                 else if (pAdapter->CommonCfg.PhyMode >= PHY_11ABGN_MIXED &&
3099                  pAdapter->CommonCfg.RegTransmitSetting.field.BW == BW_40 &&
3100                  pAdapter->CommonCfg.RegTransmitSetting.field.EXTCHA == EXTCHA_BELOW)
3101                 {
3102                         // 40MHz ,control channel at upper
3103                         RTMP_BBP_IO_READ8_BY_REG_ID(pAdapter, BBP_R4, &bbpValue);
3104                         bbpValue &= (~0x18);
3105                         bbpValue |= 0x10;
3106                         RTMP_BBP_IO_WRITE8_BY_REG_ID(pAdapter, BBP_R4, bbpValue);
3107                         pAdapter->CommonCfg.BBPCurrentBW = BW_40;
3108                         RTMP_IO_READ32(pAdapter, TX_BAND_CFG, &Value);
3109                         Value |= 0x1;
3110                         RTMP_IO_WRITE32(pAdapter, TX_BAND_CFG, Value);
3111
3112                         RTMP_BBP_IO_READ8_BY_REG_ID(pAdapter, BBP_R3, &bbpValue);
3113                         bbpValue |= (0x20);
3114                         RTMP_BBP_IO_WRITE8_BY_REG_ID(pAdapter, BBP_R3, bbpValue);
3115                         pAdapter->CommonCfg.CentralChannel = pAdapter->CommonCfg.Channel - 2;
3116             AsicSwitchChannel(pAdapter, pAdapter->CommonCfg.CentralChannel, FALSE);
3117                     AsicLockChannel(pAdapter, pAdapter->CommonCfg.CentralChannel);
3118             DBGPRINT(RT_DEBUG_TRACE, ("BW_40 ,control_channel(%d), CentralChannel(%d) \n",
3119                                        pAdapter->CommonCfg.Channel,
3120                                        pAdapter->CommonCfg.CentralChannel));
3121                 }
3122                 else
3123                 {
3124                         // 20MHz
3125                         RTMP_BBP_IO_READ8_BY_REG_ID(pAdapter, BBP_R4, &bbpValue);
3126                         bbpValue &= (~0x18);
3127                         RTMP_BBP_IO_WRITE8_BY_REG_ID(pAdapter, BBP_R4, bbpValue);
3128                         pAdapter->CommonCfg.BBPCurrentBW = BW_20;
3129                         AsicSwitchChannel(pAdapter, pAdapter->CommonCfg.Channel, FALSE);
3130                         AsicLockChannel(pAdapter, pAdapter->CommonCfg.Channel);
3131                         DBGPRINT(RT_DEBUG_TRACE, ("BW_20, Channel(%d)\n", pAdapter->CommonCfg.Channel));
3132                 }
3133                 // Enable Rx with promiscuous reception
3134                 RTMP_IO_WRITE32(pAdapter, RX_FILTR_CFG, 0x3);
3135                 // ASIC supporsts sniffer function with replacing RSSI with timestamp.
3136                 //RTMP_IO_READ32(pAdapter, MAC_SYS_CTRL, &Value);
3137                 //Value |= (0x80);
3138                 //RTMP_IO_WRITE32(pAdapter, MAC_SYS_CTRL, Value);
3139                 // disable sync
3140                 RTMP_IO_READ32(pAdapter, BCN_TIME_CFG, &csr.word);
3141                 csr.field.bBeaconGen = 0;
3142                 csr.field.bTBTTEnable = 0;
3143                 csr.field.TsfSyncMode = 0;
3144                 RTMP_IO_WRITE32(pAdapter, BCN_TIME_CFG, csr.word);
3145
3146                 pAdapter->StaCfg.BssType = BSS_MONITOR;
3147         pAdapter->net_dev->type = ARPHRD_IEEE80211_PRISM; //ARPHRD_IEEE80211; // IEEE80211
3148                 DBGPRINT(RT_DEBUG_TRACE, ("===>Set_NetworkType_Proc::(MONITOR)\n"));
3149     }
3150
3151     // Reset Ralink supplicant to not use, it will be set to start when UI set PMK key
3152     pAdapter->StaCfg.WpaState = SS_NOTUSE;
3153
3154     DBGPRINT(RT_DEBUG_TRACE, ("Set_NetworkType_Proc::(NetworkType=%d)\n", pAdapter->StaCfg.BssType));
3155
3156     return TRUE;
3157 }
3158
3159 /*
3160     ==========================================================================
3161     Description:
3162         Set Authentication mode
3163     Return:
3164         TRUE if all parameters are OK, FALSE otherwise
3165     ==========================================================================
3166 */
3167 INT Set_AuthMode_Proc(
3168     IN  PRTMP_ADAPTER   pAdapter,
3169     IN  PUCHAR          arg)
3170 {
3171     if ((strcmp(arg, "WEPAUTO") == 0) || (strcmp(arg, "wepauto") == 0))
3172         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeAutoSwitch;
3173     else if ((strcmp(arg, "OPEN") == 0) || (strcmp(arg, "open") == 0))
3174         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeOpen;
3175     else if ((strcmp(arg, "SHARED") == 0) || (strcmp(arg, "shared") == 0))
3176         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeShared;
3177     else if ((strcmp(arg, "WPAPSK") == 0) || (strcmp(arg, "wpapsk") == 0))
3178         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeWPAPSK;
3179     else if ((strcmp(arg, "WPANONE") == 0) || (strcmp(arg, "wpanone") == 0))
3180         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeWPANone;
3181     else if ((strcmp(arg, "WPA2PSK") == 0) || (strcmp(arg, "wpa2psk") == 0))
3182         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeWPA2PSK;
3183     else if ((strcmp(arg, "WPA") == 0) || (strcmp(arg, "wpa") == 0))
3184         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeWPA;
3185     else if ((strcmp(arg, "WPA2") == 0) || (strcmp(arg, "wpa2") == 0))
3186         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeWPA2;
3187     else
3188         return FALSE;
3189
3190     pAdapter->StaCfg.PortSecured = WPA_802_1X_PORT_NOT_SECURED;
3191
3192     DBGPRINT(RT_DEBUG_TRACE, ("Set_AuthMode_Proc::(AuthMode=%d)\n", pAdapter->StaCfg.AuthMode));
3193
3194     return TRUE;
3195 }
3196
3197 /*
3198     ==========================================================================
3199     Description:
3200         Set Encryption Type
3201     Return:
3202         TRUE if all parameters are OK, FALSE otherwise
3203     ==========================================================================
3204 */
3205 INT Set_EncrypType_Proc(
3206     IN  PRTMP_ADAPTER   pAdapter,
3207     IN  PUCHAR          arg)
3208 {
3209     if ((strcmp(arg, "NONE") == 0) || (strcmp(arg, "none") == 0))
3210     {
3211         if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
3212             return TRUE;    // do nothing
3213
3214         pAdapter->StaCfg.WepStatus     = Ndis802_11WEPDisabled;
3215         pAdapter->StaCfg.PairCipher    = Ndis802_11WEPDisabled;
3216             pAdapter->StaCfg.GroupCipher   = Ndis802_11WEPDisabled;
3217     }
3218     else if ((strcmp(arg, "WEP") == 0) || (strcmp(arg, "wep") == 0))
3219     {
3220         if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
3221             return TRUE;    // do nothing
3222
3223         pAdapter->StaCfg.WepStatus     = Ndis802_11WEPEnabled;
3224         pAdapter->StaCfg.PairCipher    = Ndis802_11WEPEnabled;
3225             pAdapter->StaCfg.GroupCipher   = Ndis802_11WEPEnabled;
3226     }
3227     else if ((strcmp(arg, "TKIP") == 0) || (strcmp(arg, "tkip") == 0))
3228     {
3229         if (pAdapter->StaCfg.AuthMode < Ndis802_11AuthModeWPA)
3230             return TRUE;    // do nothing
3231
3232         pAdapter->StaCfg.WepStatus     = Ndis802_11Encryption2Enabled;
3233         pAdapter->StaCfg.PairCipher    = Ndis802_11Encryption2Enabled;
3234             pAdapter->StaCfg.GroupCipher   = Ndis802_11Encryption2Enabled;
3235     }
3236     else if ((strcmp(arg, "AES") == 0) || (strcmp(arg, "aes") == 0))
3237     {
3238         if (pAdapter->StaCfg.AuthMode < Ndis802_11AuthModeWPA)
3239             return TRUE;    // do nothing
3240
3241         pAdapter->StaCfg.WepStatus     = Ndis802_11Encryption3Enabled;
3242         pAdapter->StaCfg.PairCipher    = Ndis802_11Encryption3Enabled;
3243             pAdapter->StaCfg.GroupCipher   = Ndis802_11Encryption3Enabled;
3244     }
3245     else
3246         return FALSE;
3247
3248     pAdapter->StaCfg.OrigWepStatus = pAdapter->StaCfg.WepStatus;
3249
3250     DBGPRINT(RT_DEBUG_TRACE, ("Set_EncrypType_Proc::(EncrypType=%d)\n", pAdapter->StaCfg.WepStatus));
3251
3252     return TRUE;
3253 }
3254
3255 /*
3256     ==========================================================================
3257     Description:
3258         Set Default Key ID
3259     Return:
3260         TRUE if all parameters are OK, FALSE otherwise
3261     ==========================================================================
3262 */
3263 INT Set_DefaultKeyID_Proc(
3264     IN  PRTMP_ADAPTER   pAdapter,
3265     IN  PUCHAR          arg)
3266 {
3267     ULONG                               KeyIdx;
3268
3269     KeyIdx = simple_strtol(arg, 0, 10);
3270     if((KeyIdx >= 1 ) && (KeyIdx <= 4))
3271         pAdapter->StaCfg.DefaultKeyId = (UCHAR) (KeyIdx - 1 );
3272     else
3273         return FALSE;  //Invalid argument
3274
3275     DBGPRINT(RT_DEBUG_TRACE, ("Set_DefaultKeyID_Proc::(DefaultKeyID=%d)\n", pAdapter->StaCfg.DefaultKeyId));
3276
3277     return TRUE;
3278 }
3279
3280 /*
3281     ==========================================================================
3282     Description:
3283         Set WEP KEY1
3284     Return:
3285         TRUE if all parameters are OK, FALSE otherwise
3286     ==========================================================================
3287 */
3288 INT Set_Key1_Proc(
3289     IN  PRTMP_ADAPTER   pAdapter,
3290     IN  PUCHAR          arg)
3291 {
3292     int                                 KeyLen;
3293     int                                 i;
3294     UCHAR                               CipherAlg=CIPHER_WEP64;
3295
3296     if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
3297         return TRUE;    // do nothing
3298
3299     KeyLen = strlen(arg);
3300
3301     switch (KeyLen)
3302     {
3303         case 5: //wep 40 Ascii type
3304             pAdapter->SharedKey[BSS0][0].KeyLen = KeyLen;
3305             memcpy(pAdapter->SharedKey[BSS0][0].Key, arg, KeyLen);
3306             CipherAlg = CIPHER_WEP64;
3307             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key1_Proc::(Key1=%s and type=%s)\n", arg, "Ascii"));
3308             break;
3309         case 10: //wep 40 Hex type
3310             for(i=0; i < KeyLen; i++)
3311             {
3312                 if( !isxdigit(*(arg+i)) )
3313                     return FALSE;  //Not Hex value;
3314             }
3315             pAdapter->SharedKey[BSS0][0].KeyLen = KeyLen / 2 ;
3316             AtoH(arg, pAdapter->SharedKey[BSS0][0].Key, KeyLen / 2);
3317             CipherAlg = CIPHER_WEP64;
3318             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key1_Proc::(Key1=%s and type=%s)\n", arg, "Hex"));
3319             break;
3320         case 13: //wep 104 Ascii type
3321             pAdapter->SharedKey[BSS0][0].KeyLen = KeyLen;
3322             memcpy(pAdapter->SharedKey[BSS0][0].Key, arg, KeyLen);
3323             CipherAlg = CIPHER_WEP128;
3324             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key1_Proc::(Key1=%s and type=%s)\n", arg, "Ascii"));
3325             break;
3326         case 26: //wep 104 Hex type
3327             for(i=0; i < KeyLen; i++)
3328             {
3329                 if( !isxdigit(*(arg+i)) )
3330                     return FALSE;  //Not Hex value;
3331             }
3332             pAdapter->SharedKey[BSS0][0].KeyLen = KeyLen / 2 ;
3333             AtoH(arg, pAdapter->SharedKey[BSS0][0].Key, KeyLen / 2);
3334             CipherAlg = CIPHER_WEP128;
3335             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key1_Proc::(Key1=%s and type=%s)\n", arg, "Hex"));
3336             break;
3337         default: //Invalid argument
3338             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key1_Proc::Invalid argument (=%s)\n", arg));
3339             return FALSE;
3340     }
3341
3342     pAdapter->SharedKey[BSS0][0].CipherAlg = CipherAlg;
3343
3344     // Set keys (into ASIC)
3345     if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
3346         ;   // not support
3347     else    // Old WEP stuff
3348     {
3349         AsicAddSharedKeyEntry(pAdapter,
3350                               0,
3351                               0,
3352                               pAdapter->SharedKey[BSS0][0].CipherAlg,
3353                               pAdapter->SharedKey[BSS0][0].Key,
3354                               NULL,
3355                               NULL);
3356     }
3357
3358     return TRUE;
3359 }
3360 /*
3361     ==========================================================================
3362
3363     Description:
3364         Set WEP KEY2
3365     Return:
3366         TRUE if all parameters are OK, FALSE otherwise
3367     ==========================================================================
3368 */
3369 INT Set_Key2_Proc(
3370     IN  PRTMP_ADAPTER   pAdapter,
3371     IN  PUCHAR          arg)
3372 {
3373     int                                 KeyLen;
3374     int                                 i;
3375     UCHAR                               CipherAlg=CIPHER_WEP64;
3376
3377     if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
3378         return TRUE;    // do nothing
3379
3380     KeyLen = strlen(arg);
3381
3382     switch (KeyLen)
3383     {
3384         case 5: //wep 40 Ascii type
3385             pAdapter->SharedKey[BSS0][1].KeyLen = KeyLen;
3386             memcpy(pAdapter->SharedKey[BSS0][1].Key, arg, KeyLen);
3387             CipherAlg = CIPHER_WEP64;
3388             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key2_Proc::(Key2=%s and type=%s)\n", arg, "Ascii"));
3389             break;
3390         case 10: //wep 40 Hex type
3391             for(i=0; i < KeyLen; i++)
3392             {
3393                 if( !isxdigit(*(arg+i)) )
3394                     return FALSE;  //Not Hex value;
3395             }
3396             pAdapter->SharedKey[BSS0][1].KeyLen = KeyLen / 2 ;
3397             AtoH(arg, pAdapter->SharedKey[BSS0][1].Key, KeyLen / 2);
3398             CipherAlg = CIPHER_WEP64;
3399             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key2_Proc::(Key2=%s and type=%s)\n", arg, "Hex"));
3400             break;
3401         case 13: //wep 104 Ascii type
3402             pAdapter->SharedKey[BSS0][1].KeyLen = KeyLen;
3403             memcpy(pAdapter->SharedKey[BSS0][1].Key, arg, KeyLen);
3404             CipherAlg = CIPHER_WEP128;
3405             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key2_Proc::(Key2=%s and type=%s)\n", arg, "Ascii"));
3406             break;
3407         case 26: //wep 104 Hex type
3408             for(i=0; i < KeyLen; i++)
3409             {
3410                 if( !isxdigit(*(arg+i)) )
3411                     return FALSE;  //Not Hex value;
3412             }
3413             pAdapter->SharedKey[BSS0][1].KeyLen = KeyLen / 2 ;
3414             AtoH(arg, pAdapter->SharedKey[BSS0][1].Key, KeyLen / 2);
3415             CipherAlg = CIPHER_WEP128;
3416             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key2_Proc::(Key2=%s and type=%s)\n", arg, "Hex"));
3417             break;
3418         default: //Invalid argument
3419             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key2_Proc::Invalid argument (=%s)\n", arg));
3420             return FALSE;
3421     }
3422     pAdapter->SharedKey[BSS0][1].CipherAlg = CipherAlg;
3423
3424     // Set keys (into ASIC)
3425     if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
3426         ;   // not support
3427     else    // Old WEP stuff
3428     {
3429         AsicAddSharedKeyEntry(pAdapter,
3430                               0,
3431                               1,
3432                               pAdapter->SharedKey[BSS0][1].CipherAlg,
3433                               pAdapter->SharedKey[BSS0][1].Key,
3434                               NULL,
3435                               NULL);
3436     }
3437
3438     return TRUE;
3439 }
3440 /*
3441     ==========================================================================
3442     Description:
3443         Set WEP KEY3
3444     Return:
3445         TRUE if all parameters are OK, FALSE otherwise
3446     ==========================================================================
3447 */
3448 INT Set_Key3_Proc(
3449     IN  PRTMP_ADAPTER   pAdapter,
3450     IN  PUCHAR          arg)
3451 {
3452     int                                 KeyLen;
3453     int                                 i;
3454     UCHAR                               CipherAlg=CIPHER_WEP64;
3455
3456     if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
3457         return TRUE;    // do nothing
3458
3459     KeyLen = strlen(arg);
3460
3461     switch (KeyLen)
3462     {
3463         case 5: //wep 40 Ascii type
3464             pAdapter->SharedKey[BSS0][2].KeyLen = KeyLen;
3465             memcpy(pAdapter->SharedKey[BSS0][2].Key, arg, KeyLen);
3466             CipherAlg = CIPHER_WEP64;
3467             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key3_Proc::(Key3=%s and type=Ascii)\n", arg));
3468             break;
3469         case 10: //wep 40 Hex type
3470             for(i=0; i < KeyLen; i++)
3471             {
3472                 if( !isxdigit(*(arg+i)) )
3473                     return FALSE;  //Not Hex value;
3474             }
3475             pAdapter->SharedKey[BSS0][2].KeyLen = KeyLen / 2 ;
3476             AtoH(arg, pAdapter->SharedKey[BSS0][2].Key, KeyLen / 2);
3477             CipherAlg = CIPHER_WEP64;
3478             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key3_Proc::(Key3=%s and type=Hex)\n", arg));
3479             break;
3480         case 13: //wep 104 Ascii type
3481             pAdapter->SharedKey[BSS0][2].KeyLen = KeyLen;
3482             memcpy(pAdapter->SharedKey[BSS0][2].Key, arg, KeyLen);
3483             CipherAlg = CIPHER_WEP128;
3484             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key3_Proc::(Key3=%s and type=Ascii)\n", arg));
3485             break;
3486         case 26: //wep 104 Hex type
3487             for(i=0; i < KeyLen; i++)
3488             {
3489                 if( !isxdigit(*(arg+i)) )
3490                     return FALSE;  //Not Hex value;
3491             }
3492             pAdapter->SharedKey[BSS0][2].KeyLen = KeyLen / 2 ;
3493             AtoH(arg, pAdapter->SharedKey[BSS0][2].Key, KeyLen / 2);
3494             CipherAlg = CIPHER_WEP128;
3495             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key3_Proc::(Key3=%s and type=Hex)\n", arg));
3496             break;
3497         default: //Invalid argument
3498             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key3_Proc::Invalid argument (=%s)\n", arg));
3499             return FALSE;
3500     }
3501     pAdapter->SharedKey[BSS0][2].CipherAlg = CipherAlg;
3502
3503     // Set keys (into ASIC)
3504     if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
3505         ;   // not support
3506     else    // Old WEP stuff
3507     {
3508         AsicAddSharedKeyEntry(pAdapter,
3509                               0,
3510                               2,
3511                               pAdapter->SharedKey[BSS0][2].CipherAlg,
3512                               pAdapter->SharedKey[BSS0][2].Key,
3513                               NULL,
3514                               NULL);
3515     }
3516
3517     return TRUE;
3518 }
3519 /*
3520     ==========================================================================
3521     Description:
3522         Set WEP KEY4
3523     Return:
3524         TRUE if all parameters are OK, FALSE otherwise
3525     ==========================================================================
3526 */
3527 INT Set_Key4_Proc(
3528     IN  PRTMP_ADAPTER   pAdapter,
3529     IN  PUCHAR          arg)
3530 {
3531     int                                 KeyLen;
3532     int                                 i;
3533     UCHAR                               CipherAlg=CIPHER_WEP64;
3534
3535     if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
3536         return TRUE;    // do nothing
3537
3538     KeyLen = strlen(arg);
3539
3540     switch (KeyLen)
3541     {
3542         case 5: //wep 40 Ascii type
3543             pAdapter->SharedKey[BSS0][3].KeyLen = KeyLen;
3544             memcpy(pAdapter->SharedKey[BSS0][3].Key, arg, KeyLen);
3545             CipherAlg = CIPHER_WEP64;
3546             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key4_Proc::(Key4=%s and type=%s)\n", arg, "Ascii"));
3547             break;
3548         case 10: //wep 40 Hex type
3549             for(i=0; i < KeyLen; i++)
3550             {
3551                 if( !isxdigit(*(arg+i)) )
3552                     return FALSE;  //Not Hex value;
3553             }
3554             pAdapter->SharedKey[BSS0][3].KeyLen = KeyLen / 2 ;
3555             AtoH(arg, pAdapter->SharedKey[BSS0][3].Key, KeyLen / 2);
3556             CipherAlg = CIPHER_WEP64;
3557             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key4_Proc::(Key4=%s and type=%s)\n", arg, "Hex"));
3558             break;
3559         case 13: //wep 104 Ascii type
3560             pAdapter->SharedKey[BSS0][3].KeyLen = KeyLen;
3561             memcpy(pAdapter->SharedKey[BSS0][3].Key, arg, KeyLen);
3562             CipherAlg = CIPHER_WEP128;
3563             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key4_Proc::(Key4=%s and type=%s)\n", arg, "Ascii"));
3564             break;
3565         case 26: //wep 104 Hex type
3566             for(i=0; i < KeyLen; i++)
3567             {
3568                 if( !isxdigit(*(arg+i)) )
3569                     return FALSE;  //Not Hex value;
3570             }
3571             pAdapter->SharedKey[BSS0][3].KeyLen = KeyLen / 2 ;
3572             AtoH(arg, pAdapter->SharedKey[BSS0][3].Key, KeyLen / 2);
3573             CipherAlg = CIPHER_WEP128;
3574             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key4_Proc::(Key4=%s and type=%s)\n", arg, "Hex"));
3575             break;
3576         default: //Invalid argument
3577             DBGPRINT(RT_DEBUG_TRACE, ("Set_Key4_Proc::Invalid argument (=%s)\n", arg));
3578             return FALSE;
3579     }
3580     pAdapter->SharedKey[BSS0][3].CipherAlg = CipherAlg;
3581
3582     // Set keys (into ASIC)
3583     if (pAdapter->StaCfg.AuthMode >= Ndis802_11AuthModeWPA)
3584         ;   // not support
3585     else    // Old WEP stuff
3586     {
3587         AsicAddSharedKeyEntry(pAdapter,
3588                               0,
3589                               3,
3590                               pAdapter->SharedKey[BSS0][3].CipherAlg,
3591                               pAdapter->SharedKey[BSS0][3].Key,
3592                               NULL,
3593                               NULL);
3594     }
3595
3596     return TRUE;
3597 }
3598
3599 /*
3600     ==========================================================================
3601     Description:
3602         Set WPA PSK key
3603     Return:
3604         TRUE if all parameters are OK, FALSE otherwise
3605     ==========================================================================
3606 */
3607 INT Set_WPAPSK_Proc(
3608     IN  PRTMP_ADAPTER   pAdapter,
3609     IN  PUCHAR          arg)
3610 {
3611     UCHAR                   keyMaterial[40];
3612
3613     if ((pAdapter->StaCfg.AuthMode != Ndis802_11AuthModeWPAPSK) &&
3614         (pAdapter->StaCfg.AuthMode != Ndis802_11AuthModeWPA2PSK) &&
3615             (pAdapter->StaCfg.AuthMode != Ndis802_11AuthModeWPANone)
3616                 )
3617         return TRUE;    // do nothing
3618
3619     DBGPRINT(RT_DEBUG_TRACE, ("Set_WPAPSK_Proc::(WPAPSK=%s)\n", arg));
3620
3621     NdisZeroMemory(keyMaterial, 40);
3622
3623     if ((strlen(arg) < 8) || (strlen(arg) > 64))
3624     {
3625         DBGPRINT(RT_DEBUG_TRACE, ("Set failed!!(WPAPSK=%s), WPAPSK key-string required 8 ~ 64 characters \n", arg));
3626         return FALSE;
3627     }
3628
3629     if (strlen(arg) == 64)
3630     {
3631         AtoH(arg, keyMaterial, 32);
3632         NdisMoveMemory(pAdapter->StaCfg.PMK, keyMaterial, 32);
3633
3634     }
3635     else
3636     {
3637         PasswordHash((char *)arg, pAdapter->MlmeAux.Ssid, pAdapter->MlmeAux.SsidLen, keyMaterial);
3638         NdisMoveMemory(pAdapter->StaCfg.PMK, keyMaterial, 32);
3639     }
3640
3641
3642
3643     if(pAdapter->StaCfg.BssType == BSS_ADHOC &&
3644        pAdapter->StaCfg.AuthMode == Ndis802_11AuthModeWPANone)
3645     {
3646          pAdapter->StaCfg.WpaState = SS_NOTUSE;
3647     }
3648     else
3649     {
3650         // Start STA supplicant state machine
3651         pAdapter->StaCfg.WpaState = SS_START;
3652     }
3653
3654     return TRUE;
3655 }
3656
3657 /*
3658     ==========================================================================
3659     Description:
3660         Set Power Saving mode
3661     Return:
3662         TRUE if all parameters are OK, FALSE otherwise
3663     ==========================================================================
3664 */
3665 INT Set_PSMode_Proc(
3666     IN  PRTMP_ADAPTER   pAdapter,
3667     IN  PUCHAR          arg)
3668 {
3669     if (pAdapter->StaCfg.BssType == BSS_INFRA)
3670     {
3671         if ((strcmp(arg, "Max_PSP") == 0) ||
3672                         (strcmp(arg, "max_psp") == 0) ||
3673                         (strcmp(arg, "MAX_PSP") == 0))
3674         {
3675             // do NOT turn on PSM bit here, wait until MlmeCheckForPsmChange()
3676             // to exclude certain situations.
3677             if (pAdapter->StaCfg.bWindowsACCAMEnable == FALSE)
3678                 pAdapter->StaCfg.WindowsPowerMode = Ndis802_11PowerModeMAX_PSP;
3679             pAdapter->StaCfg.WindowsBatteryPowerMode = Ndis802_11PowerModeMAX_PSP;
3680             OPSTATUS_SET_FLAG(pAdapter, fOP_STATUS_RECEIVE_DTIM);
3681             pAdapter->StaCfg.DefaultListenCount = 5;
3682
3683         }
3684         else if ((strcmp(arg, "Fast_PSP") == 0) ||
3685                                  (strcmp(arg, "fast_psp") == 0) ||
3686                  (strcmp(arg, "FAST_PSP") == 0))
3687         {
3688             // do NOT turn on PSM bit here, wait until MlmeCheckForPsmChange()
3689             // to exclude certain situations.
3690             OPSTATUS_SET_FLAG(pAdapter, fOP_STATUS_RECEIVE_DTIM);
3691             if (pAdapter->StaCfg.bWindowsACCAMEnable == FALSE)
3692                 pAdapter->StaCfg.WindowsPowerMode = Ndis802_11PowerModeFast_PSP;
3693             pAdapter->StaCfg.WindowsBatteryPowerMode = Ndis802_11PowerModeFast_PSP;
3694             pAdapter->StaCfg.DefaultListenCount = 3;
3695         }
3696         else if ((strcmp(arg, "Legacy_PSP") == 0) ||
3697                  (strcmp(arg, "legacy_psp") == 0) ||
3698                  (strcmp(arg, "LEGACY_PSP") == 0))
3699         {
3700             // do NOT turn on PSM bit here, wait until MlmeCheckForPsmChange()
3701             // to exclude certain situations.
3702             OPSTATUS_SET_FLAG(pAdapter, fOP_STATUS_RECEIVE_DTIM);
3703             if (pAdapter->StaCfg.bWindowsACCAMEnable == FALSE)
3704                 pAdapter->StaCfg.WindowsPowerMode = Ndis802_11PowerModeLegacy_PSP;
3705             pAdapter->StaCfg.WindowsBatteryPowerMode = Ndis802_11PowerModeLegacy_PSP;
3706             pAdapter->StaCfg.DefaultListenCount = 3;
3707         }
3708         else
3709         {
3710             //Default Ndis802_11PowerModeCAM
3711             // clear PSM bit immediately
3712             MlmeSetPsmBit(pAdapter, PWR_ACTIVE);
3713             OPSTATUS_SET_FLAG(pAdapter, fOP_STATUS_RECEIVE_DTIM);
3714             if (pAdapter->StaCfg.bWindowsACCAMEnable == FALSE)
3715                 pAdapter->StaCfg.WindowsPowerMode = Ndis802_11PowerModeCAM;
3716             pAdapter->StaCfg.WindowsBatteryPowerMode = Ndis802_11PowerModeCAM;
3717         }
3718
3719         DBGPRINT(RT_DEBUG_TRACE, ("Set_PSMode_Proc::(PSMode=%ld)\n", pAdapter->StaCfg.WindowsPowerMode));
3720     }
3721     else
3722         return FALSE;
3723
3724
3725     return TRUE;
3726 }
3727
3728 /*
3729     ==========================================================================
3730     Description:
3731         Set WpaSupport flag.
3732     Value:
3733         0: Driver ignore wpa_supplicant.
3734         1: wpa_supplicant initiates scanning and AP selection.
3735         2: driver takes care of scanning, AP selection, and IEEE 802.11 association parameters.
3736     Return:
3737         TRUE if all parameters are OK, FALSE otherwise
3738     ==========================================================================
3739 */
3740 INT Set_Wpa_Support(
3741     IN  PRTMP_ADAPTER   pAd,
3742         IN      PUCHAR                  arg)
3743 {
3744
3745     if ( simple_strtol(arg, 0, 10) == 0)
3746         pAd->StaCfg.WpaSupplicantUP = WPA_SUPPLICANT_DISABLE;
3747     else if ( simple_strtol(arg, 0, 10) == 1)
3748         pAd->StaCfg.WpaSupplicantUP = WPA_SUPPLICANT_ENABLE;
3749     else if ( simple_strtol(arg, 0, 10) == 2)
3750         pAd->StaCfg.WpaSupplicantUP = WPA_SUPPLICANT_ENABLE_WITH_WEB_UI;
3751     else
3752         pAd->StaCfg.WpaSupplicantUP = WPA_SUPPLICANT_DISABLE;
3753
3754     DBGPRINT(RT_DEBUG_TRACE, ("Set_Wpa_Support::(WpaSupplicantUP=%d)\n", pAd->StaCfg.WpaSupplicantUP));
3755
3756     return TRUE;
3757 }
3758
3759 INT Set_TGnWifiTest_Proc(
3760     IN  PRTMP_ADAPTER   pAd,
3761     IN  PUCHAR          arg)
3762 {
3763     if (simple_strtol(arg, 0, 10) == 0)
3764         pAd->StaCfg.bTGnWifiTest = FALSE;
3765     else
3766         pAd->StaCfg.bTGnWifiTest = TRUE;
3767
3768     DBGPRINT(RT_DEBUG_TRACE, ("IF Set_TGnWifiTest_Proc::(bTGnWifiTest=%d)\n", pAd->StaCfg.bTGnWifiTest));
3769         return TRUE;
3770 }
3771
3772 INT Set_LongRetryLimit_Proc(
3773         IN      PRTMP_ADAPTER   pAdapter,
3774         IN      PUCHAR                  arg)
3775 {
3776         TX_RTY_CFG_STRUC        tx_rty_cfg;
3777         UCHAR                           LongRetryLimit = (UCHAR)simple_strtol(arg, 0, 10);
3778
3779         RTMP_IO_READ32(pAdapter, TX_RTY_CFG, &tx_rty_cfg.word);
3780         tx_rty_cfg.field.LongRtyLimit = LongRetryLimit;
3781         RTMP_IO_WRITE32(pAdapter, TX_RTY_CFG, tx_rty_cfg.word);
3782         DBGPRINT(RT_DEBUG_TRACE, ("IF Set_LongRetryLimit_Proc::(tx_rty_cfg=0x%x)\n", tx_rty_cfg.word));
3783         return TRUE;
3784 }
3785
3786 INT Set_ShortRetryLimit_Proc(
3787         IN      PRTMP_ADAPTER   pAdapter,
3788         IN      PUCHAR                  arg)
3789 {
3790         TX_RTY_CFG_STRUC        tx_rty_cfg;
3791         UCHAR                           ShortRetryLimit = (UCHAR)simple_strtol(arg, 0, 10);
3792
3793         RTMP_IO_READ32(pAdapter, TX_RTY_CFG, &tx_rty_cfg.word);
3794         tx_rty_cfg.field.ShortRtyLimit = ShortRetryLimit;
3795         RTMP_IO_WRITE32(pAdapter, TX_RTY_CFG, tx_rty_cfg.word);
3796         DBGPRINT(RT_DEBUG_TRACE, ("IF Set_ShortRetryLimit_Proc::(tx_rty_cfg=0x%x)\n", tx_rty_cfg.word));
3797         return TRUE;
3798 }