• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
3  * All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * File: device_main.c
20  *
21  * Purpose: driver entry for initial, open, close, tx and rx.
22  *
23  * Author: Lyndon Chen
24  *
25  * Date: Jan 8, 2003
26  *
27  * Functions:
28  *
29  *   vt6655_probe - module initial (insmod) driver entry
30  *   vt6655_remove - module remove entry
31  *   vt6655_init_info - device structure resource allocation function
32  *   device_free_info - device structure resource free function
33  *   device_get_pci_info - get allocated pci io/mem resource
34  *   device_print_info - print out resource
35  *   device_open - allocate dma/descripter resource & initial mac/bbp function
36  *   device_xmit - asynchrous data tx function
37  *   device_intr - interrupt handle function
38  *   device_set_multi - set mac filter
39  *   device_ioctl - ioctl entry
40  *   device_close - shutdown mac/bbp & free dma/descripter resource
41  *   device_rx_srv - rx service function
42  *   device_receive_frame - rx data function
43  *   device_alloc_rx_buf - rx buffer pre-allocated function
44  *   device_alloc_frag_buf - rx fragement pre-allocated function
45  *   device_free_tx_buf - free tx buffer function
46  *   device_free_frag_buf- free de-fragement buffer
47  *   device_dma0_tx_80211- tx 802.11 frame via dma0
48  *   device_dma0_xmit- tx PS bufferred frame via dma0
49  *   device_init_rd0_ring- initial rd dma0 ring
50  *   device_init_rd1_ring- initial rd dma1 ring
51  *   device_init_td0_ring- initial tx dma0 ring buffer
52  *   device_init_td1_ring- initial tx dma1 ring buffer
53  *   device_init_registers- initial MAC & BBP & RF internal registers.
54  *   device_init_rings- initial tx/rx ring buffer
55  *   device_init_defrag_cb- initial & allocate de-fragement buffer.
56  *   device_free_rings- free all allocated ring buffer
57  *   device_tx_srv- tx interrupt service function
58  *
59  * Revision History:
60  */
61 #undef __NO_VERSION__
62 
63 #include <linux/file.h>
64 #include "device.h"
65 #include "card.h"
66 #include "channel.h"
67 #include "baseband.h"
68 #include "mac.h"
69 #include "tether.h"
70 #include "wmgr.h"
71 #include "wctl.h"
72 #include "power.h"
73 #include "wcmd.h"
74 #include "iocmd.h"
75 #include "tcrc.h"
76 #include "rxtx.h"
77 #include "wroute.h"
78 #include "bssdb.h"
79 #include "hostap.h"
80 #include "wpactl.h"
81 #include "ioctl.h"
82 #include "iwctl.h"
83 #include "dpc.h"
84 #include "datarate.h"
85 #include "rf.h"
86 #include "iowpa.h"
87 #include <linux/delay.h>
88 #include <linux/kthread.h>
89 #include <linux/slab.h>
90 
91 /*---------------------  Static Definitions -------------------------*/
92 //
93 // Define module options
94 //
95 MODULE_AUTHOR("VIA Networking Technologies, Inc., <lyndonchen@vntek.com.tw>");
96 MODULE_LICENSE("GPL");
97 MODULE_DESCRIPTION("VIA Networking Solomon-A/B/G Wireless LAN Adapter Driver");
98 
99 #define DEVICE_PARAM(N, D)
100 
101 #define RX_DESC_MIN0     16
102 #define RX_DESC_MAX0     128
103 #define RX_DESC_DEF0     32
104 DEVICE_PARAM(RxDescriptors0, "Number of receive descriptors0");
105 
106 #define RX_DESC_MIN1     16
107 #define RX_DESC_MAX1     128
108 #define RX_DESC_DEF1     32
109 DEVICE_PARAM(RxDescriptors1, "Number of receive descriptors1");
110 
111 #define TX_DESC_MIN0     16
112 #define TX_DESC_MAX0     128
113 #define TX_DESC_DEF0     32
114 DEVICE_PARAM(TxDescriptors0, "Number of transmit descriptors0");
115 
116 #define TX_DESC_MIN1     16
117 #define TX_DESC_MAX1     128
118 #define TX_DESC_DEF1     64
119 DEVICE_PARAM(TxDescriptors1, "Number of transmit descriptors1");
120 
121 #define IP_ALIG_DEF     0
122 /* IP_byte_align[] is used for IP header unsigned long byte aligned
123    0: indicate the IP header won't be unsigned long byte aligned.(Default) .
124    1: indicate the IP header will be unsigned long byte aligned.
125    In some environment, the IP header should be unsigned long byte aligned,
126    or the packet will be droped when we receive it. (eg: IPVS)
127 */
128 DEVICE_PARAM(IP_byte_align, "Enable IP header dword aligned");
129 
130 #define INT_WORKS_DEF   20
131 #define INT_WORKS_MIN   10
132 #define INT_WORKS_MAX   64
133 
134 DEVICE_PARAM(int_works, "Number of packets per interrupt services");
135 
136 #define CHANNEL_MIN     1
137 #define CHANNEL_MAX     14
138 #define CHANNEL_DEF     6
139 
140 DEVICE_PARAM(Channel, "Channel number");
141 
142 /* PreambleType[] is the preamble length used for transmit.
143    0: indicate allows long preamble type
144    1: indicate allows short preamble type
145 */
146 
147 #define PREAMBLE_TYPE_DEF     1
148 
149 DEVICE_PARAM(PreambleType, "Preamble Type");
150 
151 #define RTS_THRESH_MIN     512
152 #define RTS_THRESH_MAX     2347
153 #define RTS_THRESH_DEF     2347
154 
155 DEVICE_PARAM(RTSThreshold, "RTS threshold");
156 
157 #define FRAG_THRESH_MIN     256
158 #define FRAG_THRESH_MAX     2346
159 #define FRAG_THRESH_DEF     2346
160 
161 DEVICE_PARAM(FragThreshold, "Fragmentation threshold");
162 
163 #define DATA_RATE_MIN     0
164 #define DATA_RATE_MAX     13
165 #define DATA_RATE_DEF     13
166 /* datarate[] index
167    0: indicate 1 Mbps   0x02
168    1: indicate 2 Mbps   0x04
169    2: indicate 5.5 Mbps 0x0B
170    3: indicate 11 Mbps  0x16
171    4: indicate 6 Mbps   0x0c
172    5: indicate 9 Mbps   0x12
173    6: indicate 12 Mbps  0x18
174    7: indicate 18 Mbps  0x24
175    8: indicate 24 Mbps  0x30
176    9: indicate 36 Mbps  0x48
177    10: indicate 48 Mbps  0x60
178    11: indicate 54 Mbps  0x6c
179    12: indicate 72 Mbps  0x90
180    13: indicate auto rate
181 */
182 
183 DEVICE_PARAM(ConnectionRate, "Connection data rate");
184 
185 #define OP_MODE_DEF     0
186 
187 DEVICE_PARAM(OPMode, "Infrastruct, adhoc, AP mode ");
188 
189 /* OpMode[] is used for transmit.
190    0: indicate infrastruct mode used
191    1: indicate adhoc mode used
192    2: indicate AP mode used
193 */
194 
195 /* PSMode[]
196    0: indicate disable power saving mode
197    1: indicate enable power saving mode
198 */
199 
200 #define PS_MODE_DEF     0
201 
202 DEVICE_PARAM(PSMode, "Power saving mode");
203 
204 #define SHORT_RETRY_MIN     0
205 #define SHORT_RETRY_MAX     31
206 #define SHORT_RETRY_DEF     8
207 
208 DEVICE_PARAM(ShortRetryLimit, "Short frame retry limits");
209 
210 #define LONG_RETRY_MIN     0
211 #define LONG_RETRY_MAX     15
212 #define LONG_RETRY_DEF     4
213 
214 DEVICE_PARAM(LongRetryLimit, "long frame retry limits");
215 
216 /* BasebandType[] baseband type selected
217    0: indicate 802.11a type
218    1: indicate 802.11b type
219    2: indicate 802.11g type
220 */
221 #define BBP_TYPE_MIN     0
222 #define BBP_TYPE_MAX     2
223 #define BBP_TYPE_DEF     2
224 
225 DEVICE_PARAM(BasebandType, "baseband type");
226 
227 /* 80211hEnable[]
228    0: indicate disable 802.11h
229    1: indicate enable 802.11h
230 */
231 
232 #define X80211h_MODE_DEF     0
233 
234 DEVICE_PARAM(b80211hEnable, "802.11h mode");
235 
236 /* 80211hEnable[]
237    0: indicate disable 802.11h
238    1: indicate enable 802.11h
239 */
240 
241 #define DIVERSITY_ANT_DEF     0
242 
243 DEVICE_PARAM(bDiversityANTEnable, "ANT diversity mode");
244 
245 //
246 // Static vars definitions
247 //
248 static CHIP_INFO chip_info_table[] = {
249 	{ VT3253,       "VIA Networking Solomon-A/B/G Wireless LAN Adapter ",
250 	  256, 1,     DEVICE_FLAGS_IP_ALIGN|DEVICE_FLAGS_TX_ALIGN },
251 	{0, NULL}
252 };
253 
254 static const struct pci_device_id vt6655_pci_id_table[] = {
255 	{ PCI_VDEVICE(VIA, 0x3253), (kernel_ulong_t)chip_info_table},
256 	{ 0, }
257 };
258 
259 /*---------------------  Static Functions  --------------------------*/
260 
261 static int  vt6655_probe(struct pci_dev *pcid, const struct pci_device_id *ent);
262 static void vt6655_init_info(struct pci_dev *pcid,
263 			     struct vnt_private **ppDevice, PCHIP_INFO);
264 static void device_free_info(struct vnt_private *pDevice);
265 static bool device_get_pci_info(struct vnt_private *, struct pci_dev *pcid);
266 static void device_print_info(struct vnt_private *pDevice);
267 static void device_init_diversity_timer(struct vnt_private *pDevice);
268 static int  device_open(struct net_device *dev);
269 static int  device_xmit(struct sk_buff *skb, struct net_device *dev);
270 static  irqreturn_t  device_intr(int irq,  void *dev_instance);
271 static void device_set_multi(struct net_device *dev);
272 static int  device_close(struct net_device *dev);
273 static int  device_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
274 
275 #ifdef CONFIG_PM
276 static int device_notify_reboot(struct notifier_block *, unsigned long event, void *ptr);
277 static int viawget_suspend(struct pci_dev *pcid, pm_message_t state);
278 static int viawget_resume(struct pci_dev *pcid);
279 static struct notifier_block device_notifier = {
280 	.notifier_call = device_notify_reboot,
281 	.next = NULL,
282 	.priority = 0,
283 };
284 #endif
285 
286 static void device_init_rd0_ring(struct vnt_private *pDevice);
287 static void device_init_rd1_ring(struct vnt_private *pDevice);
288 static void device_init_defrag_cb(struct vnt_private *pDevice);
289 static void device_init_td0_ring(struct vnt_private *pDevice);
290 static void device_init_td1_ring(struct vnt_private *pDevice);
291 
292 static int  device_dma0_tx_80211(struct sk_buff *skb, struct net_device *dev);
293 //2008-0714<Add>by Mike Liu
294 static bool device_release_WPADEV(struct vnt_private *pDevice);
295 
296 static int  ethtool_ioctl(struct net_device *dev, void __user *useraddr);
297 static int  device_rx_srv(struct vnt_private *pDevice, unsigned int uIdx);
298 static int  device_tx_srv(struct vnt_private *pDevice, unsigned int uIdx);
299 static bool device_alloc_rx_buf(struct vnt_private *pDevice, PSRxDesc pDesc);
300 static void device_init_registers(struct vnt_private *pDevice);
301 static void device_free_tx_buf(struct vnt_private *pDevice, PSTxDesc pDesc);
302 static void device_free_td0_ring(struct vnt_private *pDevice);
303 static void device_free_td1_ring(struct vnt_private *pDevice);
304 static void device_free_rd0_ring(struct vnt_private *pDevice);
305 static void device_free_rd1_ring(struct vnt_private *pDevice);
306 static void device_free_rings(struct vnt_private *pDevice);
307 static void device_free_frag_buf(struct vnt_private *pDevice);
308 static int Config_FileGetParameter(unsigned char *string,
309 				   unsigned char *dest, unsigned char *source);
310 
311 /*---------------------  Export Variables  --------------------------*/
312 
313 /*---------------------  Export Functions  --------------------------*/
314 
get_chip_name(int chip_id)315 static char *get_chip_name(int chip_id)
316 {
317 	int i;
318 
319 	for (i = 0; chip_info_table[i].name != NULL; i++)
320 		if (chip_info_table[i].chip_id == chip_id)
321 			break;
322 	return chip_info_table[i].name;
323 }
324 
vt6655_remove(struct pci_dev * pcid)325 static void vt6655_remove(struct pci_dev *pcid)
326 {
327 	struct vnt_private *pDevice = pci_get_drvdata(pcid);
328 
329 	if (pDevice == NULL)
330 		return;
331 	device_free_info(pDevice);
332 }
333 
device_get_options(struct vnt_private * pDevice)334 static void device_get_options(struct vnt_private *pDevice)
335 {
336 	POPTIONS pOpts = &(pDevice->sOpts);
337 
338 	pOpts->nRxDescs0 = RX_DESC_DEF0;
339 	pOpts->nRxDescs1 = RX_DESC_DEF1;
340 	pOpts->nTxDescs[0] = TX_DESC_DEF0;
341 	pOpts->nTxDescs[1] = TX_DESC_DEF1;
342 	pOpts->flags |= DEVICE_FLAGS_IP_ALIGN;
343 	pOpts->int_works = INT_WORKS_DEF;
344 	pOpts->rts_thresh = RTS_THRESH_DEF;
345 	pOpts->frag_thresh = FRAG_THRESH_DEF;
346 	pOpts->data_rate = DATA_RATE_DEF;
347 	pOpts->channel_num = CHANNEL_DEF;
348 
349 	pOpts->flags |= DEVICE_FLAGS_PREAMBLE_TYPE;
350 	pOpts->flags |= DEVICE_FLAGS_OP_MODE;
351 	pOpts->short_retry = SHORT_RETRY_DEF;
352 	pOpts->long_retry = LONG_RETRY_DEF;
353 	pOpts->bbp_type = BBP_TYPE_DEF;
354 	pOpts->flags |= DEVICE_FLAGS_80211h_MODE;
355 	pOpts->flags |= DEVICE_FLAGS_DiversityANT;
356 }
357 
358 static void
device_set_options(struct vnt_private * pDevice)359 device_set_options(struct vnt_private *pDevice)
360 {
361 	unsigned char abyBroadcastAddr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
362 	unsigned char abySNAP_RFC1042[ETH_ALEN] = {0xAA, 0xAA, 0x03, 0x00, 0x00, 0x00};
363 	unsigned char abySNAP_Bridgetunnel[ETH_ALEN] = {0xAA, 0xAA, 0x03, 0x00, 0x00, 0xF8};
364 
365 	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 
s_vCompleteCurrentMeasure(struct vnt_private * pDevice,unsigned char byResult)405 static void s_vCompleteCurrentMeasure(struct vnt_private *pDevice,
406 				      unsigned char byResult)
407 {
408 	unsigned int ii;
409 	unsigned long dwDuration = 0;
410 	unsigned char byRPI0 = 0;
411 
412 	for (ii = 1; ii < 8; ii++) {
413 		pDevice->dwRPIs[ii] *= 255;
414 		dwDuration |= *((unsigned short *)(pDevice->pCurrMeasureEID->sReq.abyDuration));
415 		dwDuration <<= 10;
416 		pDevice->dwRPIs[ii] /= dwDuration;
417 		pDevice->abyRPIs[ii] = (unsigned char)pDevice->dwRPIs[ii];
418 		byRPI0 += pDevice->abyRPIs[ii];
419 	}
420 	pDevice->abyRPIs[0] = (0xFF - byRPI0);
421 
422 	if (pDevice->uNumOfMeasureEIDs == 0) {
423 		VNTWIFIbMeasureReport(pDevice->pMgmt,
424 				      true,
425 				      pDevice->pCurrMeasureEID,
426 				      byResult,
427 				      pDevice->byBasicMap,
428 				      pDevice->byCCAFraction,
429 				      pDevice->abyRPIs
430 			);
431 	} else {
432 		VNTWIFIbMeasureReport(pDevice->pMgmt,
433 				      false,
434 				      pDevice->pCurrMeasureEID,
435 				      byResult,
436 				      pDevice->byBasicMap,
437 				      pDevice->byCCAFraction,
438 				      pDevice->abyRPIs
439 			);
440 		CARDbStartMeasure(pDevice, pDevice->pCurrMeasureEID++, pDevice->uNumOfMeasureEIDs);
441 	}
442 }
443 
444 //
445 // Initialisation of MAC & BBP registers
446 //
447 
device_init_registers(struct vnt_private * pDevice)448 static void device_init_registers(struct vnt_private *pDevice)
449 {
450 	unsigned int ii;
451 	unsigned char byValue;
452 	unsigned char byValue1;
453 	unsigned char byCCKPwrdBm = 0;
454 	unsigned char byOFDMPwrdBm = 0;
455 	int zonetype = 0;
456 	PSMgmtObject    pMgmt = &(pDevice->sMgmtObj);
457 
458 	MACbShutdown(pDevice->PortOffset);
459 	BBvSoftwareReset(pDevice->PortOffset);
460 
461 	/* Do MACbSoftwareReset in MACvInitialize */
462 	MACbSoftwareReset(pDevice->PortOffset);
463 
464 	pDevice->bAES = false;
465 
466 	/* Only used in 11g type, sync with ERP IE */
467 	pDevice->bProtectMode = false;
468 
469 	pDevice->bNonERPPresent = false;
470 	pDevice->bBarkerPreambleMd = false;
471 	pDevice->wCurrentRate = RATE_1M;
472 	pDevice->byTopOFDMBasicRate = RATE_24M;
473 	pDevice->byTopCCKBasicRate = RATE_1M;
474 
475 	/* Target to IF pin while programming to RF chip. */
476 	pDevice->byRevId = 0;
477 
478 	/* init MAC */
479 	MACvInitialize(pDevice->PortOffset);
480 
481 	/* Get Local ID */
482 	VNSvInPortB(pDevice->PortOffset + MAC_REG_LOCALID, &pDevice->byLocalID);
483 
484 	spin_lock_irq(&pDevice->lock);
485 
486 	SROMvReadAllContents(pDevice->PortOffset, pDevice->abyEEPROM);
487 
488 	spin_unlock_irq(&pDevice->lock);
489 
490 	/* Get Channel range */
491 	pDevice->byMinChannel = 1;
492 	pDevice->byMaxChannel = CB_MAX_CHANNEL;
493 
494 	/* Get Antena */
495 	byValue = SROMbyReadEmbedded(pDevice->PortOffset, EEP_OFS_ANTENNA);
496 	if (byValue & EEP_ANTINV)
497 		pDevice->bTxRxAntInv = true;
498 	else
499 		pDevice->bTxRxAntInv = false;
500 
501 	byValue &= (EEP_ANTENNA_AUX | EEP_ANTENNA_MAIN);
502 	/* if not set default is All */
503 	if (byValue == 0)
504 		byValue = (EEP_ANTENNA_AUX | EEP_ANTENNA_MAIN);
505 
506 	pDevice->ulDiversityNValue = 100*260;
507 	pDevice->ulDiversityMValue = 100*16;
508 	pDevice->byTMax = 1;
509 	pDevice->byTMax2 = 4;
510 	pDevice->ulSQ3TH = 0;
511 	pDevice->byTMax3 = 64;
512 
513 	if (byValue == (EEP_ANTENNA_AUX | EEP_ANTENNA_MAIN)) {
514 		pDevice->byAntennaCount = 2;
515 		pDevice->byTxAntennaMode = ANT_B;
516 		pDevice->dwTxAntennaSel = 1;
517 		pDevice->dwRxAntennaSel = 1;
518 
519 		if (pDevice->bTxRxAntInv)
520 			pDevice->byRxAntennaMode = ANT_A;
521 		else
522 			pDevice->byRxAntennaMode = ANT_B;
523 
524 		byValue1 = SROMbyReadEmbedded(pDevice->PortOffset,
525 					      EEP_OFS_ANTENNA);
526 
527 		if ((byValue1 & 0x08) == 0)
528 			pDevice->bDiversityEnable = false;
529 		else
530 			pDevice->bDiversityEnable = true;
531 	} else  {
532 		pDevice->bDiversityEnable = false;
533 		pDevice->byAntennaCount = 1;
534 		pDevice->dwTxAntennaSel = 0;
535 		pDevice->dwRxAntennaSel = 0;
536 
537 		if (byValue & EEP_ANTENNA_AUX) {
538 			pDevice->byTxAntennaMode = ANT_A;
539 
540 			if (pDevice->bTxRxAntInv)
541 				pDevice->byRxAntennaMode = ANT_B;
542 			else
543 				pDevice->byRxAntennaMode = ANT_A;
544 		} else {
545 			pDevice->byTxAntennaMode = ANT_B;
546 
547 			if (pDevice->bTxRxAntInv)
548 				pDevice->byRxAntennaMode = ANT_A;
549 			else
550 				pDevice->byRxAntennaMode = ANT_B;
551 		}
552 	}
553 
554 	pr_debug("bDiversityEnable=[%d],NValue=[%d],MValue=[%d],TMax=[%d],TMax2=[%d]\n",
555 		 pDevice->bDiversityEnable, (int)pDevice->ulDiversityNValue,
556 		 (int)pDevice->ulDiversityMValue, pDevice->byTMax,
557 		 pDevice->byTMax2);
558 
559 	/* zonetype initial */
560 	pDevice->byOriginalZonetype = pDevice->abyEEPROM[EEP_OFS_ZONETYPE];
561 	zonetype = Config_FileOperation(pDevice, false, NULL);
562 
563 	if (zonetype >= 0) {
564 		if ((zonetype == 0) &&
565 		    (pDevice->abyEEPROM[EEP_OFS_ZONETYPE] != 0x00)) {
566 			/* for USA */
567 			pDevice->abyEEPROM[EEP_OFS_ZONETYPE] = 0;
568 			pDevice->abyEEPROM[EEP_OFS_MAXCHANNEL] = 0x0B;
569 
570 			pr_debug("Init Zone Type :USA\n");
571 		} else if ((zonetype == 1) &&
572 			 (pDevice->abyEEPROM[EEP_OFS_ZONETYPE] != 0x01)) {
573 			/* for Japan */
574 			pDevice->abyEEPROM[EEP_OFS_ZONETYPE] = 0x01;
575 			pDevice->abyEEPROM[EEP_OFS_MAXCHANNEL] = 0x0D;
576 		} else if ((zonetype == 2) &&
577 			  (pDevice->abyEEPROM[EEP_OFS_ZONETYPE] != 0x02)) {
578 			/* for Europe */
579 			pDevice->abyEEPROM[EEP_OFS_ZONETYPE] = 0x02;
580 			pDevice->abyEEPROM[EEP_OFS_MAXCHANNEL] = 0x0D;
581 
582 			pr_debug("Init Zone Type :Europe\n");
583 		} else {
584 			if (zonetype != pDevice->abyEEPROM[EEP_OFS_ZONETYPE])
585 				pr_debug("zonetype in file[%02x] mismatch with in EEPROM[%02x]\n",
586 					 zonetype,
587 					 pDevice->abyEEPROM[EEP_OFS_ZONETYPE]);
588 			else
589 				pr_debug("Read Zonetype file success,use default zonetype setting[%02x]\n",
590 					 zonetype);
591 		}
592 	} else {
593 		pr_debug("Read Zonetype file fail,use default zonetype setting[%02x]\n",
594 			 SROMbyReadEmbedded(pDevice->PortOffset, EEP_OFS_ZONETYPE));
595 	}
596 
597 	/* Get RFType */
598 	pDevice->byRFType = SROMbyReadEmbedded(pDevice->PortOffset, EEP_OFS_RFTYPE);
599 
600 	/* force change RevID for VT3253 emu */
601 	if ((pDevice->byRFType & RF_EMU) != 0)
602 			pDevice->byRevId = 0x80;
603 
604 	pDevice->byRFType &= RF_MASK;
605 	pr_debug("pDevice->byRFType = %x\n", pDevice->byRFType);
606 
607 	if (!pDevice->bZoneRegExist)
608 		pDevice->byZoneType = pDevice->abyEEPROM[EEP_OFS_ZONETYPE];
609 
610 	pr_debug("pDevice->byZoneType = %x\n", pDevice->byZoneType);
611 
612 	/* Init RF module */
613 	RFbInit(pDevice);
614 
615 	/* Get Desire Power Value */
616 	pDevice->byCurPwr = 0xFF;
617 	pDevice->byCCKPwr = SROMbyReadEmbedded(pDevice->PortOffset, EEP_OFS_PWR_CCK);
618 	pDevice->byOFDMPwrG = SROMbyReadEmbedded(pDevice->PortOffset, EEP_OFS_PWR_OFDMG);
619 
620 	/* Load power Table */
621 	for (ii = 0; ii < CB_MAX_CHANNEL_24G; ii++) {
622 		pDevice->abyCCKPwrTbl[ii + 1] =
623 			SROMbyReadEmbedded(pDevice->PortOffset,
624 					   (unsigned char)(ii + EEP_OFS_CCK_PWR_TBL));
625 		if (pDevice->abyCCKPwrTbl[ii + 1] == 0)
626 			pDevice->abyCCKPwrTbl[ii+1] = pDevice->byCCKPwr;
627 
628 		pDevice->abyOFDMPwrTbl[ii + 1] =
629 			SROMbyReadEmbedded(pDevice->PortOffset,
630 					   (unsigned char)(ii + EEP_OFS_OFDM_PWR_TBL));
631 		if (pDevice->abyOFDMPwrTbl[ii + 1] == 0)
632 			pDevice->abyOFDMPwrTbl[ii + 1] = pDevice->byOFDMPwrG;
633 
634 		pDevice->abyCCKDefaultPwr[ii + 1] = byCCKPwrdBm;
635 		pDevice->abyOFDMDefaultPwr[ii + 1] = byOFDMPwrdBm;
636 	}
637 
638 	/* recover 12,13 ,14channel for EUROPE by 11 channel */
639 	if (((pDevice->abyEEPROM[EEP_OFS_ZONETYPE] == ZoneType_Japan) ||
640 	     (pDevice->abyEEPROM[EEP_OFS_ZONETYPE] == ZoneType_Europe)) &&
641 	    (pDevice->byOriginalZonetype == ZoneType_USA)) {
642 		for (ii = 11; ii < 14; ii++) {
643 			pDevice->abyCCKPwrTbl[ii] = pDevice->abyCCKPwrTbl[10];
644 			pDevice->abyOFDMPwrTbl[ii] = pDevice->abyOFDMPwrTbl[10];
645 
646 		}
647 	}
648 
649 	/* Load OFDM A Power Table */
650 	for (ii = 0; ii < CB_MAX_CHANNEL_5G; ii++) {
651 		pDevice->abyOFDMPwrTbl[ii + CB_MAX_CHANNEL_24G + 1] =
652 			SROMbyReadEmbedded(pDevice->PortOffset,
653 					   (unsigned char)(ii + EEP_OFS_OFDMA_PWR_TBL));
654 
655 		pDevice->abyOFDMDefaultPwr[ii + CB_MAX_CHANNEL_24G + 1] =
656 			SROMbyReadEmbedded(pDevice->PortOffset,
657 					   (unsigned char)(ii + EEP_OFS_OFDMA_PWR_dBm));
658 	}
659 
660 	init_channel_table((void *)pDevice);
661 
662 	if (pDevice->byLocalID > REV_ID_VT3253_B1) {
663 		MACvSelectPage1(pDevice->PortOffset);
664 
665 		VNSvOutPortB(pDevice->PortOffset + MAC_REG_MSRCTL + 1,
666 			     (MSRCTL1_TXPWR | MSRCTL1_CSAPAREN));
667 
668 		MACvSelectPage0(pDevice->PortOffset);
669 	}
670 
671 	/* use relative tx timeout and 802.11i D4 */
672 	MACvWordRegBitsOn(pDevice->PortOffset,
673 			  MAC_REG_CFG, (CFG_TKIPOPT | CFG_NOTXTIMEOUT));
674 
675 	/* set performance parameter by registry */
676 	MACvSetShortRetryLimit(pDevice->PortOffset, pDevice->byShortRetryLimit);
677 	MACvSetLongRetryLimit(pDevice->PortOffset, pDevice->byLongRetryLimit);
678 
679 	/* reset TSF counter */
680 	VNSvOutPortB(pDevice->PortOffset + MAC_REG_TFTCTL, TFTCTL_TSFCNTRST);
681 	/* enable TSF counter */
682 	VNSvOutPortB(pDevice->PortOffset + MAC_REG_TFTCTL, TFTCTL_TSFCNTREN);
683 
684 	/* initialize BBP registers */
685 	BBbVT3253Init(pDevice);
686 
687 	if (pDevice->bUpdateBBVGA) {
688 		pDevice->byBBVGACurrent = pDevice->abyBBVGA[0];
689 		pDevice->byBBVGANew = pDevice->byBBVGACurrent;
690 		BBvSetVGAGainOffset(pDevice, pDevice->abyBBVGA[0]);
691 	}
692 
693 	BBvSetRxAntennaMode(pDevice->PortOffset, pDevice->byRxAntennaMode);
694 	BBvSetTxAntennaMode(pDevice->PortOffset, pDevice->byTxAntennaMode);
695 
696 	pDevice->byCurrentCh = 0;
697 
698 	/* Set BB and packet type at the same time. */
699 	/* Set Short Slot Time, xIFS, and RSPINF. */
700 	if (pDevice->uConnectionRate == RATE_AUTO)
701 		pDevice->wCurrentRate = RATE_54M;
702 	else
703 		pDevice->wCurrentRate = (unsigned short)pDevice->uConnectionRate;
704 
705 	/* default G Mode */
706 	VNTWIFIbConfigPhyMode(pDevice->pMgmt, PHY_TYPE_11G);
707 	VNTWIFIbConfigPhyMode(pDevice->pMgmt, PHY_TYPE_AUTO);
708 
709 	pDevice->bRadioOff = false;
710 
711 	pDevice->byRadioCtl = SROMbyReadEmbedded(pDevice->PortOffset,
712 						 EEP_OFS_RADIOCTL);
713 	pDevice->bHWRadioOff = false;
714 
715 	if (pDevice->byRadioCtl & EEP_RADIOCTL_ENABLE) {
716 		/* Get GPIO */
717 		MACvGPIOIn(pDevice->PortOffset, &pDevice->byGPIO);
718 
719 		if (((pDevice->byGPIO & GPIO0_DATA) &&
720 		     !(pDevice->byRadioCtl & EEP_RADIOCTL_INV)) ||
721 		     (!(pDevice->byGPIO & GPIO0_DATA) &&
722 		     (pDevice->byRadioCtl & EEP_RADIOCTL_INV)))
723 			pDevice->bHWRadioOff = true;
724 	}
725 
726 	if (pDevice->bHWRadioOff || pDevice->bRadioControlOff)
727 		CARDbRadioPowerOff(pDevice);
728 
729 	pMgmt->eScanType = WMAC_SCAN_PASSIVE;
730 
731 	/* get Permanent network address */
732 	SROMvReadEtherAddress(pDevice->PortOffset, pDevice->abyCurrentNetAddr);
733 	pr_debug("Network address = %pM\n", pDevice->abyCurrentNetAddr);
734 
735 	/* reset Tx pointer */
736 	CARDvSafeResetRx(pDevice);
737 	/* reset Rx pointer */
738 	CARDvSafeResetTx(pDevice);
739 
740 	if (pDevice->byLocalID <= REV_ID_VT3253_A1)
741 		MACvRegBitsOn(pDevice->PortOffset, MAC_REG_RCR, RCR_WPAERR);
742 
743 	pDevice->eEncryptionStatus = Ndis802_11EncryptionDisabled;
744 
745 	/* Turn On Rx DMA */
746 	MACvReceive0(pDevice->PortOffset);
747 	MACvReceive1(pDevice->PortOffset);
748 
749 	/* start the adapter */
750 	MACvStart(pDevice->PortOffset);
751 
752 	netif_stop_queue(pDevice->dev);
753 }
754 
device_init_diversity_timer(struct vnt_private * pDevice)755 static void device_init_diversity_timer(struct vnt_private *pDevice)
756 {
757 	init_timer(&pDevice->TimerSQ3Tmax1);
758 	pDevice->TimerSQ3Tmax1.data = (unsigned long) pDevice;
759 	pDevice->TimerSQ3Tmax1.function = (TimerFunction)TimerSQ3CallBack;
760 	pDevice->TimerSQ3Tmax1.expires = RUN_AT(HZ);
761 
762 	init_timer(&pDevice->TimerSQ3Tmax2);
763 	pDevice->TimerSQ3Tmax2.data = (unsigned long) pDevice;
764 	pDevice->TimerSQ3Tmax2.function = (TimerFunction)TimerSQ3CallBack;
765 	pDevice->TimerSQ3Tmax2.expires = RUN_AT(HZ);
766 
767 	init_timer(&pDevice->TimerSQ3Tmax3);
768 	pDevice->TimerSQ3Tmax3.data = (unsigned long) pDevice;
769 	pDevice->TimerSQ3Tmax3.function = (TimerFunction)TimerState1CallBack;
770 	pDevice->TimerSQ3Tmax3.expires = RUN_AT(HZ);
771 }
772 
device_release_WPADEV(struct vnt_private * pDevice)773 static bool device_release_WPADEV(struct vnt_private *pDevice)
774 {
775 	viawget_wpa_header *wpahdr;
776 	int ii = 0;
777 
778 	//send device close to wpa_supplicnat layer
779 	if (pDevice->bWPADEVUp) {
780 		wpahdr = (viawget_wpa_header *)pDevice->skb->data;
781 		wpahdr->type = VIAWGET_DEVICECLOSE_MSG;
782 		wpahdr->resp_ie_len = 0;
783 		wpahdr->req_ie_len = 0;
784 		skb_put(pDevice->skb, sizeof(viawget_wpa_header));
785 		pDevice->skb->dev = pDevice->wpadev;
786 		skb_reset_mac_header(pDevice->skb);
787 		pDevice->skb->pkt_type = PACKET_HOST;
788 		pDevice->skb->protocol = htons(ETH_P_802_2);
789 		memset(pDevice->skb->cb, 0, sizeof(pDevice->skb->cb));
790 		netif_rx(pDevice->skb);
791 		pDevice->skb = dev_alloc_skb((int)pDevice->rx_buf_sz);
792 
793 		while (pDevice->bWPADEVUp) {
794 			set_current_state(TASK_UNINTERRUPTIBLE);
795 			schedule_timeout(HZ / 20);          //wait 50ms
796 			ii++;
797 			if (ii > 20)
798 				break;
799 		}
800 	}
801 	return true;
802 }
803 
804 static const struct net_device_ops device_netdev_ops = {
805 	.ndo_open               = device_open,
806 	.ndo_stop               = device_close,
807 	.ndo_do_ioctl           = device_ioctl,
808 	.ndo_start_xmit         = device_xmit,
809 	.ndo_set_rx_mode	= device_set_multi,
810 };
811 
812 static int
vt6655_probe(struct pci_dev * pcid,const struct pci_device_id * ent)813 vt6655_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 
device_print_info(struct vnt_private * pDevice)952 static 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 
vt6655_init_info(struct pci_dev * pcid,struct vnt_private ** ppDevice,PCHIP_INFO pChip_info)962 static 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 
device_get_pci_info(struct vnt_private * pDevice,struct pci_dev * pcid)977 static 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 
device_free_info(struct vnt_private * pDevice)1004 static 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 
device_init_rings(struct vnt_private * pDevice)1033 static 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 
device_free_rings(struct vnt_private * pDevice)1110 static 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 
device_init_rd0_ring(struct vnt_private * pDevice)1131 static 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 
device_init_rd1_ring(struct vnt_private * pDevice)1155 static 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 
device_init_defrag_cb(struct vnt_private * pDevice)1179 static 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 
device_free_rd0_ring(struct vnt_private * pDevice)1194 static 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 
device_free_rd1_ring(struct vnt_private * pDevice)1211 static 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 
device_free_frag_buf(struct vnt_private * pDevice)1228 static 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 
device_init_td0_ring(struct vnt_private * pDevice)1242 static 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 
device_init_td1_ring(struct vnt_private * pDevice)1267 static 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 
device_free_td0_ring(struct vnt_private * pDevice)1293 static 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 
device_free_td1_ring(struct vnt_private * pDevice)1312 static 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 
device_rx_srv(struct vnt_private * pDevice,unsigned int uIdx)1333 static 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 
1344 		if (!pRD->pRDInfo->skb)
1345 			break;
1346 
1347 		if (device_receive_frame(pDevice, pRD)) {
1348 			if (!device_alloc_rx_buf(pDevice, pRD)) {
1349 				dev_err(&pDevice->pcid->dev,
1350 					"can not allocate rx buf\n");
1351 				break;
1352 			}
1353 		}
1354 		pRD->m_rd0RD0.f1Owner = OWNED_BY_NIC;
1355 		pDevice->dev->last_rx = jiffies;
1356 	}
1357 
1358 	pDevice->pCurrRD[uIdx] = pRD;
1359 
1360 	return works;
1361 }
1362 
device_alloc_rx_buf(struct vnt_private * pDevice,PSRxDesc pRD)1363 static bool device_alloc_rx_buf(struct vnt_private *pDevice, PSRxDesc pRD)
1364 {
1365 	PDEVICE_RD_INFO pRDInfo = pRD->pRDInfo;
1366 
1367 	pRDInfo->skb = dev_alloc_skb((int)pDevice->rx_buf_sz);
1368 	if (pRDInfo->skb == NULL)
1369 		return false;
1370 	ASSERT(pRDInfo->skb);
1371 	pRDInfo->skb->dev = pDevice->dev;
1372 	pRDInfo->skb_dma = pci_map_single(pDevice->pcid, skb_tail_pointer(pRDInfo->skb),
1373 					  pDevice->rx_buf_sz, PCI_DMA_FROMDEVICE);
1374 	*((unsigned int *)&(pRD->m_rd0RD0)) = 0; /* FIX cast */
1375 
1376 	pRD->m_rd0RD0.wResCount = cpu_to_le16(pDevice->rx_buf_sz);
1377 	pRD->m_rd0RD0.f1Owner = OWNED_BY_NIC;
1378 	pRD->m_rd1RD1.wReqCount = cpu_to_le16(pDevice->rx_buf_sz);
1379 	pRD->buff_addr = cpu_to_le32(pRDInfo->skb_dma);
1380 
1381 	return true;
1382 }
1383 
device_alloc_frag_buf(struct vnt_private * pDevice,PSDeFragControlBlock pDeF)1384 bool device_alloc_frag_buf(struct vnt_private *pDevice,
1385 			   PSDeFragControlBlock pDeF)
1386 {
1387 	pDeF->skb = dev_alloc_skb((int)pDevice->rx_buf_sz);
1388 	if (pDeF->skb == NULL)
1389 		return false;
1390 	ASSERT(pDeF->skb);
1391 	pDeF->skb->dev = pDevice->dev;
1392 
1393 	return true;
1394 }
1395 
device_tx_srv(struct vnt_private * pDevice,unsigned int uIdx)1396 static int device_tx_srv(struct vnt_private *pDevice, unsigned int uIdx)
1397 {
1398 	PSTxDesc                 pTD;
1399 	bool bFull = false;
1400 	int                      works = 0;
1401 	unsigned char byTsr0;
1402 	unsigned char byTsr1;
1403 	unsigned int	uFrameSize, uFIFOHeaderSize;
1404 	PSTxBufHead              pTxBufHead;
1405 	struct net_device_stats *pStats = &pDevice->dev->stats;
1406 	struct sk_buff *skb;
1407 	unsigned int	uNodeIndex;
1408 	PSMgmtObject             pMgmt = pDevice->pMgmt;
1409 
1410 	for (pTD = pDevice->apTailTD[uIdx]; pDevice->iTDUsed[uIdx] > 0; pTD = pTD->next) {
1411 		if (pTD->m_td0TD0.f1Owner == OWNED_BY_NIC)
1412 			break;
1413 		if (works++ > 15)
1414 			break;
1415 
1416 		byTsr0 = pTD->m_td0TD0.byTSR0;
1417 		byTsr1 = pTD->m_td0TD0.byTSR1;
1418 
1419 		//Only the status of first TD in the chain is correct
1420 		if (pTD->m_td1TD1.byTCR & TCR_STP) {
1421 			if ((pTD->pTDInfo->byFlags & TD_FLAGS_NETIF_SKB) != 0) {
1422 				uFIFOHeaderSize = pTD->pTDInfo->dwHeaderLength;
1423 				uFrameSize = pTD->pTDInfo->dwReqCount - uFIFOHeaderSize;
1424 				pTxBufHead = (PSTxBufHead) (pTD->pTDInfo->buf);
1425 				// Update the statistics based on the Transmit status
1426 				// now, we DONT check TSR0_CDH
1427 
1428 				STAvUpdateTDStatCounter(&pDevice->scStatistic,
1429 							byTsr0, byTsr1,
1430 							(unsigned char *)(pTD->pTDInfo->buf + uFIFOHeaderSize),
1431 							uFrameSize, uIdx);
1432 
1433 				BSSvUpdateNodeTxCounter(pDevice,
1434 							byTsr0, byTsr1,
1435 							(unsigned char *)(pTD->pTDInfo->buf),
1436 							uFIFOHeaderSize
1437 					);
1438 
1439 				if (!(byTsr1 & TSR1_TERR)) {
1440 					if (byTsr0 != 0) {
1441 						pr_debug(" Tx[%d] OK but has error. tsr1[%02X] tsr0[%02X]\n",
1442 							 (int)uIdx, byTsr1,
1443 							 byTsr0);
1444 					}
1445 					if ((pTxBufHead->wFragCtl & FRAGCTL_ENDFRAG) != FRAGCTL_NONFRAG)
1446 						pDevice->s802_11Counter.TransmittedFragmentCount++;
1447 
1448 					pStats->tx_packets++;
1449 					pStats->tx_bytes += pTD->pTDInfo->skb->len;
1450 				} else {
1451 					pr_debug(" Tx[%d] dropped & tsr1[%02X] tsr0[%02X]\n",
1452 						 (int)uIdx, byTsr1, byTsr0);
1453 					pStats->tx_errors++;
1454 					pStats->tx_dropped++;
1455 				}
1456 			}
1457 
1458 			if ((pTD->pTDInfo->byFlags & TD_FLAGS_PRIV_SKB) != 0) {
1459 				if (pDevice->bEnableHostapd) {
1460 					pr_debug("tx call back netif..\n");
1461 					skb = pTD->pTDInfo->skb;
1462 					skb->dev = pDevice->apdev;
1463 					skb_reset_mac_header(skb);
1464 					skb->pkt_type = PACKET_OTHERHOST;
1465 					memset(skb->cb, 0, sizeof(skb->cb));
1466 					netif_rx(skb);
1467 				}
1468 			}
1469 
1470 			if (byTsr1 & TSR1_TERR) {
1471 				if ((pTD->pTDInfo->byFlags & TD_FLAGS_PRIV_SKB) != 0) {
1472 					pr_debug(" Tx[%d] fail has error. tsr1[%02X] tsr0[%02X]\n",
1473 						 (int)uIdx, byTsr1, byTsr0);
1474 				}
1475 
1476 
1477 				if ((pMgmt->eCurrMode == WMAC_MODE_ESS_AP) &&
1478 				    (pTD->pTDInfo->byFlags & TD_FLAGS_NETIF_SKB)) {
1479 					unsigned short wAID;
1480 					unsigned char byMask[8] = {1, 2, 4, 8, 0x10, 0x20, 0x40, 0x80};
1481 
1482 					skb = pTD->pTDInfo->skb;
1483 					if (BSSDBbIsSTAInNodeDB(pMgmt, (unsigned char *)(skb->data), &uNodeIndex)) {
1484 						if (pMgmt->sNodeDBTable[uNodeIndex].bPSEnable) {
1485 							skb_queue_tail(&pMgmt->sNodeDBTable[uNodeIndex].sTxPSQueue, skb);
1486 							pMgmt->sNodeDBTable[uNodeIndex].wEnQueueCnt++;
1487 							// set tx map
1488 							wAID = pMgmt->sNodeDBTable[uNodeIndex].wAID;
1489 							pMgmt->abyPSTxMap[wAID >> 3] |=  byMask[wAID & 7];
1490 							pTD->pTDInfo->byFlags &= ~(TD_FLAGS_NETIF_SKB);
1491 							pr_debug("tx_srv:tx fail re-queue sta index= %d, QueCnt= %d\n",
1492 								 (int)uNodeIndex,
1493 								 pMgmt->sNodeDBTable[uNodeIndex].wEnQueueCnt);
1494 							pStats->tx_errors--;
1495 							pStats->tx_dropped--;
1496 						}
1497 					}
1498 				}
1499 			}
1500 			device_free_tx_buf(pDevice, pTD);
1501 			pDevice->iTDUsed[uIdx]--;
1502 		}
1503 	}
1504 
1505 	if (uIdx == TYPE_AC0DMA) {
1506 		// RESERV_AC0DMA reserved for relay
1507 
1508 		if (AVAIL_TD(pDevice, uIdx) < RESERV_AC0DMA) {
1509 			bFull = true;
1510 			pr_debug(" AC0DMA is Full = %d\n",
1511 				 pDevice->iTDUsed[uIdx]);
1512 		}
1513 		if (netif_queue_stopped(pDevice->dev) && !bFull)
1514 			netif_wake_queue(pDevice->dev);
1515 
1516 	}
1517 
1518 	pDevice->apTailTD[uIdx] = pTD;
1519 
1520 	return works;
1521 }
1522 
device_error(struct vnt_private * pDevice,unsigned short status)1523 static void device_error(struct vnt_private *pDevice, unsigned short status)
1524 {
1525 	if (status & ISR_FETALERR) {
1526 		dev_err(&pDevice->pcid->dev, "Hardware fatal error\n");
1527 
1528 		netif_stop_queue(pDevice->dev);
1529 		del_timer(&pDevice->sTimerCommand);
1530 		del_timer(&(pDevice->pMgmt->sTimerSecondCallback));
1531 		pDevice->bCmdRunning = false;
1532 		MACbShutdown(pDevice->PortOffset);
1533 		return;
1534 	}
1535 }
1536 
device_free_tx_buf(struct vnt_private * pDevice,PSTxDesc pDesc)1537 static void device_free_tx_buf(struct vnt_private *pDevice, PSTxDesc pDesc)
1538 {
1539 	PDEVICE_TD_INFO  pTDInfo = pDesc->pTDInfo;
1540 	struct sk_buff *skb = pTDInfo->skb;
1541 
1542 	// pre-allocated buf_dma can't be unmapped.
1543 	if (pTDInfo->skb_dma && (pTDInfo->skb_dma != pTDInfo->buf_dma)) {
1544 		pci_unmap_single(pDevice->pcid, pTDInfo->skb_dma, skb->len,
1545 				 PCI_DMA_TODEVICE);
1546 	}
1547 
1548 	if ((pTDInfo->byFlags & TD_FLAGS_NETIF_SKB) != 0)
1549 		dev_kfree_skb_irq(skb);
1550 
1551 	pTDInfo->skb_dma = 0;
1552 	pTDInfo->skb = NULL;
1553 	pTDInfo->byFlags = 0;
1554 }
1555 
device_open(struct net_device * dev)1556 static int  device_open(struct net_device *dev)
1557 {
1558 	struct vnt_private *pDevice = netdev_priv(dev);
1559 	int i;
1560 #ifdef WPA_SM_Transtatus
1561 	extern SWPAResult wpa_Result;
1562 #endif
1563 
1564 	pDevice->rx_buf_sz = PKT_BUF_SZ;
1565 	if (!device_init_rings(pDevice))
1566 		return -ENOMEM;
1567 
1568 //2008-5-13 <add> by chester
1569 	i = request_irq(pDevice->pcid->irq, &device_intr, IRQF_SHARED, dev->name, dev);
1570 	if (i)
1571 		return i;
1572 
1573 #ifdef WPA_SM_Transtatus
1574 	memset(wpa_Result.ifname, 0, sizeof(wpa_Result.ifname));
1575 	wpa_Result.proto = 0;
1576 	wpa_Result.key_mgmt = 0;
1577 	wpa_Result.eap_type = 0;
1578 	wpa_Result.authenticated = false;
1579 	pDevice->fWPA_Authened = false;
1580 #endif
1581 	pr_debug("call device init rd0 ring\n");
1582 	device_init_rd0_ring(pDevice);
1583 	device_init_rd1_ring(pDevice);
1584 	device_init_defrag_cb(pDevice);
1585 	device_init_td0_ring(pDevice);
1586 	device_init_td1_ring(pDevice);
1587 
1588 	if (pDevice->bDiversityRegCtlON)
1589 		device_init_diversity_timer(pDevice);
1590 
1591 	vMgrObjectInit(pDevice);
1592 	vMgrTimerInit(pDevice);
1593 
1594 	pr_debug("call device_init_registers\n");
1595 	device_init_registers(pDevice);
1596 
1597 	MACvReadEtherAddress(pDevice->PortOffset, pDevice->abyCurrentNetAddr);
1598 	memcpy(pDevice->pMgmt->abyMACAddr, pDevice->abyCurrentNetAddr, ETH_ALEN);
1599 	device_set_multi(pDevice->dev);
1600 
1601 	// Init for Key Management
1602 	KeyvInitTable(&pDevice->sKey, pDevice->PortOffset);
1603 	add_timer(&(pDevice->pMgmt->sTimerSecondCallback));
1604 
1605 #ifdef WPA_SUPPLICANT_DRIVER_WEXT_SUPPORT
1606 	pDevice->bwextcount = 0;
1607 	pDevice->bWPASuppWextEnabled = false;
1608 #endif
1609 	pDevice->byReAssocCount = 0;
1610 	pDevice->bWPADEVUp = false;
1611 	// Patch: if WEP key already set by iwconfig but device not yet open
1612 	if (pDevice->bEncryptionEnable && pDevice->bTransmitKey) {
1613 		KeybSetDefaultKey(&(pDevice->sKey),
1614 				  (unsigned long)(pDevice->byKeyIndex | (1 << 31)),
1615 				  pDevice->uKeyLength,
1616 				  NULL,
1617 				  pDevice->abyKey,
1618 				  KEY_CTL_WEP,
1619 				  pDevice->PortOffset,
1620 				  pDevice->byLocalID
1621 			);
1622 		pDevice->eEncryptionStatus = Ndis802_11Encryption1Enabled;
1623 	}
1624 
1625 	pr_debug("call MACvIntEnable\n");
1626 	MACvIntEnable(pDevice->PortOffset, IMR_MASK_VALUE);
1627 
1628 	if (pDevice->pMgmt->eConfigMode == WMAC_CONFIG_AP) {
1629 		bScheduleCommand((void *)pDevice, WLAN_CMD_RUN_AP, NULL);
1630 	} else {
1631 		bScheduleCommand((void *)pDevice, WLAN_CMD_BSSID_SCAN, NULL);
1632 		bScheduleCommand((void *)pDevice, WLAN_CMD_SSID, NULL);
1633 	}
1634 	pDevice->flags |= DEVICE_FLAGS_OPENED;
1635 
1636 	pr_debug("device_open success..\n");
1637 	return 0;
1638 }
1639 
device_close(struct net_device * dev)1640 static int  device_close(struct net_device *dev)
1641 {
1642 	struct vnt_private *pDevice = netdev_priv(dev);
1643 	PSMgmtObject     pMgmt = pDevice->pMgmt;
1644 //2007-1121-02<Add>by EinsnLiu
1645 	if (pDevice->bLinkPass) {
1646 		bScheduleCommand((void *)pDevice, WLAN_CMD_DISASSOCIATE, NULL);
1647 		mdelay(30);
1648 	}
1649 
1650 	del_timer(&pDevice->sTimerTxData);
1651 	del_timer(&pDevice->sTimerCommand);
1652 	del_timer(&pMgmt->sTimerSecondCallback);
1653 	if (pDevice->bDiversityRegCtlON) {
1654 		del_timer(&pDevice->TimerSQ3Tmax1);
1655 		del_timer(&pDevice->TimerSQ3Tmax2);
1656 		del_timer(&pDevice->TimerSQ3Tmax3);
1657 	}
1658 
1659 	netif_stop_queue(dev);
1660 	pDevice->bCmdRunning = false;
1661 	MACbShutdown(pDevice->PortOffset);
1662 	MACbSoftwareReset(pDevice->PortOffset);
1663 	CARDbRadioPowerOff(pDevice);
1664 
1665 	pDevice->bLinkPass = false;
1666 	memset(pMgmt->abyCurrBSSID, 0, 6);
1667 	pMgmt->eCurrState = WMAC_STATE_IDLE;
1668 	device_free_td0_ring(pDevice);
1669 	device_free_td1_ring(pDevice);
1670 	device_free_rd0_ring(pDevice);
1671 	device_free_rd1_ring(pDevice);
1672 	device_free_frag_buf(pDevice);
1673 	device_free_rings(pDevice);
1674 	BSSvClearNodeDBTable(pDevice, 0);
1675 	free_irq(dev->irq, dev);
1676 	pDevice->flags &= (~DEVICE_FLAGS_OPENED);
1677 	//2008-0714-01<Add>by chester
1678 	device_release_WPADEV(pDevice);
1679 
1680 	pr_debug("device_close..\n");
1681 	return 0;
1682 }
1683 
device_dma0_tx_80211(struct sk_buff * skb,struct net_device * dev)1684 static int device_dma0_tx_80211(struct sk_buff *skb, struct net_device *dev)
1685 {
1686 	struct vnt_private *pDevice = netdev_priv(dev);
1687 	unsigned char *pbMPDU;
1688 	unsigned int cbMPDULen = 0;
1689 
1690 	pr_debug("device_dma0_tx_80211\n");
1691 	spin_lock_irq(&pDevice->lock);
1692 
1693 	if (AVAIL_TD(pDevice, TYPE_TXDMA0) <= 0) {
1694 		pr_debug("device_dma0_tx_80211, td0 <=0\n");
1695 		dev_kfree_skb_irq(skb);
1696 		spin_unlock_irq(&pDevice->lock);
1697 		return 0;
1698 	}
1699 
1700 	if (pDevice->bStopTx0Pkt) {
1701 		dev_kfree_skb_irq(skb);
1702 		spin_unlock_irq(&pDevice->lock);
1703 		return 0;
1704 	}
1705 
1706 	cbMPDULen = skb->len;
1707 	pbMPDU = skb->data;
1708 
1709 	vDMA0_tx_80211(pDevice, skb, pbMPDU, cbMPDULen);
1710 
1711 	spin_unlock_irq(&pDevice->lock);
1712 
1713 	return 0;
1714 }
1715 
device_dma0_xmit(struct vnt_private * pDevice,struct sk_buff * skb,unsigned int uNodeIndex)1716 bool device_dma0_xmit(struct vnt_private *pDevice,
1717 		      struct sk_buff *skb, unsigned int uNodeIndex)
1718 {
1719 	PSMgmtObject    pMgmt = pDevice->pMgmt;
1720 	PSTxDesc        pHeadTD, pLastTD;
1721 	unsigned int cbFrameBodySize;
1722 	unsigned int uMACfragNum;
1723 	unsigned char byPktType;
1724 	bool bNeedEncryption = false;
1725 	PSKeyItem       pTransmitKey = NULL;
1726 	unsigned int cbHeaderSize;
1727 	unsigned int ii;
1728 	SKeyItem        STempKey;
1729 
1730 	if (pDevice->bStopTx0Pkt) {
1731 		dev_kfree_skb_irq(skb);
1732 		return false;
1733 	}
1734 
1735 	if (AVAIL_TD(pDevice, TYPE_TXDMA0) <= 0) {
1736 		dev_kfree_skb_irq(skb);
1737 		pr_debug("device_dma0_xmit, td0 <=0\n");
1738 		return false;
1739 	}
1740 
1741 	if (pMgmt->eCurrMode == WMAC_MODE_ESS_AP) {
1742 		if (pDevice->uAssocCount == 0) {
1743 			dev_kfree_skb_irq(skb);
1744 			pr_debug("device_dma0_xmit, assocCount = 0\n");
1745 			return false;
1746 		}
1747 	}
1748 
1749 	pHeadTD = pDevice->apCurrTD[TYPE_TXDMA0];
1750 
1751 	pHeadTD->m_td1TD1.byTCR = (TCR_EDP|TCR_STP);
1752 
1753 	memcpy(pDevice->sTxEthHeader.abyDstAddr, (unsigned char *)(skb->data), ETH_HLEN);
1754 	cbFrameBodySize = skb->len - ETH_HLEN;
1755 
1756 	// 802.1H
1757 	if (ntohs(pDevice->sTxEthHeader.wType) > ETH_DATA_LEN)
1758 		cbFrameBodySize += 8;
1759 
1760 	uMACfragNum = cbGetFragCount(pDevice, pTransmitKey, cbFrameBodySize, &pDevice->sTxEthHeader);
1761 
1762 	if (uMACfragNum > AVAIL_TD(pDevice, TYPE_TXDMA0)) {
1763 		dev_kfree_skb_irq(skb);
1764 		return false;
1765 	}
1766 	byPktType = (unsigned char)pDevice->byPacketType;
1767 
1768 	if (pDevice->bFixRate) {
1769 		if (pDevice->eCurrentPHYType == PHY_TYPE_11B) {
1770 			if (pDevice->uConnectionRate >= RATE_11M)
1771 				pDevice->wCurrentRate = RATE_11M;
1772 			else
1773 				pDevice->wCurrentRate = (unsigned short)pDevice->uConnectionRate;
1774 		} else {
1775 			if (pDevice->uConnectionRate >= RATE_54M)
1776 				pDevice->wCurrentRate = RATE_54M;
1777 			else
1778 				pDevice->wCurrentRate = (unsigned short)pDevice->uConnectionRate;
1779 		}
1780 	} else {
1781 		pDevice->wCurrentRate = pDevice->pMgmt->sNodeDBTable[uNodeIndex].wTxDataRate;
1782 	}
1783 
1784 	//preamble type
1785 	if (pMgmt->sNodeDBTable[uNodeIndex].bShortPreamble)
1786 		pDevice->byPreambleType = pDevice->byShortPreamble;
1787 	else
1788 		pDevice->byPreambleType = PREAMBLE_LONG;
1789 
1790 	pr_debug("dma0: pDevice->wCurrentRate = %d\n", pDevice->wCurrentRate);
1791 
1792 	if (pDevice->wCurrentRate <= RATE_11M) {
1793 		byPktType = PK_TYPE_11B;
1794 	} else if (pDevice->eCurrentPHYType == PHY_TYPE_11A) {
1795 		byPktType = PK_TYPE_11A;
1796 	} else {
1797 		if (pDevice->bProtectMode)
1798 			byPktType = PK_TYPE_11GB;
1799 		else
1800 			byPktType = PK_TYPE_11GA;
1801 	}
1802 
1803 	if (pDevice->bEncryptionEnable)
1804 		bNeedEncryption = true;
1805 
1806 	if (pDevice->bEnableHostWEP) {
1807 		pTransmitKey = &STempKey;
1808 		pTransmitKey->byCipherSuite = pMgmt->sNodeDBTable[uNodeIndex].byCipherSuite;
1809 		pTransmitKey->dwKeyIndex = pMgmt->sNodeDBTable[uNodeIndex].dwKeyIndex;
1810 		pTransmitKey->uKeyLength = pMgmt->sNodeDBTable[uNodeIndex].uWepKeyLength;
1811 		pTransmitKey->dwTSC47_16 = pMgmt->sNodeDBTable[uNodeIndex].dwTSC47_16;
1812 		pTransmitKey->wTSC15_0 = pMgmt->sNodeDBTable[uNodeIndex].wTSC15_0;
1813 		memcpy(pTransmitKey->abyKey,
1814 		       &pMgmt->sNodeDBTable[uNodeIndex].abyWepKey[0],
1815 		       pTransmitKey->uKeyLength
1816 			);
1817 	}
1818 	vGenerateFIFOHeader(pDevice, byPktType, pDevice->pbyTmpBuff, bNeedEncryption,
1819 			    cbFrameBodySize, TYPE_TXDMA0, pHeadTD,
1820 			    &pDevice->sTxEthHeader, (unsigned char *)skb->data, pTransmitKey, uNodeIndex,
1821 			    &uMACfragNum,
1822 			    &cbHeaderSize
1823 		);
1824 
1825 	if (MACbIsRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_PS)) {
1826 		// Disable PS
1827 		MACbPSWakeup(pDevice->PortOffset);
1828 	}
1829 
1830 	pDevice->bPWBitOn = false;
1831 
1832 	pLastTD = pHeadTD;
1833 	for (ii = 0; ii < uMACfragNum; ii++) {
1834 		// Poll Transmit the adapter
1835 		wmb();
1836 		pHeadTD->m_td0TD0.f1Owner = OWNED_BY_NIC;
1837 		wmb();
1838 		if (ii == (uMACfragNum - 1))
1839 			pLastTD = pHeadTD;
1840 		pHeadTD = pHeadTD->next;
1841 	}
1842 
1843 	// Save the information needed by the tx interrupt handler
1844 	// to complete the Send request
1845 	pLastTD->pTDInfo->skb = skb;
1846 	pLastTD->pTDInfo->byFlags = 0;
1847 	pLastTD->pTDInfo->byFlags |= TD_FLAGS_NETIF_SKB;
1848 
1849 	pDevice->apCurrTD[TYPE_TXDMA0] = pHeadTD;
1850 
1851 	MACvTransmit0(pDevice->PortOffset);
1852 
1853 	return true;
1854 }
1855 
1856 //TYPE_AC0DMA data tx
device_xmit(struct sk_buff * skb,struct net_device * dev)1857 static int  device_xmit(struct sk_buff *skb, struct net_device *dev) {
1858 	struct vnt_private *pDevice = netdev_priv(dev);
1859 	PSMgmtObject    pMgmt = pDevice->pMgmt;
1860 	PSTxDesc        pHeadTD, pLastTD;
1861 	unsigned int uNodeIndex = 0;
1862 	unsigned char byMask[8] = {1, 2, 4, 8, 0x10, 0x20, 0x40, 0x80};
1863 	unsigned short wAID;
1864 	unsigned int uMACfragNum = 1;
1865 	unsigned int cbFrameBodySize;
1866 	unsigned char byPktType;
1867 	unsigned int cbHeaderSize;
1868 	bool bNeedEncryption = false;
1869 	PSKeyItem       pTransmitKey = NULL;
1870 	SKeyItem        STempKey;
1871 	unsigned int ii;
1872 	bool bTKIP_UseGTK = false;
1873 	bool bNeedDeAuth = false;
1874 	unsigned char *pbyBSSID;
1875 	bool bNodeExist = false;
1876 
1877 	spin_lock_irq(&pDevice->lock);
1878 	if (!pDevice->bLinkPass) {
1879 		dev_kfree_skb_irq(skb);
1880 		spin_unlock_irq(&pDevice->lock);
1881 		return 0;
1882 	}
1883 
1884 	if (pDevice->bStopDataPkt) {
1885 		dev_kfree_skb_irq(skb);
1886 		spin_unlock_irq(&pDevice->lock);
1887 		return 0;
1888 	}
1889 
1890 	if (pMgmt->eCurrMode == WMAC_MODE_ESS_AP) {
1891 		if (pDevice->uAssocCount == 0) {
1892 			dev_kfree_skb_irq(skb);
1893 			spin_unlock_irq(&pDevice->lock);
1894 			return 0;
1895 		}
1896 		if (is_multicast_ether_addr((unsigned char *)(skb->data))) {
1897 			uNodeIndex = 0;
1898 			bNodeExist = true;
1899 			if (pMgmt->sNodeDBTable[0].bPSEnable) {
1900 				skb_queue_tail(&(pMgmt->sNodeDBTable[0].sTxPSQueue), skb);
1901 				pMgmt->sNodeDBTable[0].wEnQueueCnt++;
1902 				// set tx map
1903 				pMgmt->abyPSTxMap[0] |= byMask[0];
1904 				spin_unlock_irq(&pDevice->lock);
1905 				return 0;
1906 			}
1907 		} else {
1908 			if (BSSDBbIsSTAInNodeDB(pMgmt, (unsigned char *)(skb->data), &uNodeIndex)) {
1909 				if (pMgmt->sNodeDBTable[uNodeIndex].bPSEnable) {
1910 					skb_queue_tail(&pMgmt->sNodeDBTable[uNodeIndex].sTxPSQueue, skb);
1911 					pMgmt->sNodeDBTable[uNodeIndex].wEnQueueCnt++;
1912 					// set tx map
1913 					wAID = pMgmt->sNodeDBTable[uNodeIndex].wAID;
1914 					pMgmt->abyPSTxMap[wAID >> 3] |=  byMask[wAID & 7];
1915 					pr_debug("Set:pMgmt->abyPSTxMap[%d]= %d\n",
1916 						 (wAID >> 3),
1917 						 pMgmt->abyPSTxMap[wAID >> 3]);
1918 					spin_unlock_irq(&pDevice->lock);
1919 					return 0;
1920 				}
1921 
1922 				if (pMgmt->sNodeDBTable[uNodeIndex].bShortPreamble)
1923 					pDevice->byPreambleType = pDevice->byShortPreamble;
1924 				else
1925 					pDevice->byPreambleType = PREAMBLE_LONG;
1926 
1927 				bNodeExist = true;
1928 
1929 			}
1930 		}
1931 
1932 		if (!bNodeExist) {
1933 			pr_debug("Unknown STA not found in node DB\n");
1934 			dev_kfree_skb_irq(skb);
1935 			spin_unlock_irq(&pDevice->lock);
1936 			return 0;
1937 		}
1938 	}
1939 
1940 	pHeadTD = pDevice->apCurrTD[TYPE_AC0DMA];
1941 
1942 	pHeadTD->m_td1TD1.byTCR = (TCR_EDP|TCR_STP);
1943 
1944 	memcpy(pDevice->sTxEthHeader.abyDstAddr, (unsigned char *)(skb->data), ETH_HLEN);
1945 	cbFrameBodySize = skb->len - ETH_HLEN;
1946 	// 802.1H
1947 	if (ntohs(pDevice->sTxEthHeader.wType) > ETH_DATA_LEN)
1948 		cbFrameBodySize += 8;
1949 
1950 	if (pDevice->bEncryptionEnable) {
1951 		bNeedEncryption = true;
1952 		// get Transmit key
1953 		do {
1954 			if ((pDevice->pMgmt->eCurrMode == WMAC_MODE_ESS_STA) &&
1955 			    (pDevice->pMgmt->eCurrState == WMAC_STATE_ASSOC)) {
1956 				pbyBSSID = pDevice->abyBSSID;
1957 				// get pairwise key
1958 				if (KeybGetTransmitKey(&(pDevice->sKey), pbyBSSID, PAIRWISE_KEY, &pTransmitKey) == false) {
1959 					// get group key
1960 					if (KeybGetTransmitKey(&(pDevice->sKey), pbyBSSID, GROUP_KEY, &pTransmitKey) == true) {
1961 						bTKIP_UseGTK = true;
1962 						pr_debug("Get GTK\n");
1963 						break;
1964 					}
1965 				} else {
1966 					pr_debug("Get PTK\n");
1967 					break;
1968 				}
1969 			} else if (pDevice->pMgmt->eCurrMode == WMAC_MODE_IBSS_STA) {
1970 				pbyBSSID = pDevice->sTxEthHeader.abyDstAddr;  //TO_DS = 0 and FROM_DS = 0 --> 802.11 MAC Address1
1971 				pr_debug("IBSS Serach Key:\n");
1972 				for (ii = 0; ii < 6; ii++)
1973 					pr_debug("%x\n", *(pbyBSSID+ii));
1974 				pr_debug("\n");
1975 
1976 				// get pairwise key
1977 				if (KeybGetTransmitKey(&(pDevice->sKey), pbyBSSID, PAIRWISE_KEY, &pTransmitKey) == true)
1978 					break;
1979 			}
1980 			// get group key
1981 			pbyBSSID = pDevice->abyBroadcastAddr;
1982 			if (KeybGetTransmitKey(&(pDevice->sKey), pbyBSSID, GROUP_KEY, &pTransmitKey) == false) {
1983 				pTransmitKey = NULL;
1984 				if (pDevice->pMgmt->eCurrMode == WMAC_MODE_IBSS_STA)
1985 					pr_debug("IBSS and KEY is NULL. [%d]\n",
1986 						 pDevice->pMgmt->eCurrMode);
1987 				else
1988 					pr_debug("NOT IBSS and KEY is NULL. [%d]\n",
1989 						 pDevice->pMgmt->eCurrMode);
1990 			} else {
1991 				bTKIP_UseGTK = true;
1992 				pr_debug("Get GTK\n");
1993 			}
1994 		} while (false);
1995 	}
1996 
1997 	if (pDevice->bEnableHostWEP) {
1998 		pr_debug("acdma0: STA index %d\n", uNodeIndex);
1999 		if (pDevice->bEncryptionEnable) {
2000 			pTransmitKey = &STempKey;
2001 			pTransmitKey->byCipherSuite = pMgmt->sNodeDBTable[uNodeIndex].byCipherSuite;
2002 			pTransmitKey->dwKeyIndex = pMgmt->sNodeDBTable[uNodeIndex].dwKeyIndex;
2003 			pTransmitKey->uKeyLength = pMgmt->sNodeDBTable[uNodeIndex].uWepKeyLength;
2004 			pTransmitKey->dwTSC47_16 = pMgmt->sNodeDBTable[uNodeIndex].dwTSC47_16;
2005 			pTransmitKey->wTSC15_0 = pMgmt->sNodeDBTable[uNodeIndex].wTSC15_0;
2006 			memcpy(pTransmitKey->abyKey,
2007 			       &pMgmt->sNodeDBTable[uNodeIndex].abyWepKey[0],
2008 			       pTransmitKey->uKeyLength
2009 				);
2010 		}
2011 	}
2012 
2013 	uMACfragNum = cbGetFragCount(pDevice, pTransmitKey, cbFrameBodySize, &pDevice->sTxEthHeader);
2014 
2015 	if (uMACfragNum > AVAIL_TD(pDevice, TYPE_AC0DMA)) {
2016 		pr_debug("uMACfragNum > AVAIL_TD(TYPE_AC0DMA) = %d\n",
2017 			 uMACfragNum);
2018 		dev_kfree_skb_irq(skb);
2019 		spin_unlock_irq(&pDevice->lock);
2020 		return 0;
2021 	}
2022 
2023 	if (pTransmitKey != NULL) {
2024 		if ((pTransmitKey->byCipherSuite == KEY_CTL_WEP) &&
2025 		    (pTransmitKey->uKeyLength == WLAN_WEP232_KEYLEN)) {
2026 			uMACfragNum = 1; //WEP256 doesn't support fragment
2027 		}
2028 	}
2029 
2030 	byPktType = (unsigned char)pDevice->byPacketType;
2031 
2032 	if (pDevice->bFixRate) {
2033 		if (pDevice->eCurrentPHYType == PHY_TYPE_11B) {
2034 			if (pDevice->uConnectionRate >= RATE_11M)
2035 				pDevice->wCurrentRate = RATE_11M;
2036 			else
2037 				pDevice->wCurrentRate = (unsigned short)pDevice->uConnectionRate;
2038 		} else {
2039 			if ((pDevice->eCurrentPHYType == PHY_TYPE_11A) &&
2040 			    (pDevice->uConnectionRate <= RATE_6M)) {
2041 				pDevice->wCurrentRate = RATE_6M;
2042 			} else {
2043 				if (pDevice->uConnectionRate >= RATE_54M)
2044 					pDevice->wCurrentRate = RATE_54M;
2045 				else
2046 					pDevice->wCurrentRate = (unsigned short)pDevice->uConnectionRate;
2047 
2048 			}
2049 		}
2050 		pDevice->byACKRate = (unsigned char) pDevice->wCurrentRate;
2051 		pDevice->byTopCCKBasicRate = RATE_1M;
2052 		pDevice->byTopOFDMBasicRate = RATE_6M;
2053 	} else {
2054 		//auto rate
2055 		if (pDevice->sTxEthHeader.wType == TYPE_PKT_802_1x) {
2056 			if (pDevice->eCurrentPHYType != PHY_TYPE_11A) {
2057 				pDevice->wCurrentRate = RATE_1M;
2058 				pDevice->byACKRate = RATE_1M;
2059 				pDevice->byTopCCKBasicRate = RATE_1M;
2060 				pDevice->byTopOFDMBasicRate = RATE_6M;
2061 			} else {
2062 				pDevice->wCurrentRate = RATE_6M;
2063 				pDevice->byACKRate = RATE_6M;
2064 				pDevice->byTopCCKBasicRate = RATE_1M;
2065 				pDevice->byTopOFDMBasicRate = RATE_6M;
2066 			}
2067 		} else {
2068 			VNTWIFIvGetTxRate(pDevice->pMgmt,
2069 					  pDevice->sTxEthHeader.abyDstAddr,
2070 					  &(pDevice->wCurrentRate),
2071 					  &(pDevice->byACKRate),
2072 					  &(pDevice->byTopCCKBasicRate),
2073 					  &(pDevice->byTopOFDMBasicRate));
2074 
2075 		}
2076 	}
2077 
2078 
2079 	if (pDevice->wCurrentRate <= RATE_11M) {
2080 		byPktType = PK_TYPE_11B;
2081 	} else if (pDevice->eCurrentPHYType == PHY_TYPE_11A) {
2082 		byPktType = PK_TYPE_11A;
2083 	} else {
2084 		if (pDevice->bProtectMode)
2085 			byPktType = PK_TYPE_11GB;
2086 		else
2087 			byPktType = PK_TYPE_11GA;
2088 	}
2089 
2090 	if (bNeedEncryption) {
2091 		pr_debug("ntohs Pkt Type=%04x\n",
2092 			 ntohs(pDevice->sTxEthHeader.wType));
2093 		if ((pDevice->sTxEthHeader.wType) == TYPE_PKT_802_1x) {
2094 			bNeedEncryption = false;
2095 			pr_debug("Pkt Type=%04x\n",
2096 				 (pDevice->sTxEthHeader.wType));
2097 			if ((pDevice->pMgmt->eCurrMode == WMAC_MODE_ESS_STA) && (pDevice->pMgmt->eCurrState == WMAC_STATE_ASSOC)) {
2098 				if (pTransmitKey == NULL) {
2099 					pr_debug("Don't Find TX KEY\n");
2100 				} else {
2101 					if (bTKIP_UseGTK) {
2102 						pr_debug("error: KEY is GTK!!~~\n");
2103 					} else {
2104 						pr_debug("Find PTK [%lX]\n",
2105 							 pTransmitKey->dwKeyIndex);
2106 						bNeedEncryption = true;
2107 					}
2108 				}
2109 			}
2110 
2111 			if (pDevice->byCntMeasure == 2) {
2112 				bNeedDeAuth = true;
2113 				pDevice->s802_11Counter.TKIPCounterMeasuresInvoked++;
2114 			}
2115 
2116 			if (pDevice->bEnableHostWEP) {
2117 				if ((uNodeIndex != 0) &&
2118 				    (pMgmt->sNodeDBTable[uNodeIndex].dwKeyIndex & PAIRWISE_KEY)) {
2119 					pr_debug("Find PTK [%lX]\n",
2120 						 pTransmitKey->dwKeyIndex);
2121 					bNeedEncryption = true;
2122 				}
2123 			}
2124 		} else {
2125 			if (pTransmitKey == NULL) {
2126 				pr_debug("return no tx key\n");
2127 				dev_kfree_skb_irq(skb);
2128 				spin_unlock_irq(&pDevice->lock);
2129 				return 0;
2130 			}
2131 		}
2132 	}
2133 
2134 	vGenerateFIFOHeader(pDevice, byPktType, pDevice->pbyTmpBuff, bNeedEncryption,
2135 			    cbFrameBodySize, TYPE_AC0DMA, pHeadTD,
2136 			    &pDevice->sTxEthHeader, (unsigned char *)skb->data, pTransmitKey, uNodeIndex,
2137 			    &uMACfragNum,
2138 			    &cbHeaderSize
2139 		);
2140 
2141 	if (MACbIsRegBitsOn(pDevice->PortOffset, MAC_REG_PSCTL, PSCTL_PS)) {
2142 		// Disable PS
2143 		MACbPSWakeup(pDevice->PortOffset);
2144 	}
2145 	pDevice->bPWBitOn = false;
2146 
2147 	pLastTD = pHeadTD;
2148 	for (ii = 0; ii < uMACfragNum; ii++) {
2149 		// Poll Transmit the adapter
2150 		wmb();
2151 		pHeadTD->m_td0TD0.f1Owner = OWNED_BY_NIC;
2152 		wmb();
2153 		if (ii == uMACfragNum - 1)
2154 			pLastTD = pHeadTD;
2155 		pHeadTD = pHeadTD->next;
2156 	}
2157 
2158 	// Save the information needed by the tx interrupt handler
2159 	// to complete the Send request
2160 	pLastTD->pTDInfo->skb = skb;
2161 	pLastTD->pTDInfo->byFlags = 0;
2162 	pLastTD->pTDInfo->byFlags |= TD_FLAGS_NETIF_SKB;
2163 	pDevice->nTxDataTimeCout = 0; //2008-8-21 chester <add> for send null packet
2164 
2165 	if (AVAIL_TD(pDevice, TYPE_AC0DMA) <= 1)
2166 		netif_stop_queue(dev);
2167 
2168 	pDevice->apCurrTD[TYPE_AC0DMA] = pHeadTD;
2169 
2170 	if (pDevice->bFixRate)
2171 		pr_debug("FixRate:Rate is %d,TxPower is %d\n", pDevice->wCurrentRate, pDevice->byCurPwr);
2172 
2173 	{
2174 		unsigned char Protocol_Version;    //802.1x Authentication
2175 		unsigned char Packet_Type;           //802.1x Authentication
2176 		unsigned char Descriptor_type;
2177 		unsigned short Key_info;
2178 		bool bTxeapol_key = false;
2179 
2180 		Protocol_Version = skb->data[ETH_HLEN];
2181 		Packet_Type = skb->data[ETH_HLEN+1];
2182 		Descriptor_type = skb->data[ETH_HLEN+1+1+2];
2183 		Key_info = (skb->data[ETH_HLEN+1+1+2+1] << 8)|(skb->data[ETH_HLEN+1+1+2+2]);
2184 		if (pDevice->sTxEthHeader.wType == TYPE_PKT_802_1x) {
2185 			if (((Protocol_Version == 1) || (Protocol_Version == 2)) &&
2186 			    (Packet_Type == 3)) {  //802.1x OR eapol-key challenge frame transfer
2187 				bTxeapol_key = true;
2188 				if ((Descriptor_type == 254) || (Descriptor_type == 2)) {       //WPA or RSN
2189 					if (!(Key_info & BIT3) &&   //group-key challenge
2190 					    (Key_info & BIT8) && (Key_info & BIT9)) {    //send 2/2 key
2191 						pDevice->fWPA_Authened = true;
2192 						if (Descriptor_type == 254)
2193 							pr_debug("WPA ");
2194 						else
2195 							pr_debug("WPA2 ");
2196 						pr_debug("Authentication completed!!\n");
2197 					}
2198 				}
2199 			}
2200 		}
2201 	}
2202 
2203 	MACvTransmitAC0(pDevice->PortOffset);
2204 
2205 	dev->trans_start = jiffies;
2206 
2207 	spin_unlock_irq(&pDevice->lock);
2208 	return 0;
2209 }
2210 
device_intr(int irq,void * dev_instance)2211 static  irqreturn_t  device_intr(int irq,  void *dev_instance)
2212 {
2213 	struct net_device *dev = dev_instance;
2214 	struct vnt_private *pDevice = netdev_priv(dev);
2215 	int             max_count = 0;
2216 	unsigned long dwMIBCounter = 0;
2217 	PSMgmtObject    pMgmt = pDevice->pMgmt;
2218 	unsigned char byOrgPageSel = 0;
2219 	int             handled = 0;
2220 	unsigned char byData = 0;
2221 	int             ii = 0;
2222 	unsigned long flags;
2223 
2224 	MACvReadISR(pDevice->PortOffset, &pDevice->dwIsr);
2225 
2226 	if (pDevice->dwIsr == 0)
2227 		return IRQ_RETVAL(handled);
2228 
2229 	if (pDevice->dwIsr == 0xffffffff) {
2230 		pr_debug("dwIsr = 0xffff\n");
2231 		return IRQ_RETVAL(handled);
2232 	}
2233 
2234 	handled = 1;
2235 	MACvIntDisable(pDevice->PortOffset);
2236 
2237 	spin_lock_irqsave(&pDevice->lock, flags);
2238 
2239 	//Make sure current page is 0
2240 	VNSvInPortB(pDevice->PortOffset + MAC_REG_PAGE1SEL, &byOrgPageSel);
2241 	if (byOrgPageSel == 1)
2242 		MACvSelectPage0(pDevice->PortOffset);
2243 	else
2244 		byOrgPageSel = 0;
2245 
2246 	MACvReadMIBCounter(pDevice->PortOffset, &dwMIBCounter);
2247 	// TBD....
2248 	// Must do this after doing rx/tx, cause ISR bit is slow
2249 	// than RD/TD write back
2250 	// update ISR counter
2251 	STAvUpdate802_11Counter(&pDevice->s802_11Counter, &pDevice->scStatistic , dwMIBCounter);
2252 	while (pDevice->dwIsr != 0) {
2253 		STAvUpdateIsrStatCounter(&pDevice->scStatistic, pDevice->dwIsr);
2254 		MACvWriteISR(pDevice->PortOffset, pDevice->dwIsr);
2255 
2256 		if (pDevice->dwIsr & ISR_FETALERR) {
2257 			pr_debug(" ISR_FETALERR\n");
2258 			VNSvOutPortB(pDevice->PortOffset + MAC_REG_SOFTPWRCTL, 0);
2259 			VNSvOutPortW(pDevice->PortOffset + MAC_REG_SOFTPWRCTL, SOFTPWRCTL_SWPECTI);
2260 			device_error(pDevice, pDevice->dwIsr);
2261 		}
2262 
2263 		if (pDevice->byLocalID > REV_ID_VT3253_B1) {
2264 			if (pDevice->dwIsr & ISR_MEASURESTART) {
2265 				// 802.11h measure start
2266 				pDevice->byOrgChannel = pDevice->byCurrentCh;
2267 				VNSvInPortB(pDevice->PortOffset + MAC_REG_RCR, &(pDevice->byOrgRCR));
2268 				VNSvOutPortB(pDevice->PortOffset + MAC_REG_RCR, (RCR_RXALLTYPE | RCR_UNICAST | RCR_BROADCAST | RCR_MULTICAST | RCR_WPAERR));
2269 				MACvSelectPage1(pDevice->PortOffset);
2270 				VNSvInPortD(pDevice->PortOffset + MAC_REG_MAR0, &(pDevice->dwOrgMAR0));
2271 				VNSvInPortD(pDevice->PortOffset + MAC_REG_MAR4, &(pDevice->dwOrgMAR4));
2272 				MACvSelectPage0(pDevice->PortOffset);
2273 				//xxxx
2274 				if (set_channel(pDevice, pDevice->pCurrMeasureEID->sReq.byChannel)) {
2275 					pDevice->bMeasureInProgress = true;
2276 					MACvSelectPage1(pDevice->PortOffset);
2277 					MACvRegBitsOn(pDevice->PortOffset, MAC_REG_MSRCTL, MSRCTL_READY);
2278 					MACvSelectPage0(pDevice->PortOffset);
2279 					pDevice->byBasicMap = 0;
2280 					pDevice->byCCAFraction = 0;
2281 					for (ii = 0; ii < 8; ii++)
2282 						pDevice->dwRPIs[ii] = 0;
2283 
2284 				} else {
2285 					// can not measure because set channel fail
2286 					// clear measure control
2287 					MACvRegBitsOff(pDevice->PortOffset, MAC_REG_MSRCTL, MSRCTL_EN);
2288 					s_vCompleteCurrentMeasure(pDevice, MEASURE_MODE_INCAPABLE);
2289 					MACvSelectPage1(pDevice->PortOffset);
2290 					MACvRegBitsOn(pDevice->PortOffset, MAC_REG_MSRCTL+1, MSRCTL1_TXPAUSE);
2291 					MACvSelectPage0(pDevice->PortOffset);
2292 				}
2293 			}
2294 			if (pDevice->dwIsr & ISR_MEASUREEND) {
2295 				// 802.11h measure end
2296 				pDevice->bMeasureInProgress = false;
2297 				VNSvOutPortB(pDevice->PortOffset + MAC_REG_RCR, pDevice->byOrgRCR);
2298 				MACvSelectPage1(pDevice->PortOffset);
2299 				VNSvOutPortD(pDevice->PortOffset + MAC_REG_MAR0, pDevice->dwOrgMAR0);
2300 				VNSvOutPortD(pDevice->PortOffset + MAC_REG_MAR4, pDevice->dwOrgMAR4);
2301 				VNSvInPortB(pDevice->PortOffset + MAC_REG_MSRBBSTS, &byData);
2302 				pDevice->byBasicMap |= (byData >> 4);
2303 				VNSvInPortB(pDevice->PortOffset + MAC_REG_CCAFRACTION, &pDevice->byCCAFraction);
2304 				VNSvInPortB(pDevice->PortOffset + MAC_REG_MSRCTL, &byData);
2305 				// clear measure control
2306 				MACvRegBitsOff(pDevice->PortOffset, MAC_REG_MSRCTL, MSRCTL_EN);
2307 				MACvSelectPage0(pDevice->PortOffset);
2308 				set_channel(pDevice, pDevice->byOrgChannel);
2309 				MACvSelectPage1(pDevice->PortOffset);
2310 				MACvRegBitsOn(pDevice->PortOffset, MAC_REG_MSRCTL+1, MSRCTL1_TXPAUSE);
2311 				MACvSelectPage0(pDevice->PortOffset);
2312 				if (byData & MSRCTL_FINISH) {
2313 					// measure success
2314 					s_vCompleteCurrentMeasure(pDevice, 0);
2315 				} else {
2316 					// can not measure because not ready before end of measure time
2317 					s_vCompleteCurrentMeasure(pDevice, MEASURE_MODE_LATE);
2318 				}
2319 			}
2320 			if (pDevice->dwIsr & ISR_QUIETSTART) {
2321 				do {
2322 					;
2323 				} while (!CARDbStartQuiet(pDevice));
2324 			}
2325 		}
2326 
2327 		if (pDevice->dwIsr & ISR_TBTT) {
2328 			if (pDevice->bEnableFirstQuiet) {
2329 				pDevice->byQuietStartCount--;
2330 				if (pDevice->byQuietStartCount == 0) {
2331 					pDevice->bEnableFirstQuiet = false;
2332 					MACvSelectPage1(pDevice->PortOffset);
2333 					MACvRegBitsOn(pDevice->PortOffset, MAC_REG_MSRCTL, (MSRCTL_QUIETTXCHK | MSRCTL_QUIETEN));
2334 					MACvSelectPage0(pDevice->PortOffset);
2335 				}
2336 			}
2337 			if (pDevice->bChannelSwitch &&
2338 			    (pDevice->op_mode == NL80211_IFTYPE_STATION)) {
2339 				pDevice->byChannelSwitchCount--;
2340 				if (pDevice->byChannelSwitchCount == 0) {
2341 					pDevice->bChannelSwitch = false;
2342 					set_channel(pDevice, pDevice->byNewChannel);
2343 					VNTWIFIbChannelSwitch(pDevice->pMgmt, pDevice->byNewChannel);
2344 					MACvSelectPage1(pDevice->PortOffset);
2345 					MACvRegBitsOn(pDevice->PortOffset, MAC_REG_MSRCTL+1, MSRCTL1_TXPAUSE);
2346 					MACvSelectPage0(pDevice->PortOffset);
2347 					CARDbStartTxPacket(pDevice, PKT_TYPE_802_11_ALL);
2348 
2349 				}
2350 			}
2351 			if (pDevice->op_mode != NL80211_IFTYPE_ADHOC) {
2352 				if ((pDevice->bUpdateBBVGA) && pDevice->bLinkPass && (pDevice->uCurrRSSI != 0)) {
2353 					long            ldBm;
2354 
2355 					RFvRSSITodBm(pDevice, (unsigned char) pDevice->uCurrRSSI, &ldBm);
2356 					for (ii = 0; ii < BB_VGA_LEVEL; ii++) {
2357 						if (ldBm < pDevice->ldBmThreshold[ii]) {
2358 							pDevice->byBBVGANew = pDevice->abyBBVGA[ii];
2359 							break;
2360 						}
2361 					}
2362 					if (pDevice->byBBVGANew != pDevice->byBBVGACurrent) {
2363 						pDevice->uBBVGADiffCount++;
2364 						if (pDevice->uBBVGADiffCount == 1) {
2365 							// first VGA diff gain
2366 							BBvSetVGAGainOffset(pDevice, pDevice->byBBVGANew);
2367 							pr_debug("First RSSI[%d] NewGain[%d] OldGain[%d] Count[%d]\n",
2368 								 (int)ldBm,
2369 								 pDevice->byBBVGANew,
2370 								 pDevice->byBBVGACurrent,
2371 								 (int)pDevice->uBBVGADiffCount);
2372 						}
2373 						if (pDevice->uBBVGADiffCount >= BB_VGA_CHANGE_THRESHOLD) {
2374 							pr_debug("RSSI[%d] NewGain[%d] OldGain[%d] Count[%d]\n",
2375 								 (int)ldBm,
2376 								 pDevice->byBBVGANew,
2377 								 pDevice->byBBVGACurrent,
2378 								 (int)pDevice->uBBVGADiffCount);
2379 							BBvSetVGAGainOffset(pDevice, pDevice->byBBVGANew);
2380 						}
2381 					} else {
2382 						pDevice->uBBVGADiffCount = 1;
2383 					}
2384 				}
2385 			}
2386 
2387 			pDevice->bBeaconSent = false;
2388 			if (pDevice->bEnablePSMode)
2389 				PSbIsNextTBTTWakeUp((void *)pDevice);
2390 
2391 			if ((pDevice->op_mode == NL80211_IFTYPE_AP) ||
2392 			    (pDevice->op_mode == NL80211_IFTYPE_ADHOC)) {
2393 				MACvOneShotTimer1MicroSec(pDevice->PortOffset,
2394 							  (pMgmt->wIBSSBeaconPeriod - MAKE_BEACON_RESERVED) << 10);
2395 			}
2396 
2397 			/* TODO: adhoc PS mode */
2398 
2399 		}
2400 
2401 		if (pDevice->dwIsr & ISR_BNTX) {
2402 			if (pDevice->op_mode == NL80211_IFTYPE_ADHOC) {
2403 				pDevice->bIsBeaconBufReadySet = false;
2404 				pDevice->cbBeaconBufReadySetCnt = 0;
2405 			}
2406 
2407 			if (pDevice->op_mode == NL80211_IFTYPE_AP) {
2408 				if (pMgmt->byDTIMCount > 0) {
2409 					pMgmt->byDTIMCount--;
2410 					pMgmt->sNodeDBTable[0].bRxPSPoll = false;
2411 				} else {
2412 					if (pMgmt->byDTIMCount == 0) {
2413 						// check if mutltcast tx bufferring
2414 						pMgmt->byDTIMCount = pMgmt->byDTIMPeriod - 1;
2415 						pMgmt->sNodeDBTable[0].bRxPSPoll = true;
2416 						bScheduleCommand((void *)pDevice, WLAN_CMD_RX_PSPOLL, NULL);
2417 					}
2418 				}
2419 			}
2420 			pDevice->bBeaconSent = true;
2421 
2422 			if (pDevice->bChannelSwitch) {
2423 				pDevice->byChannelSwitchCount--;
2424 				if (pDevice->byChannelSwitchCount == 0) {
2425 					pDevice->bChannelSwitch = false;
2426 					set_channel(pDevice, pDevice->byNewChannel);
2427 					VNTWIFIbChannelSwitch(pDevice->pMgmt, pDevice->byNewChannel);
2428 					MACvSelectPage1(pDevice->PortOffset);
2429 					MACvRegBitsOn(pDevice->PortOffset, MAC_REG_MSRCTL+1, MSRCTL1_TXPAUSE);
2430 					MACvSelectPage0(pDevice->PortOffset);
2431 					CARDbStartTxPacket(pDevice, PKT_TYPE_802_11_ALL);
2432 				}
2433 			}
2434 
2435 		}
2436 
2437 		if (pDevice->dwIsr & ISR_RXDMA0)
2438 			max_count += device_rx_srv(pDevice, TYPE_RXDMA0);
2439 
2440 		if (pDevice->dwIsr & ISR_RXDMA1)
2441 			max_count += device_rx_srv(pDevice, TYPE_RXDMA1);
2442 
2443 		if (pDevice->dwIsr & ISR_TXDMA0)
2444 			max_count += device_tx_srv(pDevice, TYPE_TXDMA0);
2445 
2446 		if (pDevice->dwIsr & ISR_AC0DMA)
2447 			max_count += device_tx_srv(pDevice, TYPE_AC0DMA);
2448 
2449 		if (pDevice->dwIsr & ISR_SOFTTIMER1) {
2450 			if (pDevice->op_mode == NL80211_IFTYPE_AP) {
2451 				if (pDevice->bShortSlotTime)
2452 					pMgmt->wCurrCapInfo |= WLAN_SET_CAP_INFO_SHORTSLOTTIME(1);
2453 				else
2454 					pMgmt->wCurrCapInfo &= ~(WLAN_SET_CAP_INFO_SHORTSLOTTIME(1));
2455 			}
2456 			bMgrPrepareBeaconToSend(pDevice, pMgmt);
2457 			pDevice->byCntMeasure = 0;
2458 		}
2459 
2460 		MACvReadISR(pDevice->PortOffset, &pDevice->dwIsr);
2461 
2462 		MACvReceive0(pDevice->PortOffset);
2463 		MACvReceive1(pDevice->PortOffset);
2464 
2465 		if (max_count > pDevice->sOpts.int_works)
2466 			break;
2467 	}
2468 
2469 	if (byOrgPageSel == 1)
2470 		MACvSelectPage1(pDevice->PortOffset);
2471 
2472 	spin_unlock_irqrestore(&pDevice->lock, flags);
2473 
2474 	MACvIntEnable(pDevice->PortOffset, IMR_MASK_VALUE);
2475 
2476 	return IRQ_RETVAL(handled);
2477 }
2478 
2479 //2008-8-4 <add> by chester
Config_FileGetParameter(unsigned char * string,unsigned char * dest,unsigned char * source)2480 static int Config_FileGetParameter(unsigned char *string,
2481 				   unsigned char *dest, unsigned char *source)
2482 {
2483 	unsigned char buf1[100];
2484 	int source_len = strlen(source);
2485 
2486 	memset(buf1, 0, 100);
2487 	strcat(buf1, string);
2488 	strcat(buf1, "=");
2489 	source += strlen(buf1);
2490 
2491 	memcpy(dest, source, source_len - strlen(buf1));
2492 	return true;
2493 }
2494 
Config_FileOperation(struct vnt_private * pDevice,bool fwrite,unsigned char * Parameter)2495 int Config_FileOperation(struct vnt_private *pDevice,
2496 			 bool fwrite, unsigned char *Parameter)
2497 {
2498 	unsigned char *buffer = kmalloc(1024, GFP_KERNEL);
2499 	unsigned char tmpbuffer[20];
2500 	struct file *file;
2501 	int result = 0;
2502 
2503 	if (!buffer) {
2504 		pr_err("allocate mem for file fail?\n");
2505 		return -1;
2506 	}
2507 	file = filp_open(CONFIG_PATH, O_RDONLY, 0);
2508 	if (IS_ERR(file)) {
2509 		kfree(buffer);
2510 		pr_err("Config_FileOperation:open file fail?\n");
2511 		return -1;
2512 	}
2513 
2514 	if (kernel_read(file, 0, buffer, 1024) < 0) {
2515 		pr_err("read file error?\n");
2516 		result = -1;
2517 		goto error1;
2518 	}
2519 
2520 	if (Config_FileGetParameter("ZONETYPE", tmpbuffer, buffer) != true) {
2521 		pr_err("get parameter error?\n");
2522 		result = -1;
2523 		goto error1;
2524 	}
2525 
2526 	if (memcmp(tmpbuffer, "USA", 3) == 0) {
2527 		result = ZoneType_USA;
2528 	} else if (memcmp(tmpbuffer, "JAPAN", 5) == 0) {
2529 		result = ZoneType_Japan;
2530 	} else if (memcmp(tmpbuffer, "EUROPE", 5) == 0) {
2531 		result = ZoneType_Europe;
2532 	} else {
2533 		result = -1;
2534 		pr_err("Unknown Zonetype[%s]?\n", tmpbuffer);
2535 	}
2536 
2537 error1:
2538 	kfree(buffer);
2539 	fput(file);
2540 	return result;
2541 }
2542 
device_set_multi(struct net_device * dev)2543 static void device_set_multi(struct net_device *dev) {
2544 	struct vnt_private *pDevice = netdev_priv(dev);
2545 	PSMgmtObject     pMgmt = pDevice->pMgmt;
2546 	u32              mc_filter[2];
2547 	struct netdev_hw_addr *ha;
2548 
2549 	VNSvInPortB(pDevice->PortOffset + MAC_REG_RCR, &(pDevice->byRxMode));
2550 
2551 	if (dev->flags & IFF_PROMISC) {         /* Set promiscuous. */
2552 		pr_notice("%s: Promiscuous mode enabled\n", dev->name);
2553 		/* Unconditionally log net taps. */
2554 		pDevice->byRxMode |= (RCR_MULTICAST|RCR_BROADCAST|RCR_UNICAST);
2555 	} else if ((netdev_mc_count(dev) > pDevice->multicast_limit)
2556 		 ||  (dev->flags & IFF_ALLMULTI)) {
2557 		MACvSelectPage1(pDevice->PortOffset);
2558 		VNSvOutPortD(pDevice->PortOffset + MAC_REG_MAR0, 0xffffffff);
2559 		VNSvOutPortD(pDevice->PortOffset + MAC_REG_MAR0 + 4, 0xffffffff);
2560 		MACvSelectPage0(pDevice->PortOffset);
2561 		pDevice->byRxMode |= (RCR_MULTICAST|RCR_BROADCAST);
2562 	} else {
2563 		memset(mc_filter, 0, sizeof(mc_filter));
2564 		netdev_for_each_mc_addr(ha, dev) {
2565 			int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
2566 
2567 			mc_filter[bit_nr >> 5] |= cpu_to_le32(1 << (bit_nr & 31));
2568 		}
2569 		MACvSelectPage1(pDevice->PortOffset);
2570 		VNSvOutPortD(pDevice->PortOffset + MAC_REG_MAR0, mc_filter[0]);
2571 		VNSvOutPortD(pDevice->PortOffset + MAC_REG_MAR0 + 4, mc_filter[1]);
2572 		MACvSelectPage0(pDevice->PortOffset);
2573 		pDevice->byRxMode &= ~(RCR_UNICAST);
2574 		pDevice->byRxMode |= (RCR_MULTICAST|RCR_BROADCAST);
2575 	}
2576 
2577 	if (pMgmt->eConfigMode == WMAC_CONFIG_AP) {
2578 		// If AP mode, don't enable RCR_UNICAST. Since hw only compare addr1 with local mac.
2579 		pDevice->byRxMode |= (RCR_MULTICAST|RCR_BROADCAST);
2580 		pDevice->byRxMode &= ~(RCR_UNICAST);
2581 	}
2582 
2583 	VNSvOutPortB(pDevice->PortOffset + MAC_REG_RCR, pDevice->byRxMode);
2584 	pr_debug("pDevice->byRxMode = %x\n", pDevice->byRxMode);
2585 }
2586 
device_ioctl(struct net_device * dev,struct ifreq * rq,int cmd)2587 static int  device_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2588 {
2589 	struct vnt_private *pDevice = netdev_priv(dev);
2590 	struct iwreq *wrq = (struct iwreq *)rq;
2591 	int rc = 0;
2592 	PSMgmtObject pMgmt = pDevice->pMgmt;
2593 	PSCmdRequest pReq;
2594 
2595 	if (pMgmt == NULL) {
2596 		rc = -EFAULT;
2597 		return rc;
2598 	}
2599 
2600 	switch (cmd) {
2601 	case SIOCGIWNAME:
2602 		rc = iwctl_giwname(dev, NULL, (char *)&(wrq->u.name), NULL);
2603 		break;
2604 
2605 	case SIOCGIWNWID:     //0x8b03  support
2606 		rc = -EOPNOTSUPP;
2607 		break;
2608 
2609 		// Set frequency/channel
2610 	case SIOCSIWFREQ:
2611 		rc = iwctl_siwfreq(dev, NULL, &(wrq->u.freq), NULL);
2612 		break;
2613 
2614 		// Get frequency/channel
2615 	case SIOCGIWFREQ:
2616 		rc = iwctl_giwfreq(dev, NULL, &(wrq->u.freq), NULL);
2617 		break;
2618 
2619 		// Set desired network name (ESSID)
2620 	case SIOCSIWESSID:
2621 
2622 	{
2623 		char essid[IW_ESSID_MAX_SIZE+1];
2624 
2625 		if (wrq->u.essid.length > IW_ESSID_MAX_SIZE) {
2626 			rc = -E2BIG;
2627 			break;
2628 		}
2629 		if (copy_from_user(essid, wrq->u.essid.pointer,
2630 				   wrq->u.essid.length)) {
2631 			rc = -EFAULT;
2632 			break;
2633 		}
2634 		rc = iwctl_siwessid(dev, NULL,
2635 				    &(wrq->u.essid), essid);
2636 	}
2637 	break;
2638 
2639 	// Get current network name (ESSID)
2640 	case SIOCGIWESSID:
2641 
2642 	{
2643 		char essid[IW_ESSID_MAX_SIZE+1];
2644 
2645 		if (wrq->u.essid.pointer)
2646 			rc = iwctl_giwessid(dev, NULL,
2647 					    &(wrq->u.essid), essid);
2648 		if (copy_to_user(wrq->u.essid.pointer,
2649 				 essid,
2650 				 wrq->u.essid.length))
2651 			rc = -EFAULT;
2652 	}
2653 	break;
2654 
2655 	case SIOCSIWAP:
2656 
2657 		rc = iwctl_siwap(dev, NULL, &(wrq->u.ap_addr), NULL);
2658 		break;
2659 
2660 		// Get current Access Point (BSSID)
2661 	case SIOCGIWAP:
2662 		rc = iwctl_giwap(dev, NULL, &(wrq->u.ap_addr), NULL);
2663 		break;
2664 
2665 		// Set desired station name
2666 	case SIOCSIWNICKN:
2667 		pr_debug(" SIOCSIWNICKN\n");
2668 		rc = -EOPNOTSUPP;
2669 		break;
2670 
2671 		// Get current station name
2672 	case SIOCGIWNICKN:
2673 		pr_debug(" SIOCGIWNICKN\n");
2674 		rc = -EOPNOTSUPP;
2675 		break;
2676 
2677 		// Set the desired bit-rate
2678 	case SIOCSIWRATE:
2679 		rc = iwctl_siwrate(dev, NULL, &(wrq->u.bitrate), NULL);
2680 		break;
2681 
2682 		// Get the current bit-rate
2683 	case SIOCGIWRATE:
2684 
2685 		rc = iwctl_giwrate(dev, NULL, &(wrq->u.bitrate), NULL);
2686 		break;
2687 
2688 		// Set the desired RTS threshold
2689 	case SIOCSIWRTS:
2690 
2691 		rc = iwctl_siwrts(dev, NULL, &(wrq->u.rts), NULL);
2692 		break;
2693 
2694 		// Get the current RTS threshold
2695 	case SIOCGIWRTS:
2696 
2697 		rc = iwctl_giwrts(dev, NULL, &(wrq->u.rts), NULL);
2698 		break;
2699 
2700 		// Set the desired fragmentation threshold
2701 	case SIOCSIWFRAG:
2702 
2703 		rc = iwctl_siwfrag(dev, NULL, &(wrq->u.frag), NULL);
2704 		break;
2705 
2706 		// Get the current fragmentation threshold
2707 	case SIOCGIWFRAG:
2708 
2709 		rc = iwctl_giwfrag(dev, NULL, &(wrq->u.frag), NULL);
2710 		break;
2711 
2712 		// Set mode of operation
2713 	case SIOCSIWMODE:
2714 		rc = iwctl_siwmode(dev, NULL, &(wrq->u.mode), NULL);
2715 		break;
2716 
2717 		// Get mode of operation
2718 	case SIOCGIWMODE:
2719 		rc = iwctl_giwmode(dev, NULL, &(wrq->u.mode), NULL);
2720 		break;
2721 
2722 		// Set WEP keys and mode
2723 	case SIOCSIWENCODE: {
2724 		char abyKey[WLAN_WEP232_KEYLEN];
2725 
2726 		if (wrq->u.encoding.pointer) {
2727 			if (wrq->u.encoding.length > WLAN_WEP232_KEYLEN) {
2728 				rc = -E2BIG;
2729 				break;
2730 			}
2731 			memset(abyKey, 0, WLAN_WEP232_KEYLEN);
2732 			if (copy_from_user(abyKey,
2733 					   wrq->u.encoding.pointer,
2734 					   wrq->u.encoding.length)) {
2735 				rc = -EFAULT;
2736 				break;
2737 			}
2738 		} else if (wrq->u.encoding.length != 0) {
2739 			rc = -EINVAL;
2740 			break;
2741 		}
2742 		rc = iwctl_siwencode(dev, NULL, &(wrq->u.encoding), abyKey);
2743 	}
2744 	break;
2745 
2746 	// Get the WEP keys and mode
2747 	case SIOCGIWENCODE:
2748 
2749 		if (!capable(CAP_NET_ADMIN)) {
2750 			rc = -EPERM;
2751 			break;
2752 		}
2753 		{
2754 			char abyKey[WLAN_WEP232_KEYLEN];
2755 
2756 			rc = iwctl_giwencode(dev, NULL, &(wrq->u.encoding), abyKey);
2757 			if (rc != 0)
2758 				break;
2759 			if (wrq->u.encoding.pointer) {
2760 				if (copy_to_user(wrq->u.encoding.pointer,
2761 						 abyKey,
2762 						 wrq->u.encoding.length))
2763 					rc = -EFAULT;
2764 			}
2765 		}
2766 		break;
2767 
2768 		// Get the current Tx-Power
2769 	case SIOCGIWTXPOW:
2770 		pr_debug(" SIOCGIWTXPOW\n");
2771 		rc = -EOPNOTSUPP;
2772 		break;
2773 
2774 	case SIOCSIWTXPOW:
2775 		pr_debug(" SIOCSIWTXPOW\n");
2776 		rc = -EOPNOTSUPP;
2777 		break;
2778 
2779 	case SIOCSIWRETRY:
2780 
2781 		rc = iwctl_siwretry(dev, NULL, &(wrq->u.retry), NULL);
2782 		break;
2783 
2784 	case SIOCGIWRETRY:
2785 
2786 		rc = iwctl_giwretry(dev, NULL, &(wrq->u.retry), NULL);
2787 		break;
2788 
2789 		// Get range of parameters
2790 	case SIOCGIWRANGE:
2791 
2792 	{
2793 		struct iw_range range;
2794 
2795 		rc = iwctl_giwrange(dev, NULL, &(wrq->u.data), (char *)&range);
2796 		if (copy_to_user(wrq->u.data.pointer, &range, sizeof(struct iw_range)))
2797 			rc = -EFAULT;
2798 	}
2799 
2800 	break;
2801 
2802 	case SIOCGIWPOWER:
2803 
2804 		rc = iwctl_giwpower(dev, NULL, &(wrq->u.power), NULL);
2805 		break;
2806 
2807 	case SIOCSIWPOWER:
2808 
2809 		rc = iwctl_siwpower(dev, NULL, &(wrq->u.power), NULL);
2810 		break;
2811 
2812 	case SIOCGIWSENS:
2813 
2814 		rc = iwctl_giwsens(dev, NULL, &(wrq->u.sens), NULL);
2815 		break;
2816 
2817 	case SIOCSIWSENS:
2818 		pr_debug(" SIOCSIWSENS\n");
2819 		rc = -EOPNOTSUPP;
2820 		break;
2821 	case SIOCGIWAPLIST: {
2822 		char *buffer = kzalloc(IW_MAX_AP * (sizeof(struct sockaddr) +
2823 				       sizeof(struct iw_quality)), GFP_KERNEL);
2824 
2825 		if (!buffer) {
2826 			rc = -ENOMEM;
2827 		} else if (wrq->u.data.pointer) {
2828 			rc = iwctl_giwaplist(dev, NULL, &(wrq->u.data), buffer);
2829 			if (rc == 0) {
2830 				if (copy_to_user(wrq->u.data.pointer,
2831 						 buffer,
2832 						 (wrq->u.data.length * (sizeof(struct sockaddr) +  sizeof(struct iw_quality)))
2833 					    ))
2834 					rc = -EFAULT;
2835 			}
2836 		}
2837 		kfree(buffer);
2838 	}
2839 	break;
2840 
2841 #ifdef WIRELESS_SPY
2842 	// Set the spy list
2843 	case SIOCSIWSPY:
2844 
2845 		pr_debug(" SIOCSIWSPY\n");
2846 		rc = -EOPNOTSUPP;
2847 		break;
2848 
2849 		// Get the spy list
2850 	case SIOCGIWSPY:
2851 
2852 		pr_debug(" SIOCGIWSPY\n");
2853 		rc = -EOPNOTSUPP;
2854 		break;
2855 
2856 #endif // WIRELESS_SPY
2857 
2858 	case SIOCGIWPRIV:
2859 		pr_debug(" SIOCGIWPRIV\n");
2860 		rc = -EOPNOTSUPP;
2861 		break;
2862 
2863 //2008-0409-07, <Add> by Einsn Liu
2864 #ifdef WPA_SUPPLICANT_DRIVER_WEXT_SUPPORT
2865 	case SIOCSIWAUTH:
2866 		pr_debug(" SIOCSIWAUTH\n");
2867 		rc = iwctl_siwauth(dev, NULL, &(wrq->u.param), NULL);
2868 		break;
2869 
2870 	case SIOCGIWAUTH:
2871 		pr_debug(" SIOCGIWAUTH\n");
2872 		rc = iwctl_giwauth(dev, NULL, &(wrq->u.param), NULL);
2873 		break;
2874 
2875 	case SIOCSIWGENIE:
2876 		pr_debug(" SIOCSIWGENIE\n");
2877 		rc = iwctl_siwgenie(dev, NULL, &(wrq->u.data), wrq->u.data.pointer);
2878 		break;
2879 
2880 	case SIOCGIWGENIE:
2881 		pr_debug(" SIOCGIWGENIE\n");
2882 		rc = iwctl_giwgenie(dev, NULL, &(wrq->u.data), wrq->u.data.pointer);
2883 		break;
2884 	case SIOCSIWENCODEEXT: {
2885 		char extra[sizeof(struct iw_encode_ext)+MAX_KEY_LEN+1];
2886 
2887 		pr_debug(" SIOCSIWENCODEEXT\n");
2888 		if (wrq->u.encoding.pointer) {
2889 			memset(extra, 0, sizeof(struct iw_encode_ext)+MAX_KEY_LEN + 1);
2890 			if (wrq->u.encoding.length > (sizeof(struct iw_encode_ext) + MAX_KEY_LEN)) {
2891 				rc = -E2BIG;
2892 				break;
2893 			}
2894 			if (copy_from_user(extra, wrq->u.encoding.pointer, wrq->u.encoding.length)) {
2895 				rc = -EFAULT;
2896 				break;
2897 			}
2898 		} else if (wrq->u.encoding.length != 0) {
2899 			rc = -EINVAL;
2900 			break;
2901 		}
2902 		rc = iwctl_siwencodeext(dev, NULL, &(wrq->u.encoding), extra);
2903 	}
2904 	break;
2905 
2906 	case SIOCGIWENCODEEXT:
2907 		pr_debug(" SIOCGIWENCODEEXT\n");
2908 		rc = iwctl_giwencodeext(dev, NULL, &(wrq->u.encoding), NULL);
2909 		break;
2910 
2911 	case SIOCSIWMLME:
2912 		pr_debug(" SIOCSIWMLME\n");
2913 		rc = iwctl_siwmlme(dev, NULL, &(wrq->u.data), wrq->u.data.pointer);
2914 		break;
2915 
2916 #endif // #ifdef WPA_SUPPLICANT_DRIVER_WEXT_SUPPORT
2917 //End Add -- //2008-0409-07, <Add> by Einsn Liu
2918 
2919 	case IOCTL_CMD_TEST:
2920 
2921 		if (!(pDevice->flags & DEVICE_FLAGS_OPENED)) {
2922 			rc = -EFAULT;
2923 			break;
2924 		} else {
2925 			rc = 0;
2926 		}
2927 		pReq = (PSCmdRequest)rq;
2928 		pReq->wResult = MAGIC_CODE;
2929 		break;
2930 
2931 	case IOCTL_CMD_SET:
2932 
2933 #ifdef SndEvt_ToAPI
2934 		if ((((PSCmdRequest)rq)->wCmdCode != WLAN_CMD_SET_EVT) &&
2935 		    !(pDevice->flags & DEVICE_FLAGS_OPENED))
2936 #else
2937 			if (!(pDevice->flags & DEVICE_FLAGS_OPENED) &&
2938 			    (((PSCmdRequest)rq)->wCmdCode != WLAN_CMD_SET_WPA))
2939 #endif
2940 			{
2941 				rc = -EFAULT;
2942 				break;
2943 			} else {
2944 				rc = 0;
2945 			}
2946 
2947 		if (test_and_set_bit(0, (void *)&(pMgmt->uCmdBusy)))
2948 			return -EBUSY;
2949 
2950 		rc = private_ioctl(pDevice, rq);
2951 		clear_bit(0, (void *)&(pMgmt->uCmdBusy));
2952 		break;
2953 
2954 	case IOCTL_CMD_HOSTAPD:
2955 
2956 		rc = vt6655_hostap_ioctl(pDevice, &wrq->u.data);
2957 		break;
2958 
2959 	case IOCTL_CMD_WPA:
2960 
2961 		rc = wpa_ioctl(pDevice, &wrq->u.data);
2962 		break;
2963 
2964 	case SIOCETHTOOL:
2965 		return ethtool_ioctl(dev, rq->ifr_data);
2966 		// All other calls are currently unsupported
2967 
2968 	default:
2969 		rc = -EOPNOTSUPP;
2970 		pr_debug("Ioctl command not support..%x\n", cmd);
2971 
2972 	}
2973 
2974 	if (pDevice->bCommit) {
2975 		if (pMgmt->eConfigMode == WMAC_CONFIG_AP) {
2976 			netif_stop_queue(pDevice->dev);
2977 			spin_lock_irq(&pDevice->lock);
2978 			bScheduleCommand((void *)pDevice, WLAN_CMD_RUN_AP, NULL);
2979 			spin_unlock_irq(&pDevice->lock);
2980 		} else {
2981 			pr_debug("Commit the settings\n");
2982 			spin_lock_irq(&pDevice->lock);
2983 			pDevice->bLinkPass = false;
2984 			memset(pMgmt->abyCurrBSSID, 0, 6);
2985 			pMgmt->eCurrState = WMAC_STATE_IDLE;
2986 			netif_stop_queue(pDevice->dev);
2987 #ifdef WPA_SUPPLICANT_DRIVER_WEXT_SUPPORT
2988 			pMgmt->eScanType = WMAC_SCAN_ACTIVE;
2989 			if (!pDevice->bWPASuppWextEnabled)
2990 #endif
2991 				bScheduleCommand((void *)pDevice, WLAN_CMD_BSSID_SCAN, pMgmt->abyDesireSSID);
2992 			bScheduleCommand((void *)pDevice, WLAN_CMD_SSID, NULL);
2993 			spin_unlock_irq(&pDevice->lock);
2994 		}
2995 		pDevice->bCommit = false;
2996 	}
2997 
2998 	return rc;
2999 }
3000 
ethtool_ioctl(struct net_device * dev,void __user * useraddr)3001 static int ethtool_ioctl(struct net_device *dev, void __user *useraddr)
3002 {
3003 	u32 ethcmd;
3004 
3005 	if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
3006 		return -EFAULT;
3007 
3008 	switch (ethcmd) {
3009 	case ETHTOOL_GDRVINFO: {
3010 		struct ethtool_drvinfo info = {ETHTOOL_GDRVINFO};
3011 
3012 		strncpy(info.driver, DEVICE_NAME, sizeof(info.driver)-1);
3013 		strncpy(info.version, DEVICE_VERSION, sizeof(info.version)-1);
3014 		if (copy_to_user(useraddr, &info, sizeof(info)))
3015 			return -EFAULT;
3016 		return 0;
3017 	}
3018 
3019 	}
3020 
3021 	return -EOPNOTSUPP;
3022 }
3023 
3024 /*------------------------------------------------------------------*/
3025 
3026 MODULE_DEVICE_TABLE(pci, vt6655_pci_id_table);
3027 
3028 static struct pci_driver device_driver = {
3029 	.name = DEVICE_NAME,
3030 	.id_table = vt6655_pci_id_table,
3031 	.probe = vt6655_probe,
3032 	.remove = vt6655_remove,
3033 #ifdef CONFIG_PM
3034 	.suspend = viawget_suspend,
3035 	.resume = viawget_resume,
3036 #endif
3037 };
3038 
vt6655_init_module(void)3039 static int __init vt6655_init_module(void)
3040 {
3041 	int ret;
3042 
3043 	ret = pci_register_driver(&device_driver);
3044 #ifdef CONFIG_PM
3045 	if (ret >= 0)
3046 		register_reboot_notifier(&device_notifier);
3047 #endif
3048 
3049 	return ret;
3050 }
3051 
vt6655_cleanup_module(void)3052 static void __exit vt6655_cleanup_module(void)
3053 {
3054 #ifdef CONFIG_PM
3055 	unregister_reboot_notifier(&device_notifier);
3056 #endif
3057 	pci_unregister_driver(&device_driver);
3058 }
3059 
3060 module_init(vt6655_init_module);
3061 module_exit(vt6655_cleanup_module);
3062 
3063 #ifdef CONFIG_PM
3064 static int
device_notify_reboot(struct notifier_block * nb,unsigned long event,void * p)3065 device_notify_reboot(struct notifier_block *nb, unsigned long event, void *p)
3066 {
3067 	struct pci_dev *pdev = NULL;
3068 
3069 	switch (event) {
3070 	case SYS_DOWN:
3071 	case SYS_HALT:
3072 	case SYS_POWER_OFF:
3073 		for_each_pci_dev(pdev) {
3074 			if (pci_dev_driver(pdev) == &device_driver) {
3075 				if (pci_get_drvdata(pdev))
3076 					viawget_suspend(pdev, PMSG_HIBERNATE);
3077 			}
3078 		}
3079 	}
3080 	return NOTIFY_DONE;
3081 }
3082 
3083 static int
viawget_suspend(struct pci_dev * pcid,pm_message_t state)3084 viawget_suspend(struct pci_dev *pcid, pm_message_t state)
3085 {
3086 	int power_status;   // to silence the compiler
3087 
3088 	struct vnt_private *pDevice = pci_get_drvdata(pcid);
3089 	PSMgmtObject  pMgmt = pDevice->pMgmt;
3090 
3091 	netif_stop_queue(pDevice->dev);
3092 	spin_lock_irq(&pDevice->lock);
3093 	pci_save_state(pcid);
3094 	del_timer(&pDevice->sTimerCommand);
3095 	del_timer(&pMgmt->sTimerSecondCallback);
3096 	pDevice->cbFreeCmdQueue = CMD_Q_SIZE;
3097 	pDevice->uCmdDequeueIdx = 0;
3098 	pDevice->uCmdEnqueueIdx = 0;
3099 	pDevice->bCmdRunning = false;
3100 	MACbShutdown(pDevice->PortOffset);
3101 	MACvSaveContext(pDevice->PortOffset, pDevice->abyMacContext);
3102 	pDevice->bLinkPass = false;
3103 	memset(pMgmt->abyCurrBSSID, 0, 6);
3104 	pMgmt->eCurrState = WMAC_STATE_IDLE;
3105 	pci_disable_device(pcid);
3106 	power_status = pci_set_power_state(pcid, pci_choose_state(pcid, state));
3107 	spin_unlock_irq(&pDevice->lock);
3108 	return 0;
3109 }
3110 
3111 static int
viawget_resume(struct pci_dev * pcid)3112 viawget_resume(struct pci_dev *pcid)
3113 {
3114 	struct vnt_private *pDevice = pci_get_drvdata(pcid);
3115 	PSMgmtObject  pMgmt = pDevice->pMgmt;
3116 	int power_status;   // to silence the compiler
3117 
3118 	power_status = pci_set_power_state(pcid, PCI_D0);
3119 	power_status = pci_enable_wake(pcid, PCI_D0, 0);
3120 	pci_restore_state(pcid);
3121 	if (netif_running(pDevice->dev)) {
3122 		spin_lock_irq(&pDevice->lock);
3123 		MACvRestoreContext(pDevice->PortOffset, pDevice->abyMacContext);
3124 		device_init_registers(pDevice);
3125 		if (pMgmt->sNodeDBTable[0].bActive) { // Assoc with BSS
3126 			pMgmt->sNodeDBTable[0].bActive = false;
3127 			pDevice->bLinkPass = false;
3128 			if (pMgmt->eCurrMode == WMAC_MODE_IBSS_STA) {
3129 				// In Adhoc, BSS state set back to started.
3130 				pMgmt->eCurrState = WMAC_STATE_STARTED;
3131 			} else {
3132 				pMgmt->eCurrMode = WMAC_MODE_STANDBY;
3133 				pMgmt->eCurrState = WMAC_STATE_IDLE;
3134 			}
3135 		}
3136 		init_timer(&pMgmt->sTimerSecondCallback);
3137 		init_timer(&pDevice->sTimerCommand);
3138 		MACvIntEnable(pDevice->PortOffset, IMR_MASK_VALUE);
3139 		BSSvClearBSSList((void *)pDevice, pDevice->bLinkPass);
3140 		bScheduleCommand((void *)pDevice, WLAN_CMD_BSSID_SCAN, NULL);
3141 		bScheduleCommand((void *)pDevice, WLAN_CMD_SSID, NULL);
3142 		spin_unlock_irq(&pDevice->lock);
3143 	}
3144 	return 0;
3145 }
3146 
3147 #endif
3148