blob: 9cd46ce88e51c6b5cdcd6cb2bd9b8bb353d45237 [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 Valo46e947b2009-08-07 13:34:05 +0300254int wl1251_cmd_join(struct wl1251 *wl, u8 bss_type, u16 beacon_interval,
255 u8 dtim_interval, bool wait)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300256{
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 Valoe2fd4612009-08-07 13:34:12 +0300276 wl1251_debug(DEBUG_CMD, "cmd join%s %d %d%s",
277 bss_type == BSS_TYPE_IBSS ? " ibss" : "",
278 beacon_interval, dtim_interval,
279 wait ? " wait" : "");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300280
281 /* Reverse order BSSID */
Kalle Valoff258392009-06-12 14:14:19 +0300282 bssid = (u8 *) &join->bssid_lsb;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300283 for (i = 0; i < ETH_ALEN; i++)
284 bssid[i] = wl->bssid[ETH_ALEN - i - 1];
285
Kalle Valoff258392009-06-12 14:14:19 +0300286 join->rx_config_options = wl->rx_config;
287 join->rx_filter_options = wl->rx_filter;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300288
Kalle Valoff258392009-06-12 14:14:19 +0300289 join->basic_rate_set = RATE_MASK_1MBPS | RATE_MASK_2MBPS |
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300290 RATE_MASK_5_5MBPS | RATE_MASK_11MBPS;
291
Kalle Valoff258392009-06-12 14:14:19 +0300292 join->beacon_interval = beacon_interval;
293 join->dtim_interval = dtim_interval;
294 join->bss_type = bss_type;
295 join->channel = wl->channel;
296 join->ctrl = JOIN_CMD_CTRL_TX_FLUSH;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300297
Kalle Valo80301cd2009-06-12 14:17:39 +0300298 ret = wl1251_cmd_send(wl, CMD_START_JOIN, join, sizeof(*join));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300299 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300300 wl1251_error("failed to initiate cmd join");
Kalle Valoff258392009-06-12 14:14:19 +0300301 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300302 }
303
304 timeout = msecs_to_jiffies(JOIN_TIMEOUT);
305
306 /*
307 * ugly hack: we should wait for JOIN_EVENT_COMPLETE_ID but to
308 * simplify locking we just sleep instead, for now
309 */
310 if (wait)
311 msleep(10);
312
Kalle Valoff258392009-06-12 14:14:19 +0300313out:
314 kfree(join);
315 return ret;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300316}
317
Kalle Valo80301cd2009-06-12 14:17:39 +0300318int wl1251_cmd_ps_mode(struct wl1251 *wl, u8 ps_mode)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300319{
Kalle Valo80301cd2009-06-12 14:17:39 +0300320 struct wl1251_cmd_ps_params *ps_params = NULL;
Kalle Valoff258392009-06-12 14:14:19 +0300321 int ret = 0;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300322
323 /* FIXME: this should be in ps.c */
Kalle Valo80301cd2009-06-12 14:17:39 +0300324 ret = wl1251_acx_wake_up_conditions(wl, WAKE_UP_EVENT_DTIM_BITMAP,
Luciano Coelho9f483dc2009-06-12 14:15:46 +0300325 wl->listen_int);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300326 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300327 wl1251_error("couldn't set wake up conditions");
Kalle Valoff258392009-06-12 14:14:19 +0300328 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300329 }
330
Kalle Valo80301cd2009-06-12 14:17:39 +0300331 wl1251_debug(DEBUG_CMD, "cmd set ps mode");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300332
Kalle Valoff258392009-06-12 14:14:19 +0300333 ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
334 if (!ps_params) {
335 ret = -ENOMEM;
336 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300337 }
338
Kalle Valoff258392009-06-12 14:14:19 +0300339 ps_params->ps_mode = ps_mode;
340 ps_params->send_null_data = 1;
341 ps_params->retries = 5;
342 ps_params->hang_over_period = 128;
343 ps_params->null_data_rate = 1; /* 1 Mbps */
344
Kalle Valo80301cd2009-06-12 14:17:39 +0300345 ret = wl1251_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
Kalle Valoff258392009-06-12 14:14:19 +0300346 sizeof(*ps_params));
347 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300348 wl1251_error("cmd set_ps_mode failed");
Kalle Valoff258392009-06-12 14:14:19 +0300349 goto out;
350 }
351
352out:
353 kfree(ps_params);
354 return ret;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300355}
356
Kalle Valo80301cd2009-06-12 14:17:39 +0300357int wl1251_cmd_read_memory(struct wl1251 *wl, u32 addr, void *answer,
Kalle Valoff258392009-06-12 14:14:19 +0300358 size_t len)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300359{
Kalle Valoff258392009-06-12 14:14:19 +0300360 struct cmd_read_write_memory *cmd;
361 int ret = 0;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300362
Kalle Valo80301cd2009-06-12 14:17:39 +0300363 wl1251_debug(DEBUG_CMD, "cmd read memory");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300364
Kalle Valoff258392009-06-12 14:14:19 +0300365 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
366 if (!cmd) {
367 ret = -ENOMEM;
368 goto out;
369 }
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300370
Kalle Valoff258392009-06-12 14:14:19 +0300371 WARN_ON(len > MAX_READ_SIZE);
372 len = min_t(size_t, len, MAX_READ_SIZE);
373
374 cmd->addr = addr;
375 cmd->size = len;
376
Kalle Valo80301cd2009-06-12 14:17:39 +0300377 ret = wl1251_cmd_send(wl, CMD_READ_MEMORY, cmd, sizeof(*cmd));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300378 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300379 wl1251_error("read memory command failed: %d", ret);
Kalle Valoff258392009-06-12 14:14:19 +0300380 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300381 }
382
383 /* the read command got in, we can now read the answer */
Bob Copeland0764de62009-08-07 13:32:56 +0300384 wl1251_mem_read(wl, wl->cmd_box_addr, cmd, sizeof(*cmd));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300385
Kalle Valoff258392009-06-12 14:14:19 +0300386 if (cmd->header.status != CMD_STATUS_SUCCESS)
Kalle Valo80301cd2009-06-12 14:17:39 +0300387 wl1251_error("error in read command result: %d",
Kalle Valoff258392009-06-12 14:14:19 +0300388 cmd->header.status);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300389
Kalle Valoff258392009-06-12 14:14:19 +0300390 memcpy(answer, cmd->value, len);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300391
Kalle Valoff258392009-06-12 14:14:19 +0300392out:
393 kfree(cmd);
394 return ret;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300395}
396
Kalle Valo80301cd2009-06-12 14:17:39 +0300397int wl1251_cmd_template_set(struct wl1251 *wl, u16 cmd_id,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300398 void *buf, size_t buf_len)
399{
Kalle Valo80301cd2009-06-12 14:17:39 +0300400 struct wl1251_cmd_packet_template *cmd;
Kalle Valoff258392009-06-12 14:14:19 +0300401 size_t cmd_len;
402 int ret = 0;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300403
Kalle Valo80301cd2009-06-12 14:17:39 +0300404 wl1251_debug(DEBUG_CMD, "cmd template %d", cmd_id);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300405
Kalle Valo80301cd2009-06-12 14:17:39 +0300406 WARN_ON(buf_len > WL1251_MAX_TEMPLATE_SIZE);
407 buf_len = min_t(size_t, buf_len, WL1251_MAX_TEMPLATE_SIZE);
Kalle Valoff258392009-06-12 14:14:19 +0300408 cmd_len = ALIGN(sizeof(*cmd) + buf_len, 4);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300409
Kalle Valoff258392009-06-12 14:14:19 +0300410 cmd = kzalloc(cmd_len, GFP_KERNEL);
411 if (!cmd) {
412 ret = -ENOMEM;
413 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300414 }
415
Kalle Valoff258392009-06-12 14:14:19 +0300416 cmd->size = cpu_to_le16(buf_len);
417
418 if (buf)
419 memcpy(cmd->data, buf, buf_len);
420
Kalle Valo80301cd2009-06-12 14:17:39 +0300421 ret = wl1251_cmd_send(wl, cmd_id, cmd, cmd_len);
Kalle Valoff258392009-06-12 14:14:19 +0300422 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300423 wl1251_warning("cmd set_template failed: %d", ret);
Kalle Valoff258392009-06-12 14:14:19 +0300424 goto out;
425 }
426
427out:
428 kfree(cmd);
429 return ret;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300430}