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