wl1251: create wl1251_join()
[pandora-kernel.git] / drivers / net / wireless / wl12xx / wl1251_cmd.c
1 #include "wl1251_cmd.h"
2
3 #include <linux/module.h>
4 #include <linux/crc7.h>
5
6 #include "wl1251.h"
7 #include "reg.h"
8 #include "wl1251_io.h"
9 #include "wl1251_ps.h"
10 #include "wl1251_acx.h"
11
12 /**
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  */
20 int 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
55 out:
56         return ret;
57 }
58
59 /**
60  * send test command to firmware
61  *
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
66  */
67 int 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                  * 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.
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  * 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  */
108 int 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         /* payload length, does not include any headers */
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         /* the interrogate command got in, we can read the answer */
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
134 out:
135         return ret;
136 }
137
138 /**
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  */
146 int 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         /* payload length, does not include any headers */
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
167 int 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         /* 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;
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
200 out:
201         kfree(vbm);
202         return 0;
203 }
204
205 int 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
249 out:
250         kfree(cmd);
251         return ret;
252 }
253
254 int 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         /* 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];
275
276         join->rx_config_options = wl->rx_config;
277         join->rx_filter_options = wl->rx_filter;
278
279         join->basic_rate_set = RATE_MASK_1MBPS | RATE_MASK_2MBPS |
280                 RATE_MASK_5_5MBPS | RATE_MASK_11MBPS;
281
282         join->beacon_interval = beacon_interval;
283         join->dtim_interval = dtim_interval;
284         join->bss_type = bss_type;
285         join->channel = channel;
286         join->ctrl = JOIN_CMD_CTRL_TX_FLUSH;
287
288         ret = wl1251_cmd_send(wl, CMD_START_JOIN, join, sizeof(*join));
289         if (ret < 0) {
290                 wl1251_error("failed to initiate cmd join");
291                 goto out;
292         }
293
294 out:
295         kfree(join);
296         return ret;
297 }
298
299 int wl1251_cmd_ps_mode(struct wl1251 *wl, u8 ps_mode)
300 {
301         struct wl1251_cmd_ps_params *ps_params = NULL;
302         int ret = 0;
303
304         /* FIXME: this should be in ps.c */
305         ret = wl1251_acx_wake_up_conditions(wl, WAKE_UP_EVENT_DTIM_BITMAP,
306                                             wl->listen_int);
307         if (ret < 0) {
308                 wl1251_error("couldn't set wake up conditions");
309                 goto out;
310         }
311
312         wl1251_debug(DEBUG_CMD, "cmd set ps mode");
313
314         ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
315         if (!ps_params) {
316                 ret = -ENOMEM;
317                 goto out;
318         }
319
320         ps_params->ps_mode = ps_mode;
321         ps_params->send_null_data = 1;
322         ps_params->retries = 5;
323         ps_params->hang_over_period = 128;
324         ps_params->null_data_rate = 1; /* 1 Mbps */
325
326         ret = wl1251_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
327                               sizeof(*ps_params));
328         if (ret < 0) {
329                 wl1251_error("cmd set_ps_mode failed");
330                 goto out;
331         }
332
333 out:
334         kfree(ps_params);
335         return ret;
336 }
337
338 int wl1251_cmd_read_memory(struct wl1251 *wl, u32 addr, void *answer,
339                            size_t len)
340 {
341         struct cmd_read_write_memory *cmd;
342         int ret = 0;
343
344         wl1251_debug(DEBUG_CMD, "cmd read memory");
345
346         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
347         if (!cmd) {
348                 ret = -ENOMEM;
349                 goto out;
350         }
351
352         WARN_ON(len > MAX_READ_SIZE);
353         len = min_t(size_t, len, MAX_READ_SIZE);
354
355         cmd->addr = addr;
356         cmd->size = len;
357
358         ret = wl1251_cmd_send(wl, CMD_READ_MEMORY, cmd, sizeof(*cmd));
359         if (ret < 0) {
360                 wl1251_error("read memory command failed: %d", ret);
361                 goto out;
362         }
363
364         /* the read command got in, we can now read the answer */
365         wl1251_mem_read(wl, wl->cmd_box_addr, cmd, sizeof(*cmd));
366
367         if (cmd->header.status != CMD_STATUS_SUCCESS)
368                 wl1251_error("error in read command result: %d",
369                              cmd->header.status);
370
371         memcpy(answer, cmd->value, len);
372
373 out:
374         kfree(cmd);
375         return ret;
376 }
377
378 int wl1251_cmd_template_set(struct wl1251 *wl, u16 cmd_id,
379                             void *buf, size_t buf_len)
380 {
381         struct wl1251_cmd_packet_template *cmd;
382         size_t cmd_len;
383         int ret = 0;
384
385         wl1251_debug(DEBUG_CMD, "cmd template %d", cmd_id);
386
387         WARN_ON(buf_len > WL1251_MAX_TEMPLATE_SIZE);
388         buf_len = min_t(size_t, buf_len, WL1251_MAX_TEMPLATE_SIZE);
389         cmd_len = ALIGN(sizeof(*cmd) + buf_len, 4);
390
391         cmd = kzalloc(cmd_len, GFP_KERNEL);
392         if (!cmd) {
393                 ret = -ENOMEM;
394                 goto out;
395         }
396
397         cmd->size = cpu_to_le16(buf_len);
398
399         if (buf)
400                 memcpy(cmd->data, buf, buf_len);
401
402         ret = wl1251_cmd_send(wl, cmd_id, cmd, cmd_len);
403         if (ret < 0) {
404                 wl1251_warning("cmd set_template failed: %d", ret);
405                 goto out;
406         }
407
408 out:
409         kfree(cmd);
410         return ret;
411 }