GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / staging / vt6655 / device_main.c
bloba9b227bda401ab8196a32ee0b21d4c084a3c78b0
1 /*
2 * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
3 * All rights reserved.
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.
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.
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.
19 * File: device_main.c
21 * Purpose: driver entry for initial, open, close, tx and rx.
23 * Author: Lyndon Chen
25 * Date: Jan 8, 2003
27 * Functions:
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
59 * Revision History:
61 #undef __NO_VERSION__
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>
90 //#define DEBUG
91 /*--------------------- Static Definitions -------------------------*/
92 //static int msglevel =MSG_LEVEL_DEBUG;
93 static int msglevel = MSG_LEVEL_INFO;
95 //#define PLICE_DEBUG
97 // Define module options
99 MODULE_AUTHOR("VIA Networking Technologies, Inc., <lyndonchen@vntek.com.tw>");
100 MODULE_LICENSE("GPL");
101 MODULE_DESCRIPTION("VIA Networking Solomon-A/B/G Wireless LAN Adapter Driver");
103 //PLICE_DEBUG ->
104 static int mlme_kill;
105 //static struct task_struct * mlme_task;
106 //PLICE_DEBUG <-
108 #define DEVICE_PARAM(N,D)
110 static const int N[MAX_UINTS]=OPTION_DEFAULT;\
111 MODULE_PARM(N, "1-" __MODULE_STRING(MAX_UINTS) "i");\
112 MODULE_PARM_DESC(N, D);
115 #define RX_DESC_MIN0 16
116 #define RX_DESC_MAX0 128
117 #define RX_DESC_DEF0 32
118 DEVICE_PARAM(RxDescriptors0,"Number of receive descriptors0");
120 #define RX_DESC_MIN1 16
121 #define RX_DESC_MAX1 128
122 #define RX_DESC_DEF1 32
123 DEVICE_PARAM(RxDescriptors1,"Number of receive descriptors1");
125 #define TX_DESC_MIN0 16
126 #define TX_DESC_MAX0 128
127 #define TX_DESC_DEF0 32
128 DEVICE_PARAM(TxDescriptors0,"Number of transmit descriptors0");
130 #define TX_DESC_MIN1 16
131 #define TX_DESC_MAX1 128
132 #define TX_DESC_DEF1 64
133 DEVICE_PARAM(TxDescriptors1,"Number of transmit descriptors1");
136 #define IP_ALIG_DEF 0
137 /* IP_byte_align[] is used for IP header unsigned long byte aligned
138 0: indicate the IP header won't be unsigned long byte aligned.(Default) .
139 1: indicate the IP header will be unsigned long byte aligned.
140 In some enviroment, the IP header should be unsigned long byte aligned,
141 or the packet will be droped when we receive it. (eg: IPVS)
143 DEVICE_PARAM(IP_byte_align,"Enable IP header dword aligned");
146 #define INT_WORKS_DEF 20
147 #define INT_WORKS_MIN 10
148 #define INT_WORKS_MAX 64
150 DEVICE_PARAM(int_works,"Number of packets per interrupt services");
152 #define CHANNEL_MIN 1
153 #define CHANNEL_MAX 14
154 #define CHANNEL_DEF 6
156 DEVICE_PARAM(Channel, "Channel number");
159 /* PreambleType[] is the preamble length used for transmit.
160 0: indicate allows long preamble type
161 1: indicate allows short preamble type
164 #define PREAMBLE_TYPE_DEF 1
166 DEVICE_PARAM(PreambleType, "Preamble Type");
169 #define RTS_THRESH_MIN 512
170 #define RTS_THRESH_MAX 2347
171 #define RTS_THRESH_DEF 2347
173 DEVICE_PARAM(RTSThreshold, "RTS threshold");
176 #define FRAG_THRESH_MIN 256
177 #define FRAG_THRESH_MAX 2346
178 #define FRAG_THRESH_DEF 2346
180 DEVICE_PARAM(FragThreshold, "Fragmentation threshold");
183 #define DATA_RATE_MIN 0
184 #define DATA_RATE_MAX 13
185 #define DATA_RATE_DEF 13
186 /* datarate[] index
187 0: indicate 1 Mbps 0x02
188 1: indicate 2 Mbps 0x04
189 2: indicate 5.5 Mbps 0x0B
190 3: indicate 11 Mbps 0x16
191 4: indicate 6 Mbps 0x0c
192 5: indicate 9 Mbps 0x12
193 6: indicate 12 Mbps 0x18
194 7: indicate 18 Mbps 0x24
195 8: indicate 24 Mbps 0x30
196 9: indicate 36 Mbps 0x48
197 10: indicate 48 Mbps 0x60
198 11: indicate 54 Mbps 0x6c
199 12: indicate 72 Mbps 0x90
200 13: indicate auto rate
203 DEVICE_PARAM(ConnectionRate, "Connection data rate");
205 #define OP_MODE_DEF 0
207 DEVICE_PARAM(OPMode, "Infrastruct, adhoc, AP mode ");
209 /* OpMode[] is used for transmit.
210 0: indicate infrastruct mode used
211 1: indicate adhoc mode used
212 2: indicate AP mode used
216 /* PSMode[]
217 0: indicate disable power saving mode
218 1: indicate enable power saving mode
221 #define PS_MODE_DEF 0
223 DEVICE_PARAM(PSMode, "Power saving mode");
226 #define SHORT_RETRY_MIN 0
227 #define SHORT_RETRY_MAX 31
228 #define SHORT_RETRY_DEF 8
231 DEVICE_PARAM(ShortRetryLimit, "Short frame retry limits");
233 #define LONG_RETRY_MIN 0
234 #define LONG_RETRY_MAX 15
235 #define LONG_RETRY_DEF 4
238 DEVICE_PARAM(LongRetryLimit, "long frame retry limits");
241 /* BasebandType[] baseband type selected
242 0: indicate 802.11a type
243 1: indicate 802.11b type
244 2: indicate 802.11g type
246 #define BBP_TYPE_MIN 0
247 #define BBP_TYPE_MAX 2
248 #define BBP_TYPE_DEF 2
250 DEVICE_PARAM(BasebandType, "baseband type");
254 /* 80211hEnable[]
255 0: indicate disable 802.11h
256 1: indicate enable 802.11h
259 #define X80211h_MODE_DEF 0
261 DEVICE_PARAM(b80211hEnable, "802.11h mode");
263 /* 80211hEnable[]
264 0: indicate disable 802.11h
265 1: indicate enable 802.11h
268 #define DIVERSITY_ANT_DEF 0
270 DEVICE_PARAM(bDiversityANTEnable, "ANT diversity mode");
274 // Static vars definitions
278 static int device_nics =0;
279 static PSDevice pDevice_Infos =NULL;
280 static struct net_device *root_device_dev = NULL;
282 static CHIP_INFO chip_info_table[]= {
283 { VT3253, "VIA Networking Solomon-A/B/G Wireless LAN Adapter ",
284 256, 1, DEVICE_FLAGS_IP_ALIGN|DEVICE_FLAGS_TX_ALIGN },
285 {0,NULL}
288 DEFINE_PCI_DEVICE_TABLE(vt6655_pci_id_table) = {
289 { PCI_VDEVICE(VIA, 0x3253), (kernel_ulong_t)chip_info_table},
290 { 0, }
293 /*--------------------- Static Functions --------------------------*/
296 static int vt6655_probe(struct pci_dev *pcid, const struct pci_device_id *ent);
297 static bool vt6655_init_info(struct pci_dev* pcid, PSDevice* ppDevice, PCHIP_INFO);
298 static void device_free_info(PSDevice pDevice);
299 static bool device_get_pci_info(PSDevice, struct pci_dev* pcid);
300 static void device_print_info(PSDevice pDevice);
301 static struct net_device_stats *device_get_stats(struct net_device *dev);
302 static void device_init_diversity_timer(PSDevice pDevice);
303 static int device_open(struct net_device *dev);
304 static int device_xmit(struct sk_buff *skb, struct net_device *dev);
305 static irqreturn_t device_intr(int irq, void*dev_instance);
306 static void device_set_multi(struct net_device *dev);
307 static int device_close(struct net_device *dev);
308 static int device_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
310 #ifdef CONFIG_PM
311 static int device_notify_reboot(struct notifier_block *, unsigned long event, void *ptr);
312 static int viawget_suspend(struct pci_dev *pcid, pm_message_t state);
313 static int viawget_resume(struct pci_dev *pcid);
314 struct notifier_block device_notifier = {
315 notifier_call: device_notify_reboot,
316 next: NULL,
317 priority: 0
319 #endif
322 static void device_init_rd0_ring(PSDevice pDevice);
323 static void device_init_rd1_ring(PSDevice pDevice);
324 static void device_init_defrag_cb(PSDevice pDevice);
325 static void device_init_td0_ring(PSDevice pDevice);
326 static void device_init_td1_ring(PSDevice pDevice);
328 static int device_dma0_tx_80211(struct sk_buff *skb, struct net_device *dev);
329 //2008-0714<Add>by Mike Liu
330 static bool device_release_WPADEV(PSDevice pDevice);
332 static int ethtool_ioctl(struct net_device *dev, void *useraddr);
333 static int device_rx_srv(PSDevice pDevice, unsigned int uIdx);
334 static int device_tx_srv(PSDevice pDevice, unsigned int uIdx);
335 static bool device_alloc_rx_buf(PSDevice pDevice, PSRxDesc pDesc);
336 static void device_init_registers(PSDevice pDevice, DEVICE_INIT_TYPE InitType);
337 static void device_free_tx_buf(PSDevice pDevice, PSTxDesc pDesc);
338 static void device_free_td0_ring(PSDevice pDevice);
339 static void device_free_td1_ring(PSDevice pDevice);
340 static void device_free_rd0_ring(PSDevice pDevice);
341 static void device_free_rd1_ring(PSDevice pDevice);
342 static void device_free_rings(PSDevice pDevice);
343 static void device_free_frag_buf(PSDevice pDevice);
344 static int Config_FileGetParameter(unsigned char *string,
345 unsigned char *dest, unsigned char *source);
348 /*--------------------- Export Variables --------------------------*/
350 /*--------------------- Export Functions --------------------------*/
354 static char* get_chip_name(int chip_id) {
355 int i;
356 for (i=0;chip_info_table[i].name!=NULL;i++)
357 if (chip_info_table[i].chip_id==chip_id)
358 break;
359 return chip_info_table[i].name;
362 static void __devexit vt6655_remove(struct pci_dev *pcid)
364 PSDevice pDevice=pci_get_drvdata(pcid);
366 if (pDevice==NULL)
367 return;
368 device_free_info(pDevice);
373 static void
374 device_set_int_opt(int *opt, int val, int min, int max, int def,char* name,char* devname) {
375 if (val==-1)
376 *opt=def;
377 else if (val<min || val>max) {
378 DBG_PRT(MSG_LEVEL_INFO, KERN_NOTICE "%s: the value of parameter %s is invalid, the valid range is (%d-%d)\n" ,
379 devname,name, min,max);
380 *opt=def;
381 } else {
382 DBG_PRT(MSG_LEVEL_INFO, KERN_INFO "%s: set value of parameter %s to %d\n",
383 devname, name, val);
384 *opt=val;
388 static void
389 device_set_bool_opt(unsigned int *opt, int val,bool def,u32 flag, char* name,char* devname) {
390 (*opt)&=(~flag);
391 if (val==-1)
392 *opt|=(def ? flag : 0);
393 else if (val<0 || val>1) {
394 DBG_PRT(MSG_LEVEL_INFO, KERN_NOTICE
395 "%s: the value of parameter %s is invalid, the valid range is (0-1)\n",devname,name);
396 *opt|=(def ? flag : 0);
397 } else {
398 DBG_PRT(MSG_LEVEL_INFO, KERN_NOTICE "%s: set parameter %s to %s\n",
399 devname,name , val ? "true" : "false");
400 *opt|=(val ? flag : 0);
404 static void
405 device_get_options(PSDevice pDevice, int index, char* devname) {
407 POPTIONS pOpts = &(pDevice->sOpts);
408 pOpts->nRxDescs0=RX_DESC_DEF0;
409 pOpts->nRxDescs1=RX_DESC_DEF1;
410 pOpts->nTxDescs[0]=TX_DESC_DEF0;
411 pOpts->nTxDescs[1]=TX_DESC_DEF1;
412 pOpts->flags|=DEVICE_FLAGS_IP_ALIGN;
413 pOpts->int_works=INT_WORKS_DEF;
414 pOpts->rts_thresh=RTS_THRESH_DEF;
415 pOpts->frag_thresh=FRAG_THRESH_DEF;
416 pOpts->data_rate=DATA_RATE_DEF;
417 pOpts->channel_num=CHANNEL_DEF;
419 pOpts->flags|=DEVICE_FLAGS_PREAMBLE_TYPE;
420 pOpts->flags|=DEVICE_FLAGS_OP_MODE;
421 //pOpts->flags|=DEVICE_FLAGS_PS_MODE;
422 pOpts->short_retry=SHORT_RETRY_DEF;
423 pOpts->long_retry=LONG_RETRY_DEF;
424 pOpts->bbp_type=BBP_TYPE_DEF;
425 pOpts->flags|=DEVICE_FLAGS_80211h_MODE;
426 pOpts->flags|=DEVICE_FLAGS_DiversityANT;
431 static void
432 device_set_options(PSDevice pDevice) {
434 unsigned char abyBroadcastAddr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
435 unsigned char abySNAP_RFC1042[ETH_ALEN] = {0xAA, 0xAA, 0x03, 0x00, 0x00, 0x00};
436 unsigned char abySNAP_Bridgetunnel[ETH_ALEN] = {0xAA, 0xAA, 0x03, 0x00, 0x00, 0xF8};
439 memcpy(pDevice->abyBroadcastAddr, abyBroadcastAddr, ETH_ALEN);
440 memcpy(pDevice->abySNAP_RFC1042, abySNAP_RFC1042, ETH_ALEN);
441 memcpy(pDevice->abySNAP_Bridgetunnel, abySNAP_Bridgetunnel, ETH_ALEN);
443 pDevice->uChannel = pDevice->sOpts.channel_num;
444 pDevice->wRTSThreshold = pDevice->sOpts.rts_thresh;
445 pDevice->wFragmentationThreshold = pDevice->sOpts.frag_thresh;
446 pDevice->byShortRetryLimit = pDevice->sOpts.short_retry;
447 pDevice->byLongRetryLimit = pDevice->sOpts.long_retry;
448 pDevice->wMaxTransmitMSDULifetime = DEFAULT_MSDU_LIFETIME;
449 pDevice->byShortPreamble = (pDevice->sOpts.flags & DEVICE_FLAGS_PREAMBLE_TYPE) ? 1 : 0;
450 pDevice->byOpMode = (pDevice->sOpts.flags & DEVICE_FLAGS_OP_MODE) ? 1 : 0;
451 pDevice->ePSMode = (pDevice->sOpts.flags & DEVICE_FLAGS_PS_MODE) ? 1 : 0;
452 pDevice->b11hEnable = (pDevice->sOpts.flags & DEVICE_FLAGS_80211h_MODE) ? 1 : 0;
453 pDevice->bDiversityRegCtlON = (pDevice->sOpts.flags & DEVICE_FLAGS_DiversityANT) ? 1 : 0;
454 pDevice->uConnectionRate = pDevice->sOpts.data_rate;
455 if (pDevice->uConnectionRate < RATE_AUTO) pDevice->bFixRate = true;
456 pDevice->byBBType = pDevice->sOpts.bbp_type;
457 pDevice->byPacketType = pDevice->byBBType;
459 //PLICE_DEBUG->
460 pDevice->byAutoFBCtrl = AUTO_FB_0;
461 //pDevice->byAutoFBCtrl = AUTO_FB_1;
462 //PLICE_DEBUG<-
463 pDevice->bUpdateBBVGA = true;
464 pDevice->byFOETuning = 0;
465 pDevice->wCTSDuration = 0;
466 pDevice->byPreambleType = 0;
469 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" uChannel= %d\n",(int)pDevice->uChannel);
470 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" byOpMode= %d\n",(int)pDevice->byOpMode);
471 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" ePSMode= %d\n",(int)pDevice->ePSMode);
472 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" wRTSThreshold= %d\n",(int)pDevice->wRTSThreshold);
473 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" byShortRetryLimit= %d\n",(int)pDevice->byShortRetryLimit);
474 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" byLongRetryLimit= %d\n",(int)pDevice->byLongRetryLimit);
475 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" byPreambleType= %d\n",(int)pDevice->byPreambleType);
476 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" byShortPreamble= %d\n",(int)pDevice->byShortPreamble);
477 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" uConnectionRate= %d\n",(int)pDevice->uConnectionRate);
478 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" byBBType= %d\n",(int)pDevice->byBBType);
479 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" pDevice->b11hEnable= %d\n",(int)pDevice->b11hEnable);
480 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" pDevice->bDiversityRegCtlON= %d\n",(int)pDevice->bDiversityRegCtlON);
483 static void s_vCompleteCurrentMeasure (PSDevice pDevice, unsigned char byResult)
485 unsigned int ii;
486 unsigned long dwDuration = 0;
487 unsigned char byRPI0 = 0;
489 for(ii=1;ii<8;ii++) {
490 pDevice->dwRPIs[ii] *= 255;
491 dwDuration |= *((unsigned short *) (pDevice->pCurrMeasureEID->sReq.abyDuration));
492 dwDuration <<= 10;
493 pDevice->dwRPIs[ii] /= dwDuration;
494 pDevice->abyRPIs[ii] = (unsigned char) pDevice->dwRPIs[ii];
495 byRPI0 += pDevice->abyRPIs[ii];
497 pDevice->abyRPIs[0] = (0xFF - byRPI0);
499 if (pDevice->uNumOfMeasureEIDs == 0) {
500 VNTWIFIbMeasureReport( pDevice->pMgmt,
501 true,
502 pDevice->pCurrMeasureEID,
503 byResult,
504 pDevice->byBasicMap,
505 pDevice->byCCAFraction,
506 pDevice->abyRPIs
508 } else {
509 VNTWIFIbMeasureReport( pDevice->pMgmt,
510 false,
511 pDevice->pCurrMeasureEID,
512 byResult,
513 pDevice->byBasicMap,
514 pDevice->byCCAFraction,
515 pDevice->abyRPIs
517 CARDbStartMeasure (pDevice, pDevice->pCurrMeasureEID++, pDevice->uNumOfMeasureEIDs);
525 // Initialiation of MAC & BBP registers
528 static void device_init_registers(PSDevice pDevice, DEVICE_INIT_TYPE InitType)
530 unsigned int ii;
531 unsigned char byValue;
532 unsigned char byValue1;
533 unsigned char byCCKPwrdBm = 0;
534 unsigned char byOFDMPwrdBm = 0;
535 int zonetype=0;
536 PSMgmtObject pMgmt = &(pDevice->sMgmtObj);
537 MACbShutdown(pDevice->PortOffset);
538 BBvSoftwareReset(pDevice->PortOffset);
540 if ((InitType == DEVICE_INIT_COLD) ||
541 (InitType == DEVICE_INIT_DXPL)) {
542 // Do MACbSoftwareReset in MACvInitialize
543 MACbSoftwareReset(pDevice->PortOffset);
544 // force CCK
545 pDevice->bCCK = true;
546 pDevice->bAES = false;
547 pDevice->bProtectMode = false; //Only used in 11g type, sync with ERP IE
548 pDevice->bNonERPPresent = false;
549 pDevice->bBarkerPreambleMd = false;
550 pDevice->wCurrentRate = RATE_1M;
551 pDevice->byTopOFDMBasicRate = RATE_24M;
552 pDevice->byTopCCKBasicRate = RATE_1M;
554 pDevice->byRevId = 0; //Target to IF pin while programming to RF chip.
556 // init MAC
557 MACvInitialize(pDevice->PortOffset);
559 // Get Local ID
560 VNSvInPortB(pDevice->PortOffset + MAC_REG_LOCALID, &(pDevice->byLocalID));
562 spin_lock_irq(&pDevice->lock);
563 SROMvReadAllContents(pDevice->PortOffset,pDevice->abyEEPROM);
565 spin_unlock_irq(&pDevice->lock);
567 // Get Channel range
569 pDevice->byMinChannel = 1;
570 pDevice->byMaxChannel = CB_MAX_CHANNEL;
572 // Get Antena
573 byValue = SROMbyReadEmbedded(pDevice->PortOffset, EEP_OFS_ANTENNA);
574 if (byValue & EEP_ANTINV)
575 pDevice->bTxRxAntInv = true;
576 else
577 pDevice->bTxRxAntInv = false;
578 #ifdef PLICE_DEBUG
579 //printk("init_register:TxRxAntInv is %d,byValue is %d\n",pDevice->bTxRxAntInv,byValue);
580 #endif
582 byValue &= (EEP_ANTENNA_AUX | EEP_ANTENNA_MAIN);
583 if (byValue == 0) // if not set default is All
584 byValue = (EEP_ANTENNA_AUX | EEP_ANTENNA_MAIN);
585 #ifdef PLICE_DEBUG
586 //printk("init_register:byValue is %d\n",byValue);
587 #endif
588 pDevice->ulDiversityNValue = 100*260;//100*SROMbyReadEmbedded(pDevice->PortOffset, 0x51);
589 pDevice->ulDiversityMValue = 100*16;//SROMbyReadEmbedded(pDevice->PortOffset, 0x52);
590 pDevice->byTMax = 1;//SROMbyReadEmbedded(pDevice->PortOffset, 0x53);
591 pDevice->byTMax2 = 4;//SROMbyReadEmbedded(pDevice->PortOffset, 0x54);
592 pDevice->ulSQ3TH = 0;//(unsigned long) SROMbyReadEmbedded(pDevice->PortOffset, 0x55);
593 pDevice->byTMax3 = 64;//SROMbyReadEmbedded(pDevice->PortOffset, 0x56);
595 if (byValue == (EEP_ANTENNA_AUX | EEP_ANTENNA_MAIN)) {
596 pDevice->byAntennaCount = 2;
597 pDevice->byTxAntennaMode = ANT_B;
598 pDevice->dwTxAntennaSel = 1;
599 pDevice->dwRxAntennaSel = 1;
600 if (pDevice->bTxRxAntInv == true)
601 pDevice->byRxAntennaMode = ANT_A;
602 else
603 pDevice->byRxAntennaMode = ANT_B;
604 // chester for antenna
605 byValue1 = SROMbyReadEmbedded(pDevice->PortOffset, EEP_OFS_ANTENNA);
606 // if (pDevice->bDiversityRegCtlON)
607 if((byValue1&0x08)==0)
608 pDevice->bDiversityEnable = false;//SROMbyReadEmbedded(pDevice->PortOffset, 0x50);
609 else
610 pDevice->bDiversityEnable = true;
611 #ifdef PLICE_DEBUG
612 //printk("aux |main antenna: RxAntennaMode is %d\n",pDevice->byRxAntennaMode);
613 #endif
614 } else {
615 pDevice->bDiversityEnable = false;
616 pDevice->byAntennaCount = 1;
617 pDevice->dwTxAntennaSel = 0;
618 pDevice->dwRxAntennaSel = 0;
619 if (byValue & EEP_ANTENNA_AUX) {
620 pDevice->byTxAntennaMode = ANT_A;
621 if (pDevice->bTxRxAntInv == true)
622 pDevice->byRxAntennaMode = ANT_B;
623 else
624 pDevice->byRxAntennaMode = ANT_A;
625 } else {
626 pDevice->byTxAntennaMode = ANT_B;
627 if (pDevice->bTxRxAntInv == true)
628 pDevice->byRxAntennaMode = ANT_A;
629 else
630 pDevice->byRxAntennaMode = ANT_B;
633 #ifdef PLICE_DEBUG
634 //printk("init registers: TxAntennaMode is %d\n",pDevice->byTxAntennaMode);
635 #endif
636 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "bDiversityEnable=[%d],NValue=[%d],MValue=[%d],TMax=[%d],TMax2=[%d]\n",
637 pDevice->bDiversityEnable,(int)pDevice->ulDiversityNValue,(int)pDevice->ulDiversityMValue,pDevice->byTMax,pDevice->byTMax2);
639 //#ifdef ZoneType_DefaultSetting
640 //2008-8-4 <add> by chester
641 //zonetype initial
642 pDevice->byOriginalZonetype = pDevice->abyEEPROM[EEP_OFS_ZONETYPE];
643 zonetype = Config_FileOperation(pDevice,false,NULL);
644 if (zonetype >= 0) { //read zonetype file ok!
645 if ((zonetype == 0)&&
646 (pDevice->abyEEPROM[EEP_OFS_ZONETYPE] !=0x00)){ //for USA
647 pDevice->abyEEPROM[EEP_OFS_ZONETYPE] = 0;
648 pDevice->abyEEPROM[EEP_OFS_MAXCHANNEL] = 0x0B;
649 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Init Zone Type :USA\n");
651 else if((zonetype == 1)&&
652 (pDevice->abyEEPROM[EEP_OFS_ZONETYPE]!=0x01)){ //for Japan
653 pDevice->abyEEPROM[EEP_OFS_ZONETYPE] = 0x01;
654 pDevice->abyEEPROM[EEP_OFS_MAXCHANNEL] = 0x0D;
656 else if((zonetype == 2)&&
657 (pDevice->abyEEPROM[EEP_OFS_ZONETYPE]!=0x02)){ //for Europe
658 pDevice->abyEEPROM[EEP_OFS_ZONETYPE] = 0x02;
659 pDevice->abyEEPROM[EEP_OFS_MAXCHANNEL] = 0x0D;
660 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Init Zone Type :Europe\n");
663 else
665 if(zonetype!=pDevice->abyEEPROM[EEP_OFS_ZONETYPE])
666 printk("zonetype in file[%02x] mismatch with in EEPROM[%02x]\n",zonetype,pDevice->abyEEPROM[EEP_OFS_ZONETYPE]);
667 else
668 printk("Read Zonetype file success,use default zonetype setting[%02x]\n",zonetype);
671 else
672 printk("Read Zonetype file fail,use default zonetype setting[%02x]\n",SROMbyReadEmbedded(pDevice->PortOffset, EEP_OFS_ZONETYPE));
674 // Get RFType
675 pDevice->byRFType = SROMbyReadEmbedded(pDevice->PortOffset, EEP_OFS_RFTYPE);
677 if ((pDevice->byRFType & RF_EMU) != 0) {
678 // force change RevID for VT3253 emu
679 pDevice->byRevId = 0x80;
682 pDevice->byRFType &= RF_MASK;
683 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "pDevice->byRFType = %x\n", pDevice->byRFType);
685 if (pDevice->bZoneRegExist == false) {
686 pDevice->byZoneType = pDevice->abyEEPROM[EEP_OFS_ZONETYPE];
688 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "pDevice->byZoneType = %x\n", pDevice->byZoneType);
690 //Init RF module
691 RFbInit(pDevice);
693 //Get Desire Power Value
694 pDevice->byCurPwr = 0xFF;
695 pDevice->byCCKPwr = SROMbyReadEmbedded(pDevice->PortOffset, EEP_OFS_PWR_CCK);
696 pDevice->byOFDMPwrG = SROMbyReadEmbedded(pDevice->PortOffset, EEP_OFS_PWR_OFDMG);
697 //byCCKPwrdBm = SROMbyReadEmbedded(pDevice->PortOffset, EEP_OFS_CCK_PWR_dBm);
699 //byOFDMPwrdBm = SROMbyReadEmbedded(pDevice->PortOffset, EEP_OFS_OFDM_PWR_dBm);
700 //printk("CCKPwrdBm is 0x%x,byOFDMPwrdBm is 0x%x\n",byCCKPwrdBm,byOFDMPwrdBm);
701 // Load power Table
704 for (ii=0;ii<CB_MAX_CHANNEL_24G;ii++) {
705 pDevice->abyCCKPwrTbl[ii+1] = SROMbyReadEmbedded(pDevice->PortOffset, (unsigned char)(ii + EEP_OFS_CCK_PWR_TBL));
706 if (pDevice->abyCCKPwrTbl[ii+1] == 0) {
707 pDevice->abyCCKPwrTbl[ii+1] = pDevice->byCCKPwr;
709 pDevice->abyOFDMPwrTbl[ii+1] = SROMbyReadEmbedded(pDevice->PortOffset, (unsigned char)(ii + EEP_OFS_OFDM_PWR_TBL));
710 if (pDevice->abyOFDMPwrTbl[ii+1] == 0) {
711 pDevice->abyOFDMPwrTbl[ii+1] = pDevice->byOFDMPwrG;
713 pDevice->abyCCKDefaultPwr[ii+1] = byCCKPwrdBm;
714 pDevice->abyOFDMDefaultPwr[ii+1] = byOFDMPwrdBm;
716 //2008-8-4 <add> by chester
717 //recover 12,13 ,14channel for EUROPE by 11 channel
718 if(((pDevice->abyEEPROM[EEP_OFS_ZONETYPE] == ZoneType_Japan) ||
719 (pDevice->abyEEPROM[EEP_OFS_ZONETYPE] == ZoneType_Europe))&&
720 (pDevice->byOriginalZonetype == ZoneType_USA)) {
721 for(ii=11;ii<14;ii++) {
722 pDevice->abyCCKPwrTbl[ii] = pDevice->abyCCKPwrTbl[10];
723 pDevice->abyOFDMPwrTbl[ii] = pDevice->abyOFDMPwrTbl[10];
729 // Load OFDM A Power Table
730 for (ii=0;ii<CB_MAX_CHANNEL_5G;ii++) { //RobertYu:20041224, bug using CB_MAX_CHANNEL
731 pDevice->abyOFDMPwrTbl[ii+CB_MAX_CHANNEL_24G+1] = SROMbyReadEmbedded(pDevice->PortOffset, (unsigned char)(ii + EEP_OFS_OFDMA_PWR_TBL));
732 pDevice->abyOFDMDefaultPwr[ii+CB_MAX_CHANNEL_24G+1] = SROMbyReadEmbedded(pDevice->PortOffset, (unsigned char)(ii + EEP_OFS_OFDMA_PWR_dBm));
734 init_channel_table((void *)pDevice);
737 if (pDevice->byLocalID > REV_ID_VT3253_B1) {
738 MACvSelectPage1(pDevice->PortOffset);
739 VNSvOutPortB(pDevice->PortOffset + MAC_REG_MSRCTL + 1, (MSRCTL1_TXPWR | MSRCTL1_CSAPAREN));
740 MACvSelectPage0(pDevice->PortOffset);
744 // use relative tx timeout and 802.11i D4
745 MACvWordRegBitsOn(pDevice->PortOffset, MAC_REG_CFG, (CFG_TKIPOPT | CFG_NOTXTIMEOUT));
747 // set performance parameter by registry
748 MACvSetShortRetryLimit(pDevice->PortOffset, pDevice->byShortRetryLimit);
749 MACvSetLongRetryLimit(pDevice->PortOffset, pDevice->byLongRetryLimit);
751 // reset TSF counter
752 VNSvOutPortB(pDevice->PortOffset + MAC_REG_TFTCTL, TFTCTL_TSFCNTRST);
753 // enable TSF counter
754 VNSvOutPortB(pDevice->PortOffset + MAC_REG_TFTCTL, TFTCTL_TSFCNTREN);
756 // initialize BBP registers
757 BBbVT3253Init(pDevice);
759 if (pDevice->bUpdateBBVGA) {
760 pDevice->byBBVGACurrent = pDevice->abyBBVGA[0];
761 pDevice->byBBVGANew = pDevice->byBBVGACurrent;
762 BBvSetVGAGainOffset(pDevice, pDevice->abyBBVGA[0]);
764 #ifdef PLICE_DEBUG
765 //printk("init registers:RxAntennaMode is %x,TxAntennaMode is %x\n",pDevice->byRxAntennaMode,pDevice->byTxAntennaMode);
766 #endif
767 BBvSetRxAntennaMode(pDevice->PortOffset, pDevice->byRxAntennaMode);
768 BBvSetTxAntennaMode(pDevice->PortOffset, pDevice->byTxAntennaMode);
770 pDevice->byCurrentCh = 0;
772 //pDevice->NetworkType = Ndis802_11Automode;
773 // Set BB and packet type at the same time.
774 // Set Short Slot Time, xIFS, and RSPINF.
775 if (pDevice->uConnectionRate == RATE_AUTO) {
776 pDevice->wCurrentRate = RATE_54M;
777 } else {
778 pDevice->wCurrentRate = (unsigned short)pDevice->uConnectionRate;
781 // default G Mode
782 VNTWIFIbConfigPhyMode(pDevice->pMgmt, PHY_TYPE_11G);
783 VNTWIFIbConfigPhyMode(pDevice->pMgmt, PHY_TYPE_AUTO);
785 pDevice->bRadioOff = false;
787 pDevice->byRadioCtl = SROMbyReadEmbedded(pDevice->PortOffset, EEP_OFS_RADIOCTL);
788 pDevice->bHWRadioOff = false;
790 if (pDevice->byRadioCtl & EEP_RADIOCTL_ENABLE) {
791 // Get GPIO
792 MACvGPIOIn(pDevice->PortOffset, &pDevice->byGPIO);
793 //2008-4-14 <add> by chester for led issue
794 #ifdef FOR_LED_ON_NOTEBOOK
795 if (pDevice->byGPIO & GPIO0_DATA){pDevice->bHWRadioOff = true;}
796 if ( !(pDevice->byGPIO & GPIO0_DATA)){pDevice->bHWRadioOff = false;}
799 if ( (pDevice->bRadioControlOff == true)) {
800 CARDbRadioPowerOff(pDevice);
802 else CARDbRadioPowerOn(pDevice);
803 #else
804 if (((pDevice->byGPIO & GPIO0_DATA) && !(pDevice->byRadioCtl & EEP_RADIOCTL_INV)) ||
805 ( !(pDevice->byGPIO & GPIO0_DATA) && (pDevice->byRadioCtl & EEP_RADIOCTL_INV))) {
806 pDevice->bHWRadioOff = true;
809 if ((pDevice->bHWRadioOff == true) || (pDevice->bRadioControlOff == true)) {
810 CARDbRadioPowerOff(pDevice);
813 #endif
815 pMgmt->eScanType = WMAC_SCAN_PASSIVE;
816 // get Permanent network address
817 SROMvReadEtherAddress(pDevice->PortOffset, pDevice->abyCurrentNetAddr);
818 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Network address = %02x-%02x-%02x=%02x-%02x-%02x\n",
819 pDevice->abyCurrentNetAddr[0],
820 pDevice->abyCurrentNetAddr[1],
821 pDevice->abyCurrentNetAddr[2],
822 pDevice->abyCurrentNetAddr[3],
823 pDevice->abyCurrentNetAddr[4],
824 pDevice->abyCurrentNetAddr[5]);
827 // reset Tx pointer
828 CARDvSafeResetRx(pDevice);
829 // reset Rx pointer
830 CARDvSafeResetTx(pDevice);
832 if (pDevice->byLocalID <= REV_ID_VT3253_A1) {
833 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_RCR, RCR_WPAERR);
836 pDevice->eEncryptionStatus = Ndis802_11EncryptionDisabled;
838 // Turn On Rx DMA
839 MACvReceive0(pDevice->PortOffset);
840 MACvReceive1(pDevice->PortOffset);
842 // start the adapter
843 MACvStart(pDevice->PortOffset);
845 netif_stop_queue(pDevice->dev);
852 static void device_init_diversity_timer(PSDevice pDevice) {
854 init_timer(&pDevice->TimerSQ3Tmax1);
855 pDevice->TimerSQ3Tmax1.data = (unsigned long) pDevice;
856 pDevice->TimerSQ3Tmax1.function = (TimerFunction)TimerSQ3CallBack;
857 pDevice->TimerSQ3Tmax1.expires = RUN_AT(HZ);
859 init_timer(&pDevice->TimerSQ3Tmax2);
860 pDevice->TimerSQ3Tmax2.data = (unsigned long) pDevice;
861 pDevice->TimerSQ3Tmax2.function = (TimerFunction)TimerSQ3CallBack;
862 pDevice->TimerSQ3Tmax2.expires = RUN_AT(HZ);
864 init_timer(&pDevice->TimerSQ3Tmax3);
865 pDevice->TimerSQ3Tmax3.data = (unsigned long) pDevice;
866 pDevice->TimerSQ3Tmax3.function = (TimerFunction)TimerState1CallBack;
867 pDevice->TimerSQ3Tmax3.expires = RUN_AT(HZ);
869 return;
873 static bool device_release_WPADEV(PSDevice pDevice)
875 viawget_wpa_header *wpahdr;
876 int ii=0;
877 // wait_queue_head_t Set_wait;
878 //send device close to wpa_supplicnat layer
879 if (pDevice->bWPADEVUp==true) {
880 wpahdr = (viawget_wpa_header *)pDevice->skb->data;
881 wpahdr->type = VIAWGET_DEVICECLOSE_MSG;
882 wpahdr->resp_ie_len = 0;
883 wpahdr->req_ie_len = 0;
884 skb_put(pDevice->skb, sizeof(viawget_wpa_header));
885 pDevice->skb->dev = pDevice->wpadev;
886 skb_reset_mac_header(pDevice->skb);
887 pDevice->skb->pkt_type = PACKET_HOST;
888 pDevice->skb->protocol = htons(ETH_P_802_2);
889 memset(pDevice->skb->cb, 0, sizeof(pDevice->skb->cb));
890 netif_rx(pDevice->skb);
891 pDevice->skb = dev_alloc_skb((int)pDevice->rx_buf_sz);
893 //wait release WPADEV
894 // init_waitqueue_head(&Set_wait);
895 // wait_event_timeout(Set_wait, ((pDevice->wpadev==NULL)&&(pDevice->skb == NULL)),5*HZ); //1s wait
896 while((pDevice->bWPADEVUp==true)) {
897 set_current_state(TASK_UNINTERRUPTIBLE);
898 schedule_timeout (HZ/20); //wait 50ms
899 ii++;
900 if(ii>20)
901 break;
904 return true;
908 static const struct net_device_ops device_netdev_ops = {
909 .ndo_open = device_open,
910 .ndo_stop = device_close,
911 .ndo_do_ioctl = device_ioctl,
912 .ndo_get_stats = device_get_stats,
913 .ndo_start_xmit = device_xmit,
914 .ndo_set_multicast_list = device_set_multi,
919 static int __devinit
920 vt6655_probe(struct pci_dev *pcid, const struct pci_device_id *ent)
922 static bool bFirst = true;
923 struct net_device* dev = NULL;
924 PCHIP_INFO pChip_info = (PCHIP_INFO)ent->driver_data;
925 PSDevice pDevice;
926 int rc;
927 if (device_nics ++>= MAX_UINTS) {
928 printk(KERN_NOTICE DEVICE_NAME ": already found %d NICs\n", device_nics);
929 return -ENODEV;
933 dev = alloc_etherdev(sizeof(DEVICE_INFO));
935 pDevice = (PSDevice) netdev_priv(dev);
937 if (dev == NULL) {
938 printk(KERN_ERR DEVICE_NAME ": allocate net device failed \n");
939 return -ENODEV;
942 // Chain it all together
943 // SET_MODULE_OWNER(dev);
944 SET_NETDEV_DEV(dev, &pcid->dev);
946 if (bFirst) {
947 printk(KERN_NOTICE "%s Ver. %s\n",DEVICE_FULL_DRV_NAM, DEVICE_VERSION);
948 printk(KERN_NOTICE "Copyright (c) 2003 VIA Networking Technologies, Inc.\n");
949 bFirst=false;
952 if (!vt6655_init_info(pcid, &pDevice, pChip_info)) {
953 return -ENOMEM;
955 pDevice->dev = dev;
956 pDevice->next_module = root_device_dev;
957 root_device_dev = dev;
958 dev->irq = pcid->irq;
960 if (pci_enable_device(pcid)) {
961 device_free_info(pDevice);
962 return -ENODEV;
964 #ifdef DEBUG
965 printk("Before get pci_info memaddr is %x\n",pDevice->memaddr);
966 #endif
967 if (device_get_pci_info(pDevice,pcid) == false) {
968 printk(KERN_ERR DEVICE_NAME ": Failed to find PCI device.\n");
969 device_free_info(pDevice);
970 return -ENODEV;
974 #ifdef DEBUG
976 //pci_read_config_byte(pcid, PCI_BASE_ADDRESS_0, &pDevice->byRevId);
977 printk("after get pci_info memaddr is %x, io addr is %x,io_size is %d\n",pDevice->memaddr,pDevice->ioaddr,pDevice->io_size);
979 int i;
980 u32 bar,len;
981 u32 address[] = {
982 PCI_BASE_ADDRESS_0,
983 PCI_BASE_ADDRESS_1,
984 PCI_BASE_ADDRESS_2,
985 PCI_BASE_ADDRESS_3,
986 PCI_BASE_ADDRESS_4,
987 PCI_BASE_ADDRESS_5,
989 for (i=0;address[i];i++)
991 //pci_write_config_dword(pcid,address[i], 0xFFFFFFFF);
992 pci_read_config_dword(pcid, address[i], &bar);
993 printk("bar %d is %x\n",i,bar);
994 if (!bar)
996 printk("bar %d not implemented\n",i);
997 continue;
999 if (bar & PCI_BASE_ADDRESS_SPACE_IO) {
1000 /* This is IO */
1002 len = bar & (PCI_BASE_ADDRESS_IO_MASK & 0xFFFF);
1003 len = len & ~(len - 1);
1005 printk("IO space: len in IO %x, BAR %d\n", len, i);
1007 else
1009 len = bar & 0xFFFFFFF0;
1010 len = ~len + 1;
1012 printk("len in MEM %x, BAR %d\n", len, i);
1016 #endif
1018 #ifdef DEBUG
1019 //return 0 ;
1020 #endif
1021 pDevice->PortOffset = (unsigned long)ioremap(pDevice->memaddr & PCI_BASE_ADDRESS_MEM_MASK, pDevice->io_size);
1022 //pDevice->PortOffset = (unsigned long)ioremap(pDevice->ioaddr & PCI_BASE_ADDRESS_IO_MASK, pDevice->io_size);
1024 if(pDevice->PortOffset == 0) {
1025 printk(KERN_ERR DEVICE_NAME ": Failed to IO remapping ..\n");
1026 device_free_info(pDevice);
1027 return -ENODEV;
1033 rc = pci_request_regions(pcid, DEVICE_NAME);
1034 if (rc) {
1035 printk(KERN_ERR DEVICE_NAME ": Failed to find PCI device\n");
1036 device_free_info(pDevice);
1037 return -ENODEV;
1040 dev->base_addr = pDevice->ioaddr;
1041 #ifdef PLICE_DEBUG
1042 unsigned char value;
1044 VNSvInPortB(pDevice->PortOffset+0x4F, &value);
1045 printk("Before write: value is %x\n",value);
1046 //VNSvInPortB(pDevice->PortOffset+0x3F, 0x00);
1047 VNSvOutPortB(pDevice->PortOffset,value);
1048 VNSvInPortB(pDevice->PortOffset+0x4F, &value);
1049 printk("After write: value is %x\n",value);
1050 #endif
1054 #ifdef IO_MAP
1055 pDevice->PortOffset = pDevice->ioaddr;
1056 #endif
1057 // do reset
1058 if (!MACbSoftwareReset(pDevice->PortOffset)) {
1059 printk(KERN_ERR DEVICE_NAME ": Failed to access MAC hardware..\n");
1060 device_free_info(pDevice);
1061 return -ENODEV;
1063 // initial to reload eeprom
1064 MACvInitialize(pDevice->PortOffset);
1065 MACvReadEtherAddress(pDevice->PortOffset, dev->dev_addr);
1067 device_get_options(pDevice, device_nics-1, dev->name);
1068 device_set_options(pDevice);
1069 //Mask out the options cannot be set to the chip
1070 pDevice->sOpts.flags &= pChip_info->flags;
1072 //Enable the chip specified capbilities
1073 pDevice->flags = pDevice->sOpts.flags | (pChip_info->flags & 0xFF000000UL);
1074 pDevice->tx_80211 = device_dma0_tx_80211;
1075 pDevice->sMgmtObj.pAdapter = (void *)pDevice;
1076 pDevice->pMgmt = &(pDevice->sMgmtObj);
1078 dev->irq = pcid->irq;
1079 dev->netdev_ops = &device_netdev_ops;
1081 dev->wireless_handlers = (struct iw_handler_def *)&iwctl_handler_def;
1083 rc = register_netdev(dev);
1084 if (rc)
1086 printk(KERN_ERR DEVICE_NAME " Failed to register netdev\n");
1087 device_free_info(pDevice);
1088 return -ENODEV;
1090 //2008-07-21-01<Add>by MikeLiu
1091 //register wpadev
1092 device_print_info(pDevice);
1093 pci_set_drvdata(pcid, pDevice);
1094 return 0;
1098 static void device_print_info(PSDevice pDevice)
1100 struct net_device* dev=pDevice->dev;
1102 DBG_PRT(MSG_LEVEL_INFO, KERN_INFO "%s: %s\n",dev->name, get_chip_name(pDevice->chip_id));
1103 DBG_PRT(MSG_LEVEL_INFO, KERN_INFO "%s: MAC=%pM", dev->name, dev->dev_addr);
1104 #ifdef IO_MAP
1105 DBG_PRT(MSG_LEVEL_INFO, KERN_INFO" IO=0x%lx ",(unsigned long) pDevice->ioaddr);
1106 DBG_PRT(MSG_LEVEL_INFO, KERN_INFO" IRQ=%d \n", pDevice->dev->irq);
1107 #else
1108 DBG_PRT(MSG_LEVEL_INFO, KERN_INFO" IO=0x%lx Mem=0x%lx ",
1109 (unsigned long) pDevice->ioaddr,(unsigned long) pDevice->PortOffset);
1110 DBG_PRT(MSG_LEVEL_INFO, KERN_INFO" IRQ=%d \n", pDevice->dev->irq);
1111 #endif
1115 static bool __devinit vt6655_init_info(struct pci_dev* pcid, PSDevice* ppDevice,
1116 PCHIP_INFO pChip_info) {
1118 PSDevice p;
1120 memset(*ppDevice,0,sizeof(DEVICE_INFO));
1122 if (pDevice_Infos == NULL) {
1123 pDevice_Infos =*ppDevice;
1125 else {
1126 for (p=pDevice_Infos;p->next!=NULL;p=p->next)
1127 do {} while (0);
1128 p->next = *ppDevice;
1129 (*ppDevice)->prev = p;
1132 (*ppDevice)->pcid = pcid;
1133 (*ppDevice)->chip_id = pChip_info->chip_id;
1134 (*ppDevice)->io_size = pChip_info->io_size;
1135 (*ppDevice)->nTxQueues = pChip_info->nTxQueue;
1136 (*ppDevice)->multicast_limit =32;
1138 spin_lock_init(&((*ppDevice)->lock));
1140 return true;
1143 static bool device_get_pci_info(PSDevice pDevice, struct pci_dev* pcid) {
1145 u16 pci_cmd;
1146 u8 b;
1147 unsigned int cis_addr;
1148 #ifdef PLICE_DEBUG
1149 unsigned char pci_config[256];
1150 unsigned char value =0x00;
1151 int ii,j;
1152 u16 max_lat=0x0000;
1153 memset(pci_config,0x00,256);
1154 #endif
1156 pci_read_config_byte(pcid, PCI_REVISION_ID, &pDevice->byRevId);
1157 pci_read_config_word(pcid, PCI_SUBSYSTEM_ID,&pDevice->SubSystemID);
1158 pci_read_config_word(pcid, PCI_SUBSYSTEM_VENDOR_ID, &pDevice->SubVendorID);
1159 pci_read_config_word(pcid, PCI_COMMAND, (u16 *) & (pci_cmd));
1161 pci_set_master(pcid);
1163 pDevice->memaddr = pci_resource_start(pcid,0);
1164 pDevice->ioaddr = pci_resource_start(pcid,1);
1166 #ifdef DEBUG
1167 // pDevice->ioaddr = pci_resource_start(pcid, 0);
1168 // pDevice->memaddr = pci_resource_start(pcid,1);
1169 #endif
1171 cis_addr = pci_resource_start(pcid,2);
1173 pDevice->pcid = pcid;
1175 pci_read_config_byte(pcid, PCI_COMMAND, &b);
1176 pci_write_config_byte(pcid, PCI_COMMAND, (b|PCI_COMMAND_MASTER));
1178 #ifdef PLICE_DEBUG
1179 //pci_read_config_word(pcid,PCI_MAX_LAT,&max_lat);
1180 //printk("max lat is %x,SubSystemID is %x\n",max_lat,pDevice->SubSystemID);
1181 //for (ii=0;ii<0xFF;ii++)
1182 //pci_read_config_word(pcid,PCI_MAX_LAT,&max_lat);
1183 //max_lat = 0x20;
1184 //pci_write_config_word(pcid,PCI_MAX_LAT,max_lat);
1185 //pci_read_config_word(pcid,PCI_MAX_LAT,&max_lat);
1186 //printk("max lat is %x\n",max_lat);
1188 for (ii=0;ii<0xFF;ii++)
1190 pci_read_config_byte(pcid,ii,&value);
1191 pci_config[ii] = value;
1193 for (ii=0,j=1;ii<0x100;ii++,j++)
1195 if (j %16 == 0)
1197 printk("%x:",pci_config[ii]);
1198 printk("\n");
1200 else
1202 printk("%x:",pci_config[ii]);
1205 #endif
1206 return true;
1209 static void device_free_info(PSDevice pDevice) {
1210 PSDevice ptr;
1211 struct net_device* dev=pDevice->dev;
1213 ASSERT(pDevice);
1214 //2008-0714-01<Add>by chester
1215 device_release_WPADEV(pDevice);
1217 //2008-07-21-01<Add>by MikeLiu
1218 //unregister wpadev
1219 if(wpa_set_wpadev(pDevice, 0)!=0)
1220 printk("unregister wpadev fail?\n");
1222 if (pDevice_Infos==NULL)
1223 return;
1225 for (ptr=pDevice_Infos;ptr && (ptr!=pDevice);ptr=ptr->next)
1226 do {} while (0);
1228 if (ptr==pDevice) {
1229 if (ptr==pDevice_Infos)
1230 pDevice_Infos=ptr->next;
1231 else
1232 ptr->prev->next=ptr->next;
1234 else {
1235 DBG_PRT(MSG_LEVEL_ERR, KERN_ERR "info struct not found\n");
1236 return;
1238 #ifdef HOSTAP
1239 if (dev)
1240 vt6655_hostap_set_hostapd(pDevice, 0, 0);
1241 #endif
1242 if (dev)
1243 unregister_netdev(dev);
1245 if (pDevice->PortOffset)
1246 iounmap((void *)pDevice->PortOffset);
1248 if (pDevice->pcid)
1249 pci_release_regions(pDevice->pcid);
1250 if (dev)
1251 free_netdev(dev);
1253 if (pDevice->pcid) {
1254 pci_set_drvdata(pDevice->pcid,NULL);
1258 static bool device_init_rings(PSDevice pDevice) {
1259 void* vir_pool;
1262 /*allocate all RD/TD rings a single pool*/
1263 vir_pool = pci_alloc_consistent(pDevice->pcid,
1264 pDevice->sOpts.nRxDescs0 * sizeof(SRxDesc) +
1265 pDevice->sOpts.nRxDescs1 * sizeof(SRxDesc) +
1266 pDevice->sOpts.nTxDescs[0] * sizeof(STxDesc) +
1267 pDevice->sOpts.nTxDescs[1] * sizeof(STxDesc),
1268 &pDevice->pool_dma);
1270 if (vir_pool == NULL) {
1271 DBG_PRT(MSG_LEVEL_ERR,KERN_ERR "%s : allocate desc dma memory failed\n", pDevice->dev->name);
1272 return false;
1275 memset(vir_pool, 0,
1276 pDevice->sOpts.nRxDescs0 * sizeof(SRxDesc) +
1277 pDevice->sOpts.nRxDescs1 * sizeof(SRxDesc) +
1278 pDevice->sOpts.nTxDescs[0] * sizeof(STxDesc) +
1279 pDevice->sOpts.nTxDescs[1] * sizeof(STxDesc)
1282 pDevice->aRD0Ring = vir_pool;
1283 pDevice->aRD1Ring = vir_pool +
1284 pDevice->sOpts.nRxDescs0 * sizeof(SRxDesc);
1287 pDevice->rd0_pool_dma = pDevice->pool_dma;
1288 pDevice->rd1_pool_dma = pDevice->rd0_pool_dma +
1289 pDevice->sOpts.nRxDescs0 * sizeof(SRxDesc);
1291 pDevice->tx0_bufs = pci_alloc_consistent(pDevice->pcid,
1292 pDevice->sOpts.nTxDescs[0] * PKT_BUF_SZ +
1293 pDevice->sOpts.nTxDescs[1] * PKT_BUF_SZ +
1294 CB_BEACON_BUF_SIZE +
1295 CB_MAX_BUF_SIZE,
1296 &pDevice->tx_bufs_dma0);
1298 if (pDevice->tx0_bufs == NULL) {
1299 DBG_PRT(MSG_LEVEL_ERR,KERN_ERR "%s: allocate buf dma memory failed\n", pDevice->dev->name);
1300 pci_free_consistent(pDevice->pcid,
1301 pDevice->sOpts.nRxDescs0 * sizeof(SRxDesc) +
1302 pDevice->sOpts.nRxDescs1 * sizeof(SRxDesc) +
1303 pDevice->sOpts.nTxDescs[0] * sizeof(STxDesc) +
1304 pDevice->sOpts.nTxDescs[1] * sizeof(STxDesc),
1305 vir_pool, pDevice->pool_dma
1307 return false;
1310 memset(pDevice->tx0_bufs, 0,
1311 pDevice->sOpts.nTxDescs[0] * PKT_BUF_SZ +
1312 pDevice->sOpts.nTxDescs[1] * PKT_BUF_SZ +
1313 CB_BEACON_BUF_SIZE +
1314 CB_MAX_BUF_SIZE
1317 pDevice->td0_pool_dma = pDevice->rd1_pool_dma +
1318 pDevice->sOpts.nRxDescs1 * sizeof(SRxDesc);
1320 pDevice->td1_pool_dma = pDevice->td0_pool_dma +
1321 pDevice->sOpts.nTxDescs[0] * sizeof(STxDesc);
1324 // vir_pool: pvoid type
1325 pDevice->apTD0Rings = vir_pool
1326 + pDevice->sOpts.nRxDescs0 * sizeof(SRxDesc)
1327 + pDevice->sOpts.nRxDescs1 * sizeof(SRxDesc);
1329 pDevice->apTD1Rings = vir_pool
1330 + pDevice->sOpts.nRxDescs0 * sizeof(SRxDesc)
1331 + pDevice->sOpts.nRxDescs1 * sizeof(SRxDesc)
1332 + pDevice->sOpts.nTxDescs[0] * sizeof(STxDesc);
1335 pDevice->tx1_bufs = pDevice->tx0_bufs +
1336 pDevice->sOpts.nTxDescs[0] * PKT_BUF_SZ;
1339 pDevice->tx_beacon_bufs = pDevice->tx1_bufs +
1340 pDevice->sOpts.nTxDescs[1] * PKT_BUF_SZ;
1342 pDevice->pbyTmpBuff = pDevice->tx_beacon_bufs +
1343 CB_BEACON_BUF_SIZE;
1345 pDevice->tx_bufs_dma1 = pDevice->tx_bufs_dma0 +
1346 pDevice->sOpts.nTxDescs[0] * PKT_BUF_SZ;
1349 pDevice->tx_beacon_dma = pDevice->tx_bufs_dma1 +
1350 pDevice->sOpts.nTxDescs[1] * PKT_BUF_SZ;
1353 return true;
1356 static void device_free_rings(PSDevice pDevice) {
1358 pci_free_consistent(pDevice->pcid,
1359 pDevice->sOpts.nRxDescs0 * sizeof(SRxDesc) +
1360 pDevice->sOpts.nRxDescs1 * sizeof(SRxDesc) +
1361 pDevice->sOpts.nTxDescs[0] * sizeof(STxDesc) +
1362 pDevice->sOpts.nTxDescs[1] * sizeof(STxDesc)
1364 pDevice->aRD0Ring, pDevice->pool_dma
1367 if (pDevice->tx0_bufs)
1368 pci_free_consistent(pDevice->pcid,
1369 pDevice->sOpts.nTxDescs[0] * PKT_BUF_SZ +
1370 pDevice->sOpts.nTxDescs[1] * PKT_BUF_SZ +
1371 CB_BEACON_BUF_SIZE +
1372 CB_MAX_BUF_SIZE,
1373 pDevice->tx0_bufs, pDevice->tx_bufs_dma0
1377 static void device_init_rd0_ring(PSDevice pDevice) {
1378 int i;
1379 dma_addr_t curr = pDevice->rd0_pool_dma;
1380 PSRxDesc pDesc;
1382 /* Init the RD0 ring entries */
1383 for (i = 0; i < pDevice->sOpts.nRxDescs0; i ++, curr += sizeof(SRxDesc)) {
1384 pDesc = &(pDevice->aRD0Ring[i]);
1385 pDesc->pRDInfo = alloc_rd_info();
1386 ASSERT(pDesc->pRDInfo);
1387 if (!device_alloc_rx_buf(pDevice, pDesc)) {
1388 DBG_PRT(MSG_LEVEL_ERR,KERN_ERR "%s: can not alloc rx bufs\n",
1389 pDevice->dev->name);
1391 pDesc->next = &(pDevice->aRD0Ring[(i+1) % pDevice->sOpts.nRxDescs0]);
1392 pDesc->pRDInfo->curr_desc = cpu_to_le32(curr);
1393 pDesc->next_desc = cpu_to_le32(curr + sizeof(SRxDesc));
1396 if (i > 0)
1397 pDevice->aRD0Ring[i-1].next_desc = cpu_to_le32(pDevice->rd0_pool_dma);
1398 pDevice->pCurrRD[0] = &(pDevice->aRD0Ring[0]);
1402 static void device_init_rd1_ring(PSDevice pDevice) {
1403 int i;
1404 dma_addr_t curr = pDevice->rd1_pool_dma;
1405 PSRxDesc pDesc;
1407 /* Init the RD1 ring entries */
1408 for (i = 0; i < pDevice->sOpts.nRxDescs1; i ++, curr += sizeof(SRxDesc)) {
1409 pDesc = &(pDevice->aRD1Ring[i]);
1410 pDesc->pRDInfo = alloc_rd_info();
1411 ASSERT(pDesc->pRDInfo);
1412 if (!device_alloc_rx_buf(pDevice, pDesc)) {
1413 DBG_PRT(MSG_LEVEL_ERR,KERN_ERR "%s: can not alloc rx bufs\n",
1414 pDevice->dev->name);
1416 pDesc->next = &(pDevice->aRD1Ring[(i+1) % pDevice->sOpts.nRxDescs1]);
1417 pDesc->pRDInfo->curr_desc = cpu_to_le32(curr);
1418 pDesc->next_desc = cpu_to_le32(curr + sizeof(SRxDesc));
1421 if (i > 0)
1422 pDevice->aRD1Ring[i-1].next_desc = cpu_to_le32(pDevice->rd1_pool_dma);
1423 pDevice->pCurrRD[1] = &(pDevice->aRD1Ring[0]);
1427 static void device_init_defrag_cb(PSDevice pDevice) {
1428 int i;
1429 PSDeFragControlBlock pDeF;
1431 /* Init the fragment ctl entries */
1432 for (i = 0; i < CB_MAX_RX_FRAG; i++) {
1433 pDeF = &(pDevice->sRxDFCB[i]);
1434 if (!device_alloc_frag_buf(pDevice, pDeF)) {
1435 DBG_PRT(MSG_LEVEL_ERR,KERN_ERR "%s: can not alloc frag bufs\n",
1436 pDevice->dev->name);
1439 pDevice->cbDFCB = CB_MAX_RX_FRAG;
1440 pDevice->cbFreeDFCB = pDevice->cbDFCB;
1446 static void device_free_rd0_ring(PSDevice pDevice) {
1447 int i;
1449 for (i = 0; i < pDevice->sOpts.nRxDescs0; i++) {
1450 PSRxDesc pDesc =&(pDevice->aRD0Ring[i]);
1451 PDEVICE_RD_INFO pRDInfo =pDesc->pRDInfo;
1453 pci_unmap_single(pDevice->pcid,pRDInfo->skb_dma,
1454 pDevice->rx_buf_sz, PCI_DMA_FROMDEVICE);
1456 dev_kfree_skb(pRDInfo->skb);
1458 kfree((void *)pDesc->pRDInfo);
1463 static void device_free_rd1_ring(PSDevice pDevice) {
1464 int i;
1467 for (i = 0; i < pDevice->sOpts.nRxDescs1; i++) {
1468 PSRxDesc pDesc=&(pDevice->aRD1Ring[i]);
1469 PDEVICE_RD_INFO pRDInfo=pDesc->pRDInfo;
1471 pci_unmap_single(pDevice->pcid,pRDInfo->skb_dma,
1472 pDevice->rx_buf_sz, PCI_DMA_FROMDEVICE);
1474 dev_kfree_skb(pRDInfo->skb);
1476 kfree((void *)pDesc->pRDInfo);
1481 static void device_free_frag_buf(PSDevice pDevice) {
1482 PSDeFragControlBlock pDeF;
1483 int i;
1485 for (i = 0; i < CB_MAX_RX_FRAG; i++) {
1487 pDeF = &(pDevice->sRxDFCB[i]);
1489 if (pDeF->skb)
1490 dev_kfree_skb(pDeF->skb);
1496 static void device_init_td0_ring(PSDevice pDevice) {
1497 int i;
1498 dma_addr_t curr;
1499 PSTxDesc pDesc;
1501 curr = pDevice->td0_pool_dma;
1502 for (i = 0; i < pDevice->sOpts.nTxDescs[0]; i++, curr += sizeof(STxDesc)) {
1503 pDesc = &(pDevice->apTD0Rings[i]);
1504 pDesc->pTDInfo = alloc_td_info();
1505 ASSERT(pDesc->pTDInfo);
1506 if (pDevice->flags & DEVICE_FLAGS_TX_ALIGN) {
1507 pDesc->pTDInfo->buf = pDevice->tx0_bufs + (i)*PKT_BUF_SZ;
1508 pDesc->pTDInfo->buf_dma = pDevice->tx_bufs_dma0 + (i)*PKT_BUF_SZ;
1510 pDesc->next =&(pDevice->apTD0Rings[(i+1) % pDevice->sOpts.nTxDescs[0]]);
1511 pDesc->pTDInfo->curr_desc = cpu_to_le32(curr);
1512 pDesc->next_desc = cpu_to_le32(curr+sizeof(STxDesc));
1515 if (i > 0)
1516 pDevice->apTD0Rings[i-1].next_desc = cpu_to_le32(pDevice->td0_pool_dma);
1517 pDevice->apTailTD[0] = pDevice->apCurrTD[0] =&(pDevice->apTD0Rings[0]);
1521 static void device_init_td1_ring(PSDevice pDevice) {
1522 int i;
1523 dma_addr_t curr;
1524 PSTxDesc pDesc;
1526 /* Init the TD ring entries */
1527 curr=pDevice->td1_pool_dma;
1528 for (i = 0; i < pDevice->sOpts.nTxDescs[1]; i++, curr+=sizeof(STxDesc)) {
1529 pDesc=&(pDevice->apTD1Rings[i]);
1530 pDesc->pTDInfo = alloc_td_info();
1531 ASSERT(pDesc->pTDInfo);
1532 if (pDevice->flags & DEVICE_FLAGS_TX_ALIGN) {
1533 pDesc->pTDInfo->buf=pDevice->tx1_bufs+(i)*PKT_BUF_SZ;
1534 pDesc->pTDInfo->buf_dma=pDevice->tx_bufs_dma1+(i)*PKT_BUF_SZ;
1536 pDesc->next=&(pDevice->apTD1Rings[(i+1) % pDevice->sOpts.nTxDescs[1]]);
1537 pDesc->pTDInfo->curr_desc = cpu_to_le32(curr);
1538 pDesc->next_desc = cpu_to_le32(curr+sizeof(STxDesc));
1541 if (i > 0)
1542 pDevice->apTD1Rings[i-1].next_desc = cpu_to_le32(pDevice->td1_pool_dma);
1543 pDevice->apTailTD[1] = pDevice->apCurrTD[1] = &(pDevice->apTD1Rings[0]);
1548 static void device_free_td0_ring(PSDevice pDevice) {
1549 int i;
1550 for (i = 0; i < pDevice->sOpts.nTxDescs[0]; i++) {
1551 PSTxDesc pDesc=&(pDevice->apTD0Rings[i]);
1552 PDEVICE_TD_INFO pTDInfo=pDesc->pTDInfo;
1554 if (pTDInfo->skb_dma && (pTDInfo->skb_dma != pTDInfo->buf_dma))
1555 pci_unmap_single(pDevice->pcid,pTDInfo->skb_dma,
1556 pTDInfo->skb->len, PCI_DMA_TODEVICE);
1558 if (pTDInfo->skb)
1559 dev_kfree_skb(pTDInfo->skb);
1561 kfree((void *)pDesc->pTDInfo);
1565 static void device_free_td1_ring(PSDevice pDevice) {
1566 int i;
1568 for (i = 0; i < pDevice->sOpts.nTxDescs[1]; i++) {
1569 PSTxDesc pDesc=&(pDevice->apTD1Rings[i]);
1570 PDEVICE_TD_INFO pTDInfo=pDesc->pTDInfo;
1572 if (pTDInfo->skb_dma && (pTDInfo->skb_dma != pTDInfo->buf_dma))
1573 pci_unmap_single(pDevice->pcid, pTDInfo->skb_dma,
1574 pTDInfo->skb->len, PCI_DMA_TODEVICE);
1576 if (pTDInfo->skb)
1577 dev_kfree_skb(pTDInfo->skb);
1579 kfree((void *)pDesc->pTDInfo);
1586 /*-----------------------------------------------------------------*/
1588 static int device_rx_srv(PSDevice pDevice, unsigned int uIdx) {
1589 PSRxDesc pRD;
1590 int works = 0;
1593 for (pRD = pDevice->pCurrRD[uIdx];
1594 pRD->m_rd0RD0.f1Owner == OWNED_BY_HOST;
1595 pRD = pRD->next) {
1596 // DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "pDevice->pCurrRD = %x, works = %d\n", pRD, works);
1597 if (works++>15)
1598 break;
1599 if (device_receive_frame(pDevice, pRD)) {
1600 if (!device_alloc_rx_buf(pDevice,pRD)) {
1601 DBG_PRT(MSG_LEVEL_ERR, KERN_ERR
1602 "%s: can not allocate rx buf\n", pDevice->dev->name);
1603 break;
1606 pRD->m_rd0RD0.f1Owner = OWNED_BY_NIC;
1607 pDevice->dev->last_rx = jiffies;
1610 pDevice->pCurrRD[uIdx]=pRD;
1612 return works;
1616 static bool device_alloc_rx_buf(PSDevice pDevice, PSRxDesc pRD) {
1618 PDEVICE_RD_INFO pRDInfo=pRD->pRDInfo;
1621 pRDInfo->skb = dev_alloc_skb((int)pDevice->rx_buf_sz);
1622 #ifdef PLICE_DEBUG
1623 //printk("device_alloc_rx_buf:skb is %x\n",pRDInfo->skb);
1624 #endif
1625 if (pRDInfo->skb==NULL)
1626 return false;
1627 ASSERT(pRDInfo->skb);
1628 pRDInfo->skb->dev = pDevice->dev;
1629 pRDInfo->skb_dma = pci_map_single(pDevice->pcid, skb_tail_pointer(pRDInfo->skb),
1630 pDevice->rx_buf_sz, PCI_DMA_FROMDEVICE);
1631 *((unsigned int *) &(pRD->m_rd0RD0)) = 0; /* FIX cast */
1633 pRD->m_rd0RD0.wResCount = cpu_to_le16(pDevice->rx_buf_sz);
1634 pRD->m_rd0RD0.f1Owner = OWNED_BY_NIC;
1635 pRD->m_rd1RD1.wReqCount = cpu_to_le16(pDevice->rx_buf_sz);
1636 pRD->buff_addr = cpu_to_le32(pRDInfo->skb_dma);
1638 return true;
1643 bool device_alloc_frag_buf(PSDevice pDevice, PSDeFragControlBlock pDeF) {
1645 pDeF->skb = dev_alloc_skb((int)pDevice->rx_buf_sz);
1646 if (pDeF->skb == NULL)
1647 return false;
1648 ASSERT(pDeF->skb);
1649 pDeF->skb->dev = pDevice->dev;
1651 return true;
1656 static int device_tx_srv(PSDevice pDevice, unsigned int uIdx) {
1657 PSTxDesc pTD;
1658 bool bFull=false;
1659 int works = 0;
1660 unsigned char byTsr0;
1661 unsigned char byTsr1;
1662 unsigned int uFrameSize, uFIFOHeaderSize;
1663 PSTxBufHead pTxBufHead;
1664 struct net_device_stats* pStats = &pDevice->stats;
1665 struct sk_buff* skb;
1666 unsigned int uNodeIndex;
1667 PSMgmtObject pMgmt = pDevice->pMgmt;
1670 for (pTD = pDevice->apTailTD[uIdx]; pDevice->iTDUsed[uIdx] >0; pTD = pTD->next) {
1672 if (pTD->m_td0TD0.f1Owner == OWNED_BY_NIC)
1673 break;
1674 if (works++>15)
1675 break;
1677 byTsr0 = pTD->m_td0TD0.byTSR0;
1678 byTsr1 = pTD->m_td0TD0.byTSR1;
1680 //Only the status of first TD in the chain is correct
1681 if (pTD->m_td1TD1.byTCR & TCR_STP) {
1683 if ((pTD->pTDInfo->byFlags & TD_FLAGS_NETIF_SKB) != 0) {
1684 uFIFOHeaderSize = pTD->pTDInfo->dwHeaderLength;
1685 uFrameSize = pTD->pTDInfo->dwReqCount - uFIFOHeaderSize;
1686 pTxBufHead = (PSTxBufHead) (pTD->pTDInfo->buf);
1687 // Update the statistics based on the Transmit status
1688 // now, we DO'NT check TSR0_CDH
1690 STAvUpdateTDStatCounter(&pDevice->scStatistic,
1691 byTsr0, byTsr1,
1692 (unsigned char *)(pTD->pTDInfo->buf + uFIFOHeaderSize),
1693 uFrameSize, uIdx);
1696 BSSvUpdateNodeTxCounter(pDevice,
1697 byTsr0, byTsr1,
1698 (unsigned char *)(pTD->pTDInfo->buf),
1699 uFIFOHeaderSize
1702 if ( !(byTsr1 & TSR1_TERR)) {
1703 if (byTsr0 != 0) {
1704 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" Tx[%d] OK but has error. tsr1[%02X] tsr0[%02X].\n",
1705 (int)uIdx, byTsr1, byTsr0);
1707 if ((pTxBufHead->wFragCtl & FRAGCTL_ENDFRAG) != FRAGCTL_NONFRAG) {
1708 pDevice->s802_11Counter.TransmittedFragmentCount ++;
1710 pStats->tx_packets++;
1711 pStats->tx_bytes += pTD->pTDInfo->skb->len;
1713 else {
1714 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" Tx[%d] dropped & tsr1[%02X] tsr0[%02X].\n",
1715 (int)uIdx, byTsr1, byTsr0);
1716 pStats->tx_errors++;
1717 pStats->tx_dropped++;
1721 if ((pTD->pTDInfo->byFlags & TD_FLAGS_PRIV_SKB) != 0) {
1722 if (pDevice->bEnableHostapd) {
1723 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "tx call back netif.. \n");
1724 skb = pTD->pTDInfo->skb;
1725 skb->dev = pDevice->apdev;
1726 skb_reset_mac_header(skb);
1727 skb->pkt_type = PACKET_OTHERHOST;
1728 //skb->protocol = htons(ETH_P_802_2);
1729 memset(skb->cb, 0, sizeof(skb->cb));
1730 netif_rx(skb);
1734 if (byTsr1 & TSR1_TERR) {
1735 if ((pTD->pTDInfo->byFlags & TD_FLAGS_PRIV_SKB) != 0) {
1736 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" Tx[%d] fail has error. tsr1[%02X] tsr0[%02X].\n",
1737 (int)uIdx, byTsr1, byTsr0);
1740 // DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" Tx[%d] fail has error. tsr1[%02X] tsr0[%02X].\n",
1741 // (int)uIdx, byTsr1, byTsr0);
1743 if ((pMgmt->eCurrMode == WMAC_MODE_ESS_AP) &&
1744 (pTD->pTDInfo->byFlags & TD_FLAGS_NETIF_SKB)) {
1745 unsigned short wAID;
1746 unsigned char byMask[8] = {1, 2, 4, 8, 0x10, 0x20, 0x40, 0x80};
1748 skb = pTD->pTDInfo->skb;
1749 if (BSSDBbIsSTAInNodeDB(pMgmt, (unsigned char *)(skb->data), &uNodeIndex)) {
1750 if (pMgmt->sNodeDBTable[uNodeIndex].bPSEnable) {
1751 skb_queue_tail(&pMgmt->sNodeDBTable[uNodeIndex].sTxPSQueue, skb);
1752 pMgmt->sNodeDBTable[uNodeIndex].wEnQueueCnt++;
1753 // set tx map
1754 wAID = pMgmt->sNodeDBTable[uNodeIndex].wAID;
1755 pMgmt->abyPSTxMap[wAID >> 3] |= byMask[wAID & 7];
1756 pTD->pTDInfo->byFlags &= ~(TD_FLAGS_NETIF_SKB);
1757 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "tx_srv:tx fail re-queue sta index= %d, QueCnt= %d\n"
1758 ,(int)uNodeIndex, pMgmt->sNodeDBTable[uNodeIndex].wEnQueueCnt);
1759 pStats->tx_errors--;
1760 pStats->tx_dropped--;
1765 device_free_tx_buf(pDevice,pTD);
1766 pDevice->iTDUsed[uIdx]--;
1771 if (uIdx == TYPE_AC0DMA) {
1772 // RESERV_AC0DMA reserved for relay
1774 if (AVAIL_TD(pDevice, uIdx) < RESERV_AC0DMA) {
1775 bFull = true;
1776 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " AC0DMA is Full = %d\n", pDevice->iTDUsed[uIdx]);
1778 if (netif_queue_stopped(pDevice->dev) && (bFull==false)){
1779 netif_wake_queue(pDevice->dev);
1784 pDevice->apTailTD[uIdx] = pTD;
1786 return works;
1790 static void device_error(PSDevice pDevice, unsigned short status) {
1792 if (status & ISR_FETALERR) {
1793 DBG_PRT(MSG_LEVEL_ERR, KERN_ERR
1794 "%s: Hardware fatal error.\n",
1795 pDevice->dev->name);
1796 netif_stop_queue(pDevice->dev);
1797 del_timer(&pDevice->sTimerCommand);
1798 del_timer(&(pDevice->pMgmt->sTimerSecondCallback));
1799 pDevice->bCmdRunning = false;
1800 MACbShutdown(pDevice->PortOffset);
1801 return;
1806 static void device_free_tx_buf(PSDevice pDevice, PSTxDesc pDesc) {
1807 PDEVICE_TD_INFO pTDInfo=pDesc->pTDInfo;
1808 struct sk_buff* skb=pTDInfo->skb;
1810 // pre-allocated buf_dma can't be unmapped.
1811 if (pTDInfo->skb_dma && (pTDInfo->skb_dma != pTDInfo->buf_dma)) {
1812 pci_unmap_single(pDevice->pcid,pTDInfo->skb_dma,skb->len,
1813 PCI_DMA_TODEVICE);
1816 if ((pTDInfo->byFlags & TD_FLAGS_NETIF_SKB) != 0)
1817 dev_kfree_skb_irq(skb);
1819 pTDInfo->skb_dma = 0;
1820 pTDInfo->skb = 0;
1821 pTDInfo->byFlags = 0;
1826 //PLICE_DEBUG ->
1827 void InitRxManagementQueue(PSDevice pDevice)
1829 pDevice->rxManeQueue.packet_num = 0;
1830 pDevice->rxManeQueue.head = pDevice->rxManeQueue.tail = 0;
1832 //PLICE_DEBUG<-
1838 //PLICE_DEBUG ->
1839 int MlmeThread(
1840 void * Context)
1842 PSDevice pDevice = (PSDevice) Context;
1843 PSRxMgmtPacket pRxMgmtPacket;
1844 // int i ;
1845 //complete(&pDevice->notify);
1846 //printk("Enter MngWorkItem,Queue packet num is %d\n",pDevice->rxManeQueue.packet_num);
1848 //printk("Enter MlmeThread,packet _num is %d\n",pDevice->rxManeQueue.packet_num);
1849 //i = 0;
1850 while (1)
1853 //printk("DDDD\n");
1854 //down(&pDevice->mlme_semaphore);
1855 // pRxMgmtPacket = DeQueue(pDevice);
1856 spin_lock_irq(&pDevice->lock);
1857 while(pDevice->rxManeQueue.packet_num != 0)
1859 pRxMgmtPacket = DeQueue(pDevice);
1860 //pDevice;
1861 //DequeueManageObject(pDevice->FirstRecvMngList, pDevice->LastRecvMngList);
1862 vMgrRxManagePacket(pDevice, pDevice->pMgmt, pRxMgmtPacket);
1863 //printk("packet_num is %d\n",pDevice->rxManeQueue.packet_num);
1866 spin_unlock_irq(&pDevice->lock);
1867 if (mlme_kill == 0)
1868 break;
1869 //udelay(200);
1870 //printk("Before schedule thread jiffies is %x\n",jiffies);
1871 schedule();
1872 //printk("after schedule thread jiffies is %x\n",jiffies);
1873 if (mlme_kill == 0)
1874 break;
1875 //printk("i is %d\n",i);
1877 return 0;
1883 static int device_open(struct net_device *dev) {
1884 PSDevice pDevice=(PSDevice) netdev_priv(dev);
1885 int i;
1886 #ifdef WPA_SM_Transtatus
1887 extern SWPAResult wpa_Result;
1888 #endif
1890 pDevice->rx_buf_sz = PKT_BUF_SZ;
1891 if (!device_init_rings(pDevice)) {
1892 return -ENOMEM;
1894 //2008-5-13 <add> by chester
1895 i=request_irq(pDevice->pcid->irq, &device_intr, IRQF_SHARED, dev->name, dev);
1896 if (i)
1897 return i;
1898 //printk("DEBUG1\n");
1899 #ifdef WPA_SM_Transtatus
1900 memset(wpa_Result.ifname,0,sizeof(wpa_Result.ifname));
1901 wpa_Result.proto = 0;
1902 wpa_Result.key_mgmt = 0;
1903 wpa_Result.eap_type = 0;
1904 wpa_Result.authenticated = false;
1905 pDevice->fWPA_Authened = false;
1906 #endif
1907 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "call device init rd0 ring\n");
1908 device_init_rd0_ring(pDevice);
1909 device_init_rd1_ring(pDevice);
1910 device_init_defrag_cb(pDevice);
1911 device_init_td0_ring(pDevice);
1912 device_init_td1_ring(pDevice);
1913 // VNTWIFIvSet11h(pDevice->pMgmt, pDevice->b11hEnable);
1916 if (pDevice->bDiversityRegCtlON) {
1917 device_init_diversity_timer(pDevice);
1919 vMgrObjectInit(pDevice);
1920 vMgrTimerInit(pDevice);
1922 //PLICE_DEBUG->
1923 #ifdef TASK_LET
1924 tasklet_init (&pDevice->RxMngWorkItem,(void *)MngWorkItem,(unsigned long )pDevice);
1925 #endif
1926 #ifdef THREAD
1927 InitRxManagementQueue(pDevice);
1928 mlme_kill = 0;
1929 mlme_task = kthread_run(MlmeThread,(void *) pDevice, "MLME");
1930 if (IS_ERR(mlme_task)) {
1931 printk("thread create fail\n");
1932 return -1;
1935 mlme_kill = 1;
1936 #endif
1941 //printk("thread id is %d\n",pDevice->MLMEThr_pid);
1942 //printk("Create thread time is %x\n",jiffies);
1943 //wait_for_completion(&pDevice->notify);
1948 // if (( SROMbyReadEmbedded(pDevice->PortOffset, EEP_OFS_RADIOCTL)&0x06)==0x04)
1949 // return -ENOMEM;
1950 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "call device_init_registers\n");
1951 device_init_registers(pDevice, DEVICE_INIT_COLD);
1952 MACvReadEtherAddress(pDevice->PortOffset, pDevice->abyCurrentNetAddr);
1953 memcpy(pDevice->pMgmt->abyMACAddr, pDevice->abyCurrentNetAddr, ETH_ALEN);
1954 device_set_multi(pDevice->dev);
1956 // Init for Key Management
1957 KeyvInitTable(&pDevice->sKey, pDevice->PortOffset);
1958 add_timer(&(pDevice->pMgmt->sTimerSecondCallback));
1960 #ifdef WPA_SUPPLICANT_DRIVER_WEXT_SUPPORT
1962 pDevice->bwextstep0 = false;
1963 pDevice->bwextstep1 = false;
1964 pDevice->bwextstep2 = false;
1965 pDevice->bwextstep3 = false;
1967 pDevice->bwextcount=0;
1968 pDevice->bWPASuppWextEnabled = false;
1969 #endif
1970 pDevice->byReAssocCount = 0;
1971 pDevice->bWPADEVUp = false;
1972 // Patch: if WEP key already set by iwconfig but device not yet open
1973 if ((pDevice->bEncryptionEnable == true) && (pDevice->bTransmitKey == true)) {
1974 KeybSetDefaultKey(&(pDevice->sKey),
1975 (unsigned long)(pDevice->byKeyIndex | (1 << 31)),
1976 pDevice->uKeyLength,
1977 NULL,
1978 pDevice->abyKey,
1979 KEY_CTL_WEP,
1980 pDevice->PortOffset,
1981 pDevice->byLocalID
1983 pDevice->eEncryptionStatus = Ndis802_11Encryption1Enabled;
1986 //printk("DEBUG2\n");
1989 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "call MACvIntEnable\n");
1990 MACvIntEnable(pDevice->PortOffset, IMR_MASK_VALUE);
1992 if (pDevice->pMgmt->eConfigMode == WMAC_CONFIG_AP) {
1993 bScheduleCommand((void *)pDevice, WLAN_CMD_RUN_AP, NULL);
1995 else {
1996 bScheduleCommand((void *)pDevice, WLAN_CMD_BSSID_SCAN, NULL);
1997 bScheduleCommand((void *)pDevice, WLAN_CMD_SSID, NULL);
1999 pDevice->flags |=DEVICE_FLAGS_OPENED;
2001 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "device_open success.. \n");
2002 return 0;
2006 static int device_close(struct net_device *dev) {
2007 PSDevice pDevice=(PSDevice) netdev_priv(dev);
2008 PSMgmtObject pMgmt = pDevice->pMgmt;
2009 //PLICE_DEBUG->
2010 #ifdef THREAD
2011 mlme_kill = 0;
2012 #endif
2013 //PLICE_DEBUG<-
2014 //2007-1121-02<Add>by EinsnLiu
2015 if (pDevice->bLinkPass) {
2016 bScheduleCommand((void *)pDevice, WLAN_CMD_DISASSOCIATE, NULL);
2017 mdelay(30);
2019 #ifdef TxInSleep
2020 del_timer(&pDevice->sTimerTxData);
2021 #endif
2022 del_timer(&pDevice->sTimerCommand);
2023 del_timer(&pMgmt->sTimerSecondCallback);
2024 if (pDevice->bDiversityRegCtlON) {
2025 del_timer(&pDevice->TimerSQ3Tmax1);
2026 del_timer(&pDevice->TimerSQ3Tmax2);
2027 del_timer(&pDevice->TimerSQ3Tmax3);
2030 #ifdef TASK_LET
2031 tasklet_kill(&pDevice->RxMngWorkItem);
2032 #endif
2033 netif_stop_queue(dev);
2034 pDevice->bCmdRunning = false;
2035 MACbShutdown(pDevice->PortOffset);
2036 MACbSoftwareReset(pDevice->PortOffset);
2037 CARDbRadioPowerOff(pDevice);
2039 pDevice->bLinkPass = false;
2040 memset(pMgmt->abyCurrBSSID, 0, 6);
2041 pMgmt->eCurrState = WMAC_STATE_IDLE;
2042 device_free_td0_ring(pDevice);
2043 device_free_td1_ring(pDevice);
2044 device_free_rd0_ring(pDevice);
2045 device_free_rd1_ring(pDevice);
2046 device_free_frag_buf(pDevice);
2047 device_free_rings(pDevice);
2048 BSSvClearNodeDBTable(pDevice, 0);
2049 free_irq(dev->irq, dev);
2050 pDevice->flags &=(~DEVICE_FLAGS_OPENED);
2051 //2008-0714-01<Add>by chester
2052 device_release_WPADEV(pDevice);
2053 //PLICE_DEBUG->
2054 //tasklet_kill(&pDevice->RxMngWorkItem);
2055 //PLICE_DEBUG<-
2056 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "device_close.. \n");
2057 return 0;
2062 static int device_dma0_tx_80211(struct sk_buff *skb, struct net_device *dev) {
2063 PSDevice pDevice=netdev_priv(dev);
2064 unsigned char *pbMPDU;
2065 unsigned int cbMPDULen = 0;
2068 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "device_dma0_tx_80211\n");
2069 spin_lock_irq(&pDevice->lock);
2071 if (AVAIL_TD(pDevice, TYPE_TXDMA0) <= 0) {
2072 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "device_dma0_tx_80211, td0 <=0\n");
2073 dev_kfree_skb_irq(skb);
2074 spin_unlock_irq(&pDevice->lock);
2075 return 0;
2078 if (pDevice->bStopTx0Pkt == true) {
2079 dev_kfree_skb_irq(skb);
2080 spin_unlock_irq(&pDevice->lock);
2081 return 0;
2084 cbMPDULen = skb->len;
2085 pbMPDU = skb->data;
2087 vDMA0_tx_80211(pDevice, skb, pbMPDU, cbMPDULen);
2089 spin_unlock_irq(&pDevice->lock);
2091 return 0;
2097 bool device_dma0_xmit(PSDevice pDevice, struct sk_buff *skb, unsigned int uNodeIndex) {
2098 PSMgmtObject pMgmt = pDevice->pMgmt;
2099 PSTxDesc pHeadTD, pLastTD;
2100 unsigned int cbFrameBodySize;
2101 unsigned int uMACfragNum;
2102 unsigned char byPktType;
2103 bool bNeedEncryption = false;
2104 PSKeyItem pTransmitKey = NULL;
2105 unsigned int cbHeaderSize;
2106 unsigned int ii;
2107 SKeyItem STempKey;
2108 // unsigned char byKeyIndex = 0;
2111 if (pDevice->bStopTx0Pkt == true) {
2112 dev_kfree_skb_irq(skb);
2113 return false;
2116 if (AVAIL_TD(pDevice, TYPE_TXDMA0) <= 0) {
2117 dev_kfree_skb_irq(skb);
2118 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "device_dma0_xmit, td0 <=0\n");
2119 return false;
2122 if (pMgmt->eCurrMode == WMAC_MODE_ESS_AP) {
2123 if (pDevice->uAssocCount == 0) {
2124 dev_kfree_skb_irq(skb);
2125 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "device_dma0_xmit, assocCount = 0\n");
2126 return false;
2130 pHeadTD = pDevice->apCurrTD[TYPE_TXDMA0];
2132 pHeadTD->m_td1TD1.byTCR = (TCR_EDP|TCR_STP);
2134 memcpy(pDevice->sTxEthHeader.abyDstAddr, (unsigned char *)(skb->data), ETH_HLEN);
2135 cbFrameBodySize = skb->len - ETH_HLEN;
2137 // 802.1H
2138 if (ntohs(pDevice->sTxEthHeader.wType) > ETH_DATA_LEN) {
2139 cbFrameBodySize += 8;
2141 uMACfragNum = cbGetFragCount(pDevice, pTransmitKey, cbFrameBodySize, &pDevice->sTxEthHeader);
2143 if ( uMACfragNum > AVAIL_TD(pDevice, TYPE_TXDMA0)) {
2144 dev_kfree_skb_irq(skb);
2145 return false;
2147 byPktType = (unsigned char)pDevice->byPacketType;
2150 if (pDevice->bFixRate) {
2151 if (pDevice->eCurrentPHYType == PHY_TYPE_11B) {
2152 if (pDevice->uConnectionRate >= RATE_11M) {
2153 pDevice->wCurrentRate = RATE_11M;
2154 } else {
2155 pDevice->wCurrentRate = (unsigned short)pDevice->uConnectionRate;
2157 } else {
2158 if (pDevice->uConnectionRate >= RATE_54M)
2159 pDevice->wCurrentRate = RATE_54M;
2160 else
2161 pDevice->wCurrentRate = (unsigned short)pDevice->uConnectionRate;
2164 else {
2165 pDevice->wCurrentRate = pDevice->pMgmt->sNodeDBTable[uNodeIndex].wTxDataRate;
2168 //preamble type
2169 if (pMgmt->sNodeDBTable[uNodeIndex].bShortPreamble) {
2170 pDevice->byPreambleType = pDevice->byShortPreamble;
2172 else {
2173 pDevice->byPreambleType = PREAMBLE_LONG;
2176 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "dma0: pDevice->wCurrentRate = %d \n", pDevice->wCurrentRate);
2179 if (pDevice->wCurrentRate <= RATE_11M) {
2180 byPktType = PK_TYPE_11B;
2181 } else if (pDevice->eCurrentPHYType == PHY_TYPE_11A) {
2182 byPktType = PK_TYPE_11A;
2183 } else {
2184 if (pDevice->bProtectMode == true) {
2185 byPktType = PK_TYPE_11GB;
2186 } else {
2187 byPktType = PK_TYPE_11GA;
2191 if (pDevice->bEncryptionEnable == true)
2192 bNeedEncryption = true;
2194 if (pDevice->bEnableHostWEP) {
2195 pTransmitKey = &STempKey;
2196 pTransmitKey->byCipherSuite = pMgmt->sNodeDBTable[uNodeIndex].byCipherSuite;
2197 pTransmitKey->dwKeyIndex = pMgmt->sNodeDBTable[uNodeIndex].dwKeyIndex;
2198 pTransmitKey->uKeyLength = pMgmt->sNodeDBTable[uNodeIndex].uWepKeyLength;
2199 pTransmitKey->dwTSC47_16 = pMgmt->sNodeDBTable[uNodeIndex].dwTSC47_16;
2200 pTransmitKey->wTSC15_0 = pMgmt->sNodeDBTable[uNodeIndex].wTSC15_0;
2201 memcpy(pTransmitKey->abyKey,
2202 &pMgmt->sNodeDBTable[uNodeIndex].abyWepKey[0],
2203 pTransmitKey->uKeyLength
2206 vGenerateFIFOHeader(pDevice, byPktType, pDevice->pbyTmpBuff, bNeedEncryption,
2207 cbFrameBodySize, TYPE_TXDMA0, pHeadTD,
2208 &pDevice->sTxEthHeader, (unsigned char *)skb->data, pTransmitKey, uNodeIndex,
2209 &uMACfragNum,
2210 &cbHeaderSize
2213 if (MACbIsRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_PS)) {
2214 // Disable PS
2215 MACbPSWakeup(pDevice->PortOffset);
2218 pDevice->bPWBitOn = false;
2220 pLastTD = pHeadTD;
2221 for (ii = 0; ii < uMACfragNum; ii++) {
2222 // Poll Transmit the adapter
2223 wmb();
2224 pHeadTD->m_td0TD0.f1Owner=OWNED_BY_NIC;
2225 wmb();
2226 if (ii == (uMACfragNum - 1))
2227 pLastTD = pHeadTD;
2228 pHeadTD = pHeadTD->next;
2231 // Save the information needed by the tx interrupt handler
2232 // to complete the Send request
2233 pLastTD->pTDInfo->skb = skb;
2234 pLastTD->pTDInfo->byFlags = 0;
2235 pLastTD->pTDInfo->byFlags |= TD_FLAGS_NETIF_SKB;
2237 pDevice->apCurrTD[TYPE_TXDMA0] = pHeadTD;
2239 MACvTransmit0(pDevice->PortOffset);
2242 return true;
2245 //TYPE_AC0DMA data tx
2246 static int device_xmit(struct sk_buff *skb, struct net_device *dev) {
2247 PSDevice pDevice=netdev_priv(dev);
2249 PSMgmtObject pMgmt = pDevice->pMgmt;
2250 PSTxDesc pHeadTD, pLastTD;
2251 unsigned int uNodeIndex = 0;
2252 unsigned char byMask[8] = {1, 2, 4, 8, 0x10, 0x20, 0x40, 0x80};
2253 unsigned short wAID;
2254 unsigned int uMACfragNum = 1;
2255 unsigned int cbFrameBodySize;
2256 unsigned char byPktType;
2257 unsigned int cbHeaderSize;
2258 bool bNeedEncryption = false;
2259 PSKeyItem pTransmitKey = NULL;
2260 SKeyItem STempKey;
2261 unsigned int ii;
2262 bool bTKIP_UseGTK = false;
2263 bool bNeedDeAuth = false;
2264 unsigned char *pbyBSSID;
2265 bool bNodeExist = false;
2269 spin_lock_irq(&pDevice->lock);
2270 if (pDevice->bLinkPass == false) {
2271 dev_kfree_skb_irq(skb);
2272 spin_unlock_irq(&pDevice->lock);
2273 return 0;
2276 if (pDevice->bStopDataPkt) {
2277 dev_kfree_skb_irq(skb);
2278 spin_unlock_irq(&pDevice->lock);
2279 return 0;
2283 if (pMgmt->eCurrMode == WMAC_MODE_ESS_AP) {
2284 if (pDevice->uAssocCount == 0) {
2285 dev_kfree_skb_irq(skb);
2286 spin_unlock_irq(&pDevice->lock);
2287 return 0;
2289 if (is_multicast_ether_addr((unsigned char *)(skb->data))) {
2290 uNodeIndex = 0;
2291 bNodeExist = true;
2292 if (pMgmt->sNodeDBTable[0].bPSEnable) {
2293 skb_queue_tail(&(pMgmt->sNodeDBTable[0].sTxPSQueue), skb);
2294 pMgmt->sNodeDBTable[0].wEnQueueCnt++;
2295 // set tx map
2296 pMgmt->abyPSTxMap[0] |= byMask[0];
2297 spin_unlock_irq(&pDevice->lock);
2298 return 0;
2300 }else {
2301 if (BSSDBbIsSTAInNodeDB(pMgmt, (unsigned char *)(skb->data), &uNodeIndex)) {
2302 if (pMgmt->sNodeDBTable[uNodeIndex].bPSEnable) {
2303 skb_queue_tail(&pMgmt->sNodeDBTable[uNodeIndex].sTxPSQueue, skb);
2304 pMgmt->sNodeDBTable[uNodeIndex].wEnQueueCnt++;
2305 // set tx map
2306 wAID = pMgmt->sNodeDBTable[uNodeIndex].wAID;
2307 pMgmt->abyPSTxMap[wAID >> 3] |= byMask[wAID & 7];
2308 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Set:pMgmt->abyPSTxMap[%d]= %d\n",
2309 (wAID >> 3), pMgmt->abyPSTxMap[wAID >> 3]);
2310 spin_unlock_irq(&pDevice->lock);
2311 return 0;
2314 if (pMgmt->sNodeDBTable[uNodeIndex].bShortPreamble) {
2315 pDevice->byPreambleType = pDevice->byShortPreamble;
2317 }else {
2318 pDevice->byPreambleType = PREAMBLE_LONG;
2320 bNodeExist = true;
2325 if (bNodeExist == false) {
2326 DBG_PRT(MSG_LEVEL_DEBUG, KERN_DEBUG"Unknown STA not found in node DB \n");
2327 dev_kfree_skb_irq(skb);
2328 spin_unlock_irq(&pDevice->lock);
2329 return 0;
2333 pHeadTD = pDevice->apCurrTD[TYPE_AC0DMA];
2335 pHeadTD->m_td1TD1.byTCR = (TCR_EDP|TCR_STP);
2338 memcpy(pDevice->sTxEthHeader.abyDstAddr, (unsigned char *)(skb->data), ETH_HLEN);
2339 cbFrameBodySize = skb->len - ETH_HLEN;
2340 // 802.1H
2341 if (ntohs(pDevice->sTxEthHeader.wType) > ETH_DATA_LEN) {
2342 cbFrameBodySize += 8;
2346 if (pDevice->bEncryptionEnable == true) {
2347 bNeedEncryption = true;
2348 // get Transmit key
2349 do {
2350 if ((pDevice->pMgmt->eCurrMode == WMAC_MODE_ESS_STA) &&
2351 (pDevice->pMgmt->eCurrState == WMAC_STATE_ASSOC)) {
2352 pbyBSSID = pDevice->abyBSSID;
2353 // get pairwise key
2354 if (KeybGetTransmitKey(&(pDevice->sKey), pbyBSSID, PAIRWISE_KEY, &pTransmitKey) == false) {
2355 // get group key
2356 if(KeybGetTransmitKey(&(pDevice->sKey), pbyBSSID, GROUP_KEY, &pTransmitKey) == true) {
2357 bTKIP_UseGTK = true;
2358 DBG_PRT(MSG_LEVEL_DEBUG, KERN_DEBUG"Get GTK.\n");
2359 break;
2361 } else {
2362 DBG_PRT(MSG_LEVEL_DEBUG, KERN_DEBUG"Get PTK.\n");
2363 break;
2365 }else if (pDevice->pMgmt->eCurrMode == WMAC_MODE_IBSS_STA) {
2367 pbyBSSID = pDevice->sTxEthHeader.abyDstAddr; //TO_DS = 0 and FROM_DS = 0 --> 802.11 MAC Address1
2368 DBG_PRT(MSG_LEVEL_DEBUG, KERN_DEBUG"IBSS Serach Key: \n");
2369 for (ii = 0; ii< 6; ii++)
2370 DBG_PRT(MSG_LEVEL_DEBUG, KERN_DEBUG"%x \n", *(pbyBSSID+ii));
2371 DBG_PRT(MSG_LEVEL_DEBUG, KERN_DEBUG"\n");
2373 // get pairwise key
2374 if(KeybGetTransmitKey(&(pDevice->sKey), pbyBSSID, PAIRWISE_KEY, &pTransmitKey) == true)
2375 break;
2377 // get group key
2378 pbyBSSID = pDevice->abyBroadcastAddr;
2379 if(KeybGetTransmitKey(&(pDevice->sKey), pbyBSSID, GROUP_KEY, &pTransmitKey) == false) {
2380 pTransmitKey = NULL;
2381 if (pDevice->pMgmt->eCurrMode == WMAC_MODE_IBSS_STA) {
2382 DBG_PRT(MSG_LEVEL_DEBUG, KERN_DEBUG"IBSS and KEY is NULL. [%d]\n", pDevice->pMgmt->eCurrMode);
2384 else
2385 DBG_PRT(MSG_LEVEL_DEBUG, KERN_DEBUG"NOT IBSS and KEY is NULL. [%d]\n", pDevice->pMgmt->eCurrMode);
2386 } else {
2387 bTKIP_UseGTK = true;
2388 DBG_PRT(MSG_LEVEL_DEBUG, KERN_DEBUG"Get GTK.\n");
2390 } while(false);
2393 if (pDevice->bEnableHostWEP) {
2394 DBG_PRT(MSG_LEVEL_DEBUG, KERN_DEBUG"acdma0: STA index %d\n", uNodeIndex);
2395 if (pDevice->bEncryptionEnable == true) {
2396 pTransmitKey = &STempKey;
2397 pTransmitKey->byCipherSuite = pMgmt->sNodeDBTable[uNodeIndex].byCipherSuite;
2398 pTransmitKey->dwKeyIndex = pMgmt->sNodeDBTable[uNodeIndex].dwKeyIndex;
2399 pTransmitKey->uKeyLength = pMgmt->sNodeDBTable[uNodeIndex].uWepKeyLength;
2400 pTransmitKey->dwTSC47_16 = pMgmt->sNodeDBTable[uNodeIndex].dwTSC47_16;
2401 pTransmitKey->wTSC15_0 = pMgmt->sNodeDBTable[uNodeIndex].wTSC15_0;
2402 memcpy(pTransmitKey->abyKey,
2403 &pMgmt->sNodeDBTable[uNodeIndex].abyWepKey[0],
2404 pTransmitKey->uKeyLength
2409 uMACfragNum = cbGetFragCount(pDevice, pTransmitKey, cbFrameBodySize, &pDevice->sTxEthHeader);
2411 if (uMACfragNum > AVAIL_TD(pDevice, TYPE_AC0DMA)) {
2412 DBG_PRT(MSG_LEVEL_ERR, KERN_DEBUG "uMACfragNum > AVAIL_TD(TYPE_AC0DMA) = %d\n", uMACfragNum);
2413 dev_kfree_skb_irq(skb);
2414 spin_unlock_irq(&pDevice->lock);
2415 return 0;
2418 if (pTransmitKey != NULL) {
2419 if ((pTransmitKey->byCipherSuite == KEY_CTL_WEP) &&
2420 (pTransmitKey->uKeyLength == WLAN_WEP232_KEYLEN)) {
2421 uMACfragNum = 1; //WEP256 doesn't support fragment
2425 byPktType = (unsigned char)pDevice->byPacketType;
2427 if (pDevice->bFixRate) {
2428 #ifdef PLICE_DEBUG
2429 printk("Fix Rate: PhyType is %d,ConnectionRate is %d\n",pDevice->eCurrentPHYType,pDevice->uConnectionRate);
2430 #endif
2432 if (pDevice->eCurrentPHYType == PHY_TYPE_11B) {
2433 if (pDevice->uConnectionRate >= RATE_11M) {
2434 pDevice->wCurrentRate = RATE_11M;
2435 } else {
2436 pDevice->wCurrentRate = (unsigned short)pDevice->uConnectionRate;
2438 } else {
2439 if ((pDevice->eCurrentPHYType == PHY_TYPE_11A) &&
2440 (pDevice->uConnectionRate <= RATE_6M)) {
2441 pDevice->wCurrentRate = RATE_6M;
2442 } else {
2443 if (pDevice->uConnectionRate >= RATE_54M)
2444 pDevice->wCurrentRate = RATE_54M;
2445 else
2446 pDevice->wCurrentRate = (unsigned short)pDevice->uConnectionRate;
2450 pDevice->byACKRate = (unsigned char) pDevice->wCurrentRate;
2451 pDevice->byTopCCKBasicRate = RATE_1M;
2452 pDevice->byTopOFDMBasicRate = RATE_6M;
2454 else {
2455 //auto rate
2456 if (pDevice->sTxEthHeader.wType == TYPE_PKT_802_1x) {
2457 if (pDevice->eCurrentPHYType != PHY_TYPE_11A) {
2458 pDevice->wCurrentRate = RATE_1M;
2459 pDevice->byACKRate = RATE_1M;
2460 pDevice->byTopCCKBasicRate = RATE_1M;
2461 pDevice->byTopOFDMBasicRate = RATE_6M;
2462 } else {
2463 pDevice->wCurrentRate = RATE_6M;
2464 pDevice->byACKRate = RATE_6M;
2465 pDevice->byTopCCKBasicRate = RATE_1M;
2466 pDevice->byTopOFDMBasicRate = RATE_6M;
2469 else {
2470 VNTWIFIvGetTxRate( pDevice->pMgmt,
2471 pDevice->sTxEthHeader.abyDstAddr,
2472 &(pDevice->wCurrentRate),
2473 &(pDevice->byACKRate),
2474 &(pDevice->byTopCCKBasicRate),
2475 &(pDevice->byTopOFDMBasicRate));
2483 // DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "acdma0: pDevice->wCurrentRate = %d \n", pDevice->wCurrentRate);
2485 if (pDevice->wCurrentRate <= RATE_11M) {
2486 byPktType = PK_TYPE_11B;
2487 } else if (pDevice->eCurrentPHYType == PHY_TYPE_11A) {
2488 byPktType = PK_TYPE_11A;
2489 } else {
2490 if (pDevice->bProtectMode == true) {
2491 byPktType = PK_TYPE_11GB;
2492 } else {
2493 byPktType = PK_TYPE_11GA;
2497 //#ifdef PLICE_DEBUG
2498 // printk("FIX RATE:CurrentRate is %d");
2499 //#endif
2501 if (bNeedEncryption == true) {
2502 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ntohs Pkt Type=%04x\n", ntohs(pDevice->sTxEthHeader.wType));
2503 if ((pDevice->sTxEthHeader.wType) == TYPE_PKT_802_1x) {
2504 bNeedEncryption = false;
2505 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Pkt Type=%04x\n", (pDevice->sTxEthHeader.wType));
2506 if ((pDevice->pMgmt->eCurrMode == WMAC_MODE_ESS_STA) && (pDevice->pMgmt->eCurrState == WMAC_STATE_ASSOC)) {
2507 if (pTransmitKey == NULL) {
2508 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Don't Find TX KEY\n");
2510 else {
2511 if (bTKIP_UseGTK == true) {
2512 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"error: KEY is GTK!!~~\n");
2514 else {
2515 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Find PTK [%lX]\n", pTransmitKey->dwKeyIndex);
2516 bNeedEncryption = true;
2521 if (pDevice->byCntMeasure == 2) {
2522 bNeedDeAuth = true;
2523 pDevice->s802_11Counter.TKIPCounterMeasuresInvoked++;
2526 if (pDevice->bEnableHostWEP) {
2527 if ((uNodeIndex != 0) &&
2528 (pMgmt->sNodeDBTable[uNodeIndex].dwKeyIndex & PAIRWISE_KEY)) {
2529 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Find PTK [%lX]\n", pTransmitKey->dwKeyIndex);
2530 bNeedEncryption = true;
2534 else {
2535 if (pTransmitKey == NULL) {
2536 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"return no tx key\n");
2537 dev_kfree_skb_irq(skb);
2538 spin_unlock_irq(&pDevice->lock);
2539 return 0;
2545 #ifdef PLICE_DEBUG
2546 //if (skb->len == 98)
2548 // printk("ping:len is %d\n");
2550 #endif
2551 vGenerateFIFOHeader(pDevice, byPktType, pDevice->pbyTmpBuff, bNeedEncryption,
2552 cbFrameBodySize, TYPE_AC0DMA, pHeadTD,
2553 &pDevice->sTxEthHeader, (unsigned char *)skb->data, pTransmitKey, uNodeIndex,
2554 &uMACfragNum,
2555 &cbHeaderSize
2558 if (MACbIsRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_PS)) {
2559 // Disable PS
2560 MACbPSWakeup(pDevice->PortOffset);
2562 pDevice->bPWBitOn = false;
2564 pLastTD = pHeadTD;
2565 for (ii = 0; ii < uMACfragNum; ii++) {
2566 // Poll Transmit the adapter
2567 wmb();
2568 pHeadTD->m_td0TD0.f1Owner=OWNED_BY_NIC;
2569 wmb();
2570 if (ii == uMACfragNum - 1)
2571 pLastTD = pHeadTD;
2572 pHeadTD = pHeadTD->next;
2575 // Save the information needed by the tx interrupt handler
2576 // to complete the Send request
2577 pLastTD->pTDInfo->skb = skb;
2578 pLastTD->pTDInfo->byFlags = 0;
2579 pLastTD->pTDInfo->byFlags |= TD_FLAGS_NETIF_SKB;
2580 #ifdef TxInSleep
2581 pDevice->nTxDataTimeCout=0; //2008-8-21 chester <add> for send null packet
2582 #endif
2583 if (AVAIL_TD(pDevice, TYPE_AC0DMA) <= 1) {
2584 netif_stop_queue(dev);
2587 pDevice->apCurrTD[TYPE_AC0DMA] = pHeadTD;
2588 //#ifdef PLICE_DEBUG
2589 if (pDevice->bFixRate)
2591 printk("FixRate:Rate is %d,TxPower is %d\n",pDevice->wCurrentRate,pDevice->byCurPwr);
2593 else
2595 //printk("Auto Rate:Rate is %d,TxPower is %d\n",pDevice->wCurrentRate,pDevice->byCurPwr);
2597 //#endif
2600 unsigned char Protocol_Version; //802.1x Authentication
2601 unsigned char Packet_Type; //802.1x Authentication
2602 unsigned char Descriptor_type;
2603 unsigned short Key_info;
2604 bool bTxeapol_key = false;
2605 Protocol_Version = skb->data[ETH_HLEN];
2606 Packet_Type = skb->data[ETH_HLEN+1];
2607 Descriptor_type = skb->data[ETH_HLEN+1+1+2];
2608 Key_info = (skb->data[ETH_HLEN+1+1+2+1] << 8)|(skb->data[ETH_HLEN+1+1+2+2]);
2609 if (pDevice->sTxEthHeader.wType == TYPE_PKT_802_1x) {
2610 if(((Protocol_Version==1) ||(Protocol_Version==2)) &&
2611 (Packet_Type==3)) { //802.1x OR eapol-key challenge frame transfer
2612 bTxeapol_key = true;
2613 if((Descriptor_type==254)||(Descriptor_type==2)) { //WPA or RSN
2614 if(!(Key_info & BIT3) && //group-key challenge
2615 (Key_info & BIT8) && (Key_info & BIT9)) { //send 2/2 key
2616 pDevice->fWPA_Authened = true;
2617 if(Descriptor_type==254)
2618 printk("WPA ");
2619 else
2620 printk("WPA2 ");
2621 printk("Authentication completed!!\n");
2628 MACvTransmitAC0(pDevice->PortOffset);
2629 // DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "acdma0:pDevice->apCurrTD= %p\n", pHeadTD);
2631 dev->trans_start = jiffies;
2633 spin_unlock_irq(&pDevice->lock);
2634 return 0;
2638 static irqreturn_t device_intr(int irq, void *dev_instance) {
2639 struct net_device* dev=dev_instance;
2640 PSDevice pDevice=(PSDevice) netdev_priv(dev);
2642 int max_count=0;
2643 unsigned long dwMIBCounter=0;
2644 PSMgmtObject pMgmt = pDevice->pMgmt;
2645 unsigned char byOrgPageSel=0;
2646 int handled = 0;
2647 unsigned char byData = 0;
2648 int ii= 0;
2649 // unsigned char byRSSI;
2652 MACvReadISR(pDevice->PortOffset, &pDevice->dwIsr);
2654 if (pDevice->dwIsr == 0)
2655 return IRQ_RETVAL(handled);
2657 if (pDevice->dwIsr == 0xffffffff) {
2658 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "dwIsr = 0xffff\n");
2659 return IRQ_RETVAL(handled);
2662 // 2008-05-21 <mark> by Richardtai, we can't read RSSI here, because no packet bound with RSSI
2664 if ((pDevice->dwIsr & ISR_RXDMA0) &&
2665 (pDevice->byLocalID != REV_ID_VT3253_B0) &&
2666 (pDevice->bBSSIDFilter == true)) {
2667 // update RSSI
2668 //BBbReadEmbeded(pDevice->PortOffset, 0x3E, &byRSSI);
2669 //pDevice->uCurrRSSI = byRSSI;
2673 handled = 1;
2674 MACvIntDisable(pDevice->PortOffset);
2675 spin_lock_irq(&pDevice->lock);
2677 //Make sure current page is 0
2678 VNSvInPortB(pDevice->PortOffset + MAC_REG_PAGE1SEL, &byOrgPageSel);
2679 if (byOrgPageSel == 1) {
2680 MACvSelectPage0(pDevice->PortOffset);
2682 else
2683 byOrgPageSel = 0;
2685 MACvReadMIBCounter(pDevice->PortOffset, &dwMIBCounter);
2686 // TBD....
2687 // Must do this after doing rx/tx, cause ISR bit is slow
2688 // than RD/TD write back
2689 // update ISR counter
2690 STAvUpdate802_11Counter(&pDevice->s802_11Counter, &pDevice->scStatistic , dwMIBCounter);
2691 while (pDevice->dwIsr != 0) {
2693 STAvUpdateIsrStatCounter(&pDevice->scStatistic, pDevice->dwIsr);
2694 MACvWriteISR(pDevice->PortOffset, pDevice->dwIsr);
2696 if (pDevice->dwIsr & ISR_FETALERR){
2697 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " ISR_FETALERR \n");
2698 VNSvOutPortB(pDevice->PortOffset + MAC_REG_SOFTPWRCTL, 0);
2699 VNSvOutPortW(pDevice->PortOffset + MAC_REG_SOFTPWRCTL, SOFTPWRCTL_SWPECTI);
2700 device_error(pDevice, pDevice->dwIsr);
2703 if (pDevice->byLocalID > REV_ID_VT3253_B1) {
2705 if (pDevice->dwIsr & ISR_MEASURESTART) {
2706 // 802.11h measure start
2707 pDevice->byOrgChannel = pDevice->byCurrentCh;
2708 VNSvInPortB(pDevice->PortOffset + MAC_REG_RCR, &(pDevice->byOrgRCR));
2709 VNSvOutPortB(pDevice->PortOffset + MAC_REG_RCR, (RCR_RXALLTYPE | RCR_UNICAST | RCR_BROADCAST | RCR_MULTICAST | RCR_WPAERR));
2710 MACvSelectPage1(pDevice->PortOffset);
2711 VNSvInPortD(pDevice->PortOffset + MAC_REG_MAR0, &(pDevice->dwOrgMAR0));
2712 VNSvInPortD(pDevice->PortOffset + MAC_REG_MAR4, &(pDevice->dwOrgMAR4));
2713 MACvSelectPage0(pDevice->PortOffset);
2714 // WCMDbFlushCommandQueue(pDevice->pMgmt, true);
2715 if (set_channel(pDevice, pDevice->pCurrMeasureEID->sReq.byChannel) == true) {
2716 pDevice->bMeasureInProgress = true;
2717 MACvSelectPage1(pDevice->PortOffset);
2718 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_MSRCTL, MSRCTL_READY);
2719 MACvSelectPage0(pDevice->PortOffset);
2720 pDevice->byBasicMap = 0;
2721 pDevice->byCCAFraction = 0;
2722 for(ii=0;ii<8;ii++) {
2723 pDevice->dwRPIs[ii] = 0;
2725 } else {
2726 // can not measure because set channel fail
2727 // WCMDbResetCommandQueue(pDevice->pMgmt);
2728 // clear measure control
2729 MACvRegBitsOff(pDevice->PortOffset, MAC_REG_MSRCTL, MSRCTL_EN);
2730 s_vCompleteCurrentMeasure(pDevice, MEASURE_MODE_INCAPABLE);
2731 MACvSelectPage1(pDevice->PortOffset);
2732 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_MSRCTL+1, MSRCTL1_TXPAUSE);
2733 MACvSelectPage0(pDevice->PortOffset);
2736 if (pDevice->dwIsr & ISR_MEASUREEND) {
2737 // 802.11h measure end
2738 pDevice->bMeasureInProgress = false;
2739 VNSvOutPortB(pDevice->PortOffset + MAC_REG_RCR, pDevice->byOrgRCR);
2740 MACvSelectPage1(pDevice->PortOffset);
2741 VNSvOutPortD(pDevice->PortOffset + MAC_REG_MAR0, pDevice->dwOrgMAR0);
2742 VNSvOutPortD(pDevice->PortOffset + MAC_REG_MAR4, pDevice->dwOrgMAR4);
2743 VNSvInPortB(pDevice->PortOffset + MAC_REG_MSRBBSTS, &byData);
2744 pDevice->byBasicMap |= (byData >> 4);
2745 VNSvInPortB(pDevice->PortOffset + MAC_REG_CCAFRACTION, &pDevice->byCCAFraction);
2746 VNSvInPortB(pDevice->PortOffset + MAC_REG_MSRCTL, &byData);
2747 // clear measure control
2748 MACvRegBitsOff(pDevice->PortOffset, MAC_REG_MSRCTL, MSRCTL_EN);
2749 MACvSelectPage0(pDevice->PortOffset);
2750 set_channel(pDevice, pDevice->byOrgChannel);
2751 // WCMDbResetCommandQueue(pDevice->pMgmt);
2752 MACvSelectPage1(pDevice->PortOffset);
2753 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_MSRCTL+1, MSRCTL1_TXPAUSE);
2754 MACvSelectPage0(pDevice->PortOffset);
2755 if (byData & MSRCTL_FINISH) {
2756 // measure success
2757 s_vCompleteCurrentMeasure(pDevice, 0);
2758 } else {
2759 // can not measure because not ready before end of measure time
2760 s_vCompleteCurrentMeasure(pDevice, MEASURE_MODE_LATE);
2763 if (pDevice->dwIsr & ISR_QUIETSTART) {
2764 do {
2766 } while (CARDbStartQuiet(pDevice) == false);
2770 if (pDevice->dwIsr & ISR_TBTT) {
2771 if (pDevice->bEnableFirstQuiet == true) {
2772 pDevice->byQuietStartCount--;
2773 if (pDevice->byQuietStartCount == 0) {
2774 pDevice->bEnableFirstQuiet = false;
2775 MACvSelectPage1(pDevice->PortOffset);
2776 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_MSRCTL, (MSRCTL_QUIETTXCHK | MSRCTL_QUIETEN));
2777 MACvSelectPage0(pDevice->PortOffset);
2780 if ((pDevice->bChannelSwitch == true) &&
2781 (pDevice->eOPMode == OP_MODE_INFRASTRUCTURE)) {
2782 pDevice->byChannelSwitchCount--;
2783 if (pDevice->byChannelSwitchCount == 0) {
2784 pDevice->bChannelSwitch = false;
2785 set_channel(pDevice, pDevice->byNewChannel);
2786 VNTWIFIbChannelSwitch(pDevice->pMgmt, pDevice->byNewChannel);
2787 MACvSelectPage1(pDevice->PortOffset);
2788 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_MSRCTL+1, MSRCTL1_TXPAUSE);
2789 MACvSelectPage0(pDevice->PortOffset);
2790 CARDbStartTxPacket(pDevice, PKT_TYPE_802_11_ALL);
2794 if (pDevice->eOPMode == OP_MODE_ADHOC) {
2795 //pDevice->bBeaconSent = false;
2796 } else {
2797 if ((pDevice->bUpdateBBVGA) && (pDevice->bLinkPass == true) && (pDevice->uCurrRSSI != 0)) {
2798 long ldBm;
2800 RFvRSSITodBm(pDevice, (unsigned char) pDevice->uCurrRSSI, &ldBm);
2801 for (ii=0;ii<BB_VGA_LEVEL;ii++) {
2802 if (ldBm < pDevice->ldBmThreshold[ii]) {
2803 pDevice->byBBVGANew = pDevice->abyBBVGA[ii];
2804 break;
2807 if (pDevice->byBBVGANew != pDevice->byBBVGACurrent) {
2808 pDevice->uBBVGADiffCount++;
2809 if (pDevice->uBBVGADiffCount == 1) {
2810 // first VGA diff gain
2811 BBvSetVGAGainOffset(pDevice, pDevice->byBBVGANew);
2812 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"First RSSI[%d] NewGain[%d] OldGain[%d] Count[%d]\n",
2813 (int)ldBm, pDevice->byBBVGANew, pDevice->byBBVGACurrent, (int)pDevice->uBBVGADiffCount);
2815 if (pDevice->uBBVGADiffCount >= BB_VGA_CHANGE_THRESHOLD) {
2816 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"RSSI[%d] NewGain[%d] OldGain[%d] Count[%d]\n",
2817 (int)ldBm, pDevice->byBBVGANew, pDevice->byBBVGACurrent, (int)pDevice->uBBVGADiffCount);
2818 BBvSetVGAGainOffset(pDevice, pDevice->byBBVGANew);
2820 } else {
2821 pDevice->uBBVGADiffCount = 1;
2826 pDevice->bBeaconSent = false;
2827 if (pDevice->bEnablePSMode) {
2828 PSbIsNextTBTTWakeUp((void *)pDevice);
2831 if ((pDevice->eOPMode == OP_MODE_AP) ||
2832 (pDevice->eOPMode == OP_MODE_ADHOC)) {
2834 MACvOneShotTimer1MicroSec(pDevice->PortOffset,
2835 (pMgmt->wIBSSBeaconPeriod - MAKE_BEACON_RESERVED) << 10);
2838 if (pDevice->eOPMode == OP_MODE_ADHOC && pDevice->pMgmt->wCurrATIMWindow > 0) {
2839 // todo adhoc PS mode
2844 if (pDevice->dwIsr & ISR_BNTX) {
2846 if (pDevice->eOPMode == OP_MODE_ADHOC) {
2847 pDevice->bIsBeaconBufReadySet = false;
2848 pDevice->cbBeaconBufReadySetCnt = 0;
2851 if (pDevice->eOPMode == OP_MODE_AP) {
2852 if(pMgmt->byDTIMCount > 0) {
2853 pMgmt->byDTIMCount --;
2854 pMgmt->sNodeDBTable[0].bRxPSPoll = false;
2856 else {
2857 if(pMgmt->byDTIMCount == 0) {
2858 // check if mutltcast tx bufferring
2859 pMgmt->byDTIMCount = pMgmt->byDTIMPeriod - 1;
2860 pMgmt->sNodeDBTable[0].bRxPSPoll = true;
2861 bScheduleCommand((void *)pDevice, WLAN_CMD_RX_PSPOLL, NULL);
2865 pDevice->bBeaconSent = true;
2867 if (pDevice->bChannelSwitch == true) {
2868 pDevice->byChannelSwitchCount--;
2869 if (pDevice->byChannelSwitchCount == 0) {
2870 pDevice->bChannelSwitch = false;
2871 set_channel(pDevice, pDevice->byNewChannel);
2872 VNTWIFIbChannelSwitch(pDevice->pMgmt, pDevice->byNewChannel);
2873 MACvSelectPage1(pDevice->PortOffset);
2874 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_MSRCTL+1, MSRCTL1_TXPAUSE);
2875 MACvSelectPage0(pDevice->PortOffset);
2876 //VNTWIFIbSendBeacon(pDevice->pMgmt);
2877 CARDbStartTxPacket(pDevice, PKT_TYPE_802_11_ALL);
2883 if (pDevice->dwIsr & ISR_RXDMA0) {
2884 max_count += device_rx_srv(pDevice, TYPE_RXDMA0);
2886 if (pDevice->dwIsr & ISR_RXDMA1) {
2887 max_count += device_rx_srv(pDevice, TYPE_RXDMA1);
2889 if (pDevice->dwIsr & ISR_TXDMA0){
2890 max_count += device_tx_srv(pDevice, TYPE_TXDMA0);
2892 if (pDevice->dwIsr & ISR_AC0DMA){
2893 max_count += device_tx_srv(pDevice, TYPE_AC0DMA);
2895 if (pDevice->dwIsr & ISR_SOFTTIMER) {
2898 if (pDevice->dwIsr & ISR_SOFTTIMER1) {
2899 if (pDevice->eOPMode == OP_MODE_AP) {
2900 if (pDevice->bShortSlotTime)
2901 pMgmt->wCurrCapInfo |= WLAN_SET_CAP_INFO_SHORTSLOTTIME(1);
2902 else
2903 pMgmt->wCurrCapInfo &= ~(WLAN_SET_CAP_INFO_SHORTSLOTTIME(1));
2905 bMgrPrepareBeaconToSend(pDevice, pMgmt);
2906 pDevice->byCntMeasure = 0;
2909 MACvReadISR(pDevice->PortOffset, &pDevice->dwIsr);
2911 MACvReceive0(pDevice->PortOffset);
2912 MACvReceive1(pDevice->PortOffset);
2914 if (max_count>pDevice->sOpts.int_works)
2915 break;
2918 if (byOrgPageSel == 1) {
2919 MACvSelectPage1(pDevice->PortOffset);
2922 spin_unlock_irq(&pDevice->lock);
2923 MACvIntEnable(pDevice->PortOffset, IMR_MASK_VALUE);
2925 return IRQ_RETVAL(handled);
2929 static unsigned const ethernet_polynomial = 0x04c11db7U;
2930 static inline u32 ether_crc(int length, unsigned char *data)
2932 int crc = -1;
2934 while(--length >= 0) {
2935 unsigned char current_octet = *data++;
2936 int bit;
2937 for (bit = 0; bit < 8; bit++, current_octet >>= 1) {
2938 crc = (crc << 1) ^
2939 ((crc < 0) ^ (current_octet & 1) ? ethernet_polynomial : 0);
2942 return crc;
2945 //2008-8-4 <add> by chester
2946 static int Config_FileGetParameter(unsigned char *string,
2947 unsigned char *dest, unsigned char *source)
2949 unsigned char buf1[100];
2950 int source_len = strlen(source);
2952 memset(buf1,0,100);
2953 strcat(buf1, string);
2954 strcat(buf1, "=");
2955 source+=strlen(buf1);
2957 memcpy(dest,source,source_len-strlen(buf1));
2958 return true;
2961 int Config_FileOperation(PSDevice pDevice,bool fwrite,unsigned char *Parameter) {
2962 unsigned char *config_path = CONFIG_PATH;
2963 unsigned char *buffer = NULL;
2964 unsigned char tmpbuffer[20];
2965 struct file *filp=NULL;
2966 mm_segment_t old_fs = get_fs();
2967 //int oldfsuid=0,oldfsgid=0;
2968 int result=0;
2970 set_fs (KERNEL_DS);
2972 /* Can't do this anymore, so we rely on correct filesystem permissions:
2973 //Make sure a caller can read or write power as root
2974 oldfsuid=current->cred->fsuid;
2975 oldfsgid=current->cred->fsgid;
2976 current->cred->fsuid = 0;
2977 current->cred->fsgid = 0;
2980 //open file
2981 filp = filp_open(config_path, O_RDWR, 0);
2982 if (IS_ERR(filp)) {
2983 printk("Config_FileOperation:open file fail?\n");
2984 result=-1;
2985 goto error2;
2988 if(!(filp->f_op) || !(filp->f_op->read) ||!(filp->f_op->write)) {
2989 printk("file %s cann't readable or writable?\n",config_path);
2990 result = -1;
2991 goto error1;
2994 buffer = kmalloc(1024, GFP_KERNEL);
2995 if(buffer==NULL) {
2996 printk("alllocate mem for file fail?\n");
2997 result = -1;
2998 goto error1;
3001 if(filp->f_op->read(filp, buffer, 1024, &filp->f_pos)<0) {
3002 printk("read file error?\n");
3003 result = -1;
3004 goto error1;
3007 if(Config_FileGetParameter("ZONETYPE",tmpbuffer,buffer)!=true) {
3008 printk("get parameter error?\n");
3009 result = -1;
3010 goto error1;
3013 if(memcmp(tmpbuffer,"USA",3)==0) {
3014 result=ZoneType_USA;
3016 else if(memcmp(tmpbuffer,"JAPAN",5)==0) {
3017 result=ZoneType_Japan;
3019 else if(memcmp(tmpbuffer,"EUROPE",5)==0) {
3020 result=ZoneType_Europe;
3022 else {
3023 result = -1;
3024 printk("Unknown Zonetype[%s]?\n",tmpbuffer);
3027 error1:
3028 if(buffer)
3029 kfree(buffer);
3031 if(filp_close(filp,NULL))
3032 printk("Config_FileOperation:close file fail\n");
3034 error2:
3035 set_fs (old_fs);
3038 current->cred->fsuid=oldfsuid;
3039 current->cred->fsgid=oldfsgid;
3042 return result;
3047 static void device_set_multi(struct net_device *dev) {
3048 PSDevice pDevice = (PSDevice) netdev_priv(dev);
3050 PSMgmtObject pMgmt = pDevice->pMgmt;
3051 u32 mc_filter[2];
3052 struct netdev_hw_addr *ha;
3055 VNSvInPortB(pDevice->PortOffset + MAC_REG_RCR, &(pDevice->byRxMode));
3057 if (dev->flags & IFF_PROMISC) { /* Set promiscuous. */
3058 DBG_PRT(MSG_LEVEL_ERR,KERN_NOTICE "%s: Promiscuous mode enabled.\n", dev->name);
3059 /* Unconditionally log net taps. */
3060 pDevice->byRxMode |= (RCR_MULTICAST|RCR_BROADCAST|RCR_UNICAST);
3062 else if ((netdev_mc_count(dev) > pDevice->multicast_limit)
3063 || (dev->flags & IFF_ALLMULTI)) {
3064 MACvSelectPage1(pDevice->PortOffset);
3065 VNSvOutPortD(pDevice->PortOffset + MAC_REG_MAR0, 0xffffffff);
3066 VNSvOutPortD(pDevice->PortOffset + MAC_REG_MAR0 + 4, 0xffffffff);
3067 MACvSelectPage0(pDevice->PortOffset);
3068 pDevice->byRxMode |= (RCR_MULTICAST|RCR_BROADCAST);
3070 else {
3071 memset(mc_filter, 0, sizeof(mc_filter));
3072 netdev_for_each_mc_addr(ha, dev) {
3073 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
3074 mc_filter[bit_nr >> 5] |= cpu_to_le32(1 << (bit_nr & 31));
3076 MACvSelectPage1(pDevice->PortOffset);
3077 VNSvOutPortD(pDevice->PortOffset + MAC_REG_MAR0, mc_filter[0]);
3078 VNSvOutPortD(pDevice->PortOffset + MAC_REG_MAR0 + 4, mc_filter[1]);
3079 MACvSelectPage0(pDevice->PortOffset);
3080 pDevice->byRxMode &= ~(RCR_UNICAST);
3081 pDevice->byRxMode |= (RCR_MULTICAST|RCR_BROADCAST);
3084 if (pMgmt->eConfigMode == WMAC_CONFIG_AP) {
3085 // If AP mode, don't enable RCR_UNICAST. Since hw only compare addr1 with local mac.
3086 pDevice->byRxMode |= (RCR_MULTICAST|RCR_BROADCAST);
3087 pDevice->byRxMode &= ~(RCR_UNICAST);
3090 VNSvOutPortB(pDevice->PortOffset + MAC_REG_RCR, pDevice->byRxMode);
3091 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "pDevice->byRxMode = %x\n", pDevice->byRxMode );
3095 static struct net_device_stats *device_get_stats(struct net_device *dev) {
3096 PSDevice pDevice=(PSDevice) netdev_priv(dev);
3098 return &pDevice->stats;
3103 static int device_ioctl(struct net_device *dev, struct ifreq *rq, int cmd) {
3104 PSDevice pDevice = (PSDevice)netdev_priv(dev);
3106 struct iwreq *wrq = (struct iwreq *) rq;
3107 int rc =0;
3108 PSMgmtObject pMgmt = pDevice->pMgmt;
3109 PSCmdRequest pReq;
3112 if (pMgmt == NULL) {
3113 rc = -EFAULT;
3114 return rc;
3117 switch(cmd) {
3119 case SIOCGIWNAME:
3120 rc = iwctl_giwname(dev, NULL, (char *)&(wrq->u.name), NULL);
3121 break;
3123 case SIOCGIWNWID: //0x8b03 support
3124 #ifdef WPA_SUPPLICANT_DRIVER_WEXT_SUPPORT
3125 rc = iwctl_giwnwid(dev, NULL, &(wrq->u.nwid), NULL);
3126 #else
3127 rc = -EOPNOTSUPP;
3128 #endif
3129 break;
3131 // Set frequency/channel
3132 case SIOCSIWFREQ:
3133 rc = iwctl_siwfreq(dev, NULL, &(wrq->u.freq), NULL);
3134 break;
3136 // Get frequency/channel
3137 case SIOCGIWFREQ:
3138 rc = iwctl_giwfreq(dev, NULL, &(wrq->u.freq), NULL);
3139 break;
3141 // Set desired network name (ESSID)
3142 case SIOCSIWESSID:
3145 char essid[IW_ESSID_MAX_SIZE+1];
3146 if (wrq->u.essid.length > IW_ESSID_MAX_SIZE) {
3147 rc = -E2BIG;
3148 break;
3150 if (copy_from_user(essid, wrq->u.essid.pointer,
3151 wrq->u.essid.length)) {
3152 rc = -EFAULT;
3153 break;
3155 rc = iwctl_siwessid(dev, NULL,
3156 &(wrq->u.essid), essid);
3158 break;
3161 // Get current network name (ESSID)
3162 case SIOCGIWESSID:
3165 char essid[IW_ESSID_MAX_SIZE+1];
3166 if (wrq->u.essid.pointer)
3167 rc = iwctl_giwessid(dev, NULL,
3168 &(wrq->u.essid), essid);
3169 if (copy_to_user(wrq->u.essid.pointer,
3170 essid,
3171 wrq->u.essid.length) )
3172 rc = -EFAULT;
3174 break;
3176 case SIOCSIWAP:
3178 rc = iwctl_siwap(dev, NULL, &(wrq->u.ap_addr), NULL);
3179 break;
3182 // Get current Access Point (BSSID)
3183 case SIOCGIWAP:
3184 rc = iwctl_giwap(dev, NULL, &(wrq->u.ap_addr), NULL);
3185 break;
3188 // Set desired station name
3189 case SIOCSIWNICKN:
3190 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCSIWNICKN \n");
3191 rc = -EOPNOTSUPP;
3192 break;
3194 // Get current station name
3195 case SIOCGIWNICKN:
3196 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCGIWNICKN \n");
3197 rc = -EOPNOTSUPP;
3198 break;
3200 // Set the desired bit-rate
3201 case SIOCSIWRATE:
3202 rc = iwctl_siwrate(dev, NULL, &(wrq->u.bitrate), NULL);
3203 break;
3205 // Get the current bit-rate
3206 case SIOCGIWRATE:
3208 rc = iwctl_giwrate(dev, NULL, &(wrq->u.bitrate), NULL);
3209 break;
3211 // Set the desired RTS threshold
3212 case SIOCSIWRTS:
3214 rc = iwctl_siwrts(dev, NULL, &(wrq->u.rts), NULL);
3215 break;
3217 // Get the current RTS threshold
3218 case SIOCGIWRTS:
3220 rc = iwctl_giwrts(dev, NULL, &(wrq->u.rts), NULL);
3221 break;
3223 // Set the desired fragmentation threshold
3224 case SIOCSIWFRAG:
3226 rc = iwctl_siwfrag(dev, NULL, &(wrq->u.frag), NULL);
3227 break;
3229 // Get the current fragmentation threshold
3230 case SIOCGIWFRAG:
3232 rc = iwctl_giwfrag(dev, NULL, &(wrq->u.frag), NULL);
3233 break;
3235 // Set mode of operation
3236 case SIOCSIWMODE:
3237 rc = iwctl_siwmode(dev, NULL, &(wrq->u.mode), NULL);
3238 break;
3240 // Get mode of operation
3241 case SIOCGIWMODE:
3242 rc = iwctl_giwmode(dev, NULL, &(wrq->u.mode), NULL);
3243 break;
3245 // Set WEP keys and mode
3246 case SIOCSIWENCODE:
3248 char abyKey[WLAN_WEP232_KEYLEN];
3250 if (wrq->u.encoding.pointer) {
3253 if (wrq->u.encoding.length > WLAN_WEP232_KEYLEN) {
3254 rc = -E2BIG;
3255 break;
3257 memset(abyKey, 0, WLAN_WEP232_KEYLEN);
3258 if (copy_from_user(abyKey,
3259 wrq->u.encoding.pointer,
3260 wrq->u.encoding.length)) {
3261 rc = -EFAULT;
3262 break;
3264 } else if (wrq->u.encoding.length != 0) {
3265 rc = -EINVAL;
3266 break;
3268 rc = iwctl_siwencode(dev, NULL, &(wrq->u.encoding), abyKey);
3270 break;
3272 // Get the WEP keys and mode
3273 case SIOCGIWENCODE:
3275 if (!capable(CAP_NET_ADMIN)) {
3276 rc = -EPERM;
3277 break;
3280 char abyKey[WLAN_WEP232_KEYLEN];
3282 rc = iwctl_giwencode(dev, NULL, &(wrq->u.encoding), abyKey);
3283 if (rc != 0) break;
3284 if (wrq->u.encoding.pointer) {
3285 if (copy_to_user(wrq->u.encoding.pointer,
3286 abyKey,
3287 wrq->u.encoding.length))
3288 rc = -EFAULT;
3291 break;
3293 // Get the current Tx-Power
3294 case SIOCGIWTXPOW:
3295 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCGIWTXPOW \n");
3296 rc = -EOPNOTSUPP;
3297 break;
3299 case SIOCSIWTXPOW:
3300 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCSIWTXPOW \n");
3301 rc = -EOPNOTSUPP;
3302 break;
3304 case SIOCSIWRETRY:
3306 rc = iwctl_siwretry(dev, NULL, &(wrq->u.retry), NULL);
3307 break;
3309 case SIOCGIWRETRY:
3311 rc = iwctl_giwretry(dev, NULL, &(wrq->u.retry), NULL);
3312 break;
3314 // Get range of parameters
3315 case SIOCGIWRANGE:
3318 struct iw_range range;
3320 rc = iwctl_giwrange(dev, NULL, &(wrq->u.data), (char *) &range);
3321 if (copy_to_user(wrq->u.data.pointer, &range, sizeof(struct iw_range)))
3322 rc = -EFAULT;
3325 break;
3327 case SIOCGIWPOWER:
3329 rc = iwctl_giwpower(dev, NULL, &(wrq->u.power), NULL);
3330 break;
3333 case SIOCSIWPOWER:
3335 rc = iwctl_siwpower(dev, NULL, &(wrq->u.power), NULL);
3336 break;
3339 case SIOCGIWSENS:
3341 rc = iwctl_giwsens(dev, NULL, &(wrq->u.sens), NULL);
3342 break;
3344 case SIOCSIWSENS:
3345 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCSIWSENS \n");
3346 rc = -EOPNOTSUPP;
3347 break;
3349 case SIOCGIWAPLIST:
3351 char buffer[IW_MAX_AP * (sizeof(struct sockaddr) + sizeof(struct iw_quality))];
3353 if (wrq->u.data.pointer) {
3354 rc = iwctl_giwaplist(dev, NULL, &(wrq->u.data), buffer);
3355 if (rc == 0) {
3356 if (copy_to_user(wrq->u.data.pointer,
3357 buffer,
3358 (wrq->u.data.length * (sizeof(struct sockaddr) + sizeof(struct iw_quality)))
3360 rc = -EFAULT;
3364 break;
3367 #ifdef WIRELESS_SPY
3368 // Set the spy list
3369 case SIOCSIWSPY:
3371 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCSIWSPY \n");
3372 rc = -EOPNOTSUPP;
3373 break;
3375 // Get the spy list
3376 case SIOCGIWSPY:
3378 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCGIWSPY \n");
3379 rc = -EOPNOTSUPP;
3380 break;
3382 #endif // WIRELESS_SPY
3384 case SIOCGIWPRIV:
3385 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCGIWPRIV \n");
3386 rc = -EOPNOTSUPP;
3388 if(wrq->u.data.pointer) {
3389 wrq->u.data.length = sizeof(iwctl_private_args) / sizeof( iwctl_private_args[0]);
3391 if(copy_to_user(wrq->u.data.pointer,
3392 (u_char *) iwctl_private_args,
3393 sizeof(iwctl_private_args)))
3394 rc = -EFAULT;
3397 break;
3400 //2008-0409-07, <Add> by Einsn Liu
3401 #ifdef WPA_SUPPLICANT_DRIVER_WEXT_SUPPORT
3402 case SIOCSIWAUTH:
3403 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCSIWAUTH \n");
3404 rc = iwctl_siwauth(dev, NULL, &(wrq->u.param), NULL);
3405 break;
3407 case SIOCGIWAUTH:
3408 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCGIWAUTH \n");
3409 rc = iwctl_giwauth(dev, NULL, &(wrq->u.param), NULL);
3410 break;
3412 case SIOCSIWGENIE:
3413 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCSIWGENIE \n");
3414 rc = iwctl_siwgenie(dev, NULL, &(wrq->u.data), wrq->u.data.pointer);
3415 break;
3417 case SIOCGIWGENIE:
3418 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCGIWGENIE \n");
3419 rc = iwctl_giwgenie(dev, NULL, &(wrq->u.data), wrq->u.data.pointer);
3420 break;
3422 case SIOCSIWENCODEEXT:
3424 char extra[sizeof(struct iw_encode_ext)+MAX_KEY_LEN+1];
3425 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCSIWENCODEEXT \n");
3426 if(wrq->u.encoding.pointer){
3427 memset(extra, 0, sizeof(struct iw_encode_ext)+MAX_KEY_LEN+1);
3428 if(wrq->u.encoding.length > (sizeof(struct iw_encode_ext)+ MAX_KEY_LEN)){
3429 rc = -E2BIG;
3430 break;
3432 if(copy_from_user(extra, wrq->u.encoding.pointer,wrq->u.encoding.length)){
3433 rc = -EFAULT;
3434 break;
3436 }else if(wrq->u.encoding.length != 0){
3437 rc = -EINVAL;
3438 break;
3440 rc = iwctl_siwencodeext(dev, NULL, &(wrq->u.encoding), extra);
3442 break;
3444 case SIOCGIWENCODEEXT:
3445 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCGIWENCODEEXT \n");
3446 rc = iwctl_giwencodeext(dev, NULL, &(wrq->u.encoding), NULL);
3447 break;
3449 case SIOCSIWMLME:
3450 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCSIWMLME \n");
3451 rc = iwctl_siwmlme(dev, NULL, &(wrq->u.data), wrq->u.data.pointer);
3452 break;
3454 #endif // #ifdef WPA_SUPPLICANT_DRIVER_WEXT_SUPPORT
3455 //End Add -- //2008-0409-07, <Add> by Einsn Liu
3457 case IOCTL_CMD_TEST:
3459 if (!(pDevice->flags & DEVICE_FLAGS_OPENED)) {
3460 rc = -EFAULT;
3461 break;
3462 } else {
3463 rc = 0;
3465 pReq = (PSCmdRequest)rq;
3466 pReq->wResult = MAGIC_CODE;
3467 break;
3469 case IOCTL_CMD_SET:
3471 #ifdef SndEvt_ToAPI
3472 if((((PSCmdRequest)rq)->wCmdCode !=WLAN_CMD_SET_EVT) &&
3473 !(pDevice->flags & DEVICE_FLAGS_OPENED))
3474 #else
3475 if (!(pDevice->flags & DEVICE_FLAGS_OPENED) &&
3476 (((PSCmdRequest)rq)->wCmdCode !=WLAN_CMD_SET_WPA))
3477 #endif
3479 rc = -EFAULT;
3480 break;
3481 } else {
3482 rc = 0;
3485 if (test_and_set_bit( 0, (void*)&(pMgmt->uCmdBusy))) {
3486 return -EBUSY;
3488 rc = private_ioctl(pDevice, rq);
3489 clear_bit( 0, (void*)&(pMgmt->uCmdBusy));
3490 break;
3492 case IOCTL_CMD_HOSTAPD:
3495 rc = vt6655_hostap_ioctl(pDevice, &wrq->u.data);
3496 break;
3498 case IOCTL_CMD_WPA:
3500 rc = wpa_ioctl(pDevice, &wrq->u.data);
3501 break;
3503 case SIOCETHTOOL:
3504 return ethtool_ioctl(dev, (void *) rq->ifr_data);
3505 // All other calls are currently unsupported
3507 default:
3508 rc = -EOPNOTSUPP;
3509 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Ioctl command not support..%x\n", cmd);
3514 if (pDevice->bCommit) {
3515 if (pMgmt->eConfigMode == WMAC_CONFIG_AP) {
3516 netif_stop_queue(pDevice->dev);
3517 spin_lock_irq(&pDevice->lock);
3518 bScheduleCommand((void *)pDevice, WLAN_CMD_RUN_AP, NULL);
3519 spin_unlock_irq(&pDevice->lock);
3521 else {
3522 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Commit the settings\n");
3523 spin_lock_irq(&pDevice->lock);
3524 pDevice->bLinkPass = false;
3525 memset(pMgmt->abyCurrBSSID, 0, 6);
3526 pMgmt->eCurrState = WMAC_STATE_IDLE;
3527 netif_stop_queue(pDevice->dev);
3528 #ifdef WPA_SUPPLICANT_DRIVER_WEXT_SUPPORT
3529 pMgmt->eScanType = WMAC_SCAN_ACTIVE;
3530 if(pDevice->bWPASuppWextEnabled !=true)
3531 #endif
3532 bScheduleCommand((void *) pDevice, WLAN_CMD_BSSID_SCAN, pMgmt->abyDesireSSID);
3533 bScheduleCommand((void *) pDevice, WLAN_CMD_SSID, NULL);
3534 spin_unlock_irq(&pDevice->lock);
3536 pDevice->bCommit = false;
3539 return rc;
3543 static int ethtool_ioctl(struct net_device *dev, void *useraddr)
3545 u32 ethcmd;
3547 if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
3548 return -EFAULT;
3550 switch (ethcmd) {
3551 case ETHTOOL_GDRVINFO: {
3552 struct ethtool_drvinfo info = {ETHTOOL_GDRVINFO};
3553 strncpy(info.driver, DEVICE_NAME, sizeof(info.driver)-1);
3554 strncpy(info.version, DEVICE_VERSION, sizeof(info.version)-1);
3555 if (copy_to_user(useraddr, &info, sizeof(info)))
3556 return -EFAULT;
3557 return 0;
3562 return -EOPNOTSUPP;
3565 /*------------------------------------------------------------------*/
3567 MODULE_DEVICE_TABLE(pci, vt6655_pci_id_table);
3569 static struct pci_driver device_driver = {
3570 name: DEVICE_NAME,
3571 id_table: vt6655_pci_id_table,
3572 probe: vt6655_probe,
3573 remove: vt6655_remove,
3574 #ifdef CONFIG_PM
3575 suspend: viawget_suspend,
3576 resume: viawget_resume,
3577 #endif
3580 static int __init vt6655_init_module(void)
3582 int ret;
3585 // ret=pci_module_init(&device_driver);
3586 //ret = pcie_port_service_register(&device_driver);
3587 ret = pci_register_driver(&device_driver);
3588 #ifdef CONFIG_PM
3589 if(ret >= 0)
3590 register_reboot_notifier(&device_notifier);
3591 #endif
3593 return ret;
3596 static void __exit vt6655_cleanup_module(void)
3600 #ifdef CONFIG_PM
3601 unregister_reboot_notifier(&device_notifier);
3602 #endif
3603 pci_unregister_driver(&device_driver);
3607 module_init(vt6655_init_module);
3608 module_exit(vt6655_cleanup_module);
3611 #ifdef CONFIG_PM
3612 static int
3613 device_notify_reboot(struct notifier_block *nb, unsigned long event, void *p)
3615 struct pci_dev *pdev = NULL;
3616 switch(event) {
3617 case SYS_DOWN:
3618 case SYS_HALT:
3619 case SYS_POWER_OFF:
3620 for_each_pci_dev(pdev) {
3621 if(pci_dev_driver(pdev) == &device_driver) {
3622 if (pci_get_drvdata(pdev))
3623 viawget_suspend(pdev, PMSG_HIBERNATE);
3627 return NOTIFY_DONE;
3630 static int
3631 viawget_suspend(struct pci_dev *pcid, pm_message_t state)
3633 int power_status; // to silence the compiler
3635 PSDevice pDevice=pci_get_drvdata(pcid);
3636 PSMgmtObject pMgmt = pDevice->pMgmt;
3638 netif_stop_queue(pDevice->dev);
3639 spin_lock_irq(&pDevice->lock);
3640 pci_save_state(pcid);
3641 del_timer(&pDevice->sTimerCommand);
3642 del_timer(&pMgmt->sTimerSecondCallback);
3643 pDevice->cbFreeCmdQueue = CMD_Q_SIZE;
3644 pDevice->uCmdDequeueIdx = 0;
3645 pDevice->uCmdEnqueueIdx = 0;
3646 pDevice->bCmdRunning = false;
3647 MACbShutdown(pDevice->PortOffset);
3648 MACvSaveContext(pDevice->PortOffset, pDevice->abyMacContext);
3649 pDevice->bLinkPass = false;
3650 memset(pMgmt->abyCurrBSSID, 0, 6);
3651 pMgmt->eCurrState = WMAC_STATE_IDLE;
3652 pci_disable_device(pcid);
3653 power_status = pci_set_power_state(pcid, pci_choose_state(pcid, state));
3654 spin_unlock_irq(&pDevice->lock);
3655 return 0;
3658 static int
3659 viawget_resume(struct pci_dev *pcid)
3661 PSDevice pDevice=pci_get_drvdata(pcid);
3662 PSMgmtObject pMgmt = pDevice->pMgmt;
3663 int power_status; // to silence the compiler
3666 power_status = pci_set_power_state(pcid, 0);
3667 power_status = pci_enable_wake(pcid, 0, 0);
3668 pci_restore_state(pcid);
3669 if (netif_running(pDevice->dev)) {
3670 spin_lock_irq(&pDevice->lock);
3671 MACvRestoreContext(pDevice->PortOffset, pDevice->abyMacContext);
3672 device_init_registers(pDevice, DEVICE_INIT_DXPL);
3673 if (pMgmt->sNodeDBTable[0].bActive == true) { // Assoc with BSS
3674 pMgmt->sNodeDBTable[0].bActive = false;
3675 pDevice->bLinkPass = false;
3676 if(pMgmt->eCurrMode == WMAC_MODE_IBSS_STA) {
3677 // In Adhoc, BSS state set back to started.
3678 pMgmt->eCurrState = WMAC_STATE_STARTED;
3680 else {
3681 pMgmt->eCurrMode = WMAC_MODE_STANDBY;
3682 pMgmt->eCurrState = WMAC_STATE_IDLE;
3685 init_timer(&pMgmt->sTimerSecondCallback);
3686 init_timer(&pDevice->sTimerCommand);
3687 MACvIntEnable(pDevice->PortOffset, IMR_MASK_VALUE);
3688 BSSvClearBSSList((void *)pDevice, pDevice->bLinkPass);
3689 bScheduleCommand((void *) pDevice, WLAN_CMD_BSSID_SCAN, NULL);
3690 bScheduleCommand((void *) pDevice, WLAN_CMD_SSID, NULL);
3691 spin_unlock_irq(&pDevice->lock);
3693 return 0;
3696 #endif