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