Merge branch 'stable-3.2' into pandora-3.2
[pandora-kernel.git] / drivers / net / wireless / wl1251 / cmd.c
1 #include "cmd.h"
2
3 #include <linux/module.h>
4 #include <linux/slab.h>
5 #include <linux/crc7.h>
6 #include <linux/etherdevice.h>
7
8 #include "wl1251.h"
9 #include "reg.h"
10 #include "io.h"
11 #include "ps.h"
12 #include "acx.h"
13
14 /**
15  * send command to firmware
16  *
17  * @wl: wl struct
18  * @id: command id
19  * @buf: buffer containing the command, must work with dma
20  * @len: length of the buffer
21  */
22 int wl1251_cmd_send(struct wl1251 *wl, u16 id, void *buf, size_t len)
23 {
24         struct wl1251_cmd_header *cmd;
25         unsigned long timeout;
26         u32 poll_count = 0;
27         u32 intr;
28         int ret = 0;
29
30         cmd = buf;
31         cmd->id = id;
32         cmd->status = 0;
33
34         WARN_ON(len % 4 != 0);
35
36         wl1251_mem_write(wl, wl->cmd_box_addr, buf, len);
37
38         wl1251_reg_write32(wl, ACX_REG_INTERRUPT_TRIG, INTR_TRIG_CMD);
39
40         timeout = jiffies + msecs_to_jiffies(WL1251_COMMAND_TIMEOUT);
41
42         intr = wl1251_reg_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
43         while (!(intr & WL1251_ACX_INTR_CMD_COMPLETE)) {
44                 if (time_after(jiffies, timeout)) {
45                         wl1251_error("command complete timeout");
46                         ret = -ETIMEDOUT;
47                         goto out;
48                 }
49
50                 poll_count++;
51                 if (poll_count < 30)
52                         udelay(1);
53                 else
54                         msleep(1);
55
56                 intr = wl1251_reg_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
57         }
58
59         wl1251_mem_read(wl, wl->cmd_box_addr, cmd, sizeof(*cmd));
60
61         if (cmd->status != CMD_STATUS_SUCCESS)
62                 wl1251_error("command %d returned %d", id, cmd->status);
63
64         wl1251_reg_write32(wl, ACX_REG_INTERRUPT_ACK,
65                            WL1251_ACX_INTR_CMD_COMPLETE);
66
67 out:
68         return ret;
69 }
70
71 /**
72  * send test command to firmware
73  *
74  * @wl: wl struct
75  * @buf: buffer containing the command, with all headers, must work with dma
76  * @len: length of the buffer
77  * @answer: is answer needed
78  */
79 int wl1251_cmd_test(struct wl1251 *wl, void *buf, size_t buf_len, u8 answer)
80 {
81         int ret;
82
83         wl1251_debug(DEBUG_CMD, "cmd test");
84
85         ret = wl1251_cmd_send(wl, CMD_TEST, buf, buf_len);
86
87         if (ret < 0) {
88                 wl1251_warning("TEST command failed");
89                 return ret;
90         }
91
92         if (answer) {
93                 struct wl1251_command *cmd_answer;
94
95                 /*
96                  * The test command got in, we can read the answer.
97                  * The answer would be a wl1251_command, where the
98                  * parameter array contains the actual answer.
99                  */
100                 wl1251_mem_read(wl, wl->cmd_box_addr, buf, buf_len);
101
102                 cmd_answer = buf;
103
104                 if (cmd_answer->header.status != CMD_STATUS_SUCCESS)
105                         wl1251_error("TEST command answer error: %d",
106                                      cmd_answer->header.status);
107         }
108
109         return 0;
110 }
111
112 /**
113  * read acx from firmware
114  *
115  * @wl: wl struct
116  * @id: acx id
117  * @buf: buffer for the response, including all headers, must work with dma
118  * @len: length of buf
119  */
120 int wl1251_cmd_interrogate(struct wl1251 *wl, u16 id, void *buf, size_t len)
121 {
122         struct acx_header *acx = buf;
123         int ret;
124
125         wl1251_debug(DEBUG_CMD, "cmd interrogate");
126
127         acx->id = id;
128
129         /* payload length, does not include any headers */
130         acx->len = len - sizeof(*acx);
131
132         ret = wl1251_cmd_send(wl, CMD_INTERROGATE, acx, sizeof(*acx));
133         if (ret < 0) {
134                 wl1251_error("INTERROGATE command failed");
135                 goto out;
136         }
137
138         /* the interrogate command got in, we can read the answer */
139         wl1251_mem_read(wl, wl->cmd_box_addr, buf, len);
140
141         acx = buf;
142         if (acx->cmd.status != CMD_STATUS_SUCCESS)
143                 wl1251_error("INTERROGATE command error: %d",
144                              acx->cmd.status);
145
146 out:
147         return ret;
148 }
149
150 /**
151  * write acx value to firmware
152  *
153  * @wl: wl struct
154  * @id: acx id
155  * @buf: buffer containing acx, including all headers, must work with dma
156  * @len: length of buf
157  */
158 int wl1251_cmd_configure(struct wl1251 *wl, u16 id, void *buf, size_t len)
159 {
160         struct acx_header *acx = buf;
161         int ret;
162
163         wl1251_debug(DEBUG_CMD, "cmd configure");
164
165         acx->id = id;
166
167         /* payload length, does not include any headers */
168         acx->len = len - sizeof(*acx);
169
170         ret = wl1251_cmd_send(wl, CMD_CONFIGURE, acx, len);
171         if (ret < 0) {
172                 wl1251_warning("CONFIGURE command NOK");
173                 return ret;
174         }
175
176         return 0;
177 }
178
179 int wl1251_cmd_vbm(struct wl1251 *wl, u8 identity,
180                    void *bitmap, u16 bitmap_len, u8 bitmap_control)
181 {
182         struct wl1251_cmd_vbm_update *vbm;
183         int ret;
184
185         wl1251_debug(DEBUG_CMD, "cmd vbm");
186
187         vbm = kzalloc(sizeof(*vbm), GFP_KERNEL);
188         if (!vbm) {
189                 ret = -ENOMEM;
190                 goto out;
191         }
192
193         /* Count and period will be filled by the target */
194         vbm->tim.bitmap_ctrl = bitmap_control;
195         if (bitmap_len > PARTIAL_VBM_MAX) {
196                 wl1251_warning("cmd vbm len is %d B, truncating to %d",
197                                bitmap_len, PARTIAL_VBM_MAX);
198                 bitmap_len = PARTIAL_VBM_MAX;
199         }
200         memcpy(vbm->tim.pvb_field, bitmap, bitmap_len);
201         vbm->tim.identity = identity;
202         vbm->tim.length = bitmap_len + 3;
203
204         vbm->len = cpu_to_le16(bitmap_len + 5);
205
206         ret = wl1251_cmd_send(wl, CMD_VBM, vbm, sizeof(*vbm));
207         if (ret < 0) {
208                 wl1251_error("VBM command failed");
209                 goto out;
210         }
211
212 out:
213         kfree(vbm);
214         return ret;
215 }
216
217 int wl1251_cmd_data_path_rx(struct wl1251 *wl, u8 channel, bool enable)
218 {
219         struct cmd_enabledisable_path *cmd;
220         int ret;
221         u16 cmd_rx;
222
223         wl1251_debug(DEBUG_CMD, "cmd data path");
224
225         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
226         if (!cmd) {
227                 ret = -ENOMEM;
228                 goto out;
229         }
230
231         cmd->channel = channel;
232
233         if (enable)
234                 cmd_rx = CMD_ENABLE_RX;
235         else
236                 cmd_rx = CMD_DISABLE_RX;
237
238         ret = wl1251_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd));
239         if (ret < 0) {
240                 wl1251_error("rx %s cmd for channel %d failed",
241                              enable ? "start" : "stop", channel);
242                 goto out;
243         }
244
245         wl1251_debug(DEBUG_BOOT, "rx %s cmd channel %d",
246                      enable ? "start" : "stop", channel);
247
248 out:
249         kfree(cmd);
250         return ret;
251 }
252
253 int wl1251_cmd_data_path_tx(struct wl1251 *wl, u8 channel, bool enable)
254 {
255         struct cmd_enabledisable_path *cmd;
256         int ret;
257         u16 cmd_tx;
258
259         wl1251_debug(DEBUG_CMD, "cmd data path");
260
261         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
262         if (!cmd)
263                 return -ENOMEM;
264
265         cmd->channel = channel;
266
267         if (enable)
268                 cmd_tx = CMD_ENABLE_TX;
269         else
270                 cmd_tx = CMD_DISABLE_TX;
271
272         ret = wl1251_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd));
273         if (ret < 0)
274                 wl1251_error("tx %s cmd for channel %d failed",
275                              enable ? "start" : "stop", channel);
276         else
277                 wl1251_debug(DEBUG_BOOT, "tx %s cmd channel %d",
278                              enable ? "start" : "stop", channel);
279
280         kfree(cmd);
281         return ret;
282 }
283
284 int wl1251_cmd_join(struct wl1251 *wl, u8 bss_type, u8 channel,
285                     u16 beacon_interval, u8 dtim_interval)
286 {
287         struct cmd_join *join;
288         int ret, i;
289         u8 *bssid;
290
291         join = kzalloc(sizeof(*join), GFP_KERNEL);
292         if (!join) {
293                 ret = -ENOMEM;
294                 goto out;
295         }
296
297         wl1251_debug(DEBUG_CMD, "cmd join%s ch %d %d/%d",
298                      bss_type == BSS_TYPE_IBSS ? " ibss" : "",
299                      channel, beacon_interval, dtim_interval);
300
301         /* Reverse order BSSID */
302         bssid = (u8 *) &join->bssid_lsb;
303         for (i = 0; i < ETH_ALEN; i++)
304                 bssid[i] = wl->bssid[ETH_ALEN - i - 1];
305
306         join->rx_config_options = wl->rx_config;
307         join->rx_filter_options = wl->rx_filter;
308
309         join->basic_rate_set = RATE_MASK_1MBPS | RATE_MASK_2MBPS |
310                 RATE_MASK_5_5MBPS | RATE_MASK_11MBPS;
311
312         join->beacon_interval = beacon_interval;
313         join->dtim_interval = dtim_interval;
314         join->bss_type = bss_type;
315         join->channel = channel;
316         join->ctrl = JOIN_CMD_CTRL_TX_FLUSH;
317
318         ret = wl1251_cmd_send(wl, CMD_START_JOIN, join, sizeof(*join));
319         if (ret < 0) {
320                 wl1251_error("failed to initiate cmd join");
321                 goto out;
322         }
323
324 out:
325         kfree(join);
326         return ret;
327 }
328
329 int wl1251_cmd_ps_mode(struct wl1251 *wl, u8 ps_mode)
330 {
331         struct wl1251_cmd_ps_params *ps_params = NULL;
332         int ret = 0;
333
334         wl1251_debug(DEBUG_CMD, "cmd set ps mode");
335
336         ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
337         if (!ps_params) {
338                 ret = -ENOMEM;
339                 goto out;
340         }
341
342         ps_params->ps_mode = ps_mode;
343         ps_params->send_null_data = 1;
344         ps_params->retries = 5;
345         ps_params->hang_over_period = 128;
346         ps_params->null_data_rate = 1; /* 1 Mbps */
347
348         ret = wl1251_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
349                               sizeof(*ps_params));
350         if (ret < 0) {
351                 wl1251_error("cmd set_ps_mode failed");
352                 goto out;
353         }
354
355 out:
356         kfree(ps_params);
357         return ret;
358 }
359
360 int wl1251_cmd_read_memory(struct wl1251 *wl, u32 addr, void *answer,
361                            size_t len)
362 {
363         struct cmd_read_write_memory *cmd;
364         int ret = 0;
365
366         wl1251_debug(DEBUG_CMD, "cmd read memory");
367
368         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
369         if (!cmd) {
370                 ret = -ENOMEM;
371                 goto out;
372         }
373
374         WARN_ON(len > MAX_READ_SIZE);
375         len = min_t(size_t, len, MAX_READ_SIZE);
376
377         cmd->addr = addr;
378         cmd->size = len;
379
380         ret = wl1251_cmd_send(wl, CMD_READ_MEMORY, cmd, sizeof(*cmd));
381         if (ret < 0) {
382                 wl1251_error("read memory command failed: %d", ret);
383                 goto out;
384         }
385
386         /* the read command got in, we can now read the answer */
387         wl1251_mem_read(wl, wl->cmd_box_addr, cmd, sizeof(*cmd));
388
389         if (cmd->header.status != CMD_STATUS_SUCCESS)
390                 wl1251_error("error in read command result: %d",
391                              cmd->header.status);
392
393         memcpy(answer, cmd->value, len);
394
395 out:
396         kfree(cmd);
397         return ret;
398 }
399
400 int wl1251_cmd_template_set(struct wl1251 *wl, u16 cmd_id,
401                             void *buf, size_t buf_len)
402 {
403         struct wl1251_cmd_packet_template *cmd;
404         size_t cmd_len;
405         int ret = 0;
406
407         wl1251_debug(DEBUG_CMD, "cmd template %d", cmd_id);
408
409         WARN_ON(buf_len > WL1251_MAX_TEMPLATE_SIZE);
410         buf_len = min_t(size_t, buf_len, WL1251_MAX_TEMPLATE_SIZE);
411         cmd_len = ALIGN(sizeof(*cmd) + buf_len, 4);
412
413         cmd = kzalloc(cmd_len, GFP_KERNEL);
414         if (!cmd) {
415                 ret = -ENOMEM;
416                 goto out;
417         }
418
419         cmd->size = cpu_to_le16(buf_len);
420
421         if (buf)
422                 memcpy(cmd->data, buf, buf_len);
423
424         ret = wl1251_cmd_send(wl, cmd_id, cmd, cmd_len);
425         if (ret < 0) {
426                 wl1251_warning("cmd set_template failed: %d", ret);
427                 goto out;
428         }
429
430 out:
431         kfree(cmd);
432         return ret;
433 }
434
435 int wl1251_cmd_scan(struct wl1251 *wl, u8 *ssid, size_t ssid_len,
436                     struct ieee80211_channel *channels[],
437                     unsigned int n_channels, unsigned int n_probes)
438 {
439         struct wl1251_cmd_scan *cmd;
440         int i, ret = 0;
441
442         wl1251_debug(DEBUG_CMD, "cmd scan channels %d", n_channels);
443
444         WARN_ON(n_channels > SCAN_MAX_NUM_OF_CHANNELS);
445
446         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
447         if (!cmd)
448                 return -ENOMEM;
449
450         cmd->params.rx_config_options = cpu_to_le32(CFG_RX_ALL_GOOD);
451         cmd->params.rx_filter_options = cpu_to_le32(CFG_RX_PRSP_EN |
452                                                     CFG_RX_MGMT_EN |
453                                                     CFG_RX_BCN_EN);
454         cmd->params.scan_options = 0;
455         /*
456          * Use high priority scan when not associated to prevent fw issue
457          * causing never-ending scans (sometimes 20+ minutes).
458          * Note: This bug may be caused by the fw's DTIM handling.
459          */
460         if (is_zero_ether_addr(wl->bssid))
461                 cmd->params.scan_options |= cpu_to_le16(WL1251_SCAN_OPT_PRIORITY_HIGH);
462         cmd->params.num_channels = n_channels;
463         cmd->params.num_probe_requests = n_probes;
464         cmd->params.tx_rate = cpu_to_le16(1 << 1); /* 2 Mbps */
465         cmd->params.tid_trigger = 0;
466
467         for (i = 0; i < n_channels; i++) {
468                 cmd->channels[i].min_duration =
469                         cpu_to_le32(WL1251_SCAN_MIN_DURATION);
470                 cmd->channels[i].max_duration =
471                         cpu_to_le32(WL1251_SCAN_MAX_DURATION);
472                 memset(&cmd->channels[i].bssid_lsb, 0xff, 4);
473                 memset(&cmd->channels[i].bssid_msb, 0xff, 2);
474                 cmd->channels[i].early_termination = 0;
475                 cmd->channels[i].tx_power_att = 0;
476                 cmd->channels[i].channel = channels[i]->hw_value;
477         }
478
479         cmd->params.ssid_len = ssid_len;
480         if (ssid)
481                 memcpy(cmd->params.ssid, ssid, ssid_len);
482
483         ret = wl1251_cmd_send(wl, CMD_SCAN, cmd, sizeof(*cmd));
484         if (ret < 0) {
485                 wl1251_error("cmd scan failed: %d", ret);
486                 goto out;
487         }
488
489         wl1251_mem_read(wl, wl->cmd_box_addr, cmd, sizeof(*cmd));
490
491         if (cmd->header.status != CMD_STATUS_SUCCESS) {
492                 wl1251_error("cmd scan status wasn't success: %d",
493                              cmd->header.status);
494                 ret = -EIO;
495                 goto out;
496         }
497
498 out:
499         kfree(cmd);
500         return ret;
501 }
502
503 int wl1251_cmd_trigger_scan_to(struct wl1251 *wl, u32 timeout)
504 {
505         struct wl1251_cmd_trigger_scan_to *cmd;
506         int ret;
507
508         wl1251_debug(DEBUG_CMD, "cmd trigger scan to");
509
510         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
511         if (!cmd)
512                 return -ENOMEM;
513
514         cmd->timeout = timeout;
515
516         ret = wl1251_cmd_send(wl, CMD_TRIGGER_SCAN_TO, cmd, sizeof(*cmd));
517         if (ret < 0) {
518                 wl1251_error("cmd trigger scan to failed: %d", ret);
519                 goto out;
520         }
521
522 out:
523         kfree(cmd);
524         return ret;
525 }