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