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