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