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