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