staging: vt6656: rename CARDvUpdateNextTBTT to vnt_update_next_tbtt
[linux-2.6-block.git] / drivers / staging / vt6656 / main_usb.c
CommitLineData
92b96797
FB
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 *
26e5b65b 29 * vt6656_probe - module initial (insmod) driver entry
92b96797
FB
30 * device_remove1 - module remove entry
31 * device_open - allocate dma/descripter resource & initial mac/bbp function
a0a1f61a 32 * device_xmit - asynchronous data tx function
92b96797
FB
33 * device_set_multi - set mac filter
34 * device_ioctl - ioctl entry
a0a1f61a 35 * device_close - shutdown mac/bbp & free dma/descriptor resource
92b96797
FB
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
a0a1f61a 39 * device_dma0_xmit- tx PS buffered frame via dma0
92b96797
FB
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
7c51d177 49#include <linux/file.h>
92b96797 50#include "device.h"
92b96797 51#include "card.h"
92b96797 52#include "baseband.h"
92b96797 53#include "mac.h"
92b96797 54#include "tether.h"
92b96797 55#include "wmgr.h"
92b96797 56#include "wctl.h"
92b96797 57#include "power.h"
92b96797 58#include "wcmd.h"
92b96797 59#include "iocmd.h"
92b96797 60#include "rxtx.h"
92b96797 61#include "bssdb.h"
92b96797 62#include "wpactl.h"
92b96797 63#include "iwctl.h"
92b96797 64#include "dpc.h"
92b96797 65#include "datarate.h"
92b96797 66#include "rf.h"
92b96797 67#include "firmware.h"
62c8526d 68#include "usbpipe.h"
92b96797 69#include "channel.h"
92b96797 70#include "int.h"
92b96797 71#include "iowpa.h"
92b96797 72
ec6e0f63 73/* static int msglevel = MSG_LEVEL_DEBUG; */
92b96797
FB
74static int msglevel =MSG_LEVEL_INFO;
75
ec6e0f63
AM
76/*
77 * define module options
78 */
92b96797 79
ec6e0f63
AM
80/* version information */
81#define DRIVER_AUTHOR \
82 "VIA Networking Technologies, Inc., <lyndonchen@vntek.com.tw>"
92b96797
FB
83MODULE_AUTHOR(DRIVER_AUTHOR);
84MODULE_LICENSE("GPL");
85MODULE_DESCRIPTION(DEVICE_FULL_DRV_NAM);
86
92b96797
FB
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
92b96797
FB
92#define RX_DESC_DEF0 64
93DEVICE_PARAM(RxDescriptors0,"Number of receive usb desc buffer");
94
92b96797
FB
95#define TX_DESC_DEF0 64
96DEVICE_PARAM(TxDescriptors0,"Number of transmit usb desc buffer");
97
92b96797 98#define CHANNEL_DEF 6
92b96797
FB
99DEVICE_PARAM(Channel, "Channel number");
100
92b96797
FB
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
108DEVICE_PARAM(PreambleType, "Preamble Type");
109
92b96797 110#define RTS_THRESH_DEF 2347
92b96797
FB
111DEVICE_PARAM(RTSThreshold, "RTS threshold");
112
92b96797 113#define FRAG_THRESH_DEF 2346
92b96797
FB
114DEVICE_PARAM(FragThreshold, "Fragmentation threshold");
115
92b96797
FB
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
134DEVICE_PARAM(ConnectionRate, "Connection data rate");
135
92b96797 136#define OP_MODE_DEF 0
92b96797
FB
137DEVICE_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
92b96797
FB
145/* PSMode[]
146 0: indicate disable power saving mode
147 1: indicate enable power saving mode
148*/
149
150#define PS_MODE_DEF 0
92b96797
FB
151DEVICE_PARAM(PSMode, "Power saving mode");
152
92b96797 153#define SHORT_RETRY_DEF 8
92b96797
FB
154DEVICE_PARAM(ShortRetryLimit, "Short frame retry limits");
155
92b96797 156#define LONG_RETRY_DEF 4
92b96797
FB
157DEVICE_PARAM(LongRetryLimit, "long frame retry limits");
158
92b96797
FB
159/* BasebandType[] baseband type selected
160 0: indicate 802.11a type
161 1: indicate 802.11b type
162 2: indicate 802.11g type
163*/
92b96797 164
24b46f9e 165#define BBP_TYPE_DEF 2
92b96797
FB
166DEVICE_PARAM(BasebandType, "baseband type");
167
92b96797
FB
168/* 80211hEnable[]
169 0: indicate disable 802.11h
170 1: indicate enable 802.11h
171*/
172
173#define X80211h_MODE_DEF 0
174
175DEVICE_PARAM(b80211hEnable, "802.11h mode");
176
ec6e0f63
AM
177/*
178 * Static vars definitions
179 */
92b96797 180
4d088876 181static struct usb_device_id vt6656_table[] = {
92b96797
FB
182 {USB_DEVICE(VNT_USB_VENDOR_ID, VNT_USB_PRODUCT_ID)},
183 {}
184};
185
ec6e0f63 186/* frequency list (map channels to frequencies) */
92b96797
FB
187/*
188static 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
92b96797 196static const struct iw_handler_def iwctl_handler_def;
92b96797
FB
197*/
198
26e5b65b
AM
199static int vt6656_probe(struct usb_interface *intf,
200 const struct usb_device_id *id);
201static void vt6656_disconnect(struct usb_interface *intf);
92b96797 202
92b96797 203#ifdef CONFIG_PM /* Minimal support for suspend and resume */
26e5b65b
AM
204static int vt6656_suspend(struct usb_interface *intf, pm_message_t message);
205static int vt6656_resume(struct usb_interface *intf);
206#endif /* CONFIG_PM */
207
92b96797
FB
208static struct net_device_stats *device_get_stats(struct net_device *dev);
209static int device_open(struct net_device *dev);
210static int device_xmit(struct sk_buff *skb, struct net_device *dev);
211static void device_set_multi(struct net_device *dev);
212static int device_close(struct net_device *dev);
213static int device_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
214
302433da 215static int device_init_registers(struct vnt_private *pDevice);
dfdcc425 216static bool device_init_defrag_cb(struct vnt_private *pDevice);
92b96797 217
86a362f1 218static int ethtool_ioctl(struct net_device *dev, struct ifreq *);
dd0a774f
MP
219static void device_free_tx_bufs(struct vnt_private *pDevice);
220static void device_free_rx_bufs(struct vnt_private *pDevice);
221static void device_free_int_bufs(struct vnt_private *pDevice);
222static void device_free_frag_bufs(struct vnt_private *pDevice);
dfdcc425 223static bool device_alloc_bufs(struct vnt_private *pDevice);
dd0a774f
MP
224
225static int Read_config_file(struct vnt_private *pDevice);
226static unsigned char *Config_FileOperation(struct vnt_private *pDevice);
cc856e61
AM
227static int Config_FileGetParameter(unsigned char *string,
228 unsigned char *dest,
229 unsigned char *source);
92b96797 230
dd0a774f 231static void usb_device_reset(struct vnt_private *pDevice);
92b96797 232
92b96797 233static void
dd0a774f 234device_set_options(struct vnt_private *pDevice) {
92b96797 235
b902fbfe
AM
236 u8 abyBroadcastAddr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
237 u8 abySNAP_RFC1042[ETH_ALEN] = {0xAA, 0xAA, 0x03, 0x00, 0x00, 0x00};
9a0e756c 238 u8 abySNAP_Bridgetunnel[ETH_ALEN] = {0xAA, 0xAA, 0x03, 0x00, 0x00, 0xF8};
92b96797 239
9a0e756c
AM
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);
92b96797
FB
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;
a0ad2776 255 pDevice->op_mode = NL80211_IFTYPE_UNSPECIFIED;
92b96797 256 pDevice->uConnectionRate = DATA_RATE_DEF;
4e9b5e2b 257 if (pDevice->uConnectionRate < RATE_AUTO) pDevice->bFixRate = true;
92b96797
FB
258 pDevice->byBBType = BBP_TYPE_DEF;
259 pDevice->byPacketType = pDevice->byBBType;
260 pDevice->byAutoFBCtrl = AUTO_FB_0;
92b96797 261 pDevice->byPreambleType = 0;
e269fc2d 262 pDevice->bExistSWNetAddr = false;
92b96797
FB
263}
264
ec6e0f63
AM
265/*
266 * initialization of MAC & BBP registers
267 */
302433da 268static int device_init_registers(struct vnt_private *pDevice)
92b96797 269{
dd0a774f 270 struct vnt_manager *pMgmt = &pDevice->vnt_mgmt;
748bf69c
MP
271 struct vnt_cmd_card_init *init_cmd = &pDevice->init_command;
272 struct vnt_rsp_card_init *init_rsp = &pDevice->init_response;
dd0a774f
MP
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;
dd0a774f 279 int ntStatus = STATUS_SUCCESS;
dd0a774f
MP
280 u8 byTmp;
281 u8 byCalibTXIQ = 0, byCalibTXDC = 0, byCalibRXIQ = 0;
92b96797 282
302433da
MP
283 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "---->INIbInitAdapter. [%d][%d]\n",
284 DEVICE_INIT_COLD, pDevice->byPacketType);
285
302433da
MP
286 memcpy(pDevice->abyBroadcastAddr, abyBroadcastAddr, ETH_ALEN);
287 memcpy(pDevice->abySNAP_RFC1042, abySNAP_RFC1042, ETH_ALEN);
cbc06fb1
MP
288 memcpy(pDevice->abySNAP_Bridgetunnel, abySNAP_Bridgetunnel, ETH_ALEN);
289
290 if (!FIRMWAREbCheckVersion(pDevice)) {
291 if (FIRMWAREbDownload(pDevice) == true) {
292 if (FIRMWAREbBrach2Sram(pDevice) == false) {
293 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO
294 " FIRMWAREbBrach2Sram fail\n");
cbc06fb1
MP
295 return false;
296 }
297 } else {
298 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO
299 " FIRMWAREbDownload fail\n");
cbc06fb1
MP
300 return false;
301 }
302 }
303
304 if (!BBbVT3184Init(pDevice)) {
305 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" BBbVT3184Init fail\n");
cbc06fb1
MP
306 return false;
307 }
92b96797 308
748bf69c
MP
309 init_cmd->init_class = DEVICE_INIT_COLD;
310 init_cmd->exist_sw_net_addr = (u8) pDevice->bExistSWNetAddr;
3d47a6fb 311 for (ii = 0; ii < 6; ii++)
748bf69c
MP
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;
3d47a6fb
MP
315
316 /* issue card_init command to device */
1390b02a 317 ntStatus = vnt_control_out(pDevice,
3d47a6fb 318 MESSAGE_TYPE_CARDINIT, 0, 0,
748bf69c 319 sizeof(struct vnt_cmd_card_init), (u8 *)init_cmd);
cbc06fb1
MP
320 if (ntStatus != STATUS_SUCCESS) {
321 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" Issue Card init fail\n");
cbc06fb1
MP
322 return false;
323 }
92b96797 324
441c21c3 325 ntStatus = vnt_control_in(pDevice, MESSAGE_TYPE_INIT_RSP, 0, 0,
748bf69c 326 sizeof(struct vnt_rsp_card_init), (u8 *)init_rsp);
302433da
MP
327 if (ntStatus != STATUS_SUCCESS) {
328 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO
329 "Cardinit request in status fail!\n");
302433da
MP
330 return false;
331 }
92b96797 332
ec6e0f63 333 /* local ID for AES functions */
441c21c3 334 ntStatus = vnt_control_in(pDevice, MESSAGE_TYPE_READ,
302433da
MP
335 MAC_REG_LOCALID, MESSAGE_REQUEST_MACREG, 1,
336 &pDevice->byLocalID);
7af94000 337 if (ntStatus != STATUS_SUCCESS)
302433da 338 return false;
92b96797 339
ec6e0f63
AM
340 /* do MACbSoftwareReset in MACvInitialize */
341
e269fc2d 342 pDevice->bProtectMode = false;
ec6e0f63 343 /* only used in 11g type, sync with ERP IE */
302433da
MP
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 }
92b96797 354
302433da 355 CHvInitChannelTable(pDevice);
92b96797 356
302433da
MP
357 pDevice->byTopOFDMBasicRate = RATE_24M;
358 pDevice->byTopCCKBasicRate = RATE_1M;
2486890a 359
ec6e0f63 360 /* target to IF pin while programming to RF chip */
302433da 361 pDevice->byCurPwr = 0xFF;
92b96797 362
302433da
MP
363 pDevice->byCCKPwr = pDevice->abyEEPROM[EEP_OFS_PWR_CCK];
364 pDevice->byOFDMPwrG = pDevice->abyEEPROM[EEP_OFS_PWR_OFDMG];
ec6e0f63
AM
365 /* load power table */
366 for (ii = 0; ii < 14; ii++) {
302433da
MP
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;
be973fcd 372 pDevice->abyOFDMPwrTbl[ii] =
302433da
MP
373 pDevice->abyEEPROM[ii + EEP_OFS_OFDM_PWR_TBL];
374 if (pDevice->abyOFDMPwrTbl[ii] == 0)
375 pDevice->abyOFDMPwrTbl[ii] = pDevice->byOFDMPwrG;
376 }
92b96797 377
ec6e0f63
AM
378 /*
379 * original zonetype is USA, but custom zonetype is Europe,
380 * then need to recover 12, 13, 14 channels with 11 channel
381 */
302433da
MP
382 if (((pDevice->abyEEPROM[EEP_OFS_ZONETYPE] == ZoneType_Japan) ||
383 (pDevice->abyEEPROM[EEP_OFS_ZONETYPE] == ZoneType_Europe)) &&
384 (pDevice->byOriginalZonetype == ZoneType_USA)) {
33d33e42
AM
385 for (ii = 11; ii < 14; ii++) {
386 pDevice->abyCCKPwrTbl[ii] = pDevice->abyCCKPwrTbl[10];
387 pDevice->abyOFDMPwrTbl[ii] = pDevice->abyOFDMPwrTbl[10];
388 }
302433da 389 }
92b96797 390
302433da 391 pDevice->byOFDMPwrA = 0x34; /* same as RFbMA2829SelectChannel */
ec6e0f63 392
302433da
MP
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 }
92b96797 401
302433da 402 byAntenna = pDevice->abyEEPROM[EEP_OFS_ANTENNA];
92b96797 403
302433da
MP
404 if (byAntenna & EEP_ANTINV)
405 pDevice->bTxRxAntInv = true;
406 else
407 pDevice->bTxRxAntInv = false;
408
409 byAntenna &= (EEP_ANTENNA_AUX | EEP_ANTENNA_MAIN);
92b96797 410
ec6e0f63 411 if (byAntenna == 0) /* if not set default is both */
302433da
MP
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;
302433da 424 } else {
302433da
MP
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
ec6e0f63 446 /* get Auto Fall Back type */
cbc06fb1 447 pDevice->byAutoFBCtrl = AUTO_FB_0;
92b96797 448
ec6e0f63
AM
449 /* default Auto Mode */
450 /* pDevice->NetworkType = Ndis802_11Automode; */
cbc06fb1
MP
451 pDevice->eConfigPHYMode = PHY_TYPE_AUTO;
452 pDevice->byBBType = BB_TYPE_11G;
92b96797 453
ec6e0f63 454 /* get channel range */
cbc06fb1
MP
455 pDevice->byMinChannel = 1;
456 pDevice->byMaxChannel = CB_MAX_CHANNEL;
92b96797 457
ec6e0f63 458 /* get RFType */
748bf69c 459 pDevice->byRFType = init_rsp->rf_type;
92b96797 460
ec6e0f63 461 /* load vt3266 calibration parameters in EEPROM */
302433da
MP
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) {
ec6e0f63 470 /* CR255, enable TX/RX IQ and DC compensation mode */
285d58c4 471 vnt_control_out_u8(pDevice,
cbc06fb1
MP
472 MESSAGE_REQUEST_BBREG,
473 0xff,
474 0x03);
ec6e0f63 475 /* CR251, TX I/Q Imbalance Calibration */
285d58c4 476 vnt_control_out_u8(pDevice,
cbc06fb1
MP
477 MESSAGE_REQUEST_BBREG,
478 0xfb,
479 byCalibTXIQ);
ec6e0f63 480 /* CR252, TX DC-Offset Calibration */
285d58c4 481 vnt_control_out_u8(pDevice,
cbc06fb1
MP
482 MESSAGE_REQUEST_BBREG,
483 0xfC,
484 byCalibTXDC);
ec6e0f63 485 /* CR253, RX I/Q Imbalance Calibration */
285d58c4 486 vnt_control_out_u8(pDevice,
cbc06fb1
MP
487 MESSAGE_REQUEST_BBREG,
488 0xfd,
489 byCalibRXIQ);
302433da 490 } else {
ec6e0f63 491 /* CR255, turn off BB Calibration compensation */
285d58c4 492 vnt_control_out_u8(pDevice,
cbc06fb1
MP
493 MESSAGE_REQUEST_BBREG,
494 0xff,
495 0x0);
302433da
MP
496 }
497 }
498 }
cbc06fb1
MP
499
500 pMgmt->eScanType = WMAC_SCAN_PASSIVE;
501 pMgmt->uCurrChannel = pDevice->uChannel;
502 pMgmt->uIBSSChannel = pDevice->uChannel;
084fc8f3
MP
503
504 vnt_set_channel(pDevice, pMgmt->uCurrChannel);
92b96797 505
ec6e0f63 506 /* get permanent network address */
748bf69c 507 memcpy(pDevice->abyPermanentNetAddr, init_rsp->net_addr, 6);
9a0e756c 508 memcpy(pDevice->abyCurrentNetAddr,
cbc06fb1 509 pDevice->abyPermanentNetAddr, ETH_ALEN);
92b96797 510
ec6e0f63 511 /* if exist SW network address, use it */
d6a32aa1
AS
512 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Network address = %pM\n",
513 pDevice->abyCurrentNetAddr);
92b96797 514
cbc06fb1
MP
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) {
9a2cffd4 520 vnt_add_basic_rate(pDevice, RATE_6M);
cbc06fb1
MP
521 pDevice->bShortSlotTime = true;
522 } else {
9a2cffd4 523 vnt_add_basic_rate(pDevice, RATE_1M);
cbc06fb1
MP
524 pDevice->bShortSlotTime = false;
525 }
526
527 BBvSetShortSlotTime(pDevice);
528 CARDvSetBSSMode(pDevice);
529
9cade5ac
MP
530 pDevice->byBBVGACurrent = pDevice->abyBBVGA[0];
531 pDevice->byBBVGANew = pDevice->byBBVGACurrent;
cbc06fb1 532
9cade5ac 533 BBvSetVGAGainOffset(pDevice, pDevice->abyBBVGA[0]);
cbc06fb1
MP
534
535 pDevice->byRadioCtl = pDevice->abyEEPROM[EEP_OFS_RADIOCTL];
536 pDevice->bHWRadioOff = false;
537
538 if ((pDevice->byRadioCtl & EEP_RADIOCTL_ENABLE) != 0) {
441c21c3 539 ntStatus = vnt_control_in(pDevice, MESSAGE_TYPE_READ,
cbc06fb1
MP
540 MAC_REG_GPIOCTL1, MESSAGE_REQUEST_MACREG, 1, &byTmp);
541
7af94000 542 if (ntStatus != STATUS_SUCCESS)
cbc06fb1 543 return false;
cbc06fb1
MP
544
545 if ((byTmp & GPIO3_DATA) == 0) {
546 pDevice->bHWRadioOff = true;
547 MACvRegBitsOn(pDevice, MAC_REG_GPIOCTL1, GPIO3_INTMD);
548 } else {
549 MACvRegBitsOff(pDevice, MAC_REG_GPIOCTL1, GPIO3_INTMD);
550 pDevice->bHWRadioOff = false;
551 }
552
553 }
554
96f6975b 555 vnt_mac_set_led(pDevice, LEDSTS_TMLEN, 0x38);
cbc06fb1 556
96f6975b 557 vnt_mac_set_led(pDevice, LEDSTS_STS, LEDSTS_SLOW);
cbc06fb1
MP
558
559 MACvRegBitsOn(pDevice, MAC_REG_GPIOCTL0, 0x01);
560
561 if ((pDevice->bHWRadioOff == true) ||
562 (pDevice->bRadioControlOff == true)) {
563 CARDbRadioPowerOff(pDevice);
564 } else {
565 CARDbRadioPowerOn(pDevice);
566 }
567
cbc06fb1
MP
568 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"<----INIbInitAdapter Exit\n");
569
570 return true;
92b96797
FB
571}
572
92b96797 573#ifdef CONFIG_PM /* Minimal support for suspend and resume */
26e5b65b
AM
574
575static int vt6656_suspend(struct usb_interface *intf, pm_message_t message)
92b96797 576{
dd0a774f 577 struct vnt_private *device = usb_get_intfdata(intf);
92b96797 578
e54d9eb9
AM
579 if (!device || !device->dev)
580 return -ENODEV;
581
582 if (device->flags & DEVICE_FLAGS_OPENED)
583 device_close(device->dev);
92b96797 584
e54d9eb9 585 return 0;
92b96797
FB
586}
587
26e5b65b 588static int vt6656_resume(struct usb_interface *intf)
92b96797 589{
dd0a774f 590 struct vnt_private *device = usb_get_intfdata(intf);
e54d9eb9
AM
591
592 if (!device || !device->dev)
593 return -ENODEV;
594
e54d9eb9
AM
595 if (!(device->flags & DEVICE_FLAGS_OPENED))
596 device_open(device->dev);
597
598 return 0;
92b96797 599}
92b96797 600
26e5b65b 601#endif /* CONFIG_PM */
dd8db704
FB
602
603static const struct net_device_ops device_netdev_ops = {
604 .ndo_open = device_open,
605 .ndo_stop = device_close,
606 .ndo_do_ioctl = device_ioctl,
607 .ndo_get_stats = device_get_stats,
608 .ndo_start_xmit = device_xmit,
afc4b13d 609 .ndo_set_rx_mode = device_set_multi,
dd8db704
FB
610};
611
59cb218e 612static int
26e5b65b 613vt6656_probe(struct usb_interface *intf, const struct usb_device_id *id)
92b96797 614{
9a0e756c 615 u8 fake_mac[ETH_ALEN] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x01};
92b96797 616 struct usb_device *udev = interface_to_usbdev(intf);
7a8b0055
AM
617 int rc = 0;
618 struct net_device *netdev = NULL;
dd0a774f 619 struct vnt_private *pDevice;
92b96797 620
7a8b0055
AM
621 printk(KERN_NOTICE "%s Ver. %s\n", DEVICE_FULL_DRV_NAM, DEVICE_VERSION);
622 printk(KERN_NOTICE "Copyright (c) 2004 VIA Networking Technologies, Inc.\n");
92b96797 623
7a8b0055 624 udev = usb_get_dev(udev);
dd0a774f 625 netdev = alloc_etherdev(sizeof(struct vnt_private));
7a8b0055
AM
626 if (!netdev) {
627 printk(KERN_ERR DEVICE_NAME ": allocate net device failed\n");
e3a92cde 628 rc = -ENOMEM;
7a8b0055
AM
629 goto err_nomem;
630 }
1e28efa3 631
7a8b0055 632 pDevice = netdev_priv(netdev);
dd0a774f 633 memset(pDevice, 0, sizeof(struct vnt_private));
92b96797 634
7a8b0055
AM
635 pDevice->dev = netdev;
636 pDevice->usb = udev;
92b96797 637
7a8b0055
AM
638 device_set_options(pDevice);
639 spin_lock_init(&pDevice->lock);
c91b1869
MP
640 mutex_init(&pDevice->usb_lock);
641
94488a7e 642 INIT_DELAYED_WORK(&pDevice->run_command_work, vRunCommand);
bd9a6dba 643 INIT_DELAYED_WORK(&pDevice->second_callback_work, BSSvSecondCallBack);
81d720d3 644 INIT_WORK(&pDevice->read_work_item, RXvWorkItem);
a21fc2f5 645 INIT_WORK(&pDevice->rx_mng_work_item, RXvMngWorkItem);
92b96797 646
14c5ef57 647 pDevice->vnt_mgmt.pAdapter = (void *) pDevice;
dd8db704 648
7a8b0055
AM
649 netdev->netdev_ops = &device_netdev_ops;
650 netdev->wireless_handlers =
651 (struct iw_handler_def *) &iwctl_handler_def;
92b96797 652
7a8b0055 653 usb_set_intfdata(intf, pDevice);
92b96797 654 SET_NETDEV_DEV(netdev, &intf->dev);
7a8b0055 655 memcpy(pDevice->dev->dev_addr, fake_mac, ETH_ALEN);
2467635e
MP
656
657 usb_device_reset(pDevice);
658
7a8b0055
AM
659 rc = register_netdev(netdev);
660 if (rc) {
661 printk(KERN_ERR DEVICE_NAME " Failed to register netdev\n");
e3a92cde 662 goto err_netdev;
7a8b0055 663 }
92b96797 664
92b96797 665 return 0;
92b96797 666
e3a92cde
DC
667err_netdev:
668 free_netdev(netdev);
92b96797 669err_nomem:
7a8b0055 670 usb_put_dev(udev);
92b96797 671
e3a92cde 672 return rc;
92b96797
FB
673}
674
f2625c24 675static void device_free_tx_bufs(struct vnt_private *priv)
8611a29a 676{
f2625c24
MP
677 struct vnt_usb_send_context *tx_context;
678 int ii;
679
680 for (ii = 0; ii < priv->cbTD; ii++) {
681 tx_context = priv->apTD[ii];
682 /* deallocate URBs */
30a05b39
MP
683 if (tx_context->urb) {
684 usb_kill_urb(tx_context->urb);
685 usb_free_urb(tx_context->urb);
f2625c24
MP
686 }
687
688 kfree(tx_context);
689 }
690
691 return;
92b96797
FB
692}
693
afc5eeb8 694static void device_free_rx_bufs(struct vnt_private *priv)
8611a29a 695{
afc5eeb8 696 struct vnt_rcb *rcb;
115cac2e 697 int ii;
92b96797 698
afc5eeb8
MP
699 for (ii = 0; ii < priv->cbRD; ii++) {
700 rcb = priv->apRCB[ii];
92b96797 701
afc5eeb8
MP
702 /* deallocate URBs */
703 if (rcb->pUrb) {
704 usb_kill_urb(rcb->pUrb);
705 usb_free_urb(rcb->pUrb);
706 }
92b96797 707
afc5eeb8
MP
708 /* deallocate skb */
709 if (rcb->skb)
710 dev_kfree_skb(rcb->skb);
711 }
712
713 kfree(priv->pRCBMem);
714
715 return;
92b96797
FB
716}
717
dd0a774f 718static void usb_device_reset(struct vnt_private *pDevice)
92b96797
FB
719{
720 int status;
721 status = usb_reset_device(pDevice->usb);
722 if (status)
723 printk("usb_device_reset fail status=%d\n",status);
724 return ;
725}
92b96797 726
1506bf37 727static void device_free_int_bufs(struct vnt_private *priv)
8611a29a 728{
1506bf37
MP
729 kfree(priv->int_buf.data_buf);
730
f764e00d 731 return;
92b96797
FB
732}
733
35491de7 734static bool device_alloc_bufs(struct vnt_private *priv)
dd0a774f 735{
35491de7
MP
736 struct vnt_usb_send_context *tx_context;
737 struct vnt_rcb *rcb;
115cac2e 738 int ii;
92b96797 739
35491de7
MP
740 for (ii = 0; ii < priv->cbTD; ii++) {
741 tx_context = kmalloc(sizeof(struct vnt_usb_send_context),
742 GFP_KERNEL);
743 if (tx_context == NULL) {
744 DBG_PRT(MSG_LEVEL_ERR, KERN_ERR
745 "%s : allocate tx usb context failed\n",
746 priv->dev->name);
747 goto free_tx;
748 }
92b96797 749
35491de7 750 priv->apTD[ii] = tx_context;
30a05b39 751 tx_context->priv = priv;
35491de7
MP
752
753 /* allocate URBs */
30a05b39
MP
754 tx_context->urb = usb_alloc_urb(0, GFP_ATOMIC);
755 if (!tx_context->urb) {
35491de7
MP
756 DBG_PRT(MSG_LEVEL_ERR,
757 KERN_ERR "alloc tx urb failed\n");
758 goto free_tx;
759 }
92b96797 760
30a05b39 761 tx_context->in_use = false;
35491de7
MP
762 }
763
764 /* allocate RCB mem */
765 priv->pRCBMem = kzalloc((sizeof(struct vnt_rcb) * priv->cbRD),
115cac2e 766 GFP_KERNEL);
35491de7
MP
767 if (priv->pRCBMem == NULL) {
768 DBG_PRT(MSG_LEVEL_ERR, KERN_ERR
769 "%s : alloc rx usb context failed\n",
770 priv->dev->name);
771 goto free_tx;
772 }
92b96797 773
35491de7
MP
774 priv->FirstRecvFreeList = NULL;
775 priv->LastRecvFreeList = NULL;
776 priv->FirstRecvMngList = NULL;
777 priv->LastRecvMngList = NULL;
778 priv->NumRecvFreeList = 0;
115cac2e 779
35491de7 780 rcb = (struct vnt_rcb *)priv->pRCBMem;
92b96797 781
35491de7
MP
782 for (ii = 0; ii < priv->cbRD; ii++) {
783 priv->apRCB[ii] = rcb;
784 rcb->pDevice = priv;
92b96797 785
35491de7
MP
786 /* allocate URBs */
787 rcb->pUrb = usb_alloc_urb(0, GFP_ATOMIC);
788 if (rcb->pUrb == NULL) {
789 DBG_PRT(MSG_LEVEL_ERR, KERN_ERR
790 " Failed to alloc rx urb\n");
791 goto free_rx_tx;
792 }
92b96797 793
35491de7
MP
794 rcb->skb = netdev_alloc_skb(priv->dev, priv->rx_buf_sz);
795 if (rcb->skb == NULL) {
796 DBG_PRT(MSG_LEVEL_ERR, KERN_ERR
797 " Failed to alloc rx skb\n");
798 goto free_rx_tx;
799 }
92b96797 800
35491de7
MP
801 rcb->bBoolInUse = false;
802
803 EnqueueRCB(priv->FirstRecvFreeList,
804 priv->LastRecvFreeList, rcb);
805
806 priv->NumRecvFreeList++;
807 rcb++;
92b96797
FB
808 }
809
35491de7
MP
810 priv->pInterruptURB = usb_alloc_urb(0, GFP_ATOMIC);
811 if (priv->pInterruptURB == NULL) {
812 DBG_PRT(MSG_LEVEL_ERR, KERN_ERR"Failed to alloc int urb\n");
813 goto free_rx_tx;
92b96797
FB
814 }
815
35491de7
MP
816 priv->int_buf.data_buf = kmalloc(MAX_INTERRUPT_SIZE, GFP_KERNEL);
817 if (priv->int_buf.data_buf == NULL) {
818 DBG_PRT(MSG_LEVEL_ERR, KERN_ERR"Failed to alloc int buf\n");
819 usb_free_urb(priv->pInterruptURB);
820 goto free_rx_tx;
821 }
822
823 return true;
92b96797
FB
824
825free_rx_tx:
35491de7 826 device_free_rx_bufs(priv);
92b96797
FB
827
828free_tx:
35491de7 829 device_free_tx_bufs(priv);
92b96797 830
e269fc2d 831 return false;
92b96797
FB
832}
833
dfdcc425 834static bool device_init_defrag_cb(struct vnt_private *pDevice)
dd0a774f
MP
835{
836 int i;
837 PSDeFragControlBlock pDeF;
92b96797
FB
838
839 /* Init the fragment ctl entries */
840 for (i = 0; i < CB_MAX_RX_FRAG; i++) {
841 pDeF = &(pDevice->sRxDFCB[i]);
842 if (!device_alloc_frag_buf(pDevice, pDeF)) {
843 DBG_PRT(MSG_LEVEL_ERR,KERN_ERR "%s: can not alloc frag bufs\n",
844 pDevice->dev->name);
845 goto free_frag;
9fc86028 846 }
92b96797
FB
847 }
848 pDevice->cbDFCB = CB_MAX_RX_FRAG;
849 pDevice->cbFreeDFCB = pDevice->cbDFCB;
4e9b5e2b 850 return true;
92b96797
FB
851
852free_frag:
853 device_free_frag_bufs(pDevice);
e269fc2d 854 return false;
92b96797
FB
855}
856
dd0a774f
MP
857static void device_free_frag_bufs(struct vnt_private *pDevice)
858{
859 PSDeFragControlBlock pDeF;
860 int i;
92b96797
FB
861
862 for (i = 0; i < CB_MAX_RX_FRAG; i++) {
863
864 pDeF = &(pDevice->sRxDFCB[i]);
865
866 if (pDeF->skb)
867 dev_kfree_skb(pDeF->skb);
868 }
869}
870
dd0a774f
MP
871int device_alloc_frag_buf(struct vnt_private *pDevice,
872 PSDeFragControlBlock pDeF)
873{
29263661
MP
874 pDeF->skb = netdev_alloc_skb(pDevice->dev, pDevice->rx_buf_sz);
875 if (!pDeF->skb)
876 return false;
92b96797 877
29263661 878 return true;
92b96797
FB
879}
880
dd0a774f
MP
881static int device_open(struct net_device *dev)
882{
883 struct vnt_private *pDevice = netdev_priv(dev);
92b96797 884
e269fc2d 885 pDevice->fWPA_Authened = false;
92b96797
FB
886
887 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " device_open...\n");
888
92b96797
FB
889 pDevice->rx_buf_sz = MAX_TOTAL_SIZE_WITH_ALL_HEADERS;
890
e269fc2d 891 if (device_alloc_bufs(pDevice) == false) {
92b96797
FB
892 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " device_alloc_bufs fail... \n");
893 return -ENOMEM;
894 }
895
e269fc2d 896 if (device_init_defrag_cb(pDevice)== false) {
a0a1f61a 897 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " Initial defragment cb fail \n");
92b96797
FB
898 goto free_rx_tx;
899 }
900
901 MP_CLEAR_FLAG(pDevice, fMP_DISCONNECTED);
92b96797
FB
902 MP_SET_FLAG(pDevice, fMP_POST_READS);
903 MP_SET_FLAG(pDevice, fMP_POST_WRITES);
904
ec6e0f63 905 /* read config file */
92b96797
FB
906 Read_config_file(pDevice);
907
302433da
MP
908 if (device_init_registers(pDevice) == false) {
909 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " init register fail\n");
910 goto free_all;
911 }
92b96797 912
ec6e0f63 913 /* init for key management */
92b96797 914 KeyvInitTable(pDevice,&pDevice->sKey);
14c5ef57
MP
915 memcpy(pDevice->vnt_mgmt.abyMACAddr,
916 pDevice->abyCurrentNetAddr, ETH_ALEN);
9a0e756c 917 memcpy(pDevice->dev->dev_addr, pDevice->abyCurrentNetAddr, ETH_ALEN);
e269fc2d
AM
918 pDevice->bStopTx0Pkt = false;
919 pDevice->bStopDataPkt = false;
920 pDevice->bRoaming = false;
921 pDevice->bIsRoaming = false;
922 pDevice->bEnableRoaming = false;
92b96797
FB
923
924 vMgrObjectInit(pDevice);
a21fc2f5 925
bd9a6dba
MP
926 schedule_delayed_work(&pDevice->second_callback_work, HZ);
927
6ae46633 928 pDevice->int_interval = 1; /* bInterval is set to 1 */
92b96797
FB
929 pDevice->eEncryptionStatus = Ndis802_11EncryptionDisabled;
930
4e9b5e2b 931 pDevice->bIsRxWorkItemQueued = true;
e269fc2d
AM
932
933 pDevice->bWPADEVUp = false;
934 pDevice->bwextstep0 = false;
935 pDevice->bwextstep1 = false;
936 pDevice->bwextstep2 = false;
937 pDevice->bwextstep3 = false;
938 pDevice->bWPASuppWextEnabled = false;
92b96797
FB
939 pDevice->byReAssocCount = 0;
940
81d720d3 941 schedule_work(&pDevice->read_work_item);
92b96797
FB
942 INTvWorkItem(pDevice);
943
ec6e0f63 944 /* if WEP key already set by iwconfig but device not yet open */
4e9b5e2b 945 if ((pDevice->bEncryptionEnable == true) && (pDevice->bTransmitKey == true)) {
92b96797
FB
946 KeybSetDefaultKey( pDevice,
947 &(pDevice->sKey),
948 pDevice->byKeyIndex | (1 << 31),
949 pDevice->uKeyLength,
950 NULL,
951 pDevice->abyKey,
952 KEY_CTL_WEP
953 );
7af94000 954
92b96797
FB
955 pDevice->eEncryptionStatus = Ndis802_11Encryption1Enabled;
956 }
957
14c5ef57 958 if (pDevice->vnt_mgmt.eConfigMode == WMAC_CONFIG_AP)
0cbd8d98 959 bScheduleCommand((void *) pDevice, WLAN_CMD_RUN_AP, NULL);
14c5ef57
MP
960 else
961 bScheduleCommand((void *) pDevice, WLAN_CMD_BSSID_SCAN, NULL);
92b96797 962
92b96797
FB
963 netif_stop_queue(pDevice->dev);
964 pDevice->flags |= DEVICE_FLAGS_OPENED;
965
179f823d
MP
966 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "device_open success..\n");
967 return 0;
92b96797
FB
968
969free_all:
970 device_free_frag_bufs(pDevice);
971free_rx_tx:
972 device_free_rx_bufs(pDevice);
973 device_free_tx_bufs(pDevice);
974 device_free_int_bufs(pDevice);
dad72fed 975 usb_kill_urb(pDevice->pInterruptURB);
92b96797
FB
976 usb_free_urb(pDevice->pInterruptURB);
977
978 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "device_open fail.. \n");
979 return -ENOMEM;
980}
981
dd0a774f
MP
982static int device_close(struct net_device *dev)
983{
984 struct vnt_private *pDevice = netdev_priv(dev);
985 struct vnt_manager *pMgmt = &pDevice->vnt_mgmt;
9190c4d2 986 u8 uu;
92b96797 987
dd0a774f 988 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "device_close1\n");
92b96797
FB
989 if (pDevice == NULL)
990 return -ENODEV;
991
92b96797 992 if (pDevice->bLinkPass) {
0cbd8d98 993 bScheduleCommand((void *) pDevice, WLAN_CMD_DISASSOCIATE, NULL);
92b96797
FB
994 mdelay(30);
995 }
92b96797 996
92b96797 997 memset(pMgmt->abyDesireSSID, 0, WLAN_IEHDR_LEN + WLAN_SSID_MAXLEN + 1);
e269fc2d
AM
998 pMgmt->bShareKeyAlgorithm = false;
999 pDevice->bEncryptionEnable = false;
92b96797 1000 pDevice->eEncryptionStatus = Ndis802_11EncryptionDisabled;
7af94000 1001
33d33e42 1002 for (uu = 0; uu < MAX_KEY_TABLE; uu++)
92b96797 1003 MACvDisableKeyEntry(pDevice,uu);
92b96797 1004
e269fc2d 1005 if ((pDevice->flags & DEVICE_FLAGS_UNPLUG) == false) {
92b96797
FB
1006 MACbShutdown(pDevice);
1007 }
1008 netif_stop_queue(pDevice->dev);
1009 MP_SET_FLAG(pDevice, fMP_DISCONNECTED);
1010 MP_CLEAR_FLAG(pDevice, fMP_POST_WRITES);
1011 MP_CLEAR_FLAG(pDevice, fMP_POST_READS);
94488a7e
MP
1012
1013 cancel_delayed_work_sync(&pDevice->run_command_work);
bd9a6dba 1014 cancel_delayed_work_sync(&pDevice->second_callback_work);
92b96797 1015
a21fc2f5 1016 cancel_work_sync(&pDevice->rx_mng_work_item);
81d720d3
MP
1017 cancel_work_sync(&pDevice->read_work_item);
1018
e269fc2d
AM
1019 pDevice->bRoaming = false;
1020 pDevice->bIsRoaming = false;
1021 pDevice->bEnableRoaming = false;
1022 pDevice->bCmdRunning = false;
1023 pDevice->bLinkPass = false;
92b96797
FB
1024 memset(pMgmt->abyCurrBSSID, 0, 6);
1025 pMgmt->eCurrState = WMAC_STATE_IDLE;
1026
e3eb270f
MP
1027 pDevice->flags &= ~DEVICE_FLAGS_OPENED;
1028
92b96797
FB
1029 device_free_tx_bufs(pDevice);
1030 device_free_rx_bufs(pDevice);
1031 device_free_int_bufs(pDevice);
1032 device_free_frag_bufs(pDevice);
1033
dad72fed 1034 usb_kill_urb(pDevice->pInterruptURB);
92b96797
FB
1035 usb_free_urb(pDevice->pInterruptURB);
1036
1037 BSSvClearNodeDBTable(pDevice, 0);
92b96797
FB
1038
1039 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "device_close2 \n");
1040
1041 return 0;
1042}
1043
0d7fe14f 1044static void vt6656_disconnect(struct usb_interface *intf)
92b96797 1045{
dd0a774f 1046 struct vnt_private *device = usb_get_intfdata(intf);
92b96797 1047
6cda24f5
AM
1048 if (!device)
1049 return;
92b96797 1050
92b96797 1051 usb_set_intfdata(intf, NULL);
6cda24f5 1052 usb_put_dev(interface_to_usbdev(intf));
92b96797 1053
6cda24f5 1054 device->flags |= DEVICE_FLAGS_UNPLUG;
92b96797 1055
6cda24f5
AM
1056 if (device->dev) {
1057 unregister_netdev(device->dev);
6cda24f5
AM
1058 free_netdev(device->dev);
1059 }
92b96797
FB
1060}
1061
529e5b32
AM
1062static int device_xmit(struct sk_buff *skb, struct net_device *dev)
1063{
dd0a774f 1064 struct vnt_private *pDevice = netdev_priv(dev);
529e5b32 1065 struct net_device_stats *stats = &pDevice->stats;
3c647642 1066 unsigned long flags;
92b96797 1067
3c647642 1068 spin_lock_irqsave(&pDevice->lock, flags);
92b96797 1069
529e5b32 1070 netif_stop_queue(dev);
92b96797 1071
529e5b32
AM
1072 if (!pDevice->bLinkPass) {
1073 dev_kfree_skb_irq(skb);
1074 goto out;
1075 }
92b96797 1076
529e5b32
AM
1077 if (pDevice->bStopDataPkt) {
1078 dev_kfree_skb_irq(skb);
1079 stats->tx_dropped++;
1080 goto out;
1081 }
92b96797 1082
05cc617b 1083 if (nsDMA_tx_packet(pDevice, skb)) {
529e5b32
AM
1084 if (netif_queue_stopped(dev))
1085 netif_wake_queue(dev);
1086 }
92b96797 1087
529e5b32 1088out:
3c647642 1089 spin_unlock_irqrestore(&pDevice->lock, flags);
92b96797 1090
529e5b32 1091 return NETDEV_TX_OK;
92b96797
FB
1092}
1093
ec6e0f63 1094/* find out the start position of str2 from str1 */
cc856e61
AM
1095static unsigned char *kstrstr(const unsigned char *str1,
1096 const unsigned char *str2) {
1097 int str1_len = strlen(str1);
1098 int str2_len = strlen(str2);
92b96797
FB
1099
1100 while (str1_len >= str2_len) {
1101 str1_len--;
1102 if(memcmp(str1,str2,str2_len)==0)
cc856e61 1103 return (unsigned char *) str1;
92b96797
FB
1104 str1++;
1105 }
1106 return NULL;
1107}
1108
cc856e61
AM
1109static int Config_FileGetParameter(unsigned char *string,
1110 unsigned char *dest,
1111 unsigned char *source)
92b96797 1112{
cc856e61
AM
1113 unsigned char buf1[100];
1114 unsigned char buf2[100];
1115 unsigned char *start_p = NULL, *end_p = NULL, *tmp_p = NULL;
92b96797
FB
1116 int ii;
1117
1118 memset(buf1,0,100);
1119 strcat(buf1, string);
1120 strcat(buf1, "=");
1121 source+=strlen(buf1);
1122
ec6e0f63 1123 /* find target string start point */
bfbfeecc
JP
1124 start_p = kstrstr(source,buf1);
1125 if (start_p == NULL)
e269fc2d 1126 return false;
92b96797 1127
ec6e0f63 1128 /* check if current config line is marked by "#" */
33d33e42
AM
1129 for (ii = 1; ; ii++) {
1130 if (memcmp(start_p - ii, "\n", 1) == 0)
1131 break;
1132 if (memcmp(start_p - ii, "#", 1) == 0)
e269fc2d 1133 return false;
33d33e42 1134 }
92b96797 1135
ec6e0f63 1136 /* find target string end point */
bfbfeecc 1137 end_p = kstrstr(start_p,"\n");
ec6e0f63
AM
1138 if (end_p == NULL) { /* can't find "\n", but don't care */
1139 end_p = start_p + strlen(start_p); /* no include "\n" */
1140 }
92b96797
FB
1141
1142 memset(buf2,0,100);
ec6e0f63 1143 memcpy(buf2, start_p, end_p-start_p); /* get the target line */
92b96797
FB
1144 buf2[end_p-start_p]='\0';
1145
ec6e0f63 1146 /* find value */
bfbfeecc
JP
1147 start_p = kstrstr(buf2,"=");
1148 if (start_p == NULL)
e269fc2d 1149 return false;
92b96797
FB
1150 memset(buf1,0,100);
1151 strcpy(buf1,start_p+1);
1152
ec6e0f63 1153 /* except space */
92b96797
FB
1154 tmp_p = buf1;
1155 while(*tmp_p != 0x00) {
1156 if(*tmp_p==' ')
1157 tmp_p++;
1158 else
1159 break;
1160 }
1161
1162 memcpy(dest,tmp_p,strlen(tmp_p));
4e9b5e2b 1163 return true;
92b96797
FB
1164}
1165
ec6e0f63 1166/* if read fails, return NULL, or return data pointer */
dd0a774f 1167static unsigned char *Config_FileOperation(struct vnt_private *pDevice)
cc856e61 1168{
7c51d177
AV
1169 unsigned char *buffer = kmalloc(1024, GFP_KERNEL);
1170 struct file *file;
ec6e0f63 1171
7c51d177
AV
1172 if (!buffer) {
1173 printk("allocate mem for file fail?\n");
1174 return NULL;
1175 }
92b96797 1176
7c51d177
AV
1177 file = filp_open(CONFIG_PATH, O_RDONLY, 0);
1178 if (IS_ERR(file)) {
1179 kfree(buffer);
1180 printk("Config_FileOperation file Not exist\n");
1181 return NULL;
1182 }
92b96797 1183
7c51d177
AV
1184 if (kernel_read(file, 0, buffer, 1024) < 0) {
1185 printk("read file error?\n");
1186 kfree(buffer);
1187 buffer = NULL;
1188 }
8f9c466f 1189
7c51d177
AV
1190 fput(file);
1191 return buffer;
92b96797
FB
1192}
1193
ec6e0f63 1194/* return --->-1:fail; >=0:successful */
dd0a774f
MP
1195static int Read_config_file(struct vnt_private *pDevice)
1196{
1197 int result = 0;
1198 unsigned char tmpbuffer[100];
1199 unsigned char *buffer = NULL;
92b96797 1200
ec6e0f63 1201 /* init config setting */
92b96797
FB
1202 pDevice->config_file.ZoneType = -1;
1203 pDevice->config_file.eAuthenMode = -1;
1204 pDevice->config_file.eEncryptionStatus = -1;
1205
bfbfeecc
JP
1206 buffer = Config_FileOperation(pDevice);
1207 if (buffer == NULL) {
92b96797
FB
1208 result =-1;
1209 return result;
1210 }
1211
ec6e0f63 1212/* get zonetype */
92b96797
FB
1213{
1214 memset(tmpbuffer,0,sizeof(tmpbuffer));
4e9b5e2b 1215 if(Config_FileGetParameter("ZONETYPE",tmpbuffer,buffer) ==true) {
92b96797
FB
1216 if(memcmp(tmpbuffer,"USA",3)==0) {
1217 pDevice->config_file.ZoneType=ZoneType_USA;
1218 }
1219 else if(memcmp(tmpbuffer,"JAPAN",5)==0) {
1220 pDevice->config_file.ZoneType=ZoneType_Japan;
1221 }
1222 else if(memcmp(tmpbuffer,"EUROPE",6)==0) {
1223 pDevice->config_file.ZoneType=ZoneType_Europe;
1224 }
1225 else {
1226 printk("Unknown Zonetype[%s]?\n",tmpbuffer);
1227 }
1228 }
1229}
1230
ec6e0f63 1231/* get other parameter */
92b96797
FB
1232 {
1233 memset(tmpbuffer,0,sizeof(tmpbuffer));
4e9b5e2b 1234 if(Config_FileGetParameter("AUTHENMODE",tmpbuffer,buffer)==true) {
92b96797
FB
1235 pDevice->config_file.eAuthenMode = (int) simple_strtol(tmpbuffer, NULL, 10);
1236 }
1237
1238 memset(tmpbuffer,0,sizeof(tmpbuffer));
4e9b5e2b 1239 if(Config_FileGetParameter("ENCRYPTIONMODE",tmpbuffer,buffer)==true) {
92b96797
FB
1240 pDevice->config_file.eEncryptionStatus= (int) simple_strtol(tmpbuffer, NULL, 10);
1241 }
1242 }
92b96797
FB
1243
1244 kfree(buffer);
1245 return result;
1246}
1247
dd0a774f
MP
1248static void device_set_multi(struct net_device *dev)
1249{
e40068a6 1250 struct vnt_private *priv = netdev_priv(dev);
edd20e96
MP
1251 unsigned long flags;
1252
1253 if (priv->flags & DEVICE_FLAGS_OPENED) {
1254 spin_lock_irqsave(&priv->lock, flags);
1255
1256 bScheduleCommand(priv, WLAN_CMD_CONFIGURE_FILTER, NULL);
1257
1258 spin_unlock_irqrestore(&priv->lock, flags);
1259 }
1260}
1261
1262void vnt_configure_filter(struct vnt_private *priv)
1263{
1264 struct net_device *dev = priv->dev;
e40068a6 1265 struct vnt_manager *mgmt = &priv->vnt_mgmt;
dd0a774f 1266 struct netdev_hw_addr *ha;
0ca3e288 1267 u64 mc_filter = 0;
e40068a6 1268 u8 tmp = 0;
dd0a774f 1269 int rc;
92b96797 1270
441c21c3 1271 rc = vnt_control_in(priv, MESSAGE_TYPE_READ,
e40068a6
MP
1272 MAC_REG_RCR, MESSAGE_REQUEST_MACREG, 1, &tmp);
1273 if (rc == 0)
1274 priv->byRxMode = tmp;
1275
1276 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "priv->byRxMode in= %x\n",
1277 priv->byRxMode);
1278
1279 if (dev->flags & IFF_PROMISC) { /* set promiscuous mode */
1280 DBG_PRT(MSG_LEVEL_ERR, KERN_NOTICE
1281 "%s: Promiscuous mode enabled.\n", dev->name);
1282 /* unconditionally log net taps */
1283 priv->byRxMode |= (RCR_MULTICAST|RCR_BROADCAST|RCR_UNICAST);
1284 } else if ((netdev_mc_count(dev) > priv->multicast_limit) ||
1285 (dev->flags & IFF_ALLMULTI)) {
1286 mc_filter = ~0x0;
1287 MACvWriteMultiAddr(priv, mc_filter);
1288
1289 priv->byRxMode |= (RCR_MULTICAST|RCR_BROADCAST);
1290 } else {
1291 netdev_for_each_mc_addr(ha, dev) {
1292 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
0ca3e288 1293
e40068a6
MP
1294 mc_filter |= 1ULL << (bit_nr & 0x3f);
1295 }
1296
1297 MACvWriteMultiAddr(priv, mc_filter);
1298
1299 priv->byRxMode &= ~(RCR_UNICAST);
1300 priv->byRxMode |= (RCR_MULTICAST|RCR_BROADCAST);
0ca3e288
MP
1301 }
1302
e40068a6
MP
1303 if (mgmt->eConfigMode == WMAC_CONFIG_AP) {
1304 /*
1305 * If AP mode, don't enable RCR_UNICAST since HW only compares
1306 * addr1 with local MAC
1307 */
1308 priv->byRxMode |= (RCR_MULTICAST|RCR_BROADCAST);
1309 priv->byRxMode &= ~(RCR_UNICAST);
1310 }
0ca3e288 1311
285d58c4 1312 vnt_control_out_u8(priv, MESSAGE_REQUEST_MACREG,
e40068a6 1313 MAC_REG_RCR, priv->byRxMode);
92b96797 1314
e40068a6
MP
1315 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO
1316 "priv->byRxMode out= %x\n", priv->byRxMode);
92b96797
FB
1317}
1318
7055a075
MP
1319static struct net_device_stats *device_get_stats(struct net_device *dev)
1320{
dd0a774f 1321 struct vnt_private *pDevice = netdev_priv(dev);
92b96797 1322
dd0a774f 1323 return &pDevice->stats;
92b96797
FB
1324}
1325
7055a075
MP
1326static int device_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1327{
848ce511 1328 int rc = 0;
92b96797 1329
7055a075 1330 switch (cmd) {
92b96797 1331 case SIOCETHTOOL:
86a362f1 1332 return ethtool_ioctl(dev, rq);
92b96797 1333
7055a075 1334 }
92b96797 1335
7055a075 1336 return rc;
92b96797
FB
1337}
1338
86a362f1 1339static int ethtool_ioctl(struct net_device *dev, struct ifreq *rq)
92b96797
FB
1340{
1341 u32 ethcmd;
1342
86a362f1 1343 if (copy_from_user(&ethcmd, rq->ifr_data, sizeof(ethcmd)))
92b96797
FB
1344 return -EFAULT;
1345
1346 switch (ethcmd) {
1347 case ETHTOOL_GDRVINFO: {
1348 struct ethtool_drvinfo info = {ETHTOOL_GDRVINFO};
1349 strncpy(info.driver, DEVICE_NAME, sizeof(info.driver)-1);
1350 strncpy(info.version, DEVICE_VERSION, sizeof(info.version)-1);
86a362f1 1351 if (copy_to_user(rq->ifr_data, &info, sizeof(info)))
92b96797
FB
1352 return -EFAULT;
1353 return 0;
1354 }
1355
1356 }
1357
1358 return -EOPNOTSUPP;
1359}
1360
26e5b65b 1361MODULE_DEVICE_TABLE(usb, vt6656_table);
92b96797 1362
26e5b65b
AM
1363static struct usb_driver vt6656_driver = {
1364 .name = DEVICE_NAME,
1365 .probe = vt6656_probe,
1366 .disconnect = vt6656_disconnect,
1367 .id_table = vt6656_table,
92b96797 1368#ifdef CONFIG_PM
26e5b65b
AM
1369 .suspend = vt6656_suspend,
1370 .resume = vt6656_resume,
1371#endif /* CONFIG_PM */
92b96797
FB
1372};
1373
bac2c126 1374module_usb_driver(vt6656_driver);