Staging: vt6655: Remove LINUX_VERSION_CODE preprocessor conditionals.
[linux-2.6] / drivers / staging / vt6655 / device_main.c
1 /*
2  * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
3  * All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * File: device_main.c
20  *
21  * Purpose: driver entry for initial, open, close, tx and rx.
22  *
23  * Author: Lyndon Chen
24  *
25  * Date: Jan 8, 2003
26  *
27  * Functions:
28  *
29  *   device_found1 - module initial (insmod) driver entry
30  *   device_remove1 - module remove entry
31  *   device_init_info - device structure resource allocation function
32  *   device_free_info - device structure resource free function
33  *   device_get_pci_info - get allocated pci io/mem resource
34  *   device_print_info - print out resource
35  *   device_open - allocate dma/descripter resource & initial mac/bbp function
36  *   device_xmit - asynchrous data tx function
37  *   device_intr - interrupt handle function
38  *   device_set_multi - set mac filter
39  *   device_ioctl - ioctl entry
40  *   device_close - shutdown mac/bbp & free dma/descripter resource
41  *   device_rx_srv - rx service function
42  *   device_receive_frame - rx data function
43  *   device_alloc_rx_buf - rx buffer pre-allocated function
44  *   device_alloc_frag_buf - rx fragement pre-allocated function
45  *   device_free_tx_buf - free tx buffer function
46  *   device_free_frag_buf- free de-fragement buffer
47  *   device_dma0_tx_80211- tx 802.11 frame via dma0
48  *   device_dma0_xmit- tx PS bufferred frame via dma0
49  *   device_init_rd0_ring- initial rd dma0 ring
50  *   device_init_rd1_ring- initial rd dma1 ring
51  *   device_init_td0_ring- initial tx dma0 ring buffer
52  *   device_init_td1_ring- initial tx dma1 ring buffer
53  *   device_init_registers- initial MAC & BBP & RF internal registers.
54  *   device_init_rings- initial tx/rx ring buffer
55  *   device_init_defrag_cb- initial & allocate de-fragement buffer.
56  *   device_free_rings- free all allocated ring buffer
57  *   device_tx_srv- tx interrupt service function
58  *
59  * Revision History:
60  */
61 #undef __NO_VERSION__
62
63 #if !defined(__DEVICE_H__)
64 #include "device.h"
65 #endif
66 #if !defined(__CARD_H__)
67 #include "card.h"
68 #endif
69 #if !defined(__TBIT_H__)
70 #include "tbit.h"
71 #endif
72 #if !defined(__BASEBAND_H__)
73 #include "baseband.h"
74 #endif
75 #if !defined(__MAC_H__)
76 #include "mac.h"
77 #endif
78 #if !defined(__TETHER_H__)
79 #include "tether.h"
80 #endif
81 #if !defined(__WMGR_H__)
82 #include "wmgr.h"
83 #endif
84 #if !defined(__WCTL_H__)
85 #include "wctl.h"
86 #endif
87 #if !defined(__POWER_H__)
88 #include "power.h"
89 #endif
90 #if !defined(__WCMD_H__)
91 #include "wcmd.h"
92 #endif
93 #if !defined(__IOCMD_H__)
94 #include "iocmd.h"
95 #endif
96 #if !defined(__TCRC_H__)
97 #include "tcrc.h"
98 #endif
99 #if !defined(__RXTX_H__)
100 #include "rxtx.h"
101 #endif
102 #if !defined(__WROUTE_H__)
103 #include "wroute.h"
104 #endif
105 #if !defined(__BSSDB_H__)
106 #include "bssdb.h"
107 #endif
108 #if !defined(__HOSTAP_H__)
109 #include "hostap.h"
110 #endif
111 #if !defined(__WPACTL_H__)
112 #include "wpactl.h"
113 #endif
114 #if !defined(__IOCTL_H__)
115 #include "ioctl.h"
116 #endif
117 #if !defined(__IWCTL_H__)
118 #include "iwctl.h"
119 #endif
120 #if !defined(__DPC_H__)
121 #include "dpc.h"
122 #endif
123 #if !defined(__DATARATE_H__)
124 #include "datarate.h"
125 #endif
126 #if !defined(__RF_H__)
127 #include "rf.h"
128 #endif
129 #if !defined(__IOWPA_H__)
130 #include "iowpa.h"
131 #endif
132
133 #include <linux/delay.h>
134 #include <linux/kthread.h>
135 // #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, (int)&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, u32 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 #ifndef PRIVATE_OBJ
972
973 static int
974 device_found1(struct pci_dev *pcid, const struct pci_device_id *ent)
975 {
976     static BOOL bFirst = TRUE;
977     struct net_device*  dev = NULL;
978     PCHIP_INFO  pChip_info = (PCHIP_INFO)ent->driver_data;
979     PSDevice    pDevice;
980     int         rc;
981     if (device_nics ++>= MAX_UINTS) {
982         printk(KERN_NOTICE DEVICE_NAME ": already found %d NICs\n", device_nics);
983         return -ENODEV;
984     }
985
986
987     dev = alloc_etherdev(sizeof(DEVICE_INFO));
988
989     pDevice = (PSDevice) netdev_priv(dev);
990
991     if (dev == NULL) {
992         printk(KERN_ERR DEVICE_NAME ": allocate net device failed \n");
993         return -ENODEV;
994     }
995
996     // Chain it all together
997    // SET_MODULE_OWNER(dev);
998     SET_NETDEV_DEV(dev, &pcid->dev);
999
1000     if (bFirst) {
1001         printk(KERN_NOTICE "%s Ver. %s\n",DEVICE_FULL_DRV_NAM, DEVICE_VERSION);
1002         printk(KERN_NOTICE "Copyright (c) 2003 VIA Networking Technologies, Inc.\n");
1003         bFirst=FALSE;
1004     }
1005
1006     if (!device_init_info(pcid, &pDevice, pChip_info)) {
1007         return -ENOMEM;
1008     }
1009     pDevice->dev = dev;
1010     pDevice->next_module = root_device_dev;
1011     root_device_dev = dev;
1012     dev->irq = pcid->irq;
1013
1014     if (pci_enable_device(pcid)) {
1015         device_free_info(pDevice);
1016         return -ENODEV;
1017     }
1018 #ifdef  DEBUG
1019         printk("Before get pci_info memaddr is %x\n",pDevice->memaddr);
1020 #endif
1021     if (device_get_pci_info(pDevice,pcid) == FALSE) {
1022         printk(KERN_ERR DEVICE_NAME ": Failed to find PCI device.\n");
1023         device_free_info(pDevice);
1024         return -ENODEV;
1025     }
1026
1027 #if 1
1028
1029 #ifdef  DEBUG
1030
1031         //pci_read_config_byte(pcid, PCI_BASE_ADDRESS_0, &pDevice->byRevId);
1032         printk("after get pci_info memaddr is %x, io addr is %x,io_size is %d\n",pDevice->memaddr,pDevice->ioaddr,pDevice->io_size);
1033         {
1034                 int i;
1035                 U32                     bar,len;
1036                 u32 address[] = {
1037                 PCI_BASE_ADDRESS_0,
1038                 PCI_BASE_ADDRESS_1,
1039                 PCI_BASE_ADDRESS_2,
1040                 PCI_BASE_ADDRESS_3,
1041                 PCI_BASE_ADDRESS_4,
1042                 PCI_BASE_ADDRESS_5,
1043                 0};
1044                 for (i=0;address[i];i++)
1045                 {
1046                         //pci_write_config_dword(pcid,address[i], 0xFFFFFFFF);
1047                         pci_read_config_dword(pcid, address[i], &bar);
1048                         printk("bar %d is %x\n",i,bar);
1049                         if (!bar)
1050                         {
1051                                 printk("bar %d not implemented\n",i);
1052                                 continue;
1053                         }
1054                         if (bar & PCI_BASE_ADDRESS_SPACE_IO) {
1055                         /* This is IO */
1056
1057                         len = bar & (PCI_BASE_ADDRESS_IO_MASK & 0xFFFF);
1058                         len = len & ~(len - 1);
1059
1060                         printk("IO space:  len in IO %x, BAR %d\n", len, i);
1061                         }
1062                         else
1063                         {
1064                                 len = bar & 0xFFFFFFF0;
1065                                 len = ~len + 1;
1066
1067                                 printk("len in MEM %x, BAR %d\n", len, i);
1068                         }
1069                 }
1070         }
1071 #endif
1072
1073
1074 #endif
1075
1076 #ifdef  DEBUG
1077         //return  0  ;
1078 #endif
1079     pDevice->PortOffset = (DWORD)ioremap(pDevice->memaddr & PCI_BASE_ADDRESS_MEM_MASK, pDevice->io_size);
1080         //pDevice->PortOffset = (DWORD)ioremap(pDevice->ioaddr & PCI_BASE_ADDRESS_IO_MASK, pDevice->io_size);
1081
1082         if(pDevice->PortOffset == 0) {
1083        printk(KERN_ERR DEVICE_NAME ": Failed to IO remapping ..\n");
1084        device_free_info(pDevice);
1085         return -ENODEV;
1086     }
1087
1088
1089
1090
1091     rc = pci_request_regions(pcid, DEVICE_NAME);
1092     if (rc) {
1093         printk(KERN_ERR DEVICE_NAME ": Failed to find PCI device\n");
1094         device_free_info(pDevice);
1095         return -ENODEV;
1096     }
1097
1098     dev->base_addr = pDevice->ioaddr;
1099 #ifdef  PLICE_DEBUG
1100         BYTE    value;
1101
1102         VNSvInPortB(pDevice->PortOffset+0x4F, &value);
1103         printk("Before write: value is %x\n",value);
1104         //VNSvInPortB(pDevice->PortOffset+0x3F, 0x00);
1105         VNSvOutPortB(pDevice->PortOffset,value);
1106         VNSvInPortB(pDevice->PortOffset+0x4F, &value);
1107         printk("After write: value is %x\n",value);
1108 #endif
1109
1110
1111
1112 #ifdef IO_MAP
1113     pDevice->PortOffset = pDevice->ioaddr;
1114 #endif
1115     // do reset
1116     if (!MACbSoftwareReset(pDevice->PortOffset)) {
1117         printk(KERN_ERR DEVICE_NAME ": Failed to access MAC hardware..\n");
1118         device_free_info(pDevice);
1119         return -ENODEV;
1120     }
1121     // initial to reload eeprom
1122     MACvInitialize(pDevice->PortOffset);
1123     MACvReadEtherAddress(pDevice->PortOffset, dev->dev_addr);
1124
1125     device_get_options(pDevice, device_nics-1, dev->name);
1126     device_set_options(pDevice);
1127     //Mask out the options cannot be set to the chip
1128     pDevice->sOpts.flags &= pChip_info->flags;
1129
1130     //Enable the chip specified capbilities
1131     pDevice->flags = pDevice->sOpts.flags | (pChip_info->flags & 0xFF000000UL);
1132     pDevice->tx_80211 = device_dma0_tx_80211;
1133     pDevice->sMgmtObj.pAdapter = (PVOID)pDevice;
1134     pDevice->pMgmt = &(pDevice->sMgmtObj);
1135
1136     dev->irq                = pcid->irq;
1137     dev->open               = device_open;
1138     dev->hard_start_xmit    = device_xmit;
1139     dev->stop               = device_close;
1140     dev->get_stats          = device_get_stats;
1141     dev->set_multicast_list = device_set_multi;
1142     dev->do_ioctl           = device_ioctl;
1143
1144 #ifdef WIRELESS_EXT
1145 //Einsn Modify for ubuntu-7.04
1146 //      dev->wireless_handlers->get_wireless_stats = iwctl_get_wireless_stats;
1147 #if WIRELESS_EXT > 12
1148         dev->wireless_handlers = (struct iw_handler_def *)&iwctl_handler_def;
1149 //      netdev->wireless_handlers = NULL;
1150 #endif /* WIRELESS_EXT > 12 */
1151 #endif /* WIRELESS_EXT */
1152
1153     rc = register_netdev(dev);
1154     if (rc)
1155     {
1156         printk(KERN_ERR DEVICE_NAME " Failed to register netdev\n");
1157         device_free_info(pDevice);
1158         return -ENODEV;
1159     }
1160 //2008-07-21-01<Add>by MikeLiu
1161 //register wpadev
1162    if(wpa_set_wpadev(pDevice, 1)!=0) {
1163      printk("Fail to Register WPADEV?\n");
1164         unregister_netdev(pDevice->dev);
1165         free_netdev(dev);
1166    }
1167     device_print_info(pDevice);
1168     pci_set_drvdata(pcid, pDevice);
1169     return 0;
1170
1171 }
1172
1173 static void device_print_info(PSDevice pDevice)
1174 {
1175     struct net_device* dev=pDevice->dev;
1176
1177     DEVICE_PRT(MSG_LEVEL_INFO, KERN_INFO "%s: %s\n",dev->name, get_chip_name(pDevice->chip_id));
1178     DEVICE_PRT(MSG_LEVEL_INFO, KERN_INFO "%s: MAC=%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X",
1179         dev->name,
1180         dev->dev_addr[0],dev->dev_addr[1],dev->dev_addr[2],
1181         dev->dev_addr[3],dev->dev_addr[4],dev->dev_addr[5]);
1182 #ifdef IO_MAP
1183     DEVICE_PRT(MSG_LEVEL_INFO, KERN_INFO" IO=0x%lx  ",(ULONG) pDevice->ioaddr);
1184     DEVICE_PRT(MSG_LEVEL_INFO, KERN_INFO" IRQ=%d \n", pDevice->dev->irq);
1185 #else
1186     DEVICE_PRT(MSG_LEVEL_INFO, KERN_INFO" IO=0x%lx Mem=0x%lx ",(ULONG) pDevice->ioaddr,(ULONG) pDevice->PortOffset);
1187     DEVICE_PRT(MSG_LEVEL_INFO, KERN_INFO" IRQ=%d \n", pDevice->dev->irq);
1188 #endif
1189
1190 }
1191
1192 static BOOL device_init_info(struct pci_dev* pcid, PSDevice* ppDevice,
1193     PCHIP_INFO pChip_info) {
1194
1195     PSDevice p;
1196
1197     memset(*ppDevice,0,sizeof(DEVICE_INFO));
1198
1199     if (pDevice_Infos == NULL) {
1200         pDevice_Infos =*ppDevice;
1201     }
1202     else {
1203         for (p=pDevice_Infos;p->next!=NULL;p=p->next)
1204             do {} while (0);
1205         p->next = *ppDevice;
1206         (*ppDevice)->prev = p;
1207     }
1208
1209     (*ppDevice)->pcid = pcid;
1210     (*ppDevice)->chip_id = pChip_info->chip_id;
1211     (*ppDevice)->io_size = pChip_info->io_size;
1212     (*ppDevice)->nTxQueues = pChip_info->nTxQueue;
1213     (*ppDevice)->multicast_limit =32;
1214
1215     spin_lock_init(&((*ppDevice)->lock));
1216
1217     return TRUE;
1218 }
1219
1220 static BOOL device_get_pci_info(PSDevice pDevice, struct pci_dev* pcid) {
1221
1222     U16 pci_cmd;
1223     U8  b;
1224     UINT cis_addr;
1225 #ifdef  PLICE_DEBUG
1226         BYTE       pci_config[256];
1227         BYTE    value =0x00;
1228         int             ii,j;
1229         U16     max_lat=0x0000;
1230         memset(pci_config,0x00,256);
1231 #endif
1232
1233     pci_read_config_byte(pcid, PCI_REVISION_ID, &pDevice->byRevId);
1234     pci_read_config_word(pcid, PCI_SUBSYSTEM_ID,&pDevice->SubSystemID);
1235     pci_read_config_word(pcid, PCI_SUBSYSTEM_VENDOR_ID, &pDevice->SubVendorID);
1236     pci_read_config_word(pcid, PCI_COMMAND, (u16 *) & (pci_cmd));
1237
1238     pci_set_master(pcid);
1239
1240     pDevice->memaddr = pci_resource_start(pcid,0);
1241     pDevice->ioaddr = pci_resource_start(pcid,1);
1242
1243 #ifdef  DEBUG
1244 //      pDevice->ioaddr = pci_resource_start(pcid, 0);
1245 //      pDevice->memaddr = pci_resource_start(pcid,1);
1246 #endif
1247
1248     cis_addr = pci_resource_start(pcid,2);
1249
1250     pDevice->pcid = pcid;
1251
1252     pci_read_config_byte(pcid, PCI_REG_COMMAND, &b);
1253     pci_write_config_byte(pcid, PCI_REG_COMMAND, (b|COMMAND_BUSM));
1254
1255 #ifdef  PLICE_DEBUG
1256         //pci_read_config_word(pcid,PCI_REG_MAX_LAT,&max_lat);
1257         //printk("max lat is %x,SubSystemID is %x\n",max_lat,pDevice->SubSystemID);
1258         //for (ii=0;ii<0xFF;ii++)
1259         //pci_read_config_word(pcid,PCI_REG_MAX_LAT,&max_lat);
1260         //max_lat  = 0x20;
1261         //pci_write_config_word(pcid,PCI_REG_MAX_LAT,max_lat);
1262         //pci_read_config_word(pcid,PCI_REG_MAX_LAT,&max_lat);
1263         //printk("max lat is %x\n",max_lat);
1264
1265         for (ii=0;ii<0xFF;ii++)
1266         {
1267                 pci_read_config_byte(pcid,ii,&value);
1268                 pci_config[ii] = value;
1269         }
1270         for (ii=0,j=1;ii<0x100;ii++,j++)
1271         {
1272                 if (j %16 == 0)
1273                 {
1274                         printk("%x:",pci_config[ii]);
1275                         printk("\n");
1276                 }
1277                 else
1278                 {
1279                         printk("%x:",pci_config[ii]);
1280                 }
1281         }
1282 #endif
1283     return TRUE;
1284 }
1285
1286 static void device_free_info(PSDevice pDevice) {
1287     PSDevice         ptr;
1288     struct net_device*  dev=pDevice->dev;
1289
1290     ASSERT(pDevice);
1291 //2008-0714-01<Add>by chester
1292 device_release_WPADEV(pDevice);
1293
1294 //2008-07-21-01<Add>by MikeLiu
1295 //unregister wpadev
1296    if(wpa_set_wpadev(pDevice, 0)!=0)
1297      printk("unregister wpadev fail?\n");
1298
1299     if (pDevice_Infos==NULL)
1300         return;
1301
1302     for (ptr=pDevice_Infos;ptr && (ptr!=pDevice);ptr=ptr->next)
1303             do {} while (0);
1304
1305     if (ptr==pDevice) {
1306         if (ptr==pDevice_Infos)
1307             pDevice_Infos=ptr->next;
1308         else
1309             ptr->prev->next=ptr->next;
1310     }
1311     else {
1312         DEVICE_PRT(MSG_LEVEL_ERR, KERN_ERR "info struct not found\n");
1313         return;
1314     }
1315 #ifdef HOSTAP
1316     if (dev)
1317         hostap_set_hostapd(pDevice, 0, 0);
1318 #endif
1319     if (dev)
1320         unregister_netdev(dev);
1321
1322     if (pDevice->PortOffset)
1323         iounmap((PVOID)pDevice->PortOffset);
1324
1325     if (pDevice->pcid)
1326         pci_release_regions(pDevice->pcid);
1327     if (dev)
1328         free_netdev(dev);
1329
1330     if (pDevice->pcid) {
1331         pci_set_drvdata(pDevice->pcid,NULL);
1332     }
1333 }
1334 #endif// ifndef PRIVATE_OBJ
1335
1336 static BOOL device_init_rings(PSDevice pDevice) {
1337     void*   vir_pool;
1338
1339
1340     /*allocate all RD/TD rings a single pool*/
1341     vir_pool = pci_alloc_consistent(pDevice->pcid,
1342                     pDevice->sOpts.nRxDescs0 * sizeof(SRxDesc) +
1343                     pDevice->sOpts.nRxDescs1 * sizeof(SRxDesc) +
1344                     pDevice->sOpts.nTxDescs[0] * sizeof(STxDesc) +
1345                     pDevice->sOpts.nTxDescs[1] * sizeof(STxDesc),
1346                     &pDevice->pool_dma);
1347
1348     if (vir_pool == NULL) {
1349         DEVICE_PRT(MSG_LEVEL_ERR,KERN_ERR "%s : allocate desc dma memory failed\n", pDevice->dev->name);
1350         return FALSE;
1351     }
1352
1353     memset(vir_pool, 0,
1354             pDevice->sOpts.nRxDescs0 * sizeof(SRxDesc) +
1355             pDevice->sOpts.nRxDescs1 * sizeof(SRxDesc) +
1356             pDevice->sOpts.nTxDescs[0] * sizeof(STxDesc) +
1357             pDevice->sOpts.nTxDescs[1] * sizeof(STxDesc)
1358           );
1359
1360     pDevice->aRD0Ring = vir_pool;
1361     pDevice->aRD1Ring = vir_pool +
1362                         pDevice->sOpts.nRxDescs0 * sizeof(SRxDesc);
1363
1364
1365     pDevice->rd0_pool_dma = pDevice->pool_dma;
1366     pDevice->rd1_pool_dma = pDevice->rd0_pool_dma +
1367                             pDevice->sOpts.nRxDescs0 * sizeof(SRxDesc);
1368
1369     pDevice->tx0_bufs = pci_alloc_consistent(pDevice->pcid,
1370                     pDevice->sOpts.nTxDescs[0] * PKT_BUF_SZ +
1371                     pDevice->sOpts.nTxDescs[1] * PKT_BUF_SZ +
1372                     CB_BEACON_BUF_SIZE +
1373                     CB_MAX_BUF_SIZE,
1374                     &pDevice->tx_bufs_dma0);
1375
1376     if (pDevice->tx0_bufs == NULL) {
1377         DEVICE_PRT(MSG_LEVEL_ERR,KERN_ERR "%s: allocate buf dma memory failed\n", pDevice->dev->name);
1378         pci_free_consistent(pDevice->pcid,
1379             pDevice->sOpts.nRxDescs0 * sizeof(SRxDesc) +
1380             pDevice->sOpts.nRxDescs1 * sizeof(SRxDesc) +
1381             pDevice->sOpts.nTxDescs[0] * sizeof(STxDesc) +
1382             pDevice->sOpts.nTxDescs[1] * sizeof(STxDesc),
1383             vir_pool, pDevice->pool_dma
1384             );
1385         return FALSE;
1386     }
1387
1388     memset(pDevice->tx0_bufs, 0,
1389            pDevice->sOpts.nTxDescs[0] * PKT_BUF_SZ +
1390            pDevice->sOpts.nTxDescs[1] * PKT_BUF_SZ +
1391            CB_BEACON_BUF_SIZE +
1392            CB_MAX_BUF_SIZE
1393           );
1394
1395     pDevice->td0_pool_dma = pDevice->rd1_pool_dma +
1396             pDevice->sOpts.nRxDescs1 * sizeof(SRxDesc);
1397
1398     pDevice->td1_pool_dma = pDevice->td0_pool_dma +
1399             pDevice->sOpts.nTxDescs[0] * sizeof(STxDesc);
1400
1401
1402     // vir_pool: pvoid type
1403     pDevice->apTD0Rings = vir_pool
1404                           + pDevice->sOpts.nRxDescs0 * sizeof(SRxDesc)
1405                           + pDevice->sOpts.nRxDescs1 * sizeof(SRxDesc);
1406
1407     pDevice->apTD1Rings = vir_pool
1408             + pDevice->sOpts.nRxDescs0 * sizeof(SRxDesc)
1409             + pDevice->sOpts.nRxDescs1 * sizeof(SRxDesc)
1410             + pDevice->sOpts.nTxDescs[0] * sizeof(STxDesc);
1411
1412
1413     pDevice->tx1_bufs = pDevice->tx0_bufs +
1414             pDevice->sOpts.nTxDescs[0] * PKT_BUF_SZ;
1415
1416
1417     pDevice->tx_beacon_bufs = pDevice->tx1_bufs +
1418             pDevice->sOpts.nTxDescs[1] * PKT_BUF_SZ;
1419
1420     pDevice->pbyTmpBuff = pDevice->tx_beacon_bufs +
1421             CB_BEACON_BUF_SIZE;
1422
1423     pDevice->tx_bufs_dma1 = pDevice->tx_bufs_dma0 +
1424             pDevice->sOpts.nTxDescs[0] * PKT_BUF_SZ;
1425
1426
1427     pDevice->tx_beacon_dma = pDevice->tx_bufs_dma1 +
1428             pDevice->sOpts.nTxDescs[1] * PKT_BUF_SZ;
1429
1430
1431     return TRUE;
1432 }
1433
1434 static void device_free_rings(PSDevice pDevice) {
1435
1436     pci_free_consistent(pDevice->pcid,
1437             pDevice->sOpts.nRxDescs0 * sizeof(SRxDesc) +
1438             pDevice->sOpts.nRxDescs1 * sizeof(SRxDesc) +
1439             pDevice->sOpts.nTxDescs[0] * sizeof(STxDesc) +
1440             pDevice->sOpts.nTxDescs[1] * sizeof(STxDesc)
1441             ,
1442             pDevice->aRD0Ring, pDevice->pool_dma
1443         );
1444
1445     if (pDevice->tx0_bufs)
1446         pci_free_consistent(pDevice->pcid,
1447            pDevice->sOpts.nTxDescs[0] * PKT_BUF_SZ +
1448            pDevice->sOpts.nTxDescs[1] * PKT_BUF_SZ +
1449            CB_BEACON_BUF_SIZE +
1450            CB_MAX_BUF_SIZE,
1451            pDevice->tx0_bufs, pDevice->tx_bufs_dma0
1452         );
1453 }
1454
1455 static void device_init_rd0_ring(PSDevice pDevice) {
1456     int i;
1457     dma_addr_t      curr = pDevice->rd0_pool_dma;
1458     PSRxDesc        pDesc;
1459
1460     /* Init the RD0 ring entries */
1461     for (i = 0; i < pDevice->sOpts.nRxDescs0; i ++, curr += sizeof(SRxDesc)) {
1462         pDesc = &(pDevice->aRD0Ring[i]);
1463         pDesc->pRDInfo = alloc_rd_info();
1464         ASSERT(pDesc->pRDInfo);
1465         if (!device_alloc_rx_buf(pDevice, pDesc)) {
1466             DEVICE_PRT(MSG_LEVEL_ERR,KERN_ERR "%s: can not alloc rx bufs\n",
1467             pDevice->dev->name);
1468         }
1469         pDesc->next = &(pDevice->aRD0Ring[(i+1) % pDevice->sOpts.nRxDescs0]);
1470         pDesc->pRDInfo->curr_desc = cpu_to_le32(curr);
1471         pDesc->next_desc = cpu_to_le32(curr + sizeof(SRxDesc));
1472     }
1473
1474     pDevice->aRD0Ring[i-1].next_desc = cpu_to_le32(pDevice->rd0_pool_dma);
1475     pDevice->pCurrRD[0] = &(pDevice->aRD0Ring[0]);
1476 }
1477
1478
1479 static void device_init_rd1_ring(PSDevice pDevice) {
1480     int i;
1481     dma_addr_t      curr = pDevice->rd1_pool_dma;
1482     PSRxDesc        pDesc;
1483
1484     /* Init the RD1 ring entries */
1485     for (i = 0; i < pDevice->sOpts.nRxDescs1; i ++, curr += sizeof(SRxDesc)) {
1486         pDesc = &(pDevice->aRD1Ring[i]);
1487         pDesc->pRDInfo = alloc_rd_info();
1488         ASSERT(pDesc->pRDInfo);
1489         if (!device_alloc_rx_buf(pDevice, pDesc)) {
1490             DEVICE_PRT(MSG_LEVEL_ERR,KERN_ERR "%s: can not alloc rx bufs\n",
1491             pDevice->dev->name);
1492         }
1493         pDesc->next = &(pDevice->aRD1Ring[(i+1) % pDevice->sOpts.nRxDescs1]);
1494         pDesc->pRDInfo->curr_desc = cpu_to_le32(curr);
1495         pDesc->next_desc = cpu_to_le32(curr + sizeof(SRxDesc));
1496     }
1497
1498     pDevice->aRD1Ring[i-1].next_desc = cpu_to_le32(pDevice->rd1_pool_dma);
1499     pDevice->pCurrRD[1] = &(pDevice->aRD1Ring[0]);
1500 }
1501
1502
1503 static void device_init_defrag_cb(PSDevice pDevice) {
1504     int i;
1505     PSDeFragControlBlock pDeF;
1506
1507     /* Init the fragment ctl entries */
1508     for (i = 0; i < CB_MAX_RX_FRAG; i++) {
1509         pDeF = &(pDevice->sRxDFCB[i]);
1510         if (!device_alloc_frag_buf(pDevice, pDeF)) {
1511             DEVICE_PRT(MSG_LEVEL_ERR,KERN_ERR "%s: can not alloc frag bufs\n",
1512                 pDevice->dev->name);
1513         };
1514     }
1515     pDevice->cbDFCB = CB_MAX_RX_FRAG;
1516     pDevice->cbFreeDFCB = pDevice->cbDFCB;
1517 }
1518
1519
1520
1521
1522 static void device_free_rd0_ring(PSDevice pDevice) {
1523     int i;
1524
1525     for (i = 0; i < pDevice->sOpts.nRxDescs0; i++) {
1526         PSRxDesc        pDesc =&(pDevice->aRD0Ring[i]);
1527         PDEVICE_RD_INFO  pRDInfo =pDesc->pRDInfo;
1528
1529         pci_unmap_single(pDevice->pcid,pRDInfo->skb_dma,
1530            pDevice->rx_buf_sz, PCI_DMA_FROMDEVICE);
1531
1532         dev_kfree_skb(pRDInfo->skb);
1533
1534         kfree((PVOID)pDesc->pRDInfo);
1535     }
1536
1537 }
1538
1539 static void device_free_rd1_ring(PSDevice pDevice) {
1540     int i;
1541
1542
1543     for (i = 0; i < pDevice->sOpts.nRxDescs1; i++) {
1544         PSRxDesc        pDesc=&(pDevice->aRD1Ring[i]);
1545         PDEVICE_RD_INFO  pRDInfo=pDesc->pRDInfo;
1546
1547         pci_unmap_single(pDevice->pcid,pRDInfo->skb_dma,
1548            pDevice->rx_buf_sz, PCI_DMA_FROMDEVICE);
1549
1550         dev_kfree_skb(pRDInfo->skb);
1551
1552         kfree((PVOID)pDesc->pRDInfo);
1553     }
1554
1555 }
1556
1557 static void device_free_frag_buf(PSDevice pDevice) {
1558     PSDeFragControlBlock pDeF;
1559     int i;
1560
1561     for (i = 0; i < CB_MAX_RX_FRAG; i++) {
1562
1563         pDeF = &(pDevice->sRxDFCB[i]);
1564
1565         if (pDeF->skb)
1566             dev_kfree_skb(pDeF->skb);
1567
1568     }
1569
1570 }
1571
1572 static void device_init_td0_ring(PSDevice pDevice) {
1573     int i;
1574     dma_addr_t  curr;
1575     PSTxDesc        pDesc;
1576
1577     curr = pDevice->td0_pool_dma;
1578     for (i = 0; i < pDevice->sOpts.nTxDescs[0]; i++, curr += sizeof(STxDesc)) {
1579         pDesc = &(pDevice->apTD0Rings[i]);
1580         pDesc->pTDInfo = alloc_td_info();
1581         ASSERT(pDesc->pTDInfo);
1582         if (pDevice->flags & DEVICE_FLAGS_TX_ALIGN) {
1583             pDesc->pTDInfo->buf = pDevice->tx0_bufs + (i)*PKT_BUF_SZ;
1584             pDesc->pTDInfo->buf_dma = pDevice->tx_bufs_dma0 + (i)*PKT_BUF_SZ;
1585         }
1586         pDesc->next =&(pDevice->apTD0Rings[(i+1) % pDevice->sOpts.nTxDescs[0]]);
1587         pDesc->pTDInfo->curr_desc = cpu_to_le32(curr);
1588         pDesc->next_desc = cpu_to_le32(curr+sizeof(STxDesc));
1589     }
1590
1591     pDevice->apTD0Rings[i-1].next_desc = cpu_to_le32(pDevice->td0_pool_dma);
1592     pDevice->apTailTD[0] = pDevice->apCurrTD[0] =&(pDevice->apTD0Rings[0]);
1593
1594 }
1595
1596 static void device_init_td1_ring(PSDevice pDevice) {
1597     int i;
1598     dma_addr_t  curr;
1599     PSTxDesc    pDesc;
1600
1601     /* Init the TD ring entries */
1602     curr=pDevice->td1_pool_dma;
1603     for (i = 0; i < pDevice->sOpts.nTxDescs[1]; i++, curr+=sizeof(STxDesc)) {
1604         pDesc=&(pDevice->apTD1Rings[i]);
1605         pDesc->pTDInfo = alloc_td_info();
1606         ASSERT(pDesc->pTDInfo);
1607         if (pDevice->flags & DEVICE_FLAGS_TX_ALIGN) {
1608             pDesc->pTDInfo->buf=pDevice->tx1_bufs+(i)*PKT_BUF_SZ;
1609             pDesc->pTDInfo->buf_dma=pDevice->tx_bufs_dma1+(i)*PKT_BUF_SZ;
1610         }
1611         pDesc->next=&(pDevice->apTD1Rings[(i+1) % pDevice->sOpts.nTxDescs[1]]);
1612         pDesc->pTDInfo->curr_desc = cpu_to_le32(curr);
1613         pDesc->next_desc = cpu_to_le32(curr+sizeof(STxDesc));
1614     }
1615
1616     pDevice->apTD1Rings[i-1].next_desc = cpu_to_le32(pDevice->td1_pool_dma);
1617     pDevice->apTailTD[1] = pDevice->apCurrTD[1] = &(pDevice->apTD1Rings[0]);
1618 }
1619
1620
1621
1622 static void device_free_td0_ring(PSDevice pDevice) {
1623     int i;
1624     for (i = 0; i < pDevice->sOpts.nTxDescs[0]; i++) {
1625         PSTxDesc        pDesc=&(pDevice->apTD0Rings[i]);
1626         PDEVICE_TD_INFO  pTDInfo=pDesc->pTDInfo;
1627
1628         if (pTDInfo->skb_dma && (pTDInfo->skb_dma != pTDInfo->buf_dma))
1629             pci_unmap_single(pDevice->pcid,pTDInfo->skb_dma,
1630                pTDInfo->skb->len, PCI_DMA_TODEVICE);
1631
1632         if (pTDInfo->skb)
1633             dev_kfree_skb(pTDInfo->skb);
1634
1635         kfree((PVOID)pDesc->pTDInfo);
1636     }
1637 }
1638
1639 static void device_free_td1_ring(PSDevice pDevice) {
1640     int i;
1641
1642     for (i = 0; i < pDevice->sOpts.nTxDescs[1]; i++) {
1643         PSTxDesc        pDesc=&(pDevice->apTD1Rings[i]);
1644         PDEVICE_TD_INFO  pTDInfo=pDesc->pTDInfo;
1645
1646         if (pTDInfo->skb_dma && (pTDInfo->skb_dma != pTDInfo->buf_dma))
1647             pci_unmap_single(pDevice->pcid, pTDInfo->skb_dma,
1648                pTDInfo->skb->len, PCI_DMA_TODEVICE);
1649
1650         if (pTDInfo->skb)
1651             dev_kfree_skb(pTDInfo->skb);
1652
1653         kfree((PVOID)pDesc->pTDInfo);
1654     }
1655
1656 }
1657
1658
1659
1660 /*-----------------------------------------------------------------*/
1661
1662 static int device_rx_srv(PSDevice pDevice, UINT uIdx) {
1663     PSRxDesc    pRD;
1664     int works = 0;
1665
1666
1667     for (pRD = pDevice->pCurrRD[uIdx];
1668          pRD->m_rd0RD0.f1Owner == OWNED_BY_HOST;
1669          pRD = pRD->next) {
1670 //        DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO "pDevice->pCurrRD = %x, works = %d\n", pRD, works);
1671         if (works++>15)
1672             break;
1673         if (device_receive_frame(pDevice, pRD)) {
1674             if (!device_alloc_rx_buf(pDevice,pRD)) {
1675                     DEVICE_PRT(MSG_LEVEL_ERR, KERN_ERR
1676                     "%s: can not allocate rx buf\n", pDevice->dev->name);
1677                     break;
1678             }
1679         }
1680         pRD->m_rd0RD0.f1Owner = OWNED_BY_NIC;
1681 #ifdef PRIVATE_OBJ
1682         ref_set_rx_jiffies(pDevice->dev);
1683 #else
1684         pDevice->dev->last_rx = jiffies;
1685 #endif
1686     }
1687
1688     pDevice->pCurrRD[uIdx]=pRD;
1689
1690     return works;
1691 }
1692
1693
1694 static BOOL device_alloc_rx_buf(PSDevice pDevice, PSRxDesc pRD) {
1695
1696     PDEVICE_RD_INFO pRDInfo=pRD->pRDInfo;
1697
1698 #ifdef PRIVATE_OBJ
1699
1700     pRDInfo->skb=dev_alloc_skb(pDevice->rx_buf_sz);
1701     if (pRDInfo->skb==NULL)
1702         return FALSE;
1703     ref_skb_remap(pDevice->dev, &(pRDInfo->ref_skb), pRDInfo->skb);
1704     pRDInfo->skb_dma = pci_map_single(pDevice->pcid, pRDInfo->ref_skb.tail, pDevice->rx_buf_sz,
1705                         PCI_DMA_FROMDEVICE);
1706 #else
1707
1708     pRDInfo->skb = dev_alloc_skb((int)pDevice->rx_buf_sz);
1709 #ifdef  PLICE_DEBUG
1710         //printk("device_alloc_rx_buf:skb is %x\n",pRDInfo->skb);
1711 #endif
1712     if (pRDInfo->skb==NULL)
1713         return FALSE;
1714     ASSERT(pRDInfo->skb);
1715     pRDInfo->skb->dev = pDevice->dev;
1716     pRDInfo->skb_dma = pci_map_single(pDevice->pcid, pRDInfo->skb->tail, pDevice->rx_buf_sz,
1717                         PCI_DMA_FROMDEVICE);
1718 #endif
1719     *((PU32) &(pRD->m_rd0RD0)) = 0;
1720
1721     pRD->m_rd0RD0.wResCount = cpu_to_le16(pDevice->rx_buf_sz);
1722     pRD->m_rd0RD0.f1Owner = OWNED_BY_NIC;
1723     pRD->m_rd1RD1.wReqCount = cpu_to_le16(pDevice->rx_buf_sz);
1724     pRD->buff_addr = cpu_to_le32(pRDInfo->skb_dma);
1725
1726     return TRUE;
1727 }
1728
1729
1730
1731 BOOL device_alloc_frag_buf(PSDevice pDevice, PSDeFragControlBlock pDeF) {
1732
1733 #ifdef PRIVATE_OBJ
1734
1735     pDeF->skb=dev_alloc_skb(pDevice->rx_buf_sz);
1736     if (pDeF->skb==NULL)
1737         return FALSE;
1738     ref_skb_remap(pDevice->dev, &(pDeF->ref_skb), pDeF->skb);
1739
1740 #else
1741     pDeF->skb = dev_alloc_skb((int)pDevice->rx_buf_sz);
1742     if (pDeF->skb == NULL)
1743         return FALSE;
1744     ASSERT(pDeF->skb);
1745     pDeF->skb->dev = pDevice->dev;
1746 #endif
1747
1748     return TRUE;
1749 }
1750
1751
1752
1753 static int device_tx_srv(PSDevice pDevice, UINT uIdx) {
1754     PSTxDesc                 pTD;
1755     BOOL                     bFull=FALSE;
1756     int                      works = 0;
1757     BYTE                     byTsr0;
1758     BYTE                     byTsr1;
1759     UINT                     uFrameSize, uFIFOHeaderSize;
1760     PSTxBufHead              pTxBufHead;
1761     struct net_device_stats* pStats = &pDevice->stats;
1762     struct sk_buff*          skb;
1763     UINT                     uNodeIndex;
1764     PSMgmtObject             pMgmt = pDevice->pMgmt;
1765 #ifdef PRIVATE_OBJ
1766     ref_sk_buff              ref_skb;
1767 #endif
1768
1769
1770     for (pTD = pDevice->apTailTD[uIdx]; pDevice->iTDUsed[uIdx] >0; pTD = pTD->next) {
1771
1772         if (pTD->m_td0TD0.f1Owner == OWNED_BY_NIC)
1773             break;
1774         if (works++>15)
1775             break;
1776
1777         byTsr0 = pTD->m_td0TD0.byTSR0;
1778         byTsr1 = pTD->m_td0TD0.byTSR1;
1779
1780         //Only the status of first TD in the chain is correct
1781         if (pTD->m_td1TD1.byTCR & TCR_STP) {
1782
1783             if ((pTD->pTDInfo->byFlags & TD_FLAGS_NETIF_SKB) != 0) {
1784                 uFIFOHeaderSize = pTD->pTDInfo->dwHeaderLength;
1785                 uFrameSize = pTD->pTDInfo->dwReqCount - uFIFOHeaderSize;
1786                 pTxBufHead = (PSTxBufHead) (pTD->pTDInfo->buf);
1787 #ifdef PRIVATE_OBJ
1788                 ref_skb_remap(pDevice->dev, &ref_skb, pTD->pTDInfo->skb);
1789 #endif
1790                 // Update the statistics based on the Transmit status
1791                 // now, we DO'NT check TSR0_CDH
1792
1793                 STAvUpdateTDStatCounter(&pDevice->scStatistic,
1794                         byTsr0, byTsr1,
1795                         (PBYTE)(pTD->pTDInfo->buf + uFIFOHeaderSize),
1796                         uFrameSize, uIdx);
1797
1798
1799                 BSSvUpdateNodeTxCounter(pDevice,
1800                          byTsr0, byTsr1,
1801                          (PBYTE)(pTD->pTDInfo->buf),
1802                          uFIFOHeaderSize
1803                          );
1804
1805                 if (BITbIsBitOff(byTsr1, TSR1_TERR)) {
1806                     if (byTsr0 != 0) {
1807                         DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO" Tx[%d] OK but has error. tsr1[%02X] tsr0[%02X].\n",
1808                            (INT)uIdx, byTsr1, byTsr0);
1809                     }
1810                     if ((pTxBufHead->wFragCtl & FRAGCTL_ENDFRAG) != FRAGCTL_NONFRAG) {
1811                         pDevice->s802_11Counter.TransmittedFragmentCount ++;
1812                     }
1813                     pStats->tx_packets++;
1814 #ifdef PRIVATE_OBJ
1815                     pStats->tx_bytes += *(ref_skb.len);
1816 #else
1817                     pStats->tx_bytes += pTD->pTDInfo->skb->len;
1818 #endif
1819                 }
1820                 else {
1821                      DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO" Tx[%d] dropped & tsr1[%02X] tsr0[%02X].\n",
1822                            (INT)uIdx, byTsr1, byTsr0);
1823                     pStats->tx_errors++;
1824                     pStats->tx_dropped++;
1825                 }
1826             }
1827
1828             if ((pTD->pTDInfo->byFlags & TD_FLAGS_PRIV_SKB) != 0) {
1829                 if (pDevice->bEnableHostapd) {
1830                     DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO "tx call back netif.. \n");
1831 #ifdef PRIVATE_OBJ
1832                     ref_skb_remap(pDevice->apdev, &(ref_skb), pTD->pTDInfo->skb);
1833                         ref_skb.mac.raw = ref_skb.data;
1834                         *(ref_skb.pkt_type) = PACKET_OTHERHOST;
1835                     //*(ref_skb.protocol) = htons(ETH_P_802_2);
1836                         memset(ref_skb.cb, 0, sizeof(ref_skb.cb));
1837                         netif_rx(ref_skb.skb);
1838 #else
1839                     skb = pTD->pTDInfo->skb;
1840                         skb->dev = pDevice->apdev;
1841                                 skb->mac_header = skb->data;
1842                         skb->pkt_type = PACKET_OTHERHOST;
1843                     //skb->protocol = htons(ETH_P_802_2);
1844                         memset(skb->cb, 0, sizeof(skb->cb));
1845                         netif_rx(skb);
1846 #endif
1847                     }
1848             }
1849
1850             if (BITbIsBitOn(byTsr1, TSR1_TERR)) {
1851             if ((pTD->pTDInfo->byFlags & TD_FLAGS_PRIV_SKB) != 0) {
1852                 DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO" Tx[%d] fail has error. tsr1[%02X] tsr0[%02X].\n",
1853                           (INT)uIdx, byTsr1, byTsr0);
1854             }
1855
1856 //                DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO" Tx[%d] fail has error. tsr1[%02X] tsr0[%02X].\n",
1857 //                          (INT)uIdx, byTsr1, byTsr0);
1858
1859                 if ((pMgmt->eCurrMode == WMAC_MODE_ESS_AP) &&
1860                     (pTD->pTDInfo->byFlags & TD_FLAGS_NETIF_SKB)) {
1861                     WORD    wAID;
1862                     BYTE    byMask[8] = {1, 2, 4, 8, 0x10, 0x20, 0x40, 0x80};
1863
1864                     skb = pTD->pTDInfo->skb;
1865                     if (BSSDBbIsSTAInNodeDB(pMgmt, (PBYTE)(skb->data), &uNodeIndex)) {
1866                         if (pMgmt->sNodeDBTable[uNodeIndex].bPSEnable) {
1867                             skb_queue_tail(&pMgmt->sNodeDBTable[uNodeIndex].sTxPSQueue, skb);
1868                             pMgmt->sNodeDBTable[uNodeIndex].wEnQueueCnt++;
1869                             // set tx map
1870                             wAID = pMgmt->sNodeDBTable[uNodeIndex].wAID;
1871                             pMgmt->abyPSTxMap[wAID >> 3] |=  byMask[wAID & 7];
1872                             pTD->pTDInfo->byFlags &= ~(TD_FLAGS_NETIF_SKB);
1873                             DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO "tx_srv:tx fail re-queue sta index= %d, QueCnt= %d\n"
1874                                     ,(INT)uNodeIndex, pMgmt->sNodeDBTable[uNodeIndex].wEnQueueCnt);
1875                             pStats->tx_errors--;
1876                             pStats->tx_dropped--;
1877                         }
1878                     }
1879                 }
1880             }
1881             device_free_tx_buf(pDevice,pTD);
1882             pDevice->iTDUsed[uIdx]--;
1883         }
1884     }
1885
1886
1887     if (uIdx == TYPE_AC0DMA) {
1888         // RESERV_AC0DMA reserved for relay
1889
1890         if (AVAIL_TD(pDevice, uIdx) < RESERV_AC0DMA) {
1891             bFull = TRUE;
1892             DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO " AC0DMA is Full = %d\n", pDevice->iTDUsed[uIdx]);
1893         }
1894         if (netif_queue_stopped(pDevice->dev) && (bFull==FALSE)){
1895             netif_wake_queue(pDevice->dev);
1896         }
1897     }
1898
1899
1900     pDevice->apTailTD[uIdx] = pTD;
1901
1902     return works;
1903 }
1904
1905
1906 static void device_error(PSDevice pDevice, WORD status) {
1907
1908     if (status & ISR_FETALERR) {
1909         DEVICE_PRT(MSG_LEVEL_ERR, KERN_ERR
1910             "%s: Hardware fatal error.\n",
1911             pDevice->dev->name);
1912         netif_stop_queue(pDevice->dev);
1913         del_timer(&pDevice->sTimerCommand);
1914         del_timer(&(pDevice->pMgmt->sTimerSecondCallback));
1915         pDevice->bCmdRunning = FALSE;
1916         MACbShutdown(pDevice->PortOffset);
1917         return;
1918     }
1919
1920 }
1921
1922 static void device_free_tx_buf(PSDevice pDevice, PSTxDesc pDesc) {
1923     PDEVICE_TD_INFO  pTDInfo=pDesc->pTDInfo;
1924     struct sk_buff* skb=pTDInfo->skb;
1925
1926     // pre-allocated buf_dma can't be unmapped.
1927     if (pTDInfo->skb_dma && (pTDInfo->skb_dma != pTDInfo->buf_dma)) {
1928         pci_unmap_single(pDevice->pcid,pTDInfo->skb_dma,skb->len,
1929               PCI_DMA_TODEVICE);
1930     }
1931
1932     if ((pTDInfo->byFlags & TD_FLAGS_NETIF_SKB) != 0)
1933         dev_kfree_skb_irq(skb);
1934
1935     pTDInfo->skb_dma = 0;
1936     pTDInfo->skb = 0;
1937     pTDInfo->byFlags = 0;
1938 }
1939
1940
1941
1942 //PLICE_DEBUG ->
1943 VOID    InitRxManagementQueue(PSDevice  pDevice)
1944 {
1945         pDevice->rxManeQueue.packet_num = 0;
1946         pDevice->rxManeQueue.head = pDevice->rxManeQueue.tail = 0;
1947 }
1948 //PLICE_DEBUG<-
1949
1950
1951
1952
1953
1954 //PLICE_DEBUG ->
1955 INT MlmeThread(
1956      void * Context)
1957 {
1958         PSDevice        pDevice =  (PSDevice) Context;
1959         PSRxMgmtPacket                  pRxMgmtPacket;
1960         // int i ;
1961         //complete(&pDevice->notify);
1962 //printk("Enter MngWorkItem,Queue packet num is %d\n",pDevice->rxManeQueue.packet_num);
1963
1964         //printk("Enter MlmeThread,packet _num is %d\n",pDevice->rxManeQueue.packet_num);
1965         //i = 0;
1966 #if 1
1967         while (1)
1968         {
1969
1970         //printk("DDDD\n");
1971         //down(&pDevice->mlme_semaphore);
1972         // pRxMgmtPacket =  DeQueue(pDevice);
1973 #if 1
1974                 spin_lock_irq(&pDevice->lock);
1975                  while(pDevice->rxManeQueue.packet_num != 0)
1976                 {
1977                          pRxMgmtPacket =  DeQueue(pDevice);
1978                                 //pDevice;
1979                                 //DequeueManageObject(pDevice->FirstRecvMngList, pDevice->LastRecvMngList);
1980                         vMgrRxManagePacket(pDevice, pDevice->pMgmt, pRxMgmtPacket);
1981                         //printk("packet_num is %d\n",pDevice->rxManeQueue.packet_num);
1982
1983                  }
1984                 spin_unlock_irq(&pDevice->lock);
1985                 if (mlme_kill == 0)
1986                 break;
1987                 //udelay(200);
1988 #endif
1989         //printk("Before schedule thread jiffies is %x\n",jiffies);
1990         schedule();
1991         //printk("after schedule thread jiffies is %x\n",jiffies);
1992         if (mlme_kill == 0)
1993                 break;
1994         //printk("i is %d\n",i);
1995         }
1996
1997 #endif
1998         return 0;
1999
2000 }
2001
2002
2003 #ifdef PRIVATE_OBJ
2004
2005 int __device_open(HANDLE pExDevice) {
2006     PSDevice_info pDevice_info = (PSDevice_info)pExDevice;
2007     PSDevice    pDevice = (PSDevice)(pDevice_info->pWDevice);
2008
2009 #else
2010
2011 static int  device_open(struct net_device *dev) {
2012     PSDevice    pDevice=(PSDevice) netdev_priv(dev);
2013     int i;
2014 #endif
2015     pDevice->rx_buf_sz = PKT_BUF_SZ;
2016     if (!device_init_rings(pDevice)) {
2017         return -ENOMEM;
2018     }
2019 //2008-5-13 <add> by chester
2020 #ifndef PRIVATE_OBJ
2021     i=request_irq(pDevice->pcid->irq, &device_intr, IRQF_SHARED, dev->name, dev);
2022     if (i)
2023         return i;
2024 #endif
2025         //printk("DEBUG1\n");
2026 #ifdef WPA_SM_Transtatus
2027      extern SWPAResult wpa_Result;
2028      memset(wpa_Result.ifname,0,sizeof(wpa_Result.ifname));
2029      wpa_Result.proto = 0;
2030      wpa_Result.key_mgmt = 0;
2031      wpa_Result.eap_type = 0;
2032      wpa_Result.authenticated = FALSE;
2033      pDevice->fWPA_Authened = FALSE;
2034 #endif
2035 DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO "call device init rd0 ring\n");
2036 device_init_rd0_ring(pDevice);
2037     device_init_rd1_ring(pDevice);
2038     device_init_defrag_cb(pDevice);
2039     device_init_td0_ring(pDevice);
2040     device_init_td1_ring(pDevice);
2041 //    VNTWIFIvSet11h(pDevice->pMgmt, pDevice->b11hEnable);
2042
2043
2044     if (pDevice->bDiversityRegCtlON) {
2045         device_init_diversity_timer(pDevice);
2046     }
2047     vMgrObjectInit(pDevice);
2048     vMgrTimerInit(pDevice);
2049
2050 //PLICE_DEBUG->
2051 #ifdef  TASK_LET
2052         tasklet_init (&pDevice->RxMngWorkItem,(void *)MngWorkItem,(unsigned long )pDevice);
2053 #endif
2054 #ifdef  THREAD
2055         InitRxManagementQueue(pDevice);
2056         mlme_kill = 0;
2057         mlme_task = kthread_run(MlmeThread,(void *) pDevice, "MLME");
2058         if (IS_ERR(mlme_task)) {
2059                 printk("thread create fail\n");
2060                 return -1;
2061         }
2062
2063         mlme_kill = 1;
2064 #endif
2065
2066
2067
2068 #if 0
2069         pDevice->MLMEThr_pid = kernel_thread(MlmeThread, pDevice, CLONE_VM);
2070         if (pDevice->MLMEThr_pid <0 )
2071         {
2072                 printk("unable start thread MlmeThread\n");
2073                 return -1;
2074         }
2075 #endif
2076
2077         //printk("thread id is %d\n",pDevice->MLMEThr_pid);
2078         //printk("Create thread time is %x\n",jiffies);
2079         //wait_for_completion(&pDevice->notify);
2080
2081
2082
2083
2084   // if (( SROMbyReadEmbedded(pDevice->PortOffset, EEP_OFS_RADIOCTL)&0x06)==0x04)
2085     //    return -ENOMEM;
2086 DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO "call device_init_registers\n");
2087         device_init_registers(pDevice, DEVICE_INIT_COLD);
2088     MACvReadEtherAddress(pDevice->PortOffset, pDevice->abyCurrentNetAddr);
2089     memcpy(pDevice->pMgmt->abyMACAddr, pDevice->abyCurrentNetAddr, U_ETHER_ADDR_LEN);
2090 #ifdef PRIVATE_OBJ
2091     __device_set_multi(pExDevice);
2092 #else
2093     device_set_multi(pDevice->dev);
2094 #endif
2095
2096     // Init for Key Management
2097     KeyvInitTable(&pDevice->sKey, pDevice->PortOffset);
2098     add_timer(&(pDevice->pMgmt->sTimerSecondCallback));
2099
2100         #ifdef WPA_SUPPLICANT_DRIVER_WEXT_SUPPORT
2101         /*
2102      pDevice->bwextstep0 = FALSE;
2103      pDevice->bwextstep1 = FALSE;
2104      pDevice->bwextstep2 = FALSE;
2105      pDevice->bwextstep3 = FALSE;
2106      */
2107        pDevice->bwextcount=0;
2108      pDevice->bWPASuppWextEnabled = FALSE;
2109 #endif
2110     pDevice->byReAssocCount = 0;
2111    pDevice->bWPADEVUp = FALSE;
2112     // Patch: if WEP key already set by iwconfig but device not yet open
2113     if ((pDevice->bEncryptionEnable == TRUE) && (pDevice->bTransmitKey == TRUE)) {
2114         KeybSetDefaultKey(&(pDevice->sKey),
2115                             (DWORD)(pDevice->byKeyIndex | (1 << 31)),
2116                             pDevice->uKeyLength,
2117                             NULL,
2118                             pDevice->abyKey,
2119                             KEY_CTL_WEP,
2120                             pDevice->PortOffset,
2121                             pDevice->byLocalID
2122                           );
2123          pDevice->eEncryptionStatus = Ndis802_11Encryption1Enabled;
2124     }
2125
2126 //printk("DEBUG2\n");
2127
2128
2129 DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO "call MACvIntEnable\n");
2130         MACvIntEnable(pDevice->PortOffset, IMR_MASK_VALUE);
2131
2132     if (pDevice->pMgmt->eConfigMode == WMAC_CONFIG_AP) {
2133         bScheduleCommand((HANDLE)pDevice, WLAN_CMD_RUN_AP, NULL);
2134         }
2135         else {
2136         bScheduleCommand((HANDLE)pDevice, WLAN_CMD_BSSID_SCAN, NULL);
2137         bScheduleCommand((HANDLE)pDevice, WLAN_CMD_SSID, NULL);
2138     }
2139     pDevice->flags |=DEVICE_FLAGS_OPENED;
2140
2141     DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO "device_open success.. \n");
2142     return 0;
2143 }
2144
2145
2146 #ifdef PRIVATE_OBJ
2147
2148 int  __device_close(HANDLE pExDevice) {
2149     PSDevice_info pDevice_info = (PSDevice_info)pExDevice;
2150     struct net_device *dev = pDevice_info->dev;
2151     PSDevice    pDevice = (PSDevice)(pDevice_info->pWDevice);
2152
2153 #else
2154 static int  device_close(struct net_device *dev) {
2155     PSDevice  pDevice=(PSDevice) netdev_priv(dev);
2156 #endif
2157     PSMgmtObject     pMgmt = pDevice->pMgmt;
2158  //PLICE_DEBUG->
2159 #ifdef  THREAD
2160         mlme_kill = 0;
2161 #endif
2162 //PLICE_DEBUG<-
2163 //2007-1121-02<Add>by EinsnLiu
2164     if (pDevice->bLinkPass) {
2165         bScheduleCommand((HANDLE)pDevice, WLAN_CMD_DISASSOCIATE, NULL);
2166         mdelay(30);
2167     }
2168 #ifdef TxInSleep
2169     del_timer(&pDevice->sTimerTxData);
2170 #endif
2171     del_timer(&pDevice->sTimerCommand);
2172     del_timer(&pMgmt->sTimerSecondCallback);
2173     if (pDevice->bDiversityRegCtlON) {
2174         del_timer(&pDevice->TimerSQ3Tmax1);
2175         del_timer(&pDevice->TimerSQ3Tmax2);
2176         del_timer(&pDevice->TimerSQ3Tmax3);
2177     }
2178
2179 #ifdef  TASK_LET
2180         tasklet_kill(&pDevice->RxMngWorkItem);
2181 #endif
2182      netif_stop_queue(dev);
2183     pDevice->bCmdRunning = FALSE;
2184     MACbShutdown(pDevice->PortOffset);
2185     MACbSoftwareReset(pDevice->PortOffset);
2186     CARDbRadioPowerOff(pDevice);
2187
2188     pDevice->bLinkPass = FALSE;
2189     memset(pMgmt->abyCurrBSSID, 0, 6);
2190     pMgmt->eCurrState = WMAC_STATE_IDLE;
2191     device_free_td0_ring(pDevice);
2192     device_free_td1_ring(pDevice);
2193     device_free_rd0_ring(pDevice);
2194     device_free_rd1_ring(pDevice);
2195     device_free_frag_buf(pDevice);
2196     device_free_rings(pDevice);
2197     BSSvClearNodeDBTable(pDevice, 0);
2198     free_irq(dev->irq, dev);
2199     pDevice->flags &=(~DEVICE_FLAGS_OPENED);
2200         //2008-0714-01<Add>by chester
2201 device_release_WPADEV(pDevice);
2202 //PLICE_DEBUG->
2203         //tasklet_kill(&pDevice->RxMngWorkItem);
2204 //PLICE_DEBUG<-
2205     DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO "device_close.. \n");
2206     return 0;
2207 }
2208
2209 #ifdef PRIVATE_OBJ
2210
2211 int  __device_dma0_tx_80211(HANDLE pExDevice, struct sk_buff *skb) {
2212     PSDevice_info pDevice_info = (PSDevice_info)pExDevice;
2213     PSDevice    pDevice = (PSDevice)(pDevice_info->pWDevice);
2214     ref_sk_buff     ref_skb;
2215
2216 #else
2217
2218
2219 static int device_dma0_tx_80211(struct sk_buff *skb, struct net_device *dev) {
2220     PSDevice        pDevice=netdev_priv(dev);
2221 #endif
2222     PBYTE           pbMPDU;
2223     UINT            cbMPDULen = 0;
2224
2225
2226     DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO "device_dma0_tx_80211\n");
2227     spin_lock_irq(&pDevice->lock);
2228
2229     if (AVAIL_TD(pDevice, TYPE_TXDMA0) <= 0) {
2230         DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO "device_dma0_tx_80211, td0 <=0\n");
2231         dev_kfree_skb_irq(skb);
2232         spin_unlock_irq(&pDevice->lock);
2233         return 0;
2234     }
2235
2236     if (pDevice->bStopTx0Pkt == TRUE) {
2237         dev_kfree_skb_irq(skb);
2238         spin_unlock_irq(&pDevice->lock);
2239         return 0;
2240     };
2241
2242 #ifdef PRIVATE_OBJ
2243     ref_skb_remap(pDevice->dev, &ref_skb, skb);
2244     cbMPDULen = *(ref_skb.len);
2245     pbMPDU = ref_skb.data;
2246 #else
2247     cbMPDULen = skb->len;
2248     pbMPDU = skb->data;
2249 #endif
2250
2251     vDMA0_tx_80211(pDevice, skb, pbMPDU, cbMPDULen);
2252
2253     spin_unlock_irq(&pDevice->lock);
2254
2255     return 0;
2256
2257 }
2258
2259
2260
2261 BOOL device_dma0_xmit(PSDevice pDevice, struct sk_buff *skb, UINT uNodeIndex) {
2262     PSMgmtObject    pMgmt = pDevice->pMgmt;
2263     PSTxDesc        pHeadTD, pLastTD;
2264     UINT            cbFrameBodySize;
2265     UINT            uMACfragNum;
2266     BYTE            byPktTyp;
2267     BOOL            bNeedEncryption = FALSE;
2268     PSKeyItem       pTransmitKey = NULL;
2269     UINT            cbHeaderSize;
2270     UINT            ii;
2271     SKeyItem        STempKey;
2272 //    BYTE            byKeyIndex = 0;
2273 #ifdef PRIVATE_OBJ
2274     ref_sk_buff     ref_skb;
2275 #endif
2276
2277
2278     if (pDevice->bStopTx0Pkt == TRUE) {
2279         dev_kfree_skb_irq(skb);
2280         return FALSE;
2281     };
2282
2283     if (AVAIL_TD(pDevice, TYPE_TXDMA0) <= 0) {
2284         dev_kfree_skb_irq(skb);
2285         DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO "device_dma0_xmit, td0 <=0\n");
2286         return FALSE;
2287     }
2288
2289     if (pMgmt->eCurrMode == WMAC_MODE_ESS_AP) {
2290         if (pDevice->uAssocCount == 0) {
2291             dev_kfree_skb_irq(skb);
2292             DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO "device_dma0_xmit, assocCount = 0\n");
2293             return FALSE;
2294         }
2295     }
2296
2297 #ifdef PRIVATE_OBJ
2298     ref_skb_remap(pDevice->dev, &(ref_skb), skb);
2299 #endif
2300     pHeadTD = pDevice->apCurrTD[TYPE_TXDMA0];
2301
2302     pHeadTD->m_td1TD1.byTCR = (TCR_EDP|TCR_STP);
2303
2304 #ifdef PRIVATE_OBJ
2305     memcpy(pDevice->sTxEthHeader.abyDstAddr, (PBYTE)(ref_skb.data), U_HEADER_LEN);
2306     cbFrameBodySize = *(ref_skb.len) - U_HEADER_LEN;
2307
2308 #else
2309     memcpy(pDevice->sTxEthHeader.abyDstAddr, (PBYTE)(skb->data), U_HEADER_LEN);
2310     cbFrameBodySize = skb->len - U_HEADER_LEN;
2311 #endif
2312
2313     // 802.1H
2314     if (ntohs(pDevice->sTxEthHeader.wType) > MAX_DATA_LEN) {
2315         cbFrameBodySize += 8;
2316     }
2317     uMACfragNum = cbGetFragCount(pDevice, pTransmitKey, cbFrameBodySize, &pDevice->sTxEthHeader);
2318
2319     if ( uMACfragNum > AVAIL_TD(pDevice, TYPE_TXDMA0)) {
2320         dev_kfree_skb_irq(skb);
2321         return FALSE;
2322     }
2323     byPktTyp = (BYTE)pDevice->byPacketType;
2324
2325
2326     if (pDevice->bFixRate) {
2327         if (pDevice->eCurrentPHYType == PHY_TYPE_11B) {
2328             if (pDevice->uConnectionRate >= RATE_11M) {
2329                 pDevice->wCurrentRate = RATE_11M;
2330             } else {
2331                 pDevice->wCurrentRate = (WORD)pDevice->uConnectionRate;
2332             }
2333         } else {
2334             if (pDevice->uConnectionRate >= RATE_54M)
2335                 pDevice->wCurrentRate = RATE_54M;
2336             else
2337                 pDevice->wCurrentRate = (WORD)pDevice->uConnectionRate;
2338         }
2339     }
2340     else {
2341         pDevice->wCurrentRate = pDevice->pMgmt->sNodeDBTable[uNodeIndex].wTxDataRate;
2342     }
2343
2344     //preamble type
2345     if (pMgmt->sNodeDBTable[uNodeIndex].bShortPreamble) {
2346         pDevice->byPreambleType = pDevice->byShortPreamble;
2347     }
2348     else {
2349         pDevice->byPreambleType = PREAMBLE_LONG;
2350     }
2351
2352     DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO "dma0: pDevice->wCurrentRate = %d \n", pDevice->wCurrentRate);
2353
2354
2355     if (pDevice->wCurrentRate <= RATE_11M) {
2356         byPktTyp = PK_TYPE_11B;
2357     } else if (pDevice->eCurrentPHYType == PHY_TYPE_11A) {
2358         byPktTyp = PK_TYPE_11A;
2359     } else {
2360         if (pDevice->bProtectMode == TRUE) {
2361             byPktTyp = PK_TYPE_11GB;
2362         } else {
2363             byPktTyp = PK_TYPE_11GA;
2364         }
2365     }
2366
2367     if (pDevice->bEncryptionEnable == TRUE)
2368         bNeedEncryption = TRUE;
2369
2370     if (pDevice->bEnableHostWEP) {
2371         pTransmitKey = &STempKey;
2372         pTransmitKey->byCipherSuite = pMgmt->sNodeDBTable[uNodeIndex].byCipherSuite;
2373         pTransmitKey->dwKeyIndex = pMgmt->sNodeDBTable[uNodeIndex].dwKeyIndex;
2374         pTransmitKey->uKeyLength = pMgmt->sNodeDBTable[uNodeIndex].uWepKeyLength;
2375         pTransmitKey->dwTSC47_16 = pMgmt->sNodeDBTable[uNodeIndex].dwTSC47_16;
2376         pTransmitKey->wTSC15_0 = pMgmt->sNodeDBTable[uNodeIndex].wTSC15_0;
2377         memcpy(pTransmitKey->abyKey,
2378             &pMgmt->sNodeDBTable[uNodeIndex].abyWepKey[0],
2379             pTransmitKey->uKeyLength
2380             );
2381     }
2382     vGenerateFIFOHeader(pDevice, byPktTyp, pDevice->pbyTmpBuff, bNeedEncryption,
2383                         cbFrameBodySize, TYPE_TXDMA0, pHeadTD,
2384                         &pDevice->sTxEthHeader, (PBYTE)skb->data, pTransmitKey, uNodeIndex,
2385                         &uMACfragNum,
2386                         &cbHeaderSize
2387                         );
2388
2389     if (MACbIsRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_PS)) {
2390         // Disable PS
2391         MACbPSWakeup(pDevice->PortOffset);
2392     }
2393
2394     pDevice->bPWBitOn = FALSE;
2395
2396     pLastTD = pHeadTD;
2397     for (ii = 0; ii < uMACfragNum; ii++) {
2398         // Poll Transmit the adapter
2399         wmb();
2400         pHeadTD->m_td0TD0.f1Owner=OWNED_BY_NIC;
2401         wmb();
2402         if (ii == (uMACfragNum - 1))
2403             pLastTD = pHeadTD;
2404         pHeadTD = pHeadTD->next;
2405     }
2406
2407     // Save the information needed by the tx interrupt handler
2408     // to complete the Send request
2409     pLastTD->pTDInfo->skb = skb;
2410     pLastTD->pTDInfo->byFlags = 0;
2411     pLastTD->pTDInfo->byFlags |= TD_FLAGS_NETIF_SKB;
2412
2413     pDevice->apCurrTD[TYPE_TXDMA0] = pHeadTD;
2414
2415     MACvTransmit0(pDevice->PortOffset);
2416
2417
2418     return TRUE;
2419 }
2420
2421 //TYPE_AC0DMA data tx
2422 #ifdef PRIVATE_OBJ
2423
2424 int  __device_xmit(HANDLE pExDevice, struct sk_buff *skb) {
2425     PSDevice_info pDevice_info = (PSDevice_info)pExDevice;
2426     PSDevice    pDevice = (PSDevice)(pDevice_info->pWDevice);
2427     struct net_device *dev = pDevice_info->dev;
2428     ref_sk_buff     ref_skb;
2429
2430 #else
2431 static int  device_xmit(struct sk_buff *skb, struct net_device *dev) {
2432     PSDevice pDevice=netdev_priv(dev);
2433
2434 #endif
2435     PSMgmtObject    pMgmt = pDevice->pMgmt;
2436     PSTxDesc        pHeadTD, pLastTD;
2437     UINT            uNodeIndex = 0;
2438     BYTE            byMask[8] = {1, 2, 4, 8, 0x10, 0x20, 0x40, 0x80};
2439     WORD            wAID;
2440     UINT            uMACfragNum = 1;
2441     UINT            cbFrameBodySize;
2442     BYTE            byPktTyp;
2443     UINT            cbHeaderSize;
2444     BOOL            bNeedEncryption = FALSE;
2445     PSKeyItem       pTransmitKey = NULL;
2446     SKeyItem        STempKey;
2447     UINT            ii;
2448     BOOL            bTKIP_UseGTK = FALSE;
2449     BOOL            bNeedDeAuth = FALSE;
2450     PBYTE           pbyBSSID;
2451     BOOL            bNodeExist = FALSE;
2452
2453
2454
2455     spin_lock_irq(&pDevice->lock);
2456     if (pDevice->bLinkPass == FALSE) {
2457         dev_kfree_skb_irq(skb);
2458         spin_unlock_irq(&pDevice->lock);
2459         return 0;
2460     }
2461
2462     if (pDevice->bStopDataPkt) {
2463         dev_kfree_skb_irq(skb);
2464         spin_unlock_irq(&pDevice->lock);
2465         return 0;
2466     }
2467
2468 #ifdef PRIVATE_OBJ
2469     ref_skb_remap(pDevice->dev, &ref_skb, skb);
2470 #endif
2471
2472     if (pMgmt->eCurrMode == WMAC_MODE_ESS_AP) {
2473         if (pDevice->uAssocCount == 0) {
2474             dev_kfree_skb_irq(skb);
2475             spin_unlock_irq(&pDevice->lock);
2476             return 0;
2477         }
2478 #ifdef PRIVATE_OBJ
2479         if (IS_MULTICAST_ADDRESS((PBYTE)(ref_skb.data))) {
2480 #else
2481         if (IS_MULTICAST_ADDRESS((PBYTE)(skb->data))) {
2482 #endif
2483             uNodeIndex = 0;
2484             bNodeExist = TRUE;
2485             if (pMgmt->sNodeDBTable[0].bPSEnable) {
2486 #ifdef PRIVATE_OBJ
2487                 skb_queue_tail(&(pMgmt->sNodeDBTable[0].sTxPSQueue), ref_skb.skb);
2488 #else
2489                 skb_queue_tail(&(pMgmt->sNodeDBTable[0].sTxPSQueue), skb);
2490 #endif
2491                 pMgmt->sNodeDBTable[0].wEnQueueCnt++;
2492                 // set tx map
2493                 pMgmt->abyPSTxMap[0] |= byMask[0];
2494                 spin_unlock_irq(&pDevice->lock);
2495                 return 0;
2496             }
2497 }else {
2498 #ifdef PRIVATE_OBJ
2499             if (BSSDBbIsSTAInNodeDB(pMgmt, (PBYTE)(ref_skb.data), &uNodeIndex)) {
2500 #else
2501             if (BSSDBbIsSTAInNodeDB(pMgmt, (PBYTE)(skb->data), &uNodeIndex)) {
2502 #endif
2503                 if (pMgmt->sNodeDBTable[uNodeIndex].bPSEnable) {
2504 #ifdef PRIVATE_OBJ
2505                     skb_queue_tail(&pMgmt->sNodeDBTable[uNodeIndex].sTxPSQueue, ref_skb.skb);
2506 #else
2507                     skb_queue_tail(&pMgmt->sNodeDBTable[uNodeIndex].sTxPSQueue, skb);
2508 #endif
2509                     pMgmt->sNodeDBTable[uNodeIndex].wEnQueueCnt++;
2510                     // set tx map
2511                     wAID = pMgmt->sNodeDBTable[uNodeIndex].wAID;
2512                     pMgmt->abyPSTxMap[wAID >> 3] |=  byMask[wAID & 7];
2513                     DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Set:pMgmt->abyPSTxMap[%d]= %d\n",
2514                              (wAID >> 3), pMgmt->abyPSTxMap[wAID >> 3]);
2515                     spin_unlock_irq(&pDevice->lock);
2516                     return 0;
2517                 }
2518
2519                 if (pMgmt->sNodeDBTable[uNodeIndex].bShortPreamble) {
2520                     pDevice->byPreambleType = pDevice->byShortPreamble;
2521
2522                 }else {
2523                     pDevice->byPreambleType = PREAMBLE_LONG;
2524                 }
2525                 bNodeExist = TRUE;
2526
2527             }
2528         }
2529
2530         if (bNodeExist == FALSE) {
2531             DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_DEBUG"Unknown STA not found in node DB \n");
2532             dev_kfree_skb_irq(skb);
2533             spin_unlock_irq(&pDevice->lock);
2534             return 0;
2535         }
2536     }
2537
2538     pHeadTD = pDevice->apCurrTD[TYPE_AC0DMA];
2539
2540     pHeadTD->m_td1TD1.byTCR = (TCR_EDP|TCR_STP);
2541
2542
2543 #ifdef PRIVATE_OBJ
2544     memcpy(pDevice->sTxEthHeader.abyDstAddr, (PBYTE)(ref_skb.data), U_HEADER_LEN);
2545     cbFrameBodySize = *(ref_skb.len) - U_HEADER_LEN;
2546 #else
2547     memcpy(pDevice->sTxEthHeader.abyDstAddr, (PBYTE)(skb->data), U_HEADER_LEN);
2548     cbFrameBodySize = skb->len - U_HEADER_LEN;
2549 #endif
2550     // 802.1H
2551     if (ntohs(pDevice->sTxEthHeader.wType) > MAX_DATA_LEN) {
2552         cbFrameBodySize += 8;
2553     }
2554
2555
2556     if (pDevice->bEncryptionEnable == TRUE) {
2557         bNeedEncryption = TRUE;
2558         // get Transmit key
2559         do {
2560             if ((pDevice->pMgmt->eCurrMode == WMAC_MODE_ESS_STA) &&
2561                 (pDevice->pMgmt->eCurrState == WMAC_STATE_ASSOC)) {
2562                 pbyBSSID = pDevice->abyBSSID;
2563                 // get pairwise key
2564                 if (KeybGetTransmitKey(&(pDevice->sKey), pbyBSSID, PAIRWISE_KEY, &pTransmitKey) == FALSE) {
2565                     // get group key
2566                     if(KeybGetTransmitKey(&(pDevice->sKey), pbyBSSID, GROUP_KEY, &pTransmitKey) == TRUE) {
2567                         bTKIP_UseGTK = TRUE;
2568                         DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_DEBUG"Get GTK.\n");
2569                         break;
2570                     }
2571                 } else {
2572                     DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_DEBUG"Get PTK.\n");
2573                     break;
2574                 }
2575             }else if (pDevice->pMgmt->eCurrMode == WMAC_MODE_IBSS_STA) {
2576
2577                 pbyBSSID = pDevice->sTxEthHeader.abyDstAddr;  //TO_DS = 0 and FROM_DS = 0 --> 802.11 MAC Address1
2578                 DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_DEBUG"IBSS Serach Key: \n");
2579                 for (ii = 0; ii< 6; ii++)
2580                     DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_DEBUG"%x \n", *(pbyBSSID+ii));
2581                 DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_DEBUG"\n");
2582
2583                 // get pairwise key
2584                 if(KeybGetTransmitKey(&(pDevice->sKey), pbyBSSID, PAIRWISE_KEY, &pTransmitKey) == TRUE)
2585                     break;
2586             }
2587             // get group key
2588             pbyBSSID = pDevice->abyBroadcastAddr;
2589             if(KeybGetTransmitKey(&(pDevice->sKey), pbyBSSID, GROUP_KEY, &pTransmitKey) == FALSE) {
2590                 pTransmitKey = NULL;
2591                 if (pDevice->pMgmt->eCurrMode == WMAC_MODE_IBSS_STA) {
2592                     DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_DEBUG"IBSS and KEY is NULL. [%d]\n", pDevice->pMgmt->eCurrMode);
2593                 }
2594                 else
2595                     DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_DEBUG"NOT IBSS and KEY is NULL. [%d]\n", pDevice->pMgmt->eCurrMode);
2596             } else {
2597                 bTKIP_UseGTK = TRUE;
2598                 DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_DEBUG"Get GTK.\n");
2599             }
2600         } while(FALSE);
2601     }
2602
2603     if (pDevice->bEnableHostWEP) {
2604         DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_DEBUG"acdma0: STA index %d\n", uNodeIndex);
2605         if (pDevice->bEncryptionEnable == TRUE) {
2606             pTransmitKey = &STempKey;
2607             pTransmitKey->byCipherSuite = pMgmt->sNodeDBTable[uNodeIndex].byCipherSuite;
2608             pTransmitKey->dwKeyIndex = pMgmt->sNodeDBTable[uNodeIndex].dwKeyIndex;
2609             pTransmitKey->uKeyLength = pMgmt->sNodeDBTable[uNodeIndex].uWepKeyLength;
2610             pTransmitKey->dwTSC47_16 = pMgmt->sNodeDBTable[uNodeIndex].dwTSC47_16;
2611             pTransmitKey->wTSC15_0 = pMgmt->sNodeDBTable[uNodeIndex].wTSC15_0;
2612             memcpy(pTransmitKey->abyKey,
2613                 &pMgmt->sNodeDBTable[uNodeIndex].abyWepKey[0],
2614                 pTransmitKey->uKeyLength
2615                 );
2616          }
2617     }
2618
2619     uMACfragNum = cbGetFragCount(pDevice, pTransmitKey, cbFrameBodySize, &pDevice->sTxEthHeader);
2620
2621     if (uMACfragNum > AVAIL_TD(pDevice, TYPE_AC0DMA)) {
2622         DEVICE_PRT(MSG_LEVEL_ERR, KERN_DEBUG "uMACfragNum > AVAIL_TD(TYPE_AC0DMA) = %d\n", uMACfragNum);
2623         dev_kfree_skb_irq(skb);
2624         spin_unlock_irq(&pDevice->lock);
2625         return 0;
2626     }
2627
2628     if (pTransmitKey != NULL) {
2629         if ((pTransmitKey->byCipherSuite == KEY_CTL_WEP) &&
2630             (pTransmitKey->uKeyLength == WLAN_WEP232_KEYLEN)) {
2631             uMACfragNum = 1; //WEP256 doesn't support fragment
2632         }
2633     }
2634
2635     byPktTyp = (BYTE)pDevice->byPacketType;
2636
2637     if (pDevice->bFixRate) {
2638 #ifdef  PLICE_DEBUG
2639         printk("Fix Rate: PhyType is %d,ConnectionRate is %d\n",pDevice->eCurrentPHYType,pDevice->uConnectionRate);
2640 #endif
2641
2642         if (pDevice->eCurrentPHYType == PHY_TYPE_11B) {
2643             if (pDevice->uConnectionRate >= RATE_11M) {
2644                 pDevice->wCurrentRate = RATE_11M;
2645             } else {
2646                 pDevice->wCurrentRate = (WORD)pDevice->uConnectionRate;
2647             }
2648         } else {
2649             if ((pDevice->eCurrentPHYType == PHY_TYPE_11A) &&
2650                 (pDevice->uConnectionRate <= RATE_6M)) {
2651                 pDevice->wCurrentRate = RATE_6M;
2652             } else {
2653                 if (pDevice->uConnectionRate >= RATE_54M)
2654                     pDevice->wCurrentRate = RATE_54M;
2655                 else
2656                     pDevice->wCurrentRate = (WORD)pDevice->uConnectionRate;
2657
2658             }
2659         }
2660         pDevice->byACKRate = (BYTE) pDevice->wCurrentRate;
2661         pDevice->byTopCCKBasicRate = RATE_1M;
2662         pDevice->byTopOFDMBasicRate = RATE_6M;
2663     }
2664     else {
2665         //auto rate
2666     if (pDevice->sTxEthHeader.wType == TYPE_PKT_802_1x) {
2667             if (pDevice->eCurrentPHYType != PHY_TYPE_11A) {
2668                 pDevice->wCurrentRate = RATE_1M;
2669                 pDevice->byACKRate = RATE_1M;
2670                 pDevice->byTopCCKBasicRate = RATE_1M;
2671                 pDevice->byTopOFDMBasicRate = RATE_6M;
2672             } else {
2673                 pDevice->wCurrentRate = RATE_6M;
2674                 pDevice->byACKRate = RATE_6M;
2675                 pDevice->byTopCCKBasicRate = RATE_1M;
2676                 pDevice->byTopOFDMBasicRate = RATE_6M;
2677             }
2678         }
2679         else {
2680                 VNTWIFIvGetTxRate(  pDevice->pMgmt,
2681                                 pDevice->sTxEthHeader.abyDstAddr,
2682                                 &(pDevice->wCurrentRate),
2683                                 &(pDevice->byACKRate),
2684                                 &(pDevice->byTopCCKBasicRate),
2685                                 &(pDevice->byTopOFDMBasicRate));
2686
2687 #if 0
2688 printk("auto rate:Rate : %d,AckRate:%d,TopCCKRate:%d,TopOFDMRate:%d\n",
2689 pDevice->wCurrentRate,pDevice->byACKRate,
2690 pDevice->byTopCCKBasicRate,pDevice->byTopOFDMBasicRate);
2691
2692 #endif
2693
2694 #if 0
2695
2696         pDevice->wCurrentRate = 11;
2697         pDevice->byACKRate = 8;
2698         pDevice->byTopCCKBasicRate = 3;
2699         pDevice->byTopOFDMBasicRate = 8;
2700 #endif
2701
2702
2703                 }
2704     }
2705
2706 //    DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO "acdma0: pDevice->wCurrentRate = %d \n", pDevice->wCurrentRate);
2707
2708     if (pDevice->wCurrentRate <= RATE_11M) {
2709         byPktTyp = PK_TYPE_11B;
2710     } else if (pDevice->eCurrentPHYType == PHY_TYPE_11A) {
2711         byPktTyp = PK_TYPE_11A;
2712     } else {
2713         if (pDevice->bProtectMode == TRUE) {
2714             byPktTyp = PK_TYPE_11GB;
2715         } else {
2716             byPktTyp = PK_TYPE_11GA;
2717         }
2718     }
2719
2720 //#ifdef        PLICE_DEBUG
2721 //      printk("FIX RATE:CurrentRate is %d");
2722 //#endif
2723
2724     if (bNeedEncryption == TRUE) {
2725         DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ntohs Pkt Type=%04x\n", ntohs(pDevice->sTxEthHeader.wType));
2726         if ((pDevice->sTxEthHeader.wType) == TYPE_PKT_802_1x) {
2727             bNeedEncryption = FALSE;
2728             DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Pkt Type=%04x\n", (pDevice->sTxEthHeader.wType));
2729             if ((pDevice->pMgmt->eCurrMode == WMAC_MODE_ESS_STA) && (pDevice->pMgmt->eCurrState == WMAC_STATE_ASSOC)) {
2730                 if (pTransmitKey == NULL) {
2731                     DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Don't Find TX KEY\n");
2732                 }
2733                 else {
2734                     if (bTKIP_UseGTK == TRUE) {
2735                         DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO"error: KEY is GTK!!~~\n");
2736                     }
2737                     else {
2738                         DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Find PTK [%lX]\n", pTransmitKey->dwKeyIndex);
2739                         bNeedEncryption = TRUE;
2740                     }
2741                 }
2742             }
2743
2744             if (pDevice->byCntMeasure == 2) {
2745                 bNeedDeAuth = TRUE;
2746                 pDevice->s802_11Counter.TKIPCounterMeasuresInvoked++;
2747             }
2748
2749             if (pDevice->bEnableHostWEP) {
2750                 if ((uNodeIndex != 0) &&
2751                     (pMgmt->sNodeDBTable[uNodeIndex].dwKeyIndex & PAIRWISE_KEY)) {
2752                     DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Find PTK [%lX]\n", pTransmitKey->dwKeyIndex);
2753                     bNeedEncryption = TRUE;
2754                  }
2755              }
2756         }
2757         else {
2758             if (pTransmitKey == NULL) {
2759                 DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO"return no tx key\n");
2760                 dev_kfree_skb_irq(skb);
2761                 spin_unlock_irq(&pDevice->lock);
2762                 return 0;
2763             }
2764         }
2765     }
2766
2767
2768 #ifdef PRIVATE_OBJ
2769     vGenerateFIFOHeader(pDevice, byPktTyp, pDevice->pbyTmpBuff, bNeedEncryption,
2770                         cbFrameBodySize, TYPE_AC0DMA, pHeadTD,
2771                         &pDevice->sTxEthHeader, (PBYTE)ref_skb.data, pTransmitKey, uNodeIndex,
2772                         &uMACfragNum,
2773                         &cbHeaderSize
2774                         );
2775 #else
2776 #ifdef  PLICE_DEBUG
2777         //if (skb->len == 98)
2778         //{
2779         //      printk("ping:len is %d\n");
2780         //}
2781 #endif
2782     vGenerateFIFOHeader(pDevice, byPktTyp, pDevice->pbyTmpBuff, bNeedEncryption,
2783                         cbFrameBodySize, TYPE_AC0DMA, pHeadTD,
2784                         &pDevice->sTxEthHeader, (PBYTE)skb->data, pTransmitKey, uNodeIndex,
2785                         &uMACfragNum,
2786                         &cbHeaderSize
2787                         );
2788 #endif
2789
2790     if (MACbIsRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_PS)) {
2791         // Disable PS
2792         MACbPSWakeup(pDevice->PortOffset);
2793     }
2794     pDevice->bPWBitOn = FALSE;
2795
2796     pLastTD = pHeadTD;
2797     for (ii = 0; ii < uMACfragNum; ii++) {
2798         // Poll Transmit the adapter
2799         wmb();
2800         pHeadTD->m_td0TD0.f1Owner=OWNED_BY_NIC;
2801         wmb();
2802         if (ii == uMACfragNum - 1)
2803             pLastTD = pHeadTD;
2804         pHeadTD = pHeadTD->next;
2805     }
2806
2807     // Save the information needed by the tx interrupt handler
2808     // to complete the Send request
2809 #ifdef PRIVATE_OBJ
2810     pLastTD->pTDInfo->skb = ref_skb.skb;
2811 #else
2812     pLastTD->pTDInfo->skb = skb;
2813 #endif
2814     pLastTD->pTDInfo->byFlags = 0;
2815     pLastTD->pTDInfo->byFlags |= TD_FLAGS_NETIF_SKB;
2816 #ifdef TxInSleep
2817   pDevice->nTxDataTimeCout=0; //2008-8-21 chester <add> for send null packet
2818   #endif
2819     if (AVAIL_TD(pDevice, TYPE_AC0DMA) <= 1) {
2820         netif_stop_queue(dev);
2821     }
2822
2823     pDevice->apCurrTD[TYPE_AC0DMA] = pHeadTD;
2824 //#ifdef        PLICE_DEBUG
2825         if (pDevice->bFixRate)
2826         {
2827                 printk("FixRate:Rate is %d,TxPower is %d\n",pDevice->wCurrentRate,pDevice->byCurPwr);
2828         }
2829         else
2830         {
2831                 //printk("Auto Rate:Rate is %d,TxPower is %d\n",pDevice->wCurrentRate,pDevice->byCurPwr);
2832         }
2833 //#endif
2834
2835 {
2836     BYTE  Protocol_Version;    //802.1x Authentication
2837     BYTE  Packet_Type;           //802.1x Authentication
2838     BYTE  Descriptor_type;
2839     WORD Key_info;
2840 BOOL            bTxeapol_key = FALSE;
2841     Protocol_Version = skb->data[U_HEADER_LEN];
2842     Packet_Type = skb->data[U_HEADER_LEN+1];
2843     Descriptor_type = skb->data[U_HEADER_LEN+1+1+2];
2844     Key_info = (skb->data[U_HEADER_LEN+1+1+2+1] << 8)|(skb->data[U_HEADER_LEN+1+1+2+2]);
2845    if (pDevice->sTxEthHeader.wType == TYPE_PKT_802_1x) {
2846            if(((Protocol_Version==1) ||(Protocol_Version==2)) &&
2847                 (Packet_Type==3)) {  //802.1x OR eapol-key challenge frame transfer
2848                         bTxeapol_key = TRUE;
2849                 if((Descriptor_type==254)||(Descriptor_type==2)) {       //WPA or RSN
2850                        if(!(Key_info & BIT3) &&   //group-key challenge
2851                            (Key_info & BIT8) && (Key_info & BIT9)) {    //send 2/2 key
2852                           pDevice->fWPA_Authened = TRUE;
2853                           if(Descriptor_type==254)
2854                               printk("WPA ");
2855                           else
2856                               printk("WPA2 ");
2857                           printk("Authentication completed!!\n");
2858                         }
2859                  }
2860              }
2861    }
2862 }
2863
2864     MACvTransmitAC0(pDevice->PortOffset);
2865 //    DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO "acdma0:pDevice->apCurrTD= %p\n", pHeadTD);
2866
2867 #ifdef PRIVATE_OBJ
2868     ref_set_tx_jiffies(pDevice->dev);
2869 #else
2870     dev->trans_start = jiffies;
2871 #endif
2872
2873     spin_unlock_irq(&pDevice->lock);
2874     return 0;
2875
2876 }
2877
2878 #ifdef PRIVATE_OBJ
2879
2880 int __device_intr(int irq, HANDLE pExDevice, struct pt_regs *regs) {
2881     PSDevice_info pDevice_info = (PSDevice_info)pExDevice;
2882     PSDevice    pDevice = (PSDevice)(pDevice_info->pWDevice);
2883
2884
2885 #else
2886 static  irqreturn_t  device_intr(int irq,  void *dev_instance) {
2887     struct net_device* dev=dev_instance;
2888     PSDevice     pDevice=(PSDevice) netdev_priv(dev);
2889 #endif
2890
2891     int             max_count=0;
2892     DWORD           dwMIBCounter=0;
2893     PSMgmtObject    pMgmt = pDevice->pMgmt;
2894     BYTE            byOrgPageSel=0;
2895     int             handled = 0;
2896     BYTE            byData = 0;
2897     int             ii= 0;
2898 //    BYTE            byRSSI;
2899
2900
2901     MACvReadISR(pDevice->PortOffset, &pDevice->dwIsr);
2902
2903     if (pDevice->dwIsr == 0)
2904         return IRQ_RETVAL(handled);
2905
2906     if (pDevice->dwIsr == 0xffffffff) {
2907         DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO "dwIsr = 0xffff\n");
2908         return IRQ_RETVAL(handled);
2909     }
2910     /*
2911       // 2008-05-21 <mark> by Richardtai, we can't read RSSI here, because no packet bound with RSSI
2912
2913         if ((BITbIsBitOn(pDevice->dwIsr, ISR_RXDMA0)) &&
2914         (pDevice->byLocalID != REV_ID_VT3253_B0) &&
2915         (pDevice->bBSSIDFilter == TRUE)) {
2916         // update RSSI
2917         //BBbReadEmbeded(pDevice->PortOffset, 0x3E, &byRSSI);
2918         //pDevice->uCurrRSSI = byRSSI;
2919     }
2920     */
2921
2922     handled = 1;
2923     MACvIntDisable(pDevice->PortOffset);
2924     spin_lock_irq(&pDevice->lock);
2925
2926     //Make sure current page is 0
2927     VNSvInPortB(pDevice->PortOffset + MAC_REG_PAGE1SEL, &byOrgPageSel);
2928     if (byOrgPageSel == 1) {
2929         MACvSelectPage0(pDevice->PortOffset);
2930     }
2931     else
2932         byOrgPageSel = 0;
2933
2934     MACvReadMIBCounter(pDevice->PortOffset, &dwMIBCounter);
2935     // TBD....
2936     // Must do this after doing rx/tx, cause ISR bit is slow
2937     // than RD/TD write back
2938     // update ISR counter
2939     STAvUpdate802_11Counter(&pDevice->s802_11Counter, &pDevice->scStatistic , dwMIBCounter);
2940     while (pDevice->dwIsr != 0) {
2941
2942         STAvUpdateIsrStatCounter(&pDevice->scStatistic, pDevice->dwIsr);
2943         MACvWriteISR(pDevice->PortOffset, pDevice->dwIsr);
2944
2945         if (pDevice->dwIsr & ISR_FETALERR){
2946             DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO " ISR_FETALERR \n");
2947             VNSvOutPortB(pDevice->PortOffset + MAC_REG_SOFTPWRCTL, 0);
2948             VNSvOutPortW(pDevice->PortOffset + MAC_REG_SOFTPWRCTL, SOFTPWRCTL_SWPECTI);
2949             device_error(pDevice, pDevice->dwIsr);
2950         }
2951
2952         if (pDevice->byLocalID > REV_ID_VT3253_B1) {
2953
2954             if (BITbIsBitOn(pDevice->dwIsr, ISR_MEASURESTART)) {
2955                 // 802.11h measure start
2956                 pDevice->byOrgChannel = pDevice->byCurrentCh;
2957                 VNSvInPortB(pDevice->PortOffset + MAC_REG_RCR, &(pDevice->byOrgRCR));
2958                 VNSvOutPortB(pDevice->PortOffset + MAC_REG_RCR, (RCR_RXALLTYPE | RCR_UNICAST | RCR_BROADCAST | RCR_MULTICAST | RCR_WPAERR));
2959                 MACvSelectPage1(pDevice->PortOffset);
2960                 VNSvInPortD(pDevice->PortOffset + MAC_REG_MAR0, &(pDevice->dwOrgMAR0));
2961                 VNSvInPortD(pDevice->PortOffset + MAC_REG_MAR4, &(pDevice->dwOrgMAR4));
2962                 MACvSelectPage0(pDevice->PortOffset);
2963                //xxxx
2964                // WCMDbFlushCommandQueue(pDevice->pMgmt, TRUE);
2965                 if (CARDbSetChannel(pDevice, pDevice->pCurrMeasureEID->sReq.byChannel) == TRUE) {
2966                     pDevice->bMeasureInProgress = TRUE;
2967                     MACvSelectPage1(pDevice->PortOffset);
2968                     MACvRegBitsOn(pDevice->PortOffset, MAC_REG_MSRCTL, MSRCTL_READY);
2969                     MACvSelectPage0(pDevice->PortOffset);
2970                     pDevice->byBasicMap = 0;
2971                     pDevice->byCCAFraction = 0;
2972                     for(ii=0;ii<8;ii++) {
2973                         pDevice->dwRPIs[ii] = 0;
2974                     }
2975                 } else {
2976                     // can not measure because set channel fail
2977                    // WCMDbResetCommandQueue(pDevice->pMgmt);
2978                     // clear measure control
2979                     MACvRegBitsOff(pDevice->PortOffset, MAC_REG_MSRCTL, MSRCTL_EN);
2980                     s_vCompleteCurrentMeasure(pDevice, MEASURE_MODE_INCAPABLE);
2981                     MACvSelectPage1(pDevice->PortOffset);
2982                     MACvRegBitsOn(pDevice->PortOffset, MAC_REG_MSRCTL+1, MSRCTL1_TXPAUSE);
2983                     MACvSelectPage0(pDevice->PortOffset);
2984                 }
2985             }
2986             if (BITbIsBitOn(pDevice->dwIsr, ISR_MEASUREEND)) {
2987                 // 802.11h measure end
2988                 pDevice->bMeasureInProgress = FALSE;
2989                 VNSvOutPortB(pDevice->PortOffset + MAC_REG_RCR, pDevice->byOrgRCR);
2990                 MACvSelectPage1(pDevice->PortOffset);
2991                 VNSvOutPortD(pDevice->PortOffset + MAC_REG_MAR0, pDevice->dwOrgMAR0);
2992                 VNSvOutPortD(pDevice->PortOffset + MAC_REG_MAR4, pDevice->dwOrgMAR4);
2993                 VNSvInPortB(pDevice->PortOffset + MAC_REG_MSRBBSTS, &byData);
2994                 pDevice->byBasicMap |= (byData >> 4);
2995                 VNSvInPortB(pDevice->PortOffset + MAC_REG_CCAFRACTION, &pDevice->byCCAFraction);
2996                 VNSvInPortB(pDevice->PortOffset + MAC_REG_MSRCTL, &byData);
2997                 // clear measure control
2998                 MACvRegBitsOff(pDevice->PortOffset, MAC_REG_MSRCTL, MSRCTL_EN);
2999                 MACvSelectPage0(pDevice->PortOffset);
3000                 CARDbSetChannel(pDevice, pDevice->byOrgChannel);
3001                 // WCMDbResetCommandQueue(pDevice->pMgmt);
3002                 MACvSelectPage1(pDevice->PortOffset);
3003                 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_MSRCTL+1, MSRCTL1_TXPAUSE);
3004                 MACvSelectPage0(pDevice->PortOffset);
3005                 if (BITbIsBitOn(byData, MSRCTL_FINISH)) {
3006                     // measure success
3007                     s_vCompleteCurrentMeasure(pDevice, 0);
3008                 } else {
3009                     // can not measure because not ready before end of measure time
3010                     s_vCompleteCurrentMeasure(pDevice, MEASURE_MODE_LATE);
3011                 }
3012             }
3013             if (BITbIsBitOn(pDevice->dwIsr, ISR_QUIETSTART)) {
3014                 do {
3015                     ;
3016                 } while (CARDbStartQuiet(pDevice) == FALSE);
3017             }
3018         }
3019
3020         if (pDevice->dwIsr & ISR_TBTT) {
3021             if (pDevice->bEnableFirstQuiet == TRUE) {
3022                 pDevice->byQuietStartCount--;
3023                 if (pDevice->byQuietStartCount == 0) {
3024                     pDevice->bEnableFirstQuiet = FALSE;
3025                     MACvSelectPage1(pDevice->PortOffset);
3026                     MACvRegBitsOn(pDevice->PortOffset, MAC_REG_MSRCTL, (MSRCTL_QUIETTXCHK | MSRCTL_QUIETEN));
3027                     MACvSelectPage0(pDevice->PortOffset);
3028                 }
3029             }
3030             if ((pDevice->bChannelSwitch == TRUE) &&
3031                 (pDevice->eOPMode == OP_MODE_INFRASTRUCTURE)) {
3032                 pDevice->byChannelSwitchCount--;
3033                 if (pDevice->byChannelSwitchCount == 0) {
3034                     pDevice->bChannelSwitch = FALSE;
3035                     CARDbSetChannel(pDevice, pDevice->byNewChannel);
3036                     VNTWIFIbChannelSwitch(pDevice->pMgmt, pDevice->byNewChannel);
3037                     MACvSelectPage1(pDevice->PortOffset);
3038                     MACvRegBitsOn(pDevice->PortOffset, MAC_REG_MSRCTL+1, MSRCTL1_TXPAUSE);
3039                     MACvSelectPage0(pDevice->PortOffset);
3040                     CARDbStartTxPacket(pDevice, PKT_TYPE_802_11_ALL);
3041
3042                 }
3043             }
3044             if (pDevice->eOPMode == OP_MODE_ADHOC) {
3045                 //pDevice->bBeaconSent = FALSE;
3046             } else {
3047                 if ((pDevice->bUpdateBBVGA) && (pDevice->bLinkPass == TRUE) && (pDevice->uCurrRSSI != 0)) {
3048                     LONG            ldBm;
3049
3050                     RFvRSSITodBm(pDevice, (BYTE) pDevice->uCurrRSSI, &ldBm);
3051                     for (ii=0;ii<BB_VGA_LEVEL;ii++) {
3052                         if (ldBm < pDevice->ldBmThreshold[ii]) {
3053                             pDevice->byBBVGANew = pDevice->abyBBVGA[ii];
3054                             break;
3055                         }
3056                     }
3057                     if (pDevice->byBBVGANew != pDevice->byBBVGACurrent) {
3058                         pDevice->uBBVGADiffCount++;
3059                         if (pDevice->uBBVGADiffCount == 1) {
3060                             // first VGA diff gain
3061                             BBvSetVGAGainOffset(pDevice, pDevice->byBBVGANew);
3062                             DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO"First RSSI[%d] NewGain[%d] OldGain[%d] Count[%d]\n",
3063                                             (int)ldBm, pDevice->byBBVGANew, pDevice->byBBVGACurrent, (int)pDevice->uBBVGADiffCount);
3064                         }
3065                         if (pDevice->uBBVGADiffCount >= BB_VGA_CHANGE_THRESHOLD) {
3066                             DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO"RSSI[%d] NewGain[%d] OldGain[%d] Count[%d]\n",
3067                                             (int)ldBm, pDevice->byBBVGANew, pDevice->byBBVGACurrent, (int)pDevice->uBBVGADiffCount);
3068                             BBvSetVGAGainOffset(pDevice, pDevice->byBBVGANew);
3069                         }
3070                     } else {
3071                         pDevice->uBBVGADiffCount = 1;
3072                     }
3073                 }
3074             }
3075
3076             pDevice->bBeaconSent = FALSE;
3077             if (pDevice->bEnablePSMode) {
3078                 PSbIsNextTBTTWakeUp((HANDLE)pDevice);
3079             };
3080
3081             if ((pDevice->eOPMode == OP_MODE_AP) ||
3082                 (pDevice->eOPMode == OP_MODE_ADHOC)) {
3083
3084                 MACvOneShotTimer1MicroSec(pDevice->PortOffset,
3085                         (pMgmt->wIBSSBeaconPeriod - MAKE_BEACON_RESERVED) << 10);
3086             }
3087
3088             if (pDevice->eOPMode == OP_MODE_ADHOC && pDevice->pMgmt->wCurrATIMWindow > 0) {
3089                 // todo adhoc PS mode
3090             };
3091
3092         }
3093
3094         if (pDevice->dwIsr & ISR_BNTX) {
3095
3096             if (pDevice->eOPMode == OP_MODE_ADHOC) {
3097                 pDevice->bIsBeaconBufReadySet = FALSE;
3098                 pDevice->cbBeaconBufReadySetCnt = 0;
3099             };
3100
3101             if (pDevice->eOPMode == OP_MODE_AP) {
3102                 if(pMgmt->byDTIMCount > 0) {
3103                    pMgmt->byDTIMCount --;
3104                    pMgmt->sNodeDBTable[0].bRxPSPoll = FALSE;
3105                 }
3106                 else {
3107                     if(pMgmt->byDTIMCount == 0) {
3108                         // check if mutltcast tx bufferring
3109                         pMgmt->byDTIMCount = pMgmt->byDTIMPeriod - 1;
3110                         pMgmt->sNodeDBTable[0].bRxPSPoll = TRUE;
3111                         bScheduleCommand((HANDLE)pDevice, WLAN_CMD_RX_PSPOLL, NULL);
3112                     }
3113                 }
3114             }
3115             pDevice->bBeaconSent = TRUE;
3116
3117             if (pDevice->bChannelSwitch == TRUE) {
3118                 pDevice->byChannelSwitchCount--;
3119                 if (pDevice->byChannelSwitchCount == 0) {
3120                     pDevice->bChannelSwitch = FALSE;
3121                     CARDbSetChannel(pDevice, pDevice->byNewChannel);
3122                     VNTWIFIbChannelSwitch(pDevice->pMgmt, pDevice->byNewChannel);
3123                     MACvSelectPage1(pDevice->PortOffset);
3124                     MACvRegBitsOn(pDevice->PortOffset, MAC_REG_MSRCTL+1, MSRCTL1_TXPAUSE);
3125                     MACvSelectPage0(pDevice->PortOffset);
3126                     //VNTWIFIbSendBeacon(pDevice->pMgmt);
3127                     CARDbStartTxPacket(pDevice, PKT_TYPE_802_11_ALL);
3128                 }
3129             }
3130
3131         }
3132
3133         if (pDevice->dwIsr & ISR_RXDMA0) {
3134             max_count += device_rx_srv(pDevice, TYPE_RXDMA0);
3135         }
3136         if (pDevice->dwIsr & ISR_RXDMA1) {
3137             max_count += device_rx_srv(pDevice, TYPE_RXDMA1);
3138         }
3139         if (pDevice->dwIsr & ISR_TXDMA0){
3140             max_count += device_tx_srv(pDevice, TYPE_TXDMA0);
3141         }
3142         if (pDevice->dwIsr & ISR_AC0DMA){
3143             max_count += device_tx_srv(pDevice, TYPE_AC0DMA);
3144         }
3145         if (pDevice->dwIsr & ISR_SOFTTIMER) {
3146
3147         }
3148         if (pDevice->dwIsr & ISR_SOFTTIMER1) {
3149             if (pDevice->eOPMode == OP_MODE_AP) {
3150                if (pDevice->bShortSlotTime)
3151                    pMgmt->wCurrCapInfo |= WLAN_SET_CAP_INFO_SHORTSLOTTIME(1);
3152                else
3153                    pMgmt->wCurrCapInfo &= ~(WLAN_SET_CAP_INFO_SHORTSLOTTIME(1));
3154             }
3155             bMgrPrepareBeaconToSend(pDevice, pMgmt);
3156             pDevice->byCntMeasure = 0;
3157         }
3158
3159         MACvReadISR(pDevice->PortOffset, &pDevice->dwIsr);
3160
3161         MACvReceive0(pDevice->PortOffset);
3162         MACvReceive1(pDevice->PortOffset);
3163
3164         if (max_count>pDevice->sOpts.int_works)
3165             break;
3166     }
3167
3168     if (byOrgPageSel == 1) {
3169         MACvSelectPage1(pDevice->PortOffset);
3170     }
3171
3172     spin_unlock_irq(&pDevice->lock);
3173     MACvIntEnable(pDevice->PortOffset, IMR_MASK_VALUE);
3174
3175     return IRQ_RETVAL(handled);
3176 }
3177
3178
3179 static unsigned const ethernet_polynomial = 0x04c11db7U;
3180 static inline u32 ether_crc(int length, unsigned char *data)
3181 {
3182     int crc = -1;
3183
3184     while(--length >= 0) {
3185         unsigned char current_octet = *data++;
3186         int bit;
3187         for (bit = 0; bit < 8; bit++, current_octet >>= 1) {
3188             crc = (crc << 1) ^
3189                 ((crc < 0) ^ (current_octet & 1) ? ethernet_polynomial : 0);
3190         }
3191     }
3192     return crc;
3193 }
3194
3195 //2008-8-4 <add> by chester
3196 static int Config_FileGetParameter(UCHAR *string, UCHAR *dest,UCHAR *source)
3197 {
3198   UCHAR buf1[100];
3199   int source_len = strlen(source);
3200
3201     memset(buf1,0,100);
3202     strcat(buf1, string);
3203     strcat(buf1, "=");
3204     source+=strlen(buf1);
3205
3206    memcpy(dest,source,source_len-strlen(buf1));
3207  return TRUE;
3208 }
3209
3210 int Config_FileOperation(PSDevice pDevice,BOOL fwrite,unsigned char *Parameter) {
3211     UCHAR    *config_path=CONFIG_PATH;
3212     UCHAR    *buffer=NULL;
3213     UCHAR      tmpbuffer[20];
3214     struct file   *filp=NULL;
3215     mm_segment_t old_fs = get_fs();
3216     //int oldfsuid=0,oldfsgid=0;
3217     int result=0;
3218
3219     set_fs (KERNEL_DS);
3220
3221     /* Can't do this anymore, so we rely on correct filesystem permissions:
3222     //Make sure a caller can read or write power as root
3223     oldfsuid=current->cred->fsuid;
3224     oldfsgid=current->cred->fsgid;
3225     current->cred->fsuid = 0;
3226     current->cred->fsgid = 0;
3227     */
3228
3229     //open file
3230       filp = filp_open(config_path, O_RDWR, 0);
3231         if (IS_ERR(filp)) {
3232              printk("Config_FileOperation:open file fail?\n");
3233              result=-1;
3234              goto error2;
3235           }
3236
3237      if(!(filp->f_op) || !(filp->f_op->read) ||!(filp->f_op->write)) {
3238            printk("file %s cann't readable or writable?\n",config_path);
3239           result = -1;
3240           goto error1;
3241         }
3242
3243 buffer = (UCHAR *)kmalloc(1024, GFP_KERNEL);
3244 if(buffer==NULL) {
3245   printk("alllocate mem for file fail?\n");
3246   result = -1;
3247   goto error1;
3248 }
3249
3250 if(filp->f_op->read(filp, buffer, 1024, &filp->f_pos)<0) {
3251  printk("read file error?\n");
3252  result = -1;
3253  goto error1;
3254 }
3255
3256 if(Config_FileGetParameter("ZONETYPE",tmpbuffer,buffer)!=TRUE) {
3257   printk("get parameter error?\n");
3258   result = -1;
3259   goto error1;
3260 }
3261
3262 if(memcmp(tmpbuffer,"USA",3)==0) {
3263   result=ZoneType_USA;
3264 }
3265 else if(memcmp(tmpbuffer,"JAPAN",5)==0) {
3266   result=ZoneType_Japan;
3267 }
3268 else if(memcmp(tmpbuffer,"EUROPE",5)==0) {
3269  result=ZoneType_Europe;
3270 }
3271 else {
3272   result = -1;
3273   printk("Unknown Zonetype[%s]?\n",tmpbuffer);
3274 }
3275
3276 error1:
3277   if(buffer)
3278          kfree(buffer);
3279
3280   if(filp_close(filp,NULL))
3281        printk("Config_FileOperation:close file fail\n");
3282
3283 error2:
3284   set_fs (old_fs);
3285
3286   /*
3287   current->cred->fsuid=oldfsuid;
3288   current->cred->fsgid=oldfsgid;
3289   */
3290
3291   return result;
3292 }
3293
3294
3295 #ifdef PRIVATE_OBJ
3296
3297 void __device_set_multi(HANDLE pExDevice) {
3298     PSDevice_info pDevice_info = (PSDevice_info)pExDevice;
3299     ref_net_device  *dev = &(pDevice_info->ref_dev);
3300     PSDevice    pDevice = (PSDevice)(pDevice_info->pWDevice);
3301
3302 #else
3303
3304 static void device_set_multi(struct net_device *dev) {
3305     PSDevice         pDevice = (PSDevice) netdev_priv(dev);
3306 #endif
3307
3308     PSMgmtObject     pMgmt = pDevice->pMgmt;
3309     u32              mc_filter[2];
3310     int              i;
3311     struct dev_mc_list  *mclist;
3312
3313
3314     VNSvInPortB(pDevice->PortOffset + MAC_REG_RCR, &(pDevice->byRxMode));
3315
3316 #ifdef PRIVATE_OBJ
3317     if (*(dev->flags) & IFF_PROMISC) {         /* Set promiscuous. */
3318         DEVICE_PRT(MSG_LEVEL_ERR,KERN_ERR "%s: Promiscuous mode enabled.\n", pDevice->dev->name);
3319
3320 #else
3321     if (dev->flags & IFF_PROMISC) {         /* Set promiscuous. */
3322         DEVICE_PRT(MSG_LEVEL_ERR,KERN_NOTICE "%s: Promiscuous mode enabled.\n", dev->name);
3323 #endif
3324         /* Unconditionally log net taps. */
3325         pDevice->byRxMode |= (RCR_MULTICAST|RCR_BROADCAST|RCR_UNICAST);
3326     }
3327 #ifdef PRIVATE_OBJ
3328     else if ((*(dev->mc_count) > pDevice->multicast_limit)
3329         ||  (*(dev->flags) & IFF_ALLMULTI)) {
3330 #else
3331     else if ((dev->mc_count > pDevice->multicast_limit)
3332         ||  (dev->flags & IFF_ALLMULTI)) {
3333 #endif
3334         MACvSelectPage1(pDevice->PortOffset);
3335         VNSvOutPortD(pDevice->PortOffset + MAC_REG_MAR0, 0xffffffff);
3336         VNSvOutPortD(pDevice->PortOffset + MAC_REG_MAR0 + 4, 0xffffffff);
3337         MACvSelectPage0(pDevice->PortOffset);
3338         pDevice->byRxMode |= (RCR_MULTICAST|RCR_BROADCAST);
3339     }
3340     else {
3341         memset(mc_filter, 0, sizeof(mc_filter));
3342 #ifdef PRIVATE_OBJ
3343         for (i = 0, mclist = dev->mc_list; mclist && i < *(dev->mc_count);
3344              i++, mclist = mclist->next) {
3345 #else
3346         for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;
3347              i++, mclist = mclist->next) {
3348 #endif
3349             int bit_nr = ether_crc(ETH_ALEN, mclist->dmi_addr) >> 26;
3350             mc_filter[bit_nr >> 5] |= cpu_to_le32(1 << (bit_nr & 31));
3351         }
3352         MACvSelectPage1(pDevice->PortOffset);
3353         VNSvOutPortD(pDevice->PortOffset + MAC_REG_MAR0, mc_filter[0]);
3354         VNSvOutPortD(pDevice->PortOffset + MAC_REG_MAR0 + 4, mc_filter[1]);
3355         MACvSelectPage0(pDevice->PortOffset);
3356         pDevice->byRxMode &= ~(RCR_UNICAST);
3357         pDevice->byRxMode |= (RCR_MULTICAST|RCR_BROADCAST);
3358     }
3359
3360     if (pMgmt->eConfigMode == WMAC_CONFIG_AP) {
3361         // If AP mode, don't enable RCR_UNICAST. Since hw only compare addr1 with local mac.
3362         pDevice->byRxMode |= (RCR_MULTICAST|RCR_BROADCAST);
3363         pDevice->byRxMode &= ~(RCR_UNICAST);
3364     }
3365
3366     VNSvOutPortB(pDevice->PortOffset + MAC_REG_RCR, pDevice->byRxMode);
3367     DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO "pDevice->byRxMode = %x\n", pDevice->byRxMode );
3368 }
3369
3370
3371 #ifdef PRIVATE_OBJ
3372
3373 struct net_device_stats *__device_get_stats(HANDLE pExDevice) {
3374     PSDevice_info pDevice_info = (PSDevice_info)pExDevice;
3375     PSDevice    pDevice = (PSDevice)(pDevice_info->pWDevice);
3376
3377 #else
3378 static struct net_device_stats *device_get_stats(struct net_device *dev) {
3379     PSDevice pDevice=(PSDevice) netdev_priv(dev);
3380 #endif
3381
3382     return &pDevice->stats;
3383 }
3384
3385
3386 #ifdef PRIVATE_OBJ
3387
3388 int __device_ioctl(HANDLE pExDevice, struct ifreq *rq, int cmd) {
3389     PSDevice_info pDevice_info = (PSDevice_info)pExDevice;
3390     struct net_device *dev = pDevice_info->dev;
3391     PSDevice    pDevice = (PSDevice)(pDevice_info->pWDevice);
3392
3393 #else
3394
3395 static int  device_ioctl(struct net_device *dev, struct ifreq *rq, int cmd) {
3396         PSDevice                pDevice = (PSDevice)netdev_priv(dev);
3397 #endif
3398
3399 #ifdef WIRELESS_EXT
3400         struct iwreq *wrq = (struct iwreq *) rq;
3401         int                 rc =0;
3402 #endif
3403     PSMgmtObject        pMgmt = pDevice->pMgmt;
3404     PSCmdRequest        pReq;
3405
3406
3407     if (pMgmt == NULL) {
3408         rc = -EFAULT;
3409         return rc;
3410     }
3411
3412     switch(cmd) {
3413
3414 #ifdef WIRELESS_EXT
3415 //#if WIRELESS_EXT < 13
3416
3417         case SIOCGIWNAME:
3418                 rc = iwctl_giwname(dev, NULL, (char *)&(wrq->u.name), NULL);
3419                 break;
3420
3421         case SIOCGIWNWID:     //0x8b03  support
3422         #ifdef  WPA_SUPPLICANT_DRIVER_WEXT_SUPPORT
3423           rc = iwctl_giwnwid(dev, NULL, &(wrq->u.nwid), NULL);
3424         #else
3425         rc = -EOPNOTSUPP;
3426         #endif
3427                 break;
3428
3429                 // Set frequency/channel
3430         case SIOCSIWFREQ:
3431             rc = iwctl_siwfreq(dev, NULL, &(wrq->u.freq), NULL);
3432                 break;
3433
3434                 // Get frequency/channel
3435         case SIOCGIWFREQ:
3436                 rc = iwctl_giwfreq(dev, NULL, &(wrq->u.freq), NULL);
3437                 break;
3438
3439                 // Set desired network name (ESSID)
3440         case SIOCSIWESSID:
3441
3442                 {
3443                         char essid[IW_ESSID_MAX_SIZE+1];
3444                         if (wrq->u.essid.length > IW_ESSID_MAX_SIZE) {
3445                                 rc = -E2BIG;
3446                                 break;
3447                         }
3448                         if (copy_from_user(essid, wrq->u.essid.pointer,
3449                                            wrq->u.essid.length)) {
3450                                 rc = -EFAULT;
3451                                 break;
3452                         }
3453                         rc = iwctl_siwessid(dev, NULL,
3454                                             &(wrq->u.essid), essid);
3455                 }
3456                 break;
3457
3458
3459                 // Get current network name (ESSID)
3460         case SIOCGIWESSID:
3461
3462                 {
3463                         char essid[IW_ESSID_MAX_SIZE+1];
3464                         if (wrq->u.essid.pointer)
3465                                 rc = iwctl_giwessid(dev, NULL,
3466                                                     &(wrq->u.essid), essid);
3467                                 if (copy_to_user(wrq->u.essid.pointer,
3468                                                          essid,
3469                                                          wrq->u.essid.length) )
3470                                         rc = -EFAULT;
3471                 }
3472                 break;
3473
3474         case SIOCSIWAP:
3475
3476                 rc = iwctl_siwap(dev, NULL, &(wrq->u.ap_addr), NULL);
3477                 break;
3478
3479
3480                 // Get current Access Point (BSSID)
3481         case SIOCGIWAP:
3482                 rc = iwctl_giwap(dev, NULL, &(wrq->u.ap_addr), NULL);
3483                 break;
3484
3485
3486                 // Set desired station name
3487         case SIOCSIWNICKN:
3488         DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCSIWNICKN \n");
3489         rc = -EOPNOTSUPP;
3490                 break;
3491
3492                 // Get current station name
3493         case SIOCGIWNICKN:
3494         DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCGIWNICKN \n");
3495         rc = -EOPNOTSUPP;
3496                 break;
3497
3498                 // Set the desired bit-rate
3499         case SIOCSIWRATE:
3500                 rc = iwctl_siwrate(dev, NULL, &(wrq->u.bitrate), NULL);
3501                 break;
3502
3503         // Get the current bit-rate
3504         case SIOCGIWRATE:
3505
3506                 rc = iwctl_giwrate(dev, NULL, &(wrq->u.bitrate), NULL);
3507                 break;
3508
3509         // Set the desired RTS threshold
3510         case SIOCSIWRTS:
3511
3512                 rc = iwctl_siwrts(dev, NULL, &(wrq->u.rts), NULL);
3513                 break;
3514
3515         // Get the current RTS threshold
3516         case SIOCGIWRTS:
3517
3518                 rc = iwctl_giwrts(dev, NULL, &(wrq->u.rts), NULL);
3519                 break;
3520
3521                 // Set the desired fragmentation threshold
3522         case SIOCSIWFRAG:
3523
3524                 rc = iwctl_siwfrag(dev, NULL, &(wrq->u.frag), NULL);
3525             break;
3526
3527         // Get the current fragmentation threshold
3528         case SIOCGIWFRAG:
3529
3530                 rc = iwctl_giwfrag(dev, NULL, &(wrq->u.frag), NULL);
3531                 break;
3532
3533                 // Set mode of operation
3534         case SIOCSIWMODE:
3535         rc = iwctl_siwmode(dev, NULL, &(wrq->u.mode), NULL);
3536                 break;
3537
3538                 // Get mode of operation
3539         case SIOCGIWMODE:
3540                 rc = iwctl_giwmode(dev, NULL, &(wrq->u.mode), NULL);
3541                 break;
3542
3543                 // Set WEP keys and mode
3544         case SIOCSIWENCODE:
3545                 {
3546             char abyKey[WLAN_WEP232_KEYLEN];
3547
3548                         if (wrq->u.encoding.pointer) {
3549
3550
3551                                 if (wrq->u.encoding.length > WLAN_WEP232_KEYLEN) {
3552                                         rc = -E2BIG;
3553                                         break;
3554                                 }
3555                                 memset(abyKey, 0, WLAN_WEP232_KEYLEN);
3556                                 if (copy_from_user(abyKey,
3557                                                   wrq->u.encoding.pointer,
3558                                                   wrq->u.encoding.length)) {
3559                                         rc = -EFAULT;
3560                                         break;
3561                                 }
3562                         } else if (wrq->u.encoding.length != 0) {
3563                                 rc = -EINVAL;
3564                                 break;
3565                         }
3566                         rc = iwctl_siwencode(dev, NULL, &(wrq->u.encoding), abyKey);
3567                 }
3568                 break;
3569
3570                 // Get the WEP keys and mode
3571         case SIOCGIWENCODE:
3572
3573                 if (!capable(CAP_NET_ADMIN)) {
3574                         rc = -EPERM;
3575                         break;
3576                 }
3577                 {
3578                     char abyKey[WLAN_WEP232_KEYLEN];
3579
3580                     rc = iwctl_giwencode(dev, NULL, &(wrq->u.encoding), abyKey);
3581                     if (rc != 0) break;
3582                         if (wrq->u.encoding.pointer) {
3583                                 if (copy_to_user(wrq->u.encoding.pointer,
3584                                                         abyKey,
3585                                                         wrq->u.encoding.length))
3586                                         rc = -EFAULT;
3587                         }
3588                 }
3589                 break;
3590
3591 #if WIRELESS_EXT > 9
3592                 // Get the current Tx-Power
3593         case SIOCGIWTXPOW:
3594         DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCGIWTXPOW \n");
3595         rc = -EOPNOTSUPP;
3596                 break;
3597
3598         case SIOCSIWTXPOW:
3599         DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCGIWTXPOW \n");
3600         rc = -EOPNOTSUPP;
3601                 break;
3602
3603 #endif // WIRELESS_EXT > 9
3604
3605 #if WIRELESS_EXT > 10
3606         case SIOCSIWRETRY:
3607
3608                 rc = iwctl_siwretry(dev, NULL, &(wrq->u.retry), NULL);
3609                 break;
3610
3611         case SIOCGIWRETRY:
3612
3613                 rc = iwctl_giwretry(dev, NULL, &(wrq->u.retry), NULL);
3614                 break;
3615
3616 #endif // WIRELESS_EXT > 10
3617
3618                 // Get range of parameters
3619         case SIOCGIWRANGE:
3620
3621                 {
3622                         struct iw_range range;
3623
3624                         rc = iwctl_giwrange(dev, NULL, &(wrq->u.data), (char *) &range);
3625                         if (copy_to_user(wrq->u.data.pointer, &range, sizeof(struct iw_range)))
3626                                 rc = -EFAULT;
3627                 }
3628
3629                 break;
3630
3631         case SIOCGIWPOWER:
3632
3633                 rc = iwctl_giwpower(dev, NULL, &(wrq->u.power), NULL);
3634                 break;
3635
3636
3637         case SIOCSIWPOWER:
3638
3639                 rc = iwctl_siwpower(dev, NULL, &(wrq->u.power), NULL);
3640                 break;
3641
3642
3643         case SIOCGIWSENS:
3644
3645             rc = iwctl_giwsens(dev, NULL, &(wrq->u.sens), NULL);
3646                 break;
3647
3648         case SIOCSIWSENS:
3649         DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCSIWSENS \n");
3650                 rc = -EOPNOTSUPP;
3651                 break;
3652
3653         case SIOCGIWAPLIST:
3654             {
3655             char buffer[IW_MAX_AP * (sizeof(struct sockaddr) + sizeof(struct iw_quality))];
3656
3657                     if (wrq->u.data.pointer) {
3658                         rc = iwctl_giwaplist(dev, NULL, &(wrq->u.data), buffer);
3659                         if (rc == 0) {
3660                     if (copy_to_user(wrq->u.data.pointer,
3661                                                         buffer,
3662                                                        (wrq->u.data.length * (sizeof(struct sockaddr) +  sizeof(struct iw_quality)))
3663                                         ))
3664                                     rc = -EFAULT;
3665                         }
3666             }
3667         }
3668                 break;
3669
3670
3671 #ifdef WIRELESS_SPY
3672                 // Set the spy list
3673         case SIOCSIWSPY:
3674
3675         DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCSIWSPY \n");
3676                 rc = -EOPNOTSUPP;
3677                 break;
3678
3679                 // Get the spy list
3680         case SIOCGIWSPY:
3681
3682         DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCSIWSPY \n");
3683                 rc = -EOPNOTSUPP;
3684                 break;
3685
3686 #endif // WIRELESS_SPY
3687
3688         case SIOCGIWPRIV:
3689         DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCGIWPRIV \n");
3690                 rc = -EOPNOTSUPP;
3691 /*
3692                 if(wrq->u.data.pointer) {
3693                         wrq->u.data.length = sizeof(iwctl_private_args) / sizeof( iwctl_private_args[0]);
3694
3695                         if(copy_to_user(wrq->u.data.pointer,
3696                                         (u_char *) iwctl_private_args,
3697                                         sizeof(iwctl_private_args)))
3698                                 rc = -EFAULT;
3699                 }
3700 */
3701                 break;
3702
3703
3704 //#endif // WIRELESS_EXT < 13
3705 //2008-0409-07, <Add> by Einsn Liu
3706 #ifdef  WPA_SUPPLICANT_DRIVER_WEXT_SUPPORT
3707         case SIOCSIWAUTH:
3708                 DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCSIWAUTH \n");
3709                 rc = iwctl_siwauth(dev, NULL, &(wrq->u.param), NULL);
3710                 break;
3711
3712         case SIOCGIWAUTH:
3713                 DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCGIWAUTH \n");
3714                 rc = iwctl_giwauth(dev, NULL, &(wrq->u.param), NULL);
3715                 break;
3716
3717         case SIOCSIWGENIE:
3718                 DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCSIWGENIE \n");
3719                 rc = iwctl_siwgenie(dev, NULL, &(wrq->u.data), wrq->u.data.pointer);
3720                 break;
3721
3722         case SIOCGIWGENIE:
3723                 DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCGIWGENIE \n");
3724                 rc = iwctl_giwgenie(dev, NULL, &(wrq->u.data), wrq->u.data.pointer);
3725                 break;
3726
3727         case SIOCSIWENCODEEXT:
3728                 {
3729                         char extra[sizeof(struct iw_encode_ext)+MAX_KEY_LEN+1];
3730                         DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCSIWENCODEEXT \n");
3731                         if(wrq->u.encoding.pointer){
3732                                 memset(extra, 0, sizeof(struct iw_encode_ext)+MAX_KEY_LEN+1);
3733                                 if(wrq->u.encoding.length > (sizeof(struct iw_encode_ext)+ MAX_KEY_LEN)){
3734                                         rc = -E2BIG;
3735                                         break;
3736                                 }
3737                                 if(copy_from_user(extra, wrq->u.encoding.pointer,wrq->u.encoding.length)){
3738                                         rc = -EFAULT;
3739                                         break;
3740                                 }
3741                         }else if(wrq->u.encoding.length != 0){
3742                                 rc = -EINVAL;
3743                                 break;
3744                         }
3745                         rc = iwctl_siwencodeext(dev, NULL, &(wrq->u.encoding), extra);
3746                 }
3747                 break;
3748
3749         case SIOCGIWENCODEEXT:
3750                 DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCGIWENCODEEXT \n");
3751                 rc = iwctl_giwencodeext(dev, NULL, &(wrq->u.encoding), NULL);
3752                 break;
3753
3754         case SIOCSIWMLME:
3755                 DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCSIWMLME \n");
3756                 rc = iwctl_siwmlme(dev, NULL, &(wrq->u.data), wrq->u.data.pointer);
3757                 break;
3758
3759 #endif // #ifdef WPA_SUPPLICANT_DRIVER_WEXT_SUPPORT
3760 //End Add -- //2008-0409-07, <Add> by Einsn Liu
3761
3762 #endif // WIRELESS_EXT
3763
3764     case IOCTL_CMD_TEST:
3765
3766                 if (!(pDevice->flags & DEVICE_FLAGS_OPENED)) {
3767                     rc = -EFAULT;
3768                     break;
3769                 } else {
3770                     rc = 0;
3771                 }
3772         pReq = (PSCmdRequest)rq;
3773         pReq->wResult = MAGIC_CODE;
3774         break;
3775
3776     case IOCTL_CMD_SET:
3777
3778                #ifdef SndEvt_ToAPI
3779                   if((((PSCmdRequest)rq)->wCmdCode !=WLAN_CMD_SET_EVT) &&
3780                        !(pDevice->flags & DEVICE_FLAGS_OPENED))
3781               #else
3782                 if (!(pDevice->flags & DEVICE_FLAGS_OPENED) &&
3783                        (((PSCmdRequest)rq)->wCmdCode !=WLAN_CMD_SET_WPA))
3784               #endif
3785                 {
3786                     rc = -EFAULT;
3787                     break;
3788                 } else {
3789                     rc = 0;
3790                 }
3791
3792             if (test_and_set_bit( 0, (void*)&(pMgmt->uCmdBusy))) {
3793                     return -EBUSY;
3794             }
3795         rc = private_ioctl(pDevice, rq);
3796         clear_bit( 0, (void*)&(pMgmt->uCmdBusy));
3797         break;
3798
3799     case IOCTL_CMD_HOSTAPD:
3800
3801
3802 #if WIRELESS_EXT > 8
3803                 rc = hostap_ioctl(pDevice, &wrq->u.data);
3804 #else // WIRELESS_EXT > 8
3805                 rc = hostap_ioctl(pDevice, (struct iw_point *) &wrq->u.data);
3806 #endif // WIRELESS_EXT > 8
3807         break;
3808
3809     case IOCTL_CMD_WPA:
3810
3811 #if WIRELESS_EXT > 8
3812                 rc = wpa_ioctl(pDevice, &wrq->u.data);
3813 #else // WIRELESS_EXT > 8
3814                 rc = wpa_ioctl(pDevice, (struct iw_point *) &wrq->u.data);
3815 #endif // WIRELESS_EXT > 8
3816         break;
3817
3818         case SIOCETHTOOL:
3819         return ethtool_ioctl(dev, (void *) rq->ifr_data);
3820         // All other calls are currently unsupported
3821
3822         default:
3823                 rc = -EOPNOTSUPP;
3824         DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Ioctl command not support..%x\n", cmd);
3825
3826
3827     }
3828
3829     if (pDevice->bCommit) {
3830        if (pMgmt->eConfigMode == WMAC_CONFIG_AP) {
3831            netif_stop_queue(pDevice->dev);
3832            spin_lock_irq(&pDevice->lock);
3833            bScheduleCommand((HANDLE)pDevice, WLAN_CMD_RUN_AP, NULL);
3834            spin_unlock_irq(&pDevice->lock);
3835        }
3836        else {
3837            DEVICE_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Commit the settings\n");
3838            spin_lock_irq(&pDevice->lock);
3839            pDevice->bLinkPass = FALSE;
3840            memset(pMgmt->abyCurrBSSID, 0, 6);
3841            pMgmt->eCurrState = WMAC_STATE_IDLE;
3842            netif_stop_queue(pDevice->dev);
3843         #ifdef WPA_SUPPLICANT_DRIVER_WEXT_SUPPORT
3844               pMgmt->eScanType = WMAC_SCAN_ACTIVE;
3845          if(pDevice->bWPASuppWextEnabled !=TRUE)
3846          #endif
3847            bScheduleCommand((HANDLE) pDevice, WLAN_CMD_BSSID_SCAN, pMgmt->abyDesireSSID);
3848            bScheduleCommand((HANDLE) pDevice, WLAN_CMD_SSID, NULL);
3849            spin_unlock_irq(&pDevice->lock);
3850       }
3851       pDevice->bCommit = FALSE;
3852     }
3853
3854     return rc;
3855 }
3856
3857
3858 static int ethtool_ioctl(struct net_device *dev, void *useraddr)
3859 {
3860         u32 ethcmd;
3861
3862         if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
3863                 return -EFAULT;
3864
3865         switch (ethcmd) {
3866         case ETHTOOL_GDRVINFO: {
3867                 struct ethtool_drvinfo info = {ETHTOOL_GDRVINFO};
3868                 strncpy(info.driver, DEVICE_NAME, sizeof(info.driver)-1);
3869                 strncpy(info.version, DEVICE_VERSION, sizeof(info.version)-1);
3870                 if (copy_to_user(useraddr, &info, sizeof(info)))
3871                         return -EFAULT;
3872                 return 0;
3873         }
3874
3875         }
3876
3877         return -EOPNOTSUPP;
3878 }
3879
3880 /*------------------------------------------------------------------*/
3881 #ifndef PRIVATE_OBJ
3882
3883 MODULE_DEVICE_TABLE(pci, device_id_table);
3884
3885 static struct pci_driver device_driver = {
3886         name:       DEVICE_NAME,
3887         id_table:   device_id_table,
3888         probe:      device_found1,
3889         remove:     device_remove1,
3890 #ifdef CONFIG_PM
3891         suspend:    viawget_suspend,
3892         resume:     viawget_resume,
3893 #endif
3894 };
3895
3896 static int __init device_init_module(void)
3897 {
3898     int ret;
3899
3900
3901 //    ret=pci_module_init(&device_driver);
3902         //ret = pcie_port_service_register(&device_driver);
3903         ret = pci_register_driver(&device_driver);
3904 #ifdef CONFIG_PM
3905     if(ret >= 0)
3906         register_reboot_notifier(&device_notifier);
3907 #endif
3908
3909     return ret;
3910 }
3911
3912 static void __exit device_cleanup_module(void)
3913 {
3914
3915
3916 #ifdef CONFIG_PM
3917     unregister_reboot_notifier(&device_notifier);
3918 #endif
3919     pci_unregister_driver(&device_driver);
3920
3921 }
3922
3923 module_init(device_init_module);
3924 module_exit(device_cleanup_module);
3925
3926
3927 #ifdef CONFIG_PM
3928 static int
3929 device_notify_reboot(struct notifier_block *nb, unsigned long event, void *p)
3930 {
3931     struct pci_dev *pdev = NULL;
3932     switch(event) {
3933     case SYS_DOWN:
3934     case SYS_HALT:
3935     case SYS_POWER_OFF:
3936         while ((pdev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, pdev)) != NULL) {
3937             if(pci_dev_driver(pdev) == &device_driver) {
3938                 if (pci_get_drvdata(pdev))
3939                     viawget_suspend(pdev, 3);
3940             }
3941         }
3942     }
3943     return NOTIFY_DONE;
3944 }
3945
3946 static int
3947 viawget_suspend(struct pci_dev *pcid, u32 state)
3948 {
3949     int power_status;   // to silence the compiler
3950
3951     PSDevice pDevice=pci_get_drvdata(pcid);
3952     PSMgmtObject  pMgmt = pDevice->pMgmt;
3953
3954     netif_stop_queue(pDevice->dev);
3955     spin_lock_irq(&pDevice->lock);
3956     pci_save_state(pcid);
3957     del_timer(&pDevice->sTimerCommand);
3958     del_timer(&pMgmt->sTimerSecondCallback);
3959     pDevice->cbFreeCmdQueue = CMD_Q_SIZE;
3960     pDevice->uCmdDequeueIdx = 0;
3961     pDevice->uCmdEnqueueIdx = 0;
3962     pDevice->bCmdRunning = FALSE;
3963     MACbShutdown(pDevice->PortOffset);
3964     MACvSaveContext(pDevice->PortOffset, pDevice->abyMacContext);
3965     pDevice->bLinkPass = FALSE;
3966     memset(pMgmt->abyCurrBSSID, 0, 6);
3967     pMgmt->eCurrState = WMAC_STATE_IDLE;
3968     pci_disable_device(pcid);
3969     power_status = pci_set_power_state(pcid, state);
3970     spin_unlock_irq(&pDevice->lock);
3971     return 0;
3972 }
3973
3974 static int
3975 viawget_resume(struct pci_dev *pcid)
3976 {
3977     PSDevice  pDevice=pci_get_drvdata(pcid);
3978     PSMgmtObject  pMgmt = pDevice->pMgmt;
3979     int power_status;   // to silence the compiler
3980
3981
3982     power_status = pci_set_power_state(pcid, 0);
3983     power_status = pci_enable_wake(pcid, 0, 0);
3984     pci_restore_state(pcid);
3985     if (netif_running(pDevice->dev)) {
3986         spin_lock_irq(&pDevice->lock);
3987         MACvRestoreContext(pDevice->PortOffset, pDevice->abyMacContext);
3988         device_init_registers(pDevice, DEVICE_INIT_DXPL);
3989         if (pMgmt->sNodeDBTable[0].bActive == TRUE) { // Assoc with BSS
3990             pMgmt->sNodeDBTable[0].bActive = FALSE;
3991             pDevice->bLinkPass = FALSE;
3992             if(pMgmt->eCurrMode == WMAC_MODE_IBSS_STA) {
3993                 // In Adhoc, BSS state set back to started.
3994                 pMgmt->eCurrState = WMAC_STATE_STARTED;
3995            }
3996             else {
3997                 pMgmt->eCurrMode = WMAC_MODE_STANDBY;
3998                 pMgmt->eCurrState = WMAC_STATE_IDLE;
3999             }
4000         }
4001         init_timer(&pMgmt->sTimerSecondCallback);
4002         init_timer(&pDevice->sTimerCommand);
4003         MACvIntEnable(pDevice->PortOffset, IMR_MASK_VALUE);
4004         BSSvClearBSSList((HANDLE)pDevice, pDevice->bLinkPass);
4005         bScheduleCommand((HANDLE) pDevice, WLAN_CMD_BSSID_SCAN, NULL);
4006         bScheduleCommand((HANDLE) pDevice, WLAN_CMD_SSID, NULL);
4007         spin_unlock_irq(&pDevice->lock);
4008     }
4009     return 0;
4010 }
4011
4012 #endif
4013
4014 #endif //#ifndef PRIVATE_OBJ
4015
4016 #ifdef PRIVATE_OBJ
4017
4018
4019 int __device_hw_reset(HANDLE pExDevice){
4020      PSDevice_info pDevice_info = (PSDevice_info)pExDevice;
4021
4022      return MACbSoftwareReset(pDevice_info->port_offset);
4023 }
4024
4025
4026 int __device_hw_init(HANDLE pExDevice){
4027     PSDevice_info pDevice_info = (PSDevice_info)pExDevice;
4028     PSDevice    pDevice;
4029
4030     pDevice = (PSDevice)kmalloc(sizeof(DEVICE_INFO), (int)GFP_ATOMIC);
4031     if (pDevice == NULL)
4032         return FALSE;
4033
4034     memset(pDevice, 0, sizeof(DEVICE_INFO));
4035     pDevice_info->pWDevice = pDevice;
4036     pDevice->PortOffset = pDevice_info->port_offset;
4037     pDevice->dev = pDevice_info->dev;
4038     pDevice->pcid = pDevice_info->pcid;
4039     pDevice->chip_id = pDevice_info->chip_id;
4040     pDevice->memaddr = pDevice_info->mem_addr;
4041     pDevice->ioaddr = pDevice_info->io_addr;
4042     pDevice->io_size = pDevice_info->io_size;
4043     pDevice->nTxQueues = pDevice_info->nTxQueues;
4044     pDevice->multicast_limit = pDevice_info->multicast_limit;
4045     pDevice->sMgmtObj.pAdapter = (PVOID)pDevice;
4046     pDevice->pMgmt = &(pDevice->sMgmtObj);
4047     MACvInitialize(pDevice->PortOffset);
4048     device_get_options(pDevice, 0 , pDevice_info->dev->name);
4049     device_set_options(pDevice);
4050     pDevice->sOpts.flags &= pDevice_info->flags;
4051     pDevice->flags = pDevice->sOpts.flags | (pDevice_info->flags & 0xFF000000UL);
4052     spin_lock_init(&(pDevice->lock));
4053
4054     return TRUE;
4055 }
4056
4057
4058 void __device_read_mac(HANDLE pExDevice, PBYTE dev_addr){
4059     PSDevice_info pDevice_info = (PSDevice_info)pExDevice;
4060     PSDevice    pDevice = (PSDevice)(pDevice_info->pWDevice);
4061
4062     MACvReadEtherAddress(pDevice->PortOffset, dev_addr);
4063     return;
4064 }
4065
4066
4067 #endif
4068
4069