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