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