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