blob: 0a02cdde935bf98d54682f14092fad38238b98d9 [file] [log] [blame]
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001#include "cmd.h"
2
3#include <linux/module.h>
4#include <linux/crc7.h>
5#include <linux/spi/spi.h>
6
7#include "wl12xx.h"
8#include "wl12xx_80211.h"
9#include "reg.h"
10#include "spi.h"
11#include "ps.h"
Kalle Valoff258392009-06-12 14:14:19 +030012#include "acx.h"
Kalle Valo2f01a1f2009-04-29 23:33:31 +030013
Kalle Valoff258392009-06-12 14:14:19 +030014/**
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 wl12xx_cmd_send(struct wl12xx *wl, u16 id, void *buf, size_t len)
Kalle Valo2f01a1f2009-04-29 23:33:31 +030023{
Kalle Valoff258392009-06-12 14:14:19 +030024 struct wl12xx_cmd_header *cmd;
Kalle Valo2f01a1f2009-04-29 23:33:31 +030025 unsigned long timeout;
Kalle Valo2f01a1f2009-04-29 23:33:31 +030026 u32 intr;
27 int ret = 0;
28
Kalle Valoff258392009-06-12 14:14:19 +030029 cmd = buf;
30 cmd->id = id;
31 cmd->status = 0;
32
33 WARN_ON(len % 4 != 0);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030034
35 wl12xx_ps_elp_wakeup(wl);
36
Kalle Valoff258392009-06-12 14:14:19 +030037 wl12xx_spi_mem_write(wl, wl->cmd_box_addr, buf, len);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030038
39 wl12xx_reg_write32(wl, ACX_REG_INTERRUPT_TRIG, INTR_TRIG_CMD);
40
41 timeout = jiffies + msecs_to_jiffies(WL12XX_COMMAND_TIMEOUT);
42
43 intr = wl12xx_reg_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
44 while (!(intr & wl->chip.intr_cmd_complete)) {
45 if (time_after(jiffies, timeout)) {
46 wl12xx_error("command complete timeout");
47 ret = -ETIMEDOUT;
48 goto out;
49 }
50
51 msleep(1);
52
53 intr = wl12xx_reg_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
54 }
55
56 wl12xx_reg_write32(wl, ACX_REG_INTERRUPT_ACK,
57 wl->chip.intr_cmd_complete);
58
59out:
60 wl12xx_ps_elp_sleep(wl);
61
62 return ret;
63}
64
Kalle Valoff258392009-06-12 14:14:19 +030065/**
66 * send test command to firmware
67 *
68 * @wl: wl struct
69 * @buf: buffer containing the command, without headers, no dma requirements
70 * @len: length of the buffer
71 * @answer: is answer needed
72 *
73 * FIXME: cmd_test users need to be converted to the new interface
74 */
Kalle Valo2f01a1f2009-04-29 23:33:31 +030075int wl12xx_cmd_test(struct wl12xx *wl, void *buf, size_t buf_len, u8 answer)
76{
Kalle Valoff258392009-06-12 14:14:19 +030077 struct wl12xx_command *cmd;
78 size_t cmd_len;
Kalle Valo2f01a1f2009-04-29 23:33:31 +030079 int ret;
80
81 wl12xx_debug(DEBUG_CMD, "cmd test");
82
Kalle Valoff258392009-06-12 14:14:19 +030083 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
84 if (!cmd) {
85 ret = -ENOMEM;
86 goto out;
87 }
88
89 memcpy(cmd->parameters, buf, buf_len);
90
91 /* FIXME: ugly */
92 cmd_len = sizeof(struct wl12xx_cmd_header) + buf_len;
93
94 ret = wl12xx_cmd_send(wl, CMD_TEST, cmd, cmd_len);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030095 if (ret < 0) {
96 wl12xx_warning("TEST command failed");
Kalle Valoff258392009-06-12 14:14:19 +030097 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +030098 }
99
100 if (answer) {
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300101 /*
102 * The test command got in, we can read the answer.
103 * The answer would be a wl12xx_command, where the
104 * parameter array contains the actual answer.
105 */
106
107 wl12xx_ps_elp_wakeup(wl);
108
Kalle Valoff258392009-06-12 14:14:19 +0300109 wl12xx_spi_mem_read(wl, wl->cmd_box_addr, cmd, cmd_len);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300110
111 wl12xx_ps_elp_sleep(wl);
112
Kalle Valoff258392009-06-12 14:14:19 +0300113 if (cmd->header.status != CMD_STATUS_SUCCESS)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300114 wl12xx_error("TEST command answer error: %d",
Kalle Valoff258392009-06-12 14:14:19 +0300115 cmd->header.status);
116 memcpy(buf, cmd->parameters, buf_len);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300117 }
118
Kalle Valoff258392009-06-12 14:14:19 +0300119out:
120 kfree(cmd);
121 return ret;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300122}
123
Kalle Valoff258392009-06-12 14:14:19 +0300124/**
125 * read acx from firmware
126 *
127 * @wl: wl struct
128 * @id: acx id
129 * @buf: buffer for the response, including all headers, must work with dma
130 * @len: lenght of buf
131 */
132int wl12xx_cmd_interrogate(struct wl12xx *wl, u16 id, void *buf, size_t len)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300133{
Kalle Valoff258392009-06-12 14:14:19 +0300134 struct acx_header *acx = buf;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300135 int ret;
136
137 wl12xx_debug(DEBUG_CMD, "cmd interrogate");
138
Kalle Valoff258392009-06-12 14:14:19 +0300139 acx->id = id;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300140
Kalle Valoff258392009-06-12 14:14:19 +0300141 /* payload length, does not include any headers */
142 acx->len = len - sizeof(*acx);
143
144 ret = wl12xx_cmd_send(wl, CMD_INTERROGATE, acx, sizeof(*acx));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300145 if (ret < 0) {
146 wl12xx_error("INTERROGATE command failed");
Kalle Valoff258392009-06-12 14:14:19 +0300147 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300148 }
149
150 wl12xx_ps_elp_wakeup(wl);
151
152 /* the interrogate command got in, we can read the answer */
Kalle Valoff258392009-06-12 14:14:19 +0300153 wl12xx_spi_mem_read(wl, wl->cmd_box_addr, buf, len);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300154
155 wl12xx_ps_elp_sleep(wl);
156
Kalle Valoff258392009-06-12 14:14:19 +0300157 acx = buf;
158 if (acx->cmd.status != CMD_STATUS_SUCCESS)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300159 wl12xx_error("INTERROGATE command error: %d",
Kalle Valoff258392009-06-12 14:14:19 +0300160 acx->cmd.status);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300161
Kalle Valoff258392009-06-12 14:14:19 +0300162out:
163 return ret;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300164}
165
Kalle Valoff258392009-06-12 14:14:19 +0300166/**
167 * write acx value to firmware
168 *
169 * @wl: wl struct
170 * @id: acx id
171 * @buf: buffer containing acx, including all headers, must work with dma
172 * @len: length of buf
173 */
174int wl12xx_cmd_configure(struct wl12xx *wl, u16 id, void *buf, size_t len)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300175{
Kalle Valoff258392009-06-12 14:14:19 +0300176 struct acx_header *acx = buf;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300177 int ret;
178
179 wl12xx_debug(DEBUG_CMD, "cmd configure");
180
Kalle Valoff258392009-06-12 14:14:19 +0300181 acx->id = id;
182
183 /* payload length, does not include any headers */
184 acx->len = len - sizeof(*acx);
185
186 ret = wl12xx_cmd_send(wl, CMD_CONFIGURE, acx, len);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300187 if (ret < 0) {
188 wl12xx_warning("CONFIGURE command NOK");
189 return ret;
190 }
191
192 return 0;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300193}
194
195int wl12xx_cmd_vbm(struct wl12xx *wl, u8 identity,
196 void *bitmap, u16 bitmap_len, u8 bitmap_control)
197{
Kalle Valoff258392009-06-12 14:14:19 +0300198 struct wl12xx_cmd_vbm_update *vbm;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300199 int ret;
200
201 wl12xx_debug(DEBUG_CMD, "cmd vbm");
202
Kalle Valoff258392009-06-12 14:14:19 +0300203 vbm = kzalloc(sizeof(*vbm), GFP_KERNEL);
204 if (!vbm) {
205 ret = -ENOMEM;
206 goto out;
207 }
208
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300209 /* Count and period will be filled by the target */
Kalle Valoff258392009-06-12 14:14:19 +0300210 vbm->tim.bitmap_ctrl = bitmap_control;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300211 if (bitmap_len > PARTIAL_VBM_MAX) {
212 wl12xx_warning("cmd vbm len is %d B, truncating to %d",
213 bitmap_len, PARTIAL_VBM_MAX);
214 bitmap_len = PARTIAL_VBM_MAX;
215 }
Kalle Valoff258392009-06-12 14:14:19 +0300216 memcpy(vbm->tim.pvb_field, bitmap, bitmap_len);
217 vbm->tim.identity = identity;
218 vbm->tim.length = bitmap_len + 3;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300219
Kalle Valoff258392009-06-12 14:14:19 +0300220 vbm->len = cpu_to_le16(bitmap_len + 5);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300221
Kalle Valoff258392009-06-12 14:14:19 +0300222 ret = wl12xx_cmd_send(wl, CMD_VBM, vbm, sizeof(*vbm));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300223 if (ret < 0) {
224 wl12xx_error("VBM command failed");
Kalle Valoff258392009-06-12 14:14:19 +0300225 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300226 }
227
Kalle Valoff258392009-06-12 14:14:19 +0300228out:
229 kfree(vbm);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300230 return 0;
231}
232
Kalle Valoff258392009-06-12 14:14:19 +0300233int wl12xx_cmd_data_path(struct wl12xx *wl, u8 channel, bool enable)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300234{
Kalle Valoff258392009-06-12 14:14:19 +0300235 struct cmd_enabledisable_path *cmd;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300236 int ret;
237 u16 cmd_rx, cmd_tx;
238
239 wl12xx_debug(DEBUG_CMD, "cmd data path");
240
Kalle Valoff258392009-06-12 14:14:19 +0300241 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
242 if (!cmd) {
243 ret = -ENOMEM;
244 goto out;
245 }
246
247 cmd->channel = channel;
248
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300249 if (enable) {
250 cmd_rx = CMD_ENABLE_RX;
251 cmd_tx = CMD_ENABLE_TX;
252 } else {
253 cmd_rx = CMD_DISABLE_RX;
254 cmd_tx = CMD_DISABLE_TX;
255 }
256
Kalle Valoff258392009-06-12 14:14:19 +0300257 ret = wl12xx_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300258 if (ret < 0) {
259 wl12xx_error("rx %s cmd for channel %d failed",
260 enable ? "start" : "stop", channel);
Kalle Valoff258392009-06-12 14:14:19 +0300261 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300262 }
263
264 wl12xx_debug(DEBUG_BOOT, "rx %s cmd channel %d",
265 enable ? "start" : "stop", channel);
266
Kalle Valoff258392009-06-12 14:14:19 +0300267 ret = wl12xx_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300268 if (ret < 0) {
269 wl12xx_error("tx %s cmd for channel %d failed",
270 enable ? "start" : "stop", channel);
271 return ret;
272 }
273
274 wl12xx_debug(DEBUG_BOOT, "tx %s cmd channel %d",
275 enable ? "start" : "stop", channel);
276
Kalle Valoff258392009-06-12 14:14:19 +0300277out:
278 kfree(cmd);
279 return ret;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300280}
281
282int wl12xx_cmd_join(struct wl12xx *wl, u8 bss_type, u8 dtim_interval,
283 u16 beacon_interval, u8 wait)
284{
285 unsigned long timeout;
Kalle Valoff258392009-06-12 14:14:19 +0300286 struct cmd_join *join;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300287 int ret, i;
288 u8 *bssid;
289
Kalle Valoff258392009-06-12 14:14:19 +0300290 join = kzalloc(sizeof(*join), GFP_KERNEL);
291 if (!join) {
292 ret = -ENOMEM;
293 goto out;
294 }
295
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300296 /* FIXME: this should be in main.c */
297 ret = wl12xx_acx_frame_rates(wl, DEFAULT_HW_GEN_TX_RATE,
298 DEFAULT_HW_GEN_MODULATION_TYPE,
299 wl->tx_mgmt_frm_rate,
300 wl->tx_mgmt_frm_mod);
301 if (ret < 0)
Kalle Valoff258392009-06-12 14:14:19 +0300302 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300303
304 wl12xx_debug(DEBUG_CMD, "cmd join");
305
306 /* Reverse order BSSID */
Kalle Valoff258392009-06-12 14:14:19 +0300307 bssid = (u8 *) &join->bssid_lsb;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300308 for (i = 0; i < ETH_ALEN; i++)
309 bssid[i] = wl->bssid[ETH_ALEN - i - 1];
310
Kalle Valoff258392009-06-12 14:14:19 +0300311 join->rx_config_options = wl->rx_config;
312 join->rx_filter_options = wl->rx_filter;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300313
Kalle Valoff258392009-06-12 14:14:19 +0300314 join->basic_rate_set = RATE_MASK_1MBPS | RATE_MASK_2MBPS |
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300315 RATE_MASK_5_5MBPS | RATE_MASK_11MBPS;
316
Kalle Valoff258392009-06-12 14:14:19 +0300317 join->beacon_interval = beacon_interval;
318 join->dtim_interval = dtim_interval;
319 join->bss_type = bss_type;
320 join->channel = wl->channel;
321 join->ctrl = JOIN_CMD_CTRL_TX_FLUSH;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300322
Kalle Valoff258392009-06-12 14:14:19 +0300323 ret = wl12xx_cmd_send(wl, CMD_START_JOIN, join, sizeof(*join));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300324 if (ret < 0) {
325 wl12xx_error("failed to initiate cmd join");
Kalle Valoff258392009-06-12 14:14:19 +0300326 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300327 }
328
329 timeout = msecs_to_jiffies(JOIN_TIMEOUT);
330
331 /*
332 * ugly hack: we should wait for JOIN_EVENT_COMPLETE_ID but to
333 * simplify locking we just sleep instead, for now
334 */
335 if (wait)
336 msleep(10);
337
Kalle Valoff258392009-06-12 14:14:19 +0300338out:
339 kfree(join);
340 return ret;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300341}
342
343int wl12xx_cmd_ps_mode(struct wl12xx *wl, u8 ps_mode)
344{
Kalle Valoff258392009-06-12 14:14:19 +0300345 struct wl12xx_cmd_ps_params *ps_params = NULL;
346 int ret = 0;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300347
348 /* FIXME: this should be in ps.c */
349 ret = wl12xx_acx_wake_up_conditions(wl, wl->listen_int);
350 if (ret < 0) {
Kalle Valoff258392009-06-12 14:14:19 +0300351 wl12xx_error("couldn't set wake up conditions");
352 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300353 }
354
355 wl12xx_debug(DEBUG_CMD, "cmd set ps mode");
356
Kalle Valoff258392009-06-12 14:14:19 +0300357 ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
358 if (!ps_params) {
359 ret = -ENOMEM;
360 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300361 }
362
Kalle Valoff258392009-06-12 14:14:19 +0300363 ps_params->ps_mode = ps_mode;
364 ps_params->send_null_data = 1;
365 ps_params->retries = 5;
366 ps_params->hang_over_period = 128;
367 ps_params->null_data_rate = 1; /* 1 Mbps */
368
369 ret = wl12xx_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
370 sizeof(*ps_params));
371 if (ret < 0) {
372 wl12xx_error("cmd set_ps_mode failed");
373 goto out;
374 }
375
376out:
377 kfree(ps_params);
378 return ret;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300379}
380
Kalle Valoff258392009-06-12 14:14:19 +0300381int wl12xx_cmd_read_memory(struct wl12xx *wl, u32 addr, void *answer,
382 size_t len)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300383{
Kalle Valoff258392009-06-12 14:14:19 +0300384 struct cmd_read_write_memory *cmd;
385 int ret = 0;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300386
387 wl12xx_debug(DEBUG_CMD, "cmd read memory");
388
Kalle Valoff258392009-06-12 14:14:19 +0300389 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
390 if (!cmd) {
391 ret = -ENOMEM;
392 goto out;
393 }
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300394
Kalle Valoff258392009-06-12 14:14:19 +0300395 WARN_ON(len > MAX_READ_SIZE);
396 len = min_t(size_t, len, MAX_READ_SIZE);
397
398 cmd->addr = addr;
399 cmd->size = len;
400
401 ret = wl12xx_cmd_send(wl, CMD_READ_MEMORY, cmd, sizeof(*cmd));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300402 if (ret < 0) {
403 wl12xx_error("read memory command failed: %d", ret);
Kalle Valoff258392009-06-12 14:14:19 +0300404 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300405 }
406
407 /* the read command got in, we can now read the answer */
Kalle Valoff258392009-06-12 14:14:19 +0300408 wl12xx_spi_mem_read(wl, wl->cmd_box_addr, cmd, sizeof(*cmd));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300409
Kalle Valoff258392009-06-12 14:14:19 +0300410 if (cmd->header.status != CMD_STATUS_SUCCESS)
411 wl12xx_error("error in read command result: %d",
412 cmd->header.status);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300413
Kalle Valoff258392009-06-12 14:14:19 +0300414 memcpy(answer, cmd->value, len);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300415
Kalle Valoff258392009-06-12 14:14:19 +0300416out:
417 kfree(cmd);
418 return ret;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300419}
420
421int wl12xx_cmd_template_set(struct wl12xx *wl, u16 cmd_id,
422 void *buf, size_t buf_len)
423{
Kalle Valoff258392009-06-12 14:14:19 +0300424 struct wl12xx_cmd_packet_template *cmd;
425 size_t cmd_len;
426 int ret = 0;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300427
428 wl12xx_debug(DEBUG_CMD, "cmd template %d", cmd_id);
429
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300430 WARN_ON(buf_len > WL12XX_MAX_TEMPLATE_SIZE);
431 buf_len = min_t(size_t, buf_len, WL12XX_MAX_TEMPLATE_SIZE);
Kalle Valoff258392009-06-12 14:14:19 +0300432 cmd_len = ALIGN(sizeof(*cmd) + buf_len, 4);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300433
Kalle Valoff258392009-06-12 14:14:19 +0300434 cmd = kzalloc(cmd_len, GFP_KERNEL);
435 if (!cmd) {
436 ret = -ENOMEM;
437 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300438 }
439
Kalle Valoff258392009-06-12 14:14:19 +0300440 cmd->size = cpu_to_le16(buf_len);
441
442 if (buf)
443 memcpy(cmd->data, buf, buf_len);
444
445 ret = wl12xx_cmd_send(wl, cmd_id, cmd, cmd_len);
446 if (ret < 0) {
447 wl12xx_warning("cmd set_template failed: %d", ret);
448 goto out;
449 }
450
451out:
452 kfree(cmd);
453 return ret;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300454}