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