dfbf6811976d2f4c5207eb39dde50a44d4109773
[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 & wl->chip.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                            wl->chip.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 dtim_interval,
255                     u16 beacon_interval, u8 wait)
256 {
257         unsigned long timeout;
258         struct cmd_join *join;
259         int ret, i;
260         u8 *bssid;
261
262         join = kzalloc(sizeof(*join), GFP_KERNEL);
263         if (!join) {
264                 ret = -ENOMEM;
265                 goto out;
266         }
267
268         /* FIXME: this should be in main.c */
269         ret = wl1251_acx_frame_rates(wl, DEFAULT_HW_GEN_TX_RATE,
270                                      DEFAULT_HW_GEN_MODULATION_TYPE,
271                                      wl->tx_mgmt_frm_rate,
272                                      wl->tx_mgmt_frm_mod);
273         if (ret < 0)
274                 goto out;
275
276         wl1251_debug(DEBUG_CMD, "cmd join");
277
278         /* Reverse order BSSID */
279         bssid = (u8 *) &join->bssid_lsb;
280         for (i = 0; i < ETH_ALEN; i++)
281                 bssid[i] = wl->bssid[ETH_ALEN - i - 1];
282
283         join->rx_config_options = wl->rx_config;
284         join->rx_filter_options = wl->rx_filter;
285
286         join->basic_rate_set = RATE_MASK_1MBPS | RATE_MASK_2MBPS |
287                 RATE_MASK_5_5MBPS | RATE_MASK_11MBPS;
288
289         join->beacon_interval = beacon_interval;
290         join->dtim_interval = dtim_interval;
291         join->bss_type = bss_type;
292         join->channel = wl->channel;
293         join->ctrl = JOIN_CMD_CTRL_TX_FLUSH;
294
295         ret = wl1251_cmd_send(wl, CMD_START_JOIN, join, sizeof(*join));
296         if (ret < 0) {
297                 wl1251_error("failed to initiate cmd join");
298                 goto out;
299         }
300
301         timeout = msecs_to_jiffies(JOIN_TIMEOUT);
302
303         /*
304          * ugly hack: we should wait for JOIN_EVENT_COMPLETE_ID but to
305          * simplify locking we just sleep instead, for now
306          */
307         if (wait)
308                 msleep(10);
309
310 out:
311         kfree(join);
312         return ret;
313 }
314
315 int wl1251_cmd_ps_mode(struct wl1251 *wl, u8 ps_mode)
316 {
317         struct wl1251_cmd_ps_params *ps_params = NULL;
318         int ret = 0;
319
320         /* FIXME: this should be in ps.c */
321         ret = wl1251_acx_wake_up_conditions(wl, WAKE_UP_EVENT_DTIM_BITMAP,
322                                             wl->listen_int);
323         if (ret < 0) {
324                 wl1251_error("couldn't set wake up conditions");
325                 goto out;
326         }
327
328         wl1251_debug(DEBUG_CMD, "cmd set ps mode");
329
330         ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
331         if (!ps_params) {
332                 ret = -ENOMEM;
333                 goto out;
334         }
335
336         ps_params->ps_mode = ps_mode;
337         ps_params->send_null_data = 1;
338         ps_params->retries = 5;
339         ps_params->hang_over_period = 128;
340         ps_params->null_data_rate = 1; /* 1 Mbps */
341
342         ret = wl1251_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
343                               sizeof(*ps_params));
344         if (ret < 0) {
345                 wl1251_error("cmd set_ps_mode failed");
346                 goto out;
347         }
348
349 out:
350         kfree(ps_params);
351         return ret;
352 }
353
354 int wl1251_cmd_read_memory(struct wl1251 *wl, u32 addr, void *answer,
355                            size_t len)
356 {
357         struct cmd_read_write_memory *cmd;
358         int ret = 0;
359
360         wl1251_debug(DEBUG_CMD, "cmd read memory");
361
362         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
363         if (!cmd) {
364                 ret = -ENOMEM;
365                 goto out;
366         }
367
368         WARN_ON(len > MAX_READ_SIZE);
369         len = min_t(size_t, len, MAX_READ_SIZE);
370
371         cmd->addr = addr;
372         cmd->size = len;
373
374         ret = wl1251_cmd_send(wl, CMD_READ_MEMORY, cmd, sizeof(*cmd));
375         if (ret < 0) {
376                 wl1251_error("read memory command failed: %d", ret);
377                 goto out;
378         }
379
380         /* the read command got in, we can now read the answer */
381         wl1251_mem_read(wl, wl->cmd_box_addr, cmd, sizeof(*cmd));
382
383         if (cmd->header.status != CMD_STATUS_SUCCESS)
384                 wl1251_error("error in read command result: %d",
385                              cmd->header.status);
386
387         memcpy(answer, cmd->value, len);
388
389 out:
390         kfree(cmd);
391         return ret;
392 }
393
394 int wl1251_cmd_template_set(struct wl1251 *wl, u16 cmd_id,
395                             void *buf, size_t buf_len)
396 {
397         struct wl1251_cmd_packet_template *cmd;
398         size_t cmd_len;
399         int ret = 0;
400
401         wl1251_debug(DEBUG_CMD, "cmd template %d", cmd_id);
402
403         WARN_ON(buf_len > WL1251_MAX_TEMPLATE_SIZE);
404         buf_len = min_t(size_t, buf_len, WL1251_MAX_TEMPLATE_SIZE);
405         cmd_len = ALIGN(sizeof(*cmd) + buf_len, 4);
406
407         cmd = kzalloc(cmd_len, GFP_KERNEL);
408         if (!cmd) {
409                 ret = -ENOMEM;
410                 goto out;
411         }
412
413         cmd->size = cpu_to_le16(buf_len);
414
415         if (buf)
416                 memcpy(cmd->data, buf, buf_len);
417
418         ret = wl1251_cmd_send(wl, cmd_id, cmd, cmd_len);
419         if (ret < 0) {
420                 wl1251_warning("cmd set_template failed: %d", ret);
421                 goto out;
422         }
423
424 out:
425         kfree(cmd);
426         return ret;
427 }