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