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