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