Merge branch 'ieee802154-for-davem-2019-08-24' of git://git.kernel.org/pub/scm/linux...
[sfrench/cifs-2.6.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         vnt_set_bss_mode(priv);
637
638         /* LED blink on TX */
639         vnt_mac_set_led(priv, LEDSTS_STS, LEDSTS_INTER);
640
641         return 0;
642 }
643
644 static void vnt_remove_interface(struct ieee80211_hw *hw,
645                                  struct ieee80211_vif *vif)
646 {
647         struct vnt_private *priv = hw->priv;
648
649         switch (vif->type) {
650         case NL80211_IFTYPE_STATION:
651                 break;
652         case NL80211_IFTYPE_ADHOC:
653                 vnt_mac_reg_bits_off(priv, MAC_REG_TCR, TCR_AUTOBCNTX);
654                 vnt_mac_reg_bits_off(priv, MAC_REG_TFTCTL, TFTCTL_TSFCNTREN);
655                 vnt_mac_reg_bits_off(priv, MAC_REG_HOSTCR, HOSTCR_ADHOC);
656                 break;
657         case NL80211_IFTYPE_AP:
658                 vnt_mac_reg_bits_off(priv, MAC_REG_TCR, TCR_AUTOBCNTX);
659                 vnt_mac_reg_bits_off(priv, MAC_REG_TFTCTL, TFTCTL_TSFCNTREN);
660                 vnt_mac_reg_bits_off(priv, MAC_REG_HOSTCR, HOSTCR_AP);
661                 break;
662         default:
663                 break;
664         }
665
666         vnt_radio_power_off(priv);
667
668         priv->op_mode = NL80211_IFTYPE_UNSPECIFIED;
669
670         /* LED slow blink */
671         vnt_mac_set_led(priv, LEDSTS_STS, LEDSTS_SLOW);
672 }
673
674 static int vnt_config(struct ieee80211_hw *hw, u32 changed)
675 {
676         struct vnt_private *priv = hw->priv;
677         struct ieee80211_conf *conf = &hw->conf;
678
679         if (changed & IEEE80211_CONF_CHANGE_PS) {
680                 if (conf->flags & IEEE80211_CONF_PS)
681                         vnt_enable_power_saving(priv, conf->listen_interval);
682                 else
683                         vnt_disable_power_saving(priv);
684         }
685
686         if ((changed & IEEE80211_CONF_CHANGE_CHANNEL) ||
687             (conf->flags & IEEE80211_CONF_OFFCHANNEL)) {
688                 vnt_set_channel(priv, conf->chandef.chan->hw_value);
689
690                 if (conf->chandef.chan->band == NL80211_BAND_5GHZ)
691                         priv->bb_type = BB_TYPE_11A;
692                 else
693                         priv->bb_type = BB_TYPE_11G;
694         }
695
696         if (changed & IEEE80211_CONF_CHANGE_POWER) {
697                 if (priv->bb_type == BB_TYPE_11B)
698                         priv->current_rate = RATE_1M;
699                 else
700                         priv->current_rate = RATE_54M;
701
702                 vnt_rf_setpower(priv, priv->current_rate,
703                                 conf->chandef.chan->hw_value);
704         }
705
706         return 0;
707 }
708
709 static void vnt_bss_info_changed(struct ieee80211_hw *hw,
710                                  struct ieee80211_vif *vif,
711                                  struct ieee80211_bss_conf *conf, u32 changed)
712 {
713         struct vnt_private *priv = hw->priv;
714
715         priv->current_aid = conf->aid;
716
717         if (changed & BSS_CHANGED_BSSID && conf->bssid)
718                 vnt_mac_set_bssid_addr(priv, (u8 *)conf->bssid);
719
720         if (changed & BSS_CHANGED_BASIC_RATES) {
721                 priv->basic_rates = conf->basic_rates;
722
723                 vnt_update_top_rates(priv);
724                 vnt_set_bss_mode(priv);
725
726                 dev_dbg(&priv->usb->dev, "basic rates %x\n", conf->basic_rates);
727         }
728
729         if (changed & BSS_CHANGED_ERP_PREAMBLE) {
730                 if (conf->use_short_preamble) {
731                         vnt_mac_enable_barker_preamble_mode(priv);
732                         priv->preamble_type = true;
733                 } else {
734                         vnt_mac_disable_barker_preamble_mode(priv);
735                         priv->preamble_type = false;
736                 }
737         }
738
739         if (changed & BSS_CHANGED_ERP_CTS_PROT) {
740                 if (conf->use_cts_prot)
741                         vnt_mac_enable_protect_mode(priv);
742                 else
743                         vnt_mac_disable_protect_mode(priv);
744         }
745
746         if (changed & BSS_CHANGED_ERP_SLOT) {
747                 if (conf->use_short_slot)
748                         priv->short_slot_time = true;
749                 else
750                         priv->short_slot_time = false;
751
752                 vnt_set_short_slot_time(priv);
753                 vnt_update_ifs(priv);
754                 vnt_set_vga_gain_offset(priv, priv->bb_vga[0]);
755                 vnt_update_pre_ed_threshold(priv, false);
756         }
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_adjust_tsf(priv, conf->beacon_rate->hw_value,
782                                        conf->sync_tsf, priv->current_tsf);
783
784                         vnt_mac_set_beacon_interval(priv, conf->beacon_int);
785
786                         vnt_reset_next_tbtt(priv, conf->beacon_int);
787                 } else {
788                         vnt_clear_current_tsf(priv);
789
790                         vnt_mac_reg_bits_off(priv, MAC_REG_TFTCTL,
791                                              TFTCTL_TSFCNTREN);
792                 }
793         }
794 }
795
796 static u64 vnt_prepare_multicast(struct ieee80211_hw *hw,
797                                  struct netdev_hw_addr_list *mc_list)
798 {
799         struct vnt_private *priv = hw->priv;
800         struct netdev_hw_addr *ha;
801         u64 mc_filter = 0;
802         u32 bit_nr = 0;
803
804         netdev_hw_addr_list_for_each(ha, mc_list) {
805                 bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
806
807                 mc_filter |= 1ULL << (bit_nr & 0x3f);
808         }
809
810         priv->mc_list_count = mc_list->count;
811
812         return mc_filter;
813 }
814
815 static void vnt_configure(struct ieee80211_hw *hw,
816                           unsigned int changed_flags,
817                           unsigned int *total_flags, u64 multicast)
818 {
819         struct vnt_private *priv = hw->priv;
820         u8 rx_mode = 0;
821         int rc;
822
823         *total_flags &= FIF_ALLMULTI | FIF_OTHER_BSS | FIF_BCN_PRBRESP_PROMISC;
824
825         rc = vnt_control_in(priv, MESSAGE_TYPE_READ, MAC_REG_RCR,
826                             MESSAGE_REQUEST_MACREG, sizeof(u8), &rx_mode);
827
828         if (!rc)
829                 rx_mode = RCR_MULTICAST | RCR_BROADCAST;
830
831         dev_dbg(&priv->usb->dev, "rx mode in = %x\n", rx_mode);
832
833         if (changed_flags & FIF_ALLMULTI) {
834                 if (*total_flags & FIF_ALLMULTI) {
835                         if (priv->mc_list_count > 2)
836                                 vnt_mac_set_filter(priv, ~0);
837                         else
838                                 vnt_mac_set_filter(priv, multicast);
839
840                         rx_mode |= RCR_MULTICAST | RCR_BROADCAST;
841                 } else {
842                         rx_mode &= ~(RCR_MULTICAST | RCR_BROADCAST);
843                 }
844         }
845
846         if (changed_flags & (FIF_OTHER_BSS | FIF_BCN_PRBRESP_PROMISC)) {
847                 if (*total_flags & (FIF_OTHER_BSS | FIF_BCN_PRBRESP_PROMISC))
848                         rx_mode &= ~RCR_BSSID;
849                 else
850                         rx_mode |= RCR_BSSID;
851         }
852
853         vnt_control_out_u8(priv, MESSAGE_REQUEST_MACREG, MAC_REG_RCR, rx_mode);
854
855         dev_dbg(&priv->usb->dev, "rx mode out= %x\n", rx_mode);
856 }
857
858 static int vnt_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
859                        struct ieee80211_vif *vif, struct ieee80211_sta *sta,
860                        struct ieee80211_key_conf *key)
861 {
862         struct vnt_private *priv = hw->priv;
863
864         switch (cmd) {
865         case SET_KEY:
866                 if (vnt_set_keys(hw, sta, vif, key))
867                         return -EOPNOTSUPP;
868                 break;
869         case DISABLE_KEY:
870                 if (test_bit(key->hw_key_idx, &priv->key_entry_inuse))
871                         clear_bit(key->hw_key_idx, &priv->key_entry_inuse);
872         default:
873                 break;
874         }
875
876         return 0;
877 }
878
879 static void vnt_sw_scan_start(struct ieee80211_hw *hw,
880                               struct ieee80211_vif *vif,
881                               const u8 *addr)
882 {
883         struct vnt_private *priv = hw->priv;
884
885         /* Set max sensitivity*/
886         vnt_update_pre_ed_threshold(priv, true);
887 }
888
889 static void vnt_sw_scan_complete(struct ieee80211_hw *hw,
890                                  struct ieee80211_vif *vif)
891 {
892         struct vnt_private *priv = hw->priv;
893
894         /* Return sensitivity to channel level*/
895         vnt_update_pre_ed_threshold(priv, false);
896 }
897
898 static int vnt_get_stats(struct ieee80211_hw *hw,
899                          struct ieee80211_low_level_stats *stats)
900 {
901         struct vnt_private *priv = hw->priv;
902
903         memcpy(stats, &priv->low_stats, sizeof(*stats));
904
905         return 0;
906 }
907
908 static u64 vnt_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
909 {
910         struct vnt_private *priv = hw->priv;
911
912         return priv->current_tsf;
913 }
914
915 static void vnt_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
916                         u64 tsf)
917 {
918         struct vnt_private *priv = hw->priv;
919
920         vnt_update_next_tbtt(priv, tsf, vif->bss_conf.beacon_int);
921 }
922
923 static void vnt_reset_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
924 {
925         struct vnt_private *priv = hw->priv;
926
927         vnt_mac_reg_bits_off(priv, MAC_REG_TFTCTL, TFTCTL_TSFCNTREN);
928
929         vnt_clear_current_tsf(priv);
930 }
931
932 static const struct ieee80211_ops vnt_mac_ops = {
933         .tx                     = vnt_tx_80211,
934         .start                  = vnt_start,
935         .stop                   = vnt_stop,
936         .add_interface          = vnt_add_interface,
937         .remove_interface       = vnt_remove_interface,
938         .config                 = vnt_config,
939         .bss_info_changed       = vnt_bss_info_changed,
940         .prepare_multicast      = vnt_prepare_multicast,
941         .configure_filter       = vnt_configure,
942         .set_key                = vnt_set_key,
943         .sw_scan_start          = vnt_sw_scan_start,
944         .sw_scan_complete       = vnt_sw_scan_complete,
945         .get_stats              = vnt_get_stats,
946         .get_tsf                = vnt_get_tsf,
947         .set_tsf                = vnt_set_tsf,
948         .reset_tsf              = vnt_reset_tsf,
949 };
950
951 int vnt_init(struct vnt_private *priv)
952 {
953         if (!(vnt_init_registers(priv)))
954                 return -EAGAIN;
955
956         SET_IEEE80211_PERM_ADDR(priv->hw, priv->permanent_net_addr);
957
958         vnt_init_bands(priv);
959
960         if (ieee80211_register_hw(priv->hw))
961                 return -ENODEV;
962
963         priv->mac_hw = true;
964
965         vnt_radio_power_off(priv);
966
967         return 0;
968 }
969
970 static int
971 vt6656_probe(struct usb_interface *intf, const struct usb_device_id *id)
972 {
973         struct usb_device *udev;
974         struct vnt_private *priv;
975         struct ieee80211_hw *hw;
976         struct wiphy *wiphy;
977         int rc = 0;
978
979         udev = usb_get_dev(interface_to_usbdev(intf));
980
981         dev_notice(&udev->dev, "%s Ver. %s\n",
982                    DEVICE_FULL_DRV_NAM, DEVICE_VERSION);
983         dev_notice(&udev->dev,
984                    "Copyright (c) 2004 VIA Networking Technologies, Inc.\n");
985
986         hw = ieee80211_alloc_hw(sizeof(struct vnt_private), &vnt_mac_ops);
987         if (!hw) {
988                 dev_err(&udev->dev, "could not register ieee80211_hw\n");
989                 rc = -ENOMEM;
990                 goto err_nomem;
991         }
992
993         priv = hw->priv;
994         priv->hw = hw;
995         priv->usb = udev;
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
1019         priv->hw->max_signal = 100;
1020
1021         SET_IEEE80211_DEV(priv->hw, &intf->dev);
1022
1023         rc = usb_reset_device(priv->usb);
1024         if (rc)
1025                 dev_warn(&priv->usb->dev,
1026                          "%s reset fail status=%d\n", __func__, rc);
1027
1028         clear_bit(DEVICE_FLAGS_DISCONNECTED, &priv->flags);
1029         vnt_reset_command_timer(priv);
1030
1031         vnt_schedule_command(priv, WLAN_CMD_INIT_MAC80211);
1032
1033         return 0;
1034
1035 err_nomem:
1036         usb_put_dev(udev);
1037
1038         return rc;
1039 }
1040
1041 static void vt6656_disconnect(struct usb_interface *intf)
1042 {
1043         struct vnt_private *priv = usb_get_intfdata(intf);
1044
1045         if (!priv)
1046                 return;
1047
1048         if (priv->mac_hw)
1049                 ieee80211_unregister_hw(priv->hw);
1050
1051         usb_set_intfdata(intf, NULL);
1052         usb_put_dev(interface_to_usbdev(intf));
1053
1054         set_bit(DEVICE_FLAGS_UNPLUG, &priv->flags);
1055
1056         ieee80211_free_hw(priv->hw);
1057 }
1058
1059 #ifdef CONFIG_PM
1060
1061 static int vt6656_suspend(struct usb_interface *intf, pm_message_t message)
1062 {
1063         return 0;
1064 }
1065
1066 static int vt6656_resume(struct usb_interface *intf)
1067 {
1068         return 0;
1069 }
1070
1071 #endif /* CONFIG_PM */
1072
1073 MODULE_DEVICE_TABLE(usb, vt6656_table);
1074
1075 static struct usb_driver vt6656_driver = {
1076         .name =         DEVICE_NAME,
1077         .probe =        vt6656_probe,
1078         .disconnect =   vt6656_disconnect,
1079         .id_table =     vt6656_table,
1080 #ifdef CONFIG_PM
1081         .suspend = vt6656_suspend,
1082         .resume = vt6656_resume,
1083 #endif /* CONFIG_PM */
1084 };
1085
1086 module_usb_driver(vt6656_driver);