wl1251: set ps rates like ti driver does
[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_disconnect(struct wl1251 *wl)
330 {
331         struct wl1251_cmd_disconnect *cmd;
332         int ret;
333
334         wl1251_debug(DEBUG_CMD, "cmd disconnect");
335
336         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
337         if (!cmd)
338                 return -ENOMEM;
339
340         cmd->rx_config_options = wl->rx_config;
341         cmd->rx_filter_options = 0;
342
343         ret = wl1251_cmd_send(wl, CMD_DISCONNECT, cmd, sizeof(*cmd));
344         if (ret < 0)
345                 wl1251_error("cmd disconnect failed: %d", ret);
346
347         kfree(cmd);
348         return ret;
349 }
350
351 int wl1251_cmd_ps_mode(struct wl1251 *wl, u8 ps_mode)
352 {
353         struct wl1251_cmd_ps_params *ps_params = NULL;
354         int ret = 0;
355
356         wl1251_debug(DEBUG_CMD, "cmd set ps mode");
357
358         ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
359         if (!ps_params) {
360                 ret = -ENOMEM;
361                 goto out;
362         }
363
364         ps_params->ps_mode = ps_mode;
365         ps_params->send_null_data = 1;
366         ps_params->retries = 5;
367         ps_params->hang_over_period = 128;
368         ps_params->null_data_rate = RATE_MASK_1MBPS | RATE_MASK_2MBPS |
369                 RATE_MASK_5_5MBPS | RATE_MASK_11MBPS;
370
371         ret = wl1251_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
372                               sizeof(*ps_params));
373         if (ret < 0) {
374                 wl1251_error("cmd set_ps_mode failed");
375                 goto out;
376         }
377
378 out:
379         kfree(ps_params);
380         return ret;
381 }
382
383 int wl1251_cmd_read_memory(struct wl1251 *wl, u32 addr, void *answer,
384                            size_t len)
385 {
386         struct cmd_read_write_memory *cmd;
387         int ret = 0;
388
389         wl1251_debug(DEBUG_CMD, "cmd read memory");
390
391         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
392         if (!cmd) {
393                 ret = -ENOMEM;
394                 goto out;
395         }
396
397         WARN_ON(len > MAX_READ_SIZE);
398         len = min_t(size_t, len, MAX_READ_SIZE);
399
400         cmd->addr = addr;
401         cmd->size = len;
402
403         ret = wl1251_cmd_send(wl, CMD_READ_MEMORY, cmd, sizeof(*cmd));
404         if (ret < 0) {
405                 wl1251_error("read memory command failed: %d", ret);
406                 goto out;
407         }
408
409         /* the read command got in, we can now read the answer */
410         wl1251_mem_read(wl, wl->cmd_box_addr, cmd, sizeof(*cmd));
411
412         if (cmd->header.status != CMD_STATUS_SUCCESS)
413                 wl1251_error("error in read command result: %d",
414                              cmd->header.status);
415
416         memcpy(answer, cmd->value, len);
417
418 out:
419         kfree(cmd);
420         return ret;
421 }
422
423 int wl1251_cmd_template_set(struct wl1251 *wl, u16 cmd_id,
424                             void *buf, size_t buf_len)
425 {
426         struct wl1251_cmd_packet_template *cmd;
427         size_t cmd_len;
428         int ret = 0;
429
430         wl1251_debug(DEBUG_CMD, "cmd template %d", cmd_id);
431
432         WARN_ON(buf_len > WL1251_MAX_TEMPLATE_SIZE);
433         buf_len = min_t(size_t, buf_len, WL1251_MAX_TEMPLATE_SIZE);
434         cmd_len = ALIGN(sizeof(*cmd) + buf_len, 4);
435
436         cmd = kzalloc(cmd_len, GFP_KERNEL);
437         if (!cmd) {
438                 ret = -ENOMEM;
439                 goto out;
440         }
441
442         cmd->size = cpu_to_le16(buf_len);
443
444         if (buf)
445                 memcpy(cmd->data, buf, buf_len);
446
447         ret = wl1251_cmd_send(wl, cmd_id, cmd, cmd_len);
448         if (ret < 0) {
449                 wl1251_warning("cmd set_template failed: %d", ret);
450                 goto out;
451         }
452
453 out:
454         kfree(cmd);
455         return ret;
456 }
457
458 int wl1251_cmd_scan(struct wl1251 *wl, u8 *ssid, size_t ssid_len,
459                     struct ieee80211_channel *channels[],
460                     unsigned int n_channels, unsigned int n_probes)
461 {
462         struct wl1251_cmd_scan *cmd;
463         int i, ret = 0;
464
465         wl1251_debug(DEBUG_CMD, "cmd scan channels %d", n_channels);
466
467         WARN_ON(n_channels > SCAN_MAX_NUM_OF_CHANNELS);
468
469         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
470         if (!cmd)
471                 return -ENOMEM;
472
473         cmd->params.rx_config_options = cpu_to_le32(CFG_RX_ALL_GOOD);
474         cmd->params.rx_filter_options = cpu_to_le32(CFG_RX_PRSP_EN |
475                                                     CFG_RX_MGMT_EN |
476                                                     CFG_RX_BCN_EN);
477         cmd->params.scan_options = 0;
478         /*
479          * Use high priority scan when not associated to prevent fw issue
480          * causing never-ending scans (sometimes 20+ minutes).
481          * Note: This bug may be caused by the fw's DTIM handling.
482          */
483         if (is_zero_ether_addr(wl->bssid))
484                 cmd->params.scan_options |= cpu_to_le16(WL1251_SCAN_OPT_PRIORITY_HIGH);
485         cmd->params.num_channels = n_channels;
486         cmd->params.num_probe_requests = n_probes;
487         cmd->params.tx_rate = cpu_to_le16(1 << 1); /* 2 Mbps */
488         cmd->params.tid_trigger = 0;
489
490         for (i = 0; i < n_channels; i++) {
491                 cmd->channels[i].min_duration =
492                         cpu_to_le32(WL1251_SCAN_MIN_DURATION);
493                 cmd->channels[i].max_duration =
494                         cpu_to_le32(WL1251_SCAN_MAX_DURATION);
495                 memset(&cmd->channels[i].bssid_lsb, 0xff, 4);
496                 memset(&cmd->channels[i].bssid_msb, 0xff, 2);
497                 cmd->channels[i].early_termination = 0;
498                 cmd->channels[i].tx_power_att = 0;
499                 cmd->channels[i].channel = channels[i]->hw_value;
500         }
501
502         cmd->params.ssid_len = ssid_len;
503         if (ssid)
504                 memcpy(cmd->params.ssid, ssid, ssid_len);
505
506         ret = wl1251_cmd_send(wl, CMD_SCAN, cmd, sizeof(*cmd));
507         if (ret < 0) {
508                 wl1251_error("cmd scan failed: %d", ret);
509                 goto out;
510         }
511
512         wl1251_mem_read(wl, wl->cmd_box_addr, cmd, sizeof(*cmd));
513
514         if (cmd->header.status != CMD_STATUS_SUCCESS) {
515                 wl1251_error("cmd scan status wasn't success: %d",
516                              cmd->header.status);
517                 ret = -EIO;
518                 goto out;
519         }
520
521 out:
522         kfree(cmd);
523         return ret;
524 }
525
526 int wl1251_cmd_trigger_scan_to(struct wl1251 *wl, u32 timeout)
527 {
528         struct wl1251_cmd_trigger_scan_to *cmd;
529         int ret;
530
531         wl1251_debug(DEBUG_CMD, "cmd trigger scan to");
532
533         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
534         if (!cmd)
535                 return -ENOMEM;
536
537         cmd->timeout = timeout;
538
539         ret = wl1251_cmd_send(wl, CMD_TRIGGER_SCAN_TO, cmd, sizeof(*cmd));
540         if (ret < 0) {
541                 wl1251_error("cmd trigger scan to failed: %d", ret);
542                 goto out;
543         }
544
545 out:
546         kfree(cmd);
547         return ret;
548 }