1#include "wl1251_cmd.h"
2
3#include <linux/module.h>
4#include <linux/crc7.h>
5
6#include "wl1251.h"
7#include "wl1251_reg.h"
8#include "wl1251_io.h"
9#include "wl1251_ps.h"
10#include "wl1251_acx.h"
11
12
13
14
15
16
17
18
19
20int wl1251_cmd_send(struct wl1251 *wl, u16 id, void *buf, size_t len)
21{
22 struct wl1251_cmd_header *cmd;
23 unsigned long timeout;
24 u32 intr;
25 int ret = 0;
26
27 cmd = buf;
28 cmd->id = id;
29 cmd->status = 0;
30
31 WARN_ON(len % 4 != 0);
32
33 wl1251_mem_write(wl, wl->cmd_box_addr, buf, len);
34
35 wl1251_reg_write32(wl, ACX_REG_INTERRUPT_TRIG, INTR_TRIG_CMD);
36
37 timeout = jiffies + msecs_to_jiffies(WL1251_COMMAND_TIMEOUT);
38
39 intr = wl1251_reg_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
40 while (!(intr & WL1251_ACX_INTR_CMD_COMPLETE)) {
41 if (time_after(jiffies, timeout)) {
42 wl1251_error("command complete timeout");
43 ret = -ETIMEDOUT;
44 goto out;
45 }
46
47 msleep(1);
48
49 intr = wl1251_reg_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
50 }
51
52 wl1251_reg_write32(wl, ACX_REG_INTERRUPT_ACK,
53 WL1251_ACX_INTR_CMD_COMPLETE);
54
55out:
56 return ret;
57}
58
59
60
61
62
63
64
65
66
67int wl1251_cmd_test(struct wl1251 *wl, void *buf, size_t buf_len, u8 answer)
68{
69 int ret;
70
71 wl1251_debug(DEBUG_CMD, "cmd test");
72
73 ret = wl1251_cmd_send(wl, CMD_TEST, buf, buf_len);
74
75 if (ret < 0) {
76 wl1251_warning("TEST command failed");
77 return ret;
78 }
79
80 if (answer) {
81 struct wl1251_command *cmd_answer;
82
83
84
85
86
87
88 wl1251_mem_read(wl, wl->cmd_box_addr, buf, buf_len);
89
90 cmd_answer = buf;
91
92 if (cmd_answer->header.status != CMD_STATUS_SUCCESS)
93 wl1251_error("TEST command answer error: %d",
94 cmd_answer->header.status);
95 }
96
97 return 0;
98}
99
100
101
102
103
104
105
106
107
108int wl1251_cmd_interrogate(struct wl1251 *wl, u16 id, void *buf, size_t len)
109{
110 struct acx_header *acx = buf;
111 int ret;
112
113 wl1251_debug(DEBUG_CMD, "cmd interrogate");
114
115 acx->id = id;
116
117
118 acx->len = len - sizeof(*acx);
119
120 ret = wl1251_cmd_send(wl, CMD_INTERROGATE, acx, sizeof(*acx));
121 if (ret < 0) {
122 wl1251_error("INTERROGATE command failed");
123 goto out;
124 }
125
126
127 wl1251_mem_read(wl, wl->cmd_box_addr, buf, len);
128
129 acx = buf;
130 if (acx->cmd.status != CMD_STATUS_SUCCESS)
131 wl1251_error("INTERROGATE command error: %d",
132 acx->cmd.status);
133
134out:
135 return ret;
136}
137
138
139
140
141
142
143
144
145
146int wl1251_cmd_configure(struct wl1251 *wl, u16 id, void *buf, size_t len)
147{
148 struct acx_header *acx = buf;
149 int ret;
150
151 wl1251_debug(DEBUG_CMD, "cmd configure");
152
153 acx->id = id;
154
155
156 acx->len = len - sizeof(*acx);
157
158 ret = wl1251_cmd_send(wl, CMD_CONFIGURE, acx, len);
159 if (ret < 0) {
160 wl1251_warning("CONFIGURE command NOK");
161 return ret;
162 }
163
164 return 0;
165}
166
167int wl1251_cmd_vbm(struct wl1251 *wl, u8 identity,
168 void *bitmap, u16 bitmap_len, u8 bitmap_control)
169{
170 struct wl1251_cmd_vbm_update *vbm;
171 int ret;
172
173 wl1251_debug(DEBUG_CMD, "cmd vbm");
174
175 vbm = kzalloc(sizeof(*vbm), GFP_KERNEL);
176 if (!vbm) {
177 ret = -ENOMEM;
178 goto out;
179 }
180
181
182 vbm->tim.bitmap_ctrl = bitmap_control;
183 if (bitmap_len > PARTIAL_VBM_MAX) {
184 wl1251_warning("cmd vbm len is %d B, truncating to %d",
185 bitmap_len, PARTIAL_VBM_MAX);
186 bitmap_len = PARTIAL_VBM_MAX;
187 }
188 memcpy(vbm->tim.pvb_field, bitmap, bitmap_len);
189 vbm->tim.identity = identity;
190 vbm->tim.length = bitmap_len + 3;
191
192 vbm->len = cpu_to_le16(bitmap_len + 5);
193
194 ret = wl1251_cmd_send(wl, CMD_VBM, vbm, sizeof(*vbm));
195 if (ret < 0) {
196 wl1251_error("VBM command failed");
197 goto out;
198 }
199
200out:
201 kfree(vbm);
202 return 0;
203}
204
205int wl1251_cmd_data_path(struct wl1251 *wl, u8 channel, bool enable)
206{
207 struct cmd_enabledisable_path *cmd;
208 int ret;
209 u16 cmd_rx, cmd_tx;
210
211 wl1251_debug(DEBUG_CMD, "cmd data path");
212
213 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
214 if (!cmd) {
215 ret = -ENOMEM;
216 goto out;
217 }
218
219 cmd->channel = channel;
220
221 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
229 ret = wl1251_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd));
230 if (ret < 0) {
231 wl1251_error("rx %s cmd for channel %d failed",
232 enable ? "start" : "stop", channel);
233 goto out;
234 }
235
236 wl1251_debug(DEBUG_BOOT, "rx %s cmd channel %d",
237 enable ? "start" : "stop", channel);
238
239 ret = wl1251_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd));
240 if (ret < 0) {
241 wl1251_error("tx %s cmd for channel %d failed",
242 enable ? "start" : "stop", channel);
243 return ret;
244 }
245
246 wl1251_debug(DEBUG_BOOT, "tx %s cmd channel %d",
247 enable ? "start" : "stop", channel);
248
249out:
250 kfree(cmd);
251 return ret;
252}
253
254int wl1251_cmd_join(struct wl1251 *wl, u8 bss_type, u8 channel,
255 u16 beacon_interval, u8 dtim_interval)
256{
257 struct cmd_join *join;
258 int ret, i;
259 u8 *bssid;
260
261 join = kzalloc(sizeof(*join), GFP_KERNEL);
262 if (!join) {
263 ret = -ENOMEM;
264 goto out;
265 }
266
267 wl1251_debug(DEBUG_CMD, "cmd join%s ch %d %d/%d",
268 bss_type == BSS_TYPE_IBSS ? " ibss" : "",
269 channel, beacon_interval, dtim_interval);
270
271
272 bssid = (u8 *) &join->bssid_lsb;
273 for (i = 0; i < ETH_ALEN; i++)
274 bssid[i] = wl->bssid[ETH_ALEN - i - 1];
275
276 join->rx_config_options = wl->rx_config;
277 join->rx_filter_options = wl->rx_filter;
278
279
280
281
282
283
284
285 join->rx_config_options = 0;
286 join->rx_filter_options = WL1251_DEFAULT_RX_FILTER;
287
288 join->basic_rate_set = RATE_MASK_1MBPS | RATE_MASK_2MBPS |
289 RATE_MASK_5_5MBPS | RATE_MASK_11MBPS;
290
291 join->beacon_interval = beacon_interval;
292 join->dtim_interval = dtim_interval;
293 join->bss_type = bss_type;
294 join->channel = channel;
295 join->ctrl = JOIN_CMD_CTRL_TX_FLUSH;
296
297 ret = wl1251_cmd_send(wl, CMD_START_JOIN, join, sizeof(*join));
298 if (ret < 0) {
299 wl1251_error("failed to initiate cmd join");
300 goto out;
301 }
302
303out:
304 kfree(join);
305 return ret;
306}
307
308int wl1251_cmd_ps_mode(struct wl1251 *wl, u8 ps_mode)
309{
310 struct wl1251_cmd_ps_params *ps_params = NULL;
311 int ret = 0;
312
313 wl1251_debug(DEBUG_CMD, "cmd set ps mode");
314
315 ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
316 if (!ps_params) {
317 ret = -ENOMEM;
318 goto out;
319 }
320
321 ps_params->ps_mode = ps_mode;
322 ps_params->send_null_data = 1;
323 ps_params->retries = 5;
324 ps_params->hang_over_period = 128;
325 ps_params->null_data_rate = 1;
326
327 ret = wl1251_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
328 sizeof(*ps_params));
329 if (ret < 0) {
330 wl1251_error("cmd set_ps_mode failed");
331 goto out;
332 }
333
334out:
335 kfree(ps_params);
336 return ret;
337}
338
339int wl1251_cmd_read_memory(struct wl1251 *wl, u32 addr, void *answer,
340 size_t len)
341{
342 struct cmd_read_write_memory *cmd;
343 int ret = 0;
344
345 wl1251_debug(DEBUG_CMD, "cmd read memory");
346
347 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
348 if (!cmd) {
349 ret = -ENOMEM;
350 goto out;
351 }
352
353 WARN_ON(len > MAX_READ_SIZE);
354 len = min_t(size_t, len, MAX_READ_SIZE);
355
356 cmd->addr = addr;
357 cmd->size = len;
358
359 ret = wl1251_cmd_send(wl, CMD_READ_MEMORY, cmd, sizeof(*cmd));
360 if (ret < 0) {
361 wl1251_error("read memory command failed: %d", ret);
362 goto out;
363 }
364
365
366 wl1251_mem_read(wl, wl->cmd_box_addr, cmd, sizeof(*cmd));
367
368 if (cmd->header.status != CMD_STATUS_SUCCESS)
369 wl1251_error("error in read command result: %d",
370 cmd->header.status);
371
372 memcpy(answer, cmd->value, len);
373
374out:
375 kfree(cmd);
376 return ret;
377}
378
379int wl1251_cmd_template_set(struct wl1251 *wl, u16 cmd_id,
380 void *buf, size_t buf_len)
381{
382 struct wl1251_cmd_packet_template *cmd;
383 size_t cmd_len;
384 int ret = 0;
385
386 wl1251_debug(DEBUG_CMD, "cmd template %d", cmd_id);
387
388 WARN_ON(buf_len > WL1251_MAX_TEMPLATE_SIZE);
389 buf_len = min_t(size_t, buf_len, WL1251_MAX_TEMPLATE_SIZE);
390 cmd_len = ALIGN(sizeof(*cmd) + buf_len, 4);
391
392 cmd = kzalloc(cmd_len, GFP_KERNEL);
393 if (!cmd) {
394 ret = -ENOMEM;
395 goto out;
396 }
397
398 cmd->size = cpu_to_le16(buf_len);
399
400 if (buf)
401 memcpy(cmd->data, buf, buf_len);
402
403 ret = wl1251_cmd_send(wl, cmd_id, cmd, cmd_len);
404 if (ret < 0) {
405 wl1251_warning("cmd set_template failed: %d", ret);
406 goto out;
407 }
408
409out:
410 kfree(cmd);
411 return ret;
412}
413