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