[go: nahoru, domu]

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