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