linux/drivers/staging/vt6655/device_main.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: device_main.c
  20 *
  21 * Purpose: driver entry for initial, open, close, tx and rx.
  22 *
  23 * Author: Lyndon Chen
  24 *
  25 * Date: Jan 8, 2003
  26 *
  27 * Functions:
  28 *
  29 *   vt6655_probe - module initial (insmod) driver entry
  30 *   vt6655_remove - module remove entry
  31 *   vt6655_init_info - device structure resource allocation function
  32 *   device_free_info - device structure resource free function
  33 *   device_get_pci_info - get allocated pci io/mem resource
  34 *   device_print_info - print out resource
  35 *   device_open - allocate dma/descripter resource & initial mac/bbp function
  36 *   device_xmit - asynchrous data tx function
  37 *   device_intr - interrupt handle function
  38 *   device_set_multi - set mac filter
  39 *   device_ioctl - ioctl entry
  40 *   device_close - shutdown mac/bbp & free dma/descripter resource
  41 *   device_rx_srv - rx service function
  42 *   device_receive_frame - rx data function
  43 *   device_alloc_rx_buf - rx buffer pre-allocated function
  44 *   device_alloc_frag_buf - rx fragement pre-allocated function
  45 *   device_free_tx_buf - free tx buffer function
  46 *   device_free_frag_buf- free de-fragement buffer
  47 *   device_dma0_tx_80211- tx 802.11 frame via dma0
  48 *   device_dma0_xmit- tx PS bufferred frame via dma0
  49 *   device_init_rd0_ring- initial rd dma0 ring
  50 *   device_init_rd1_ring- initial rd dma1 ring
  51 *   device_init_td0_ring- initial tx dma0 ring buffer
  52 *   device_init_td1_ring- initial tx dma1 ring buffer
  53 *   device_init_registers- initial MAC & BBP & RF internal registers.
  54 *   device_init_rings- initial tx/rx ring buffer
  55 *   device_init_defrag_cb- initial & allocate de-fragement buffer.
  56 *   device_free_rings- free all allocated ring buffer
  57 *   device_tx_srv- tx interrupt service function
  58 *
  59 * Revision History:
  60 */
  61#undef __NO_VERSION__
  62
  63#include "device.h"
  64#include "card.h"
  65#include "channel.h"
  66#include "baseband.h"
  67#include "mac.h"
  68#include "tether.h"
  69#include "wmgr.h"
  70#include "wctl.h"
  71#include "power.h"
  72#include "wcmd.h"
  73#include "iocmd.h"
  74#include "tcrc.h"
  75#include "rxtx.h"
  76#include "wroute.h"
  77#include "bssdb.h"
  78#include "hostap.h"
  79#include "wpactl.h"
  80#include "ioctl.h"
  81#include "iwctl.h"
  82#include "dpc.h"
  83#include "datarate.h"
  84#include "rf.h"
  85#include "iowpa.h"
  86#include <linux/delay.h>
  87#include <linux/kthread.h>
  88#include <linux/slab.h>
  89
  90/*---------------------  Static Definitions -------------------------*/
  91//static int          msglevel                =MSG_LEVEL_DEBUG;
  92static int          msglevel                =   MSG_LEVEL_INFO;
  93
  94//
  95// Define module options
  96//
  97MODULE_AUTHOR("VIA Networking Technologies, Inc., <lyndonchen@vntek.com.tw>");
  98MODULE_LICENSE("GPL");
  99MODULE_DESCRIPTION("VIA Networking Solomon-A/B/G Wireless LAN Adapter Driver");
 100
 101        static int mlme_kill;
 102        //static  struct task_struct * mlme_task;
 103
 104#define DEVICE_PARAM(N,D)
 105/*
 106        static const int N[MAX_UINTS]=OPTION_DEFAULT;\
 107        MODULE_PARM(N, "1-" __MODULE_STRING(MAX_UINTS) "i");\
 108        MODULE_PARM_DESC(N, D);
 109*/
 110
 111#define RX_DESC_MIN0     16
 112#define RX_DESC_MAX0     128
 113#define RX_DESC_DEF0     32
 114DEVICE_PARAM(RxDescriptors0,"Number of receive descriptors0");
 115
 116#define RX_DESC_MIN1     16
 117#define RX_DESC_MAX1     128
 118#define RX_DESC_DEF1     32
 119DEVICE_PARAM(RxDescriptors1,"Number of receive descriptors1");
 120
 121#define TX_DESC_MIN0     16
 122#define TX_DESC_MAX0     128
 123#define TX_DESC_DEF0     32
 124DEVICE_PARAM(TxDescriptors0,"Number of transmit descriptors0");
 125
 126#define TX_DESC_MIN1     16
 127#define TX_DESC_MAX1     128
 128#define TX_DESC_DEF1     64
 129DEVICE_PARAM(TxDescriptors1,"Number of transmit descriptors1");
 130
 131
 132#define IP_ALIG_DEF     0
 133/* IP_byte_align[] is used for IP header unsigned long byte aligned
 134   0: indicate the IP header won't be unsigned long byte aligned.(Default) .
 135   1: indicate the IP header will be unsigned long byte aligned.
 136      In some environment, the IP header should be unsigned long byte aligned,
 137      or the packet will be droped when we receive it. (eg: IPVS)
 138*/
 139DEVICE_PARAM(IP_byte_align,"Enable IP header dword aligned");
 140
 141
 142#define INT_WORKS_DEF   20
 143#define INT_WORKS_MIN   10
 144#define INT_WORKS_MAX   64
 145
 146DEVICE_PARAM(int_works,"Number of packets per interrupt services");
 147
 148#define CHANNEL_MIN     1
 149#define CHANNEL_MAX     14
 150#define CHANNEL_DEF     6
 151
 152DEVICE_PARAM(Channel, "Channel number");
 153
 154
 155/* PreambleType[] is the preamble length used for transmit.
 156   0: indicate allows long preamble type
 157   1: indicate allows short preamble type
 158*/
 159
 160#define PREAMBLE_TYPE_DEF     1
 161
 162DEVICE_PARAM(PreambleType, "Preamble Type");
 163
 164
 165#define RTS_THRESH_MIN     512
 166#define RTS_THRESH_MAX     2347
 167#define RTS_THRESH_DEF     2347
 168
 169DEVICE_PARAM(RTSThreshold, "RTS threshold");
 170
 171
 172#define FRAG_THRESH_MIN     256
 173#define FRAG_THRESH_MAX     2346
 174#define FRAG_THRESH_DEF     2346
 175
 176DEVICE_PARAM(FragThreshold, "Fragmentation threshold");
 177
 178
 179#define DATA_RATE_MIN     0
 180#define DATA_RATE_MAX     13
 181#define DATA_RATE_DEF     13
 182/* datarate[] index
 183   0: indicate 1 Mbps   0x02
 184   1: indicate 2 Mbps   0x04
 185   2: indicate 5.5 Mbps 0x0B
 186   3: indicate 11 Mbps  0x16
 187   4: indicate 6 Mbps   0x0c
 188   5: indicate 9 Mbps   0x12
 189   6: indicate 12 Mbps  0x18
 190   7: indicate 18 Mbps  0x24
 191   8: indicate 24 Mbps  0x30
 192   9: indicate 36 Mbps  0x48
 193  10: indicate 48 Mbps  0x60
 194  11: indicate 54 Mbps  0x6c
 195  12: indicate 72 Mbps  0x90
 196  13: indicate auto rate
 197*/
 198
 199DEVICE_PARAM(ConnectionRate, "Connection data rate");
 200
 201#define OP_MODE_DEF     0
 202
 203DEVICE_PARAM(OPMode, "Infrastruct, adhoc, AP mode ");
 204
 205/* OpMode[] is used for transmit.
 206   0: indicate infrastruct mode used
 207   1: indicate adhoc mode used
 208   2: indicate AP mode used
 209*/
 210
 211
 212/* PSMode[]
 213   0: indicate disable power saving mode
 214   1: indicate enable power saving mode
 215*/
 216
 217#define PS_MODE_DEF     0
 218
 219DEVICE_PARAM(PSMode, "Power saving mode");
 220
 221
 222#define SHORT_RETRY_MIN     0
 223#define SHORT_RETRY_MAX     31
 224#define SHORT_RETRY_DEF     8
 225
 226
 227DEVICE_PARAM(ShortRetryLimit, "Short frame retry limits");
 228
 229#define LONG_RETRY_MIN     0
 230#define LONG_RETRY_MAX     15
 231#define LONG_RETRY_DEF     4
 232
 233
 234DEVICE_PARAM(LongRetryLimit, "long frame retry limits");
 235
 236
 237/* BasebandType[] baseband type selected
 238   0: indicate 802.11a type
 239   1: indicate 802.11b type
 240   2: indicate 802.11g type
 241*/
 242#define BBP_TYPE_MIN     0
 243#define BBP_TYPE_MAX     2
 244#define BBP_TYPE_DEF     2
 245
 246DEVICE_PARAM(BasebandType, "baseband type");
 247
 248
 249
 250/* 80211hEnable[]
 251   0: indicate disable 802.11h
 252   1: indicate enable 802.11h
 253*/
 254
 255#define X80211h_MODE_DEF     0
 256
 257DEVICE_PARAM(b80211hEnable, "802.11h mode");
 258
 259/* 80211hEnable[]
 260   0: indicate disable 802.11h
 261   1: indicate enable 802.11h
 262*/
 263
 264#define DIVERSITY_ANT_DEF     0
 265
 266DEVICE_PARAM(bDiversityANTEnable, "ANT diversity mode");
 267
 268
 269//
 270// Static vars definitions
 271//
 272
 273
 274static int          device_nics             =0;
 275static PSDevice     pDevice_Infos           =NULL;
 276static struct net_device *root_device_dev = NULL;
 277
 278static CHIP_INFO chip_info_table[]= {
 279    { VT3253,       "VIA Networking Solomon-A/B/G Wireless LAN Adapter ",
 280        256, 1,     DEVICE_FLAGS_IP_ALIGN|DEVICE_FLAGS_TX_ALIGN },
 281    {0,NULL}
 282};
 283
 284DEFINE_PCI_DEVICE_TABLE(vt6655_pci_id_table) = {
 285        { PCI_VDEVICE(VIA, 0x3253), (kernel_ulong_t)chip_info_table},
 286        { 0, }
 287};
 288
 289/*---------------------  Static Functions  --------------------------*/
 290
 291
 292static int  vt6655_probe(struct pci_dev *pcid, const struct pci_device_id *ent);
 293static void vt6655_init_info(struct pci_dev* pcid, PSDevice* ppDevice, PCHIP_INFO);
 294static void device_free_info(PSDevice pDevice);
 295static bool device_get_pci_info(PSDevice, struct pci_dev* pcid);
 296static void device_print_info(PSDevice pDevice);
 297static struct net_device_stats *device_get_stats(struct net_device *dev);
 298static void device_init_diversity_timer(PSDevice pDevice);
 299static int  device_open(struct net_device *dev);
 300static int  device_xmit(struct sk_buff *skb, struct net_device *dev);
 301static  irqreturn_t  device_intr(int irq,  void*dev_instance);
 302static void device_set_multi(struct net_device *dev);
 303static int  device_close(struct net_device *dev);
 304static int  device_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
 305
 306#ifdef CONFIG_PM
 307static int device_notify_reboot(struct notifier_block *, unsigned long event, void *ptr);
 308static int viawget_suspend(struct pci_dev *pcid, pm_message_t state);
 309static int viawget_resume(struct pci_dev *pcid);
 310struct notifier_block device_notifier = {
 311        .notifier_call = device_notify_reboot,
 312        .next = NULL,
 313        .priority = 0,
 314};
 315#endif
 316
 317
 318static void device_init_rd0_ring(PSDevice pDevice);
 319static void device_init_rd1_ring(PSDevice pDevice);
 320static void device_init_defrag_cb(PSDevice pDevice);
 321static void device_init_td0_ring(PSDevice pDevice);
 322static void device_init_td1_ring(PSDevice pDevice);
 323
 324static int  device_dma0_tx_80211(struct sk_buff *skb, struct net_device *dev);
 325//2008-0714<Add>by Mike Liu
 326static bool device_release_WPADEV(PSDevice pDevice);
 327
 328static int  ethtool_ioctl(struct net_device *dev, void *useraddr);
 329static int  device_rx_srv(PSDevice pDevice, unsigned int uIdx);
 330static int  device_tx_srv(PSDevice pDevice, unsigned int uIdx);
 331static bool device_alloc_rx_buf(PSDevice pDevice, PSRxDesc pDesc);
 332static void device_init_registers(PSDevice pDevice, DEVICE_INIT_TYPE InitType);
 333static void device_free_tx_buf(PSDevice pDevice, PSTxDesc pDesc);
 334static void device_free_td0_ring(PSDevice pDevice);
 335static void device_free_td1_ring(PSDevice pDevice);
 336static void device_free_rd0_ring(PSDevice pDevice);
 337static void device_free_rd1_ring(PSDevice pDevice);
 338static void device_free_rings(PSDevice pDevice);
 339static void device_free_frag_buf(PSDevice pDevice);
 340static int Config_FileGetParameter(unsigned char *string,
 341                unsigned char *dest, unsigned char *source);
 342
 343
 344/*---------------------  Export Variables  --------------------------*/
 345
 346/*---------------------  Export Functions  --------------------------*/
 347
 348
 349
 350static char* get_chip_name(int chip_id)
 351{
 352        int i;
 353        for (i = 0; chip_info_table[i].name != NULL; i++)
 354                if (chip_info_table[i].chip_id == chip_id)
 355                        break;
 356        return chip_info_table[i].name;
 357}
 358
 359static void vt6655_remove(struct pci_dev *pcid)
 360{
 361        PSDevice pDevice = pci_get_drvdata(pcid);
 362
 363        if (pDevice == NULL)
 364                return;
 365        device_free_info(pDevice);
 366
 367}
 368
 369/*
 370static void
 371device_set_int_opt(int *opt, int val, int min, int max, int def,char* name,char* devname) {
 372    if (val==-1)
 373        *opt=def;
 374    else if (val<min || val>max) {
 375        DBG_PRT(MSG_LEVEL_INFO, KERN_NOTICE "%s: the value of parameter %s is invalid, the valid range is (%d-%d)\n" ,
 376            devname,name, min,max);
 377        *opt=def;
 378    } else {
 379        DBG_PRT(MSG_LEVEL_INFO, KERN_INFO "%s: set value of parameter %s to %d\n",
 380            devname, name, val);
 381        *opt=val;
 382    }
 383}
 384
 385static void
 386device_set_bool_opt(unsigned int *opt, int val,bool def,u32 flag, char* name,char* devname) {
 387    (*opt)&=(~flag);
 388    if (val==-1)
 389        *opt|=(def ? flag : 0);
 390    else if (val<0 || val>1) {
 391        DBG_PRT(MSG_LEVEL_INFO, KERN_NOTICE
 392            "%s: the value of parameter %s is invalid, the valid range is (0-1)\n",devname,name);
 393        *opt|=(def ? flag : 0);
 394    } else {
 395        DBG_PRT(MSG_LEVEL_INFO, KERN_NOTICE "%s: set parameter %s to %s\n",
 396            devname,name , val ? "true" : "false");
 397        *opt|=(val ? flag : 0);
 398    }
 399}
 400*/
 401static void device_get_options(PSDevice pDevice, int index, char* devname)
 402{
 403        POPTIONS pOpts = &(pDevice->sOpts);
 404
 405        pOpts->nRxDescs0 = RX_DESC_DEF0;
 406        pOpts->nRxDescs1 = RX_DESC_DEF1;
 407        pOpts->nTxDescs[0] = TX_DESC_DEF0;
 408        pOpts->nTxDescs[1] = TX_DESC_DEF1;
 409        pOpts->flags |= DEVICE_FLAGS_IP_ALIGN;
 410        pOpts->int_works = INT_WORKS_DEF;
 411        pOpts->rts_thresh = RTS_THRESH_DEF;
 412        pOpts->frag_thresh = FRAG_THRESH_DEF;
 413        pOpts->data_rate = DATA_RATE_DEF;
 414        pOpts->channel_num = CHANNEL_DEF;
 415
 416        pOpts->flags |= DEVICE_FLAGS_PREAMBLE_TYPE;
 417        pOpts->flags |= DEVICE_FLAGS_OP_MODE;
 418        //pOpts->flags|=DEVICE_FLAGS_PS_MODE;
 419        pOpts->short_retry = SHORT_RETRY_DEF;
 420        pOpts->long_retry = LONG_RETRY_DEF;
 421        pOpts->bbp_type = BBP_TYPE_DEF;
 422        pOpts->flags |= DEVICE_FLAGS_80211h_MODE;
 423        pOpts->flags |= DEVICE_FLAGS_DiversityANT;
 424}
 425
 426static void
 427device_set_options(PSDevice pDevice) {
 428
 429    unsigned char abyBroadcastAddr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
 430    unsigned char abySNAP_RFC1042[ETH_ALEN] = {0xAA, 0xAA, 0x03, 0x00, 0x00, 0x00};
 431    unsigned char abySNAP_Bridgetunnel[ETH_ALEN] = {0xAA, 0xAA, 0x03, 0x00, 0x00, 0xF8};
 432
 433
 434    memcpy(pDevice->abyBroadcastAddr, abyBroadcastAddr, ETH_ALEN);
 435    memcpy(pDevice->abySNAP_RFC1042, abySNAP_RFC1042, ETH_ALEN);
 436    memcpy(pDevice->abySNAP_Bridgetunnel, abySNAP_Bridgetunnel, ETH_ALEN);
 437
 438    pDevice->uChannel = pDevice->sOpts.channel_num;
 439    pDevice->wRTSThreshold = pDevice->sOpts.rts_thresh;
 440    pDevice->wFragmentationThreshold = pDevice->sOpts.frag_thresh;
 441    pDevice->byShortRetryLimit = pDevice->sOpts.short_retry;
 442    pDevice->byLongRetryLimit = pDevice->sOpts.long_retry;
 443    pDevice->wMaxTransmitMSDULifetime = DEFAULT_MSDU_LIFETIME;
 444    pDevice->byShortPreamble = (pDevice->sOpts.flags & DEVICE_FLAGS_PREAMBLE_TYPE) ? 1 : 0;
 445    pDevice->byOpMode = (pDevice->sOpts.flags & DEVICE_FLAGS_OP_MODE) ? 1 : 0;
 446    pDevice->ePSMode = (pDevice->sOpts.flags & DEVICE_FLAGS_PS_MODE) ? 1 : 0;
 447    pDevice->b11hEnable = (pDevice->sOpts.flags & DEVICE_FLAGS_80211h_MODE) ? 1 : 0;
 448    pDevice->bDiversityRegCtlON = (pDevice->sOpts.flags & DEVICE_FLAGS_DiversityANT) ? 1 : 0;
 449    pDevice->uConnectionRate = pDevice->sOpts.data_rate;
 450    if (pDevice->uConnectionRate < RATE_AUTO) pDevice->bFixRate = true;
 451    pDevice->byBBType = pDevice->sOpts.bbp_type;
 452    pDevice->byPacketType = pDevice->byBBType;
 453
 454//PLICE_DEBUG->
 455        pDevice->byAutoFBCtrl = AUTO_FB_0;
 456        //pDevice->byAutoFBCtrl = AUTO_FB_1;
 457//PLICE_DEBUG<-
 458pDevice->bUpdateBBVGA = true;
 459    pDevice->byFOETuning = 0;
 460    pDevice->wCTSDuration = 0;
 461    pDevice->byPreambleType = 0;
 462
 463
 464    DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" uChannel= %d\n",(int)pDevice->uChannel);
 465    DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" byOpMode= %d\n",(int)pDevice->byOpMode);
 466    DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" ePSMode= %d\n",(int)pDevice->ePSMode);
 467    DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" wRTSThreshold= %d\n",(int)pDevice->wRTSThreshold);
 468    DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" byShortRetryLimit= %d\n",(int)pDevice->byShortRetryLimit);
 469    DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" byLongRetryLimit= %d\n",(int)pDevice->byLongRetryLimit);
 470    DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" byPreambleType= %d\n",(int)pDevice->byPreambleType);
 471    DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" byShortPreamble= %d\n",(int)pDevice->byShortPreamble);
 472    DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" uConnectionRate= %d\n",(int)pDevice->uConnectionRate);
 473    DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" byBBType= %d\n",(int)pDevice->byBBType);
 474    DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" pDevice->b11hEnable= %d\n",(int)pDevice->b11hEnable);
 475    DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" pDevice->bDiversityRegCtlON= %d\n",(int)pDevice->bDiversityRegCtlON);
 476}
 477
 478static void s_vCompleteCurrentMeasure (PSDevice pDevice, unsigned char byResult)
 479{
 480    unsigned int ii;
 481    unsigned long dwDuration = 0;
 482    unsigned char byRPI0 = 0;
 483
 484    for(ii=1;ii<8;ii++) {
 485        pDevice->dwRPIs[ii] *= 255;
 486        dwDuration |= *((unsigned short *) (pDevice->pCurrMeasureEID->sReq.abyDuration));
 487        dwDuration <<= 10;
 488        pDevice->dwRPIs[ii] /= dwDuration;
 489        pDevice->abyRPIs[ii] = (unsigned char) pDevice->dwRPIs[ii];
 490        byRPI0 += pDevice->abyRPIs[ii];
 491    }
 492    pDevice->abyRPIs[0] = (0xFF - byRPI0);
 493
 494     if (pDevice->uNumOfMeasureEIDs == 0) {
 495        VNTWIFIbMeasureReport(  pDevice->pMgmt,
 496                                true,
 497                                pDevice->pCurrMeasureEID,
 498                                byResult,
 499                                pDevice->byBasicMap,
 500                                pDevice->byCCAFraction,
 501                                pDevice->abyRPIs
 502                                );
 503    } else {
 504        VNTWIFIbMeasureReport(  pDevice->pMgmt,
 505                                false,
 506                                pDevice->pCurrMeasureEID,
 507                                byResult,
 508                                pDevice->byBasicMap,
 509                                pDevice->byCCAFraction,
 510                                pDevice->abyRPIs
 511                                );
 512        CARDbStartMeasure (pDevice, pDevice->pCurrMeasureEID++, pDevice->uNumOfMeasureEIDs);
 513    }
 514
 515}
 516
 517
 518
 519//
 520// Initialisation of MAC & BBP registers
 521//
 522
 523static void device_init_registers(PSDevice pDevice, DEVICE_INIT_TYPE InitType)
 524{
 525    unsigned int ii;
 526    unsigned char byValue;
 527    unsigned char byValue1;
 528    unsigned char byCCKPwrdBm = 0;
 529    unsigned char byOFDMPwrdBm = 0;
 530    int zonetype=0;
 531     PSMgmtObject    pMgmt = &(pDevice->sMgmtObj);
 532    MACbShutdown(pDevice->PortOffset);
 533    BBvSoftwareReset(pDevice->PortOffset);
 534
 535    if ((InitType == DEVICE_INIT_COLD) ||
 536        (InitType == DEVICE_INIT_DXPL)) {
 537        // Do MACbSoftwareReset in MACvInitialize
 538        MACbSoftwareReset(pDevice->PortOffset);
 539        // force CCK
 540        pDevice->bCCK = true;
 541        pDevice->bAES = false;
 542        pDevice->bProtectMode = false;      //Only used in 11g type, sync with ERP IE
 543        pDevice->bNonERPPresent = false;
 544        pDevice->bBarkerPreambleMd = false;
 545        pDevice->wCurrentRate = RATE_1M;
 546        pDevice->byTopOFDMBasicRate = RATE_24M;
 547        pDevice->byTopCCKBasicRate = RATE_1M;
 548
 549        pDevice->byRevId = 0;                   //Target to IF pin while programming to RF chip.
 550
 551        // init MAC
 552        MACvInitialize(pDevice->PortOffset);
 553
 554        // Get Local ID
 555        VNSvInPortB(pDevice->PortOffset + MAC_REG_LOCALID, &(pDevice->byLocalID));
 556
 557           spin_lock_irq(&pDevice->lock);
 558         SROMvReadAllContents(pDevice->PortOffset,pDevice->abyEEPROM);
 559
 560           spin_unlock_irq(&pDevice->lock);
 561
 562        // Get Channel range
 563
 564        pDevice->byMinChannel = 1;
 565        pDevice->byMaxChannel = CB_MAX_CHANNEL;
 566
 567        // Get Antena
 568        byValue = SROMbyReadEmbedded(pDevice->PortOffset, EEP_OFS_ANTENNA);
 569        if (byValue & EEP_ANTINV)
 570            pDevice->bTxRxAntInv = true;
 571        else
 572            pDevice->bTxRxAntInv = false;
 573#ifdef  PLICE_DEBUG
 574        //printk("init_register:TxRxAntInv is %d,byValue is %d\n",pDevice->bTxRxAntInv,byValue);
 575#endif
 576
 577        byValue &= (EEP_ANTENNA_AUX | EEP_ANTENNA_MAIN);
 578        if (byValue == 0) // if not set default is All
 579            byValue = (EEP_ANTENNA_AUX | EEP_ANTENNA_MAIN);
 580#ifdef  PLICE_DEBUG
 581        //printk("init_register:byValue is %d\n",byValue);
 582#endif
 583        pDevice->ulDiversityNValue = 100*260;//100*SROMbyReadEmbedded(pDevice->PortOffset, 0x51);
 584        pDevice->ulDiversityMValue = 100*16;//SROMbyReadEmbedded(pDevice->PortOffset, 0x52);
 585        pDevice->byTMax = 1;//SROMbyReadEmbedded(pDevice->PortOffset, 0x53);
 586        pDevice->byTMax2 = 4;//SROMbyReadEmbedded(pDevice->PortOffset, 0x54);
 587        pDevice->ulSQ3TH = 0;//(unsigned long) SROMbyReadEmbedded(pDevice->PortOffset, 0x55);
 588        pDevice->byTMax3 = 64;//SROMbyReadEmbedded(pDevice->PortOffset, 0x56);
 589
 590        if (byValue == (EEP_ANTENNA_AUX | EEP_ANTENNA_MAIN)) {
 591            pDevice->byAntennaCount = 2;
 592            pDevice->byTxAntennaMode = ANT_B;
 593            pDevice->dwTxAntennaSel = 1;
 594            pDevice->dwRxAntennaSel = 1;
 595            if (pDevice->bTxRxAntInv == true)
 596                pDevice->byRxAntennaMode = ANT_A;
 597            else
 598                pDevice->byRxAntennaMode = ANT_B;
 599                // chester for antenna
 600byValue1 = SROMbyReadEmbedded(pDevice->PortOffset, EEP_OFS_ANTENNA);
 601          //  if (pDevice->bDiversityRegCtlON)
 602          if((byValue1&0x08)==0)
 603                pDevice->bDiversityEnable = false;//SROMbyReadEmbedded(pDevice->PortOffset, 0x50);
 604            else
 605                pDevice->bDiversityEnable = true;
 606#ifdef  PLICE_DEBUG
 607                //printk("aux |main antenna: RxAntennaMode is %d\n",pDevice->byRxAntennaMode);
 608#endif
 609        } else  {
 610            pDevice->bDiversityEnable = false;
 611            pDevice->byAntennaCount = 1;
 612            pDevice->dwTxAntennaSel = 0;
 613            pDevice->dwRxAntennaSel = 0;
 614            if (byValue & EEP_ANTENNA_AUX) {
 615                pDevice->byTxAntennaMode = ANT_A;
 616                if (pDevice->bTxRxAntInv == true)
 617                    pDevice->byRxAntennaMode = ANT_B;
 618                else
 619                    pDevice->byRxAntennaMode = ANT_A;
 620            } else {
 621                pDevice->byTxAntennaMode = ANT_B;
 622                if (pDevice->bTxRxAntInv == true)
 623                    pDevice->byRxAntennaMode = ANT_A;
 624                else
 625                    pDevice->byRxAntennaMode = ANT_B;
 626            }
 627        }
 628#ifdef  PLICE_DEBUG
 629        //printk("init registers: TxAntennaMode is %d\n",pDevice->byTxAntennaMode);
 630#endif
 631        DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "bDiversityEnable=[%d],NValue=[%d],MValue=[%d],TMax=[%d],TMax2=[%d]\n",
 632            pDevice->bDiversityEnable,(int)pDevice->ulDiversityNValue,(int)pDevice->ulDiversityMValue,pDevice->byTMax,pDevice->byTMax2);
 633
 634//#ifdef ZoneType_DefaultSetting
 635//2008-8-4 <add> by chester
 636//zonetype initial
 637 pDevice->byOriginalZonetype = pDevice->abyEEPROM[EEP_OFS_ZONETYPE];
 638 zonetype = Config_FileOperation(pDevice,false,NULL);
 639 if (zonetype >= 0) {         //read zonetype file ok!
 640  if ((zonetype == 0)&&
 641        (pDevice->abyEEPROM[EEP_OFS_ZONETYPE] !=0x00)){          //for USA
 642    pDevice->abyEEPROM[EEP_OFS_ZONETYPE] = 0;
 643    pDevice->abyEEPROM[EEP_OFS_MAXCHANNEL] = 0x0B;
 644    DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Init Zone Type :USA\n");
 645  }
 646 else if((zonetype == 1)&&
 647             (pDevice->abyEEPROM[EEP_OFS_ZONETYPE]!=0x01)){   //for Japan
 648    pDevice->abyEEPROM[EEP_OFS_ZONETYPE] = 0x01;
 649    pDevice->abyEEPROM[EEP_OFS_MAXCHANNEL] = 0x0D;
 650  }
 651 else if((zonetype == 2)&&
 652             (pDevice->abyEEPROM[EEP_OFS_ZONETYPE]!=0x02)){   //for Europe
 653    pDevice->abyEEPROM[EEP_OFS_ZONETYPE] = 0x02;
 654    pDevice->abyEEPROM[EEP_OFS_MAXCHANNEL] = 0x0D;
 655    DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Init Zone Type :Europe\n");
 656  }
 657
 658else
 659{
 660   if(zonetype!=pDevice->abyEEPROM[EEP_OFS_ZONETYPE])
 661      printk("zonetype in file[%02x] mismatch with in EEPROM[%02x]\n",zonetype,pDevice->abyEEPROM[EEP_OFS_ZONETYPE]);
 662   else
 663      printk("Read Zonetype file success,use default zonetype setting[%02x]\n",zonetype);
 664 }
 665        }
 666  else
 667    printk("Read Zonetype file fail,use default zonetype setting[%02x]\n",SROMbyReadEmbedded(pDevice->PortOffset, EEP_OFS_ZONETYPE));
 668
 669        // Get RFType
 670        pDevice->byRFType = SROMbyReadEmbedded(pDevice->PortOffset, EEP_OFS_RFTYPE);
 671
 672        if ((pDevice->byRFType & RF_EMU) != 0) {
 673            // force change RevID for VT3253 emu
 674            pDevice->byRevId = 0x80;
 675        }
 676
 677        pDevice->byRFType &= RF_MASK;
 678        DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "pDevice->byRFType = %x\n", pDevice->byRFType);
 679
 680        if (pDevice->bZoneRegExist == false) {
 681            pDevice->byZoneType = pDevice->abyEEPROM[EEP_OFS_ZONETYPE];
 682        }
 683        DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "pDevice->byZoneType = %x\n", pDevice->byZoneType);
 684
 685        //Init RF module
 686        RFbInit(pDevice);
 687
 688        //Get Desire Power Value
 689        pDevice->byCurPwr = 0xFF;
 690        pDevice->byCCKPwr = SROMbyReadEmbedded(pDevice->PortOffset, EEP_OFS_PWR_CCK);
 691        pDevice->byOFDMPwrG = SROMbyReadEmbedded(pDevice->PortOffset, EEP_OFS_PWR_OFDMG);
 692        //byCCKPwrdBm = SROMbyReadEmbedded(pDevice->PortOffset, EEP_OFS_CCK_PWR_dBm);
 693
 694        //byOFDMPwrdBm = SROMbyReadEmbedded(pDevice->PortOffset, EEP_OFS_OFDM_PWR_dBm);
 695//printk("CCKPwrdBm is 0x%x,byOFDMPwrdBm is 0x%x\n",byCCKPwrdBm,byOFDMPwrdBm);
 696                // Load power Table
 697
 698
 699        for (ii=0;ii<CB_MAX_CHANNEL_24G;ii++) {
 700            pDevice->abyCCKPwrTbl[ii+1] = SROMbyReadEmbedded(pDevice->PortOffset, (unsigned char)(ii + EEP_OFS_CCK_PWR_TBL));
 701            if (pDevice->abyCCKPwrTbl[ii+1] == 0) {
 702                pDevice->abyCCKPwrTbl[ii+1] = pDevice->byCCKPwr;
 703            }
 704            pDevice->abyOFDMPwrTbl[ii+1] = SROMbyReadEmbedded(pDevice->PortOffset, (unsigned char)(ii + EEP_OFS_OFDM_PWR_TBL));
 705            if (pDevice->abyOFDMPwrTbl[ii+1] == 0) {
 706                pDevice->abyOFDMPwrTbl[ii+1] = pDevice->byOFDMPwrG;
 707            }
 708            pDevice->abyCCKDefaultPwr[ii+1] = byCCKPwrdBm;
 709            pDevice->abyOFDMDefaultPwr[ii+1] = byOFDMPwrdBm;
 710        }
 711                //2008-8-4 <add> by chester
 712          //recover 12,13 ,14channel for EUROPE by 11 channel
 713          if(((pDevice->abyEEPROM[EEP_OFS_ZONETYPE] == ZoneType_Japan) ||
 714                (pDevice->abyEEPROM[EEP_OFS_ZONETYPE] == ZoneType_Europe))&&
 715             (pDevice->byOriginalZonetype == ZoneType_USA)) {
 716            for(ii=11;ii<14;ii++) {
 717                pDevice->abyCCKPwrTbl[ii] = pDevice->abyCCKPwrTbl[10];
 718               pDevice->abyOFDMPwrTbl[ii] = pDevice->abyOFDMPwrTbl[10];
 719
 720            }
 721          }
 722
 723
 724        // Load OFDM A Power Table
 725        for (ii=0;ii<CB_MAX_CHANNEL_5G;ii++) { //RobertYu:20041224, bug using CB_MAX_CHANNEL
 726            pDevice->abyOFDMPwrTbl[ii+CB_MAX_CHANNEL_24G+1] = SROMbyReadEmbedded(pDevice->PortOffset, (unsigned char)(ii + EEP_OFS_OFDMA_PWR_TBL));
 727            pDevice->abyOFDMDefaultPwr[ii+CB_MAX_CHANNEL_24G+1] = SROMbyReadEmbedded(pDevice->PortOffset, (unsigned char)(ii + EEP_OFS_OFDMA_PWR_dBm));
 728        }
 729        init_channel_table((void *)pDevice);
 730
 731
 732        if (pDevice->byLocalID > REV_ID_VT3253_B1) {
 733            MACvSelectPage1(pDevice->PortOffset);
 734            VNSvOutPortB(pDevice->PortOffset + MAC_REG_MSRCTL + 1, (MSRCTL1_TXPWR | MSRCTL1_CSAPAREN));
 735            MACvSelectPage0(pDevice->PortOffset);
 736        }
 737
 738
 739         // use relative tx timeout and 802.11i D4
 740        MACvWordRegBitsOn(pDevice->PortOffset, MAC_REG_CFG, (CFG_TKIPOPT | CFG_NOTXTIMEOUT));
 741
 742        // set performance parameter by registry
 743        MACvSetShortRetryLimit(pDevice->PortOffset, pDevice->byShortRetryLimit);
 744        MACvSetLongRetryLimit(pDevice->PortOffset, pDevice->byLongRetryLimit);
 745
 746        // reset TSF counter
 747        VNSvOutPortB(pDevice->PortOffset + MAC_REG_TFTCTL, TFTCTL_TSFCNTRST);
 748        // enable TSF counter
 749        VNSvOutPortB(pDevice->PortOffset + MAC_REG_TFTCTL, TFTCTL_TSFCNTREN);
 750
 751        // initialize BBP registers
 752        BBbVT3253Init(pDevice);
 753
 754        if (pDevice->bUpdateBBVGA) {
 755            pDevice->byBBVGACurrent = pDevice->abyBBVGA[0];
 756            pDevice->byBBVGANew = pDevice->byBBVGACurrent;
 757            BBvSetVGAGainOffset(pDevice, pDevice->abyBBVGA[0]);
 758        }
 759#ifdef  PLICE_DEBUG
 760        //printk("init registers:RxAntennaMode is %x,TxAntennaMode is %x\n",pDevice->byRxAntennaMode,pDevice->byTxAntennaMode);
 761#endif
 762        BBvSetRxAntennaMode(pDevice->PortOffset, pDevice->byRxAntennaMode);
 763        BBvSetTxAntennaMode(pDevice->PortOffset, pDevice->byTxAntennaMode);
 764
 765        pDevice->byCurrentCh = 0;
 766
 767        //pDevice->NetworkType = Ndis802_11Automode;
 768        // Set BB and packet type at the same time.
 769        // Set Short Slot Time, xIFS, and RSPINF.
 770        if (pDevice->uConnectionRate == RATE_AUTO) {
 771            pDevice->wCurrentRate = RATE_54M;
 772        } else {
 773            pDevice->wCurrentRate = (unsigned short)pDevice->uConnectionRate;
 774        }
 775
 776        // default G Mode
 777        VNTWIFIbConfigPhyMode(pDevice->pMgmt, PHY_TYPE_11G);
 778        VNTWIFIbConfigPhyMode(pDevice->pMgmt, PHY_TYPE_AUTO);
 779
 780        pDevice->bRadioOff = false;
 781
 782        pDevice->byRadioCtl = SROMbyReadEmbedded(pDevice->PortOffset, EEP_OFS_RADIOCTL);
 783        pDevice->bHWRadioOff = false;
 784
 785        if (pDevice->byRadioCtl & EEP_RADIOCTL_ENABLE) {
 786            // Get GPIO
 787            MACvGPIOIn(pDevice->PortOffset, &pDevice->byGPIO);
 788//2008-4-14 <add> by chester for led issue
 789 #ifdef FOR_LED_ON_NOTEBOOK
 790if (pDevice->byGPIO & GPIO0_DATA){pDevice->bHWRadioOff = true;}
 791if ( !(pDevice->byGPIO & GPIO0_DATA)){pDevice->bHWRadioOff = false;}
 792
 793            }
 794        if ( (pDevice->bRadioControlOff == true)) {
 795            CARDbRadioPowerOff(pDevice);
 796        }
 797else  CARDbRadioPowerOn(pDevice);
 798#else
 799            if (((pDevice->byGPIO & GPIO0_DATA) && !(pDevice->byRadioCtl & EEP_RADIOCTL_INV)) ||
 800                ( !(pDevice->byGPIO & GPIO0_DATA) && (pDevice->byRadioCtl & EEP_RADIOCTL_INV))) {
 801                pDevice->bHWRadioOff = true;
 802            }
 803        }
 804        if ((pDevice->bHWRadioOff == true) || (pDevice->bRadioControlOff == true)) {
 805            CARDbRadioPowerOff(pDevice);
 806        }
 807
 808#endif
 809    }
 810            pMgmt->eScanType = WMAC_SCAN_PASSIVE;
 811    // get Permanent network address
 812    SROMvReadEtherAddress(pDevice->PortOffset, pDevice->abyCurrentNetAddr);
 813        DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Network address = %pM\n",
 814                pDevice->abyCurrentNetAddr);
 815
 816    // reset Tx pointer
 817    CARDvSafeResetRx(pDevice);
 818    // reset Rx pointer
 819    CARDvSafeResetTx(pDevice);
 820
 821    if (pDevice->byLocalID <= REV_ID_VT3253_A1) {
 822        MACvRegBitsOn(pDevice->PortOffset, MAC_REG_RCR, RCR_WPAERR);
 823    }
 824
 825    pDevice->eEncryptionStatus = Ndis802_11EncryptionDisabled;
 826
 827    // Turn On Rx DMA
 828    MACvReceive0(pDevice->PortOffset);
 829    MACvReceive1(pDevice->PortOffset);
 830
 831    // start the adapter
 832    MACvStart(pDevice->PortOffset);
 833
 834    netif_stop_queue(pDevice->dev);
 835
 836
 837}
 838
 839
 840
 841static void device_init_diversity_timer(PSDevice pDevice) {
 842
 843    init_timer(&pDevice->TimerSQ3Tmax1);
 844    pDevice->TimerSQ3Tmax1.data = (unsigned long) pDevice;
 845    pDevice->TimerSQ3Tmax1.function = (TimerFunction)TimerSQ3CallBack;
 846    pDevice->TimerSQ3Tmax1.expires = RUN_AT(HZ);
 847
 848    init_timer(&pDevice->TimerSQ3Tmax2);
 849    pDevice->TimerSQ3Tmax2.data = (unsigned long) pDevice;
 850    pDevice->TimerSQ3Tmax2.function = (TimerFunction)TimerSQ3CallBack;
 851    pDevice->TimerSQ3Tmax2.expires = RUN_AT(HZ);
 852
 853    init_timer(&pDevice->TimerSQ3Tmax3);
 854    pDevice->TimerSQ3Tmax3.data = (unsigned long) pDevice;
 855    pDevice->TimerSQ3Tmax3.function = (TimerFunction)TimerState1CallBack;
 856    pDevice->TimerSQ3Tmax3.expires = RUN_AT(HZ);
 857
 858    return;
 859}
 860
 861
 862static bool device_release_WPADEV(PSDevice pDevice)
 863{
 864  viawget_wpa_header *wpahdr;
 865  int ii=0;
 866 // wait_queue_head_t   Set_wait;
 867  //send device close to wpa_supplicnat layer
 868    if (pDevice->bWPADEVUp==true) {
 869                 wpahdr = (viawget_wpa_header *)pDevice->skb->data;
 870                 wpahdr->type = VIAWGET_DEVICECLOSE_MSG;
 871                 wpahdr->resp_ie_len = 0;
 872                 wpahdr->req_ie_len = 0;
 873                 skb_put(pDevice->skb, sizeof(viawget_wpa_header));
 874                 pDevice->skb->dev = pDevice->wpadev;
 875                 skb_reset_mac_header(pDevice->skb);
 876                 pDevice->skb->pkt_type = PACKET_HOST;
 877                 pDevice->skb->protocol = htons(ETH_P_802_2);
 878                 memset(pDevice->skb->cb, 0, sizeof(pDevice->skb->cb));
 879                 netif_rx(pDevice->skb);
 880                 pDevice->skb = dev_alloc_skb((int)pDevice->rx_buf_sz);
 881
 882 //wait release WPADEV
 883              //    init_waitqueue_head(&Set_wait);
 884              //    wait_event_timeout(Set_wait, ((pDevice->wpadev==NULL)&&(pDevice->skb == NULL)),5*HZ);    //1s wait
 885              while((pDevice->bWPADEVUp==true)) {
 886                set_current_state(TASK_UNINTERRUPTIBLE);
 887                 schedule_timeout (HZ/20);          //wait 50ms
 888                 ii++;
 889                if(ii>20)
 890                  break;
 891              }
 892           }
 893    return true;
 894}
 895
 896static const struct net_device_ops device_netdev_ops = {
 897        .ndo_open               = device_open,
 898        .ndo_stop               = device_close,
 899        .ndo_do_ioctl           = device_ioctl,
 900        .ndo_get_stats          = device_get_stats,
 901        .ndo_start_xmit         = device_xmit,
 902        .ndo_set_rx_mode        = device_set_multi,
 903};
 904
 905static int
 906vt6655_probe(struct pci_dev *pcid, const struct pci_device_id *ent)
 907{
 908    static bool bFirst = true;
 909    struct net_device*  dev = NULL;
 910    PCHIP_INFO  pChip_info = (PCHIP_INFO)ent->driver_data;
 911    PSDevice    pDevice;
 912    int         rc;
 913    if (device_nics ++>= MAX_UINTS) {
 914        printk(KERN_NOTICE DEVICE_NAME ": already found %d NICs\n", device_nics);
 915        return -ENODEV;
 916    }
 917
 918
 919    dev = alloc_etherdev(sizeof(DEVICE_INFO));
 920
 921    pDevice = (PSDevice) netdev_priv(dev);
 922
 923    if (dev == NULL) {
 924        printk(KERN_ERR DEVICE_NAME ": allocate net device failed \n");
 925        return -ENOMEM;
 926    }
 927
 928    // Chain it all together
 929   // SET_MODULE_OWNER(dev);
 930    SET_NETDEV_DEV(dev, &pcid->dev);
 931
 932    if (bFirst) {
 933        printk(KERN_NOTICE "%s Ver. %s\n",DEVICE_FULL_DRV_NAM, DEVICE_VERSION);
 934        printk(KERN_NOTICE "Copyright (c) 2003 VIA Networking Technologies, Inc.\n");
 935        bFirst=false;
 936    }
 937
 938    vt6655_init_info(pcid, &pDevice, pChip_info);
 939    pDevice->dev = dev;
 940    pDevice->next_module = root_device_dev;
 941    root_device_dev = dev;
 942
 943    if (pci_enable_device(pcid)) {
 944        device_free_info(pDevice);
 945        return -ENODEV;
 946    }
 947    dev->irq = pcid->irq;
 948
 949#ifdef  DEBUG
 950        printk("Before get pci_info memaddr is %x\n",pDevice->memaddr);
 951#endif
 952    if (device_get_pci_info(pDevice,pcid) == false) {
 953        printk(KERN_ERR DEVICE_NAME ": Failed to find PCI device.\n");
 954        device_free_info(pDevice);
 955        return -ENODEV;
 956    }
 957
 958#if 1
 959
 960#ifdef  DEBUG
 961
 962        //pci_read_config_byte(pcid, PCI_BASE_ADDRESS_0, &pDevice->byRevId);
 963        printk("after get pci_info memaddr is %x, io addr is %x,io_size is %d\n",pDevice->memaddr,pDevice->ioaddr,pDevice->io_size);
 964        {
 965                int i;
 966                u32                     bar,len;
 967                u32 address[] = {
 968                PCI_BASE_ADDRESS_0,
 969                PCI_BASE_ADDRESS_1,
 970                PCI_BASE_ADDRESS_2,
 971                PCI_BASE_ADDRESS_3,
 972                PCI_BASE_ADDRESS_4,
 973                PCI_BASE_ADDRESS_5,
 974                0};
 975                for (i=0;address[i];i++)
 976                {
 977                        //pci_write_config_dword(pcid,address[i], 0xFFFFFFFF);
 978                        pci_read_config_dword(pcid, address[i], &bar);
 979                        printk("bar %d is %x\n",i,bar);
 980                        if (!bar)
 981                        {
 982                                printk("bar %d not implemented\n",i);
 983                                continue;
 984                        }
 985                        if (bar & PCI_BASE_ADDRESS_SPACE_IO) {
 986                        /* This is IO */
 987
 988                        len = bar & (PCI_BASE_ADDRESS_IO_MASK & 0xFFFF);
 989                        len = len & ~(len - 1);
 990
 991                        printk("IO space:  len in IO %x, BAR %d\n", len, i);
 992                        }
 993                        else
 994                        {
 995                                len = bar & 0xFFFFFFF0;
 996                                len = ~len + 1;
 997
 998                                printk("len in MEM %x, BAR %d\n", len, i);
 999                        }
1000                }
1001        }
1002#endif
1003
1004
1005#endif
1006
1007#ifdef  DEBUG
1008        //return  0  ;
1009#endif
1010    pDevice->PortOffset = (unsigned long)ioremap(pDevice->memaddr & PCI_BASE_ADDRESS_MEM_MASK, pDevice->io_size);
1011        //pDevice->PortOffset = (unsigned long)ioremap(pDevice->ioaddr & PCI_BASE_ADDRESS_IO_MASK, pDevice->io_size);
1012
1013        if(pDevice->PortOffset == 0) {
1014       printk(KERN_ERR DEVICE_NAME ": Failed to IO remapping ..\n");
1015       device_free_info(pDevice);
1016        return -ENODEV;
1017    }
1018
1019
1020
1021
1022    rc = pci_request_regions(pcid, DEVICE_NAME);
1023    if (rc) {
1024        printk(KERN_ERR DEVICE_NAME ": Failed to find PCI device\n");
1025        device_free_info(pDevice);
1026        return -ENODEV;
1027    }
1028
1029    dev->base_addr = pDevice->ioaddr;
1030#ifdef  PLICE_DEBUG
1031        unsigned char   value;
1032
1033        VNSvInPortB(pDevice->PortOffset+0x4F, &value);
1034        printk("Before write: value is %x\n",value);
1035        //VNSvInPortB(pDevice->PortOffset+0x3F, 0x00);
1036        VNSvOutPortB(pDevice->PortOffset,value);
1037        VNSvInPortB(pDevice->PortOffset+0x4F, &value);
1038        printk("After write: value is %x\n",value);
1039#endif
1040
1041
1042
1043#ifdef IO_MAP
1044    pDevice->PortOffset = pDevice->ioaddr;
1045#endif
1046    // do reset
1047    if (!MACbSoftwareReset(pDevice->PortOffset)) {
1048        printk(KERN_ERR DEVICE_NAME ": Failed to access MAC hardware..\n");
1049        device_free_info(pDevice);
1050        return -ENODEV;
1051    }
1052    // initial to reload eeprom
1053    MACvInitialize(pDevice->PortOffset);
1054    MACvReadEtherAddress(pDevice->PortOffset, dev->dev_addr);
1055
1056    device_get_options(pDevice, device_nics-1, dev->name);
1057    device_set_options(pDevice);
1058    //Mask out the options cannot be set to the chip
1059    pDevice->sOpts.flags &= pChip_info->flags;
1060
1061    //Enable the chip specified capabilities
1062    pDevice->flags = pDevice->sOpts.flags | (pChip_info->flags & 0xFF000000UL);
1063    pDevice->tx_80211 = device_dma0_tx_80211;
1064    pDevice->sMgmtObj.pAdapter = (void *)pDevice;
1065    pDevice->pMgmt = &(pDevice->sMgmtObj);
1066
1067    dev->irq                = pcid->irq;
1068    dev->netdev_ops         = &device_netdev_ops;
1069
1070        dev->wireless_handlers = (struct iw_handler_def *)&iwctl_handler_def;
1071
1072    rc = register_netdev(dev);
1073    if (rc)
1074    {
1075        printk(KERN_ERR DEVICE_NAME " Failed to register netdev\n");
1076        device_free_info(pDevice);
1077        return -ENODEV;
1078    }
1079    device_print_info(pDevice);
1080    pci_set_drvdata(pcid, pDevice);
1081    return 0;
1082
1083}
1084
1085static void device_print_info(PSDevice pDevice)
1086{
1087    struct net_device* dev=pDevice->dev;
1088
1089    DBG_PRT(MSG_LEVEL_INFO, KERN_INFO "%s: %s\n",dev->name, get_chip_name(pDevice->chip_id));
1090    DBG_PRT(MSG_LEVEL_INFO, KERN_INFO "%s: MAC=%pM", dev->name, dev->dev_addr);
1091#ifdef IO_MAP
1092    DBG_PRT(MSG_LEVEL_INFO, KERN_INFO" IO=0x%lx  ",(unsigned long) pDevice->ioaddr);
1093    DBG_PRT(MSG_LEVEL_INFO, KERN_INFO" IRQ=%d \n", pDevice->dev->irq);
1094#else
1095    DBG_PRT(MSG_LEVEL_INFO, KERN_INFO" IO=0x%lx Mem=0x%lx ",
1096                    (unsigned long) pDevice->ioaddr,(unsigned long) pDevice->PortOffset);
1097    DBG_PRT(MSG_LEVEL_INFO, KERN_INFO" IRQ=%d \n", pDevice->dev->irq);
1098#endif
1099
1100}
1101
1102static void vt6655_init_info(struct pci_dev* pcid, PSDevice* ppDevice,
1103    PCHIP_INFO pChip_info) {
1104
1105    PSDevice p;
1106
1107    memset(*ppDevice,0,sizeof(DEVICE_INFO));
1108
1109    if (pDevice_Infos == NULL) {
1110        pDevice_Infos =*ppDevice;
1111    }
1112    else {
1113        for (p=pDevice_Infos;p->next!=NULL;p=p->next)
1114            do {} while (0);
1115        p->next = *ppDevice;
1116        (*ppDevice)->prev = p;
1117    }
1118
1119    (*ppDevice)->pcid = pcid;
1120    (*ppDevice)->chip_id = pChip_info->chip_id;
1121    (*ppDevice)->io_size = pChip_info->io_size;
1122    (*ppDevice)->nTxQueues = pChip_info->nTxQueue;
1123    (*ppDevice)->multicast_limit =32;
1124
1125    spin_lock_init(&((*ppDevice)->lock));
1126}
1127
1128static bool device_get_pci_info(PSDevice pDevice, struct pci_dev* pcid) {
1129
1130    u16 pci_cmd;
1131    u8  b;
1132    unsigned int cis_addr;
1133#ifdef  PLICE_DEBUG
1134        unsigned char pci_config[256];
1135        unsigned char   value =0x00;
1136        int             ii,j;
1137        u16     max_lat=0x0000;
1138        memset(pci_config,0x00,256);
1139#endif
1140
1141    pci_read_config_byte(pcid, PCI_REVISION_ID, &pDevice->byRevId);
1142    pci_read_config_word(pcid, PCI_SUBSYSTEM_ID,&pDevice->SubSystemID);
1143    pci_read_config_word(pcid, PCI_SUBSYSTEM_VENDOR_ID, &pDevice->SubVendorID);
1144    pci_read_config_word(pcid, PCI_COMMAND, (u16 *) & (pci_cmd));
1145
1146    pci_set_master(pcid);
1147
1148    pDevice->memaddr = pci_resource_start(pcid,0);
1149    pDevice->ioaddr = pci_resource_start(pcid,1);
1150
1151#ifdef  DEBUG
1152//      pDevice->ioaddr = pci_resource_start(pcid, 0);
1153//      pDevice->memaddr = pci_resource_start(pcid,1);
1154#endif
1155
1156    cis_addr = pci_resource_start(pcid,2);
1157
1158    pDevice->pcid = pcid;
1159
1160    pci_read_config_byte(pcid, PCI_COMMAND, &b);
1161    pci_write_config_byte(pcid, PCI_COMMAND, (b|PCI_COMMAND_MASTER));
1162
1163#ifdef  PLICE_DEBUG
1164        //pci_read_config_word(pcid,PCI_MAX_LAT,&max_lat);
1165        //printk("max lat is %x,SubSystemID is %x\n",max_lat,pDevice->SubSystemID);
1166        //for (ii=0;ii<0xFF;ii++)
1167        //pci_read_config_word(pcid,PCI_MAX_LAT,&max_lat);
1168        //max_lat  = 0x20;
1169        //pci_write_config_word(pcid,PCI_MAX_LAT,max_lat);
1170        //pci_read_config_word(pcid,PCI_MAX_LAT,&max_lat);
1171        //printk("max lat is %x\n",max_lat);
1172
1173        for (ii=0;ii<0xFF;ii++)
1174        {
1175                pci_read_config_byte(pcid,ii,&value);
1176                pci_config[ii] = value;
1177        }
1178        for (ii=0,j=1;ii<0x100;ii++,j++)
1179        {
1180                if (j %16 == 0)
1181                {
1182                        printk("%x:",pci_config[ii]);
1183                        printk("\n");
1184                }
1185                else
1186                {
1187                        printk("%x:",pci_config[ii]);
1188                }
1189        }
1190#endif
1191    return true;
1192}
1193
1194static void device_free_info(PSDevice pDevice) {
1195    PSDevice         ptr;
1196    struct net_device*  dev=pDevice->dev;
1197
1198    ASSERT(pDevice);
1199//2008-0714-01<Add>by chester
1200device_release_WPADEV(pDevice);
1201
1202//2008-07-21-01<Add>by MikeLiu
1203//unregister wpadev
1204   if(wpa_set_wpadev(pDevice, 0)!=0)
1205     printk("unregister wpadev fail?\n");
1206
1207    if (pDevice_Infos==NULL)
1208        return;
1209
1210    for (ptr=pDevice_Infos;ptr && (ptr!=pDevice);ptr=ptr->next)
1211            do {} while (0);
1212
1213    if (ptr==pDevice) {
1214        if (ptr==pDevice_Infos)
1215            pDevice_Infos=ptr->next;
1216        else
1217            ptr->prev->next=ptr->next;
1218    }
1219    else {
1220        DBG_PRT(MSG_LEVEL_ERR, KERN_ERR "info struct not found\n");
1221        return;
1222    }
1223#ifdef HOSTAP
1224    if (dev)
1225        vt6655_hostap_set_hostapd(pDevice, 0, 0);
1226#endif
1227    if (dev)
1228        unregister_netdev(dev);
1229
1230    if (pDevice->PortOffset)
1231        iounmap((void *)pDevice->PortOffset);
1232
1233    if (pDevice->pcid)
1234        pci_release_regions(pDevice->pcid);
1235    if (dev)
1236        free_netdev(dev);
1237
1238    if (pDevice->pcid) {
1239        pci_set_drvdata(pDevice->pcid,NULL);
1240    }
1241}
1242
1243static bool device_init_rings(PSDevice pDevice) {
1244    void*   vir_pool;
1245
1246
1247    /*allocate all RD/TD rings a single pool*/
1248    vir_pool = pci_alloc_consistent(pDevice->pcid,
1249                    pDevice->sOpts.nRxDescs0 * sizeof(SRxDesc) +
1250                    pDevice->sOpts.nRxDescs1 * sizeof(SRxDesc) +
1251                    pDevice->sOpts.nTxDescs[0] * sizeof(STxDesc) +
1252                    pDevice->sOpts.nTxDescs[1] * sizeof(STxDesc),
1253                    &pDevice->pool_dma);
1254
1255    if (vir_pool == NULL) {
1256        DBG_PRT(MSG_LEVEL_ERR,KERN_ERR "%s : allocate desc dma memory failed\n", pDevice->dev->name);
1257        return false;
1258    }
1259
1260    memset(vir_pool, 0,
1261            pDevice->sOpts.nRxDescs0 * sizeof(SRxDesc) +
1262            pDevice->sOpts.nRxDescs1 * sizeof(SRxDesc) +
1263            pDevice->sOpts.nTxDescs[0] * sizeof(STxDesc) +
1264            pDevice->sOpts.nTxDescs[1] * sizeof(STxDesc)
1265          );
1266
1267    pDevice->aRD0Ring = vir_pool;
1268    pDevice->aRD1Ring = vir_pool +
1269                        pDevice->sOpts.nRxDescs0 * sizeof(SRxDesc);
1270
1271
1272    pDevice->rd0_pool_dma = pDevice->pool_dma;
1273    pDevice->rd1_pool_dma = pDevice->rd0_pool_dma +
1274                            pDevice->sOpts.nRxDescs0 * sizeof(SRxDesc);
1275
1276    pDevice->tx0_bufs = pci_alloc_consistent(pDevice->pcid,
1277                    pDevice->sOpts.nTxDescs[0] * PKT_BUF_SZ +
1278                    pDevice->sOpts.nTxDescs[1] * PKT_BUF_SZ +
1279                    CB_BEACON_BUF_SIZE +
1280                    CB_MAX_BUF_SIZE,
1281                    &pDevice->tx_bufs_dma0);
1282
1283    if (pDevice->tx0_bufs == NULL) {
1284        DBG_PRT(MSG_LEVEL_ERR,KERN_ERR "%s: allocate buf dma memory failed\n", pDevice->dev->name);
1285        pci_free_consistent(pDevice->pcid,
1286            pDevice->sOpts.nRxDescs0 * sizeof(SRxDesc) +
1287            pDevice->sOpts.nRxDescs1 * sizeof(SRxDesc) +
1288            pDevice->sOpts.nTxDescs[0] * sizeof(STxDesc) +
1289            pDevice->sOpts.nTxDescs[1] * sizeof(STxDesc),
1290            vir_pool, pDevice->pool_dma
1291            );
1292        return false;
1293    }
1294
1295    memset(pDevice->tx0_bufs, 0,
1296           pDevice->sOpts.nTxDescs[0] * PKT_BUF_SZ +
1297           pDevice->sOpts.nTxDescs[1] * PKT_BUF_SZ +
1298           CB_BEACON_BUF_SIZE +
1299           CB_MAX_BUF_SIZE
1300          );
1301
1302    pDevice->td0_pool_dma = pDevice->rd1_pool_dma +
1303            pDevice->sOpts.nRxDescs1 * sizeof(SRxDesc);
1304
1305    pDevice->td1_pool_dma = pDevice->td0_pool_dma +
1306            pDevice->sOpts.nTxDescs[0] * sizeof(STxDesc);
1307
1308
1309    // vir_pool: pvoid type
1310    pDevice->apTD0Rings = vir_pool
1311                          + pDevice->sOpts.nRxDescs0 * sizeof(SRxDesc)
1312                          + pDevice->sOpts.nRxDescs1 * sizeof(SRxDesc);
1313
1314    pDevice->apTD1Rings = vir_pool
1315            + pDevice->sOpts.nRxDescs0 * sizeof(SRxDesc)
1316            + pDevice->sOpts.nRxDescs1 * sizeof(SRxDesc)
1317            + pDevice->sOpts.nTxDescs[0] * sizeof(STxDesc);
1318
1319
1320    pDevice->tx1_bufs = pDevice->tx0_bufs +
1321            pDevice->sOpts.nTxDescs[0] * PKT_BUF_SZ;
1322
1323
1324    pDevice->tx_beacon_bufs = pDevice->tx1_bufs +
1325            pDevice->sOpts.nTxDescs[1] * PKT_BUF_SZ;
1326
1327    pDevice->pbyTmpBuff = pDevice->tx_beacon_bufs +
1328            CB_BEACON_BUF_SIZE;
1329
1330    pDevice->tx_bufs_dma1 = pDevice->tx_bufs_dma0 +
1331            pDevice->sOpts.nTxDescs[0] * PKT_BUF_SZ;
1332
1333
1334    pDevice->tx_beacon_dma = pDevice->tx_bufs_dma1 +
1335            pDevice->sOpts.nTxDescs[1] * PKT_BUF_SZ;
1336
1337
1338    return true;
1339}
1340
1341static void device_free_rings(PSDevice pDevice) {
1342
1343    pci_free_consistent(pDevice->pcid,
1344            pDevice->sOpts.nRxDescs0 * sizeof(SRxDesc) +
1345            pDevice->sOpts.nRxDescs1 * sizeof(SRxDesc) +
1346            pDevice->sOpts.nTxDescs[0] * sizeof(STxDesc) +
1347            pDevice->sOpts.nTxDescs[1] * sizeof(STxDesc)
1348            ,
1349            pDevice->aRD0Ring, pDevice->pool_dma
1350        );
1351
1352    if (pDevice->tx0_bufs)
1353        pci_free_consistent(pDevice->pcid,
1354           pDevice->sOpts.nTxDescs[0] * PKT_BUF_SZ +
1355           pDevice->sOpts.nTxDescs[1] * PKT_BUF_SZ +
1356           CB_BEACON_BUF_SIZE +
1357           CB_MAX_BUF_SIZE,
1358           pDevice->tx0_bufs, pDevice->tx_bufs_dma0
1359        );
1360}
1361
1362static void device_init_rd0_ring(PSDevice pDevice) {
1363    int i;
1364    dma_addr_t      curr = pDevice->rd0_pool_dma;
1365    PSRxDesc        pDesc;
1366
1367    /* Init the RD0 ring entries */
1368    for (i = 0; i < pDevice->sOpts.nRxDescs0; i ++, curr += sizeof(SRxDesc)) {
1369        pDesc = &(pDevice->aRD0Ring[i]);
1370        pDesc->pRDInfo = alloc_rd_info();
1371        ASSERT(pDesc->pRDInfo);
1372        if (!device_alloc_rx_buf(pDevice, pDesc)) {
1373            DBG_PRT(MSG_LEVEL_ERR,KERN_ERR "%s: can not alloc rx bufs\n",
1374            pDevice->dev->name);
1375        }
1376        pDesc->next = &(pDevice->aRD0Ring[(i+1) % pDevice->sOpts.nRxDescs0]);
1377        pDesc->pRDInfo->curr_desc = cpu_to_le32(curr);
1378        pDesc->next_desc = cpu_to_le32(curr + sizeof(SRxDesc));
1379    }
1380
1381    if (i > 0)
1382        pDevice->aRD0Ring[i-1].next_desc = cpu_to_le32(pDevice->rd0_pool_dma);
1383    pDevice->pCurrRD[0] = &(pDevice->aRD0Ring[0]);
1384}
1385
1386
1387static void device_init_rd1_ring(PSDevice pDevice) {
1388    int i;
1389    dma_addr_t      curr = pDevice->rd1_pool_dma;
1390    PSRxDesc        pDesc;
1391
1392    /* Init the RD1 ring entries */
1393    for (i = 0; i < pDevice->sOpts.nRxDescs1; i ++, curr += sizeof(SRxDesc)) {
1394        pDesc = &(pDevice->aRD1Ring[i]);
1395        pDesc->pRDInfo = alloc_rd_info();
1396        ASSERT(pDesc->pRDInfo);
1397        if (!device_alloc_rx_buf(pDevice, pDesc)) {
1398            DBG_PRT(MSG_LEVEL_ERR,KERN_ERR "%s: can not alloc rx bufs\n",
1399            pDevice->dev->name);
1400        }
1401        pDesc->next = &(pDevice->aRD1Ring[(i+1) % pDevice->sOpts.nRxDescs1]);
1402        pDesc->pRDInfo->curr_desc = cpu_to_le32(curr);
1403        pDesc->next_desc = cpu_to_le32(curr + sizeof(SRxDesc));
1404    }
1405
1406    if (i > 0)
1407        pDevice->aRD1Ring[i-1].next_desc = cpu_to_le32(pDevice->rd1_pool_dma);
1408    pDevice->pCurrRD[1] = &(pDevice->aRD1Ring[0]);
1409}
1410
1411
1412static void device_init_defrag_cb(PSDevice pDevice) {
1413    int i;
1414    PSDeFragControlBlock pDeF;
1415
1416    /* Init the fragment ctl entries */
1417    for (i = 0; i < CB_MAX_RX_FRAG; i++) {
1418        pDeF = &(pDevice->sRxDFCB[i]);
1419        if (!device_alloc_frag_buf(pDevice, pDeF)) {
1420            DBG_PRT(MSG_LEVEL_ERR,KERN_ERR "%s: can not alloc frag bufs\n",
1421                pDevice->dev->name);
1422        }
1423    }
1424    pDevice->cbDFCB = CB_MAX_RX_FRAG;
1425    pDevice->cbFreeDFCB = pDevice->cbDFCB;
1426}
1427
1428
1429
1430
1431static void device_free_rd0_ring(PSDevice pDevice) {
1432    int i;
1433
1434    for (i = 0; i < pDevice->sOpts.nRxDescs0; i++) {
1435        PSRxDesc        pDesc =&(pDevice->aRD0Ring[i]);
1436        PDEVICE_RD_INFO  pRDInfo =pDesc->pRDInfo;
1437
1438        pci_unmap_single(pDevice->pcid,pRDInfo->skb_dma,
1439           pDevice->rx_buf_sz, PCI_DMA_FROMDEVICE);
1440
1441        dev_kfree_skb(pRDInfo->skb);
1442
1443        kfree((void *)pDesc->pRDInfo);
1444    }
1445
1446}
1447
1448static void device_free_rd1_ring(PSDevice pDevice) {
1449    int i;
1450
1451
1452    for (i = 0; i < pDevice->sOpts.nRxDescs1; i++) {
1453        PSRxDesc        pDesc=&(pDevice->aRD1Ring[i]);
1454        PDEVICE_RD_INFO  pRDInfo=pDesc->pRDInfo;
1455
1456        pci_unmap_single(pDevice->pcid,pRDInfo->skb_dma,
1457           pDevice->rx_buf_sz, PCI_DMA_FROMDEVICE);
1458
1459        dev_kfree_skb(pRDInfo->skb);
1460
1461        kfree((void *)pDesc->pRDInfo);
1462    }
1463
1464}
1465
1466static void device_free_frag_buf(PSDevice pDevice) {
1467    PSDeFragControlBlock pDeF;
1468    int i;
1469
1470    for (i = 0; i < CB_MAX_RX_FRAG; i++) {
1471
1472        pDeF = &(pDevice->sRxDFCB[i]);
1473
1474        if (pDeF->skb)
1475            dev_kfree_skb(pDeF->skb);
1476
1477    }
1478
1479}
1480
1481static void device_init_td0_ring(PSDevice pDevice) {
1482    int i;
1483    dma_addr_t  curr;
1484    PSTxDesc        pDesc;
1485
1486    curr = pDevice->td0_pool_dma;
1487    for (i = 0; i < pDevice->sOpts.nTxDescs[0]; i++, curr += sizeof(STxDesc)) {
1488        pDesc = &(pDevice->apTD0Rings[i]);
1489        pDesc->pTDInfo = alloc_td_info();
1490        ASSERT(pDesc->pTDInfo);
1491        if (pDevice->flags & DEVICE_FLAGS_TX_ALIGN) {
1492            pDesc->pTDInfo->buf = pDevice->tx0_bufs + (i)*PKT_BUF_SZ;
1493            pDesc->pTDInfo->buf_dma = pDevice->tx_bufs_dma0 + (i)*PKT_BUF_SZ;
1494        }
1495        pDesc->next =&(pDevice->apTD0Rings[(i+1) % pDevice->sOpts.nTxDescs[0]]);
1496        pDesc->pTDInfo->curr_desc = cpu_to_le32(curr);
1497        pDesc->next_desc = cpu_to_le32(curr+sizeof(STxDesc));
1498    }
1499
1500    if (i > 0)
1501        pDevice->apTD0Rings[i-1].next_desc = cpu_to_le32(pDevice->td0_pool_dma);
1502    pDevice->apTailTD[0] = pDevice->apCurrTD[0] =&(pDevice->apTD0Rings[0]);
1503
1504}
1505
1506static void device_init_td1_ring(PSDevice pDevice) {
1507    int i;
1508    dma_addr_t  curr;
1509    PSTxDesc    pDesc;
1510
1511    /* Init the TD ring entries */
1512    curr=pDevice->td1_pool_dma;
1513    for (i = 0; i < pDevice->sOpts.nTxDescs[1]; i++, curr+=sizeof(STxDesc)) {
1514        pDesc=&(pDevice->apTD1Rings[i]);
1515        pDesc->pTDInfo = alloc_td_info();
1516        ASSERT(pDesc->pTDInfo);
1517        if (pDevice->flags & DEVICE_FLAGS_TX_ALIGN) {
1518            pDesc->pTDInfo->buf=pDevice->tx1_bufs+(i)*PKT_BUF_SZ;
1519            pDesc->pTDInfo->buf_dma=pDevice->tx_bufs_dma1+(i)*PKT_BUF_SZ;
1520        }
1521        pDesc->next=&(pDevice->apTD1Rings[(i+1) % pDevice->sOpts.nTxDescs[1]]);
1522        pDesc->pTDInfo->curr_desc = cpu_to_le32(curr);
1523        pDesc->next_desc = cpu_to_le32(curr+sizeof(STxDesc));
1524    }
1525
1526    if (i > 0)
1527        pDevice->apTD1Rings[i-1].next_desc = cpu_to_le32(pDevice->td1_pool_dma);
1528    pDevice->apTailTD[1] = pDevice->apCurrTD[1] = &(pDevice->apTD1Rings[0]);
1529}
1530
1531
1532
1533static void device_free_td0_ring(PSDevice pDevice) {
1534    int i;
1535    for (i = 0; i < pDevice->sOpts.nTxDescs[0]; i++) {
1536        PSTxDesc        pDesc=&(pDevice->apTD0Rings[i]);
1537        PDEVICE_TD_INFO  pTDInfo=pDesc->pTDInfo;
1538
1539        if (pTDInfo->skb_dma && (pTDInfo->skb_dma != pTDInfo->buf_dma))
1540            pci_unmap_single(pDevice->pcid,pTDInfo->skb_dma,
1541               pTDInfo->skb->len, PCI_DMA_TODEVICE);
1542
1543        if (pTDInfo->skb)
1544            dev_kfree_skb(pTDInfo->skb);
1545
1546        kfree((void *)pDesc->pTDInfo);
1547    }
1548}
1549
1550static void device_free_td1_ring(PSDevice pDevice) {
1551    int i;
1552
1553    for (i = 0; i < pDevice->sOpts.nTxDescs[1]; i++) {
1554        PSTxDesc        pDesc=&(pDevice->apTD1Rings[i]);
1555        PDEVICE_TD_INFO  pTDInfo=pDesc->pTDInfo;
1556
1557        if (pTDInfo->skb_dma && (pTDInfo->skb_dma != pTDInfo->buf_dma))
1558            pci_unmap_single(pDevice->pcid, pTDInfo->skb_dma,
1559               pTDInfo->skb->len, PCI_DMA_TODEVICE);
1560
1561        if (pTDInfo->skb)
1562            dev_kfree_skb(pTDInfo->skb);
1563
1564        kfree((void *)pDesc->pTDInfo);
1565    }
1566
1567}
1568
1569
1570
1571/*-----------------------------------------------------------------*/
1572
1573static int device_rx_srv(PSDevice pDevice, unsigned int uIdx) {
1574    PSRxDesc    pRD;
1575    int works = 0;
1576
1577
1578    for (pRD = pDevice->pCurrRD[uIdx];
1579         pRD->m_rd0RD0.f1Owner == OWNED_BY_HOST;
1580         pRD = pRD->next) {
1581//        DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "pDevice->pCurrRD = %x, works = %d\n", pRD, works);
1582        if (works++>15)
1583            break;
1584        if (device_receive_frame(pDevice, pRD)) {
1585            if (!device_alloc_rx_buf(pDevice,pRD)) {
1586                    DBG_PRT(MSG_LEVEL_ERR, KERN_ERR
1587                    "%s: can not allocate rx buf\n", pDevice->dev->name);
1588                    break;
1589            }
1590        }
1591        pRD->m_rd0RD0.f1Owner = OWNED_BY_NIC;
1592        pDevice->dev->last_rx = jiffies;
1593    }
1594
1595    pDevice->pCurrRD[uIdx]=pRD;
1596
1597    return works;
1598}
1599
1600
1601static bool device_alloc_rx_buf(PSDevice pDevice, PSRxDesc pRD) {
1602
1603    PDEVICE_RD_INFO pRDInfo=pRD->pRDInfo;
1604
1605
1606    pRDInfo->skb = dev_alloc_skb((int)pDevice->rx_buf_sz);
1607#ifdef  PLICE_DEBUG
1608        //printk("device_alloc_rx_buf:skb is %x\n",pRDInfo->skb);
1609#endif
1610    if (pRDInfo->skb==NULL)
1611        return false;
1612    ASSERT(pRDInfo->skb);
1613    pRDInfo->skb->dev = pDevice->dev;
1614    pRDInfo->skb_dma = pci_map_single(pDevice->pcid, skb_tail_pointer(pRDInfo->skb),
1615                                      pDevice->rx_buf_sz, PCI_DMA_FROMDEVICE);
1616    *((unsigned int *) &(pRD->m_rd0RD0)) = 0; /* FIX cast */
1617
1618    pRD->m_rd0RD0.wResCount = cpu_to_le16(pDevice->rx_buf_sz);
1619    pRD->m_rd0RD0.f1Owner = OWNED_BY_NIC;
1620    pRD->m_rd1RD1.wReqCount = cpu_to_le16(pDevice->rx_buf_sz);
1621    pRD->buff_addr = cpu_to_le32(pRDInfo->skb_dma);
1622
1623    return true;
1624}
1625
1626
1627
1628bool device_alloc_frag_buf(PSDevice pDevice, PSDeFragControlBlock pDeF) {
1629
1630    pDeF->skb = dev_alloc_skb((int)pDevice->rx_buf_sz);
1631    if (pDeF->skb == NULL)
1632        return false;
1633    ASSERT(pDeF->skb);
1634    pDeF->skb->dev = pDevice->dev;
1635
1636    return true;
1637}
1638
1639
1640
1641static int device_tx_srv(PSDevice pDevice, unsigned int uIdx) {
1642    PSTxDesc                 pTD;
1643    bool bFull=false;
1644    int                      works = 0;
1645    unsigned char byTsr0;
1646    unsigned char byTsr1;
1647    unsigned int        uFrameSize, uFIFOHeaderSize;
1648    PSTxBufHead              pTxBufHead;
1649    struct net_device_stats* pStats = &pDevice->stats;
1650    struct sk_buff*          skb;
1651    unsigned int        uNodeIndex;
1652    PSMgmtObject             pMgmt = pDevice->pMgmt;
1653
1654
1655    for (pTD = pDevice->apTailTD[uIdx]; pDevice->iTDUsed[uIdx] >0; pTD = pTD->next) {
1656
1657        if (pTD->m_td0TD0.f1Owner == OWNED_BY_NIC)
1658            break;
1659        if (works++>15)
1660            break;
1661
1662        byTsr0 = pTD->m_td0TD0.byTSR0;
1663        byTsr1 = pTD->m_td0TD0.byTSR1;
1664
1665        //Only the status of first TD in the chain is correct
1666        if (pTD->m_td1TD1.byTCR & TCR_STP) {
1667
1668            if ((pTD->pTDInfo->byFlags & TD_FLAGS_NETIF_SKB) != 0) {
1669                uFIFOHeaderSize = pTD->pTDInfo->dwHeaderLength;
1670                uFrameSize = pTD->pTDInfo->dwReqCount - uFIFOHeaderSize;
1671                pTxBufHead = (PSTxBufHead) (pTD->pTDInfo->buf);
1672                // Update the statistics based on the Transmit status
1673                // now, we DONT check TSR0_CDH
1674
1675                STAvUpdateTDStatCounter(&pDevice->scStatistic,
1676                        byTsr0, byTsr1,
1677                        (unsigned char *)(pTD->pTDInfo->buf + uFIFOHeaderSize),
1678                        uFrameSize, uIdx);
1679
1680
1681                BSSvUpdateNodeTxCounter(pDevice,
1682                         byTsr0, byTsr1,
1683                         (unsigned char *)(pTD->pTDInfo->buf),
1684                         uFIFOHeaderSize
1685                         );
1686
1687                if ( !(byTsr1 & TSR1_TERR)) {
1688                    if (byTsr0 != 0) {
1689                        DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" Tx[%d] OK but has error. tsr1[%02X] tsr0[%02X].\n",
1690                           (int)uIdx, byTsr1, byTsr0);
1691                    }
1692                    if ((pTxBufHead->wFragCtl & FRAGCTL_ENDFRAG) != FRAGCTL_NONFRAG) {
1693                        pDevice->s802_11Counter.TransmittedFragmentCount ++;
1694                    }
1695                    pStats->tx_packets++;
1696                    pStats->tx_bytes += pTD->pTDInfo->skb->len;
1697                }
1698                else {
1699                     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" Tx[%d] dropped & tsr1[%02X] tsr0[%02X].\n",
1700                           (int)uIdx, byTsr1, byTsr0);
1701                    pStats->tx_errors++;
1702                    pStats->tx_dropped++;
1703                }
1704            }
1705
1706            if ((pTD->pTDInfo->byFlags & TD_FLAGS_PRIV_SKB) != 0) {
1707                if (pDevice->bEnableHostapd) {
1708                    DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "tx call back netif.. \n");
1709                    skb = pTD->pTDInfo->skb;
1710                        skb->dev = pDevice->apdev;
1711                        skb_reset_mac_header(skb);
1712                        skb->pkt_type = PACKET_OTHERHOST;
1713                    //skb->protocol = htons(ETH_P_802_2);
1714                        memset(skb->cb, 0, sizeof(skb->cb));
1715                        netif_rx(skb);
1716                    }
1717            }
1718
1719            if (byTsr1 & TSR1_TERR) {
1720            if ((pTD->pTDInfo->byFlags & TD_FLAGS_PRIV_SKB) != 0) {
1721                DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" Tx[%d] fail has error. tsr1[%02X] tsr0[%02X].\n",
1722                          (int)uIdx, byTsr1, byTsr0);
1723            }
1724
1725//                DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" Tx[%d] fail has error. tsr1[%02X] tsr0[%02X].\n",
1726//                          (int)uIdx, byTsr1, byTsr0);
1727
1728                if ((pMgmt->eCurrMode == WMAC_MODE_ESS_AP) &&
1729                    (pTD->pTDInfo->byFlags & TD_FLAGS_NETIF_SKB)) {
1730                    unsigned short wAID;
1731                    unsigned char byMask[8] = {1, 2, 4, 8, 0x10, 0x20, 0x40, 0x80};
1732
1733                    skb = pTD->pTDInfo->skb;
1734                    if (BSSDBbIsSTAInNodeDB(pMgmt, (unsigned char *)(skb->data), &uNodeIndex)) {
1735                        if (pMgmt->sNodeDBTable[uNodeIndex].bPSEnable) {
1736                            skb_queue_tail(&pMgmt->sNodeDBTable[uNodeIndex].sTxPSQueue, skb);
1737                            pMgmt->sNodeDBTable[uNodeIndex].wEnQueueCnt++;
1738                            // set tx map
1739                            wAID = pMgmt->sNodeDBTable[uNodeIndex].wAID;
1740                            pMgmt->abyPSTxMap[wAID >> 3] |=  byMask[wAID & 7];
1741                            pTD->pTDInfo->byFlags &= ~(TD_FLAGS_NETIF_SKB);
1742                            DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "tx_srv:tx fail re-queue sta index= %d, QueCnt= %d\n"
1743                                    ,(int)uNodeIndex, pMgmt->sNodeDBTable[uNodeIndex].wEnQueueCnt);
1744                            pStats->tx_errors--;
1745                            pStats->tx_dropped--;
1746                        }
1747                    }
1748                }
1749            }
1750            device_free_tx_buf(pDevice,pTD);
1751            pDevice->iTDUsed[uIdx]--;
1752        }
1753    }
1754
1755
1756    if (uIdx == TYPE_AC0DMA) {
1757        // RESERV_AC0DMA reserved for relay
1758
1759        if (AVAIL_TD(pDevice, uIdx) < RESERV_AC0DMA) {
1760            bFull = true;
1761            DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " AC0DMA is Full = %d\n", pDevice->iTDUsed[uIdx]);
1762        }
1763        if (netif_queue_stopped(pDevice->dev) && (bFull==false)){
1764            netif_wake_queue(pDevice->dev);
1765        }
1766    }
1767
1768
1769    pDevice->apTailTD[uIdx] = pTD;
1770
1771    return works;
1772}
1773
1774
1775static void device_error(PSDevice pDevice, unsigned short status) {
1776
1777    if (status & ISR_FETALERR) {
1778        DBG_PRT(MSG_LEVEL_ERR, KERN_ERR
1779            "%s: Hardware fatal error.\n",
1780            pDevice->dev->name);
1781        netif_stop_queue(pDevice->dev);
1782        del_timer(&pDevice->sTimerCommand);
1783        del_timer(&(pDevice->pMgmt->sTimerSecondCallback));
1784        pDevice->bCmdRunning = false;
1785        MACbShutdown(pDevice->PortOffset);
1786        return;
1787    }
1788
1789}
1790
1791static void device_free_tx_buf(PSDevice pDevice, PSTxDesc pDesc) {
1792    PDEVICE_TD_INFO  pTDInfo=pDesc->pTDInfo;
1793    struct sk_buff* skb=pTDInfo->skb;
1794
1795    // pre-allocated buf_dma can't be unmapped.
1796    if (pTDInfo->skb_dma && (pTDInfo->skb_dma != pTDInfo->buf_dma)) {
1797        pci_unmap_single(pDevice->pcid,pTDInfo->skb_dma,skb->len,
1798              PCI_DMA_TODEVICE);
1799    }
1800
1801    if ((pTDInfo->byFlags & TD_FLAGS_NETIF_SKB) != 0)
1802        dev_kfree_skb_irq(skb);
1803
1804    pTDInfo->skb_dma = 0;
1805    pTDInfo->skb = 0;
1806    pTDInfo->byFlags = 0;
1807}
1808
1809
1810
1811//PLICE_DEBUG ->
1812void    InitRxManagementQueue(PSDevice  pDevice)
1813{
1814        pDevice->rxManeQueue.packet_num = 0;
1815        pDevice->rxManeQueue.head = pDevice->rxManeQueue.tail = 0;
1816}
1817//PLICE_DEBUG<-
1818
1819
1820
1821
1822
1823//PLICE_DEBUG ->
1824int MlmeThread(
1825     void * Context)
1826{
1827        PSDevice        pDevice =  (PSDevice) Context;
1828        PSRxMgmtPacket                  pRxMgmtPacket;
1829        // int i ;
1830        //complete(&pDevice->notify);
1831//printk("Enter MngWorkItem,Queue packet num is %d\n",pDevice->rxManeQueue.packet_num);
1832
1833        //printk("Enter MlmeThread,packet _num is %d\n",pDevice->rxManeQueue.packet_num);
1834        //i = 0;
1835#if 1
1836        while (1)
1837        {
1838
1839        //printk("DDDD\n");
1840        //down(&pDevice->mlme_semaphore);
1841        // pRxMgmtPacket =  DeQueue(pDevice);
1842#if 1
1843                spin_lock_irq(&pDevice->lock);
1844                 while(pDevice->rxManeQueue.packet_num != 0)
1845                {
1846                         pRxMgmtPacket =  DeQueue(pDevice);
1847                                //pDevice;
1848                                //DequeueManageObject(pDevice->FirstRecvMngList, pDevice->LastRecvMngList);
1849                        vMgrRxManagePacket(pDevice, pDevice->pMgmt, pRxMgmtPacket);
1850                        //printk("packet_num is %d\n",pDevice->rxManeQueue.packet_num);
1851
1852                 }
1853                spin_unlock_irq(&pDevice->lock);
1854                if (mlme_kill == 0)
1855                break;
1856                //udelay(200);
1857#endif
1858        //printk("Before schedule thread jiffies is %x\n",jiffies);
1859        schedule();
1860        //printk("after schedule thread jiffies is %x\n",jiffies);
1861        if (mlme_kill == 0)
1862                break;
1863        //printk("i is %d\n",i);
1864        }
1865
1866#endif
1867        return 0;
1868
1869}
1870
1871
1872
1873static int  device_open(struct net_device *dev) {
1874    PSDevice    pDevice=(PSDevice) netdev_priv(dev);
1875    int i;
1876#ifdef WPA_SM_Transtatus
1877    extern SWPAResult wpa_Result;
1878#endif
1879
1880    pDevice->rx_buf_sz = PKT_BUF_SZ;
1881    if (!device_init_rings(pDevice)) {
1882        return -ENOMEM;
1883    }
1884//2008-5-13 <add> by chester
1885    i=request_irq(pDevice->pcid->irq, &device_intr, IRQF_SHARED, dev->name, dev);
1886    if (i)
1887        return i;
1888        //printk("DEBUG1\n");
1889#ifdef WPA_SM_Transtatus
1890     memset(wpa_Result.ifname,0,sizeof(wpa_Result.ifname));
1891     wpa_Result.proto = 0;
1892     wpa_Result.key_mgmt = 0;
1893     wpa_Result.eap_type = 0;
1894     wpa_Result.authenticated = false;
1895     pDevice->fWPA_Authened = false;
1896#endif
1897DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "call device init rd0 ring\n");
1898device_init_rd0_ring(pDevice);
1899    device_init_rd1_ring(pDevice);
1900    device_init_defrag_cb(pDevice);
1901    device_init_td0_ring(pDevice);
1902    device_init_td1_ring(pDevice);
1903//    VNTWIFIvSet11h(pDevice->pMgmt, pDevice->b11hEnable);
1904
1905
1906    if (pDevice->bDiversityRegCtlON) {
1907        device_init_diversity_timer(pDevice);
1908    }
1909    vMgrObjectInit(pDevice);
1910    vMgrTimerInit(pDevice);
1911
1912//PLICE_DEBUG->
1913#ifdef  TASK_LET
1914        tasklet_init (&pDevice->RxMngWorkItem,(void *)MngWorkItem,(unsigned long )pDevice);
1915#endif
1916#ifdef  THREAD
1917        InitRxManagementQueue(pDevice);
1918        mlme_kill = 0;
1919        mlme_task = kthread_run(MlmeThread,(void *) pDevice, "MLME");
1920        if (IS_ERR(mlme_task)) {
1921                printk("thread create fail\n");
1922                return -1;
1923        }
1924
1925        mlme_kill = 1;
1926#endif
1927
1928
1929
1930        //printk("thread id is %d\n",pDevice->MLMEThr_pid);
1931        //printk("Create thread time is %x\n",jiffies);
1932        //wait_for_completion(&pDevice->notify);
1933
1934
1935
1936
1937  // if (( SROMbyReadEmbedded(pDevice->PortOffset, EEP_OFS_RADIOCTL)&0x06)==0x04)
1938    //    return -ENOMEM;
1939DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "call device_init_registers\n");
1940        device_init_registers(pDevice, DEVICE_INIT_COLD);
1941    MACvReadEtherAddress(pDevice->PortOffset, pDevice->abyCurrentNetAddr);
1942    memcpy(pDevice->pMgmt->abyMACAddr, pDevice->abyCurrentNetAddr, ETH_ALEN);
1943    device_set_multi(pDevice->dev);
1944
1945    // Init for Key Management
1946    KeyvInitTable(&pDevice->sKey, pDevice->PortOffset);
1947    add_timer(&(pDevice->pMgmt->sTimerSecondCallback));
1948
1949        #ifdef WPA_SUPPLICANT_DRIVER_WEXT_SUPPORT
1950        /*
1951     pDevice->bwextstep0 = false;
1952     pDevice->bwextstep1 = false;
1953     pDevice->bwextstep2 = false;
1954     pDevice->bwextstep3 = false;
1955     */
1956       pDevice->bwextcount=0;
1957     pDevice->bWPASuppWextEnabled = false;
1958#endif
1959    pDevice->byReAssocCount = 0;
1960   pDevice->bWPADEVUp = false;
1961    // Patch: if WEP key already set by iwconfig but device not yet open
1962    if ((pDevice->bEncryptionEnable == true) && (pDevice->bTransmitKey == true)) {
1963        KeybSetDefaultKey(&(pDevice->sKey),
1964                            (unsigned long)(pDevice->byKeyIndex | (1 << 31)),
1965                            pDevice->uKeyLength,
1966                            NULL,
1967                            pDevice->abyKey,
1968                            KEY_CTL_WEP,
1969                            pDevice->PortOffset,
1970                            pDevice->byLocalID
1971                          );
1972         pDevice->eEncryptionStatus = Ndis802_11Encryption1Enabled;
1973    }
1974
1975//printk("DEBUG2\n");
1976
1977
1978DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "call MACvIntEnable\n");
1979        MACvIntEnable(pDevice->PortOffset, IMR_MASK_VALUE);
1980
1981    if (pDevice->pMgmt->eConfigMode == WMAC_CONFIG_AP) {
1982        bScheduleCommand((void *)pDevice, WLAN_CMD_RUN_AP, NULL);
1983        }
1984        else {
1985        bScheduleCommand((void *)pDevice, WLAN_CMD_BSSID_SCAN, NULL);
1986        bScheduleCommand((void *)pDevice, WLAN_CMD_SSID, NULL);
1987    }
1988    pDevice->flags |=DEVICE_FLAGS_OPENED;
1989
1990    DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "device_open success.. \n");
1991    return 0;
1992}
1993
1994
1995static int  device_close(struct net_device *dev) {
1996    PSDevice  pDevice=(PSDevice) netdev_priv(dev);
1997    PSMgmtObject     pMgmt = pDevice->pMgmt;
1998 //PLICE_DEBUG->
1999#ifdef  THREAD
2000        mlme_kill = 0;
2001#endif
2002//PLICE_DEBUG<-
2003//2007-1121-02<Add>by EinsnLiu
2004    if (pDevice->bLinkPass) {
2005        bScheduleCommand((void *)pDevice, WLAN_CMD_DISASSOCIATE, NULL);
2006        mdelay(30);
2007    }
2008#ifdef TxInSleep
2009    del_timer(&pDevice->sTimerTxData);
2010#endif
2011    del_timer(&pDevice->sTimerCommand);
2012    del_timer(&pMgmt->sTimerSecondCallback);
2013    if (pDevice->bDiversityRegCtlON) {
2014        del_timer(&pDevice->TimerSQ3Tmax1);
2015        del_timer(&pDevice->TimerSQ3Tmax2);
2016        del_timer(&pDevice->TimerSQ3Tmax3);
2017    }
2018
2019#ifdef  TASK_LET
2020        tasklet_kill(&pDevice->RxMngWorkItem);
2021#endif
2022     netif_stop_queue(dev);
2023    pDevice->bCmdRunning = false;
2024    MACbShutdown(pDevice->PortOffset);
2025    MACbSoftwareReset(pDevice->PortOffset);
2026    CARDbRadioPowerOff(pDevice);
2027
2028    pDevice->bLinkPass = false;
2029    memset(pMgmt->abyCurrBSSID, 0, 6);
2030    pMgmt->eCurrState = WMAC_STATE_IDLE;
2031    device_free_td0_ring(pDevice);
2032    device_free_td1_ring(pDevice);
2033    device_free_rd0_ring(pDevice);
2034    device_free_rd1_ring(pDevice);
2035    device_free_frag_buf(pDevice);
2036    device_free_rings(pDevice);
2037    BSSvClearNodeDBTable(pDevice, 0);
2038    free_irq(dev->irq, dev);
2039    pDevice->flags &=(~DEVICE_FLAGS_OPENED);
2040        //2008-0714-01<Add>by chester
2041device_release_WPADEV(pDevice);
2042//PLICE_DEBUG->
2043        //tasklet_kill(&pDevice->RxMngWorkItem);
2044//PLICE_DEBUG<-
2045    DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "device_close.. \n");
2046    return 0;
2047}
2048
2049
2050
2051static int device_dma0_tx_80211(struct sk_buff *skb, struct net_device *dev) {
2052    PSDevice        pDevice=netdev_priv(dev);
2053    unsigned char *pbMPDU;
2054    unsigned int cbMPDULen = 0;
2055
2056
2057    DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "device_dma0_tx_80211\n");
2058    spin_lock_irq(&pDevice->lock);
2059
2060    if (AVAIL_TD(pDevice, TYPE_TXDMA0) <= 0) {
2061        DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "device_dma0_tx_80211, td0 <=0\n");
2062        dev_kfree_skb_irq(skb);
2063        spin_unlock_irq(&pDevice->lock);
2064        return 0;
2065    }
2066
2067    if (pDevice->bStopTx0Pkt == true) {
2068        dev_kfree_skb_irq(skb);
2069        spin_unlock_irq(&pDevice->lock);
2070        return 0;
2071    }
2072
2073    cbMPDULen = skb->len;
2074    pbMPDU = skb->data;
2075
2076    vDMA0_tx_80211(pDevice, skb, pbMPDU, cbMPDULen);
2077
2078    spin_unlock_irq(&pDevice->lock);
2079
2080    return 0;
2081
2082}
2083
2084
2085
2086bool device_dma0_xmit(PSDevice pDevice, struct sk_buff *skb, unsigned int uNodeIndex) {
2087    PSMgmtObject    pMgmt = pDevice->pMgmt;
2088    PSTxDesc        pHeadTD, pLastTD;
2089    unsigned int cbFrameBodySize;
2090    unsigned int uMACfragNum;
2091    unsigned char byPktType;
2092    bool bNeedEncryption = false;
2093    PSKeyItem       pTransmitKey = NULL;
2094    unsigned int cbHeaderSize;
2095    unsigned int ii;
2096    SKeyItem        STempKey;
2097//    unsigned char byKeyIndex = 0;
2098
2099
2100    if (pDevice->bStopTx0Pkt == true) {
2101        dev_kfree_skb_irq(skb);
2102        return false;
2103    }
2104
2105    if (AVAIL_TD(pDevice, TYPE_TXDMA0) <= 0) {
2106        dev_kfree_skb_irq(skb);
2107        DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "device_dma0_xmit, td0 <=0\n");
2108        return false;
2109    }
2110
2111    if (pMgmt->eCurrMode == WMAC_MODE_ESS_AP) {
2112        if (pDevice->uAssocCount == 0) {
2113            dev_kfree_skb_irq(skb);
2114            DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "device_dma0_xmit, assocCount = 0\n");
2115            return false;
2116        }
2117    }
2118
2119    pHeadTD = pDevice->apCurrTD[TYPE_TXDMA0];
2120
2121    pHeadTD->m_td1TD1.byTCR = (TCR_EDP|TCR_STP);
2122
2123    memcpy(pDevice->sTxEthHeader.abyDstAddr, (unsigned char *)(skb->data), ETH_HLEN);
2124    cbFrameBodySize = skb->len - ETH_HLEN;
2125
2126    // 802.1H
2127    if (ntohs(pDevice->sTxEthHeader.wType) > ETH_DATA_LEN) {
2128        cbFrameBodySize += 8;
2129    }
2130    uMACfragNum = cbGetFragCount(pDevice, pTransmitKey, cbFrameBodySize, &pDevice->sTxEthHeader);
2131
2132    if ( uMACfragNum > AVAIL_TD(pDevice, TYPE_TXDMA0)) {
2133        dev_kfree_skb_irq(skb);
2134        return false;
2135    }
2136    byPktType = (unsigned char)pDevice->byPacketType;
2137
2138
2139    if (pDevice->bFixRate) {
2140        if (pDevice->eCurrentPHYType == PHY_TYPE_11B) {
2141            if (pDevice->uConnectionRate >= RATE_11M) {
2142                pDevice->wCurrentRate = RATE_11M;
2143            } else {
2144                pDevice->wCurrentRate = (unsigned short)pDevice->uConnectionRate;
2145            }
2146        } else {
2147            if (pDevice->uConnectionRate >= RATE_54M)
2148                pDevice->wCurrentRate = RATE_54M;
2149            else
2150                pDevice->wCurrentRate = (unsigned short)pDevice->uConnectionRate;
2151        }
2152    }
2153    else {
2154        pDevice->wCurrentRate = pDevice->pMgmt->sNodeDBTable[uNodeIndex].wTxDataRate;
2155    }
2156
2157    //preamble type
2158    if (pMgmt->sNodeDBTable[uNodeIndex].bShortPreamble) {
2159        pDevice->byPreambleType = pDevice->byShortPreamble;
2160    }
2161    else {
2162        pDevice->byPreambleType = PREAMBLE_LONG;
2163    }
2164
2165    DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "dma0: pDevice->wCurrentRate = %d \n", pDevice->wCurrentRate);
2166
2167
2168    if (pDevice->wCurrentRate <= RATE_11M) {
2169        byPktType = PK_TYPE_11B;
2170    } else if (pDevice->eCurrentPHYType == PHY_TYPE_11A) {
2171        byPktType = PK_TYPE_11A;
2172    } else {
2173        if (pDevice->bProtectMode == true) {
2174            byPktType = PK_TYPE_11GB;
2175        } else {
2176            byPktType = PK_TYPE_11GA;
2177        }
2178    }
2179
2180    if (pDevice->bEncryptionEnable == true)
2181        bNeedEncryption = true;
2182
2183    if (pDevice->bEnableHostWEP) {
2184        pTransmitKey = &STempKey;
2185        pTransmitKey->byCipherSuite = pMgmt->sNodeDBTable[uNodeIndex].byCipherSuite;
2186        pTransmitKey->dwKeyIndex = pMgmt->sNodeDBTable[uNodeIndex].dwKeyIndex;
2187        pTransmitKey->uKeyLength = pMgmt->sNodeDBTable[uNodeIndex].uWepKeyLength;
2188        pTransmitKey->dwTSC47_16 = pMgmt->sNodeDBTable[uNodeIndex].dwTSC47_16;
2189        pTransmitKey->wTSC15_0 = pMgmt->sNodeDBTable[uNodeIndex].wTSC15_0;
2190        memcpy(pTransmitKey->abyKey,
2191            &pMgmt->sNodeDBTable[uNodeIndex].abyWepKey[0],
2192            pTransmitKey->uKeyLength
2193            );
2194    }
2195    vGenerateFIFOHeader(pDevice, byPktType, pDevice->pbyTmpBuff, bNeedEncryption,
2196                        cbFrameBodySize, TYPE_TXDMA0, pHeadTD,
2197                        &pDevice->sTxEthHeader, (unsigned char *)skb->data, pTransmitKey, uNodeIndex,
2198                        &uMACfragNum,
2199                        &cbHeaderSize
2200                        );
2201
2202    if (MACbIsRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_PS)) {
2203        // Disable PS
2204        MACbPSWakeup(pDevice->PortOffset);
2205    }
2206
2207    pDevice->bPWBitOn = false;
2208
2209    pLastTD = pHeadTD;
2210    for (ii = 0; ii < uMACfragNum; ii++) {
2211        // Poll Transmit the adapter
2212        wmb();
2213        pHeadTD->m_td0TD0.f1Owner=OWNED_BY_NIC;
2214        wmb();
2215        if (ii == (uMACfragNum - 1))
2216            pLastTD = pHeadTD;
2217        pHeadTD = pHeadTD->next;
2218    }
2219
2220    // Save the information needed by the tx interrupt handler
2221    // to complete the Send request
2222    pLastTD->pTDInfo->skb = skb;
2223    pLastTD->pTDInfo->byFlags = 0;
2224    pLastTD->pTDInfo->byFlags |= TD_FLAGS_NETIF_SKB;
2225
2226    pDevice->apCurrTD[TYPE_TXDMA0] = pHeadTD;
2227
2228    MACvTransmit0(pDevice->PortOffset);
2229
2230
2231    return true;
2232}
2233
2234//TYPE_AC0DMA data tx
2235static int  device_xmit(struct sk_buff *skb, struct net_device *dev) {
2236    PSDevice pDevice=netdev_priv(dev);
2237
2238    PSMgmtObject    pMgmt = pDevice->pMgmt;
2239    PSTxDesc        pHeadTD, pLastTD;
2240    unsigned int uNodeIndex = 0;
2241    unsigned char byMask[8] = {1, 2, 4, 8, 0x10, 0x20, 0x40, 0x80};
2242    unsigned short wAID;
2243    unsigned int uMACfragNum = 1;
2244    unsigned int cbFrameBodySize;
2245    unsigned char byPktType;
2246    unsigned int cbHeaderSize;
2247    bool bNeedEncryption = false;
2248    PSKeyItem       pTransmitKey = NULL;
2249    SKeyItem        STempKey;
2250    unsigned int ii;
2251    bool bTKIP_UseGTK = false;
2252    bool bNeedDeAuth = false;
2253    unsigned char *pbyBSSID;
2254    bool bNodeExist = false;
2255
2256
2257
2258    spin_lock_irq(&pDevice->lock);
2259    if (pDevice->bLinkPass == false) {
2260        dev_kfree_skb_irq(skb);
2261        spin_unlock_irq(&pDevice->lock);
2262        return 0;
2263    }
2264
2265    if (pDevice->bStopDataPkt) {
2266        dev_kfree_skb_irq(skb);
2267        spin_unlock_irq(&pDevice->lock);
2268        return 0;
2269    }
2270
2271
2272    if (pMgmt->eCurrMode == WMAC_MODE_ESS_AP) {
2273        if (pDevice->uAssocCount == 0) {
2274            dev_kfree_skb_irq(skb);
2275            spin_unlock_irq(&pDevice->lock);
2276            return 0;
2277        }
2278        if (is_multicast_ether_addr((unsigned char *)(skb->data))) {
2279            uNodeIndex = 0;
2280            bNodeExist = true;
2281            if (pMgmt->sNodeDBTable[0].bPSEnable) {
2282                skb_queue_tail(&(pMgmt->sNodeDBTable[0].sTxPSQueue), skb);
2283                pMgmt->sNodeDBTable[0].wEnQueueCnt++;
2284                // set tx map
2285                pMgmt->abyPSTxMap[0] |= byMask[0];
2286                spin_unlock_irq(&pDevice->lock);
2287                return 0;
2288            }
2289}else {
2290            if (BSSDBbIsSTAInNodeDB(pMgmt, (unsigned char *)(skb->data), &uNodeIndex)) {
2291                if (pMgmt->sNodeDBTable[uNodeIndex].bPSEnable) {
2292                    skb_queue_tail(&pMgmt->sNodeDBTable[uNodeIndex].sTxPSQueue, skb);
2293                    pMgmt->sNodeDBTable[uNodeIndex].wEnQueueCnt++;
2294                    // set tx map
2295                    wAID = pMgmt->sNodeDBTable[uNodeIndex].wAID;
2296                    pMgmt->abyPSTxMap[wAID >> 3] |=  byMask[wAID & 7];
2297                    DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Set:pMgmt->abyPSTxMap[%d]= %d\n",
2298                             (wAID >> 3), pMgmt->abyPSTxMap[wAID >> 3]);
2299                    spin_unlock_irq(&pDevice->lock);
2300                    return 0;
2301                }
2302
2303                if (pMgmt->sNodeDBTable[uNodeIndex].bShortPreamble) {
2304                    pDevice->byPreambleType = pDevice->byShortPreamble;
2305
2306                }else {
2307                    pDevice->byPreambleType = PREAMBLE_LONG;
2308                }
2309                bNodeExist = true;
2310
2311            }
2312        }
2313
2314        if (bNodeExist == false) {
2315            DBG_PRT(MSG_LEVEL_DEBUG, KERN_DEBUG"Unknown STA not found in node DB \n");
2316            dev_kfree_skb_irq(skb);
2317            spin_unlock_irq(&pDevice->lock);
2318            return 0;
2319        }
2320    }
2321
2322    pHeadTD = pDevice->apCurrTD[TYPE_AC0DMA];
2323
2324    pHeadTD->m_td1TD1.byTCR = (TCR_EDP|TCR_STP);
2325
2326
2327    memcpy(pDevice->sTxEthHeader.abyDstAddr, (unsigned char *)(skb->data), ETH_HLEN);
2328    cbFrameBodySize = skb->len - ETH_HLEN;
2329    // 802.1H
2330    if (ntohs(pDevice->sTxEthHeader.wType) > ETH_DATA_LEN) {
2331        cbFrameBodySize += 8;
2332    }
2333
2334
2335    if (pDevice->bEncryptionEnable == true) {
2336        bNeedEncryption = true;
2337        // get Transmit key
2338        do {
2339            if ((pDevice->pMgmt->eCurrMode == WMAC_MODE_ESS_STA) &&
2340                (pDevice->pMgmt->eCurrState == WMAC_STATE_ASSOC)) {
2341                pbyBSSID = pDevice->abyBSSID;
2342                // get pairwise key
2343                if (KeybGetTransmitKey(&(pDevice->sKey), pbyBSSID, PAIRWISE_KEY, &pTransmitKey) == false) {
2344                    // get group key
2345                    if(KeybGetTransmitKey(&(pDevice->sKey), pbyBSSID, GROUP_KEY, &pTransmitKey) == true) {
2346                        bTKIP_UseGTK = true;
2347                        DBG_PRT(MSG_LEVEL_DEBUG, KERN_DEBUG"Get GTK.\n");
2348                        break;
2349                    }
2350                } else {
2351                    DBG_PRT(MSG_LEVEL_DEBUG, KERN_DEBUG"Get PTK.\n");
2352                    break;
2353                }
2354            }else if (pDevice->pMgmt->eCurrMode == WMAC_MODE_IBSS_STA) {
2355
2356                pbyBSSID = pDevice->sTxEthHeader.abyDstAddr;  //TO_DS = 0 and FROM_DS = 0 --> 802.11 MAC Address1
2357                DBG_PRT(MSG_LEVEL_DEBUG, KERN_DEBUG"IBSS Serach Key: \n");
2358                for (ii = 0; ii< 6; ii++)
2359                    DBG_PRT(MSG_LEVEL_DEBUG, KERN_DEBUG"%x \n", *(pbyBSSID+ii));
2360                DBG_PRT(MSG_LEVEL_DEBUG, KERN_DEBUG"\n");
2361
2362                // get pairwise key
2363                if(KeybGetTransmitKey(&(pDevice->sKey), pbyBSSID, PAIRWISE_KEY, &pTransmitKey) == true)
2364                    break;
2365            }
2366            // get group key
2367            pbyBSSID = pDevice->abyBroadcastAddr;
2368            if(KeybGetTransmitKey(&(pDevice->sKey), pbyBSSID, GROUP_KEY, &pTransmitKey) == false) {
2369                pTransmitKey = NULL;
2370                if (pDevice->pMgmt->eCurrMode == WMAC_MODE_IBSS_STA) {
2371                    DBG_PRT(MSG_LEVEL_DEBUG, KERN_DEBUG"IBSS and KEY is NULL. [%d]\n", pDevice->pMgmt->eCurrMode);
2372                }
2373                else
2374                    DBG_PRT(MSG_LEVEL_DEBUG, KERN_DEBUG"NOT IBSS and KEY is NULL. [%d]\n", pDevice->pMgmt->eCurrMode);
2375            } else {
2376                bTKIP_UseGTK = true;
2377                DBG_PRT(MSG_LEVEL_DEBUG, KERN_DEBUG"Get GTK.\n");
2378            }
2379        } while(false);
2380    }
2381
2382    if (pDevice->bEnableHostWEP) {
2383        DBG_PRT(MSG_LEVEL_DEBUG, KERN_DEBUG"acdma0: STA index %d\n", uNodeIndex);
2384        if (pDevice->bEncryptionEnable == true) {
2385            pTransmitKey = &STempKey;
2386            pTransmitKey->byCipherSuite = pMgmt->sNodeDBTable[uNodeIndex].byCipherSuite;
2387            pTransmitKey->dwKeyIndex = pMgmt->sNodeDBTable[uNodeIndex].dwKeyIndex;
2388            pTransmitKey->uKeyLength = pMgmt->sNodeDBTable[uNodeIndex].uWepKeyLength;
2389            pTransmitKey->dwTSC47_16 = pMgmt->sNodeDBTable[uNodeIndex].dwTSC47_16;
2390            pTransmitKey->wTSC15_0 = pMgmt->sNodeDBTable[uNodeIndex].wTSC15_0;
2391            memcpy(pTransmitKey->abyKey,
2392                &pMgmt->sNodeDBTable[uNodeIndex].abyWepKey[0],
2393                pTransmitKey->uKeyLength
2394                );
2395         }
2396    }
2397
2398    uMACfragNum = cbGetFragCount(pDevice, pTransmitKey, cbFrameBodySize, &pDevice->sTxEthHeader);
2399
2400    if (uMACfragNum > AVAIL_TD(pDevice, TYPE_AC0DMA)) {
2401        DBG_PRT(MSG_LEVEL_ERR, KERN_DEBUG "uMACfragNum > AVAIL_TD(TYPE_AC0DMA) = %d\n", uMACfragNum);
2402        dev_kfree_skb_irq(skb);
2403        spin_unlock_irq(&pDevice->lock);
2404        return 0;
2405    }
2406
2407    if (pTransmitKey != NULL) {
2408        if ((pTransmitKey->byCipherSuite == KEY_CTL_WEP) &&
2409            (pTransmitKey->uKeyLength == WLAN_WEP232_KEYLEN)) {
2410            uMACfragNum = 1; //WEP256 doesn't support fragment
2411        }
2412    }
2413
2414    byPktType = (unsigned char)pDevice->byPacketType;
2415
2416    if (pDevice->bFixRate) {
2417#ifdef  PLICE_DEBUG
2418        printk("Fix Rate: PhyType is %d,ConnectionRate is %d\n",pDevice->eCurrentPHYType,pDevice->uConnectionRate);
2419#endif
2420
2421        if (pDevice->eCurrentPHYType == PHY_TYPE_11B) {
2422            if (pDevice->uConnectionRate >= RATE_11M) {
2423                pDevice->wCurrentRate = RATE_11M;
2424            } else {
2425                pDevice->wCurrentRate = (unsigned short)pDevice->uConnectionRate;
2426            }
2427        } else {
2428            if ((pDevice->eCurrentPHYType == PHY_TYPE_11A) &&
2429                (pDevice->uConnectionRate <= RATE_6M)) {
2430                pDevice->wCurrentRate = RATE_6M;
2431            } else {
2432                if (pDevice->uConnectionRate >= RATE_54M)
2433                    pDevice->wCurrentRate = RATE_54M;
2434                else
2435                    pDevice->wCurrentRate = (unsigned short)pDevice->uConnectionRate;
2436
2437            }
2438        }
2439        pDevice->byACKRate = (unsigned char) pDevice->wCurrentRate;
2440        pDevice->byTopCCKBasicRate = RATE_1M;
2441        pDevice->byTopOFDMBasicRate = RATE_6M;
2442    }
2443    else {
2444        //auto rate
2445    if (pDevice->sTxEthHeader.wType == TYPE_PKT_802_1x) {
2446            if (pDevice->eCurrentPHYType != PHY_TYPE_11A) {
2447                pDevice->wCurrentRate = RATE_1M;
2448                pDevice->byACKRate = RATE_1M;
2449                pDevice->byTopCCKBasicRate = RATE_1M;
2450                pDevice->byTopOFDMBasicRate = RATE_6M;
2451            } else {
2452                pDevice->wCurrentRate = RATE_6M;
2453                pDevice->byACKRate = RATE_6M;
2454                pDevice->byTopCCKBasicRate = RATE_1M;
2455                pDevice->byTopOFDMBasicRate = RATE_6M;
2456            }
2457        }
2458        else {
2459                VNTWIFIvGetTxRate(  pDevice->pMgmt,
2460                                pDevice->sTxEthHeader.abyDstAddr,
2461                                &(pDevice->wCurrentRate),
2462                                &(pDevice->byACKRate),
2463                                &(pDevice->byTopCCKBasicRate),
2464                                &(pDevice->byTopOFDMBasicRate));
2465
2466
2467                }
2468    }
2469
2470//    DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "acdma0: pDevice->wCurrentRate = %d \n", pDevice->wCurrentRate);
2471
2472    if (pDevice->wCurrentRate <= RATE_11M) {
2473        byPktType = PK_TYPE_11B;
2474    } else if (pDevice->eCurrentPHYType == PHY_TYPE_11A) {
2475        byPktType = PK_TYPE_11A;
2476    } else {
2477        if (pDevice->bProtectMode == true) {
2478            byPktType = PK_TYPE_11GB;
2479        } else {
2480            byPktType = PK_TYPE_11GA;
2481        }
2482    }
2483
2484//#ifdef        PLICE_DEBUG
2485//      printk("FIX RATE:CurrentRate is %d");
2486//#endif
2487
2488    if (bNeedEncryption == true) {
2489        DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ntohs Pkt Type=%04x\n", ntohs(pDevice->sTxEthHeader.wType));
2490        if ((pDevice->sTxEthHeader.wType) == TYPE_PKT_802_1x) {
2491            bNeedEncryption = false;
2492            DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Pkt Type=%04x\n", (pDevice->sTxEthHeader.wType));
2493            if ((pDevice->pMgmt->eCurrMode == WMAC_MODE_ESS_STA) && (pDevice->pMgmt->eCurrState == WMAC_STATE_ASSOC)) {
2494                if (pTransmitKey == NULL) {
2495                    DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Don't Find TX KEY\n");
2496                }
2497                else {
2498                    if (bTKIP_UseGTK == true) {
2499                        DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"error: KEY is GTK!!~~\n");
2500                    }
2501                    else {
2502                        DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Find PTK [%lX]\n", pTransmitKey->dwKeyIndex);
2503                        bNeedEncryption = true;
2504                    }
2505                }
2506            }
2507
2508            if (pDevice->byCntMeasure == 2) {
2509                bNeedDeAuth = true;
2510                pDevice->s802_11Counter.TKIPCounterMeasuresInvoked++;
2511            }
2512
2513            if (pDevice->bEnableHostWEP) {
2514                if ((uNodeIndex != 0) &&
2515                    (pMgmt->sNodeDBTable[uNodeIndex].dwKeyIndex & PAIRWISE_KEY)) {
2516                    DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Find PTK [%lX]\n", pTransmitKey->dwKeyIndex);
2517                    bNeedEncryption = true;
2518                 }
2519             }
2520        }
2521        else {
2522            if (pTransmitKey == NULL) {
2523                DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"return no tx key\n");
2524                dev_kfree_skb_irq(skb);
2525                spin_unlock_irq(&pDevice->lock);
2526                return 0;
2527            }
2528        }
2529    }
2530
2531
2532#ifdef  PLICE_DEBUG
2533        //if (skb->len == 98)
2534        //{
2535        //      printk("ping:len is %d\n");
2536        //}
2537#endif
2538    vGenerateFIFOHeader(pDevice, byPktType, pDevice->pbyTmpBuff, bNeedEncryption,
2539                        cbFrameBodySize, TYPE_AC0DMA, pHeadTD,
2540                        &pDevice->sTxEthHeader, (unsigned char *)skb->data, pTransmitKey, uNodeIndex,
2541                        &uMACfragNum,
2542                        &cbHeaderSize
2543                        );
2544
2545    if (MACbIsRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_PS)) {
2546        // Disable PS
2547        MACbPSWakeup(pDevice->PortOffset);
2548    }
2549    pDevice->bPWBitOn = false;
2550
2551    pLastTD = pHeadTD;
2552    for (ii = 0; ii < uMACfragNum; ii++) {
2553        // Poll Transmit the adapter
2554        wmb();
2555        pHeadTD->m_td0TD0.f1Owner=OWNED_BY_NIC;
2556        wmb();
2557        if (ii == uMACfragNum - 1)
2558            pLastTD = pHeadTD;
2559        pHeadTD = pHeadTD->next;
2560    }
2561
2562    // Save the information needed by the tx interrupt handler
2563    // to complete the Send request
2564    pLastTD->pTDInfo->skb = skb;
2565    pLastTD->pTDInfo->byFlags = 0;
2566    pLastTD->pTDInfo->byFlags |= TD_FLAGS_NETIF_SKB;
2567#ifdef TxInSleep
2568  pDevice->nTxDataTimeCout=0; //2008-8-21 chester <add> for send null packet
2569  #endif
2570    if (AVAIL_TD(pDevice, TYPE_AC0DMA) <= 1) {
2571        netif_stop_queue(dev);
2572    }
2573
2574    pDevice->apCurrTD[TYPE_AC0DMA] = pHeadTD;
2575//#ifdef        PLICE_DEBUG
2576        if (pDevice->bFixRate)
2577        {
2578                printk("FixRate:Rate is %d,TxPower is %d\n",pDevice->wCurrentRate,pDevice->byCurPwr);
2579        }
2580        else
2581        {
2582                //printk("Auto Rate:Rate is %d,TxPower is %d\n",pDevice->wCurrentRate,pDevice->byCurPwr);
2583        }
2584//#endif
2585
2586{
2587    unsigned char Protocol_Version;    //802.1x Authentication
2588    unsigned char Packet_Type;           //802.1x Authentication
2589    unsigned char Descriptor_type;
2590    unsigned short Key_info;
2591bool bTxeapol_key = false;
2592    Protocol_Version = skb->data[ETH_HLEN];
2593    Packet_Type = skb->data[ETH_HLEN+1];
2594    Descriptor_type = skb->data[ETH_HLEN+1+1+2];
2595    Key_info = (skb->data[ETH_HLEN+1+1+2+1] << 8)|(skb->data[ETH_HLEN+1+1+2+2]);
2596   if (pDevice->sTxEthHeader.wType == TYPE_PKT_802_1x) {
2597           if(((Protocol_Version==1) ||(Protocol_Version==2)) &&
2598                (Packet_Type==3)) {  //802.1x OR eapol-key challenge frame transfer
2599                        bTxeapol_key = true;
2600                if((Descriptor_type==254)||(Descriptor_type==2)) {       //WPA or RSN
2601                       if(!(Key_info & BIT3) &&   //group-key challenge
2602                           (Key_info & BIT8) && (Key_info & BIT9)) {    //send 2/2 key
2603                          pDevice->fWPA_Authened = true;
2604                          if(Descriptor_type==254)
2605                              printk("WPA ");
2606                          else
2607                              printk("WPA2 ");
2608                          printk("Authentication completed!!\n");
2609                        }
2610                 }
2611             }
2612   }
2613}
2614
2615    MACvTransmitAC0(pDevice->PortOffset);
2616//    DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "acdma0:pDevice->apCurrTD= %p\n", pHeadTD);
2617
2618    dev->trans_start = jiffies;
2619
2620    spin_unlock_irq(&pDevice->lock);
2621    return 0;
2622
2623}
2624
2625static  irqreturn_t  device_intr(int irq,  void *dev_instance) {
2626    struct net_device* dev=dev_instance;
2627    PSDevice     pDevice=(PSDevice) netdev_priv(dev);
2628
2629    int             max_count=0;
2630    unsigned long dwMIBCounter=0;
2631    PSMgmtObject    pMgmt = pDevice->pMgmt;
2632    unsigned char byOrgPageSel=0;
2633    int             handled = 0;
2634    unsigned char byData = 0;
2635    int             ii= 0;
2636//    unsigned char byRSSI;
2637
2638
2639    MACvReadISR(pDevice->PortOffset, &pDevice->dwIsr);
2640
2641    if (pDevice->dwIsr == 0)
2642        return IRQ_RETVAL(handled);
2643
2644    if (pDevice->dwIsr == 0xffffffff) {
2645        DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "dwIsr = 0xffff\n");
2646        return IRQ_RETVAL(handled);
2647    }
2648    /*
2649      // 2008-05-21 <mark> by Richardtai, we can't read RSSI here, because no packet bound with RSSI
2650
2651        if ((pDevice->dwIsr & ISR_RXDMA0) &&
2652        (pDevice->byLocalID != REV_ID_VT3253_B0) &&
2653        (pDevice->bBSSIDFilter == true)) {
2654        // update RSSI
2655        //BBbReadEmbedded(pDevice->PortOffset, 0x3E, &byRSSI);
2656        //pDevice->uCurrRSSI = byRSSI;
2657    }
2658    */
2659
2660    handled = 1;
2661    MACvIntDisable(pDevice->PortOffset);
2662    spin_lock_irq(&pDevice->lock);
2663
2664    //Make sure current page is 0
2665    VNSvInPortB(pDevice->PortOffset + MAC_REG_PAGE1SEL, &byOrgPageSel);
2666    if (byOrgPageSel == 1) {
2667        MACvSelectPage0(pDevice->PortOffset);
2668    }
2669    else
2670        byOrgPageSel = 0;
2671
2672    MACvReadMIBCounter(pDevice->PortOffset, &dwMIBCounter);
2673    // TBD....
2674    // Must do this after doing rx/tx, cause ISR bit is slow
2675    // than RD/TD write back
2676    // update ISR counter
2677    STAvUpdate802_11Counter(&pDevice->s802_11Counter, &pDevice->scStatistic , dwMIBCounter);
2678    while (pDevice->dwIsr != 0) {
2679
2680        STAvUpdateIsrStatCounter(&pDevice->scStatistic, pDevice->dwIsr);
2681        MACvWriteISR(pDevice->PortOffset, pDevice->dwIsr);
2682
2683        if (pDevice->dwIsr & ISR_FETALERR){
2684            DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " ISR_FETALERR \n");
2685            VNSvOutPortB(pDevice->PortOffset + MAC_REG_SOFTPWRCTL, 0);
2686            VNSvOutPortW(pDevice->PortOffset + MAC_REG_SOFTPWRCTL, SOFTPWRCTL_SWPECTI);
2687            device_error(pDevice, pDevice->dwIsr);
2688        }
2689
2690        if (pDevice->byLocalID > REV_ID_VT3253_B1) {
2691
2692            if (pDevice->dwIsr & ISR_MEASURESTART) {
2693                // 802.11h measure start
2694                pDevice->byOrgChannel = pDevice->byCurrentCh;
2695                VNSvInPortB(pDevice->PortOffset + MAC_REG_RCR, &(pDevice->byOrgRCR));
2696                VNSvOutPortB(pDevice->PortOffset + MAC_REG_RCR, (RCR_RXALLTYPE | RCR_UNICAST | RCR_BROADCAST | RCR_MULTICAST | RCR_WPAERR));
2697                MACvSelectPage1(pDevice->PortOffset);
2698                VNSvInPortD(pDevice->PortOffset + MAC_REG_MAR0, &(pDevice->dwOrgMAR0));
2699                VNSvInPortD(pDevice->PortOffset + MAC_REG_MAR4, &(pDevice->dwOrgMAR4));
2700                MACvSelectPage0(pDevice->PortOffset);
2701               //xxxx
2702               // WCMDbFlushCommandQueue(pDevice->pMgmt, true);
2703                if (set_channel(pDevice, pDevice->pCurrMeasureEID->sReq.byChannel) == true) {
2704                    pDevice->bMeasureInProgress = true;
2705                    MACvSelectPage1(pDevice->PortOffset);
2706                    MACvRegBitsOn(pDevice->PortOffset, MAC_REG_MSRCTL, MSRCTL_READY);
2707                    MACvSelectPage0(pDevice->PortOffset);
2708                    pDevice->byBasicMap = 0;
2709                    pDevice->byCCAFraction = 0;
2710                    for(ii=0;ii<8;ii++) {
2711                        pDevice->dwRPIs[ii] = 0;
2712                    }
2713                } else {
2714                    // can not measure because set channel fail
2715                   // WCMDbResetCommandQueue(pDevice->pMgmt);
2716                    // clear measure control
2717                    MACvRegBitsOff(pDevice->PortOffset, MAC_REG_MSRCTL, MSRCTL_EN);
2718                    s_vCompleteCurrentMeasure(pDevice, MEASURE_MODE_INCAPABLE);
2719                    MACvSelectPage1(pDevice->PortOffset);
2720                    MACvRegBitsOn(pDevice->PortOffset, MAC_REG_MSRCTL+1, MSRCTL1_TXPAUSE);
2721                    MACvSelectPage0(pDevice->PortOffset);
2722                }
2723            }
2724            if (pDevice->dwIsr & ISR_MEASUREEND) {
2725                // 802.11h measure end
2726                pDevice->bMeasureInProgress = false;
2727                VNSvOutPortB(pDevice->PortOffset + MAC_REG_RCR, pDevice->byOrgRCR);
2728                MACvSelectPage1(pDevice->PortOffset);
2729                VNSvOutPortD(pDevice->PortOffset + MAC_REG_MAR0, pDevice->dwOrgMAR0);
2730                VNSvOutPortD(pDevice->PortOffset + MAC_REG_MAR4, pDevice->dwOrgMAR4);
2731                VNSvInPortB(pDevice->PortOffset + MAC_REG_MSRBBSTS, &byData);
2732                pDevice->byBasicMap |= (byData >> 4);
2733                VNSvInPortB(pDevice->PortOffset + MAC_REG_CCAFRACTION, &pDevice->byCCAFraction);
2734                VNSvInPortB(pDevice->PortOffset + MAC_REG_MSRCTL, &byData);
2735                // clear measure control
2736                MACvRegBitsOff(pDevice->PortOffset, MAC_REG_MSRCTL, MSRCTL_EN);
2737                MACvSelectPage0(pDevice->PortOffset);
2738                set_channel(pDevice, pDevice->byOrgChannel);
2739                // WCMDbResetCommandQueue(pDevice->pMgmt);
2740                MACvSelectPage1(pDevice->PortOffset);
2741                MACvRegBitsOn(pDevice->PortOffset, MAC_REG_MSRCTL+1, MSRCTL1_TXPAUSE);
2742                MACvSelectPage0(pDevice->PortOffset);
2743                if (byData & MSRCTL_FINISH) {
2744                    // measure success
2745                    s_vCompleteCurrentMeasure(pDevice, 0);
2746                } else {
2747                    // can not measure because not ready before end of measure time
2748                    s_vCompleteCurrentMeasure(pDevice, MEASURE_MODE_LATE);
2749                }
2750            }
2751            if (pDevice->dwIsr & ISR_QUIETSTART) {
2752                do {
2753                    ;
2754                } while (CARDbStartQuiet(pDevice) == false);
2755            }
2756        }
2757
2758        if (pDevice->dwIsr & ISR_TBTT) {
2759            if (pDevice->bEnableFirstQuiet == true) {
2760                pDevice->byQuietStartCount--;
2761                if (pDevice->byQuietStartCount == 0) {
2762                    pDevice->bEnableFirstQuiet = false;
2763                    MACvSelectPage1(pDevice->PortOffset);
2764                    MACvRegBitsOn(pDevice->PortOffset, MAC_REG_MSRCTL, (MSRCTL_QUIETTXCHK | MSRCTL_QUIETEN));
2765                    MACvSelectPage0(pDevice->PortOffset);
2766                }
2767            }
2768            if ((pDevice->bChannelSwitch == true) &&
2769                (pDevice->eOPMode == OP_MODE_INFRASTRUCTURE)) {
2770                pDevice->byChannelSwitchCount--;
2771                if (pDevice->byChannelSwitchCount == 0) {
2772                    pDevice->bChannelSwitch = false;
2773                    set_channel(pDevice, pDevice->byNewChannel);
2774                    VNTWIFIbChannelSwitch(pDevice->pMgmt, pDevice->byNewChannel);
2775                    MACvSelectPage1(pDevice->PortOffset);
2776                    MACvRegBitsOn(pDevice->PortOffset, MAC_REG_MSRCTL+1, MSRCTL1_TXPAUSE);
2777                    MACvSelectPage0(pDevice->PortOffset);
2778                    CARDbStartTxPacket(pDevice, PKT_TYPE_802_11_ALL);
2779
2780                }
2781            }
2782            if (pDevice->eOPMode == OP_MODE_ADHOC) {
2783                //pDevice->bBeaconSent = false;
2784            } else {
2785                if ((pDevice->bUpdateBBVGA) && (pDevice->bLinkPass == true) && (pDevice->uCurrRSSI != 0)) {
2786                    long            ldBm;
2787
2788                    RFvRSSITodBm(pDevice, (unsigned char) pDevice->uCurrRSSI, &ldBm);
2789                    for (ii=0;ii<BB_VGA_LEVEL;ii++) {
2790                        if (ldBm < pDevice->ldBmThreshold[ii]) {
2791                            pDevice->byBBVGANew = pDevice->abyBBVGA[ii];
2792                            break;
2793                        }
2794                    }
2795                    if (pDevice->byBBVGANew != pDevice->byBBVGACurrent) {
2796                        pDevice->uBBVGADiffCount++;
2797                        if (pDevice->uBBVGADiffCount == 1) {
2798                            // first VGA diff gain
2799                            BBvSetVGAGainOffset(pDevice, pDevice->byBBVGANew);
2800                            DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"First RSSI[%d] NewGain[%d] OldGain[%d] Count[%d]\n",
2801                                            (int)ldBm, pDevice->byBBVGANew, pDevice->byBBVGACurrent, (int)pDevice->uBBVGADiffCount);
2802                        }
2803                        if (pDevice->uBBVGADiffCount >= BB_VGA_CHANGE_THRESHOLD) {
2804                            DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"RSSI[%d] NewGain[%d] OldGain[%d] Count[%d]\n",
2805                                            (int)ldBm, pDevice->byBBVGANew, pDevice->byBBVGACurrent, (int)pDevice->uBBVGADiffCount);
2806                            BBvSetVGAGainOffset(pDevice, pDevice->byBBVGANew);
2807                        }
2808                    } else {
2809                        pDevice->uBBVGADiffCount = 1;
2810                    }
2811                }
2812            }
2813
2814            pDevice->bBeaconSent = false;
2815            if (pDevice->bEnablePSMode) {
2816                PSbIsNextTBTTWakeUp((void *)pDevice);
2817            }
2818
2819            if ((pDevice->eOPMode == OP_MODE_AP) ||
2820                (pDevice->eOPMode == OP_MODE_ADHOC)) {
2821
2822                MACvOneShotTimer1MicroSec(pDevice->PortOffset,
2823                        (pMgmt->wIBSSBeaconPeriod - MAKE_BEACON_RESERVED) << 10);
2824            }
2825
2826            if (pDevice->eOPMode == OP_MODE_ADHOC && pDevice->pMgmt->wCurrATIMWindow > 0) {
2827                // todo adhoc PS mode
2828            }
2829
2830        }
2831
2832        if (pDevice->dwIsr & ISR_BNTX) {
2833
2834            if (pDevice->eOPMode == OP_MODE_ADHOC) {
2835                pDevice->bIsBeaconBufReadySet = false;
2836                pDevice->cbBeaconBufReadySetCnt = 0;
2837            }
2838
2839            if (pDevice->eOPMode == OP_MODE_AP) {
2840                if(pMgmt->byDTIMCount > 0) {
2841                   pMgmt->byDTIMCount --;
2842                   pMgmt->sNodeDBTable[0].bRxPSPoll = false;
2843                }
2844                else {
2845                    if(pMgmt->byDTIMCount == 0) {
2846                        // check if mutltcast tx bufferring
2847                        pMgmt->byDTIMCount = pMgmt->byDTIMPeriod - 1;
2848                        pMgmt->sNodeDBTable[0].bRxPSPoll = true;
2849                        bScheduleCommand((void *)pDevice, WLAN_CMD_RX_PSPOLL, NULL);
2850                    }
2851                }
2852            }
2853            pDevice->bBeaconSent = true;
2854
2855            if (pDevice->bChannelSwitch == true) {
2856                pDevice->byChannelSwitchCount--;
2857                if (pDevice->byChannelSwitchCount == 0) {
2858                    pDevice->bChannelSwitch = false;
2859                    set_channel(pDevice, pDevice->byNewChannel);
2860                    VNTWIFIbChannelSwitch(pDevice->pMgmt, pDevice->byNewChannel);
2861                    MACvSelectPage1(pDevice->PortOffset);
2862                    MACvRegBitsOn(pDevice->PortOffset, MAC_REG_MSRCTL+1, MSRCTL1_TXPAUSE);
2863                    MACvSelectPage0(pDevice->PortOffset);
2864                    //VNTWIFIbSendBeacon(pDevice->pMgmt);
2865                    CARDbStartTxPacket(pDevice, PKT_TYPE_802_11_ALL);
2866                }
2867            }
2868
2869        }
2870
2871        if (pDevice->dwIsr & ISR_RXDMA0) {
2872            max_count += device_rx_srv(pDevice, TYPE_RXDMA0);
2873        }
2874        if (pDevice->dwIsr & ISR_RXDMA1) {
2875            max_count += device_rx_srv(pDevice, TYPE_RXDMA1);
2876        }
2877        if (pDevice->dwIsr & ISR_TXDMA0){
2878            max_count += device_tx_srv(pDevice, TYPE_TXDMA0);
2879        }
2880        if (pDevice->dwIsr & ISR_AC0DMA){
2881            max_count += device_tx_srv(pDevice, TYPE_AC0DMA);
2882        }
2883        if (pDevice->dwIsr & ISR_SOFTTIMER) {
2884
2885        }
2886        if (pDevice->dwIsr & ISR_SOFTTIMER1) {
2887            if (pDevice->eOPMode == OP_MODE_AP) {
2888               if (pDevice->bShortSlotTime)
2889                   pMgmt->wCurrCapInfo |= WLAN_SET_CAP_INFO_SHORTSLOTTIME(1);
2890               else
2891                   pMgmt->wCurrCapInfo &= ~(WLAN_SET_CAP_INFO_SHORTSLOTTIME(1));
2892            }
2893            bMgrPrepareBeaconToSend(pDevice, pMgmt);
2894            pDevice->byCntMeasure = 0;
2895        }
2896
2897        MACvReadISR(pDevice->PortOffset, &pDevice->dwIsr);
2898
2899        MACvReceive0(pDevice->PortOffset);
2900        MACvReceive1(pDevice->PortOffset);
2901
2902        if (max_count>pDevice->sOpts.int_works)
2903            break;
2904    }
2905
2906    if (byOrgPageSel == 1) {
2907        MACvSelectPage1(pDevice->PortOffset);
2908    }
2909
2910    spin_unlock_irq(&pDevice->lock);
2911    MACvIntEnable(pDevice->PortOffset, IMR_MASK_VALUE);
2912
2913    return IRQ_RETVAL(handled);
2914}
2915
2916
2917static unsigned const ethernet_polynomial = 0x04c11db7U;
2918static inline u32 ether_crc(int length, unsigned char *data)
2919{
2920    int crc = -1;
2921
2922    while(--length >= 0) {
2923        unsigned char current_octet = *data++;
2924        int bit;
2925        for (bit = 0; bit < 8; bit++, current_octet >>= 1) {
2926            crc = (crc << 1) ^
2927                ((crc < 0) ^ (current_octet & 1) ? ethernet_polynomial : 0);
2928        }
2929    }
2930    return crc;
2931}
2932
2933//2008-8-4 <add> by chester
2934static int Config_FileGetParameter(unsigned char *string,
2935                unsigned char *dest, unsigned char *source)
2936{
2937  unsigned char buf1[100];
2938  int source_len = strlen(source);
2939
2940    memset(buf1,0,100);
2941    strcat(buf1, string);
2942    strcat(buf1, "=");
2943    source+=strlen(buf1);
2944
2945   memcpy(dest,source,source_len-strlen(buf1));
2946 return true;
2947}
2948
2949int Config_FileOperation(PSDevice pDevice,bool fwrite,unsigned char *Parameter) {
2950    unsigned char *config_path = CONFIG_PATH;
2951    unsigned char *buffer = NULL;
2952    unsigned char tmpbuffer[20];
2953    struct file   *filp=NULL;
2954    mm_segment_t old_fs = get_fs();
2955    //int oldfsuid=0,oldfsgid=0;
2956    int result=0;
2957
2958    set_fs (KERNEL_DS);
2959
2960    /* Can't do this anymore, so we rely on correct filesystem permissions:
2961    //Make sure a caller can read or write power as root
2962    oldfsuid=current->cred->fsuid;
2963    oldfsgid=current->cred->fsgid;
2964    current->cred->fsuid = 0;
2965    current->cred->fsgid = 0;
2966    */
2967
2968    //open file
2969      filp = filp_open(config_path, O_RDWR, 0);
2970        if (IS_ERR(filp)) {
2971             printk("Config_FileOperation:open file fail?\n");
2972             result=-1;
2973             goto error2;
2974          }
2975
2976     if(!(filp->f_op) || !(filp->f_op->read) ||!(filp->f_op->write)) {
2977           printk("file %s cann't readable or writable?\n",config_path);
2978          result = -1;
2979          goto error1;
2980        }
2981
2982buffer = kmalloc(1024, GFP_KERNEL);
2983if(buffer==NULL) {
2984  printk("allocate mem for file fail?\n");
2985  result = -1;
2986  goto error1;
2987}
2988
2989if(filp->f_op->read(filp, buffer, 1024, &filp->f_pos)<0) {
2990 printk("read file error?\n");
2991 result = -1;
2992 goto error1;
2993}
2994
2995if(Config_FileGetParameter("ZONETYPE",tmpbuffer,buffer)!=true) {
2996  printk("get parameter error?\n");
2997  result = -1;
2998  goto error1;
2999}
3000
3001if(memcmp(tmpbuffer,"USA",3)==0) {
3002  result=ZoneType_USA;
3003}
3004else if(memcmp(tmpbuffer,"JAPAN",5)==0) {
3005  result=ZoneType_Japan;
3006}
3007else if(memcmp(tmpbuffer,"EUROPE",5)==0) {
3008 result=ZoneType_Europe;
3009}
3010else {
3011  result = -1;
3012  printk("Unknown Zonetype[%s]?\n",tmpbuffer);
3013}
3014
3015error1:
3016  kfree(buffer);
3017
3018  if(filp_close(filp,NULL))
3019       printk("Config_FileOperation:close file fail\n");
3020
3021error2:
3022  set_fs (old_fs);
3023
3024  /*
3025  current->cred->fsuid=oldfsuid;
3026  current->cred->fsgid=oldfsgid;
3027  */
3028
3029  return result;
3030}
3031
3032
3033
3034static void device_set_multi(struct net_device *dev) {
3035    PSDevice         pDevice = (PSDevice) netdev_priv(dev);
3036
3037    PSMgmtObject     pMgmt = pDevice->pMgmt;
3038    u32              mc_filter[2];
3039    struct netdev_hw_addr *ha;
3040
3041
3042    VNSvInPortB(pDevice->PortOffset + MAC_REG_RCR, &(pDevice->byRxMode));
3043
3044    if (dev->flags & IFF_PROMISC) {         /* Set promiscuous. */
3045        DBG_PRT(MSG_LEVEL_ERR,KERN_NOTICE "%s: Promiscuous mode enabled.\n", dev->name);
3046        /* Unconditionally log net taps. */
3047        pDevice->byRxMode |= (RCR_MULTICAST|RCR_BROADCAST|RCR_UNICAST);
3048    }
3049    else if ((netdev_mc_count(dev) > pDevice->multicast_limit)
3050        ||  (dev->flags & IFF_ALLMULTI)) {
3051        MACvSelectPage1(pDevice->PortOffset);
3052        VNSvOutPortD(pDevice->PortOffset + MAC_REG_MAR0, 0xffffffff);
3053        VNSvOutPortD(pDevice->PortOffset + MAC_REG_MAR0 + 4, 0xffffffff);
3054        MACvSelectPage0(pDevice->PortOffset);
3055        pDevice->byRxMode |= (RCR_MULTICAST|RCR_BROADCAST);
3056    }
3057    else {
3058        memset(mc_filter, 0, sizeof(mc_filter));
3059        netdev_for_each_mc_addr(ha, dev) {
3060            int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
3061            mc_filter[bit_nr >> 5] |= cpu_to_le32(1 << (bit_nr & 31));
3062        }
3063        MACvSelectPage1(pDevice->PortOffset);
3064        VNSvOutPortD(pDevice->PortOffset + MAC_REG_MAR0, mc_filter[0]);
3065        VNSvOutPortD(pDevice->PortOffset + MAC_REG_MAR0 + 4, mc_filter[1]);
3066        MACvSelectPage0(pDevice->PortOffset);
3067        pDevice->byRxMode &= ~(RCR_UNICAST);
3068        pDevice->byRxMode |= (RCR_MULTICAST|RCR_BROADCAST);
3069    }
3070
3071    if (pMgmt->eConfigMode == WMAC_CONFIG_AP) {
3072        // If AP mode, don't enable RCR_UNICAST. Since hw only compare addr1 with local mac.
3073        pDevice->byRxMode |= (RCR_MULTICAST|RCR_BROADCAST);
3074        pDevice->byRxMode &= ~(RCR_UNICAST);
3075    }
3076
3077    VNSvOutPortB(pDevice->PortOffset + MAC_REG_RCR, pDevice->byRxMode);
3078    DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "pDevice->byRxMode = %x\n", pDevice->byRxMode );
3079}
3080
3081
3082static struct net_device_stats *device_get_stats(struct net_device *dev) {
3083    PSDevice pDevice=(PSDevice) netdev_priv(dev);
3084
3085    return &pDevice->stats;
3086}
3087
3088
3089
3090static int  device_ioctl(struct net_device *dev, struct ifreq *rq, int cmd) {
3091        PSDevice                pDevice = (PSDevice)netdev_priv(dev);
3092
3093        struct iwreq *wrq = (struct iwreq *) rq;
3094        int                 rc =0;
3095    PSMgmtObject        pMgmt = pDevice->pMgmt;
3096    PSCmdRequest        pReq;
3097
3098
3099    if (pMgmt == NULL) {
3100        rc = -EFAULT;
3101        return rc;
3102    }
3103
3104    switch(cmd) {
3105
3106        case SIOCGIWNAME:
3107                rc = iwctl_giwname(dev, NULL, (char *)&(wrq->u.name), NULL);
3108                break;
3109
3110        case SIOCGIWNWID:     //0x8b03  support
3111                rc = -EOPNOTSUPP;
3112                break;
3113
3114                // Set frequency/channel
3115        case SIOCSIWFREQ:
3116            rc = iwctl_siwfreq(dev, NULL, &(wrq->u.freq), NULL);
3117                break;
3118
3119                // Get frequency/channel
3120        case SIOCGIWFREQ:
3121                rc = iwctl_giwfreq(dev, NULL, &(wrq->u.freq), NULL);
3122                break;
3123
3124                // Set desired network name (ESSID)
3125        case SIOCSIWESSID:
3126
3127                {
3128                        char essid[IW_ESSID_MAX_SIZE+1];
3129                        if (wrq->u.essid.length > IW_ESSID_MAX_SIZE) {
3130                                rc = -E2BIG;
3131                                break;
3132                        }
3133                        if (copy_from_user(essid, wrq->u.essid.pointer,
3134                                           wrq->u.essid.length)) {
3135                                rc = -EFAULT;
3136                                break;
3137                        }
3138                        rc = iwctl_siwessid(dev, NULL,
3139                                            &(wrq->u.essid), essid);
3140                }
3141                break;
3142
3143
3144                // Get current network name (ESSID)
3145        case SIOCGIWESSID:
3146
3147                {
3148                        char essid[IW_ESSID_MAX_SIZE+1];
3149                        if (wrq->u.essid.pointer)
3150                                rc = iwctl_giwessid(dev, NULL,
3151                                                    &(wrq->u.essid), essid);
3152                                if (copy_to_user(wrq->u.essid.pointer,
3153                                                         essid,
3154                                                         wrq->u.essid.length) )
3155                                        rc = -EFAULT;
3156                }
3157                break;
3158
3159        case SIOCSIWAP:
3160
3161                rc = iwctl_siwap(dev, NULL, &(wrq->u.ap_addr), NULL);
3162                break;
3163
3164
3165                // Get current Access Point (BSSID)
3166        case SIOCGIWAP:
3167                rc = iwctl_giwap(dev, NULL, &(wrq->u.ap_addr), NULL);
3168                break;
3169
3170
3171                // Set desired station name
3172        case SIOCSIWNICKN:
3173        DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCSIWNICKN \n");
3174        rc = -EOPNOTSUPP;
3175                break;
3176
3177                // Get current station name
3178        case SIOCGIWNICKN:
3179        DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCGIWNICKN \n");
3180        rc = -EOPNOTSUPP;
3181                break;
3182
3183                // Set the desired bit-rate
3184        case SIOCSIWRATE:
3185                rc = iwctl_siwrate(dev, NULL, &(wrq->u.bitrate), NULL);
3186                break;
3187
3188        // Get the current bit-rate
3189        case SIOCGIWRATE:
3190
3191                rc = iwctl_giwrate(dev, NULL, &(wrq->u.bitrate), NULL);
3192                break;
3193
3194        // Set the desired RTS threshold
3195        case SIOCSIWRTS:
3196
3197                rc = iwctl_siwrts(dev, NULL, &(wrq->u.rts), NULL);
3198                break;
3199
3200        // Get the current RTS threshold
3201        case SIOCGIWRTS:
3202
3203                rc = iwctl_giwrts(dev, NULL, &(wrq->u.rts), NULL);
3204                break;
3205
3206                // Set the desired fragmentation threshold
3207        case SIOCSIWFRAG:
3208
3209                rc = iwctl_siwfrag(dev, NULL, &(wrq->u.frag), NULL);
3210            break;
3211
3212        // Get the current fragmentation threshold
3213        case SIOCGIWFRAG:
3214
3215                rc = iwctl_giwfrag(dev, NULL, &(wrq->u.frag), NULL);
3216                break;
3217
3218                // Set mode of operation
3219        case SIOCSIWMODE:
3220        rc = iwctl_siwmode(dev, NULL, &(wrq->u.mode), NULL);
3221                break;
3222
3223                // Get mode of operation
3224        case SIOCGIWMODE:
3225                rc = iwctl_giwmode(dev, NULL, &(wrq->u.mode), NULL);
3226                break;
3227
3228                // Set WEP keys and mode
3229        case SIOCSIWENCODE:
3230                {
3231            char abyKey[WLAN_WEP232_KEYLEN];
3232
3233                        if (wrq->u.encoding.pointer) {
3234
3235
3236                                if (wrq->u.encoding.length > WLAN_WEP232_KEYLEN) {
3237                                        rc = -E2BIG;
3238                                        break;
3239                                }
3240                                memset(abyKey, 0, WLAN_WEP232_KEYLEN);
3241                                if (copy_from_user(abyKey,
3242                                                  wrq->u.encoding.pointer,
3243                                                  wrq->u.encoding.length)) {
3244                                        rc = -EFAULT;
3245                                        break;
3246                                }
3247                        } else if (wrq->u.encoding.length != 0) {
3248                                rc = -EINVAL;
3249                                break;
3250                        }
3251                        rc = iwctl_siwencode(dev, NULL, &(wrq->u.encoding), abyKey);
3252                }
3253                break;
3254
3255                // Get the WEP keys and mode
3256        case SIOCGIWENCODE:
3257
3258                if (!capable(CAP_NET_ADMIN)) {
3259                        rc = -EPERM;
3260                        break;
3261                }
3262                {
3263                    char abyKey[WLAN_WEP232_KEYLEN];
3264
3265                    rc = iwctl_giwencode(dev, NULL, &(wrq->u.encoding), abyKey);
3266                    if (rc != 0) break;
3267                        if (wrq->u.encoding.pointer) {
3268                                if (copy_to_user(wrq->u.encoding.pointer,
3269                                                        abyKey,
3270                                                        wrq->u.encoding.length))
3271                                        rc = -EFAULT;
3272                        }
3273                }
3274                break;
3275
3276                // Get the current Tx-Power
3277        case SIOCGIWTXPOW:
3278        DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCGIWTXPOW \n");
3279        rc = -EOPNOTSUPP;
3280                break;
3281
3282        case SIOCSIWTXPOW:
3283        DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCSIWTXPOW \n");
3284        rc = -EOPNOTSUPP;
3285                break;
3286
3287        case SIOCSIWRETRY:
3288
3289                rc = iwctl_siwretry(dev, NULL, &(wrq->u.retry), NULL);
3290                break;
3291
3292        case SIOCGIWRETRY:
3293
3294                rc = iwctl_giwretry(dev, NULL, &(wrq->u.retry), NULL);
3295                break;
3296
3297                // Get range of parameters
3298        case SIOCGIWRANGE:
3299
3300                {
3301                        struct iw_range range;
3302
3303                        rc = iwctl_giwrange(dev, NULL, &(wrq->u.data), (char *) &range);
3304                        if (copy_to_user(wrq->u.data.pointer, &range, sizeof(struct iw_range)))
3305                                rc = -EFAULT;
3306                }
3307
3308                break;
3309
3310        case SIOCGIWPOWER:
3311
3312                rc = iwctl_giwpower(dev, NULL, &(wrq->u.power), NULL);
3313                break;
3314
3315
3316        case SIOCSIWPOWER:
3317
3318                rc = iwctl_siwpower(dev, NULL, &(wrq->u.power), NULL);
3319                break;
3320
3321
3322        case SIOCGIWSENS:
3323
3324            rc = iwctl_giwsens(dev, NULL, &(wrq->u.sens), NULL);
3325                break;
3326
3327        case SIOCSIWSENS:
3328        DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCSIWSENS \n");
3329                rc = -EOPNOTSUPP;
3330                break;
3331
3332        case SIOCGIWAPLIST:
3333            {
3334            char buffer[IW_MAX_AP * (sizeof(struct sockaddr) + sizeof(struct iw_quality))];
3335
3336                    if (wrq->u.data.pointer) {
3337                        rc = iwctl_giwaplist(dev, NULL, &(wrq->u.data), buffer);
3338                        if (rc == 0) {
3339                    if (copy_to_user(wrq->u.data.pointer,
3340                                                        buffer,
3341                                                       (wrq->u.data.length * (sizeof(struct sockaddr) +  sizeof(struct iw_quality)))
3342                                        ))
3343                                    rc = -EFAULT;
3344                        }
3345            }
3346        }
3347                break;
3348
3349
3350#ifdef WIRELESS_SPY
3351                // Set the spy list
3352        case SIOCSIWSPY:
3353
3354        DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCSIWSPY \n");
3355                rc = -EOPNOTSUPP;
3356                break;
3357
3358                // Get the spy list
3359        case SIOCGIWSPY:
3360
3361        DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCGIWSPY \n");
3362                rc = -EOPNOTSUPP;
3363                break;
3364
3365#endif // WIRELESS_SPY
3366
3367        case SIOCGIWPRIV:
3368        DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCGIWPRIV \n");
3369                rc = -EOPNOTSUPP;
3370/*
3371                if(wrq->u.data.pointer) {
3372                        wrq->u.data.length = sizeof(iwctl_private_args) / sizeof( iwctl_private_args[0]);
3373
3374                        if(copy_to_user(wrq->u.data.pointer,
3375                                        (u_char *) iwctl_private_args,
3376                                        sizeof(iwctl_private_args)))
3377                                rc = -EFAULT;
3378                }
3379*/
3380                break;
3381
3382
3383//2008-0409-07, <Add> by Einsn Liu
3384#ifdef  WPA_SUPPLICANT_DRIVER_WEXT_SUPPORT
3385        case SIOCSIWAUTH:
3386                DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCSIWAUTH \n");
3387                rc = iwctl_siwauth(dev, NULL, &(wrq->u.param), NULL);
3388                break;
3389
3390        case SIOCGIWAUTH:
3391                DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCGIWAUTH \n");
3392                rc = iwctl_giwauth(dev, NULL, &(wrq->u.param), NULL);
3393                break;
3394
3395        case SIOCSIWGENIE:
3396                DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCSIWGENIE \n");
3397                rc = iwctl_siwgenie(dev, NULL, &(wrq->u.data), wrq->u.data.pointer);
3398                break;
3399
3400        case SIOCGIWGENIE:
3401                DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCGIWGENIE \n");
3402                rc = iwctl_giwgenie(dev, NULL, &(wrq->u.data), wrq->u.data.pointer);
3403                break;
3404
3405        case SIOCSIWENCODEEXT:
3406                {
3407                        char extra[sizeof(struct iw_encode_ext)+MAX_KEY_LEN+1];
3408                        DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCSIWENCODEEXT \n");
3409                        if(wrq->u.encoding.pointer){
3410                                memset(extra, 0, sizeof(struct iw_encode_ext)+MAX_KEY_LEN+1);
3411                                if(wrq->u.encoding.length > (sizeof(struct iw_encode_ext)+ MAX_KEY_LEN)){
3412                                        rc = -E2BIG;
3413                                        break;
3414                                }
3415                                if(copy_from_user(extra, wrq->u.encoding.pointer,wrq->u.encoding.length)){
3416                                        rc = -EFAULT;
3417                                        break;
3418                                }
3419                        }else if(wrq->u.encoding.length != 0){
3420                                rc = -EINVAL;
3421                                break;
3422                        }
3423                        rc = iwctl_siwencodeext(dev, NULL, &(wrq->u.encoding), extra);
3424                }
3425                break;
3426
3427        case SIOCGIWENCODEEXT:
3428                DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCGIWENCODEEXT \n");
3429                rc = iwctl_giwencodeext(dev, NULL, &(wrq->u.encoding), NULL);
3430                break;
3431
3432        case SIOCSIWMLME:
3433                DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCSIWMLME \n");
3434                rc = iwctl_siwmlme(dev, NULL, &(wrq->u.data), wrq->u.data.pointer);
3435                break;
3436
3437#endif // #ifdef WPA_SUPPLICANT_DRIVER_WEXT_SUPPORT
3438//End Add -- //2008-0409-07, <Add> by Einsn Liu
3439
3440    case IOCTL_CMD_TEST:
3441
3442                if (!(pDevice->flags & DEVICE_FLAGS_OPENED)) {
3443                    rc = -EFAULT;
3444                    break;
3445                } else {
3446                    rc = 0;
3447                }
3448        pReq = (PSCmdRequest)rq;
3449        pReq->wResult = MAGIC_CODE;
3450        break;
3451
3452    case IOCTL_CMD_SET:
3453
3454               #ifdef SndEvt_ToAPI
3455                  if((((PSCmdRequest)rq)->wCmdCode !=WLAN_CMD_SET_EVT) &&
3456                       !(pDevice->flags & DEVICE_FLAGS_OPENED))
3457              #else
3458                if (!(pDevice->flags & DEVICE_FLAGS_OPENED) &&
3459                       (((PSCmdRequest)rq)->wCmdCode !=WLAN_CMD_SET_WPA))
3460              #endif
3461                {
3462                    rc = -EFAULT;
3463                    break;
3464                } else {
3465                    rc = 0;
3466                }
3467
3468            if (test_and_set_bit( 0, (void*)&(pMgmt->uCmdBusy))) {
3469                    return -EBUSY;
3470            }
3471        rc = private_ioctl(pDevice, rq);
3472        clear_bit( 0, (void*)&(pMgmt->uCmdBusy));
3473        break;
3474
3475    case IOCTL_CMD_HOSTAPD:
3476
3477
3478        rc = vt6655_hostap_ioctl(pDevice, &wrq->u.data);
3479        break;
3480
3481    case IOCTL_CMD_WPA:
3482
3483        rc = wpa_ioctl(pDevice, &wrq->u.data);
3484        break;
3485
3486        case SIOCETHTOOL:
3487        return ethtool_ioctl(dev, (void *) rq->ifr_data);
3488        // All other calls are currently unsupported
3489
3490        default:
3491                rc = -EOPNOTSUPP;
3492        DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Ioctl command not support..%x\n", cmd);
3493
3494
3495    }
3496
3497    if (pDevice->bCommit) {
3498       if (pMgmt->eConfigMode == WMAC_CONFIG_AP) {
3499           netif_stop_queue(pDevice->dev);
3500           spin_lock_irq(&pDevice->lock);
3501           bScheduleCommand((void *)pDevice, WLAN_CMD_RUN_AP, NULL);
3502           spin_unlock_irq(&pDevice->lock);
3503       }
3504       else {
3505           DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Commit the settings\n");
3506           spin_lock_irq(&pDevice->lock);
3507           pDevice->bLinkPass = false;
3508           memset(pMgmt->abyCurrBSSID, 0, 6);
3509           pMgmt->eCurrState = WMAC_STATE_IDLE;
3510           netif_stop_queue(pDevice->dev);
3511        #ifdef WPA_SUPPLICANT_DRIVER_WEXT_SUPPORT
3512              pMgmt->eScanType = WMAC_SCAN_ACTIVE;
3513         if(pDevice->bWPASuppWextEnabled !=true)
3514         #endif
3515           bScheduleCommand((void *) pDevice, WLAN_CMD_BSSID_SCAN, pMgmt->abyDesireSSID);
3516           bScheduleCommand((void *) pDevice, WLAN_CMD_SSID, NULL);
3517           spin_unlock_irq(&pDevice->lock);
3518      }
3519      pDevice->bCommit = false;
3520    }
3521
3522    return rc;
3523}
3524
3525
3526static int ethtool_ioctl(struct net_device *dev, void *useraddr)
3527{
3528        u32 ethcmd;
3529
3530        if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
3531                return -EFAULT;
3532
3533        switch (ethcmd) {
3534        case ETHTOOL_GDRVINFO: {
3535                struct ethtool_drvinfo info = {ETHTOOL_GDRVINFO};
3536                strncpy(info.driver, DEVICE_NAME, sizeof(info.driver)-1);
3537                strncpy(info.version, DEVICE_VERSION, sizeof(info.version)-1);
3538                if (copy_to_user(useraddr, &info, sizeof(info)))
3539                        return -EFAULT;
3540                return 0;
3541        }
3542
3543        }
3544
3545        return -EOPNOTSUPP;
3546}
3547
3548/*------------------------------------------------------------------*/
3549
3550MODULE_DEVICE_TABLE(pci, vt6655_pci_id_table);
3551
3552static struct pci_driver device_driver = {
3553        .name = DEVICE_NAME,
3554        .id_table = vt6655_pci_id_table,
3555        .probe = vt6655_probe,
3556        .remove = vt6655_remove,
3557#ifdef CONFIG_PM
3558        .suspend = viawget_suspend,
3559        .resume = viawget_resume,
3560#endif
3561};
3562
3563static int __init vt6655_init_module(void)
3564{
3565    int ret;
3566
3567
3568//    ret=pci_module_init(&device_driver);
3569        //ret = pcie_port_service_register(&device_driver);
3570        ret = pci_register_driver(&device_driver);
3571#ifdef CONFIG_PM
3572    if(ret >= 0)
3573        register_reboot_notifier(&device_notifier);
3574#endif
3575
3576    return ret;
3577}
3578
3579static void __exit vt6655_cleanup_module(void)
3580{
3581
3582
3583#ifdef CONFIG_PM
3584    unregister_reboot_notifier(&device_notifier);
3585#endif
3586    pci_unregister_driver(&device_driver);
3587
3588}
3589
3590module_init(vt6655_init_module);
3591module_exit(vt6655_cleanup_module);
3592
3593
3594#ifdef CONFIG_PM
3595static int
3596device_notify_reboot(struct notifier_block *nb, unsigned long event, void *p)
3597{
3598    struct pci_dev *pdev = NULL;
3599    switch(event) {
3600    case SYS_DOWN:
3601    case SYS_HALT:
3602    case SYS_POWER_OFF:
3603        for_each_pci_dev(pdev) {
3604            if(pci_dev_driver(pdev) == &device_driver) {
3605                if (pci_get_drvdata(pdev))
3606                    viawget_suspend(pdev, PMSG_HIBERNATE);
3607            }
3608        }
3609    }
3610    return NOTIFY_DONE;
3611}
3612
3613static int
3614viawget_suspend(struct pci_dev *pcid, pm_message_t state)
3615{
3616    int power_status;   // to silence the compiler
3617
3618    PSDevice pDevice=pci_get_drvdata(pcid);
3619    PSMgmtObject  pMgmt = pDevice->pMgmt;
3620
3621    netif_stop_queue(pDevice->dev);
3622    spin_lock_irq(&pDevice->lock);
3623    pci_save_state(pcid);
3624    del_timer(&pDevice->sTimerCommand);
3625    del_timer(&pMgmt->sTimerSecondCallback);
3626    pDevice->cbFreeCmdQueue = CMD_Q_SIZE;
3627    pDevice->uCmdDequeueIdx = 0;
3628    pDevice->uCmdEnqueueIdx = 0;
3629    pDevice->bCmdRunning = false;
3630    MACbShutdown(pDevice->PortOffset);
3631    MACvSaveContext(pDevice->PortOffset, pDevice->abyMacContext);
3632    pDevice->bLinkPass = false;
3633    memset(pMgmt->abyCurrBSSID, 0, 6);
3634    pMgmt->eCurrState = WMAC_STATE_IDLE;
3635    pci_disable_device(pcid);
3636    power_status = pci_set_power_state(pcid, pci_choose_state(pcid, state));
3637    spin_unlock_irq(&pDevice->lock);
3638    return 0;
3639}
3640
3641static int
3642viawget_resume(struct pci_dev *pcid)
3643{
3644    PSDevice  pDevice=pci_get_drvdata(pcid);
3645    PSMgmtObject  pMgmt = pDevice->pMgmt;
3646    int power_status;   // to silence the compiler
3647
3648
3649    power_status = pci_set_power_state(pcid, 0);
3650    power_status = pci_enable_wake(pcid, 0, 0);
3651    pci_restore_state(pcid);
3652    if (netif_running(pDevice->dev)) {
3653        spin_lock_irq(&pDevice->lock);
3654        MACvRestoreContext(pDevice->PortOffset, pDevice->abyMacContext);
3655        device_init_registers(pDevice, DEVICE_INIT_DXPL);
3656        if (pMgmt->sNodeDBTable[0].bActive == true) { // Assoc with BSS
3657            pMgmt->sNodeDBTable[0].bActive = false;
3658            pDevice->bLinkPass = false;
3659            if(pMgmt->eCurrMode == WMAC_MODE_IBSS_STA) {
3660                // In Adhoc, BSS state set back to started.
3661                pMgmt->eCurrState = WMAC_STATE_STARTED;
3662           }
3663            else {
3664                pMgmt->eCurrMode = WMAC_MODE_STANDBY;
3665                pMgmt->eCurrState = WMAC_STATE_IDLE;
3666            }
3667        }
3668        init_timer(&pMgmt->sTimerSecondCallback);
3669        init_timer(&pDevice->sTimerCommand);
3670        MACvIntEnable(pDevice->PortOffset, IMR_MASK_VALUE);
3671        BSSvClearBSSList((void *)pDevice, pDevice->bLinkPass);
3672        bScheduleCommand((void *) pDevice, WLAN_CMD_BSSID_SCAN, NULL);
3673        bScheduleCommand((void *) pDevice, WLAN_CMD_SSID, NULL);
3674        spin_unlock_irq(&pDevice->lock);
3675    }
3676    return 0;
3677}
3678
3679#endif
3680
3681
3682
3683
3684
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.