staging: vt6655: dead code remove wpactl.c/h
[firefly-linux-kernel-4.4.55.git] / drivers / staging / vt6655 / device_main.c
1 /*
2  * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
3  * All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * File: device_main.c
20  *
21  * Purpose: driver entry for initial, open, close, tx and rx.
22  *
23  * Author: Lyndon Chen
24  *
25  * Date: Jan 8, 2003
26  *
27  * Functions:
28  *
29  *   vt6655_probe - module initial (insmod) driver entry
30  *   vt6655_remove - module remove entry
31  *   vt6655_init_info - device structure resource allocation function
32  *   device_free_info - device structure resource free function
33  *   device_get_pci_info - get allocated pci io/mem resource
34  *   device_print_info - print out resource
35  *   device_intr - interrupt handle function
36  *   device_rx_srv - rx service function
37  *   device_alloc_rx_buf - rx buffer pre-allocated function
38  *   device_alloc_frag_buf - rx fragement pre-allocated function
39  *   device_free_tx_buf - free tx buffer function
40  *   device_free_frag_buf- free de-fragement buffer
41  *   device_init_rd0_ring- initial rd dma0 ring
42  *   device_init_rd1_ring- initial rd dma1 ring
43  *   device_init_td0_ring- initial tx dma0 ring buffer
44  *   device_init_td1_ring- initial tx dma1 ring buffer
45  *   device_init_registers- initial MAC & BBP & RF internal registers.
46  *   device_init_rings- initial tx/rx ring buffer
47  *   device_init_defrag_cb- initial & allocate de-fragement buffer.
48  *   device_free_rings- free all allocated ring buffer
49  *   device_tx_srv- tx interrupt service function
50  *
51  * Revision History:
52  */
53 #undef __NO_VERSION__
54
55 #include <linux/file.h>
56 #include "device.h"
57 #include "card.h"
58 #include "channel.h"
59 #include "baseband.h"
60 #include "mac.h"
61 #include "tether.h"
62 #include "wmgr.h"
63 #include "wctl.h"
64 #include "power.h"
65 #include "wcmd.h"
66 #include "iocmd.h"
67 #include "tcrc.h"
68 #include "rxtx.h"
69 #include "wroute.h"
70 #include "bssdb.h"
71 #include "dpc.h"
72 #include "datarate.h"
73 #include "rf.h"
74 #include "iowpa.h"
75 #include <linux/delay.h>
76 #include <linux/kthread.h>
77 #include <linux/slab.h>
78
79 /*---------------------  Static Definitions -------------------------*/
80 //
81 // Define module options
82 //
83 MODULE_AUTHOR("VIA Networking Technologies, Inc., <lyndonchen@vntek.com.tw>");
84 MODULE_LICENSE("GPL");
85 MODULE_DESCRIPTION("VIA Networking Solomon-A/B/G Wireless LAN Adapter Driver");
86
87 #define DEVICE_PARAM(N, D)
88
89 #define RX_DESC_MIN0     16
90 #define RX_DESC_MAX0     128
91 #define RX_DESC_DEF0     32
92 DEVICE_PARAM(RxDescriptors0, "Number of receive descriptors0");
93
94 #define RX_DESC_MIN1     16
95 #define RX_DESC_MAX1     128
96 #define RX_DESC_DEF1     32
97 DEVICE_PARAM(RxDescriptors1, "Number of receive descriptors1");
98
99 #define TX_DESC_MIN0     16
100 #define TX_DESC_MAX0     128
101 #define TX_DESC_DEF0     32
102 DEVICE_PARAM(TxDescriptors0, "Number of transmit descriptors0");
103
104 #define TX_DESC_MIN1     16
105 #define TX_DESC_MAX1     128
106 #define TX_DESC_DEF1     64
107 DEVICE_PARAM(TxDescriptors1, "Number of transmit descriptors1");
108
109 #define IP_ALIG_DEF     0
110 /* IP_byte_align[] is used for IP header unsigned long byte aligned
111    0: indicate the IP header won't be unsigned long byte aligned.(Default) .
112    1: indicate the IP header will be unsigned long byte aligned.
113    In some environment, the IP header should be unsigned long byte aligned,
114    or the packet will be droped when we receive it. (eg: IPVS)
115 */
116 DEVICE_PARAM(IP_byte_align, "Enable IP header dword aligned");
117
118 #define INT_WORKS_DEF   20
119 #define INT_WORKS_MIN   10
120 #define INT_WORKS_MAX   64
121
122 DEVICE_PARAM(int_works, "Number of packets per interrupt services");
123
124 #define CHANNEL_MIN     1
125 #define CHANNEL_MAX     14
126 #define CHANNEL_DEF     6
127
128 DEVICE_PARAM(Channel, "Channel number");
129
130 /* PreambleType[] is the preamble length used for transmit.
131    0: indicate allows long preamble type
132    1: indicate allows short preamble type
133 */
134
135 #define PREAMBLE_TYPE_DEF     1
136
137 DEVICE_PARAM(PreambleType, "Preamble Type");
138
139 #define RTS_THRESH_MIN     512
140 #define RTS_THRESH_MAX     2347
141 #define RTS_THRESH_DEF     2347
142
143 DEVICE_PARAM(RTSThreshold, "RTS threshold");
144
145 #define FRAG_THRESH_MIN     256
146 #define FRAG_THRESH_MAX     2346
147 #define FRAG_THRESH_DEF     2346
148
149 DEVICE_PARAM(FragThreshold, "Fragmentation threshold");
150
151 #define DATA_RATE_MIN     0
152 #define DATA_RATE_MAX     13
153 #define DATA_RATE_DEF     13
154 /* datarate[] index
155    0: indicate 1 Mbps   0x02
156    1: indicate 2 Mbps   0x04
157    2: indicate 5.5 Mbps 0x0B
158    3: indicate 11 Mbps  0x16
159    4: indicate 6 Mbps   0x0c
160    5: indicate 9 Mbps   0x12
161    6: indicate 12 Mbps  0x18
162    7: indicate 18 Mbps  0x24
163    8: indicate 24 Mbps  0x30
164    9: indicate 36 Mbps  0x48
165    10: indicate 48 Mbps  0x60
166    11: indicate 54 Mbps  0x6c
167    12: indicate 72 Mbps  0x90
168    13: indicate auto rate
169 */
170
171 DEVICE_PARAM(ConnectionRate, "Connection data rate");
172
173 #define OP_MODE_DEF     0
174
175 DEVICE_PARAM(OPMode, "Infrastruct, adhoc, AP mode ");
176
177 /* OpMode[] is used for transmit.
178    0: indicate infrastruct mode used
179    1: indicate adhoc mode used
180    2: indicate AP mode used
181 */
182
183 /* PSMode[]
184    0: indicate disable power saving mode
185    1: indicate enable power saving mode
186 */
187
188 #define PS_MODE_DEF     0
189
190 DEVICE_PARAM(PSMode, "Power saving mode");
191
192 #define SHORT_RETRY_MIN     0
193 #define SHORT_RETRY_MAX     31
194 #define SHORT_RETRY_DEF     8
195
196 DEVICE_PARAM(ShortRetryLimit, "Short frame retry limits");
197
198 #define LONG_RETRY_MIN     0
199 #define LONG_RETRY_MAX     15
200 #define LONG_RETRY_DEF     4
201
202 DEVICE_PARAM(LongRetryLimit, "long frame retry limits");
203
204 /* BasebandType[] baseband type selected
205    0: indicate 802.11a type
206    1: indicate 802.11b type
207    2: indicate 802.11g type
208 */
209 #define BBP_TYPE_MIN     0
210 #define BBP_TYPE_MAX     2
211 #define BBP_TYPE_DEF     2
212
213 DEVICE_PARAM(BasebandType, "baseband type");
214
215 /* 80211hEnable[]
216    0: indicate disable 802.11h
217    1: indicate enable 802.11h
218 */
219
220 #define X80211h_MODE_DEF     0
221
222 DEVICE_PARAM(b80211hEnable, "802.11h mode");
223
224 /* 80211hEnable[]
225    0: indicate disable 802.11h
226    1: indicate enable 802.11h
227 */
228
229 #define DIVERSITY_ANT_DEF     0
230
231 DEVICE_PARAM(bDiversityANTEnable, "ANT diversity mode");
232
233 //
234 // Static vars definitions
235 //
236 static CHIP_INFO chip_info_table[] = {
237         { VT3253,       "VIA Networking Solomon-A/B/G Wireless LAN Adapter ",
238           256, 1,     DEVICE_FLAGS_IP_ALIGN|DEVICE_FLAGS_TX_ALIGN },
239         {0, NULL}
240 };
241
242 static const struct pci_device_id vt6655_pci_id_table[] = {
243         { PCI_VDEVICE(VIA, 0x3253), (kernel_ulong_t)chip_info_table},
244         { 0, }
245 };
246
247 /*---------------------  Static Functions  --------------------------*/
248
249 static int  vt6655_probe(struct pci_dev *pcid, const struct pci_device_id *ent);
250 static void vt6655_init_info(struct pci_dev *pcid,
251                              struct vnt_private **ppDevice, PCHIP_INFO);
252 static void device_free_info(struct vnt_private *pDevice);
253 static bool device_get_pci_info(struct vnt_private *, struct pci_dev *pcid);
254 static void device_print_info(struct vnt_private *pDevice);
255 static void device_init_diversity_timer(struct vnt_private *pDevice);
256 static  irqreturn_t  device_intr(int irq,  void *dev_instance);
257
258 #ifdef CONFIG_PM
259 static int device_notify_reboot(struct notifier_block *, unsigned long event, void *ptr);
260 static struct notifier_block device_notifier = {
261         .notifier_call = device_notify_reboot,
262         .next = NULL,
263         .priority = 0,
264 };
265 #endif
266
267 static void device_init_rd0_ring(struct vnt_private *pDevice);
268 static void device_init_rd1_ring(struct vnt_private *pDevice);
269 static void device_init_defrag_cb(struct vnt_private *pDevice);
270 static void device_init_td0_ring(struct vnt_private *pDevice);
271 static void device_init_td1_ring(struct vnt_private *pDevice);
272
273 static int  device_rx_srv(struct vnt_private *pDevice, unsigned int uIdx);
274 static int  device_tx_srv(struct vnt_private *pDevice, unsigned int uIdx);
275 static bool device_alloc_rx_buf(struct vnt_private *pDevice, PSRxDesc pDesc);
276 static void device_init_registers(struct vnt_private *pDevice);
277 static void device_free_tx_buf(struct vnt_private *pDevice, PSTxDesc pDesc);
278 static void device_free_td0_ring(struct vnt_private *pDevice);
279 static void device_free_td1_ring(struct vnt_private *pDevice);
280 static void device_free_rd0_ring(struct vnt_private *pDevice);
281 static void device_free_rd1_ring(struct vnt_private *pDevice);
282 static void device_free_rings(struct vnt_private *pDevice);
283 static void device_free_frag_buf(struct vnt_private *pDevice);
284
285 /*---------------------  Export Variables  --------------------------*/
286
287 /*---------------------  Export Functions  --------------------------*/
288
289 static char *get_chip_name(int chip_id)
290 {
291         int i;
292
293         for (i = 0; chip_info_table[i].name != NULL; i++)
294                 if (chip_info_table[i].chip_id == chip_id)
295                         break;
296         return chip_info_table[i].name;
297 }
298
299 static void vt6655_remove(struct pci_dev *pcid)
300 {
301         struct vnt_private *pDevice = pci_get_drvdata(pcid);
302
303         if (pDevice == NULL)
304                 return;
305         device_free_info(pDevice);
306 }
307
308 static void device_get_options(struct vnt_private *pDevice)
309 {
310         POPTIONS pOpts = &(pDevice->sOpts);
311
312         pOpts->nRxDescs0 = RX_DESC_DEF0;
313         pOpts->nRxDescs1 = RX_DESC_DEF1;
314         pOpts->nTxDescs[0] = TX_DESC_DEF0;
315         pOpts->nTxDescs[1] = TX_DESC_DEF1;
316         pOpts->flags |= DEVICE_FLAGS_IP_ALIGN;
317         pOpts->int_works = INT_WORKS_DEF;
318         pOpts->rts_thresh = RTS_THRESH_DEF;
319         pOpts->frag_thresh = FRAG_THRESH_DEF;
320         pOpts->data_rate = DATA_RATE_DEF;
321         pOpts->channel_num = CHANNEL_DEF;
322
323         pOpts->flags |= DEVICE_FLAGS_PREAMBLE_TYPE;
324         pOpts->flags |= DEVICE_FLAGS_OP_MODE;
325         pOpts->short_retry = SHORT_RETRY_DEF;
326         pOpts->long_retry = LONG_RETRY_DEF;
327         pOpts->bbp_type = BBP_TYPE_DEF;
328         pOpts->flags |= DEVICE_FLAGS_80211h_MODE;
329         pOpts->flags |= DEVICE_FLAGS_DiversityANT;
330 }
331
332 static void
333 device_set_options(struct vnt_private *pDevice)
334 {
335         unsigned char abyBroadcastAddr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
336         unsigned char abySNAP_RFC1042[ETH_ALEN] = {0xAA, 0xAA, 0x03, 0x00, 0x00, 0x00};
337         unsigned char abySNAP_Bridgetunnel[ETH_ALEN] = {0xAA, 0xAA, 0x03, 0x00, 0x00, 0xF8};
338
339         ether_addr_copy(pDevice->abyBroadcastAddr, abyBroadcastAddr);
340         ether_addr_copy(pDevice->abySNAP_RFC1042, abySNAP_RFC1042);
341         ether_addr_copy(pDevice->abySNAP_Bridgetunnel, abySNAP_Bridgetunnel);
342
343         pDevice->uChannel = pDevice->sOpts.channel_num;
344         pDevice->wRTSThreshold = pDevice->sOpts.rts_thresh;
345         pDevice->wFragmentationThreshold = pDevice->sOpts.frag_thresh;
346         pDevice->byShortRetryLimit = pDevice->sOpts.short_retry;
347         pDevice->byLongRetryLimit = pDevice->sOpts.long_retry;
348         pDevice->wMaxTransmitMSDULifetime = DEFAULT_MSDU_LIFETIME;
349         pDevice->byShortPreamble = (pDevice->sOpts.flags & DEVICE_FLAGS_PREAMBLE_TYPE) ? 1 : 0;
350         pDevice->byOpMode = (pDevice->sOpts.flags & DEVICE_FLAGS_OP_MODE) ? 1 : 0;
351         pDevice->ePSMode = (pDevice->sOpts.flags & DEVICE_FLAGS_PS_MODE) ? 1 : 0;
352         pDevice->b11hEnable = (pDevice->sOpts.flags & DEVICE_FLAGS_80211h_MODE) ? 1 : 0;
353         pDevice->bDiversityRegCtlON = (pDevice->sOpts.flags & DEVICE_FLAGS_DiversityANT) ? 1 : 0;
354         pDevice->uConnectionRate = pDevice->sOpts.data_rate;
355         if (pDevice->uConnectionRate < RATE_AUTO)
356                 pDevice->bFixRate = true;
357         pDevice->byBBType = pDevice->sOpts.bbp_type;
358         pDevice->byPacketType = (VIA_PKT_TYPE)pDevice->byBBType;
359         pDevice->byAutoFBCtrl = AUTO_FB_0;
360         pDevice->bUpdateBBVGA = true;
361         pDevice->byFOETuning = 0;
362         pDevice->byPreambleType = 0;
363
364         pr_debug(" uChannel= %d\n", (int)pDevice->uChannel);
365         pr_debug(" byOpMode= %d\n", (int)pDevice->byOpMode);
366         pr_debug(" ePSMode= %d\n", (int)pDevice->ePSMode);
367         pr_debug(" wRTSThreshold= %d\n", (int)pDevice->wRTSThreshold);
368         pr_debug(" byShortRetryLimit= %d\n", (int)pDevice->byShortRetryLimit);
369         pr_debug(" byLongRetryLimit= %d\n", (int)pDevice->byLongRetryLimit);
370         pr_debug(" byPreambleType= %d\n", (int)pDevice->byPreambleType);
371         pr_debug(" byShortPreamble= %d\n", (int)pDevice->byShortPreamble);
372         pr_debug(" uConnectionRate= %d\n", (int)pDevice->uConnectionRate);
373         pr_debug(" byBBType= %d\n", (int)pDevice->byBBType);
374         pr_debug(" pDevice->b11hEnable= %d\n", (int)pDevice->b11hEnable);
375         pr_debug(" pDevice->bDiversityRegCtlON= %d\n",
376                  (int)pDevice->bDiversityRegCtlON);
377 }
378
379 //
380 // Initialisation of MAC & BBP registers
381 //
382
383 static void device_init_registers(struct vnt_private *pDevice)
384 {
385         unsigned long flags;
386         unsigned int ii;
387         unsigned char byValue;
388         unsigned char byValue1;
389         unsigned char byCCKPwrdBm = 0;
390         unsigned char byOFDMPwrdBm = 0;
391
392         MACbShutdown(pDevice->PortOffset);
393         BBvSoftwareReset(pDevice->PortOffset);
394
395         /* Do MACbSoftwareReset in MACvInitialize */
396         MACbSoftwareReset(pDevice->PortOffset);
397
398         pDevice->bAES = false;
399
400         /* Only used in 11g type, sync with ERP IE */
401         pDevice->bProtectMode = false;
402
403         pDevice->bNonERPPresent = false;
404         pDevice->bBarkerPreambleMd = false;
405         pDevice->wCurrentRate = RATE_1M;
406         pDevice->byTopOFDMBasicRate = RATE_24M;
407         pDevice->byTopCCKBasicRate = RATE_1M;
408
409         /* Target to IF pin while programming to RF chip. */
410         pDevice->byRevId = 0;
411
412         /* init MAC */
413         MACvInitialize(pDevice->PortOffset);
414
415         /* Get Local ID */
416         VNSvInPortB(pDevice->PortOffset + MAC_REG_LOCALID, &pDevice->byLocalID);
417
418         spin_lock_irqsave(&pDevice->lock, flags);
419
420         SROMvReadAllContents(pDevice->PortOffset, pDevice->abyEEPROM);
421
422         spin_unlock_irqrestore(&pDevice->lock, flags);
423
424         /* Get Channel range */
425         pDevice->byMinChannel = 1;
426         pDevice->byMaxChannel = CB_MAX_CHANNEL;
427
428         /* Get Antena */
429         byValue = SROMbyReadEmbedded(pDevice->PortOffset, EEP_OFS_ANTENNA);
430         if (byValue & EEP_ANTINV)
431                 pDevice->bTxRxAntInv = true;
432         else
433                 pDevice->bTxRxAntInv = false;
434
435         byValue &= (EEP_ANTENNA_AUX | EEP_ANTENNA_MAIN);
436         /* if not set default is All */
437         if (byValue == 0)
438                 byValue = (EEP_ANTENNA_AUX | EEP_ANTENNA_MAIN);
439
440         pDevice->ulDiversityNValue = 100*260;
441         pDevice->ulDiversityMValue = 100*16;
442         pDevice->byTMax = 1;
443         pDevice->byTMax2 = 4;
444         pDevice->ulSQ3TH = 0;
445         pDevice->byTMax3 = 64;
446
447         if (byValue == (EEP_ANTENNA_AUX | EEP_ANTENNA_MAIN)) {
448                 pDevice->byAntennaCount = 2;
449                 pDevice->byTxAntennaMode = ANT_B;
450                 pDevice->dwTxAntennaSel = 1;
451                 pDevice->dwRxAntennaSel = 1;
452
453                 if (pDevice->bTxRxAntInv)
454                         pDevice->byRxAntennaMode = ANT_A;
455                 else
456                         pDevice->byRxAntennaMode = ANT_B;
457
458                 byValue1 = SROMbyReadEmbedded(pDevice->PortOffset,
459                                               EEP_OFS_ANTENNA);
460
461                 if ((byValue1 & 0x08) == 0)
462                         pDevice->bDiversityEnable = false;
463                 else
464                         pDevice->bDiversityEnable = true;
465         } else  {
466                 pDevice->bDiversityEnable = false;
467                 pDevice->byAntennaCount = 1;
468                 pDevice->dwTxAntennaSel = 0;
469                 pDevice->dwRxAntennaSel = 0;
470
471                 if (byValue & EEP_ANTENNA_AUX) {
472                         pDevice->byTxAntennaMode = ANT_A;
473
474                         if (pDevice->bTxRxAntInv)
475                                 pDevice->byRxAntennaMode = ANT_B;
476                         else
477                                 pDevice->byRxAntennaMode = ANT_A;
478                 } else {
479                         pDevice->byTxAntennaMode = ANT_B;
480
481                         if (pDevice->bTxRxAntInv)
482                                 pDevice->byRxAntennaMode = ANT_A;
483                         else
484                                 pDevice->byRxAntennaMode = ANT_B;
485                 }
486         }
487
488         pr_debug("bDiversityEnable=[%d],NValue=[%d],MValue=[%d],TMax=[%d],TMax2=[%d]\n",
489                  pDevice->bDiversityEnable, (int)pDevice->ulDiversityNValue,
490                  (int)pDevice->ulDiversityMValue, pDevice->byTMax,
491                  pDevice->byTMax2);
492
493         /* zonetype initial */
494         pDevice->byOriginalZonetype = pDevice->abyEEPROM[EEP_OFS_ZONETYPE];
495
496         /* Get RFType */
497         pDevice->byRFType = SROMbyReadEmbedded(pDevice->PortOffset, EEP_OFS_RFTYPE);
498
499         /* force change RevID for VT3253 emu */
500         if ((pDevice->byRFType & RF_EMU) != 0)
501                         pDevice->byRevId = 0x80;
502
503         pDevice->byRFType &= RF_MASK;
504         pr_debug("pDevice->byRFType = %x\n", pDevice->byRFType);
505
506         if (!pDevice->bZoneRegExist)
507                 pDevice->byZoneType = pDevice->abyEEPROM[EEP_OFS_ZONETYPE];
508
509         pr_debug("pDevice->byZoneType = %x\n", pDevice->byZoneType);
510
511         /* Init RF module */
512         RFbInit(pDevice);
513
514         /* Get Desire Power Value */
515         pDevice->byCurPwr = 0xFF;
516         pDevice->byCCKPwr = SROMbyReadEmbedded(pDevice->PortOffset, EEP_OFS_PWR_CCK);
517         pDevice->byOFDMPwrG = SROMbyReadEmbedded(pDevice->PortOffset, EEP_OFS_PWR_OFDMG);
518
519         /* Load power Table */
520         for (ii = 0; ii < CB_MAX_CHANNEL_24G; ii++) {
521                 pDevice->abyCCKPwrTbl[ii + 1] =
522                         SROMbyReadEmbedded(pDevice->PortOffset,
523                                            (unsigned char)(ii + EEP_OFS_CCK_PWR_TBL));
524                 if (pDevice->abyCCKPwrTbl[ii + 1] == 0)
525                         pDevice->abyCCKPwrTbl[ii+1] = pDevice->byCCKPwr;
526
527                 pDevice->abyOFDMPwrTbl[ii + 1] =
528                         SROMbyReadEmbedded(pDevice->PortOffset,
529                                            (unsigned char)(ii + EEP_OFS_OFDM_PWR_TBL));
530                 if (pDevice->abyOFDMPwrTbl[ii + 1] == 0)
531                         pDevice->abyOFDMPwrTbl[ii + 1] = pDevice->byOFDMPwrG;
532
533                 pDevice->abyCCKDefaultPwr[ii + 1] = byCCKPwrdBm;
534                 pDevice->abyOFDMDefaultPwr[ii + 1] = byOFDMPwrdBm;
535         }
536
537         /* recover 12,13 ,14channel for EUROPE by 11 channel */
538         for (ii = 11; ii < 14; ii++) {
539                 pDevice->abyCCKPwrTbl[ii] = pDevice->abyCCKPwrTbl[10];
540                 pDevice->abyOFDMPwrTbl[ii] = pDevice->abyOFDMPwrTbl[10];
541         }
542
543         /* Load OFDM A Power Table */
544         for (ii = 0; ii < CB_MAX_CHANNEL_5G; ii++) {
545                 pDevice->abyOFDMPwrTbl[ii + CB_MAX_CHANNEL_24G + 1] =
546                         SROMbyReadEmbedded(pDevice->PortOffset,
547                                            (unsigned char)(ii + EEP_OFS_OFDMA_PWR_TBL));
548
549                 pDevice->abyOFDMDefaultPwr[ii + CB_MAX_CHANNEL_24G + 1] =
550                         SROMbyReadEmbedded(pDevice->PortOffset,
551                                            (unsigned char)(ii + EEP_OFS_OFDMA_PWR_dBm));
552         }
553
554         if (pDevice->byLocalID > REV_ID_VT3253_B1) {
555                 MACvSelectPage1(pDevice->PortOffset);
556
557                 VNSvOutPortB(pDevice->PortOffset + MAC_REG_MSRCTL + 1,
558                              (MSRCTL1_TXPWR | MSRCTL1_CSAPAREN));
559
560                 MACvSelectPage0(pDevice->PortOffset);
561         }
562
563         /* use relative tx timeout and 802.11i D4 */
564         MACvWordRegBitsOn(pDevice->PortOffset,
565                           MAC_REG_CFG, (CFG_TKIPOPT | CFG_NOTXTIMEOUT));
566
567         /* set performance parameter by registry */
568         MACvSetShortRetryLimit(pDevice->PortOffset, pDevice->byShortRetryLimit);
569         MACvSetLongRetryLimit(pDevice->PortOffset, pDevice->byLongRetryLimit);
570
571         /* reset TSF counter */
572         VNSvOutPortB(pDevice->PortOffset + MAC_REG_TFTCTL, TFTCTL_TSFCNTRST);
573         /* enable TSF counter */
574         VNSvOutPortB(pDevice->PortOffset + MAC_REG_TFTCTL, TFTCTL_TSFCNTREN);
575
576         /* initialize BBP registers */
577         BBbVT3253Init(pDevice);
578
579         if (pDevice->bUpdateBBVGA) {
580                 pDevice->byBBVGACurrent = pDevice->abyBBVGA[0];
581                 pDevice->byBBVGANew = pDevice->byBBVGACurrent;
582                 BBvSetVGAGainOffset(pDevice, pDevice->abyBBVGA[0]);
583         }
584
585         BBvSetRxAntennaMode(pDevice->PortOffset, pDevice->byRxAntennaMode);
586         BBvSetTxAntennaMode(pDevice->PortOffset, pDevice->byTxAntennaMode);
587
588         /* Set BB and packet type at the same time. */
589         /* Set Short Slot Time, xIFS, and RSPINF. */
590         if (pDevice->uConnectionRate == RATE_AUTO)
591                 pDevice->wCurrentRate = RATE_54M;
592         else
593                 pDevice->wCurrentRate = (unsigned short)pDevice->uConnectionRate;
594
595         pDevice->bRadioOff = false;
596
597         pDevice->byRadioCtl = SROMbyReadEmbedded(pDevice->PortOffset,
598                                                  EEP_OFS_RADIOCTL);
599         pDevice->bHWRadioOff = false;
600
601         if (pDevice->byRadioCtl & EEP_RADIOCTL_ENABLE) {
602                 /* Get GPIO */
603                 MACvGPIOIn(pDevice->PortOffset, &pDevice->byGPIO);
604
605                 if (((pDevice->byGPIO & GPIO0_DATA) &&
606                      !(pDevice->byRadioCtl & EEP_RADIOCTL_INV)) ||
607                      (!(pDevice->byGPIO & GPIO0_DATA) &&
608                      (pDevice->byRadioCtl & EEP_RADIOCTL_INV)))
609                         pDevice->bHWRadioOff = true;
610         }
611
612         if (pDevice->bHWRadioOff || pDevice->bRadioControlOff)
613                 CARDbRadioPowerOff(pDevice);
614
615         /* get Permanent network address */
616         SROMvReadEtherAddress(pDevice->PortOffset, pDevice->abyCurrentNetAddr);
617         pr_debug("Network address = %pM\n", pDevice->abyCurrentNetAddr);
618
619         /* reset Tx pointer */
620         CARDvSafeResetRx(pDevice);
621         /* reset Rx pointer */
622         CARDvSafeResetTx(pDevice);
623
624         if (pDevice->byLocalID <= REV_ID_VT3253_A1)
625                 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_RCR, RCR_WPAERR);
626
627         /* Turn On Rx DMA */
628         MACvReceive0(pDevice->PortOffset);
629         MACvReceive1(pDevice->PortOffset);
630
631         /* start the adapter */
632         MACvStart(pDevice->PortOffset);
633 }
634
635 static void device_init_diversity_timer(struct vnt_private *pDevice)
636 {
637         init_timer(&pDevice->TimerSQ3Tmax1);
638         pDevice->TimerSQ3Tmax1.data = (unsigned long) pDevice;
639         pDevice->TimerSQ3Tmax1.function = (TimerFunction)TimerSQ3CallBack;
640         pDevice->TimerSQ3Tmax1.expires = RUN_AT(HZ);
641
642         init_timer(&pDevice->TimerSQ3Tmax2);
643         pDevice->TimerSQ3Tmax2.data = (unsigned long) pDevice;
644         pDevice->TimerSQ3Tmax2.function = (TimerFunction)TimerSQ3CallBack;
645         pDevice->TimerSQ3Tmax2.expires = RUN_AT(HZ);
646
647         init_timer(&pDevice->TimerSQ3Tmax3);
648         pDevice->TimerSQ3Tmax3.data = (unsigned long) pDevice;
649         pDevice->TimerSQ3Tmax3.function = (TimerFunction)TimerState1CallBack;
650         pDevice->TimerSQ3Tmax3.expires = RUN_AT(HZ);
651 }
652
653 static void device_print_info(struct vnt_private *pDevice)
654 {
655         dev_info(&pDevice->pcid->dev, "%s\n", get_chip_name(pDevice->chip_id));
656
657         dev_info(&pDevice->pcid->dev, "MAC=%pM IO=0x%lx Mem=0x%lx IRQ=%d\n",
658                  pDevice->abyCurrentNetAddr, (unsigned long)pDevice->ioaddr,
659                  (unsigned long)pDevice->PortOffset, pDevice->pcid->irq);
660 }
661
662 static void vt6655_init_info(struct pci_dev *pcid,
663                              struct vnt_private **ppDevice,
664                              PCHIP_INFO pChip_info)
665 {
666         memset(*ppDevice, 0, sizeof(**ppDevice));
667
668         (*ppDevice)->pcid = pcid;
669         (*ppDevice)->chip_id = pChip_info->chip_id;
670         (*ppDevice)->io_size = pChip_info->io_size;
671         (*ppDevice)->nTxQueues = pChip_info->nTxQueue;
672         (*ppDevice)->multicast_limit = 32;
673
674         spin_lock_init(&((*ppDevice)->lock));
675 }
676
677 static bool device_get_pci_info(struct vnt_private *pDevice,
678                                 struct pci_dev *pcid)
679 {
680         u16 pci_cmd;
681         u8  b;
682         unsigned int cis_addr;
683
684         pci_read_config_byte(pcid, PCI_REVISION_ID, &pDevice->byRevId);
685         pci_read_config_word(pcid, PCI_SUBSYSTEM_ID, &pDevice->SubSystemID);
686         pci_read_config_word(pcid, PCI_SUBSYSTEM_VENDOR_ID, &pDevice->SubVendorID);
687         pci_read_config_word(pcid, PCI_COMMAND, (u16 *)&(pci_cmd));
688
689         pci_set_master(pcid);
690
691         pDevice->memaddr = pci_resource_start(pcid, 0);
692         pDevice->ioaddr = pci_resource_start(pcid, 1);
693
694         cis_addr = pci_resource_start(pcid, 2);
695
696         pDevice->pcid = pcid;
697
698         pci_read_config_byte(pcid, PCI_COMMAND, &b);
699         pci_write_config_byte(pcid, PCI_COMMAND, (b|PCI_COMMAND_MASTER));
700
701         return true;
702 }
703
704 static void device_free_info(struct vnt_private *pDevice)
705 {
706         if (!pDevice)
707                 return;
708
709         if (pDevice->mac_hw)
710                 ieee80211_unregister_hw(pDevice->hw);
711
712         if (pDevice->PortOffset)
713                 iounmap(pDevice->PortOffset);
714
715         if (pDevice->pcid)
716                 pci_release_regions(pDevice->pcid);
717
718         if (pDevice->hw)
719                 ieee80211_free_hw(pDevice->hw);
720 }
721
722 static bool device_init_rings(struct vnt_private *pDevice)
723 {
724         void *vir_pool;
725
726         /*allocate all RD/TD rings a single pool*/
727         vir_pool = pci_zalloc_consistent(pDevice->pcid,
728                                          pDevice->sOpts.nRxDescs0 * sizeof(SRxDesc) +
729                                          pDevice->sOpts.nRxDescs1 * sizeof(SRxDesc) +
730                                          pDevice->sOpts.nTxDescs[0] * sizeof(STxDesc) +
731                                          pDevice->sOpts.nTxDescs[1] * sizeof(STxDesc),
732                                          &pDevice->pool_dma);
733         if (vir_pool == NULL) {
734                 dev_err(&pDevice->pcid->dev, "allocate desc dma memory failed\n");
735                 return false;
736         }
737
738         pDevice->aRD0Ring = vir_pool;
739         pDevice->aRD1Ring = vir_pool +
740                 pDevice->sOpts.nRxDescs0 * sizeof(SRxDesc);
741
742         pDevice->rd0_pool_dma = pDevice->pool_dma;
743         pDevice->rd1_pool_dma = pDevice->rd0_pool_dma +
744                 pDevice->sOpts.nRxDescs0 * sizeof(SRxDesc);
745
746         pDevice->tx0_bufs = pci_zalloc_consistent(pDevice->pcid,
747                                                   pDevice->sOpts.nTxDescs[0] * PKT_BUF_SZ +
748                                                   pDevice->sOpts.nTxDescs[1] * PKT_BUF_SZ +
749                                                   CB_BEACON_BUF_SIZE +
750                                                   CB_MAX_BUF_SIZE,
751                                                   &pDevice->tx_bufs_dma0);
752         if (pDevice->tx0_bufs == NULL) {
753                 dev_err(&pDevice->pcid->dev, "allocate buf dma memory failed\n");
754
755                 pci_free_consistent(pDevice->pcid,
756                                     pDevice->sOpts.nRxDescs0 * sizeof(SRxDesc) +
757                                     pDevice->sOpts.nRxDescs1 * sizeof(SRxDesc) +
758                                     pDevice->sOpts.nTxDescs[0] * sizeof(STxDesc) +
759                                     pDevice->sOpts.nTxDescs[1] * sizeof(STxDesc),
760                                     vir_pool, pDevice->pool_dma
761                         );
762                 return false;
763         }
764
765         pDevice->td0_pool_dma = pDevice->rd1_pool_dma +
766                 pDevice->sOpts.nRxDescs1 * sizeof(SRxDesc);
767
768         pDevice->td1_pool_dma = pDevice->td0_pool_dma +
769                 pDevice->sOpts.nTxDescs[0] * sizeof(STxDesc);
770
771         // vir_pool: pvoid type
772         pDevice->apTD0Rings = vir_pool
773                 + pDevice->sOpts.nRxDescs0 * sizeof(SRxDesc)
774                 + pDevice->sOpts.nRxDescs1 * sizeof(SRxDesc);
775
776         pDevice->apTD1Rings = vir_pool
777                 + pDevice->sOpts.nRxDescs0 * sizeof(SRxDesc)
778                 + pDevice->sOpts.nRxDescs1 * sizeof(SRxDesc)
779                 + pDevice->sOpts.nTxDescs[0] * sizeof(STxDesc);
780
781         pDevice->tx1_bufs = pDevice->tx0_bufs +
782                 pDevice->sOpts.nTxDescs[0] * PKT_BUF_SZ;
783
784         pDevice->tx_beacon_bufs = pDevice->tx1_bufs +
785                 pDevice->sOpts.nTxDescs[1] * PKT_BUF_SZ;
786
787         pDevice->pbyTmpBuff = pDevice->tx_beacon_bufs +
788                 CB_BEACON_BUF_SIZE;
789
790         pDevice->tx_bufs_dma1 = pDevice->tx_bufs_dma0 +
791                 pDevice->sOpts.nTxDescs[0] * PKT_BUF_SZ;
792
793         pDevice->tx_beacon_dma = pDevice->tx_bufs_dma1 +
794                 pDevice->sOpts.nTxDescs[1] * PKT_BUF_SZ;
795
796         return true;
797 }
798
799 static void device_free_rings(struct vnt_private *pDevice)
800 {
801         pci_free_consistent(pDevice->pcid,
802                             pDevice->sOpts.nRxDescs0 * sizeof(SRxDesc) +
803                             pDevice->sOpts.nRxDescs1 * sizeof(SRxDesc) +
804                             pDevice->sOpts.nTxDescs[0] * sizeof(STxDesc) +
805                             pDevice->sOpts.nTxDescs[1] * sizeof(STxDesc)
806                             ,
807                             pDevice->aRD0Ring, pDevice->pool_dma
808                 );
809
810         if (pDevice->tx0_bufs)
811                 pci_free_consistent(pDevice->pcid,
812                                     pDevice->sOpts.nTxDescs[0] * PKT_BUF_SZ +
813                                     pDevice->sOpts.nTxDescs[1] * PKT_BUF_SZ +
814                                     CB_BEACON_BUF_SIZE +
815                                     CB_MAX_BUF_SIZE,
816                                     pDevice->tx0_bufs, pDevice->tx_bufs_dma0
817                         );
818 }
819
820 static void device_init_rd0_ring(struct vnt_private *pDevice)
821 {
822         int i;
823         dma_addr_t      curr = pDevice->rd0_pool_dma;
824         PSRxDesc        pDesc;
825
826         /* Init the RD0 ring entries */
827         for (i = 0; i < pDevice->sOpts.nRxDescs0; i ++, curr += sizeof(SRxDesc)) {
828                 pDesc = &(pDevice->aRD0Ring[i]);
829                 pDesc->pRDInfo = alloc_rd_info();
830                 ASSERT(pDesc->pRDInfo);
831                 if (!device_alloc_rx_buf(pDevice, pDesc))
832                         dev_err(&pDevice->pcid->dev, "can not alloc rx bufs\n");
833
834                 pDesc->next = &(pDevice->aRD0Ring[(i+1) % pDevice->sOpts.nRxDescs0]);
835                 pDesc->pRDInfo->curr_desc = cpu_to_le32(curr);
836                 pDesc->next_desc = cpu_to_le32(curr + sizeof(SRxDesc));
837         }
838
839         if (i > 0)
840                 pDevice->aRD0Ring[i-1].next_desc = cpu_to_le32(pDevice->rd0_pool_dma);
841         pDevice->pCurrRD[0] = &(pDevice->aRD0Ring[0]);
842 }
843
844 static void device_init_rd1_ring(struct vnt_private *pDevice)
845 {
846         int i;
847         dma_addr_t      curr = pDevice->rd1_pool_dma;
848         PSRxDesc        pDesc;
849
850         /* Init the RD1 ring entries */
851         for (i = 0; i < pDevice->sOpts.nRxDescs1; i ++, curr += sizeof(SRxDesc)) {
852                 pDesc = &(pDevice->aRD1Ring[i]);
853                 pDesc->pRDInfo = alloc_rd_info();
854                 ASSERT(pDesc->pRDInfo);
855                 if (!device_alloc_rx_buf(pDevice, pDesc))
856                         dev_err(&pDevice->pcid->dev, "can not alloc rx bufs\n");
857
858                 pDesc->next = &(pDevice->aRD1Ring[(i+1) % pDevice->sOpts.nRxDescs1]);
859                 pDesc->pRDInfo->curr_desc = cpu_to_le32(curr);
860                 pDesc->next_desc = cpu_to_le32(curr + sizeof(SRxDesc));
861         }
862
863         if (i > 0)
864                 pDevice->aRD1Ring[i-1].next_desc = cpu_to_le32(pDevice->rd1_pool_dma);
865         pDevice->pCurrRD[1] = &(pDevice->aRD1Ring[0]);
866 }
867
868 static void device_init_defrag_cb(struct vnt_private *pDevice)
869 {
870         int i;
871         PSDeFragControlBlock pDeF;
872
873         /* Init the fragment ctl entries */
874         for (i = 0; i < CB_MAX_RX_FRAG; i++) {
875                 pDeF = &(pDevice->sRxDFCB[i]);
876                 if (!device_alloc_frag_buf(pDevice, pDeF))
877                         dev_err(&pDevice->pcid->dev, "can not alloc frag bufs\n");
878         }
879         pDevice->cbDFCB = CB_MAX_RX_FRAG;
880         pDevice->cbFreeDFCB = pDevice->cbDFCB;
881 }
882
883 static void device_free_rd0_ring(struct vnt_private *pDevice)
884 {
885         int i;
886
887         for (i = 0; i < pDevice->sOpts.nRxDescs0; i++) {
888                 PSRxDesc        pDesc = &(pDevice->aRD0Ring[i]);
889                 PDEVICE_RD_INFO  pRDInfo = pDesc->pRDInfo;
890
891                 pci_unmap_single(pDevice->pcid, pRDInfo->skb_dma,
892                                  pDevice->rx_buf_sz, PCI_DMA_FROMDEVICE);
893
894                 dev_kfree_skb(pRDInfo->skb);
895
896                 kfree(pDesc->pRDInfo);
897         }
898 }
899
900 static void device_free_rd1_ring(struct vnt_private *pDevice)
901 {
902         int i;
903
904         for (i = 0; i < pDevice->sOpts.nRxDescs1; i++) {
905                 PSRxDesc        pDesc = &(pDevice->aRD1Ring[i]);
906                 PDEVICE_RD_INFO  pRDInfo = pDesc->pRDInfo;
907
908                 pci_unmap_single(pDevice->pcid, pRDInfo->skb_dma,
909                                  pDevice->rx_buf_sz, PCI_DMA_FROMDEVICE);
910
911                 dev_kfree_skb(pRDInfo->skb);
912
913                 kfree(pDesc->pRDInfo);
914         }
915 }
916
917 static void device_free_frag_buf(struct vnt_private *pDevice)
918 {
919         PSDeFragControlBlock pDeF;
920         int i;
921
922         for (i = 0; i < CB_MAX_RX_FRAG; i++) {
923                 pDeF = &(pDevice->sRxDFCB[i]);
924
925                 if (pDeF->skb)
926                         dev_kfree_skb(pDeF->skb);
927
928         }
929 }
930
931 static void device_init_td0_ring(struct vnt_private *pDevice)
932 {
933         int i;
934         dma_addr_t  curr;
935         PSTxDesc        pDesc;
936
937         curr = pDevice->td0_pool_dma;
938         for (i = 0; i < pDevice->sOpts.nTxDescs[0]; i++, curr += sizeof(STxDesc)) {
939                 pDesc = &(pDevice->apTD0Rings[i]);
940                 pDesc->pTDInfo = alloc_td_info();
941                 ASSERT(pDesc->pTDInfo);
942                 if (pDevice->flags & DEVICE_FLAGS_TX_ALIGN) {
943                         pDesc->pTDInfo->buf = pDevice->tx0_bufs + (i)*PKT_BUF_SZ;
944                         pDesc->pTDInfo->buf_dma = pDevice->tx_bufs_dma0 + (i)*PKT_BUF_SZ;
945                 }
946                 pDesc->next = &(pDevice->apTD0Rings[(i+1) % pDevice->sOpts.nTxDescs[0]]);
947                 pDesc->pTDInfo->curr_desc = cpu_to_le32(curr);
948                 pDesc->next_desc = cpu_to_le32(curr+sizeof(STxDesc));
949         }
950
951         if (i > 0)
952                 pDevice->apTD0Rings[i-1].next_desc = cpu_to_le32(pDevice->td0_pool_dma);
953         pDevice->apTailTD[0] = pDevice->apCurrTD[0] = &(pDevice->apTD0Rings[0]);
954 }
955
956 static void device_init_td1_ring(struct vnt_private *pDevice)
957 {
958         int i;
959         dma_addr_t  curr;
960         PSTxDesc    pDesc;
961
962         /* Init the TD ring entries */
963         curr = pDevice->td1_pool_dma;
964         for (i = 0; i < pDevice->sOpts.nTxDescs[1]; i++, curr += sizeof(STxDesc)) {
965                 pDesc = &(pDevice->apTD1Rings[i]);
966                 pDesc->pTDInfo = alloc_td_info();
967                 ASSERT(pDesc->pTDInfo);
968                 if (pDevice->flags & DEVICE_FLAGS_TX_ALIGN) {
969                         pDesc->pTDInfo->buf = pDevice->tx1_bufs + (i) * PKT_BUF_SZ;
970                         pDesc->pTDInfo->buf_dma = pDevice->tx_bufs_dma1 + (i) * PKT_BUF_SZ;
971                 }
972                 pDesc->next = &(pDevice->apTD1Rings[(i + 1) % pDevice->sOpts.nTxDescs[1]]);
973                 pDesc->pTDInfo->curr_desc = cpu_to_le32(curr);
974                 pDesc->next_desc = cpu_to_le32(curr+sizeof(STxDesc));
975         }
976
977         if (i > 0)
978                 pDevice->apTD1Rings[i-1].next_desc = cpu_to_le32(pDevice->td1_pool_dma);
979         pDevice->apTailTD[1] = pDevice->apCurrTD[1] = &(pDevice->apTD1Rings[0]);
980 }
981
982 static void device_free_td0_ring(struct vnt_private *pDevice)
983 {
984         int i;
985
986         for (i = 0; i < pDevice->sOpts.nTxDescs[0]; i++) {
987                 PSTxDesc        pDesc = &(pDevice->apTD0Rings[i]);
988                 PDEVICE_TD_INFO  pTDInfo = pDesc->pTDInfo;
989
990                 if (pTDInfo->skb_dma && (pTDInfo->skb_dma != pTDInfo->buf_dma))
991                         pci_unmap_single(pDevice->pcid, pTDInfo->skb_dma,
992                                          pTDInfo->skb->len, PCI_DMA_TODEVICE);
993
994                 if (pTDInfo->skb)
995                         dev_kfree_skb(pTDInfo->skb);
996
997                 kfree(pDesc->pTDInfo);
998         }
999 }
1000
1001 static void device_free_td1_ring(struct vnt_private *pDevice)
1002 {
1003         int i;
1004
1005         for (i = 0; i < pDevice->sOpts.nTxDescs[1]; i++) {
1006                 PSTxDesc        pDesc = &(pDevice->apTD1Rings[i]);
1007                 PDEVICE_TD_INFO  pTDInfo = pDesc->pTDInfo;
1008
1009                 if (pTDInfo->skb_dma && (pTDInfo->skb_dma != pTDInfo->buf_dma))
1010                         pci_unmap_single(pDevice->pcid, pTDInfo->skb_dma,
1011                                          pTDInfo->skb->len, PCI_DMA_TODEVICE);
1012
1013                 if (pTDInfo->skb)
1014                         dev_kfree_skb(pTDInfo->skb);
1015
1016                 kfree(pDesc->pTDInfo);
1017         }
1018 }
1019
1020 /*-----------------------------------------------------------------*/
1021
1022 static int device_rx_srv(struct vnt_private *pDevice, unsigned int uIdx)
1023 {
1024         PSRxDesc    pRD;
1025         int works = 0;
1026
1027         for (pRD = pDevice->pCurrRD[uIdx];
1028              pRD->m_rd0RD0.f1Owner == OWNED_BY_HOST;
1029              pRD = pRD->next) {
1030                 if (works++ > 15)
1031                         break;
1032                 if (vnt_receive_frame(pDevice, pRD)) {
1033                         if (!device_alloc_rx_buf(pDevice, pRD)) {
1034                                 dev_err(&pDevice->pcid->dev,
1035                                         "can not allocate rx buf\n");
1036                                 break;
1037                         }
1038                 }
1039                 pRD->m_rd0RD0.f1Owner = OWNED_BY_NIC;
1040         }
1041
1042         pDevice->pCurrRD[uIdx] = pRD;
1043
1044         return works;
1045 }
1046
1047 static bool device_alloc_rx_buf(struct vnt_private *pDevice, PSRxDesc pRD)
1048 {
1049         PDEVICE_RD_INFO pRDInfo = pRD->pRDInfo;
1050
1051         pRDInfo->skb = dev_alloc_skb((int)pDevice->rx_buf_sz);
1052         if (pRDInfo->skb == NULL)
1053                 return false;
1054         ASSERT(pRDInfo->skb);
1055
1056         pRDInfo->skb_dma =
1057                 pci_map_single(pDevice->pcid,
1058                                skb_put(pRDInfo->skb, skb_tailroom(pRDInfo->skb)),
1059                                pDevice->rx_buf_sz, PCI_DMA_FROMDEVICE);
1060
1061         *((unsigned int *)&(pRD->m_rd0RD0)) = 0; /* FIX cast */
1062
1063         pRD->m_rd0RD0.wResCount = cpu_to_le16(pDevice->rx_buf_sz);
1064         pRD->m_rd0RD0.f1Owner = OWNED_BY_NIC;
1065         pRD->m_rd1RD1.wReqCount = cpu_to_le16(pDevice->rx_buf_sz);
1066         pRD->buff_addr = cpu_to_le32(pRDInfo->skb_dma);
1067
1068         return true;
1069 }
1070
1071 bool device_alloc_frag_buf(struct vnt_private *pDevice,
1072                            PSDeFragControlBlock pDeF)
1073 {
1074         pDeF->skb = dev_alloc_skb((int)pDevice->rx_buf_sz);
1075         if (pDeF->skb == NULL)
1076                 return false;
1077         ASSERT(pDeF->skb);
1078
1079         return true;
1080 }
1081
1082 static const u8 fallback_rate0[5][5] = {
1083         {RATE_18M, RATE_18M, RATE_12M, RATE_12M, RATE_12M},
1084         {RATE_24M, RATE_24M, RATE_18M, RATE_12M, RATE_12M},
1085         {RATE_36M, RATE_36M, RATE_24M, RATE_18M, RATE_18M},
1086         {RATE_48M, RATE_48M, RATE_36M, RATE_24M, RATE_24M},
1087         {RATE_54M, RATE_54M, RATE_48M, RATE_36M, RATE_36M}
1088 };
1089
1090 static const u8 fallback_rate1[5][5] = {
1091         {RATE_18M, RATE_18M, RATE_12M, RATE_6M, RATE_6M},
1092         {RATE_24M, RATE_24M, RATE_18M, RATE_6M, RATE_6M},
1093         {RATE_36M, RATE_36M, RATE_24M, RATE_12M, RATE_12M},
1094         {RATE_48M, RATE_48M, RATE_24M, RATE_12M, RATE_12M},
1095         {RATE_54M, RATE_54M, RATE_36M, RATE_18M, RATE_18M}
1096 };
1097
1098 static int vnt_int_report_rate(struct vnt_private *priv,
1099                                PDEVICE_TD_INFO context, u8 tsr0, u8 tsr1)
1100 {
1101         struct vnt_tx_fifo_head *fifo_head;
1102         struct ieee80211_tx_info *info;
1103         struct ieee80211_rate *rate;
1104         u16 fb_option;
1105         u8 tx_retry = (tsr0 & TSR0_NCR);
1106         s8 idx;
1107
1108         if (!context)
1109                 return -ENOMEM;
1110
1111         if (!context->skb)
1112                 return -EINVAL;
1113
1114         fifo_head = (struct vnt_tx_fifo_head *)context->buf;
1115         fb_option = (le16_to_cpu(fifo_head->fifo_ctl) &
1116                         (FIFOCTL_AUTO_FB_0 | FIFOCTL_AUTO_FB_1));
1117
1118         info = IEEE80211_SKB_CB(context->skb);
1119         idx = info->control.rates[0].idx;
1120
1121         if (fb_option && !(tsr1 & TSR1_TERR)) {
1122                 u8 tx_rate;
1123                 u8 retry = tx_retry;
1124
1125                 rate = ieee80211_get_tx_rate(priv->hw, info);
1126                 tx_rate = rate->hw_value - RATE_18M;
1127
1128                 if (retry > 4)
1129                         retry = 4;
1130
1131                 if (fb_option & FIFOCTL_AUTO_FB_0)
1132                         tx_rate = fallback_rate0[tx_rate][retry];
1133                 else if (fb_option & FIFOCTL_AUTO_FB_1)
1134                         tx_rate = fallback_rate1[tx_rate][retry];
1135
1136                 if (info->band == IEEE80211_BAND_5GHZ)
1137                         idx = tx_rate - RATE_6M;
1138                 else
1139                         idx = tx_rate;
1140         }
1141
1142         ieee80211_tx_info_clear_status(info);
1143
1144         info->status.rates[0].count = tx_retry;
1145
1146         if (!(tsr1 & TSR1_TERR)) {
1147                 info->status.rates[0].idx = idx;
1148                 info->flags |= IEEE80211_TX_STAT_ACK;
1149         }
1150
1151         return 0;
1152 }
1153
1154 static int device_tx_srv(struct vnt_private *pDevice, unsigned int uIdx)
1155 {
1156         PSTxDesc                 pTD;
1157         int                      works = 0;
1158         unsigned char byTsr0;
1159         unsigned char byTsr1;
1160
1161         for (pTD = pDevice->apTailTD[uIdx]; pDevice->iTDUsed[uIdx] > 0; pTD = pTD->next) {
1162                 if (pTD->m_td0TD0.f1Owner == OWNED_BY_NIC)
1163                         break;
1164                 if (works++ > 15)
1165                         break;
1166
1167                 byTsr0 = pTD->m_td0TD0.byTSR0;
1168                 byTsr1 = pTD->m_td0TD0.byTSR1;
1169
1170                 //Only the status of first TD in the chain is correct
1171                 if (pTD->m_td1TD1.byTCR & TCR_STP) {
1172                         if ((pTD->pTDInfo->byFlags & TD_FLAGS_NETIF_SKB) != 0) {
1173
1174                                 vnt_int_report_rate(pDevice, pTD->pTDInfo, byTsr0, byTsr1);
1175
1176                                 if (!(byTsr1 & TSR1_TERR)) {
1177                                         if (byTsr0 != 0) {
1178                                                 pr_debug(" Tx[%d] OK but has error. tsr1[%02X] tsr0[%02X]\n",
1179                                                          (int)uIdx, byTsr1,
1180                                                          byTsr0);
1181                                         }
1182                                 } else {
1183                                         pr_debug(" Tx[%d] dropped & tsr1[%02X] tsr0[%02X]\n",
1184                                                  (int)uIdx, byTsr1, byTsr0);
1185                                 }
1186                         }
1187
1188                         if (byTsr1 & TSR1_TERR) {
1189                                 if ((pTD->pTDInfo->byFlags & TD_FLAGS_PRIV_SKB) != 0) {
1190                                         pr_debug(" Tx[%d] fail has error. tsr1[%02X] tsr0[%02X]\n",
1191                                                  (int)uIdx, byTsr1, byTsr0);
1192                                 }
1193                         }
1194                         device_free_tx_buf(pDevice, pTD);
1195                         pDevice->iTDUsed[uIdx]--;
1196
1197                         /* Make sure queue is available */
1198                         if (AVAIL_TD(pDevice, uIdx))
1199                                 ieee80211_wake_queues(pDevice->hw);
1200                 }
1201         }
1202
1203         pDevice->apTailTD[uIdx] = pTD;
1204
1205         return works;
1206 }
1207
1208 static void device_error(struct vnt_private *pDevice, unsigned short status)
1209 {
1210         if (status & ISR_FETALERR) {
1211                 dev_err(&pDevice->pcid->dev, "Hardware fatal error\n");
1212
1213                 MACbShutdown(pDevice->PortOffset);
1214                 return;
1215         }
1216 }
1217
1218 static void device_free_tx_buf(struct vnt_private *pDevice, PSTxDesc pDesc)
1219 {
1220         PDEVICE_TD_INFO  pTDInfo = pDesc->pTDInfo;
1221         struct sk_buff *skb = pTDInfo->skb;
1222
1223         // pre-allocated buf_dma can't be unmapped.
1224         if (pTDInfo->skb_dma && (pTDInfo->skb_dma != pTDInfo->buf_dma)) {
1225                 pci_unmap_single(pDevice->pcid, pTDInfo->skb_dma, skb->len,
1226                                  PCI_DMA_TODEVICE);
1227         }
1228
1229         if (pTDInfo->byFlags & TD_FLAGS_NETIF_SKB)
1230                 ieee80211_tx_status_irqsafe(pDevice->hw, skb);
1231         else
1232                 dev_kfree_skb_irq(skb);
1233
1234         pTDInfo->skb_dma = 0;
1235         pTDInfo->skb = NULL;
1236         pTDInfo->byFlags = 0;
1237 }
1238
1239 static  irqreturn_t  device_intr(int irq,  void *dev_instance)
1240 {
1241         struct vnt_private *pDevice = dev_instance;
1242         int             max_count = 0;
1243         unsigned long dwMIBCounter = 0;
1244         unsigned char byOrgPageSel = 0;
1245         int             handled = 0;
1246         int             ii = 0;
1247         unsigned long flags;
1248
1249         MACvReadISR(pDevice->PortOffset, &pDevice->dwIsr);
1250
1251         if (pDevice->dwIsr == 0)
1252                 return IRQ_RETVAL(handled);
1253
1254         if (pDevice->dwIsr == 0xffffffff) {
1255                 pr_debug("dwIsr = 0xffff\n");
1256                 return IRQ_RETVAL(handled);
1257         }
1258
1259         handled = 1;
1260         MACvIntDisable(pDevice->PortOffset);
1261
1262         spin_lock_irqsave(&pDevice->lock, flags);
1263
1264         //Make sure current page is 0
1265         VNSvInPortB(pDevice->PortOffset + MAC_REG_PAGE1SEL, &byOrgPageSel);
1266         if (byOrgPageSel == 1)
1267                 MACvSelectPage0(pDevice->PortOffset);
1268         else
1269                 byOrgPageSel = 0;
1270
1271         MACvReadMIBCounter(pDevice->PortOffset, &dwMIBCounter);
1272         // TBD....
1273         // Must do this after doing rx/tx, cause ISR bit is slow
1274         // than RD/TD write back
1275         // update ISR counter
1276         STAvUpdate802_11Counter(&pDevice->s802_11Counter, &pDevice->scStatistic , dwMIBCounter);
1277         while (pDevice->dwIsr != 0) {
1278                 STAvUpdateIsrStatCounter(&pDevice->scStatistic, pDevice->dwIsr);
1279                 MACvWriteISR(pDevice->PortOffset, pDevice->dwIsr);
1280
1281                 if (pDevice->dwIsr & ISR_FETALERR) {
1282                         pr_debug(" ISR_FETALERR\n");
1283                         VNSvOutPortB(pDevice->PortOffset + MAC_REG_SOFTPWRCTL, 0);
1284                         VNSvOutPortW(pDevice->PortOffset + MAC_REG_SOFTPWRCTL, SOFTPWRCTL_SWPECTI);
1285                         device_error(pDevice, pDevice->dwIsr);
1286                 }
1287
1288                 if (pDevice->dwIsr & ISR_TBTT) {
1289                         if (pDevice->vif &&
1290                             pDevice->op_mode != NL80211_IFTYPE_ADHOC) {
1291                                 if (pDevice->bUpdateBBVGA &&
1292                                     !(pDevice->hw->conf.flags & IEEE80211_CONF_OFFCHANNEL) &&
1293                                     pDevice->vif->bss_conf.assoc &&
1294                                     pDevice->uCurrRSSI) {
1295                                         long            ldBm;
1296
1297                                         RFvRSSITodBm(pDevice, (unsigned char) pDevice->uCurrRSSI, &ldBm);
1298                                         for (ii = 0; ii < BB_VGA_LEVEL; ii++) {
1299                                                 if (ldBm < pDevice->ldBmThreshold[ii]) {
1300                                                         pDevice->byBBVGANew = pDevice->abyBBVGA[ii];
1301                                                         break;
1302                                                 }
1303                                         }
1304                                         if (pDevice->byBBVGANew != pDevice->byBBVGACurrent) {
1305                                                 pDevice->uBBVGADiffCount++;
1306                                                 if (pDevice->uBBVGADiffCount == 1) {
1307                                                         // first VGA diff gain
1308                                                         BBvSetVGAGainOffset(pDevice, pDevice->byBBVGANew);
1309                                                         pr_debug("First RSSI[%d] NewGain[%d] OldGain[%d] Count[%d]\n",
1310                                                                  (int)ldBm,
1311                                                                  pDevice->byBBVGANew,
1312                                                                  pDevice->byBBVGACurrent,
1313                                                                  (int)pDevice->uBBVGADiffCount);
1314                                                 }
1315                                                 if (pDevice->uBBVGADiffCount >= BB_VGA_CHANGE_THRESHOLD) {
1316                                                         pr_debug("RSSI[%d] NewGain[%d] OldGain[%d] Count[%d]\n",
1317                                                                  (int)ldBm,
1318                                                                  pDevice->byBBVGANew,
1319                                                                  pDevice->byBBVGACurrent,
1320                                                                  (int)pDevice->uBBVGADiffCount);
1321                                                         BBvSetVGAGainOffset(pDevice, pDevice->byBBVGANew);
1322                                                 }
1323                                         } else {
1324                                                 pDevice->uBBVGADiffCount = 1;
1325                                         }
1326                                 }
1327                         }
1328
1329                         pDevice->bBeaconSent = false;
1330                         if (pDevice->bEnablePSMode)
1331                                 PSbIsNextTBTTWakeUp((void *)pDevice);
1332
1333                         if ((pDevice->op_mode == NL80211_IFTYPE_AP ||
1334                             pDevice->op_mode == NL80211_IFTYPE_ADHOC) &&
1335                             pDevice->vif->bss_conf.enable_beacon) {
1336                                 MACvOneShotTimer1MicroSec(pDevice->PortOffset,
1337                                                           (pDevice->vif->bss_conf.beacon_int - MAKE_BEACON_RESERVED) << 10);
1338                         }
1339
1340                         /* TODO: adhoc PS mode */
1341
1342                 }
1343
1344                 if (pDevice->dwIsr & ISR_BNTX) {
1345                         if (pDevice->op_mode == NL80211_IFTYPE_ADHOC) {
1346                                 pDevice->bIsBeaconBufReadySet = false;
1347                                 pDevice->cbBeaconBufReadySetCnt = 0;
1348                         }
1349
1350                         pDevice->bBeaconSent = true;
1351                 }
1352
1353                 if (pDevice->dwIsr & ISR_RXDMA0)
1354                         max_count += device_rx_srv(pDevice, TYPE_RXDMA0);
1355
1356                 if (pDevice->dwIsr & ISR_RXDMA1)
1357                         max_count += device_rx_srv(pDevice, TYPE_RXDMA1);
1358
1359                 if (pDevice->dwIsr & ISR_TXDMA0)
1360                         max_count += device_tx_srv(pDevice, TYPE_TXDMA0);
1361
1362                 if (pDevice->dwIsr & ISR_AC0DMA)
1363                         max_count += device_tx_srv(pDevice, TYPE_AC0DMA);
1364
1365                 if (pDevice->dwIsr & ISR_SOFTTIMER1) {
1366                         if (pDevice->vif) {
1367                                 if (pDevice->vif->bss_conf.enable_beacon)
1368                                         vnt_beacon_make(pDevice, pDevice->vif);
1369                         }
1370                 }
1371
1372                 MACvReadISR(pDevice->PortOffset, &pDevice->dwIsr);
1373
1374                 MACvReceive0(pDevice->PortOffset);
1375                 MACvReceive1(pDevice->PortOffset);
1376
1377                 if (max_count > pDevice->sOpts.int_works)
1378                         break;
1379         }
1380
1381         if (byOrgPageSel == 1)
1382                 MACvSelectPage1(pDevice->PortOffset);
1383
1384         spin_unlock_irqrestore(&pDevice->lock, flags);
1385
1386         MACvIntEnable(pDevice->PortOffset, IMR_MASK_VALUE);
1387
1388         return IRQ_RETVAL(handled);
1389 }
1390
1391 static int vnt_tx_packet(struct vnt_private *priv, struct sk_buff *skb)
1392 {
1393         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1394         PSTxDesc head_td;
1395         u32 dma_idx = TYPE_AC0DMA;
1396         unsigned long flags;
1397
1398         spin_lock_irqsave(&priv->lock, flags);
1399
1400         if (!ieee80211_is_data(hdr->frame_control))
1401                 dma_idx = TYPE_TXDMA0;
1402
1403         if (AVAIL_TD(priv, dma_idx) < 1) {
1404                 spin_unlock_irqrestore(&priv->lock, flags);
1405                 return -ENOMEM;
1406         }
1407
1408         head_td = priv->apCurrTD[dma_idx];
1409
1410         head_td->m_td1TD1.byTCR = (TCR_EDP|TCR_STP);
1411
1412         head_td->pTDInfo->skb = skb;
1413
1414         priv->iTDUsed[dma_idx]++;
1415
1416         /* Take ownership */
1417         wmb();
1418         head_td->m_td0TD0.f1Owner = OWNED_BY_NIC;
1419
1420         /* get Next */
1421         wmb();
1422         priv->apCurrTD[dma_idx] = head_td->next;
1423
1424         spin_unlock_irqrestore(&priv->lock, flags);
1425
1426         vnt_generate_fifo_header(priv, dma_idx, head_td, skb);
1427
1428         if (MACbIsRegBitsOn(priv->PortOffset, MAC_REG_PSCTL, PSCTL_PS))
1429                 MACbPSWakeup(priv->PortOffset);
1430
1431         spin_lock_irqsave(&priv->lock, flags);
1432
1433         priv->bPWBitOn = false;
1434
1435         head_td->pTDInfo->byFlags = TD_FLAGS_NETIF_SKB;
1436
1437         if (dma_idx == TYPE_AC0DMA)
1438                 MACvTransmitAC0(priv->PortOffset);
1439         else
1440                 MACvTransmit0(priv->PortOffset);
1441
1442         spin_unlock_irqrestore(&priv->lock, flags);
1443
1444         return 0;
1445 }
1446
1447 static void vnt_tx_80211(struct ieee80211_hw *hw,
1448                          struct ieee80211_tx_control *control,
1449                          struct sk_buff *skb)
1450 {
1451         struct vnt_private *priv = hw->priv;
1452
1453         ieee80211_stop_queues(hw);
1454
1455         if (vnt_tx_packet(priv, skb)) {
1456                 ieee80211_free_txskb(hw, skb);
1457
1458                 ieee80211_wake_queues(hw);
1459         }
1460 }
1461
1462 static int vnt_start(struct ieee80211_hw *hw)
1463 {
1464         struct vnt_private *priv = hw->priv;
1465         int ret;
1466
1467         priv->rx_buf_sz = PKT_BUF_SZ;
1468         if (!device_init_rings(priv))
1469                 return -ENOMEM;
1470
1471         ret = request_irq(priv->pcid->irq, &device_intr,
1472                           IRQF_SHARED, "vt6655", priv);
1473         if (ret) {
1474                 dev_dbg(&priv->pcid->dev, "failed to start irq\n");
1475                 return ret;
1476         }
1477
1478         dev_dbg(&priv->pcid->dev, "call device init rd0 ring\n");
1479         device_init_rd0_ring(priv);
1480         device_init_rd1_ring(priv);
1481         device_init_defrag_cb(priv);
1482         device_init_td0_ring(priv);
1483         device_init_td1_ring(priv);
1484
1485         device_init_registers(priv);
1486
1487         dev_dbg(&priv->pcid->dev, "call MACvIntEnable\n");
1488         MACvIntEnable(priv->PortOffset, IMR_MASK_VALUE);
1489
1490         ieee80211_wake_queues(hw);
1491
1492         return 0;
1493 }
1494
1495 static void vnt_stop(struct ieee80211_hw *hw)
1496 {
1497         struct vnt_private *priv = hw->priv;
1498
1499         ieee80211_stop_queues(hw);
1500
1501         MACbShutdown(priv->PortOffset);
1502         MACbSoftwareReset(priv->PortOffset);
1503         CARDbRadioPowerOff(priv);
1504
1505         device_free_td0_ring(priv);
1506         device_free_td1_ring(priv);
1507         device_free_rd0_ring(priv);
1508         device_free_rd1_ring(priv);
1509         device_free_frag_buf(priv);
1510         device_free_rings(priv);
1511
1512         free_irq(priv->pcid->irq, priv);
1513 }
1514
1515 static int vnt_add_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1516 {
1517         struct vnt_private *priv = hw->priv;
1518
1519         priv->vif = vif;
1520
1521         switch (vif->type) {
1522         case NL80211_IFTYPE_STATION:
1523                 if (priv->bDiversityRegCtlON)
1524                         device_init_diversity_timer(priv);
1525                 break;
1526         case NL80211_IFTYPE_ADHOC:
1527                 MACvRegBitsOff(priv->PortOffset, MAC_REG_RCR, RCR_UNICAST);
1528
1529                 MACvRegBitsOn(priv->PortOffset, MAC_REG_HOSTCR, HOSTCR_ADHOC);
1530
1531                 break;
1532         case NL80211_IFTYPE_AP:
1533                 MACvRegBitsOff(priv->PortOffset, MAC_REG_RCR, RCR_UNICAST);
1534
1535                 MACvRegBitsOn(priv->PortOffset, MAC_REG_HOSTCR, HOSTCR_AP);
1536
1537                 break;
1538         default:
1539                 return -EOPNOTSUPP;
1540         }
1541
1542         priv->op_mode = vif->type;
1543
1544         return 0;
1545 }
1546
1547 static void vnt_remove_interface(struct ieee80211_hw *hw,
1548                                  struct ieee80211_vif *vif)
1549 {
1550         struct vnt_private *priv = hw->priv;
1551
1552         switch (vif->type) {
1553         case NL80211_IFTYPE_STATION:
1554                 if (priv->bDiversityRegCtlON) {
1555                         del_timer(&priv->TimerSQ3Tmax1);
1556                         del_timer(&priv->TimerSQ3Tmax2);
1557                         del_timer(&priv->TimerSQ3Tmax3);
1558                 }
1559                 break;
1560         case NL80211_IFTYPE_ADHOC:
1561                 MACvRegBitsOff(priv->PortOffset, MAC_REG_TCR, TCR_AUTOBCNTX);
1562                 MACvRegBitsOff(priv->PortOffset,
1563                                MAC_REG_TFTCTL, TFTCTL_TSFCNTREN);
1564                 MACvRegBitsOff(priv->PortOffset, MAC_REG_HOSTCR, HOSTCR_ADHOC);
1565                 break;
1566         case NL80211_IFTYPE_AP:
1567                 MACvRegBitsOff(priv->PortOffset, MAC_REG_TCR, TCR_AUTOBCNTX);
1568                 MACvRegBitsOff(priv->PortOffset,
1569                                MAC_REG_TFTCTL, TFTCTL_TSFCNTREN);
1570                 MACvRegBitsOff(priv->PortOffset, MAC_REG_HOSTCR, HOSTCR_AP);
1571                 break;
1572         default:
1573                 break;
1574         }
1575
1576         priv->op_mode = NL80211_IFTYPE_UNSPECIFIED;
1577 }
1578
1579
1580 static int vnt_config(struct ieee80211_hw *hw, u32 changed)
1581 {
1582         struct vnt_private *priv = hw->priv;
1583         struct ieee80211_conf *conf = &hw->conf;
1584         u8 bb_type;
1585
1586         if (changed & IEEE80211_CONF_CHANGE_PS) {
1587                 if (conf->flags & IEEE80211_CONF_PS)
1588                         PSvEnablePowerSaving(priv, conf->listen_interval);
1589                 else
1590                         PSvDisablePowerSaving(priv);
1591         }
1592
1593         if ((changed & IEEE80211_CONF_CHANGE_CHANNEL) ||
1594             (conf->flags & IEEE80211_CONF_OFFCHANNEL)) {
1595                 set_channel(priv, conf->chandef.chan->hw_value);
1596
1597                 if (conf->chandef.chan->band == IEEE80211_BAND_5GHZ)
1598                         bb_type = BB_TYPE_11A;
1599                 else
1600                         bb_type = BB_TYPE_11G;
1601
1602                 if (priv->byBBType != bb_type) {
1603                         priv->byBBType = bb_type;
1604
1605                         CARDbSetPhyParameter(priv,
1606                                              priv->byBBType, 0, 0, NULL, NULL);
1607                 }
1608         }
1609
1610         if (changed & IEEE80211_CONF_CHANGE_POWER) {
1611                 if (priv->byBBType == BB_TYPE_11B)
1612                         priv->wCurrentRate = RATE_1M;
1613                 else
1614                         priv->wCurrentRate = RATE_54M;
1615
1616                 RFbSetPower(priv, priv->wCurrentRate,
1617                             conf->chandef.chan->hw_value);
1618         }
1619
1620         return 0;
1621 }
1622
1623 static void vnt_bss_info_changed(struct ieee80211_hw *hw,
1624                 struct ieee80211_vif *vif, struct ieee80211_bss_conf *conf,
1625                 u32 changed)
1626 {
1627         struct vnt_private *priv = hw->priv;
1628
1629         priv->current_aid = conf->aid;
1630
1631         if (changed & BSS_CHANGED_BSSID)
1632                 MACvWriteBSSIDAddress(priv->PortOffset, (u8 *)conf->bssid);
1633
1634         if (changed & BSS_CHANGED_BASIC_RATES) {
1635                 priv->basic_rates = conf->basic_rates;
1636
1637                 CARDvUpdateBasicTopRate(priv);
1638
1639                 dev_dbg(&priv->pcid->dev,
1640                         "basic rates %x\n", conf->basic_rates);
1641         }
1642
1643         if (changed & BSS_CHANGED_ERP_PREAMBLE) {
1644                 if (conf->use_short_preamble) {
1645                         MACvEnableBarkerPreambleMd(priv->PortOffset);
1646                         priv->byPreambleType = true;
1647                 } else {
1648                         MACvDisableBarkerPreambleMd(priv->PortOffset);
1649                         priv->byPreambleType = false;
1650                 }
1651         }
1652
1653         if (changed & BSS_CHANGED_ERP_CTS_PROT) {
1654                 if (conf->use_cts_prot)
1655                         MACvEnableProtectMD(priv->PortOffset);
1656                 else
1657                         MACvDisableProtectMD(priv->PortOffset);
1658         }
1659
1660         if (changed & BSS_CHANGED_ERP_SLOT) {
1661                 if (conf->use_short_slot)
1662                         priv->bShortSlotTime = true;
1663                 else
1664                         priv->bShortSlotTime = false;
1665
1666                 vUpdateIFS(priv);
1667                 CARDbSetPhyParameter(priv, priv->byBBType, 0, 0, NULL, NULL);
1668                 BBvSetVGAGainOffset(priv, priv->abyBBVGA[0]);
1669         }
1670
1671         if (changed & BSS_CHANGED_TXPOWER)
1672                 RFbSetPower(priv, priv->wCurrentRate,
1673                             conf->chandef.chan->hw_value);
1674
1675         if (changed & BSS_CHANGED_BEACON_ENABLED) {
1676                 dev_dbg(&priv->pcid->dev,
1677                         "Beacon enable %d\n", conf->enable_beacon);
1678
1679                 if (conf->enable_beacon) {
1680                         vnt_beacon_enable(priv, vif, conf);
1681
1682                         MACvRegBitsOn(priv, MAC_REG_TCR, TCR_AUTOBCNTX);
1683                 } else {
1684                         MACvRegBitsOff(priv, MAC_REG_TCR, TCR_AUTOBCNTX);
1685                 }
1686         }
1687
1688         if (changed & BSS_CHANGED_ASSOC && priv->op_mode != NL80211_IFTYPE_AP) {
1689                 if (conf->assoc) {
1690                         CARDbUpdateTSF(priv, conf->beacon_rate->hw_value,
1691                                        conf->sync_device_ts, conf->sync_tsf);
1692
1693                         CARDbSetBeaconPeriod(priv, conf->beacon_int);
1694
1695                         CARDvSetFirstNextTBTT(priv->PortOffset,
1696                                               conf->beacon_int);
1697                 } else {
1698                         VNSvOutPortB(priv->PortOffset + MAC_REG_TFTCTL,
1699                                      TFTCTL_TSFCNTRST);
1700                         VNSvOutPortB(priv->PortOffset + MAC_REG_TFTCTL,
1701                                      TFTCTL_TSFCNTREN);
1702                 }
1703         }
1704 }
1705
1706 static u64 vnt_prepare_multicast(struct ieee80211_hw *hw,
1707         struct netdev_hw_addr_list *mc_list)
1708 {
1709         struct vnt_private *priv = hw->priv;
1710         struct netdev_hw_addr *ha;
1711         u64 mc_filter = 0;
1712         u32 bit_nr = 0;
1713
1714         netdev_hw_addr_list_for_each(ha, mc_list) {
1715                 bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
1716
1717                 mc_filter |= 1ULL << (bit_nr & 0x3f);
1718         }
1719
1720         priv->mc_list_count = mc_list->count;
1721
1722         return mc_filter;
1723 }
1724
1725 static void vnt_configure(struct ieee80211_hw *hw,
1726         unsigned int changed_flags, unsigned int *total_flags, u64 multicast)
1727 {
1728         struct vnt_private *priv = hw->priv;
1729         u8 rx_mode = 0;
1730
1731         *total_flags &= FIF_ALLMULTI | FIF_OTHER_BSS | FIF_PROMISC_IN_BSS |
1732                 FIF_BCN_PRBRESP_PROMISC;
1733
1734         VNSvInPortB(priv->PortOffset + MAC_REG_RCR, &rx_mode);
1735
1736         dev_dbg(&priv->pcid->dev, "rx mode in = %x\n", rx_mode);
1737
1738         if (changed_flags & FIF_PROMISC_IN_BSS) {
1739                 /* unconditionally log net taps */
1740                 if (*total_flags & FIF_PROMISC_IN_BSS)
1741                         rx_mode |= RCR_UNICAST;
1742                 else
1743                         rx_mode &= ~RCR_UNICAST;
1744         }
1745
1746         if (changed_flags & FIF_ALLMULTI) {
1747                 if (*total_flags & FIF_ALLMULTI) {
1748                         if (priv->mc_list_count > 2) {
1749                                 MACvSelectPage1(priv->PortOffset);
1750
1751                                 VNSvOutPortD(priv->PortOffset +
1752                                              MAC_REG_MAR0, 0xffffffff);
1753                                 VNSvOutPortD(priv->PortOffset +
1754                                             MAC_REG_MAR0 + 4, 0xffffffff);
1755
1756                                 MACvSelectPage0(priv->PortOffset);
1757                         } else {
1758                                 MACvSelectPage1(priv->PortOffset);
1759
1760                                 VNSvOutPortD(priv->PortOffset +
1761                                              MAC_REG_MAR0, (u32)multicast);
1762                                 VNSvOutPortD(priv->PortOffset +
1763                                              MAC_REG_MAR0 + 4,
1764                                              (u32)(multicast >> 32));
1765
1766                                 MACvSelectPage0(priv->PortOffset);
1767                         }
1768
1769                         rx_mode |= RCR_MULTICAST | RCR_BROADCAST;
1770                 } else {
1771                         rx_mode &= ~(RCR_MULTICAST | RCR_BROADCAST);
1772                 }
1773         }
1774
1775         if (changed_flags & (FIF_OTHER_BSS | FIF_BCN_PRBRESP_PROMISC)) {
1776                 rx_mode |= RCR_MULTICAST | RCR_BROADCAST;
1777
1778                 if (*total_flags & (FIF_OTHER_BSS | FIF_BCN_PRBRESP_PROMISC))
1779                         rx_mode &= ~RCR_BSSID;
1780                 else
1781                         rx_mode |= RCR_BSSID;
1782         }
1783
1784         VNSvOutPortB(priv->PortOffset + MAC_REG_RCR, rx_mode);
1785
1786         dev_dbg(&priv->pcid->dev, "rx mode out= %x\n", rx_mode);
1787 }
1788
1789 static int vnt_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
1790         struct ieee80211_vif *vif, struct ieee80211_sta *sta,
1791                 struct ieee80211_key_conf *key)
1792 {
1793         struct vnt_private *priv = hw->priv;
1794
1795         switch (cmd) {
1796         case SET_KEY:
1797                 if (vnt_set_keys(hw, sta, vif, key))
1798                         return -EOPNOTSUPP;
1799                 break;
1800         case DISABLE_KEY:
1801                 if (test_bit(key->hw_key_idx, &priv->key_entry_inuse))
1802                         clear_bit(key->hw_key_idx, &priv->key_entry_inuse);
1803         default:
1804                 break;
1805         }
1806
1807         return 0;
1808 }
1809
1810 static u64 vnt_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1811 {
1812         struct vnt_private *priv = hw->priv;
1813         u64 tsf;
1814
1815         CARDbGetCurrentTSF(priv->PortOffset, &tsf);
1816
1817         return tsf;
1818 }
1819
1820 static void vnt_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1821                         u64 tsf)
1822 {
1823         struct vnt_private *priv = hw->priv;
1824
1825         CARDvUpdateNextTBTT(priv->PortOffset, tsf, vif->bss_conf.beacon_int);
1826 }
1827
1828 static void vnt_reset_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1829 {
1830         struct vnt_private *priv = hw->priv;
1831
1832         /* reset TSF counter */
1833         VNSvOutPortB(priv->PortOffset + MAC_REG_TFTCTL, TFTCTL_TSFCNTRST);
1834 }
1835
1836 static const struct ieee80211_ops vnt_mac_ops = {
1837         .tx                     = vnt_tx_80211,
1838         .start                  = vnt_start,
1839         .stop                   = vnt_stop,
1840         .add_interface          = vnt_add_interface,
1841         .remove_interface       = vnt_remove_interface,
1842         .config                 = vnt_config,
1843         .bss_info_changed       = vnt_bss_info_changed,
1844         .prepare_multicast      = vnt_prepare_multicast,
1845         .configure_filter       = vnt_configure,
1846         .set_key                = vnt_set_key,
1847         .get_tsf                = vnt_get_tsf,
1848         .set_tsf                = vnt_set_tsf,
1849         .reset_tsf              = vnt_reset_tsf,
1850 };
1851
1852 int vnt_init(struct vnt_private *priv)
1853 {
1854         SET_IEEE80211_PERM_ADDR(priv->hw, priv->abyCurrentNetAddr);
1855
1856         vnt_init_bands(priv);
1857
1858         if (ieee80211_register_hw(priv->hw))
1859                 return -ENODEV;
1860
1861         priv->mac_hw = true;
1862
1863         CARDbRadioPowerOff(priv);
1864
1865         return 0;
1866 }
1867
1868 static int
1869 vt6655_probe(struct pci_dev *pcid, const struct pci_device_id *ent)
1870 {
1871         PCHIP_INFO  pChip_info = (PCHIP_INFO)ent->driver_data;
1872         struct vnt_private *priv;
1873         struct ieee80211_hw *hw;
1874         struct wiphy *wiphy;
1875         int         rc;
1876
1877         dev_notice(&pcid->dev,
1878                    "%s Ver. %s\n", DEVICE_FULL_DRV_NAM, DEVICE_VERSION);
1879
1880         dev_notice(&pcid->dev,
1881                    "Copyright (c) 2003 VIA Networking Technologies, Inc.\n");
1882
1883         hw = ieee80211_alloc_hw(sizeof(*priv), &vnt_mac_ops);
1884         if (!hw) {
1885                 dev_err(&pcid->dev, "could not register ieee80211_hw\n");
1886                 return -ENOMEM;
1887         }
1888
1889         priv = hw->priv;
1890
1891         vt6655_init_info(pcid, &priv, pChip_info);
1892
1893         priv->hw = hw;
1894
1895         SET_IEEE80211_DEV(priv->hw, &pcid->dev);
1896
1897         if (pci_enable_device(pcid)) {
1898                 device_free_info(priv);
1899                 return -ENODEV;
1900         }
1901
1902         dev_dbg(&pcid->dev,
1903                 "Before get pci_info memaddr is %x\n", priv->memaddr);
1904
1905         if (!device_get_pci_info(priv, pcid)) {
1906                 dev_err(&pcid->dev, ": Failed to find PCI device.\n");
1907                 device_free_info(priv);
1908                 return -ENODEV;
1909         }
1910
1911 #ifdef  DEBUG
1912         dev_dbg(&pcid->dev,
1913                 "after get pci_info memaddr is %x, io addr is %x,io_size is %d\n",
1914                 priv->memaddr, priv->ioaddr, priv->io_size);
1915         {
1916                 int i;
1917                 u32 bar, len;
1918                 u32 address[] = {
1919                         PCI_BASE_ADDRESS_0,
1920                         PCI_BASE_ADDRESS_1,
1921                         PCI_BASE_ADDRESS_2,
1922                         PCI_BASE_ADDRESS_3,
1923                         PCI_BASE_ADDRESS_4,
1924                         PCI_BASE_ADDRESS_5,
1925                         0};
1926                 for (i = 0; address[i]; i++) {
1927                         pci_read_config_dword(pcid, address[i], &bar);
1928
1929                         dev_dbg(&pcid->dev, "bar %d is %x\n", i, bar);
1930
1931                         if (!bar) {
1932                                 dev_dbg(&pcid->dev,
1933                                         "bar %d not implemented\n", i);
1934                                 continue;
1935                         }
1936
1937                         if (bar & PCI_BASE_ADDRESS_SPACE_IO) {
1938                                 /* This is IO */
1939
1940                                 len = bar & (PCI_BASE_ADDRESS_IO_MASK & 0xffff);
1941                                 len = len & ~(len - 1);
1942
1943                                 dev_dbg(&pcid->dev,
1944                                         "IO space:  len in IO %x, BAR %d\n",
1945                                         len, i);
1946                         } else {
1947                                 len = bar & 0xfffffff0;
1948                                 len = ~len + 1;
1949
1950                                 dev_dbg(&pcid->dev,
1951                                         "len in MEM %x, BAR %d\n", len, i);
1952                         }
1953                 }
1954         }
1955 #endif
1956
1957         priv->PortOffset = ioremap(priv->memaddr & PCI_BASE_ADDRESS_MEM_MASK,
1958                                    priv->io_size);
1959         if (!priv->PortOffset) {
1960                 dev_err(&pcid->dev, ": Failed to IO remapping ..\n");
1961                 device_free_info(priv);
1962                 return -ENODEV;
1963         }
1964
1965         rc = pci_request_regions(pcid, DEVICE_NAME);
1966         if (rc) {
1967                 dev_err(&pcid->dev, ": Failed to find PCI device\n");
1968                 device_free_info(priv);
1969                 return -ENODEV;
1970         }
1971
1972         /* do reset */
1973         if (!MACbSoftwareReset(priv->PortOffset)) {
1974                 dev_err(&pcid->dev, ": Failed to access MAC hardware..\n");
1975                 device_free_info(priv);
1976                 return -ENODEV;
1977         }
1978         /* initial to reload eeprom */
1979         MACvInitialize(priv->PortOffset);
1980         MACvReadEtherAddress(priv->PortOffset, priv->abyCurrentNetAddr);
1981
1982         device_get_options(priv);
1983         device_set_options(priv);
1984         /* Mask out the options cannot be set to the chip */
1985         priv->sOpts.flags &= pChip_info->flags;
1986
1987         /* Enable the chip specified capabilities */
1988         priv->flags = priv->sOpts.flags | (pChip_info->flags & 0xff000000UL);
1989
1990         wiphy = priv->hw->wiphy;
1991
1992         wiphy->frag_threshold = FRAG_THRESH_DEF;
1993         wiphy->rts_threshold = RTS_THRESH_DEF;
1994         wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
1995                 BIT(NL80211_IFTYPE_ADHOC) | BIT(NL80211_IFTYPE_AP);
1996
1997         priv->hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
1998                 IEEE80211_HW_REPORTS_TX_ACK_STATUS |
1999                 IEEE80211_HW_SIGNAL_DBM |
2000                 IEEE80211_HW_TIMING_BEACON_ONLY;
2001
2002         priv->hw->max_signal = 100;
2003
2004         if (vnt_init(priv))
2005                 return -ENODEV;
2006
2007         device_print_info(priv);
2008         pci_set_drvdata(pcid, priv);
2009
2010         return 0;
2011 }
2012
2013 /*------------------------------------------------------------------*/
2014
2015 #ifdef CONFIG_PM
2016 static int vt6655_suspend(struct pci_dev *pcid, pm_message_t state)
2017 {
2018         struct vnt_private *priv = pci_get_drvdata(pcid);
2019         unsigned long flags;
2020
2021         spin_lock_irqsave(&priv->lock, flags);
2022
2023         pci_save_state(pcid);
2024
2025         MACbShutdown(priv->PortOffset);
2026
2027         pci_disable_device(pcid);
2028         pci_set_power_state(pcid, pci_choose_state(pcid, state));
2029
2030         spin_unlock_irqrestore(&priv->lock, flags);
2031
2032         return 0;
2033 }
2034
2035 static int vt6655_resume(struct pci_dev *pcid)
2036 {
2037
2038         pci_set_power_state(pcid, PCI_D0);
2039         pci_enable_wake(pcid, PCI_D0, 0);
2040         pci_restore_state(pcid);
2041
2042         return 0;
2043 }
2044 #endif
2045
2046 MODULE_DEVICE_TABLE(pci, vt6655_pci_id_table);
2047
2048 static struct pci_driver device_driver = {
2049         .name = DEVICE_NAME,
2050         .id_table = vt6655_pci_id_table,
2051         .probe = vt6655_probe,
2052         .remove = vt6655_remove,
2053 #ifdef CONFIG_PM
2054         .suspend = vt6655_suspend,
2055         .resume = vt6655_resume,
2056 #endif
2057 };
2058
2059 static int __init vt6655_init_module(void)
2060 {
2061         int ret;
2062
2063         ret = pci_register_driver(&device_driver);
2064 #ifdef CONFIG_PM
2065         if (ret >= 0)
2066                 register_reboot_notifier(&device_notifier);
2067 #endif
2068
2069         return ret;
2070 }
2071
2072 static void __exit vt6655_cleanup_module(void)
2073 {
2074 #ifdef CONFIG_PM
2075         unregister_reboot_notifier(&device_notifier);
2076 #endif
2077         pci_unregister_driver(&device_driver);
2078 }
2079
2080 module_init(vt6655_init_module);
2081 module_exit(vt6655_cleanup_module);
2082
2083 #ifdef CONFIG_PM
2084 static int
2085 device_notify_reboot(struct notifier_block *nb, unsigned long event, void *p)
2086 {
2087         struct pci_dev *pdev = NULL;
2088
2089         switch (event) {
2090         case SYS_DOWN:
2091         case SYS_HALT:
2092         case SYS_POWER_OFF:
2093                 for_each_pci_dev(pdev) {
2094                         if (pci_dev_driver(pdev) == &device_driver) {
2095                                 if (pci_get_drvdata(pdev))
2096                                         vt6655_suspend(pdev, PMSG_HIBERNATE);
2097                         }
2098                 }
2099         }
2100         return NOTIFY_DONE;
2101 }
2102 #endif