summaryrefslogtreecommitdiffstats
path: root/drivers/net/wireless/wl12xx/cmd.c
blob: f73ab602b7aee4e11739dfdb1ff5703394ad3f1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#include "cmd.h"

#include <linux/module.h>
#include <linux/crc7.h>
#include <linux/spi/spi.h>

#include "wl12xx.h"
#include "wl12xx_80211.h"
#include "reg.h"
#include "spi.h"
#include "ps.h"

int wl12xx_cmd_send(struct wl12xx *wl, u16 type, void *buf, size_t buf_len)
{
	struct wl12xx_command cmd;
	unsigned long timeout;
	size_t cmd_len;
	u32 intr;
	int ret = 0;

	memset(&cmd, 0, sizeof(cmd));
	cmd.id = type;
	cmd.status = 0;
	memcpy(cmd.parameters, buf, buf_len);
	cmd_len = ALIGN(buf_len, 4) + CMDMBOX_HEADER_LEN;

	wl12xx_ps_elp_wakeup(wl);

	wl12xx_spi_mem_write(wl, wl->cmd_box_addr, &cmd, cmd_len);

	wl12xx_reg_write32(wl, ACX_REG_INTERRUPT_TRIG, INTR_TRIG_CMD);

	timeout = jiffies + msecs_to_jiffies(WL12XX_COMMAND_TIMEOUT);

	intr = wl12xx_reg_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
	while (!(intr & wl->chip.intr_cmd_complete)) {
		if (time_after(jiffies, timeout)) {
			wl12xx_error("command complete timeout");
			ret = -ETIMEDOUT;
			goto out;
		}

		msleep(1);

		intr = wl12xx_reg_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
	}

	wl12xx_reg_write32(wl, ACX_REG_INTERRUPT_ACK,
			   wl->chip.intr_cmd_complete);

out:
	wl12xx_ps_elp_sleep(wl);

	return ret;
}

int wl12xx_cmd_test(struct wl12xx *wl, void *buf, size_t buf_len, u8 answer)
{
	int ret;

	wl12xx_debug(DEBUG_CMD, "cmd test");

	ret = wl12xx_cmd_send(wl, CMD_TEST, buf, buf_len);
	if (ret < 0) {
		wl12xx_warning("TEST command failed");
		return ret;
	}

	if (answer) {
		struct wl12xx_command *cmd_answer;

		/*
		 * The test command got in, we can read the answer.
		 * The answer would be a wl12xx_command, where the
		 * parameter array contains the actual answer.
		 */

		wl12xx_ps_elp_wakeup(wl);

		wl12xx_spi_mem_read(wl, wl->cmd_box_addr, buf, buf_len);

		wl12xx_ps_elp_sleep(wl);

		cmd_answer = buf;
		if (cmd_answer->status != CMD_STATUS_SUCCESS)
			wl12xx_error("TEST command answer error: %d",
				     cmd_answer->status);
	}

	return 0;
}


int wl12xx_cmd_interrogate(struct wl12xx *wl, u16 ie_id, u16 ie_len,
			   void *answer)
{
	struct wl12xx_command *cmd;
	struct acx_header header;
	int ret;

	wl12xx_debug(DEBUG_CMD, "cmd interrogate");

	header.id = ie_id;
	header.len = ie_len - sizeof(header);

	ret = wl12xx_cmd_send(wl, CMD_INTERROGATE, &header, sizeof(header));
	if (ret < 0) {
		wl12xx_error("INTERROGATE command failed");
		return ret;
	}

	wl12xx_ps_elp_wakeup(wl);

	/* the interrogate command got in, we can read the answer */
	wl12xx_spi_mem_read(wl, wl->cmd_box_addr, answer,
			    CMDMBOX_HEADER_LEN + ie_len);

	wl12xx_ps_elp_sleep(wl);

	cmd = answer;
	if (cmd->status != CMD_STATUS_SUCCESS)
		wl12xx_error("INTERROGATE command error: %d",
			     cmd->status);

	return 0;

}

int wl12xx_cmd_configure(struct wl12xx *wl, void *ie, int ie_len)
{
	int ret;

	wl12xx_debug(DEBUG_CMD, "cmd configure");

	ret = wl12xx_cmd_send(wl, CMD_CONFIGURE, ie,
			      ie_len);
	if (ret < 0) {
		wl12xx_warning("CONFIGURE command NOK");
		return ret;
	}

	return 0;

}

int wl12xx_cmd_vbm(struct wl12xx *wl, u8 identity,
		   void *bitmap, u16 bitmap_len, u8 bitmap_control)
{
	struct vbm_update_request vbm;
	int ret;

	wl12xx_debug(DEBUG_CMD, "cmd vbm");

	/* Count and period will be filled by the target */
	vbm.tim.bitmap_ctrl = bitmap_control;
	if (bitmap_len > PARTIAL_VBM_MAX) {
		wl12xx_warning("cmd vbm len is %d B, truncating to %d",
			       bitmap_len, PARTIAL_VBM_MAX);
		bitmap_len = PARTIAL_VBM_MAX;
	}
	memcpy(vbm.tim.pvb_field, bitmap, bitmap_len);
	vbm.tim.identity = identity;
	vbm.tim.length = bitmap_len + 3;

	vbm.len = cpu_to_le16(bitmap_len + 5);

	ret = wl12xx_cmd_send(wl, CMD_VBM, &vbm, sizeof(vbm));
	if (ret < 0) {
		wl12xx_error("VBM command failed");
		return ret;
	}

	return 0;
}

int wl12xx_cmd_data_path(struct wl12xx *wl, u8 channel, u8 enable)
{
	int ret;
	u16 cmd_rx, cmd_tx;

	wl12xx_debug(DEBUG_CMD, "cmd data path");

	if (enable) {
		cmd_rx = CMD_ENABLE_RX;
		cmd_tx = CMD_ENABLE_TX;
	} else {
		cmd_rx = CMD_DISABLE_RX;
		cmd_tx = CMD_DISABLE_TX;
	}

	ret = wl12xx_cmd_send(wl, cmd_rx, &channel, sizeof(channel));
	if (ret < 0) {
		wl12xx_error("rx %s cmd for channel %d failed",
			     enable ? "start" : "stop", channel);
		return ret;
	}

	wl12xx_debug(DEBUG_BOOT, "rx %s cmd channel %d",
		     enable ? "start" : "stop", channel);

	ret = wl12xx_cmd_send(wl, cmd_tx, &channel, sizeof(channel));
	if (ret < 0) {
		wl12xx_error("tx %s cmd for channel %d failed",
			     enable ? "start" : "stop", channel);
		return ret;
	}

	wl12xx_debug(DEBUG_BOOT, "tx %s cmd channel %d",
		     enable ? "start" : "stop", channel);

	return 0;
}

int wl12xx_cmd_join(struct wl12xx *wl, u8 bss_type, u8 dtim_interval,
		    u16 beacon_interval, u8 wait)
{
	unsigned long timeout;
	struct cmd_join join = {};
	int ret, i;
	u8 *bssid;

	/* FIXME: this should be in main.c */
	ret = wl12xx_acx_frame_rates(wl, DEFAULT_HW_GEN_TX_RATE,
				     DEFAULT_HW_GEN_MODULATION_TYPE,
				     wl->tx_mgmt_frm_rate,
				     wl->tx_mgmt_frm_mod);
	if (ret < 0)
		return ret;

	wl12xx_debug(DEBUG_CMD, "cmd join");

	/* Reverse order BSSID */
	bssid = (u8 *)&join.bssid_lsb;
	for (i = 0; i < ETH_ALEN; i++)
		bssid[i] = wl->bssid[ETH_ALEN - i - 1];

	join.rx_config_options = wl->rx_config;
	join.rx_filter_options = wl->rx_filter;

	join.basic_rate_set = RATE_MASK_1MBPS | RATE_MASK_2MBPS |
		RATE_MASK_5_5MBPS | RATE_MASK_11MBPS;

	join.beacon_interval = beacon_interval;
	join.dtim_interval = dtim_interval;
	join.bss_type = bss_type;
	join.channel = wl->channel;
	join.ctrl = JOIN_CMD_CTRL_TX_FLUSH;

	ret = wl12xx_cmd_send(wl, CMD_START_JOIN, &join, sizeof(join));
	if (ret < 0) {
		wl12xx_error("failed to initiate cmd join");
		return ret;
	}

	timeout = msecs_to_jiffies(JOIN_TIMEOUT);

	/*
	 * ugly hack: we should wait for JOIN_EVENT_COMPLETE_ID but to
	 * simplify locking we just sleep instead, for now
	 */
	if (wait)
		msleep(10);

	return 0;
}

int wl12xx_cmd_ps_mode(struct wl12xx *wl, u8 ps_mode)
{
	int ret;
	struct acx_ps_params ps_params;

	/* FIXME: this should be in ps.c */
	ret = wl12xx_acx_wake_up_conditions(wl, wl->listen_int);
	if (ret < 0) {
		wl12xx_error("Couldnt set wake up conditions");
		return ret;
	}

	wl12xx_debug(DEBUG_CMD, "cmd set ps mode");

	ps_params.ps_mode = ps_mode;
	ps_params.send_null_data = 1;
	ps_params.retries = 5;
	ps_params.hang_over_period = 128;
	ps_params.null_data_rate = 1; /* 1 Mbps */

	ret = wl12xx_cmd_send(wl, CMD_SET_PS_MODE, &ps_params,
			      sizeof(ps_params));
	if (ret < 0) {
		wl12xx_error("cmd set_ps_mode failed");
		return ret;
	}

	return 0;
}

int wl12xx_cmd_read_memory(struct wl12xx *wl, u32 addr, u32 len, void *answer)
{
	struct cmd_read_write_memory mem_cmd, *mem_answer;
	struct wl12xx_command cmd;
	int ret;

	wl12xx_debug(DEBUG_CMD, "cmd read memory");

	memset(&mem_cmd, 0, sizeof(mem_cmd));
	mem_cmd.addr = addr;
	mem_cmd.size = len;

	ret = wl12xx_cmd_send(wl, CMD_READ_MEMORY, &mem_cmd, sizeof(mem_cmd));
	if (ret < 0) {
		wl12xx_error("read memory command failed: %d", ret);
		return ret;
	}

	/* the read command got in, we can now read the answer */
	wl12xx_spi_mem_read(wl, wl->cmd_box_addr, &cmd,
			    CMDMBOX_HEADER_LEN + sizeof(mem_cmd));

	if (cmd.status != CMD_STATUS_SUCCESS)
		wl12xx_error("error in read command result: %d", cmd.status);

	mem_answer = (struct cmd_read_write_memory *) cmd.parameters;
	memcpy(answer, mem_answer->value, len);

	return 0;
}

int wl12xx_cmd_template_set(struct wl12xx *wl, u16 cmd_id,
			    void *buf, size_t buf_len)
{
	struct wl12xx_cmd_packet_template template;
	int ret;

	wl12xx_debug(DEBUG_CMD, "cmd template %d", cmd_id);

	memset(&template, 0, sizeof(template));

	WARN_ON(buf_len > WL12XX_MAX_TEMPLATE_SIZE);
	buf_len = min_t(size_t, buf_len, WL12XX_MAX_TEMPLATE_SIZE);
	template.size = cpu_to_le16(buf_len);

	if (buf)
		memcpy(template.template, buf, buf_len);

	ret = wl12xx_cmd_send(wl, cmd_id, &template,
			      sizeof(template.size) + buf_len);
	if (ret < 0) {
		wl12xx_warning("cmd set_template failed: %d", ret);
		return ret;
	}

	return 0;
}