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