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