blob: 53f5da65bcb4829c8ec9607fb673bb0aedd761c7 [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 Valo2f01a1f2009-04-29 23:33:31 +03007#include "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 Valo80301cd2009-06-12 14:17:39 +0300254int wl1251_cmd_join(struct wl1251 *wl, u8 bss_type, u8 dtim_interval,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300255 u16 beacon_interval, u8 wait)
256{
257 unsigned long timeout;
Kalle Valoff258392009-06-12 14:14:19 +0300258 struct cmd_join *join;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300259 int ret, i;
260 u8 *bssid;
261
Kalle Valoff258392009-06-12 14:14:19 +0300262 join = kzalloc(sizeof(*join), GFP_KERNEL);
263 if (!join) {
264 ret = -ENOMEM;
265 goto out;
266 }
267
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300268 /* FIXME: this should be in main.c */
Kalle Valo80301cd2009-06-12 14:17:39 +0300269 ret = wl1251_acx_frame_rates(wl, DEFAULT_HW_GEN_TX_RATE,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300270 DEFAULT_HW_GEN_MODULATION_TYPE,
271 wl->tx_mgmt_frm_rate,
272 wl->tx_mgmt_frm_mod);
273 if (ret < 0)
Kalle Valoff258392009-06-12 14:14:19 +0300274 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300275
Kalle Valo80301cd2009-06-12 14:17:39 +0300276 wl1251_debug(DEBUG_CMD, "cmd join");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300277
278 /* Reverse order BSSID */
Kalle Valoff258392009-06-12 14:14:19 +0300279 bssid = (u8 *) &join->bssid_lsb;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300280 for (i = 0; i < ETH_ALEN; i++)
281 bssid[i] = wl->bssid[ETH_ALEN - i - 1];
282
Kalle Valoff258392009-06-12 14:14:19 +0300283 join->rx_config_options = wl->rx_config;
284 join->rx_filter_options = wl->rx_filter;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300285
Kalle Valoff258392009-06-12 14:14:19 +0300286 join->basic_rate_set = RATE_MASK_1MBPS | RATE_MASK_2MBPS |
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300287 RATE_MASK_5_5MBPS | RATE_MASK_11MBPS;
288
Kalle Valoff258392009-06-12 14:14:19 +0300289 join->beacon_interval = beacon_interval;
290 join->dtim_interval = dtim_interval;
291 join->bss_type = bss_type;
292 join->channel = wl->channel;
293 join->ctrl = JOIN_CMD_CTRL_TX_FLUSH;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300294
Kalle Valo80301cd2009-06-12 14:17:39 +0300295 ret = wl1251_cmd_send(wl, CMD_START_JOIN, join, sizeof(*join));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300296 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300297 wl1251_error("failed to initiate cmd join");
Kalle Valoff258392009-06-12 14:14:19 +0300298 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300299 }
300
301 timeout = msecs_to_jiffies(JOIN_TIMEOUT);
302
303 /*
304 * ugly hack: we should wait for JOIN_EVENT_COMPLETE_ID but to
305 * simplify locking we just sleep instead, for now
306 */
307 if (wait)
308 msleep(10);
309
Kalle Valoff258392009-06-12 14:14:19 +0300310out:
311 kfree(join);
312 return ret;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300313}
314
Kalle Valo80301cd2009-06-12 14:17:39 +0300315int wl1251_cmd_ps_mode(struct wl1251 *wl, u8 ps_mode)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300316{
Kalle Valo80301cd2009-06-12 14:17:39 +0300317 struct wl1251_cmd_ps_params *ps_params = NULL;
Kalle Valoff258392009-06-12 14:14:19 +0300318 int ret = 0;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300319
320 /* FIXME: this should be in ps.c */
Kalle Valo80301cd2009-06-12 14:17:39 +0300321 ret = wl1251_acx_wake_up_conditions(wl, WAKE_UP_EVENT_DTIM_BITMAP,
Luciano Coelho9f483dc2009-06-12 14:15:46 +0300322 wl->listen_int);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300323 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300324 wl1251_error("couldn't set wake up conditions");
Kalle Valoff258392009-06-12 14:14:19 +0300325 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300326 }
327
Kalle Valo80301cd2009-06-12 14:17:39 +0300328 wl1251_debug(DEBUG_CMD, "cmd set ps mode");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300329
Kalle Valoff258392009-06-12 14:14:19 +0300330 ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
331 if (!ps_params) {
332 ret = -ENOMEM;
333 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300334 }
335
Kalle Valoff258392009-06-12 14:14:19 +0300336 ps_params->ps_mode = ps_mode;
337 ps_params->send_null_data = 1;
338 ps_params->retries = 5;
339 ps_params->hang_over_period = 128;
340 ps_params->null_data_rate = 1; /* 1 Mbps */
341
Kalle Valo80301cd2009-06-12 14:17:39 +0300342 ret = wl1251_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
Kalle Valoff258392009-06-12 14:14:19 +0300343 sizeof(*ps_params));
344 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300345 wl1251_error("cmd set_ps_mode failed");
Kalle Valoff258392009-06-12 14:14:19 +0300346 goto out;
347 }
348
349out:
350 kfree(ps_params);
351 return ret;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300352}
353
Kalle Valo80301cd2009-06-12 14:17:39 +0300354int wl1251_cmd_read_memory(struct wl1251 *wl, u32 addr, void *answer,
Kalle Valoff258392009-06-12 14:14:19 +0300355 size_t len)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300356{
Kalle Valoff258392009-06-12 14:14:19 +0300357 struct cmd_read_write_memory *cmd;
358 int ret = 0;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300359
Kalle Valo80301cd2009-06-12 14:17:39 +0300360 wl1251_debug(DEBUG_CMD, "cmd read memory");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300361
Kalle Valoff258392009-06-12 14:14:19 +0300362 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
363 if (!cmd) {
364 ret = -ENOMEM;
365 goto out;
366 }
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300367
Kalle Valoff258392009-06-12 14:14:19 +0300368 WARN_ON(len > MAX_READ_SIZE);
369 len = min_t(size_t, len, MAX_READ_SIZE);
370
371 cmd->addr = addr;
372 cmd->size = len;
373
Kalle Valo80301cd2009-06-12 14:17:39 +0300374 ret = wl1251_cmd_send(wl, CMD_READ_MEMORY, cmd, sizeof(*cmd));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300375 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300376 wl1251_error("read memory command failed: %d", ret);
Kalle Valoff258392009-06-12 14:14:19 +0300377 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300378 }
379
380 /* the read command got in, we can now read the answer */
Bob Copeland0764de62009-08-07 13:32:56 +0300381 wl1251_mem_read(wl, wl->cmd_box_addr, cmd, sizeof(*cmd));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300382
Kalle Valoff258392009-06-12 14:14:19 +0300383 if (cmd->header.status != CMD_STATUS_SUCCESS)
Kalle Valo80301cd2009-06-12 14:17:39 +0300384 wl1251_error("error in read command result: %d",
Kalle Valoff258392009-06-12 14:14:19 +0300385 cmd->header.status);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300386
Kalle Valoff258392009-06-12 14:14:19 +0300387 memcpy(answer, cmd->value, len);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300388
Kalle Valoff258392009-06-12 14:14:19 +0300389out:
390 kfree(cmd);
391 return ret;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300392}
393
Kalle Valo80301cd2009-06-12 14:17:39 +0300394int wl1251_cmd_template_set(struct wl1251 *wl, u16 cmd_id,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300395 void *buf, size_t buf_len)
396{
Kalle Valo80301cd2009-06-12 14:17:39 +0300397 struct wl1251_cmd_packet_template *cmd;
Kalle Valoff258392009-06-12 14:14:19 +0300398 size_t cmd_len;
399 int ret = 0;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300400
Kalle Valo80301cd2009-06-12 14:17:39 +0300401 wl1251_debug(DEBUG_CMD, "cmd template %d", cmd_id);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300402
Kalle Valo80301cd2009-06-12 14:17:39 +0300403 WARN_ON(buf_len > WL1251_MAX_TEMPLATE_SIZE);
404 buf_len = min_t(size_t, buf_len, WL1251_MAX_TEMPLATE_SIZE);
Kalle Valoff258392009-06-12 14:14:19 +0300405 cmd_len = ALIGN(sizeof(*cmd) + buf_len, 4);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300406
Kalle Valoff258392009-06-12 14:14:19 +0300407 cmd = kzalloc(cmd_len, GFP_KERNEL);
408 if (!cmd) {
409 ret = -ENOMEM;
410 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300411 }
412
Kalle Valoff258392009-06-12 14:14:19 +0300413 cmd->size = cpu_to_le16(buf_len);
414
415 if (buf)
416 memcpy(cmd->data, buf, buf_len);
417
Kalle Valo80301cd2009-06-12 14:17:39 +0300418 ret = wl1251_cmd_send(wl, cmd_id, cmd, cmd_len);
Kalle Valoff258392009-06-12 14:14:19 +0300419 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300420 wl1251_warning("cmd set_template failed: %d", ret);
Kalle Valoff258392009-06-12 14:14:19 +0300421 goto out;
422 }
423
424out:
425 kfree(cmd);
426 return ret;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300427}