]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/staging/vt6656/main_usb.c
663ea0cade2201862b9516a5eb82407326d1706d
[karo-tx-linux.git] / drivers / staging / vt6656 / main_usb.c
1 /*
2  * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
3  * All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * File: main_usb.c
20  *
21  * Purpose: driver entry for initial, open, close, tx and rx.
22  *
23  * Author: Lyndon Chen
24  *
25  * Date: Dec 8, 2005
26  *
27  * Functions:
28  *
29  *   vt6656_probe - module initial (insmod) driver entry
30  *   device_remove1 - module remove entry
31  *   device_open - allocate dma/descripter resource & initial mac/bbp function
32  *   device_xmit - asynchronous data tx function
33  *   device_set_multi - set mac filter
34  *   device_ioctl - ioctl entry
35  *   device_close - shutdown mac/bbp & free dma/descriptor resource
36  *   device_alloc_frag_buf - rx fragement pre-allocated function
37  *   device_free_tx_bufs - free tx buffer function
38  *   device_dma0_tx_80211- tx 802.11 frame via dma0
39  *   device_dma0_xmit- tx PS buffered frame via dma0
40  *   device_init_registers- initial MAC & BBP & RF internal registers.
41  *   device_init_rings- initial tx/rx ring buffer
42  *   device_init_defrag_cb- initial & allocate de-fragement buffer.
43  *   device_tx_srv- tx interrupt service function
44  *
45  * Revision History:
46  */
47 #undef __NO_VERSION__
48
49 #include <linux/file.h>
50 #include "device.h"
51 #include "card.h"
52 #include "baseband.h"
53 #include "mac.h"
54 #include "tether.h"
55 #include "wmgr.h"
56 #include "wctl.h"
57 #include "power.h"
58 #include "wcmd.h"
59 #include "iocmd.h"
60 #include "rxtx.h"
61 #include "bssdb.h"
62 #include "wpactl.h"
63 #include "iwctl.h"
64 #include "dpc.h"
65 #include "datarate.h"
66 #include "rf.h"
67 #include "firmware.h"
68 #include "usbpipe.h"
69 #include "channel.h"
70 #include "int.h"
71 #include "iowpa.h"
72
73 /* static int msglevel = MSG_LEVEL_DEBUG; */
74 static int          msglevel                =MSG_LEVEL_INFO;
75
76 /*
77  * define module options
78  */
79
80 /* version information */
81 #define DRIVER_AUTHOR \
82         "VIA Networking Technologies, Inc., <lyndonchen@vntek.com.tw>"
83 MODULE_AUTHOR(DRIVER_AUTHOR);
84 MODULE_LICENSE("GPL");
85 MODULE_DESCRIPTION(DEVICE_FULL_DRV_NAM);
86
87 #define DEVICE_PARAM(N,D) \
88         static int N[MAX_UINTS]=OPTION_DEFAULT;\
89         module_param_array(N, int, NULL, 0);\
90         MODULE_PARM_DESC(N, D);
91
92 #define RX_DESC_DEF0     64
93 DEVICE_PARAM(RxDescriptors0,"Number of receive usb desc buffer");
94
95 #define TX_DESC_DEF0     64
96 DEVICE_PARAM(TxDescriptors0,"Number of transmit usb desc buffer");
97
98 #define CHANNEL_DEF     6
99 DEVICE_PARAM(Channel, "Channel number");
100
101 /* PreambleType[] is the preamble length used for transmit.
102    0: indicate allows long preamble type
103    1: indicate allows short preamble type
104 */
105
106 #define PREAMBLE_TYPE_DEF     1
107
108 DEVICE_PARAM(PreambleType, "Preamble Type");
109
110 #define RTS_THRESH_DEF     2347
111 DEVICE_PARAM(RTSThreshold, "RTS threshold");
112
113 #define FRAG_THRESH_DEF     2346
114 DEVICE_PARAM(FragThreshold, "Fragmentation threshold");
115
116 #define DATA_RATE_DEF     13
117 /* datarate[] index
118    0: indicate 1 Mbps   0x02
119    1: indicate 2 Mbps   0x04
120    2: indicate 5.5 Mbps 0x0B
121    3: indicate 11 Mbps  0x16
122    4: indicate 6 Mbps   0x0c
123    5: indicate 9 Mbps   0x12
124    6: indicate 12 Mbps  0x18
125    7: indicate 18 Mbps  0x24
126    8: indicate 24 Mbps  0x30
127    9: indicate 36 Mbps  0x48
128   10: indicate 48 Mbps  0x60
129   11: indicate 54 Mbps  0x6c
130   12: indicate 72 Mbps  0x90
131   13: indicate auto rate
132 */
133
134 DEVICE_PARAM(ConnectionRate, "Connection data rate");
135
136 #define OP_MODE_DEF     0
137 DEVICE_PARAM(OPMode, "Infrastruct, adhoc, AP mode ");
138
139 /* OpMode[] is used for transmit.
140    0: indicate infrastruct mode used
141    1: indicate adhoc mode used
142    2: indicate AP mode used
143 */
144
145 /* PSMode[]
146    0: indicate disable power saving mode
147    1: indicate enable power saving mode
148 */
149
150 #define PS_MODE_DEF     0
151 DEVICE_PARAM(PSMode, "Power saving mode");
152
153 #define SHORT_RETRY_DEF     8
154 DEVICE_PARAM(ShortRetryLimit, "Short frame retry limits");
155
156 #define LONG_RETRY_DEF     4
157 DEVICE_PARAM(LongRetryLimit, "long frame retry limits");
158
159 /* BasebandType[] baseband type selected
160    0: indicate 802.11a type
161    1: indicate 802.11b type
162    2: indicate 802.11g type
163 */
164
165 #define BBP_TYPE_DEF     2
166 DEVICE_PARAM(BasebandType, "baseband type");
167
168 /* 80211hEnable[]
169    0: indicate disable 802.11h
170    1: indicate enable 802.11h
171 */
172
173 #define X80211h_MODE_DEF     0
174
175 DEVICE_PARAM(b80211hEnable, "802.11h mode");
176
177 /*
178  * Static vars definitions
179  */
180
181 static struct usb_device_id vt6656_table[] = {
182         {USB_DEVICE(VNT_USB_VENDOR_ID, VNT_USB_PRODUCT_ID)},
183         {}
184 };
185
186 /* frequency list (map channels to frequencies) */
187 /*
188 static const long frequency_list[] = {
189     2412, 2417, 2422, 2427, 2432, 2437, 2442, 2447, 2452, 2457, 2462, 2467, 2472, 2484,
190     4915, 4920, 4925, 4935, 4940, 4945, 4960, 4980,
191     5035, 5040, 5045, 5055, 5060, 5080, 5170, 5180, 5190, 5200, 5210, 5220, 5230, 5240,
192     5260, 5280, 5300, 5320, 5500, 5520, 5540, 5560, 5580, 5600, 5620, 5640, 5660, 5680,
193     5700, 5745, 5765, 5785, 5805, 5825
194         };
195
196 static const struct iw_handler_def      iwctl_handler_def;
197 */
198
199 static int vt6656_probe(struct usb_interface *intf,
200                         const struct usb_device_id *id);
201 static void vt6656_disconnect(struct usb_interface *intf);
202
203 #ifdef CONFIG_PM        /* Minimal support for suspend and resume */
204 static int vt6656_suspend(struct usb_interface *intf, pm_message_t message);
205 static int vt6656_resume(struct usb_interface *intf);
206 #endif /* CONFIG_PM */
207
208 static struct net_device_stats *device_get_stats(struct net_device *dev);
209 static int  device_open(struct net_device *dev);
210 static int  device_xmit(struct sk_buff *skb, struct net_device *dev);
211 static void device_set_multi(struct net_device *dev);
212 static int  device_close(struct net_device *dev);
213 static int  device_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
214
215 static int device_init_registers(struct vnt_private *pDevice);
216 static bool device_init_defrag_cb(struct vnt_private *pDevice);
217
218 static int  ethtool_ioctl(struct net_device *dev, struct ifreq *);
219 static void device_free_tx_bufs(struct vnt_private *pDevice);
220 static void device_free_rx_bufs(struct vnt_private *pDevice);
221 static void device_free_int_bufs(struct vnt_private *pDevice);
222 static void device_free_frag_bufs(struct vnt_private *pDevice);
223 static bool device_alloc_bufs(struct vnt_private *pDevice);
224
225 static int Read_config_file(struct vnt_private *pDevice);
226 static unsigned char *Config_FileOperation(struct vnt_private *pDevice);
227 static int Config_FileGetParameter(unsigned char *string,
228                                    unsigned char *dest,
229                                    unsigned char *source);
230
231 static void usb_device_reset(struct vnt_private *pDevice);
232
233 static void
234 device_set_options(struct vnt_private *pDevice) {
235
236     u8    abyBroadcastAddr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
237     u8    abySNAP_RFC1042[ETH_ALEN] = {0xAA, 0xAA, 0x03, 0x00, 0x00, 0x00};
238     u8 abySNAP_Bridgetunnel[ETH_ALEN] = {0xAA, 0xAA, 0x03, 0x00, 0x00, 0xF8};
239
240     memcpy(pDevice->abyBroadcastAddr, abyBroadcastAddr, ETH_ALEN);
241     memcpy(pDevice->abySNAP_RFC1042, abySNAP_RFC1042, ETH_ALEN);
242     memcpy(pDevice->abySNAP_Bridgetunnel, abySNAP_Bridgetunnel, ETH_ALEN);
243
244     pDevice->cbTD = TX_DESC_DEF0;
245     pDevice->cbRD = RX_DESC_DEF0;
246     pDevice->uChannel = CHANNEL_DEF;
247     pDevice->wRTSThreshold = RTS_THRESH_DEF;
248     pDevice->wFragmentationThreshold = FRAG_THRESH_DEF;
249     pDevice->byShortRetryLimit = SHORT_RETRY_DEF;
250     pDevice->byLongRetryLimit = LONG_RETRY_DEF;
251     pDevice->wMaxTransmitMSDULifetime = DEFAULT_MSDU_LIFETIME;
252     pDevice->byShortPreamble = PREAMBLE_TYPE_DEF;
253     pDevice->ePSMode = PS_MODE_DEF;
254     pDevice->b11hEnable = X80211h_MODE_DEF;
255     pDevice->op_mode = NL80211_IFTYPE_UNSPECIFIED;
256     pDevice->uConnectionRate = DATA_RATE_DEF;
257     if (pDevice->uConnectionRate < RATE_AUTO) pDevice->bFixRate = true;
258     pDevice->byBBType = BBP_TYPE_DEF;
259     pDevice->byPacketType = pDevice->byBBType;
260     pDevice->byAutoFBCtrl = AUTO_FB_0;
261     pDevice->byPreambleType = 0;
262     pDevice->bExistSWNetAddr = false;
263 }
264
265 /*
266  * initialization of MAC & BBP registers
267  */
268 static int device_init_registers(struct vnt_private *pDevice)
269 {
270         struct vnt_manager *pMgmt = &pDevice->vnt_mgmt;
271         struct vnt_cmd_card_init *init_cmd = &pDevice->init_command;
272         struct vnt_rsp_card_init *init_rsp = &pDevice->init_response;
273         u8 abyBroadcastAddr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
274         u8 abySNAP_RFC1042[ETH_ALEN] = {0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00};
275         u8 abySNAP_Bridgetunnel[ETH_ALEN]
276                 = {0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8};
277         u8 byAntenna;
278         int ii;
279         int ntStatus = STATUS_SUCCESS;
280         u8 byTmp;
281         u8 byCalibTXIQ = 0, byCalibTXDC = 0, byCalibRXIQ = 0;
282
283         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "---->INIbInitAdapter. [%d][%d]\n",
284                                 DEVICE_INIT_COLD, pDevice->byPacketType);
285
286         memcpy(pDevice->abyBroadcastAddr, abyBroadcastAddr, ETH_ALEN);
287         memcpy(pDevice->abySNAP_RFC1042, abySNAP_RFC1042, ETH_ALEN);
288         memcpy(pDevice->abySNAP_Bridgetunnel, abySNAP_Bridgetunnel, ETH_ALEN);
289
290         if (!vnt_check_firmware_version(pDevice)) {
291                 if (vnt_download_firmware(pDevice) == true) {
292                         if (vnt_firmware_branch_to_sram(pDevice) == false) {
293                                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO
294                                         " vnt_firmware_branch_to_sram fail\n");
295                                 return false;
296                         }
297                 } else {
298                         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO
299                                 " FIRMWAREbDownload fail\n");
300                         return false;
301                 }
302         }
303
304         if (!BBbVT3184Init(pDevice)) {
305                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" BBbVT3184Init fail\n");
306                 return false;
307         }
308
309         init_cmd->init_class = DEVICE_INIT_COLD;
310         init_cmd->exist_sw_net_addr = (u8) pDevice->bExistSWNetAddr;
311         for (ii = 0; ii < 6; ii++)
312                 init_cmd->sw_net_addr[ii] = pDevice->abyCurrentNetAddr[ii];
313         init_cmd->short_retry_limit = pDevice->byShortRetryLimit;
314         init_cmd->long_retry_limit = pDevice->byLongRetryLimit;
315
316         /* issue card_init command to device */
317         ntStatus = vnt_control_out(pDevice,
318                 MESSAGE_TYPE_CARDINIT, 0, 0,
319                 sizeof(struct vnt_cmd_card_init), (u8 *)init_cmd);
320         if (ntStatus != STATUS_SUCCESS) {
321                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" Issue Card init fail\n");
322                 return false;
323         }
324
325         ntStatus = vnt_control_in(pDevice, MESSAGE_TYPE_INIT_RSP, 0, 0,
326                 sizeof(struct vnt_rsp_card_init), (u8 *)init_rsp);
327         if (ntStatus != STATUS_SUCCESS) {
328                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO
329                         "Cardinit request in status fail!\n");
330                 return false;
331         }
332
333         /* local ID for AES functions */
334         ntStatus = vnt_control_in(pDevice, MESSAGE_TYPE_READ,
335                 MAC_REG_LOCALID, MESSAGE_REQUEST_MACREG, 1,
336                         &pDevice->byLocalID);
337         if (ntStatus != STATUS_SUCCESS)
338                 return false;
339
340         /* do MACbSoftwareReset in MACvInitialize */
341
342         pDevice->bProtectMode = false;
343         /* only used in 11g type, sync with ERP IE */
344         pDevice->bNonERPPresent = false;
345         pDevice->bBarkerPreambleMd = false;
346         if (pDevice->bFixRate) {
347                 pDevice->wCurrentRate = (u16)pDevice->uConnectionRate;
348         } else {
349                 if (pDevice->byBBType == BB_TYPE_11B)
350                         pDevice->wCurrentRate = RATE_11M;
351                 else
352                         pDevice->wCurrentRate = RATE_54M;
353         }
354
355         CHvInitChannelTable(pDevice);
356
357         pDevice->byTopOFDMBasicRate = RATE_24M;
358         pDevice->byTopCCKBasicRate = RATE_1M;
359
360         /* target to IF pin while programming to RF chip */
361         pDevice->byCurPwr = 0xFF;
362
363         pDevice->byCCKPwr = pDevice->abyEEPROM[EEP_OFS_PWR_CCK];
364         pDevice->byOFDMPwrG = pDevice->abyEEPROM[EEP_OFS_PWR_OFDMG];
365         /* load power table */
366         for (ii = 0; ii < 14; ii++) {
367                 pDevice->abyCCKPwrTbl[ii] =
368                         pDevice->abyEEPROM[ii + EEP_OFS_CCK_PWR_TBL];
369
370                 if (pDevice->abyCCKPwrTbl[ii] == 0)
371                         pDevice->abyCCKPwrTbl[ii] = pDevice->byCCKPwr;
372                 pDevice->abyOFDMPwrTbl[ii] =
373                                 pDevice->abyEEPROM[ii + EEP_OFS_OFDM_PWR_TBL];
374                 if (pDevice->abyOFDMPwrTbl[ii] == 0)
375                         pDevice->abyOFDMPwrTbl[ii] = pDevice->byOFDMPwrG;
376         }
377
378         /*
379          * original zonetype is USA, but custom zonetype is Europe,
380          * then need to recover 12, 13, 14 channels with 11 channel
381          */
382         if (((pDevice->abyEEPROM[EEP_OFS_ZONETYPE] == ZoneType_Japan) ||
383                 (pDevice->abyEEPROM[EEP_OFS_ZONETYPE] == ZoneType_Europe)) &&
384                 (pDevice->byOriginalZonetype == ZoneType_USA)) {
385                 for (ii = 11; ii < 14; ii++) {
386                         pDevice->abyCCKPwrTbl[ii] = pDevice->abyCCKPwrTbl[10];
387                         pDevice->abyOFDMPwrTbl[ii] = pDevice->abyOFDMPwrTbl[10];
388                 }
389         }
390
391         pDevice->byOFDMPwrA = 0x34; /* same as RFbMA2829SelectChannel */
392
393         /* load OFDM A power table */
394         for (ii = 0; ii < CB_MAX_CHANNEL_5G; ii++) {
395                 pDevice->abyOFDMAPwrTbl[ii] =
396                         pDevice->abyEEPROM[ii + EEP_OFS_OFDMA_PWR_TBL];
397
398                 if (pDevice->abyOFDMAPwrTbl[ii] == 0)
399                         pDevice->abyOFDMAPwrTbl[ii] = pDevice->byOFDMPwrA;
400         }
401
402         byAntenna = pDevice->abyEEPROM[EEP_OFS_ANTENNA];
403
404         if (byAntenna & EEP_ANTINV)
405                 pDevice->bTxRxAntInv = true;
406         else
407                 pDevice->bTxRxAntInv = false;
408
409         byAntenna &= (EEP_ANTENNA_AUX | EEP_ANTENNA_MAIN);
410
411         if (byAntenna == 0) /* if not set default is both */
412                 byAntenna = (EEP_ANTENNA_AUX | EEP_ANTENNA_MAIN);
413
414         if (byAntenna == (EEP_ANTENNA_AUX | EEP_ANTENNA_MAIN)) {
415                 pDevice->byAntennaCount = 2;
416                 pDevice->byTxAntennaMode = ANT_B;
417                 pDevice->dwTxAntennaSel = 1;
418                 pDevice->dwRxAntennaSel = 1;
419
420                 if (pDevice->bTxRxAntInv == true)
421                         pDevice->byRxAntennaMode = ANT_A;
422                 else
423                         pDevice->byRxAntennaMode = ANT_B;
424         } else  {
425                 pDevice->byAntennaCount = 1;
426                 pDevice->dwTxAntennaSel = 0;
427                 pDevice->dwRxAntennaSel = 0;
428
429                 if (byAntenna & EEP_ANTENNA_AUX) {
430                         pDevice->byTxAntennaMode = ANT_A;
431
432                         if (pDevice->bTxRxAntInv == true)
433                                 pDevice->byRxAntennaMode = ANT_B;
434                         else
435                                 pDevice->byRxAntennaMode = ANT_A;
436                 } else {
437                         pDevice->byTxAntennaMode = ANT_B;
438
439                 if (pDevice->bTxRxAntInv == true)
440                         pDevice->byRxAntennaMode = ANT_A;
441                 else
442                         pDevice->byRxAntennaMode = ANT_B;
443                 }
444         }
445
446         /* get Auto Fall Back type */
447         pDevice->byAutoFBCtrl = AUTO_FB_0;
448
449         /* default Auto Mode */
450         /* pDevice->NetworkType = Ndis802_11Automode; */
451         pDevice->eConfigPHYMode = PHY_TYPE_AUTO;
452         pDevice->byBBType = BB_TYPE_11G;
453
454         /* get channel range */
455         pDevice->byMinChannel = 1;
456         pDevice->byMaxChannel = CB_MAX_CHANNEL;
457
458         /* get RFType */
459         pDevice->byRFType = init_rsp->rf_type;
460
461         /* load vt3266 calibration parameters in EEPROM */
462         if (pDevice->byRFType == RF_VT3226D0) {
463                 if ((pDevice->abyEEPROM[EEP_OFS_MAJOR_VER] == 0x1) &&
464                         (pDevice->abyEEPROM[EEP_OFS_MINOR_VER] >= 0x4)) {
465
466                         byCalibTXIQ = pDevice->abyEEPROM[EEP_OFS_CALIB_TX_IQ];
467                         byCalibTXDC = pDevice->abyEEPROM[EEP_OFS_CALIB_TX_DC];
468                         byCalibRXIQ = pDevice->abyEEPROM[EEP_OFS_CALIB_RX_IQ];
469                         if (byCalibTXIQ || byCalibTXDC || byCalibRXIQ) {
470                         /* CR255, enable TX/RX IQ and DC compensation mode */
471                                 vnt_control_out_u8(pDevice,
472                                         MESSAGE_REQUEST_BBREG,
473                                         0xff,
474                                         0x03);
475                         /* CR251, TX I/Q Imbalance Calibration */
476                                 vnt_control_out_u8(pDevice,
477                                         MESSAGE_REQUEST_BBREG,
478                                         0xfb,
479                                         byCalibTXIQ);
480                         /* CR252, TX DC-Offset Calibration */
481                                 vnt_control_out_u8(pDevice,
482                                         MESSAGE_REQUEST_BBREG,
483                                         0xfC,
484                                         byCalibTXDC);
485                         /* CR253, RX I/Q Imbalance Calibration */
486                                 vnt_control_out_u8(pDevice,
487                                         MESSAGE_REQUEST_BBREG,
488                                         0xfd,
489                                         byCalibRXIQ);
490                         } else {
491                         /* CR255, turn off BB Calibration compensation */
492                                 vnt_control_out_u8(pDevice,
493                                         MESSAGE_REQUEST_BBREG,
494                                         0xff,
495                                         0x0);
496                         }
497                 }
498         }
499
500         pMgmt->eScanType = WMAC_SCAN_PASSIVE;
501         pMgmt->uCurrChannel = pDevice->uChannel;
502         pMgmt->uIBSSChannel = pDevice->uChannel;
503
504         vnt_set_channel(pDevice, pMgmt->uCurrChannel);
505
506         /* get permanent network address */
507         memcpy(pDevice->abyPermanentNetAddr, init_rsp->net_addr, 6);
508         memcpy(pDevice->abyCurrentNetAddr,
509                                 pDevice->abyPermanentNetAddr, ETH_ALEN);
510
511         /* if exist SW network address, use it */
512         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Network address = %pM\n",
513                 pDevice->abyCurrentNetAddr);
514
515         /*
516         * set BB and packet type at the same time
517         * set Short Slot Time, xIFS, and RSPINF
518         */
519         if (pDevice->byBBType == BB_TYPE_11A) {
520                 vnt_add_basic_rate(pDevice, RATE_6M);
521                 pDevice->bShortSlotTime = true;
522         } else {
523                 vnt_add_basic_rate(pDevice, RATE_1M);
524                 pDevice->bShortSlotTime = false;
525         }
526
527         BBvSetShortSlotTime(pDevice);
528         vnt_set_bss_mode(pDevice);
529
530         pDevice->byBBVGACurrent = pDevice->abyBBVGA[0];
531         pDevice->byBBVGANew = pDevice->byBBVGACurrent;
532
533         BBvSetVGAGainOffset(pDevice, pDevice->abyBBVGA[0]);
534
535         pDevice->byRadioCtl = pDevice->abyEEPROM[EEP_OFS_RADIOCTL];
536         pDevice->bHWRadioOff = false;
537
538         if ((pDevice->byRadioCtl & EEP_RADIOCTL_ENABLE) != 0) {
539                 ntStatus = vnt_control_in(pDevice, MESSAGE_TYPE_READ,
540                         MAC_REG_GPIOCTL1, MESSAGE_REQUEST_MACREG, 1, &byTmp);
541
542                 if (ntStatus != STATUS_SUCCESS)
543                         return false;
544
545                 if ((byTmp & GPIO3_DATA) == 0) {
546                         pDevice->bHWRadioOff = true;
547                         vnt_mac_reg_bits_on(pDevice, MAC_REG_GPIOCTL1,
548                                                                 GPIO3_INTMD);
549                 } else {
550                         vnt_mac_reg_bits_off(pDevice, MAC_REG_GPIOCTL1,
551                                                                 GPIO3_INTMD);
552                         pDevice->bHWRadioOff = false;
553                 }
554
555         }
556
557         vnt_mac_set_led(pDevice, LEDSTS_TMLEN, 0x38);
558
559         vnt_mac_set_led(pDevice, LEDSTS_STS, LEDSTS_SLOW);
560
561         vnt_mac_reg_bits_on(pDevice, MAC_REG_GPIOCTL0, 0x01);
562
563         if ((pDevice->bHWRadioOff == true) ||
564                                 (pDevice->bRadioControlOff == true)) {
565                 vnt_radio_power_off(pDevice);
566         } else {
567                 vnt_radio_power_on(pDevice);
568         }
569
570         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"<----INIbInitAdapter Exit\n");
571
572         return true;
573 }
574
575 static const struct net_device_ops device_netdev_ops = {
576     .ndo_open               = device_open,
577     .ndo_stop               = device_close,
578     .ndo_do_ioctl           = device_ioctl,
579     .ndo_get_stats          = device_get_stats,
580     .ndo_start_xmit         = device_xmit,
581     .ndo_set_rx_mode        = device_set_multi,
582 };
583
584 static void device_free_tx_bufs(struct vnt_private *priv)
585 {
586         struct vnt_usb_send_context *tx_context;
587         int ii;
588
589         for (ii = 0; ii < priv->cbTD; ii++) {
590                 tx_context = priv->apTD[ii];
591                 /* deallocate URBs */
592                 if (tx_context->urb) {
593                         usb_kill_urb(tx_context->urb);
594                         usb_free_urb(tx_context->urb);
595                 }
596
597                 kfree(tx_context);
598         }
599
600         return;
601 }
602
603 static void device_free_rx_bufs(struct vnt_private *priv)
604 {
605         struct vnt_rcb *rcb;
606         int ii;
607
608         for (ii = 0; ii < priv->cbRD; ii++) {
609                 rcb = priv->apRCB[ii];
610
611                 /* deallocate URBs */
612                 if (rcb->pUrb) {
613                         usb_kill_urb(rcb->pUrb);
614                         usb_free_urb(rcb->pUrb);
615                 }
616
617                 /* deallocate skb */
618                 if (rcb->skb)
619                         dev_kfree_skb(rcb->skb);
620         }
621
622         kfree(priv->pRCBMem);
623
624         return;
625 }
626
627 static void usb_device_reset(struct vnt_private *pDevice)
628 {
629  int status;
630  status = usb_reset_device(pDevice->usb);
631         if (status)
632             printk("usb_device_reset fail status=%d\n",status);
633         return ;
634 }
635
636 static void device_free_int_bufs(struct vnt_private *priv)
637 {
638         kfree(priv->int_buf.data_buf);
639
640         return;
641 }
642
643 static bool device_alloc_bufs(struct vnt_private *priv)
644 {
645         struct vnt_usb_send_context *tx_context;
646         struct vnt_rcb *rcb;
647         int ii;
648
649         for (ii = 0; ii < priv->cbTD; ii++) {
650                 tx_context = kmalloc(sizeof(struct vnt_usb_send_context),
651                                                                 GFP_KERNEL);
652                 if (tx_context == NULL) {
653                         DBG_PRT(MSG_LEVEL_ERR, KERN_ERR
654                                 "%s : allocate tx usb context failed\n",
655                                         priv->dev->name);
656                         goto free_tx;
657                 }
658
659                 priv->apTD[ii] = tx_context;
660                 tx_context->priv = priv;
661
662                 /* allocate URBs */
663                 tx_context->urb = usb_alloc_urb(0, GFP_ATOMIC);
664                 if (!tx_context->urb) {
665                         DBG_PRT(MSG_LEVEL_ERR,
666                                 KERN_ERR "alloc tx urb failed\n");
667                         goto free_tx;
668                 }
669
670                 tx_context->in_use = false;
671         }
672
673         /* allocate RCB mem */
674         priv->pRCBMem = kzalloc((sizeof(struct vnt_rcb) * priv->cbRD),
675                                                                 GFP_KERNEL);
676         if (priv->pRCBMem == NULL) {
677                 DBG_PRT(MSG_LEVEL_ERR, KERN_ERR
678                         "%s : alloc rx usb context failed\n",
679                                 priv->dev->name);
680                 goto free_tx;
681         }
682
683         priv->FirstRecvFreeList = NULL;
684         priv->LastRecvFreeList = NULL;
685         priv->FirstRecvMngList = NULL;
686         priv->LastRecvMngList = NULL;
687         priv->NumRecvFreeList = 0;
688
689         rcb = (struct vnt_rcb *)priv->pRCBMem;
690
691         for (ii = 0; ii < priv->cbRD; ii++) {
692                 priv->apRCB[ii] = rcb;
693                 rcb->pDevice = priv;
694
695                 /* allocate URBs */
696                 rcb->pUrb = usb_alloc_urb(0, GFP_ATOMIC);
697                 if (rcb->pUrb == NULL) {
698                         DBG_PRT(MSG_LEVEL_ERR, KERN_ERR
699                                 " Failed to alloc rx urb\n");
700                         goto free_rx_tx;
701                 }
702
703                 rcb->skb = dev_alloc_skb(priv->rx_buf_sz);
704                 if (rcb->skb == NULL) {
705                         DBG_PRT(MSG_LEVEL_ERR, KERN_ERR
706                                                 " Failed to alloc rx skb\n");
707                         goto free_rx_tx;
708                 }
709
710                 rcb->bBoolInUse = false;
711
712                 EnqueueRCB(priv->FirstRecvFreeList,
713                                                 priv->LastRecvFreeList, rcb);
714
715                 priv->NumRecvFreeList++;
716                 rcb++;
717         }
718
719         priv->pInterruptURB = usb_alloc_urb(0, GFP_ATOMIC);
720         if (priv->pInterruptURB == NULL) {
721                 DBG_PRT(MSG_LEVEL_ERR, KERN_ERR"Failed to alloc int urb\n");
722                 goto free_rx_tx;
723         }
724
725         priv->int_buf.data_buf = kmalloc(MAX_INTERRUPT_SIZE, GFP_KERNEL);
726         if (priv->int_buf.data_buf == NULL) {
727                 DBG_PRT(MSG_LEVEL_ERR, KERN_ERR"Failed to alloc int buf\n");
728                 usb_free_urb(priv->pInterruptURB);
729                 goto free_rx_tx;
730         }
731
732         return true;
733
734 free_rx_tx:
735         device_free_rx_bufs(priv);
736
737 free_tx:
738         device_free_tx_bufs(priv);
739
740         return false;
741 }
742
743 static bool device_init_defrag_cb(struct vnt_private *pDevice)
744 {
745         int i;
746         PSDeFragControlBlock pDeF;
747
748     /* Init the fragment ctl entries */
749     for (i = 0; i < CB_MAX_RX_FRAG; i++) {
750         pDeF = &(pDevice->sRxDFCB[i]);
751         if (!device_alloc_frag_buf(pDevice, pDeF)) {
752             DBG_PRT(MSG_LEVEL_ERR,KERN_ERR "%s: can not alloc frag bufs\n",
753                 pDevice->dev->name);
754             goto free_frag;
755         }
756     }
757     pDevice->cbDFCB = CB_MAX_RX_FRAG;
758     pDevice->cbFreeDFCB = pDevice->cbDFCB;
759     return true;
760
761 free_frag:
762     device_free_frag_bufs(pDevice);
763     return false;
764 }
765
766 static void device_free_frag_bufs(struct vnt_private *pDevice)
767 {
768         PSDeFragControlBlock pDeF;
769         int i;
770
771     for (i = 0; i < CB_MAX_RX_FRAG; i++) {
772
773         pDeF = &(pDevice->sRxDFCB[i]);
774
775         if (pDeF->skb)
776             dev_kfree_skb(pDeF->skb);
777     }
778 }
779
780 int device_alloc_frag_buf(struct vnt_private *pDevice,
781                 PSDeFragControlBlock pDeF)
782 {
783         pDeF->skb = netdev_alloc_skb(pDevice->dev, pDevice->rx_buf_sz);
784         if (!pDeF->skb)
785                 return false;
786
787         return true;
788 }
789
790 static int  device_open(struct net_device *dev)
791 {
792         struct vnt_private *pDevice = netdev_priv(dev);
793
794      pDevice->fWPA_Authened = false;
795
796     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " device_open...\n");
797
798     pDevice->rx_buf_sz = MAX_TOTAL_SIZE_WITH_ALL_HEADERS;
799
800     if (device_alloc_bufs(pDevice) == false) {
801         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " device_alloc_bufs fail... \n");
802         return -ENOMEM;
803     }
804
805     if (device_init_defrag_cb(pDevice)== false) {
806         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " Initial defragment cb fail \n");
807         goto free_rx_tx;
808     }
809
810     MP_CLEAR_FLAG(pDevice, fMP_DISCONNECTED);
811     MP_SET_FLAG(pDevice, fMP_POST_READS);
812     MP_SET_FLAG(pDevice, fMP_POST_WRITES);
813
814     /* read config file */
815     Read_config_file(pDevice);
816
817         if (device_init_registers(pDevice) == false) {
818                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " init register fail\n");
819                 goto free_all;
820         }
821
822     /* init for key management */
823     KeyvInitTable(pDevice,&pDevice->sKey);
824         memcpy(pDevice->vnt_mgmt.abyMACAddr,
825                 pDevice->abyCurrentNetAddr, ETH_ALEN);
826     memcpy(pDevice->dev->dev_addr, pDevice->abyCurrentNetAddr, ETH_ALEN);
827     pDevice->bStopTx0Pkt = false;
828     pDevice->bStopDataPkt = false;
829     pDevice->bRoaming = false;
830     pDevice->bIsRoaming = false;
831     pDevice->bEnableRoaming = false;
832
833     vMgrObjectInit(pDevice);
834
835         schedule_delayed_work(&pDevice->second_callback_work, HZ);
836
837         pDevice->int_interval = 1;  /* bInterval is set to 1 */
838     pDevice->eEncryptionStatus = Ndis802_11EncryptionDisabled;
839
840     pDevice->bIsRxWorkItemQueued = true;
841
842    pDevice->bWPADEVUp = false;
843      pDevice->bwextstep0 = false;
844      pDevice->bwextstep1 = false;
845      pDevice->bwextstep2 = false;
846      pDevice->bwextstep3 = false;
847      pDevice->bWPASuppWextEnabled = false;
848     pDevice->byReAssocCount = 0;
849
850         schedule_work(&pDevice->read_work_item);
851     INTvWorkItem(pDevice);
852
853     /* if WEP key already set by iwconfig but device not yet open */
854     if ((pDevice->bEncryptionEnable == true) && (pDevice->bTransmitKey == true)) {
855          KeybSetDefaultKey( pDevice,
856                             &(pDevice->sKey),
857                             pDevice->byKeyIndex | (1 << 31),
858                             pDevice->uKeyLength,
859                             NULL,
860                             pDevice->abyKey,
861                             KEY_CTL_WEP
862                           );
863
864          pDevice->eEncryptionStatus = Ndis802_11Encryption1Enabled;
865     }
866
867         if (pDevice->vnt_mgmt.eConfigMode == WMAC_CONFIG_AP)
868                 bScheduleCommand((void *) pDevice, WLAN_CMD_RUN_AP, NULL);
869         else
870                 bScheduleCommand((void *) pDevice, WLAN_CMD_BSSID_SCAN, NULL);
871
872     netif_stop_queue(pDevice->dev);
873     pDevice->flags |= DEVICE_FLAGS_OPENED;
874
875         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "device_open success..\n");
876         return 0;
877
878 free_all:
879     device_free_frag_bufs(pDevice);
880 free_rx_tx:
881     device_free_rx_bufs(pDevice);
882     device_free_tx_bufs(pDevice);
883     device_free_int_bufs(pDevice);
884         usb_kill_urb(pDevice->pInterruptURB);
885     usb_free_urb(pDevice->pInterruptURB);
886
887     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "device_open fail.. \n");
888     return -ENOMEM;
889 }
890
891 static int device_close(struct net_device *dev)
892 {
893         struct vnt_private *pDevice = netdev_priv(dev);
894         struct vnt_manager *pMgmt = &pDevice->vnt_mgmt;
895         u8 uu;
896
897         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "device_close1\n");
898     if (pDevice == NULL)
899         return -ENODEV;
900
901     if (pDevice->bLinkPass) {
902         bScheduleCommand((void *) pDevice, WLAN_CMD_DISASSOCIATE, NULL);
903         mdelay(30);
904     }
905
906         memset(pMgmt->abyDesireSSID, 0, WLAN_IEHDR_LEN + WLAN_SSID_MAXLEN + 1);
907         pMgmt->bShareKeyAlgorithm = false;
908         pDevice->bEncryptionEnable = false;
909         pDevice->eEncryptionStatus = Ndis802_11EncryptionDisabled;
910
911         for (uu = 0; uu < MAX_KEY_TABLE; uu++)
912                 vnt_mac_disable_keyentry(pDevice, uu);
913
914         if ((pDevice->flags & DEVICE_FLAGS_UNPLUG) == false)
915                 vnt_mac_shutdown(pDevice);
916
917     netif_stop_queue(pDevice->dev);
918     MP_SET_FLAG(pDevice, fMP_DISCONNECTED);
919     MP_CLEAR_FLAG(pDevice, fMP_POST_WRITES);
920     MP_CLEAR_FLAG(pDevice, fMP_POST_READS);
921
922         cancel_delayed_work_sync(&pDevice->run_command_work);
923         cancel_delayed_work_sync(&pDevice->second_callback_work);
924
925         cancel_work_sync(&pDevice->rx_mng_work_item);
926         cancel_work_sync(&pDevice->read_work_item);
927
928    pDevice->bRoaming = false;
929    pDevice->bIsRoaming = false;
930    pDevice->bEnableRoaming = false;
931     pDevice->bCmdRunning = false;
932     pDevice->bLinkPass = false;
933     memset(pMgmt->abyCurrBSSID, 0, 6);
934     pMgmt->eCurrState = WMAC_STATE_IDLE;
935
936         pDevice->flags &= ~DEVICE_FLAGS_OPENED;
937
938     device_free_tx_bufs(pDevice);
939     device_free_rx_bufs(pDevice);
940     device_free_int_bufs(pDevice);
941     device_free_frag_bufs(pDevice);
942
943         usb_kill_urb(pDevice->pInterruptURB);
944     usb_free_urb(pDevice->pInterruptURB);
945
946     BSSvClearNodeDBTable(pDevice, 0);
947
948     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "device_close2 \n");
949
950     return 0;
951 }
952
953 static void vnt_tx_80211(struct ieee80211_hw *hw,
954         struct ieee80211_tx_control *control, struct sk_buff *skb)
955 {
956         struct vnt_private *priv = hw->priv;
957
958         ieee80211_stop_queues(hw);
959
960         if (vnt_tx_packet(priv, skb)) {
961                 ieee80211_free_txskb(hw, skb);
962
963                 ieee80211_wake_queues(hw);
964         }
965 }
966
967 static int vnt_start(struct ieee80211_hw *hw)
968 {
969         struct vnt_private *priv = hw->priv;
970
971         priv->rx_buf_sz = MAX_TOTAL_SIZE_WITH_ALL_HEADERS;
972
973         if (device_alloc_bufs(priv) == false) {
974                 dev_dbg(&priv->usb->dev, "device_alloc_bufs fail...\n");
975                 return -ENOMEM;
976         }
977
978         MP_CLEAR_FLAG(priv, fMP_DISCONNECTED);
979         MP_SET_FLAG(priv, fMP_POST_READS);
980         MP_SET_FLAG(priv, fMP_POST_WRITES);
981
982         if (device_init_registers(priv) == false) {
983                 dev_dbg(&priv->usb->dev, " init register fail\n");
984                 goto free_all;
985         }
986
987         priv->int_interval = 1;  /* bInterval is set to 1 */
988
989         schedule_work(&priv->read_work_item);
990         INTvWorkItem(priv);
991
992         priv->flags |= DEVICE_FLAGS_OPENED;
993
994         ieee80211_wake_queues(hw);
995
996         return 0;
997
998 free_all:
999         device_free_rx_bufs(priv);
1000         device_free_tx_bufs(priv);
1001         device_free_int_bufs(priv);
1002
1003         usb_kill_urb(priv->pInterruptURB);
1004         usb_free_urb(priv->pInterruptURB);
1005
1006         return -ENOMEM;
1007 }
1008
1009 static void vnt_stop(struct ieee80211_hw *hw)
1010 {
1011         struct vnt_private *priv = hw->priv;
1012         int i;
1013
1014         if (!priv)
1015                 return;
1016
1017         for (i = 0; i < MAX_KEY_TABLE; i++)
1018                 vnt_mac_disable_keyentry(priv, i);
1019
1020         /* clear all keys */
1021         priv->key_entry_inuse = 0;
1022
1023         if ((priv->flags & DEVICE_FLAGS_UNPLUG) == false)
1024                 vnt_mac_shutdown(priv);
1025
1026         ieee80211_stop_queues(hw);
1027
1028         MP_SET_FLAG(priv, fMP_DISCONNECTED);
1029         MP_CLEAR_FLAG(priv, fMP_POST_WRITES);
1030         MP_CLEAR_FLAG(priv, fMP_POST_READS);
1031
1032         cancel_delayed_work_sync(&priv->run_command_work);
1033         cancel_work_sync(&priv->read_work_item);
1034
1035         priv->bCmdRunning = false;
1036
1037         priv->flags &= ~DEVICE_FLAGS_OPENED;
1038
1039         device_free_tx_bufs(priv);
1040         device_free_rx_bufs(priv);
1041         device_free_int_bufs(priv);
1042
1043         usb_kill_urb(priv->pInterruptURB);
1044         usb_free_urb(priv->pInterruptURB);
1045
1046         return;
1047 }
1048
1049 static int vnt_add_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1050 {
1051         struct vnt_private *priv = hw->priv;
1052
1053         priv->vif = vif;
1054
1055         switch (vif->type) {
1056         case NL80211_IFTYPE_STATION:
1057                 break;
1058         case NL80211_IFTYPE_ADHOC:
1059                 vnt_mac_reg_bits_off(priv, MAC_REG_RCR, RCR_UNICAST);
1060
1061                 vnt_mac_reg_bits_on(priv, MAC_REG_HOSTCR, HOSTCR_ADHOC);
1062
1063                 break;
1064         case NL80211_IFTYPE_AP:
1065                 vnt_mac_reg_bits_off(priv, MAC_REG_RCR, RCR_UNICAST);
1066
1067                 vnt_mac_reg_bits_on(priv, MAC_REG_HOSTCR, HOSTCR_AP);
1068
1069                 break;
1070         default:
1071                 return -EOPNOTSUPP;
1072         }
1073
1074         priv->op_mode = vif->type;
1075
1076         /* LED blink on TX */
1077         vnt_mac_set_led(priv, LEDSTS_STS, LEDSTS_INTER);
1078
1079         return 0;
1080 }
1081
1082 static void vnt_remove_interface(struct ieee80211_hw *hw,
1083                 struct ieee80211_vif *vif)
1084 {
1085         struct vnt_private *priv = hw->priv;
1086
1087         switch (vif->type) {
1088         case NL80211_IFTYPE_STATION:
1089                 break;
1090         case NL80211_IFTYPE_ADHOC:
1091                 vnt_mac_reg_bits_off(priv, MAC_REG_TCR, TCR_AUTOBCNTX);
1092                 vnt_mac_reg_bits_off(priv, MAC_REG_TFTCTL, TFTCTL_TSFCNTREN);
1093                 vnt_mac_reg_bits_off(priv, MAC_REG_HOSTCR, HOSTCR_ADHOC);
1094                 break;
1095         case NL80211_IFTYPE_AP:
1096                 vnt_mac_reg_bits_off(priv, MAC_REG_TCR, TCR_AUTOBCNTX);
1097                 vnt_mac_reg_bits_off(priv, MAC_REG_TFTCTL, TFTCTL_TSFCNTREN);
1098                 vnt_mac_reg_bits_off(priv, MAC_REG_HOSTCR, HOSTCR_AP);
1099                 break;
1100         default:
1101                 break;
1102         }
1103
1104         vnt_radio_power_off(priv);
1105
1106         priv->op_mode = NL80211_IFTYPE_UNSPECIFIED;
1107
1108         /* LED slow blink */
1109         vnt_mac_set_led(priv, LEDSTS_STS, LEDSTS_SLOW);
1110
1111         return;
1112 }
1113
1114 static int vnt_config(struct ieee80211_hw *hw, u32 changed)
1115 {
1116         struct vnt_private *priv = hw->priv;
1117         struct ieee80211_conf *conf = &hw->conf;
1118         u8 bb_type;
1119
1120         if (changed & IEEE80211_CONF_CHANGE_PS) {
1121                 if (conf->flags & IEEE80211_CONF_PS)
1122                         vnt_enable_power_saving(priv, conf->listen_interval);
1123                 else
1124                         vnt_disable_power_saving(priv);
1125         }
1126
1127         if ((changed & IEEE80211_CONF_CHANGE_CHANNEL) ||
1128                         (conf->flags & IEEE80211_CONF_OFFCHANNEL)) {
1129                 vnt_set_channel(priv, conf->chandef.chan->hw_value);
1130
1131                 if (conf->chandef.chan->band == IEEE80211_BAND_5GHZ)
1132                         bb_type = BB_TYPE_11A;
1133                 else
1134                         bb_type = BB_TYPE_11G;
1135
1136                 if (priv->byBBType != bb_type) {
1137                         priv->byBBType = bb_type;
1138
1139                         vnt_set_bss_mode(priv);
1140                 }
1141         }
1142
1143         if (changed & IEEE80211_CONF_CHANGE_POWER) {
1144                 if (priv->byBBType == BB_TYPE_11B)
1145                         priv->wCurrentRate = RATE_1M;
1146                 else
1147                         priv->wCurrentRate = RATE_54M;
1148
1149                 vnt_rf_setpower(priv, priv->wCurrentRate,
1150                                 conf->chandef.chan->hw_value);
1151         }
1152
1153         return 0;
1154 }
1155
1156 static void vnt_bss_info_changed(struct ieee80211_hw *hw,
1157                 struct ieee80211_vif *vif, struct ieee80211_bss_conf *conf,
1158                 u32 changed)
1159 {
1160         struct vnt_private *priv = hw->priv;
1161         priv->current_aid = conf->aid;
1162
1163         if (changed & BSS_CHANGED_BSSID)
1164                 vnt_mac_set_bssid_addr(priv, (u8 *)conf->bssid);
1165
1166
1167         if (changed & BSS_CHANGED_BASIC_RATES) {
1168                 priv->wBasicRate = conf->basic_rates;
1169
1170                 vnt_update_top_rates(priv);
1171
1172                 dev_dbg(&priv->usb->dev, "basic rates %x\n", conf->basic_rates);
1173         }
1174
1175         if (changed & BSS_CHANGED_ERP_PREAMBLE) {
1176                 if (conf->use_short_preamble) {
1177                         vnt_mac_enable_barker_preamble_mode(priv);
1178                         priv->byPreambleType = true;
1179                 } else {
1180                         vnt_mac_disable_barker_preamble_mode(priv);
1181                         priv->byPreambleType = false;
1182                 }
1183         }
1184
1185         if (changed & BSS_CHANGED_ERP_CTS_PROT) {
1186                 if (conf->use_cts_prot)
1187                         vnt_mac_enable_protect_mode(priv);
1188                 else
1189                         vnt_mac_disable_protect_mode(priv);
1190         }
1191
1192         if (changed & BSS_CHANGED_ERP_SLOT) {
1193                 if (conf->use_short_slot)
1194                         priv->bShortSlotTime = true;
1195                 else
1196                         priv->bShortSlotTime = false;
1197
1198                 BBvSetShortSlotTime(priv);
1199                 BBvSetVGAGainOffset(priv, priv->abyBBVGA[0]);
1200                 BBvUpdatePreEDThreshold(priv, false);
1201         }
1202
1203         if (changed & BSS_CHANGED_TXPOWER)
1204                 vnt_rf_setpower(priv, priv->wCurrentRate,
1205                                         conf->chandef.chan->hw_value);
1206
1207         if (changed & BSS_CHANGED_BEACON_ENABLED) {
1208                 dev_dbg(&priv->usb->dev,
1209                                 "Beacon enable %d\n", conf->enable_beacon);
1210
1211                 if (conf->enable_beacon) {
1212                         vnt_beacon_enable(priv, vif, conf);
1213
1214                         vnt_mac_reg_bits_on(priv, MAC_REG_TCR, TCR_AUTOBCNTX);
1215                 } else {
1216                         vnt_mac_reg_bits_off(priv, MAC_REG_TCR, TCR_AUTOBCNTX);
1217                 }
1218         }
1219 }
1220
1221 static u64 vnt_prepare_multicast(struct ieee80211_hw *hw,
1222         struct netdev_hw_addr_list *mc_list)
1223 {
1224         struct vnt_private *priv = hw->priv;
1225         struct netdev_hw_addr *ha;
1226         u64 mc_filter = 0;
1227         u32 bit_nr = 0;
1228
1229         netdev_hw_addr_list_for_each(ha, mc_list) {
1230                 bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
1231
1232                 mc_filter |= 1ULL << (bit_nr & 0x3f);
1233         }
1234
1235         priv->mc_list_count = mc_list->count;
1236
1237         return mc_filter;
1238 }
1239
1240 static void vnt_configure(struct ieee80211_hw *hw,
1241         unsigned int changed_flags, unsigned int *total_flags, u64 multicast)
1242 {
1243         struct vnt_private *priv = hw->priv;
1244         u8 rx_mode = 0;
1245         int rc;
1246
1247         *total_flags &= FIF_ALLMULTI | FIF_OTHER_BSS | FIF_PROMISC_IN_BSS |
1248                 FIF_BCN_PRBRESP_PROMISC;
1249
1250         rc = vnt_control_in(priv, MESSAGE_TYPE_READ, MAC_REG_RCR,
1251                 MESSAGE_REQUEST_MACREG, sizeof(u8), &rx_mode);
1252
1253         if (!rc)
1254                 rx_mode = RCR_MULTICAST | RCR_BROADCAST;
1255
1256         dev_dbg(&priv->usb->dev, "rx mode in = %x\n", rx_mode);
1257
1258         if (changed_flags & FIF_PROMISC_IN_BSS) {
1259                 /* unconditionally log net taps */
1260                 if (*total_flags & FIF_PROMISC_IN_BSS)
1261                         rx_mode |= RCR_UNICAST;
1262                 else
1263                         rx_mode &= ~RCR_UNICAST;
1264         }
1265
1266         if (changed_flags & FIF_ALLMULTI) {
1267                 if (*total_flags & FIF_ALLMULTI) {
1268                         if (priv->mc_list_count > 2)
1269                                 vnt_mac_set_filter(priv, ~0);
1270                         else
1271                                 vnt_mac_set_filter(priv, multicast);
1272
1273                         rx_mode |= RCR_MULTICAST | RCR_BROADCAST;
1274                 } else {
1275                         rx_mode &= ~(RCR_MULTICAST | RCR_BROADCAST);
1276                 }
1277
1278         }
1279
1280         if (changed_flags & (FIF_OTHER_BSS | FIF_BCN_PRBRESP_PROMISC)) {
1281                 if (*total_flags & (FIF_OTHER_BSS | FIF_BCN_PRBRESP_PROMISC))
1282                         rx_mode &= ~RCR_BSSID;
1283                 else
1284                         rx_mode |= RCR_BSSID;
1285         }
1286
1287         vnt_control_out_u8(priv, MESSAGE_REQUEST_MACREG, MAC_REG_RCR, rx_mode);
1288
1289         dev_dbg(&priv->usb->dev, "rx mode out= %x\n", rx_mode);
1290
1291         return;
1292 }
1293
1294 static int vnt_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
1295         struct ieee80211_vif *vif, struct ieee80211_sta *sta,
1296                 struct ieee80211_key_conf *key)
1297 {
1298         struct vnt_private *priv = hw->priv;
1299
1300         switch (cmd) {
1301         case SET_KEY:
1302                 if (vnt_set_keys(hw, sta, vif, key))
1303                         return -EOPNOTSUPP;
1304                 break;
1305         case DISABLE_KEY:
1306                 if (test_bit(key->hw_key_idx, &priv->key_entry_inuse))
1307                         clear_bit(key->hw_key_idx, &priv->key_entry_inuse);
1308         default:
1309                 break;
1310         }
1311
1312         return 0;
1313 }
1314
1315 static void vnt_sw_scan_start(struct ieee80211_hw *hw)
1316 {
1317         struct vnt_private *priv = hw->priv;
1318
1319         /* Set max sensitivity*/
1320         BBvUpdatePreEDThreshold(priv, true);
1321 }
1322
1323 static void vnt_sw_scan_complete(struct ieee80211_hw *hw)
1324 {
1325         struct vnt_private *priv = hw->priv;
1326
1327         /* Return sensitivity to channel level*/
1328         BBvUpdatePreEDThreshold(priv, false);
1329 }
1330
1331 static u64 vnt_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1332 {
1333         struct vnt_private *priv = hw->priv;
1334
1335         return priv->qwCurrTSF;
1336 }
1337
1338 static void vnt_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1339                         u64 tsf)
1340 {
1341         struct vnt_private *priv = hw->priv;
1342
1343         vnt_update_next_tbtt(priv, tsf, vif->bss_conf.beacon_int);
1344 }
1345
1346 static void vnt_reset_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1347 {
1348         struct vnt_private *priv = hw->priv;
1349
1350         vnt_mac_reg_bits_off(priv, MAC_REG_TFTCTL, TFTCTL_TSFCNTREN);
1351
1352         vnt_clear_current_tsf(priv);
1353 }
1354
1355 static const struct ieee80211_ops vnt_mac_ops = {
1356         .tx                     = vnt_tx_80211,
1357         .start                  = vnt_start,
1358         .stop                   = vnt_stop,
1359         .add_interface          = vnt_add_interface,
1360         .remove_interface       = vnt_remove_interface,
1361         .config                 = vnt_config,
1362         .bss_info_changed       = vnt_bss_info_changed,
1363         .prepare_multicast      = vnt_prepare_multicast,
1364         .configure_filter       = vnt_configure,
1365         .set_key                = vnt_set_key,
1366         .sw_scan_start          = vnt_sw_scan_start,
1367         .sw_scan_complete       = vnt_sw_scan_complete,
1368         .get_tsf                = vnt_get_tsf,
1369         .set_tsf                = vnt_set_tsf,
1370         .reset_tsf              = vnt_reset_tsf,
1371 };
1372
1373 int vnt_init(struct vnt_private *priv)
1374 {
1375
1376         if (!(device_init_registers(priv)))
1377                 return -EAGAIN;
1378
1379         SET_IEEE80211_PERM_ADDR(priv->hw, priv->abyPermanentNetAddr);
1380
1381         vnt_init_bands(priv);
1382
1383         if (ieee80211_register_hw(priv->hw))
1384                 return -ENODEV;
1385
1386         return 0;
1387 }
1388
1389 static int
1390 vt6656_probe(struct usb_interface *intf, const struct usb_device_id *id)
1391 {
1392         struct usb_device *udev;
1393         struct vnt_private *priv;
1394         struct ieee80211_hw *hw;
1395         struct wiphy *wiphy;
1396         int rc = 0;
1397
1398         udev = usb_get_dev(interface_to_usbdev(intf));
1399
1400         dev_notice(&udev->dev, "%s Ver. %s\n",
1401                                         DEVICE_FULL_DRV_NAM, DEVICE_VERSION);
1402         dev_notice(&udev->dev,
1403                 "Copyright (c) 2004 VIA Networking Technologies, Inc.\n");
1404
1405         hw = ieee80211_alloc_hw(sizeof(struct vnt_private), &vnt_mac_ops);
1406         if (!hw) {
1407                 dev_err(&udev->dev, "could not register ieee80211_hw\n");
1408                 goto err_nomem;
1409         }
1410
1411         priv = hw->priv;
1412         priv->hw = hw;
1413         priv->usb = udev;
1414
1415         device_set_options(priv);
1416
1417         spin_lock_init(&priv->lock);
1418         mutex_init(&priv->usb_lock);
1419
1420         INIT_DELAYED_WORK(&priv->run_command_work, vRunCommand);
1421         INIT_WORK(&priv->read_work_item, RXvWorkItem);
1422
1423         priv->vnt_mgmt.pAdapter = priv;
1424
1425         usb_set_intfdata(intf, priv);
1426
1427         wiphy = priv->hw->wiphy;
1428
1429         wiphy->frag_threshold = FRAG_THRESH_DEF;
1430         wiphy->rts_threshold = RTS_THRESH_DEF;
1431         wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
1432                 BIT(NL80211_IFTYPE_ADHOC) | BIT(NL80211_IFTYPE_AP);
1433
1434         priv->hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
1435                 IEEE80211_HW_REPORTS_TX_ACK_STATUS |
1436                 IEEE80211_HW_SIGNAL_DBM |
1437                 IEEE80211_HW_TIMING_BEACON_ONLY;
1438
1439         priv->hw->rate_control_algorithm = "pid";
1440         priv->hw->max_signal = 100;
1441
1442         SET_IEEE80211_DEV(priv->hw, &intf->dev);
1443
1444         usb_device_reset(priv);
1445
1446         return 0;
1447
1448 err_nomem:
1449         usb_put_dev(udev);
1450
1451         return rc;
1452 }
1453
1454 static void vt6656_disconnect(struct usb_interface *intf)
1455 {
1456         struct vnt_private *priv = usb_get_intfdata(intf);
1457
1458         if (!priv)
1459                 return;
1460
1461         ieee80211_unregister_hw(priv->hw);
1462
1463         usb_set_intfdata(intf, NULL);
1464         usb_put_dev(interface_to_usbdev(intf));
1465
1466         priv->flags |= DEVICE_FLAGS_UNPLUG;
1467
1468         ieee80211_free_hw(priv->hw);
1469 }
1470
1471 #ifdef CONFIG_PM
1472
1473 static int vt6656_suspend(struct usb_interface *intf, pm_message_t message)
1474 {
1475         return 0;
1476 }
1477
1478 static int vt6656_resume(struct usb_interface *intf)
1479 {
1480         return 0;
1481 }
1482
1483 #endif /* CONFIG_PM */
1484
1485 static int device_xmit(struct sk_buff *skb, struct net_device *dev)
1486 {
1487         struct vnt_private *pDevice = netdev_priv(dev);
1488         struct net_device_stats *stats = &pDevice->stats;
1489         unsigned long flags;
1490
1491         spin_lock_irqsave(&pDevice->lock, flags);
1492
1493         netif_stop_queue(dev);
1494
1495         if (!pDevice->bLinkPass) {
1496                 dev_kfree_skb_irq(skb);
1497                 goto out;
1498         }
1499
1500         if (pDevice->bStopDataPkt) {
1501                 dev_kfree_skb_irq(skb);
1502                 stats->tx_dropped++;
1503                 goto out;
1504         }
1505
1506         if (nsDMA_tx_packet(pDevice, skb)) {
1507                 if (netif_queue_stopped(dev))
1508                         netif_wake_queue(dev);
1509         }
1510
1511 out:
1512         spin_unlock_irqrestore(&pDevice->lock, flags);
1513
1514         return NETDEV_TX_OK;
1515 }
1516
1517 /* find out the start position of str2 from str1 */
1518 static unsigned char *kstrstr(const unsigned char *str1,
1519                               const unsigned char *str2) {
1520   int str1_len = strlen(str1);
1521   int str2_len = strlen(str2);
1522
1523   while (str1_len >= str2_len) {
1524        str1_len--;
1525       if(memcmp(str1,str2,str2_len)==0)
1526         return (unsigned char *) str1;
1527         str1++;
1528   }
1529   return NULL;
1530 }
1531
1532 static int Config_FileGetParameter(unsigned char *string,
1533                                    unsigned char *dest,
1534                                    unsigned char *source)
1535 {
1536   unsigned char buf1[100];
1537   unsigned char buf2[100];
1538   unsigned char *start_p = NULL, *end_p = NULL, *tmp_p = NULL;
1539   int ii;
1540
1541     memset(buf1,0,100);
1542     strcat(buf1, string);
1543     strcat(buf1, "=");
1544     source+=strlen(buf1);
1545
1546     /* find target string start point */
1547     start_p = kstrstr(source,buf1);
1548     if (start_p == NULL)
1549         return false;
1550
1551     /* check if current config line is marked by "#" */
1552     for (ii = 1; ; ii++) {
1553         if (memcmp(start_p - ii, "\n", 1) == 0)
1554                 break;
1555         if (memcmp(start_p - ii, "#", 1) == 0)
1556                 return false;
1557     }
1558
1559     /* find target string end point */
1560      end_p = kstrstr(start_p,"\n");
1561      if (end_p == NULL) {       /* can't find "\n", but don't care */
1562              end_p = start_p + strlen(start_p);   /* no include "\n" */
1563      }
1564
1565    memset(buf2,0,100);
1566    memcpy(buf2, start_p, end_p-start_p); /* get the target line */
1567    buf2[end_p-start_p]='\0';
1568
1569    /* find value */
1570    start_p = kstrstr(buf2,"=");
1571    if (start_p == NULL)
1572       return false;
1573    memset(buf1,0,100);
1574    strcpy(buf1,start_p+1);
1575
1576    /* except space */
1577   tmp_p = buf1;
1578   while(*tmp_p != 0x00) {
1579         if(*tmp_p==' ')
1580             tmp_p++;
1581          else
1582           break;
1583   }
1584
1585    memcpy(dest,tmp_p,strlen(tmp_p));
1586  return true;
1587 }
1588
1589 /* if read fails, return NULL, or return data pointer */
1590 static unsigned char *Config_FileOperation(struct vnt_private *pDevice)
1591 {
1592         unsigned char *buffer = kmalloc(1024, GFP_KERNEL);
1593         struct file   *file;
1594
1595         if (!buffer) {
1596                 printk("allocate mem for file fail?\n");
1597                 return NULL;
1598         }
1599
1600         file = filp_open(CONFIG_PATH, O_RDONLY, 0);
1601         if (IS_ERR(file)) {
1602                 kfree(buffer);
1603                 printk("Config_FileOperation file Not exist\n");
1604                 return NULL;
1605         }
1606
1607         if (kernel_read(file, 0, buffer, 1024) < 0) {
1608                 printk("read file error?\n");
1609                 kfree(buffer);
1610                 buffer = NULL;
1611         }
1612
1613         fput(file);
1614         return buffer;
1615 }
1616
1617 /* return --->-1:fail; >=0:successful */
1618 static int Read_config_file(struct vnt_private *pDevice)
1619 {
1620         int result = 0;
1621         unsigned char tmpbuffer[100];
1622         unsigned char *buffer = NULL;
1623
1624         /* init config setting */
1625  pDevice->config_file.ZoneType = -1;
1626  pDevice->config_file.eAuthenMode = -1;
1627  pDevice->config_file.eEncryptionStatus = -1;
1628
1629   buffer = Config_FileOperation(pDevice);
1630   if (buffer == NULL) {
1631      result =-1;
1632      return result;
1633   }
1634
1635 /* get zonetype */
1636 {
1637     memset(tmpbuffer,0,sizeof(tmpbuffer));
1638     if(Config_FileGetParameter("ZONETYPE",tmpbuffer,buffer) ==true) {
1639     if(memcmp(tmpbuffer,"USA",3)==0) {
1640       pDevice->config_file.ZoneType=ZoneType_USA;
1641     }
1642     else if(memcmp(tmpbuffer,"JAPAN",5)==0) {
1643       pDevice->config_file.ZoneType=ZoneType_Japan;
1644     }
1645     else if(memcmp(tmpbuffer,"EUROPE",6)==0) {
1646      pDevice->config_file.ZoneType=ZoneType_Europe;
1647     }
1648     else {
1649       printk("Unknown Zonetype[%s]?\n",tmpbuffer);
1650    }
1651  }
1652 }
1653
1654 /* get other parameter */
1655   {
1656         memset(tmpbuffer,0,sizeof(tmpbuffer));
1657        if(Config_FileGetParameter("AUTHENMODE",tmpbuffer,buffer)==true) {
1658          pDevice->config_file.eAuthenMode = (int) simple_strtol(tmpbuffer, NULL, 10);
1659        }
1660
1661         memset(tmpbuffer,0,sizeof(tmpbuffer));
1662        if(Config_FileGetParameter("ENCRYPTIONMODE",tmpbuffer,buffer)==true) {
1663          pDevice->config_file.eEncryptionStatus= (int) simple_strtol(tmpbuffer, NULL, 10);
1664        }
1665   }
1666
1667   kfree(buffer);
1668   return result;
1669 }
1670
1671 static void device_set_multi(struct net_device *dev)
1672 {
1673         struct vnt_private *priv = netdev_priv(dev);
1674         unsigned long flags;
1675
1676         if (priv->flags & DEVICE_FLAGS_OPENED) {
1677                 spin_lock_irqsave(&priv->lock, flags);
1678
1679                 bScheduleCommand(priv, WLAN_CMD_CONFIGURE_FILTER, NULL);
1680
1681                 spin_unlock_irqrestore(&priv->lock, flags);
1682         }
1683 }
1684
1685 void vnt_configure_filter(struct vnt_private *priv)
1686 {
1687         struct net_device *dev = priv->dev;
1688         struct vnt_manager *mgmt = &priv->vnt_mgmt;
1689         struct netdev_hw_addr *ha;
1690         u64 mc_filter = 0;
1691         u8 tmp = 0;
1692         int rc;
1693
1694         rc = vnt_control_in(priv, MESSAGE_TYPE_READ,
1695                 MAC_REG_RCR, MESSAGE_REQUEST_MACREG, 1, &tmp);
1696         if (rc == 0)
1697                 priv->byRxMode = tmp;
1698
1699         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "priv->byRxMode in= %x\n",
1700                                                         priv->byRxMode);
1701
1702         if (dev->flags & IFF_PROMISC) { /* set promiscuous mode */
1703                 DBG_PRT(MSG_LEVEL_ERR, KERN_NOTICE
1704                         "%s: Promiscuous mode enabled.\n", dev->name);
1705                 /* unconditionally log net taps */
1706                 priv->byRxMode |= (RCR_MULTICAST|RCR_BROADCAST|RCR_UNICAST);
1707         } else if ((netdev_mc_count(dev) > priv->multicast_limit) ||
1708                         (dev->flags & IFF_ALLMULTI)) {
1709                 mc_filter = ~0x0;
1710                 vnt_mac_set_filter(priv, mc_filter);
1711
1712                 priv->byRxMode |= (RCR_MULTICAST|RCR_BROADCAST);
1713         } else {
1714                 netdev_for_each_mc_addr(ha, dev) {
1715                         int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
1716
1717                         mc_filter |= 1ULL << (bit_nr & 0x3f);
1718                 }
1719
1720                 vnt_mac_set_filter(priv, mc_filter);
1721
1722                 priv->byRxMode &= ~(RCR_UNICAST);
1723                 priv->byRxMode |= (RCR_MULTICAST|RCR_BROADCAST);
1724         }
1725
1726         if (mgmt->eConfigMode == WMAC_CONFIG_AP) {
1727                 /*
1728                  * If AP mode, don't enable RCR_UNICAST since HW only compares
1729                  * addr1 with local MAC
1730                  */
1731                 priv->byRxMode |= (RCR_MULTICAST|RCR_BROADCAST);
1732                 priv->byRxMode &= ~(RCR_UNICAST);
1733         }
1734
1735         vnt_control_out_u8(priv, MESSAGE_REQUEST_MACREG,
1736                                         MAC_REG_RCR, priv->byRxMode);
1737
1738         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO
1739                                 "priv->byRxMode out= %x\n", priv->byRxMode);
1740 }
1741
1742 static struct net_device_stats *device_get_stats(struct net_device *dev)
1743 {
1744         struct vnt_private *pDevice = netdev_priv(dev);
1745
1746         return &pDevice->stats;
1747 }
1748
1749 static int device_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1750 {
1751         int rc = 0;
1752
1753         switch (cmd) {
1754         case SIOCETHTOOL:
1755                 return ethtool_ioctl(dev, rq);
1756
1757         }
1758
1759         return rc;
1760 }
1761
1762 static int ethtool_ioctl(struct net_device *dev, struct ifreq *rq)
1763 {
1764         u32 ethcmd;
1765
1766         if (copy_from_user(&ethcmd, rq->ifr_data, sizeof(ethcmd)))
1767                 return -EFAULT;
1768
1769         switch (ethcmd) {
1770         case ETHTOOL_GDRVINFO: {
1771                 struct ethtool_drvinfo info = {ETHTOOL_GDRVINFO};
1772                 strncpy(info.driver, DEVICE_NAME, sizeof(info.driver)-1);
1773                 strncpy(info.version, DEVICE_VERSION, sizeof(info.version)-1);
1774                 if (copy_to_user(rq->ifr_data, &info, sizeof(info)))
1775                         return -EFAULT;
1776                 return 0;
1777         }
1778
1779         }
1780
1781         return -EOPNOTSUPP;
1782 }
1783
1784 MODULE_DEVICE_TABLE(usb, vt6656_table);
1785
1786 static struct usb_driver vt6656_driver = {
1787         .name =         DEVICE_NAME,
1788         .probe =        vt6656_probe,
1789         .disconnect =   vt6656_disconnect,
1790         .id_table =     vt6656_table,
1791 #ifdef CONFIG_PM
1792         .suspend = vt6656_suspend,
1793         .resume = vt6656_resume,
1794 #endif /* CONFIG_PM */
1795 };
1796
1797 module_usb_driver(vt6656_driver);