OSDN Git Service

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