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