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