Merge tag 'pinctrl-v3.17-1' of git://git.kernel.org/pub/scm/linux/kernel/git/linusw...
[cascardo/linux.git] / drivers / staging / vt6655 / iwctl.c
1 /*
2  * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
3  * All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * File: iwctl.c
20  *
21  * Purpose:  wireless ext & ioctl functions
22  *
23  * Author: Lyndon Chen
24  *
25  * Date: July 5, 2006
26  *
27  * Functions:
28  *
29  * Revision History:
30  *
31  */
32
33 #include "device.h"
34 #include "ioctl.h"
35 #include "iocmd.h"
36 #include "iwctl.h"
37 #include "mac.h"
38 #include "card.h"
39 #include "hostap.h"
40 #include "power.h"
41 #include "rf.h"
42
43 #ifdef WPA_SUPPLICANT_DRIVER_WEXT_SUPPORT
44 #include "iowpa.h"
45 #include "wpactl.h"
46 #endif
47
48 #include <net/iw_handler.h>
49 extern unsigned short TxRate_iwconfig;//2008-5-8 <add> by chester
50
51 /*---------------------  Static Definitions -------------------------*/
52
53 //2008-0409-07, <Add> by Einsn Liu
54 #ifdef WPA_SUPPLICANT_DRIVER_WEXT_SUPPORT
55 #define SUPPORTED_WIRELESS_EXT                  18
56 #else
57 #define SUPPORTED_WIRELESS_EXT                  17
58 #endif
59
60 static const long frequency_list[] = {
61         2412, 2417, 2422, 2427, 2432, 2437, 2442, 2447, 2452, 2457, 2462, 2467, 2472, 2484,
62         4915, 4920, 4925, 4935, 4940, 4945, 4960, 4980,
63         5035, 5040, 5045, 5055, 5060, 5080, 5170, 5180, 5190, 5200, 5210, 5220, 5230, 5240,
64         5260, 5280, 5300, 5320, 5500, 5520, 5540, 5560, 5580, 5600, 5620, 5640, 5660, 5680,
65         5700, 5745, 5765, 5785, 5805, 5825
66 };
67
68 /*---------------------  Static Classes  ----------------------------*/
69
70 static int msglevel = MSG_LEVEL_INFO;
71
72 /*---------------------  Static Variables  --------------------------*/
73 /*---------------------  Static Functions  --------------------------*/
74
75 /*---------------------  Export Variables  --------------------------*/
76
77 struct iw_statistics *iwctl_get_wireless_stats(struct net_device *dev)
78 {
79         PSDevice pDevice = netdev_priv(dev);
80         long ldBm;
81
82         pDevice->wstats.status = pDevice->eOPMode;
83 #ifdef Calcu_LinkQual
84         if (pDevice->scStatistic.LinkQuality > 100)
85                 pDevice->scStatistic.LinkQuality = 100;
86         pDevice->wstats.qual.qual = (unsigned char)pDevice->scStatistic.LinkQuality;
87 #else
88         pDevice->wstats.qual.qual = pDevice->byCurrSQ;
89 #endif
90         RFvRSSITodBm(pDevice, (unsigned char)(pDevice->uCurrRSSI), &ldBm);
91         pDevice->wstats.qual.level = ldBm;
92         pDevice->wstats.qual.noise = 0;
93         pDevice->wstats.qual.updated = 1;
94         pDevice->wstats.discard.nwid = 0;
95         pDevice->wstats.discard.code = 0;
96         pDevice->wstats.discard.fragment = 0;
97         pDevice->wstats.discard.retries = (unsigned long)pDevice->scStatistic.dwTsrErr;
98         pDevice->wstats.discard.misc = 0;
99         pDevice->wstats.miss.beacon = 0;
100
101         return &pDevice->wstats;
102 }
103
104 /*------------------------------------------------------------------*/
105
106 static int iwctl_commit(struct net_device *dev,
107                         struct iw_request_info *info,
108                         void *wrq,
109                         char *extra)
110 {
111         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCSIWCOMMIT\n");
112
113         return 0;
114 }
115 /*
116  * Wireless Handler : get protocol name
117  */
118
119 int iwctl_giwname(struct net_device *dev,
120                   struct iw_request_info *info,
121                   char *wrq,
122                   char *extra)
123 {
124         strcpy(wrq, "802.11-a/b/g");
125         return 0;
126 }
127
128 /*
129  * Wireless Handler : set scan
130  */
131
132 static int iwctl_siwscan(struct net_device *dev,
133                   struct iw_request_info *info,
134                   struct iw_point *wrq,
135                   char *extra)
136 {
137         PSDevice                pDevice = (PSDevice)netdev_priv(dev);
138         PSMgmtObject        pMgmt = &(pDevice->sMgmtObj);
139         struct iw_scan_req  *req = (struct iw_scan_req *)extra;
140         unsigned char abyScanSSID[WLAN_IEHDR_LEN + WLAN_SSID_MAXLEN + 1];
141         PWLAN_IE_SSID pItemSSID = NULL;
142
143         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCSIWSCAN\n");
144
145         if (pDevice->byReAssocCount > 0) {   //reject scan when re-associating!
146 //send scan event to wpa_Supplicant
147                 union iwreq_data wrqu;
148
149                 PRINT_K("wireless_send_event--->SIOCGIWSCAN(scan done)\n");
150                 memset(&wrqu, 0, sizeof(wrqu));
151                 wireless_send_event(pDevice->dev, SIOCGIWSCAN, &wrqu, NULL);
152                 return 0;
153         }
154
155         spin_lock_irq(&pDevice->lock);
156         BSSvClearBSSList((void *)pDevice, pDevice->bLinkPass);
157
158 //mike add: active scan OR passive scan OR desire_ssid scan
159         if (wrq->length == sizeof(struct iw_scan_req)) {
160                 if (wrq->flags & IW_SCAN_THIS_ESSID)  {                               //desire_ssid scan
161                         memset(abyScanSSID, 0, WLAN_IEHDR_LEN + WLAN_SSID_MAXLEN + 1);
162                         pItemSSID = (PWLAN_IE_SSID)abyScanSSID;
163                         pItemSSID->byElementID = WLAN_EID_SSID;
164                         memcpy(pItemSSID->abySSID, req->essid, (int)req->essid_len);
165                         if (pItemSSID->abySSID[req->essid_len - 1] == '\0') {
166                                 if (req->essid_len > 0)
167                                         pItemSSID->len = req->essid_len - 1;
168                         } else
169                                 pItemSSID->len = req->essid_len;
170                         pMgmt->eScanType = WMAC_SCAN_PASSIVE;
171                         PRINT_K("SIOCSIWSCAN:[desired_ssid=%s,len=%d]\n", ((PWLAN_IE_SSID)abyScanSSID)->abySSID,
172                                 ((PWLAN_IE_SSID)abyScanSSID)->len);
173                         bScheduleCommand((void *)pDevice, WLAN_CMD_BSSID_SCAN, abyScanSSID);
174                         spin_unlock_irq(&pDevice->lock);
175
176                         return 0;
177                 } else if (req->scan_type == IW_SCAN_TYPE_PASSIVE) {          //passive scan
178                         pMgmt->eScanType = WMAC_SCAN_PASSIVE;
179                 }
180         } else {           //active scan
181                 pMgmt->eScanType = WMAC_SCAN_ACTIVE;
182         }
183
184         pMgmt->eScanType = WMAC_SCAN_PASSIVE;
185         bScheduleCommand((void *)pDevice, WLAN_CMD_BSSID_SCAN, NULL);
186         spin_unlock_irq(&pDevice->lock);
187
188         return 0;
189 }
190
191 /*
192  * Wireless Handler : get scan results
193  */
194
195 static int iwctl_giwscan(struct net_device *dev,
196                   struct iw_request_info *info,
197                   struct iw_point *wrq,
198                   char *extra)
199 {
200         int ii, jj, kk;
201         PSDevice                pDevice = (PSDevice)netdev_priv(dev);
202         PSMgmtObject        pMgmt = &(pDevice->sMgmtObj);
203         PKnownBSS           pBSS;
204         PWLAN_IE_SSID       pItemSSID;
205         PWLAN_IE_SUPP_RATES pSuppRates, pExtSuppRates;
206         char *current_ev = extra;
207         char *end_buf = extra + IW_SCAN_MAX_DATA;
208         char *current_val = NULL;
209         struct iw_event iwe;
210         long ldBm;
211         char buf[MAX_WPA_IE_LEN * 2 + 30];
212
213         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCGIWSCAN\n");
214
215         if (pMgmt->eScanState ==  WMAC_IS_SCANNING) {
216                 // In scanning..
217                 return -EAGAIN;
218         }
219         pBSS = &(pMgmt->sBSSList[0]);
220         for (ii = 0, jj = 0; jj < MAX_BSS_NUM; jj++) {
221                 if (current_ev >= end_buf)
222                         break;
223                 pBSS = &(pMgmt->sBSSList[jj]);
224                 if (pBSS->bActive) {
225                         //ADD mac address
226                         memset(&iwe, 0, sizeof(iwe));
227                         iwe.cmd = SIOCGIWAP;
228                         iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
229                         memcpy(iwe.u.ap_addr.sa_data, pBSS->abyBSSID, WLAN_BSSID_LEN);
230                         current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe, IW_EV_ADDR_LEN);
231                         //ADD ssid
232                         memset(&iwe, 0, sizeof(iwe));
233                         iwe.cmd = SIOCGIWESSID;
234                         pItemSSID = (PWLAN_IE_SSID)pBSS->abySSID;
235                         iwe.u.data.length = pItemSSID->len;
236                         iwe.u.data.flags = 1;
237                         current_ev = iwe_stream_add_point(info, current_ev, end_buf, &iwe, pItemSSID->abySSID);
238                         //ADD mode
239                         memset(&iwe, 0, sizeof(iwe));
240                         iwe.cmd = SIOCGIWMODE;
241                         if (WLAN_GET_CAP_INFO_ESS(pBSS->wCapInfo))
242                                 iwe.u.mode = IW_MODE_INFRA;
243                         else
244                                 iwe.u.mode = IW_MODE_ADHOC;
245
246                         iwe.len = IW_EV_UINT_LEN;
247                         current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe, IW_EV_UINT_LEN);
248                         //ADD frequency
249                         pSuppRates = (PWLAN_IE_SUPP_RATES)pBSS->abySuppRates;
250                         pExtSuppRates = (PWLAN_IE_SUPP_RATES)pBSS->abyExtSuppRates;
251                         memset(&iwe, 0, sizeof(iwe));
252                         iwe.cmd = SIOCGIWFREQ;
253                         iwe.u.freq.m = pBSS->uChannel;
254                         iwe.u.freq.e = 0;
255                         iwe.u.freq.i = 0;
256                         current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe, IW_EV_FREQ_LEN);
257                         //2008-0409-04, <Add> by Einsn Liu
258                         {
259                                 int f = (int)pBSS->uChannel - 1;
260
261                                 if (f < 0)f = 0;
262                                 iwe.u.freq.m = frequency_list[f] * 100000;
263                                 iwe.u.freq.e = 1;
264                         }
265                         current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe, IW_EV_FREQ_LEN);
266                         //ADD quality
267                         memset(&iwe, 0, sizeof(iwe));
268                         iwe.cmd = IWEVQUAL;
269                         RFvRSSITodBm(pDevice, (unsigned char)(pBSS->uRSSI), &ldBm);
270                         iwe.u.qual.level = ldBm;
271                         iwe.u.qual.noise = 0;
272 //2008-0409-01, <Add> by Einsn Liu
273                         if (-ldBm < 50)
274                                 iwe.u.qual.qual = 100;
275                         else if (-ldBm > 90)
276                                 iwe.u.qual.qual = 0;
277                         else
278                                 iwe.u.qual.qual = (40 - (-ldBm - 50)) * 100 / 40;
279
280                         iwe.u.qual.updated = 7;
281
282                         current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe, IW_EV_QUAL_LEN);
283
284                         memset(&iwe, 0, sizeof(iwe));
285                         iwe.cmd = SIOCGIWENCODE;
286                         iwe.u.data.length = 0;
287                         if (WLAN_GET_CAP_INFO_PRIVACY(pBSS->wCapInfo))
288                                 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
289                         else
290                                 iwe.u.data.flags = IW_ENCODE_DISABLED;
291
292                         current_ev = iwe_stream_add_point(info, current_ev, end_buf, &iwe, pItemSSID->abySSID);
293
294                         memset(&iwe, 0, sizeof(iwe));
295                         iwe.cmd = SIOCGIWRATE;
296                         iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
297                         current_val = current_ev + IW_EV_LCP_LEN;
298
299                         for (kk = 0; kk < 12; kk++) {
300                                 if (pSuppRates->abyRates[kk] == 0)
301                                         break;
302                                 // Bit rate given in 500 kb/s units (+ 0x80)
303                                 iwe.u.bitrate.value = ((pSuppRates->abyRates[kk] & 0x7f) * 500000);
304                                 current_val = iwe_stream_add_value(info, current_ev, current_val, end_buf, &iwe, IW_EV_PARAM_LEN);
305                         }
306                         for (kk = 0; kk < 8; kk++) {
307                                 if (pExtSuppRates->abyRates[kk] == 0)
308                                         break;
309                                 // Bit rate given in 500 kb/s units (+ 0x80)
310                                 iwe.u.bitrate.value = ((pExtSuppRates->abyRates[kk] & 0x7f) * 500000);
311                                 current_val = iwe_stream_add_value(info, current_ev, current_val, end_buf, &iwe, IW_EV_PARAM_LEN);
312                         }
313
314                         if ((current_val - current_ev) > IW_EV_LCP_LEN)
315                                 current_ev = current_val;
316
317                         memset(&iwe, 0, sizeof(iwe));
318                         iwe.cmd = IWEVCUSTOM;
319                         sprintf(buf, "bcn_int=%d", pBSS->wBeaconInterval);
320                         iwe.u.data.length = strlen(buf);
321                         current_ev = iwe_stream_add_point(info, current_ev, end_buf, &iwe, buf);
322
323                         if ((pBSS->wWPALen > 0) && (pBSS->wWPALen <= MAX_WPA_IE_LEN)) {
324                                 memset(&iwe, 0, sizeof(iwe));
325                                 iwe.cmd = IWEVGENIE;
326                                 iwe.u.data.length = pBSS->wWPALen;
327                                 current_ev = iwe_stream_add_point(info, current_ev, end_buf, &iwe, pBSS->byWPAIE);
328                         }
329
330                         if ((pBSS->wRSNLen > 0) && (pBSS->wRSNLen <= MAX_WPA_IE_LEN)) {
331                                 memset(&iwe, 0, sizeof(iwe));
332                                 iwe.cmd = IWEVGENIE;
333                                 iwe.u.data.length = pBSS->wRSNLen;
334                                 current_ev = iwe_stream_add_point(info, current_ev, end_buf, &iwe, pBSS->byRSNIE);
335                         }
336
337                 }
338         }// for
339
340         wrq->length = current_ev - extra;
341         return 0;
342 }
343
344 /*
345  * Wireless Handler : set frequency or channel
346  */
347
348 int iwctl_siwfreq(struct net_device *dev,
349                   struct iw_request_info *info,
350                   struct iw_freq *wrq,
351                   char *extra)
352 {
353         PSDevice                pDevice = (PSDevice)netdev_priv(dev);
354         int rc = 0;
355
356         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCSIWFREQ\n");
357
358         // If setting by frequency, convert to a channel
359         if ((wrq->e == 1) &&
360             (wrq->m >= (int) 2.412e8) &&
361             (wrq->m <= (int) 2.487e8)) {
362                 int f = wrq->m / 100000;
363                 int c = 0;
364
365                 while ((c < 14) && (f != frequency_list[c]))
366                         c++;
367                 wrq->e = 0;
368                 wrq->m = c + 1;
369         }
370         // Setting by channel number
371         if ((wrq->m > 14) || (wrq->e > 0))
372                 rc = -EOPNOTSUPP;
373         else {
374                 int channel = wrq->m;
375
376                 if ((channel < 1) || (channel > 14)) {
377                         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "%s: New channel value of %d is invalid!\n", dev->name, wrq->m);
378                         rc = -EINVAL;
379                 } else {
380                         // Yes ! We can set it !!!
381                         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " Set to channel = %d\n", channel);
382                         pDevice->uChannel = channel;
383                         //2007-0207-04,<Add> by EinsnLiu
384                         //Make change effect at once
385                         pDevice->bCommit = true;
386                 }
387         }
388
389         return rc;
390 }
391
392 /*
393  * Wireless Handler : get frequency or channel
394  */
395
396 int iwctl_giwfreq(struct net_device *dev,
397                   struct iw_request_info *info,
398                   struct iw_freq *wrq,
399                   char *extra)
400 {
401         PSDevice                pDevice = (PSDevice)netdev_priv(dev);
402         PSMgmtObject        pMgmt = &(pDevice->sMgmtObj);
403
404         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCGIWFREQ\n");
405
406 #ifdef WEXT_USECHANNELS
407         wrq->m = (int)pMgmt->uCurrChannel;
408         wrq->e = 0;
409 #else
410         {
411                 int f = (int)pMgmt->uCurrChannel - 1;
412
413                 if (f < 0)
414                         f = 0;
415                 wrq->m = frequency_list[f] * 100000;
416                 wrq->e = 1;
417         }
418 #endif
419
420         return 0;
421 }
422
423 /*
424  * Wireless Handler : set operation mode
425  */
426
427 int iwctl_siwmode(struct net_device *dev,
428                   struct iw_request_info *info,
429                   __u32 *wmode,
430                   char *extra)
431 {
432         PSDevice                pDevice = (PSDevice)netdev_priv(dev);
433         PSMgmtObject        pMgmt = &(pDevice->sMgmtObj);
434         int rc = 0;
435
436         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCSIWMODE\n");
437
438         if (pMgmt->eCurrMode == WMAC_MODE_ESS_AP && pDevice->bEnableHostapd) {
439                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Can't set operation mode, hostapd is running\n");
440                 return rc;
441         }
442
443         switch (*wmode) {
444         case IW_MODE_ADHOC:
445                 if (pMgmt->eConfigMode != WMAC_CONFIG_IBSS_STA) {
446                         pMgmt->eConfigMode = WMAC_CONFIG_IBSS_STA;
447                         if (pDevice->flags & DEVICE_FLAGS_OPENED)
448                                 pDevice->bCommit = true;
449
450                 }
451                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "set mode to ad-hoc\n");
452                 break;
453         case IW_MODE_AUTO:
454         case IW_MODE_INFRA:
455                 if (pMgmt->eConfigMode != WMAC_CONFIG_ESS_STA) {
456                         pMgmt->eConfigMode = WMAC_CONFIG_ESS_STA;
457                         if (pDevice->flags & DEVICE_FLAGS_OPENED)
458                                 pDevice->bCommit = true;
459
460                 }
461                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "set mode to infrastructure\n");
462                 break;
463         case IW_MODE_MASTER:
464
465                 pMgmt->eConfigMode = WMAC_CONFIG_ESS_STA;
466                 rc = -EOPNOTSUPP;
467                 break;
468
469                 if (pMgmt->eConfigMode != WMAC_CONFIG_AP) {
470                         pMgmt->eConfigMode = WMAC_CONFIG_AP;
471                         if (pDevice->flags & DEVICE_FLAGS_OPENED)
472                                 pDevice->bCommit = true;
473
474                 }
475                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "set mode to Access Point\n");
476                 break;
477
478         case IW_MODE_REPEAT:
479                 pMgmt->eConfigMode = WMAC_CONFIG_ESS_STA;
480                 rc = -EOPNOTSUPP;
481                 break;
482         default:
483                 rc = -EINVAL;
484         }
485
486         return rc;
487 }
488
489 /*
490  * Wireless Handler : get operation mode
491  */
492
493 int iwctl_giwmode(struct net_device *dev,
494                   struct iw_request_info *info,
495                   __u32 *wmode,
496                   char *extra)
497 {
498         PSDevice                pDevice = (PSDevice)netdev_priv(dev);
499         PSMgmtObject        pMgmt = &(pDevice->sMgmtObj);
500
501         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCGIWMODE\n");
502         // If not managed, assume it's ad-hoc
503         switch (pMgmt->eConfigMode) {
504         case WMAC_CONFIG_ESS_STA:
505                 *wmode = IW_MODE_INFRA;
506                 break;
507         case WMAC_CONFIG_IBSS_STA:
508                 *wmode = IW_MODE_ADHOC;
509                 break;
510         case WMAC_CONFIG_AUTO:
511                 *wmode = IW_MODE_INFRA;
512                 break;
513         case WMAC_CONFIG_AP:
514                 *wmode = IW_MODE_MASTER;
515                 break;
516         default:
517                 *wmode = IW_MODE_ADHOC;
518         }
519
520         return 0;
521 }
522
523 /*
524  * Wireless Handler : get capability range
525  */
526
527 int iwctl_giwrange(struct net_device *dev,
528                    struct iw_request_info *info,
529                    struct iw_point *wrq,
530                    char *extra)
531 {
532         struct iw_range *range = (struct iw_range *)extra;
533         int i, k;
534         unsigned char abySupportedRates[13] = {0x02, 0x04, 0x0B, 0x16, 0x0c, 0x12, 0x18, 0x24, 0x30, 0x48, 0x60, 0x6C, 0x90};
535
536         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCGIWRANGE\n");
537         if (wrq->pointer) {
538                 wrq->length = sizeof(struct iw_range);
539                 memset(range, 0, sizeof(struct iw_range));
540                 range->min_nwid = 0x0000;
541                 range->max_nwid = 0x0000;
542                 range->num_channels = 14;
543                 // Should be based on cap_rid.country to give only
544                 //  what the current card support
545                 k = 0;
546                 for (i = 0; i < 14; i++) {
547                         range->freq[k].i = i + 1; // List index
548                         range->freq[k].m = frequency_list[i] * 100000;
549                         range->freq[k++].e = 1; // Values in table in MHz -> * 10^5 * 10
550                 }
551                 range->num_frequency = k;
552                 // Hum... Should put the right values there
553 #ifdef Calcu_LinkQual
554                 range->max_qual.qual = 100;
555 #else
556                 range->max_qual.qual = 255;
557 #endif
558                 range->max_qual.level = 0;
559                 range->max_qual.noise = 0;
560                 range->sensitivity = 255;
561
562                 for (i = 0; i < 13; i++) {
563                         range->bitrate[i] = abySupportedRates[i] * 500000;
564                         if (range->bitrate[i] == 0)
565                                 break;
566                 }
567                 range->num_bitrates = i;
568
569                 // Set an indication of the max TCP throughput
570                 // in bit/s that we can expect using this interface.
571                 //  May be use for QoS stuff... Jean II
572                 if (i > 2)
573                         range->throughput = 5 * 1000 * 1000;
574                 else
575                         range->throughput = 1.5 * 1000 * 1000;
576
577                 range->min_rts = 0;
578                 range->max_rts = 2312;
579                 range->min_frag = 256;
580                 range->max_frag = 2312;
581
582                 // the encoding capabilities
583                 range->num_encoding_sizes = 3;
584                 // 64(40) bits WEP
585                 range->encoding_size[0] = 5;
586                 // 128(104) bits WEP
587                 range->encoding_size[1] = 13;
588                 // 256 bits for WPA-PSK
589                 range->encoding_size[2] = 32;
590                 // 4 keys are allowed
591                 range->max_encoding_tokens = 4;
592
593                 range->enc_capa = IW_ENC_CAPA_WPA | IW_ENC_CAPA_WPA2 |
594                         IW_ENC_CAPA_CIPHER_TKIP | IW_ENC_CAPA_CIPHER_CCMP;
595
596                 range->min_pmp = 0;
597                 range->max_pmp = 1000000;// 1 secs
598                 range->min_pmt = 0;
599                 range->max_pmt = 1000000;// 1 secs
600                 range->pmp_flags = IW_POWER_PERIOD;
601                 range->pmt_flags = IW_POWER_TIMEOUT;
602                 range->pm_capa = IW_POWER_PERIOD | IW_POWER_TIMEOUT | IW_POWER_ALL_R;
603
604                 // Transmit Power - values are in mW
605
606                 range->txpower[0] = 100;
607                 range->num_txpower = 1;
608                 range->txpower_capa = IW_TXPOW_MWATT;
609                 range->we_version_source = SUPPORTED_WIRELESS_EXT;
610                 range->we_version_compiled = WIRELESS_EXT;
611                 range->retry_capa = IW_RETRY_LIMIT | IW_RETRY_LIFETIME;
612                 range->retry_flags = IW_RETRY_LIMIT;
613                 range->r_time_flags = IW_RETRY_LIFETIME;
614                 range->min_retry = 1;
615                 range->max_retry = 65535;
616                 range->min_r_time = 1024;
617                 range->max_r_time = 65535 * 1024;
618                 // Experimental measurements - boundary 11/5.5 Mb/s
619                 // Note : with or without the (local->rssi), results
620                 //  are somewhat different. - Jean II
621                 range->avg_qual.qual = 6;
622                 range->avg_qual.level = 176;    // -80 dBm
623                 range->avg_qual.noise = 0;
624         }
625
626         return 0;
627 }
628
629 /*
630  * Wireless Handler : set ap mac address
631  */
632
633 int iwctl_siwap(struct net_device *dev,
634                 struct iw_request_info *info,
635                 struct sockaddr *wrq,
636                 char *extra)
637 {
638         PSDevice                pDevice = (PSDevice)netdev_priv(dev);
639         PSMgmtObject        pMgmt = &(pDevice->sMgmtObj);
640         int rc = 0;
641         unsigned char ZeroBSSID[WLAN_BSSID_LEN] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
642
643         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCSIWAP\n");
644         if (pMgmt->eScanState ==  WMAC_IS_SCANNING) {
645                 // In scanning..
646                 pr_debug("SIOCSIWAP(??)-->In scanning..\n");
647         }
648         if (wrq->sa_family != ARPHRD_ETHER)
649                 rc = -EINVAL;
650         else {
651                 memcpy(pMgmt->abyDesireBSSID, wrq->sa_data, 6);
652                 //2008-0409-05, <Add> by Einsn Liu
653                 if ((pDevice->bLinkPass == true) &&
654                     (memcmp(pMgmt->abyDesireBSSID, pMgmt->abyCurrBSSID, 6) == 0)) {
655                         return rc;
656                 }
657                 //mike :add
658                 if ((is_broadcast_ether_addr(pMgmt->abyDesireBSSID)) ||
659                     (memcmp(pMgmt->abyDesireBSSID, ZeroBSSID, 6) == 0)) {
660                         PRINT_K("SIOCSIWAP:invalid desired BSSID return!\n");
661                         return rc;
662                 }
663                 //mike add: if desired AP is hidden ssid(there are two same BSSID in list),
664                 //                  then ignore,because you don't known which one to be connect with??
665                 {
666                         unsigned int ii, uSameBssidNum = 0;
667
668                         for (ii = 0; ii < MAX_BSS_NUM; ii++) {
669                                 if (pMgmt->sBSSList[ii].bActive &&
670                                     ether_addr_equal(pMgmt->sBSSList[ii].abyBSSID,
671                                                      pMgmt->abyDesireBSSID)) {
672                                         uSameBssidNum++;
673                                 }
674                         }
675                         if (uSameBssidNum >= 2) {  //hit: desired AP is in hidden ssid mode!!!
676                                 PRINT_K("SIOCSIWAP:ignore for desired AP in hidden mode\n");
677                                 return rc;
678                         }
679                 }
680
681                 if (pDevice->flags & DEVICE_FLAGS_OPENED)
682                         pDevice->bCommit = true;
683
684         }
685         return rc;
686 }
687
688 /*
689  * Wireless Handler : get ap mac address
690  */
691
692 int iwctl_giwap(struct net_device *dev,
693                 struct iw_request_info *info,
694                 struct sockaddr *wrq,
695                 char *extra)
696 {
697         PSDevice                pDevice = (PSDevice)netdev_priv(dev);
698         PSMgmtObject        pMgmt = &(pDevice->sMgmtObj);
699
700         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCGIWAP\n");
701
702         memcpy(wrq->sa_data, pMgmt->abyCurrBSSID, 6);
703         //2008-0410,<Modify> by Einsn Liu
704         if ((pDevice->bLinkPass == false) && (pMgmt->eCurrMode != WMAC_MODE_ESS_AP))
705                 memset(wrq->sa_data, 0, 6);
706
707         if (pMgmt->eCurrMode == WMAC_MODE_ESS_AP)
708                 memcpy(wrq->sa_data, pMgmt->abyCurrBSSID, 6);
709
710         wrq->sa_family = ARPHRD_ETHER;
711
712         return 0;
713 }
714
715 /*
716  * Wireless Handler : get ap list
717  */
718
719 int iwctl_giwaplist(struct net_device *dev,
720                     struct iw_request_info *info,
721                     struct iw_point *wrq,
722                     char *extra)
723 {
724         int ii, jj, rc = 0;
725         struct sockaddr *sock   = NULL;
726         struct sockaddr *s      = NULL;
727         struct iw_quality *qual = NULL;
728         struct iw_quality *q    = NULL;
729         PKnownBSS pBSS          = NULL;
730
731         PSDevice pDevice = (PSDevice)netdev_priv(dev);
732         PSMgmtObject pMgmt = &(pDevice->sMgmtObj);
733
734         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCGIWAPLIST\n");
735
736         if (!capable(CAP_NET_ADMIN)) {
737                 rc = -EPERM;
738                 goto exit;
739         }
740
741         if (!wrq->pointer)
742                 goto exit;
743
744         sock = kmalloc_array(IW_MAX_AP, sizeof(struct sockaddr), GFP_KERNEL);
745         if (!sock) {
746                 rc = -ENOMEM;
747                 goto exit;
748         }
749
750         qual = kmalloc_array(IW_MAX_AP, sizeof(struct iw_quality), GFP_KERNEL);
751         if (!qual) {
752                 rc = -ENOMEM;
753                 goto exit;
754         }
755
756         for (ii = 0, jj = 0; ii < MAX_BSS_NUM; ii++) {
757                 pBSS = &(pMgmt->sBSSList[ii]);
758
759                 if (!pBSS->bActive)
760                         continue;
761                 if (jj >= IW_MAX_AP)
762                         break;
763
764                 s = &sock[jj];
765                 q = &qual[jj];
766
767                 memcpy(s->sa_data, pBSS->abyBSSID, 6);
768                 s->sa_family    = ARPHRD_ETHER;
769                 q->level        = pBSS->uRSSI;
770                 q->qual         = 0;
771                 q->noise        = 0;
772                 q->updated      = 2;
773                 jj++;
774         }
775
776         wrq->flags = 1; /* Should be define'd */
777         wrq->length = jj;
778         memcpy(extra, sock, sizeof(struct sockaddr) * jj);
779         memcpy(extra + sizeof(struct sockaddr) * jj,
780                 qual,
781                 sizeof(struct iw_quality) * jj);
782 exit:
783         kfree(sock);
784         kfree(qual);
785         return rc;
786 }
787
788 /*
789  * Wireless Handler : set essid
790  */
791
792 int iwctl_siwessid(struct net_device *dev,
793                    struct iw_request_info *info,
794                    struct iw_point *wrq,
795                    char *extra)
796 {
797         PSDevice                pDevice = (PSDevice)netdev_priv(dev);
798         PSMgmtObject        pMgmt = &(pDevice->sMgmtObj);
799         PWLAN_IE_SSID       pItemSSID;
800         //2008-0409-05, <Add> by Einsn Liu
801         unsigned char len;
802
803         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCSIWESSID\n");
804         pDevice->fWPA_Authened = false;
805         if (pMgmt->eScanState ==  WMAC_IS_SCANNING) {
806                 // In scanning..
807                 pr_debug("SIOCSIWESSID(??)-->In scanning..\n");
808         }
809         // Check if we asked for `any'
810         if (wrq->flags == 0) {
811                 // Just send an empty SSID list
812                 memset(pMgmt->abyDesireSSID, 0, WLAN_IEHDR_LEN + WLAN_SSID_MAXLEN + 1);
813                 memset(pMgmt->abyDesireBSSID, 0xFF, 6);
814                 PRINT_K("set essid to 'any'\n");
815 #ifdef WPA_SUPPLICANT_DRIVER_WEXT_SUPPORT
816                 return 0;
817 #endif
818         } else {
819                 // Set the SSID
820                 memset(pMgmt->abyDesireSSID, 0, WLAN_IEHDR_LEN + WLAN_SSID_MAXLEN + 1);
821                 pItemSSID = (PWLAN_IE_SSID)pMgmt->abyDesireSSID;
822                 pItemSSID->byElementID = WLAN_EID_SSID;
823
824                 memcpy(pItemSSID->abySSID, extra, wrq->length);
825                 if (pItemSSID->abySSID[wrq->length - 1] == '\0') {
826                         if (wrq->length > 0)
827                                 pItemSSID->len = wrq->length - 1;
828                 } else
829                         pItemSSID->len = wrq->length;
830                 pr_debug("set essid to %s\n", pItemSSID->abySSID);
831                 //2008-0409-05, <Add> by Einsn Liu
832                 len = (pItemSSID->len > ((PWLAN_IE_SSID)pMgmt->abyCurrSSID)->len) ? pItemSSID->len : ((PWLAN_IE_SSID)pMgmt->abyCurrSSID)->len;
833                 if ((pDevice->bLinkPass == true) &&
834                     (memcmp(pItemSSID->abySSID, ((PWLAN_IE_SSID)pMgmt->abyCurrSSID)->abySSID, len) == 0))
835                         return 0;
836
837                 //mike:need clear desiredBSSID
838                 if (pItemSSID->len == 0) {
839                         memset(pMgmt->abyDesireBSSID, 0xFF, 6);
840                         return 0;
841                 }
842
843 #ifdef WPA_SUPPLICANT_DRIVER_WEXT_SUPPORT
844                 //Wext wil order another command of siwap to link with desired AP,
845                 //so here need not associate??
846                 if (pDevice->bWPASuppWextEnabled == true)  {
847                         /*******search if  in hidden ssid mode ****/
848                         {
849                                 PKnownBSS       pCurr = NULL;
850                                 unsigned char abyTmpDesireSSID[WLAN_IEHDR_LEN + WLAN_SSID_MAXLEN + 1];
851                                 unsigned int ii, uSameBssidNum = 0;
852
853                                 memcpy(abyTmpDesireSSID, pMgmt->abyDesireSSID, sizeof(abyTmpDesireSSID));
854                                 pCurr = BSSpSearchBSSList(pDevice,
855                                                           NULL,
856                                                           abyTmpDesireSSID,
857                                                           pMgmt->eConfigPHYMode
858 );
859
860                                 if (pCurr == NULL) {
861                                         PRINT_K("SIOCSIWESSID:hidden ssid site survey before associate.......\n");
862                                         vResetCommandTimer((void *)pDevice);
863                                         pMgmt->eScanType = WMAC_SCAN_ACTIVE;
864                                         bScheduleCommand((void *)pDevice, WLAN_CMD_BSSID_SCAN, pMgmt->abyDesireSSID);
865                                         bScheduleCommand((void *)pDevice, WLAN_CMD_SSID, pMgmt->abyDesireSSID);
866                                 } else {  //mike:to find out if that desired SSID is a hidden-ssid AP ,
867                                         //         by means of judging if there are two same BSSID exist in list ?
868                                         for (ii = 0; ii < MAX_BSS_NUM; ii++) {
869                                                 if (pMgmt->sBSSList[ii].bActive &&
870                                                     ether_addr_equal(pMgmt->sBSSList[ii].abyBSSID,
871                                                                      pCurr->abyBSSID)) {
872                                                         uSameBssidNum++;
873                                                 }
874                                         }
875                                         if (uSameBssidNum >= 2) {  //hit: desired AP is in hidden ssid mode!!!
876                                                 pr_debug("SIOCSIWESSID:hidden ssid directly associate.......\n");
877                                                 vResetCommandTimer((void *)pDevice);
878                                                 pMgmt->eScanType = WMAC_SCAN_PASSIVE;          //this scan type,you'll submit scan result!
879                                                 bScheduleCommand((void *)pDevice, WLAN_CMD_BSSID_SCAN, pMgmt->abyDesireSSID);
880                                                 bScheduleCommand((void *)pDevice, WLAN_CMD_SSID, pMgmt->abyDesireSSID);
881                                         }
882                                 }
883                         }
884                         return 0;
885                 }
886 #endif
887
888                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "set essid = %s\n", pItemSSID->abySSID);
889         }
890
891         if (pDevice->flags & DEVICE_FLAGS_OPENED)
892                 pDevice->bCommit = true;
893
894         return 0;
895 }
896
897 /*
898  * Wireless Handler : get essid
899  */
900
901 int iwctl_giwessid(struct net_device *dev,
902                    struct iw_request_info *info,
903                    struct iw_point *wrq,
904                    char *extra)
905 {
906         PSDevice                pDevice = (PSDevice)netdev_priv(dev);
907         PSMgmtObject        pMgmt = &(pDevice->sMgmtObj);
908         PWLAN_IE_SSID       pItemSSID;
909
910         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCGIWESSID\n");
911
912         // Note : if wrq->u.data.flags != 0, we should
913         // get the relevant SSID from the SSID list...
914
915         // Get the current SSID
916         pItemSSID = (PWLAN_IE_SSID)pMgmt->abyCurrSSID;
917         memcpy(extra, pItemSSID->abySSID , pItemSSID->len);
918         extra[pItemSSID->len] = '\0';
919         wrq->length = pItemSSID->len + 1;
920         //2008-0409-03, <Add> by Einsn Liu
921         wrq->length = pItemSSID->len;
922         wrq->flags = 1; // active
923
924         return 0;
925 }
926
927 /*
928  * Wireless Handler : set data rate
929  */
930
931 int iwctl_siwrate(struct net_device *dev,
932                   struct iw_request_info *info,
933                   struct iw_param *wrq,
934                   char *extra)
935 {
936         PSDevice                pDevice = (PSDevice)netdev_priv(dev);
937         int rc = 0;
938         u8      brate = 0;
939         int     i;
940         unsigned char abySupportedRates[13] = {0x02, 0x04, 0x0B, 0x16, 0x0c, 0x12, 0x18, 0x24, 0x30, 0x48, 0x60, 0x6C, 0x90};
941
942         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCSIWRATE\n");
943         if (!(pDevice->flags & DEVICE_FLAGS_OPENED)) {
944                 rc = -EINVAL;
945                 return rc;
946         }
947
948         // First : get a valid bit rate value
949
950         // Which type of value
951         if ((wrq->value < 13) &&
952             (wrq->value >= 0)) {
953                 // Setting by rate index
954                 // Find value in the magic rate table
955                 brate = wrq->value;
956         } else {
957                 // Setting by frequency value
958                 u8      normvalue = (u8) (wrq->value/500000);
959
960                 // Check if rate is valid
961                 for (i = 0; i < 13; i++) {
962                         if (normvalue == abySupportedRates[i]) {
963                                 brate = i;
964                                 break;
965                         }
966                 }
967         }
968         // -1 designed the max rate (mostly auto mode)
969         if (wrq->value == -1) {
970                 // Get the highest available rate
971                 for (i = 0; i < 13; i++) {
972                         if (abySupportedRates[i] == 0)
973                                 break;
974                 }
975                 if (i != 0)
976                         brate = i - 1;
977
978         }
979         // Check that it is valid
980         // brate is index of abySupportedRates[]
981         if (brate > 13) {
982                 rc = -EINVAL;
983                 return rc;
984         }
985
986         // Now, check if we want a fixed or auto value
987         if (wrq->fixed != 0) {
988                 // Fixed mode
989                 // One rate, fixed
990                 pr_debug("Rate Fix\n");
991                 pDevice->bFixRate = true;
992                 if ((pDevice->byBBType == BB_TYPE_11B) && (brate > 3)) {
993                         pDevice->uConnectionRate = 3;
994                 } else {
995                         pDevice->uConnectionRate = brate;
996                         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Fixed to Rate %d\n", pDevice->uConnectionRate);
997                 }
998
999         } else {
1000                 pDevice->bFixRate = false;
1001                 pDevice->uConnectionRate = 13;
1002                 pr_debug("auto rate:connection_rate is 13\n");
1003         }
1004
1005         return rc;
1006 }
1007
1008 /*
1009  * Wireless Handler : get data rate
1010  */
1011
1012 int iwctl_giwrate(struct net_device *dev,
1013                   struct iw_request_info *info,
1014                   struct iw_param *wrq,
1015                   char *extra)
1016 {
1017         PSDevice                pDevice = (PSDevice)netdev_priv(dev);
1018 //2007-0118-05,<Mark> by EinsnLiu
1019 //Mark the unnecessary sentences.
1020 //    PSMgmtObject        pMgmt = &(pDevice->sMgmtObj);
1021
1022         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCGIWRATE\n");
1023         {
1024                 unsigned char abySupportedRates[13] = {0x02, 0x04, 0x0B, 0x16, 0x0c, 0x12, 0x18, 0x24, 0x30, 0x48, 0x60, 0x6C, 0x90};
1025                 int brate = 0;
1026 //2008-5-8 <modify> by chester
1027                 if (pDevice->bLinkPass) {
1028                         if (pDevice->bFixRate == true) {
1029                                 if (pDevice->uConnectionRate < 13) {
1030                                         brate = abySupportedRates[pDevice->uConnectionRate];
1031                                 } else {
1032                                         if (pDevice->byBBType == BB_TYPE_11B)
1033                                                 brate = 0x16;
1034                                         if (pDevice->byBBType == BB_TYPE_11G)
1035                                                 brate = 0x6C;
1036                                         if (pDevice->byBBType == BB_TYPE_11A)
1037                                                 brate = 0x6C;
1038                                 }
1039                         } else {
1040                                 brate = abySupportedRates[TxRate_iwconfig];
1041                         }
1042                 } else brate = 0;
1043
1044                 wrq->value = brate * 500000;
1045                 // If more than one rate, set auto
1046                 if (pDevice->bFixRate == true)
1047                         wrq->fixed = true;
1048         }
1049
1050         return 0;
1051 }
1052
1053 /*
1054  * Wireless Handler : set rts threshold
1055  */
1056
1057 int iwctl_siwrts(struct net_device *dev,
1058                  struct iw_request_info *info,
1059                  struct iw_param *wrq,
1060                  char *extra)
1061 {
1062         PSDevice                pDevice = (PSDevice)netdev_priv(dev);
1063         int rc = 0;
1064
1065         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCSIWRTS\n");
1066
1067         {
1068                 int rthr = wrq->value;
1069
1070                 if (wrq->disabled)
1071                         rthr = 2312;
1072
1073                 if ((rthr < 0) || (rthr > 2312))
1074                         rc = -EINVAL;
1075                 else
1076                         pDevice->wRTSThreshold = rthr;
1077         }
1078
1079         return 0;
1080 }
1081
1082 /*
1083  * Wireless Handler : get rts
1084  */
1085
1086 int iwctl_giwrts(struct net_device *dev,
1087                  struct iw_request_info *info,
1088                  struct iw_param *wrq,
1089                  char *extra)
1090 {
1091         PSDevice                pDevice = (PSDevice)netdev_priv(dev);
1092
1093         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCGIWRTS\n");
1094         wrq->value = pDevice->wRTSThreshold;
1095         wrq->disabled = (wrq->value >= 2312);
1096         wrq->fixed = 1;
1097
1098         return 0;
1099 }
1100
1101 /*
1102  * Wireless Handler : set fragment threshold
1103  */
1104
1105 int iwctl_siwfrag(struct net_device *dev,
1106                   struct iw_request_info *info,
1107                   struct iw_param *wrq,
1108                   char *extra)
1109 {
1110         PSDevice                pDevice = (PSDevice)netdev_priv(dev);
1111         int rc = 0;
1112         int fthr = wrq->value;
1113
1114         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCSIWFRAG\n");
1115
1116         if (wrq->disabled)
1117                 fthr = 2312;
1118         if ((fthr < 256) || (fthr > 2312)) {
1119                 rc = -EINVAL;
1120         } else {
1121                 fthr &= ~0x1;   // Get an even value
1122                 pDevice->wFragmentationThreshold = (u16)fthr;
1123         }
1124
1125         return rc;
1126 }
1127
1128 /*
1129  * Wireless Handler : get fragment threshold
1130  */
1131
1132 int iwctl_giwfrag(struct net_device *dev,
1133                   struct iw_request_info *info,
1134                   struct iw_param *wrq,
1135                   char *extra)
1136 {
1137         PSDevice                pDevice = (PSDevice)netdev_priv(dev);
1138
1139         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCGIWFRAG\n");
1140         wrq->value = pDevice->wFragmentationThreshold;
1141         wrq->disabled = (wrq->value >= 2312);
1142         wrq->fixed = 1;
1143
1144         return 0;
1145 }
1146
1147 /*
1148  * Wireless Handler : set retry threshold
1149  */
1150 int iwctl_siwretry(struct net_device *dev,
1151                    struct iw_request_info *info,
1152                    struct iw_param *wrq,
1153                    char *extra)
1154 {
1155         PSDevice                pDevice = (PSDevice)netdev_priv(dev);
1156         int rc = 0;
1157
1158         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCSIWRETRY\n");
1159
1160         if (wrq->disabled) {
1161                 rc = -EINVAL;
1162                 return rc;
1163         }
1164
1165         if (wrq->flags & IW_RETRY_LIMIT) {
1166                 if (wrq->flags & IW_RETRY_MAX)
1167                         pDevice->byLongRetryLimit = wrq->value;
1168                 else if (wrq->flags & IW_RETRY_MIN)
1169                         pDevice->byShortRetryLimit = wrq->value;
1170                 else {
1171                         // No modifier : set both
1172                         pDevice->byShortRetryLimit = wrq->value;
1173                         pDevice->byLongRetryLimit = wrq->value;
1174                 }
1175         }
1176         if (wrq->flags & IW_RETRY_LIFETIME)
1177                 pDevice->wMaxTransmitMSDULifetime = wrq->value;
1178
1179         return rc;
1180 }
1181
1182 /*
1183  * Wireless Handler : get retry threshold
1184  */
1185 int iwctl_giwretry(struct net_device *dev,
1186                    struct iw_request_info *info,
1187                    struct iw_param *wrq,
1188                    char *extra)
1189 {
1190         PSDevice                pDevice = (PSDevice)netdev_priv(dev);
1191
1192         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCGIWRETRY\n");
1193         wrq->disabled = 0;      // Can't be disabled
1194
1195         // Note : by default, display the min retry number
1196         if ((wrq->flags & IW_RETRY_TYPE) == IW_RETRY_LIFETIME) {
1197                 wrq->flags = IW_RETRY_LIFETIME;
1198                 wrq->value = (int)pDevice->wMaxTransmitMSDULifetime; //ms
1199         } else if ((wrq->flags & IW_RETRY_MAX)) {
1200                 wrq->flags = IW_RETRY_LIMIT | IW_RETRY_MAX;
1201                 wrq->value = (int)pDevice->byLongRetryLimit;
1202         } else {
1203                 wrq->flags = IW_RETRY_LIMIT;
1204                 wrq->value = (int)pDevice->byShortRetryLimit;
1205                 if ((int)pDevice->byShortRetryLimit != (int)pDevice->byLongRetryLimit)
1206                         wrq->flags |= IW_RETRY_MIN;
1207         }
1208
1209         return 0;
1210 }
1211
1212 /*
1213  * Wireless Handler : set encode mode
1214  */
1215 int iwctl_siwencode(struct net_device *dev,
1216                     struct iw_request_info *info,
1217                     struct iw_point *wrq,
1218                     char *extra)
1219 {
1220         PSDevice                pDevice = (PSDevice)netdev_priv(dev);
1221         PSMgmtObject        pMgmt = &(pDevice->sMgmtObj);
1222         unsigned long dwKeyIndex = (unsigned long)(wrq->flags & IW_ENCODE_INDEX);
1223         int ii, uu, rc = 0;
1224         int index = (wrq->flags & IW_ENCODE_INDEX);
1225
1226 //2007-0207-07,<Modify> by EinsnLiu
1227 //There are some problems when using iwconfig encode/key command to set the WEP key.
1228 //I almost rewrite this function.
1229 //now it support:(assume the wireless interface's name is eth0)
1230 //iwconfig eth0 key [1] 1122334455 open  /*set key stirng to index 1,and driver using key index is set to 1*/
1231 //iwconfig eth0 key [3]    /*set driver using  key index to 3,the key string no change */
1232 //iwconfig eth0 key 1122334455  /*set key string to driver using index*/
1233 //iwconfig eth0 key restricted  /*enable share key*/
1234
1235         PSKeyTable pkeytab;
1236
1237         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCSIWENCODE\n");
1238
1239         if ((wrq->flags & IW_ENCODE_DISABLED) == 0) {
1240                 //Not disable encryption
1241
1242                 if (dwKeyIndex > WLAN_WEP_NKEYS) {
1243                         rc = -EINVAL;
1244                         return rc;
1245                 }
1246
1247                 if (dwKeyIndex < 1 && ((wrq->flags & IW_ENCODE_NOKEY) == 0)) {//set default key
1248                         if (pDevice->byKeyIndex < WLAN_WEP_NKEYS)
1249                                 dwKeyIndex = pDevice->byKeyIndex;
1250                         else
1251                                 dwKeyIndex = 0;
1252                 } else {
1253                         dwKeyIndex--;
1254                 }
1255
1256                 // Check the size of the key
1257                 if (wrq->length > WLAN_WEP232_KEYLEN) {
1258                         rc = -EINVAL;
1259                         return rc;
1260                 }
1261
1262                 if (wrq->length > 0) {//have key
1263
1264                         if (wrq->length ==  WLAN_WEP232_KEYLEN) {
1265                                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Set 232 bit wep key\n");
1266                         } else if (wrq->length ==  WLAN_WEP104_KEYLEN) {
1267                                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Set 104 bit wep key\n");
1268                         } else if (wrq->length == WLAN_WEP40_KEYLEN) {
1269                                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Set 40 bit wep key, index= %d\n", (int)dwKeyIndex);
1270                         } else {//no support length
1271                                 rc = -EINVAL;
1272                                 return rc;
1273                         }
1274                         memset(pDevice->abyKey, 0, WLAN_WEP232_KEYLEN);
1275                         memcpy(pDevice->abyKey, extra, wrq->length);
1276
1277                         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "abyKey: ");
1278                         for (ii = 0; ii < wrq->length; ii++)
1279                                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "%02x ", pDevice->abyKey[ii]);
1280
1281                         if (pDevice->flags & DEVICE_FLAGS_OPENED) {
1282                                 spin_lock_irq(&pDevice->lock);
1283                                 KeybSetDefaultKey(&(pDevice->sKey),
1284                                                   (unsigned long)(dwKeyIndex | (1 << 31)),
1285                                                   wrq->length,
1286                                                   NULL,
1287                                                   pDevice->abyKey,
1288                                                   KEY_CTL_WEP,
1289                                                   pDevice->PortOffset,
1290                                                   pDevice->byLocalID
1291 );
1292                                 spin_unlock_irq(&pDevice->lock);
1293                         }
1294                         pDevice->byKeyIndex = (unsigned char)dwKeyIndex;
1295                         pDevice->uKeyLength = wrq->length;
1296                         pDevice->bTransmitKey = true;
1297                         pDevice->bEncryptionEnable = true;
1298                         pDevice->eEncryptionStatus = Ndis802_11Encryption1Enabled;
1299
1300                 } else if (index > 0) {
1301                         //when the length is 0 the request only changes the default transmit key index
1302                         //check the new key if it has a non zero length
1303                         if (pDevice->bEncryptionEnable == false) {
1304                                 rc = -EINVAL;
1305                                 return rc;
1306                         }
1307                         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Just set Default key Index:\n");
1308                         pkeytab = &(pDevice->sKey.KeyTable[MAX_KEY_TABLE - 1]);
1309                         if (pkeytab->GroupKey[(unsigned char)dwKeyIndex].uKeyLength == 0) {
1310                                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Default key len is 0\n");
1311                                 rc = -EINVAL;
1312                                 return rc;
1313                         }
1314                         pDevice->byKeyIndex = (unsigned char)dwKeyIndex;
1315                         pkeytab->dwGTKeyIndex = dwKeyIndex | (1 << 31);
1316                         pkeytab->GroupKey[(unsigned char)dwKeyIndex].dwKeyIndex = dwKeyIndex | (1 << 31);
1317                 }
1318
1319         } else {//disable the key
1320                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Disable WEP function\n");
1321                 if (pDevice->bEncryptionEnable == false)
1322                         return 0;
1323                 pMgmt->bShareKeyAlgorithm = false;
1324                 pDevice->bEncryptionEnable = false;
1325                 pDevice->eEncryptionStatus = Ndis802_11EncryptionDisabled;
1326                 if (pDevice->flags & DEVICE_FLAGS_OPENED) {
1327                         spin_lock_irq(&pDevice->lock);
1328                         for (uu = 0; uu < MAX_KEY_TABLE; uu++)
1329                                 MACvDisableKeyEntry(pDevice->PortOffset, uu);
1330                         spin_unlock_irq(&pDevice->lock);
1331                 }
1332         }
1333 //End Modify,Einsn
1334
1335         if (wrq->flags & IW_ENCODE_RESTRICTED) {
1336                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Enable WEP & ShareKey System\n");
1337                 pMgmt->bShareKeyAlgorithm = true;
1338         }
1339         if (wrq->flags & IW_ENCODE_OPEN) {
1340                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Enable WEP & Open System\n");
1341                 pMgmt->bShareKeyAlgorithm = false;
1342         }
1343         return rc;
1344 }
1345
1346 int iwctl_giwencode(struct net_device *dev,
1347                     struct iw_request_info *info,
1348                     struct iw_point *wrq,
1349                     char *extra)
1350 {
1351         PSDevice                        pDevice = (PSDevice)netdev_priv(dev);
1352         PSMgmtObject            pMgmt = &(pDevice->sMgmtObj);
1353         char abyKey[WLAN_WEP232_KEYLEN];
1354
1355         unsigned int index = (unsigned int)(wrq->flags & IW_ENCODE_INDEX);
1356         PSKeyItem       pKey = NULL;
1357
1358         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCGIWENCODE\n");
1359
1360         if (index > WLAN_WEP_NKEYS)
1361                 return  -EINVAL;
1362
1363         if (index < 1) {//get default key
1364                 if (pDevice->byKeyIndex < WLAN_WEP_NKEYS)
1365                         index = pDevice->byKeyIndex;
1366                 else
1367                         index = 0;
1368         } else {
1369                 index--;
1370         }
1371
1372         memset(abyKey, 0, WLAN_WEP232_KEYLEN);
1373         // Check encryption mode
1374         wrq->flags = IW_ENCODE_NOKEY;
1375         // Is WEP enabled ???
1376         if (pDevice->bEncryptionEnable)
1377                 wrq->flags |=  IW_ENCODE_ENABLED;
1378         else
1379                 wrq->flags |=  IW_ENCODE_DISABLED;
1380
1381         if (pMgmt->bShareKeyAlgorithm)
1382                 wrq->flags |=  IW_ENCODE_RESTRICTED;
1383         else
1384                 wrq->flags |=  IW_ENCODE_OPEN;
1385         wrq->length = 0;
1386
1387         if ((index == 0) && (pDevice->eEncryptionStatus == Ndis802_11Encryption2Enabled ||
1388                              pDevice->eEncryptionStatus == Ndis802_11Encryption3Enabled)) {//get wpa pairwise  key
1389                 if (KeybGetKey(&(pDevice->sKey), pMgmt->abyCurrBSSID, 0xffffffff, &pKey)) {
1390                         wrq->length = pKey->uKeyLength;
1391                         memcpy(abyKey, pKey->abyKey,    pKey->uKeyLength);
1392                         memcpy(extra,  abyKey, WLAN_WEP232_KEYLEN);
1393                 }
1394         } else if (KeybGetKey(&(pDevice->sKey), pDevice->abyBroadcastAddr, (unsigned char)index , &pKey)) {
1395                 wrq->length = pKey->uKeyLength;
1396                 memcpy(abyKey, pKey->abyKey,  pKey->uKeyLength);
1397                 memcpy(extra,  abyKey, WLAN_WEP232_KEYLEN);
1398         }
1399
1400         wrq->flags |= index+1;
1401
1402         return 0;
1403 }
1404
1405 /*
1406  * Wireless Handler : set power mode
1407  */
1408 int iwctl_siwpower(struct net_device *dev,
1409                    struct iw_request_info *info,
1410                    struct iw_param *wrq,
1411                    char *extra)
1412 {
1413         PSDevice            pDevice = (PSDevice)netdev_priv(dev);
1414         PSMgmtObject        pMgmt = &(pDevice->sMgmtObj);
1415         int rc = 0;
1416
1417         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCSIWPOWER\n");
1418
1419         if (!(pDevice->flags & DEVICE_FLAGS_OPENED)) {
1420                 rc = -EINVAL;
1421                 return rc;
1422         }
1423
1424         if (wrq->disabled) {
1425                 pDevice->ePSMode = WMAC_POWER_CAM;
1426                 PSvDisablePowerSaving(pDevice);
1427                 return rc;
1428         }
1429         if ((wrq->flags & IW_POWER_TYPE) == IW_POWER_TIMEOUT) {
1430                 pDevice->ePSMode = WMAC_POWER_FAST;
1431                 PSvEnablePowerSaving((void *)pDevice, pMgmt->wListenInterval);
1432
1433         } else if ((wrq->flags & IW_POWER_TYPE) == IW_POWER_PERIOD) {
1434                 pDevice->ePSMode = WMAC_POWER_FAST;
1435                 PSvEnablePowerSaving((void *)pDevice, pMgmt->wListenInterval);
1436         }
1437         switch (wrq->flags & IW_POWER_MODE) {
1438         case IW_POWER_UNICAST_R:
1439                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCSIWPOWER: IW_POWER_UNICAST_R\n");
1440                 rc = -EINVAL;
1441                 break;
1442         case IW_POWER_ALL_R:
1443                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCSIWPOWER: IW_POWER_ALL_R\n");
1444                 rc = -EINVAL;
1445         case IW_POWER_ON:
1446                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCSIWPOWER: IW_POWER_ON\n");
1447                 break;
1448         default:
1449                 rc = -EINVAL;
1450         }
1451
1452         return rc;
1453 }
1454
1455 /*
1456  * Wireless Handler : get power mode
1457  */
1458 int iwctl_giwpower(struct net_device *dev,
1459                    struct iw_request_info *info,
1460                    struct iw_param *wrq,
1461                    char *extra)
1462 {
1463         PSDevice            pDevice = (PSDevice)netdev_priv(dev);
1464         PSMgmtObject        pMgmt = &(pDevice->sMgmtObj);
1465         int mode = pDevice->ePSMode;
1466
1467         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCGIWPOWER\n");
1468
1469         wrq->disabled = (mode == WMAC_POWER_CAM);
1470         if (wrq->disabled)
1471                 return 0;
1472
1473         if ((wrq->flags & IW_POWER_TYPE) == IW_POWER_TIMEOUT) {
1474                 wrq->value = (int)((pMgmt->wListenInterval * pMgmt->wCurrBeaconPeriod) << 10);
1475                 wrq->flags = IW_POWER_TIMEOUT;
1476         } else {
1477                 wrq->value = (int)((pMgmt->wListenInterval * pMgmt->wCurrBeaconPeriod) << 10);
1478                 wrq->flags = IW_POWER_PERIOD;
1479         }
1480         wrq->flags |= IW_POWER_ALL_R;
1481
1482         return 0;
1483 }
1484
1485 /*
1486  * Wireless Handler : get Sensitivity
1487  */
1488 int iwctl_giwsens(struct net_device *dev,
1489                   struct iw_request_info *info,
1490                   struct iw_param *wrq,
1491                   char *extra)
1492 {
1493         PSDevice                pDevice = (PSDevice)netdev_priv(dev);
1494         long ldBm;
1495
1496         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCGIWSENS\n");
1497         if (pDevice->bLinkPass == true) {
1498                 RFvRSSITodBm(pDevice, (unsigned char)(pDevice->uCurrRSSI), &ldBm);
1499                 wrq->value = ldBm;
1500         } else {
1501                 wrq->value = 0;
1502         }
1503         wrq->disabled = (wrq->value == 0);
1504         wrq->fixed = 1;
1505
1506         return 0;
1507 }
1508
1509 //2008-0409-07, <Add> by Einsn Liu
1510 #ifdef WPA_SUPPLICANT_DRIVER_WEXT_SUPPORT
1511
1512 int iwctl_siwauth(struct net_device *dev,
1513                   struct iw_request_info *info,
1514                   struct iw_param *wrq,
1515                   char *extra)
1516 {
1517         PSDevice                        pDevice = (PSDevice)netdev_priv(dev);
1518         PSMgmtObject    pMgmt = &(pDevice->sMgmtObj);
1519         int ret = 0;
1520         static int wpa_version = 0;  //must be static to save the last value,einsn liu
1521         static int pairwise = 0;
1522
1523         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCSIWAUTH\n");
1524         switch (wrq->flags & IW_AUTH_INDEX) {
1525         case IW_AUTH_WPA_VERSION:
1526                 wpa_version = wrq->value;
1527                 if (wrq->value == IW_AUTH_WPA_VERSION_DISABLED)
1528                         PRINT_K("iwctl_siwauth:set WPADEV to disable at 1??????\n");
1529                 else if (wrq->value == IW_AUTH_WPA_VERSION_WPA)
1530                         PRINT_K("iwctl_siwauth:set WPADEV to WPA1******\n");
1531                 else
1532                         PRINT_K("iwctl_siwauth:set WPADEV to WPA2******\n");
1533
1534                 break;
1535         case IW_AUTH_CIPHER_PAIRWISE:
1536                 pairwise = wrq->value;
1537                 if (pairwise == IW_AUTH_CIPHER_CCMP)
1538                         pDevice->eEncryptionStatus = Ndis802_11Encryption3Enabled;
1539                 else if (pairwise == IW_AUTH_CIPHER_TKIP)
1540                         pDevice->eEncryptionStatus = Ndis802_11Encryption2Enabled;
1541                 else if (pairwise == IW_AUTH_CIPHER_WEP40 || pairwise == IW_AUTH_CIPHER_WEP104)
1542                         pDevice->eEncryptionStatus = Ndis802_11Encryption1Enabled;
1543                 else if (pairwise == IW_AUTH_CIPHER_NONE)
1544                         ; /* do nothing,einsn liu */
1545                 else
1546                         pDevice->eEncryptionStatus = Ndis802_11EncryptionDisabled;
1547
1548                 break;
1549         case IW_AUTH_CIPHER_GROUP:
1550                 if (wpa_version == IW_AUTH_WPA_VERSION_DISABLED)
1551                         break;
1552                 if (pairwise == IW_AUTH_CIPHER_NONE) {
1553                         if (wrq->value == IW_AUTH_CIPHER_CCMP)
1554                                 pDevice->eEncryptionStatus = Ndis802_11Encryption3Enabled;
1555                         else
1556                                 pDevice->eEncryptionStatus = Ndis802_11Encryption2Enabled;
1557                 }
1558                 break;
1559         case IW_AUTH_KEY_MGMT:
1560
1561                 if (wpa_version == IW_AUTH_WPA_VERSION_WPA2) {
1562                         if (wrq->value == IW_AUTH_KEY_MGMT_PSK)
1563                                 pMgmt->eAuthenMode = WMAC_AUTH_WPA2PSK;
1564                         else
1565                                 pMgmt->eAuthenMode = WMAC_AUTH_WPA2;
1566                 } else if (wpa_version == IW_AUTH_WPA_VERSION_WPA) {
1567                         if (wrq->value == 0)
1568                                 pMgmt->eAuthenMode = WMAC_AUTH_WPANONE;
1569                         else if (wrq->value == IW_AUTH_KEY_MGMT_PSK)
1570                                 pMgmt->eAuthenMode = WMAC_AUTH_WPAPSK;
1571                         else
1572                                 pMgmt->eAuthenMode = WMAC_AUTH_WPA;
1573                 }
1574
1575                 break;
1576         case IW_AUTH_TKIP_COUNTERMEASURES:
1577                 break;          /* FIXME */
1578         case IW_AUTH_DROP_UNENCRYPTED:
1579                 break;
1580         case IW_AUTH_80211_AUTH_ALG:
1581                 if (wrq->value == IW_AUTH_ALG_OPEN_SYSTEM)
1582                         pMgmt->bShareKeyAlgorithm = false;
1583                 else if (wrq->value == IW_AUTH_ALG_SHARED_KEY)
1584                         pMgmt->bShareKeyAlgorithm = true;
1585
1586                 break;
1587         case IW_AUTH_WPA_ENABLED:
1588                 break;
1589         case IW_AUTH_RX_UNENCRYPTED_EAPOL:
1590                 break;
1591         case IW_AUTH_ROAMING_CONTROL:
1592                 ret = -EOPNOTSUPP;
1593                 break;
1594         case IW_AUTH_PRIVACY_INVOKED:
1595                 pDevice->bEncryptionEnable = !!wrq->value;
1596                 if (pDevice->bEncryptionEnable == false) {
1597                         wpa_version = 0;
1598                         pairwise = 0;
1599                         pDevice->eEncryptionStatus = Ndis802_11EncryptionDisabled;
1600                         pMgmt->bShareKeyAlgorithm = false;
1601                         pMgmt->eAuthenMode = false;
1602                 }
1603
1604                 break;
1605         default:
1606                 ret = -EOPNOTSUPP;
1607                 break;
1608         }
1609
1610         return ret;
1611 }
1612
1613 int iwctl_giwauth(struct net_device *dev,
1614                   struct iw_request_info *info,
1615                   struct iw_param *wrq,
1616                   char *extra)
1617 {
1618         return -EOPNOTSUPP;
1619 }
1620
1621 int iwctl_siwgenie(struct net_device *dev,
1622                    struct iw_request_info *info,
1623                    struct iw_point *wrq,
1624                    char __user *extra)
1625 {
1626         PSDevice                        pDevice = (PSDevice)netdev_priv(dev);
1627         PSMgmtObject    pMgmt = &(pDevice->sMgmtObj);
1628         int ret = 0;
1629         char length;
1630
1631         if (wrq->length) {
1632                 if (wrq->length < 2)
1633                         return -EINVAL;
1634
1635                 ret = get_user(length, extra + 1);
1636                 if (ret)
1637                         return ret;
1638
1639                 if (length + 2 != wrq->length)
1640                         return -EINVAL;
1641
1642                 if (wrq->length > MAX_WPA_IE_LEN) {
1643                         ret = -ENOMEM;
1644                         goto out;
1645                 }
1646                 memset(pMgmt->abyWPAIE, 0, MAX_WPA_IE_LEN);
1647                 if (copy_from_user(pMgmt->abyWPAIE, extra, wrq->length)) {
1648                         ret = -EFAULT;
1649                         goto out;
1650                 }
1651                 pMgmt->wWPAIELen = wrq->length;
1652         } else {
1653                 memset(pMgmt->abyWPAIE, 0, MAX_WPA_IE_LEN);
1654                 pMgmt->wWPAIELen = 0;
1655         }
1656
1657 out://not completely ...not necessary in wpa_supplicant 0.5.8
1658         return ret;
1659 }
1660
1661 int iwctl_giwgenie(struct net_device *dev,
1662                    struct iw_request_info *info,
1663                    struct iw_point *wrq,
1664                    char __user *extra)
1665 {
1666         PSDevice                        pDevice = (PSDevice)netdev_priv(dev);
1667         PSMgmtObject    pMgmt = &(pDevice->sMgmtObj);
1668         int ret = 0;
1669         int space = wrq->length;
1670
1671         wrq->length = 0;
1672         if (pMgmt->wWPAIELen > 0) {
1673                 wrq->length = pMgmt->wWPAIELen;
1674                 if (pMgmt->wWPAIELen <= space) {
1675                         if (copy_to_user(extra, pMgmt->abyWPAIE, pMgmt->wWPAIELen))
1676                                 ret = -EFAULT;
1677
1678                 } else {
1679                         ret = -E2BIG;
1680                 }
1681         }
1682
1683         return ret;
1684 }
1685
1686 int iwctl_siwencodeext(struct net_device *dev,
1687                        struct iw_request_info *info,
1688                        struct iw_point *wrq,
1689                        char *extra)
1690 {
1691         PSDevice                pDevice = (PSDevice)netdev_priv(dev);
1692         struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
1693         struct viawget_wpa_param *param = NULL;
1694 //original member
1695         enum wpa_alg alg_name;
1696         u8  addr[6];
1697         int key_idx, set_tx = 0;
1698         u8  seq[IW_ENCODE_SEQ_MAX_SIZE];
1699         u8 key[64];
1700         size_t seq_len = 0, key_len = 0;
1701
1702         u8 key_array[64];
1703         int ret = 0;
1704
1705         PRINT_K("SIOCSIWENCODEEXT......\n");
1706
1707         param = kzalloc(sizeof(*param), GFP_KERNEL);
1708         if (param == NULL)
1709                 return -ENOMEM;
1710
1711 //recover alg_name
1712         switch (ext->alg) {
1713         case IW_ENCODE_ALG_NONE:
1714                 alg_name = WPA_ALG_NONE;
1715                 break;
1716         case IW_ENCODE_ALG_WEP:
1717                 alg_name = WPA_ALG_WEP;
1718                 break;
1719         case IW_ENCODE_ALG_TKIP:
1720                 alg_name = WPA_ALG_TKIP;
1721                 break;
1722         case IW_ENCODE_ALG_CCMP:
1723                 alg_name = WPA_ALG_CCMP;
1724                 break;
1725         default:
1726                 PRINT_K("Unknown alg = %d\n", ext->alg);
1727                 ret = -ENOMEM;
1728                 goto error;
1729         }
1730 //recover addr
1731         memcpy(addr, ext->addr.sa_data, ETH_ALEN);
1732 //recover key_idx
1733         key_idx = (wrq->flags&IW_ENCODE_INDEX) - 1;
1734 //recover set_tx
1735         if (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY)
1736                 set_tx = 1;
1737 //recover seq,seq_len
1738         if (ext->ext_flags & IW_ENCODE_EXT_RX_SEQ_VALID) {
1739                 seq_len = IW_ENCODE_SEQ_MAX_SIZE;
1740                 memcpy(seq, ext->rx_seq, seq_len);
1741         }
1742 //recover key,key_len
1743         if (ext->key_len) {
1744                 key_len = ext->key_len;
1745                 memcpy(key, &ext->key[0], key_len);
1746         }
1747
1748         memset(key_array, 0, 64);
1749         if (key_len > 0) {
1750                 memcpy(key_array, key, key_len);
1751                 if (key_len == 32) {
1752                         // notice ! the oder
1753                         memcpy(&key_array[16], &key[24], 8);
1754                         memcpy(&key_array[24], &key[16], 8);
1755                 }
1756         }
1757
1758 /**************Translate iw_encode_ext to viawget_wpa_param****************/
1759         memcpy(param->addr, addr, ETH_ALEN);
1760         param->u.wpa_key.alg_name = (int)alg_name;
1761         param->u.wpa_key.set_tx = set_tx;
1762         param->u.wpa_key.key_index = key_idx;
1763         param->u.wpa_key.key_len = key_len;
1764         param->u.wpa_key.key = (u8 *)key_array;
1765         param->u.wpa_key.seq = (u8 *)seq;
1766         param->u.wpa_key.seq_len = seq_len;
1767
1768 //****set if current action is Network Manager count??
1769 //****this method is so foolish,but there is no other way???
1770         if (param->u.wpa_key.alg_name == WPA_ALG_NONE) {
1771                 if (param->u.wpa_key.key_index == 0)
1772                         pDevice->bwextcount++;
1773
1774                 if ((pDevice->bwextcount == 1) && (param->u.wpa_key.key_index == 1))
1775                         pDevice->bwextcount++;
1776
1777                 if ((pDevice->bwextcount == 2) && (param->u.wpa_key.key_index == 2))
1778                         pDevice->bwextcount++;
1779
1780                 if ((pDevice->bwextcount == 3) && (param->u.wpa_key.key_index == 3))
1781                         pDevice->bwextcount++;
1782
1783         }
1784         if (pDevice->bwextcount == 4) {
1785                 pr_debug("SIOCSIWENCODEEXT:Enable WPA WEXT SUPPORT!!!!!\n");
1786                 pDevice->bwextcount = 0;
1787                 pDevice->bWPASuppWextEnabled = true;
1788         }
1789 //******
1790
1791         spin_lock_irq(&pDevice->lock);
1792         ret = wpa_set_keys(pDevice, param, true);
1793         spin_unlock_irq(&pDevice->lock);
1794
1795 error:
1796         kfree(param);
1797         return ret;
1798 }
1799
1800 int iwctl_giwencodeext(struct net_device *dev,
1801                        struct iw_request_info *info,
1802                        struct iw_point *wrq,
1803                        char *extra)
1804 {
1805         return -EOPNOTSUPP;
1806 }
1807
1808 int iwctl_siwmlme(struct net_device *dev,
1809                   struct iw_request_info *info,
1810                   struct iw_point *wrq,
1811                   char __user *extra)
1812 {
1813         PSDevice                        pDevice = (PSDevice)netdev_priv(dev);
1814         PSMgmtObject    pMgmt = &(pDevice->sMgmtObj);
1815         struct iw_mlme mime;
1816
1817         int ret = 0;
1818
1819         ret = copy_from_user(&mime, extra, sizeof(mime));
1820         if (ret)
1821                 return -EFAULT;
1822
1823         if (memcmp(pMgmt->abyCurrBSSID, mime.addr.sa_data, ETH_ALEN)) {
1824                 ret = -EINVAL;
1825                 return ret;
1826         }
1827         switch (mime.cmd) {
1828         case IW_MLME_DEAUTH:
1829                 //this command seems to be not complete,please test it --einsnliu
1830                 //bScheduleCommand((void *) pDevice, WLAN_CMD_DEAUTH, (unsigned char *)&reason);
1831                 break;
1832         case IW_MLME_DISASSOC:
1833                 if (pDevice->bLinkPass == true) {
1834                         pr_debug("iwctl_siwmlme--->send DISASSOCIATE\n");
1835                         //clear related flags
1836                         memset(pMgmt->abyDesireBSSID, 0xFF, 6);
1837                         KeyvInitTable(&pDevice->sKey, pDevice->PortOffset);
1838                         bScheduleCommand((void *)pDevice, WLAN_CMD_DISASSOCIATE, NULL);
1839                 }
1840                 break;
1841         default:
1842                 ret = -EOPNOTSUPP;
1843         }
1844
1845         return ret;
1846 }
1847
1848 #endif
1849
1850 /*------------------------------------------------------------------*/
1851 /*
1852  * Structures to export the Wireless Handlers
1853  */
1854
1855 static const iw_handler         iwctl_handler[] =
1856 {
1857         (iw_handler) iwctl_commit,      // SIOCSIWCOMMIT
1858         (iw_handler) NULL,              // SIOCGIWNAME
1859         (iw_handler) NULL,              // SIOCSIWNWID
1860         (iw_handler) NULL,              // SIOCGIWNWID
1861         (iw_handler) NULL,              // SIOCSIWFREQ
1862         (iw_handler) NULL,              // SIOCGIWFREQ
1863         (iw_handler) NULL,              // SIOCSIWMODE
1864         (iw_handler) NULL,              // SIOCGIWMODE
1865         (iw_handler) NULL,              // SIOCSIWSENS
1866         (iw_handler) NULL,              // SIOCGIWSENS
1867         (iw_handler) NULL,              // SIOCSIWRANGE
1868         (iw_handler) iwctl_giwrange,    // SIOCGIWRANGE
1869         (iw_handler) NULL,              // SIOCSIWPRIV
1870         (iw_handler) NULL,              // SIOCGIWPRIV
1871         (iw_handler) NULL,              // SIOCSIWSTATS
1872         (iw_handler) NULL,              // SIOCGIWSTATS
1873         (iw_handler) NULL,              // SIOCSIWSPY
1874         (iw_handler) NULL,              // SIOCGIWSPY
1875         (iw_handler) NULL,              // -- hole --
1876         (iw_handler) NULL,              // -- hole --
1877         (iw_handler) NULL,              // SIOCSIWAP
1878         (iw_handler) NULL,              // SIOCGIWAP
1879         (iw_handler) NULL,              // -- hole -- 0x16
1880         (iw_handler) NULL,              // SIOCGIWAPLIST
1881         (iw_handler) iwctl_siwscan,     // SIOCSIWSCAN
1882         (iw_handler) iwctl_giwscan,     // SIOCGIWSCAN
1883         (iw_handler) NULL,              // SIOCSIWESSID
1884         (iw_handler) NULL,              // SIOCGIWESSID
1885         (iw_handler) NULL,              // SIOCSIWNICKN
1886         (iw_handler) NULL,              // SIOCGIWNICKN
1887         (iw_handler) NULL,              // -- hole --
1888         (iw_handler) NULL,              // -- hole --
1889         (iw_handler) NULL,              // SIOCSIWRATE 0x20
1890         (iw_handler) NULL,              // SIOCGIWRATE
1891         (iw_handler) NULL,              // SIOCSIWRTS
1892         (iw_handler) NULL,              // SIOCGIWRTS
1893         (iw_handler) NULL,              // SIOCSIWFRAG
1894         (iw_handler) NULL,              // SIOCGIWFRAG
1895         (iw_handler) NULL,              // SIOCSIWTXPOW
1896         (iw_handler) NULL,              // SIOCGIWTXPOW
1897         (iw_handler) NULL,              // SIOCSIWRETRY
1898         (iw_handler) NULL,              // SIOCGIWRETRY
1899         (iw_handler) NULL,              // SIOCSIWENCODE
1900         (iw_handler) NULL,              // SIOCGIWENCODE
1901         (iw_handler) NULL,              // SIOCSIWPOWER
1902         (iw_handler) NULL,              // SIOCGIWPOWER
1903
1904 //2008-0409-07, <Add> by Einsn Liu
1905         (iw_handler) NULL,              // -- hole --
1906         (iw_handler) NULL,              // -- hole --
1907         (iw_handler) NULL,              // SIOCSIWGENIE
1908         (iw_handler) NULL,              // SIOCGIWGENIE
1909         (iw_handler) NULL,              // SIOCSIWAUTH
1910         (iw_handler) NULL,              // SIOCGIWAUTH
1911         (iw_handler) NULL,              // SIOCSIWENCODEEXT
1912         (iw_handler) NULL,              // SIOCGIWENCODEEXT
1913         (iw_handler) NULL,              // SIOCSIWPMKSA
1914         (iw_handler) NULL,              // -- hole --
1915 };
1916
1917 static const iw_handler         iwctl_private_handler[] =
1918 {
1919         NULL,                           // SIOCIWFIRSTPRIV
1920 };
1921
1922 struct iw_priv_args iwctl_private_args[] = {
1923         { IOCTL_CMD_SET,
1924           IW_PRIV_TYPE_CHAR | 1024, 0,
1925           "set"},
1926 };
1927
1928 const struct iw_handler_def     iwctl_handler_def =
1929 {
1930         .get_wireless_stats = &iwctl_get_wireless_stats,
1931         .num_standard   = sizeof(iwctl_handler)/sizeof(iw_handler),
1932         .num_private    = 0,
1933         .num_private_args = 0,
1934         .standard       = (iw_handler *)iwctl_handler,
1935         .private        = NULL,
1936         .private_args   = NULL,
1937 };