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