Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next-2.6
[sfrench/cifs-2.6.git] / drivers / net / wireless / wl12xx / cmd.c
1 #include "cmd.h"
2
3 #include <linux/module.h>
4 #include <linux/crc7.h>
5 #include <linux/spi/spi.h>
6
7 #include "wl12xx.h"
8 #include "wl12xx_80211.h"
9 #include "reg.h"
10 #include "spi.h"
11 #include "ps.h"
12
13 int wl12xx_cmd_send(struct wl12xx *wl, u16 type, void *buf, size_t buf_len)
14 {
15         struct wl12xx_command cmd;
16         unsigned long timeout;
17         size_t cmd_len;
18         u32 intr;
19         int ret = 0;
20
21         memset(&cmd, 0, sizeof(cmd));
22         cmd.id = type;
23         cmd.status = 0;
24         memcpy(cmd.parameters, buf, buf_len);
25         cmd_len = ALIGN(buf_len, 4) + CMDMBOX_HEADER_LEN;
26
27         wl12xx_ps_elp_wakeup(wl);
28
29         wl12xx_spi_mem_write(wl, wl->cmd_box_addr, &cmd, cmd_len);
30
31         wl12xx_reg_write32(wl, ACX_REG_INTERRUPT_TRIG, INTR_TRIG_CMD);
32
33         timeout = jiffies + msecs_to_jiffies(WL12XX_COMMAND_TIMEOUT);
34
35         intr = wl12xx_reg_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
36         while (!(intr & wl->chip.intr_cmd_complete)) {
37                 if (time_after(jiffies, timeout)) {
38                         wl12xx_error("command complete timeout");
39                         ret = -ETIMEDOUT;
40                         goto out;
41                 }
42
43                 msleep(1);
44
45                 intr = wl12xx_reg_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
46         }
47
48         wl12xx_reg_write32(wl, ACX_REG_INTERRUPT_ACK,
49                            wl->chip.intr_cmd_complete);
50
51 out:
52         wl12xx_ps_elp_sleep(wl);
53
54         return ret;
55 }
56
57 int wl12xx_cmd_test(struct wl12xx *wl, void *buf, size_t buf_len, u8 answer)
58 {
59         int ret;
60
61         wl12xx_debug(DEBUG_CMD, "cmd test");
62
63         ret = wl12xx_cmd_send(wl, CMD_TEST, buf, buf_len);
64         if (ret < 0) {
65                 wl12xx_warning("TEST command failed");
66                 return ret;
67         }
68
69         if (answer) {
70                 struct wl12xx_command *cmd_answer;
71
72                 /*
73                  * The test command got in, we can read the answer.
74                  * The answer would be a wl12xx_command, where the
75                  * parameter array contains the actual answer.
76                  */
77
78                 wl12xx_ps_elp_wakeup(wl);
79
80                 wl12xx_spi_mem_read(wl, wl->cmd_box_addr, buf, buf_len);
81
82                 wl12xx_ps_elp_sleep(wl);
83
84                 cmd_answer = buf;
85                 if (cmd_answer->status != CMD_STATUS_SUCCESS)
86                         wl12xx_error("TEST command answer error: %d",
87                                      cmd_answer->status);
88         }
89
90         return 0;
91 }
92
93
94 int wl12xx_cmd_interrogate(struct wl12xx *wl, u16 ie_id, u16 ie_len,
95                            void *answer)
96 {
97         struct wl12xx_command *cmd;
98         struct acx_header header;
99         int ret;
100
101         wl12xx_debug(DEBUG_CMD, "cmd interrogate");
102
103         header.id = ie_id;
104         header.len = ie_len - sizeof(header);
105
106         ret = wl12xx_cmd_send(wl, CMD_INTERROGATE, &header, sizeof(header));
107         if (ret < 0) {
108                 wl12xx_error("INTERROGATE command failed");
109                 return ret;
110         }
111
112         wl12xx_ps_elp_wakeup(wl);
113
114         /* the interrogate command got in, we can read the answer */
115         wl12xx_spi_mem_read(wl, wl->cmd_box_addr, answer,
116                             CMDMBOX_HEADER_LEN + ie_len);
117
118         wl12xx_ps_elp_sleep(wl);
119
120         cmd = answer;
121         if (cmd->status != CMD_STATUS_SUCCESS)
122                 wl12xx_error("INTERROGATE command error: %d",
123                              cmd->status);
124
125         return 0;
126
127 }
128
129 int wl12xx_cmd_configure(struct wl12xx *wl, void *ie, int ie_len)
130 {
131         int ret;
132
133         wl12xx_debug(DEBUG_CMD, "cmd configure");
134
135         ret = wl12xx_cmd_send(wl, CMD_CONFIGURE, ie,
136                               ie_len);
137         if (ret < 0) {
138                 wl12xx_warning("CONFIGURE command NOK");
139                 return ret;
140         }
141
142         return 0;
143
144 }
145
146 int wl12xx_cmd_vbm(struct wl12xx *wl, u8 identity,
147                    void *bitmap, u16 bitmap_len, u8 bitmap_control)
148 {
149         struct vbm_update_request vbm;
150         int ret;
151
152         wl12xx_debug(DEBUG_CMD, "cmd vbm");
153
154         /* Count and period will be filled by the target */
155         vbm.tim.bitmap_ctrl = bitmap_control;
156         if (bitmap_len > PARTIAL_VBM_MAX) {
157                 wl12xx_warning("cmd vbm len is %d B, truncating to %d",
158                                bitmap_len, PARTIAL_VBM_MAX);
159                 bitmap_len = PARTIAL_VBM_MAX;
160         }
161         memcpy(vbm.tim.pvb_field, bitmap, bitmap_len);
162         vbm.tim.identity = identity;
163         vbm.tim.length = bitmap_len + 3;
164
165         vbm.len = cpu_to_le16(bitmap_len + 5);
166
167         ret = wl12xx_cmd_send(wl, CMD_VBM, &vbm, sizeof(vbm));
168         if (ret < 0) {
169                 wl12xx_error("VBM command failed");
170                 return ret;
171         }
172
173         return 0;
174 }
175
176 int wl12xx_cmd_data_path(struct wl12xx *wl, u8 channel, u8 enable)
177 {
178         int ret;
179         u16 cmd_rx, cmd_tx;
180
181         wl12xx_debug(DEBUG_CMD, "cmd data path");
182
183         if (enable) {
184                 cmd_rx = CMD_ENABLE_RX;
185                 cmd_tx = CMD_ENABLE_TX;
186         } else {
187                 cmd_rx = CMD_DISABLE_RX;
188                 cmd_tx = CMD_DISABLE_TX;
189         }
190
191         ret = wl12xx_cmd_send(wl, cmd_rx, &channel, sizeof(channel));
192         if (ret < 0) {
193                 wl12xx_error("rx %s cmd for channel %d failed",
194                              enable ? "start" : "stop", channel);
195                 return ret;
196         }
197
198         wl12xx_debug(DEBUG_BOOT, "rx %s cmd channel %d",
199                      enable ? "start" : "stop", channel);
200
201         ret = wl12xx_cmd_send(wl, cmd_tx, &channel, sizeof(channel));
202         if (ret < 0) {
203                 wl12xx_error("tx %s cmd for channel %d failed",
204                              enable ? "start" : "stop", channel);
205                 return ret;
206         }
207
208         wl12xx_debug(DEBUG_BOOT, "tx %s cmd channel %d",
209                      enable ? "start" : "stop", channel);
210
211         return 0;
212 }
213
214 int wl12xx_cmd_join(struct wl12xx *wl, u8 bss_type, u8 dtim_interval,
215                     u16 beacon_interval, u8 wait)
216 {
217         unsigned long timeout;
218         struct cmd_join join = {};
219         int ret, i;
220         u8 *bssid;
221
222         /* FIXME: this should be in main.c */
223         ret = wl12xx_acx_frame_rates(wl, DEFAULT_HW_GEN_TX_RATE,
224                                      DEFAULT_HW_GEN_MODULATION_TYPE,
225                                      wl->tx_mgmt_frm_rate,
226                                      wl->tx_mgmt_frm_mod);
227         if (ret < 0)
228                 return ret;
229
230         wl12xx_debug(DEBUG_CMD, "cmd join");
231
232         /* Reverse order BSSID */
233         bssid = (u8 *)&join.bssid_lsb;
234         for (i = 0; i < ETH_ALEN; i++)
235                 bssid[i] = wl->bssid[ETH_ALEN - i - 1];
236
237         join.rx_config_options = wl->rx_config;
238         join.rx_filter_options = wl->rx_filter;
239
240         join.basic_rate_set = RATE_MASK_1MBPS | RATE_MASK_2MBPS |
241                 RATE_MASK_5_5MBPS | RATE_MASK_11MBPS;
242
243         join.beacon_interval = beacon_interval;
244         join.dtim_interval = dtim_interval;
245         join.bss_type = bss_type;
246         join.channel = wl->channel;
247         join.ctrl = JOIN_CMD_CTRL_TX_FLUSH;
248
249         ret = wl12xx_cmd_send(wl, CMD_START_JOIN, &join, sizeof(join));
250         if (ret < 0) {
251                 wl12xx_error("failed to initiate cmd join");
252                 return ret;
253         }
254
255         timeout = msecs_to_jiffies(JOIN_TIMEOUT);
256
257         /*
258          * ugly hack: we should wait for JOIN_EVENT_COMPLETE_ID but to
259          * simplify locking we just sleep instead, for now
260          */
261         if (wait)
262                 msleep(10);
263
264         return 0;
265 }
266
267 int wl12xx_cmd_ps_mode(struct wl12xx *wl, u8 ps_mode)
268 {
269         int ret;
270         struct acx_ps_params ps_params;
271
272         /* FIXME: this should be in ps.c */
273         ret = wl12xx_acx_wake_up_conditions(wl, wl->listen_int);
274         if (ret < 0) {
275                 wl12xx_error("Couldnt set wake up conditions");
276                 return ret;
277         }
278
279         wl12xx_debug(DEBUG_CMD, "cmd set ps mode");
280
281         ps_params.ps_mode = ps_mode;
282         ps_params.send_null_data = 1;
283         ps_params.retries = 5;
284         ps_params.hang_over_period = 128;
285         ps_params.null_data_rate = 1; /* 1 Mbps */
286
287         ret = wl12xx_cmd_send(wl, CMD_SET_PS_MODE, &ps_params,
288                               sizeof(ps_params));
289         if (ret < 0) {
290                 wl12xx_error("cmd set_ps_mode failed");
291                 return ret;
292         }
293
294         return 0;
295 }
296
297 int wl12xx_cmd_read_memory(struct wl12xx *wl, u32 addr, u32 len, void *answer)
298 {
299         struct cmd_read_write_memory mem_cmd, *mem_answer;
300         struct wl12xx_command cmd;
301         int ret;
302
303         wl12xx_debug(DEBUG_CMD, "cmd read memory");
304
305         memset(&mem_cmd, 0, sizeof(mem_cmd));
306         mem_cmd.addr = addr;
307         mem_cmd.size = len;
308
309         ret = wl12xx_cmd_send(wl, CMD_READ_MEMORY, &mem_cmd, sizeof(mem_cmd));
310         if (ret < 0) {
311                 wl12xx_error("read memory command failed: %d", ret);
312                 return ret;
313         }
314
315         /* the read command got in, we can now read the answer */
316         wl12xx_spi_mem_read(wl, wl->cmd_box_addr, &cmd,
317                             CMDMBOX_HEADER_LEN + sizeof(mem_cmd));
318
319         if (cmd.status != CMD_STATUS_SUCCESS)
320                 wl12xx_error("error in read command result: %d", cmd.status);
321
322         mem_answer = (struct cmd_read_write_memory *) cmd.parameters;
323         memcpy(answer, mem_answer->value, len);
324
325         return 0;
326 }
327
328 int wl12xx_cmd_template_set(struct wl12xx *wl, u16 cmd_id,
329                             void *buf, size_t buf_len)
330 {
331         struct wl12xx_cmd_packet_template template;
332         int ret;
333
334         wl12xx_debug(DEBUG_CMD, "cmd template %d", cmd_id);
335
336         memset(&template, 0, sizeof(template));
337
338         WARN_ON(buf_len > WL12XX_MAX_TEMPLATE_SIZE);
339         buf_len = min_t(size_t, buf_len, WL12XX_MAX_TEMPLATE_SIZE);
340         template.size = cpu_to_le16(buf_len);
341
342         if (buf)
343                 memcpy(template.template, buf, buf_len);
344
345         ret = wl12xx_cmd_send(wl, cmd_id, &template,
346                               sizeof(template.size) + buf_len);
347         if (ret < 0) {
348                 wl12xx_warning("cmd set_template failed: %d", ret);
349                 return ret;
350         }
351
352         return 0;
353 }