wl1251: cleanup scanning code
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / wireless / wl12xx / wl1251_cmd.c
blobfcbfbd7585f8d13c700a67b364c393d4d1112acb
1 #include "wl1251_cmd.h"
3 #include <linux/module.h>
4 #include <linux/crc7.h>
6 #include "wl1251.h"
7 #include "wl1251_reg.h"
8 #include "wl1251_io.h"
9 #include "wl1251_ps.h"
10 #include "wl1251_acx.h"
12 /**
13 * send command to firmware
15 * @wl: wl struct
16 * @id: command id
17 * @buf: buffer containing the command, must work with dma
18 * @len: length of the buffer
20 int wl1251_cmd_send(struct wl1251 *wl, u16 id, void *buf, size_t len)
22 struct wl1251_cmd_header *cmd;
23 unsigned long timeout;
24 u32 intr;
25 int ret = 0;
27 cmd = buf;
28 cmd->id = id;
29 cmd->status = 0;
31 WARN_ON(len % 4 != 0);
33 wl1251_mem_write(wl, wl->cmd_box_addr, buf, len);
35 wl1251_reg_write32(wl, ACX_REG_INTERRUPT_TRIG, INTR_TRIG_CMD);
37 timeout = jiffies + msecs_to_jiffies(WL1251_COMMAND_TIMEOUT);
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;
47 msleep(1);
49 intr = wl1251_reg_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
52 wl1251_reg_write32(wl, ACX_REG_INTERRUPT_ACK,
53 WL1251_ACX_INTR_CMD_COMPLETE);
55 out:
56 return ret;
59 /**
60 * send test command to firmware
62 * @wl: wl struct
63 * @buf: buffer containing the command, with all headers, must work with dma
64 * @len: length of the buffer
65 * @answer: is answer needed
67 int wl1251_cmd_test(struct wl1251 *wl, void *buf, size_t buf_len, u8 answer)
69 int ret;
71 wl1251_debug(DEBUG_CMD, "cmd test");
73 ret = wl1251_cmd_send(wl, CMD_TEST, buf, buf_len);
75 if (ret < 0) {
76 wl1251_warning("TEST command failed");
77 return ret;
80 if (answer) {
81 struct wl1251_command *cmd_answer;
84 * The test command got in, we can read the answer.
85 * The answer would be a wl1251_command, where the
86 * parameter array contains the actual answer.
88 wl1251_mem_read(wl, wl->cmd_box_addr, buf, buf_len);
90 cmd_answer = buf;
92 if (cmd_answer->header.status != CMD_STATUS_SUCCESS)
93 wl1251_error("TEST command answer error: %d",
94 cmd_answer->header.status);
97 return 0;
101 * read acx from firmware
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
108 int wl1251_cmd_interrogate(struct wl1251 *wl, u16 id, void *buf, size_t len)
110 struct acx_header *acx = buf;
111 int ret;
113 wl1251_debug(DEBUG_CMD, "cmd interrogate");
115 acx->id = id;
117 /* payload length, does not include any headers */
118 acx->len = len - sizeof(*acx);
120 ret = wl1251_cmd_send(wl, CMD_INTERROGATE, acx, sizeof(*acx));
121 if (ret < 0) {
122 wl1251_error("INTERROGATE command failed");
123 goto out;
126 /* the interrogate command got in, we can read the answer */
127 wl1251_mem_read(wl, wl->cmd_box_addr, buf, len);
129 acx = buf;
130 if (acx->cmd.status != CMD_STATUS_SUCCESS)
131 wl1251_error("INTERROGATE command error: %d",
132 acx->cmd.status);
134 out:
135 return ret;
139 * write acx value to firmware
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
146 int wl1251_cmd_configure(struct wl1251 *wl, u16 id, void *buf, size_t len)
148 struct acx_header *acx = buf;
149 int ret;
151 wl1251_debug(DEBUG_CMD, "cmd configure");
153 acx->id = id;
155 /* payload length, does not include any headers */
156 acx->len = len - sizeof(*acx);
158 ret = wl1251_cmd_send(wl, CMD_CONFIGURE, acx, len);
159 if (ret < 0) {
160 wl1251_warning("CONFIGURE command NOK");
161 return ret;
164 return 0;
167 int wl1251_cmd_vbm(struct wl1251 *wl, u8 identity,
168 void *bitmap, u16 bitmap_len, u8 bitmap_control)
170 struct wl1251_cmd_vbm_update *vbm;
171 int ret;
173 wl1251_debug(DEBUG_CMD, "cmd vbm");
175 vbm = kzalloc(sizeof(*vbm), GFP_KERNEL);
176 if (!vbm) {
177 ret = -ENOMEM;
178 goto out;
181 /* Count and period will be filled by the target */
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;
188 memcpy(vbm->tim.pvb_field, bitmap, bitmap_len);
189 vbm->tim.identity = identity;
190 vbm->tim.length = bitmap_len + 3;
192 vbm->len = cpu_to_le16(bitmap_len + 5);
194 ret = wl1251_cmd_send(wl, CMD_VBM, vbm, sizeof(*vbm));
195 if (ret < 0) {
196 wl1251_error("VBM command failed");
197 goto out;
200 out:
201 kfree(vbm);
202 return 0;
205 int wl1251_cmd_data_path(struct wl1251 *wl, u8 channel, bool enable)
207 struct cmd_enabledisable_path *cmd;
208 int ret;
209 u16 cmd_rx, cmd_tx;
211 wl1251_debug(DEBUG_CMD, "cmd data path");
213 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
214 if (!cmd) {
215 ret = -ENOMEM;
216 goto out;
219 cmd->channel = channel;
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;
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;
236 wl1251_debug(DEBUG_BOOT, "rx %s cmd channel %d",
237 enable ? "start" : "stop", channel);
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;
246 wl1251_debug(DEBUG_BOOT, "tx %s cmd channel %d",
247 enable ? "start" : "stop", channel);
249 out:
250 kfree(cmd);
251 return ret;
254 int wl1251_cmd_join(struct wl1251 *wl, u8 bss_type, u8 channel,
255 u16 beacon_interval, u8 dtim_interval)
257 struct cmd_join *join;
258 int ret, i;
259 u8 *bssid;
261 join = kzalloc(sizeof(*join), GFP_KERNEL);
262 if (!join) {
263 ret = -ENOMEM;
264 goto out;
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);
271 /* Reverse order BSSID */
272 bssid = (u8 *) &join->bssid_lsb;
273 for (i = 0; i < ETH_ALEN; i++)
274 bssid[i] = wl->bssid[ETH_ALEN - i - 1];
276 join->rx_config_options = wl->rx_config;
277 join->rx_filter_options = wl->rx_filter;
280 * FIXME: disable temporarily all filters because after commit
281 * 9cef8737 "mac80211: fix managed mode BSSID handling" broke
282 * association. The filter logic needs to be implemented properly
283 * and once that is done, this hack can be removed.
285 join->rx_config_options = 0;
286 join->rx_filter_options = WL1251_DEFAULT_RX_FILTER;
288 join->basic_rate_set = RATE_MASK_1MBPS | RATE_MASK_2MBPS |
289 RATE_MASK_5_5MBPS | RATE_MASK_11MBPS;
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;
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;
303 out:
304 kfree(join);
305 return ret;
308 int wl1251_cmd_ps_mode(struct wl1251 *wl, u8 ps_mode)
310 struct wl1251_cmd_ps_params *ps_params = NULL;
311 int ret = 0;
313 wl1251_debug(DEBUG_CMD, "cmd set ps mode");
315 ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
316 if (!ps_params) {
317 ret = -ENOMEM;
318 goto out;
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; /* 1 Mbps */
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;
334 out:
335 kfree(ps_params);
336 return ret;
339 int wl1251_cmd_read_memory(struct wl1251 *wl, u32 addr, void *answer,
340 size_t len)
342 struct cmd_read_write_memory *cmd;
343 int ret = 0;
345 wl1251_debug(DEBUG_CMD, "cmd read memory");
347 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
348 if (!cmd) {
349 ret = -ENOMEM;
350 goto out;
353 WARN_ON(len > MAX_READ_SIZE);
354 len = min_t(size_t, len, MAX_READ_SIZE);
356 cmd->addr = addr;
357 cmd->size = len;
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;
365 /* the read command got in, we can now read the answer */
366 wl1251_mem_read(wl, wl->cmd_box_addr, cmd, sizeof(*cmd));
368 if (cmd->header.status != CMD_STATUS_SUCCESS)
369 wl1251_error("error in read command result: %d",
370 cmd->header.status);
372 memcpy(answer, cmd->value, len);
374 out:
375 kfree(cmd);
376 return ret;
379 int wl1251_cmd_template_set(struct wl1251 *wl, u16 cmd_id,
380 void *buf, size_t buf_len)
382 struct wl1251_cmd_packet_template *cmd;
383 size_t cmd_len;
384 int ret = 0;
386 wl1251_debug(DEBUG_CMD, "cmd template %d", cmd_id);
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);
392 cmd = kzalloc(cmd_len, GFP_KERNEL);
393 if (!cmd) {
394 ret = -ENOMEM;
395 goto out;
398 cmd->size = cpu_to_le16(buf_len);
400 if (buf)
401 memcpy(cmd->data, buf, buf_len);
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;
409 out:
410 kfree(cmd);
411 return ret;
414 int wl1251_cmd_scan(struct wl1251 *wl, u8 *ssid, size_t ssid_len,
415 unsigned int n_channels, unsigned int n_probes)
417 struct wl1251_cmd_scan *cmd;
418 int i, ret = 0;
420 wl1251_debug(DEBUG_CMD, "cmd scan");
422 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
423 if (!cmd)
424 return -ENOMEM;
426 cmd->params.rx_config_options = cpu_to_le32(CFG_RX_ALL_GOOD);
427 cmd->params.rx_filter_options = cpu_to_le32(CFG_RX_PRSP_EN |
428 CFG_RX_MGMT_EN |
429 CFG_RX_BCN_EN);
430 cmd->params.scan_options = 0;
431 cmd->params.num_channels = n_channels;
432 cmd->params.num_probe_requests = n_probes;
433 cmd->params.tx_rate = cpu_to_le16(1 << 1); /* 2 Mbps */
434 cmd->params.tid_trigger = 0;
436 for (i = 0; i < n_channels; i++) {
437 cmd->channels[i].min_duration =
438 cpu_to_le32(WL1251_SCAN_MIN_DURATION);
439 cmd->channels[i].max_duration =
440 cpu_to_le32(WL1251_SCAN_MAX_DURATION);
441 memset(&cmd->channels[i].bssid_lsb, 0xff, 4);
442 memset(&cmd->channels[i].bssid_msb, 0xff, 2);
443 cmd->channels[i].early_termination = 0;
444 cmd->channels[i].tx_power_att = 0;
445 cmd->channels[i].channel = i + 1;
448 cmd->params.ssid_len = ssid_len;
449 if (ssid)
450 memcpy(cmd->params.ssid, ssid, ssid_len);
452 ret = wl1251_cmd_send(wl, CMD_SCAN, cmd, sizeof(*cmd));
453 if (ret < 0) {
454 wl1251_error("cmd scan failed: %d", ret);
455 goto out;
458 wl1251_mem_read(wl, wl->cmd_box_addr, cmd, sizeof(*cmd));
460 if (cmd->header.status != CMD_STATUS_SUCCESS) {
461 wl1251_error("cmd scan status wasn't success: %d",
462 cmd->header.status);
463 ret = -EIO;
464 goto out;
467 out:
468 kfree(cmd);
469 return ret;
472 int wl1251_cmd_trigger_scan_to(struct wl1251 *wl, u32 timeout)
474 struct wl1251_cmd_trigger_scan_to *cmd;
475 int ret;
477 wl1251_debug(DEBUG_CMD, "cmd trigger scan to");
479 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
480 if (!cmd)
481 return -ENOMEM;
483 cmd->timeout = timeout;
485 ret = wl1251_cmd_send(wl, CMD_SCAN, cmd, sizeof(*cmd));
486 if (ret < 0) {
487 wl1251_error("cmd trigger scan to failed: %d", ret);
488 goto out;
491 out:
492 kfree(cmd);
493 return ret;