OSDN Git Service

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