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