RT-AC56 3.0.0.4.374.37 core
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / net / wireless / wl12xx / wl1251_cmd.c
blob9eefeabe5d05116ac5d47002c953a7b667a8eb5e
1 #include "wl1251_cmd.h"
3 #include <linux/module.h>
4 #include <linux/slab.h>
5 #include <linux/crc7.h>
7 #include "wl1251.h"
8 #include "wl1251_reg.h"
9 #include "wl1251_io.h"
10 #include "wl1251_ps.h"
11 #include "wl1251_acx.h"
13 /**
14 * send command to firmware
16 * @wl: wl struct
17 * @id: command id
18 * @buf: buffer containing the command, must work with dma
19 * @len: length of the buffer
21 int wl1251_cmd_send(struct wl1251 *wl, u16 id, void *buf, size_t len)
23 struct wl1251_cmd_header *cmd;
24 unsigned long timeout;
25 u32 intr;
26 int ret = 0;
28 cmd = buf;
29 cmd->id = id;
30 cmd->status = 0;
32 WARN_ON(len % 4 != 0);
34 wl1251_mem_write(wl, wl->cmd_box_addr, buf, len);
36 wl1251_reg_write32(wl, ACX_REG_INTERRUPT_TRIG, INTR_TRIG_CMD);
38 timeout = jiffies + msecs_to_jiffies(WL1251_COMMAND_TIMEOUT);
40 intr = wl1251_reg_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
41 while (!(intr & WL1251_ACX_INTR_CMD_COMPLETE)) {
42 if (time_after(jiffies, timeout)) {
43 wl1251_error("command complete timeout");
44 ret = -ETIMEDOUT;
45 goto out;
48 msleep(1);
50 intr = wl1251_reg_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
53 wl1251_reg_write32(wl, ACX_REG_INTERRUPT_ACK,
54 WL1251_ACX_INTR_CMD_COMPLETE);
56 out:
57 return ret;
60 /**
61 * send test command to firmware
63 * @wl: wl struct
64 * @buf: buffer containing the command, with all headers, must work with dma
65 * @len: length of the buffer
66 * @answer: is answer needed
68 int wl1251_cmd_test(struct wl1251 *wl, void *buf, size_t buf_len, u8 answer)
70 int ret;
72 wl1251_debug(DEBUG_CMD, "cmd test");
74 ret = wl1251_cmd_send(wl, CMD_TEST, buf, buf_len);
76 if (ret < 0) {
77 wl1251_warning("TEST command failed");
78 return ret;
81 if (answer) {
82 struct wl1251_command *cmd_answer;
85 * The test command got in, we can read the answer.
86 * The answer would be a wl1251_command, where the
87 * parameter array contains the actual answer.
89 wl1251_mem_read(wl, wl->cmd_box_addr, buf, buf_len);
91 cmd_answer = buf;
93 if (cmd_answer->header.status != CMD_STATUS_SUCCESS)
94 wl1251_error("TEST command answer error: %d",
95 cmd_answer->header.status);
98 return 0;
102 * read acx from firmware
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
109 int wl1251_cmd_interrogate(struct wl1251 *wl, u16 id, void *buf, size_t len)
111 struct acx_header *acx = buf;
112 int ret;
114 wl1251_debug(DEBUG_CMD, "cmd interrogate");
116 acx->id = id;
118 /* payload length, does not include any headers */
119 acx->len = len - sizeof(*acx);
121 ret = wl1251_cmd_send(wl, CMD_INTERROGATE, acx, sizeof(*acx));
122 if (ret < 0) {
123 wl1251_error("INTERROGATE command failed");
124 goto out;
127 /* the interrogate command got in, we can read the answer */
128 wl1251_mem_read(wl, wl->cmd_box_addr, buf, len);
130 acx = buf;
131 if (acx->cmd.status != CMD_STATUS_SUCCESS)
132 wl1251_error("INTERROGATE command error: %d",
133 acx->cmd.status);
135 out:
136 return ret;
140 * write acx value to firmware
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
147 int wl1251_cmd_configure(struct wl1251 *wl, u16 id, void *buf, size_t len)
149 struct acx_header *acx = buf;
150 int ret;
152 wl1251_debug(DEBUG_CMD, "cmd configure");
154 acx->id = id;
156 /* payload length, does not include any headers */
157 acx->len = len - sizeof(*acx);
159 ret = wl1251_cmd_send(wl, CMD_CONFIGURE, acx, len);
160 if (ret < 0) {
161 wl1251_warning("CONFIGURE command NOK");
162 return ret;
165 return 0;
168 int wl1251_cmd_vbm(struct wl1251 *wl, u8 identity,
169 void *bitmap, u16 bitmap_len, u8 bitmap_control)
171 struct wl1251_cmd_vbm_update *vbm;
172 int ret;
174 wl1251_debug(DEBUG_CMD, "cmd vbm");
176 vbm = kzalloc(sizeof(*vbm), GFP_KERNEL);
177 if (!vbm) {
178 ret = -ENOMEM;
179 goto out;
182 /* Count and period will be filled by the target */
183 vbm->tim.bitmap_ctrl = bitmap_control;
184 if (bitmap_len > PARTIAL_VBM_MAX) {
185 wl1251_warning("cmd vbm len is %d B, truncating to %d",
186 bitmap_len, PARTIAL_VBM_MAX);
187 bitmap_len = PARTIAL_VBM_MAX;
189 memcpy(vbm->tim.pvb_field, bitmap, bitmap_len);
190 vbm->tim.identity = identity;
191 vbm->tim.length = bitmap_len + 3;
193 vbm->len = cpu_to_le16(bitmap_len + 5);
195 ret = wl1251_cmd_send(wl, CMD_VBM, vbm, sizeof(*vbm));
196 if (ret < 0) {
197 wl1251_error("VBM command failed");
198 goto out;
201 out:
202 kfree(vbm);
203 return 0;
206 int wl1251_cmd_data_path(struct wl1251 *wl, u8 channel, bool enable)
208 struct cmd_enabledisable_path *cmd;
209 int ret;
210 u16 cmd_rx, cmd_tx;
212 wl1251_debug(DEBUG_CMD, "cmd data path");
214 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
215 if (!cmd) {
216 ret = -ENOMEM;
217 goto out;
220 cmd->channel = channel;
222 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;
230 ret = wl1251_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd));
231 if (ret < 0) {
232 wl1251_error("rx %s cmd for channel %d failed",
233 enable ? "start" : "stop", channel);
234 goto out;
237 wl1251_debug(DEBUG_BOOT, "rx %s cmd channel %d",
238 enable ? "start" : "stop", channel);
240 ret = wl1251_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd));
241 if (ret < 0) {
242 wl1251_error("tx %s cmd for channel %d failed",
243 enable ? "start" : "stop", channel);
244 return ret;
247 wl1251_debug(DEBUG_BOOT, "tx %s cmd channel %d",
248 enable ? "start" : "stop", channel);
250 out:
251 kfree(cmd);
252 return ret;
255 int wl1251_cmd_join(struct wl1251 *wl, u8 bss_type, u8 channel,
256 u16 beacon_interval, u8 dtim_interval)
258 struct cmd_join *join;
259 int ret, i;
260 u8 *bssid;
262 join = kzalloc(sizeof(*join), GFP_KERNEL);
263 if (!join) {
264 ret = -ENOMEM;
265 goto out;
268 wl1251_debug(DEBUG_CMD, "cmd join%s ch %d %d/%d",
269 bss_type == BSS_TYPE_IBSS ? " ibss" : "",
270 channel, beacon_interval, dtim_interval);
272 /* Reverse order BSSID */
273 bssid = (u8 *) &join->bssid_lsb;
274 for (i = 0; i < ETH_ALEN; i++)
275 bssid[i] = wl->bssid[ETH_ALEN - i - 1];
277 join->rx_config_options = wl->rx_config;
278 join->rx_filter_options = wl->rx_filter;
280 join->rx_config_options = 0;
281 join->rx_filter_options = WL1251_DEFAULT_RX_FILTER;
283 join->basic_rate_set = RATE_MASK_1MBPS | RATE_MASK_2MBPS |
284 RATE_MASK_5_5MBPS | RATE_MASK_11MBPS;
286 join->beacon_interval = beacon_interval;
287 join->dtim_interval = dtim_interval;
288 join->bss_type = bss_type;
289 join->channel = channel;
290 join->ctrl = JOIN_CMD_CTRL_TX_FLUSH;
292 ret = wl1251_cmd_send(wl, CMD_START_JOIN, join, sizeof(*join));
293 if (ret < 0) {
294 wl1251_error("failed to initiate cmd join");
295 goto out;
298 out:
299 kfree(join);
300 return ret;
303 int wl1251_cmd_ps_mode(struct wl1251 *wl, u8 ps_mode)
305 struct wl1251_cmd_ps_params *ps_params = NULL;
306 int ret = 0;
308 wl1251_debug(DEBUG_CMD, "cmd set ps mode");
310 ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
311 if (!ps_params) {
312 ret = -ENOMEM;
313 goto out;
316 ps_params->ps_mode = ps_mode;
317 ps_params->send_null_data = 1;
318 ps_params->retries = 5;
319 ps_params->hang_over_period = 128;
320 ps_params->null_data_rate = 1; /* 1 Mbps */
322 ret = wl1251_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
323 sizeof(*ps_params));
324 if (ret < 0) {
325 wl1251_error("cmd set_ps_mode failed");
326 goto out;
329 out:
330 kfree(ps_params);
331 return ret;
334 int wl1251_cmd_read_memory(struct wl1251 *wl, u32 addr, void *answer,
335 size_t len)
337 struct cmd_read_write_memory *cmd;
338 int ret = 0;
340 wl1251_debug(DEBUG_CMD, "cmd read memory");
342 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
343 if (!cmd) {
344 ret = -ENOMEM;
345 goto out;
348 WARN_ON(len > MAX_READ_SIZE);
349 len = min_t(size_t, len, MAX_READ_SIZE);
351 cmd->addr = addr;
352 cmd->size = len;
354 ret = wl1251_cmd_send(wl, CMD_READ_MEMORY, cmd, sizeof(*cmd));
355 if (ret < 0) {
356 wl1251_error("read memory command failed: %d", ret);
357 goto out;
360 /* the read command got in, we can now read the answer */
361 wl1251_mem_read(wl, wl->cmd_box_addr, cmd, sizeof(*cmd));
363 if (cmd->header.status != CMD_STATUS_SUCCESS)
364 wl1251_error("error in read command result: %d",
365 cmd->header.status);
367 memcpy(answer, cmd->value, len);
369 out:
370 kfree(cmd);
371 return ret;
374 int wl1251_cmd_template_set(struct wl1251 *wl, u16 cmd_id,
375 void *buf, size_t buf_len)
377 struct wl1251_cmd_packet_template *cmd;
378 size_t cmd_len;
379 int ret = 0;
381 wl1251_debug(DEBUG_CMD, "cmd template %d", cmd_id);
383 WARN_ON(buf_len > WL1251_MAX_TEMPLATE_SIZE);
384 buf_len = min_t(size_t, buf_len, WL1251_MAX_TEMPLATE_SIZE);
385 cmd_len = ALIGN(sizeof(*cmd) + buf_len, 4);
387 cmd = kzalloc(cmd_len, GFP_KERNEL);
388 if (!cmd) {
389 ret = -ENOMEM;
390 goto out;
393 cmd->size = cpu_to_le16(buf_len);
395 if (buf)
396 memcpy(cmd->data, buf, buf_len);
398 ret = wl1251_cmd_send(wl, cmd_id, cmd, cmd_len);
399 if (ret < 0) {
400 wl1251_warning("cmd set_template failed: %d", ret);
401 goto out;
404 out:
405 kfree(cmd);
406 return ret;
409 int wl1251_cmd_scan(struct wl1251 *wl, u8 *ssid, size_t ssid_len,
410 struct ieee80211_channel *channels[],
411 unsigned int n_channels, unsigned int n_probes)
413 struct wl1251_cmd_scan *cmd;
414 int i, ret = 0;
416 wl1251_debug(DEBUG_CMD, "cmd scan");
418 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
419 if (!cmd)
420 return -ENOMEM;
422 cmd->params.rx_config_options = cpu_to_le32(CFG_RX_ALL_GOOD);
423 cmd->params.rx_filter_options = cpu_to_le32(CFG_RX_PRSP_EN |
424 CFG_RX_MGMT_EN |
425 CFG_RX_BCN_EN);
426 cmd->params.scan_options = 0;
427 cmd->params.num_channels = n_channels;
428 cmd->params.num_probe_requests = n_probes;
429 cmd->params.tx_rate = cpu_to_le16(1 << 1); /* 2 Mbps */
430 cmd->params.tid_trigger = 0;
432 for (i = 0; i < n_channels; i++) {
433 cmd->channels[i].min_duration =
434 cpu_to_le32(WL1251_SCAN_MIN_DURATION);
435 cmd->channels[i].max_duration =
436 cpu_to_le32(WL1251_SCAN_MAX_DURATION);
437 memset(&cmd->channels[i].bssid_lsb, 0xff, 4);
438 memset(&cmd->channels[i].bssid_msb, 0xff, 2);
439 cmd->channels[i].early_termination = 0;
440 cmd->channels[i].tx_power_att = 0;
441 cmd->channels[i].channel = channels[i]->hw_value;
444 cmd->params.ssid_len = ssid_len;
445 if (ssid)
446 memcpy(cmd->params.ssid, ssid, ssid_len);
448 ret = wl1251_cmd_send(wl, CMD_SCAN, cmd, sizeof(*cmd));
449 if (ret < 0) {
450 wl1251_error("cmd scan failed: %d", ret);
451 goto out;
454 wl1251_mem_read(wl, wl->cmd_box_addr, cmd, sizeof(*cmd));
456 if (cmd->header.status != CMD_STATUS_SUCCESS) {
457 wl1251_error("cmd scan status wasn't success: %d",
458 cmd->header.status);
459 ret = -EIO;
460 goto out;
463 out:
464 kfree(cmd);
465 return ret;
468 int wl1251_cmd_trigger_scan_to(struct wl1251 *wl, u32 timeout)
470 struct wl1251_cmd_trigger_scan_to *cmd;
471 int ret;
473 wl1251_debug(DEBUG_CMD, "cmd trigger scan to");
475 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
476 if (!cmd)
477 return -ENOMEM;
479 cmd->timeout = timeout;
481 ret = wl1251_cmd_send(wl, CMD_TRIGGER_SCAN_TO, cmd, sizeof(*cmd));
482 if (ret < 0) {
483 wl1251_error("cmd trigger scan to failed: %d", ret);
484 goto out;
487 out:
488 kfree(cmd);
489 return ret;