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