blob: 92c7fb6853eb7c24a5eda133ba0febafcde9e6ea [file] [log] [blame]
Kalle Valoef2f8d42009-06-12 14:17:19 +03001#include "wl1251_cmd.h"
Kalle Valo2f01a1f2009-04-29 23:33:31 +03002
3#include <linux/module.h>
4#include <linux/crc7.h>
5#include <linux/spi/spi.h>
6
Kalle Valo13674112009-06-12 14:17:25 +03007#include "wl1251.h"
Kalle Valo2f01a1f2009-04-29 23:33:31 +03008#include "wl12xx_80211.h"
9#include "reg.h"
Kalle Valoef2f8d42009-06-12 14:17:19 +030010#include "wl1251_spi.h"
11#include "wl1251_ps.h"
12#include "wl1251_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
Kalle Valoff258392009-06-12 14:14:19 +030035 wl12xx_spi_mem_write(wl, wl->cmd_box_addr, buf, len);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030036
37 wl12xx_reg_write32(wl, ACX_REG_INTERRUPT_TRIG, INTR_TRIG_CMD);
38
39 timeout = jiffies + msecs_to_jiffies(WL12XX_COMMAND_TIMEOUT);
40
41 intr = wl12xx_reg_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
42 while (!(intr & wl->chip.intr_cmd_complete)) {
43 if (time_after(jiffies, timeout)) {
44 wl12xx_error("command complete timeout");
45 ret = -ETIMEDOUT;
46 goto out;
47 }
48
49 msleep(1);
50
51 intr = wl12xx_reg_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
52 }
53
54 wl12xx_reg_write32(wl, ACX_REG_INTERRUPT_ACK,
55 wl->chip.intr_cmd_complete);
56
57out:
Kalle Valo2f01a1f2009-04-29 23:33:31 +030058 return ret;
59}
60
Kalle Valoff258392009-06-12 14:14:19 +030061/**
62 * send test command to firmware
63 *
64 * @wl: wl struct
Ari Kauppi6021b282009-06-12 14:16:13 +030065 * @buf: buffer containing the command, with all headers, must work with dma
Kalle Valoff258392009-06-12 14:14:19 +030066 * @len: length of the buffer
67 * @answer: is answer needed
Kalle Valoff258392009-06-12 14:14:19 +030068 */
Kalle Valo2f01a1f2009-04-29 23:33:31 +030069int wl12xx_cmd_test(struct wl12xx *wl, void *buf, size_t buf_len, u8 answer)
70{
71 int ret;
72
73 wl12xx_debug(DEBUG_CMD, "cmd test");
74
Ari Kauppi6021b282009-06-12 14:16:13 +030075 ret = wl12xx_cmd_send(wl, CMD_TEST, buf, buf_len);
Kalle Valoff258392009-06-12 14:14:19 +030076
Kalle Valo2f01a1f2009-04-29 23:33:31 +030077 if (ret < 0) {
78 wl12xx_warning("TEST command failed");
Ari Kauppi6021b282009-06-12 14:16:13 +030079 return ret;
Kalle Valo2f01a1f2009-04-29 23:33:31 +030080 }
81
82 if (answer) {
Ari Kauppi6021b282009-06-12 14:16:13 +030083 struct wl12xx_command *cmd_answer;
84
Kalle Valo2f01a1f2009-04-29 23:33:31 +030085 /*
86 * The test command got in, we can read the answer.
87 * The answer would be a wl12xx_command, where the
88 * parameter array contains the actual answer.
89 */
Ari Kauppi6021b282009-06-12 14:16:13 +030090 wl12xx_spi_mem_read(wl, wl->cmd_box_addr, buf, buf_len);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030091
Ari Kauppi6021b282009-06-12 14:16:13 +030092 cmd_answer = buf;
93
94 if (cmd_answer->header.status != CMD_STATUS_SUCCESS)
Kalle Valo2f01a1f2009-04-29 23:33:31 +030095 wl12xx_error("TEST command answer error: %d",
Ari Kauppi6021b282009-06-12 14:16:13 +030096 cmd_answer->header.status);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030097 }
98
Ari Kauppi6021b282009-06-12 14:16:13 +030099 return 0;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300100}
101
Kalle Valoff258392009-06-12 14:14:19 +0300102/**
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: lenght of buf
109 */
110int wl12xx_cmd_interrogate(struct wl12xx *wl, u16 id, void *buf, size_t len)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300111{
Kalle Valoff258392009-06-12 14:14:19 +0300112 struct acx_header *acx = buf;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300113 int ret;
114
115 wl12xx_debug(DEBUG_CMD, "cmd interrogate");
116
Kalle Valoff258392009-06-12 14:14:19 +0300117 acx->id = id;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300118
Kalle Valoff258392009-06-12 14:14:19 +0300119 /* payload length, does not include any headers */
120 acx->len = len - sizeof(*acx);
121
122 ret = wl12xx_cmd_send(wl, CMD_INTERROGATE, acx, sizeof(*acx));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300123 if (ret < 0) {
124 wl12xx_error("INTERROGATE command failed");
Kalle Valoff258392009-06-12 14:14:19 +0300125 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300126 }
127
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300128 /* the interrogate command got in, we can read the answer */
Kalle Valoff258392009-06-12 14:14:19 +0300129 wl12xx_spi_mem_read(wl, wl->cmd_box_addr, buf, len);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300130
Kalle Valoff258392009-06-12 14:14:19 +0300131 acx = buf;
132 if (acx->cmd.status != CMD_STATUS_SUCCESS)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300133 wl12xx_error("INTERROGATE command error: %d",
Kalle Valoff258392009-06-12 14:14:19 +0300134 acx->cmd.status);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300135
Kalle Valoff258392009-06-12 14:14:19 +0300136out:
137 return ret;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300138}
139
Kalle Valoff258392009-06-12 14:14:19 +0300140/**
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 wl12xx_cmd_configure(struct wl12xx *wl, u16 id, void *buf, size_t len)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300149{
Kalle Valoff258392009-06-12 14:14:19 +0300150 struct acx_header *acx = buf;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300151 int ret;
152
153 wl12xx_debug(DEBUG_CMD, "cmd configure");
154
Kalle Valoff258392009-06-12 14:14:19 +0300155 acx->id = id;
156
157 /* payload length, does not include any headers */
158 acx->len = len - sizeof(*acx);
159
160 ret = wl12xx_cmd_send(wl, CMD_CONFIGURE, acx, len);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300161 if (ret < 0) {
162 wl12xx_warning("CONFIGURE command NOK");
163 return ret;
164 }
165
166 return 0;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300167}
168
169int wl12xx_cmd_vbm(struct wl12xx *wl, u8 identity,
170 void *bitmap, u16 bitmap_len, u8 bitmap_control)
171{
Kalle Valoff258392009-06-12 14:14:19 +0300172 struct wl12xx_cmd_vbm_update *vbm;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300173 int ret;
174
175 wl12xx_debug(DEBUG_CMD, "cmd vbm");
176
Kalle Valoff258392009-06-12 14:14:19 +0300177 vbm = kzalloc(sizeof(*vbm), GFP_KERNEL);
178 if (!vbm) {
179 ret = -ENOMEM;
180 goto out;
181 }
182
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300183 /* Count and period will be filled by the target */
Kalle Valoff258392009-06-12 14:14:19 +0300184 vbm->tim.bitmap_ctrl = bitmap_control;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300185 if (bitmap_len > PARTIAL_VBM_MAX) {
186 wl12xx_warning("cmd vbm len is %d B, truncating to %d",
187 bitmap_len, PARTIAL_VBM_MAX);
188 bitmap_len = PARTIAL_VBM_MAX;
189 }
Kalle Valoff258392009-06-12 14:14:19 +0300190 memcpy(vbm->tim.pvb_field, bitmap, bitmap_len);
191 vbm->tim.identity = identity;
192 vbm->tim.length = bitmap_len + 3;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300193
Kalle Valoff258392009-06-12 14:14:19 +0300194 vbm->len = cpu_to_le16(bitmap_len + 5);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300195
Kalle Valoff258392009-06-12 14:14:19 +0300196 ret = wl12xx_cmd_send(wl, CMD_VBM, vbm, sizeof(*vbm));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300197 if (ret < 0) {
198 wl12xx_error("VBM command failed");
Kalle Valoff258392009-06-12 14:14:19 +0300199 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300200 }
201
Kalle Valoff258392009-06-12 14:14:19 +0300202out:
203 kfree(vbm);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300204 return 0;
205}
206
Kalle Valoff258392009-06-12 14:14:19 +0300207int wl12xx_cmd_data_path(struct wl12xx *wl, u8 channel, bool enable)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300208{
Kalle Valoff258392009-06-12 14:14:19 +0300209 struct cmd_enabledisable_path *cmd;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300210 int ret;
211 u16 cmd_rx, cmd_tx;
212
213 wl12xx_debug(DEBUG_CMD, "cmd data path");
214
Kalle Valoff258392009-06-12 14:14:19 +0300215 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
216 if (!cmd) {
217 ret = -ENOMEM;
218 goto out;
219 }
220
221 cmd->channel = channel;
222
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300223 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
Kalle Valoff258392009-06-12 14:14:19 +0300231 ret = wl12xx_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300232 if (ret < 0) {
233 wl12xx_error("rx %s cmd for channel %d failed",
234 enable ? "start" : "stop", channel);
Kalle Valoff258392009-06-12 14:14:19 +0300235 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300236 }
237
238 wl12xx_debug(DEBUG_BOOT, "rx %s cmd channel %d",
239 enable ? "start" : "stop", channel);
240
Kalle Valoff258392009-06-12 14:14:19 +0300241 ret = wl12xx_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300242 if (ret < 0) {
243 wl12xx_error("tx %s cmd for channel %d failed",
244 enable ? "start" : "stop", channel);
245 return ret;
246 }
247
248 wl12xx_debug(DEBUG_BOOT, "tx %s cmd channel %d",
249 enable ? "start" : "stop", channel);
250
Kalle Valoff258392009-06-12 14:14:19 +0300251out:
252 kfree(cmd);
253 return ret;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300254}
255
Juuso Oikarinen77cc9e42009-06-12 14:16:52 +0300256int wl1251_cmd_join(struct wl12xx *wl, u8 bss_type, u8 dtim_interval,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300257 u16 beacon_interval, u8 wait)
258{
259 unsigned long timeout;
Kalle Valoff258392009-06-12 14:14:19 +0300260 struct cmd_join *join;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300261 int ret, i;
262 u8 *bssid;
263
Kalle Valoff258392009-06-12 14:14:19 +0300264 join = kzalloc(sizeof(*join), GFP_KERNEL);
265 if (!join) {
266 ret = -ENOMEM;
267 goto out;
268 }
269
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300270 /* FIXME: this should be in main.c */
271 ret = wl12xx_acx_frame_rates(wl, DEFAULT_HW_GEN_TX_RATE,
272 DEFAULT_HW_GEN_MODULATION_TYPE,
273 wl->tx_mgmt_frm_rate,
274 wl->tx_mgmt_frm_mod);
275 if (ret < 0)
Kalle Valoff258392009-06-12 14:14:19 +0300276 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300277
278 wl12xx_debug(DEBUG_CMD, "cmd join");
279
280 /* Reverse order BSSID */
Kalle Valoff258392009-06-12 14:14:19 +0300281 bssid = (u8 *) &join->bssid_lsb;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300282 for (i = 0; i < ETH_ALEN; i++)
283 bssid[i] = wl->bssid[ETH_ALEN - i - 1];
284
Kalle Valoff258392009-06-12 14:14:19 +0300285 join->rx_config_options = wl->rx_config;
286 join->rx_filter_options = wl->rx_filter;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300287
Kalle Valoff258392009-06-12 14:14:19 +0300288 join->basic_rate_set = RATE_MASK_1MBPS | RATE_MASK_2MBPS |
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300289 RATE_MASK_5_5MBPS | RATE_MASK_11MBPS;
290
Kalle Valoff258392009-06-12 14:14:19 +0300291 join->beacon_interval = beacon_interval;
292 join->dtim_interval = dtim_interval;
293 join->bss_type = bss_type;
294 join->channel = wl->channel;
295 join->ctrl = JOIN_CMD_CTRL_TX_FLUSH;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300296
Kalle Valoff258392009-06-12 14:14:19 +0300297 ret = wl12xx_cmd_send(wl, CMD_START_JOIN, join, sizeof(*join));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300298 if (ret < 0) {
299 wl12xx_error("failed to initiate cmd join");
Kalle Valoff258392009-06-12 14:14:19 +0300300 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300301 }
302
303 timeout = msecs_to_jiffies(JOIN_TIMEOUT);
304
305 /*
306 * ugly hack: we should wait for JOIN_EVENT_COMPLETE_ID but to
307 * simplify locking we just sleep instead, for now
308 */
309 if (wait)
310 msleep(10);
311
Kalle Valoff258392009-06-12 14:14:19 +0300312out:
313 kfree(join);
314 return ret;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300315}
316
317int wl12xx_cmd_ps_mode(struct wl12xx *wl, u8 ps_mode)
318{
Kalle Valoff258392009-06-12 14:14:19 +0300319 struct wl12xx_cmd_ps_params *ps_params = NULL;
320 int ret = 0;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300321
322 /* FIXME: this should be in ps.c */
Luciano Coelho9f483dc2009-06-12 14:15:46 +0300323 ret = wl12xx_acx_wake_up_conditions(wl, WAKE_UP_EVENT_DTIM_BITMAP,
324 wl->listen_int);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300325 if (ret < 0) {
Kalle Valoff258392009-06-12 14:14:19 +0300326 wl12xx_error("couldn't set wake up conditions");
327 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300328 }
329
330 wl12xx_debug(DEBUG_CMD, "cmd set ps mode");
331
Kalle Valoff258392009-06-12 14:14:19 +0300332 ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
333 if (!ps_params) {
334 ret = -ENOMEM;
335 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300336 }
337
Kalle Valoff258392009-06-12 14:14:19 +0300338 ps_params->ps_mode = ps_mode;
339 ps_params->send_null_data = 1;
340 ps_params->retries = 5;
341 ps_params->hang_over_period = 128;
342 ps_params->null_data_rate = 1; /* 1 Mbps */
343
344 ret = wl12xx_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
345 sizeof(*ps_params));
346 if (ret < 0) {
347 wl12xx_error("cmd set_ps_mode failed");
348 goto out;
349 }
350
351out:
352 kfree(ps_params);
353 return ret;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300354}
355
Kalle Valoff258392009-06-12 14:14:19 +0300356int wl12xx_cmd_read_memory(struct wl12xx *wl, u32 addr, void *answer,
357 size_t len)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300358{
Kalle Valoff258392009-06-12 14:14:19 +0300359 struct cmd_read_write_memory *cmd;
360 int ret = 0;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300361
362 wl12xx_debug(DEBUG_CMD, "cmd read memory");
363
Kalle Valoff258392009-06-12 14:14:19 +0300364 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
365 if (!cmd) {
366 ret = -ENOMEM;
367 goto out;
368 }
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300369
Kalle Valoff258392009-06-12 14:14:19 +0300370 WARN_ON(len > MAX_READ_SIZE);
371 len = min_t(size_t, len, MAX_READ_SIZE);
372
373 cmd->addr = addr;
374 cmd->size = len;
375
376 ret = wl12xx_cmd_send(wl, CMD_READ_MEMORY, cmd, sizeof(*cmd));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300377 if (ret < 0) {
378 wl12xx_error("read memory command failed: %d", ret);
Kalle Valoff258392009-06-12 14:14:19 +0300379 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300380 }
381
382 /* the read command got in, we can now read the answer */
Kalle Valoff258392009-06-12 14:14:19 +0300383 wl12xx_spi_mem_read(wl, wl->cmd_box_addr, cmd, sizeof(*cmd));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300384
Kalle Valoff258392009-06-12 14:14:19 +0300385 if (cmd->header.status != CMD_STATUS_SUCCESS)
386 wl12xx_error("error in read command result: %d",
387 cmd->header.status);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300388
Kalle Valoff258392009-06-12 14:14:19 +0300389 memcpy(answer, cmd->value, len);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300390
Kalle Valoff258392009-06-12 14:14:19 +0300391out:
392 kfree(cmd);
393 return ret;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300394}
395
396int wl12xx_cmd_template_set(struct wl12xx *wl, u16 cmd_id,
397 void *buf, size_t buf_len)
398{
Kalle Valoff258392009-06-12 14:14:19 +0300399 struct wl12xx_cmd_packet_template *cmd;
400 size_t cmd_len;
401 int ret = 0;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300402
403 wl12xx_debug(DEBUG_CMD, "cmd template %d", cmd_id);
404
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300405 WARN_ON(buf_len > WL12XX_MAX_TEMPLATE_SIZE);
406 buf_len = min_t(size_t, buf_len, WL12XX_MAX_TEMPLATE_SIZE);
Kalle Valoff258392009-06-12 14:14:19 +0300407 cmd_len = ALIGN(sizeof(*cmd) + buf_len, 4);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300408
Kalle Valoff258392009-06-12 14:14:19 +0300409 cmd = kzalloc(cmd_len, GFP_KERNEL);
410 if (!cmd) {
411 ret = -ENOMEM;
412 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300413 }
414
Kalle Valoff258392009-06-12 14:14:19 +0300415 cmd->size = cpu_to_le16(buf_len);
416
417 if (buf)
418 memcpy(cmd->data, buf, buf_len);
419
420 ret = wl12xx_cmd_send(wl, cmd_id, cmd, cmd_len);
421 if (ret < 0) {
422 wl12xx_warning("cmd set_template failed: %d", ret);
423 goto out;
424 }
425
426out:
427 kfree(cmd);
428 return ret;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300429}