main_usb.c 24.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 *
 * File: main_usb.c
 *
 * Purpose: driver entry for initial, open, close, tx and rx.
 *
 * Author: Lyndon Chen
 *
 * Date: Dec 8, 2005
 *
 * Functions:
 *
26
 *   vt6656_probe - module initial (insmod) driver entry
27
 *   vnt_free_tx_bufs - free tx buffer function
28
 *   vnt_init_registers- initial MAC & BBP & RF internal registers.
29 30 31 32 33
 *
 * Revision History:
 */
#undef __NO_VERSION__

34
#include <linux/etherdevice.h>
35
#include <linux/file.h>
36 37 38 39 40 41 42 43 44 45
#include "device.h"
#include "card.h"
#include "baseband.h"
#include "mac.h"
#include "power.h"
#include "wcmd.h"
#include "rxtx.h"
#include "dpc.h"
#include "rf.h"
#include "firmware.h"
46
#include "usbpipe.h"
47 48 49
#include "channel.h"
#include "int.h"

50 51 52
/*
 * define module options
 */
53

54 55 56
/* version information */
#define DRIVER_AUTHOR \
	"VIA Networking Technologies, Inc., <lyndonchen@vntek.com.tw>"
57 58 59 60
MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION(DEVICE_FULL_DRV_NAM);

61
#define RX_DESC_DEF0 64
62 63 64
static int vnt_rx_buffers = RX_DESC_DEF0;
module_param_named(rx_buffers, vnt_rx_buffers, int, 0644);
MODULE_PARM_DESC(rx_buffers, "Number of receive usb rx buffers");
65

66
#define TX_DESC_DEF0 64
67
static int vnt_tx_buffers = TX_DESC_DEF0;
68
module_param_named(tx_buffers, vnt_tx_buffers, int, 0644);
69 70
MODULE_PARM_DESC(tx_buffers, "Number of receive usb tx buffers");

71 72 73 74 75 76 77 78 79 80 81
#define RTS_THRESH_DEF     2347
#define FRAG_THRESH_DEF     2346
#define SHORT_RETRY_DEF     8
#define LONG_RETRY_DEF     4

/* BasebandType[] baseband type selected
   0: indicate 802.11a type
   1: indicate 802.11b type
   2: indicate 802.11g type
*/

82
#define BBP_TYPE_DEF     2
83

84 85 86
/*
 * Static vars definitions
 */
87

88
static struct usb_device_id vt6656_table[] = {
89 90 91 92
	{USB_DEVICE(VNT_USB_VENDOR_ID, VNT_USB_PRODUCT_ID)},
	{}
};

93
static void vnt_set_options(struct vnt_private *priv)
94
{
95 96
	/* Set number of TX buffers */
	if (vnt_tx_buffers < CB_MIN_TX_DESC || vnt_tx_buffers > CB_MAX_TX_DESC)
97
		priv->num_tx_context = TX_DESC_DEF0;
98
	else
99
		priv->num_tx_context = vnt_tx_buffers;
100 101 102

	/* Set number of RX buffers */
	if (vnt_rx_buffers < CB_MIN_RX_DESC || vnt_rx_buffers > CB_MAX_RX_DESC)
103
		priv->num_rcb = RX_DESC_DEF0;
104
	else
105
		priv->num_rcb = vnt_rx_buffers;
106

107 108
	priv->short_retry_limit = SHORT_RETRY_DEF;
	priv->long_retry_limit = LONG_RETRY_DEF;
109
	priv->op_mode = NL80211_IFTYPE_UNSPECIFIED;
110
	priv->bb_type = BBP_TYPE_DEF;
111
	priv->packet_type = priv->bb_type;
112
	priv->auto_fb_ctrl = AUTO_FB_0;
113
	priv->preamble_type = 0;
114
	priv->exist_sw_net_addr = false;
115 116
}

117 118 119
/*
 * initialization of MAC & BBP registers
 */
120
static int vnt_init_registers(struct vnt_private *priv)
121
{
122 123 124
	struct vnt_cmd_card_init *init_cmd = &priv->init_command;
	struct vnt_rsp_card_init *init_rsp = &priv->init_response;
	u8 antenna;
125
	int ii;
126 127 128
	int status = STATUS_SUCCESS;
	u8 tmp;
	u8 calib_tx_iq = 0, calib_tx_dc = 0, calib_rx_iq = 0;
129

130
	dev_dbg(&priv->usb->dev, "---->INIbInitAdapter. [%d][%d]\n",
131
				DEVICE_INIT_COLD, priv->packet_type);
132

133 134 135
	if (!vnt_check_firmware_version(priv)) {
		if (vnt_download_firmware(priv) == true) {
			if (vnt_firmware_branch_to_sram(priv) == false) {
136
				dev_dbg(&priv->usb->dev,
137
					" vnt_firmware_branch_to_sram fail\n");
138 139 140
				return false;
			}
		} else {
141
			dev_dbg(&priv->usb->dev, "FIRMWAREbDownload fail\n");
142 143 144 145
			return false;
		}
	}

146
	if (!vnt_vt3184_init(priv)) {
147
		dev_dbg(&priv->usb->dev, "vnt_vt3184_init fail\n");
148 149
		return false;
	}
150

151
	init_cmd->init_class = DEVICE_INIT_COLD;
152
	init_cmd->exist_sw_net_addr = priv->exist_sw_net_addr;
153
	for (ii = 0; ii < 6; ii++)
154
		init_cmd->sw_net_addr[ii] = priv->current_net_addr[ii];
155 156
	init_cmd->short_retry_limit = priv->short_retry_limit;
	init_cmd->long_retry_limit = priv->long_retry_limit;
157 158

	/* issue card_init command to device */
159
	status = vnt_control_out(priv,
160
		MESSAGE_TYPE_CARDINIT, 0, 0,
161
		sizeof(struct vnt_cmd_card_init), (u8 *)init_cmd);
162
	if (status != STATUS_SUCCESS) {
163
		dev_dbg(&priv->usb->dev, "Issue Card init fail\n");
164 165
		return false;
	}
166

167
	status = vnt_control_in(priv, MESSAGE_TYPE_INIT_RSP, 0, 0,
168
		sizeof(struct vnt_rsp_card_init), (u8 *)init_rsp);
169
	if (status != STATUS_SUCCESS) {
170
		dev_dbg(&priv->usb->dev,
171 172 173
			"Cardinit request in status fail!\n");
		return false;
	}
174

175
	/* local ID for AES functions */
176
	status = vnt_control_in(priv, MESSAGE_TYPE_READ,
177
		MAC_REG_LOCALID, MESSAGE_REQUEST_MACREG, 1,
178
			&priv->local_id);
179
	if (status != STATUS_SUCCESS)
180
		return false;
181

182 183
	/* do MACbSoftwareReset in MACvInitialize */

184
	priv->top_ofdm_basic_rate = RATE_24M;
185
	priv->top_cck_basic_rate = RATE_1M;
186

187
	/* target to IF pin while programming to RF chip */
188
	priv->power = 0xFF;
189

190 191
	priv->cck_pwr = priv->eeprom[EEP_OFS_PWR_CCK];
	priv->ofdm_pwr_g = priv->eeprom[EEP_OFS_PWR_OFDMG];
192 193
	/* load power table */
	for (ii = 0; ii < 14; ii++) {
194
		priv->cck_pwr_tbl[ii] =
195
			priv->eeprom[ii + EEP_OFS_CCK_PWR_TBL];
196 197
		if (priv->cck_pwr_tbl[ii] == 0)
			priv->cck_pwr_tbl[ii] = priv->cck_pwr;
198

199
		priv->ofdm_pwr_tbl[ii] =
200
				priv->eeprom[ii + EEP_OFS_OFDM_PWR_TBL];
201 202
		if (priv->ofdm_pwr_tbl[ii] == 0)
			priv->ofdm_pwr_tbl[ii] = priv->ofdm_pwr_g;
203
	}
204

205 206 207 208
	/*
	 * original zonetype is USA, but custom zonetype is Europe,
	 * then need to recover 12, 13, 14 channels with 11 channel
	 */
209
	for (ii = 11; ii < 14; ii++) {
210 211
		priv->cck_pwr_tbl[ii] = priv->cck_pwr_tbl[10];
		priv->ofdm_pwr_tbl[ii] = priv->ofdm_pwr_tbl[10];
212
	}
213

214
	priv->ofdm_pwr_a = 0x34; /* same as RFbMA2829SelectChannel */
215

216 217
	/* load OFDM A power table */
	for (ii = 0; ii < CB_MAX_CHANNEL_5G; ii++) {
218
		priv->ofdm_a_pwr_tbl[ii] =
219
			priv->eeprom[ii + EEP_OFS_OFDMA_PWR_TBL];
220

221 222
		if (priv->ofdm_a_pwr_tbl[ii] == 0)
			priv->ofdm_a_pwr_tbl[ii] = priv->ofdm_pwr_a;
223
	}
224

225
	antenna = priv->eeprom[EEP_OFS_ANTENNA];
226

227
	if (antenna & EEP_ANTINV)
228
		priv->tx_rx_ant_inv = true;
229
	else
230
		priv->tx_rx_ant_inv = false;
231

232
	antenna &= (EEP_ANTENNA_AUX | EEP_ANTENNA_MAIN);
233

234 235
	if (antenna == 0) /* if not set default is both */
		antenna = (EEP_ANTENNA_AUX | EEP_ANTENNA_MAIN);
236

237
	if (antenna == (EEP_ANTENNA_AUX | EEP_ANTENNA_MAIN)) {
238 239
		priv->tx_antenna_mode = ANT_B;
		priv->rx_antenna_sel = 1;
240

241 242
		if (priv->tx_rx_ant_inv == true)
			priv->rx_antenna_mode = ANT_A;
243
		else
244
			priv->rx_antenna_mode = ANT_B;
245
	} else  {
246
		priv->rx_antenna_sel = 0;
247

248
		if (antenna & EEP_ANTENNA_AUX) {
249
			priv->tx_antenna_mode = ANT_A;
250

251 252
			if (priv->tx_rx_ant_inv == true)
				priv->rx_antenna_mode = ANT_B;
253
			else
254
				priv->rx_antenna_mode = ANT_A;
255
		} else {
256
			priv->tx_antenna_mode = ANT_B;
257

258 259
		if (priv->tx_rx_ant_inv == true)
			priv->rx_antenna_mode = ANT_A;
260
		else
261
			priv->rx_antenna_mode = ANT_B;
262 263 264
		}
	}

265
	/* Set initial antenna mode */
266
	vnt_set_antenna_mode(priv, priv->rx_antenna_mode);
267

268
	/* get Auto Fall Back type */
269
	priv->auto_fb_ctrl = AUTO_FB_0;
270

271
	/* default Auto Mode */
272
	priv->bb_type = BB_TYPE_11G;
273

274
	/* get RFType */
275
	priv->rf_type = init_rsp->rf_type;
276

277
	/* load vt3266 calibration parameters in EEPROM */
278
	if (priv->rf_type == RF_VT3226D0) {
279 280
		if ((priv->eeprom[EEP_OFS_MAJOR_VER] == 0x1) &&
		    (priv->eeprom[EEP_OFS_MINOR_VER] >= 0x4)) {
281

282 283 284
			calib_tx_iq = priv->eeprom[EEP_OFS_CALIB_TX_IQ];
			calib_tx_dc = priv->eeprom[EEP_OFS_CALIB_TX_DC];
			calib_rx_iq = priv->eeprom[EEP_OFS_CALIB_RX_IQ];
285
			if (calib_tx_iq || calib_tx_dc || calib_rx_iq) {
286 287
				/* CR255, enable TX/RX IQ and
				   DC compensation mode */
288
				vnt_control_out_u8(priv,
289 290 291 292
						   MESSAGE_REQUEST_BBREG,
						   0xff,
						   0x03);
				/* CR251, TX I/Q Imbalance Calibration */
293
				vnt_control_out_u8(priv,
294 295
						   MESSAGE_REQUEST_BBREG,
						   0xfb,
296
						   calib_tx_iq);
297
				/* CR252, TX DC-Offset Calibration */
298
				vnt_control_out_u8(priv,
299 300
						   MESSAGE_REQUEST_BBREG,
						   0xfC,
301
						   calib_tx_dc);
302
				/* CR253, RX I/Q Imbalance Calibration */
303
				vnt_control_out_u8(priv,
304 305
						   MESSAGE_REQUEST_BBREG,
						   0xfd,
306
						   calib_rx_iq);
307
			} else {
308 309
				/* CR255, turn off
				   BB Calibration compensation */
310
				vnt_control_out_u8(priv,
311 312 313
						   MESSAGE_REQUEST_BBREG,
						   0xff,
						   0x0);
314 315 316
			}
		}
	}
317

318
	/* get permanent network address */
319
	memcpy(priv->permanent_net_addr, init_rsp->net_addr, 6);
320
	ether_addr_copy(priv->current_net_addr, priv->permanent_net_addr);
321

322
	/* if exist SW network address, use it */
323
	dev_dbg(&priv->usb->dev, "Network address = %pM\n",
324
		priv->current_net_addr);
325

326 327 328 329
	/*
	* set BB and packet type at the same time
	* set Short Slot Time, xIFS, and RSPINF
	*/
330
	if (priv->bb_type == BB_TYPE_11A)
331
		priv->short_slot_time = true;
332
	else
333
		priv->short_slot_time = false;
334

335
	vnt_set_short_slot_time(priv);
336

337
	priv->radio_ctl = priv->eeprom[EEP_OFS_RADIOCTL];
338

339
	if ((priv->radio_ctl & EEP_RADIOCTL_ENABLE) != 0) {
340 341
		status = vnt_control_in(priv, MESSAGE_TYPE_READ,
			MAC_REG_GPIOCTL1, MESSAGE_REQUEST_MACREG, 1, &tmp);
342

343
		if (status != STATUS_SUCCESS)
344 345
			return false;

346
		if ((tmp & GPIO3_DATA) == 0)
347
			vnt_mac_reg_bits_on(priv, MAC_REG_GPIOCTL1,
348
								GPIO3_INTMD);
349
		else
350
			vnt_mac_reg_bits_off(priv, MAC_REG_GPIOCTL1,
351
								GPIO3_INTMD);
352 353
	}

354
	vnt_mac_set_led(priv, LEDSTS_TMLEN, 0x38);
355

356
	vnt_mac_set_led(priv, LEDSTS_STS, LEDSTS_SLOW);
357

358
	vnt_mac_reg_bits_on(priv, MAC_REG_GPIOCTL0, 0x01);
359

360
	vnt_radio_power_on(priv);
361

362
	dev_dbg(&priv->usb->dev, "<----INIbInitAdapter Exit\n");
363 364

	return true;
365 366
}

367
static void vnt_free_tx_bufs(struct vnt_private *priv)
368
{
369 370 371
	struct vnt_usb_send_context *tx_context;
	int ii;

372
	for (ii = 0; ii < priv->num_tx_context; ii++) {
373
		tx_context = priv->tx_context[ii];
374
		/* deallocate URBs */
375 376 377
		if (tx_context->urb) {
			usb_kill_urb(tx_context->urb);
			usb_free_urb(tx_context->urb);
378 379 380 381
		}

		kfree(tx_context);
	}
382 383
}

384
static void vnt_free_rx_bufs(struct vnt_private *priv)
385
{
386
	struct vnt_rcb *rcb;
387
	int ii;
388

389
	for (ii = 0; ii < priv->num_rcb; ii++) {
390
		rcb = priv->rcb[ii];
391 392
		if (!rcb)
			continue;
393

394
		/* deallocate URBs */
395 396 397
		if (rcb->urb) {
			usb_kill_urb(rcb->urb);
			usb_free_urb(rcb->urb);
398
		}
399

400 401 402 403
		/* deallocate skb */
		if (rcb->skb)
			dev_kfree_skb(rcb->skb);

404 405
		kfree(rcb);
	}
406 407
}

408
static void usb_device_reset(struct vnt_private *priv)
409
{
410 411
	int status;

412
	status = usb_reset_device(priv->usb);
413
	if (status)
414
		dev_warn(&priv->usb->dev,
415
			 "usb_device_reset fail status=%d\n", status);
416 417
}

418
static void vnt_free_int_bufs(struct vnt_private *priv)
419
{
420
	kfree(priv->int_buf.data_buf);
421 422
}

423
static bool vnt_alloc_bufs(struct vnt_private *priv)
424
{
425 426
	struct vnt_usb_send_context *tx_context;
	struct vnt_rcb *rcb;
427
	int ii;
428

429
	for (ii = 0; ii < priv->num_tx_context; ii++) {
430 431
		tx_context = kmalloc(sizeof(struct vnt_usb_send_context),
								GFP_KERNEL);
432
		if (tx_context == NULL)
433
			goto free_tx;
434

435
		priv->tx_context[ii] = tx_context;
436
		tx_context->priv = priv;
437
		tx_context->pkt_no = ii;
438 439

		/* allocate URBs */
440 441
		tx_context->urb = usb_alloc_urb(0, GFP_ATOMIC);
		if (!tx_context->urb) {
442
			dev_err(&priv->usb->dev, "alloc tx urb failed\n");
443 444
			goto free_tx;
		}
445

446
		tx_context->in_use = false;
447 448
	}

449
	for (ii = 0; ii < priv->num_rcb; ii++) {
450 451
		priv->rcb[ii] = kzalloc(sizeof(struct vnt_rcb), GFP_KERNEL);
		if (!priv->rcb[ii]) {
452 453 454 455
			dev_err(&priv->usb->dev,
					"failed to allocate rcb no %d\n", ii);
			goto free_rx_tx;
		}
456

457
		rcb = priv->rcb[ii];
458

459
		rcb->priv = priv;
460

461
		/* allocate URBs */
462 463
		rcb->urb = usb_alloc_urb(0, GFP_ATOMIC);
		if (rcb->urb == NULL) {
464
			dev_err(&priv->usb->dev, "Failed to alloc rx urb\n");
465 466
			goto free_rx_tx;
		}
467

468
		rcb->skb = dev_alloc_skb(priv->rx_buf_sz);
469
		if (rcb->skb == NULL)
470
			goto free_rx_tx;
471

472
		rcb->in_use = false;
473

474
		/* submit rx urb */
475
		if (vnt_submit_rx_urb(priv, rcb))
476
			goto free_rx_tx;
477 478
	}

479 480
	priv->interrupt_urb = usb_alloc_urb(0, GFP_ATOMIC);
	if (priv->interrupt_urb == NULL) {
481
		dev_err(&priv->usb->dev, "Failed to alloc int urb\n");
482
		goto free_rx_tx;
483 484
	}

485 486
	priv->int_buf.data_buf = kmalloc(MAX_INTERRUPT_SIZE, GFP_KERNEL);
	if (priv->int_buf.data_buf == NULL) {
487
		usb_free_urb(priv->interrupt_urb);
488 489 490 491
		goto free_rx_tx;
	}

	return true;
492 493

free_rx_tx:
494
	vnt_free_rx_bufs(priv);
495 496

free_tx:
497
	vnt_free_tx_bufs(priv);
498

499
	return false;
500 501
}

502 503 504 505 506
static void vnt_tx_80211(struct ieee80211_hw *hw,
	struct ieee80211_tx_control *control, struct sk_buff *skb)
{
	struct vnt_private *priv = hw->priv;

507
	if (vnt_tx_packet(priv, skb))
508 509 510 511 512 513 514 515 516
		ieee80211_free_txskb(hw, skb);
}

static int vnt_start(struct ieee80211_hw *hw)
{
	struct vnt_private *priv = hw->priv;

	priv->rx_buf_sz = MAX_TOTAL_SIZE_WITH_ALL_HEADERS;

517
	if (!vnt_alloc_bufs(priv)) {
518
		dev_dbg(&priv->usb->dev, "vnt_alloc_bufs fail...\n");
519 520 521
		return -ENOMEM;
	}

522
	clear_bit(DEVICE_FLAGS_DISCONNECTED, &priv->flags);
523

524
	if (vnt_init_registers(priv) == false) {
525 526 527 528 529 530
		dev_dbg(&priv->usb->dev, " init register fail\n");
		goto free_all;
	}

	priv->int_interval = 1;  /* bInterval is set to 1 */

531
	vnt_int_start_interrupt(priv);
532 533 534 535 536 537

	ieee80211_wake_queues(hw);

	return 0;

free_all:
538
	vnt_free_rx_bufs(priv);
539
	vnt_free_tx_bufs(priv);
540
	vnt_free_int_bufs(priv);
541

542 543
	usb_kill_urb(priv->interrupt_urb);
	usb_free_urb(priv->interrupt_urb);
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561

	return -ENOMEM;
}

static void vnt_stop(struct ieee80211_hw *hw)
{
	struct vnt_private *priv = hw->priv;
	int i;

	if (!priv)
		return;

	for (i = 0; i < MAX_KEY_TABLE; i++)
		vnt_mac_disable_keyentry(priv, i);

	/* clear all keys */
	priv->key_entry_inuse = 0;

562
	if (!test_bit(DEVICE_FLAGS_UNPLUG, &priv->flags))
563 564 565 566
		vnt_mac_shutdown(priv);

	ieee80211_stop_queues(hw);

567
	set_bit(DEVICE_FLAGS_DISCONNECTED, &priv->flags);
568 569 570

	cancel_delayed_work_sync(&priv->run_command_work);

571
	priv->cmd_running = false;
572

573
	vnt_free_tx_bufs(priv);
574
	vnt_free_rx_bufs(priv);
575
	vnt_free_int_bufs(priv);
576

577 578
	usb_kill_urb(priv->interrupt_urb);
	usb_free_urb(priv->interrupt_urb);
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
}

static int vnt_add_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
{
	struct vnt_private *priv = hw->priv;

	priv->vif = vif;

	switch (vif->type) {
	case NL80211_IFTYPE_STATION:
		break;
	case NL80211_IFTYPE_ADHOC:
		vnt_mac_reg_bits_off(priv, MAC_REG_RCR, RCR_UNICAST);

		vnt_mac_reg_bits_on(priv, MAC_REG_HOSTCR, HOSTCR_ADHOC);

		break;
	case NL80211_IFTYPE_AP:
		vnt_mac_reg_bits_off(priv, MAC_REG_RCR, RCR_UNICAST);

		vnt_mac_reg_bits_on(priv, MAC_REG_HOSTCR, HOSTCR_AP);

		break;
	default:
		return -EOPNOTSUPP;
	}

	priv->op_mode = vif->type;

608 609
	vnt_set_bss_mode(priv);

610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667
	/* LED blink on TX */
	vnt_mac_set_led(priv, LEDSTS_STS, LEDSTS_INTER);

	return 0;
}

static void vnt_remove_interface(struct ieee80211_hw *hw,
		struct ieee80211_vif *vif)
{
	struct vnt_private *priv = hw->priv;

	switch (vif->type) {
	case NL80211_IFTYPE_STATION:
		break;
	case NL80211_IFTYPE_ADHOC:
		vnt_mac_reg_bits_off(priv, MAC_REG_TCR, TCR_AUTOBCNTX);
		vnt_mac_reg_bits_off(priv, MAC_REG_TFTCTL, TFTCTL_TSFCNTREN);
		vnt_mac_reg_bits_off(priv, MAC_REG_HOSTCR, HOSTCR_ADHOC);
		break;
	case NL80211_IFTYPE_AP:
		vnt_mac_reg_bits_off(priv, MAC_REG_TCR, TCR_AUTOBCNTX);
		vnt_mac_reg_bits_off(priv, MAC_REG_TFTCTL, TFTCTL_TSFCNTREN);
		vnt_mac_reg_bits_off(priv, MAC_REG_HOSTCR, HOSTCR_AP);
		break;
	default:
		break;
	}

	vnt_radio_power_off(priv);

	priv->op_mode = NL80211_IFTYPE_UNSPECIFIED;

	/* LED slow blink */
	vnt_mac_set_led(priv, LEDSTS_STS, LEDSTS_SLOW);
}

static int vnt_config(struct ieee80211_hw *hw, u32 changed)
{
	struct vnt_private *priv = hw->priv;
	struct ieee80211_conf *conf = &hw->conf;
	u8 bb_type;

	if (changed & IEEE80211_CONF_CHANGE_PS) {
		if (conf->flags & IEEE80211_CONF_PS)
			vnt_enable_power_saving(priv, conf->listen_interval);
		else
			vnt_disable_power_saving(priv);
	}

	if ((changed & IEEE80211_CONF_CHANGE_CHANNEL) ||
			(conf->flags & IEEE80211_CONF_OFFCHANNEL)) {
		vnt_set_channel(priv, conf->chandef.chan->hw_value);

		if (conf->chandef.chan->band == IEEE80211_BAND_5GHZ)
			bb_type = BB_TYPE_11A;
		else
			bb_type = BB_TYPE_11G;

668 669
		if (priv->bb_type != bb_type) {
			priv->bb_type = bb_type;
670 671 672 673 674 675

			vnt_set_bss_mode(priv);
		}
	}

	if (changed & IEEE80211_CONF_CHANGE_POWER) {
676
		if (priv->bb_type == BB_TYPE_11B)
677
			priv->current_rate = RATE_1M;
678
		else
679
			priv->current_rate = RATE_54M;
680

681
		vnt_rf_setpower(priv, priv->current_rate,
682 683 684 685 686 687 688 689 690 691 692
				conf->chandef.chan->hw_value);
	}

	return 0;
}

static void vnt_bss_info_changed(struct ieee80211_hw *hw,
		struct ieee80211_vif *vif, struct ieee80211_bss_conf *conf,
		u32 changed)
{
	struct vnt_private *priv = hw->priv;
693

694 695
	priv->current_aid = conf->aid;

696
	if (changed & BSS_CHANGED_BSSID && conf->bssid)
697 698 699 700
		vnt_mac_set_bssid_addr(priv, (u8 *)conf->bssid);


	if (changed & BSS_CHANGED_BASIC_RATES) {
701
		priv->basic_rates = conf->basic_rates;
702 703 704 705 706 707 708 709 710

		vnt_update_top_rates(priv);

		dev_dbg(&priv->usb->dev, "basic rates %x\n", conf->basic_rates);
	}

	if (changed & BSS_CHANGED_ERP_PREAMBLE) {
		if (conf->use_short_preamble) {
			vnt_mac_enable_barker_preamble_mode(priv);
711
			priv->preamble_type = true;
712 713
		} else {
			vnt_mac_disable_barker_preamble_mode(priv);
714
			priv->preamble_type = false;
715 716 717 718 719 720 721 722 723 724 725 726
		}
	}

	if (changed & BSS_CHANGED_ERP_CTS_PROT) {
		if (conf->use_cts_prot)
			vnt_mac_enable_protect_mode(priv);
		else
			vnt_mac_disable_protect_mode(priv);
	}

	if (changed & BSS_CHANGED_ERP_SLOT) {
		if (conf->use_short_slot)
727
			priv->short_slot_time = true;
728
		else
729
			priv->short_slot_time = false;
730

731
		vnt_set_short_slot_time(priv);
732
		vnt_set_vga_gain_offset(priv, priv->bb_vga[0]);
733
		vnt_update_pre_ed_threshold(priv, false);
734 735 736
	}

	if (changed & BSS_CHANGED_TXPOWER)
737
		vnt_rf_setpower(priv, priv->current_rate,
738 739 740 741 742 743 744 745 746 747 748 749 750 751
					conf->chandef.chan->hw_value);

	if (changed & BSS_CHANGED_BEACON_ENABLED) {
		dev_dbg(&priv->usb->dev,
				"Beacon enable %d\n", conf->enable_beacon);

		if (conf->enable_beacon) {
			vnt_beacon_enable(priv, vif, conf);

			vnt_mac_reg_bits_on(priv, MAC_REG_TCR, TCR_AUTOBCNTX);
		} else {
			vnt_mac_reg_bits_off(priv, MAC_REG_TCR, TCR_AUTOBCNTX);
		}
	}
752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771

	if (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_BEACON_INFO) &&
	    priv->op_mode != NL80211_IFTYPE_AP) {
		if (conf->assoc && conf->beacon_rate) {
			vnt_mac_reg_bits_on(priv, MAC_REG_TFTCTL,
					    TFTCTL_TSFCNTREN);

			vnt_adjust_tsf(priv, conf->beacon_rate->hw_value,
				       conf->sync_tsf, priv->current_tsf);

			vnt_mac_set_beacon_interval(priv, conf->beacon_int);

			vnt_reset_next_tbtt(priv, conf->beacon_int);
		} else {
			vnt_clear_current_tsf(priv);

			vnt_mac_reg_bits_off(priv, MAC_REG_TFTCTL,
					     TFTCTL_TSFCNTREN);
		}
	}
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
}

static u64 vnt_prepare_multicast(struct ieee80211_hw *hw,
	struct netdev_hw_addr_list *mc_list)
{
	struct vnt_private *priv = hw->priv;
	struct netdev_hw_addr *ha;
	u64 mc_filter = 0;
	u32 bit_nr = 0;

	netdev_hw_addr_list_for_each(ha, mc_list) {
		bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;

		mc_filter |= 1ULL << (bit_nr & 0x3f);
	}

	priv->mc_list_count = mc_list->count;

	return mc_filter;
}

static void vnt_configure(struct ieee80211_hw *hw,
	unsigned int changed_flags, unsigned int *total_flags, u64 multicast)
{
	struct vnt_private *priv = hw->priv;
	u8 rx_mode = 0;
	int rc;

800
	*total_flags &= FIF_ALLMULTI | FIF_OTHER_BSS | FIF_BCN_PRBRESP_PROMISC;
801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856

	rc = vnt_control_in(priv, MESSAGE_TYPE_READ, MAC_REG_RCR,
		MESSAGE_REQUEST_MACREG, sizeof(u8), &rx_mode);

	if (!rc)
		rx_mode = RCR_MULTICAST | RCR_BROADCAST;

	dev_dbg(&priv->usb->dev, "rx mode in = %x\n", rx_mode);

	if (changed_flags & FIF_ALLMULTI) {
		if (*total_flags & FIF_ALLMULTI) {
			if (priv->mc_list_count > 2)
				vnt_mac_set_filter(priv, ~0);
			else
				vnt_mac_set_filter(priv, multicast);

			rx_mode |= RCR_MULTICAST | RCR_BROADCAST;
		} else {
			rx_mode &= ~(RCR_MULTICAST | RCR_BROADCAST);
		}

	}

	if (changed_flags & (FIF_OTHER_BSS | FIF_BCN_PRBRESP_PROMISC)) {
		if (*total_flags & (FIF_OTHER_BSS | FIF_BCN_PRBRESP_PROMISC))
			rx_mode &= ~RCR_BSSID;
		else
			rx_mode |= RCR_BSSID;
	}

	vnt_control_out_u8(priv, MESSAGE_REQUEST_MACREG, MAC_REG_RCR, rx_mode);

	dev_dbg(&priv->usb->dev, "rx mode out= %x\n", rx_mode);
}

static int vnt_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
	struct ieee80211_vif *vif, struct ieee80211_sta *sta,
		struct ieee80211_key_conf *key)
{
	struct vnt_private *priv = hw->priv;

	switch (cmd) {
	case SET_KEY:
		if (vnt_set_keys(hw, sta, vif, key))
			return -EOPNOTSUPP;
		break;
	case DISABLE_KEY:
		if (test_bit(key->hw_key_idx, &priv->key_entry_inuse))
			clear_bit(key->hw_key_idx, &priv->key_entry_inuse);
	default:
		break;
	}

	return 0;
}

857 858 859
static void vnt_sw_scan_start(struct ieee80211_hw *hw,
			      struct ieee80211_vif *vif,
			      const u8 *addr)
860 861 862
{
	struct vnt_private *priv = hw->priv;

863
	vnt_set_bss_mode(priv);
864
	/* Set max sensitivity*/
865
	vnt_update_pre_ed_threshold(priv, true);
866 867
}

868 869
static void vnt_sw_scan_complete(struct ieee80211_hw *hw,
				 struct ieee80211_vif *vif)
870 871 872 873
{
	struct vnt_private *priv = hw->priv;

	/* Return sensitivity to channel level*/
874
	vnt_update_pre_ed_threshold(priv, false);
875 876
}

877 878 879 880 881 882 883 884 885 886
static int vnt_get_stats(struct ieee80211_hw *hw,
				struct ieee80211_low_level_stats *stats)
{
	struct vnt_private *priv = hw->priv;

	memcpy(stats, &priv->low_stats, sizeof(*stats));

	return 0;
}

887 888 889 890
static u64 vnt_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
{
	struct vnt_private *priv = hw->priv;

891
	return priv->current_tsf;
892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923
}

static void vnt_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
			u64 tsf)
{
	struct vnt_private *priv = hw->priv;

	vnt_update_next_tbtt(priv, tsf, vif->bss_conf.beacon_int);
}

static void vnt_reset_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
{
	struct vnt_private *priv = hw->priv;

	vnt_mac_reg_bits_off(priv, MAC_REG_TFTCTL, TFTCTL_TSFCNTREN);

	vnt_clear_current_tsf(priv);
}

static const struct ieee80211_ops vnt_mac_ops = {
	.tx			= vnt_tx_80211,
	.start			= vnt_start,
	.stop			= vnt_stop,
	.add_interface		= vnt_add_interface,
	.remove_interface	= vnt_remove_interface,
	.config			= vnt_config,
	.bss_info_changed	= vnt_bss_info_changed,
	.prepare_multicast	= vnt_prepare_multicast,
	.configure_filter	= vnt_configure,
	.set_key		= vnt_set_key,
	.sw_scan_start		= vnt_sw_scan_start,
	.sw_scan_complete	= vnt_sw_scan_complete,
924
	.get_stats		= vnt_get_stats,
925 926 927 928 929 930 931 932
	.get_tsf		= vnt_get_tsf,
	.set_tsf		= vnt_set_tsf,
	.reset_tsf		= vnt_reset_tsf,
};

int vnt_init(struct vnt_private *priv)
{

933
	if (!(vnt_init_registers(priv)))
934 935
		return -EAGAIN;

936
	SET_IEEE80211_PERM_ADDR(priv->hw, priv->permanent_net_addr);
937

938 939
	vnt_init_bands(priv);

940 941 942
	if (ieee80211_register_hw(priv->hw))
		return -ENODEV;

943 944
	priv->mac_hw = true;

945 946
	vnt_radio_power_off(priv);

947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968
	return 0;
}

static int
vt6656_probe(struct usb_interface *intf, const struct usb_device_id *id)
{
	struct usb_device *udev;
	struct vnt_private *priv;
	struct ieee80211_hw *hw;
	struct wiphy *wiphy;
	int rc = 0;

	udev = usb_get_dev(interface_to_usbdev(intf));

	dev_notice(&udev->dev, "%s Ver. %s\n",
					DEVICE_FULL_DRV_NAM, DEVICE_VERSION);
	dev_notice(&udev->dev,
		"Copyright (c) 2004 VIA Networking Technologies, Inc.\n");

	hw = ieee80211_alloc_hw(sizeof(struct vnt_private), &vnt_mac_ops);
	if (!hw) {
		dev_err(&udev->dev, "could not register ieee80211_hw\n");
969
		rc = -ENOMEM;
970 971 972 973 974 975 976
		goto err_nomem;
	}

	priv = hw->priv;
	priv->hw = hw;
	priv->usb = udev;

977
	vnt_set_options(priv);
978 979 980 981

	spin_lock_init(&priv->lock);
	mutex_init(&priv->usb_lock);

982
	INIT_DELAYED_WORK(&priv->run_command_work, vnt_run_command);
983 984 985 986 987 988 989 990 991 992

	usb_set_intfdata(intf, priv);

	wiphy = priv->hw->wiphy;

	wiphy->frag_threshold = FRAG_THRESH_DEF;
	wiphy->rts_threshold = RTS_THRESH_DEF;
	wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
		BIT(NL80211_IFTYPE_ADHOC) | BIT(NL80211_IFTYPE_AP);

993 994 995 996
	ieee80211_hw_set(priv->hw, TIMING_BEACON_ONLY);
	ieee80211_hw_set(priv->hw, SIGNAL_DBM);
	ieee80211_hw_set(priv->hw, RX_INCLUDES_FCS);
	ieee80211_hw_set(priv->hw, REPORTS_TX_ACK_STATUS);
997
	ieee80211_hw_set(priv->hw, SUPPORTS_PS);
998 999 1000 1001 1002 1003 1004

	priv->hw->max_signal = 100;

	SET_IEEE80211_DEV(priv->hw, &intf->dev);

	usb_device_reset(priv);

1005
	clear_bit(DEVICE_FLAGS_DISCONNECTED, &priv->flags);
1006
	vnt_reset_command_timer(priv);
1007

1008
	vnt_schedule_command(priv, WLAN_CMD_INIT_MAC80211);
1009

1010 1011 1012 1013 1014 1015 1016 1017
	return 0;

err_nomem:
	usb_put_dev(udev);

	return rc;
}

1018
static void vt6656_disconnect(struct usb_interface *intf)
1019
{
1020
	struct vnt_private *priv = usb_get_intfdata(intf);
1021

1022
	if (!priv)
1023
		return;
1024

1025 1026
	if (priv->mac_hw)
		ieee80211_unregister_hw(priv->hw);
1027

1028
	usb_set_intfdata(intf, NULL);
1029
	usb_put_dev(interface_to_usbdev(intf));
1030

1031
	set_bit(DEVICE_FLAGS_UNPLUG, &priv->flags);
1032

1033
	ieee80211_free_hw(priv->hw);
1034 1035
}

1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
#ifdef CONFIG_PM

static int vt6656_suspend(struct usb_interface *intf, pm_message_t message)
{
	return 0;
}

static int vt6656_resume(struct usb_interface *intf)
{
	return 0;
}

#endif /* CONFIG_PM */

1050
MODULE_DEVICE_TABLE(usb, vt6656_table);
1051

1052 1053 1054 1055 1056
static struct usb_driver vt6656_driver = {
	.name =		DEVICE_NAME,
	.probe =	vt6656_probe,
	.disconnect =	vt6656_disconnect,
	.id_table =	vt6656_table,
1057
#ifdef CONFIG_PM
1058 1059 1060
	.suspend = vt6656_suspend,
	.resume = vt6656_resume,
#endif /* CONFIG_PM */
1061 1062
};

1063
module_usb_driver(vt6656_driver);