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