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