0320b478bb3f0dfde1fc6cfbee04859fe9b01a35
[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
6 #include "wl1251.h"
7 #include "wl1251_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 & WL1251_ACX_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                            WL1251_ACX_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 channel,
255                     u16 beacon_interval, u8 dtim_interval)
256 {
257         struct cmd_join *join;
258         int ret, i;
259         u8 *bssid;
260
261         join = kzalloc(sizeof(*join), GFP_KERNEL);
262         if (!join) {
263                 ret = -ENOMEM;
264                 goto out;
265         }
266
267         wl1251_debug(DEBUG_CMD, "cmd join%s ch %d %d/%d",
268                      bss_type == BSS_TYPE_IBSS ? " ibss" : "",
269                      channel, beacon_interval, dtim_interval);
270
271         /* Reverse order BSSID */
272         bssid = (u8 *) &join->bssid_lsb;
273         for (i = 0; i < ETH_ALEN; i++)
274                 bssid[i] = wl->bssid[ETH_ALEN - i - 1];
275
276         join->rx_config_options = wl->rx_config;
277         join->rx_filter_options = wl->rx_filter;
278
279         /*
280          * FIXME: disable temporarily all filters because after commit
281          * 9cef8737 "mac80211: fix managed mode BSSID handling" broke
282          * association. The filter logic needs to be implemented properly
283          * and once that is done, this hack can be removed.
284          */
285         join->rx_config_options = 0;
286         join->rx_filter_options = WL1251_DEFAULT_RX_FILTER;
287
288         join->basic_rate_set = RATE_MASK_1MBPS | RATE_MASK_2MBPS |
289                 RATE_MASK_5_5MBPS | RATE_MASK_11MBPS;
290
291         join->beacon_interval = beacon_interval;
292         join->dtim_interval = dtim_interval;
293         join->bss_type = bss_type;
294         join->channel = channel;
295         join->ctrl = JOIN_CMD_CTRL_TX_FLUSH;
296
297         ret = wl1251_cmd_send(wl, CMD_START_JOIN, join, sizeof(*join));
298         if (ret < 0) {
299                 wl1251_error("failed to initiate cmd join");
300                 goto out;
301         }
302
303 out:
304         kfree(join);
305         return ret;
306 }
307
308 int wl1251_cmd_ps_mode(struct wl1251 *wl, u8 ps_mode)
309 {
310         struct wl1251_cmd_ps_params *ps_params = NULL;
311         int ret = 0;
312
313         wl1251_debug(DEBUG_CMD, "cmd set ps mode");
314
315         ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
316         if (!ps_params) {
317                 ret = -ENOMEM;
318                 goto out;
319         }
320
321         ps_params->ps_mode = ps_mode;
322         ps_params->send_null_data = 1;
323         ps_params->retries = 5;
324         ps_params->hang_over_period = 128;
325         ps_params->null_data_rate = 1; /* 1 Mbps */
326
327         ret = wl1251_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
328                               sizeof(*ps_params));
329         if (ret < 0) {
330                 wl1251_error("cmd set_ps_mode failed");
331                 goto out;
332         }
333
334 out:
335         kfree(ps_params);
336         return ret;
337 }
338
339 int wl1251_cmd_read_memory(struct wl1251 *wl, u32 addr, void *answer,
340                            size_t len)
341 {
342         struct cmd_read_write_memory *cmd;
343         int ret = 0;
344
345         wl1251_debug(DEBUG_CMD, "cmd read memory");
346
347         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
348         if (!cmd) {
349                 ret = -ENOMEM;
350                 goto out;
351         }
352
353         WARN_ON(len > MAX_READ_SIZE);
354         len = min_t(size_t, len, MAX_READ_SIZE);
355
356         cmd->addr = addr;
357         cmd->size = len;
358
359         ret = wl1251_cmd_send(wl, CMD_READ_MEMORY, cmd, sizeof(*cmd));
360         if (ret < 0) {
361                 wl1251_error("read memory command failed: %d", ret);
362                 goto out;
363         }
364
365         /* the read command got in, we can now read the answer */
366         wl1251_mem_read(wl, wl->cmd_box_addr, cmd, sizeof(*cmd));
367
368         if (cmd->header.status != CMD_STATUS_SUCCESS)
369                 wl1251_error("error in read command result: %d",
370                              cmd->header.status);
371
372         memcpy(answer, cmd->value, len);
373
374 out:
375         kfree(cmd);
376         return ret;
377 }
378
379 int wl1251_cmd_template_set(struct wl1251 *wl, u16 cmd_id,
380                             void *buf, size_t buf_len)
381 {
382         struct wl1251_cmd_packet_template *cmd;
383         size_t cmd_len;
384         int ret = 0;
385
386         wl1251_debug(DEBUG_CMD, "cmd template %d", cmd_id);
387
388         WARN_ON(buf_len > WL1251_MAX_TEMPLATE_SIZE);
389         buf_len = min_t(size_t, buf_len, WL1251_MAX_TEMPLATE_SIZE);
390         cmd_len = ALIGN(sizeof(*cmd) + buf_len, 4);
391
392         cmd = kzalloc(cmd_len, GFP_KERNEL);
393         if (!cmd) {
394                 ret = -ENOMEM;
395                 goto out;
396         }
397
398         cmd->size = cpu_to_le16(buf_len);
399
400         if (buf)
401                 memcpy(cmd->data, buf, buf_len);
402
403         ret = wl1251_cmd_send(wl, cmd_id, cmd, cmd_len);
404         if (ret < 0) {
405                 wl1251_warning("cmd set_template failed: %d", ret);
406                 goto out;
407         }
408
409 out:
410         kfree(cmd);
411         return ret;
412 }
413
414 int wl1251_cmd_scan(struct wl1251 *wl, u8 *ssid, size_t ssid_len,
415                     struct ieee80211_channel *channels[],
416                     unsigned int n_channels, unsigned int n_probes)
417 {
418         struct wl1251_cmd_scan *cmd;
419         int i, ret = 0;
420
421         wl1251_debug(DEBUG_CMD, "cmd scan");
422
423         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
424         if (!cmd)
425                 return -ENOMEM;
426
427         cmd->params.rx_config_options = cpu_to_le32(CFG_RX_ALL_GOOD);
428         cmd->params.rx_filter_options = cpu_to_le32(CFG_RX_PRSP_EN |
429                                                     CFG_RX_MGMT_EN |
430                                                     CFG_RX_BCN_EN);
431         cmd->params.scan_options = 0;
432         cmd->params.num_channels = n_channels;
433         cmd->params.num_probe_requests = n_probes;
434         cmd->params.tx_rate = cpu_to_le16(1 << 1); /* 2 Mbps */
435         cmd->params.tid_trigger = 0;
436
437         for (i = 0; i < n_channels; i++) {
438                 cmd->channels[i].min_duration =
439                         cpu_to_le32(WL1251_SCAN_MIN_DURATION);
440                 cmd->channels[i].max_duration =
441                         cpu_to_le32(WL1251_SCAN_MAX_DURATION);
442                 memset(&cmd->channels[i].bssid_lsb, 0xff, 4);
443                 memset(&cmd->channels[i].bssid_msb, 0xff, 2);
444                 cmd->channels[i].early_termination = 0;
445                 cmd->channels[i].tx_power_att = 0;
446                 cmd->channels[i].channel = channels[i]->hw_value;
447         }
448
449         cmd->params.ssid_len = ssid_len;
450         if (ssid)
451                 memcpy(cmd->params.ssid, ssid, ssid_len);
452
453         ret = wl1251_cmd_send(wl, CMD_SCAN, cmd, sizeof(*cmd));
454         if (ret < 0) {
455                 wl1251_error("cmd scan failed: %d", ret);
456                 goto out;
457         }
458
459         wl1251_mem_read(wl, wl->cmd_box_addr, cmd, sizeof(*cmd));
460
461         if (cmd->header.status != CMD_STATUS_SUCCESS) {
462                 wl1251_error("cmd scan status wasn't success: %d",
463                              cmd->header.status);
464                 ret = -EIO;
465                 goto out;
466         }
467
468 out:
469         kfree(cmd);
470         return ret;
471 }
472
473 int wl1251_cmd_trigger_scan_to(struct wl1251 *wl, u32 timeout)
474 {
475         struct wl1251_cmd_trigger_scan_to *cmd;
476         int ret;
477
478         wl1251_debug(DEBUG_CMD, "cmd trigger scan to");
479
480         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
481         if (!cmd)
482                 return -ENOMEM;
483
484         cmd->timeout = timeout;
485
486         ret = wl1251_cmd_send(wl, CMD_SCAN, cmd, sizeof(*cmd));
487         if (ret < 0) {
488                 wl1251_error("cmd trigger scan to failed: %d", ret);
489                 goto out;
490         }
491
492 out:
493         kfree(cmd);
494         return ret;
495 }