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