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