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