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