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