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