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