blob: 6cd024b50a7ce6d6e63edd6f68fa00d06a81542a [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>
Kalle Valo2f01a1f2009-04-29 23:33:31 +03005
Kalle Valo13674112009-06-12 14:17:25 +03006#include "wl1251.h"
Kalle Valo29d904c2009-08-07 13:35:11 +03007#include "wl1251_reg.h"
Bob Copeland0764de62009-08-07 13:32:56 +03008#include "wl1251_io.h"
Kalle Valoef2f8d42009-06-12 14:17:19 +03009#include "wl1251_ps.h"
10#include "wl1251_acx.h"
Kalle Valo2f01a1f2009-04-29 23:33:31 +030011
Kalle Valoff258392009-06-12 14:14:19 +030012/**
13 * send command to firmware
14 *
15 * @wl: wl struct
16 * @id: command id
17 * @buf: buffer containing the command, must work with dma
18 * @len: length of the buffer
19 */
Kalle Valo80301cd2009-06-12 14:17:39 +030020int wl1251_cmd_send(struct wl1251 *wl, u16 id, void *buf, size_t len)
Kalle Valo2f01a1f2009-04-29 23:33:31 +030021{
Kalle Valo80301cd2009-06-12 14:17:39 +030022 struct wl1251_cmd_header *cmd;
Kalle Valo2f01a1f2009-04-29 23:33:31 +030023 unsigned long timeout;
Kalle Valo2f01a1f2009-04-29 23:33:31 +030024 u32 intr;
25 int ret = 0;
26
Kalle Valoff258392009-06-12 14:14:19 +030027 cmd = buf;
28 cmd->id = id;
29 cmd->status = 0;
30
31 WARN_ON(len % 4 != 0);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030032
Bob Copeland0764de62009-08-07 13:32:56 +030033 wl1251_mem_write(wl, wl->cmd_box_addr, buf, len);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030034
Kalle Valo80301cd2009-06-12 14:17:39 +030035 wl1251_reg_write32(wl, ACX_REG_INTERRUPT_TRIG, INTR_TRIG_CMD);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030036
Kalle Valo80301cd2009-06-12 14:17:39 +030037 timeout = jiffies + msecs_to_jiffies(WL1251_COMMAND_TIMEOUT);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030038
Kalle Valo80301cd2009-06-12 14:17:39 +030039 intr = wl1251_reg_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
Kalle Valo0e71bb02009-08-07 13:33:57 +030040 while (!(intr & WL1251_ACX_INTR_CMD_COMPLETE)) {
Kalle Valo2f01a1f2009-04-29 23:33:31 +030041 if (time_after(jiffies, timeout)) {
Kalle Valo80301cd2009-06-12 14:17:39 +030042 wl1251_error("command complete timeout");
Kalle Valo2f01a1f2009-04-29 23:33:31 +030043 ret = -ETIMEDOUT;
44 goto out;
45 }
46
47 msleep(1);
48
Kalle Valo80301cd2009-06-12 14:17:39 +030049 intr = wl1251_reg_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030050 }
51
Kalle Valo80301cd2009-06-12 14:17:39 +030052 wl1251_reg_write32(wl, ACX_REG_INTERRUPT_ACK,
Kalle Valo0e71bb02009-08-07 13:33:57 +030053 WL1251_ACX_INTR_CMD_COMPLETE);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030054
55out:
Kalle Valo2f01a1f2009-04-29 23:33:31 +030056 return ret;
57}
58
Kalle Valoff258392009-06-12 14:14:19 +030059/**
60 * send test command to firmware
61 *
62 * @wl: wl struct
Ari Kauppi6021b282009-06-12 14:16:13 +030063 * @buf: buffer containing the command, with all headers, must work with dma
Kalle Valoff258392009-06-12 14:14:19 +030064 * @len: length of the buffer
65 * @answer: is answer needed
Kalle Valoff258392009-06-12 14:14:19 +030066 */
Kalle Valo80301cd2009-06-12 14:17:39 +030067int wl1251_cmd_test(struct wl1251 *wl, void *buf, size_t buf_len, u8 answer)
Kalle Valo2f01a1f2009-04-29 23:33:31 +030068{
69 int ret;
70
Kalle Valo80301cd2009-06-12 14:17:39 +030071 wl1251_debug(DEBUG_CMD, "cmd test");
Kalle Valo2f01a1f2009-04-29 23:33:31 +030072
Kalle Valo80301cd2009-06-12 14:17:39 +030073 ret = wl1251_cmd_send(wl, CMD_TEST, buf, buf_len);
Kalle Valoff258392009-06-12 14:14:19 +030074
Kalle Valo2f01a1f2009-04-29 23:33:31 +030075 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +030076 wl1251_warning("TEST command failed");
Ari Kauppi6021b282009-06-12 14:16:13 +030077 return ret;
Kalle Valo2f01a1f2009-04-29 23:33:31 +030078 }
79
80 if (answer) {
Kalle Valo80301cd2009-06-12 14:17:39 +030081 struct wl1251_command *cmd_answer;
Ari Kauppi6021b282009-06-12 14:16:13 +030082
Kalle Valo2f01a1f2009-04-29 23:33:31 +030083 /*
84 * The test command got in, we can read the answer.
Kalle Valo80301cd2009-06-12 14:17:39 +030085 * The answer would be a wl1251_command, where the
Kalle Valo2f01a1f2009-04-29 23:33:31 +030086 * parameter array contains the actual answer.
87 */
Bob Copeland0764de62009-08-07 13:32:56 +030088 wl1251_mem_read(wl, wl->cmd_box_addr, buf, buf_len);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030089
Ari Kauppi6021b282009-06-12 14:16:13 +030090 cmd_answer = buf;
91
92 if (cmd_answer->header.status != CMD_STATUS_SUCCESS)
Kalle Valo80301cd2009-06-12 14:17:39 +030093 wl1251_error("TEST command answer error: %d",
Ari Kauppi6021b282009-06-12 14:16:13 +030094 cmd_answer->header.status);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030095 }
96
Ari Kauppi6021b282009-06-12 14:16:13 +030097 return 0;
Kalle Valo2f01a1f2009-04-29 23:33:31 +030098}
99
Kalle Valoff258392009-06-12 14:14:19 +0300100/**
101 * read acx from firmware
102 *
103 * @wl: wl struct
104 * @id: acx id
105 * @buf: buffer for the response, including all headers, must work with dma
106 * @len: lenght of buf
107 */
Kalle Valo80301cd2009-06-12 14:17:39 +0300108int wl1251_cmd_interrogate(struct wl1251 *wl, u16 id, void *buf, size_t len)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300109{
Kalle Valoff258392009-06-12 14:14:19 +0300110 struct acx_header *acx = buf;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300111 int ret;
112
Kalle Valo80301cd2009-06-12 14:17:39 +0300113 wl1251_debug(DEBUG_CMD, "cmd interrogate");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300114
Kalle Valoff258392009-06-12 14:14:19 +0300115 acx->id = id;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300116
Kalle Valoff258392009-06-12 14:14:19 +0300117 /* payload length, does not include any headers */
118 acx->len = len - sizeof(*acx);
119
Kalle Valo80301cd2009-06-12 14:17:39 +0300120 ret = wl1251_cmd_send(wl, CMD_INTERROGATE, acx, sizeof(*acx));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300121 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300122 wl1251_error("INTERROGATE command failed");
Kalle Valoff258392009-06-12 14:14:19 +0300123 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300124 }
125
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300126 /* the interrogate command got in, we can read the answer */
Bob Copeland0764de62009-08-07 13:32:56 +0300127 wl1251_mem_read(wl, wl->cmd_box_addr, buf, len);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300128
Kalle Valoff258392009-06-12 14:14:19 +0300129 acx = buf;
130 if (acx->cmd.status != CMD_STATUS_SUCCESS)
Kalle Valo80301cd2009-06-12 14:17:39 +0300131 wl1251_error("INTERROGATE command error: %d",
Kalle Valoff258392009-06-12 14:14:19 +0300132 acx->cmd.status);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300133
Kalle Valoff258392009-06-12 14:14:19 +0300134out:
135 return ret;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300136}
137
Kalle Valoff258392009-06-12 14:14:19 +0300138/**
139 * write acx value to firmware
140 *
141 * @wl: wl struct
142 * @id: acx id
143 * @buf: buffer containing acx, including all headers, must work with dma
144 * @len: length of buf
145 */
Kalle Valo80301cd2009-06-12 14:17:39 +0300146int wl1251_cmd_configure(struct wl1251 *wl, u16 id, void *buf, size_t len)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300147{
Kalle Valoff258392009-06-12 14:14:19 +0300148 struct acx_header *acx = buf;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300149 int ret;
150
Kalle Valo80301cd2009-06-12 14:17:39 +0300151 wl1251_debug(DEBUG_CMD, "cmd configure");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300152
Kalle Valoff258392009-06-12 14:14:19 +0300153 acx->id = id;
154
155 /* payload length, does not include any headers */
156 acx->len = len - sizeof(*acx);
157
Kalle Valo80301cd2009-06-12 14:17:39 +0300158 ret = wl1251_cmd_send(wl, CMD_CONFIGURE, acx, len);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300159 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300160 wl1251_warning("CONFIGURE command NOK");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300161 return ret;
162 }
163
164 return 0;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300165}
166
Kalle Valo80301cd2009-06-12 14:17:39 +0300167int wl1251_cmd_vbm(struct wl1251 *wl, u8 identity,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300168 void *bitmap, u16 bitmap_len, u8 bitmap_control)
169{
Kalle Valo80301cd2009-06-12 14:17:39 +0300170 struct wl1251_cmd_vbm_update *vbm;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300171 int ret;
172
Kalle Valo80301cd2009-06-12 14:17:39 +0300173 wl1251_debug(DEBUG_CMD, "cmd vbm");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300174
Kalle Valoff258392009-06-12 14:14:19 +0300175 vbm = kzalloc(sizeof(*vbm), GFP_KERNEL);
176 if (!vbm) {
177 ret = -ENOMEM;
178 goto out;
179 }
180
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300181 /* Count and period will be filled by the target */
Kalle Valoff258392009-06-12 14:14:19 +0300182 vbm->tim.bitmap_ctrl = bitmap_control;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300183 if (bitmap_len > PARTIAL_VBM_MAX) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300184 wl1251_warning("cmd vbm len is %d B, truncating to %d",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300185 bitmap_len, PARTIAL_VBM_MAX);
186 bitmap_len = PARTIAL_VBM_MAX;
187 }
Kalle Valoff258392009-06-12 14:14:19 +0300188 memcpy(vbm->tim.pvb_field, bitmap, bitmap_len);
189 vbm->tim.identity = identity;
190 vbm->tim.length = bitmap_len + 3;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300191
Kalle Valoff258392009-06-12 14:14:19 +0300192 vbm->len = cpu_to_le16(bitmap_len + 5);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300193
Kalle Valo80301cd2009-06-12 14:17:39 +0300194 ret = wl1251_cmd_send(wl, CMD_VBM, vbm, sizeof(*vbm));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300195 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300196 wl1251_error("VBM command failed");
Kalle Valoff258392009-06-12 14:14:19 +0300197 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300198 }
199
Kalle Valoff258392009-06-12 14:14:19 +0300200out:
201 kfree(vbm);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300202 return 0;
203}
204
Kalle Valo80301cd2009-06-12 14:17:39 +0300205int wl1251_cmd_data_path(struct wl1251 *wl, u8 channel, bool enable)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300206{
Kalle Valoff258392009-06-12 14:14:19 +0300207 struct cmd_enabledisable_path *cmd;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300208 int ret;
209 u16 cmd_rx, cmd_tx;
210
Kalle Valo80301cd2009-06-12 14:17:39 +0300211 wl1251_debug(DEBUG_CMD, "cmd data path");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300212
Kalle Valoff258392009-06-12 14:14:19 +0300213 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
214 if (!cmd) {
215 ret = -ENOMEM;
216 goto out;
217 }
218
219 cmd->channel = channel;
220
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300221 if (enable) {
222 cmd_rx = CMD_ENABLE_RX;
223 cmd_tx = CMD_ENABLE_TX;
224 } else {
225 cmd_rx = CMD_DISABLE_RX;
226 cmd_tx = CMD_DISABLE_TX;
227 }
228
Kalle Valo80301cd2009-06-12 14:17:39 +0300229 ret = wl1251_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300230 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300231 wl1251_error("rx %s cmd for channel %d failed",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300232 enable ? "start" : "stop", channel);
Kalle Valoff258392009-06-12 14:14:19 +0300233 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300234 }
235
Kalle Valo80301cd2009-06-12 14:17:39 +0300236 wl1251_debug(DEBUG_BOOT, "rx %s cmd channel %d",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300237 enable ? "start" : "stop", channel);
238
Kalle Valo80301cd2009-06-12 14:17:39 +0300239 ret = wl1251_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300240 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300241 wl1251_error("tx %s cmd for channel %d failed",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300242 enable ? "start" : "stop", channel);
243 return ret;
244 }
245
Kalle Valo80301cd2009-06-12 14:17:39 +0300246 wl1251_debug(DEBUG_BOOT, "tx %s cmd channel %d",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300247 enable ? "start" : "stop", channel);
248
Kalle Valoff258392009-06-12 14:14:19 +0300249out:
250 kfree(cmd);
251 return ret;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300252}
253
Kalle Valoc88f8752009-08-07 13:34:34 +0300254int wl1251_cmd_join(struct wl1251 *wl, u8 bss_type, u8 channel,
255 u16 beacon_interval, u8 dtim_interval)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300256{
Kalle Valoff258392009-06-12 14:14:19 +0300257 struct cmd_join *join;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300258 int ret, i;
259 u8 *bssid;
260
Kalle Valoff258392009-06-12 14:14:19 +0300261 join = kzalloc(sizeof(*join), GFP_KERNEL);
262 if (!join) {
263 ret = -ENOMEM;
264 goto out;
265 }
266
Kalle Valoc88f8752009-08-07 13:34:34 +0300267 wl1251_debug(DEBUG_CMD, "cmd join%s ch %d %d/%d",
Kalle Valoe2fd4612009-08-07 13:34:12 +0300268 bss_type == BSS_TYPE_IBSS ? " ibss" : "",
Kalle Valoc88f8752009-08-07 13:34:34 +0300269 channel, beacon_interval, dtim_interval);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300270
271 /* Reverse order BSSID */
Kalle Valoff258392009-06-12 14:14:19 +0300272 bssid = (u8 *) &join->bssid_lsb;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300273 for (i = 0; i < ETH_ALEN; i++)
274 bssid[i] = wl->bssid[ETH_ALEN - i - 1];
275
Kalle Valoff258392009-06-12 14:14:19 +0300276 join->rx_config_options = wl->rx_config;
277 join->rx_filter_options = wl->rx_filter;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300278
Kalle Valoff258392009-06-12 14:14:19 +0300279 join->basic_rate_set = RATE_MASK_1MBPS | RATE_MASK_2MBPS |
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300280 RATE_MASK_5_5MBPS | RATE_MASK_11MBPS;
281
Kalle Valoff258392009-06-12 14:14:19 +0300282 join->beacon_interval = beacon_interval;
283 join->dtim_interval = dtim_interval;
284 join->bss_type = bss_type;
Kalle Valoc88f8752009-08-07 13:34:34 +0300285 join->channel = channel;
Kalle Valoff258392009-06-12 14:14:19 +0300286 join->ctrl = JOIN_CMD_CTRL_TX_FLUSH;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300287
Kalle Valo80301cd2009-06-12 14:17:39 +0300288 ret = wl1251_cmd_send(wl, CMD_START_JOIN, join, sizeof(*join));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300289 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300290 wl1251_error("failed to initiate cmd join");
Kalle Valoff258392009-06-12 14:14:19 +0300291 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300292 }
293
Kalle Valoff258392009-06-12 14:14:19 +0300294out:
295 kfree(join);
296 return ret;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300297}
298
Kalle Valo80301cd2009-06-12 14:17:39 +0300299int wl1251_cmd_ps_mode(struct wl1251 *wl, u8 ps_mode)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300300{
Kalle Valo80301cd2009-06-12 14:17:39 +0300301 struct wl1251_cmd_ps_params *ps_params = NULL;
Kalle Valoff258392009-06-12 14:14:19 +0300302 int ret = 0;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300303
Kalle Valo80301cd2009-06-12 14:17:39 +0300304 wl1251_debug(DEBUG_CMD, "cmd set ps mode");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300305
Kalle Valoff258392009-06-12 14:14:19 +0300306 ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
307 if (!ps_params) {
308 ret = -ENOMEM;
309 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300310 }
311
Kalle Valoff258392009-06-12 14:14:19 +0300312 ps_params->ps_mode = ps_mode;
313 ps_params->send_null_data = 1;
314 ps_params->retries = 5;
315 ps_params->hang_over_period = 128;
316 ps_params->null_data_rate = 1; /* 1 Mbps */
317
Kalle Valo80301cd2009-06-12 14:17:39 +0300318 ret = wl1251_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
Kalle Valoff258392009-06-12 14:14:19 +0300319 sizeof(*ps_params));
320 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300321 wl1251_error("cmd set_ps_mode failed");
Kalle Valoff258392009-06-12 14:14:19 +0300322 goto out;
323 }
324
325out:
326 kfree(ps_params);
327 return ret;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300328}
329
Kalle Valo80301cd2009-06-12 14:17:39 +0300330int wl1251_cmd_read_memory(struct wl1251 *wl, u32 addr, void *answer,
Kalle Valoff258392009-06-12 14:14:19 +0300331 size_t len)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300332{
Kalle Valoff258392009-06-12 14:14:19 +0300333 struct cmd_read_write_memory *cmd;
334 int ret = 0;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300335
Kalle Valo80301cd2009-06-12 14:17:39 +0300336 wl1251_debug(DEBUG_CMD, "cmd read memory");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300337
Kalle Valoff258392009-06-12 14:14:19 +0300338 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
339 if (!cmd) {
340 ret = -ENOMEM;
341 goto out;
342 }
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300343
Kalle Valoff258392009-06-12 14:14:19 +0300344 WARN_ON(len > MAX_READ_SIZE);
345 len = min_t(size_t, len, MAX_READ_SIZE);
346
347 cmd->addr = addr;
348 cmd->size = len;
349
Kalle Valo80301cd2009-06-12 14:17:39 +0300350 ret = wl1251_cmd_send(wl, CMD_READ_MEMORY, cmd, sizeof(*cmd));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300351 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300352 wl1251_error("read memory command failed: %d", ret);
Kalle Valoff258392009-06-12 14:14:19 +0300353 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300354 }
355
356 /* the read command got in, we can now read the answer */
Bob Copeland0764de62009-08-07 13:32:56 +0300357 wl1251_mem_read(wl, wl->cmd_box_addr, cmd, sizeof(*cmd));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300358
Kalle Valoff258392009-06-12 14:14:19 +0300359 if (cmd->header.status != CMD_STATUS_SUCCESS)
Kalle Valo80301cd2009-06-12 14:17:39 +0300360 wl1251_error("error in read command result: %d",
Kalle Valoff258392009-06-12 14:14:19 +0300361 cmd->header.status);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300362
Kalle Valoff258392009-06-12 14:14:19 +0300363 memcpy(answer, cmd->value, len);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300364
Kalle Valoff258392009-06-12 14:14:19 +0300365out:
366 kfree(cmd);
367 return ret;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300368}
369
Kalle Valo80301cd2009-06-12 14:17:39 +0300370int wl1251_cmd_template_set(struct wl1251 *wl, u16 cmd_id,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300371 void *buf, size_t buf_len)
372{
Kalle Valo80301cd2009-06-12 14:17:39 +0300373 struct wl1251_cmd_packet_template *cmd;
Kalle Valoff258392009-06-12 14:14:19 +0300374 size_t cmd_len;
375 int ret = 0;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300376
Kalle Valo80301cd2009-06-12 14:17:39 +0300377 wl1251_debug(DEBUG_CMD, "cmd template %d", cmd_id);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300378
Kalle Valo80301cd2009-06-12 14:17:39 +0300379 WARN_ON(buf_len > WL1251_MAX_TEMPLATE_SIZE);
380 buf_len = min_t(size_t, buf_len, WL1251_MAX_TEMPLATE_SIZE);
Kalle Valoff258392009-06-12 14:14:19 +0300381 cmd_len = ALIGN(sizeof(*cmd) + buf_len, 4);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300382
Kalle Valoff258392009-06-12 14:14:19 +0300383 cmd = kzalloc(cmd_len, GFP_KERNEL);
384 if (!cmd) {
385 ret = -ENOMEM;
386 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300387 }
388
Kalle Valoff258392009-06-12 14:14:19 +0300389 cmd->size = cpu_to_le16(buf_len);
390
391 if (buf)
392 memcpy(cmd->data, buf, buf_len);
393
Kalle Valo80301cd2009-06-12 14:17:39 +0300394 ret = wl1251_cmd_send(wl, cmd_id, cmd, cmd_len);
Kalle Valoff258392009-06-12 14:14:19 +0300395 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300396 wl1251_warning("cmd set_template failed: %d", ret);
Kalle Valoff258392009-06-12 14:14:19 +0300397 goto out;
398 }
399
400out:
401 kfree(cmd);
402 return ret;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300403}