OSDN Git Service

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