linux/drivers/staging/vt6656/main_usb.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: main_usb.c
  20 *
  21 * Purpose: driver entry for initial, open, close, tx and rx.
  22 *
  23 * Author: Lyndon Chen
  24 *
  25 * Date: Dec 8, 2005
  26 *
  27 * Functions:
  28 *
  29 *   vntwusb_found1 - module initial (insmod) driver entry
  30 *   device_remove1 - module remove entry
  31 *   device_open - allocate dma/descripter resource & initial mac/bbp function
  32 *   device_xmit - asynchrous data tx function
  33 *   device_set_multi - set mac filter
  34 *   device_ioctl - ioctl entry
  35 *   device_close - shutdown mac/bbp & free dma/descripter resource
  36 *   device_alloc_frag_buf - rx fragement pre-allocated function
  37 *   device_free_tx_bufs - free tx buffer function
  38 *   device_dma0_tx_80211- tx 802.11 frame via dma0
  39 *   device_dma0_xmit- tx PS bufferred frame via dma0
  40 *   device_init_registers- initial MAC & BBP & RF internal registers.
  41 *   device_init_rings- initial tx/rx ring buffer
  42 *   device_init_defrag_cb- initial & allocate de-fragement buffer.
  43 *   device_tx_srv- tx interrupt service function
  44 *
  45 * Revision History:
  46 */
  47#undef __NO_VERSION__
  48
  49#include "device.h"
  50#include "card.h"
  51#include "baseband.h"
  52#include "mac.h"
  53#include "tether.h"
  54#include "wmgr.h"
  55#include "wctl.h"
  56#include "power.h"
  57#include "wcmd.h"
  58#include "iocmd.h"
  59#include "tcrc.h"
  60#include "rxtx.h"
  61#include "bssdb.h"
  62#include "hostap.h"
  63#include "wpactl.h"
  64#include "ioctl.h"
  65#include "iwctl.h"
  66#include "dpc.h"
  67#include "iocmd.h"
  68#include "datarate.h"
  69#include "rf.h"
  70#include "firmware.h"
  71#include "mac.h"
  72#include "rndis.h"
  73#include "control.h"
  74#include "channel.h"
  75#include "int.h"
  76#include "iowpa.h"
  77
  78/*---------------------  Static Definitions -------------------------*/
  79//static int          msglevel                =MSG_LEVEL_DEBUG;
  80static int          msglevel                =MSG_LEVEL_INFO;
  81
  82//
  83// Define module options
  84//
  85
  86// Version Information
  87#define DRIVER_AUTHOR "VIA Networking Technologies, Inc., <lyndonchen@vntek.com.tw>"
  88MODULE_AUTHOR(DRIVER_AUTHOR);
  89MODULE_LICENSE("GPL");
  90MODULE_DESCRIPTION(DEVICE_FULL_DRV_NAM);
  91
  92#define DEVICE_PARAM(N,D) \
  93        static int N[MAX_UINTS]=OPTION_DEFAULT;\
  94        module_param_array(N, int, NULL, 0);\
  95        MODULE_PARM_DESC(N, D);
  96
  97#define RX_DESC_MIN0     16
  98#define RX_DESC_MAX0     128
  99#define RX_DESC_DEF0     64
 100DEVICE_PARAM(RxDescriptors0,"Number of receive usb desc buffer");
 101
 102
 103#define TX_DESC_MIN0     16
 104#define TX_DESC_MAX0     128
 105#define TX_DESC_DEF0     64
 106DEVICE_PARAM(TxDescriptors0,"Number of transmit usb desc buffer");
 107
 108
 109#define CHANNEL_MIN     1
 110#define CHANNEL_MAX     14
 111#define CHANNEL_DEF     6
 112
 113DEVICE_PARAM(Channel, "Channel number");
 114
 115
 116/* PreambleType[] is the preamble length used for transmit.
 117   0: indicate allows long preamble type
 118   1: indicate allows short preamble type
 119*/
 120
 121#define PREAMBLE_TYPE_DEF     1
 122
 123DEVICE_PARAM(PreambleType, "Preamble Type");
 124
 125
 126#define RTS_THRESH_MIN     512
 127#define RTS_THRESH_MAX     2347
 128#define RTS_THRESH_DEF     2347
 129
 130DEVICE_PARAM(RTSThreshold, "RTS threshold");
 131
 132
 133#define FRAG_THRESH_MIN     256
 134#define FRAG_THRESH_MAX     2346
 135#define FRAG_THRESH_DEF     2346
 136
 137DEVICE_PARAM(FragThreshold, "Fragmentation threshold");
 138
 139
 140#define DATA_RATE_MIN     0
 141#define DATA_RATE_MAX     13
 142#define DATA_RATE_DEF     13
 143/* datarate[] index
 144   0: indicate 1 Mbps   0x02
 145   1: indicate 2 Mbps   0x04
 146   2: indicate 5.5 Mbps 0x0B
 147   3: indicate 11 Mbps  0x16
 148   4: indicate 6 Mbps   0x0c
 149   5: indicate 9 Mbps   0x12
 150   6: indicate 12 Mbps  0x18
 151   7: indicate 18 Mbps  0x24
 152   8: indicate 24 Mbps  0x30
 153   9: indicate 36 Mbps  0x48
 154  10: indicate 48 Mbps  0x60
 155  11: indicate 54 Mbps  0x6c
 156  12: indicate 72 Mbps  0x90
 157  13: indicate auto rate
 158*/
 159
 160DEVICE_PARAM(ConnectionRate, "Connection data rate");
 161
 162#define OP_MODE_MAX     2
 163#define OP_MODE_DEF     0
 164#define OP_MODE_MIN     0
 165
 166DEVICE_PARAM(OPMode, "Infrastruct, adhoc, AP mode ");
 167
 168/* OpMode[] is used for transmit.
 169   0: indicate infrastruct mode used
 170   1: indicate adhoc mode used
 171   2: indicate AP mode used
 172*/
 173
 174
 175/* PSMode[]
 176   0: indicate disable power saving mode
 177   1: indicate enable power saving mode
 178*/
 179
 180#define PS_MODE_DEF     0
 181
 182DEVICE_PARAM(PSMode, "Power saving mode");
 183
 184
 185#define SHORT_RETRY_MIN     0
 186#define SHORT_RETRY_MAX     31
 187#define SHORT_RETRY_DEF     8
 188
 189
 190DEVICE_PARAM(ShortRetryLimit, "Short frame retry limits");
 191
 192#define LONG_RETRY_MIN     0
 193#define LONG_RETRY_MAX     15
 194#define LONG_RETRY_DEF     4
 195
 196
 197DEVICE_PARAM(LongRetryLimit, "long frame retry limits");
 198
 199
 200/* BasebandType[] baseband type selected
 201   0: indicate 802.11a type
 202   1: indicate 802.11b type
 203   2: indicate 802.11g type
 204*/
 205#define BBP_TYPE_MIN     0
 206#define BBP_TYPE_MAX     2
 207#define BBP_TYPE_DEF     2
 208
 209DEVICE_PARAM(BasebandType, "baseband type");
 210
 211
 212
 213/* 80211hEnable[]
 214   0: indicate disable 802.11h
 215   1: indicate enable 802.11h
 216*/
 217
 218#define X80211h_MODE_DEF     0
 219
 220DEVICE_PARAM(b80211hEnable, "802.11h mode");
 221
 222
 223//
 224// Static vars definitions
 225//
 226
 227
 228
 229static struct usb_device_id vntwusb_table[] = {
 230        {USB_DEVICE(VNT_USB_VENDOR_ID, VNT_USB_PRODUCT_ID)},
 231        {}
 232};
 233
 234
 235
 236// Frequency list (map channels to frequencies)
 237/*
 238static const long frequency_list[] = {
 239    2412, 2417, 2422, 2427, 2432, 2437, 2442, 2447, 2452, 2457, 2462, 2467, 2472, 2484,
 240    4915, 4920, 4925, 4935, 4940, 4945, 4960, 4980,
 241    5035, 5040, 5045, 5055, 5060, 5080, 5170, 5180, 5190, 5200, 5210, 5220, 5230, 5240,
 242    5260, 5280, 5300, 5320, 5500, 5520, 5540, 5560, 5580, 5600, 5620, 5640, 5660, 5680,
 243    5700, 5745, 5765, 5785, 5805, 5825
 244        };
 245
 246
 247#ifndef IW_ENCODE_NOKEY
 248#define IW_ENCODE_NOKEY         0x0800
 249#define IW_ENCODE_MODE  (IW_ENCODE_DISABLED | IW_ENCODE_RESTRICTED | IW_ENCODE_OPEN)
 250#endif
 251
 252static const struct iw_handler_def      iwctl_handler_def;
 253*/
 254
 255
 256
 257/*---------------------  Static Functions  --------------------------*/
 258static int vntwusb_found1(struct usb_interface *intf, const struct usb_device_id *id);
 259static void vntwusb_disconnect(struct usb_interface *intf);
 260#ifdef CONFIG_PM        /* Minimal support for suspend and resume */
 261static int vntwusb_suspend(struct usb_interface *intf, pm_message_t message);
 262static int vntwusb_resume(struct usb_interface *intf);
 263#endif
 264static struct net_device_stats *device_get_stats(struct net_device *dev);
 265static int  device_open(struct net_device *dev);
 266static int  device_xmit(struct sk_buff *skb, struct net_device *dev);
 267static void device_set_multi(struct net_device *dev);
 268static int  device_close(struct net_device *dev);
 269static int  device_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
 270
 271static BOOL device_init_registers(PSDevice pDevice, DEVICE_INIT_TYPE InitType);
 272static BOOL device_init_defrag_cb(PSDevice pDevice);
 273static void device_init_diversity_timer(PSDevice pDevice);
 274static int  device_dma0_tx_80211(struct sk_buff *skb, struct net_device *dev);
 275
 276static int  ethtool_ioctl(struct net_device *dev, void *useraddr);
 277static void device_free_tx_bufs(PSDevice pDevice);
 278static void device_free_rx_bufs(PSDevice pDevice);
 279static void device_free_int_bufs(PSDevice pDevice);
 280static void device_free_frag_bufs(PSDevice pDevice);
 281static BOOL device_alloc_bufs(PSDevice pDevice);
 282
 283static int Read_config_file(PSDevice pDevice);
 284static UCHAR *Config_FileOperation(PSDevice pDevice);
 285static int Config_FileGetParameter(UCHAR *string, UCHAR *dest,UCHAR *source);
 286
 287//2008-0714<Add>by Mike Liu
 288static BOOL device_release_WPADEV(PSDevice pDevice);
 289
 290static void usb_device_reset(PSDevice pDevice);
 291
 292
 293
 294/*---------------------  Export Variables  --------------------------*/
 295
 296/*---------------------  Export Functions  --------------------------*/
 297
 298
 299static void
 300device_set_options(PSDevice pDevice) {
 301
 302    BYTE    abyBroadcastAddr[U_ETHER_ADDR_LEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
 303    BYTE    abySNAP_RFC1042[U_ETHER_ADDR_LEN] = {0xAA, 0xAA, 0x03, 0x00, 0x00, 0x00};
 304    BYTE    abySNAP_Bridgetunnel[U_ETHER_ADDR_LEN] = {0xAA, 0xAA, 0x03, 0x00, 0x00, 0xF8};
 305
 306
 307    memcpy(pDevice->abyBroadcastAddr, abyBroadcastAddr, U_ETHER_ADDR_LEN);
 308    memcpy(pDevice->abySNAP_RFC1042, abySNAP_RFC1042, U_ETHER_ADDR_LEN);
 309    memcpy(pDevice->abySNAP_Bridgetunnel, abySNAP_Bridgetunnel, U_ETHER_ADDR_LEN);
 310
 311    pDevice->cbTD = TX_DESC_DEF0;
 312    pDevice->cbRD = RX_DESC_DEF0;
 313    pDevice->uChannel = CHANNEL_DEF;
 314    pDevice->wRTSThreshold = RTS_THRESH_DEF;
 315    pDevice->wFragmentationThreshold = FRAG_THRESH_DEF;
 316    pDevice->byShortRetryLimit = SHORT_RETRY_DEF;
 317    pDevice->byLongRetryLimit = LONG_RETRY_DEF;
 318    pDevice->wMaxTransmitMSDULifetime = DEFAULT_MSDU_LIFETIME;
 319    pDevice->byShortPreamble = PREAMBLE_TYPE_DEF;
 320    pDevice->ePSMode = PS_MODE_DEF;
 321    pDevice->b11hEnable = X80211h_MODE_DEF;
 322    pDevice->eOPMode = OP_MODE_DEF;
 323    pDevice->uConnectionRate = DATA_RATE_DEF;
 324    if (pDevice->uConnectionRate < RATE_AUTO) pDevice->bFixRate = TRUE;
 325    pDevice->byBBType = BBP_TYPE_DEF;
 326    pDevice->byPacketType = pDevice->byBBType;
 327    pDevice->byAutoFBCtrl = AUTO_FB_0;
 328    pDevice->bUpdateBBVGA = TRUE;
 329    pDevice->byFOETuning = 0;
 330    pDevice->byAutoPwrTunning = 0;
 331    pDevice->wCTSDuration = 0;
 332    pDevice->byPreambleType = 0;
 333    pDevice->bExistSWNetAddr = FALSE;
 334//    pDevice->bDiversityRegCtlON = TRUE;
 335    pDevice->bDiversityRegCtlON = FALSE;
 336}
 337
 338
 339static VOID device_init_diversity_timer(PSDevice pDevice) {
 340
 341    init_timer(&pDevice->TimerSQ3Tmax1);
 342    pDevice->TimerSQ3Tmax1.data = (ULONG)pDevice;
 343    pDevice->TimerSQ3Tmax1.function = (TimerFunction)TimerSQ3CallBack;
 344    pDevice->TimerSQ3Tmax1.expires = RUN_AT(HZ);
 345
 346    init_timer(&pDevice->TimerSQ3Tmax2);
 347    pDevice->TimerSQ3Tmax2.data = (ULONG)pDevice;
 348    pDevice->TimerSQ3Tmax2.function = (TimerFunction)TimerSQ3CallBack;
 349    pDevice->TimerSQ3Tmax2.expires = RUN_AT(HZ);
 350
 351    init_timer(&pDevice->TimerSQ3Tmax3);
 352    pDevice->TimerSQ3Tmax3.data = (ULONG)pDevice;
 353    pDevice->TimerSQ3Tmax3.function = (TimerFunction)TimerSQ3Tmax3CallBack;
 354    pDevice->TimerSQ3Tmax3.expires = RUN_AT(HZ);
 355
 356    return;
 357}
 358
 359
 360//
 361// Initialiation of MAC & BBP registers
 362//
 363
 364static BOOL device_init_registers(PSDevice pDevice, DEVICE_INIT_TYPE InitType)
 365{
 366    BYTE            abyBroadcastAddr[U_ETHER_ADDR_LEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
 367    BYTE            abySNAP_RFC1042[U_ETHER_ADDR_LEN] = {0xAA, 0xAA, 0x03, 0x00, 0x00, 0x00};
 368    BYTE            abySNAP_Bridgetunnel[U_ETHER_ADDR_LEN] = {0xAA, 0xAA, 0x03, 0x00, 0x00, 0xF8};
 369    BYTE            byAntenna;
 370    UINT            ii;
 371    CMD_CARD_INIT   sInitCmd;
 372    NTSTATUS        ntStatus = STATUS_SUCCESS;
 373    RSP_CARD_INIT   sInitRsp;
 374    PSMgmtObject    pMgmt = &(pDevice->sMgmtObj);
 375    BYTE            byTmp;
 376    BYTE            byCalibTXIQ = 0;
 377    BYTE            byCalibTXDC = 0;
 378    BYTE            byCalibRXIQ = 0;
 379
 380    DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "---->INIbInitAdapter. [%d][%d]\n", InitType, pDevice->byPacketType);
 381        spin_lock_irq(&pDevice->lock);
 382    if (InitType == DEVICE_INIT_COLD) {
 383        memcpy(pDevice->abyBroadcastAddr, abyBroadcastAddr, U_ETHER_ADDR_LEN);
 384        memcpy(pDevice->abySNAP_RFC1042, abySNAP_RFC1042, U_ETHER_ADDR_LEN);
 385        memcpy(pDevice->abySNAP_Bridgetunnel, abySNAP_Bridgetunnel, U_ETHER_ADDR_LEN);
 386
 387        if ( !FIRMWAREbCheckVersion(pDevice) ) {
 388            if (FIRMWAREbDownload(pDevice) == TRUE) {
 389                if (FIRMWAREbBrach2Sram(pDevice) == FALSE) {
 390                    DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" FIRMWAREbBrach2Sram fail \n");
 391                        spin_unlock_irq(&pDevice->lock);
 392                    return FALSE;
 393                }
 394            } else {
 395
 396                DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" FIRMWAREbDownload fail \n");
 397                spin_unlock_irq(&pDevice->lock);
 398                return FALSE;
 399            }
 400        }
 401
 402        if ( !BBbVT3184Init(pDevice) ) {
 403            DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" BBbVT3184Init fail \n");
 404            spin_unlock_irq(&pDevice->lock);
 405            return FALSE;
 406        }
 407    }
 408
 409    sInitCmd.byInitClass = (BYTE)InitType;
 410    sInitCmd.bExistSWNetAddr = (BYTE) pDevice->bExistSWNetAddr;
 411    for(ii=0;ii<6;ii++)
 412        sInitCmd.bySWNetAddr[ii] = pDevice->abyCurrentNetAddr[ii];
 413    sInitCmd.byShortRetryLimit = pDevice->byShortRetryLimit;
 414    sInitCmd.byLongRetryLimit = pDevice->byLongRetryLimit;
 415
 416    //issue Card_init command to device
 417    ntStatus = CONTROLnsRequestOut(pDevice,
 418                                    MESSAGE_TYPE_CARDINIT,
 419                                    0,
 420                                    0,
 421                                    sizeof(CMD_CARD_INIT),
 422                                    (PBYTE) &(sInitCmd));
 423
 424    if ( ntStatus != STATUS_SUCCESS ) {
 425        DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" Issue Card init fail \n");
 426        spin_unlock_irq(&pDevice->lock);
 427        return FALSE;
 428    }
 429    if (InitType == DEVICE_INIT_COLD) {
 430
 431        ntStatus = CONTROLnsRequestIn(pDevice,MESSAGE_TYPE_INIT_RSP,0,0,sizeof(RSP_CARD_INIT), (PBYTE) &(sInitRsp));
 432
 433        if (ntStatus != STATUS_SUCCESS) {
 434            DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Cardinit request in status fail!\n");
 435            spin_unlock_irq(&pDevice->lock);
 436            return FALSE;
 437        }
 438
 439        //Local ID for AES functions
 440        ntStatus = CONTROLnsRequestIn(pDevice,
 441                                    MESSAGE_TYPE_READ,
 442                                    MAC_REG_LOCALID,
 443                                    MESSAGE_REQUEST_MACREG,
 444                                    1,
 445                                    &pDevice->byLocalID);
 446
 447        if ( ntStatus != STATUS_SUCCESS ) {
 448            spin_unlock_irq(&pDevice->lock);
 449            return FALSE;
 450        }
 451
 452        // Do MACbSoftwareReset in MACvInitialize
 453        // force CCK
 454        pDevice->bCCK = TRUE;
 455        pDevice->bProtectMode = FALSE;          //Only used in 11g type, sync with ERP IE
 456        pDevice->bNonERPPresent = FALSE;
 457        pDevice->bBarkerPreambleMd = FALSE;
 458        if ( pDevice->bFixRate ) {
 459            pDevice->wCurrentRate = (WORD) pDevice->uConnectionRate;
 460        } else {
 461            if ( pDevice->byBBType == BB_TYPE_11B )
 462                pDevice->wCurrentRate = RATE_11M;
 463            else
 464                pDevice->wCurrentRate = RATE_54M;
 465        }
 466
 467        CHvInitChannelTable(pDevice);
 468
 469        pDevice->byTopOFDMBasicRate = RATE_24M;
 470        pDevice->byTopCCKBasicRate = RATE_1M;
 471        pDevice->byRevId = 0;                   //Target to IF pin while programming to RF chip.
 472        pDevice->byCurPwr = 0xFF;
 473
 474        pDevice->byCCKPwr = pDevice->abyEEPROM[EEP_OFS_PWR_CCK];
 475        pDevice->byOFDMPwrG = pDevice->abyEEPROM[EEP_OFS_PWR_OFDMG];
 476        // Load power Table
 477        for (ii=0;ii<14;ii++) {
 478            pDevice->abyCCKPwrTbl[ii] = pDevice->abyEEPROM[ii + EEP_OFS_CCK_PWR_TBL];
 479            if (pDevice->abyCCKPwrTbl[ii] == 0)
 480                pDevice->abyCCKPwrTbl[ii] = pDevice->byCCKPwr;
 481            pDevice->abyOFDMPwrTbl[ii] = pDevice->abyEEPROM[ii + EEP_OFS_OFDM_PWR_TBL];
 482            if (pDevice->abyOFDMPwrTbl[ii] == 0)
 483                pDevice->abyOFDMPwrTbl[ii] = pDevice->byOFDMPwrG;
 484        }
 485
 486          //original zonetype is USA,but customize zonetype is europe,
 487          // then need recover 12,13 ,14 channel  with 11 channel
 488          if(((pDevice->abyEEPROM[EEP_OFS_ZONETYPE] == ZoneType_Japan) ||
 489                (pDevice->abyEEPROM[EEP_OFS_ZONETYPE] == ZoneType_Europe))&&
 490             (pDevice->byOriginalZonetype == ZoneType_USA)) {
 491            for(ii=11;ii<14;ii++) {
 492                pDevice->abyCCKPwrTbl[ii] = pDevice->abyCCKPwrTbl[10];
 493               pDevice->abyOFDMPwrTbl[ii] = pDevice->abyOFDMPwrTbl[10];
 494            }
 495          }
 496
 497        //{{ RobertYu: 20041124
 498        pDevice->byOFDMPwrA = 0x34; // same as RFbMA2829SelectChannel
 499        // Load OFDM A Power Table
 500        for (ii=0;ii<CB_MAX_CHANNEL_5G;ii++) { //RobertYu:20041224, bug using CB_MAX_CHANNEL
 501            pDevice->abyOFDMAPwrTbl[ii] = pDevice->abyEEPROM[ii + EEP_OFS_OFDMA_PWR_TBL];
 502            if (pDevice->abyOFDMAPwrTbl[ii] == 0)
 503                pDevice->abyOFDMAPwrTbl[ii] = pDevice->byOFDMPwrA;
 504        }
 505        //}} RobertYu
 506
 507        byAntenna = pDevice->abyEEPROM[EEP_OFS_ANTENNA];
 508        if (byAntenna & EEP_ANTINV)
 509            pDevice->bTxRxAntInv = TRUE;
 510        else
 511            pDevice->bTxRxAntInv = FALSE;
 512
 513        byAntenna &= (EEP_ANTENNA_AUX | EEP_ANTENNA_MAIN);
 514
 515        if (byAntenna == 0) // if not set default is All
 516            byAntenna = (EEP_ANTENNA_AUX | EEP_ANTENNA_MAIN);
 517
 518        if (byAntenna == (EEP_ANTENNA_AUX | EEP_ANTENNA_MAIN)) {
 519            pDevice->byAntennaCount = 2;
 520            pDevice->byTxAntennaMode = ANT_B;
 521            pDevice->dwTxAntennaSel = 1;
 522            pDevice->dwRxAntennaSel = 1;
 523            if (pDevice->bTxRxAntInv == TRUE)
 524                pDevice->byRxAntennaMode = ANT_A;
 525            else
 526                pDevice->byRxAntennaMode = ANT_B;
 527
 528            if (pDevice->bDiversityRegCtlON)
 529                pDevice->bDiversityEnable = TRUE;
 530            else
 531                pDevice->bDiversityEnable = FALSE;
 532        } else  {
 533            pDevice->bDiversityEnable = FALSE;
 534            pDevice->byAntennaCount = 1;
 535            pDevice->dwTxAntennaSel = 0;
 536            pDevice->dwRxAntennaSel = 0;
 537            if (byAntenna & EEP_ANTENNA_AUX) {
 538                pDevice->byTxAntennaMode = ANT_A;
 539                if (pDevice->bTxRxAntInv == TRUE)
 540                    pDevice->byRxAntennaMode = ANT_B;
 541                else
 542                    pDevice->byRxAntennaMode = ANT_A;
 543            } else {
 544                pDevice->byTxAntennaMode = ANT_B;
 545                if (pDevice->bTxRxAntInv == TRUE)
 546                    pDevice->byRxAntennaMode = ANT_A;
 547                else
 548                    pDevice->byRxAntennaMode = ANT_B;
 549            }
 550        }
 551        pDevice->ulDiversityNValue = 100*255;
 552        pDevice->ulDiversityMValue = 100*16;
 553        pDevice->byTMax = 1;
 554        pDevice->byTMax2 = 4;
 555        pDevice->ulSQ3TH = 0;
 556        pDevice->byTMax3 = 64;
 557        // -----------------------------------------------------------------
 558
 559        //Get Auto Fall Back Type
 560        pDevice->byAutoFBCtrl = AUTO_FB_0;
 561
 562        // Set SCAN Time
 563        pDevice->uScanTime = WLAN_SCAN_MINITIME;
 564
 565        // default Auto Mode
 566        //pDevice->NetworkType = Ndis802_11Automode;
 567        pDevice->eConfigPHYMode = PHY_TYPE_AUTO;
 568        pDevice->byBBType = BB_TYPE_11G;
 569
 570        // initialize BBP registers
 571        pDevice->ulTxPower = 25;
 572
 573        // Get Channel range
 574        pDevice->byMinChannel = 1;
 575        pDevice->byMaxChannel = CB_MAX_CHANNEL;
 576
 577        // Get RFType
 578        pDevice->byRFType = sInitRsp.byRFType;
 579
 580        if ((pDevice->byRFType & RF_EMU) != 0) {
 581            // force change RevID for VT3253 emu
 582            pDevice->byRevId = 0x80;
 583        }
 584
 585        // Load EEPROM calibrated vt3266 parameters
 586        if (pDevice->byRFType == RF_VT3226D0) {
 587            if((pDevice->abyEEPROM[EEP_OFS_MAJOR_VER] == 0x1) &&
 588                (pDevice->abyEEPROM[EEP_OFS_MINOR_VER] >= 0x4)) {
 589                byCalibTXIQ = pDevice->abyEEPROM[EEP_OFS_CALIB_TX_IQ];
 590                byCalibTXDC = pDevice->abyEEPROM[EEP_OFS_CALIB_TX_DC];
 591                byCalibRXIQ = pDevice->abyEEPROM[EEP_OFS_CALIB_RX_IQ];
 592                if( (byCalibTXIQ || byCalibTXDC || byCalibRXIQ) ) {
 593                    ControlvWriteByte(pDevice, MESSAGE_REQUEST_BBREG, 0xFF, 0x03); // CR255, Set BB to support TX/RX IQ and DC compensation Mode
 594                    ControlvWriteByte(pDevice, MESSAGE_REQUEST_BBREG, 0xFB, byCalibTXIQ); // CR251, TX I/Q Imbalance Calibration
 595                    ControlvWriteByte(pDevice, MESSAGE_REQUEST_BBREG, 0xFC, byCalibTXDC); // CR252, TX DC-Offset Calibration
 596                    ControlvWriteByte(pDevice, MESSAGE_REQUEST_BBREG, 0xFD, byCalibRXIQ); // CR253, RX I/Q Imbalance Calibration
 597                } else {
 598                // turn off BB Calibration compensation
 599                    ControlvWriteByte(pDevice, MESSAGE_REQUEST_BBREG, 0xFF, 0x0); // CR255
 600                }
 601            }
 602        }
 603        pMgmt->eScanType = WMAC_SCAN_PASSIVE;
 604        pMgmt->uCurrChannel = pDevice->uChannel;
 605        pMgmt->uIBSSChannel = pDevice->uChannel;
 606        CARDbSetMediaChannel(pDevice, pMgmt->uCurrChannel);
 607
 608        // get Permanent network address
 609        memcpy(pDevice->abyPermanentNetAddr,&(sInitRsp.byNetAddr[0]),6);
 610        memcpy(pDevice->abyCurrentNetAddr, pDevice->abyPermanentNetAddr, U_ETHER_ADDR_LEN);
 611
 612        // if exist SW network address, use SW network address.
 613
 614        DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Network address = %02x-%02x-%02x=%02x-%02x-%02x\n",
 615            pDevice->abyCurrentNetAddr[0],
 616            pDevice->abyCurrentNetAddr[1],
 617            pDevice->abyCurrentNetAddr[2],
 618            pDevice->abyCurrentNetAddr[3],
 619            pDevice->abyCurrentNetAddr[4],
 620            pDevice->abyCurrentNetAddr[5]);
 621    }
 622
 623
 624
 625    // Set BB and packet type at the same time.
 626    // Set Short Slot Time, xIFS, and RSPINF.
 627    if (pDevice->byBBType == BB_TYPE_11A) {
 628        CARDbAddBasicRate(pDevice, RATE_6M);
 629        pDevice->bShortSlotTime = TRUE;
 630    } else {
 631        CARDbAddBasicRate(pDevice, RATE_1M);
 632        pDevice->bShortSlotTime = FALSE;
 633    }
 634    BBvSetShortSlotTime(pDevice);
 635    CARDvSetBSSMode(pDevice);
 636
 637    if (pDevice->bUpdateBBVGA) {
 638        pDevice->byBBVGACurrent = pDevice->abyBBVGA[0];
 639        pDevice->byBBVGANew = pDevice->byBBVGACurrent;
 640        BBvSetVGAGainOffset(pDevice, pDevice->abyBBVGA[0]);
 641    }
 642
 643    pDevice->byRadioCtl = pDevice->abyEEPROM[EEP_OFS_RADIOCTL];
 644    pDevice->bHWRadioOff = FALSE;
 645    if ( (pDevice->byRadioCtl & EEP_RADIOCTL_ENABLE) != 0 ) {
 646        ntStatus = CONTROLnsRequestIn(pDevice,
 647                                    MESSAGE_TYPE_READ,
 648                                    MAC_REG_GPIOCTL1,
 649                                    MESSAGE_REQUEST_MACREG,
 650                                    1,
 651                                    &byTmp);
 652
 653        if ( ntStatus != STATUS_SUCCESS ) {
 654            spin_unlock_irq(&pDevice->lock);
 655            return FALSE;
 656        }
 657        if ( (byTmp & GPIO3_DATA) == 0 ) {
 658            pDevice->bHWRadioOff = TRUE;
 659            MACvRegBitsOn(pDevice,MAC_REG_GPIOCTL1,GPIO3_INTMD);
 660        } else {
 661            MACvRegBitsOff(pDevice,MAC_REG_GPIOCTL1,GPIO3_INTMD);
 662            pDevice->bHWRadioOff = FALSE;
 663        }
 664
 665    } //EEP_RADIOCTL_ENABLE
 666
 667    ControlvMaskByte(pDevice,MESSAGE_REQUEST_MACREG,MAC_REG_PAPEDELAY,LEDSTS_TMLEN,0x38);
 668    ControlvMaskByte(pDevice,MESSAGE_REQUEST_MACREG,MAC_REG_PAPEDELAY,LEDSTS_STS,LEDSTS_SLOW);
 669    MACvRegBitsOn(pDevice,MAC_REG_GPIOCTL0,0x01);
 670
 671    if ((pDevice->bHWRadioOff == TRUE) || (pDevice->bRadioControlOff == TRUE)) {
 672        CARDbRadioPowerOff(pDevice);
 673    } else {
 674        CARDbRadioPowerOn(pDevice);
 675    }
 676
 677    spin_unlock_irq(&pDevice->lock);
 678    DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"<----INIbInitAdapter Exit\n");
 679    return TRUE;
 680}
 681
 682static BOOL device_release_WPADEV(PSDevice pDevice)
 683{
 684  viawget_wpa_header *wpahdr;
 685  int ii=0;
 686 // wait_queue_head_t   Set_wait;
 687  //send device close to wpa_supplicnat layer
 688    if (pDevice->bWPADEVUp==TRUE) {
 689                 wpahdr = (viawget_wpa_header *)pDevice->skb->data;
 690                 wpahdr->type = VIAWGET_DEVICECLOSE_MSG;
 691                 wpahdr->resp_ie_len = 0;
 692                 wpahdr->req_ie_len = 0;
 693                 skb_put(pDevice->skb, sizeof(viawget_wpa_header));
 694                 pDevice->skb->dev = pDevice->wpadev;
 695                 skb_reset_mac_header(pDevice->skb);
 696                 pDevice->skb->pkt_type = PACKET_HOST;
 697                 pDevice->skb->protocol = htons(ETH_P_802_2);
 698                 memset(pDevice->skb->cb, 0, sizeof(pDevice->skb->cb));
 699                 netif_rx(pDevice->skb);
 700                 pDevice->skb = dev_alloc_skb((int)pDevice->rx_buf_sz);
 701
 702 //wait release WPADEV
 703              //    init_waitqueue_head(&Set_wait);
 704              //    wait_event_timeout(Set_wait, ((pDevice->wpadev==NULL)&&(pDevice->skb == NULL)),5*HZ);    //1s wait
 705              while(pDevice->bWPADEVUp==TRUE) {
 706                set_current_state(TASK_UNINTERRUPTIBLE);
 707                 schedule_timeout (HZ/20);          //wait 50ms
 708                 ii++;
 709                if(ii>20)
 710                  break;
 711              }
 712           };
 713    return TRUE;
 714}
 715
 716#ifdef CONFIG_PM        /* Minimal support for suspend and resume */
 717static int vntwusb_suspend(struct usb_interface *intf, pm_message_t message)
 718{
 719 PSDevice  pDevice = usb_get_intfdata(intf);
 720 struct net_device *dev = pDevice->dev;
 721
 722 printk("VNTWUSB Suspend Start======>\n");
 723if(dev != NULL) {
 724  if(pDevice->flags & DEVICE_FLAGS_OPENED)
 725     device_close(dev);
 726}
 727
 728 usb_put_dev(interface_to_usbdev(intf));
 729 return 0;
 730}
 731
 732static int vntwusb_resume(struct usb_interface *intf)
 733{
 734 PSDevice  pDevice = usb_get_intfdata(intf);
 735 struct net_device *dev = pDevice->dev;
 736
 737 printk("VNTWUSB Resume Start======>\n");
 738 if(dev != NULL) {
 739  usb_get_dev(interface_to_usbdev(intf));
 740  if(!(pDevice->flags & DEVICE_FLAGS_OPENED)) {
 741    if(device_open(dev)!=0)
 742        printk("VNTWUSB Resume Start======>open fail\n");
 743   }
 744 }
 745 return 0;
 746}
 747#endif
 748
 749
 750static const struct net_device_ops device_netdev_ops = {
 751    .ndo_open               = device_open,
 752    .ndo_stop               = device_close,
 753    .ndo_do_ioctl           = device_ioctl,
 754    .ndo_get_stats          = device_get_stats,
 755    .ndo_start_xmit         = device_xmit,
 756    .ndo_set_multicast_list = device_set_multi,
 757};
 758
 759
 760static int
 761vntwusb_found1(struct usb_interface *intf, const struct usb_device_id *id)
 762{
 763   BYTE            fake_mac[U_ETHER_ADDR_LEN] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x01};//fake MAC address
 764        struct usb_device *udev = interface_to_usbdev(intf);
 765    int         rc = 0;
 766    struct net_device *netdev = NULL;
 767    PSDevice    pDevice = NULL;
 768
 769
 770    printk(KERN_NOTICE "%s Ver. %s\n",DEVICE_FULL_DRV_NAM, DEVICE_VERSION);
 771    printk(KERN_NOTICE "Copyright (c) 2004 VIA Networking Technologies, Inc.\n");
 772
 773  udev = usb_get_dev(udev);
 774
 775    netdev = alloc_etherdev(sizeof(DEVICE_INFO));
 776
 777    if (netdev == NULL) {
 778        printk(KERN_ERR DEVICE_NAME ": allocate net device failed \n");
 779        kfree(pDevice);
 780            goto err_nomem;
 781    }
 782
 783    pDevice = netdev_priv(netdev);
 784    memset(pDevice, 0, sizeof(DEVICE_INFO));
 785
 786    pDevice->dev = netdev;
 787    pDevice->usb = udev;
 788
 789    // Set initial settings
 790    device_set_options(pDevice);
 791    spin_lock_init(&pDevice->lock);
 792
 793    pDevice->tx_80211 = device_dma0_tx_80211;
 794    pDevice->sMgmtObj.pAdapter = (PVOID)pDevice;
 795
 796    netdev->netdev_ops         = &device_netdev_ops;
 797
 798        netdev->wireless_handlers = (struct iw_handler_def *)&iwctl_handler_def;
 799
 800   //2008-0623-01<Remark>by MikeLiu
 801  //2007-0821-01<Add>by MikeLiu
 802         usb_set_intfdata(intf, pDevice);
 803        SET_NETDEV_DEV(netdev, &intf->dev);
 804    memcpy(pDevice->dev->dev_addr, fake_mac, U_ETHER_ADDR_LEN); //use fake mac address
 805    rc = register_netdev(netdev);
 806    if (rc != 0) {
 807        printk(KERN_ERR DEVICE_NAME " Failed to register netdev\n");
 808                free_netdev(netdev);
 809        kfree(pDevice);
 810        return -ENODEV;
 811    }
 812        //2008-0623-02<Remark>by MikeLiu
 813        //2007-0821-01<Add>by MikeLiu
 814       //#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,21)
 815        //usb_set_intfdata(intf, pDevice);
 816        //SET_NETDEV_DEV(netdev, &intf->dev);
 817       //#endif
 818
 819//2008-07-21-01<Add>by MikeLiu
 820//register wpadev
 821#if 0
 822   if(wpa_set_wpadev(pDevice, 1)!=0) {
 823     printk("Fail to Register WPADEV?\n");
 824        unregister_netdev(pDevice->dev);
 825        free_netdev(netdev);
 826        kfree(pDevice);
 827   }
 828#endif
 829         usb_device_reset(pDevice);
 830
 831#ifdef SndEvt_ToAPI
 832{
 833  union iwreq_data      wrqu;
 834  memset(&wrqu, 0, sizeof(wrqu));
 835  wrqu.data.flags = RT_INSMOD_EVENT_FLAG;
 836  wrqu.data.length =IFNAMSIZ;
 837  wireless_send_event(pDevice->dev, IWEVCUSTOM, &wrqu, pDevice->dev->name);
 838}
 839#endif
 840
 841        return 0;
 842
 843
 844err_nomem:
 845 //2008-0922-01<Add>by MikeLiu, decrease usb counter.
 846    usb_put_dev(udev);
 847
 848    return -ENOMEM;
 849}
 850
 851
 852static VOID device_free_tx_bufs(PSDevice pDevice) {
 853    PUSB_SEND_CONTEXT pTxContext;
 854    int ii;
 855
 856    for (ii = 0; ii < pDevice->cbTD; ii++) {
 857
 858        pTxContext = pDevice->apTD[ii];
 859        //de-allocate URBs
 860        if (pTxContext->pUrb) {
 861            usb_kill_urb(pTxContext->pUrb);
 862            usb_free_urb(pTxContext->pUrb);
 863        }
 864        if (pTxContext)
 865            kfree(pTxContext);
 866    }
 867    return;
 868}
 869
 870
 871static VOID device_free_rx_bufs(PSDevice pDevice) {
 872    PRCB pRCB;
 873    int ii;
 874
 875    for (ii = 0; ii < pDevice->cbRD; ii++) {
 876
 877        pRCB = pDevice->apRCB[ii];
 878        //de-allocate URBs
 879        if (pRCB->pUrb) {
 880            usb_kill_urb(pRCB->pUrb);
 881            usb_free_urb(pRCB->pUrb);
 882        }
 883        //de-allocate skb
 884        if (pRCB->skb)
 885            dev_kfree_skb(pRCB->skb);
 886    }
 887    if (pDevice->pRCBMem)
 888        kfree(pDevice->pRCBMem);
 889
 890    return;
 891}
 892
 893//2007-1107-02<Add>by MikeLiu
 894static void usb_device_reset(PSDevice pDevice)
 895{
 896 int status;
 897 status = usb_reset_device(pDevice->usb);
 898        if (status)
 899            printk("usb_device_reset fail status=%d\n",status);
 900        return ;
 901}
 902
 903static VOID device_free_int_bufs(PSDevice pDevice) {
 904
 905    if (pDevice->intBuf.pDataBuf != NULL)
 906        kfree(pDevice->intBuf.pDataBuf);
 907    return;
 908}
 909
 910
 911static BOOL device_alloc_bufs(PSDevice pDevice) {
 912
 913    PUSB_SEND_CONTEXT pTxContext;
 914    PRCB pRCB;
 915    int ii;
 916
 917
 918    for (ii = 0; ii < pDevice->cbTD; ii++) {
 919
 920        pTxContext = kmalloc(sizeof(USB_SEND_CONTEXT), GFP_KERNEL);
 921        if (pTxContext == NULL) {
 922            DBG_PRT(MSG_LEVEL_ERR,KERN_ERR "%s : allocate tx usb context failed\n", pDevice->dev->name);
 923            goto free_tx;
 924        }
 925        pDevice->apTD[ii] = pTxContext;
 926        pTxContext->pDevice = (PVOID) pDevice;
 927        //allocate URBs
 928        pTxContext->pUrb = usb_alloc_urb(0, GFP_ATOMIC);
 929        if (pTxContext->pUrb == NULL) {
 930            DBG_PRT(MSG_LEVEL_ERR,KERN_ERR "alloc tx urb failed\n");
 931            goto free_tx;
 932        }
 933        pTxContext->bBoolInUse = FALSE;
 934    }
 935
 936    // allocate rcb mem
 937    pDevice->pRCBMem = kmalloc((sizeof(RCB) * pDevice->cbRD), GFP_KERNEL);
 938    if (pDevice->pRCBMem == NULL) {
 939        DBG_PRT(MSG_LEVEL_ERR,KERN_ERR "%s : alloc rx usb context failed\n", pDevice->dev->name);
 940        goto free_tx;
 941    }
 942
 943
 944    pDevice->FirstRecvFreeList = NULL;
 945    pDevice->LastRecvFreeList = NULL;
 946    pDevice->FirstRecvMngList = NULL;
 947    pDevice->LastRecvMngList = NULL;
 948    pDevice->NumRecvFreeList = 0;
 949    memset(pDevice->pRCBMem, 0, (sizeof(RCB) * pDevice->cbRD));
 950    pRCB = (PRCB) pDevice->pRCBMem;
 951
 952    for (ii = 0; ii < pDevice->cbRD; ii++) {
 953
 954        pDevice->apRCB[ii] = pRCB;
 955        pRCB->pDevice = (PVOID) pDevice;
 956        //allocate URBs
 957        pRCB->pUrb = usb_alloc_urb(0, GFP_ATOMIC);
 958
 959        if (pRCB->pUrb == NULL) {
 960            DBG_PRT(MSG_LEVEL_ERR,KERN_ERR" Failed to alloc rx urb\n");
 961            goto free_rx_tx;
 962        }
 963        pRCB->skb = dev_alloc_skb((int)pDevice->rx_buf_sz);
 964        if (pRCB->skb == NULL) {
 965            DBG_PRT(MSG_LEVEL_ERR,KERN_ERR" Failed to alloc rx skb\n");
 966            goto free_rx_tx;
 967        }
 968        pRCB->skb->dev = pDevice->dev;
 969        pRCB->bBoolInUse = FALSE;
 970        EnqueueRCB(pDevice->FirstRecvFreeList, pDevice->LastRecvFreeList, pRCB);
 971        pDevice->NumRecvFreeList++;
 972        pRCB++;
 973    }
 974
 975
 976        pDevice->pControlURB = usb_alloc_urb(0, GFP_ATOMIC);
 977        if (pDevice->pControlURB == NULL) {
 978            DBG_PRT(MSG_LEVEL_ERR,KERN_ERR"Failed to alloc control urb\n");
 979            goto free_rx_tx;
 980        }
 981
 982        pDevice->pInterruptURB = usb_alloc_urb(0, GFP_ATOMIC);
 983        if (pDevice->pInterruptURB == NULL) {
 984            DBG_PRT(MSG_LEVEL_ERR,KERN_ERR"Failed to alloc int urb\n");
 985            usb_kill_urb(pDevice->pControlURB);
 986            usb_free_urb(pDevice->pControlURB);
 987            goto free_rx_tx;
 988        }
 989
 990    pDevice->intBuf.pDataBuf = kmalloc(MAX_INTERRUPT_SIZE, GFP_KERNEL);
 991        if (pDevice->intBuf.pDataBuf == NULL) {
 992            DBG_PRT(MSG_LEVEL_ERR,KERN_ERR"Failed to alloc int buf\n");
 993            usb_kill_urb(pDevice->pControlURB);
 994            usb_kill_urb(pDevice->pInterruptURB);
 995            usb_free_urb(pDevice->pControlURB);
 996            usb_free_urb(pDevice->pInterruptURB);
 997            goto free_rx_tx;
 998        }
 999
1000    return TRUE;
1001
1002free_rx_tx:
1003    device_free_rx_bufs(pDevice);
1004
1005free_tx:
1006    device_free_tx_bufs(pDevice);
1007
1008        return FALSE;
1009}
1010
1011
1012
1013
1014static BOOL device_init_defrag_cb(PSDevice pDevice) {
1015    int i;
1016    PSDeFragControlBlock pDeF;
1017
1018    /* Init the fragment ctl entries */
1019    for (i = 0; i < CB_MAX_RX_FRAG; i++) {
1020        pDeF = &(pDevice->sRxDFCB[i]);
1021        if (!device_alloc_frag_buf(pDevice, pDeF)) {
1022            DBG_PRT(MSG_LEVEL_ERR,KERN_ERR "%s: can not alloc frag bufs\n",
1023                pDevice->dev->name);
1024            goto free_frag;
1025        };
1026    }
1027    pDevice->cbDFCB = CB_MAX_RX_FRAG;
1028    pDevice->cbFreeDFCB = pDevice->cbDFCB;
1029    return TRUE;
1030
1031free_frag:
1032    device_free_frag_bufs(pDevice);
1033    return FALSE;
1034}
1035
1036
1037
1038static void device_free_frag_bufs(PSDevice pDevice) {
1039    PSDeFragControlBlock pDeF;
1040    int i;
1041
1042    for (i = 0; i < CB_MAX_RX_FRAG; i++) {
1043
1044        pDeF = &(pDevice->sRxDFCB[i]);
1045
1046        if (pDeF->skb)
1047            dev_kfree_skb(pDeF->skb);
1048    }
1049}
1050
1051
1052
1053BOOL device_alloc_frag_buf(PSDevice pDevice, PSDeFragControlBlock pDeF) {
1054
1055    pDeF->skb = dev_alloc_skb((int)pDevice->rx_buf_sz);
1056    if (pDeF->skb == NULL)
1057        return FALSE;
1058    ASSERT(pDeF->skb);
1059    pDeF->skb->dev = pDevice->dev;
1060
1061    return TRUE;
1062}
1063
1064
1065/*-----------------------------------------------------------------*/
1066
1067static int  device_open(struct net_device *dev) {
1068    PSDevice    pDevice=(PSDevice) netdev_priv(dev);
1069
1070#ifdef WPA_SM_Transtatus
1071     extern SWPAResult wpa_Result;
1072     memset(wpa_Result.ifname,0,sizeof(wpa_Result.ifname));
1073     wpa_Result.proto = 0;
1074     wpa_Result.key_mgmt = 0;
1075     wpa_Result.eap_type = 0;
1076     wpa_Result.authenticated = FALSE;
1077     pDevice->fWPA_Authened = FALSE;
1078#endif
1079
1080    DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " device_open...\n");
1081
1082
1083    pDevice->rx_buf_sz = MAX_TOTAL_SIZE_WITH_ALL_HEADERS;
1084
1085    if (device_alloc_bufs(pDevice) == FALSE) {
1086        DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " device_alloc_bufs fail... \n");
1087        return -ENOMEM;
1088    }
1089
1090    if (device_init_defrag_cb(pDevice)== FALSE) {
1091        DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " Initial defragement cb fail \n");
1092        goto free_rx_tx;
1093    }
1094
1095    MP_CLEAR_FLAG(pDevice, fMP_DISCONNECTED);
1096    MP_CLEAR_FLAG(pDevice, fMP_CONTROL_READS);
1097    MP_CLEAR_FLAG(pDevice, fMP_CONTROL_WRITES);
1098    MP_SET_FLAG(pDevice, fMP_POST_READS);
1099    MP_SET_FLAG(pDevice, fMP_POST_WRITES);
1100
1101   //read config file
1102    Read_config_file(pDevice);
1103
1104    if (device_init_registers(pDevice, DEVICE_INIT_COLD) == FALSE) {
1105        DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " init register fail\n");
1106        goto free_all;
1107    }
1108
1109    device_set_multi(pDevice->dev);
1110    // Init for Key Management
1111
1112    KeyvInitTable(pDevice,&pDevice->sKey);
1113    memcpy(pDevice->sMgmtObj.abyMACAddr, pDevice->abyCurrentNetAddr, U_ETHER_ADDR_LEN);
1114    memcpy(pDevice->dev->dev_addr, pDevice->abyCurrentNetAddr, U_ETHER_ADDR_LEN);
1115    pDevice->bStopTx0Pkt = FALSE;
1116    pDevice->bStopDataPkt = FALSE;
1117    pDevice->bRoaming = FALSE;  //DavidWang
1118    pDevice->bIsRoaming = FALSE;//DavidWang
1119    pDevice->bEnableRoaming = FALSE;
1120    if (pDevice->bDiversityRegCtlON) {
1121        device_init_diversity_timer(pDevice);
1122    }
1123
1124    vMgrObjectInit(pDevice);
1125    tasklet_init(&pDevice->RxMngWorkItem, (void *)RXvMngWorkItem, (unsigned long)pDevice);
1126    tasklet_init(&pDevice->ReadWorkItem, (void *)RXvWorkItem, (unsigned long)pDevice);
1127    tasklet_init(&pDevice->EventWorkItem, (void *)INTvWorkItem, (unsigned long)pDevice);
1128    add_timer(&(pDevice->sMgmtObj.sTimerSecondCallback));
1129    pDevice->int_interval = 100;  //Max 100 microframes.
1130    pDevice->eEncryptionStatus = Ndis802_11EncryptionDisabled;
1131
1132    pDevice->bIsRxWorkItemQueued = TRUE;
1133    pDevice->fKillEventPollingThread = FALSE;
1134    pDevice->bEventAvailable = FALSE;
1135
1136   pDevice->bWPADEVUp = FALSE;
1137#ifdef WPA_SUPPLICANT_DRIVER_WEXT_SUPPORT
1138     pDevice->bwextstep0 = FALSE;
1139     pDevice->bwextstep1 = FALSE;
1140     pDevice->bwextstep2 = FALSE;
1141     pDevice->bwextstep3 = FALSE;
1142     pDevice->bWPASuppWextEnabled = FALSE;
1143#endif
1144    pDevice->byReAssocCount = 0;
1145
1146    RXvWorkItem(pDevice);
1147    INTvWorkItem(pDevice);
1148
1149    // Patch: if WEP key already set by iwconfig but device not yet open
1150    if ((pDevice->bEncryptionEnable == TRUE) && (pDevice->bTransmitKey == TRUE)) {
1151         spin_lock_irq(&pDevice->lock);
1152         KeybSetDefaultKey( pDevice,
1153                            &(pDevice->sKey),
1154                            pDevice->byKeyIndex | (1 << 31),
1155                            pDevice->uKeyLength,
1156                            NULL,
1157                            pDevice->abyKey,
1158                            KEY_CTL_WEP
1159                          );
1160         spin_unlock_irq(&pDevice->lock);
1161         pDevice->eEncryptionStatus = Ndis802_11Encryption1Enabled;
1162    }
1163
1164    if (pDevice->sMgmtObj.eConfigMode == WMAC_CONFIG_AP) {
1165        bScheduleCommand((HANDLE)pDevice, WLAN_CMD_RUN_AP, NULL);
1166        }
1167        else {
1168        //mike:mark@2008-11-10
1169            bScheduleCommand((HANDLE)pDevice, WLAN_CMD_BSSID_SCAN, NULL);
1170        //bScheduleCommand((HANDLE)pDevice, WLAN_CMD_SSID, NULL);
1171    }
1172
1173
1174    netif_stop_queue(pDevice->dev);
1175    pDevice->flags |= DEVICE_FLAGS_OPENED;
1176
1177#ifdef SndEvt_ToAPI
1178{
1179  union iwreq_data      wrqu;
1180  memset(&wrqu, 0, sizeof(wrqu));
1181  wrqu.data.flags = RT_UPDEV_EVENT_FLAG;
1182  wireless_send_event(pDevice->dev, IWEVCUSTOM, &wrqu, NULL);
1183}
1184#endif
1185
1186    DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "device_open success.. \n");
1187    return 0;
1188
1189free_all:
1190    device_free_frag_bufs(pDevice);
1191free_rx_tx:
1192    device_free_rx_bufs(pDevice);
1193    device_free_tx_bufs(pDevice);
1194    device_free_int_bufs(pDevice);
1195        usb_kill_urb(pDevice->pControlURB);
1196        usb_kill_urb(pDevice->pInterruptURB);
1197    usb_free_urb(pDevice->pControlURB);
1198    usb_free_urb(pDevice->pInterruptURB);
1199
1200    DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "device_open fail.. \n");
1201    return -ENOMEM;
1202}
1203
1204
1205
1206static int  device_close(struct net_device *dev) {
1207    PSDevice    pDevice=(PSDevice) netdev_priv(dev);
1208    PSMgmtObject     pMgmt = &(pDevice->sMgmtObj);
1209
1210        int uu;
1211
1212    DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "device_close1 \n");
1213    if (pDevice == NULL)
1214        return -ENODEV;
1215
1216#ifdef SndEvt_ToAPI
1217{
1218  union iwreq_data      wrqu;
1219  memset(&wrqu, 0, sizeof(wrqu));
1220  wrqu.data.flags = RT_DOWNDEV_EVENT_FLAG;
1221  wireless_send_event(pDevice->dev, IWEVCUSTOM, &wrqu, NULL);
1222}
1223#endif
1224
1225//2007-1121-02<Add>by EinsnLiu
1226    if (pDevice->bLinkPass) {
1227        bScheduleCommand((HANDLE)pDevice, WLAN_CMD_DISASSOCIATE, NULL);
1228        mdelay(30);
1229    }
1230//End Add
1231
1232//2008-0714-01<Add>by MikeLiu
1233device_release_WPADEV(pDevice);
1234
1235        memset(pMgmt->abyDesireSSID, 0, WLAN_IEHDR_LEN + WLAN_SSID_MAXLEN + 1);
1236        pMgmt->bShareKeyAlgorithm = FALSE;
1237        pDevice->bEncryptionEnable = FALSE;
1238        pDevice->eEncryptionStatus = Ndis802_11EncryptionDisabled;
1239            spin_lock_irq(&pDevice->lock);
1240            for(uu=0;uu<MAX_KEY_TABLE;uu++)
1241                MACvDisableKeyEntry(pDevice,uu);
1242            spin_unlock_irq(&pDevice->lock);
1243
1244    if ((pDevice->flags & DEVICE_FLAGS_UNPLUG) == FALSE) {
1245        MACbShutdown(pDevice);
1246    }
1247    netif_stop_queue(pDevice->dev);
1248    MP_SET_FLAG(pDevice, fMP_DISCONNECTED);
1249    MP_CLEAR_FLAG(pDevice, fMP_POST_WRITES);
1250    MP_CLEAR_FLAG(pDevice, fMP_POST_READS);
1251    pDevice->fKillEventPollingThread = TRUE;
1252    del_timer(&pDevice->sTimerCommand);
1253    del_timer(&pMgmt->sTimerSecondCallback);
1254
1255//2007-0115-02<Add>by MikeLiu
1256#ifdef TxInSleep
1257    del_timer(&pDevice->sTimerTxData);
1258#endif
1259
1260    if (pDevice->bDiversityRegCtlON) {
1261        del_timer(&pDevice->TimerSQ3Tmax1);
1262        del_timer(&pDevice->TimerSQ3Tmax2);
1263        del_timer(&pDevice->TimerSQ3Tmax3);
1264    }
1265    tasklet_kill(&pDevice->RxMngWorkItem);
1266    tasklet_kill(&pDevice->ReadWorkItem);
1267    tasklet_kill(&pDevice->EventWorkItem);
1268
1269   pDevice->bRoaming = FALSE;  //DavidWang
1270   pDevice->bIsRoaming = FALSE;//DavidWang
1271   pDevice->bEnableRoaming = FALSE;
1272    pDevice->bCmdRunning = FALSE;
1273    pDevice->bLinkPass = FALSE;
1274    memset(pMgmt->abyCurrBSSID, 0, 6);
1275    pMgmt->eCurrState = WMAC_STATE_IDLE;
1276
1277    device_free_tx_bufs(pDevice);
1278    device_free_rx_bufs(pDevice);
1279    device_free_int_bufs(pDevice);
1280    device_free_frag_bufs(pDevice);
1281
1282        usb_kill_urb(pDevice->pControlURB);
1283        usb_kill_urb(pDevice->pInterruptURB);
1284    usb_free_urb(pDevice->pControlURB);
1285    usb_free_urb(pDevice->pInterruptURB);
1286
1287    BSSvClearNodeDBTable(pDevice, 0);
1288    pDevice->flags &=(~DEVICE_FLAGS_OPENED);
1289
1290    DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "device_close2 \n");
1291
1292    return 0;
1293}
1294
1295
1296static void vntwusb_disconnect(struct usb_interface *intf)
1297
1298{
1299
1300        PSDevice  pDevice = usb_get_intfdata(intf);
1301
1302    DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "device_disconnect1.. \n");
1303    if (pDevice == NULL)
1304        return;
1305
1306#ifdef SndEvt_ToAPI
1307{
1308  union iwreq_data      wrqu;
1309  memset(&wrqu, 0, sizeof(wrqu));
1310  wrqu.data.flags = RT_RMMOD_EVENT_FLAG;
1311  wireless_send_event(pDevice->dev, IWEVCUSTOM, &wrqu, NULL);
1312}
1313#endif
1314
1315//2008-0714-01<Add>by MikeLiu
1316device_release_WPADEV(pDevice);
1317
1318        usb_set_intfdata(intf, NULL);
1319//2008-0922-01<Add>by MikeLiu, decrease usb counter.
1320     usb_put_dev(interface_to_usbdev(intf));
1321
1322    pDevice->flags |= DEVICE_FLAGS_UNPLUG;
1323    if (pDevice->dev != NULL) {
1324        DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "unregister_netdev..\n");
1325        unregister_netdev(pDevice->dev);
1326
1327//2008-07-21-01<Add>by MikeLiu
1328//unregister wpadev
1329   if(wpa_set_wpadev(pDevice, 0)!=0)
1330     printk("unregister wpadev fail?\n");
1331
1332        free_netdev(pDevice->dev);
1333    }
1334
1335    DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "device_disconnect3.. \n");
1336}
1337
1338
1339
1340
1341static int device_dma0_tx_80211(struct sk_buff *skb, struct net_device *dev) {
1342    PSDevice        pDevice=netdev_priv(dev);
1343    PBYTE           pbMPDU;
1344    UINT            cbMPDULen = 0;
1345
1346
1347    DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "device_dma0_tx_80211\n");
1348    spin_lock_irq(&pDevice->lock);
1349
1350    if (pDevice->bStopTx0Pkt == TRUE) {
1351        dev_kfree_skb_irq(skb);
1352        spin_unlock_irq(&pDevice->lock);
1353        return 0;
1354    };
1355
1356
1357    cbMPDULen = skb->len;
1358    pbMPDU = skb->data;
1359
1360    vDMA0_tx_80211(pDevice, skb);
1361
1362    spin_unlock_irq(&pDevice->lock);
1363
1364    return 0;
1365
1366}
1367
1368
1369static int  device_xmit(struct sk_buff *skb, struct net_device *dev) {
1370    PSDevice    pDevice=netdev_priv(dev);
1371    struct net_device_stats* pStats = &pDevice->stats;
1372
1373
1374    spin_lock_irq(&pDevice->lock);
1375
1376    netif_stop_queue(pDevice->dev);
1377
1378    if (pDevice->bLinkPass == FALSE) {
1379        dev_kfree_skb_irq(skb);
1380        spin_unlock_irq(&pDevice->lock);
1381        return 0;
1382    }
1383    if (pDevice->bStopDataPkt == TRUE) {
1384        dev_kfree_skb_irq(skb);
1385        pStats->tx_dropped++;
1386        spin_unlock_irq(&pDevice->lock);
1387        return 0;
1388    }
1389
1390    if(nsDMA_tx_packet(pDevice, TYPE_AC0DMA, skb) !=0) {  //mike add:xmit fail!
1391         if (netif_queue_stopped(pDevice->dev))
1392              netif_wake_queue(pDevice->dev);
1393    }
1394
1395    spin_unlock_irq(&pDevice->lock);
1396
1397    return 0;
1398}
1399
1400
1401
1402static unsigned const ethernet_polynomial = 0x04c11db7U;
1403static inline u32 ether_crc(int length, unsigned char *data)
1404{
1405    int crc = -1;
1406
1407    while(--length >= 0) {
1408        unsigned char current_octet = *data++;
1409        int bit;
1410        for (bit = 0; bit < 8; bit++, current_octet >>= 1) {
1411            crc = (crc << 1) ^
1412                ((crc < 0) ^ (current_octet & 1) ? ethernet_polynomial : 0);
1413        }
1414    }
1415    return crc;
1416}
1417
1418//find out  the start  position of str2 from str1
1419static UCHAR *kstrstr(const UCHAR *str1,const UCHAR *str2) {
1420  int str1_len=strlen(str1);
1421  int str2_len=strlen(str2);
1422
1423  while (str1_len >= str2_len) {
1424       str1_len--;
1425      if(memcmp(str1,str2,str2_len)==0)
1426         return (UCHAR *)str1;
1427        str1++;
1428  }
1429  return NULL;
1430}
1431
1432static int Config_FileGetParameter(UCHAR *string, UCHAR *dest,UCHAR *source)
1433{
1434  UCHAR buf1[100];
1435  UCHAR buf2[100];
1436  UCHAR *start_p=NULL,*end_p=NULL,*tmp_p=NULL;
1437  int ii;
1438
1439    memset(buf1,0,100);
1440    strcat(buf1, string);
1441    strcat(buf1, "=");
1442    source+=strlen(buf1);
1443
1444//find target string start point
1445    if((start_p = kstrstr(source,buf1))==NULL)
1446        return FALSE;
1447
1448//check if current config line is marked by "#" ??
1449for(ii=1;;ii++) {
1450  if(memcmp(start_p-ii,"\n",1)==0)
1451      break;
1452  if(memcmp(start_p-ii,"#",1)==0)
1453      return FALSE;
1454}
1455
1456//find target string end point
1457     if((end_p = kstrstr(start_p,"\n"))==NULL) {       //cann't find "\n",but don't care
1458          end_p=start_p+strlen(start_p);   //no include "\n"
1459       }
1460
1461   memset(buf2,0,100);
1462   memcpy(buf2,start_p,end_p-start_p);    //get the tartget line
1463   buf2[end_p-start_p]='\0';
1464
1465   //find value
1466   if((start_p = kstrstr(buf2,"="))==NULL)
1467      return FALSE;
1468   memset(buf1,0,100);
1469   strcpy(buf1,start_p+1);
1470
1471  //except space
1472  tmp_p = buf1;
1473  while(*tmp_p != 0x00) {
1474        if(*tmp_p==' ')
1475            tmp_p++;
1476         else
1477          break;
1478  }
1479
1480   memcpy(dest,tmp_p,strlen(tmp_p));
1481 return TRUE;
1482}
1483
1484//if read fail,return NULL,or return data pointer;
1485static UCHAR *Config_FileOperation(PSDevice pDevice) {
1486    UCHAR    *config_path=CONFIG_PATH;
1487    UCHAR    *buffer=NULL;
1488    struct file   *filp=NULL;
1489    mm_segment_t old_fs = get_fs();
1490    //int oldfsuid=0,oldfsgid=0;
1491    int result=0;
1492
1493    set_fs (KERNEL_DS);
1494    /* Can't do this anymore, so we rely on correct filesystem permissions:
1495    //Make sure a caller can read or write power as root
1496    oldfsuid=current->fsuid;
1497    oldfsgid=current->fsgid;
1498    current->fsuid = 0;
1499    current->fsgid = 0;
1500    */
1501
1502    //open file
1503      filp = filp_open(config_path, O_RDWR, 0);
1504        if (IS_ERR(filp)) {
1505             printk("Config_FileOperation file Not exist\n");
1506             result=-1;
1507             goto error2;
1508          }
1509
1510     if(!(filp->f_op) || !(filp->f_op->read) ||!(filp->f_op->write)) {
1511           printk("file %s cann't readable or writable?\n",config_path);
1512          result = -1;
1513          goto error1;
1514        }
1515
1516    buffer = (UCHAR *)kmalloc(1024, GFP_KERNEL);
1517    if(buffer==NULL) {
1518      printk("alllocate mem for file fail?\n");
1519      result = -1;
1520      goto error1;
1521    }
1522
1523    if(filp->f_op->read(filp, buffer, 1024, &filp->f_pos)<0) {
1524     printk("read file error?\n");
1525     result = -1;
1526    }
1527
1528error1:
1529  if(filp_close(filp,NULL))
1530       printk("Config_FileOperation:close file fail\n");
1531
1532error2:
1533  set_fs (old_fs);
1534
1535  /*
1536  current->fsuid=oldfsuid;
1537  current->fsgid=oldfsgid;
1538  */
1539
1540if(result!=0) {
1541    if(buffer)
1542         kfree(buffer);
1543    buffer=NULL;
1544}
1545  return buffer;
1546}
1547
1548//return --->-1:fail;  >=0:sucessful
1549static int Read_config_file(PSDevice pDevice) {
1550  int result=0;
1551  UCHAR      tmpbuffer[100];
1552  UCHAR *buffer=NULL;
1553
1554  //init config setting
1555 pDevice->config_file.ZoneType = -1;
1556 pDevice->config_file.eAuthenMode = -1;
1557 pDevice->config_file.eEncryptionStatus = -1;
1558
1559  if((buffer=Config_FileOperation(pDevice)) ==NULL) {
1560     result =-1;
1561     return result;
1562  }
1563
1564//get zonetype
1565{
1566    memset(tmpbuffer,0,sizeof(tmpbuffer));
1567    if(Config_FileGetParameter("ZONETYPE",tmpbuffer,buffer) ==TRUE) {
1568    if(memcmp(tmpbuffer,"USA",3)==0) {
1569      pDevice->config_file.ZoneType=ZoneType_USA;
1570    }
1571    else if(memcmp(tmpbuffer,"JAPAN",5)==0) {
1572      pDevice->config_file.ZoneType=ZoneType_Japan;
1573    }
1574    else if(memcmp(tmpbuffer,"EUROPE",6)==0) {
1575     pDevice->config_file.ZoneType=ZoneType_Europe;
1576    }
1577    else {
1578      printk("Unknown Zonetype[%s]?\n",tmpbuffer);
1579   }
1580 }
1581}
1582
1583#if 1
1584//get other parameter
1585  {
1586        memset(tmpbuffer,0,sizeof(tmpbuffer));
1587       if(Config_FileGetParameter("AUTHENMODE",tmpbuffer,buffer)==TRUE) {
1588         pDevice->config_file.eAuthenMode = (int) simple_strtol(tmpbuffer, NULL, 10);
1589       }
1590
1591        memset(tmpbuffer,0,sizeof(tmpbuffer));
1592       if(Config_FileGetParameter("ENCRYPTIONMODE",tmpbuffer,buffer)==TRUE) {
1593         pDevice->config_file.eEncryptionStatus= (int) simple_strtol(tmpbuffer, NULL, 10);
1594       }
1595  }
1596#endif
1597
1598  kfree(buffer);
1599  return result;
1600}
1601
1602static void device_set_multi(struct net_device *dev) {
1603    PSDevice         pDevice = (PSDevice) netdev_priv(dev);
1604    PSMgmtObject     pMgmt = &(pDevice->sMgmtObj);
1605    u32              mc_filter[2];
1606    int              ii;
1607    struct dev_mc_list  *mclist;
1608    BYTE             pbyData[8] = {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
1609    BYTE             byTmpMode = 0;
1610    int              rc;
1611
1612
1613        spin_lock_irq(&pDevice->lock);
1614    rc = CONTROLnsRequestIn(pDevice,
1615                            MESSAGE_TYPE_READ,
1616                            MAC_REG_RCR,
1617                            MESSAGE_REQUEST_MACREG,
1618                            1,
1619                            &byTmpMode
1620                            );
1621    if (rc == 0) pDevice->byRxMode = byTmpMode;
1622
1623    DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "pDevice->byRxMode in= %x\n", pDevice->byRxMode);
1624
1625    if (dev->flags & IFF_PROMISC) {         // Set promiscuous.
1626        DBG_PRT(MSG_LEVEL_ERR,KERN_NOTICE "%s: Promiscuous mode enabled.\n", dev->name);
1627        // Unconditionally log net taps.
1628        pDevice->byRxMode |= (RCR_MULTICAST|RCR_BROADCAST|RCR_UNICAST);
1629    }
1630    else if ((dev->mc_count > pDevice->multicast_limit) || (dev->flags & IFF_ALLMULTI)) {
1631        CONTROLnsRequestOut(pDevice,
1632                            MESSAGE_TYPE_WRITE,
1633                            MAC_REG_MAR0,
1634                            MESSAGE_REQUEST_MACREG,
1635                            8,
1636                            pbyData
1637                            );
1638        pDevice->byRxMode |= (RCR_MULTICAST|RCR_BROADCAST);
1639    }
1640    else {
1641        memset(mc_filter, 0, sizeof(mc_filter));
1642        for (ii = 0, mclist = dev->mc_list; mclist && ii < dev->mc_count;
1643             ii++, mclist = mclist->next) {
1644            int bit_nr = ether_crc(ETH_ALEN, mclist->dmi_addr) >> 26;
1645            mc_filter[bit_nr >> 5] |= cpu_to_le32(1 << (bit_nr & 31));
1646        }
1647        for (ii = 0; ii < 4; ii++) {
1648             MACvWriteMultiAddr(pDevice, ii, *((PBYTE)&mc_filter[0] + ii));
1649             MACvWriteMultiAddr(pDevice, ii+ 4, *((PBYTE)&mc_filter[1] + ii));
1650        }
1651        pDevice->byRxMode &= ~(RCR_UNICAST);
1652        pDevice->byRxMode |= (RCR_MULTICAST|RCR_BROADCAST);
1653    }
1654
1655    if (pMgmt->eConfigMode == WMAC_CONFIG_AP) {
1656        // If AP mode, don't enable RCR_UNICAST. Since hw only compare addr1 with local mac.
1657        pDevice->byRxMode |= (RCR_MULTICAST|RCR_BROADCAST);
1658        pDevice->byRxMode &= ~(RCR_UNICAST);
1659    }
1660    ControlvWriteByte(pDevice, MESSAGE_REQUEST_MACREG, MAC_REG_RCR, pDevice->byRxMode);
1661    DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "pDevice->byRxMode out= %x\n", pDevice->byRxMode);
1662        spin_unlock_irq(&pDevice->lock);
1663
1664}
1665
1666
1667static struct net_device_stats *device_get_stats(struct net_device *dev) {
1668    PSDevice pDevice=(PSDevice) netdev_priv(dev);
1669
1670    return &pDevice->stats;
1671}
1672
1673
1674static int  device_ioctl(struct net_device *dev, struct ifreq *rq, int cmd) {
1675        PSDevice                pDevice = (PSDevice)netdev_priv(dev);
1676    PSMgmtObject        pMgmt = &(pDevice->sMgmtObj);
1677    PSCmdRequest        pReq;
1678    //BOOL                bCommit = FALSE;
1679        struct iwreq *wrq = (struct iwreq *) rq;
1680        int                 rc =0;
1681
1682    if (pMgmt == NULL) {
1683        rc = -EFAULT;
1684        return rc;
1685    }
1686
1687    switch(cmd) {
1688
1689        case SIOCGIWNAME:
1690                rc = iwctl_giwname(dev, NULL, (char *)&(wrq->u.name), NULL);
1691                break;
1692
1693        case SIOCSIWNWID:
1694        rc = -EOPNOTSUPP;
1695                break;
1696
1697        case SIOCGIWNWID:     //0x8b03  support
1698        #ifdef  WPA_SUPPLICANT_DRIVER_WEXT_SUPPORT
1699          rc = iwctl_giwnwid(dev, NULL, &(wrq->u.nwid), NULL);
1700        #else
1701        rc = -EOPNOTSUPP;
1702        #endif
1703                break;
1704
1705                // Set frequency/channel
1706        case SIOCSIWFREQ:
1707            rc = iwctl_siwfreq(dev, NULL, &(wrq->u.freq), NULL);
1708                break;
1709
1710                // Get frequency/channel
1711        case SIOCGIWFREQ:
1712                rc = iwctl_giwfreq(dev, NULL, &(wrq->u.freq), NULL);
1713                break;
1714
1715                // Set desired network name (ESSID)
1716        case SIOCSIWESSID:
1717
1718                {
1719                        char essid[IW_ESSID_MAX_SIZE+1];
1720                        if (wrq->u.essid.length > IW_ESSID_MAX_SIZE) {
1721                                rc = -E2BIG;
1722                                break;
1723                        }
1724                        if (copy_from_user(essid, wrq->u.essid.pointer,
1725                                           wrq->u.essid.length)) {
1726                                rc = -EFAULT;
1727                                break;
1728                        }
1729                        rc = iwctl_siwessid(dev, NULL,
1730                                            &(wrq->u.essid), essid);
1731                }
1732                break;
1733
1734
1735                // Get current network name (ESSID)
1736        case SIOCGIWESSID:
1737
1738                {
1739                        char essid[IW_ESSID_MAX_SIZE+1];
1740                        if (wrq->u.essid.pointer)
1741                                rc = iwctl_giwessid(dev, NULL,
1742                                                    &(wrq->u.essid), essid);
1743                                if (copy_to_user(wrq->u.essid.pointer,
1744                                                         essid,
1745                                                         wrq->u.essid.length) )
1746                                        rc = -EFAULT;
1747                }
1748                break;
1749
1750        case SIOCSIWAP:
1751
1752                rc = iwctl_siwap(dev, NULL, &(wrq->u.ap_addr), NULL);
1753                break;
1754
1755
1756                // Get current Access Point (BSSID)
1757        case SIOCGIWAP:
1758                rc = iwctl_giwap(dev, NULL, &(wrq->u.ap_addr), NULL);
1759                break;
1760
1761
1762                // Set desired station name
1763        case SIOCSIWNICKN:
1764        DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCSIWNICKN \n");
1765        rc = -EOPNOTSUPP;
1766                break;
1767
1768                // Get current station name
1769        case SIOCGIWNICKN:
1770        DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCGIWNICKN \n");
1771        rc = -EOPNOTSUPP;
1772                break;
1773
1774                // Set the desired bit-rate
1775        case SIOCSIWRATE:
1776                rc = iwctl_siwrate(dev, NULL, &(wrq->u.bitrate), NULL);
1777                break;
1778
1779        // Get the current bit-rate
1780        case SIOCGIWRATE:
1781
1782                rc = iwctl_giwrate(dev, NULL, &(wrq->u.bitrate), NULL);
1783                break;
1784
1785        // Set the desired RTS threshold
1786        case SIOCSIWRTS:
1787
1788                rc = iwctl_siwrts(dev, NULL, &(wrq->u.rts), NULL);
1789                break;
1790
1791        // Get the current RTS threshold
1792        case SIOCGIWRTS:
1793
1794                rc = iwctl_giwrts(dev, NULL, &(wrq->u.rts), NULL);
1795                break;
1796
1797                // Set the desired fragmentation threshold
1798        case SIOCSIWFRAG:
1799
1800                rc = iwctl_siwfrag(dev, NULL, &(wrq->u.frag), NULL);
1801            break;
1802
1803        // Get the current fragmentation threshold
1804        case SIOCGIWFRAG:
1805
1806                rc = iwctl_giwfrag(dev, NULL, &(wrq->u.frag), NULL);
1807                break;
1808
1809                // Set mode of operation
1810        case SIOCSIWMODE:
1811        rc = iwctl_siwmode(dev, NULL, &(wrq->u.mode), NULL);
1812                break;
1813
1814                // Get mode of operation
1815        case SIOCGIWMODE:
1816                rc = iwctl_giwmode(dev, NULL, &(wrq->u.mode), NULL);
1817                break;
1818
1819                // Set WEP keys and mode
1820        case SIOCSIWENCODE:
1821                {
1822            char abyKey[WLAN_WEP232_KEYLEN];
1823
1824                        if (wrq->u.encoding.pointer) {
1825
1826
1827                                if (wrq->u.encoding.length > WLAN_WEP232_KEYLEN) {
1828                                        rc = -E2BIG;
1829                                        break;
1830                                }
1831                                memset(abyKey, 0, WLAN_WEP232_KEYLEN);
1832                                if (copy_from_user(abyKey,
1833                                                  wrq->u.encoding.pointer,
1834                                                  wrq->u.encoding.length)) {
1835                                        rc = -EFAULT;
1836                                        break;
1837                                }
1838                        } else if (wrq->u.encoding.length != 0) {
1839                                rc = -EINVAL;
1840                                break;
1841                        }
1842                        rc = iwctl_siwencode(dev, NULL, &(wrq->u.encoding), abyKey);
1843                }
1844                break;
1845
1846                // Get the WEP keys and mode
1847        case SIOCGIWENCODE:
1848
1849                if (!capable(CAP_NET_ADMIN)) {
1850                        rc = -EPERM;
1851                        break;
1852                }
1853                {
1854                    char abyKey[WLAN_WEP232_KEYLEN];
1855
1856                    rc = iwctl_giwencode(dev, NULL, &(wrq->u.encoding), abyKey);
1857                    if (rc != 0) break;
1858                        if (wrq->u.encoding.pointer) {
1859                                if (copy_to_user(wrq->u.encoding.pointer,
1860                                                        abyKey,
1861                                                        wrq->u.encoding.length))
1862                                        rc = -EFAULT;
1863                        }
1864                }
1865                break;
1866
1867                // Get the current Tx-Power
1868        case SIOCGIWTXPOW:
1869        DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCGIWTXPOW \n");
1870        rc = -EOPNOTSUPP;
1871                break;
1872
1873        case SIOCSIWTXPOW:
1874        DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCGIWTXPOW \n");
1875        rc = -EOPNOTSUPP;
1876                break;
1877
1878        case SIOCSIWRETRY:
1879
1880                rc = iwctl_siwretry(dev, NULL, &(wrq->u.retry), NULL);
1881                break;
1882
1883        case SIOCGIWRETRY:
1884
1885                rc = iwctl_giwretry(dev, NULL, &(wrq->u.retry), NULL);
1886                break;
1887
1888                // Get range of parameters
1889        case SIOCGIWRANGE:
1890
1891                {
1892                        struct iw_range range;
1893
1894                        rc = iwctl_giwrange(dev, NULL, &(wrq->u.data), (char *) &range);
1895                        if (copy_to_user(wrq->u.data.pointer, &range, sizeof(struct iw_range)))
1896                                rc = -EFAULT;
1897                }
1898
1899                break;
1900
1901        case SIOCGIWPOWER:
1902
1903                rc = iwctl_giwpower(dev, NULL, &(wrq->u.power), NULL);
1904                break;
1905
1906
1907        case SIOCSIWPOWER:
1908
1909                rc = iwctl_siwpower(dev, NULL, &(wrq->u.power), NULL);
1910                break;
1911
1912
1913        case SIOCGIWSENS:
1914
1915            rc = iwctl_giwsens(dev, NULL, &(wrq->u.sens), NULL);
1916                break;
1917
1918        case SIOCSIWSENS:
1919        DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCSIWSENS \n");
1920                rc = -EOPNOTSUPP;
1921                break;
1922
1923        case SIOCGIWAPLIST:
1924            {
1925            char buffer[IW_MAX_AP * (sizeof(struct sockaddr) + sizeof(struct iw_quality))];
1926
1927                    if (wrq->u.data.pointer) {
1928                        rc = iwctl_giwaplist(dev, NULL, &(wrq->u.data), buffer);
1929                        if (rc == 0) {
1930                    if (copy_to_user(wrq->u.data.pointer,
1931                                                        buffer,
1932                                                       (wrq->u.data.length * (sizeof(struct sockaddr) +  sizeof(struct iw_quality)))
1933                                        ))
1934                                    rc = -EFAULT;
1935                        }
1936            }
1937        }
1938                break;
1939
1940
1941#ifdef WIRELESS_SPY
1942                // Set the spy list
1943        case SIOCSIWSPY:
1944
1945        DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCSIWSPY \n");
1946                rc = -EOPNOTSUPP;
1947                break;
1948
1949                // Get the spy list
1950        case SIOCGIWSPY:
1951
1952        DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCSIWSPY \n");
1953                rc = -EOPNOTSUPP;
1954                break;
1955
1956#endif // WIRELESS_SPY
1957
1958        case SIOCGIWPRIV:
1959        DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCGIWPRIV \n");
1960                rc = -EOPNOTSUPP;
1961/*
1962                if(wrq->u.data.pointer) {
1963                        wrq->u.data.length = sizeof(iwctl_private_args) / sizeof( iwctl_private_args[0]);
1964
1965                        if(copy_to_user(wrq->u.data.pointer,
1966                                        (u_char *) iwctl_private_args,
1967                                        sizeof(iwctl_private_args)))
1968                                rc = -EFAULT;
1969                }
1970*/
1971                break;
1972
1973
1974//2008-0409-07, <Add> by Einsn Liu
1975#ifdef  WPA_SUPPLICANT_DRIVER_WEXT_SUPPORT
1976        case SIOCSIWAUTH:
1977                DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCSIWAUTH \n");
1978                rc = iwctl_siwauth(dev, NULL, &(wrq->u.param), NULL);
1979                break;
1980
1981        case SIOCGIWAUTH:
1982                DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCGIWAUTH \n");
1983                rc = iwctl_giwauth(dev, NULL, &(wrq->u.param), NULL);
1984                break;
1985
1986        case SIOCSIWGENIE:
1987                DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCSIWGENIE \n");
1988                rc = iwctl_siwgenie(dev, NULL, &(wrq->u.data), wrq->u.data.pointer);
1989                break;
1990
1991        case SIOCGIWGENIE:
1992                DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCGIWGENIE \n");
1993                rc = iwctl_giwgenie(dev, NULL, &(wrq->u.data), wrq->u.data.pointer);
1994                break;
1995
1996        case SIOCSIWENCODEEXT:
1997                {
1998                        char extra[sizeof(struct iw_encode_ext)+MAX_KEY_LEN+1];
1999                        DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCSIWENCODEEXT \n");
2000                        if(wrq->u.encoding.pointer){
2001                                memset(extra, 0, sizeof(struct iw_encode_ext)+MAX_KEY_LEN+1);
2002                                if(wrq->u.encoding.length > (sizeof(struct iw_encode_ext)+ MAX_KEY_LEN)){
2003                                        rc = -E2BIG;
2004                                        break;
2005                                }
2006                                if(copy_from_user(extra, wrq->u.encoding.pointer,wrq->u.encoding.length)){
2007                                        rc = -EFAULT;
2008                                        break;
2009                                }
2010                        }else if(wrq->u.encoding.length != 0){
2011                                rc = -EINVAL;
2012                                break;
2013                        }
2014                        rc = iwctl_siwencodeext(dev, NULL, &(wrq->u.encoding), extra);
2015                }
2016                break;
2017
2018        case SIOCGIWENCODEEXT:
2019                DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCGIWENCODEEXT \n");
2020                rc = iwctl_giwencodeext(dev, NULL, &(wrq->u.encoding), NULL);
2021                break;
2022
2023        case SIOCSIWMLME:
2024                DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCSIWMLME \n");
2025                rc = iwctl_siwmlme(dev, NULL, &(wrq->u.data), wrq->u.data.pointer);
2026                break;
2027
2028#endif // #ifdef WPA_SUPPLICANT_DRIVER_WEXT_SUPPORT
2029//End Add -- //2008-0409-07, <Add> by Einsn Liu
2030
2031    case IOCTL_CMD_TEST:
2032
2033                if (!(pDevice->flags & DEVICE_FLAGS_OPENED)) {
2034                    rc = -EFAULT;
2035                    break;
2036                } else {
2037                    rc = 0;
2038                }
2039        pReq = (PSCmdRequest)rq;
2040
2041   //20080130-01,<Remark> by Mike Liu
2042      // if(pDevice->bLinkPass==TRUE)
2043          pReq->wResult = MAGIC_CODE;         //Linking status:0x3142
2044   //20080130-02,<Remark> by Mike Liu
2045      //  else
2046      //         pReq->wResult = MAGIC_CODE+1;    //disconnect status:0x3143
2047        break;
2048
2049    case IOCTL_CMD_SET:
2050                if (!(pDevice->flags & DEVICE_FLAGS_OPENED) &&
2051                       (((PSCmdRequest)rq)->wCmdCode !=WLAN_CMD_SET_WPA))
2052                {
2053                    rc = -EFAULT;
2054                    break;
2055                } else {
2056                    rc = 0;
2057                }
2058
2059            if (test_and_set_bit( 0, (void*)&(pMgmt->uCmdBusy))) {
2060                    return -EBUSY;
2061            }
2062        rc = private_ioctl(pDevice, rq);
2063        clear_bit( 0, (void*)&(pMgmt->uCmdBusy));
2064        break;
2065
2066    case IOCTL_CMD_HOSTAPD:
2067
2068                if (!(pDevice->flags & DEVICE_FLAGS_OPENED)) {
2069                    rc = -EFAULT;
2070                    break;
2071                } else {
2072                    rc = 0;
2073                }
2074
2075                rc = hostap_ioctl(pDevice, &wrq->u.data);
2076        break;
2077
2078    case IOCTL_CMD_WPA:
2079
2080                if (!(pDevice->flags & DEVICE_FLAGS_OPENED)) {
2081                    rc = -EFAULT;
2082                    break;
2083                } else {
2084                    rc = 0;
2085                }
2086
2087                rc = wpa_ioctl(pDevice, &wrq->u.data);
2088        break;
2089
2090        case SIOCETHTOOL:
2091        return ethtool_ioctl(dev, (void *) rq->ifr_data);
2092        // All other calls are currently unsupported
2093
2094        default:
2095                rc = -EOPNOTSUPP;
2096        DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Ioctl command not support..%x\n", cmd);
2097
2098
2099    }
2100
2101    if (pDevice->bCommit) {
2102       if (pMgmt->eConfigMode == WMAC_CONFIG_AP) {
2103           netif_stop_queue(pDevice->dev);
2104           spin_lock_irq(&pDevice->lock);
2105           bScheduleCommand((HANDLE)pDevice, WLAN_CMD_RUN_AP, NULL);
2106           spin_unlock_irq(&pDevice->lock);
2107       }
2108       else {
2109           DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Commit the settings\n");
2110           spin_lock_irq(&pDevice->lock);
2111//2007-1121-01<Modify>by EinsnLiu
2112            if (pDevice->bLinkPass&&
2113                  memcmp(pMgmt->abyCurrSSID,pMgmt->abyDesireSSID,WLAN_IEHDR_LEN + WLAN_SSID_MAXLEN)) {
2114                  bScheduleCommand((HANDLE)pDevice, WLAN_CMD_DISASSOCIATE, NULL);
2115             } else {
2116           pDevice->bLinkPass = FALSE;
2117           pMgmt->eCurrState = WMAC_STATE_IDLE;
2118           memset(pMgmt->abyCurrBSSID, 0, 6);
2119                 }
2120           ControlvMaskByte(pDevice,MESSAGE_REQUEST_MACREG,MAC_REG_PAPEDELAY,LEDSTS_STS,LEDSTS_SLOW);
2121//End Modify
2122           netif_stop_queue(pDevice->dev);
2123#ifdef  WPA_SUPPLICANT_DRIVER_WEXT_SUPPORT
2124           pMgmt->eScanType = WMAC_SCAN_ACTIVE;
2125           if(pDevice->bWPASuppWextEnabled !=TRUE)
2126#endif
2127           bScheduleCommand((HANDLE) pDevice, WLAN_CMD_BSSID_SCAN, pMgmt->abyDesireSSID);
2128           bScheduleCommand((HANDLE) pDevice, WLAN_CMD_SSID, NULL);
2129           spin_unlock_irq(&pDevice->lock);
2130      }
2131      pDevice->bCommit = FALSE;
2132    }
2133
2134
2135    return rc;
2136}
2137
2138
2139static int ethtool_ioctl(struct net_device *dev, void *useraddr)
2140{
2141        u32 ethcmd;
2142
2143        if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
2144                return -EFAULT;
2145
2146        switch (ethcmd) {
2147        case ETHTOOL_GDRVINFO: {
2148                struct ethtool_drvinfo info = {ETHTOOL_GDRVINFO};
2149                strncpy(info.driver, DEVICE_NAME, sizeof(info.driver)-1);
2150                strncpy(info.version, DEVICE_VERSION, sizeof(info.version)-1);
2151                if (copy_to_user(useraddr, &info, sizeof(info)))
2152                        return -EFAULT;
2153                return 0;
2154        }
2155
2156        }
2157
2158        return -EOPNOTSUPP;
2159}
2160
2161
2162/*------------------------------------------------------------------*/
2163
2164
2165MODULE_DEVICE_TABLE(usb, vntwusb_table);
2166
2167
2168static struct usb_driver vntwusb_driver = {
2169            .name =             DEVICE_NAME,
2170            .probe =    vntwusb_found1,
2171            .disconnect =       vntwusb_disconnect,
2172            .id_table = vntwusb_table,
2173
2174//2008-0920-01<Add>by MikeLiu
2175//for supporting S3 & S4 function
2176#ifdef CONFIG_PM
2177           .suspend = vntwusb_suspend,
2178           .resume = vntwusb_resume,
2179#endif
2180};
2181
2182static int __init vntwusb_init_module(void)
2183{
2184    printk(KERN_NOTICE DEVICE_FULL_DRV_NAM " " DEVICE_VERSION);
2185    return usb_register(&vntwusb_driver);
2186}
2187
2188static void __exit vntwusb_cleanup_module(void)
2189{
2190        usb_deregister(&vntwusb_driver);
2191}
2192
2193module_init(vntwusb_init_module);
2194module_exit(vntwusb_cleanup_module);
2195
2196