]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/staging/rt2860/sta_ioctl.c
d542f4f5f6b5d26ff197ad380c8fa1db171502fa
[net-next-2.6.git] / 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 unsigned long 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 u8 CipherWpa2Template[];
53
54 typedef struct PACKED _RT_VERSION_INFO {
55         u8 DriverVersionW;
56         u8 DriverVersionX;
57         u8 DriverVersionY;
58         u8 DriverVersionZ;
59         u32 DriverBuildYear;
60         u32 DriverBuildMonth;
61         u32 DriverBuildDay;
62 } RT_VERSION_INFO, *PRT_VERSION_INFO;
63
64 static __s32 ralinkrate[] = { 2, 4, 11, 22,     /* CCK */
65         12, 18, 24, 36, 48, 72, 96, 108,        /* OFDM */
66         13, 26, 39, 52, 78, 104, 117, 130, 26, 52, 78, 104, 156, 208, 234, 260, /* 20MHz, 800ns GI, MCS: 0 ~ 15 */
67         39, 78, 117, 156, 234, 312, 351, 390,   /* 20MHz, 800ns GI, MCS: 16 ~ 23 */
68         27, 54, 81, 108, 162, 216, 243, 270, 54, 108, 162, 216, 324, 432, 486, 540,     /* 40MHz, 800ns GI, MCS: 0 ~ 15 */
69         81, 162, 243, 324, 486, 648, 729, 810,  /* 40MHz, 800ns GI, MCS: 16 ~ 23 */
70         14, 29, 43, 57, 87, 115, 130, 144, 29, 59, 87, 115, 173, 230, 260, 288, /* 20MHz, 400ns GI, MCS: 0 ~ 15 */
71         43, 87, 130, 173, 260, 317, 390, 433,   /* 20MHz, 400ns GI, MCS: 16 ~ 23 */
72         30, 60, 90, 120, 180, 240, 270, 300, 60, 120, 180, 240, 360, 480, 540, 600,     /* 40MHz, 400ns GI, MCS: 0 ~ 15 */
73         90, 180, 270, 360, 540, 720, 810, 900
74 };
75
76 int Set_SSID_Proc(IN PRTMP_ADAPTER pAdapter, char *arg);
77
78 int Set_NetworkType_Proc(IN PRTMP_ADAPTER pAdapter, char *arg);
79
80 void RTMPAddKey(IN PRTMP_ADAPTER pAd, IN PNDIS_802_11_KEY pKey)
81 {
82         unsigned long KeyIdx;
83         MAC_TABLE_ENTRY *pEntry;
84
85         DBGPRINT(RT_DEBUG_TRACE, ("RTMPAddKey ------>\n"));
86
87         if (pAd->StaCfg.AuthMode >= Ndis802_11AuthModeWPA) {
88                 if (pKey->KeyIndex & 0x80000000) {
89                         if (pAd->StaCfg.AuthMode == Ndis802_11AuthModeWPANone) {
90                                 NdisZeroMemory(pAd->StaCfg.PMK, 32);
91                                 NdisMoveMemory(pAd->StaCfg.PMK,
92                                                pKey->KeyMaterial,
93                                                pKey->KeyLength);
94                                 goto end;
95                         }
96                         /* Update PTK */
97                         NdisZeroMemory(&pAd->SharedKey[BSS0][0],
98                                        sizeof(CIPHER_KEY));
99                         pAd->SharedKey[BSS0][0].KeyLen = LEN_TKIP_EK;
100                         NdisMoveMemory(pAd->SharedKey[BSS0][0].Key,
101                                        pKey->KeyMaterial, LEN_TKIP_EK);
102
103                         if (pAd->StaCfg.PairCipher ==
104                             Ndis802_11Encryption2Enabled) {
105                                 NdisMoveMemory(pAd->SharedKey[BSS0][0].RxMic,
106                                                pKey->KeyMaterial + LEN_TKIP_EK,
107                                                LEN_TKIP_TXMICK);
108                                 NdisMoveMemory(pAd->SharedKey[BSS0][0].TxMic,
109                                                pKey->KeyMaterial + LEN_TKIP_EK +
110                                                LEN_TKIP_TXMICK,
111                                                LEN_TKIP_RXMICK);
112                         } else {
113                                 NdisMoveMemory(pAd->SharedKey[BSS0][0].TxMic,
114                                                pKey->KeyMaterial + LEN_TKIP_EK,
115                                                LEN_TKIP_TXMICK);
116                                 NdisMoveMemory(pAd->SharedKey[BSS0][0].RxMic,
117                                                pKey->KeyMaterial + LEN_TKIP_EK +
118                                                LEN_TKIP_TXMICK,
119                                                LEN_TKIP_RXMICK);
120                         }
121
122                         /* Decide its ChiperAlg */
123                         if (pAd->StaCfg.PairCipher ==
124                             Ndis802_11Encryption2Enabled)
125                                 pAd->SharedKey[BSS0][0].CipherAlg = CIPHER_TKIP;
126                         else if (pAd->StaCfg.PairCipher ==
127                                  Ndis802_11Encryption3Enabled)
128                                 pAd->SharedKey[BSS0][0].CipherAlg = CIPHER_AES;
129                         else
130                                 pAd->SharedKey[BSS0][0].CipherAlg = CIPHER_NONE;
131
132                         /* Update these related information to MAC_TABLE_ENTRY */
133                         pEntry = &pAd->MacTab.Content[BSSID_WCID];
134                         NdisMoveMemory(pEntry->PairwiseKey.Key,
135                                        pAd->SharedKey[BSS0][0].Key,
136                                        LEN_TKIP_EK);
137                         NdisMoveMemory(pEntry->PairwiseKey.RxMic,
138                                        pAd->SharedKey[BSS0][0].RxMic,
139                                        LEN_TKIP_RXMICK);
140                         NdisMoveMemory(pEntry->PairwiseKey.TxMic,
141                                        pAd->SharedKey[BSS0][0].TxMic,
142                                        LEN_TKIP_TXMICK);
143                         pEntry->PairwiseKey.CipherAlg =
144                             pAd->SharedKey[BSS0][0].CipherAlg;
145
146                         /* Update pairwise key information to ASIC Shared Key Table */
147                         AsicAddSharedKeyEntry(pAd,
148                                               BSS0,
149                                               0,
150                                               pAd->SharedKey[BSS0][0].CipherAlg,
151                                               pAd->SharedKey[BSS0][0].Key,
152                                               pAd->SharedKey[BSS0][0].TxMic,
153                                               pAd->SharedKey[BSS0][0].RxMic);
154
155                         /* Update ASIC WCID attribute table and IVEIV table */
156                         RTMPAddWcidAttributeEntry(pAd,
157                                                   BSS0,
158                                                   0,
159                                                   pAd->SharedKey[BSS0][0].
160                                                   CipherAlg, pEntry);
161
162                         if (pAd->StaCfg.AuthMode >= Ndis802_11AuthModeWPA2) {
163                                 /* set 802.1x port control */
164                                 /*pAd->StaCfg.PortSecured = WPA_802_1X_PORT_SECURED; */
165                                 STA_PORT_SECURED(pAd);
166
167                                 /* Indicate Connected for GUI */
168                                 pAd->IndicateMediaState =
169                                     NdisMediaStateConnected;
170                         }
171                 } else {
172                         /* Update GTK */
173                         pAd->StaCfg.DefaultKeyId = (pKey->KeyIndex & 0xFF);
174                         NdisZeroMemory(&pAd->
175                                        SharedKey[BSS0][pAd->StaCfg.
176                                                        DefaultKeyId],
177                                        sizeof(CIPHER_KEY));
178                         pAd->SharedKey[BSS0][pAd->StaCfg.DefaultKeyId].KeyLen =
179                             LEN_TKIP_EK;
180                         NdisMoveMemory(pAd->
181                                        SharedKey[BSS0][pAd->StaCfg.
182                                                        DefaultKeyId].Key,
183                                        pKey->KeyMaterial, LEN_TKIP_EK);
184
185                         if (pAd->StaCfg.GroupCipher ==
186                             Ndis802_11Encryption2Enabled) {
187                                 NdisMoveMemory(pAd->
188                                                SharedKey[BSS0][pAd->StaCfg.
189                                                                DefaultKeyId].
190                                                RxMic,
191                                                pKey->KeyMaterial + LEN_TKIP_EK,
192                                                LEN_TKIP_TXMICK);
193                                 NdisMoveMemory(pAd->
194                                                SharedKey[BSS0][pAd->StaCfg.
195                                                                DefaultKeyId].
196                                                TxMic,
197                                                pKey->KeyMaterial + LEN_TKIP_EK +
198                                                LEN_TKIP_TXMICK,
199                                                LEN_TKIP_RXMICK);
200                         } else {
201                                 NdisMoveMemory(pAd->
202                                                SharedKey[BSS0][pAd->StaCfg.
203                                                                DefaultKeyId].
204                                                TxMic,
205                                                pKey->KeyMaterial + LEN_TKIP_EK,
206                                                LEN_TKIP_TXMICK);
207                                 NdisMoveMemory(pAd->
208                                                SharedKey[BSS0][pAd->StaCfg.
209                                                                DefaultKeyId].
210                                                RxMic,
211                                                pKey->KeyMaterial + LEN_TKIP_EK +
212                                                LEN_TKIP_TXMICK,
213                                                LEN_TKIP_RXMICK);
214                         }
215
216                         /* Update Shared Key CipherAlg */
217                         pAd->SharedKey[BSS0][pAd->StaCfg.DefaultKeyId].
218                             CipherAlg = CIPHER_NONE;
219                         if (pAd->StaCfg.GroupCipher ==
220                             Ndis802_11Encryption2Enabled)
221                                 pAd->SharedKey[BSS0][pAd->StaCfg.DefaultKeyId].
222                                     CipherAlg = CIPHER_TKIP;
223                         else if (pAd->StaCfg.GroupCipher ==
224                                  Ndis802_11Encryption3Enabled)
225                                 pAd->SharedKey[BSS0][pAd->StaCfg.DefaultKeyId].
226                                     CipherAlg = CIPHER_AES;
227
228                         /* Update group key information to ASIC Shared Key Table */
229                         AsicAddSharedKeyEntry(pAd,
230                                               BSS0,
231                                               pAd->StaCfg.DefaultKeyId,
232                                               pAd->SharedKey[BSS0][pAd->StaCfg.
233                                                                    DefaultKeyId].
234                                               CipherAlg,
235                                               pAd->SharedKey[BSS0][pAd->StaCfg.
236                                                                    DefaultKeyId].
237                                               Key,
238                                               pAd->SharedKey[BSS0][pAd->StaCfg.
239                                                                    DefaultKeyId].
240                                               TxMic,
241                                               pAd->SharedKey[BSS0][pAd->StaCfg.
242                                                                    DefaultKeyId].
243                                               RxMic);
244
245                         /* Update ASIC WCID attribute table and IVEIV table */
246                         RTMPAddWcidAttributeEntry(pAd,
247                                                   BSS0,
248                                                   pAd->StaCfg.DefaultKeyId,
249                                                   pAd->SharedKey[BSS0][pAd->
250                                                                        StaCfg.
251                                                                        DefaultKeyId].
252                                                   CipherAlg, NULL);
253
254                         /* set 802.1x port control */
255                         /*pAd->StaCfg.PortSecured = WPA_802_1X_PORT_SECURED; */
256                         STA_PORT_SECURED(pAd);
257
258                         /* Indicate Connected for GUI */
259                         pAd->IndicateMediaState = NdisMediaStateConnected;
260                 }
261         } else                  /* dynamic WEP from wpa_supplicant */
262         {
263                 u8 CipherAlg;
264                 u8 *Key;
265
266                 if (pKey->KeyLength == 32)
267                         goto end;
268
269                 KeyIdx = pKey->KeyIndex & 0x0fffffff;
270
271                 if (KeyIdx < 4) {
272                         /* it is a default shared key, for Pairwise key setting */
273                         if (pKey->KeyIndex & 0x80000000) {
274                                 pEntry = MacTableLookup(pAd, pKey->BSSID);
275
276                                 if (pEntry) {
277                                         DBGPRINT(RT_DEBUG_TRACE,
278                                                  ("RTMPAddKey: Set Pair-wise Key\n"));
279
280                                         /* set key material and key length */
281                                         pEntry->PairwiseKey.KeyLen =
282                                             (u8)pKey->KeyLength;
283                                         NdisMoveMemory(pEntry->PairwiseKey.Key,
284                                                        &pKey->KeyMaterial,
285                                                        pKey->KeyLength);
286
287                                         /* set Cipher type */
288                                         if (pKey->KeyLength == 5)
289                                                 pEntry->PairwiseKey.CipherAlg =
290                                                     CIPHER_WEP64;
291                                         else
292                                                 pEntry->PairwiseKey.CipherAlg =
293                                                     CIPHER_WEP128;
294
295                                         /* Add Pair-wise key to Asic */
296                                         AsicAddPairwiseKeyEntry(pAd,
297                                                                 pEntry->Addr,
298                                                                 (u8)pEntry->
299                                                                 Aid,
300                                                                 &pEntry->
301                                                                 PairwiseKey);
302
303                                         /* update WCID attribute table and IVEIV table for this entry */
304                                         RTMPAddWcidAttributeEntry(pAd, BSS0, KeyIdx,    /* The value may be not zero */
305                                                                   pEntry->
306                                                                   PairwiseKey.
307                                                                   CipherAlg,
308                                                                   pEntry);
309
310                                 }
311                         } else {
312                                 /* Default key for tx (shared key) */
313                                 pAd->StaCfg.DefaultKeyId = (u8)KeyIdx;
314
315                                 /* set key material and key length */
316                                 pAd->SharedKey[BSS0][KeyIdx].KeyLen =
317                                     (u8)pKey->KeyLength;
318                                 NdisMoveMemory(pAd->SharedKey[BSS0][KeyIdx].Key,
319                                                &pKey->KeyMaterial,
320                                                pKey->KeyLength);
321
322                                 /* Set Ciper type */
323                                 if (pKey->KeyLength == 5)
324                                         pAd->SharedKey[BSS0][KeyIdx].CipherAlg =
325                                             CIPHER_WEP64;
326                                 else
327                                         pAd->SharedKey[BSS0][KeyIdx].CipherAlg =
328                                             CIPHER_WEP128;
329
330                                 CipherAlg =
331                                     pAd->SharedKey[BSS0][KeyIdx].CipherAlg;
332                                 Key = pAd->SharedKey[BSS0][KeyIdx].Key;
333
334                                 /* Set Group key material to Asic */
335                                 AsicAddSharedKeyEntry(pAd, BSS0, KeyIdx,
336                                                       CipherAlg, Key, NULL,
337                                                       NULL);
338
339                                 /* Update WCID attribute table and IVEIV table for this group key table */
340                                 RTMPAddWcidAttributeEntry(pAd, BSS0, KeyIdx,
341                                                           CipherAlg, NULL);
342
343                         }
344                 }
345         }
346 end:
347         return;
348 }
349
350 char *rtstrchr(const char *s, int c)
351 {
352         for (; *s != (char)c; ++s)
353                 if (*s == '\0')
354                         return NULL;
355         return (char *)s;
356 }
357
358 /*
359 This is required for LinEX2004/kernel2.6.7 to provide iwlist scanning function
360 */
361
362 int
363 rt_ioctl_giwname(struct net_device *dev,
364                  struct iw_request_info *info, char *name, char *extra)
365 {
366         strncpy(name, "Ralink STA", IFNAMSIZ);
367         /* RT2870 2.1.0.0 uses "RT2870 Wireless" */
368         /* RT3090 2.1.0.0 uses "RT2860 Wireless" */
369         return 0;
370 }
371
372 int rt_ioctl_siwfreq(struct net_device *dev,
373                      struct iw_request_info *info,
374                      struct iw_freq *freq, char *extra)
375 {
376         PRTMP_ADAPTER pAdapter = NULL;
377         int chan = -1;
378
379         GET_PAD_FROM_NET_DEV(pAdapter, dev);
380
381         /*check if the interface is down */
382         if (!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE)) {
383                 DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
384                 return -ENETDOWN;
385         }
386
387         if (freq->e > 1)
388                 return -EINVAL;
389
390         if ((freq->e == 0) && (freq->m <= 1000))
391                 chan = freq->m; /* Setting by channel number */
392         else
393                 MAP_KHZ_TO_CHANNEL_ID((freq->m / 100), chan);   /* Setting by frequency - search the table , like 2.412G, 2.422G, */
394
395         if (ChannelSanity(pAdapter, chan) == TRUE) {
396                 pAdapter->CommonCfg.Channel = chan;
397                 DBGPRINT(RT_DEBUG_ERROR,
398                          ("==>rt_ioctl_siwfreq::SIOCSIWFREQ[cmd=0x%x] (Channel=%d)\n",
399                           SIOCSIWFREQ, pAdapter->CommonCfg.Channel));
400         } else
401                 return -EINVAL;
402
403         return 0;
404 }
405
406 int rt_ioctl_giwfreq(struct net_device *dev,
407                      struct iw_request_info *info,
408                      struct iw_freq *freq, char *extra)
409 {
410         PRTMP_ADAPTER pAdapter = NULL;
411         u8 ch;
412         unsigned long m = 2412000;
413
414         GET_PAD_FROM_NET_DEV(pAdapter, dev);
415
416         ch = pAdapter->CommonCfg.Channel;
417
418         DBGPRINT(RT_DEBUG_TRACE, ("==>rt_ioctl_giwfreq  %d\n", ch));
419
420         MAP_CHANNEL_ID_TO_KHZ(ch, m);
421         freq->m = m * 100;
422         freq->e = 1;
423         return 0;
424 }
425
426 int rt_ioctl_siwmode(struct net_device *dev,
427                      struct iw_request_info *info, __u32 * mode, char *extra)
428 {
429         PRTMP_ADAPTER pAdapter = NULL;
430
431         GET_PAD_FROM_NET_DEV(pAdapter, dev);
432
433         /*check if the interface is down */
434         if (!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE)) {
435                 DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
436                 return -ENETDOWN;
437         }
438
439         switch (*mode) {
440         case IW_MODE_ADHOC:
441                 Set_NetworkType_Proc(pAdapter, "Adhoc");
442                 break;
443         case IW_MODE_INFRA:
444                 Set_NetworkType_Proc(pAdapter, "Infra");
445                 break;
446         case IW_MODE_MONITOR:
447                 Set_NetworkType_Proc(pAdapter, "Monitor");
448                 break;
449         default:
450                 DBGPRINT(RT_DEBUG_TRACE,
451                          ("===>rt_ioctl_siwmode::SIOCSIWMODE (unknown %d)\n",
452                           *mode));
453                 return -EINVAL;
454         }
455
456         /* Reset Ralink supplicant to not use, it will be set to start when UI set PMK key */
457         pAdapter->StaCfg.WpaState = SS_NOTUSE;
458
459         return 0;
460 }
461
462 int rt_ioctl_giwmode(struct net_device *dev,
463                      struct iw_request_info *info, __u32 * mode, char *extra)
464 {
465         PRTMP_ADAPTER pAdapter = NULL;
466
467         GET_PAD_FROM_NET_DEV(pAdapter, dev);
468
469         if (ADHOC_ON(pAdapter))
470                 *mode = IW_MODE_ADHOC;
471         else if (INFRA_ON(pAdapter))
472                 *mode = IW_MODE_INFRA;
473         else if (MONITOR_ON(pAdapter)) {
474                 *mode = IW_MODE_MONITOR;
475         } else
476                 *mode = IW_MODE_AUTO;
477
478         DBGPRINT(RT_DEBUG_TRACE, ("==>rt_ioctl_giwmode(mode=%d)\n", *mode));
479         return 0;
480 }
481
482 int rt_ioctl_siwsens(struct net_device *dev,
483                      struct iw_request_info *info, char *name, char *extra)
484 {
485         PRTMP_ADAPTER pAdapter = NULL;
486
487         GET_PAD_FROM_NET_DEV(pAdapter, dev);
488
489         /*check if the interface is down */
490         if (!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE)) {
491                 DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
492                 return -ENETDOWN;
493         }
494
495         return 0;
496 }
497
498 int rt_ioctl_giwsens(struct net_device *dev,
499                      struct iw_request_info *info, char *name, char *extra)
500 {
501         return 0;
502 }
503
504 int rt_ioctl_giwrange(struct net_device *dev,
505                       struct iw_request_info *info,
506                       struct iw_point *data, char *extra)
507 {
508         PRTMP_ADAPTER pAdapter = NULL;
509         struct iw_range *range = (struct iw_range *)extra;
510         u16 val;
511         int i;
512
513         GET_PAD_FROM_NET_DEV(pAdapter, dev);
514
515         DBGPRINT(RT_DEBUG_TRACE, ("===>rt_ioctl_giwrange\n"));
516         data->length = sizeof(struct iw_range);
517         memset(range, 0, sizeof(struct iw_range));
518
519         range->txpower_capa = IW_TXPOW_DBM;
520
521         if (INFRA_ON(pAdapter) || ADHOC_ON(pAdapter)) {
522                 range->min_pmp = 1 * 1024;
523                 range->max_pmp = 65535 * 1024;
524                 range->min_pmt = 1 * 1024;
525                 range->max_pmt = 1000 * 1024;
526                 range->pmp_flags = IW_POWER_PERIOD;
527                 range->pmt_flags = IW_POWER_TIMEOUT;
528                 range->pm_capa = IW_POWER_PERIOD | IW_POWER_TIMEOUT |
529                     IW_POWER_UNICAST_R | IW_POWER_ALL_R;
530         }
531
532         range->we_version_compiled = WIRELESS_EXT;
533         range->we_version_source = 14;
534
535         range->retry_capa = IW_RETRY_LIMIT;
536         range->retry_flags = IW_RETRY_LIMIT;
537         range->min_retry = 0;
538         range->max_retry = 255;
539
540         range->num_channels = pAdapter->ChannelListNum;
541
542         val = 0;
543         for (i = 1; i <= range->num_channels; i++) {
544                 u32 m = 2412000;
545                 range->freq[val].i = pAdapter->ChannelList[i - 1].Channel;
546                 MAP_CHANNEL_ID_TO_KHZ(pAdapter->ChannelList[i - 1].Channel, m);
547                 range->freq[val].m = m * 100;   /* OS_HZ */
548
549                 range->freq[val].e = 1;
550                 val++;
551                 if (val == IW_MAX_FREQUENCIES)
552                         break;
553         }
554         range->num_frequency = val;
555
556         range->max_qual.qual = 100;     /* what is correct max? This was not
557                                          * documented exactly. At least
558                                          * 69 has been observed. */
559         range->max_qual.level = 0;      /* dB */
560         range->max_qual.noise = 0;      /* dB */
561
562         /* What would be suitable values for "average/typical" qual? */
563         range->avg_qual.qual = 20;
564         range->avg_qual.level = -60;
565         range->avg_qual.noise = -95;
566         range->sensitivity = 3;
567
568         range->max_encoding_tokens = NR_WEP_KEYS;
569         range->num_encoding_sizes = 2;
570         range->encoding_size[0] = 5;
571         range->encoding_size[1] = 13;
572
573         range->min_rts = 0;
574         range->max_rts = 2347;
575         range->min_frag = 256;
576         range->max_frag = 2346;
577
578         /* IW_ENC_CAPA_* bit field */
579         range->enc_capa = IW_ENC_CAPA_WPA | IW_ENC_CAPA_WPA2 |
580             IW_ENC_CAPA_CIPHER_TKIP | IW_ENC_CAPA_CIPHER_CCMP;
581
582         return 0;
583 }
584
585 int rt_ioctl_siwap(struct net_device *dev,
586                    struct iw_request_info *info,
587                    struct sockaddr *ap_addr, char *extra)
588 {
589         PRTMP_ADAPTER pAdapter = NULL;
590         NDIS_802_11_MAC_ADDRESS Bssid;
591
592         GET_PAD_FROM_NET_DEV(pAdapter, dev);
593
594         /*check if the interface is down */
595         if (!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE)) {
596                 DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
597                 return -ENETDOWN;
598         }
599
600         if (pAdapter->Mlme.CntlMachine.CurrState != CNTL_IDLE) {
601                 RTMP_MLME_RESET_STATE_MACHINE(pAdapter);
602                 DBGPRINT(RT_DEBUG_TRACE,
603                          ("!!! MLME busy, reset MLME state machine !!!\n"));
604         }
605         /* tell CNTL state machine to call NdisMSetInformationComplete() after completing */
606         /* this request, because this request is initiated by NDIS. */
607         pAdapter->MlmeAux.CurrReqIsFromNdis = FALSE;
608         /* Prevent to connect AP again in STAMlmePeriodicExec */
609         pAdapter->MlmeAux.AutoReconnectSsidLen = 32;
610
611         memset(Bssid, 0, MAC_ADDR_LEN);
612         memcpy(Bssid, ap_addr->sa_data, MAC_ADDR_LEN);
613         MlmeEnqueue(pAdapter,
614                     MLME_CNTL_STATE_MACHINE,
615                     OID_802_11_BSSID,
616                     sizeof(NDIS_802_11_MAC_ADDRESS), (void *) & Bssid);
617
618         DBGPRINT(RT_DEBUG_TRACE,
619                  ("IOCTL::SIOCSIWAP %02x:%02x:%02x:%02x:%02x:%02x\n", Bssid[0],
620                   Bssid[1], Bssid[2], Bssid[3], Bssid[4], Bssid[5]));
621
622         return 0;
623 }
624
625 int rt_ioctl_giwap(struct net_device *dev,
626                    struct iw_request_info *info,
627                    struct sockaddr *ap_addr, char *extra)
628 {
629         PRTMP_ADAPTER pAdapter = NULL;
630
631         GET_PAD_FROM_NET_DEV(pAdapter, dev);
632
633         if (INFRA_ON(pAdapter) || ADHOC_ON(pAdapter)) {
634                 ap_addr->sa_family = ARPHRD_ETHER;
635                 memcpy(ap_addr->sa_data, &pAdapter->CommonCfg.Bssid, ETH_ALEN);
636         }
637         /* Add for RT2870 */
638         else if (pAdapter->StaCfg.WpaSupplicantUP != WPA_SUPPLICANT_DISABLE) {
639                 ap_addr->sa_family = ARPHRD_ETHER;
640                 memcpy(ap_addr->sa_data, &pAdapter->MlmeAux.Bssid, ETH_ALEN);
641         } else {
642                 DBGPRINT(RT_DEBUG_TRACE, ("IOCTL::SIOCGIWAP(=EMPTY)\n"));
643                 return -ENOTCONN;
644         }
645
646         return 0;
647 }
648
649 /*
650  * Units are in db above the noise floor. That means the
651  * rssi values reported in the tx/rx descriptors in the
652  * driver are the SNR expressed in db.
653  *
654  * If you assume that the noise floor is -95, which is an
655  * excellent assumption 99.5 % of the time, then you can
656  * derive the absolute signal level (i.e. -95 + rssi).
657  * There are some other slight factors to take into account
658  * depending on whether the rssi measurement is from 11b,
659  * 11g, or 11a.   These differences are at most 2db and
660  * can be documented.
661  *
662  * NB: various calculations are based on the orinoco/wavelan
663  *     drivers for compatibility
664  */
665 static void set_quality(PRTMP_ADAPTER pAdapter,
666                         struct iw_quality *iq, signed char rssi)
667 {
668         __u8 ChannelQuality;
669
670         /* Normalize Rssi */
671         if (rssi >= -50)
672                 ChannelQuality = 100;
673         else if (rssi >= -80)   /* between -50 ~ -80dbm */
674                 ChannelQuality = (__u8) (24 + ((rssi + 80) * 26) / 10);
675         else if (rssi >= -90)   /* between -80 ~ -90dbm */
676                 ChannelQuality = (__u8) ((rssi + 90) * 26) / 10;
677         else
678                 ChannelQuality = 0;
679
680         iq->qual = (__u8) ChannelQuality;
681
682         iq->level = (__u8) (rssi);
683         iq->noise = (pAdapter->BbpWriteLatch[66] > pAdapter->BbpTuning.FalseCcaUpperThreshold) ? ((__u8) pAdapter->BbpTuning.FalseCcaUpperThreshold) : ((__u8) pAdapter->BbpWriteLatch[66]);    /* noise level (dBm) */
684         iq->noise += 256 - 143;
685         iq->updated = pAdapter->iw_stats.qual.updated;
686 }
687
688 int rt_ioctl_iwaplist(struct net_device *dev,
689                       struct iw_request_info *info,
690                       struct iw_point *data, char *extra)
691 {
692         PRTMP_ADAPTER pAdapter = NULL;
693
694         struct sockaddr addr[IW_MAX_AP];
695         struct iw_quality qual[IW_MAX_AP];
696         int i;
697
698         GET_PAD_FROM_NET_DEV(pAdapter, dev);
699
700         /*check if the interface is down */
701         if (!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE)) {
702                 DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
703                 data->length = 0;
704                 return 0;
705                 /*return -ENETDOWN; */
706         }
707
708         for (i = 0; i < IW_MAX_AP; i++) {
709                 if (i >= pAdapter->ScanTab.BssNr)
710                         break;
711                 addr[i].sa_family = ARPHRD_ETHER;
712                 memcpy(addr[i].sa_data, &pAdapter->ScanTab.BssEntry[i].Bssid,
713                        MAC_ADDR_LEN);
714                 set_quality(pAdapter, &qual[i],
715                             pAdapter->ScanTab.BssEntry[i].Rssi);
716         }
717         data->length = i;
718         memcpy(extra, &addr, i * sizeof(addr[0]));
719         data->flags = 1;        /* signal quality present (sort of) */
720         memcpy(extra + i * sizeof(addr[0]), &qual, i * sizeof(qual[i]));
721
722         return 0;
723 }
724
725 int rt_ioctl_siwscan(struct net_device *dev,
726                      struct iw_request_info *info,
727                      struct iw_point *data, char *extra)
728 {
729         PRTMP_ADAPTER pAdapter = NULL;
730
731         unsigned long Now;
732         int Status = NDIS_STATUS_SUCCESS;
733
734         GET_PAD_FROM_NET_DEV(pAdapter, dev);
735
736         /*check if the interface is down */
737         if (!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE)) {
738                 DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
739                 return -ENETDOWN;
740         }
741
742         if (MONITOR_ON(pAdapter)) {
743                 DBGPRINT(RT_DEBUG_TRACE,
744                          ("!!! Driver is in Monitor Mode now !!!\n"));
745                 return -EINVAL;
746         }
747
748         if (pAdapter->StaCfg.WpaSupplicantUP == WPA_SUPPLICANT_ENABLE) {
749                 pAdapter->StaCfg.WpaSupplicantScanCount++;
750         }
751
752         pAdapter->StaCfg.bScanReqIsFromWebUI = TRUE;
753         if (RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_BSS_SCAN_IN_PROGRESS))
754                 return NDIS_STATUS_SUCCESS;
755         do {
756                 Now = jiffies;
757
758                 if ((pAdapter->StaCfg.WpaSupplicantUP == WPA_SUPPLICANT_ENABLE)
759                     && (pAdapter->StaCfg.WpaSupplicantScanCount > 3)) {
760                         DBGPRINT(RT_DEBUG_TRACE,
761                                  ("!!! WpaSupplicantScanCount > 3\n"));
762                         Status = NDIS_STATUS_SUCCESS;
763                         break;
764                 }
765
766                 if ((OPSTATUS_TEST_FLAG
767                      (pAdapter, fOP_STATUS_MEDIA_STATE_CONNECTED))
768                     && ((pAdapter->StaCfg.AuthMode == Ndis802_11AuthModeWPA)
769                         || (pAdapter->StaCfg.AuthMode ==
770                             Ndis802_11AuthModeWPAPSK))
771                     && (pAdapter->StaCfg.PortSecured ==
772                         WPA_802_1X_PORT_NOT_SECURED)) {
773                         DBGPRINT(RT_DEBUG_TRACE,
774                                  ("!!! Link UP, Port Not Secured! ignore this set::OID_802_11_BSSID_LIST_SCAN\n"));
775                         Status = NDIS_STATUS_SUCCESS;
776                         break;
777                 }
778
779                 if (pAdapter->Mlme.CntlMachine.CurrState != CNTL_IDLE) {
780                         RTMP_MLME_RESET_STATE_MACHINE(pAdapter);
781                         DBGPRINT(RT_DEBUG_TRACE,
782                                  ("!!! MLME busy, reset MLME state machine !!!\n"));
783                 }
784                 /* tell CNTL state machine to call NdisMSetInformationComplete() after completing */
785                 /* this request, because this request is initiated by NDIS. */
786                 pAdapter->MlmeAux.CurrReqIsFromNdis = FALSE;
787                 /* Reset allowed scan retries */
788                 pAdapter->StaCfg.ScanCnt = 0;
789                 pAdapter->StaCfg.LastScanTime = Now;
790
791                 MlmeEnqueue(pAdapter,
792                             MLME_CNTL_STATE_MACHINE,
793                             OID_802_11_BSSID_LIST_SCAN, 0, NULL);
794
795                 Status = NDIS_STATUS_SUCCESS;
796                 RTMP_MLME_HANDLER(pAdapter);
797         } while (0);
798         return NDIS_STATUS_SUCCESS;
799 }
800
801 int rt_ioctl_giwscan(struct net_device *dev,
802                      struct iw_request_info *info,
803                      struct iw_point *data, char *extra)
804 {
805         PRTMP_ADAPTER pAdapter = NULL;
806         int i = 0;
807         char *current_ev = extra, *previous_ev = extra;
808         char *end_buf;
809         char *current_val;
810         char custom[MAX_CUSTOM_LEN] = { 0 };
811         struct iw_event iwe;
812
813         GET_PAD_FROM_NET_DEV(pAdapter, dev);
814
815         if (RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_BSS_SCAN_IN_PROGRESS)) {
816                 /*
817                  * Still scanning, indicate the caller should try again.
818                  */
819                 return -EAGAIN;
820         }
821
822         if (pAdapter->StaCfg.WpaSupplicantUP == WPA_SUPPLICANT_ENABLE) {
823                 pAdapter->StaCfg.WpaSupplicantScanCount = 0;
824         }
825
826         if (pAdapter->ScanTab.BssNr == 0) {
827                 data->length = 0;
828                 return 0;
829         }
830
831         if (data->length > 0)
832                 end_buf = extra + data->length;
833         else
834                 end_buf = extra + IW_SCAN_MAX_DATA;
835
836         for (i = 0; i < pAdapter->ScanTab.BssNr; i++) {
837                 if (current_ev >= end_buf) {
838                         return -E2BIG;
839                 }
840                 /*MAC address */
841                 /*================================ */
842                 memset(&iwe, 0, sizeof(iwe));
843                 iwe.cmd = SIOCGIWAP;
844                 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
845                 memcpy(iwe.u.ap_addr.sa_data,
846                        &pAdapter->ScanTab.BssEntry[i].Bssid, ETH_ALEN);
847
848                 previous_ev = current_ev;
849                 current_ev =
850                     iwe_stream_add_event(info, current_ev, end_buf, &iwe,
851                                          IW_EV_ADDR_LEN);
852                 if (current_ev == previous_ev)
853                         return -E2BIG;
854
855                 /*
856                    Protocol:
857                    it will show scanned AP's WirelessMode .
858                    it might be
859                    802.11a
860                    802.11a/n
861                    802.11g/n
862                    802.11b/g/n
863                    802.11g
864                    802.11b/g
865                  */
866                 memset(&iwe, 0, sizeof(iwe));
867                 iwe.cmd = SIOCGIWNAME;
868
869                 {
870                         PBSS_ENTRY pBssEntry = &pAdapter->ScanTab.BssEntry[i];
871                         BOOLEAN isGonly = FALSE;
872                         int rateCnt = 0;
873
874                         if (pBssEntry->Channel > 14) {
875                                 if (pBssEntry->HtCapabilityLen != 0)
876                                         strcpy(iwe.u.name, "802.11a/n");
877                                 else
878                                         strcpy(iwe.u.name, "802.11a");
879                         } else {
880                                 /*
881                                    if one of non B mode rate is set supported rate . it mean G only.
882                                  */
883                                 for (rateCnt = 0;
884                                      rateCnt < pBssEntry->SupRateLen;
885                                      rateCnt++) {
886                                         /*
887                                            6Mbps(140) 9Mbps(146) and >=12Mbps(152) are supported rate , it mean G only.
888                                          */
889                                         if (pBssEntry->SupRate[rateCnt] == 140
890                                             || pBssEntry->SupRate[rateCnt] ==
891                                             146
892                                             || pBssEntry->SupRate[rateCnt] >=
893                                             152)
894                                                 isGonly = TRUE;
895                                 }
896
897                                 for (rateCnt = 0;
898                                      rateCnt < pBssEntry->ExtRateLen;
899                                      rateCnt++) {
900                                         if (pBssEntry->ExtRate[rateCnt] == 140
901                                             || pBssEntry->ExtRate[rateCnt] ==
902                                             146
903                                             || pBssEntry->ExtRate[rateCnt] >=
904                                             152)
905                                                 isGonly = TRUE;
906                                 }
907
908                                 if (pBssEntry->HtCapabilityLen != 0) {
909                                         if (isGonly == TRUE)
910                                                 strcpy(iwe.u.name, "802.11g/n");
911                                         else
912                                                 strcpy(iwe.u.name,
913                                                        "802.11b/g/n");
914                                 } else {
915                                         if (isGonly == TRUE)
916                                                 strcpy(iwe.u.name, "802.11g");
917                                         else {
918                                                 if (pBssEntry->SupRateLen == 4
919                                                     && pBssEntry->ExtRateLen ==
920                                                     0)
921                                                         strcpy(iwe.u.name,
922                                                                "802.11b");
923                                                 else
924                                                         strcpy(iwe.u.name,
925                                                                "802.11b/g");
926                                         }
927                                 }
928                         }
929                 }
930
931                 previous_ev = current_ev;
932                 current_ev =
933                     iwe_stream_add_event(info, current_ev, end_buf, &iwe,
934                                          IW_EV_ADDR_LEN);
935                 if (current_ev == previous_ev)
936                         return -E2BIG;
937
938                 /*ESSID */
939                 /*================================ */
940                 memset(&iwe, 0, sizeof(iwe));
941                 iwe.cmd = SIOCGIWESSID;
942                 iwe.u.data.length = pAdapter->ScanTab.BssEntry[i].SsidLen;
943                 iwe.u.data.flags = 1;
944
945                 previous_ev = current_ev;
946                 current_ev =
947                     iwe_stream_add_point(info, current_ev, end_buf, &iwe,
948                                          (char *)pAdapter->ScanTab.
949                                          BssEntry[i].Ssid);
950                 if (current_ev == previous_ev)
951                         return -E2BIG;
952
953                 /*Network Type */
954                 /*================================ */
955                 memset(&iwe, 0, sizeof(iwe));
956                 iwe.cmd = SIOCGIWMODE;
957                 if (pAdapter->ScanTab.BssEntry[i].BssType == Ndis802_11IBSS) {
958                         iwe.u.mode = IW_MODE_ADHOC;
959                 } else if (pAdapter->ScanTab.BssEntry[i].BssType ==
960                            Ndis802_11Infrastructure) {
961                         iwe.u.mode = IW_MODE_INFRA;
962                 } else {
963                         iwe.u.mode = IW_MODE_AUTO;
964                 }
965                 iwe.len = IW_EV_UINT_LEN;
966
967                 previous_ev = current_ev;
968                 current_ev =
969                     iwe_stream_add_event(info, current_ev, end_buf, &iwe,
970                                          IW_EV_UINT_LEN);
971                 if (current_ev == previous_ev)
972                         return -E2BIG;
973
974                 /*Channel and Frequency */
975                 /*================================ */
976                 memset(&iwe, 0, sizeof(iwe));
977                 iwe.cmd = SIOCGIWFREQ;
978                 if (INFRA_ON(pAdapter) || ADHOC_ON(pAdapter))
979                         iwe.u.freq.m = pAdapter->ScanTab.BssEntry[i].Channel;
980                 else
981                         iwe.u.freq.m = pAdapter->ScanTab.BssEntry[i].Channel;
982                 iwe.u.freq.e = 0;
983                 iwe.u.freq.i = 0;
984
985                 previous_ev = current_ev;
986                 current_ev =
987                     iwe_stream_add_event(info, current_ev, end_buf, &iwe,
988                                          IW_EV_FREQ_LEN);
989                 if (current_ev == previous_ev)
990                         return -E2BIG;
991
992                 /*Add quality statistics */
993                 /*================================ */
994                 memset(&iwe, 0, sizeof(iwe));
995                 iwe.cmd = IWEVQUAL;
996                 iwe.u.qual.level = 0;
997                 iwe.u.qual.noise = 0;
998                 set_quality(pAdapter, &iwe.u.qual,
999                             pAdapter->ScanTab.BssEntry[i].Rssi);
1000                 current_ev =
1001                     iwe_stream_add_event(info, current_ev, end_buf, &iwe,
1002                                          IW_EV_QUAL_LEN);
1003                 if (current_ev == previous_ev)
1004                         return -E2BIG;
1005
1006                 /*Encyption key */
1007                 /*================================ */
1008                 memset(&iwe, 0, sizeof(iwe));
1009                 iwe.cmd = SIOCGIWENCODE;
1010                 if (CAP_IS_PRIVACY_ON
1011                     (pAdapter->ScanTab.BssEntry[i].CapabilityInfo))
1012                         iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
1013                 else
1014                         iwe.u.data.flags = IW_ENCODE_DISABLED;
1015
1016                 previous_ev = current_ev;
1017                 current_ev =
1018                     iwe_stream_add_point(info, current_ev, end_buf, &iwe,
1019                                          (char *)pAdapter->
1020                                          SharedKey[BSS0][(iwe.u.data.
1021                                                           flags &
1022                                                           IW_ENCODE_INDEX) -
1023                                                          1].Key);
1024                 if (current_ev == previous_ev)
1025                         return -E2BIG;
1026
1027                 /*Bit Rate */
1028                 /*================================ */
1029                 if (pAdapter->ScanTab.BssEntry[i].SupRateLen) {
1030                         u8 tmpRate =
1031                             pAdapter->ScanTab.BssEntry[i].SupRate[pAdapter->
1032                                                                   ScanTab.
1033                                                                   BssEntry[i].
1034                                                                   SupRateLen -
1035                                                                   1];
1036                         memset(&iwe, 0, sizeof(iwe));
1037                         iwe.cmd = SIOCGIWRATE;
1038                         current_val = current_ev + IW_EV_LCP_LEN;
1039                         if (tmpRate == 0x82)
1040                                 iwe.u.bitrate.value = 1 * 1000000;
1041                         else if (tmpRate == 0x84)
1042                                 iwe.u.bitrate.value = 2 * 1000000;
1043                         else if (tmpRate == 0x8B)
1044                                 iwe.u.bitrate.value = 5.5 * 1000000;
1045                         else if (tmpRate == 0x96)
1046                                 iwe.u.bitrate.value = 11 * 1000000;
1047                         else
1048                                 iwe.u.bitrate.value = (tmpRate / 2) * 1000000;
1049
1050                         if (tmpRate == 0x6c
1051                             && pAdapter->ScanTab.BssEntry[i].HtCapabilityLen >
1052                             0) {
1053                                 int rate_count =
1054                                     sizeof(ralinkrate) / sizeof(__s32);
1055                                 HT_CAP_INFO capInfo =
1056                                     pAdapter->ScanTab.BssEntry[i].HtCapability.
1057                                     HtCapInfo;
1058                                 int shortGI =
1059                                     capInfo.ChannelWidth ? capInfo.
1060                                     ShortGIfor40 : capInfo.ShortGIfor20;
1061                                 int maxMCS =
1062                                     pAdapter->ScanTab.BssEntry[i].HtCapability.
1063                                     MCSSet[1] ? 15 : 7;
1064                                 int rate_index =
1065                                     12 + ((u8)capInfo.ChannelWidth * 24) +
1066                                     ((u8)shortGI * 48) + ((u8)maxMCS);
1067                                 if (rate_index < 0)
1068                                         rate_index = 0;
1069                                 if (rate_index > rate_count)
1070                                         rate_index = rate_count;
1071                                 iwe.u.bitrate.value =
1072                                     ralinkrate[rate_index] * 500000;
1073                         }
1074
1075                         iwe.u.bitrate.disabled = 0;
1076                         current_val = iwe_stream_add_value(info, current_ev,
1077                                                            current_val, end_buf,
1078                                                            &iwe,
1079                                                            IW_EV_PARAM_LEN);
1080
1081                         if ((current_val - current_ev) > IW_EV_LCP_LEN)
1082                                 current_ev = current_val;
1083                         else
1084                                 return -E2BIG;
1085                 }
1086                 /*WPA IE */
1087                 if (pAdapter->ScanTab.BssEntry[i].WpaIE.IELen > 0) {
1088                         memset(&iwe, 0, sizeof(iwe));
1089                         memset(&custom[0], 0, MAX_CUSTOM_LEN);
1090                         memcpy(custom,
1091                                &(pAdapter->ScanTab.BssEntry[i].WpaIE.IE[0]),
1092                                pAdapter->ScanTab.BssEntry[i].WpaIE.IELen);
1093                         iwe.cmd = IWEVGENIE;
1094                         iwe.u.data.length =
1095                             pAdapter->ScanTab.BssEntry[i].WpaIE.IELen;
1096                         current_ev =
1097                             iwe_stream_add_point(info, current_ev, end_buf,
1098                                                  &iwe, custom);
1099                         if (current_ev == previous_ev)
1100                                 return -E2BIG;
1101                 }
1102                 /*WPA2 IE */
1103                 if (pAdapter->ScanTab.BssEntry[i].RsnIE.IELen > 0) {
1104                         memset(&iwe, 0, sizeof(iwe));
1105                         memset(&custom[0], 0, MAX_CUSTOM_LEN);
1106                         memcpy(custom,
1107                                &(pAdapter->ScanTab.BssEntry[i].RsnIE.IE[0]),
1108                                pAdapter->ScanTab.BssEntry[i].RsnIE.IELen);
1109                         iwe.cmd = IWEVGENIE;
1110                         iwe.u.data.length =
1111                             pAdapter->ScanTab.BssEntry[i].RsnIE.IELen;
1112                         current_ev =
1113                             iwe_stream_add_point(info, current_ev, end_buf,
1114                                                  &iwe, custom);
1115                         if (current_ev == previous_ev)
1116                                 return -E2BIG;
1117                 }
1118         }
1119
1120         data->length = current_ev - extra;
1121         pAdapter->StaCfg.bScanReqIsFromWebUI = FALSE;
1122         DBGPRINT(RT_DEBUG_ERROR,
1123                  ("===>rt_ioctl_giwscan. %d(%d) BSS returned, data->length = %d\n",
1124                   i, pAdapter->ScanTab.BssNr, data->length));
1125         return 0;
1126 }
1127
1128 int rt_ioctl_siwessid(struct net_device *dev,
1129                       struct iw_request_info *info,
1130                       struct iw_point *data, char *essid)
1131 {
1132         PRTMP_ADAPTER pAdapter = NULL;
1133
1134         GET_PAD_FROM_NET_DEV(pAdapter, dev);
1135
1136         /*check if the interface is down */
1137         if (!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE)) {
1138                 DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
1139                 return -ENETDOWN;
1140         }
1141
1142         if (data->flags) {
1143                 char *pSsidString = NULL;
1144
1145                 /* Includes null character. */
1146                 if (data->length > (IW_ESSID_MAX_SIZE + 1))
1147                         return -E2BIG;
1148
1149                 pSsidString = kmalloc(MAX_LEN_OF_SSID + 1, MEM_ALLOC_FLAG);
1150                 if (pSsidString) {
1151                         NdisZeroMemory(pSsidString, MAX_LEN_OF_SSID + 1);
1152                         NdisMoveMemory(pSsidString, essid, data->length);
1153                         if (Set_SSID_Proc(pAdapter, pSsidString) == FALSE)
1154                                 return -EINVAL;
1155                 } else
1156                         return -ENOMEM;
1157         } else {
1158                 /* ANY ssid */
1159                 if (Set_SSID_Proc(pAdapter, "") == FALSE)
1160                         return -EINVAL;
1161         }
1162         return 0;
1163 }
1164
1165 int rt_ioctl_giwessid(struct net_device *dev,
1166                       struct iw_request_info *info,
1167                       struct iw_point *data, char *essid)
1168 {
1169         PRTMP_ADAPTER pAdapter = NULL;
1170
1171         GET_PAD_FROM_NET_DEV(pAdapter, dev);
1172
1173         data->flags = 1;
1174         if (MONITOR_ON(pAdapter)) {
1175                 data->length = 0;
1176                 return 0;
1177         }
1178
1179         if (OPSTATUS_TEST_FLAG(pAdapter, fOP_STATUS_MEDIA_STATE_CONNECTED)) {
1180                 DBGPRINT(RT_DEBUG_TRACE, ("MediaState is connected\n"));
1181                 data->length = pAdapter->CommonCfg.SsidLen;
1182                 memcpy(essid, pAdapter->CommonCfg.Ssid,
1183                        pAdapter->CommonCfg.SsidLen);
1184         }
1185 #ifdef RTMP_MAC_USB
1186         /* Add for RT2870 */
1187         else if (pAdapter->StaCfg.WpaSupplicantUP != WPA_SUPPLICANT_DISABLE) {
1188                 data->length = pAdapter->CommonCfg.SsidLen;
1189                 memcpy(essid, pAdapter->CommonCfg.Ssid,
1190                        pAdapter->CommonCfg.SsidLen);
1191         }
1192 #endif /* RTMP_MAC_USB // */
1193         else {                  /*the ANY ssid was specified */
1194                 data->length = 0;
1195                 DBGPRINT(RT_DEBUG_TRACE,
1196                          ("MediaState is not connected, ess\n"));
1197         }
1198
1199         return 0;
1200
1201 }
1202
1203 int rt_ioctl_siwnickn(struct net_device *dev,
1204                       struct iw_request_info *info,
1205                       struct iw_point *data, char *nickname)
1206 {
1207         PRTMP_ADAPTER pAdapter = NULL;
1208
1209         GET_PAD_FROM_NET_DEV(pAdapter, dev);
1210
1211         /*check if the interface is down */
1212         if (!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE)) {
1213                 DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
1214                 return -ENETDOWN;
1215         }
1216
1217         if (data->length > IW_ESSID_MAX_SIZE)
1218                 return -EINVAL;
1219
1220         memset(pAdapter->nickname, 0, IW_ESSID_MAX_SIZE + 1);
1221         memcpy(pAdapter->nickname, nickname, data->length);
1222
1223         return 0;
1224 }
1225
1226 int rt_ioctl_giwnickn(struct net_device *dev,
1227                       struct iw_request_info *info,
1228                       struct iw_point *data, char *nickname)
1229 {
1230         PRTMP_ADAPTER pAdapter = NULL;
1231
1232         GET_PAD_FROM_NET_DEV(pAdapter, dev);
1233
1234         if (data->length > strlen((char *)pAdapter->nickname) + 1)
1235                 data->length = strlen((char *)pAdapter->nickname) + 1;
1236         if (data->length > 0) {
1237                 memcpy(nickname, pAdapter->nickname, data->length - 1);
1238                 nickname[data->length - 1] = '\0';
1239         }
1240         return 0;
1241 }
1242
1243 int rt_ioctl_siwrts(struct net_device *dev,
1244                     struct iw_request_info *info,
1245                     struct iw_param *rts, char *extra)
1246 {
1247         PRTMP_ADAPTER pAdapter = NULL;
1248         u16 val;
1249
1250         GET_PAD_FROM_NET_DEV(pAdapter, dev);
1251
1252         /*check if the interface is down */
1253         if (!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE)) {
1254                 DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
1255                 return -ENETDOWN;
1256         }
1257
1258         if (rts->disabled)
1259                 val = MAX_RTS_THRESHOLD;
1260         else if (rts->value < 0 || rts->value > MAX_RTS_THRESHOLD)
1261                 return -EINVAL;
1262         else if (rts->value == 0)
1263                 val = MAX_RTS_THRESHOLD;
1264         else
1265                 val = rts->value;
1266
1267         if (val != pAdapter->CommonCfg.RtsThreshold)
1268                 pAdapter->CommonCfg.RtsThreshold = val;
1269
1270         return 0;
1271 }
1272
1273 int rt_ioctl_giwrts(struct net_device *dev,
1274                     struct iw_request_info *info,
1275                     struct iw_param *rts, char *extra)
1276 {
1277         PRTMP_ADAPTER pAdapter = NULL;
1278
1279         GET_PAD_FROM_NET_DEV(pAdapter, dev);
1280
1281         /*check if the interface is down */
1282         if (!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE)) {
1283                 DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
1284                 return -ENETDOWN;
1285         }
1286
1287         rts->value = pAdapter->CommonCfg.RtsThreshold;
1288         rts->disabled = (rts->value == MAX_RTS_THRESHOLD);
1289         rts->fixed = 1;
1290
1291         return 0;
1292 }
1293
1294 int rt_ioctl_siwfrag(struct net_device *dev,
1295                      struct iw_request_info *info,
1296                      struct iw_param *frag, char *extra)
1297 {
1298         PRTMP_ADAPTER pAdapter = NULL;
1299         u16 val;
1300
1301         GET_PAD_FROM_NET_DEV(pAdapter, dev);
1302
1303         /*check if the interface is down */
1304         if (!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE)) {
1305                 DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
1306                 return -ENETDOWN;
1307         }
1308
1309         if (frag->disabled)
1310                 val = MAX_FRAG_THRESHOLD;
1311         else if (frag->value >= MIN_FRAG_THRESHOLD
1312                  && frag->value <= MAX_FRAG_THRESHOLD)
1313                 val = __cpu_to_le16(frag->value & ~0x1);        /* even numbers only */
1314         else if (frag->value == 0)
1315                 val = MAX_FRAG_THRESHOLD;
1316         else
1317                 return -EINVAL;
1318
1319         pAdapter->CommonCfg.FragmentThreshold = val;
1320         return 0;
1321 }
1322
1323 int rt_ioctl_giwfrag(struct net_device *dev,
1324                      struct iw_request_info *info,
1325                      struct iw_param *frag, char *extra)
1326 {
1327         PRTMP_ADAPTER pAdapter = NULL;
1328
1329         GET_PAD_FROM_NET_DEV(pAdapter, dev);
1330
1331         /*check if the interface is down */
1332         if (!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE)) {
1333                 DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
1334                 return -ENETDOWN;
1335         }
1336
1337         frag->value = pAdapter->CommonCfg.FragmentThreshold;
1338         frag->disabled = (frag->value == MAX_FRAG_THRESHOLD);
1339         frag->fixed = 1;
1340
1341         return 0;
1342 }
1343
1344 #define MAX_WEP_KEY_SIZE 13
1345 #define MIN_WEP_KEY_SIZE 5
1346 int rt_ioctl_siwencode(struct net_device *dev,
1347                        struct iw_request_info *info,
1348                        struct iw_point *erq, char *extra)
1349 {
1350         PRTMP_ADAPTER pAdapter = NULL;
1351
1352         GET_PAD_FROM_NET_DEV(pAdapter, dev);
1353
1354         /*check if the interface is down */
1355         if (!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE)) {
1356                 DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
1357                 return -ENETDOWN;
1358         }
1359
1360         if ((erq->length == 0) && (erq->flags & IW_ENCODE_DISABLED)) {
1361                 pAdapter->StaCfg.PairCipher = Ndis802_11WEPDisabled;
1362                 pAdapter->StaCfg.GroupCipher = Ndis802_11WEPDisabled;
1363                 pAdapter->StaCfg.WepStatus = Ndis802_11WEPDisabled;
1364                 pAdapter->StaCfg.OrigWepStatus = pAdapter->StaCfg.WepStatus;
1365                 pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeOpen;
1366                 goto done;
1367         } else if (erq->flags & IW_ENCODE_RESTRICTED
1368                    || erq->flags & IW_ENCODE_OPEN) {
1369                 /*pAdapter->StaCfg.PortSecured = WPA_802_1X_PORT_SECURED; */
1370                 STA_PORT_SECURED(pAdapter);
1371                 pAdapter->StaCfg.PairCipher = Ndis802_11WEPEnabled;
1372                 pAdapter->StaCfg.GroupCipher = Ndis802_11WEPEnabled;
1373                 pAdapter->StaCfg.WepStatus = Ndis802_11WEPEnabled;
1374                 pAdapter->StaCfg.OrigWepStatus = pAdapter->StaCfg.WepStatus;
1375                 if (erq->flags & IW_ENCODE_RESTRICTED)
1376                         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeShared;
1377                 else
1378                         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeOpen;
1379         }
1380
1381         if (erq->length > 0) {
1382                 int keyIdx = (erq->flags & IW_ENCODE_INDEX) - 1;
1383                 /* Check the size of the key */
1384                 if (erq->length > MAX_WEP_KEY_SIZE) {
1385                         return -EINVAL;
1386                 }
1387                 /* Check key index */
1388                 if ((keyIdx < 0) || (keyIdx >= NR_WEP_KEYS)) {
1389                         DBGPRINT(RT_DEBUG_TRACE,
1390                                  ("==>rt_ioctl_siwencode::Wrong keyIdx=%d! Using default key instead (%d)\n",
1391                                   keyIdx, pAdapter->StaCfg.DefaultKeyId));
1392
1393                         /*Using default key */
1394                         keyIdx = pAdapter->StaCfg.DefaultKeyId;
1395                 } else
1396                         pAdapter->StaCfg.DefaultKeyId = keyIdx;
1397
1398                 NdisZeroMemory(pAdapter->SharedKey[BSS0][keyIdx].Key, 16);
1399
1400                 if (erq->length == MAX_WEP_KEY_SIZE) {
1401                         pAdapter->SharedKey[BSS0][keyIdx].KeyLen =
1402                             MAX_WEP_KEY_SIZE;
1403                         pAdapter->SharedKey[BSS0][keyIdx].CipherAlg =
1404                             CIPHER_WEP128;
1405                 } else if (erq->length == MIN_WEP_KEY_SIZE) {
1406                         pAdapter->SharedKey[BSS0][keyIdx].KeyLen =
1407                             MIN_WEP_KEY_SIZE;
1408                         pAdapter->SharedKey[BSS0][keyIdx].CipherAlg =
1409                             CIPHER_WEP64;
1410                 } else
1411                         /* Disable the key */
1412                         pAdapter->SharedKey[BSS0][keyIdx].KeyLen = 0;
1413
1414                 /* Check if the key is not marked as invalid */
1415                 if (!(erq->flags & IW_ENCODE_NOKEY)) {
1416                         /* Copy the key in the driver */
1417                         NdisMoveMemory(pAdapter->SharedKey[BSS0][keyIdx].Key,
1418                                        extra, erq->length);
1419                 }
1420         } else {
1421                 /* Do we want to just set the transmit key index ? */
1422                 int index = (erq->flags & IW_ENCODE_INDEX) - 1;
1423                 if ((index >= 0) && (index < 4)) {
1424                         pAdapter->StaCfg.DefaultKeyId = index;
1425                 } else
1426                         /* Don't complain if only change the mode */
1427                 if (!(erq->flags & IW_ENCODE_MODE))
1428                         return -EINVAL;
1429         }
1430
1431 done:
1432         DBGPRINT(RT_DEBUG_TRACE,
1433                  ("==>rt_ioctl_siwencode::erq->flags=%x\n", erq->flags));
1434         DBGPRINT(RT_DEBUG_TRACE,
1435                  ("==>rt_ioctl_siwencode::AuthMode=%x\n",
1436                   pAdapter->StaCfg.AuthMode));
1437         DBGPRINT(RT_DEBUG_TRACE,
1438                  ("==>rt_ioctl_siwencode::DefaultKeyId=%x, KeyLen = %d\n",
1439                   pAdapter->StaCfg.DefaultKeyId,
1440                   pAdapter->SharedKey[BSS0][pAdapter->StaCfg.DefaultKeyId].
1441                   KeyLen));
1442         DBGPRINT(RT_DEBUG_TRACE,
1443                  ("==>rt_ioctl_siwencode::WepStatus=%x\n",
1444                   pAdapter->StaCfg.WepStatus));
1445         return 0;
1446 }
1447
1448 int
1449 rt_ioctl_giwencode(struct net_device *dev,
1450                    struct iw_request_info *info,
1451                    struct iw_point *erq, char *key)
1452 {
1453         int kid;
1454         PRTMP_ADAPTER pAdapter = NULL;
1455
1456         GET_PAD_FROM_NET_DEV(pAdapter, dev);
1457
1458         /*check if the interface is down */
1459         if (!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE)) {
1460                 DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
1461                 return -ENETDOWN;
1462         }
1463
1464         kid = erq->flags & IW_ENCODE_INDEX;
1465         DBGPRINT(RT_DEBUG_TRACE,
1466                  ("===>rt_ioctl_giwencode %d\n", erq->flags & IW_ENCODE_INDEX));
1467
1468         if (pAdapter->StaCfg.WepStatus == Ndis802_11WEPDisabled) {
1469                 erq->length = 0;
1470                 erq->flags = IW_ENCODE_DISABLED;
1471         } else if ((kid > 0) && (kid <= 4)) {
1472                 /* copy wep key */
1473                 erq->flags = kid;       /* NB: base 1 */
1474                 if (erq->length > pAdapter->SharedKey[BSS0][kid - 1].KeyLen)
1475                         erq->length = pAdapter->SharedKey[BSS0][kid - 1].KeyLen;
1476                 memcpy(key, pAdapter->SharedKey[BSS0][kid - 1].Key,
1477                        erq->length);
1478                 /*if ((kid == pAdapter->PortCfg.DefaultKeyId)) */
1479                 /*erq->flags |= IW_ENCODE_ENABLED; */   /* XXX */
1480                 if (pAdapter->StaCfg.AuthMode == Ndis802_11AuthModeShared)
1481                         erq->flags |= IW_ENCODE_RESTRICTED;     /* XXX */
1482                 else
1483                         erq->flags |= IW_ENCODE_OPEN;   /* XXX */
1484
1485         } else if (kid == 0) {
1486                 if (pAdapter->StaCfg.AuthMode == Ndis802_11AuthModeShared)
1487                         erq->flags |= IW_ENCODE_RESTRICTED;     /* XXX */
1488                 else
1489                         erq->flags |= IW_ENCODE_OPEN;   /* XXX */
1490                 erq->length =
1491                     pAdapter->SharedKey[BSS0][pAdapter->StaCfg.DefaultKeyId].
1492                     KeyLen;
1493                 memcpy(key,
1494                        pAdapter->SharedKey[BSS0][pAdapter->StaCfg.DefaultKeyId].
1495                        Key, erq->length);
1496                 /* copy default key ID */
1497                 if (pAdapter->StaCfg.AuthMode == Ndis802_11AuthModeShared)
1498                         erq->flags |= IW_ENCODE_RESTRICTED;     /* XXX */
1499                 else
1500                         erq->flags |= IW_ENCODE_OPEN;   /* XXX */
1501                 erq->flags = pAdapter->StaCfg.DefaultKeyId + 1; /* NB: base 1 */
1502                 erq->flags |= IW_ENCODE_ENABLED;        /* XXX */
1503         }
1504
1505         return 0;
1506
1507 }
1508
1509 void getBaInfo(IN PRTMP_ADAPTER pAd, char *pOutBuf)
1510 {
1511         int i, j;
1512         BA_ORI_ENTRY *pOriBAEntry;
1513         BA_REC_ENTRY *pRecBAEntry;
1514
1515         for (i = 0; i < MAX_LEN_OF_MAC_TABLE; i++) {
1516                 PMAC_TABLE_ENTRY pEntry = &pAd->MacTab.Content[i];
1517                 if (((pEntry->ValidAsCLI || pEntry->ValidAsApCli)
1518                      && (pEntry->Sst == SST_ASSOC))
1519                     || (pEntry->ValidAsWDS) || (pEntry->ValidAsMesh)) {
1520                         sprintf(pOutBuf + strlen(pOutBuf),
1521                                 "\n%02X:%02X:%02X:%02X:%02X:%02X (Aid = %d) (AP) -\n",
1522                                 pEntry->Addr[0], pEntry->Addr[1],
1523                                 pEntry->Addr[2], pEntry->Addr[3],
1524                                 pEntry->Addr[4], pEntry->Addr[5], pEntry->Aid);
1525
1526                         sprintf(pOutBuf, "%s[Recipient]\n", pOutBuf);
1527                         for (j = 0; j < NUM_OF_TID; j++) {
1528                                 if (pEntry->BARecWcidArray[j] != 0) {
1529                                         pRecBAEntry =
1530                                             &pAd->BATable.BARecEntry[pEntry->
1531                                                                      BARecWcidArray
1532                                                                      [j]];
1533                                         sprintf(pOutBuf + strlen(pOutBuf),
1534                                                 "TID=%d, BAWinSize=%d, LastIndSeq=%d, ReorderingPkts=%d\n",
1535                                                 j, pRecBAEntry->BAWinSize,
1536                                                 pRecBAEntry->LastIndSeq,
1537                                                 pRecBAEntry->list.qlen);
1538                                 }
1539                         }
1540                         sprintf(pOutBuf, "%s\n", pOutBuf);
1541
1542                         sprintf(pOutBuf, "%s[Originator]\n", pOutBuf);
1543                         for (j = 0; j < NUM_OF_TID; j++) {
1544                                 if (pEntry->BAOriWcidArray[j] != 0) {
1545                                         pOriBAEntry =
1546                                             &pAd->BATable.BAOriEntry[pEntry->
1547                                                                      BAOriWcidArray
1548                                                                      [j]];
1549                                         sprintf(pOutBuf + strlen(pOutBuf),
1550                                                 "TID=%d, BAWinSize=%d, StartSeq=%d, CurTxSeq=%d\n",
1551                                                 j, pOriBAEntry->BAWinSize,
1552                                                 pOriBAEntry->Sequence,
1553                                                 pEntry->TxSeq[j]);
1554                                 }
1555                         }
1556                         sprintf(pOutBuf, "%s\n\n", pOutBuf);
1557                 }
1558                 if (strlen(pOutBuf) > (IW_PRIV_SIZE_MASK - 30))
1559                         break;
1560         }
1561
1562         return;
1563 }
1564
1565 int rt_ioctl_siwmlme(struct net_device *dev,
1566                      struct iw_request_info *info,
1567                      union iwreq_data *wrqu, char *extra)
1568 {
1569         PRTMP_ADAPTER pAd = NULL;
1570         struct iw_mlme *pMlme = (struct iw_mlme *)wrqu->data.pointer;
1571         MLME_QUEUE_ELEM MsgElem;
1572         MLME_DISASSOC_REQ_STRUCT DisAssocReq;
1573         MLME_DEAUTH_REQ_STRUCT DeAuthReq;
1574
1575         GET_PAD_FROM_NET_DEV(pAd, dev);
1576
1577         DBGPRINT(RT_DEBUG_TRACE, ("====> %s\n", __func__));
1578
1579         if (pMlme == NULL)
1580                 return -EINVAL;
1581
1582         switch (pMlme->cmd) {
1583 #ifdef IW_MLME_DEAUTH
1584         case IW_MLME_DEAUTH:
1585                 DBGPRINT(RT_DEBUG_TRACE,
1586                          ("====> %s - IW_MLME_DEAUTH\n", __func__));
1587                 COPY_MAC_ADDR(DeAuthReq.Addr, pAd->CommonCfg.Bssid);
1588                 DeAuthReq.Reason = pMlme->reason_code;
1589                 MsgElem.MsgLen = sizeof(MLME_DEAUTH_REQ_STRUCT);
1590                 NdisMoveMemory(MsgElem.Msg, &DeAuthReq,
1591                                sizeof(MLME_DEAUTH_REQ_STRUCT));
1592                 MlmeDeauthReqAction(pAd, &MsgElem);
1593                 if (INFRA_ON(pAd)) {
1594                         LinkDown(pAd, FALSE);
1595                         pAd->Mlme.AssocMachine.CurrState = ASSOC_IDLE;
1596                 }
1597                 break;
1598 #endif /* IW_MLME_DEAUTH // */
1599 #ifdef IW_MLME_DISASSOC
1600         case IW_MLME_DISASSOC:
1601                 DBGPRINT(RT_DEBUG_TRACE,
1602                          ("====> %s - IW_MLME_DISASSOC\n", __func__));
1603                 COPY_MAC_ADDR(DisAssocReq.Addr, pAd->CommonCfg.Bssid);
1604                 DisAssocReq.Reason = pMlme->reason_code;
1605
1606                 MsgElem.Machine = ASSOC_STATE_MACHINE;
1607                 MsgElem.MsgType = MT2_MLME_DISASSOC_REQ;
1608                 MsgElem.MsgLen = sizeof(MLME_DISASSOC_REQ_STRUCT);
1609                 NdisMoveMemory(MsgElem.Msg, &DisAssocReq,
1610                                sizeof(MLME_DISASSOC_REQ_STRUCT));
1611
1612                 pAd->Mlme.CntlMachine.CurrState = CNTL_WAIT_OID_DISASSOC;
1613                 MlmeDisassocReqAction(pAd, &MsgElem);
1614                 break;
1615 #endif /* IW_MLME_DISASSOC // */
1616         default:
1617                 DBGPRINT(RT_DEBUG_TRACE,
1618                          ("====> %s - Unknow Command\n", __func__));
1619                 break;
1620         }
1621
1622         return 0;
1623 }
1624
1625 int rt_ioctl_siwauth(struct net_device *dev,
1626                      struct iw_request_info *info,
1627                      union iwreq_data *wrqu, char *extra)
1628 {
1629         PRTMP_ADAPTER pAdapter = NULL;
1630         struct iw_param *param = &wrqu->param;
1631
1632         GET_PAD_FROM_NET_DEV(pAdapter, dev);
1633
1634         /*check if the interface is down */
1635         if (!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE)) {
1636                 DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
1637                 return -ENETDOWN;
1638         }
1639         switch (param->flags & IW_AUTH_INDEX) {
1640         case IW_AUTH_WPA_VERSION:
1641                 if (param->value == IW_AUTH_WPA_VERSION_WPA) {
1642                         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeWPAPSK;
1643                         if (pAdapter->StaCfg.BssType == BSS_ADHOC)
1644                                 pAdapter->StaCfg.AuthMode =
1645                                     Ndis802_11AuthModeWPANone;
1646                 } else if (param->value == IW_AUTH_WPA_VERSION_WPA2)
1647                         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeWPA2PSK;
1648
1649                 DBGPRINT(RT_DEBUG_TRACE,
1650                          ("%s::IW_AUTH_WPA_VERSION - param->value = %d!\n",
1651                           __func__, param->value));
1652                 break;
1653         case IW_AUTH_CIPHER_PAIRWISE:
1654                 if (param->value == IW_AUTH_CIPHER_NONE) {
1655                         pAdapter->StaCfg.WepStatus = Ndis802_11WEPDisabled;
1656                         pAdapter->StaCfg.OrigWepStatus =
1657                             pAdapter->StaCfg.WepStatus;
1658                         pAdapter->StaCfg.PairCipher = Ndis802_11WEPDisabled;
1659                 } else if (param->value == IW_AUTH_CIPHER_WEP40 ||
1660                            param->value == IW_AUTH_CIPHER_WEP104) {
1661                         pAdapter->StaCfg.WepStatus = Ndis802_11WEPEnabled;
1662                         pAdapter->StaCfg.OrigWepStatus =
1663                             pAdapter->StaCfg.WepStatus;
1664                         pAdapter->StaCfg.PairCipher = Ndis802_11WEPEnabled;
1665                         pAdapter->StaCfg.IEEE8021X = FALSE;
1666                 } else if (param->value == IW_AUTH_CIPHER_TKIP) {
1667                         pAdapter->StaCfg.WepStatus =
1668                             Ndis802_11Encryption2Enabled;
1669                         pAdapter->StaCfg.OrigWepStatus =
1670                             pAdapter->StaCfg.WepStatus;
1671                         pAdapter->StaCfg.PairCipher =
1672                             Ndis802_11Encryption2Enabled;
1673                 } else if (param->value == IW_AUTH_CIPHER_CCMP) {
1674                         pAdapter->StaCfg.WepStatus =
1675                             Ndis802_11Encryption3Enabled;
1676                         pAdapter->StaCfg.OrigWepStatus =
1677                             pAdapter->StaCfg.WepStatus;
1678                         pAdapter->StaCfg.PairCipher =
1679                             Ndis802_11Encryption3Enabled;
1680                 }
1681                 DBGPRINT(RT_DEBUG_TRACE,
1682                          ("%s::IW_AUTH_CIPHER_PAIRWISE - param->value = %d!\n",
1683                           __func__, param->value));
1684                 break;
1685         case IW_AUTH_CIPHER_GROUP:
1686                 if (param->value == IW_AUTH_CIPHER_NONE) {
1687                         pAdapter->StaCfg.GroupCipher = Ndis802_11WEPDisabled;
1688                 } else if (param->value == IW_AUTH_CIPHER_WEP40 ||
1689                            param->value == IW_AUTH_CIPHER_WEP104) {
1690                         pAdapter->StaCfg.GroupCipher = Ndis802_11WEPEnabled;
1691                 } else if (param->value == IW_AUTH_CIPHER_TKIP) {
1692                         pAdapter->StaCfg.GroupCipher =
1693                             Ndis802_11Encryption2Enabled;
1694                 } else if (param->value == IW_AUTH_CIPHER_CCMP) {
1695                         pAdapter->StaCfg.GroupCipher =
1696                             Ndis802_11Encryption3Enabled;
1697                 }
1698                 DBGPRINT(RT_DEBUG_TRACE,
1699                          ("%s::IW_AUTH_CIPHER_GROUP - param->value = %d!\n",
1700                           __func__, param->value));
1701                 break;
1702         case IW_AUTH_KEY_MGMT:
1703                 if (param->value == IW_AUTH_KEY_MGMT_802_1X) {
1704                         if (pAdapter->StaCfg.AuthMode ==
1705                             Ndis802_11AuthModeWPAPSK) {
1706                                 pAdapter->StaCfg.AuthMode =
1707                                     Ndis802_11AuthModeWPA;
1708                                 pAdapter->StaCfg.IEEE8021X = FALSE;
1709                         } else if (pAdapter->StaCfg.AuthMode ==
1710                                    Ndis802_11AuthModeWPA2PSK) {
1711                                 pAdapter->StaCfg.AuthMode =
1712                                     Ndis802_11AuthModeWPA2;
1713                                 pAdapter->StaCfg.IEEE8021X = FALSE;
1714                         } else
1715                                 /* WEP 1x */
1716                                 pAdapter->StaCfg.IEEE8021X = TRUE;
1717                 } else if (param->value == 0) {
1718                         /*pAdapter->StaCfg.PortSecured = WPA_802_1X_PORT_SECURED; */
1719                         STA_PORT_SECURED(pAdapter);
1720                 }
1721                 DBGPRINT(RT_DEBUG_TRACE,
1722                          ("%s::IW_AUTH_KEY_MGMT - param->value = %d!\n",
1723                           __func__, param->value));
1724                 break;
1725         case IW_AUTH_RX_UNENCRYPTED_EAPOL:
1726                 break;
1727         case IW_AUTH_PRIVACY_INVOKED:
1728                 /*if (param->value == 0)
1729                    {
1730                    pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeOpen;
1731                    pAdapter->StaCfg.WepStatus = Ndis802_11WEPDisabled;
1732                    pAdapter->StaCfg.OrigWepStatus = pAdapter->StaCfg.WepStatus;
1733                    pAdapter->StaCfg.PairCipher = Ndis802_11WEPDisabled;
1734                    pAdapter->StaCfg.GroupCipher = Ndis802_11WEPDisabled;
1735                    } */
1736                 DBGPRINT(RT_DEBUG_TRACE,
1737                          ("%s::IW_AUTH_PRIVACY_INVOKED - param->value = %d!\n",
1738                           __func__, param->value));
1739                 break;
1740         case IW_AUTH_DROP_UNENCRYPTED:
1741                 if (param->value != 0)
1742                         pAdapter->StaCfg.PortSecured =
1743                             WPA_802_1X_PORT_NOT_SECURED;
1744                 else {
1745                         /*pAdapter->StaCfg.PortSecured = WPA_802_1X_PORT_SECURED; */
1746                         STA_PORT_SECURED(pAdapter);
1747                 }
1748                 DBGPRINT(RT_DEBUG_TRACE,
1749                          ("%s::IW_AUTH_WPA_VERSION - param->value = %d!\n",
1750                           __func__, param->value));
1751                 break;
1752         case IW_AUTH_80211_AUTH_ALG:
1753                 if (param->value & IW_AUTH_ALG_SHARED_KEY) {
1754                         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeShared;
1755                 } else if (param->value & IW_AUTH_ALG_OPEN_SYSTEM) {
1756                         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeOpen;
1757                 } else
1758                         return -EINVAL;
1759                 DBGPRINT(RT_DEBUG_TRACE,
1760                          ("%s::IW_AUTH_80211_AUTH_ALG - param->value = %d!\n",
1761                           __func__, param->value));
1762                 break;
1763         case IW_AUTH_WPA_ENABLED:
1764                 DBGPRINT(RT_DEBUG_TRACE,
1765                          ("%s::IW_AUTH_WPA_ENABLED - Driver supports WPA!(param->value = %d)\n",
1766                           __func__, param->value));
1767                 break;
1768         default:
1769                 return -EOPNOTSUPP;
1770         }
1771
1772         return 0;
1773 }
1774
1775 int rt_ioctl_giwauth(struct net_device *dev,
1776                      struct iw_request_info *info,
1777                      union iwreq_data *wrqu, char *extra)
1778 {
1779         PRTMP_ADAPTER pAdapter = NULL;
1780         struct iw_param *param = &wrqu->param;
1781
1782         GET_PAD_FROM_NET_DEV(pAdapter, dev);
1783
1784         /*check if the interface is down */
1785         if (!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE)) {
1786                 DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
1787                 return -ENETDOWN;
1788         }
1789
1790         switch (param->flags & IW_AUTH_INDEX) {
1791         case IW_AUTH_DROP_UNENCRYPTED:
1792                 param->value =
1793                     (pAdapter->StaCfg.WepStatus ==
1794                      Ndis802_11WEPDisabled) ? 0 : 1;
1795                 break;
1796
1797         case IW_AUTH_80211_AUTH_ALG:
1798                 param->value =
1799                     (pAdapter->StaCfg.AuthMode ==
1800                      Ndis802_11AuthModeShared) ? IW_AUTH_ALG_SHARED_KEY :
1801                     IW_AUTH_ALG_OPEN_SYSTEM;
1802                 break;
1803
1804         case IW_AUTH_WPA_ENABLED:
1805                 param->value =
1806                     (pAdapter->StaCfg.AuthMode >=
1807                      Ndis802_11AuthModeWPA) ? 1 : 0;
1808                 break;
1809
1810         default:
1811                 return -EOPNOTSUPP;
1812         }
1813         DBGPRINT(RT_DEBUG_TRACE,
1814                  ("rt_ioctl_giwauth::param->value = %d!\n", param->value));
1815         return 0;
1816 }
1817
1818 void fnSetCipherKey(IN PRTMP_ADAPTER pAdapter,
1819                     int keyIdx,
1820                     u8 CipherAlg,
1821                     IN BOOLEAN bGTK, IN struct iw_encode_ext *ext)
1822 {
1823         NdisZeroMemory(&pAdapter->SharedKey[BSS0][keyIdx], sizeof(CIPHER_KEY));
1824         pAdapter->SharedKey[BSS0][keyIdx].KeyLen = LEN_TKIP_EK;
1825         NdisMoveMemory(pAdapter->SharedKey[BSS0][keyIdx].Key, ext->key,
1826                        LEN_TKIP_EK);
1827         NdisMoveMemory(pAdapter->SharedKey[BSS0][keyIdx].TxMic,
1828                        ext->key + LEN_TKIP_EK, LEN_TKIP_TXMICK);
1829         NdisMoveMemory(pAdapter->SharedKey[BSS0][keyIdx].RxMic,
1830                        ext->key + LEN_TKIP_EK + LEN_TKIP_TXMICK,
1831                        LEN_TKIP_RXMICK);
1832         pAdapter->SharedKey[BSS0][keyIdx].CipherAlg = CipherAlg;
1833
1834         /* Update group key information to ASIC Shared Key Table */
1835         AsicAddSharedKeyEntry(pAdapter,
1836                               BSS0,
1837                               keyIdx,
1838                               pAdapter->SharedKey[BSS0][keyIdx].CipherAlg,
1839                               pAdapter->SharedKey[BSS0][keyIdx].Key,
1840                               pAdapter->SharedKey[BSS0][keyIdx].TxMic,
1841                               pAdapter->SharedKey[BSS0][keyIdx].RxMic);
1842
1843         if (bGTK)
1844                 /* Update ASIC WCID attribute table and IVEIV table */
1845                 RTMPAddWcidAttributeEntry(pAdapter,
1846                                           BSS0,
1847                                           keyIdx,
1848                                           pAdapter->SharedKey[BSS0][keyIdx].
1849                                           CipherAlg, NULL);
1850         else
1851                 /* Update ASIC WCID attribute table and IVEIV table */
1852                 RTMPAddWcidAttributeEntry(pAdapter,
1853                                           BSS0,
1854                                           keyIdx,
1855                                           pAdapter->SharedKey[BSS0][keyIdx].
1856                                           CipherAlg,
1857                                           &pAdapter->MacTab.
1858                                           Content[BSSID_WCID]);
1859 }
1860
1861 int rt_ioctl_siwencodeext(struct net_device *dev,
1862                           struct iw_request_info *info,
1863                           union iwreq_data *wrqu, char *extra)
1864 {
1865         PRTMP_ADAPTER pAdapter = NULL;
1866         struct iw_point *encoding = &wrqu->encoding;
1867         struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
1868         int keyIdx, alg = ext->alg;
1869
1870         GET_PAD_FROM_NET_DEV(pAdapter, dev);
1871
1872         /*check if the interface is down */
1873         if (!RTMP_TEST_FLAG(pAdapter, fRTMP_ADAPTER_INTERRUPT_IN_USE)) {
1874                 DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
1875                 return -ENETDOWN;
1876         }
1877
1878         if (encoding->flags & IW_ENCODE_DISABLED) {
1879                 keyIdx = (encoding->flags & IW_ENCODE_INDEX) - 1;
1880                 /* set BSSID wcid entry of the Pair-wise Key table as no-security mode */
1881                 AsicRemovePairwiseKeyEntry(pAdapter, BSS0, BSSID_WCID);
1882                 pAdapter->SharedKey[BSS0][keyIdx].KeyLen = 0;
1883                 pAdapter->SharedKey[BSS0][keyIdx].CipherAlg = CIPHER_NONE;
1884                 AsicRemoveSharedKeyEntry(pAdapter, 0, (u8)keyIdx);
1885                 NdisZeroMemory(&pAdapter->SharedKey[BSS0][keyIdx],
1886                                sizeof(CIPHER_KEY));
1887                 DBGPRINT(RT_DEBUG_TRACE,
1888                          ("%s::Remove all keys!(encoding->flags = %x)\n",
1889                           __func__, encoding->flags));
1890         } else {
1891                 /* Get Key Index and convet to our own defined key index */
1892                 keyIdx = (encoding->flags & IW_ENCODE_INDEX) - 1;
1893                 if ((keyIdx < 0) || (keyIdx >= NR_WEP_KEYS))
1894                         return -EINVAL;
1895
1896                 if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY) {
1897                         pAdapter->StaCfg.DefaultKeyId = keyIdx;
1898                         DBGPRINT(RT_DEBUG_TRACE,
1899                                  ("%s::DefaultKeyId = %d\n", __func__,
1900                                   pAdapter->StaCfg.DefaultKeyId));
1901                 }
1902
1903                 switch (alg) {
1904                 case IW_ENCODE_ALG_NONE:
1905                         DBGPRINT(RT_DEBUG_TRACE,
1906                                  ("%s::IW_ENCODE_ALG_NONE\n", __func__));
1907                         break;
1908                 case IW_ENCODE_ALG_WEP:
1909                         DBGPRINT(RT_DEBUG_TRACE,
1910                                  ("%s::IW_ENCODE_ALG_WEP - ext->key_len = %d, keyIdx = %d\n",
1911                                   __func__, ext->key_len, keyIdx));
1912                         if (ext->key_len == MAX_WEP_KEY_SIZE) {
1913                                 pAdapter->SharedKey[BSS0][keyIdx].KeyLen =
1914                                     MAX_WEP_KEY_SIZE;
1915                                 pAdapter->SharedKey[BSS0][keyIdx].CipherAlg =
1916                                     CIPHER_WEP128;
1917                         } else if (ext->key_len == MIN_WEP_KEY_SIZE) {
1918                                 pAdapter->SharedKey[BSS0][keyIdx].KeyLen =
1919                                     MIN_WEP_KEY_SIZE;
1920                                 pAdapter->SharedKey[BSS0][keyIdx].CipherAlg =
1921                                     CIPHER_WEP64;
1922                         } else
1923                                 return -EINVAL;
1924
1925                         NdisZeroMemory(pAdapter->SharedKey[BSS0][keyIdx].Key,
1926                                        16);
1927                         NdisMoveMemory(pAdapter->SharedKey[BSS0][keyIdx].Key,
1928                                        ext->key, ext->key_len);
1929                         if (pAdapter->StaCfg.GroupCipher ==
1930                             Ndis802_11GroupWEP40Enabled
1931                             || pAdapter->StaCfg.GroupCipher ==
1932                             Ndis802_11GroupWEP104Enabled) {
1933                                 /* Set Group key material to Asic */
1934                                 AsicAddSharedKeyEntry(pAdapter, BSS0, keyIdx,
1935                                                       pAdapter->
1936                                                       SharedKey[BSS0][keyIdx].
1937                                                       CipherAlg,
1938                                                       pAdapter->
1939                                                       SharedKey[BSS0][keyIdx].
1940                                                       Key, NULL, NULL);
1941
1942                                 /* Update WCID attribute table and IVEIV table for this group key table */
1943                                 RTMPAddWcidAttributeEntry(pAdapter, BSS0,
1944                                                           keyIdx,
1945                                                           pAdapter->
1946                                                           SharedKey[BSS0]
1947                                                           [keyIdx].CipherAlg,
1948                                                           NULL);
1949
1950                                 STA_PORT_SECURED(pAdapter);
1951
1952                                 /* Indicate Connected for GUI */
1953                                 pAdapter->IndicateMediaState =
1954                                     NdisMediaStateConnected;
1955                         }
1956                         break;
1957                 case IW_ENCODE_ALG_TKIP:
1958                         DBGPRINT(RT_DEBUG_TRACE,
1959                                  ("%s::IW_ENCODE_ALG_TKIP - keyIdx = %d, ext->key_len = %d\n",
1960                                   __func__, keyIdx, ext->key_len));
1961                         if (ext->key_len == 32) {
1962                                 if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY) {
1963                                         fnSetCipherKey(pAdapter, keyIdx,
1964                                                        CIPHER_TKIP, FALSE, ext);
1965                                         if (pAdapter->StaCfg.AuthMode >=
1966                                             Ndis802_11AuthModeWPA2) {
1967                                                 /*pAdapter->StaCfg.PortSecured = WPA_802_1X_PORT_SECURED; */
1968                                                 STA_PORT_SECURED(pAdapter);
1969                                                 pAdapter->IndicateMediaState =
1970                                                     NdisMediaStateConnected;
1971                                         }
1972                                 } else if (ext->
1973                                            ext_flags & IW_ENCODE_EXT_GROUP_KEY)
1974                                 {
1975                                         fnSetCipherKey(pAdapter, keyIdx,
1976                                                        CIPHER_TKIP, TRUE, ext);
1977
1978                                         /* set 802.1x port control */
1979                                         /*pAdapter->StaCfg.PortSecured = WPA_802_1X_PORT_SECURED; */
1980                                         STA_PORT_SECURED(pAdapter);
1981                                         pAdapter->IndicateMediaState =
1982                                             NdisMediaStateConnected;
1983                                 }
1984                         } else
1985                                 return -EINVAL;
1986                         break;
1987                 case IW_ENCODE_ALG_CCMP:
1988                         if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY) {
1989                                 fnSetCipherKey(pAdapter, keyIdx, CIPHER_AES,
1990                                                FALSE, ext);
1991                                 if (pAdapter->StaCfg.AuthMode >=
1992                                     Ndis802_11AuthModeWPA2)
1993                                         /*pAdapter->StaCfg.PortSecured = WPA_802_1X_PORT_SECURED; */
1994                                         STA_PORT_SECURED(pAdapter);
1995                                 pAdapter->IndicateMediaState =
1996                                     NdisMediaStateConnected;
1997                         } else if (ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY) {
1998                                 fnSetCipherKey(pAdapter, keyIdx, CIPHER_AES,
1999                                                TRUE, ext);
2000
2001                                 /* set 802.1x port control */
2002                                 /*pAdapter->StaCfg.PortSecured = WPA_802_1X_PORT_SECURED; */
2003                                 STA_PORT_SECURED(pAdapter);
2004                                 pAdapter->IndicateMediaState =
2005                                     NdisMediaStateConnected;
2006                         }
2007                         break;
2008                 default:
2009                         return -EINVAL;
2010                 }
2011         }
2012
2013         return 0;
2014 }
2015
2016 int
2017 rt_ioctl_giwencodeext(struct net_device *dev,
2018                       struct iw_request_info *info,
2019                       union iwreq_data *wrqu, char *extra)
2020 {
2021         PRTMP_ADAPTER pAd = NULL;
2022         char *pKey = NULL;
2023         struct iw_point *encoding = &wrqu->encoding;
2024         struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
2025         int idx, max_key_len;
2026
2027         GET_PAD_FROM_NET_DEV(pAd, dev);
2028
2029         DBGPRINT(RT_DEBUG_TRACE, ("===> rt_ioctl_giwencodeext\n"));
2030
2031         max_key_len = encoding->length - sizeof(*ext);
2032         if (max_key_len < 0)
2033                 return -EINVAL;
2034
2035         idx = encoding->flags & IW_ENCODE_INDEX;
2036         if (idx) {
2037                 if (idx < 1 || idx > 4)
2038                         return -EINVAL;
2039                 idx--;
2040
2041                 if ((pAd->StaCfg.WepStatus == Ndis802_11Encryption2Enabled) ||
2042                     (pAd->StaCfg.WepStatus == Ndis802_11Encryption3Enabled)) {
2043                         if (idx != pAd->StaCfg.DefaultKeyId) {
2044                                 ext->key_len = 0;
2045                                 return 0;
2046                         }
2047                 }
2048         } else
2049                 idx = pAd->StaCfg.DefaultKeyId;
2050
2051         encoding->flags = idx + 1;
2052         memset(ext, 0, sizeof(*ext));
2053
2054         ext->key_len = 0;
2055         switch (pAd->StaCfg.WepStatus) {
2056         case Ndis802_11WEPDisabled:
2057                 ext->alg = IW_ENCODE_ALG_NONE;
2058                 encoding->flags |= IW_ENCODE_DISABLED;
2059                 break;
2060         case Ndis802_11WEPEnabled:
2061                 ext->alg = IW_ENCODE_ALG_WEP;
2062                 if (pAd->SharedKey[BSS0][idx].KeyLen > max_key_len)
2063                         return -E2BIG;
2064                 else {
2065                         ext->key_len = pAd->SharedKey[BSS0][idx].KeyLen;
2066                         pKey = (char *)& (pAd->SharedKey[BSS0][idx].Key[0]);
2067                 }
2068                 break;
2069         case Ndis802_11Encryption2Enabled:
2070         case Ndis802_11Encryption3Enabled:
2071                 if (pAd->StaCfg.WepStatus == Ndis802_11Encryption2Enabled)
2072                         ext->alg = IW_ENCODE_ALG_TKIP;
2073                 else
2074                         ext->alg = IW_ENCODE_ALG_CCMP;
2075
2076                 if (max_key_len < 32)
2077                         return -E2BIG;
2078                 else {
2079                         ext->key_len = 32;
2080                         pKey = (char *)& pAd->StaCfg.PMK[0];
2081                 }
2082                 break;
2083         default:
2084                 return -EINVAL;
2085         }
2086
2087         if (ext->key_len && pKey) {
2088                 encoding->flags |= IW_ENCODE_ENABLED;
2089                 memcpy(ext->key, pKey, ext->key_len);
2090         }
2091
2092         return 0;
2093 }
2094
2095 int rt_ioctl_siwgenie(struct net_device *dev,
2096                       struct iw_request_info *info,
2097                       union iwreq_data *wrqu, char *extra)
2098 {
2099         PRTMP_ADAPTER pAd = NULL;
2100
2101         GET_PAD_FROM_NET_DEV(pAd, dev);
2102
2103         DBGPRINT(RT_DEBUG_TRACE, ("===> rt_ioctl_siwgenie\n"));
2104         pAd->StaCfg.bRSN_IE_FromWpaSupplicant = FALSE;
2105         if (wrqu->data.length > MAX_LEN_OF_RSNIE ||
2106             (wrqu->data.length && extra == NULL))
2107                 return -EINVAL;
2108
2109         if (wrqu->data.length) {
2110                 pAd->StaCfg.RSNIE_Len = wrqu->data.length;
2111                 NdisMoveMemory(&pAd->StaCfg.RSN_IE[0], extra,
2112                                pAd->StaCfg.RSNIE_Len);
2113                 pAd->StaCfg.bRSN_IE_FromWpaSupplicant = TRUE;
2114         } else {
2115                 pAd->StaCfg.RSNIE_Len = 0;
2116                 NdisZeroMemory(&pAd->StaCfg.RSN_IE[0], MAX_LEN_OF_RSNIE);
2117         }
2118
2119         return 0;
2120 }
2121
2122 int rt_ioctl_giwgenie(struct net_device *dev,
2123                       struct iw_request_info *info,
2124                       union iwreq_data *wrqu, char *extra)
2125 {
2126         PRTMP_ADAPTER pAd = NULL;
2127
2128         GET_PAD_FROM_NET_DEV(pAd, dev);
2129
2130         if ((pAd->StaCfg.RSNIE_Len == 0) ||
2131             (pAd->StaCfg.AuthMode < Ndis802_11AuthModeWPA)) {
2132                 wrqu->data.length = 0;
2133                 return 0;
2134         }
2135
2136         if (pAd->StaCfg.WpaSupplicantUP == WPA_SUPPLICANT_ENABLE) {
2137                 if (wrqu->data.length < pAd->StaCfg.RSNIE_Len)
2138                         return -E2BIG;
2139
2140                 wrqu->data.length = pAd->StaCfg.RSNIE_Len;
2141                 memcpy(extra, &pAd->StaCfg.RSN_IE[0], pAd->StaCfg.RSNIE_Len);
2142         } else {
2143                 u8 RSNIe = IE_WPA;
2144
2145                 if (wrqu->data.length < (pAd->StaCfg.RSNIE_Len + 2))    /* ID, Len */
2146                         return -E2BIG;
2147                 wrqu->data.length = pAd->StaCfg.RSNIE_Len + 2;
2148
2149                 if ((pAd->StaCfg.AuthMode == Ndis802_11AuthModeWPA2PSK) ||
2150                     (pAd->StaCfg.AuthMode == Ndis802_11AuthModeWPA2))
2151                         RSNIe = IE_RSN;
2152
2153                 extra[0] = (char)RSNIe;
2154                 extra[1] = pAd->StaCfg.RSNIE_Len;
2155                 memcpy(extra + 2, &pAd->StaCfg.RSN_IE[0],
2156                        pAd->StaCfg.RSNIE_Len);
2157         }
2158
2159         return 0;
2160 }
2161
2162 int rt_ioctl_siwpmksa(struct net_device *dev,
2163                       struct iw_request_info *info,
2164                       union iwreq_data *wrqu, char *extra)
2165 {
2166         PRTMP_ADAPTER pAd = NULL;
2167         struct iw_pmksa *pPmksa = (struct iw_pmksa *)wrqu->data.pointer;
2168         int CachedIdx = 0, idx = 0;
2169
2170         GET_PAD_FROM_NET_DEV(pAd, dev);
2171
2172         if (pPmksa == NULL)
2173                 return -EINVAL;
2174
2175         DBGPRINT(RT_DEBUG_TRACE, ("===> rt_ioctl_siwpmksa\n"));
2176         switch (pPmksa->cmd) {
2177         case IW_PMKSA_FLUSH:
2178                 NdisZeroMemory(pAd->StaCfg.SavedPMK,
2179                                sizeof(BSSID_INFO) * PMKID_NO);
2180                 DBGPRINT(RT_DEBUG_TRACE,
2181                          ("rt_ioctl_siwpmksa - IW_PMKSA_FLUSH\n"));
2182                 break;
2183         case IW_PMKSA_REMOVE:
2184                 for (CachedIdx = 0; CachedIdx < pAd->StaCfg.SavedPMKNum;
2185                      CachedIdx++) {
2186                         /* compare the BSSID */
2187                         if (NdisEqualMemory
2188                             (pPmksa->bssid.sa_data,
2189                              pAd->StaCfg.SavedPMK[CachedIdx].BSSID,
2190                              MAC_ADDR_LEN)) {
2191                                 NdisZeroMemory(pAd->StaCfg.SavedPMK[CachedIdx].
2192                                                BSSID, MAC_ADDR_LEN);
2193                                 NdisZeroMemory(pAd->StaCfg.SavedPMK[CachedIdx].
2194                                                PMKID, 16);
2195                                 for (idx = CachedIdx;
2196                                      idx < (pAd->StaCfg.SavedPMKNum - 1);
2197                                      idx++) {
2198                                         NdisMoveMemory(&pAd->StaCfg.
2199                                                        SavedPMK[idx].BSSID[0],
2200                                                        &pAd->StaCfg.
2201                                                        SavedPMK[idx +
2202                                                                 1].BSSID[0],
2203                                                        MAC_ADDR_LEN);
2204                                         NdisMoveMemory(&pAd->StaCfg.
2205                                                        SavedPMK[idx].PMKID[0],
2206                                                        &pAd->StaCfg.
2207                                                        SavedPMK[idx +
2208                                                                 1].PMKID[0],
2209                                                        16);
2210                                 }
2211                                 pAd->StaCfg.SavedPMKNum--;
2212                                 break;
2213                         }
2214                 }
2215
2216                 DBGPRINT(RT_DEBUG_TRACE,
2217                          ("rt_ioctl_siwpmksa - IW_PMKSA_REMOVE\n"));
2218                 break;
2219         case IW_PMKSA_ADD:
2220                 for (CachedIdx = 0; CachedIdx < pAd->StaCfg.SavedPMKNum;
2221                      CachedIdx++) {
2222                         /* compare the BSSID */
2223                         if (NdisEqualMemory
2224                             (pPmksa->bssid.sa_data,
2225                              pAd->StaCfg.SavedPMK[CachedIdx].BSSID,
2226                              MAC_ADDR_LEN))
2227                                 break;
2228                 }
2229
2230                 /* Found, replace it */
2231                 if (CachedIdx < PMKID_NO) {
2232                         DBGPRINT(RT_DEBUG_OFF,
2233                                  ("Update PMKID, idx = %d\n", CachedIdx));
2234                         NdisMoveMemory(&pAd->StaCfg.SavedPMK[CachedIdx].
2235                                        BSSID[0], pPmksa->bssid.sa_data,
2236                                        MAC_ADDR_LEN);
2237                         NdisMoveMemory(&pAd->StaCfg.SavedPMK[CachedIdx].
2238                                        PMKID[0], pPmksa->pmkid, 16);
2239                         pAd->StaCfg.SavedPMKNum++;
2240                 }
2241                 /* Not found, replace the last one */
2242                 else {
2243                         /* Randomly replace one */
2244                         CachedIdx = (pPmksa->bssid.sa_data[5] % PMKID_NO);
2245                         DBGPRINT(RT_DEBUG_OFF,
2246                                  ("Update PMKID, idx = %d\n", CachedIdx));
2247                         NdisMoveMemory(&pAd->StaCfg.SavedPMK[CachedIdx].
2248                                        BSSID[0], pPmksa->bssid.sa_data,
2249                                        MAC_ADDR_LEN);
2250                         NdisMoveMemory(&pAd->StaCfg.SavedPMK[CachedIdx].
2251                                        PMKID[0], pPmksa->pmkid, 16);
2252                 }
2253
2254                 DBGPRINT(RT_DEBUG_TRACE,
2255                          ("rt_ioctl_siwpmksa - IW_PMKSA_ADD\n"));
2256                 break;
2257         default:
2258                 DBGPRINT(RT_DEBUG_TRACE,
2259                          ("rt_ioctl_siwpmksa - Unknow Command!!\n"));
2260                 break;
2261         }
2262
2263         return 0;
2264 }
2265
2266 int rt_ioctl_siwrate(struct net_device *dev,
2267                      struct iw_request_info *info,
2268                      union iwreq_data *wrqu, char *extra)
2269 {
2270         PRTMP_ADAPTER pAd = NULL;
2271         u32 rate = wrqu->bitrate.value, fixed = wrqu->bitrate.fixed;
2272
2273         GET_PAD_FROM_NET_DEV(pAd, dev);
2274
2275         /*check if the interface is down */
2276         if (!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_INTERRUPT_IN_USE)) {
2277                 DBGPRINT(RT_DEBUG_TRACE,
2278                          ("rt_ioctl_siwrate::Network is down!\n"));
2279                 return -ENETDOWN;
2280         }
2281
2282         DBGPRINT(RT_DEBUG_TRACE,
2283                  ("rt_ioctl_siwrate::(rate = %d, fixed = %d)\n", rate, fixed));
2284         /* rate = -1 => auto rate
2285            rate = X, fixed = 1 => (fixed rate X)
2286          */
2287         if (rate == -1) {
2288                 /*Auto Rate */
2289                 pAd->StaCfg.DesiredTransmitSetting.field.MCS = MCS_AUTO;
2290                 pAd->StaCfg.bAutoTxRateSwitch = TRUE;
2291                 if ((pAd->CommonCfg.PhyMode <= PHY_11G) ||
2292                     (pAd->MacTab.Content[BSSID_WCID].HTPhyMode.field.MODE <=
2293                      MODE_OFDM))
2294                         RTMPSetDesiredRates(pAd, -1);
2295
2296                 SetCommonHT(pAd);
2297         } else {
2298                 if (fixed) {
2299                         pAd->StaCfg.bAutoTxRateSwitch = FALSE;
2300                         if ((pAd->CommonCfg.PhyMode <= PHY_11G) ||
2301                             (pAd->MacTab.Content[BSSID_WCID].HTPhyMode.field.
2302                              MODE <= MODE_OFDM))
2303                                 RTMPSetDesiredRates(pAd, rate);
2304                         else {
2305                                 pAd->StaCfg.DesiredTransmitSetting.field.MCS =
2306                                     MCS_AUTO;
2307                                 SetCommonHT(pAd);
2308                         }
2309                         DBGPRINT(RT_DEBUG_TRACE,
2310                                  ("rt_ioctl_siwrate::(HtMcs=%d)\n",
2311                                   pAd->StaCfg.DesiredTransmitSetting.field.
2312                                   MCS));
2313                 } else {
2314                         /* TODO: rate = X, fixed = 0 => (rates <= X) */
2315                         return -EOPNOTSUPP;
2316                 }
2317         }
2318
2319         return 0;
2320 }
2321
2322 int rt_ioctl_giwrate(struct net_device *dev,
2323                      struct iw_request_info *info,
2324                      union iwreq_data *wrqu, char *extra)
2325 {
2326         PRTMP_ADAPTER pAd = NULL;
2327         int rate_index = 0, rate_count = 0;
2328         HTTRANSMIT_SETTING ht_setting;
2329 /* Remove to global variable
2330     __s32 ralinkrate[] =
2331         {2,  4,   11,  22, // CCK
2332         12, 18,   24,  36, 48, 72, 96, 108, // OFDM
2333         13, 26,   39,  52,  78, 104, 117, 130, 26,  52,  78, 104, 156, 208, 234, 260, // 20MHz, 800ns GI, MCS: 0 ~ 15
2334         39, 78,  117, 156, 234, 312, 351, 390,                                                                            // 20MHz, 800ns GI, MCS: 16 ~ 23
2335         27, 54,   81, 108, 162, 216, 243, 270, 54, 108, 162, 216, 324, 432, 486, 540, // 40MHz, 800ns GI, MCS: 0 ~ 15
2336         81, 162, 243, 324, 486, 648, 729, 810,                                                                            // 40MHz, 800ns GI, MCS: 16 ~ 23
2337         14, 29,   43,  57,  87, 115, 130, 144, 29, 59,   87, 115, 173, 230, 260, 288, // 20MHz, 400ns GI, MCS: 0 ~ 15
2338         43, 87,  130, 173, 260, 317, 390, 433,                                                                            // 20MHz, 400ns GI, MCS: 16 ~ 23
2339         30, 60,   90, 120, 180, 240, 270, 300, 60, 120, 180, 240, 360, 480, 540, 600, // 40MHz, 400ns GI, MCS: 0 ~ 15
2340         90, 180, 270, 360, 540, 720, 810, 900};                                                                           // 40MHz, 400ns GI, MCS: 16 ~ 23
2341 */
2342         GET_PAD_FROM_NET_DEV(pAd, dev);
2343
2344         rate_count = sizeof(ralinkrate) / sizeof(__s32);
2345         /*check if the interface is down */
2346         if (!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_INTERRUPT_IN_USE)) {
2347                 DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
2348                 return -ENETDOWN;
2349         }
2350
2351         if ((pAd->StaCfg.bAutoTxRateSwitch == FALSE) &&
2352             (INFRA_ON(pAd)) &&
2353             ((pAd->CommonCfg.PhyMode <= PHY_11G)
2354              || (pAd->MacTab.Content[BSSID_WCID].HTPhyMode.field.MODE <=
2355                  MODE_OFDM)))
2356                 ht_setting.word = pAd->StaCfg.HTPhyMode.word;
2357         else
2358                 ht_setting.word =
2359                     pAd->MacTab.Content[BSSID_WCID].HTPhyMode.word;
2360
2361         if (ht_setting.field.MODE >= MODE_HTMIX) {
2362 /*      rate_index = 12 + ((u8)ht_setting.field.BW *16) + ((u8)ht_setting.field.ShortGI *32) + ((u8)ht_setting.field.MCS); */
2363                 rate_index =
2364                     12 + ((u8)ht_setting.field.BW * 24) +
2365                     ((u8)ht_setting.field.ShortGI * 48) +
2366                     ((u8)ht_setting.field.MCS);
2367         } else if (ht_setting.field.MODE == MODE_OFDM)
2368                 rate_index = (u8)(ht_setting.field.MCS) + 4;
2369         else if (ht_setting.field.MODE == MODE_CCK)
2370                 rate_index = (u8)(ht_setting.field.MCS);
2371
2372         if (rate_index < 0)
2373                 rate_index = 0;
2374
2375         if (rate_index > rate_count)
2376                 rate_index = rate_count;
2377
2378         wrqu->bitrate.value = ralinkrate[rate_index] * 500000;
2379         wrqu->bitrate.disabled = 0;
2380
2381         return 0;
2382 }
2383
2384 static const iw_handler rt_handler[] = {
2385         (iw_handler) NULL,      /* SIOCSIWCOMMIT */
2386         (iw_handler) rt_ioctl_giwname,  /* SIOCGIWNAME   */
2387         (iw_handler) NULL,      /* SIOCSIWNWID   */
2388         (iw_handler) NULL,      /* SIOCGIWNWID   */
2389         (iw_handler) rt_ioctl_siwfreq,  /* SIOCSIWFREQ   */
2390         (iw_handler) rt_ioctl_giwfreq,  /* SIOCGIWFREQ   */
2391         (iw_handler) rt_ioctl_siwmode,  /* SIOCSIWMODE   */
2392         (iw_handler) rt_ioctl_giwmode,  /* SIOCGIWMODE   */
2393         (iw_handler) NULL,      /* SIOCSIWSENS   */
2394         (iw_handler) NULL,      /* SIOCGIWSENS   */
2395         (iw_handler) NULL /* not used */ ,      /* SIOCSIWRANGE  */
2396         (iw_handler) rt_ioctl_giwrange, /* SIOCGIWRANGE  */
2397         (iw_handler) NULL /* not used */ ,      /* SIOCSIWPRIV   */
2398         (iw_handler) NULL /* kernel code */ ,   /* SIOCGIWPRIV   */
2399         (iw_handler) NULL /* not used */ ,      /* SIOCSIWSTATS  */
2400         (iw_handler) rt28xx_get_wireless_stats /* kernel code */ ,      /* SIOCGIWSTATS  */
2401         (iw_handler) NULL,      /* SIOCSIWSPY    */
2402         (iw_handler) NULL,      /* SIOCGIWSPY    */
2403         (iw_handler) NULL,      /* SIOCSIWTHRSPY */
2404         (iw_handler) NULL,      /* SIOCGIWTHRSPY */
2405         (iw_handler) rt_ioctl_siwap,    /* SIOCSIWAP     */
2406         (iw_handler) rt_ioctl_giwap,    /* SIOCGIWAP     */
2407         (iw_handler) rt_ioctl_siwmlme,  /* SIOCSIWMLME   */
2408         (iw_handler) rt_ioctl_iwaplist, /* SIOCGIWAPLIST */
2409         (iw_handler) rt_ioctl_siwscan,  /* SIOCSIWSCAN   */
2410         (iw_handler) rt_ioctl_giwscan,  /* SIOCGIWSCAN   */
2411         (iw_handler) rt_ioctl_siwessid, /* SIOCSIWESSID  */
2412         (iw_handler) rt_ioctl_giwessid, /* SIOCGIWESSID  */
2413         (iw_handler) rt_ioctl_siwnickn, /* SIOCSIWNICKN  */
2414         (iw_handler) rt_ioctl_giwnickn, /* SIOCGIWNICKN  */
2415         (iw_handler) NULL,      /* -- hole --    */
2416         (iw_handler) NULL,      /* -- hole --    */
2417         (iw_handler) rt_ioctl_siwrate,  /* SIOCSIWRATE   */
2418         (iw_handler) rt_ioctl_giwrate,  /* SIOCGIWRATE   */
2419         (iw_handler) rt_ioctl_siwrts,   /* SIOCSIWRTS    */
2420         (iw_handler) rt_ioctl_giwrts,   /* SIOCGIWRTS    */
2421         (iw_handler) rt_ioctl_siwfrag,  /* SIOCSIWFRAG   */
2422         (iw_handler) rt_ioctl_giwfrag,  /* SIOCGIWFRAG   */
2423         (iw_handler) NULL,      /* SIOCSIWTXPOW  */
2424         (iw_handler) NULL,      /* SIOCGIWTXPOW  */
2425         (iw_handler) NULL,      /* SIOCSIWRETRY  */
2426         (iw_handler) NULL,      /* SIOCGIWRETRY  */
2427         (iw_handler) rt_ioctl_siwencode,        /* SIOCSIWENCODE */
2428         (iw_handler) rt_ioctl_giwencode,        /* SIOCGIWENCODE */
2429         (iw_handler) NULL,      /* SIOCSIWPOWER  */
2430         (iw_handler) NULL,      /* SIOCGIWPOWER  */
2431         (iw_handler) NULL,      /* -- hole -- */
2432         (iw_handler) NULL,      /* -- hole -- */
2433         (iw_handler) rt_ioctl_siwgenie, /* SIOCSIWGENIE  */
2434         (iw_handler) rt_ioctl_giwgenie, /* SIOCGIWGENIE  */
2435         (iw_handler) rt_ioctl_siwauth,  /* SIOCSIWAUTH   */
2436         (iw_handler) rt_ioctl_giwauth,  /* SIOCGIWAUTH   */
2437         (iw_handler) rt_ioctl_siwencodeext,     /* SIOCSIWENCODEEXT */
2438         (iw_handler) rt_ioctl_giwencodeext,     /* SIOCGIWENCODEEXT */
2439         (iw_handler) rt_ioctl_siwpmksa, /* SIOCSIWPMKSA  */
2440 };
2441
2442 const struct iw_handler_def rt28xx_iw_handler_def = {
2443         .standard = (iw_handler *) rt_handler,
2444         .num_standard = sizeof(rt_handler) / sizeof(iw_handler),
2445 #if IW_HANDLER_VERSION >= 7
2446         .get_wireless_stats = rt28xx_get_wireless_stats,
2447 #endif
2448 };
2449
2450 int rt28xx_sta_ioctl(IN struct net_device *net_dev,
2451                      IN OUT struct ifreq *rq, int cmd)
2452 {
2453         struct os_cookie *pObj;
2454         RTMP_ADAPTER *pAd = NULL;
2455         struct iwreq *wrq = (struct iwreq *)rq;
2456         BOOLEAN StateMachineTouched = FALSE;
2457         int Status = NDIS_STATUS_SUCCESS;
2458
2459         GET_PAD_FROM_NET_DEV(pAd, net_dev);
2460
2461         pObj = (struct os_cookie *)pAd->OS_Cookie;
2462
2463         /*check if the interface is down */
2464         if (!RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_INTERRUPT_IN_USE)) {
2465                 {
2466                         DBGPRINT(RT_DEBUG_TRACE, ("INFO::Network is down!\n"));
2467                         return -ENETDOWN;
2468                 }
2469         }
2470
2471         {                       /* determine this ioctl command is comming from which interface. */
2472                 pObj->ioctl_if_type = INT_MAIN;
2473                 pObj->ioctl_if = MAIN_MBSSID;
2474         }
2475
2476         switch (cmd) {
2477         case SIOCGIFHWADDR:
2478                 DBGPRINT(RT_DEBUG_TRACE, ("IOCTL::SIOCGIFHWADDR\n"));
2479                 memcpy(wrq->u.name, pAd->CurrentAddress, ETH_ALEN);
2480                 break;
2481         case SIOCGIWNAME:
2482                 {
2483                         char *name = &wrq->u.name[0];
2484                         rt_ioctl_giwname(net_dev, NULL, name, NULL);
2485                         break;
2486                 }
2487         case SIOCGIWESSID:      /*Get ESSID */
2488                 {
2489                         struct iw_point *essid = &wrq->u.essid;
2490                         rt_ioctl_giwessid(net_dev, NULL, essid, essid->pointer);
2491                         break;
2492                 }
2493         case SIOCSIWESSID:      /*Set ESSID */
2494                 {
2495                         struct iw_point *essid = &wrq->u.essid;
2496                         rt_ioctl_siwessid(net_dev, NULL, essid, essid->pointer);
2497                         break;
2498                 }
2499         case SIOCSIWNWID:       /* set network id (the cell) */
2500         case SIOCGIWNWID:       /* get network id */
2501                 Status = -EOPNOTSUPP;
2502                 break;
2503         case SIOCSIWFREQ:       /*set channel/frequency (Hz) */
2504                 {
2505                         struct iw_freq *freq = &wrq->u.freq;
2506                         rt_ioctl_siwfreq(net_dev, NULL, freq, NULL);
2507                         break;
2508                 }
2509         case SIOCGIWFREQ:       /* get channel/frequency (Hz) */
2510                 {
2511                         struct iw_freq *freq = &wrq->u.freq;
2512                         rt_ioctl_giwfreq(net_dev, NULL, freq, NULL);
2513                         break;
2514                 }
2515         case SIOCSIWNICKN:      /*set node name/nickname */
2516                 {
2517                         /*struct iw_point *data=&wrq->u.data; */
2518                         /*rt_ioctl_siwnickn(net_dev, NULL, data, NULL); */
2519                         break;
2520                 }
2521         case SIOCGIWNICKN:      /*get node name/nickname */
2522                 {
2523                         struct iw_point *erq = NULL;
2524                         erq = &wrq->u.data;
2525                         erq->length = strlen((char *)pAd->nickname);
2526                         Status =
2527                             copy_to_user(erq->pointer, pAd->nickname,
2528                                          erq->length);
2529                         break;
2530                 }
2531         case SIOCGIWRATE:       /*get default bit rate (bps) */
2532                 rt_ioctl_giwrate(net_dev, NULL, &wrq->u, NULL);
2533                 break;
2534         case SIOCSIWRATE:       /*set default bit rate (bps) */
2535                 rt_ioctl_siwrate(net_dev, NULL, &wrq->u, NULL);
2536                 break;
2537         case SIOCGIWRTS:        /* get RTS/CTS threshold (bytes) */
2538                 {
2539                         struct iw_param *rts = &wrq->u.rts;
2540                         rt_ioctl_giwrts(net_dev, NULL, rts, NULL);
2541                         break;
2542                 }
2543         case SIOCSIWRTS:        /*set RTS/CTS threshold (bytes) */
2544                 {
2545                         struct iw_param *rts = &wrq->u.rts;
2546                         rt_ioctl_siwrts(net_dev, NULL, rts, NULL);
2547                         break;
2548                 }
2549         case SIOCGIWFRAG:       /*get fragmentation thr (bytes) */
2550                 {
2551                         struct iw_param *frag = &wrq->u.frag;
2552                         rt_ioctl_giwfrag(net_dev, NULL, frag, NULL);
2553                         break;
2554                 }
2555         case SIOCSIWFRAG:       /*set fragmentation thr (bytes) */
2556                 {
2557                         struct iw_param *frag = &wrq->u.frag;
2558                         rt_ioctl_siwfrag(net_dev, NULL, frag, NULL);
2559                         break;
2560                 }
2561         case SIOCGIWENCODE:     /*get encoding token & mode */
2562                 {
2563                         struct iw_point *erq = &wrq->u.encoding;
2564                         if (erq)
2565                                 rt_ioctl_giwencode(net_dev, NULL, erq,
2566                                                    erq->pointer);
2567                         break;
2568                 }
2569         case SIOCSIWENCODE:     /*set encoding token & mode */
2570                 {
2571                         struct iw_point *erq = &wrq->u.encoding;
2572                         if (erq)
2573                                 rt_ioctl_siwencode(net_dev, NULL, erq,
2574                                                    erq->pointer);
2575                         break;
2576                 }
2577         case SIOCGIWAP: /*get access point MAC addresses */
2578                 {
2579                         struct sockaddr *ap_addr = &wrq->u.ap_addr;
2580                         rt_ioctl_giwap(net_dev, NULL, ap_addr,
2581                                        ap_addr->sa_data);
2582                         break;
2583                 }
2584         case SIOCSIWAP: /*set access point MAC addresses */
2585                 {
2586                         struct sockaddr *ap_addr = &wrq->u.ap_addr;
2587                         rt_ioctl_siwap(net_dev, NULL, ap_addr,
2588                                        ap_addr->sa_data);
2589                         break;
2590                 }
2591         case SIOCGIWMODE:       /*get operation mode */
2592                 {
2593                         __u32 *mode = &wrq->u.mode;
2594                         rt_ioctl_giwmode(net_dev, NULL, mode, NULL);
2595                         break;
2596                 }
2597         case SIOCSIWMODE:       /*set operation mode */
2598                 {
2599                         __u32 *mode = &wrq->u.mode;
2600                         rt_ioctl_siwmode(net_dev, NULL, mode, NULL);
2601                         break;
2602                 }
2603         case SIOCGIWSENS:       /*get sensitivity (dBm) */
2604         case SIOCSIWSENS:       /*set sensitivity (dBm) */
2605         case SIOCGIWPOWER:      /*get Power Management settings */
2606         case SIOCSIWPOWER:      /*set Power Management settings */
2607         case SIOCGIWTXPOW:      /*get transmit power (dBm) */
2608         case SIOCSIWTXPOW:      /*set transmit power (dBm) */
2609         case SIOCGIWRANGE:      /*Get range of parameters */
2610         case SIOCGIWRETRY:      /*get retry limits and lifetime */
2611         case SIOCSIWRETRY:      /*set retry limits and lifetime */
2612         case RT_PRIV_IOCTL:
2613         case RT_PRIV_IOCTL_EXT:
2614         case RTPRIV_IOCTL_SET:
2615         case RTPRIV_IOCTL_GSITESURVEY:
2616         case SIOCGIWPRIV:
2617                 Status = -EOPNOTSUPP;
2618                 break;
2619         case SIOCETHTOOL:
2620                 break;
2621         default:
2622                 DBGPRINT(RT_DEBUG_ERROR,
2623                          ("IOCTL::unknown IOCTL's cmd = 0x%08x\n", cmd));
2624                 Status = -EOPNOTSUPP;
2625                 break;
2626         }
2627
2628         if (StateMachineTouched)        /* Upper layer sent a MLME-related operations */
2629                 RTMP_MLME_HANDLER(pAd);
2630
2631         return Status;
2632 }
2633
2634 /*
2635     ==========================================================================
2636     Description:
2637         Set SSID
2638     Return:
2639         TRUE if all parameters are OK, FALSE otherwise
2640     ==========================================================================
2641 */
2642 int Set_SSID_Proc(IN PRTMP_ADAPTER pAdapter, char *arg)
2643 {
2644         NDIS_802_11_SSID Ssid, *pSsid = NULL;
2645         BOOLEAN StateMachineTouched = FALSE;
2646         int success = TRUE;
2647
2648         if (strlen(arg) <= MAX_LEN_OF_SSID) {
2649                 NdisZeroMemory(&Ssid, sizeof(NDIS_802_11_SSID));
2650                 if (strlen(arg) != 0) {
2651                         NdisMoveMemory(Ssid.Ssid, arg, strlen(arg));
2652                         Ssid.SsidLength = strlen(arg);
2653                 } else          /*ANY ssid */
2654                 {
2655                         Ssid.SsidLength = 0;
2656                         memcpy(Ssid.Ssid, "", 0);
2657                         pAdapter->StaCfg.BssType = BSS_INFRA;
2658                         pAdapter->StaCfg.AuthMode = Ndis802_11AuthModeOpen;
2659                         pAdapter->StaCfg.WepStatus =
2660                             Ndis802_11EncryptionDisabled;
2661                 }
2662                 pSsid = &Ssid;
2663
2664                 if (pAdapter->Mlme.CntlMachine.CurrState != CNTL_IDLE) {
2665                         RTMP_MLME_RESET_STATE_MACHINE(pAdapter);
2666                         DBGPRINT(RT_DEBUG_TRACE,
2667                                  ("!!! MLME busy, reset MLME state machine !!!\n"));
2668                 }
2669
2670                 if ((pAdapter->StaCfg.WpaPassPhraseLen >= 8) &&
2671                     (pAdapter->StaCfg.WpaPassPhraseLen <= 64)) {
2672                         char passphrase_str[65] = { 0 };
2673                         u8 keyMaterial[40];
2674
2675                         RTMPMoveMemory(passphrase_str,
2676                                        pAdapter->StaCfg.WpaPassPhrase,
2677                                        pAdapter->StaCfg.WpaPassPhraseLen);
2678                         RTMPZeroMemory(pAdapter->StaCfg.PMK, 32);
2679                         if (pAdapter->StaCfg.WpaPassPhraseLen == 64) {
2680                                 AtoH((char *)pAdapter->StaCfg.WpaPassPhrase,
2681                                      pAdapter->StaCfg.PMK, 32);
2682                         } else {
2683                                 PasswordHash((char *)pAdapter->StaCfg.
2684                                              WpaPassPhrase, Ssid.Ssid,
2685                                              Ssid.SsidLength, keyMaterial);
2686                                 NdisMoveMemory(pAdapter->StaCfg.PMK,
2687                                                keyMaterial, 32);
2688                         }
2689                 }
2690
2691                 pAdapter->MlmeAux.CurrReqIsFromNdis = TRUE;
2692                 pAdapter->StaCfg.bScanReqIsFromWebUI = FALSE;
2693                 pAdapter->bConfigChanged = TRUE;
2694
2695                 MlmeEnqueue(pAdapter,
2696                             MLME_CNTL_STATE_MACHINE,
2697                             OID_802_11_SSID,
2698                             sizeof(NDIS_802_11_SSID), (void *) pSsid);
2699
2700                 StateMachineTouched = TRUE;
2701                 DBGPRINT(RT_DEBUG_TRACE,
2702                          ("Set_SSID_Proc::(Len=%d,Ssid=%s)\n", Ssid.SsidLength,
2703                           Ssid.Ssid));
2704         } else
2705                 success = FALSE;
2706
2707         if (StateMachineTouched)        /* Upper layer sent a MLME-related operations */
2708                 RTMP_MLME_HANDLER(pAdapter);
2709
2710         return success;
2711 }
2712
2713 /*
2714     ==========================================================================
2715     Description:
2716         Set Network Type(Infrastructure/Adhoc mode)
2717     Return:
2718         TRUE if all parameters are OK, FALSE otherwise
2719     ==========================================================================
2720 */
2721 int Set_NetworkType_Proc(IN PRTMP_ADAPTER pAdapter, char *arg)
2722 {
2723         u32 Value = 0;
2724
2725         if (strcmp(arg, "Adhoc") == 0) {
2726                 if (pAdapter->StaCfg.BssType != BSS_ADHOC) {
2727                         /* Config has changed */
2728                         pAdapter->bConfigChanged = TRUE;
2729                         if (MONITOR_ON(pAdapter)) {
2730                                 RTMP_IO_WRITE32(pAdapter, RX_FILTR_CFG,
2731                                                 STANORMAL);
2732                                 RTMP_IO_READ32(pAdapter, MAC_SYS_CTRL, &Value);
2733                                 Value &= (~0x80);
2734                                 RTMP_IO_WRITE32(pAdapter, MAC_SYS_CTRL, Value);
2735                                 OPSTATUS_CLEAR_FLAG(pAdapter,
2736                                                     fOP_STATUS_MEDIA_STATE_CONNECTED);
2737                                 pAdapter->StaCfg.bAutoReconnect = TRUE;
2738                                 LinkDown(pAdapter, FALSE);
2739                         }
2740                         if (INFRA_ON(pAdapter)) {
2741                                 /*BOOLEAN Cancelled; */
2742                                 /* Set the AutoReconnectSsid to prevent it reconnect to old SSID */
2743                                 /* Since calling this indicate user don't want to connect to that SSID anymore. */
2744                                 pAdapter->MlmeAux.AutoReconnectSsidLen = 32;
2745                                 NdisZeroMemory(pAdapter->MlmeAux.
2746                                                AutoReconnectSsid,
2747                                                pAdapter->MlmeAux.
2748                                                AutoReconnectSsidLen);
2749
2750                                 LinkDown(pAdapter, FALSE);
2751
2752                                 DBGPRINT(RT_DEBUG_TRACE,
2753                                          ("NDIS_STATUS_MEDIA_DISCONNECT Event BB!\n"));
2754                         }
2755                 }
2756                 pAdapter->StaCfg.BssType = BSS_ADHOC;
2757                 pAdapter->net_dev->type = pAdapter->StaCfg.OriDevType;
2758                 DBGPRINT(RT_DEBUG_TRACE,
2759                          ("===>Set_NetworkType_Proc::(AD-HOC)\n"));
2760         } else if (strcmp(arg, "Infra") == 0) {
2761                 if (pAdapter->StaCfg.BssType != BSS_INFRA) {
2762                         /* Config has changed */
2763                         pAdapter->bConfigChanged = TRUE;
2764                         if (MONITOR_ON(pAdapter)) {
2765                                 RTMP_IO_WRITE32(pAdapter, RX_FILTR_CFG,
2766                                                 STANORMAL);
2767                                 RTMP_IO_READ32(pAdapter, MAC_SYS_CTRL, &Value);
2768                                 Value &= (~0x80);
2769                                 RTMP_IO_WRITE32(pAdapter, MAC_SYS_CTRL, Value);
2770                                 OPSTATUS_CLEAR_FLAG(pAdapter,
2771                                                     fOP_STATUS_MEDIA_STATE_CONNECTED);
2772                                 pAdapter->StaCfg.bAutoReconnect = TRUE;
2773                                 LinkDown(pAdapter, FALSE);
2774                         }
2775                         if (ADHOC_ON(pAdapter)) {
2776                                 /* Set the AutoReconnectSsid to prevent it reconnect to old SSID */
2777                                 /* Since calling this indicate user don't want to connect to that SSID anymore. */
2778                                 pAdapter->MlmeAux.AutoReconnectSsidLen = 32;
2779                                 NdisZeroMemory(pAdapter->MlmeAux.
2780                                                AutoReconnectSsid,
2781                                                pAdapter->MlmeAux.
2782                                                AutoReconnectSsidLen);
2783
2784                                 LinkDown(pAdapter, FALSE);
2785                         }
2786                 }
2787                 pAdapter->StaCfg.BssType = BSS_INFRA;
2788                 pAdapter->net_dev->type = pAdapter->StaCfg.OriDevType;
2789                 DBGPRINT(RT_DEBUG_TRACE,
2790                          ("===>Set_NetworkType_Proc::(INFRA)\n"));
2791         } else if (strcmp(arg, "Monitor") == 0) {
2792                 u8 bbpValue = 0;
2793                 BCN_TIME_CFG_STRUC csr;
2794                 OPSTATUS_CLEAR_FLAG(pAdapter, fOP_STATUS_INFRA_ON);
2795                 OPSTATUS_CLEAR_FLAG(pAdapter, fOP_STATUS_ADHOC_ON);
2796                 OPSTATUS_SET_FLAG(pAdapter, fOP_STATUS_MEDIA_STATE_CONNECTED);
2797                 /* disable all periodic state machine */
2798                 pAdapter->StaCfg.bAutoReconnect = FALSE;
2799                 /* reset all mlme state machine */
2800                 RTMP_MLME_RESET_STATE_MACHINE(pAdapter);
2801                 DBGPRINT(RT_DEBUG_TRACE,
2802                          ("fOP_STATUS_MEDIA_STATE_CONNECTED \n"));
2803                 if (pAdapter->CommonCfg.CentralChannel == 0) {
2804                         if (pAdapter->CommonCfg.PhyMode == PHY_11AN_MIXED)
2805                                 pAdapter->CommonCfg.CentralChannel = 36;
2806                         else
2807                                 pAdapter->CommonCfg.CentralChannel = 6;
2808                 } else
2809                         N_ChannelCheck(pAdapter);
2810
2811                 if (pAdapter->CommonCfg.PhyMode >= PHY_11ABGN_MIXED &&
2812                     pAdapter->CommonCfg.RegTransmitSetting.field.BW == BW_40 &&
2813                     pAdapter->CommonCfg.RegTransmitSetting.field.EXTCHA ==
2814                     EXTCHA_ABOVE) {
2815                         /* 40MHz ,control channel at lower */
2816                         RTMP_BBP_IO_READ8_BY_REG_ID(pAdapter, BBP_R4,
2817                                                     &bbpValue);
2818                         bbpValue &= (~0x18);
2819                         bbpValue |= 0x10;
2820                         RTMP_BBP_IO_WRITE8_BY_REG_ID(pAdapter, BBP_R4,
2821                                                      bbpValue);
2822                         pAdapter->CommonCfg.BBPCurrentBW = BW_40;
2823                         /*  RX : control channel at lower */
2824                         RTMP_BBP_IO_READ8_BY_REG_ID(pAdapter, BBP_R3,
2825                                                     &bbpValue);
2826                         bbpValue &= (~0x20);
2827                         RTMP_BBP_IO_WRITE8_BY_REG_ID(pAdapter, BBP_R3,
2828                                                      bbpValue);
2829
2830                         RTMP_IO_READ32(pAdapter, TX_BAND_CFG, &Value);
2831                         Value &= 0xfffffffe;
2832                         RTMP_IO_WRITE32(pAdapter, TX_BAND_CFG, Value);
2833                         pAdapter->CommonCfg.CentralChannel =
2834                             pAdapter->CommonCfg.Channel + 2;
2835                         AsicSwitchChannel(pAdapter,
2836                                           pAdapter->CommonCfg.CentralChannel,
2837                                           FALSE);
2838                         AsicLockChannel(pAdapter,
2839                                         pAdapter->CommonCfg.CentralChannel);
2840                         DBGPRINT(RT_DEBUG_TRACE,
2841                                  ("BW_40 ,control_channel(%d), CentralChannel(%d) \n",
2842                                   pAdapter->CommonCfg.Channel,
2843                                   pAdapter->CommonCfg.CentralChannel));
2844                 } else if (pAdapter->CommonCfg.PhyMode >= PHY_11ABGN_MIXED
2845                            && pAdapter->CommonCfg.RegTransmitSetting.field.BW ==
2846                            BW_40
2847                            && pAdapter->CommonCfg.RegTransmitSetting.field.
2848                            EXTCHA == EXTCHA_BELOW) {
2849                         /* 40MHz ,control channel at upper */
2850                         RTMP_BBP_IO_READ8_BY_REG_ID(pAdapter, BBP_R4,
2851                                                     &bbpValue);
2852                         bbpValue &= (~0x18);
2853                         bbpValue |= 0x10;
2854                         RTMP_BBP_IO_WRITE8_BY_REG_ID(pAdapter, BBP_R4,
2855                                                      bbpValue);
2856                         pAdapter->CommonCfg.BBPCurrentBW = BW_40;
2857                         RTMP_IO_READ32(pAdapter, TX_BAND_CFG, &Value);
2858                         Value |= 0x1;
2859                         RTMP_IO_WRITE32(pAdapter, TX_BAND_CFG, Value);
2860
2861                         RTMP_BBP_IO_READ8_BY_REG_ID(pAdapter, BBP_R3,
2862                                                     &bbpValue);
2863                         bbpValue |= (0x20);
2864                         RTMP_BBP_IO_WRITE8_BY_REG_ID(pAdapter, BBP_R3,
2865                                                      bbpValue);
2866                         pAdapter->CommonCfg.CentralChannel =
2867                             pAdapter->CommonCfg.Channel - 2;
2868                         AsicSwitchChannel(pAdapter,
2869                                           pAdapter->CommonCfg.CentralChannel,
2870                                           FALSE);
2871                         AsicLockChannel(pAdapter,
2872                                         pAdapter->CommonCfg.CentralChannel);
2873                         DBGPRINT(RT_DEBUG_TRACE,
2874                                  ("BW_40 ,control_channel(%d), CentralChannel(%d) \n",
2875                                   pAdapter->CommonCfg.Channel,
2876                                   pAdapter->CommonCfg.CentralChannel));
2877                 } else {
2878                         /* 20MHz */
2879                         RTMP_BBP_IO_READ8_BY_REG_ID(pAdapter, BBP_R4,
2880                                                     &bbpValue);
2881                         bbpValue &= (~0x18);
2882                         RTMP_BBP_IO_WRITE8_BY_REG_ID(pAdapter, BBP_R4,
2883                                                      bbpValue);
2884                         pAdapter->CommonCfg.BBPCurrentBW = BW_20;
2885                         AsicSwitchChannel(pAdapter, pAdapter->CommonCfg.Channel,
2886                                           FALSE);
2887                         AsicLockChannel(pAdapter, pAdapter->CommonCfg.Channel);
2888                         DBGPRINT(RT_DEBUG_TRACE,
2889                                  ("BW_20, Channel(%d)\n",
2890                                   pAdapter->CommonCfg.Channel));
2891                 }
2892                 /* Enable Rx with promiscuous reception */
2893                 RTMP_IO_WRITE32(pAdapter, RX_FILTR_CFG, 0x3);
2894                 /* ASIC supporsts sniffer function with replacing RSSI with timestamp. */
2895                 /*RTMP_IO_READ32(pAdapter, MAC_SYS_CTRL, &Value); */
2896                 /*Value |= (0x80); */
2897                 /*RTMP_IO_WRITE32(pAdapter, MAC_SYS_CTRL, Value); */
2898                 /* disable sync */
2899                 RTMP_IO_READ32(pAdapter, BCN_TIME_CFG, &csr.word);
2900                 csr.field.bBeaconGen = 0;
2901                 csr.field.bTBTTEnable = 0;
2902                 csr.field.TsfSyncMode = 0;
2903                 RTMP_IO_WRITE32(pAdapter, BCN_TIME_CFG, csr.word);
2904
2905                 pAdapter->StaCfg.BssType = BSS_MONITOR;
2906                 pAdapter->net_dev->type = ARPHRD_IEEE80211_PRISM;       /*ARPHRD_IEEE80211; // IEEE80211 */
2907                 DBGPRINT(RT_DEBUG_TRACE,
2908                          ("===>Set_NetworkType_Proc::(MONITOR)\n"));
2909         }
2910         /* Reset Ralink supplicant to not use, it will be set to start when UI set PMK key */
2911         pAdapter->StaCfg.WpaState = SS_NOTUSE;
2912
2913         DBGPRINT(RT_DEBUG_TRACE,
2914                  ("Set_NetworkType_Proc::(NetworkType=%d)\n",
2915                   pAdapter->StaCfg.BssType));
2916
2917         return TRUE;
2918 }