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