wlcore, wl1251: fix spelling: "Couldnt" -> "Couldn't" and remove error on -ENOMEM
[linux-2.6/btrfs-unstable.git] / drivers / staging / vt6656 / main_usb.c
blobcc6d8778fe5ba89220dd60e5b878a7bf5dc7c5f6
1 /*
2 * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
3 * All rights reserved.
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.
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.
16 * File: main_usb.c
18 * Purpose: driver entry for initial, open, close, tx and rx.
20 * Author: Lyndon Chen
22 * Date: Dec 8, 2005
24 * Functions:
26 * vt6656_probe - module initial (insmod) driver entry
27 * vnt_free_tx_bufs - free tx buffer function
28 * vnt_init_registers- initial MAC & BBP & RF internal registers.
30 * Revision History:
32 #undef __NO_VERSION__
34 #include <linux/etherdevice.h>
35 #include <linux/file.h>
36 #include "device.h"
37 #include "card.h"
38 #include "baseband.h"
39 #include "mac.h"
40 #include "power.h"
41 #include "wcmd.h"
42 #include "rxtx.h"
43 #include "dpc.h"
44 #include "rf.h"
45 #include "firmware.h"
46 #include "usbpipe.h"
47 #include "channel.h"
48 #include "int.h"
51 * define module options
54 /* version information */
55 #define DRIVER_AUTHOR \
56 "VIA Networking Technologies, Inc., <lyndonchen@vntek.com.tw>"
57 MODULE_AUTHOR(DRIVER_AUTHOR);
58 MODULE_LICENSE("GPL");
59 MODULE_DESCRIPTION(DEVICE_FULL_DRV_NAM);
61 #define RX_DESC_DEF0 64
62 static int vnt_rx_buffers = RX_DESC_DEF0;
63 module_param_named(rx_buffers, vnt_rx_buffers, int, 0644);
64 MODULE_PARM_DESC(rx_buffers, "Number of receive usb rx buffers");
66 #define TX_DESC_DEF0 64
67 static int vnt_tx_buffers = TX_DESC_DEF0;
68 module_param_named(tx_buffers, vnt_tx_buffers, int, 0644);
69 MODULE_PARM_DESC(tx_buffers, "Number of receive usb tx buffers");
71 #define RTS_THRESH_DEF 2347
72 #define FRAG_THRESH_DEF 2346
73 #define SHORT_RETRY_DEF 8
74 #define LONG_RETRY_DEF 4
76 /* BasebandType[] baseband type selected
77 * 0: indicate 802.11a type
78 * 1: indicate 802.11b type
79 * 2: indicate 802.11g type
82 #define BBP_TYPE_DEF 2
85 * Static vars definitions
88 static const struct usb_device_id vt6656_table[] = {
89 {USB_DEVICE(VNT_USB_VENDOR_ID, VNT_USB_PRODUCT_ID)},
93 static void vnt_set_options(struct vnt_private *priv)
95 /* Set number of TX buffers */
96 if (vnt_tx_buffers < CB_MIN_TX_DESC || vnt_tx_buffers > CB_MAX_TX_DESC)
97 priv->num_tx_context = TX_DESC_DEF0;
98 else
99 priv->num_tx_context = vnt_tx_buffers;
101 /* Set number of RX buffers */
102 if (vnt_rx_buffers < CB_MIN_RX_DESC || vnt_rx_buffers > CB_MAX_RX_DESC)
103 priv->num_rcb = RX_DESC_DEF0;
104 else
105 priv->num_rcb = vnt_rx_buffers;
107 priv->short_retry_limit = SHORT_RETRY_DEF;
108 priv->long_retry_limit = LONG_RETRY_DEF;
109 priv->op_mode = NL80211_IFTYPE_UNSPECIFIED;
110 priv->bb_type = BBP_TYPE_DEF;
111 priv->packet_type = priv->bb_type;
112 priv->auto_fb_ctrl = AUTO_FB_0;
113 priv->preamble_type = 0;
114 priv->exist_sw_net_addr = false;
118 * initialization of MAC & BBP registers
120 static int vnt_init_registers(struct vnt_private *priv)
122 struct vnt_cmd_card_init *init_cmd = &priv->init_command;
123 struct vnt_rsp_card_init *init_rsp = &priv->init_response;
124 u8 antenna;
125 int ii;
126 int status = STATUS_SUCCESS;
127 u8 tmp;
128 u8 calib_tx_iq = 0, calib_tx_dc = 0, calib_rx_iq = 0;
130 dev_dbg(&priv->usb->dev, "---->INIbInitAdapter. [%d][%d]\n",
131 DEVICE_INIT_COLD, priv->packet_type);
133 if (!vnt_check_firmware_version(priv)) {
134 if (vnt_download_firmware(priv) == true) {
135 if (vnt_firmware_branch_to_sram(priv) == false) {
136 dev_dbg(&priv->usb->dev,
137 " vnt_firmware_branch_to_sram fail\n");
138 return false;
140 } else {
141 dev_dbg(&priv->usb->dev, "FIRMWAREbDownload fail\n");
142 return false;
146 if (!vnt_vt3184_init(priv)) {
147 dev_dbg(&priv->usb->dev, "vnt_vt3184_init fail\n");
148 return false;
151 init_cmd->init_class = DEVICE_INIT_COLD;
152 init_cmd->exist_sw_net_addr = priv->exist_sw_net_addr;
153 for (ii = 0; ii < 6; ii++)
154 init_cmd->sw_net_addr[ii] = priv->current_net_addr[ii];
155 init_cmd->short_retry_limit = priv->short_retry_limit;
156 init_cmd->long_retry_limit = priv->long_retry_limit;
158 /* issue card_init command to device */
159 status = vnt_control_out(priv, MESSAGE_TYPE_CARDINIT, 0, 0,
160 sizeof(struct vnt_cmd_card_init),
161 (u8 *)init_cmd);
162 if (status != STATUS_SUCCESS) {
163 dev_dbg(&priv->usb->dev, "Issue Card init fail\n");
164 return false;
167 status = vnt_control_in(priv, MESSAGE_TYPE_INIT_RSP, 0, 0,
168 sizeof(struct vnt_rsp_card_init),
169 (u8 *)init_rsp);
170 if (status != STATUS_SUCCESS) {
171 dev_dbg(&priv->usb->dev,
172 "Cardinit request in status fail!\n");
173 return false;
176 /* local ID for AES functions */
177 status = vnt_control_in(priv, MESSAGE_TYPE_READ, MAC_REG_LOCALID,
178 MESSAGE_REQUEST_MACREG, 1, &priv->local_id);
179 if (status != STATUS_SUCCESS)
180 return false;
182 /* do MACbSoftwareReset in MACvInitialize */
184 priv->top_ofdm_basic_rate = RATE_24M;
185 priv->top_cck_basic_rate = RATE_1M;
187 /* target to IF pin while programming to RF chip */
188 priv->power = 0xFF;
190 priv->cck_pwr = priv->eeprom[EEP_OFS_PWR_CCK];
191 priv->ofdm_pwr_g = priv->eeprom[EEP_OFS_PWR_OFDMG];
192 /* load power table */
193 for (ii = 0; ii < 14; ii++) {
194 priv->cck_pwr_tbl[ii] =
195 priv->eeprom[ii + EEP_OFS_CCK_PWR_TBL];
196 if (priv->cck_pwr_tbl[ii] == 0)
197 priv->cck_pwr_tbl[ii] = priv->cck_pwr;
199 priv->ofdm_pwr_tbl[ii] =
200 priv->eeprom[ii + EEP_OFS_OFDM_PWR_TBL];
201 if (priv->ofdm_pwr_tbl[ii] == 0)
202 priv->ofdm_pwr_tbl[ii] = priv->ofdm_pwr_g;
206 * original zonetype is USA, but custom zonetype is Europe,
207 * then need to recover 12, 13, 14 channels with 11 channel
209 for (ii = 11; ii < 14; ii++) {
210 priv->cck_pwr_tbl[ii] = priv->cck_pwr_tbl[10];
211 priv->ofdm_pwr_tbl[ii] = priv->ofdm_pwr_tbl[10];
214 priv->ofdm_pwr_a = 0x34; /* same as RFbMA2829SelectChannel */
216 /* load OFDM A power table */
217 for (ii = 0; ii < CB_MAX_CHANNEL_5G; ii++) {
218 priv->ofdm_a_pwr_tbl[ii] =
219 priv->eeprom[ii + EEP_OFS_OFDMA_PWR_TBL];
221 if (priv->ofdm_a_pwr_tbl[ii] == 0)
222 priv->ofdm_a_pwr_tbl[ii] = priv->ofdm_pwr_a;
225 antenna = priv->eeprom[EEP_OFS_ANTENNA];
227 if (antenna & EEP_ANTINV)
228 priv->tx_rx_ant_inv = true;
229 else
230 priv->tx_rx_ant_inv = false;
232 antenna &= (EEP_ANTENNA_AUX | EEP_ANTENNA_MAIN);
234 if (antenna == 0) /* if not set default is both */
235 antenna = (EEP_ANTENNA_AUX | EEP_ANTENNA_MAIN);
237 if (antenna == (EEP_ANTENNA_AUX | EEP_ANTENNA_MAIN)) {
238 priv->tx_antenna_mode = ANT_B;
239 priv->rx_antenna_sel = 1;
241 if (priv->tx_rx_ant_inv)
242 priv->rx_antenna_mode = ANT_A;
243 else
244 priv->rx_antenna_mode = ANT_B;
245 } else {
246 priv->rx_antenna_sel = 0;
248 if (antenna & EEP_ANTENNA_AUX) {
249 priv->tx_antenna_mode = ANT_A;
251 if (priv->tx_rx_ant_inv)
252 priv->rx_antenna_mode = ANT_B;
253 else
254 priv->rx_antenna_mode = ANT_A;
255 } else {
256 priv->tx_antenna_mode = ANT_B;
258 if (priv->tx_rx_ant_inv)
259 priv->rx_antenna_mode = ANT_A;
260 else
261 priv->rx_antenna_mode = ANT_B;
265 /* Set initial antenna mode */
266 vnt_set_antenna_mode(priv, priv->rx_antenna_mode);
268 /* get Auto Fall Back type */
269 priv->auto_fb_ctrl = AUTO_FB_0;
271 /* default Auto Mode */
272 priv->bb_type = BB_TYPE_11G;
274 /* get RFType */
275 priv->rf_type = init_rsp->rf_type;
277 /* load vt3266 calibration parameters in EEPROM */
278 if (priv->rf_type == RF_VT3226D0) {
279 if ((priv->eeprom[EEP_OFS_MAJOR_VER] == 0x1) &&
280 (priv->eeprom[EEP_OFS_MINOR_VER] >= 0x4)) {
281 calib_tx_iq = priv->eeprom[EEP_OFS_CALIB_TX_IQ];
282 calib_tx_dc = priv->eeprom[EEP_OFS_CALIB_TX_DC];
283 calib_rx_iq = priv->eeprom[EEP_OFS_CALIB_RX_IQ];
284 if (calib_tx_iq || calib_tx_dc || calib_rx_iq) {
285 /* CR255, enable TX/RX IQ and
286 * DC compensation mode
288 vnt_control_out_u8(priv,
289 MESSAGE_REQUEST_BBREG,
290 0xff,
291 0x03);
292 /* CR251, TX I/Q Imbalance Calibration */
293 vnt_control_out_u8(priv,
294 MESSAGE_REQUEST_BBREG,
295 0xfb,
296 calib_tx_iq);
297 /* CR252, TX DC-Offset Calibration */
298 vnt_control_out_u8(priv,
299 MESSAGE_REQUEST_BBREG,
300 0xfC,
301 calib_tx_dc);
302 /* CR253, RX I/Q Imbalance Calibration */
303 vnt_control_out_u8(priv,
304 MESSAGE_REQUEST_BBREG,
305 0xfd,
306 calib_rx_iq);
307 } else {
308 /* CR255, turn off
309 * BB Calibration compensation
311 vnt_control_out_u8(priv,
312 MESSAGE_REQUEST_BBREG,
313 0xff,
314 0x0);
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);
323 /* if exist SW network address, use it */
324 dev_dbg(&priv->usb->dev, "Network address = %pM\n",
325 priv->current_net_addr);
328 * set BB and packet type at the same time
329 * set Short Slot Time, xIFS, and RSPINF
331 if (priv->bb_type == BB_TYPE_11A)
332 priv->short_slot_time = true;
333 else
334 priv->short_slot_time = false;
336 vnt_set_short_slot_time(priv);
338 priv->radio_ctl = priv->eeprom[EEP_OFS_RADIOCTL];
340 if ((priv->radio_ctl & EEP_RADIOCTL_ENABLE) != 0) {
341 status = vnt_control_in(priv, MESSAGE_TYPE_READ,
342 MAC_REG_GPIOCTL1,
343 MESSAGE_REQUEST_MACREG, 1, &tmp);
345 if (status != STATUS_SUCCESS)
346 return false;
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);
356 vnt_mac_set_led(priv, LEDSTS_TMLEN, 0x38);
358 vnt_mac_set_led(priv, LEDSTS_STS, LEDSTS_SLOW);
360 vnt_mac_reg_bits_on(priv, MAC_REG_GPIOCTL0, 0x01);
362 vnt_radio_power_on(priv);
364 dev_dbg(&priv->usb->dev, "<----INIbInitAdapter Exit\n");
366 return true;
369 static void vnt_free_tx_bufs(struct vnt_private *priv)
371 struct vnt_usb_send_context *tx_context;
372 int ii;
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);
382 kfree(tx_context);
386 static void vnt_free_rx_bufs(struct vnt_private *priv)
388 struct vnt_rcb *rcb;
389 int ii;
391 for (ii = 0; ii < priv->num_rcb; ii++) {
392 rcb = priv->rcb[ii];
393 if (!rcb)
394 continue;
396 /* deallocate URBs */
397 if (rcb->urb) {
398 usb_kill_urb(rcb->urb);
399 usb_free_urb(rcb->urb);
402 /* deallocate skb */
403 if (rcb->skb)
404 dev_kfree_skb(rcb->skb);
406 kfree(rcb);
410 static void vnt_free_int_bufs(struct vnt_private *priv)
412 kfree(priv->int_buf.data_buf);
415 static bool vnt_alloc_bufs(struct vnt_private *priv)
417 struct vnt_usb_send_context *tx_context;
418 struct vnt_rcb *rcb;
419 int ii;
421 for (ii = 0; ii < priv->num_tx_context; ii++) {
422 tx_context = kmalloc(sizeof(*tx_context), GFP_KERNEL);
423 if (!tx_context)
424 goto free_tx;
426 priv->tx_context[ii] = tx_context;
427 tx_context->priv = priv;
428 tx_context->pkt_no = ii;
430 /* allocate URBs */
431 tx_context->urb = usb_alloc_urb(0, GFP_KERNEL);
432 if (!tx_context->urb)
433 goto free_tx;
435 tx_context->in_use = false;
438 for (ii = 0; ii < priv->num_rcb; ii++) {
439 priv->rcb[ii] = kzalloc(sizeof(*priv->rcb[ii]), GFP_KERNEL);
440 if (!priv->rcb[ii]) {
441 dev_err(&priv->usb->dev,
442 "failed to allocate rcb no %d\n", ii);
443 goto free_rx_tx;
446 rcb = priv->rcb[ii];
448 rcb->priv = priv;
450 /* allocate URBs */
451 rcb->urb = usb_alloc_urb(0, GFP_KERNEL);
452 if (!rcb->urb)
453 goto free_rx_tx;
455 rcb->skb = dev_alloc_skb(priv->rx_buf_sz);
456 if (!rcb->skb)
457 goto free_rx_tx;
459 rcb->in_use = false;
461 /* submit rx urb */
462 if (vnt_submit_rx_urb(priv, rcb))
463 goto free_rx_tx;
466 priv->interrupt_urb = usb_alloc_urb(0, GFP_KERNEL);
467 if (!priv->interrupt_urb)
468 goto free_rx_tx;
470 priv->int_buf.data_buf = kmalloc(MAX_INTERRUPT_SIZE, GFP_KERNEL);
471 if (!priv->int_buf.data_buf) {
472 usb_free_urb(priv->interrupt_urb);
473 goto free_rx_tx;
476 return true;
478 free_rx_tx:
479 vnt_free_rx_bufs(priv);
481 free_tx:
482 vnt_free_tx_bufs(priv);
484 return false;
487 static void vnt_tx_80211(struct ieee80211_hw *hw,
488 struct ieee80211_tx_control *control,
489 struct sk_buff *skb)
491 struct vnt_private *priv = hw->priv;
493 if (vnt_tx_packet(priv, skb))
494 ieee80211_free_txskb(hw, skb);
497 static int vnt_start(struct ieee80211_hw *hw)
499 struct vnt_private *priv = hw->priv;
501 priv->rx_buf_sz = MAX_TOTAL_SIZE_WITH_ALL_HEADERS;
503 if (!vnt_alloc_bufs(priv)) {
504 dev_dbg(&priv->usb->dev, "vnt_alloc_bufs fail...\n");
505 return -ENOMEM;
508 clear_bit(DEVICE_FLAGS_DISCONNECTED, &priv->flags);
510 if (vnt_init_registers(priv) == false) {
511 dev_dbg(&priv->usb->dev, " init register fail\n");
512 goto free_all;
515 if (vnt_key_init_table(priv))
516 goto free_all;
518 priv->int_interval = 1; /* bInterval is set to 1 */
520 vnt_int_start_interrupt(priv);
522 ieee80211_wake_queues(hw);
524 return 0;
526 free_all:
527 vnt_free_rx_bufs(priv);
528 vnt_free_tx_bufs(priv);
529 vnt_free_int_bufs(priv);
531 usb_kill_urb(priv->interrupt_urb);
532 usb_free_urb(priv->interrupt_urb);
534 return -ENOMEM;
537 static void vnt_stop(struct ieee80211_hw *hw)
539 struct vnt_private *priv = hw->priv;
540 int i;
542 if (!priv)
543 return;
545 for (i = 0; i < MAX_KEY_TABLE; i++)
546 vnt_mac_disable_keyentry(priv, i);
548 /* clear all keys */
549 priv->key_entry_inuse = 0;
551 if (!test_bit(DEVICE_FLAGS_UNPLUG, &priv->flags))
552 vnt_mac_shutdown(priv);
554 ieee80211_stop_queues(hw);
556 set_bit(DEVICE_FLAGS_DISCONNECTED, &priv->flags);
558 cancel_delayed_work_sync(&priv->run_command_work);
560 priv->cmd_running = false;
562 vnt_free_tx_bufs(priv);
563 vnt_free_rx_bufs(priv);
564 vnt_free_int_bufs(priv);
566 usb_kill_urb(priv->interrupt_urb);
567 usb_free_urb(priv->interrupt_urb);
570 static int vnt_add_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
572 struct vnt_private *priv = hw->priv;
574 priv->vif = vif;
576 switch (vif->type) {
577 case NL80211_IFTYPE_STATION:
578 break;
579 case NL80211_IFTYPE_ADHOC:
580 vnt_mac_reg_bits_off(priv, MAC_REG_RCR, RCR_UNICAST);
582 vnt_mac_reg_bits_on(priv, MAC_REG_HOSTCR, HOSTCR_ADHOC);
584 break;
585 case NL80211_IFTYPE_AP:
586 vnt_mac_reg_bits_off(priv, MAC_REG_RCR, RCR_UNICAST);
588 vnt_mac_reg_bits_on(priv, MAC_REG_HOSTCR, HOSTCR_AP);
590 break;
591 default:
592 return -EOPNOTSUPP;
595 priv->op_mode = vif->type;
597 vnt_set_bss_mode(priv);
599 /* LED blink on TX */
600 vnt_mac_set_led(priv, LEDSTS_STS, LEDSTS_INTER);
602 return 0;
605 static void vnt_remove_interface(struct ieee80211_hw *hw,
606 struct ieee80211_vif *vif)
608 struct vnt_private *priv = hw->priv;
610 switch (vif->type) {
611 case NL80211_IFTYPE_STATION:
612 break;
613 case NL80211_IFTYPE_ADHOC:
614 vnt_mac_reg_bits_off(priv, MAC_REG_TCR, TCR_AUTOBCNTX);
615 vnt_mac_reg_bits_off(priv, MAC_REG_TFTCTL, TFTCTL_TSFCNTREN);
616 vnt_mac_reg_bits_off(priv, MAC_REG_HOSTCR, HOSTCR_ADHOC);
617 break;
618 case NL80211_IFTYPE_AP:
619 vnt_mac_reg_bits_off(priv, MAC_REG_TCR, TCR_AUTOBCNTX);
620 vnt_mac_reg_bits_off(priv, MAC_REG_TFTCTL, TFTCTL_TSFCNTREN);
621 vnt_mac_reg_bits_off(priv, MAC_REG_HOSTCR, HOSTCR_AP);
622 break;
623 default:
624 break;
627 vnt_radio_power_off(priv);
629 priv->op_mode = NL80211_IFTYPE_UNSPECIFIED;
631 /* LED slow blink */
632 vnt_mac_set_led(priv, LEDSTS_STS, LEDSTS_SLOW);
635 static int vnt_config(struct ieee80211_hw *hw, u32 changed)
637 struct vnt_private *priv = hw->priv;
638 struct ieee80211_conf *conf = &hw->conf;
640 if (changed & IEEE80211_CONF_CHANGE_PS) {
641 if (conf->flags & IEEE80211_CONF_PS)
642 vnt_enable_power_saving(priv, conf->listen_interval);
643 else
644 vnt_disable_power_saving(priv);
647 if ((changed & IEEE80211_CONF_CHANGE_CHANNEL) ||
648 (conf->flags & IEEE80211_CONF_OFFCHANNEL)) {
649 vnt_set_channel(priv, conf->chandef.chan->hw_value);
651 if (conf->chandef.chan->band == NL80211_BAND_5GHZ)
652 priv->bb_type = BB_TYPE_11A;
653 else
654 priv->bb_type = BB_TYPE_11G;
657 if (changed & IEEE80211_CONF_CHANGE_POWER) {
658 if (priv->bb_type == BB_TYPE_11B)
659 priv->current_rate = RATE_1M;
660 else
661 priv->current_rate = RATE_54M;
663 vnt_rf_setpower(priv, priv->current_rate,
664 conf->chandef.chan->hw_value);
667 return 0;
670 static void vnt_bss_info_changed(struct ieee80211_hw *hw,
671 struct ieee80211_vif *vif,
672 struct ieee80211_bss_conf *conf, u32 changed)
674 struct vnt_private *priv = hw->priv;
676 priv->current_aid = conf->aid;
678 if (changed & BSS_CHANGED_BSSID && conf->bssid)
679 vnt_mac_set_bssid_addr(priv, (u8 *)conf->bssid);
681 if (changed & BSS_CHANGED_BASIC_RATES) {
682 priv->basic_rates = conf->basic_rates;
684 vnt_update_top_rates(priv);
685 vnt_set_bss_mode(priv);
687 dev_dbg(&priv->usb->dev, "basic rates %x\n", conf->basic_rates);
690 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
691 if (conf->use_short_preamble) {
692 vnt_mac_enable_barker_preamble_mode(priv);
693 priv->preamble_type = true;
694 } else {
695 vnt_mac_disable_barker_preamble_mode(priv);
696 priv->preamble_type = false;
700 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
701 if (conf->use_cts_prot)
702 vnt_mac_enable_protect_mode(priv);
703 else
704 vnt_mac_disable_protect_mode(priv);
707 if (changed & BSS_CHANGED_ERP_SLOT) {
708 if (conf->use_short_slot)
709 priv->short_slot_time = true;
710 else
711 priv->short_slot_time = false;
713 vnt_set_short_slot_time(priv);
714 vnt_update_ifs(priv);
715 vnt_set_vga_gain_offset(priv, priv->bb_vga[0]);
716 vnt_update_pre_ed_threshold(priv, false);
719 if (changed & BSS_CHANGED_TXPOWER)
720 vnt_rf_setpower(priv, priv->current_rate,
721 conf->chandef.chan->hw_value);
723 if (changed & BSS_CHANGED_BEACON_ENABLED) {
724 dev_dbg(&priv->usb->dev,
725 "Beacon enable %d\n", conf->enable_beacon);
727 if (conf->enable_beacon) {
728 vnt_beacon_enable(priv, vif, conf);
730 vnt_mac_reg_bits_on(priv, MAC_REG_TCR, TCR_AUTOBCNTX);
731 } else {
732 vnt_mac_reg_bits_off(priv, MAC_REG_TCR, TCR_AUTOBCNTX);
736 if (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_BEACON_INFO) &&
737 priv->op_mode != NL80211_IFTYPE_AP) {
738 if (conf->assoc && conf->beacon_rate) {
739 vnt_mac_reg_bits_on(priv, MAC_REG_TFTCTL,
740 TFTCTL_TSFCNTREN);
742 vnt_adjust_tsf(priv, conf->beacon_rate->hw_value,
743 conf->sync_tsf, priv->current_tsf);
745 vnt_mac_set_beacon_interval(priv, conf->beacon_int);
747 vnt_reset_next_tbtt(priv, conf->beacon_int);
748 } else {
749 vnt_clear_current_tsf(priv);
751 vnt_mac_reg_bits_off(priv, MAC_REG_TFTCTL,
752 TFTCTL_TSFCNTREN);
757 static u64 vnt_prepare_multicast(struct ieee80211_hw *hw,
758 struct netdev_hw_addr_list *mc_list)
760 struct vnt_private *priv = hw->priv;
761 struct netdev_hw_addr *ha;
762 u64 mc_filter = 0;
763 u32 bit_nr = 0;
765 netdev_hw_addr_list_for_each(ha, mc_list) {
766 bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
768 mc_filter |= 1ULL << (bit_nr & 0x3f);
771 priv->mc_list_count = mc_list->count;
773 return mc_filter;
776 static void vnt_configure(struct ieee80211_hw *hw,
777 unsigned int changed_flags,
778 unsigned int *total_flags, u64 multicast)
780 struct vnt_private *priv = hw->priv;
781 u8 rx_mode = 0;
782 int rc;
784 *total_flags &= FIF_ALLMULTI | FIF_OTHER_BSS | FIF_BCN_PRBRESP_PROMISC;
786 rc = vnt_control_in(priv, MESSAGE_TYPE_READ, MAC_REG_RCR,
787 MESSAGE_REQUEST_MACREG, sizeof(u8), &rx_mode);
789 if (!rc)
790 rx_mode = RCR_MULTICAST | RCR_BROADCAST;
792 dev_dbg(&priv->usb->dev, "rx mode in = %x\n", rx_mode);
794 if (changed_flags & FIF_ALLMULTI) {
795 if (*total_flags & FIF_ALLMULTI) {
796 if (priv->mc_list_count > 2)
797 vnt_mac_set_filter(priv, ~0);
798 else
799 vnt_mac_set_filter(priv, multicast);
801 rx_mode |= RCR_MULTICAST | RCR_BROADCAST;
802 } else {
803 rx_mode &= ~(RCR_MULTICAST | RCR_BROADCAST);
807 if (changed_flags & (FIF_OTHER_BSS | FIF_BCN_PRBRESP_PROMISC)) {
808 if (*total_flags & (FIF_OTHER_BSS | FIF_BCN_PRBRESP_PROMISC))
809 rx_mode &= ~RCR_BSSID;
810 else
811 rx_mode |= RCR_BSSID;
814 vnt_control_out_u8(priv, MESSAGE_REQUEST_MACREG, MAC_REG_RCR, rx_mode);
816 dev_dbg(&priv->usb->dev, "rx mode out= %x\n", rx_mode);
819 static int vnt_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
820 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
821 struct ieee80211_key_conf *key)
823 struct vnt_private *priv = hw->priv;
825 switch (cmd) {
826 case SET_KEY:
827 if (vnt_set_keys(hw, sta, vif, key))
828 return -EOPNOTSUPP;
829 break;
830 case DISABLE_KEY:
831 if (test_bit(key->hw_key_idx, &priv->key_entry_inuse))
832 clear_bit(key->hw_key_idx, &priv->key_entry_inuse);
833 default:
834 break;
837 return 0;
840 static void vnt_sw_scan_start(struct ieee80211_hw *hw,
841 struct ieee80211_vif *vif,
842 const u8 *addr)
844 struct vnt_private *priv = hw->priv;
846 /* Set max sensitivity*/
847 vnt_update_pre_ed_threshold(priv, true);
850 static void vnt_sw_scan_complete(struct ieee80211_hw *hw,
851 struct ieee80211_vif *vif)
853 struct vnt_private *priv = hw->priv;
855 /* Return sensitivity to channel level*/
856 vnt_update_pre_ed_threshold(priv, false);
859 static int vnt_get_stats(struct ieee80211_hw *hw,
860 struct ieee80211_low_level_stats *stats)
862 struct vnt_private *priv = hw->priv;
864 memcpy(stats, &priv->low_stats, sizeof(*stats));
866 return 0;
869 static u64 vnt_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
871 struct vnt_private *priv = hw->priv;
873 return priv->current_tsf;
876 static void vnt_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
877 u64 tsf)
879 struct vnt_private *priv = hw->priv;
881 vnt_update_next_tbtt(priv, tsf, vif->bss_conf.beacon_int);
884 static void vnt_reset_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
886 struct vnt_private *priv = hw->priv;
888 vnt_mac_reg_bits_off(priv, MAC_REG_TFTCTL, TFTCTL_TSFCNTREN);
890 vnt_clear_current_tsf(priv);
893 static const struct ieee80211_ops vnt_mac_ops = {
894 .tx = vnt_tx_80211,
895 .start = vnt_start,
896 .stop = vnt_stop,
897 .add_interface = vnt_add_interface,
898 .remove_interface = vnt_remove_interface,
899 .config = vnt_config,
900 .bss_info_changed = vnt_bss_info_changed,
901 .prepare_multicast = vnt_prepare_multicast,
902 .configure_filter = vnt_configure,
903 .set_key = vnt_set_key,
904 .sw_scan_start = vnt_sw_scan_start,
905 .sw_scan_complete = vnt_sw_scan_complete,
906 .get_stats = vnt_get_stats,
907 .get_tsf = vnt_get_tsf,
908 .set_tsf = vnt_set_tsf,
909 .reset_tsf = vnt_reset_tsf,
912 int vnt_init(struct vnt_private *priv)
914 if (!(vnt_init_registers(priv)))
915 return -EAGAIN;
917 SET_IEEE80211_PERM_ADDR(priv->hw, priv->permanent_net_addr);
919 vnt_init_bands(priv);
921 if (ieee80211_register_hw(priv->hw))
922 return -ENODEV;
924 priv->mac_hw = true;
926 vnt_radio_power_off(priv);
928 return 0;
931 static int
932 vt6656_probe(struct usb_interface *intf, const struct usb_device_id *id)
934 struct usb_device *udev;
935 struct vnt_private *priv;
936 struct ieee80211_hw *hw;
937 struct wiphy *wiphy;
938 int rc = 0;
940 udev = usb_get_dev(interface_to_usbdev(intf));
942 dev_notice(&udev->dev, "%s Ver. %s\n",
943 DEVICE_FULL_DRV_NAM, DEVICE_VERSION);
944 dev_notice(&udev->dev,
945 "Copyright (c) 2004 VIA Networking Technologies, Inc.\n");
947 hw = ieee80211_alloc_hw(sizeof(struct vnt_private), &vnt_mac_ops);
948 if (!hw) {
949 dev_err(&udev->dev, "could not register ieee80211_hw\n");
950 rc = -ENOMEM;
951 goto err_nomem;
954 priv = hw->priv;
955 priv->hw = hw;
956 priv->usb = udev;
958 vnt_set_options(priv);
960 spin_lock_init(&priv->lock);
961 mutex_init(&priv->usb_lock);
963 INIT_DELAYED_WORK(&priv->run_command_work, vnt_run_command);
965 usb_set_intfdata(intf, priv);
967 wiphy = priv->hw->wiphy;
969 wiphy->frag_threshold = FRAG_THRESH_DEF;
970 wiphy->rts_threshold = RTS_THRESH_DEF;
971 wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
972 BIT(NL80211_IFTYPE_ADHOC) | BIT(NL80211_IFTYPE_AP);
974 ieee80211_hw_set(priv->hw, TIMING_BEACON_ONLY);
975 ieee80211_hw_set(priv->hw, SIGNAL_DBM);
976 ieee80211_hw_set(priv->hw, RX_INCLUDES_FCS);
977 ieee80211_hw_set(priv->hw, REPORTS_TX_ACK_STATUS);
978 ieee80211_hw_set(priv->hw, SUPPORTS_PS);
980 priv->hw->max_signal = 100;
982 SET_IEEE80211_DEV(priv->hw, &intf->dev);
984 rc = usb_reset_device(priv->usb);
985 if (rc)
986 dev_warn(&priv->usb->dev,
987 "%s reset fail status=%d\n", __func__, rc);
989 clear_bit(DEVICE_FLAGS_DISCONNECTED, &priv->flags);
990 vnt_reset_command_timer(priv);
992 vnt_schedule_command(priv, WLAN_CMD_INIT_MAC80211);
994 return 0;
996 err_nomem:
997 usb_put_dev(udev);
999 return rc;
1002 static void vt6656_disconnect(struct usb_interface *intf)
1004 struct vnt_private *priv = usb_get_intfdata(intf);
1006 if (!priv)
1007 return;
1009 if (priv->mac_hw)
1010 ieee80211_unregister_hw(priv->hw);
1012 usb_set_intfdata(intf, NULL);
1013 usb_put_dev(interface_to_usbdev(intf));
1015 set_bit(DEVICE_FLAGS_UNPLUG, &priv->flags);
1017 ieee80211_free_hw(priv->hw);
1020 #ifdef CONFIG_PM
1022 static int vt6656_suspend(struct usb_interface *intf, pm_message_t message)
1024 return 0;
1027 static int vt6656_resume(struct usb_interface *intf)
1029 return 0;
1032 #endif /* CONFIG_PM */
1034 MODULE_DEVICE_TABLE(usb, vt6656_table);
1036 static struct usb_driver vt6656_driver = {
1037 .name = DEVICE_NAME,
1038 .probe = vt6656_probe,
1039 .disconnect = vt6656_disconnect,
1040 .id_table = vt6656_table,
1041 #ifdef CONFIG_PM
1042 .suspend = vt6656_suspend,
1043 .resume = vt6656_resume,
1044 #endif /* CONFIG_PM */
1047 module_usb_driver(vt6656_driver);