blob: dc04d1fc2ee470ac76b14a925938956749b2374e [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 "reg.h"
Kalle Valoef2f8d42009-06-12 14:17:19 +03009#include "wl1251_spi.h"
10#include "wl1251_ps.h"
11#include "wl1251_acx.h"
Kalle Valo2f01a1f2009-04-29 23:33:31 +030012
Kalle Valoff258392009-06-12 14:14:19 +030013/**
14 * send command to firmware
15 *
16 * @wl: wl struct
17 * @id: command id
18 * @buf: buffer containing the command, must work with dma
19 * @len: length of the buffer
20 */
Kalle Valo80301cd2009-06-12 14:17:39 +030021int wl1251_cmd_send(struct wl1251 *wl, u16 id, void *buf, size_t len)
Kalle Valo2f01a1f2009-04-29 23:33:31 +030022{
Kalle Valo80301cd2009-06-12 14:17:39 +030023 struct wl1251_cmd_header *cmd;
Kalle Valo2f01a1f2009-04-29 23:33:31 +030024 unsigned long timeout;
Kalle Valo2f01a1f2009-04-29 23:33:31 +030025 u32 intr;
26 int ret = 0;
27
Kalle Valoff258392009-06-12 14:14:19 +030028 cmd = buf;
29 cmd->id = id;
30 cmd->status = 0;
31
32 WARN_ON(len % 4 != 0);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030033
Kalle Valo80301cd2009-06-12 14:17:39 +030034 wl1251_spi_mem_write(wl, wl->cmd_box_addr, buf, len);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030035
Kalle Valo80301cd2009-06-12 14:17:39 +030036 wl1251_reg_write32(wl, ACX_REG_INTERRUPT_TRIG, INTR_TRIG_CMD);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030037
Kalle Valo80301cd2009-06-12 14:17:39 +030038 timeout = jiffies + msecs_to_jiffies(WL1251_COMMAND_TIMEOUT);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030039
Kalle Valo80301cd2009-06-12 14:17:39 +030040 intr = wl1251_reg_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030041 while (!(intr & wl->chip.intr_cmd_complete)) {
42 if (time_after(jiffies, timeout)) {
Kalle Valo80301cd2009-06-12 14:17:39 +030043 wl1251_error("command complete timeout");
Kalle Valo2f01a1f2009-04-29 23:33:31 +030044 ret = -ETIMEDOUT;
45 goto out;
46 }
47
48 msleep(1);
49
Kalle Valo80301cd2009-06-12 14:17:39 +030050 intr = wl1251_reg_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030051 }
52
Kalle Valo80301cd2009-06-12 14:17:39 +030053 wl1251_reg_write32(wl, ACX_REG_INTERRUPT_ACK,
Kalle Valo2f01a1f2009-04-29 23:33:31 +030054 wl->chip.intr_cmd_complete);
55
56out:
Kalle Valo2f01a1f2009-04-29 23:33:31 +030057 return ret;
58}
59
Kalle Valoff258392009-06-12 14:14:19 +030060/**
61 * send test command to firmware
62 *
63 * @wl: wl struct
Ari Kauppi6021b282009-06-12 14:16:13 +030064 * @buf: buffer containing the command, with all headers, must work with dma
Kalle Valoff258392009-06-12 14:14:19 +030065 * @len: length of the buffer
66 * @answer: is answer needed
Kalle Valoff258392009-06-12 14:14:19 +030067 */
Kalle Valo80301cd2009-06-12 14:17:39 +030068int wl1251_cmd_test(struct wl1251 *wl, void *buf, size_t buf_len, u8 answer)
Kalle Valo2f01a1f2009-04-29 23:33:31 +030069{
70 int ret;
71
Kalle Valo80301cd2009-06-12 14:17:39 +030072 wl1251_debug(DEBUG_CMD, "cmd test");
Kalle Valo2f01a1f2009-04-29 23:33:31 +030073
Kalle Valo80301cd2009-06-12 14:17:39 +030074 ret = wl1251_cmd_send(wl, CMD_TEST, buf, buf_len);
Kalle Valoff258392009-06-12 14:14:19 +030075
Kalle Valo2f01a1f2009-04-29 23:33:31 +030076 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +030077 wl1251_warning("TEST command failed");
Ari Kauppi6021b282009-06-12 14:16:13 +030078 return ret;
Kalle Valo2f01a1f2009-04-29 23:33:31 +030079 }
80
81 if (answer) {
Kalle Valo80301cd2009-06-12 14:17:39 +030082 struct wl1251_command *cmd_answer;
Ari Kauppi6021b282009-06-12 14:16:13 +030083
Kalle Valo2f01a1f2009-04-29 23:33:31 +030084 /*
85 * The test command got in, we can read the answer.
Kalle Valo80301cd2009-06-12 14:17:39 +030086 * The answer would be a wl1251_command, where the
Kalle Valo2f01a1f2009-04-29 23:33:31 +030087 * parameter array contains the actual answer.
88 */
Kalle Valo80301cd2009-06-12 14:17:39 +030089 wl1251_spi_mem_read(wl, wl->cmd_box_addr, buf, buf_len);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030090
Ari Kauppi6021b282009-06-12 14:16:13 +030091 cmd_answer = buf;
92
93 if (cmd_answer->header.status != CMD_STATUS_SUCCESS)
Kalle Valo80301cd2009-06-12 14:17:39 +030094 wl1251_error("TEST command answer error: %d",
Ari Kauppi6021b282009-06-12 14:16:13 +030095 cmd_answer->header.status);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030096 }
97
Ari Kauppi6021b282009-06-12 14:16:13 +030098 return 0;
Kalle Valo2f01a1f2009-04-29 23:33:31 +030099}
100
Kalle Valoff258392009-06-12 14:14:19 +0300101/**
102 * read acx from firmware
103 *
104 * @wl: wl struct
105 * @id: acx id
106 * @buf: buffer for the response, including all headers, must work with dma
107 * @len: lenght of buf
108 */
Kalle Valo80301cd2009-06-12 14:17:39 +0300109int wl1251_cmd_interrogate(struct wl1251 *wl, u16 id, void *buf, size_t len)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300110{
Kalle Valoff258392009-06-12 14:14:19 +0300111 struct acx_header *acx = buf;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300112 int ret;
113
Kalle Valo80301cd2009-06-12 14:17:39 +0300114 wl1251_debug(DEBUG_CMD, "cmd interrogate");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300115
Kalle Valoff258392009-06-12 14:14:19 +0300116 acx->id = id;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300117
Kalle Valoff258392009-06-12 14:14:19 +0300118 /* payload length, does not include any headers */
119 acx->len = len - sizeof(*acx);
120
Kalle Valo80301cd2009-06-12 14:17:39 +0300121 ret = wl1251_cmd_send(wl, CMD_INTERROGATE, acx, sizeof(*acx));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300122 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300123 wl1251_error("INTERROGATE command failed");
Kalle Valoff258392009-06-12 14:14:19 +0300124 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300125 }
126
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300127 /* the interrogate command got in, we can read the answer */
Kalle Valo80301cd2009-06-12 14:17:39 +0300128 wl1251_spi_mem_read(wl, wl->cmd_box_addr, buf, len);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300129
Kalle Valoff258392009-06-12 14:14:19 +0300130 acx = buf;
131 if (acx->cmd.status != CMD_STATUS_SUCCESS)
Kalle Valo80301cd2009-06-12 14:17:39 +0300132 wl1251_error("INTERROGATE command error: %d",
Kalle Valoff258392009-06-12 14:14:19 +0300133 acx->cmd.status);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300134
Kalle Valoff258392009-06-12 14:14:19 +0300135out:
136 return ret;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300137}
138
Kalle Valoff258392009-06-12 14:14:19 +0300139/**
140 * write acx value to firmware
141 *
142 * @wl: wl struct
143 * @id: acx id
144 * @buf: buffer containing acx, including all headers, must work with dma
145 * @len: length of buf
146 */
Kalle Valo80301cd2009-06-12 14:17:39 +0300147int wl1251_cmd_configure(struct wl1251 *wl, u16 id, void *buf, size_t len)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300148{
Kalle Valoff258392009-06-12 14:14:19 +0300149 struct acx_header *acx = buf;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300150 int ret;
151
Kalle Valo80301cd2009-06-12 14:17:39 +0300152 wl1251_debug(DEBUG_CMD, "cmd configure");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300153
Kalle Valoff258392009-06-12 14:14:19 +0300154 acx->id = id;
155
156 /* payload length, does not include any headers */
157 acx->len = len - sizeof(*acx);
158
Kalle Valo80301cd2009-06-12 14:17:39 +0300159 ret = wl1251_cmd_send(wl, CMD_CONFIGURE, acx, len);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300160 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300161 wl1251_warning("CONFIGURE command NOK");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300162 return ret;
163 }
164
165 return 0;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300166}
167
Kalle Valo80301cd2009-06-12 14:17:39 +0300168int wl1251_cmd_vbm(struct wl1251 *wl, u8 identity,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300169 void *bitmap, u16 bitmap_len, u8 bitmap_control)
170{
Kalle Valo80301cd2009-06-12 14:17:39 +0300171 struct wl1251_cmd_vbm_update *vbm;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300172 int ret;
173
Kalle Valo80301cd2009-06-12 14:17:39 +0300174 wl1251_debug(DEBUG_CMD, "cmd vbm");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300175
Kalle Valoff258392009-06-12 14:14:19 +0300176 vbm = kzalloc(sizeof(*vbm), GFP_KERNEL);
177 if (!vbm) {
178 ret = -ENOMEM;
179 goto out;
180 }
181
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300182 /* Count and period will be filled by the target */
Kalle Valoff258392009-06-12 14:14:19 +0300183 vbm->tim.bitmap_ctrl = bitmap_control;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300184 if (bitmap_len > PARTIAL_VBM_MAX) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300185 wl1251_warning("cmd vbm len is %d B, truncating to %d",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300186 bitmap_len, PARTIAL_VBM_MAX);
187 bitmap_len = PARTIAL_VBM_MAX;
188 }
Kalle Valoff258392009-06-12 14:14:19 +0300189 memcpy(vbm->tim.pvb_field, bitmap, bitmap_len);
190 vbm->tim.identity = identity;
191 vbm->tim.length = bitmap_len + 3;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300192
Kalle Valoff258392009-06-12 14:14:19 +0300193 vbm->len = cpu_to_le16(bitmap_len + 5);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300194
Kalle Valo80301cd2009-06-12 14:17:39 +0300195 ret = wl1251_cmd_send(wl, CMD_VBM, vbm, sizeof(*vbm));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300196 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300197 wl1251_error("VBM command failed");
Kalle Valoff258392009-06-12 14:14:19 +0300198 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300199 }
200
Kalle Valoff258392009-06-12 14:14:19 +0300201out:
202 kfree(vbm);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300203 return 0;
204}
205
Kalle Valo80301cd2009-06-12 14:17:39 +0300206int wl1251_cmd_data_path(struct wl1251 *wl, u8 channel, bool enable)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300207{
Kalle Valoff258392009-06-12 14:14:19 +0300208 struct cmd_enabledisable_path *cmd;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300209 int ret;
210 u16 cmd_rx, cmd_tx;
211
Kalle Valo80301cd2009-06-12 14:17:39 +0300212 wl1251_debug(DEBUG_CMD, "cmd data path");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300213
Kalle Valoff258392009-06-12 14:14:19 +0300214 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
215 if (!cmd) {
216 ret = -ENOMEM;
217 goto out;
218 }
219
220 cmd->channel = channel;
221
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300222 if (enable) {
223 cmd_rx = CMD_ENABLE_RX;
224 cmd_tx = CMD_ENABLE_TX;
225 } else {
226 cmd_rx = CMD_DISABLE_RX;
227 cmd_tx = CMD_DISABLE_TX;
228 }
229
Kalle Valo80301cd2009-06-12 14:17:39 +0300230 ret = wl1251_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300231 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300232 wl1251_error("rx %s cmd for channel %d failed",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300233 enable ? "start" : "stop", channel);
Kalle Valoff258392009-06-12 14:14:19 +0300234 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300235 }
236
Kalle Valo80301cd2009-06-12 14:17:39 +0300237 wl1251_debug(DEBUG_BOOT, "rx %s cmd channel %d",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300238 enable ? "start" : "stop", channel);
239
Kalle Valo80301cd2009-06-12 14:17:39 +0300240 ret = wl1251_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300241 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300242 wl1251_error("tx %s cmd for channel %d failed",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300243 enable ? "start" : "stop", channel);
244 return ret;
245 }
246
Kalle Valo80301cd2009-06-12 14:17:39 +0300247 wl1251_debug(DEBUG_BOOT, "tx %s cmd channel %d",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300248 enable ? "start" : "stop", channel);
249
Kalle Valoff258392009-06-12 14:14:19 +0300250out:
251 kfree(cmd);
252 return ret;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300253}
254
Kalle Valo80301cd2009-06-12 14:17:39 +0300255int wl1251_cmd_join(struct wl1251 *wl, u8 bss_type, u8 dtim_interval,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300256 u16 beacon_interval, u8 wait)
257{
258 unsigned long timeout;
Kalle Valoff258392009-06-12 14:14:19 +0300259 struct cmd_join *join;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300260 int ret, i;
261 u8 *bssid;
262
Kalle Valoff258392009-06-12 14:14:19 +0300263 join = kzalloc(sizeof(*join), GFP_KERNEL);
264 if (!join) {
265 ret = -ENOMEM;
266 goto out;
267 }
268
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300269 /* FIXME: this should be in main.c */
Kalle Valo80301cd2009-06-12 14:17:39 +0300270 ret = wl1251_acx_frame_rates(wl, DEFAULT_HW_GEN_TX_RATE,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300271 DEFAULT_HW_GEN_MODULATION_TYPE,
272 wl->tx_mgmt_frm_rate,
273 wl->tx_mgmt_frm_mod);
274 if (ret < 0)
Kalle Valoff258392009-06-12 14:14:19 +0300275 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300276
Kalle Valo80301cd2009-06-12 14:17:39 +0300277 wl1251_debug(DEBUG_CMD, "cmd join");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300278
279 /* Reverse order BSSID */
Kalle Valoff258392009-06-12 14:14:19 +0300280 bssid = (u8 *) &join->bssid_lsb;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300281 for (i = 0; i < ETH_ALEN; i++)
282 bssid[i] = wl->bssid[ETH_ALEN - i - 1];
283
Kalle Valoff258392009-06-12 14:14:19 +0300284 join->rx_config_options = wl->rx_config;
285 join->rx_filter_options = wl->rx_filter;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300286
Kalle Valoff258392009-06-12 14:14:19 +0300287 join->basic_rate_set = RATE_MASK_1MBPS | RATE_MASK_2MBPS |
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300288 RATE_MASK_5_5MBPS | RATE_MASK_11MBPS;
289
Kalle Valoff258392009-06-12 14:14:19 +0300290 join->beacon_interval = beacon_interval;
291 join->dtim_interval = dtim_interval;
292 join->bss_type = bss_type;
293 join->channel = wl->channel;
294 join->ctrl = JOIN_CMD_CTRL_TX_FLUSH;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300295
Kalle Valo80301cd2009-06-12 14:17:39 +0300296 ret = wl1251_cmd_send(wl, CMD_START_JOIN, join, sizeof(*join));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300297 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300298 wl1251_error("failed to initiate cmd join");
Kalle Valoff258392009-06-12 14:14:19 +0300299 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300300 }
301
302 timeout = msecs_to_jiffies(JOIN_TIMEOUT);
303
304 /*
305 * ugly hack: we should wait for JOIN_EVENT_COMPLETE_ID but to
306 * simplify locking we just sleep instead, for now
307 */
308 if (wait)
309 msleep(10);
310
Kalle Valoff258392009-06-12 14:14:19 +0300311out:
312 kfree(join);
313 return ret;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300314}
315
Kalle Valo80301cd2009-06-12 14:17:39 +0300316int wl1251_cmd_ps_mode(struct wl1251 *wl, u8 ps_mode)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300317{
Kalle Valo80301cd2009-06-12 14:17:39 +0300318 struct wl1251_cmd_ps_params *ps_params = NULL;
Kalle Valoff258392009-06-12 14:14:19 +0300319 int ret = 0;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300320
321 /* FIXME: this should be in ps.c */
Kalle Valo80301cd2009-06-12 14:17:39 +0300322 ret = wl1251_acx_wake_up_conditions(wl, WAKE_UP_EVENT_DTIM_BITMAP,
Luciano Coelho9f483dc2009-06-12 14:15:46 +0300323 wl->listen_int);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300324 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300325 wl1251_error("couldn't set wake up conditions");
Kalle Valoff258392009-06-12 14:14:19 +0300326 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300327 }
328
Kalle Valo80301cd2009-06-12 14:17:39 +0300329 wl1251_debug(DEBUG_CMD, "cmd set ps mode");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300330
Kalle Valoff258392009-06-12 14:14:19 +0300331 ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
332 if (!ps_params) {
333 ret = -ENOMEM;
334 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300335 }
336
Kalle Valoff258392009-06-12 14:14:19 +0300337 ps_params->ps_mode = ps_mode;
338 ps_params->send_null_data = 1;
339 ps_params->retries = 5;
340 ps_params->hang_over_period = 128;
341 ps_params->null_data_rate = 1; /* 1 Mbps */
342
Kalle Valo80301cd2009-06-12 14:17:39 +0300343 ret = wl1251_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
Kalle Valoff258392009-06-12 14:14:19 +0300344 sizeof(*ps_params));
345 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300346 wl1251_error("cmd set_ps_mode failed");
Kalle Valoff258392009-06-12 14:14:19 +0300347 goto out;
348 }
349
350out:
351 kfree(ps_params);
352 return ret;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300353}
354
Kalle Valo80301cd2009-06-12 14:17:39 +0300355int wl1251_cmd_read_memory(struct wl1251 *wl, u32 addr, void *answer,
Kalle Valoff258392009-06-12 14:14:19 +0300356 size_t len)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300357{
Kalle Valoff258392009-06-12 14:14:19 +0300358 struct cmd_read_write_memory *cmd;
359 int ret = 0;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300360
Kalle Valo80301cd2009-06-12 14:17:39 +0300361 wl1251_debug(DEBUG_CMD, "cmd read memory");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300362
Kalle Valoff258392009-06-12 14:14:19 +0300363 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
364 if (!cmd) {
365 ret = -ENOMEM;
366 goto out;
367 }
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300368
Kalle Valoff258392009-06-12 14:14:19 +0300369 WARN_ON(len > MAX_READ_SIZE);
370 len = min_t(size_t, len, MAX_READ_SIZE);
371
372 cmd->addr = addr;
373 cmd->size = len;
374
Kalle Valo80301cd2009-06-12 14:17:39 +0300375 ret = wl1251_cmd_send(wl, CMD_READ_MEMORY, cmd, sizeof(*cmd));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300376 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300377 wl1251_error("read memory command failed: %d", ret);
Kalle Valoff258392009-06-12 14:14:19 +0300378 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300379 }
380
381 /* the read command got in, we can now read the answer */
Kalle Valo80301cd2009-06-12 14:17:39 +0300382 wl1251_spi_mem_read(wl, wl->cmd_box_addr, cmd, sizeof(*cmd));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300383
Kalle Valoff258392009-06-12 14:14:19 +0300384 if (cmd->header.status != CMD_STATUS_SUCCESS)
Kalle Valo80301cd2009-06-12 14:17:39 +0300385 wl1251_error("error in read command result: %d",
Kalle Valoff258392009-06-12 14:14:19 +0300386 cmd->header.status);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300387
Kalle Valoff258392009-06-12 14:14:19 +0300388 memcpy(answer, cmd->value, len);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300389
Kalle Valoff258392009-06-12 14:14:19 +0300390out:
391 kfree(cmd);
392 return ret;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300393}
394
Kalle Valo80301cd2009-06-12 14:17:39 +0300395int wl1251_cmd_template_set(struct wl1251 *wl, u16 cmd_id,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300396 void *buf, size_t buf_len)
397{
Kalle Valo80301cd2009-06-12 14:17:39 +0300398 struct wl1251_cmd_packet_template *cmd;
Kalle Valoff258392009-06-12 14:14:19 +0300399 size_t cmd_len;
400 int ret = 0;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300401
Kalle Valo80301cd2009-06-12 14:17:39 +0300402 wl1251_debug(DEBUG_CMD, "cmd template %d", cmd_id);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300403
Kalle Valo80301cd2009-06-12 14:17:39 +0300404 WARN_ON(buf_len > WL1251_MAX_TEMPLATE_SIZE);
405 buf_len = min_t(size_t, buf_len, WL1251_MAX_TEMPLATE_SIZE);
Kalle Valoff258392009-06-12 14:14:19 +0300406 cmd_len = ALIGN(sizeof(*cmd) + buf_len, 4);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300407
Kalle Valoff258392009-06-12 14:14:19 +0300408 cmd = kzalloc(cmd_len, GFP_KERNEL);
409 if (!cmd) {
410 ret = -ENOMEM;
411 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300412 }
413
Kalle Valoff258392009-06-12 14:14:19 +0300414 cmd->size = cpu_to_le16(buf_len);
415
416 if (buf)
417 memcpy(cmd->data, buf, buf_len);
418
Kalle Valo80301cd2009-06-12 14:17:39 +0300419 ret = wl1251_cmd_send(wl, cmd_id, cmd, cmd_len);
Kalle Valoff258392009-06-12 14:14:19 +0300420 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300421 wl1251_warning("cmd set_template failed: %d", ret);
Kalle Valoff258392009-06-12 14:14:19 +0300422 goto out;
423 }
424
425out:
426 kfree(cmd);
427 return ret;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300428}