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