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