staging: vt6656: remove duplicated blank line
[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         ret = vnt_mac_set_led(priv, LEDSTS_TMLEN, 0x38);
366         if (ret)
367                 goto end;
368
369         ret = vnt_mac_set_led(priv, LEDSTS_STS, LEDSTS_SLOW);
370         if (ret)
371                 goto end;
372
373         ret = vnt_mac_reg_bits_on(priv, MAC_REG_GPIOCTL0, 0x01);
374         if (ret)
375                 goto end;
376
377         ret = vnt_radio_power_on(priv);
378         if (ret)
379                 goto end;
380
381         dev_dbg(&priv->usb->dev, "<----INIbInitAdapter Exit\n");
382
383 end:
384         return ret;
385 }
386
387 static void vnt_free_tx_bufs(struct vnt_private *priv)
388 {
389         struct vnt_usb_send_context *tx_context;
390         int ii;
391
392         for (ii = 0; ii < priv->num_tx_context; ii++) {
393                 tx_context = priv->tx_context[ii];
394                 if (!tx_context)
395                         continue;
396
397                 /* deallocate URBs */
398                 if (tx_context->urb) {
399                         usb_kill_urb(tx_context->urb);
400                         usb_free_urb(tx_context->urb);
401                 }
402
403                 kfree(tx_context);
404         }
405 }
406
407 static void vnt_free_rx_bufs(struct vnt_private *priv)
408 {
409         struct vnt_rcb *rcb;
410         int ii;
411
412         for (ii = 0; ii < priv->num_rcb; ii++) {
413                 rcb = priv->rcb[ii];
414                 if (!rcb)
415                         continue;
416
417                 /* deallocate URBs */
418                 if (rcb->urb) {
419                         usb_kill_urb(rcb->urb);
420                         usb_free_urb(rcb->urb);
421                 }
422
423                 /* deallocate skb */
424                 if (rcb->skb)
425                         dev_kfree_skb(rcb->skb);
426
427                 kfree(rcb);
428         }
429 }
430
431 static void vnt_free_int_bufs(struct vnt_private *priv)
432 {
433         kfree(priv->int_buf.data_buf);
434 }
435
436 static int vnt_alloc_bufs(struct vnt_private *priv)
437 {
438         int ret = 0;
439         struct vnt_usb_send_context *tx_context;
440         struct vnt_rcb *rcb;
441         int ii;
442
443         for (ii = 0; ii < priv->num_tx_context; ii++) {
444                 tx_context = kmalloc(sizeof(*tx_context), GFP_KERNEL);
445                 if (!tx_context) {
446                         ret = -ENOMEM;
447                         goto free_tx;
448                 }
449
450                 priv->tx_context[ii] = tx_context;
451                 tx_context->priv = priv;
452                 tx_context->pkt_no = ii;
453
454                 /* allocate URBs */
455                 tx_context->urb = usb_alloc_urb(0, GFP_KERNEL);
456                 if (!tx_context->urb) {
457                         ret = -ENOMEM;
458                         goto free_tx;
459                 }
460
461                 tx_context->in_use = false;
462         }
463
464         for (ii = 0; ii < priv->num_rcb; ii++) {
465                 priv->rcb[ii] = kzalloc(sizeof(*priv->rcb[ii]), GFP_KERNEL);
466                 if (!priv->rcb[ii]) {
467                         ret = -ENOMEM;
468                         goto free_rx_tx;
469                 }
470
471                 rcb = priv->rcb[ii];
472
473                 rcb->priv = priv;
474
475                 /* allocate URBs */
476                 rcb->urb = usb_alloc_urb(0, GFP_KERNEL);
477                 if (!rcb->urb) {
478                         ret = -ENOMEM;
479                         goto free_rx_tx;
480                 }
481
482                 rcb->skb = dev_alloc_skb(priv->rx_buf_sz);
483                 if (!rcb->skb) {
484                         ret = -ENOMEM;
485                         goto free_rx_tx;
486                 }
487
488                 rcb->in_use = false;
489
490                 /* submit rx urb */
491                 ret = vnt_submit_rx_urb(priv, rcb);
492                 if (ret)
493                         goto free_rx_tx;
494         }
495
496         priv->interrupt_urb = usb_alloc_urb(0, GFP_KERNEL);
497         if (!priv->interrupt_urb) {
498                 ret = -ENOMEM;
499                 goto free_rx_tx;
500         }
501
502         priv->int_buf.data_buf = kmalloc(MAX_INTERRUPT_SIZE, GFP_KERNEL);
503         if (!priv->int_buf.data_buf) {
504                 ret = -ENOMEM;
505                 goto free_rx_tx_urb;
506         }
507
508         return 0;
509
510 free_rx_tx_urb:
511         usb_free_urb(priv->interrupt_urb);
512 free_rx_tx:
513         vnt_free_rx_bufs(priv);
514 free_tx:
515         vnt_free_tx_bufs(priv);
516         return ret;
517 }
518
519 static void vnt_tx_80211(struct ieee80211_hw *hw,
520                          struct ieee80211_tx_control *control,
521                          struct sk_buff *skb)
522 {
523         struct vnt_private *priv = hw->priv;
524
525         if (vnt_tx_packet(priv, skb))
526                 ieee80211_free_txskb(hw, skb);
527 }
528
529 static int vnt_start(struct ieee80211_hw *hw)
530 {
531         int ret = 0;
532         struct vnt_private *priv = hw->priv;
533
534         priv->rx_buf_sz = MAX_TOTAL_SIZE_WITH_ALL_HEADERS;
535
536         ret = vnt_alloc_bufs(priv);
537         if (ret) {
538                 dev_dbg(&priv->usb->dev, "vnt_alloc_bufs fail...\n");
539                 goto err;
540         }
541
542         clear_bit(DEVICE_FLAGS_DISCONNECTED, &priv->flags);
543
544         ret = vnt_init_registers(priv);
545         if (ret) {
546                 dev_dbg(&priv->usb->dev, " init register fail\n");
547                 goto free_all;
548         }
549
550         ret = vnt_key_init_table(priv);
551         if (ret)
552                 goto free_all;
553
554         priv->int_interval = 1;  /* bInterval is set to 1 */
555
556         ret = vnt_int_start_interrupt(priv);
557         if (ret)
558                 goto free_all;
559
560         ieee80211_wake_queues(hw);
561
562         return 0;
563
564 free_all:
565         vnt_free_rx_bufs(priv);
566         vnt_free_tx_bufs(priv);
567         vnt_free_int_bufs(priv);
568
569         usb_kill_urb(priv->interrupt_urb);
570         usb_free_urb(priv->interrupt_urb);
571 err:
572         return ret;
573 }
574
575 static void vnt_stop(struct ieee80211_hw *hw)
576 {
577         struct vnt_private *priv = hw->priv;
578         int i;
579
580         if (!priv)
581                 return;
582
583         for (i = 0; i < MAX_KEY_TABLE; i++)
584                 vnt_mac_disable_keyentry(priv, i);
585
586         /* clear all keys */
587         priv->key_entry_inuse = 0;
588
589         if (!test_bit(DEVICE_FLAGS_UNPLUG, &priv->flags))
590                 vnt_mac_shutdown(priv);
591
592         ieee80211_stop_queues(hw);
593
594         set_bit(DEVICE_FLAGS_DISCONNECTED, &priv->flags);
595
596         cancel_delayed_work_sync(&priv->run_command_work);
597
598         priv->cmd_running = false;
599
600         vnt_free_tx_bufs(priv);
601         vnt_free_rx_bufs(priv);
602         vnt_free_int_bufs(priv);
603
604         usb_kill_urb(priv->interrupt_urb);
605         usb_free_urb(priv->interrupt_urb);
606 }
607
608 static int vnt_add_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
609 {
610         struct vnt_private *priv = hw->priv;
611
612         priv->vif = vif;
613
614         switch (vif->type) {
615         case NL80211_IFTYPE_STATION:
616                 break;
617         case NL80211_IFTYPE_ADHOC:
618                 vnt_mac_reg_bits_off(priv, MAC_REG_RCR, RCR_UNICAST);
619
620                 vnt_mac_reg_bits_on(priv, MAC_REG_HOSTCR, HOSTCR_ADHOC);
621
622                 break;
623         case NL80211_IFTYPE_AP:
624                 vnt_mac_reg_bits_off(priv, MAC_REG_RCR, RCR_UNICAST);
625
626                 vnt_mac_reg_bits_on(priv, MAC_REG_HOSTCR, HOSTCR_AP);
627
628                 break;
629         default:
630                 return -EOPNOTSUPP;
631         }
632
633         priv->op_mode = vif->type;
634
635         vnt_set_bss_mode(priv);
636
637         /* LED blink on TX */
638         vnt_mac_set_led(priv, LEDSTS_STS, LEDSTS_INTER);
639
640         return 0;
641 }
642
643 static void vnt_remove_interface(struct ieee80211_hw *hw,
644                                  struct ieee80211_vif *vif)
645 {
646         struct vnt_private *priv = hw->priv;
647
648         switch (vif->type) {
649         case NL80211_IFTYPE_STATION:
650                 break;
651         case NL80211_IFTYPE_ADHOC:
652                 vnt_mac_reg_bits_off(priv, MAC_REG_TCR, TCR_AUTOBCNTX);
653                 vnt_mac_reg_bits_off(priv, MAC_REG_TFTCTL, TFTCTL_TSFCNTREN);
654                 vnt_mac_reg_bits_off(priv, MAC_REG_HOSTCR, HOSTCR_ADHOC);
655                 break;
656         case NL80211_IFTYPE_AP:
657                 vnt_mac_reg_bits_off(priv, MAC_REG_TCR, TCR_AUTOBCNTX);
658                 vnt_mac_reg_bits_off(priv, MAC_REG_TFTCTL, TFTCTL_TSFCNTREN);
659                 vnt_mac_reg_bits_off(priv, MAC_REG_HOSTCR, HOSTCR_AP);
660                 break;
661         default:
662                 break;
663         }
664
665         vnt_radio_power_off(priv);
666
667         priv->op_mode = NL80211_IFTYPE_UNSPECIFIED;
668
669         /* LED slow blink */
670         vnt_mac_set_led(priv, LEDSTS_STS, LEDSTS_SLOW);
671 }
672
673 static int vnt_config(struct ieee80211_hw *hw, u32 changed)
674 {
675         struct vnt_private *priv = hw->priv;
676         struct ieee80211_conf *conf = &hw->conf;
677
678         if (changed & IEEE80211_CONF_CHANGE_PS) {
679                 if (conf->flags & IEEE80211_CONF_PS)
680                         vnt_enable_power_saving(priv, conf->listen_interval);
681                 else
682                         vnt_disable_power_saving(priv);
683         }
684
685         if ((changed & IEEE80211_CONF_CHANGE_CHANNEL) ||
686             (conf->flags & IEEE80211_CONF_OFFCHANNEL)) {
687                 vnt_set_channel(priv, conf->chandef.chan->hw_value);
688
689                 if (conf->chandef.chan->band == NL80211_BAND_5GHZ)
690                         priv->bb_type = BB_TYPE_11A;
691                 else
692                         priv->bb_type = BB_TYPE_11G;
693         }
694
695         if (changed & IEEE80211_CONF_CHANGE_POWER) {
696                 if (priv->bb_type == BB_TYPE_11B)
697                         priv->current_rate = RATE_1M;
698                 else
699                         priv->current_rate = RATE_54M;
700
701                 vnt_rf_setpower(priv, priv->current_rate,
702                                 conf->chandef.chan->hw_value);
703         }
704
705         return 0;
706 }
707
708 static void vnt_bss_info_changed(struct ieee80211_hw *hw,
709                                  struct ieee80211_vif *vif,
710                                  struct ieee80211_bss_conf *conf, u32 changed)
711 {
712         struct vnt_private *priv = hw->priv;
713
714         priv->current_aid = conf->aid;
715
716         if (changed & BSS_CHANGED_BSSID && conf->bssid)
717                 vnt_mac_set_bssid_addr(priv, (u8 *)conf->bssid);
718
719         if (changed & BSS_CHANGED_BASIC_RATES) {
720                 priv->basic_rates = conf->basic_rates;
721
722                 vnt_update_top_rates(priv);
723                 vnt_set_bss_mode(priv);
724
725                 dev_dbg(&priv->usb->dev, "basic rates %x\n", conf->basic_rates);
726         }
727
728         if (changed & BSS_CHANGED_ERP_PREAMBLE) {
729                 if (conf->use_short_preamble) {
730                         vnt_mac_enable_barker_preamble_mode(priv);
731                         priv->preamble_type = true;
732                 } else {
733                         vnt_mac_disable_barker_preamble_mode(priv);
734                         priv->preamble_type = false;
735                 }
736         }
737
738         if (changed & BSS_CHANGED_ERP_CTS_PROT) {
739                 if (conf->use_cts_prot)
740                         vnt_mac_enable_protect_mode(priv);
741                 else
742                         vnt_mac_disable_protect_mode(priv);
743         }
744
745         if (changed & BSS_CHANGED_ERP_SLOT) {
746                 if (conf->use_short_slot)
747                         priv->short_slot_time = true;
748                 else
749                         priv->short_slot_time = false;
750
751                 vnt_set_short_slot_time(priv);
752                 vnt_update_ifs(priv);
753                 vnt_set_vga_gain_offset(priv, priv->bb_vga[0]);
754                 vnt_update_pre_ed_threshold(priv, false);
755         }
756
757         if (changed & BSS_CHANGED_TXPOWER)
758                 vnt_rf_setpower(priv, priv->current_rate,
759                                 conf->chandef.chan->hw_value);
760
761         if (changed & BSS_CHANGED_BEACON_ENABLED) {
762                 dev_dbg(&priv->usb->dev,
763                         "Beacon enable %d\n", conf->enable_beacon);
764
765                 if (conf->enable_beacon) {
766                         vnt_beacon_enable(priv, vif, conf);
767
768                         vnt_mac_reg_bits_on(priv, MAC_REG_TCR, TCR_AUTOBCNTX);
769                 } else {
770                         vnt_mac_reg_bits_off(priv, MAC_REG_TCR, TCR_AUTOBCNTX);
771                 }
772         }
773
774         if (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_BEACON_INFO) &&
775             priv->op_mode != NL80211_IFTYPE_AP) {
776                 if (conf->assoc && conf->beacon_rate) {
777                         vnt_mac_reg_bits_on(priv, MAC_REG_TFTCTL,
778                                             TFTCTL_TSFCNTREN);
779
780                         vnt_adjust_tsf(priv, conf->beacon_rate->hw_value,
781                                        conf->sync_tsf, priv->current_tsf);
782
783                         vnt_mac_set_beacon_interval(priv, conf->beacon_int);
784
785                         vnt_reset_next_tbtt(priv, conf->beacon_int);
786                 } else {
787                         vnt_clear_current_tsf(priv);
788
789                         vnt_mac_reg_bits_off(priv, MAC_REG_TFTCTL,
790                                              TFTCTL_TSFCNTREN);
791                 }
792         }
793 }
794
795 static u64 vnt_prepare_multicast(struct ieee80211_hw *hw,
796                                  struct netdev_hw_addr_list *mc_list)
797 {
798         struct vnt_private *priv = hw->priv;
799         struct netdev_hw_addr *ha;
800         u64 mc_filter = 0;
801         u32 bit_nr = 0;
802
803         netdev_hw_addr_list_for_each(ha, mc_list) {
804                 bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
805
806                 mc_filter |= 1ULL << (bit_nr & 0x3f);
807         }
808
809         priv->mc_list_count = mc_list->count;
810
811         return mc_filter;
812 }
813
814 static void vnt_configure(struct ieee80211_hw *hw,
815                           unsigned int changed_flags,
816                           unsigned int *total_flags, u64 multicast)
817 {
818         struct vnt_private *priv = hw->priv;
819         u8 rx_mode = 0;
820         int rc;
821
822         *total_flags &= FIF_ALLMULTI | FIF_OTHER_BSS | FIF_BCN_PRBRESP_PROMISC;
823
824         rc = vnt_control_in(priv, MESSAGE_TYPE_READ, MAC_REG_RCR,
825                             MESSAGE_REQUEST_MACREG, sizeof(u8), &rx_mode);
826
827         if (!rc)
828                 rx_mode = RCR_MULTICAST | RCR_BROADCAST;
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
996         vnt_set_options(priv);
997
998         spin_lock_init(&priv->lock);
999         mutex_init(&priv->usb_lock);
1000
1001         INIT_DELAYED_WORK(&priv->run_command_work, vnt_run_command);
1002
1003         usb_set_intfdata(intf, priv);
1004
1005         wiphy = priv->hw->wiphy;
1006
1007         wiphy->frag_threshold = FRAG_THRESH_DEF;
1008         wiphy->rts_threshold = RTS_THRESH_DEF;
1009         wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
1010                 BIT(NL80211_IFTYPE_ADHOC) | BIT(NL80211_IFTYPE_AP);
1011
1012         ieee80211_hw_set(priv->hw, TIMING_BEACON_ONLY);
1013         ieee80211_hw_set(priv->hw, SIGNAL_DBM);
1014         ieee80211_hw_set(priv->hw, RX_INCLUDES_FCS);
1015         ieee80211_hw_set(priv->hw, REPORTS_TX_ACK_STATUS);
1016         ieee80211_hw_set(priv->hw, SUPPORTS_PS);
1017
1018         priv->hw->max_signal = 100;
1019
1020         SET_IEEE80211_DEV(priv->hw, &intf->dev);
1021
1022         rc = usb_reset_device(priv->usb);
1023         if (rc)
1024                 dev_warn(&priv->usb->dev,
1025                          "%s reset fail status=%d\n", __func__, rc);
1026
1027         clear_bit(DEVICE_FLAGS_DISCONNECTED, &priv->flags);
1028         vnt_reset_command_timer(priv);
1029
1030         vnt_schedule_command(priv, WLAN_CMD_INIT_MAC80211);
1031
1032         return 0;
1033
1034 err_nomem:
1035         usb_put_dev(udev);
1036
1037         return rc;
1038 }
1039
1040 static void vt6656_disconnect(struct usb_interface *intf)
1041 {
1042         struct vnt_private *priv = usb_get_intfdata(intf);
1043
1044         if (!priv)
1045                 return;
1046
1047         if (priv->mac_hw)
1048                 ieee80211_unregister_hw(priv->hw);
1049
1050         usb_set_intfdata(intf, NULL);
1051         usb_put_dev(interface_to_usbdev(intf));
1052
1053         set_bit(DEVICE_FLAGS_UNPLUG, &priv->flags);
1054
1055         ieee80211_free_hw(priv->hw);
1056 }
1057
1058 #ifdef CONFIG_PM
1059
1060 static int vt6656_suspend(struct usb_interface *intf, pm_message_t message)
1061 {
1062         return 0;
1063 }
1064
1065 static int vt6656_resume(struct usb_interface *intf)
1066 {
1067         return 0;
1068 }
1069
1070 #endif /* CONFIG_PM */
1071
1072 MODULE_DEVICE_TABLE(usb, vt6656_table);
1073
1074 static struct usb_driver vt6656_driver = {
1075         .name =         DEVICE_NAME,
1076         .probe =        vt6656_probe,
1077         .disconnect =   vt6656_disconnect,
1078         .id_table =     vt6656_table,
1079 #ifdef CONFIG_PM
1080         .suspend = vt6656_suspend,
1081         .resume = vt6656_resume,
1082 #endif /* CONFIG_PM */
1083 };
1084
1085 module_usb_driver(vt6656_driver);