Merge tag 'tty-3.10-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/tty
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / staging / vt6656 / main_usb.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: main_usb.c
20  *
21  * Purpose: driver entry for initial, open, close, tx and rx.
22  *
23  * Author: Lyndon Chen
24  *
25  * Date: Dec 8, 2005
26  *
27  * Functions:
28  *
29  *   vt6656_probe - module initial (insmod) driver entry
30  *   device_remove1 - module remove entry
31  *   device_open - allocate dma/descripter resource & initial mac/bbp function
32  *   device_xmit - asynchronous data tx function
33  *   device_set_multi - set mac filter
34  *   device_ioctl - ioctl entry
35  *   device_close - shutdown mac/bbp & free dma/descriptor resource
36  *   device_alloc_frag_buf - rx fragement pre-allocated function
37  *   device_free_tx_bufs - free tx buffer function
38  *   device_dma0_tx_80211- tx 802.11 frame via dma0
39  *   device_dma0_xmit- tx PS buffered frame via dma0
40  *   device_init_registers- initial MAC & BBP & RF internal registers.
41  *   device_init_rings- initial tx/rx ring buffer
42  *   device_init_defrag_cb- initial & allocate de-fragement buffer.
43  *   device_tx_srv- tx interrupt service function
44  *
45  * Revision History:
46  */
47 #undef __NO_VERSION__
48
49 #include "device.h"
50 #include "card.h"
51 #include "baseband.h"
52 #include "mac.h"
53 #include "tether.h"
54 #include "wmgr.h"
55 #include "wctl.h"
56 #include "power.h"
57 #include "wcmd.h"
58 #include "iocmd.h"
59 #include "tcrc.h"
60 #include "rxtx.h"
61 #include "bssdb.h"
62 #include "hostap.h"
63 #include "wpactl.h"
64 #include "iwctl.h"
65 #include "dpc.h"
66 #include "datarate.h"
67 #include "rf.h"
68 #include "firmware.h"
69 #include "rndis.h"
70 #include "control.h"
71 #include "channel.h"
72 #include "int.h"
73 #include "iowpa.h"
74
75 /* static int msglevel = MSG_LEVEL_DEBUG; */
76 static int          msglevel                =MSG_LEVEL_INFO;
77
78 /*
79  * define module options
80  */
81
82 /* version information */
83 #define DRIVER_AUTHOR \
84         "VIA Networking Technologies, Inc., <lyndonchen@vntek.com.tw>"
85 MODULE_AUTHOR(DRIVER_AUTHOR);
86 MODULE_LICENSE("GPL");
87 MODULE_DESCRIPTION(DEVICE_FULL_DRV_NAM);
88
89 #define DEVICE_PARAM(N,D) \
90         static int N[MAX_UINTS]=OPTION_DEFAULT;\
91         module_param_array(N, int, NULL, 0);\
92         MODULE_PARM_DESC(N, D);
93
94 #define RX_DESC_DEF0     64
95 DEVICE_PARAM(RxDescriptors0,"Number of receive usb desc buffer");
96
97 #define TX_DESC_DEF0     64
98 DEVICE_PARAM(TxDescriptors0,"Number of transmit usb desc buffer");
99
100 #define CHANNEL_DEF     6
101 DEVICE_PARAM(Channel, "Channel number");
102
103 /* PreambleType[] is the preamble length used for transmit.
104    0: indicate allows long preamble type
105    1: indicate allows short preamble type
106 */
107
108 #define PREAMBLE_TYPE_DEF     1
109
110 DEVICE_PARAM(PreambleType, "Preamble Type");
111
112 #define RTS_THRESH_DEF     2347
113 DEVICE_PARAM(RTSThreshold, "RTS threshold");
114
115 #define FRAG_THRESH_DEF     2346
116 DEVICE_PARAM(FragThreshold, "Fragmentation threshold");
117
118 #define DATA_RATE_DEF     13
119 /* datarate[] index
120    0: indicate 1 Mbps   0x02
121    1: indicate 2 Mbps   0x04
122    2: indicate 5.5 Mbps 0x0B
123    3: indicate 11 Mbps  0x16
124    4: indicate 6 Mbps   0x0c
125    5: indicate 9 Mbps   0x12
126    6: indicate 12 Mbps  0x18
127    7: indicate 18 Mbps  0x24
128    8: indicate 24 Mbps  0x30
129    9: indicate 36 Mbps  0x48
130   10: indicate 48 Mbps  0x60
131   11: indicate 54 Mbps  0x6c
132   12: indicate 72 Mbps  0x90
133   13: indicate auto rate
134 */
135
136 DEVICE_PARAM(ConnectionRate, "Connection data rate");
137
138 #define OP_MODE_DEF     0
139 DEVICE_PARAM(OPMode, "Infrastruct, adhoc, AP mode ");
140
141 /* OpMode[] is used for transmit.
142    0: indicate infrastruct mode used
143    1: indicate adhoc mode used
144    2: indicate AP mode used
145 */
146
147 /* PSMode[]
148    0: indicate disable power saving mode
149    1: indicate enable power saving mode
150 */
151
152 #define PS_MODE_DEF     0
153 DEVICE_PARAM(PSMode, "Power saving mode");
154
155 #define SHORT_RETRY_DEF     8
156 DEVICE_PARAM(ShortRetryLimit, "Short frame retry limits");
157
158 #define LONG_RETRY_DEF     4
159 DEVICE_PARAM(LongRetryLimit, "long frame retry limits");
160
161 /* BasebandType[] baseband type selected
162    0: indicate 802.11a type
163    1: indicate 802.11b type
164    2: indicate 802.11g type
165 */
166
167 #define BBP_TYPE_DEF     2
168 DEVICE_PARAM(BasebandType, "baseband type");
169
170 /* 80211hEnable[]
171    0: indicate disable 802.11h
172    1: indicate enable 802.11h
173 */
174
175 #define X80211h_MODE_DEF     0
176
177 DEVICE_PARAM(b80211hEnable, "802.11h mode");
178
179 /*
180  * Static vars definitions
181  */
182
183 static struct usb_device_id vt6656_table[] = {
184         {USB_DEVICE(VNT_USB_VENDOR_ID, VNT_USB_PRODUCT_ID)},
185         {}
186 };
187
188 /* frequency list (map channels to frequencies) */
189 /*
190 static const long frequency_list[] = {
191     2412, 2417, 2422, 2427, 2432, 2437, 2442, 2447, 2452, 2457, 2462, 2467, 2472, 2484,
192     4915, 4920, 4925, 4935, 4940, 4945, 4960, 4980,
193     5035, 5040, 5045, 5055, 5060, 5080, 5170, 5180, 5190, 5200, 5210, 5220, 5230, 5240,
194     5260, 5280, 5300, 5320, 5500, 5520, 5540, 5560, 5580, 5600, 5620, 5640, 5660, 5680,
195     5700, 5745, 5765, 5785, 5805, 5825
196         };
197
198 static const struct iw_handler_def      iwctl_handler_def;
199 */
200
201 static int vt6656_probe(struct usb_interface *intf,
202                         const struct usb_device_id *id);
203 static void vt6656_disconnect(struct usb_interface *intf);
204
205 #ifdef CONFIG_PM        /* Minimal support for suspend and resume */
206 static int vt6656_suspend(struct usb_interface *intf, pm_message_t message);
207 static int vt6656_resume(struct usb_interface *intf);
208 #endif /* CONFIG_PM */
209
210 static struct net_device_stats *device_get_stats(struct net_device *dev);
211 static int  device_open(struct net_device *dev);
212 static int  device_xmit(struct sk_buff *skb, struct net_device *dev);
213 static void device_set_multi(struct net_device *dev);
214 static int  device_close(struct net_device *dev);
215 static int  device_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
216
217 static int device_init_registers(struct vnt_private *pDevice,
218         DEVICE_INIT_TYPE InitType);
219 static bool device_init_defrag_cb(struct vnt_private *pDevice);
220 static void device_init_diversity_timer(struct vnt_private *pDevice);
221 static int  device_dma0_tx_80211(struct sk_buff *skb, struct net_device *dev);
222
223 static int  ethtool_ioctl(struct net_device *dev, void *useraddr);
224 static void device_free_tx_bufs(struct vnt_private *pDevice);
225 static void device_free_rx_bufs(struct vnt_private *pDevice);
226 static void device_free_int_bufs(struct vnt_private *pDevice);
227 static void device_free_frag_bufs(struct vnt_private *pDevice);
228 static bool device_alloc_bufs(struct vnt_private *pDevice);
229
230 static int Read_config_file(struct vnt_private *pDevice);
231 static unsigned char *Config_FileOperation(struct vnt_private *pDevice);
232 static int Config_FileGetParameter(unsigned char *string,
233                                    unsigned char *dest,
234                                    unsigned char *source);
235
236 static void usb_device_reset(struct vnt_private *pDevice);
237
238 static void
239 device_set_options(struct vnt_private *pDevice) {
240
241     u8    abyBroadcastAddr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
242     u8    abySNAP_RFC1042[ETH_ALEN] = {0xAA, 0xAA, 0x03, 0x00, 0x00, 0x00};
243     u8 abySNAP_Bridgetunnel[ETH_ALEN] = {0xAA, 0xAA, 0x03, 0x00, 0x00, 0xF8};
244
245     memcpy(pDevice->abyBroadcastAddr, abyBroadcastAddr, ETH_ALEN);
246     memcpy(pDevice->abySNAP_RFC1042, abySNAP_RFC1042, ETH_ALEN);
247     memcpy(pDevice->abySNAP_Bridgetunnel, abySNAP_Bridgetunnel, ETH_ALEN);
248
249     pDevice->cbTD = TX_DESC_DEF0;
250     pDevice->cbRD = RX_DESC_DEF0;
251     pDevice->uChannel = CHANNEL_DEF;
252     pDevice->wRTSThreshold = RTS_THRESH_DEF;
253     pDevice->wFragmentationThreshold = FRAG_THRESH_DEF;
254     pDevice->byShortRetryLimit = SHORT_RETRY_DEF;
255     pDevice->byLongRetryLimit = LONG_RETRY_DEF;
256     pDevice->wMaxTransmitMSDULifetime = DEFAULT_MSDU_LIFETIME;
257     pDevice->byShortPreamble = PREAMBLE_TYPE_DEF;
258     pDevice->ePSMode = PS_MODE_DEF;
259     pDevice->b11hEnable = X80211h_MODE_DEF;
260     pDevice->eOPMode = OP_MODE_DEF;
261     pDevice->uConnectionRate = DATA_RATE_DEF;
262     if (pDevice->uConnectionRate < RATE_AUTO) pDevice->bFixRate = true;
263     pDevice->byBBType = BBP_TYPE_DEF;
264     pDevice->byPacketType = pDevice->byBBType;
265     pDevice->byAutoFBCtrl = AUTO_FB_0;
266     pDevice->bUpdateBBVGA = true;
267     pDevice->byFOETuning = 0;
268     pDevice->byAutoPwrTunning = 0;
269     pDevice->wCTSDuration = 0;
270     pDevice->byPreambleType = 0;
271     pDevice->bExistSWNetAddr = false;
272     /* pDevice->bDiversityRegCtlON = true; */
273     pDevice->bDiversityRegCtlON = false;
274 }
275
276 static void device_init_diversity_timer(struct vnt_private *pDevice)
277 {
278     init_timer(&pDevice->TimerSQ3Tmax1);
279     pDevice->TimerSQ3Tmax1.data = (unsigned long)pDevice;
280     pDevice->TimerSQ3Tmax1.function = (TimerFunction)TimerSQ3CallBack;
281     pDevice->TimerSQ3Tmax1.expires = RUN_AT(HZ);
282
283     init_timer(&pDevice->TimerSQ3Tmax2);
284     pDevice->TimerSQ3Tmax2.data = (unsigned long)pDevice;
285     pDevice->TimerSQ3Tmax2.function = (TimerFunction)TimerSQ3CallBack;
286     pDevice->TimerSQ3Tmax2.expires = RUN_AT(HZ);
287
288     init_timer(&pDevice->TimerSQ3Tmax3);
289     pDevice->TimerSQ3Tmax3.data = (unsigned long)pDevice;
290     pDevice->TimerSQ3Tmax3.function = (TimerFunction)TimerSQ3Tmax3CallBack;
291     pDevice->TimerSQ3Tmax3.expires = RUN_AT(HZ);
292
293     return;
294 }
295
296 /*
297  * initialization of MAC & BBP registers
298  */
299
300 static int device_init_registers(struct vnt_private *pDevice,
301         DEVICE_INIT_TYPE InitType)
302 {
303         struct vnt_manager *pMgmt = &pDevice->vnt_mgmt;
304         u8 abyBroadcastAddr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
305         u8 abySNAP_RFC1042[ETH_ALEN] = {0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00};
306         u8 abySNAP_Bridgetunnel[ETH_ALEN]
307                 = {0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8};
308         u8 byAntenna;
309         int ii;
310         CMD_CARD_INIT sInitCmd;
311         int ntStatus = STATUS_SUCCESS;
312         RSP_CARD_INIT   sInitRsp;
313         u8 byTmp;
314         u8 byCalibTXIQ = 0, byCalibTXDC = 0, byCalibRXIQ = 0;
315
316     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "---->INIbInitAdapter. [%d][%d]\n", InitType, pDevice->byPacketType);
317         spin_lock_irq(&pDevice->lock);
318         if (InitType == DEVICE_INIT_COLD) {
319                 memcpy(pDevice->abyBroadcastAddr, abyBroadcastAddr, ETH_ALEN);
320                 memcpy(pDevice->abySNAP_RFC1042, abySNAP_RFC1042, ETH_ALEN);
321                 memcpy(pDevice->abySNAP_Bridgetunnel,
322                        abySNAP_Bridgetunnel,
323                        ETH_ALEN);
324
325         if ( !FIRMWAREbCheckVersion(pDevice) ) {
326             if (FIRMWAREbDownload(pDevice) == true) {
327                 if (FIRMWAREbBrach2Sram(pDevice) == false) {
328                     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" FIRMWAREbBrach2Sram fail \n");
329                         spin_unlock_irq(&pDevice->lock);
330                     return false;
331                 }
332             } else {
333
334                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" FIRMWAREbDownload fail \n");
335                 spin_unlock_irq(&pDevice->lock);
336                 return false;
337             }
338         }
339
340         if ( !BBbVT3184Init(pDevice) ) {
341             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" BBbVT3184Init fail \n");
342             spin_unlock_irq(&pDevice->lock);
343             return false;
344         }
345     }
346
347     sInitCmd.byInitClass = (u8)InitType;
348     sInitCmd.bExistSWNetAddr = (u8) pDevice->bExistSWNetAddr;
349     for (ii = 0; ii < 6; ii++)
350         sInitCmd.bySWNetAddr[ii] = pDevice->abyCurrentNetAddr[ii];
351     sInitCmd.byShortRetryLimit = pDevice->byShortRetryLimit;
352     sInitCmd.byLongRetryLimit = pDevice->byLongRetryLimit;
353
354     /* issue card_init command to device */
355     ntStatus = CONTROLnsRequestOut(pDevice,
356                                     MESSAGE_TYPE_CARDINIT,
357                                     0,
358                                     0,
359                                     sizeof(CMD_CARD_INIT),
360                                     (u8 *) &(sInitCmd));
361
362     if ( ntStatus != STATUS_SUCCESS ) {
363         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" Issue Card init fail \n");
364         spin_unlock_irq(&pDevice->lock);
365         return false;
366     }
367     if (InitType == DEVICE_INIT_COLD) {
368
369         ntStatus = CONTROLnsRequestIn(pDevice,MESSAGE_TYPE_INIT_RSP,0,0,sizeof(RSP_CARD_INIT), (u8 *) &(sInitRsp));
370
371         if (ntStatus != STATUS_SUCCESS) {
372             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Cardinit request in status fail!\n");
373             spin_unlock_irq(&pDevice->lock);
374             return false;
375         }
376
377         /* local ID for AES functions */
378         ntStatus = CONTROLnsRequestIn(pDevice,
379                                     MESSAGE_TYPE_READ,
380                                     MAC_REG_LOCALID,
381                                     MESSAGE_REQUEST_MACREG,
382                                     1,
383                                     &pDevice->byLocalID);
384
385         if ( ntStatus != STATUS_SUCCESS ) {
386             spin_unlock_irq(&pDevice->lock);
387             return false;
388         }
389
390         /* do MACbSoftwareReset in MACvInitialize */
391
392         /* force CCK */
393         pDevice->bCCK = true;
394         pDevice->bProtectMode = false;
395         /* only used in 11g type, sync with ERP IE */
396         pDevice->bNonERPPresent = false;
397         pDevice->bBarkerPreambleMd = false;
398         if ( pDevice->bFixRate ) {
399             pDevice->wCurrentRate = (u16) pDevice->uConnectionRate;
400         } else {
401             if ( pDevice->byBBType == BB_TYPE_11B )
402                 pDevice->wCurrentRate = RATE_11M;
403             else
404                 pDevice->wCurrentRate = RATE_54M;
405         }
406
407         CHvInitChannelTable(pDevice);
408
409         pDevice->byTopOFDMBasicRate = RATE_24M;
410         pDevice->byTopCCKBasicRate = RATE_1M;
411         pDevice->byRevId = 0;
412         /* target to IF pin while programming to RF chip */
413         pDevice->byCurPwr = 0xFF;
414
415         pDevice->byCCKPwr = pDevice->abyEEPROM[EEP_OFS_PWR_CCK];
416         pDevice->byOFDMPwrG = pDevice->abyEEPROM[EEP_OFS_PWR_OFDMG];
417         /* load power table */
418         for (ii = 0; ii < 14; ii++) {
419             pDevice->abyCCKPwrTbl[ii] = pDevice->abyEEPROM[ii + EEP_OFS_CCK_PWR_TBL];
420             if (pDevice->abyCCKPwrTbl[ii] == 0)
421                 pDevice->abyCCKPwrTbl[ii] = pDevice->byCCKPwr;
422             pDevice->abyOFDMPwrTbl[ii] = pDevice->abyEEPROM[ii + EEP_OFS_OFDM_PWR_TBL];
423             if (pDevice->abyOFDMPwrTbl[ii] == 0)
424                 pDevice->abyOFDMPwrTbl[ii] = pDevice->byOFDMPwrG;
425         }
426
427         /*
428          * original zonetype is USA, but custom zonetype is Europe,
429          * then need to recover 12, 13, 14 channels with 11 channel
430          */
431           if(((pDevice->abyEEPROM[EEP_OFS_ZONETYPE] == ZoneType_Japan) ||
432                 (pDevice->abyEEPROM[EEP_OFS_ZONETYPE] == ZoneType_Europe))&&
433              (pDevice->byOriginalZonetype == ZoneType_USA)) {
434                 for (ii = 11; ii < 14; ii++) {
435                         pDevice->abyCCKPwrTbl[ii] = pDevice->abyCCKPwrTbl[10];
436                         pDevice->abyOFDMPwrTbl[ii] = pDevice->abyOFDMPwrTbl[10];
437                 }
438           }
439
440           pDevice->byOFDMPwrA = 0x34; /* same as RFbMA2829SelectChannel */
441
442           /* load OFDM A power table */
443           for (ii = 0; ii < CB_MAX_CHANNEL_5G; ii++) {
444             pDevice->abyOFDMAPwrTbl[ii] = pDevice->abyEEPROM[ii + EEP_OFS_OFDMA_PWR_TBL];
445             if (pDevice->abyOFDMAPwrTbl[ii] == 0)
446                 pDevice->abyOFDMAPwrTbl[ii] = pDevice->byOFDMPwrA;
447         }
448
449         byAntenna = pDevice->abyEEPROM[EEP_OFS_ANTENNA];
450         if (byAntenna & EEP_ANTINV)
451             pDevice->bTxRxAntInv = true;
452         else
453             pDevice->bTxRxAntInv = false;
454
455         byAntenna &= (EEP_ANTENNA_AUX | EEP_ANTENNA_MAIN);
456
457         if (byAntenna == 0) /* if not set default is both */
458             byAntenna = (EEP_ANTENNA_AUX | EEP_ANTENNA_MAIN);
459
460         if (byAntenna == (EEP_ANTENNA_AUX | EEP_ANTENNA_MAIN)) {
461             pDevice->byAntennaCount = 2;
462             pDevice->byTxAntennaMode = ANT_B;
463             pDevice->dwTxAntennaSel = 1;
464             pDevice->dwRxAntennaSel = 1;
465             if (pDevice->bTxRxAntInv == true)
466                 pDevice->byRxAntennaMode = ANT_A;
467             else
468                 pDevice->byRxAntennaMode = ANT_B;
469
470             if (pDevice->bDiversityRegCtlON)
471                 pDevice->bDiversityEnable = true;
472             else
473                 pDevice->bDiversityEnable = false;
474         } else  {
475             pDevice->bDiversityEnable = false;
476             pDevice->byAntennaCount = 1;
477             pDevice->dwTxAntennaSel = 0;
478             pDevice->dwRxAntennaSel = 0;
479             if (byAntenna & EEP_ANTENNA_AUX) {
480                 pDevice->byTxAntennaMode = ANT_A;
481                 if (pDevice->bTxRxAntInv == true)
482                     pDevice->byRxAntennaMode = ANT_B;
483                 else
484                     pDevice->byRxAntennaMode = ANT_A;
485             } else {
486                 pDevice->byTxAntennaMode = ANT_B;
487                 if (pDevice->bTxRxAntInv == true)
488                     pDevice->byRxAntennaMode = ANT_A;
489                 else
490                     pDevice->byRxAntennaMode = ANT_B;
491             }
492         }
493         pDevice->ulDiversityNValue = 100*255;
494         pDevice->ulDiversityMValue = 100*16;
495         pDevice->byTMax = 1;
496         pDevice->byTMax2 = 4;
497         pDevice->ulSQ3TH = 0;
498         pDevice->byTMax3 = 64;
499
500         /* get Auto Fall Back type */
501         pDevice->byAutoFBCtrl = AUTO_FB_0;
502
503         /* set SCAN Time */
504         pDevice->uScanTime = WLAN_SCAN_MINITIME;
505
506         /* default Auto Mode */
507         /* pDevice->NetworkType = Ndis802_11Automode; */
508         pDevice->eConfigPHYMode = PHY_TYPE_AUTO;
509         pDevice->byBBType = BB_TYPE_11G;
510
511         /* initialize BBP registers */
512         pDevice->ulTxPower = 25;
513
514         /* get channel range */
515         pDevice->byMinChannel = 1;
516         pDevice->byMaxChannel = CB_MAX_CHANNEL;
517
518         /* get RFType */
519         pDevice->byRFType = sInitRsp.byRFType;
520
521         if ((pDevice->byRFType & RF_EMU) != 0) {
522                 /* force change RevID for VT3253 emu */
523                 pDevice->byRevId = 0x80;
524         }
525
526         /* load vt3266 calibration parameters in EEPROM */
527         if (pDevice->byRFType == RF_VT3226D0) {
528             if((pDevice->abyEEPROM[EEP_OFS_MAJOR_VER] == 0x1) &&
529                 (pDevice->abyEEPROM[EEP_OFS_MINOR_VER] >= 0x4)) {
530                 byCalibTXIQ = pDevice->abyEEPROM[EEP_OFS_CALIB_TX_IQ];
531                 byCalibTXDC = pDevice->abyEEPROM[EEP_OFS_CALIB_TX_DC];
532                 byCalibRXIQ = pDevice->abyEEPROM[EEP_OFS_CALIB_RX_IQ];
533                 if( (byCalibTXIQ || byCalibTXDC || byCalibRXIQ) ) {
534                         /* CR255, enable TX/RX IQ and DC compensation mode */
535                         ControlvWriteByte(pDevice,
536                                           MESSAGE_REQUEST_BBREG,
537                                           0xFF,
538                                           0x03);
539                         /* CR251, TX I/Q Imbalance Calibration */
540                         ControlvWriteByte(pDevice,
541                                           MESSAGE_REQUEST_BBREG,
542                                           0xFB,
543                                           byCalibTXIQ);
544                         /* CR252, TX DC-Offset Calibration */
545                         ControlvWriteByte(pDevice,
546                                           MESSAGE_REQUEST_BBREG,
547                                           0xFC,
548                                           byCalibTXDC);
549                         /* CR253, RX I/Q Imbalance Calibration */
550                         ControlvWriteByte(pDevice,
551                                           MESSAGE_REQUEST_BBREG,
552                                           0xFD,
553                                           byCalibRXIQ);
554                 } else {
555                         /* CR255, turn off BB Calibration compensation */
556                         ControlvWriteByte(pDevice,
557                                           MESSAGE_REQUEST_BBREG,
558                                           0xFF,
559                                           0x0);
560                 }
561             }
562         }
563         pMgmt->eScanType = WMAC_SCAN_PASSIVE;
564         pMgmt->uCurrChannel = pDevice->uChannel;
565         pMgmt->uIBSSChannel = pDevice->uChannel;
566         CARDbSetMediaChannel(pDevice, pMgmt->uCurrChannel);
567
568         /* get permanent network address */
569         memcpy(pDevice->abyPermanentNetAddr,&(sInitRsp.byNetAddr[0]),6);
570         memcpy(pDevice->abyCurrentNetAddr,
571                pDevice->abyPermanentNetAddr,
572                ETH_ALEN);
573
574         /* if exist SW network address, use it */
575         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Network address = %pM\n",
576                 pDevice->abyCurrentNetAddr);
577     }
578
579     /*
580      * set BB and packet type at the same time
581      * set Short Slot Time, xIFS, and RSPINF
582      */
583     if (pDevice->byBBType == BB_TYPE_11A) {
584         CARDbAddBasicRate(pDevice, RATE_6M);
585         pDevice->bShortSlotTime = true;
586     } else {
587         CARDbAddBasicRate(pDevice, RATE_1M);
588         pDevice->bShortSlotTime = false;
589     }
590     BBvSetShortSlotTime(pDevice);
591     CARDvSetBSSMode(pDevice);
592
593     if (pDevice->bUpdateBBVGA) {
594         pDevice->byBBVGACurrent = pDevice->abyBBVGA[0];
595         pDevice->byBBVGANew = pDevice->byBBVGACurrent;
596         BBvSetVGAGainOffset(pDevice, pDevice->abyBBVGA[0]);
597     }
598
599     pDevice->byRadioCtl = pDevice->abyEEPROM[EEP_OFS_RADIOCTL];
600     pDevice->bHWRadioOff = false;
601     if ( (pDevice->byRadioCtl & EEP_RADIOCTL_ENABLE) != 0 ) {
602         ntStatus = CONTROLnsRequestIn(pDevice,
603                                     MESSAGE_TYPE_READ,
604                                     MAC_REG_GPIOCTL1,
605                                     MESSAGE_REQUEST_MACREG,
606                                     1,
607                                     &byTmp);
608
609         if ( ntStatus != STATUS_SUCCESS ) {
610             spin_unlock_irq(&pDevice->lock);
611             return false;
612         }
613         if ( (byTmp & GPIO3_DATA) == 0 ) {
614             pDevice->bHWRadioOff = true;
615             MACvRegBitsOn(pDevice,MAC_REG_GPIOCTL1,GPIO3_INTMD);
616         } else {
617             MACvRegBitsOff(pDevice,MAC_REG_GPIOCTL1,GPIO3_INTMD);
618             pDevice->bHWRadioOff = false;
619         }
620
621     }
622
623     ControlvMaskByte(pDevice,MESSAGE_REQUEST_MACREG,MAC_REG_PAPEDELAY,LEDSTS_TMLEN,0x38);
624     ControlvMaskByte(pDevice,MESSAGE_REQUEST_MACREG,MAC_REG_PAPEDELAY,LEDSTS_STS,LEDSTS_SLOW);
625     MACvRegBitsOn(pDevice,MAC_REG_GPIOCTL0,0x01);
626
627     if ((pDevice->bHWRadioOff == true) || (pDevice->bRadioControlOff == true)) {
628         CARDbRadioPowerOff(pDevice);
629     } else {
630         CARDbRadioPowerOn(pDevice);
631     }
632
633     spin_unlock_irq(&pDevice->lock);
634     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"<----INIbInitAdapter Exit\n");
635     return true;
636 }
637
638 #ifdef CONFIG_PM        /* Minimal support for suspend and resume */
639
640 static int vt6656_suspend(struct usb_interface *intf, pm_message_t message)
641 {
642         struct vnt_private *device = usb_get_intfdata(intf);
643
644         if (!device || !device->dev)
645                 return -ENODEV;
646
647         if (device->flags & DEVICE_FLAGS_OPENED)
648                 device_close(device->dev);
649
650         return 0;
651 }
652
653 static int vt6656_resume(struct usb_interface *intf)
654 {
655         struct vnt_private *device = usb_get_intfdata(intf);
656
657         if (!device || !device->dev)
658                 return -ENODEV;
659
660         if (!(device->flags & DEVICE_FLAGS_OPENED))
661                 device_open(device->dev);
662
663         return 0;
664 }
665
666 #endif /* CONFIG_PM */
667
668 static const struct net_device_ops device_netdev_ops = {
669     .ndo_open               = device_open,
670     .ndo_stop               = device_close,
671     .ndo_do_ioctl           = device_ioctl,
672     .ndo_get_stats          = device_get_stats,
673     .ndo_start_xmit         = device_xmit,
674     .ndo_set_rx_mode        = device_set_multi,
675 };
676
677 static int
678 vt6656_probe(struct usb_interface *intf, const struct usb_device_id *id)
679 {
680         u8 fake_mac[ETH_ALEN] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x01};
681         struct usb_device *udev = interface_to_usbdev(intf);
682         int rc = 0;
683         struct net_device *netdev = NULL;
684         struct vnt_private *pDevice;
685
686         printk(KERN_NOTICE "%s Ver. %s\n", DEVICE_FULL_DRV_NAM, DEVICE_VERSION);
687         printk(KERN_NOTICE "Copyright (c) 2004 VIA Networking Technologies, Inc.\n");
688
689         udev = usb_get_dev(udev);
690         netdev = alloc_etherdev(sizeof(struct vnt_private));
691         if (!netdev) {
692                 printk(KERN_ERR DEVICE_NAME ": allocate net device failed\n");
693                 rc = -ENOMEM;
694                 goto err_nomem;
695         }
696
697         pDevice = netdev_priv(netdev);
698         memset(pDevice, 0, sizeof(struct vnt_private));
699
700         pDevice->dev = netdev;
701         pDevice->usb = udev;
702
703         device_set_options(pDevice);
704         spin_lock_init(&pDevice->lock);
705
706         pDevice->tx_80211 = device_dma0_tx_80211;
707         pDevice->vnt_mgmt.pAdapter = (void *) pDevice;
708
709         netdev->netdev_ops = &device_netdev_ops;
710         netdev->wireless_handlers =
711                 (struct iw_handler_def *) &iwctl_handler_def;
712
713         usb_set_intfdata(intf, pDevice);
714         SET_NETDEV_DEV(netdev, &intf->dev);
715         memcpy(pDevice->dev->dev_addr, fake_mac, ETH_ALEN);
716         rc = register_netdev(netdev);
717         if (rc) {
718                 printk(KERN_ERR DEVICE_NAME " Failed to register netdev\n");
719                 goto err_netdev;
720         }
721
722         usb_device_reset(pDevice);
723
724         return 0;
725
726 err_netdev:
727         free_netdev(netdev);
728 err_nomem:
729         usb_put_dev(udev);
730
731         return rc;
732 }
733
734 static void device_free_tx_bufs(struct vnt_private *pDevice)
735 {
736     PUSB_SEND_CONTEXT pTxContext;
737     int ii;
738
739     for (ii = 0; ii < pDevice->cbTD; ii++) {
740
741         pTxContext = pDevice->apTD[ii];
742         /* deallocate URBs */
743         if (pTxContext->pUrb) {
744             usb_kill_urb(pTxContext->pUrb);
745             usb_free_urb(pTxContext->pUrb);
746         }
747         kfree(pTxContext);
748     }
749     return;
750 }
751
752 static void device_free_rx_bufs(struct vnt_private *pDevice)
753 {
754     PRCB pRCB;
755     int ii;
756
757     for (ii = 0; ii < pDevice->cbRD; ii++) {
758
759         pRCB = pDevice->apRCB[ii];
760         /* deallocate URBs */
761         if (pRCB->pUrb) {
762             usb_kill_urb(pRCB->pUrb);
763             usb_free_urb(pRCB->pUrb);
764         }
765         /* deallocate skb */
766         if (pRCB->skb)
767             dev_kfree_skb(pRCB->skb);
768     }
769     kfree(pDevice->pRCBMem);
770
771     return;
772 }
773
774 static void usb_device_reset(struct vnt_private *pDevice)
775 {
776  int status;
777  status = usb_reset_device(pDevice->usb);
778         if (status)
779             printk("usb_device_reset fail status=%d\n",status);
780         return ;
781 }
782
783 static void device_free_int_bufs(struct vnt_private *pDevice)
784 {
785     kfree(pDevice->intBuf.pDataBuf);
786     return;
787 }
788
789 static bool device_alloc_bufs(struct vnt_private *pDevice)
790 {
791
792     PUSB_SEND_CONTEXT pTxContext;
793     PRCB pRCB;
794     int ii;
795
796     for (ii = 0; ii < pDevice->cbTD; ii++) {
797
798         pTxContext = kmalloc(sizeof(USB_SEND_CONTEXT), GFP_KERNEL);
799         if (pTxContext == NULL) {
800             DBG_PRT(MSG_LEVEL_ERR,KERN_ERR "%s : allocate tx usb context failed\n", pDevice->dev->name);
801             goto free_tx;
802         }
803         pDevice->apTD[ii] = pTxContext;
804         pTxContext->pDevice = (void *) pDevice;
805         /* allocate URBs */
806         pTxContext->pUrb = usb_alloc_urb(0, GFP_ATOMIC);
807         if (pTxContext->pUrb == NULL) {
808             DBG_PRT(MSG_LEVEL_ERR,KERN_ERR "alloc tx urb failed\n");
809             goto free_tx;
810         }
811         pTxContext->bBoolInUse = false;
812     }
813
814     /* allocate RCB mem */
815         pDevice->pRCBMem = kzalloc((sizeof(RCB) * pDevice->cbRD), GFP_KERNEL);
816     if (pDevice->pRCBMem == NULL) {
817         DBG_PRT(MSG_LEVEL_ERR,KERN_ERR "%s : alloc rx usb context failed\n", pDevice->dev->name);
818         goto free_tx;
819     }
820
821     pDevice->FirstRecvFreeList = NULL;
822     pDevice->LastRecvFreeList = NULL;
823     pDevice->FirstRecvMngList = NULL;
824     pDevice->LastRecvMngList = NULL;
825     pDevice->NumRecvFreeList = 0;
826     pRCB = (PRCB) pDevice->pRCBMem;
827
828     for (ii = 0; ii < pDevice->cbRD; ii++) {
829
830         pDevice->apRCB[ii] = pRCB;
831         pRCB->pDevice = (void *) pDevice;
832         /* allocate URBs */
833         pRCB->pUrb = usb_alloc_urb(0, GFP_ATOMIC);
834
835         if (pRCB->pUrb == NULL) {
836             DBG_PRT(MSG_LEVEL_ERR,KERN_ERR" Failed to alloc rx urb\n");
837             goto free_rx_tx;
838         }
839         pRCB->skb = dev_alloc_skb((int)pDevice->rx_buf_sz);
840         if (pRCB->skb == NULL) {
841             DBG_PRT(MSG_LEVEL_ERR,KERN_ERR" Failed to alloc rx skb\n");
842             goto free_rx_tx;
843         }
844         pRCB->skb->dev = pDevice->dev;
845         pRCB->bBoolInUse = false;
846         EnqueueRCB(pDevice->FirstRecvFreeList, pDevice->LastRecvFreeList, pRCB);
847         pDevice->NumRecvFreeList++;
848         pRCB++;
849     }
850
851         pDevice->pControlURB = usb_alloc_urb(0, GFP_ATOMIC);
852         if (pDevice->pControlURB == NULL) {
853             DBG_PRT(MSG_LEVEL_ERR,KERN_ERR"Failed to alloc control urb\n");
854             goto free_rx_tx;
855         }
856
857         pDevice->pInterruptURB = usb_alloc_urb(0, GFP_ATOMIC);
858         if (pDevice->pInterruptURB == NULL) {
859             DBG_PRT(MSG_LEVEL_ERR,KERN_ERR"Failed to alloc int urb\n");
860             usb_free_urb(pDevice->pControlURB);
861             goto free_rx_tx;
862         }
863
864     pDevice->intBuf.pDataBuf = kmalloc(MAX_INTERRUPT_SIZE, GFP_KERNEL);
865         if (pDevice->intBuf.pDataBuf == NULL) {
866             DBG_PRT(MSG_LEVEL_ERR,KERN_ERR"Failed to alloc int buf\n");
867             usb_free_urb(pDevice->pControlURB);
868             usb_free_urb(pDevice->pInterruptURB);
869             goto free_rx_tx;
870         }
871
872     return true;
873
874 free_rx_tx:
875     device_free_rx_bufs(pDevice);
876
877 free_tx:
878     device_free_tx_bufs(pDevice);
879
880         return false;
881 }
882
883 static bool device_init_defrag_cb(struct vnt_private *pDevice)
884 {
885         int i;
886         PSDeFragControlBlock pDeF;
887
888     /* Init the fragment ctl entries */
889     for (i = 0; i < CB_MAX_RX_FRAG; i++) {
890         pDeF = &(pDevice->sRxDFCB[i]);
891         if (!device_alloc_frag_buf(pDevice, pDeF)) {
892             DBG_PRT(MSG_LEVEL_ERR,KERN_ERR "%s: can not alloc frag bufs\n",
893                 pDevice->dev->name);
894             goto free_frag;
895         }
896     }
897     pDevice->cbDFCB = CB_MAX_RX_FRAG;
898     pDevice->cbFreeDFCB = pDevice->cbDFCB;
899     return true;
900
901 free_frag:
902     device_free_frag_bufs(pDevice);
903     return false;
904 }
905
906 static void device_free_frag_bufs(struct vnt_private *pDevice)
907 {
908         PSDeFragControlBlock pDeF;
909         int i;
910
911     for (i = 0; i < CB_MAX_RX_FRAG; i++) {
912
913         pDeF = &(pDevice->sRxDFCB[i]);
914
915         if (pDeF->skb)
916             dev_kfree_skb(pDeF->skb);
917     }
918 }
919
920 int device_alloc_frag_buf(struct vnt_private *pDevice,
921                 PSDeFragControlBlock pDeF)
922 {
923
924     pDeF->skb = dev_alloc_skb((int)pDevice->rx_buf_sz);
925     if (pDeF->skb == NULL)
926         return false;
927     ASSERT(pDeF->skb);
928     pDeF->skb->dev = pDevice->dev;
929
930     return true;
931 }
932
933 static int  device_open(struct net_device *dev)
934 {
935         struct vnt_private *pDevice = netdev_priv(dev);
936
937      pDevice->fWPA_Authened = false;
938
939     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " device_open...\n");
940
941     pDevice->rx_buf_sz = MAX_TOTAL_SIZE_WITH_ALL_HEADERS;
942
943     if (device_alloc_bufs(pDevice) == false) {
944         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " device_alloc_bufs fail... \n");
945         return -ENOMEM;
946     }
947
948     if (device_init_defrag_cb(pDevice)== false) {
949         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " Initial defragment cb fail \n");
950         goto free_rx_tx;
951     }
952
953     MP_CLEAR_FLAG(pDevice, fMP_DISCONNECTED);
954     MP_CLEAR_FLAG(pDevice, fMP_CONTROL_READS);
955     MP_CLEAR_FLAG(pDevice, fMP_CONTROL_WRITES);
956     MP_SET_FLAG(pDevice, fMP_POST_READS);
957     MP_SET_FLAG(pDevice, fMP_POST_WRITES);
958
959     /* read config file */
960     Read_config_file(pDevice);
961
962     if (device_init_registers(pDevice, DEVICE_INIT_COLD) == false) {
963         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " init register fail\n");
964         goto free_all;
965     }
966
967     device_set_multi(pDevice->dev);
968
969     /* init for key management */
970     KeyvInitTable(pDevice,&pDevice->sKey);
971         memcpy(pDevice->vnt_mgmt.abyMACAddr,
972                 pDevice->abyCurrentNetAddr, ETH_ALEN);
973     memcpy(pDevice->dev->dev_addr, pDevice->abyCurrentNetAddr, ETH_ALEN);
974     pDevice->bStopTx0Pkt = false;
975     pDevice->bStopDataPkt = false;
976     pDevice->bRoaming = false;
977     pDevice->bIsRoaming = false;
978     pDevice->bEnableRoaming = false;
979     if (pDevice->bDiversityRegCtlON) {
980         device_init_diversity_timer(pDevice);
981     }
982
983     vMgrObjectInit(pDevice);
984     tasklet_init(&pDevice->RxMngWorkItem, (void *)RXvMngWorkItem, (unsigned long)pDevice);
985     tasklet_init(&pDevice->ReadWorkItem, (void *)RXvWorkItem, (unsigned long)pDevice);
986     tasklet_init(&pDevice->EventWorkItem, (void *)INTvWorkItem, (unsigned long)pDevice);
987         add_timer(&pDevice->vnt_mgmt.sTimerSecondCallback);
988         pDevice->int_interval = 100;  /* max 100 microframes */
989     pDevice->eEncryptionStatus = Ndis802_11EncryptionDisabled;
990
991     pDevice->bIsRxWorkItemQueued = true;
992     pDevice->fKillEventPollingThread = false;
993     pDevice->bEventAvailable = false;
994
995    pDevice->bWPADEVUp = false;
996      pDevice->bwextstep0 = false;
997      pDevice->bwextstep1 = false;
998      pDevice->bwextstep2 = false;
999      pDevice->bwextstep3 = false;
1000      pDevice->bWPASuppWextEnabled = false;
1001     pDevice->byReAssocCount = 0;
1002
1003     RXvWorkItem(pDevice);
1004     INTvWorkItem(pDevice);
1005
1006     /* if WEP key already set by iwconfig but device not yet open */
1007     if ((pDevice->bEncryptionEnable == true) && (pDevice->bTransmitKey == true)) {
1008          spin_lock_irq(&pDevice->lock);
1009          KeybSetDefaultKey( pDevice,
1010                             &(pDevice->sKey),
1011                             pDevice->byKeyIndex | (1 << 31),
1012                             pDevice->uKeyLength,
1013                             NULL,
1014                             pDevice->abyKey,
1015                             KEY_CTL_WEP
1016                           );
1017          spin_unlock_irq(&pDevice->lock);
1018          pDevice->eEncryptionStatus = Ndis802_11Encryption1Enabled;
1019     }
1020
1021         if (pDevice->vnt_mgmt.eConfigMode == WMAC_CONFIG_AP)
1022                 bScheduleCommand((void *) pDevice, WLAN_CMD_RUN_AP, NULL);
1023         else
1024                 bScheduleCommand((void *) pDevice, WLAN_CMD_BSSID_SCAN, NULL);
1025
1026     netif_stop_queue(pDevice->dev);
1027     pDevice->flags |= DEVICE_FLAGS_OPENED;
1028
1029         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "device_open success..\n");
1030         return 0;
1031
1032 free_all:
1033     device_free_frag_bufs(pDevice);
1034 free_rx_tx:
1035     device_free_rx_bufs(pDevice);
1036     device_free_tx_bufs(pDevice);
1037     device_free_int_bufs(pDevice);
1038         usb_kill_urb(pDevice->pControlURB);
1039         usb_kill_urb(pDevice->pInterruptURB);
1040     usb_free_urb(pDevice->pControlURB);
1041     usb_free_urb(pDevice->pInterruptURB);
1042
1043     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "device_open fail.. \n");
1044     return -ENOMEM;
1045 }
1046
1047 static int device_close(struct net_device *dev)
1048 {
1049         struct vnt_private *pDevice = netdev_priv(dev);
1050         struct vnt_manager *pMgmt = &pDevice->vnt_mgmt;
1051         int uu;
1052
1053         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "device_close1\n");
1054     if (pDevice == NULL)
1055         return -ENODEV;
1056
1057     if (pDevice->bLinkPass) {
1058         bScheduleCommand((void *) pDevice, WLAN_CMD_DISASSOCIATE, NULL);
1059         mdelay(30);
1060     }
1061
1062         memset(pMgmt->abyDesireSSID, 0, WLAN_IEHDR_LEN + WLAN_SSID_MAXLEN + 1);
1063         pMgmt->bShareKeyAlgorithm = false;
1064         pDevice->bEncryptionEnable = false;
1065         pDevice->eEncryptionStatus = Ndis802_11EncryptionDisabled;
1066         spin_lock_irq(&pDevice->lock);
1067         for (uu = 0; uu < MAX_KEY_TABLE; uu++)
1068                 MACvDisableKeyEntry(pDevice,uu);
1069         spin_unlock_irq(&pDevice->lock);
1070
1071     if ((pDevice->flags & DEVICE_FLAGS_UNPLUG) == false) {
1072         MACbShutdown(pDevice);
1073     }
1074     netif_stop_queue(pDevice->dev);
1075     MP_SET_FLAG(pDevice, fMP_DISCONNECTED);
1076     MP_CLEAR_FLAG(pDevice, fMP_POST_WRITES);
1077     MP_CLEAR_FLAG(pDevice, fMP_POST_READS);
1078     pDevice->fKillEventPollingThread = true;
1079     del_timer(&pDevice->sTimerCommand);
1080     del_timer(&pMgmt->sTimerSecondCallback);
1081
1082     del_timer(&pDevice->sTimerTxData);
1083
1084     if (pDevice->bDiversityRegCtlON) {
1085         del_timer(&pDevice->TimerSQ3Tmax1);
1086         del_timer(&pDevice->TimerSQ3Tmax2);
1087         del_timer(&pDevice->TimerSQ3Tmax3);
1088     }
1089     tasklet_kill(&pDevice->RxMngWorkItem);
1090     tasklet_kill(&pDevice->ReadWorkItem);
1091     tasklet_kill(&pDevice->EventWorkItem);
1092
1093    pDevice->bRoaming = false;
1094    pDevice->bIsRoaming = false;
1095    pDevice->bEnableRoaming = false;
1096     pDevice->bCmdRunning = false;
1097     pDevice->bLinkPass = false;
1098     memset(pMgmt->abyCurrBSSID, 0, 6);
1099     pMgmt->eCurrState = WMAC_STATE_IDLE;
1100
1101     device_free_tx_bufs(pDevice);
1102     device_free_rx_bufs(pDevice);
1103     device_free_int_bufs(pDevice);
1104     device_free_frag_bufs(pDevice);
1105
1106         usb_kill_urb(pDevice->pControlURB);
1107         usb_kill_urb(pDevice->pInterruptURB);
1108     usb_free_urb(pDevice->pControlURB);
1109     usb_free_urb(pDevice->pInterruptURB);
1110
1111     BSSvClearNodeDBTable(pDevice, 0);
1112     pDevice->flags &=(~DEVICE_FLAGS_OPENED);
1113
1114     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "device_close2 \n");
1115
1116     return 0;
1117 }
1118
1119 static void vt6656_disconnect(struct usb_interface *intf)
1120 {
1121         struct vnt_private *device = usb_get_intfdata(intf);
1122
1123         if (!device)
1124                 return;
1125
1126         usb_set_intfdata(intf, NULL);
1127         usb_put_dev(interface_to_usbdev(intf));
1128
1129         device->flags |= DEVICE_FLAGS_UNPLUG;
1130
1131         if (device->dev) {
1132                 unregister_netdev(device->dev);
1133                 free_netdev(device->dev);
1134         }
1135
1136 }
1137
1138 static int device_dma0_tx_80211(struct sk_buff *skb, struct net_device *dev)
1139 {
1140         struct vnt_private *pDevice = netdev_priv(dev);
1141
1142         spin_lock_irq(&pDevice->lock);
1143
1144         if (unlikely(pDevice->bStopTx0Pkt))
1145                 dev_kfree_skb_irq(skb);
1146         else
1147                 vDMA0_tx_80211(pDevice, skb);
1148
1149         spin_unlock_irq(&pDevice->lock);
1150
1151         return NETDEV_TX_OK;
1152 }
1153
1154 static int device_xmit(struct sk_buff *skb, struct net_device *dev)
1155 {
1156         struct vnt_private *pDevice = netdev_priv(dev);
1157         struct net_device_stats *stats = &pDevice->stats;
1158
1159         spin_lock_irq(&pDevice->lock);
1160
1161         netif_stop_queue(dev);
1162
1163         if (!pDevice->bLinkPass) {
1164                 dev_kfree_skb_irq(skb);
1165                 goto out;
1166         }
1167
1168         if (pDevice->bStopDataPkt) {
1169                 dev_kfree_skb_irq(skb);
1170                 stats->tx_dropped++;
1171                 goto out;
1172         }
1173
1174         if (nsDMA_tx_packet(pDevice, TYPE_AC0DMA, skb)) {
1175                 if (netif_queue_stopped(dev))
1176                         netif_wake_queue(dev);
1177         }
1178
1179 out:
1180         spin_unlock_irq(&pDevice->lock);
1181
1182         return NETDEV_TX_OK;
1183 }
1184
1185 static unsigned const ethernet_polynomial = 0x04c11db7U;
1186 static inline u32 ether_crc(int length, unsigned char *data)
1187 {
1188     int crc = -1;
1189
1190     while(--length >= 0) {
1191         unsigned char current_octet = *data++;
1192         int bit;
1193         for (bit = 0; bit < 8; bit++, current_octet >>= 1) {
1194             crc = (crc << 1) ^
1195                 ((crc < 0) ^ (current_octet & 1) ? ethernet_polynomial : 0);
1196         }
1197     }
1198     return crc;
1199 }
1200
1201 /* find out the start position of str2 from str1 */
1202 static unsigned char *kstrstr(const unsigned char *str1,
1203                               const unsigned char *str2) {
1204   int str1_len = strlen(str1);
1205   int str2_len = strlen(str2);
1206
1207   while (str1_len >= str2_len) {
1208        str1_len--;
1209       if(memcmp(str1,str2,str2_len)==0)
1210         return (unsigned char *) str1;
1211         str1++;
1212   }
1213   return NULL;
1214 }
1215
1216 static int Config_FileGetParameter(unsigned char *string,
1217                                    unsigned char *dest,
1218                                    unsigned char *source)
1219 {
1220   unsigned char buf1[100];
1221   unsigned char buf2[100];
1222   unsigned char *start_p = NULL, *end_p = NULL, *tmp_p = NULL;
1223   int ii;
1224
1225     memset(buf1,0,100);
1226     strcat(buf1, string);
1227     strcat(buf1, "=");
1228     source+=strlen(buf1);
1229
1230     /* find target string start point */
1231     start_p = kstrstr(source,buf1);
1232     if (start_p == NULL)
1233         return false;
1234
1235     /* check if current config line is marked by "#" */
1236     for (ii = 1; ; ii++) {
1237         if (memcmp(start_p - ii, "\n", 1) == 0)
1238                 break;
1239         if (memcmp(start_p - ii, "#", 1) == 0)
1240                 return false;
1241     }
1242
1243     /* find target string end point */
1244      end_p = kstrstr(start_p,"\n");
1245      if (end_p == NULL) {       /* can't find "\n", but don't care */
1246              end_p = start_p + strlen(start_p);   /* no include "\n" */
1247      }
1248
1249    memset(buf2,0,100);
1250    memcpy(buf2, start_p, end_p-start_p); /* get the target line */
1251    buf2[end_p-start_p]='\0';
1252
1253    /* find value */
1254    start_p = kstrstr(buf2,"=");
1255    if (start_p == NULL)
1256       return false;
1257    memset(buf1,0,100);
1258    strcpy(buf1,start_p+1);
1259
1260    /* except space */
1261   tmp_p = buf1;
1262   while(*tmp_p != 0x00) {
1263         if(*tmp_p==' ')
1264             tmp_p++;
1265          else
1266           break;
1267   }
1268
1269    memcpy(dest,tmp_p,strlen(tmp_p));
1270  return true;
1271 }
1272
1273 /* if read fails, return NULL, or return data pointer */
1274 static unsigned char *Config_FileOperation(struct vnt_private *pDevice)
1275 {
1276     unsigned char *config_path = CONFIG_PATH;
1277     unsigned char *buffer = NULL;
1278     struct file   *filp=NULL;
1279     mm_segment_t old_fs = get_fs();
1280
1281     int result = 0;
1282
1283     set_fs (KERNEL_DS);
1284
1285     /* open file */
1286       filp = filp_open(config_path, O_RDWR, 0);
1287         if (IS_ERR(filp)) {
1288              printk("Config_FileOperation file Not exist\n");
1289              result=-1;
1290              goto error2;
1291           }
1292
1293      if(!(filp->f_op) || !(filp->f_op->read) ||!(filp->f_op->write)) {
1294            printk("file %s is not read or writeable?\n",config_path);
1295           result = -1;
1296           goto error1;
1297         }
1298
1299     buffer = kmalloc(1024, GFP_KERNEL);
1300     if(buffer==NULL) {
1301       printk("allocate mem for file fail?\n");
1302       result = -1;
1303       goto error1;
1304     }
1305
1306     if(filp->f_op->read(filp, buffer, 1024, &filp->f_pos)<0) {
1307      printk("read file error?\n");
1308      result = -1;
1309     }
1310
1311 error1:
1312   if(filp_close(filp,NULL))
1313        printk("Config_FileOperation:close file fail\n");
1314
1315 error2:
1316   set_fs (old_fs);
1317
1318 if(result!=0) {
1319     kfree(buffer);
1320     buffer=NULL;
1321 }
1322   return buffer;
1323 }
1324
1325 /* return --->-1:fail; >=0:successful */
1326 static int Read_config_file(struct vnt_private *pDevice)
1327 {
1328         int result = 0;
1329         unsigned char tmpbuffer[100];
1330         unsigned char *buffer = NULL;
1331
1332         /* init config setting */
1333  pDevice->config_file.ZoneType = -1;
1334  pDevice->config_file.eAuthenMode = -1;
1335  pDevice->config_file.eEncryptionStatus = -1;
1336
1337   buffer = Config_FileOperation(pDevice);
1338   if (buffer == NULL) {
1339      result =-1;
1340      return result;
1341   }
1342
1343 /* get zonetype */
1344 {
1345     memset(tmpbuffer,0,sizeof(tmpbuffer));
1346     if(Config_FileGetParameter("ZONETYPE",tmpbuffer,buffer) ==true) {
1347     if(memcmp(tmpbuffer,"USA",3)==0) {
1348       pDevice->config_file.ZoneType=ZoneType_USA;
1349     }
1350     else if(memcmp(tmpbuffer,"JAPAN",5)==0) {
1351       pDevice->config_file.ZoneType=ZoneType_Japan;
1352     }
1353     else if(memcmp(tmpbuffer,"EUROPE",6)==0) {
1354      pDevice->config_file.ZoneType=ZoneType_Europe;
1355     }
1356     else {
1357       printk("Unknown Zonetype[%s]?\n",tmpbuffer);
1358    }
1359  }
1360 }
1361
1362 /* get other parameter */
1363   {
1364         memset(tmpbuffer,0,sizeof(tmpbuffer));
1365        if(Config_FileGetParameter("AUTHENMODE",tmpbuffer,buffer)==true) {
1366          pDevice->config_file.eAuthenMode = (int) simple_strtol(tmpbuffer, NULL, 10);
1367        }
1368
1369         memset(tmpbuffer,0,sizeof(tmpbuffer));
1370        if(Config_FileGetParameter("ENCRYPTIONMODE",tmpbuffer,buffer)==true) {
1371          pDevice->config_file.eEncryptionStatus= (int) simple_strtol(tmpbuffer, NULL, 10);
1372        }
1373   }
1374
1375   kfree(buffer);
1376   return result;
1377 }
1378
1379 static void device_set_multi(struct net_device *dev)
1380 {
1381         struct vnt_private *pDevice = netdev_priv(dev);
1382         struct vnt_manager *pMgmt = &pDevice->vnt_mgmt;
1383         struct netdev_hw_addr *ha;
1384         u32 mc_filter[2];
1385         int ii;
1386         u8 pbyData[8] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
1387         u8 byTmpMode = 0;
1388         int rc;
1389
1390         spin_lock_irq(&pDevice->lock);
1391     rc = CONTROLnsRequestIn(pDevice,
1392                             MESSAGE_TYPE_READ,
1393                             MAC_REG_RCR,
1394                             MESSAGE_REQUEST_MACREG,
1395                             1,
1396                             &byTmpMode
1397                             );
1398     if (rc == 0) pDevice->byRxMode = byTmpMode;
1399
1400     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "pDevice->byRxMode in= %x\n", pDevice->byRxMode);
1401
1402     if (dev->flags & IFF_PROMISC) { /* set promiscuous mode */
1403         DBG_PRT(MSG_LEVEL_ERR,KERN_NOTICE "%s: Promiscuous mode enabled.\n", dev->name);
1404         /* unconditionally log net taps */
1405         pDevice->byRxMode |= (RCR_MULTICAST|RCR_BROADCAST|RCR_UNICAST);
1406     }
1407     else if ((netdev_mc_count(dev) > pDevice->multicast_limit) ||
1408              (dev->flags & IFF_ALLMULTI)) {
1409         CONTROLnsRequestOut(pDevice,
1410                             MESSAGE_TYPE_WRITE,
1411                             MAC_REG_MAR0,
1412                             MESSAGE_REQUEST_MACREG,
1413                             8,
1414                             pbyData
1415                             );
1416         pDevice->byRxMode |= (RCR_MULTICAST|RCR_BROADCAST);
1417     }
1418     else {
1419         memset(mc_filter, 0, sizeof(mc_filter));
1420         netdev_for_each_mc_addr(ha, dev) {
1421             int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
1422             mc_filter[bit_nr >> 5] |= cpu_to_le32(1 << (bit_nr & 31));
1423         }
1424         for (ii = 0; ii < 4; ii++) {
1425              MACvWriteMultiAddr(pDevice, ii, *((u8 *)&mc_filter[0] + ii));
1426              MACvWriteMultiAddr(pDevice, ii+ 4, *((u8 *)&mc_filter[1] + ii));
1427         }
1428         pDevice->byRxMode &= ~(RCR_UNICAST);
1429         pDevice->byRxMode |= (RCR_MULTICAST|RCR_BROADCAST);
1430     }
1431
1432     if (pMgmt->eConfigMode == WMAC_CONFIG_AP) {
1433         /*
1434          * If AP mode, don't enable RCR_UNICAST since HW only compares
1435          * addr1 with local MAC
1436          */
1437         pDevice->byRxMode |= (RCR_MULTICAST|RCR_BROADCAST);
1438         pDevice->byRxMode &= ~(RCR_UNICAST);
1439     }
1440     ControlvWriteByte(pDevice, MESSAGE_REQUEST_MACREG, MAC_REG_RCR, pDevice->byRxMode);
1441     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "pDevice->byRxMode out= %x\n", pDevice->byRxMode);
1442         spin_unlock_irq(&pDevice->lock);
1443
1444 }
1445
1446 static struct net_device_stats *device_get_stats(struct net_device *dev)
1447 {
1448         struct vnt_private *pDevice = netdev_priv(dev);
1449
1450         return &pDevice->stats;
1451 }
1452
1453 static int device_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1454 {
1455         struct vnt_private *pDevice = netdev_priv(dev);
1456         struct iwreq *wrq = (struct iwreq *) rq;
1457         int rc = 0;
1458
1459         switch (cmd) {
1460
1461         case IOCTL_CMD_HOSTAPD:
1462
1463                 if (!(pDevice->flags & DEVICE_FLAGS_OPENED))
1464                         rc = -EFAULT;
1465
1466                 rc = vt6656_hostap_ioctl(pDevice, &wrq->u.data);
1467                 break;
1468
1469         case SIOCETHTOOL:
1470                 return ethtool_ioctl(dev, (void *) rq->ifr_data);
1471
1472         }
1473
1474         return rc;
1475 }
1476
1477 static int ethtool_ioctl(struct net_device *dev, void *useraddr)
1478 {
1479         u32 ethcmd;
1480
1481         if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
1482                 return -EFAULT;
1483
1484         switch (ethcmd) {
1485         case ETHTOOL_GDRVINFO: {
1486                 struct ethtool_drvinfo info = {ETHTOOL_GDRVINFO};
1487                 strncpy(info.driver, DEVICE_NAME, sizeof(info.driver)-1);
1488                 strncpy(info.version, DEVICE_VERSION, sizeof(info.version)-1);
1489                 if (copy_to_user(useraddr, &info, sizeof(info)))
1490                         return -EFAULT;
1491                 return 0;
1492         }
1493
1494         }
1495
1496         return -EOPNOTSUPP;
1497 }
1498
1499 MODULE_DEVICE_TABLE(usb, vt6656_table);
1500
1501 static struct usb_driver vt6656_driver = {
1502         .name =         DEVICE_NAME,
1503         .probe =        vt6656_probe,
1504         .disconnect =   vt6656_disconnect,
1505         .id_table =     vt6656_table,
1506 #ifdef CONFIG_PM
1507         .suspend = vt6656_suspend,
1508         .resume = vt6656_resume,
1509 #endif /* CONFIG_PM */
1510 };
1511
1512 module_usb_driver(vt6656_driver);