cmd.c revision 64322e28d3f91a196c0e86de2790cdd338a74c28
1#include "cmd.h"
2
3#include <linux/module.h>
4#include <linux/slab.h>
5#include <linux/crc7.h>
6#include <linux/etherdevice.h>
7
8#include "wl1251.h"
9#include "reg.h"
10#include "io.h"
11#include "ps.h"
12#include "acx.h"
13
14/**
15 * send command to firmware
16 *
17 * @wl: wl struct
18 * @id: command id
19 * @buf: buffer containing the command, must work with dma
20 * @len: length of the buffer
21 */
22int wl1251_cmd_send(struct wl1251 *wl, u16 id, void *buf, size_t len)
23{
24	struct wl1251_cmd_header *cmd;
25	unsigned long timeout;
26	u32 intr;
27	int ret = 0;
28
29	cmd = buf;
30	cmd->id = id;
31	cmd->status = 0;
32
33	WARN_ON(len % 4 != 0);
34
35	wl1251_mem_write(wl, wl->cmd_box_addr, buf, len);
36
37	wl1251_reg_write32(wl, ACX_REG_INTERRUPT_TRIG, INTR_TRIG_CMD);
38
39	timeout = jiffies + msecs_to_jiffies(WL1251_COMMAND_TIMEOUT);
40
41	intr = wl1251_reg_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
42	while (!(intr & WL1251_ACX_INTR_CMD_COMPLETE)) {
43		if (time_after(jiffies, timeout)) {
44			wl1251_error("command complete timeout");
45			ret = -ETIMEDOUT;
46			goto out;
47		}
48
49		msleep(1);
50
51		intr = wl1251_reg_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
52	}
53
54	wl1251_reg_write32(wl, ACX_REG_INTERRUPT_ACK,
55			   WL1251_ACX_INTR_CMD_COMPLETE);
56
57out:
58	return ret;
59}
60
61/**
62 * send test command to firmware
63 *
64 * @wl: wl struct
65 * @buf: buffer containing the command, with all headers, must work with dma
66 * @len: length of the buffer
67 * @answer: is answer needed
68 */
69int wl1251_cmd_test(struct wl1251 *wl, void *buf, size_t buf_len, u8 answer)
70{
71	int ret;
72
73	wl1251_debug(DEBUG_CMD, "cmd test");
74
75	ret = wl1251_cmd_send(wl, CMD_TEST, buf, buf_len);
76
77	if (ret < 0) {
78		wl1251_warning("TEST command failed");
79		return ret;
80	}
81
82	if (answer) {
83		struct wl1251_command *cmd_answer;
84
85		/*
86		 * The test command got in, we can read the answer.
87		 * The answer would be a wl1251_command, where the
88		 * parameter array contains the actual answer.
89		 */
90		wl1251_mem_read(wl, wl->cmd_box_addr, buf, buf_len);
91
92		cmd_answer = buf;
93
94		if (cmd_answer->header.status != CMD_STATUS_SUCCESS)
95			wl1251_error("TEST command answer error: %d",
96				     cmd_answer->header.status);
97	}
98
99	return 0;
100}
101
102/**
103 * read acx from firmware
104 *
105 * @wl: wl struct
106 * @id: acx id
107 * @buf: buffer for the response, including all headers, must work with dma
108 * @len: length of buf
109 */
110int wl1251_cmd_interrogate(struct wl1251 *wl, u16 id, void *buf, size_t len)
111{
112	struct acx_header *acx = buf;
113	int ret;
114
115	wl1251_debug(DEBUG_CMD, "cmd interrogate");
116
117	acx->id = id;
118
119	/* payload length, does not include any headers */
120	acx->len = len - sizeof(*acx);
121
122	ret = wl1251_cmd_send(wl, CMD_INTERROGATE, acx, sizeof(*acx));
123	if (ret < 0) {
124		wl1251_error("INTERROGATE command failed");
125		goto out;
126	}
127
128	/* the interrogate command got in, we can read the answer */
129	wl1251_mem_read(wl, wl->cmd_box_addr, buf, len);
130
131	acx = buf;
132	if (acx->cmd.status != CMD_STATUS_SUCCESS)
133		wl1251_error("INTERROGATE command error: %d",
134			     acx->cmd.status);
135
136out:
137	return ret;
138}
139
140/**
141 * write acx value to firmware
142 *
143 * @wl: wl struct
144 * @id: acx id
145 * @buf: buffer containing acx, including all headers, must work with dma
146 * @len: length of buf
147 */
148int wl1251_cmd_configure(struct wl1251 *wl, u16 id, void *buf, size_t len)
149{
150	struct acx_header *acx = buf;
151	int ret;
152
153	wl1251_debug(DEBUG_CMD, "cmd configure");
154
155	acx->id = id;
156
157	/* payload length, does not include any headers */
158	acx->len = len - sizeof(*acx);
159
160	ret = wl1251_cmd_send(wl, CMD_CONFIGURE, acx, len);
161	if (ret < 0) {
162		wl1251_warning("CONFIGURE command NOK");
163		return ret;
164	}
165
166	return 0;
167}
168
169int wl1251_cmd_vbm(struct wl1251 *wl, u8 identity,
170		   void *bitmap, u16 bitmap_len, u8 bitmap_control)
171{
172	struct wl1251_cmd_vbm_update *vbm;
173	int ret;
174
175	wl1251_debug(DEBUG_CMD, "cmd vbm");
176
177	vbm = kzalloc(sizeof(*vbm), GFP_KERNEL);
178	if (!vbm) {
179		ret = -ENOMEM;
180		goto out;
181	}
182
183	/* Count and period will be filled by the target */
184	vbm->tim.bitmap_ctrl = bitmap_control;
185	if (bitmap_len > PARTIAL_VBM_MAX) {
186		wl1251_warning("cmd vbm len is %d B, truncating to %d",
187			       bitmap_len, PARTIAL_VBM_MAX);
188		bitmap_len = PARTIAL_VBM_MAX;
189	}
190	memcpy(vbm->tim.pvb_field, bitmap, bitmap_len);
191	vbm->tim.identity = identity;
192	vbm->tim.length = bitmap_len + 3;
193
194	vbm->len = cpu_to_le16(bitmap_len + 5);
195
196	ret = wl1251_cmd_send(wl, CMD_VBM, vbm, sizeof(*vbm));
197	if (ret < 0) {
198		wl1251_error("VBM command failed");
199		goto out;
200	}
201
202out:
203	kfree(vbm);
204	return ret;
205}
206
207int wl1251_cmd_data_path(struct wl1251 *wl, u8 channel, bool enable)
208{
209	struct cmd_enabledisable_path *cmd;
210	int ret;
211	u16 cmd_rx, cmd_tx;
212
213	wl1251_debug(DEBUG_CMD, "cmd data path");
214
215	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
216	if (!cmd) {
217		ret = -ENOMEM;
218		goto out;
219	}
220
221	cmd->channel = channel;
222
223	if (enable) {
224		cmd_rx = CMD_ENABLE_RX;
225		cmd_tx = CMD_ENABLE_TX;
226	} else {
227		cmd_rx = CMD_DISABLE_RX;
228		cmd_tx = CMD_DISABLE_TX;
229	}
230
231	ret = wl1251_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd));
232	if (ret < 0) {
233		wl1251_error("rx %s cmd for channel %d failed",
234			     enable ? "start" : "stop", channel);
235		goto out;
236	}
237
238	wl1251_debug(DEBUG_BOOT, "rx %s cmd channel %d",
239		     enable ? "start" : "stop", channel);
240
241	ret = wl1251_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd));
242	if (ret < 0) {
243		wl1251_error("tx %s cmd for channel %d failed",
244			     enable ? "start" : "stop", channel);
245		goto out;
246	}
247
248	wl1251_debug(DEBUG_BOOT, "tx %s cmd channel %d",
249		     enable ? "start" : "stop", channel);
250
251out:
252	kfree(cmd);
253	return ret;
254}
255
256int wl1251_cmd_join(struct wl1251 *wl, u8 bss_type, u8 channel,
257		    u16 beacon_interval, u8 dtim_interval)
258{
259	struct cmd_join *join;
260	int ret, i;
261	u8 *bssid;
262
263	join = kzalloc(sizeof(*join), GFP_KERNEL);
264	if (!join) {
265		ret = -ENOMEM;
266		goto out;
267	}
268
269	wl1251_debug(DEBUG_CMD, "cmd join%s ch %d %d/%d",
270		     bss_type == BSS_TYPE_IBSS ? " ibss" : "",
271		     channel, beacon_interval, dtim_interval);
272
273	/* Reverse order BSSID */
274	bssid = (u8 *) &join->bssid_lsb;
275	for (i = 0; i < ETH_ALEN; i++)
276		bssid[i] = wl->bssid[ETH_ALEN - i - 1];
277
278	join->rx_config_options = wl->rx_config;
279	join->rx_filter_options = wl->rx_filter;
280
281	join->basic_rate_set = RATE_MASK_1MBPS | RATE_MASK_2MBPS |
282		RATE_MASK_5_5MBPS | RATE_MASK_11MBPS;
283
284	join->beacon_interval = beacon_interval;
285	join->dtim_interval = dtim_interval;
286	join->bss_type = bss_type;
287	join->channel = channel;
288	join->ctrl = JOIN_CMD_CTRL_TX_FLUSH;
289
290	ret = wl1251_cmd_send(wl, CMD_START_JOIN, join, sizeof(*join));
291	if (ret < 0) {
292		wl1251_error("failed to initiate cmd join");
293		goto out;
294	}
295
296out:
297	kfree(join);
298	return ret;
299}
300
301int wl1251_cmd_ps_mode(struct wl1251 *wl, u8 ps_mode)
302{
303	struct wl1251_cmd_ps_params *ps_params = NULL;
304	int ret = 0;
305
306	wl1251_debug(DEBUG_CMD, "cmd set ps mode");
307
308	ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
309	if (!ps_params) {
310		ret = -ENOMEM;
311		goto out;
312	}
313
314	ps_params->ps_mode = ps_mode;
315	ps_params->send_null_data = 1;
316	ps_params->retries = 5;
317	ps_params->hang_over_period = 128;
318	ps_params->null_data_rate = 1; /* 1 Mbps */
319
320	ret = wl1251_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
321			      sizeof(*ps_params));
322	if (ret < 0) {
323		wl1251_error("cmd set_ps_mode failed");
324		goto out;
325	}
326
327out:
328	kfree(ps_params);
329	return ret;
330}
331
332int wl1251_cmd_read_memory(struct wl1251 *wl, u32 addr, void *answer,
333			   size_t len)
334{
335	struct cmd_read_write_memory *cmd;
336	int ret = 0;
337
338	wl1251_debug(DEBUG_CMD, "cmd read memory");
339
340	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
341	if (!cmd) {
342		ret = -ENOMEM;
343		goto out;
344	}
345
346	WARN_ON(len > MAX_READ_SIZE);
347	len = min_t(size_t, len, MAX_READ_SIZE);
348
349	cmd->addr = addr;
350	cmd->size = len;
351
352	ret = wl1251_cmd_send(wl, CMD_READ_MEMORY, cmd, sizeof(*cmd));
353	if (ret < 0) {
354		wl1251_error("read memory command failed: %d", ret);
355		goto out;
356	}
357
358	/* the read command got in, we can now read the answer */
359	wl1251_mem_read(wl, wl->cmd_box_addr, cmd, sizeof(*cmd));
360
361	if (cmd->header.status != CMD_STATUS_SUCCESS)
362		wl1251_error("error in read command result: %d",
363			     cmd->header.status);
364
365	memcpy(answer, cmd->value, len);
366
367out:
368	kfree(cmd);
369	return ret;
370}
371
372int wl1251_cmd_template_set(struct wl1251 *wl, u16 cmd_id,
373			    void *buf, size_t buf_len)
374{
375	struct wl1251_cmd_packet_template *cmd;
376	size_t cmd_len;
377	int ret = 0;
378
379	wl1251_debug(DEBUG_CMD, "cmd template %d", cmd_id);
380
381	WARN_ON(buf_len > WL1251_MAX_TEMPLATE_SIZE);
382	buf_len = min_t(size_t, buf_len, WL1251_MAX_TEMPLATE_SIZE);
383	cmd_len = ALIGN(sizeof(*cmd) + buf_len, 4);
384
385	cmd = kzalloc(cmd_len, GFP_KERNEL);
386	if (!cmd) {
387		ret = -ENOMEM;
388		goto out;
389	}
390
391	cmd->size = cpu_to_le16(buf_len);
392
393	if (buf)
394		memcpy(cmd->data, buf, buf_len);
395
396	ret = wl1251_cmd_send(wl, cmd_id, cmd, cmd_len);
397	if (ret < 0) {
398		wl1251_warning("cmd set_template failed: %d", ret);
399		goto out;
400	}
401
402out:
403	kfree(cmd);
404	return ret;
405}
406
407int wl1251_cmd_scan(struct wl1251 *wl, u8 *ssid, size_t ssid_len,
408		    struct ieee80211_channel *channels[],
409		    unsigned int n_channels, unsigned int n_probes)
410{
411	struct wl1251_cmd_scan *cmd;
412	int i, ret = 0;
413
414	wl1251_debug(DEBUG_CMD, "cmd scan channels %d", n_channels);
415
416	WARN_ON(n_channels > SCAN_MAX_NUM_OF_CHANNELS);
417
418	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
419	if (!cmd)
420		return -ENOMEM;
421
422	cmd->params.rx_config_options = cpu_to_le32(CFG_RX_ALL_GOOD);
423	cmd->params.rx_filter_options = cpu_to_le32(CFG_RX_PRSP_EN |
424						    CFG_RX_MGMT_EN |
425						    CFG_RX_BCN_EN);
426	cmd->params.scan_options = 0;
427	/*
428	 * Use high priority scan when not associated to prevent fw issue
429	 * causing never-ending scans (sometimes 20+ minutes).
430	 * Note: This bug may be caused by the fw's DTIM handling.
431	 */
432	if (is_zero_ether_addr(wl->bssid))
433		cmd->params.scan_options |= WL1251_SCAN_OPT_PRIORITY_HIGH;
434	cmd->params.num_channels = n_channels;
435	cmd->params.num_probe_requests = n_probes;
436	cmd->params.tx_rate = cpu_to_le16(1 << 1); /* 2 Mbps */
437	cmd->params.tid_trigger = 0;
438
439	for (i = 0; i < n_channels; i++) {
440		cmd->channels[i].min_duration =
441			cpu_to_le32(WL1251_SCAN_MIN_DURATION);
442		cmd->channels[i].max_duration =
443			cpu_to_le32(WL1251_SCAN_MAX_DURATION);
444		memset(&cmd->channels[i].bssid_lsb, 0xff, 4);
445		memset(&cmd->channels[i].bssid_msb, 0xff, 2);
446		cmd->channels[i].early_termination = 0;
447		cmd->channels[i].tx_power_att = 0;
448		cmd->channels[i].channel = channels[i]->hw_value;
449	}
450
451	cmd->params.ssid_len = ssid_len;
452	if (ssid)
453		memcpy(cmd->params.ssid, ssid, ssid_len);
454
455	ret = wl1251_cmd_send(wl, CMD_SCAN, cmd, sizeof(*cmd));
456	if (ret < 0) {
457		wl1251_error("cmd scan failed: %d", ret);
458		goto out;
459	}
460
461	wl1251_mem_read(wl, wl->cmd_box_addr, cmd, sizeof(*cmd));
462
463	if (cmd->header.status != CMD_STATUS_SUCCESS) {
464		wl1251_error("cmd scan status wasn't success: %d",
465			     cmd->header.status);
466		ret = -EIO;
467		goto out;
468	}
469
470out:
471	kfree(cmd);
472	return ret;
473}
474
475int wl1251_cmd_trigger_scan_to(struct wl1251 *wl, u32 timeout)
476{
477	struct wl1251_cmd_trigger_scan_to *cmd;
478	int ret;
479
480	wl1251_debug(DEBUG_CMD, "cmd trigger scan to");
481
482	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
483	if (!cmd)
484		return -ENOMEM;
485
486	cmd->timeout = timeout;
487
488	ret = wl1251_cmd_send(wl, CMD_TRIGGER_SCAN_TO, cmd, sizeof(*cmd));
489	if (ret < 0) {
490		wl1251_error("cmd trigger scan to failed: %d", ret);
491		goto out;
492	}
493
494out:
495	kfree(cmd);
496	return ret;
497}
498