GNU Linux-libre 4.9.337-gnu1
[releases.git] / drivers / staging / vt6656 / wcmd.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: wcmd.c
17  *
18  * Purpose: Handles the management command interface functions
19  *
20  * Author: Lyndon Chen
21  *
22  * Date: May 8, 2003
23  *
24  * Functions:
25  *      vnt_cmd_complete - Command Complete function
26  *      vnt_schedule_command - Push Command and wait Command Scheduler to do
27  *      vnt_cmd_timer_wait- Call back timer
28  *
29  * Revision History:
30  *
31  */
32
33 #include "device.h"
34 #include "mac.h"
35 #include "wcmd.h"
36 #include "power.h"
37 #include "usbpipe.h"
38 #include "rxtx.h"
39 #include "rf.h"
40
41 static void vnt_cmd_timer_wait(struct vnt_private *priv, unsigned long msecs)
42 {
43         schedule_delayed_work(&priv->run_command_work, msecs_to_jiffies(msecs));
44 }
45
46 static int vnt_cmd_complete(struct vnt_private *priv)
47 {
48
49         priv->command_state = WLAN_CMD_IDLE;
50         if (priv->free_cmd_queue == CMD_Q_SIZE) {
51                 /* Command Queue Empty */
52                 priv->cmd_running = false;
53                 return true;
54         }
55
56         priv->command = priv->cmd_queue[priv->cmd_dequeue_idx];
57
58         ADD_ONE_WITH_WRAP_AROUND(priv->cmd_dequeue_idx, CMD_Q_SIZE);
59         priv->free_cmd_queue++;
60         priv->cmd_running = true;
61
62         switch (priv->command) {
63         case WLAN_CMD_INIT_MAC80211:
64                 priv->command_state = WLAN_CMD_INIT_MAC80211_START;
65                 break;
66
67         case WLAN_CMD_TBTT_WAKEUP:
68                 priv->command_state = WLAN_CMD_TBTT_WAKEUP_START;
69                 break;
70
71         case WLAN_CMD_BECON_SEND:
72                 priv->command_state = WLAN_CMD_BECON_SEND_START;
73                 break;
74
75         case WLAN_CMD_SETPOWER:
76                 priv->command_state = WLAN_CMD_SETPOWER_START;
77                 break;
78
79         case WLAN_CMD_CHANGE_ANTENNA:
80                 priv->command_state = WLAN_CMD_CHANGE_ANTENNA_START;
81                 break;
82
83         default:
84                 break;
85         }
86
87         vnt_cmd_timer_wait(priv, 0);
88
89         return true;
90 }
91
92 void vnt_run_command(struct work_struct *work)
93 {
94         struct vnt_private *priv =
95                 container_of(work, struct vnt_private, run_command_work.work);
96
97         if (test_bit(DEVICE_FLAGS_DISCONNECTED, &priv->flags))
98                 return;
99
100         if (!priv->cmd_running)
101                 return;
102
103         switch (priv->command_state) {
104         case WLAN_CMD_INIT_MAC80211_START:
105                 if (priv->mac_hw)
106                         break;
107
108                 dev_info(&priv->usb->dev, "Starting mac80211\n");
109
110                 if (vnt_init(priv)) {
111                         /* If fail all ends TODO retry */
112                         dev_err(&priv->usb->dev, "failed to start\n");
113                         usb_set_intfdata(priv->intf, NULL);
114                         ieee80211_free_hw(priv->hw);
115                         return;
116                 }
117
118                 break;
119
120         case WLAN_CMD_TBTT_WAKEUP_START:
121                 vnt_next_tbtt_wakeup(priv);
122                 break;
123
124         case WLAN_CMD_BECON_SEND_START:
125                 if (!priv->vif)
126                         break;
127
128                 vnt_beacon_make(priv, priv->vif);
129
130                 vnt_mac_reg_bits_on(priv, MAC_REG_TCR, TCR_AUTOBCNTX);
131
132                 break;
133
134         case WLAN_CMD_SETPOWER_START:
135
136                 vnt_rf_setpower(priv, priv->current_rate,
137                                 priv->hw->conf.chandef.chan->hw_value);
138
139                 break;
140
141         case WLAN_CMD_CHANGE_ANTENNA_START:
142                 dev_dbg(&priv->usb->dev, "Change from Antenna%d to",
143                                                         priv->rx_antenna_sel);
144
145                 if (priv->rx_antenna_sel == 0) {
146                         priv->rx_antenna_sel = 1;
147                         if (priv->tx_rx_ant_inv)
148                                 vnt_set_antenna_mode(priv, ANT_RXA);
149                         else
150                                 vnt_set_antenna_mode(priv, ANT_RXB);
151                 } else {
152                         priv->rx_antenna_sel = 0;
153                         if (priv->tx_rx_ant_inv)
154                                 vnt_set_antenna_mode(priv, ANT_RXB);
155                         else
156                                 vnt_set_antenna_mode(priv, ANT_RXA);
157                 }
158                 break;
159
160         default:
161                 break;
162         }
163
164         vnt_cmd_complete(priv);
165 }
166
167 int vnt_schedule_command(struct vnt_private *priv, enum vnt_cmd command)
168 {
169
170         if (priv->free_cmd_queue == 0)
171                 return false;
172
173         priv->cmd_queue[priv->cmd_enqueue_idx] = command;
174
175         ADD_ONE_WITH_WRAP_AROUND(priv->cmd_enqueue_idx, CMD_Q_SIZE);
176         priv->free_cmd_queue--;
177
178         if (!priv->cmd_running)
179                 vnt_cmd_complete(priv);
180
181         return true;
182
183 }
184
185 void vnt_reset_command_timer(struct vnt_private *priv)
186 {
187         priv->free_cmd_queue = CMD_Q_SIZE;
188         priv->cmd_dequeue_idx = 0;
189         priv->cmd_enqueue_idx = 0;
190         priv->command_state = WLAN_CMD_IDLE;
191         priv->cmd_running = false;
192 }