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