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  *
16  * File: main_usb.c
17  *
18  * Purpose: driver entry for initial, open, close, tx and rx.
19  *
20  * Author: Lyndon Chen
21  *
22  * Date: Dec 8, 2005
23  *
24  * Functions:
25  *
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  * Revision History:
31  */
32 #undef __NO_VERSION__
33 
34 #include <linux/etherdevice.h>
35 #include <linux/file.h>
36 #include "device.h"
37 #include "card.h"
38 #include "baseband.h"
39 #include "mac.h"
40 #include "power.h"
41 #include "wcmd.h"
42 #include "rxtx.h"
43 #include "dpc.h"
44 #include "rf.h"
45 #include "firmware.h"
46 #include "usbpipe.h"
47 #include "channel.h"
48 #include "int.h"
49 
50 /*
51  * define module options
52  */
53 
54 /* version information */
55 #define DRIVER_AUTHOR \
56 	"VIA Networking Technologies, Inc., <lyndonchen@vntek.com.tw>"
57 MODULE_AUTHOR(DRIVER_AUTHOR);
58 MODULE_LICENSE("GPL");
59 MODULE_DESCRIPTION(DEVICE_FULL_DRV_NAM);
60 
61 #define RX_DESC_DEF0 64
62 static int vnt_rx_buffers = RX_DESC_DEF0;
63 module_param_named(rx_buffers, vnt_rx_buffers, int, 0644);
64 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 MODULE_PARM_DESC(tx_buffers, "Number of receive usb tx buffers");
70 
71 #define RTS_THRESH_DEF     2347
72 #define FRAG_THRESH_DEF     2346
73 #define SHORT_RETRY_DEF     8
74 #define LONG_RETRY_DEF     4
75 
76 /* BasebandType[] baseband type selected
77  * 0: indicate 802.11a type
78  * 1: indicate 802.11b type
79  * 2: indicate 802.11g type
80  */
81 
82 #define BBP_TYPE_DEF     2
83 
84 /*
85  * Static vars definitions
86  */
87 
88 static const struct usb_device_id vt6656_table[] = {
89 	{USB_DEVICE(VNT_USB_VENDOR_ID, VNT_USB_PRODUCT_ID)},
90 	{}
91 };
92 
93 static void vnt_set_options(struct vnt_private *priv)
94 {
95 	/* Set number of TX buffers */
96 	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 	/* Set number of RX buffers */
102 	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 	priv->short_retry_limit = SHORT_RETRY_DEF;
108 	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  * initialization of MAC & BBP registers
119  */
120 static int vnt_init_registers(struct vnt_private *priv)
121 {
122 	struct vnt_cmd_card_init *init_cmd = &priv->init_command;
123 	struct vnt_rsp_card_init *init_rsp = &priv->init_response;
124 	u8 antenna;
125 	int ii;
126 	int status = STATUS_SUCCESS;
127 	u8 tmp;
128 	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 	if (!vnt_check_firmware_version(priv)) {
134 		if (vnt_download_firmware(priv) == true) {
135 			if (vnt_firmware_branch_to_sram(priv) == false) {
136 				dev_dbg(&priv->usb->dev,
137 					" vnt_firmware_branch_to_sram fail\n");
138 				return false;
139 			}
140 		} else {
141 			dev_dbg(&priv->usb->dev, "FIRMWAREbDownload fail\n");
142 			return false;
143 		}
144 	}
145 
146 	if (!vnt_vt3184_init(priv)) {
147 		dev_dbg(&priv->usb->dev, "vnt_vt3184_init fail\n");
148 		return false;
149 	}
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 	init_cmd->short_retry_limit = priv->short_retry_limit;
156 	init_cmd->long_retry_limit = priv->long_retry_limit;
157 
158 	/* issue card_init command to device */
159 	status = vnt_control_out(priv, MESSAGE_TYPE_CARDINIT, 0, 0,
160 				 sizeof(struct vnt_cmd_card_init),
161 				 (u8 *)init_cmd);
162 	if (status != STATUS_SUCCESS) {
163 		dev_dbg(&priv->usb->dev, "Issue Card init fail\n");
164 		return false;
165 	}
166 
167 	status = vnt_control_in(priv, MESSAGE_TYPE_INIT_RSP, 0, 0,
168 				sizeof(struct vnt_rsp_card_init),
169 				(u8 *)init_rsp);
170 	if (status != STATUS_SUCCESS) {
171 		dev_dbg(&priv->usb->dev,
172 			"Cardinit request in status fail!\n");
173 		return false;
174 	}
175 
176 	/* local ID for AES functions */
177 	status = vnt_control_in(priv, MESSAGE_TYPE_READ, MAC_REG_LOCALID,
178 				MESSAGE_REQUEST_MACREG, 1, &priv->local_id);
179 	if (status != STATUS_SUCCESS)
180 		return false;
181 
182 	/* do MACbSoftwareReset in MACvInitialize */
183 
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 	priv->cck_pwr = priv->eeprom[EEP_OFS_PWR_CCK];
191 	priv->ofdm_pwr_g = priv->eeprom[EEP_OFS_PWR_OFDMG];
192 	/* load power table */
193 	for (ii = 0; ii < 14; ii++) {
194 		priv->cck_pwr_tbl[ii] =
195 			priv->eeprom[ii + EEP_OFS_CCK_PWR_TBL];
196 		if (priv->cck_pwr_tbl[ii] == 0)
197 			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 		if (priv->ofdm_pwr_tbl[ii] == 0)
202 			priv->ofdm_pwr_tbl[ii] = priv->ofdm_pwr_g;
203 	}
204 
205 	/*
206 	 * original zonetype is USA, but custom zonetype is Europe,
207 	 * then need to recover 12, 13, 14 channels with 11 channel
208 	 */
209 	for (ii = 11; ii < 14; ii++) {
210 		priv->cck_pwr_tbl[ii] = priv->cck_pwr_tbl[10];
211 		priv->ofdm_pwr_tbl[ii] = priv->ofdm_pwr_tbl[10];
212 	}
213 
214 	priv->ofdm_pwr_a = 0x34; /* same as RFbMA2829SelectChannel */
215 
216 	/* load OFDM A power table */
217 	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 		if (priv->ofdm_a_pwr_tbl[ii] == 0)
222 			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 	if (antenna == 0) /* if not set default is both */
235 		antenna = (EEP_ANTENNA_AUX | EEP_ANTENNA_MAIN);
236 
237 	if (antenna == (EEP_ANTENNA_AUX | EEP_ANTENNA_MAIN)) {
238 		priv->tx_antenna_mode = ANT_B;
239 		priv->rx_antenna_sel = 1;
240 
241 		if (priv->tx_rx_ant_inv)
242 			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 			if (priv->tx_rx_ant_inv)
252 				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 		if (priv->tx_rx_ant_inv)
259 			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 		if ((priv->eeprom[EEP_OFS_MAJOR_VER] == 0x1) &&
280 		    (priv->eeprom[EEP_OFS_MINOR_VER] >= 0x4)) {
281 			calib_tx_iq = priv->eeprom[EEP_OFS_CALIB_TX_IQ];
282 			calib_tx_dc = priv->eeprom[EEP_OFS_CALIB_TX_DC];
283 			calib_rx_iq = priv->eeprom[EEP_OFS_CALIB_RX_IQ];
284 			if (calib_tx_iq || calib_tx_dc || calib_rx_iq) {
285 				/* CR255, enable TX/RX IQ and
286 				 * DC compensation mode
287 				 */
288 				vnt_control_out_u8(priv,
289 						   MESSAGE_REQUEST_BBREG,
290 						   0xff,
291 						   0x03);
292 				/* CR251, TX I/Q Imbalance Calibration */
293 				vnt_control_out_u8(priv,
294 						   MESSAGE_REQUEST_BBREG,
295 						   0xfb,
296 						   calib_tx_iq);
297 				/* CR252, TX DC-Offset Calibration */
298 				vnt_control_out_u8(priv,
299 						   MESSAGE_REQUEST_BBREG,
300 						   0xfC,
301 						   calib_tx_dc);
302 				/* CR253, RX I/Q Imbalance Calibration */
303 				vnt_control_out_u8(priv,
304 						   MESSAGE_REQUEST_BBREG,
305 						   0xfd,
306 						   calib_rx_iq);
307 			} else {
308 				/* CR255, turn off
309 				 * BB Calibration compensation
310 				 */
311 				vnt_control_out_u8(priv,
312 						   MESSAGE_REQUEST_BBREG,
313 						   0xff,
314 						   0x0);
315 			}
316 		}
317 	}
318 
319 	/* get permanent network address */
320 	memcpy(priv->permanent_net_addr, init_rsp->net_addr, 6);
321 	ether_addr_copy(priv->current_net_addr, priv->permanent_net_addr);
322 
323 	/* if exist SW network address, use it */
324 	dev_dbg(&priv->usb->dev, "Network address = %pM\n",
325 		priv->current_net_addr);
326 
327 	/*
328 	 * set BB and packet type at the same time
329 	 * set Short Slot Time, xIFS, and RSPINF
330 	 */
331 	if (priv->bb_type == BB_TYPE_11A)
332 		priv->short_slot_time = true;
333 	else
334 		priv->short_slot_time = false;
335 
336 	vnt_set_short_slot_time(priv);
337 
338 	priv->radio_ctl = priv->eeprom[EEP_OFS_RADIOCTL];
339 
340 	if ((priv->radio_ctl & EEP_RADIOCTL_ENABLE) != 0) {
341 		status = vnt_control_in(priv, MESSAGE_TYPE_READ,
342 					MAC_REG_GPIOCTL1,
343 					MESSAGE_REQUEST_MACREG, 1, &tmp);
344 
345 		if (status != STATUS_SUCCESS)
346 			return false;
347 
348 		if ((tmp & GPIO3_DATA) == 0)
349 			vnt_mac_reg_bits_on(priv, MAC_REG_GPIOCTL1,
350 					    GPIO3_INTMD);
351 		else
352 			vnt_mac_reg_bits_off(priv, MAC_REG_GPIOCTL1,
353 					     GPIO3_INTMD);
354 	}
355 
356 	vnt_mac_set_led(priv, LEDSTS_TMLEN, 0x38);
357 
358 	vnt_mac_set_led(priv, LEDSTS_STS, LEDSTS_SLOW);
359 
360 	vnt_mac_reg_bits_on(priv, MAC_REG_GPIOCTL0, 0x01);
361 
362 	vnt_radio_power_on(priv);
363 
364 	dev_dbg(&priv->usb->dev, "<----INIbInitAdapter Exit\n");
365 
366 	return true;
367 }
368 
369 static void vnt_free_tx_bufs(struct vnt_private *priv)
370 {
371 	struct vnt_usb_send_context *tx_context;
372 	int ii;
373 
374 	for (ii = 0; ii < priv->num_tx_context; ii++) {
375 		tx_context = priv->tx_context[ii];
376 		/* deallocate URBs */
377 		if (tx_context->urb) {
378 			usb_kill_urb(tx_context->urb);
379 			usb_free_urb(tx_context->urb);
380 		}
381 
382 		kfree(tx_context);
383 	}
384 }
385 
386 static void vnt_free_rx_bufs(struct vnt_private *priv)
387 {
388 	struct vnt_rcb *rcb;
389 	int ii;
390 
391 	for (ii = 0; ii < priv->num_rcb; ii++) {
392 		rcb = priv->rcb[ii];
393 		if (!rcb)
394 			continue;
395 
396 		/* deallocate URBs */
397 		if (rcb->urb) {
398 			usb_kill_urb(rcb->urb);
399 			usb_free_urb(rcb->urb);
400 		}
401 
402 		/* deallocate skb */
403 		if (rcb->skb)
404 			dev_kfree_skb(rcb->skb);
405 
406 		kfree(rcb);
407 	}
408 }
409 
410 static void usb_device_reset(struct vnt_private *priv)
411 {
412 	int status;
413 
414 	status = usb_reset_device(priv->usb);
415 	if (status)
416 		dev_warn(&priv->usb->dev,
417 			 "usb_device_reset fail status=%d\n", status);
418 }
419 
420 static void vnt_free_int_bufs(struct vnt_private *priv)
421 {
422 	kfree(priv->int_buf.data_buf);
423 }
424 
425 static bool vnt_alloc_bufs(struct vnt_private *priv)
426 {
427 	struct vnt_usb_send_context *tx_context;
428 	struct vnt_rcb *rcb;
429 	int ii;
430 
431 	for (ii = 0; ii < priv->num_tx_context; ii++) {
432 		tx_context = kmalloc(sizeof(struct vnt_usb_send_context),
433 				     GFP_KERNEL);
434 		if (!tx_context)
435 			goto free_tx;
436 
437 		priv->tx_context[ii] = tx_context;
438 		tx_context->priv = priv;
439 		tx_context->pkt_no = ii;
440 
441 		/* allocate URBs */
442 		tx_context->urb = usb_alloc_urb(0, GFP_KERNEL);
443 		if (!tx_context->urb)
444 			goto free_tx;
445 
446 		tx_context->in_use = false;
447 	}
448 
449 	for (ii = 0; ii < priv->num_rcb; ii++) {
450 		priv->rcb[ii] = kzalloc(sizeof(struct vnt_rcb), GFP_KERNEL);
451 		if (!priv->rcb[ii]) {
452 			dev_err(&priv->usb->dev,
453 				"failed to allocate rcb no %d\n", ii);
454 			goto free_rx_tx;
455 		}
456 
457 		rcb = priv->rcb[ii];
458 
459 		rcb->priv = priv;
460 
461 		/* allocate URBs */
462 		rcb->urb = usb_alloc_urb(0, GFP_KERNEL);
463 		if (!rcb->urb)
464 			goto free_rx_tx;
465 
466 		rcb->skb = dev_alloc_skb(priv->rx_buf_sz);
467 		if (!rcb->skb)
468 			goto free_rx_tx;
469 
470 		rcb->in_use = false;
471 
472 		/* submit rx urb */
473 		if (vnt_submit_rx_urb(priv, rcb))
474 			goto free_rx_tx;
475 	}
476 
477 	priv->interrupt_urb = usb_alloc_urb(0, GFP_KERNEL);
478 	if (!priv->interrupt_urb)
479 		goto free_rx_tx;
480 
481 	priv->int_buf.data_buf = kmalloc(MAX_INTERRUPT_SIZE, GFP_KERNEL);
482 	if (!priv->int_buf.data_buf) {
483 		usb_free_urb(priv->interrupt_urb);
484 		goto free_rx_tx;
485 	}
486 
487 	return true;
488 
489 free_rx_tx:
490 	vnt_free_rx_bufs(priv);
491 
492 free_tx:
493 	vnt_free_tx_bufs(priv);
494 
495 	return false;
496 }
497 
498 static void vnt_tx_80211(struct ieee80211_hw *hw,
499 			 struct ieee80211_tx_control *control,
500 			 struct sk_buff *skb)
501 {
502 	struct vnt_private *priv = hw->priv;
503 
504 	if (vnt_tx_packet(priv, skb))
505 		ieee80211_free_txskb(hw, skb);
506 }
507 
508 static int vnt_start(struct ieee80211_hw *hw)
509 {
510 	struct vnt_private *priv = hw->priv;
511 
512 	priv->rx_buf_sz = MAX_TOTAL_SIZE_WITH_ALL_HEADERS;
513 
514 	if (!vnt_alloc_bufs(priv)) {
515 		dev_dbg(&priv->usb->dev, "vnt_alloc_bufs fail...\n");
516 		return -ENOMEM;
517 	}
518 
519 	clear_bit(DEVICE_FLAGS_DISCONNECTED, &priv->flags);
520 
521 	if (vnt_init_registers(priv) == false) {
522 		dev_dbg(&priv->usb->dev, " init register fail\n");
523 		goto free_all;
524 	}
525 
526 	priv->int_interval = 1;  /* bInterval is set to 1 */
527 
528 	vnt_int_start_interrupt(priv);
529 
530 	ieee80211_wake_queues(hw);
531 
532 	return 0;
533 
534 free_all:
535 	vnt_free_rx_bufs(priv);
536 	vnt_free_tx_bufs(priv);
537 	vnt_free_int_bufs(priv);
538 
539 	usb_kill_urb(priv->interrupt_urb);
540 	usb_free_urb(priv->interrupt_urb);
541 
542 	return -ENOMEM;
543 }
544 
545 static void vnt_stop(struct ieee80211_hw *hw)
546 {
547 	struct vnt_private *priv = hw->priv;
548 	int i;
549 
550 	if (!priv)
551 		return;
552 
553 	for (i = 0; i < MAX_KEY_TABLE; i++)
554 		vnt_mac_disable_keyentry(priv, i);
555 
556 	/* clear all keys */
557 	priv->key_entry_inuse = 0;
558 
559 	if (!test_bit(DEVICE_FLAGS_UNPLUG, &priv->flags))
560 		vnt_mac_shutdown(priv);
561 
562 	ieee80211_stop_queues(hw);
563 
564 	set_bit(DEVICE_FLAGS_DISCONNECTED, &priv->flags);
565 
566 	cancel_delayed_work_sync(&priv->run_command_work);
567 
568 	priv->cmd_running = false;
569 
570 	vnt_free_tx_bufs(priv);
571 	vnt_free_rx_bufs(priv);
572 	vnt_free_int_bufs(priv);
573 
574 	usb_kill_urb(priv->interrupt_urb);
575 	usb_free_urb(priv->interrupt_urb);
576 }
577 
578 static int vnt_add_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
579 {
580 	struct vnt_private *priv = hw->priv;
581 
582 	priv->vif = vif;
583 
584 	switch (vif->type) {
585 	case NL80211_IFTYPE_STATION:
586 		break;
587 	case NL80211_IFTYPE_ADHOC:
588 		vnt_mac_reg_bits_off(priv, MAC_REG_RCR, RCR_UNICAST);
589 
590 		vnt_mac_reg_bits_on(priv, MAC_REG_HOSTCR, HOSTCR_ADHOC);
591 
592 		break;
593 	case NL80211_IFTYPE_AP:
594 		vnt_mac_reg_bits_off(priv, MAC_REG_RCR, RCR_UNICAST);
595 
596 		vnt_mac_reg_bits_on(priv, MAC_REG_HOSTCR, HOSTCR_AP);
597 
598 		break;
599 	default:
600 		return -EOPNOTSUPP;
601 	}
602 
603 	priv->op_mode = vif->type;
604 
605 	vnt_set_bss_mode(priv);
606 
607 	/* LED blink on TX */
608 	vnt_mac_set_led(priv, LEDSTS_STS, LEDSTS_INTER);
609 
610 	return 0;
611 }
612 
613 static void vnt_remove_interface(struct ieee80211_hw *hw,
614 				 struct ieee80211_vif *vif)
615 {
616 	struct vnt_private *priv = hw->priv;
617 
618 	switch (vif->type) {
619 	case NL80211_IFTYPE_STATION:
620 		break;
621 	case NL80211_IFTYPE_ADHOC:
622 		vnt_mac_reg_bits_off(priv, MAC_REG_TCR, TCR_AUTOBCNTX);
623 		vnt_mac_reg_bits_off(priv, MAC_REG_TFTCTL, TFTCTL_TSFCNTREN);
624 		vnt_mac_reg_bits_off(priv, MAC_REG_HOSTCR, HOSTCR_ADHOC);
625 		break;
626 	case NL80211_IFTYPE_AP:
627 		vnt_mac_reg_bits_off(priv, MAC_REG_TCR, TCR_AUTOBCNTX);
628 		vnt_mac_reg_bits_off(priv, MAC_REG_TFTCTL, TFTCTL_TSFCNTREN);
629 		vnt_mac_reg_bits_off(priv, MAC_REG_HOSTCR, HOSTCR_AP);
630 		break;
631 	default:
632 		break;
633 	}
634 
635 	vnt_radio_power_off(priv);
636 
637 	priv->op_mode = NL80211_IFTYPE_UNSPECIFIED;
638 
639 	/* LED slow blink */
640 	vnt_mac_set_led(priv, LEDSTS_STS, LEDSTS_SLOW);
641 }
642 
643 static int vnt_config(struct ieee80211_hw *hw, u32 changed)
644 {
645 	struct vnt_private *priv = hw->priv;
646 	struct ieee80211_conf *conf = &hw->conf;
647 	u8 bb_type;
648 
649 	if (changed & IEEE80211_CONF_CHANGE_PS) {
650 		if (conf->flags & IEEE80211_CONF_PS)
651 			vnt_enable_power_saving(priv, conf->listen_interval);
652 		else
653 			vnt_disable_power_saving(priv);
654 	}
655 
656 	if ((changed & IEEE80211_CONF_CHANGE_CHANNEL) ||
657 	    (conf->flags & IEEE80211_CONF_OFFCHANNEL)) {
658 		vnt_set_channel(priv, conf->chandef.chan->hw_value);
659 
660 		if (conf->chandef.chan->band == NL80211_BAND_5GHZ)
661 			bb_type = BB_TYPE_11A;
662 		else
663 			bb_type = BB_TYPE_11G;
664 
665 		if (priv->bb_type != bb_type) {
666 			priv->bb_type = bb_type;
667 
668 			vnt_set_bss_mode(priv);
669 		}
670 	}
671 
672 	if (changed & IEEE80211_CONF_CHANGE_POWER) {
673 		if (priv->bb_type == BB_TYPE_11B)
674 			priv->current_rate = RATE_1M;
675 		else
676 			priv->current_rate = RATE_54M;
677 
678 		vnt_rf_setpower(priv, priv->current_rate,
679 				conf->chandef.chan->hw_value);
680 	}
681 
682 	return 0;
683 }
684 
685 static void vnt_bss_info_changed(struct ieee80211_hw *hw,
686 				 struct ieee80211_vif *vif,
687 				 struct ieee80211_bss_conf *conf, u32 changed)
688 {
689 	struct vnt_private *priv = hw->priv;
690 
691 	priv->current_aid = conf->aid;
692 
693 	if (changed & BSS_CHANGED_BSSID && conf->bssid)
694 		vnt_mac_set_bssid_addr(priv, (u8 *)conf->bssid);
695 
696 	if (changed & BSS_CHANGED_BASIC_RATES) {
697 		priv->basic_rates = conf->basic_rates;
698 
699 		vnt_update_top_rates(priv);
700 
701 		dev_dbg(&priv->usb->dev, "basic rates %x\n", conf->basic_rates);
702 	}
703 
704 	if (changed & BSS_CHANGED_ERP_PREAMBLE) {
705 		if (conf->use_short_preamble) {
706 			vnt_mac_enable_barker_preamble_mode(priv);
707 			priv->preamble_type = true;
708 		} else {
709 			vnt_mac_disable_barker_preamble_mode(priv);
710 			priv->preamble_type = false;
711 		}
712 	}
713 
714 	if (changed & BSS_CHANGED_ERP_CTS_PROT) {
715 		if (conf->use_cts_prot)
716 			vnt_mac_enable_protect_mode(priv);
717 		else
718 			vnt_mac_disable_protect_mode(priv);
719 	}
720 
721 	if (changed & BSS_CHANGED_ERP_SLOT) {
722 		if (conf->use_short_slot)
723 			priv->short_slot_time = true;
724 		else
725 			priv->short_slot_time = false;
726 
727 		vnt_set_short_slot_time(priv);
728 		vnt_set_vga_gain_offset(priv, priv->bb_vga[0]);
729 		vnt_update_pre_ed_threshold(priv, false);
730 	}
731 
732 	if (changed & BSS_CHANGED_TXPOWER)
733 		vnt_rf_setpower(priv, priv->current_rate,
734 				conf->chandef.chan->hw_value);
735 
736 	if (changed & BSS_CHANGED_BEACON_ENABLED) {
737 		dev_dbg(&priv->usb->dev,
738 			"Beacon enable %d\n", conf->enable_beacon);
739 
740 		if (conf->enable_beacon) {
741 			vnt_beacon_enable(priv, vif, conf);
742 
743 			vnt_mac_reg_bits_on(priv, MAC_REG_TCR, TCR_AUTOBCNTX);
744 		} else {
745 			vnt_mac_reg_bits_off(priv, MAC_REG_TCR, TCR_AUTOBCNTX);
746 		}
747 	}
748 
749 	if (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_BEACON_INFO) &&
750 	    priv->op_mode != NL80211_IFTYPE_AP) {
751 		if (conf->assoc && conf->beacon_rate) {
752 			vnt_mac_reg_bits_on(priv, MAC_REG_TFTCTL,
753 					    TFTCTL_TSFCNTREN);
754 
755 			vnt_adjust_tsf(priv, conf->beacon_rate->hw_value,
756 				       conf->sync_tsf, priv->current_tsf);
757 
758 			vnt_mac_set_beacon_interval(priv, conf->beacon_int);
759 
760 			vnt_reset_next_tbtt(priv, conf->beacon_int);
761 		} else {
762 			vnt_clear_current_tsf(priv);
763 
764 			vnt_mac_reg_bits_off(priv, MAC_REG_TFTCTL,
765 					     TFTCTL_TSFCNTREN);
766 		}
767 	}
768 }
769 
770 static u64 vnt_prepare_multicast(struct ieee80211_hw *hw,
771 				 struct netdev_hw_addr_list *mc_list)
772 {
773 	struct vnt_private *priv = hw->priv;
774 	struct netdev_hw_addr *ha;
775 	u64 mc_filter = 0;
776 	u32 bit_nr = 0;
777 
778 	netdev_hw_addr_list_for_each(ha, mc_list) {
779 		bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
780 
781 		mc_filter |= 1ULL << (bit_nr & 0x3f);
782 	}
783 
784 	priv->mc_list_count = mc_list->count;
785 
786 	return mc_filter;
787 }
788 
789 static void vnt_configure(struct ieee80211_hw *hw,
790 			  unsigned int changed_flags,
791 			  unsigned int *total_flags, u64 multicast)
792 {
793 	struct vnt_private *priv = hw->priv;
794 	u8 rx_mode = 0;
795 	int rc;
796 
797 	*total_flags &= FIF_ALLMULTI | FIF_OTHER_BSS | FIF_BCN_PRBRESP_PROMISC;
798 
799 	rc = vnt_control_in(priv, MESSAGE_TYPE_READ, MAC_REG_RCR,
800 			    MESSAGE_REQUEST_MACREG, sizeof(u8), &rx_mode);
801 
802 	if (!rc)
803 		rx_mode = RCR_MULTICAST | RCR_BROADCAST;
804 
805 	dev_dbg(&priv->usb->dev, "rx mode in = %x\n", rx_mode);
806 
807 	if (changed_flags & FIF_ALLMULTI) {
808 		if (*total_flags & FIF_ALLMULTI) {
809 			if (priv->mc_list_count > 2)
810 				vnt_mac_set_filter(priv, ~0);
811 			else
812 				vnt_mac_set_filter(priv, multicast);
813 
814 			rx_mode |= RCR_MULTICAST | RCR_BROADCAST;
815 		} else {
816 			rx_mode &= ~(RCR_MULTICAST | RCR_BROADCAST);
817 		}
818 	}
819 
820 	if (changed_flags & (FIF_OTHER_BSS | FIF_BCN_PRBRESP_PROMISC)) {
821 		if (*total_flags & (FIF_OTHER_BSS | FIF_BCN_PRBRESP_PROMISC))
822 			rx_mode &= ~RCR_BSSID;
823 		else
824 			rx_mode |= RCR_BSSID;
825 	}
826 
827 	vnt_control_out_u8(priv, MESSAGE_REQUEST_MACREG, MAC_REG_RCR, rx_mode);
828 
829 	dev_dbg(&priv->usb->dev, "rx mode out= %x\n", rx_mode);
830 }
831 
832 static int vnt_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
833 		       struct ieee80211_vif *vif, struct ieee80211_sta *sta,
834 		       struct ieee80211_key_conf *key)
835 {
836 	struct vnt_private *priv = hw->priv;
837 
838 	switch (cmd) {
839 	case SET_KEY:
840 		if (vnt_set_keys(hw, sta, vif, key))
841 			return -EOPNOTSUPP;
842 		break;
843 	case DISABLE_KEY:
844 		if (test_bit(key->hw_key_idx, &priv->key_entry_inuse))
845 			clear_bit(key->hw_key_idx, &priv->key_entry_inuse);
846 	default:
847 		break;
848 	}
849 
850 	return 0;
851 }
852 
853 static void vnt_sw_scan_start(struct ieee80211_hw *hw,
854 			      struct ieee80211_vif *vif,
855 			      const u8 *addr)
856 {
857 	struct vnt_private *priv = hw->priv;
858 
859 	vnt_set_bss_mode(priv);
860 	/* Set max sensitivity*/
861 	vnt_update_pre_ed_threshold(priv, true);
862 }
863 
864 static void vnt_sw_scan_complete(struct ieee80211_hw *hw,
865 				 struct ieee80211_vif *vif)
866 {
867 	struct vnt_private *priv = hw->priv;
868 
869 	/* Return sensitivity to channel level*/
870 	vnt_update_pre_ed_threshold(priv, false);
871 }
872 
873 static int vnt_get_stats(struct ieee80211_hw *hw,
874 			 struct ieee80211_low_level_stats *stats)
875 {
876 	struct vnt_private *priv = hw->priv;
877 
878 	memcpy(stats, &priv->low_stats, sizeof(*stats));
879 
880 	return 0;
881 }
882 
883 static u64 vnt_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
884 {
885 	struct vnt_private *priv = hw->priv;
886 
887 	return priv->current_tsf;
888 }
889 
890 static void vnt_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
891 			u64 tsf)
892 {
893 	struct vnt_private *priv = hw->priv;
894 
895 	vnt_update_next_tbtt(priv, tsf, vif->bss_conf.beacon_int);
896 }
897 
898 static void vnt_reset_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
899 {
900 	struct vnt_private *priv = hw->priv;
901 
902 	vnt_mac_reg_bits_off(priv, MAC_REG_TFTCTL, TFTCTL_TSFCNTREN);
903 
904 	vnt_clear_current_tsf(priv);
905 }
906 
907 static const struct ieee80211_ops vnt_mac_ops = {
908 	.tx			= vnt_tx_80211,
909 	.start			= vnt_start,
910 	.stop			= vnt_stop,
911 	.add_interface		= vnt_add_interface,
912 	.remove_interface	= vnt_remove_interface,
913 	.config			= vnt_config,
914 	.bss_info_changed	= vnt_bss_info_changed,
915 	.prepare_multicast	= vnt_prepare_multicast,
916 	.configure_filter	= vnt_configure,
917 	.set_key		= vnt_set_key,
918 	.sw_scan_start		= vnt_sw_scan_start,
919 	.sw_scan_complete	= vnt_sw_scan_complete,
920 	.get_stats		= vnt_get_stats,
921 	.get_tsf		= vnt_get_tsf,
922 	.set_tsf		= vnt_set_tsf,
923 	.reset_tsf		= vnt_reset_tsf,
924 };
925 
926 int vnt_init(struct vnt_private *priv)
927 {
928 	if (!(vnt_init_registers(priv)))
929 		return -EAGAIN;
930 
931 	SET_IEEE80211_PERM_ADDR(priv->hw, priv->permanent_net_addr);
932 
933 	vnt_init_bands(priv);
934 
935 	if (ieee80211_register_hw(priv->hw))
936 		return -ENODEV;
937 
938 	priv->mac_hw = true;
939 
940 	vnt_radio_power_off(priv);
941 
942 	return 0;
943 }
944 
945 static int
946 vt6656_probe(struct usb_interface *intf, const struct usb_device_id *id)
947 {
948 	struct usb_device *udev;
949 	struct vnt_private *priv;
950 	struct ieee80211_hw *hw;
951 	struct wiphy *wiphy;
952 	int rc = 0;
953 
954 	udev = usb_get_dev(interface_to_usbdev(intf));
955 
956 	dev_notice(&udev->dev, "%s Ver. %s\n",
957 		   DEVICE_FULL_DRV_NAM, DEVICE_VERSION);
958 	dev_notice(&udev->dev,
959 		   "Copyright (c) 2004 VIA Networking Technologies, Inc.\n");
960 
961 	hw = ieee80211_alloc_hw(sizeof(struct vnt_private), &vnt_mac_ops);
962 	if (!hw) {
963 		dev_err(&udev->dev, "could not register ieee80211_hw\n");
964 		rc = -ENOMEM;
965 		goto err_nomem;
966 	}
967 
968 	priv = hw->priv;
969 	priv->hw = hw;
970 	priv->usb = udev;
971 
972 	vnt_set_options(priv);
973 
974 	spin_lock_init(&priv->lock);
975 	mutex_init(&priv->usb_lock);
976 
977 	INIT_DELAYED_WORK(&priv->run_command_work, vnt_run_command);
978 
979 	usb_set_intfdata(intf, priv);
980 
981 	wiphy = priv->hw->wiphy;
982 
983 	wiphy->frag_threshold = FRAG_THRESH_DEF;
984 	wiphy->rts_threshold = RTS_THRESH_DEF;
985 	wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
986 		BIT(NL80211_IFTYPE_ADHOC) | BIT(NL80211_IFTYPE_AP);
987 
988 	ieee80211_hw_set(priv->hw, TIMING_BEACON_ONLY);
989 	ieee80211_hw_set(priv->hw, SIGNAL_DBM);
990 	ieee80211_hw_set(priv->hw, RX_INCLUDES_FCS);
991 	ieee80211_hw_set(priv->hw, REPORTS_TX_ACK_STATUS);
992 	ieee80211_hw_set(priv->hw, SUPPORTS_PS);
993 
994 	priv->hw->max_signal = 100;
995 
996 	SET_IEEE80211_DEV(priv->hw, &intf->dev);
997 
998 	usb_device_reset(priv);
999 
1000 	clear_bit(DEVICE_FLAGS_DISCONNECTED, &priv->flags);
1001 	vnt_reset_command_timer(priv);
1002 
1003 	vnt_schedule_command(priv, WLAN_CMD_INIT_MAC80211);
1004 
1005 	return 0;
1006 
1007 err_nomem:
1008 	usb_put_dev(udev);
1009 
1010 	return rc;
1011 }
1012 
1013 static void vt6656_disconnect(struct usb_interface *intf)
1014 {
1015 	struct vnt_private *priv = usb_get_intfdata(intf);
1016 
1017 	if (!priv)
1018 		return;
1019 
1020 	if (priv->mac_hw)
1021 		ieee80211_unregister_hw(priv->hw);
1022 
1023 	usb_set_intfdata(intf, NULL);
1024 	usb_put_dev(interface_to_usbdev(intf));
1025 
1026 	set_bit(DEVICE_FLAGS_UNPLUG, &priv->flags);
1027 
1028 	ieee80211_free_hw(priv->hw);
1029 }
1030 
1031 #ifdef CONFIG_PM
1032 
1033 static int vt6656_suspend(struct usb_interface *intf, pm_message_t message)
1034 {
1035 	return 0;
1036 }
1037 
1038 static int vt6656_resume(struct usb_interface *intf)
1039 {
1040 	return 0;
1041 }
1042 
1043 #endif /* CONFIG_PM */
1044 
1045 MODULE_DEVICE_TABLE(usb, vt6656_table);
1046 
1047 static struct usb_driver vt6656_driver = {
1048 	.name =		DEVICE_NAME,
1049 	.probe =	vt6656_probe,
1050 	.disconnect =	vt6656_disconnect,
1051 	.id_table =	vt6656_table,
1052 #ifdef CONFIG_PM
1053 	.suspend = vt6656_suspend,
1054 	.resume = vt6656_resume,
1055 #endif /* CONFIG_PM */
1056 };
1057 
1058 module_usb_driver(vt6656_driver);
1059