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