Staging: Drop memory allocation cast
[safe/jmp/linux-2.6] / 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 - asynchrous data tx function
33  *   device_set_multi - set mac filter
34  *   device_ioctl - ioctl entry
35  *   device_close - shutdown mac/bbp & free dma/descripter 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 bufferred 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 "ioctl.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 Definitions -------------------------*/
77 //static int          msglevel                =MSG_LEVEL_DEBUG;
78 static int          msglevel                =MSG_LEVEL_INFO;
79
80 //
81 // Define module options
82 //
83
84 // Version Information
85 #define DRIVER_AUTHOR "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_MIN0     16
96 #define RX_DESC_MAX0     128
97 #define RX_DESC_DEF0     64
98 DEVICE_PARAM(RxDescriptors0,"Number of receive usb desc buffer");
99
100
101 #define TX_DESC_MIN0     16
102 #define TX_DESC_MAX0     128
103 #define TX_DESC_DEF0     64
104 DEVICE_PARAM(TxDescriptors0,"Number of transmit usb desc buffer");
105
106
107 #define CHANNEL_MIN     1
108 #define CHANNEL_MAX     14
109 #define CHANNEL_DEF     6
110
111 DEVICE_PARAM(Channel, "Channel number");
112
113
114 /* PreambleType[] is the preamble length used for transmit.
115    0: indicate allows long preamble type
116    1: indicate allows short preamble type
117 */
118
119 #define PREAMBLE_TYPE_DEF     1
120
121 DEVICE_PARAM(PreambleType, "Preamble Type");
122
123
124 #define RTS_THRESH_MIN     512
125 #define RTS_THRESH_MAX     2347
126 #define RTS_THRESH_DEF     2347
127
128 DEVICE_PARAM(RTSThreshold, "RTS threshold");
129
130
131 #define FRAG_THRESH_MIN     256
132 #define FRAG_THRESH_MAX     2346
133 #define FRAG_THRESH_DEF     2346
134
135 DEVICE_PARAM(FragThreshold, "Fragmentation threshold");
136
137
138 #define DATA_RATE_MIN     0
139 #define DATA_RATE_MAX     13
140 #define DATA_RATE_DEF     13
141 /* datarate[] index
142    0: indicate 1 Mbps   0x02
143    1: indicate 2 Mbps   0x04
144    2: indicate 5.5 Mbps 0x0B
145    3: indicate 11 Mbps  0x16
146    4: indicate 6 Mbps   0x0c
147    5: indicate 9 Mbps   0x12
148    6: indicate 12 Mbps  0x18
149    7: indicate 18 Mbps  0x24
150    8: indicate 24 Mbps  0x30
151    9: indicate 36 Mbps  0x48
152   10: indicate 48 Mbps  0x60
153   11: indicate 54 Mbps  0x6c
154   12: indicate 72 Mbps  0x90
155   13: indicate auto rate
156 */
157
158 DEVICE_PARAM(ConnectionRate, "Connection data rate");
159
160 #define OP_MODE_MAX     2
161 #define OP_MODE_DEF     0
162 #define OP_MODE_MIN     0
163
164 DEVICE_PARAM(OPMode, "Infrastruct, adhoc, AP mode ");
165
166 /* OpMode[] is used for transmit.
167    0: indicate infrastruct mode used
168    1: indicate adhoc mode used
169    2: indicate AP mode used
170 */
171
172
173 /* PSMode[]
174    0: indicate disable power saving mode
175    1: indicate enable power saving mode
176 */
177
178 #define PS_MODE_DEF     0
179
180 DEVICE_PARAM(PSMode, "Power saving mode");
181
182
183 #define SHORT_RETRY_MIN     0
184 #define SHORT_RETRY_MAX     31
185 #define SHORT_RETRY_DEF     8
186
187
188 DEVICE_PARAM(ShortRetryLimit, "Short frame retry limits");
189
190 #define LONG_RETRY_MIN     0
191 #define LONG_RETRY_MAX     15
192 #define LONG_RETRY_DEF     4
193
194
195 DEVICE_PARAM(LongRetryLimit, "long frame retry limits");
196
197
198 /* BasebandType[] baseband type selected
199    0: indicate 802.11a type
200    1: indicate 802.11b type
201    2: indicate 802.11g type
202 */
203 #define BBP_TYPE_MIN     0
204 #define BBP_TYPE_MAX     2
205 #define BBP_TYPE_DEF     2
206
207 DEVICE_PARAM(BasebandType, "baseband type");
208
209
210
211 /* 80211hEnable[]
212    0: indicate disable 802.11h
213    1: indicate enable 802.11h
214 */
215
216 #define X80211h_MODE_DEF     0
217
218 DEVICE_PARAM(b80211hEnable, "802.11h mode");
219
220
221 //
222 // Static vars definitions
223 //
224
225 static struct usb_device_id vt6656_table[] __devinitdata = {
226         {USB_DEVICE(VNT_USB_VENDOR_ID, VNT_USB_PRODUCT_ID)},
227         {}
228 };
229
230 // Frequency list (map channels to frequencies)
231 /*
232 static const long frequency_list[] = {
233     2412, 2417, 2422, 2427, 2432, 2437, 2442, 2447, 2452, 2457, 2462, 2467, 2472, 2484,
234     4915, 4920, 4925, 4935, 4940, 4945, 4960, 4980,
235     5035, 5040, 5045, 5055, 5060, 5080, 5170, 5180, 5190, 5200, 5210, 5220, 5230, 5240,
236     5260, 5280, 5300, 5320, 5500, 5520, 5540, 5560, 5580, 5600, 5620, 5640, 5660, 5680,
237     5700, 5745, 5765, 5785, 5805, 5825
238         };
239
240
241 #ifndef IW_ENCODE_NOKEY
242 #define IW_ENCODE_NOKEY         0x0800
243 #define IW_ENCODE_MODE  (IW_ENCODE_DISABLED | IW_ENCODE_RESTRICTED | IW_ENCODE_OPEN)
244 #endif
245
246 static const struct iw_handler_def      iwctl_handler_def;
247 */
248
249 /*---------------------  Static Functions  --------------------------*/
250
251 static int vt6656_probe(struct usb_interface *intf,
252                         const struct usb_device_id *id);
253 static void vt6656_disconnect(struct usb_interface *intf);
254
255 #ifdef CONFIG_PM        /* Minimal support for suspend and resume */
256 static int vt6656_suspend(struct usb_interface *intf, pm_message_t message);
257 static int vt6656_resume(struct usb_interface *intf);
258 #endif /* CONFIG_PM */
259
260 static struct net_device_stats *device_get_stats(struct net_device *dev);
261 static int  device_open(struct net_device *dev);
262 static int  device_xmit(struct sk_buff *skb, struct net_device *dev);
263 static void device_set_multi(struct net_device *dev);
264 static int  device_close(struct net_device *dev);
265 static int  device_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
266
267 static BOOL device_init_registers(PSDevice pDevice, DEVICE_INIT_TYPE InitType);
268 static BOOL device_init_defrag_cb(PSDevice pDevice);
269 static void device_init_diversity_timer(PSDevice pDevice);
270 static int  device_dma0_tx_80211(struct sk_buff *skb, struct net_device *dev);
271
272 static int  ethtool_ioctl(struct net_device *dev, void *useraddr);
273 static void device_free_tx_bufs(PSDevice pDevice);
274 static void device_free_rx_bufs(PSDevice pDevice);
275 static void device_free_int_bufs(PSDevice pDevice);
276 static void device_free_frag_bufs(PSDevice pDevice);
277 static BOOL device_alloc_bufs(PSDevice pDevice);
278
279 static int Read_config_file(PSDevice pDevice);
280 static UCHAR *Config_FileOperation(PSDevice pDevice);
281 static int Config_FileGetParameter(UCHAR *string, UCHAR *dest,UCHAR *source);
282
283 //2008-0714<Add>by Mike Liu
284 static BOOL device_release_WPADEV(PSDevice pDevice);
285
286 static void usb_device_reset(PSDevice pDevice);
287
288
289
290 /*---------------------  Export Variables  --------------------------*/
291
292 /*---------------------  Export Functions  --------------------------*/
293
294
295 static void
296 device_set_options(PSDevice pDevice) {
297
298     BYTE    abyBroadcastAddr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
299     BYTE    abySNAP_RFC1042[ETH_ALEN] = {0xAA, 0xAA, 0x03, 0x00, 0x00, 0x00};
300     u8 abySNAP_Bridgetunnel[ETH_ALEN] = {0xAA, 0xAA, 0x03, 0x00, 0x00, 0xF8};
301
302     memcpy(pDevice->abyBroadcastAddr, abyBroadcastAddr, ETH_ALEN);
303     memcpy(pDevice->abySNAP_RFC1042, abySNAP_RFC1042, ETH_ALEN);
304     memcpy(pDevice->abySNAP_Bridgetunnel, abySNAP_Bridgetunnel, ETH_ALEN);
305
306     pDevice->cbTD = TX_DESC_DEF0;
307     pDevice->cbRD = RX_DESC_DEF0;
308     pDevice->uChannel = CHANNEL_DEF;
309     pDevice->wRTSThreshold = RTS_THRESH_DEF;
310     pDevice->wFragmentationThreshold = FRAG_THRESH_DEF;
311     pDevice->byShortRetryLimit = SHORT_RETRY_DEF;
312     pDevice->byLongRetryLimit = LONG_RETRY_DEF;
313     pDevice->wMaxTransmitMSDULifetime = DEFAULT_MSDU_LIFETIME;
314     pDevice->byShortPreamble = PREAMBLE_TYPE_DEF;
315     pDevice->ePSMode = PS_MODE_DEF;
316     pDevice->b11hEnable = X80211h_MODE_DEF;
317     pDevice->eOPMode = OP_MODE_DEF;
318     pDevice->uConnectionRate = DATA_RATE_DEF;
319     if (pDevice->uConnectionRate < RATE_AUTO) pDevice->bFixRate = TRUE;
320     pDevice->byBBType = BBP_TYPE_DEF;
321     pDevice->byPacketType = pDevice->byBBType;
322     pDevice->byAutoFBCtrl = AUTO_FB_0;
323     pDevice->bUpdateBBVGA = TRUE;
324     pDevice->byFOETuning = 0;
325     pDevice->byAutoPwrTunning = 0;
326     pDevice->wCTSDuration = 0;
327     pDevice->byPreambleType = 0;
328     pDevice->bExistSWNetAddr = FALSE;
329 //    pDevice->bDiversityRegCtlON = TRUE;
330     pDevice->bDiversityRegCtlON = FALSE;
331 }
332
333
334 static void device_init_diversity_timer(PSDevice pDevice)
335 {
336     init_timer(&pDevice->TimerSQ3Tmax1);
337     pDevice->TimerSQ3Tmax1.data = (ULONG)pDevice;
338     pDevice->TimerSQ3Tmax1.function = (TimerFunction)TimerSQ3CallBack;
339     pDevice->TimerSQ3Tmax1.expires = RUN_AT(HZ);
340
341     init_timer(&pDevice->TimerSQ3Tmax2);
342     pDevice->TimerSQ3Tmax2.data = (ULONG)pDevice;
343     pDevice->TimerSQ3Tmax2.function = (TimerFunction)TimerSQ3CallBack;
344     pDevice->TimerSQ3Tmax2.expires = RUN_AT(HZ);
345
346     init_timer(&pDevice->TimerSQ3Tmax3);
347     pDevice->TimerSQ3Tmax3.data = (ULONG)pDevice;
348     pDevice->TimerSQ3Tmax3.function = (TimerFunction)TimerSQ3Tmax3CallBack;
349     pDevice->TimerSQ3Tmax3.expires = RUN_AT(HZ);
350
351     return;
352 }
353
354
355 //
356 // Initialiation of MAC & BBP registers
357 //
358
359 static BOOL device_init_registers(PSDevice pDevice, DEVICE_INIT_TYPE InitType)
360 {
361     u8 abyBroadcastAddr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
362     u8 abySNAP_RFC1042[ETH_ALEN] = {0xAA, 0xAA, 0x03, 0x00, 0x00, 0x00};
363     u8 abySNAP_Bridgetunnel[ETH_ALEN] = {0xAA, 0xAA, 0x03, 0x00, 0x00, 0xF8};
364     BYTE            byAntenna;
365     UINT            ii;
366     CMD_CARD_INIT   sInitCmd;
367     NTSTATUS        ntStatus = STATUS_SUCCESS;
368     RSP_CARD_INIT   sInitRsp;
369     PSMgmtObject    pMgmt = &(pDevice->sMgmtObj);
370     BYTE            byTmp;
371     BYTE            byCalibTXIQ = 0;
372     BYTE            byCalibTXDC = 0;
373     BYTE            byCalibRXIQ = 0;
374
375     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "---->INIbInitAdapter. [%d][%d]\n", InitType, pDevice->byPacketType);
376         spin_lock_irq(&pDevice->lock);
377         if (InitType == DEVICE_INIT_COLD) {
378                 memcpy(pDevice->abyBroadcastAddr, abyBroadcastAddr, ETH_ALEN);
379                 memcpy(pDevice->abySNAP_RFC1042, abySNAP_RFC1042, ETH_ALEN);
380                 memcpy(pDevice->abySNAP_Bridgetunnel,
381                        abySNAP_Bridgetunnel,
382                        ETH_ALEN);
383
384         if ( !FIRMWAREbCheckVersion(pDevice) ) {
385             if (FIRMWAREbDownload(pDevice) == TRUE) {
386                 if (FIRMWAREbBrach2Sram(pDevice) == FALSE) {
387                     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" FIRMWAREbBrach2Sram fail \n");
388                         spin_unlock_irq(&pDevice->lock);
389                     return FALSE;
390                 }
391             } else {
392
393                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" FIRMWAREbDownload fail \n");
394                 spin_unlock_irq(&pDevice->lock);
395                 return FALSE;
396             }
397         }
398
399         if ( !BBbVT3184Init(pDevice) ) {
400             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" BBbVT3184Init fail \n");
401             spin_unlock_irq(&pDevice->lock);
402             return FALSE;
403         }
404     }
405
406     sInitCmd.byInitClass = (BYTE)InitType;
407     sInitCmd.bExistSWNetAddr = (BYTE) pDevice->bExistSWNetAddr;
408     for(ii=0;ii<6;ii++)
409         sInitCmd.bySWNetAddr[ii] = pDevice->abyCurrentNetAddr[ii];
410     sInitCmd.byShortRetryLimit = pDevice->byShortRetryLimit;
411     sInitCmd.byLongRetryLimit = pDevice->byLongRetryLimit;
412
413     //issue Card_init command to device
414     ntStatus = CONTROLnsRequestOut(pDevice,
415                                     MESSAGE_TYPE_CARDINIT,
416                                     0,
417                                     0,
418                                     sizeof(CMD_CARD_INIT),
419                                     (PBYTE) &(sInitCmd));
420
421     if ( ntStatus != STATUS_SUCCESS ) {
422         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" Issue Card init fail \n");
423         spin_unlock_irq(&pDevice->lock);
424         return FALSE;
425     }
426     if (InitType == DEVICE_INIT_COLD) {
427
428         ntStatus = CONTROLnsRequestIn(pDevice,MESSAGE_TYPE_INIT_RSP,0,0,sizeof(RSP_CARD_INIT), (PBYTE) &(sInitRsp));
429
430         if (ntStatus != STATUS_SUCCESS) {
431             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Cardinit request in status fail!\n");
432             spin_unlock_irq(&pDevice->lock);
433             return FALSE;
434         }
435
436         //Local ID for AES functions
437         ntStatus = CONTROLnsRequestIn(pDevice,
438                                     MESSAGE_TYPE_READ,
439                                     MAC_REG_LOCALID,
440                                     MESSAGE_REQUEST_MACREG,
441                                     1,
442                                     &pDevice->byLocalID);
443
444         if ( ntStatus != STATUS_SUCCESS ) {
445             spin_unlock_irq(&pDevice->lock);
446             return FALSE;
447         }
448
449         // Do MACbSoftwareReset in MACvInitialize
450         // force CCK
451         pDevice->bCCK = TRUE;
452         pDevice->bProtectMode = FALSE;          //Only used in 11g type, sync with ERP IE
453         pDevice->bNonERPPresent = FALSE;
454         pDevice->bBarkerPreambleMd = FALSE;
455         if ( pDevice->bFixRate ) {
456             pDevice->wCurrentRate = (WORD) pDevice->uConnectionRate;
457         } else {
458             if ( pDevice->byBBType == BB_TYPE_11B )
459                 pDevice->wCurrentRate = RATE_11M;
460             else
461                 pDevice->wCurrentRate = RATE_54M;
462         }
463
464         CHvInitChannelTable(pDevice);
465
466         pDevice->byTopOFDMBasicRate = RATE_24M;
467         pDevice->byTopCCKBasicRate = RATE_1M;
468         pDevice->byRevId = 0;                   //Target to IF pin while programming to RF chip.
469         pDevice->byCurPwr = 0xFF;
470
471         pDevice->byCCKPwr = pDevice->abyEEPROM[EEP_OFS_PWR_CCK];
472         pDevice->byOFDMPwrG = pDevice->abyEEPROM[EEP_OFS_PWR_OFDMG];
473         // Load power Table
474         for (ii=0;ii<14;ii++) {
475             pDevice->abyCCKPwrTbl[ii] = pDevice->abyEEPROM[ii + EEP_OFS_CCK_PWR_TBL];
476             if (pDevice->abyCCKPwrTbl[ii] == 0)
477                 pDevice->abyCCKPwrTbl[ii] = pDevice->byCCKPwr;
478             pDevice->abyOFDMPwrTbl[ii] = pDevice->abyEEPROM[ii + EEP_OFS_OFDM_PWR_TBL];
479             if (pDevice->abyOFDMPwrTbl[ii] == 0)
480                 pDevice->abyOFDMPwrTbl[ii] = pDevice->byOFDMPwrG;
481         }
482
483           //original zonetype is USA,but customize zonetype is europe,
484           // then need recover 12,13 ,14 channel  with 11 channel
485           if(((pDevice->abyEEPROM[EEP_OFS_ZONETYPE] == ZoneType_Japan) ||
486                 (pDevice->abyEEPROM[EEP_OFS_ZONETYPE] == ZoneType_Europe))&&
487              (pDevice->byOriginalZonetype == ZoneType_USA)) {
488             for(ii=11;ii<14;ii++) {
489                 pDevice->abyCCKPwrTbl[ii] = pDevice->abyCCKPwrTbl[10];
490                pDevice->abyOFDMPwrTbl[ii] = pDevice->abyOFDMPwrTbl[10];
491             }
492           }
493
494         //{{ RobertYu: 20041124
495         pDevice->byOFDMPwrA = 0x34; // same as RFbMA2829SelectChannel
496         // Load OFDM A Power Table
497         for (ii=0;ii<CB_MAX_CHANNEL_5G;ii++) { //RobertYu:20041224, bug using CB_MAX_CHANNEL
498             pDevice->abyOFDMAPwrTbl[ii] = pDevice->abyEEPROM[ii + EEP_OFS_OFDMA_PWR_TBL];
499             if (pDevice->abyOFDMAPwrTbl[ii] == 0)
500                 pDevice->abyOFDMAPwrTbl[ii] = pDevice->byOFDMPwrA;
501         }
502         //}} RobertYu
503
504         byAntenna = pDevice->abyEEPROM[EEP_OFS_ANTENNA];
505         if (byAntenna & EEP_ANTINV)
506             pDevice->bTxRxAntInv = TRUE;
507         else
508             pDevice->bTxRxAntInv = FALSE;
509
510         byAntenna &= (EEP_ANTENNA_AUX | EEP_ANTENNA_MAIN);
511
512         if (byAntenna == 0) // if not set default is All
513             byAntenna = (EEP_ANTENNA_AUX | EEP_ANTENNA_MAIN);
514
515         if (byAntenna == (EEP_ANTENNA_AUX | EEP_ANTENNA_MAIN)) {
516             pDevice->byAntennaCount = 2;
517             pDevice->byTxAntennaMode = ANT_B;
518             pDevice->dwTxAntennaSel = 1;
519             pDevice->dwRxAntennaSel = 1;
520             if (pDevice->bTxRxAntInv == TRUE)
521                 pDevice->byRxAntennaMode = ANT_A;
522             else
523                 pDevice->byRxAntennaMode = ANT_B;
524
525             if (pDevice->bDiversityRegCtlON)
526                 pDevice->bDiversityEnable = TRUE;
527             else
528                 pDevice->bDiversityEnable = FALSE;
529         } else  {
530             pDevice->bDiversityEnable = FALSE;
531             pDevice->byAntennaCount = 1;
532             pDevice->dwTxAntennaSel = 0;
533             pDevice->dwRxAntennaSel = 0;
534             if (byAntenna & EEP_ANTENNA_AUX) {
535                 pDevice->byTxAntennaMode = ANT_A;
536                 if (pDevice->bTxRxAntInv == TRUE)
537                     pDevice->byRxAntennaMode = ANT_B;
538                 else
539                     pDevice->byRxAntennaMode = ANT_A;
540             } else {
541                 pDevice->byTxAntennaMode = ANT_B;
542                 if (pDevice->bTxRxAntInv == TRUE)
543                     pDevice->byRxAntennaMode = ANT_A;
544                 else
545                     pDevice->byRxAntennaMode = ANT_B;
546             }
547         }
548         pDevice->ulDiversityNValue = 100*255;
549         pDevice->ulDiversityMValue = 100*16;
550         pDevice->byTMax = 1;
551         pDevice->byTMax2 = 4;
552         pDevice->ulSQ3TH = 0;
553         pDevice->byTMax3 = 64;
554         // -----------------------------------------------------------------
555
556         //Get Auto Fall Back Type
557         pDevice->byAutoFBCtrl = AUTO_FB_0;
558
559         // Set SCAN Time
560         pDevice->uScanTime = WLAN_SCAN_MINITIME;
561
562         // default Auto Mode
563         //pDevice->NetworkType = Ndis802_11Automode;
564         pDevice->eConfigPHYMode = PHY_TYPE_AUTO;
565         pDevice->byBBType = BB_TYPE_11G;
566
567         // initialize BBP registers
568         pDevice->ulTxPower = 25;
569
570         // Get Channel range
571         pDevice->byMinChannel = 1;
572         pDevice->byMaxChannel = CB_MAX_CHANNEL;
573
574         // Get RFType
575         pDevice->byRFType = sInitRsp.byRFType;
576
577         if ((pDevice->byRFType & RF_EMU) != 0) {
578             // force change RevID for VT3253 emu
579             pDevice->byRevId = 0x80;
580         }
581
582         // Load EEPROM calibrated vt3266 parameters
583         if (pDevice->byRFType == RF_VT3226D0) {
584             if((pDevice->abyEEPROM[EEP_OFS_MAJOR_VER] == 0x1) &&
585                 (pDevice->abyEEPROM[EEP_OFS_MINOR_VER] >= 0x4)) {
586                 byCalibTXIQ = pDevice->abyEEPROM[EEP_OFS_CALIB_TX_IQ];
587                 byCalibTXDC = pDevice->abyEEPROM[EEP_OFS_CALIB_TX_DC];
588                 byCalibRXIQ = pDevice->abyEEPROM[EEP_OFS_CALIB_RX_IQ];
589                 if( (byCalibTXIQ || byCalibTXDC || byCalibRXIQ) ) {
590                     ControlvWriteByte(pDevice, MESSAGE_REQUEST_BBREG, 0xFF, 0x03); // CR255, Set BB to support TX/RX IQ and DC compensation Mode
591                     ControlvWriteByte(pDevice, MESSAGE_REQUEST_BBREG, 0xFB, byCalibTXIQ); // CR251, TX I/Q Imbalance Calibration
592                     ControlvWriteByte(pDevice, MESSAGE_REQUEST_BBREG, 0xFC, byCalibTXDC); // CR252, TX DC-Offset Calibration
593                     ControlvWriteByte(pDevice, MESSAGE_REQUEST_BBREG, 0xFD, byCalibRXIQ); // CR253, RX I/Q Imbalance Calibration
594                 } else {
595                 // turn off BB Calibration compensation
596                     ControlvWriteByte(pDevice, MESSAGE_REQUEST_BBREG, 0xFF, 0x0); // CR255
597                 }
598             }
599         }
600         pMgmt->eScanType = WMAC_SCAN_PASSIVE;
601         pMgmt->uCurrChannel = pDevice->uChannel;
602         pMgmt->uIBSSChannel = pDevice->uChannel;
603         CARDbSetMediaChannel(pDevice, pMgmt->uCurrChannel);
604
605         // get Permanent network address
606         memcpy(pDevice->abyPermanentNetAddr,&(sInitRsp.byNetAddr[0]),6);
607         memcpy(pDevice->abyCurrentNetAddr,
608                pDevice->abyPermanentNetAddr,
609                ETH_ALEN);
610
611         // if exist SW network address, use SW network address.
612
613         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Network address = %02x-%02x-%02x=%02x-%02x-%02x\n",
614             pDevice->abyCurrentNetAddr[0],
615             pDevice->abyCurrentNetAddr[1],
616             pDevice->abyCurrentNetAddr[2],
617             pDevice->abyCurrentNetAddr[3],
618             pDevice->abyCurrentNetAddr[4],
619             pDevice->abyCurrentNetAddr[5]);
620     }
621
622
623
624     // Set BB and packet type at the same time.
625     // Set Short Slot Time, xIFS, and RSPINF.
626     if (pDevice->byBBType == BB_TYPE_11A) {
627         CARDbAddBasicRate(pDevice, RATE_6M);
628         pDevice->bShortSlotTime = TRUE;
629     } else {
630         CARDbAddBasicRate(pDevice, RATE_1M);
631         pDevice->bShortSlotTime = FALSE;
632     }
633     BBvSetShortSlotTime(pDevice);
634     CARDvSetBSSMode(pDevice);
635
636     if (pDevice->bUpdateBBVGA) {
637         pDevice->byBBVGACurrent = pDevice->abyBBVGA[0];
638         pDevice->byBBVGANew = pDevice->byBBVGACurrent;
639         BBvSetVGAGainOffset(pDevice, pDevice->abyBBVGA[0]);
640     }
641
642     pDevice->byRadioCtl = pDevice->abyEEPROM[EEP_OFS_RADIOCTL];
643     pDevice->bHWRadioOff = FALSE;
644     if ( (pDevice->byRadioCtl & EEP_RADIOCTL_ENABLE) != 0 ) {
645         ntStatus = CONTROLnsRequestIn(pDevice,
646                                     MESSAGE_TYPE_READ,
647                                     MAC_REG_GPIOCTL1,
648                                     MESSAGE_REQUEST_MACREG,
649                                     1,
650                                     &byTmp);
651
652         if ( ntStatus != STATUS_SUCCESS ) {
653             spin_unlock_irq(&pDevice->lock);
654             return FALSE;
655         }
656         if ( (byTmp & GPIO3_DATA) == 0 ) {
657             pDevice->bHWRadioOff = TRUE;
658             MACvRegBitsOn(pDevice,MAC_REG_GPIOCTL1,GPIO3_INTMD);
659         } else {
660             MACvRegBitsOff(pDevice,MAC_REG_GPIOCTL1,GPIO3_INTMD);
661             pDevice->bHWRadioOff = FALSE;
662         }
663
664     } //EEP_RADIOCTL_ENABLE
665
666     ControlvMaskByte(pDevice,MESSAGE_REQUEST_MACREG,MAC_REG_PAPEDELAY,LEDSTS_TMLEN,0x38);
667     ControlvMaskByte(pDevice,MESSAGE_REQUEST_MACREG,MAC_REG_PAPEDELAY,LEDSTS_STS,LEDSTS_SLOW);
668     MACvRegBitsOn(pDevice,MAC_REG_GPIOCTL0,0x01);
669
670     if ((pDevice->bHWRadioOff == TRUE) || (pDevice->bRadioControlOff == TRUE)) {
671         CARDbRadioPowerOff(pDevice);
672     } else {
673         CARDbRadioPowerOn(pDevice);
674     }
675
676     spin_unlock_irq(&pDevice->lock);
677     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"<----INIbInitAdapter Exit\n");
678     return TRUE;
679 }
680
681 static BOOL device_release_WPADEV(PSDevice pDevice)
682 {
683   viawget_wpa_header *wpahdr;
684   int ii=0;
685  // wait_queue_head_t   Set_wait;
686   //send device close to wpa_supplicnat layer
687     if (pDevice->bWPADEVUp==TRUE) {
688                  wpahdr = (viawget_wpa_header *)pDevice->skb->data;
689                  wpahdr->type = VIAWGET_DEVICECLOSE_MSG;
690                  wpahdr->resp_ie_len = 0;
691                  wpahdr->req_ie_len = 0;
692                  skb_put(pDevice->skb, sizeof(viawget_wpa_header));
693                  pDevice->skb->dev = pDevice->wpadev;
694                  skb_reset_mac_header(pDevice->skb);
695                  pDevice->skb->pkt_type = PACKET_HOST;
696                  pDevice->skb->protocol = htons(ETH_P_802_2);
697                  memset(pDevice->skb->cb, 0, sizeof(pDevice->skb->cb));
698                  netif_rx(pDevice->skb);
699                  pDevice->skb = dev_alloc_skb((int)pDevice->rx_buf_sz);
700
701  //wait release WPADEV
702               //    init_waitqueue_head(&Set_wait);
703               //    wait_event_timeout(Set_wait, ((pDevice->wpadev==NULL)&&(pDevice->skb == NULL)),5*HZ);    //1s wait
704               while(pDevice->bWPADEVUp==TRUE) {
705                 set_current_state(TASK_UNINTERRUPTIBLE);
706                  schedule_timeout (HZ/20);          //wait 50ms
707                  ii++;
708                 if(ii>20)
709                   break;
710               }
711            };
712     return TRUE;
713 }
714
715 #ifdef CONFIG_PM        /* Minimal support for suspend and resume */
716
717 static int vt6656_suspend(struct usb_interface *intf, pm_message_t message)
718 {
719  PSDevice  pDevice = usb_get_intfdata(intf);
720  struct net_device *dev = pDevice->dev;
721
722  printk("VNTWUSB Suspend Start======>\n");
723 if(dev != NULL) {
724   if(pDevice->flags & DEVICE_FLAGS_OPENED)
725      device_close(dev);
726 }
727
728  usb_put_dev(interface_to_usbdev(intf));
729  return 0;
730 }
731
732 static int vt6656_resume(struct usb_interface *intf)
733 {
734  PSDevice  pDevice = usb_get_intfdata(intf);
735  struct net_device *dev = pDevice->dev;
736
737  printk("VNTWUSB Resume Start======>\n");
738  if(dev != NULL) {
739   usb_get_dev(interface_to_usbdev(intf));
740   if(!(pDevice->flags & DEVICE_FLAGS_OPENED)) {
741     if(device_open(dev)!=0)
742         printk("VNTWUSB Resume Start======>open fail\n");
743    }
744  }
745  return 0;
746 }
747
748 #endif /* CONFIG_PM */
749
750 static const struct net_device_ops device_netdev_ops = {
751     .ndo_open               = device_open,
752     .ndo_stop               = device_close,
753     .ndo_do_ioctl           = device_ioctl,
754     .ndo_get_stats          = device_get_stats,
755     .ndo_start_xmit         = device_xmit,
756     .ndo_set_multicast_list = device_set_multi,
757 };
758
759
760 static int __devinit
761 vt6656_probe(struct usb_interface *intf, const struct usb_device_id *id)
762 {
763         u8 fake_mac[ETH_ALEN] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x01};
764         struct usb_device *udev = interface_to_usbdev(intf);
765     int         rc = 0;
766     struct net_device *netdev = NULL;
767     PSDevice    pDevice = NULL;
768
769
770     printk(KERN_NOTICE "%s Ver. %s\n",DEVICE_FULL_DRV_NAM, DEVICE_VERSION);
771     printk(KERN_NOTICE "Copyright (c) 2004 VIA Networking Technologies, Inc.\n");
772
773   udev = usb_get_dev(udev);
774
775     netdev = alloc_etherdev(sizeof(DEVICE_INFO));
776
777     if (netdev == NULL) {
778         printk(KERN_ERR DEVICE_NAME ": allocate net device failed \n");
779         kfree(pDevice);
780             goto err_nomem;
781     }
782
783     pDevice = netdev_priv(netdev);
784     memset(pDevice, 0, sizeof(DEVICE_INFO));
785
786     pDevice->dev = netdev;
787     pDevice->usb = udev;
788
789     // Set initial settings
790     device_set_options(pDevice);
791     spin_lock_init(&pDevice->lock);
792
793     pDevice->tx_80211 = device_dma0_tx_80211;
794     pDevice->sMgmtObj.pAdapter = (void *)pDevice;
795
796     netdev->netdev_ops         = &device_netdev_ops;
797
798         netdev->wireless_handlers = (struct iw_handler_def *)&iwctl_handler_def;
799
800    //2008-0623-01<Remark>by MikeLiu
801   //2007-0821-01<Add>by MikeLiu
802          usb_set_intfdata(intf, pDevice);
803         SET_NETDEV_DEV(netdev, &intf->dev);
804     memcpy(pDevice->dev->dev_addr, fake_mac, ETH_ALEN);
805     rc = register_netdev(netdev);
806     if (rc != 0) {
807         printk(KERN_ERR DEVICE_NAME " Failed to register netdev\n");
808                 free_netdev(netdev);
809         kfree(pDevice);
810         return -ENODEV;
811     }
812
813 //2008-07-21-01<Add>by MikeLiu
814 //register wpadev
815 #if 0
816    if(wpa_set_wpadev(pDevice, 1)!=0) {
817      printk("Fail to Register WPADEV?\n");
818         unregister_netdev(pDevice->dev);
819         free_netdev(netdev);
820         kfree(pDevice);
821    }
822 #endif
823          usb_device_reset(pDevice);
824
825 #ifdef SndEvt_ToAPI
826 {
827   union iwreq_data      wrqu;
828   memset(&wrqu, 0, sizeof(wrqu));
829   wrqu.data.flags = RT_INSMOD_EVENT_FLAG;
830   wrqu.data.length =IFNAMSIZ;
831   wireless_send_event(pDevice->dev, IWEVCUSTOM, &wrqu, pDevice->dev->name);
832 }
833 #endif
834
835         return 0;
836
837
838 err_nomem:
839  //2008-0922-01<Add>by MikeLiu, decrease usb counter.
840     usb_put_dev(udev);
841
842     return -ENOMEM;
843 }
844
845
846 static void device_free_tx_bufs(PSDevice pDevice)
847 {
848     PUSB_SEND_CONTEXT pTxContext;
849     int ii;
850
851     for (ii = 0; ii < pDevice->cbTD; ii++) {
852
853         pTxContext = pDevice->apTD[ii];
854         //de-allocate URBs
855         if (pTxContext->pUrb) {
856             usb_kill_urb(pTxContext->pUrb);
857             usb_free_urb(pTxContext->pUrb);
858         }
859         if (pTxContext)
860             kfree(pTxContext);
861     }
862     return;
863 }
864
865
866 static void device_free_rx_bufs(PSDevice pDevice)
867 {
868     PRCB pRCB;
869     int ii;
870
871     for (ii = 0; ii < pDevice->cbRD; ii++) {
872
873         pRCB = pDevice->apRCB[ii];
874         //de-allocate URBs
875         if (pRCB->pUrb) {
876             usb_kill_urb(pRCB->pUrb);
877             usb_free_urb(pRCB->pUrb);
878         }
879         //de-allocate skb
880         if (pRCB->skb)
881             dev_kfree_skb(pRCB->skb);
882     }
883     if (pDevice->pRCBMem)
884         kfree(pDevice->pRCBMem);
885
886     return;
887 }
888
889 //2007-1107-02<Add>by MikeLiu
890 static void usb_device_reset(PSDevice pDevice)
891 {
892  int status;
893  status = usb_reset_device(pDevice->usb);
894         if (status)
895             printk("usb_device_reset fail status=%d\n",status);
896         return ;
897 }
898
899 static void device_free_int_bufs(PSDevice pDevice)
900 {
901     if (pDevice->intBuf.pDataBuf != NULL)
902         kfree(pDevice->intBuf.pDataBuf);
903     return;
904 }
905
906
907 static BOOL device_alloc_bufs(PSDevice pDevice) {
908
909     PUSB_SEND_CONTEXT pTxContext;
910     PRCB pRCB;
911     int ii;
912
913
914     for (ii = 0; ii < pDevice->cbTD; ii++) {
915
916         pTxContext = kmalloc(sizeof(USB_SEND_CONTEXT), GFP_KERNEL);
917         if (pTxContext == NULL) {
918             DBG_PRT(MSG_LEVEL_ERR,KERN_ERR "%s : allocate tx usb context failed\n", pDevice->dev->name);
919             goto free_tx;
920         }
921         pDevice->apTD[ii] = pTxContext;
922         pTxContext->pDevice = (void *) pDevice;
923         //allocate URBs
924         pTxContext->pUrb = usb_alloc_urb(0, GFP_ATOMIC);
925         if (pTxContext->pUrb == NULL) {
926             DBG_PRT(MSG_LEVEL_ERR,KERN_ERR "alloc tx urb failed\n");
927             goto free_tx;
928         }
929         pTxContext->bBoolInUse = FALSE;
930     }
931
932     // allocate rcb mem
933     pDevice->pRCBMem = kmalloc((sizeof(RCB) * pDevice->cbRD), GFP_KERNEL);
934     if (pDevice->pRCBMem == NULL) {
935         DBG_PRT(MSG_LEVEL_ERR,KERN_ERR "%s : alloc rx usb context failed\n", pDevice->dev->name);
936         goto free_tx;
937     }
938
939
940     pDevice->FirstRecvFreeList = NULL;
941     pDevice->LastRecvFreeList = NULL;
942     pDevice->FirstRecvMngList = NULL;
943     pDevice->LastRecvMngList = NULL;
944     pDevice->NumRecvFreeList = 0;
945     memset(pDevice->pRCBMem, 0, (sizeof(RCB) * pDevice->cbRD));
946     pRCB = (PRCB) pDevice->pRCBMem;
947
948     for (ii = 0; ii < pDevice->cbRD; ii++) {
949
950         pDevice->apRCB[ii] = pRCB;
951         pRCB->pDevice = (void *) pDevice;
952         //allocate URBs
953         pRCB->pUrb = usb_alloc_urb(0, GFP_ATOMIC);
954
955         if (pRCB->pUrb == NULL) {
956             DBG_PRT(MSG_LEVEL_ERR,KERN_ERR" Failed to alloc rx urb\n");
957             goto free_rx_tx;
958         }
959         pRCB->skb = dev_alloc_skb((int)pDevice->rx_buf_sz);
960         if (pRCB->skb == NULL) {
961             DBG_PRT(MSG_LEVEL_ERR,KERN_ERR" Failed to alloc rx skb\n");
962             goto free_rx_tx;
963         }
964         pRCB->skb->dev = pDevice->dev;
965         pRCB->bBoolInUse = FALSE;
966         EnqueueRCB(pDevice->FirstRecvFreeList, pDevice->LastRecvFreeList, pRCB);
967         pDevice->NumRecvFreeList++;
968         pRCB++;
969     }
970
971
972         pDevice->pControlURB = usb_alloc_urb(0, GFP_ATOMIC);
973         if (pDevice->pControlURB == NULL) {
974             DBG_PRT(MSG_LEVEL_ERR,KERN_ERR"Failed to alloc control urb\n");
975             goto free_rx_tx;
976         }
977
978         pDevice->pInterruptURB = usb_alloc_urb(0, GFP_ATOMIC);
979         if (pDevice->pInterruptURB == NULL) {
980             DBG_PRT(MSG_LEVEL_ERR,KERN_ERR"Failed to alloc int urb\n");
981             usb_kill_urb(pDevice->pControlURB);
982             usb_free_urb(pDevice->pControlURB);
983             goto free_rx_tx;
984         }
985
986     pDevice->intBuf.pDataBuf = kmalloc(MAX_INTERRUPT_SIZE, GFP_KERNEL);
987         if (pDevice->intBuf.pDataBuf == NULL) {
988             DBG_PRT(MSG_LEVEL_ERR,KERN_ERR"Failed to alloc int buf\n");
989             usb_kill_urb(pDevice->pControlURB);
990             usb_kill_urb(pDevice->pInterruptURB);
991             usb_free_urb(pDevice->pControlURB);
992             usb_free_urb(pDevice->pInterruptURB);
993             goto free_rx_tx;
994         }
995
996     return TRUE;
997
998 free_rx_tx:
999     device_free_rx_bufs(pDevice);
1000
1001 free_tx:
1002     device_free_tx_bufs(pDevice);
1003
1004         return FALSE;
1005 }
1006
1007
1008
1009
1010 static BOOL device_init_defrag_cb(PSDevice pDevice) {
1011     int i;
1012     PSDeFragControlBlock pDeF;
1013
1014     /* Init the fragment ctl entries */
1015     for (i = 0; i < CB_MAX_RX_FRAG; i++) {
1016         pDeF = &(pDevice->sRxDFCB[i]);
1017         if (!device_alloc_frag_buf(pDevice, pDeF)) {
1018             DBG_PRT(MSG_LEVEL_ERR,KERN_ERR "%s: can not alloc frag bufs\n",
1019                 pDevice->dev->name);
1020             goto free_frag;
1021         };
1022     }
1023     pDevice->cbDFCB = CB_MAX_RX_FRAG;
1024     pDevice->cbFreeDFCB = pDevice->cbDFCB;
1025     return TRUE;
1026
1027 free_frag:
1028     device_free_frag_bufs(pDevice);
1029     return FALSE;
1030 }
1031
1032
1033
1034 static void device_free_frag_bufs(PSDevice pDevice) {
1035     PSDeFragControlBlock pDeF;
1036     int i;
1037
1038     for (i = 0; i < CB_MAX_RX_FRAG; i++) {
1039
1040         pDeF = &(pDevice->sRxDFCB[i]);
1041
1042         if (pDeF->skb)
1043             dev_kfree_skb(pDeF->skb);
1044     }
1045 }
1046
1047
1048
1049 BOOL device_alloc_frag_buf(PSDevice pDevice, PSDeFragControlBlock pDeF) {
1050
1051     pDeF->skb = dev_alloc_skb((int)pDevice->rx_buf_sz);
1052     if (pDeF->skb == NULL)
1053         return FALSE;
1054     ASSERT(pDeF->skb);
1055     pDeF->skb->dev = pDevice->dev;
1056
1057     return TRUE;
1058 }
1059
1060
1061 /*-----------------------------------------------------------------*/
1062
1063 static int  device_open(struct net_device *dev) {
1064     PSDevice    pDevice=(PSDevice) netdev_priv(dev);
1065
1066 #ifdef WPA_SM_Transtatus
1067      extern SWPAResult wpa_Result;
1068      memset(wpa_Result.ifname,0,sizeof(wpa_Result.ifname));
1069      wpa_Result.proto = 0;
1070      wpa_Result.key_mgmt = 0;
1071      wpa_Result.eap_type = 0;
1072      wpa_Result.authenticated = FALSE;
1073      pDevice->fWPA_Authened = FALSE;
1074 #endif
1075
1076     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " device_open...\n");
1077
1078
1079     pDevice->rx_buf_sz = MAX_TOTAL_SIZE_WITH_ALL_HEADERS;
1080
1081     if (device_alloc_bufs(pDevice) == FALSE) {
1082         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " device_alloc_bufs fail... \n");
1083         return -ENOMEM;
1084     }
1085
1086     if (device_init_defrag_cb(pDevice)== FALSE) {
1087         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " Initial defragement cb fail \n");
1088         goto free_rx_tx;
1089     }
1090
1091     MP_CLEAR_FLAG(pDevice, fMP_DISCONNECTED);
1092     MP_CLEAR_FLAG(pDevice, fMP_CONTROL_READS);
1093     MP_CLEAR_FLAG(pDevice, fMP_CONTROL_WRITES);
1094     MP_SET_FLAG(pDevice, fMP_POST_READS);
1095     MP_SET_FLAG(pDevice, fMP_POST_WRITES);
1096
1097    //read config file
1098     Read_config_file(pDevice);
1099
1100     if (device_init_registers(pDevice, DEVICE_INIT_COLD) == FALSE) {
1101         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " init register fail\n");
1102         goto free_all;
1103     }
1104
1105     device_set_multi(pDevice->dev);
1106     // Init for Key Management
1107
1108     KeyvInitTable(pDevice,&pDevice->sKey);
1109     memcpy(pDevice->sMgmtObj.abyMACAddr, pDevice->abyCurrentNetAddr, ETH_ALEN);
1110     memcpy(pDevice->dev->dev_addr, pDevice->abyCurrentNetAddr, ETH_ALEN);
1111     pDevice->bStopTx0Pkt = FALSE;
1112     pDevice->bStopDataPkt = FALSE;
1113     pDevice->bRoaming = FALSE;  //DavidWang
1114     pDevice->bIsRoaming = FALSE;//DavidWang
1115     pDevice->bEnableRoaming = FALSE;
1116     if (pDevice->bDiversityRegCtlON) {
1117         device_init_diversity_timer(pDevice);
1118     }
1119
1120     vMgrObjectInit(pDevice);
1121     tasklet_init(&pDevice->RxMngWorkItem, (void *)RXvMngWorkItem, (unsigned long)pDevice);
1122     tasklet_init(&pDevice->ReadWorkItem, (void *)RXvWorkItem, (unsigned long)pDevice);
1123     tasklet_init(&pDevice->EventWorkItem, (void *)INTvWorkItem, (unsigned long)pDevice);
1124     add_timer(&(pDevice->sMgmtObj.sTimerSecondCallback));
1125     pDevice->int_interval = 100;  //Max 100 microframes.
1126     pDevice->eEncryptionStatus = Ndis802_11EncryptionDisabled;
1127
1128     pDevice->bIsRxWorkItemQueued = TRUE;
1129     pDevice->fKillEventPollingThread = FALSE;
1130     pDevice->bEventAvailable = FALSE;
1131
1132    pDevice->bWPADEVUp = FALSE;
1133 #ifdef WPA_SUPPLICANT_DRIVER_WEXT_SUPPORT
1134      pDevice->bwextstep0 = FALSE;
1135      pDevice->bwextstep1 = FALSE;
1136      pDevice->bwextstep2 = FALSE;
1137      pDevice->bwextstep3 = FALSE;
1138      pDevice->bWPASuppWextEnabled = FALSE;
1139 #endif
1140     pDevice->byReAssocCount = 0;
1141
1142     RXvWorkItem(pDevice);
1143     INTvWorkItem(pDevice);
1144
1145     // Patch: if WEP key already set by iwconfig but device not yet open
1146     if ((pDevice->bEncryptionEnable == TRUE) && (pDevice->bTransmitKey == TRUE)) {
1147          spin_lock_irq(&pDevice->lock);
1148          KeybSetDefaultKey( pDevice,
1149                             &(pDevice->sKey),
1150                             pDevice->byKeyIndex | (1 << 31),
1151                             pDevice->uKeyLength,
1152                             NULL,
1153                             pDevice->abyKey,
1154                             KEY_CTL_WEP
1155                           );
1156          spin_unlock_irq(&pDevice->lock);
1157          pDevice->eEncryptionStatus = Ndis802_11Encryption1Enabled;
1158     }
1159
1160     if (pDevice->sMgmtObj.eConfigMode == WMAC_CONFIG_AP) {
1161                 bScheduleCommand((void *) pDevice, WLAN_CMD_RUN_AP, NULL);
1162         }
1163         else {
1164         //mike:mark@2008-11-10
1165           bScheduleCommand((void *) pDevice, WLAN_CMD_BSSID_SCAN, NULL);
1166           /* bScheduleCommand((void *) pDevice, WLAN_CMD_SSID, NULL); */
1167     }
1168
1169
1170     netif_stop_queue(pDevice->dev);
1171     pDevice->flags |= DEVICE_FLAGS_OPENED;
1172
1173 #ifdef SndEvt_ToAPI
1174 {
1175   union iwreq_data      wrqu;
1176   memset(&wrqu, 0, sizeof(wrqu));
1177   wrqu.data.flags = RT_UPDEV_EVENT_FLAG;
1178   wireless_send_event(pDevice->dev, IWEVCUSTOM, &wrqu, NULL);
1179 }
1180 #endif
1181
1182     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "device_open success.. \n");
1183     return 0;
1184
1185 free_all:
1186     device_free_frag_bufs(pDevice);
1187 free_rx_tx:
1188     device_free_rx_bufs(pDevice);
1189     device_free_tx_bufs(pDevice);
1190     device_free_int_bufs(pDevice);
1191         usb_kill_urb(pDevice->pControlURB);
1192         usb_kill_urb(pDevice->pInterruptURB);
1193     usb_free_urb(pDevice->pControlURB);
1194     usb_free_urb(pDevice->pInterruptURB);
1195
1196     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "device_open fail.. \n");
1197     return -ENOMEM;
1198 }
1199
1200
1201
1202 static int  device_close(struct net_device *dev) {
1203     PSDevice    pDevice=(PSDevice) netdev_priv(dev);
1204     PSMgmtObject     pMgmt = &(pDevice->sMgmtObj);
1205
1206         int uu;
1207
1208     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "device_close1 \n");
1209     if (pDevice == NULL)
1210         return -ENODEV;
1211
1212 #ifdef SndEvt_ToAPI
1213 {
1214   union iwreq_data      wrqu;
1215   memset(&wrqu, 0, sizeof(wrqu));
1216   wrqu.data.flags = RT_DOWNDEV_EVENT_FLAG;
1217   wireless_send_event(pDevice->dev, IWEVCUSTOM, &wrqu, NULL);
1218 }
1219 #endif
1220
1221 //2007-1121-02<Add>by EinsnLiu
1222     if (pDevice->bLinkPass) {
1223         bScheduleCommand((void *) pDevice, WLAN_CMD_DISASSOCIATE, NULL);
1224         mdelay(30);
1225     }
1226 //End Add
1227
1228 //2008-0714-01<Add>by MikeLiu
1229 device_release_WPADEV(pDevice);
1230
1231         memset(pMgmt->abyDesireSSID, 0, WLAN_IEHDR_LEN + WLAN_SSID_MAXLEN + 1);
1232         pMgmt->bShareKeyAlgorithm = FALSE;
1233         pDevice->bEncryptionEnable = FALSE;
1234         pDevice->eEncryptionStatus = Ndis802_11EncryptionDisabled;
1235             spin_lock_irq(&pDevice->lock);
1236             for(uu=0;uu<MAX_KEY_TABLE;uu++)
1237                 MACvDisableKeyEntry(pDevice,uu);
1238             spin_unlock_irq(&pDevice->lock);
1239
1240     if ((pDevice->flags & DEVICE_FLAGS_UNPLUG) == FALSE) {
1241         MACbShutdown(pDevice);
1242     }
1243     netif_stop_queue(pDevice->dev);
1244     MP_SET_FLAG(pDevice, fMP_DISCONNECTED);
1245     MP_CLEAR_FLAG(pDevice, fMP_POST_WRITES);
1246     MP_CLEAR_FLAG(pDevice, fMP_POST_READS);
1247     pDevice->fKillEventPollingThread = TRUE;
1248     del_timer(&pDevice->sTimerCommand);
1249     del_timer(&pMgmt->sTimerSecondCallback);
1250
1251 //2007-0115-02<Add>by MikeLiu
1252 #ifdef TxInSleep
1253     del_timer(&pDevice->sTimerTxData);
1254 #endif
1255
1256     if (pDevice->bDiversityRegCtlON) {
1257         del_timer(&pDevice->TimerSQ3Tmax1);
1258         del_timer(&pDevice->TimerSQ3Tmax2);
1259         del_timer(&pDevice->TimerSQ3Tmax3);
1260     }
1261     tasklet_kill(&pDevice->RxMngWorkItem);
1262     tasklet_kill(&pDevice->ReadWorkItem);
1263     tasklet_kill(&pDevice->EventWorkItem);
1264
1265    pDevice->bRoaming = FALSE;  //DavidWang
1266    pDevice->bIsRoaming = FALSE;//DavidWang
1267    pDevice->bEnableRoaming = FALSE;
1268     pDevice->bCmdRunning = FALSE;
1269     pDevice->bLinkPass = FALSE;
1270     memset(pMgmt->abyCurrBSSID, 0, 6);
1271     pMgmt->eCurrState = WMAC_STATE_IDLE;
1272
1273     device_free_tx_bufs(pDevice);
1274     device_free_rx_bufs(pDevice);
1275     device_free_int_bufs(pDevice);
1276     device_free_frag_bufs(pDevice);
1277
1278         usb_kill_urb(pDevice->pControlURB);
1279         usb_kill_urb(pDevice->pInterruptURB);
1280     usb_free_urb(pDevice->pControlURB);
1281     usb_free_urb(pDevice->pInterruptURB);
1282
1283     BSSvClearNodeDBTable(pDevice, 0);
1284     pDevice->flags &=(~DEVICE_FLAGS_OPENED);
1285
1286     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "device_close2 \n");
1287
1288     return 0;
1289 }
1290
1291
1292 static void __devexit vt6656_disconnect(struct usb_interface *intf)
1293 {
1294
1295         PSDevice  pDevice = usb_get_intfdata(intf);
1296
1297     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "device_disconnect1.. \n");
1298     if (pDevice == NULL)
1299         return;
1300
1301 #ifdef SndEvt_ToAPI
1302 {
1303   union iwreq_data      wrqu;
1304   memset(&wrqu, 0, sizeof(wrqu));
1305   wrqu.data.flags = RT_RMMOD_EVENT_FLAG;
1306   wireless_send_event(pDevice->dev, IWEVCUSTOM, &wrqu, NULL);
1307 }
1308 #endif
1309
1310 //2008-0714-01<Add>by MikeLiu
1311 device_release_WPADEV(pDevice);
1312
1313         usb_set_intfdata(intf, NULL);
1314 //2008-0922-01<Add>by MikeLiu, decrease usb counter.
1315      usb_put_dev(interface_to_usbdev(intf));
1316
1317     pDevice->flags |= DEVICE_FLAGS_UNPLUG;
1318     if (pDevice->dev != NULL) {
1319         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "unregister_netdev..\n");
1320         unregister_netdev(pDevice->dev);
1321
1322 //2008-07-21-01<Add>by MikeLiu
1323 //unregister wpadev
1324    if(wpa_set_wpadev(pDevice, 0)!=0)
1325      printk("unregister wpadev fail?\n");
1326
1327         free_netdev(pDevice->dev);
1328     }
1329
1330     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "device_disconnect3.. \n");
1331 }
1332
1333
1334
1335
1336 static int device_dma0_tx_80211(struct sk_buff *skb, struct net_device *dev) {
1337     PSDevice        pDevice=netdev_priv(dev);
1338     PBYTE           pbMPDU;
1339     UINT            cbMPDULen = 0;
1340
1341
1342     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "device_dma0_tx_80211\n");
1343     spin_lock_irq(&pDevice->lock);
1344
1345     if (pDevice->bStopTx0Pkt == TRUE) {
1346         dev_kfree_skb_irq(skb);
1347         spin_unlock_irq(&pDevice->lock);
1348         return 0;
1349     };
1350
1351
1352     cbMPDULen = skb->len;
1353     pbMPDU = skb->data;
1354
1355     vDMA0_tx_80211(pDevice, skb);
1356
1357     spin_unlock_irq(&pDevice->lock);
1358
1359     return 0;
1360
1361 }
1362
1363
1364 static int  device_xmit(struct sk_buff *skb, struct net_device *dev) {
1365     PSDevice    pDevice=netdev_priv(dev);
1366     struct net_device_stats* pStats = &pDevice->stats;
1367
1368
1369     spin_lock_irq(&pDevice->lock);
1370
1371     netif_stop_queue(pDevice->dev);
1372
1373     if (pDevice->bLinkPass == FALSE) {
1374         dev_kfree_skb_irq(skb);
1375         spin_unlock_irq(&pDevice->lock);
1376         return 0;
1377     }
1378     if (pDevice->bStopDataPkt == TRUE) {
1379         dev_kfree_skb_irq(skb);
1380         pStats->tx_dropped++;
1381         spin_unlock_irq(&pDevice->lock);
1382         return 0;
1383     }
1384
1385     if(nsDMA_tx_packet(pDevice, TYPE_AC0DMA, skb) !=0) {  //mike add:xmit fail!
1386          if (netif_queue_stopped(pDevice->dev))
1387               netif_wake_queue(pDevice->dev);
1388     }
1389
1390     spin_unlock_irq(&pDevice->lock);
1391
1392     return 0;
1393 }
1394
1395
1396
1397 static unsigned const ethernet_polynomial = 0x04c11db7U;
1398 static inline u32 ether_crc(int length, unsigned char *data)
1399 {
1400     int crc = -1;
1401
1402     while(--length >= 0) {
1403         unsigned char current_octet = *data++;
1404         int bit;
1405         for (bit = 0; bit < 8; bit++, current_octet >>= 1) {
1406             crc = (crc << 1) ^
1407                 ((crc < 0) ^ (current_octet & 1) ? ethernet_polynomial : 0);
1408         }
1409     }
1410     return crc;
1411 }
1412
1413 //find out  the start  position of str2 from str1
1414 static UCHAR *kstrstr(const UCHAR *str1,const UCHAR *str2) {
1415   int str1_len=strlen(str1);
1416   int str2_len=strlen(str2);
1417
1418   while (str1_len >= str2_len) {
1419        str1_len--;
1420       if(memcmp(str1,str2,str2_len)==0)
1421          return (UCHAR *)str1;
1422         str1++;
1423   }
1424   return NULL;
1425 }
1426
1427 static int Config_FileGetParameter(UCHAR *string, UCHAR *dest,UCHAR *source)
1428 {
1429   UCHAR buf1[100];
1430   UCHAR buf2[100];
1431   UCHAR *start_p=NULL,*end_p=NULL,*tmp_p=NULL;
1432   int ii;
1433
1434     memset(buf1,0,100);
1435     strcat(buf1, string);
1436     strcat(buf1, "=");
1437     source+=strlen(buf1);
1438
1439 //find target string start point
1440     start_p = kstrstr(source,buf1);
1441     if (start_p == NULL)
1442         return FALSE;
1443
1444 //check if current config line is marked by "#" ??
1445 for(ii=1;;ii++) {
1446   if(memcmp(start_p-ii,"\n",1)==0)
1447       break;
1448   if(memcmp(start_p-ii,"#",1)==0)
1449       return FALSE;
1450 }
1451
1452 //find target string end point
1453      end_p = kstrstr(start_p,"\n");
1454      if (end_p == NULL) {       //can't find "\n",but don't care
1455           end_p=start_p+strlen(start_p);   //no include "\n"
1456        }
1457
1458    memset(buf2,0,100);
1459    memcpy(buf2,start_p,end_p-start_p);    //get the tartget line
1460    buf2[end_p-start_p]='\0';
1461
1462    //find value
1463    start_p = kstrstr(buf2,"=");
1464    if (start_p == NULL)
1465       return FALSE;
1466    memset(buf1,0,100);
1467    strcpy(buf1,start_p+1);
1468
1469   //except space
1470   tmp_p = buf1;
1471   while(*tmp_p != 0x00) {
1472         if(*tmp_p==' ')
1473             tmp_p++;
1474          else
1475           break;
1476   }
1477
1478    memcpy(dest,tmp_p,strlen(tmp_p));
1479  return TRUE;
1480 }
1481
1482 //if read fail,return NULL,or return data pointer;
1483 static UCHAR *Config_FileOperation(PSDevice pDevice) {
1484     UCHAR    *config_path=CONFIG_PATH;
1485     UCHAR    *buffer=NULL;
1486     struct file   *filp=NULL;
1487     mm_segment_t old_fs = get_fs();
1488     //int oldfsuid=0,oldfsgid=0;
1489     int result=0;
1490
1491     set_fs (KERNEL_DS);
1492     /* Can't do this anymore, so we rely on correct filesystem permissions:
1493     //Make sure a caller can read or write power as root
1494     oldfsuid=current->fsuid;
1495     oldfsgid=current->fsgid;
1496     current->fsuid = 0;
1497     current->fsgid = 0;
1498     */
1499
1500     //open file
1501       filp = filp_open(config_path, O_RDWR, 0);
1502         if (IS_ERR(filp)) {
1503              printk("Config_FileOperation file Not exist\n");
1504              result=-1;
1505              goto error2;
1506           }
1507
1508      if(!(filp->f_op) || !(filp->f_op->read) ||!(filp->f_op->write)) {
1509            printk("file %s cann't readable or writable?\n",config_path);
1510           result = -1;
1511           goto error1;
1512         }
1513
1514     buffer = kmalloc(1024, GFP_KERNEL);
1515     if(buffer==NULL) {
1516       printk("alllocate mem for file fail?\n");
1517       result = -1;
1518       goto error1;
1519     }
1520
1521     if(filp->f_op->read(filp, buffer, 1024, &filp->f_pos)<0) {
1522      printk("read file error?\n");
1523      result = -1;
1524     }
1525
1526 error1:
1527   if(filp_close(filp,NULL))
1528        printk("Config_FileOperation:close file fail\n");
1529
1530 error2:
1531   set_fs (old_fs);
1532
1533   /*
1534   current->fsuid=oldfsuid;
1535   current->fsgid=oldfsgid;
1536   */
1537
1538 if(result!=0) {
1539     if(buffer)
1540          kfree(buffer);
1541     buffer=NULL;
1542 }
1543   return buffer;
1544 }
1545
1546 //return --->-1:fail;  >=0:successful
1547 static int Read_config_file(PSDevice pDevice) {
1548   int result=0;
1549   UCHAR      tmpbuffer[100];
1550   UCHAR *buffer=NULL;
1551
1552   //init config setting
1553  pDevice->config_file.ZoneType = -1;
1554  pDevice->config_file.eAuthenMode = -1;
1555  pDevice->config_file.eEncryptionStatus = -1;
1556
1557   buffer = Config_FileOperation(pDevice);
1558   if (buffer == NULL) {
1559      result =-1;
1560      return result;
1561   }
1562
1563 //get zonetype
1564 {
1565     memset(tmpbuffer,0,sizeof(tmpbuffer));
1566     if(Config_FileGetParameter("ZONETYPE",tmpbuffer,buffer) ==TRUE) {
1567     if(memcmp(tmpbuffer,"USA",3)==0) {
1568       pDevice->config_file.ZoneType=ZoneType_USA;
1569     }
1570     else if(memcmp(tmpbuffer,"JAPAN",5)==0) {
1571       pDevice->config_file.ZoneType=ZoneType_Japan;
1572     }
1573     else if(memcmp(tmpbuffer,"EUROPE",6)==0) {
1574      pDevice->config_file.ZoneType=ZoneType_Europe;
1575     }
1576     else {
1577       printk("Unknown Zonetype[%s]?\n",tmpbuffer);
1578    }
1579  }
1580 }
1581
1582 #if 1
1583 //get other parameter
1584   {
1585         memset(tmpbuffer,0,sizeof(tmpbuffer));
1586        if(Config_FileGetParameter("AUTHENMODE",tmpbuffer,buffer)==TRUE) {
1587          pDevice->config_file.eAuthenMode = (int) simple_strtol(tmpbuffer, NULL, 10);
1588        }
1589
1590         memset(tmpbuffer,0,sizeof(tmpbuffer));
1591        if(Config_FileGetParameter("ENCRYPTIONMODE",tmpbuffer,buffer)==TRUE) {
1592          pDevice->config_file.eEncryptionStatus= (int) simple_strtol(tmpbuffer, NULL, 10);
1593        }
1594   }
1595 #endif
1596
1597   kfree(buffer);
1598   return result;
1599 }
1600
1601 static void device_set_multi(struct net_device *dev) {
1602     PSDevice         pDevice = (PSDevice) netdev_priv(dev);
1603     PSMgmtObject     pMgmt = &(pDevice->sMgmtObj);
1604     u32              mc_filter[2];
1605     int              ii;
1606     struct dev_mc_list *mclist;
1607     BYTE             pbyData[8] = {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
1608     BYTE             byTmpMode = 0;
1609     int              rc;
1610
1611
1612         spin_lock_irq(&pDevice->lock);
1613     rc = CONTROLnsRequestIn(pDevice,
1614                             MESSAGE_TYPE_READ,
1615                             MAC_REG_RCR,
1616                             MESSAGE_REQUEST_MACREG,
1617                             1,
1618                             &byTmpMode
1619                             );
1620     if (rc == 0) pDevice->byRxMode = byTmpMode;
1621
1622     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "pDevice->byRxMode in= %x\n", pDevice->byRxMode);
1623
1624     if (dev->flags & IFF_PROMISC) {         // Set promiscuous.
1625         DBG_PRT(MSG_LEVEL_ERR,KERN_NOTICE "%s: Promiscuous mode enabled.\n", dev->name);
1626         // Unconditionally log net taps.
1627         pDevice->byRxMode |= (RCR_MULTICAST|RCR_BROADCAST|RCR_UNICAST);
1628     }
1629     else if ((netdev_mc_count(dev) > pDevice->multicast_limit) ||
1630              (dev->flags & IFF_ALLMULTI)) {
1631         CONTROLnsRequestOut(pDevice,
1632                             MESSAGE_TYPE_WRITE,
1633                             MAC_REG_MAR0,
1634                             MESSAGE_REQUEST_MACREG,
1635                             8,
1636                             pbyData
1637                             );
1638         pDevice->byRxMode |= (RCR_MULTICAST|RCR_BROADCAST);
1639     }
1640     else {
1641         memset(mc_filter, 0, sizeof(mc_filter));
1642         netdev_for_each_mc_addr(mclist, dev) {
1643             int bit_nr = ether_crc(ETH_ALEN, mclist->dmi_addr) >> 26;
1644             mc_filter[bit_nr >> 5] |= cpu_to_le32(1 << (bit_nr & 31));
1645         }
1646         for (ii = 0; ii < 4; ii++) {
1647              MACvWriteMultiAddr(pDevice, ii, *((PBYTE)&mc_filter[0] + ii));
1648              MACvWriteMultiAddr(pDevice, ii+ 4, *((PBYTE)&mc_filter[1] + ii));
1649         }
1650         pDevice->byRxMode &= ~(RCR_UNICAST);
1651         pDevice->byRxMode |= (RCR_MULTICAST|RCR_BROADCAST);
1652     }
1653
1654     if (pMgmt->eConfigMode == WMAC_CONFIG_AP) {
1655         // If AP mode, don't enable RCR_UNICAST. Since hw only compare addr1 with local mac.
1656         pDevice->byRxMode |= (RCR_MULTICAST|RCR_BROADCAST);
1657         pDevice->byRxMode &= ~(RCR_UNICAST);
1658     }
1659     ControlvWriteByte(pDevice, MESSAGE_REQUEST_MACREG, MAC_REG_RCR, pDevice->byRxMode);
1660     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "pDevice->byRxMode out= %x\n", pDevice->byRxMode);
1661         spin_unlock_irq(&pDevice->lock);
1662
1663 }
1664
1665
1666 static struct net_device_stats *device_get_stats(struct net_device *dev) {
1667     PSDevice pDevice=(PSDevice) netdev_priv(dev);
1668
1669     return &pDevice->stats;
1670 }
1671
1672
1673 static int  device_ioctl(struct net_device *dev, struct ifreq *rq, int cmd) {
1674         PSDevice                pDevice = (PSDevice)netdev_priv(dev);
1675     PSMgmtObject        pMgmt = &(pDevice->sMgmtObj);
1676     PSCmdRequest        pReq;
1677     //BOOL                bCommit = FALSE;
1678         struct iwreq *wrq = (struct iwreq *) rq;
1679         int                 rc =0;
1680
1681     if (pMgmt == NULL) {
1682         rc = -EFAULT;
1683         return rc;
1684     }
1685
1686     switch(cmd) {
1687
1688         case SIOCGIWNAME:
1689                 rc = iwctl_giwname(dev, NULL, (char *)&(wrq->u.name), NULL);
1690                 break;
1691
1692         case SIOCSIWNWID:
1693         rc = -EOPNOTSUPP;
1694                 break;
1695
1696         case SIOCGIWNWID:     //0x8b03  support
1697         #ifdef  WPA_SUPPLICANT_DRIVER_WEXT_SUPPORT
1698           rc = iwctl_giwnwid(dev, NULL, &(wrq->u.nwid), NULL);
1699         #else
1700         rc = -EOPNOTSUPP;
1701         #endif
1702                 break;
1703
1704                 // Set frequency/channel
1705         case SIOCSIWFREQ:
1706             rc = iwctl_siwfreq(dev, NULL, &(wrq->u.freq), NULL);
1707                 break;
1708
1709                 // Get frequency/channel
1710         case SIOCGIWFREQ:
1711                 rc = iwctl_giwfreq(dev, NULL, &(wrq->u.freq), NULL);
1712                 break;
1713
1714                 // Set desired network name (ESSID)
1715         case SIOCSIWESSID:
1716
1717                 {
1718                         char essid[IW_ESSID_MAX_SIZE+1];
1719                         if (wrq->u.essid.length > IW_ESSID_MAX_SIZE) {
1720                                 rc = -E2BIG;
1721                                 break;
1722                         }
1723                         if (copy_from_user(essid, wrq->u.essid.pointer,
1724                                            wrq->u.essid.length)) {
1725                                 rc = -EFAULT;
1726                                 break;
1727                         }
1728                         rc = iwctl_siwessid(dev, NULL,
1729                                             &(wrq->u.essid), essid);
1730                 }
1731                 break;
1732
1733
1734                 // Get current network name (ESSID)
1735         case SIOCGIWESSID:
1736
1737                 {
1738                         char essid[IW_ESSID_MAX_SIZE+1];
1739                         if (wrq->u.essid.pointer)
1740                                 rc = iwctl_giwessid(dev, NULL,
1741                                                     &(wrq->u.essid), essid);
1742                                 if (copy_to_user(wrq->u.essid.pointer,
1743                                                          essid,
1744                                                          wrq->u.essid.length) )
1745                                         rc = -EFAULT;
1746                 }
1747                 break;
1748
1749         case SIOCSIWAP:
1750
1751                 rc = iwctl_siwap(dev, NULL, &(wrq->u.ap_addr), NULL);
1752                 break;
1753
1754
1755                 // Get current Access Point (BSSID)
1756         case SIOCGIWAP:
1757                 rc = iwctl_giwap(dev, NULL, &(wrq->u.ap_addr), NULL);
1758                 break;
1759
1760
1761                 // Set desired station name
1762         case SIOCSIWNICKN:
1763         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCSIWNICKN \n");
1764         rc = -EOPNOTSUPP;
1765                 break;
1766
1767                 // Get current station name
1768         case SIOCGIWNICKN:
1769         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCGIWNICKN \n");
1770         rc = -EOPNOTSUPP;
1771                 break;
1772
1773                 // Set the desired bit-rate
1774         case SIOCSIWRATE:
1775                 rc = iwctl_siwrate(dev, NULL, &(wrq->u.bitrate), NULL);
1776                 break;
1777
1778         // Get the current bit-rate
1779         case SIOCGIWRATE:
1780
1781                 rc = iwctl_giwrate(dev, NULL, &(wrq->u.bitrate), NULL);
1782                 break;
1783
1784         // Set the desired RTS threshold
1785         case SIOCSIWRTS:
1786
1787                 rc = iwctl_siwrts(dev, NULL, &(wrq->u.rts), NULL);
1788                 break;
1789
1790         // Get the current RTS threshold
1791         case SIOCGIWRTS:
1792
1793                 rc = iwctl_giwrts(dev, NULL, &(wrq->u.rts), NULL);
1794                 break;
1795
1796                 // Set the desired fragmentation threshold
1797         case SIOCSIWFRAG:
1798
1799                 rc = iwctl_siwfrag(dev, NULL, &(wrq->u.frag), NULL);
1800             break;
1801
1802         // Get the current fragmentation threshold
1803         case SIOCGIWFRAG:
1804
1805                 rc = iwctl_giwfrag(dev, NULL, &(wrq->u.frag), NULL);
1806                 break;
1807
1808                 // Set mode of operation
1809         case SIOCSIWMODE:
1810         rc = iwctl_siwmode(dev, NULL, &(wrq->u.mode), NULL);
1811                 break;
1812
1813                 // Get mode of operation
1814         case SIOCGIWMODE:
1815                 rc = iwctl_giwmode(dev, NULL, &(wrq->u.mode), NULL);
1816                 break;
1817
1818                 // Set WEP keys and mode
1819         case SIOCSIWENCODE:
1820                 {
1821             char abyKey[WLAN_WEP232_KEYLEN];
1822
1823                         if (wrq->u.encoding.pointer) {
1824
1825
1826                                 if (wrq->u.encoding.length > WLAN_WEP232_KEYLEN) {
1827                                         rc = -E2BIG;
1828                                         break;
1829                                 }
1830                                 memset(abyKey, 0, WLAN_WEP232_KEYLEN);
1831                                 if (copy_from_user(abyKey,
1832                                                   wrq->u.encoding.pointer,
1833                                                   wrq->u.encoding.length)) {
1834                                         rc = -EFAULT;
1835                                         break;
1836                                 }
1837                         } else if (wrq->u.encoding.length != 0) {
1838                                 rc = -EINVAL;
1839                                 break;
1840                         }
1841                         rc = iwctl_siwencode(dev, NULL, &(wrq->u.encoding), abyKey);
1842                 }
1843                 break;
1844
1845                 // Get the WEP keys and mode
1846         case SIOCGIWENCODE:
1847
1848                 if (!capable(CAP_NET_ADMIN)) {
1849                         rc = -EPERM;
1850                         break;
1851                 }
1852                 {
1853                     char abyKey[WLAN_WEP232_KEYLEN];
1854
1855                     rc = iwctl_giwencode(dev, NULL, &(wrq->u.encoding), abyKey);
1856                     if (rc != 0) break;
1857                         if (wrq->u.encoding.pointer) {
1858                                 if (copy_to_user(wrq->u.encoding.pointer,
1859                                                         abyKey,
1860                                                         wrq->u.encoding.length))
1861                                         rc = -EFAULT;
1862                         }
1863                 }
1864                 break;
1865
1866                 // Get the current Tx-Power
1867         case SIOCGIWTXPOW:
1868         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCGIWTXPOW \n");
1869         rc = -EOPNOTSUPP;
1870                 break;
1871
1872         case SIOCSIWTXPOW:
1873         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCGIWTXPOW \n");
1874         rc = -EOPNOTSUPP;
1875                 break;
1876
1877         case SIOCSIWRETRY:
1878
1879                 rc = iwctl_siwretry(dev, NULL, &(wrq->u.retry), NULL);
1880                 break;
1881
1882         case SIOCGIWRETRY:
1883
1884                 rc = iwctl_giwretry(dev, NULL, &(wrq->u.retry), NULL);
1885                 break;
1886
1887                 // Get range of parameters
1888         case SIOCGIWRANGE:
1889
1890                 {
1891                         struct iw_range range;
1892
1893                         rc = iwctl_giwrange(dev, NULL, &(wrq->u.data), (char *) &range);
1894                         if (copy_to_user(wrq->u.data.pointer, &range, sizeof(struct iw_range)))
1895                                 rc = -EFAULT;
1896                 }
1897
1898                 break;
1899
1900         case SIOCGIWPOWER:
1901
1902                 rc = iwctl_giwpower(dev, NULL, &(wrq->u.power), NULL);
1903                 break;
1904
1905
1906         case SIOCSIWPOWER:
1907
1908                 rc = iwctl_siwpower(dev, NULL, &(wrq->u.power), NULL);
1909                 break;
1910
1911
1912         case SIOCGIWSENS:
1913
1914             rc = iwctl_giwsens(dev, NULL, &(wrq->u.sens), NULL);
1915                 break;
1916
1917         case SIOCSIWSENS:
1918         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCSIWSENS \n");
1919                 rc = -EOPNOTSUPP;
1920                 break;
1921
1922         case SIOCGIWAPLIST:
1923             {
1924             char buffer[IW_MAX_AP * (sizeof(struct sockaddr) + sizeof(struct iw_quality))];
1925
1926                     if (wrq->u.data.pointer) {
1927                         rc = iwctl_giwaplist(dev, NULL, &(wrq->u.data), buffer);
1928                         if (rc == 0) {
1929                     if (copy_to_user(wrq->u.data.pointer,
1930                                                         buffer,
1931                                                        (wrq->u.data.length * (sizeof(struct sockaddr) +  sizeof(struct iw_quality)))
1932                                         ))
1933                                     rc = -EFAULT;
1934                         }
1935             }
1936         }
1937                 break;
1938
1939
1940 #ifdef WIRELESS_SPY
1941                 // Set the spy list
1942         case SIOCSIWSPY:
1943
1944         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCSIWSPY \n");
1945                 rc = -EOPNOTSUPP;
1946                 break;
1947
1948                 // Get the spy list
1949         case SIOCGIWSPY:
1950
1951         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCSIWSPY \n");
1952                 rc = -EOPNOTSUPP;
1953                 break;
1954
1955 #endif // WIRELESS_SPY
1956
1957         case SIOCGIWPRIV:
1958         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCGIWPRIV \n");
1959                 rc = -EOPNOTSUPP;
1960 /*
1961                 if(wrq->u.data.pointer) {
1962                         wrq->u.data.length = sizeof(iwctl_private_args) / sizeof( iwctl_private_args[0]);
1963
1964                         if(copy_to_user(wrq->u.data.pointer,
1965                                         (u_char *) iwctl_private_args,
1966                                         sizeof(iwctl_private_args)))
1967                                 rc = -EFAULT;
1968                 }
1969 */
1970                 break;
1971
1972
1973 //2008-0409-07, <Add> by Einsn Liu
1974 #ifdef  WPA_SUPPLICANT_DRIVER_WEXT_SUPPORT
1975         case SIOCSIWAUTH:
1976                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCSIWAUTH \n");
1977                 rc = iwctl_siwauth(dev, NULL, &(wrq->u.param), NULL);
1978                 break;
1979
1980         case SIOCGIWAUTH:
1981                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCGIWAUTH \n");
1982                 rc = iwctl_giwauth(dev, NULL, &(wrq->u.param), NULL);
1983                 break;
1984
1985         case SIOCSIWGENIE:
1986                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCSIWGENIE \n");
1987                 rc = iwctl_siwgenie(dev, NULL, &(wrq->u.data), wrq->u.data.pointer);
1988                 break;
1989
1990         case SIOCGIWGENIE:
1991                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCGIWGENIE \n");
1992                 rc = iwctl_giwgenie(dev, NULL, &(wrq->u.data), wrq->u.data.pointer);
1993                 break;
1994
1995         case SIOCSIWENCODEEXT:
1996                 {
1997                         char extra[sizeof(struct iw_encode_ext)+MAX_KEY_LEN+1];
1998                         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCSIWENCODEEXT \n");
1999                         if(wrq->u.encoding.pointer){
2000                                 memset(extra, 0, sizeof(struct iw_encode_ext)+MAX_KEY_LEN+1);
2001                                 if(wrq->u.encoding.length > (sizeof(struct iw_encode_ext)+ MAX_KEY_LEN)){
2002                                         rc = -E2BIG;
2003                                         break;
2004                                 }
2005                                 if(copy_from_user(extra, wrq->u.encoding.pointer,wrq->u.encoding.length)){
2006                                         rc = -EFAULT;
2007                                         break;
2008                                 }
2009                         }else if(wrq->u.encoding.length != 0){
2010                                 rc = -EINVAL;
2011                                 break;
2012                         }
2013                         rc = iwctl_siwencodeext(dev, NULL, &(wrq->u.encoding), extra);
2014                 }
2015                 break;
2016
2017         case SIOCGIWENCODEEXT:
2018                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCGIWENCODEEXT \n");
2019                 rc = iwctl_giwencodeext(dev, NULL, &(wrq->u.encoding), NULL);
2020                 break;
2021
2022         case SIOCSIWMLME:
2023                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " SIOCSIWMLME \n");
2024                 rc = iwctl_siwmlme(dev, NULL, &(wrq->u.data), wrq->u.data.pointer);
2025                 break;
2026
2027 #endif // #ifdef WPA_SUPPLICANT_DRIVER_WEXT_SUPPORT
2028 //End Add -- //2008-0409-07, <Add> by Einsn Liu
2029
2030     case IOCTL_CMD_TEST:
2031
2032                 if (!(pDevice->flags & DEVICE_FLAGS_OPENED)) {
2033                     rc = -EFAULT;
2034                     break;
2035                 } else {
2036                     rc = 0;
2037                 }
2038         pReq = (PSCmdRequest)rq;
2039
2040    //20080130-01,<Remark> by Mike Liu
2041       // if(pDevice->bLinkPass==TRUE)
2042           pReq->wResult = MAGIC_CODE;         //Linking status:0x3142
2043    //20080130-02,<Remark> by Mike Liu
2044       //  else
2045       //         pReq->wResult = MAGIC_CODE+1;    //disconnect status:0x3143
2046         break;
2047
2048     case IOCTL_CMD_SET:
2049                 if (!(pDevice->flags & DEVICE_FLAGS_OPENED) &&
2050                        (((PSCmdRequest)rq)->wCmdCode !=WLAN_CMD_SET_WPA))
2051                 {
2052                     rc = -EFAULT;
2053                     break;
2054                 } else {
2055                     rc = 0;
2056                 }
2057
2058             if (test_and_set_bit( 0, (void*)&(pMgmt->uCmdBusy))) {
2059                     return -EBUSY;
2060             }
2061         rc = private_ioctl(pDevice, rq);
2062         clear_bit( 0, (void*)&(pMgmt->uCmdBusy));
2063         break;
2064
2065     case IOCTL_CMD_HOSTAPD:
2066
2067                 if (!(pDevice->flags & DEVICE_FLAGS_OPENED)) {
2068                     rc = -EFAULT;
2069                     break;
2070                 } else {
2071                     rc = 0;
2072                 }
2073
2074                 rc = vt6656_hostap_ioctl(pDevice, &wrq->u.data);
2075         break;
2076
2077     case IOCTL_CMD_WPA:
2078
2079                 if (!(pDevice->flags & DEVICE_FLAGS_OPENED)) {
2080                     rc = -EFAULT;
2081                     break;
2082                 } else {
2083                     rc = 0;
2084                 }
2085
2086                 rc = wpa_ioctl(pDevice, &wrq->u.data);
2087         break;
2088
2089         case SIOCETHTOOL:
2090         return ethtool_ioctl(dev, (void *) rq->ifr_data);
2091         // All other calls are currently unsupported
2092
2093         default:
2094                 rc = -EOPNOTSUPP;
2095         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Ioctl command not support..%x\n", cmd);
2096
2097
2098     }
2099
2100     if (pDevice->bCommit) {
2101        if (pMgmt->eConfigMode == WMAC_CONFIG_AP) {
2102            netif_stop_queue(pDevice->dev);
2103            spin_lock_irq(&pDevice->lock);
2104         bScheduleCommand((void *) pDevice, WLAN_CMD_RUN_AP, NULL);
2105            spin_unlock_irq(&pDevice->lock);
2106        }
2107        else {
2108            DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Commit the settings\n");
2109            spin_lock_irq(&pDevice->lock);
2110 //2007-1121-01<Modify>by EinsnLiu
2111             if (pDevice->bLinkPass &&
2112                   memcmp(pMgmt->abyCurrSSID,pMgmt->abyDesireSSID,WLAN_IEHDR_LEN + WLAN_SSID_MAXLEN)) {
2113                 bScheduleCommand((void *) pDevice, WLAN_CMD_DISASSOCIATE, NULL);
2114              } else {
2115            pDevice->bLinkPass = FALSE;
2116            pMgmt->eCurrState = WMAC_STATE_IDLE;
2117            memset(pMgmt->abyCurrBSSID, 0, 6);
2118                  }
2119            ControlvMaskByte(pDevice,MESSAGE_REQUEST_MACREG,MAC_REG_PAPEDELAY,LEDSTS_STS,LEDSTS_SLOW);
2120 //End Modify
2121            netif_stop_queue(pDevice->dev);
2122 #ifdef  WPA_SUPPLICANT_DRIVER_WEXT_SUPPORT
2123            pMgmt->eScanType = WMAC_SCAN_ACTIVE;
2124            if (!pDevice->bWPASuppWextEnabled)
2125 #endif
2126                 bScheduleCommand((void *) pDevice,
2127                                  WLAN_CMD_BSSID_SCAN,
2128                                  pMgmt->abyDesireSSID);
2129                 bScheduleCommand((void *) pDevice,
2130                                  WLAN_CMD_SSID,
2131                                  NULL);
2132            spin_unlock_irq(&pDevice->lock);
2133       }
2134       pDevice->bCommit = FALSE;
2135     }
2136
2137
2138     return rc;
2139 }
2140
2141
2142 static int ethtool_ioctl(struct net_device *dev, void *useraddr)
2143 {
2144         u32 ethcmd;
2145
2146         if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
2147                 return -EFAULT;
2148
2149         switch (ethcmd) {
2150         case ETHTOOL_GDRVINFO: {
2151                 struct ethtool_drvinfo info = {ETHTOOL_GDRVINFO};
2152                 strncpy(info.driver, DEVICE_NAME, sizeof(info.driver)-1);
2153                 strncpy(info.version, DEVICE_VERSION, sizeof(info.version)-1);
2154                 if (copy_to_user(useraddr, &info, sizeof(info)))
2155                         return -EFAULT;
2156                 return 0;
2157         }
2158
2159         }
2160
2161         return -EOPNOTSUPP;
2162 }
2163
2164
2165 /*------------------------------------------------------------------*/
2166
2167 MODULE_DEVICE_TABLE(usb, vt6656_table);
2168
2169 static struct usb_driver vt6656_driver = {
2170         .name =         DEVICE_NAME,
2171         .probe =        vt6656_probe,
2172         .disconnect =   vt6656_disconnect,
2173         .id_table =     vt6656_table,
2174 #ifdef CONFIG_PM
2175         .suspend = vt6656_suspend,
2176         .resume = vt6656_resume,
2177 #endif /* CONFIG_PM */
2178 };
2179
2180 static int __init vt6656_init_module(void)
2181 {
2182     printk(KERN_NOTICE DEVICE_FULL_DRV_NAM " " DEVICE_VERSION);
2183     return usb_register(&vt6656_driver);
2184 }
2185
2186 static void __exit vt6656_cleanup_module(void)
2187 {
2188         usb_deregister(&vt6656_driver);
2189 }
2190
2191 module_init(vt6656_init_module);
2192 module_exit(vt6656_cleanup_module);