3 #include <linux/module.h>
4 #include <linux/crc7.h>
5 #include <linux/spi/spi.h>
8 #include "wl12xx_80211.h"
15 * send command to firmware
19 * @buf: buffer containing the command, must work with dma
20 * @len: length of the buffer
22 int wl12xx_cmd_send(struct wl12xx *wl, u16 id, void *buf, size_t len)
24 struct wl12xx_cmd_header *cmd;
25 unsigned long timeout;
33 WARN_ON(len % 4 != 0);
35 wl12xx_spi_mem_write(wl, wl->cmd_box_addr, buf, len);
37 wl12xx_reg_write32(wl, ACX_REG_INTERRUPT_TRIG, INTR_TRIG_CMD);
39 timeout = jiffies + msecs_to_jiffies(WL12XX_COMMAND_TIMEOUT);
41 intr = wl12xx_reg_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
42 while (!(intr & wl->chip.intr_cmd_complete)) {
43 if (time_after(jiffies, timeout)) {
44 wl12xx_error("command complete timeout");
51 intr = wl12xx_reg_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
54 wl12xx_reg_write32(wl, ACX_REG_INTERRUPT_ACK,
55 wl->chip.intr_cmd_complete);
62 * send test command to firmware
65 * @buf: buffer containing the command, with all headers, must work with dma
66 * @len: length of the buffer
67 * @answer: is answer needed
69 int wl12xx_cmd_test(struct wl12xx *wl, void *buf, size_t buf_len, u8 answer)
73 wl12xx_debug(DEBUG_CMD, "cmd test");
75 ret = wl12xx_cmd_send(wl, CMD_TEST, buf, buf_len);
78 wl12xx_warning("TEST command failed");
83 struct wl12xx_command *cmd_answer;
86 * The test command got in, we can read the answer.
87 * The answer would be a wl12xx_command, where the
88 * parameter array contains the actual answer.
90 wl12xx_spi_mem_read(wl, wl->cmd_box_addr, buf, buf_len);
94 if (cmd_answer->header.status != CMD_STATUS_SUCCESS)
95 wl12xx_error("TEST command answer error: %d",
96 cmd_answer->header.status);
103 * read acx from firmware
107 * @buf: buffer for the response, including all headers, must work with dma
108 * @len: lenght of buf
110 int wl12xx_cmd_interrogate(struct wl12xx *wl, u16 id, void *buf, size_t len)
112 struct acx_header *acx = buf;
115 wl12xx_debug(DEBUG_CMD, "cmd interrogate");
119 /* payload length, does not include any headers */
120 acx->len = len - sizeof(*acx);
122 ret = wl12xx_cmd_send(wl, CMD_INTERROGATE, acx, sizeof(*acx));
124 wl12xx_error("INTERROGATE command failed");
128 /* the interrogate command got in, we can read the answer */
129 wl12xx_spi_mem_read(wl, wl->cmd_box_addr, buf, len);
132 if (acx->cmd.status != CMD_STATUS_SUCCESS)
133 wl12xx_error("INTERROGATE command error: %d",
141 * write acx value to firmware
145 * @buf: buffer containing acx, including all headers, must work with dma
146 * @len: length of buf
148 int wl12xx_cmd_configure(struct wl12xx *wl, u16 id, void *buf, size_t len)
150 struct acx_header *acx = buf;
153 wl12xx_debug(DEBUG_CMD, "cmd configure");
157 /* payload length, does not include any headers */
158 acx->len = len - sizeof(*acx);
160 ret = wl12xx_cmd_send(wl, CMD_CONFIGURE, acx, len);
162 wl12xx_warning("CONFIGURE command NOK");
169 int wl12xx_cmd_vbm(struct wl12xx *wl, u8 identity,
170 void *bitmap, u16 bitmap_len, u8 bitmap_control)
172 struct wl12xx_cmd_vbm_update *vbm;
175 wl12xx_debug(DEBUG_CMD, "cmd vbm");
177 vbm = kzalloc(sizeof(*vbm), GFP_KERNEL);
183 /* Count and period will be filled by the target */
184 vbm->tim.bitmap_ctrl = bitmap_control;
185 if (bitmap_len > PARTIAL_VBM_MAX) {
186 wl12xx_warning("cmd vbm len is %d B, truncating to %d",
187 bitmap_len, PARTIAL_VBM_MAX);
188 bitmap_len = PARTIAL_VBM_MAX;
190 memcpy(vbm->tim.pvb_field, bitmap, bitmap_len);
191 vbm->tim.identity = identity;
192 vbm->tim.length = bitmap_len + 3;
194 vbm->len = cpu_to_le16(bitmap_len + 5);
196 ret = wl12xx_cmd_send(wl, CMD_VBM, vbm, sizeof(*vbm));
198 wl12xx_error("VBM command failed");
207 int wl12xx_cmd_data_path(struct wl12xx *wl, u8 channel, bool enable)
209 struct cmd_enabledisable_path *cmd;
213 wl12xx_debug(DEBUG_CMD, "cmd data path");
215 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
221 cmd->channel = channel;
224 cmd_rx = CMD_ENABLE_RX;
225 cmd_tx = CMD_ENABLE_TX;
227 cmd_rx = CMD_DISABLE_RX;
228 cmd_tx = CMD_DISABLE_TX;
231 ret = wl12xx_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd));
233 wl12xx_error("rx %s cmd for channel %d failed",
234 enable ? "start" : "stop", channel);
238 wl12xx_debug(DEBUG_BOOT, "rx %s cmd channel %d",
239 enable ? "start" : "stop", channel);
241 ret = wl12xx_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd));
243 wl12xx_error("tx %s cmd for channel %d failed",
244 enable ? "start" : "stop", channel);
248 wl12xx_debug(DEBUG_BOOT, "tx %s cmd channel %d",
249 enable ? "start" : "stop", channel);
256 int wl1251_cmd_join(struct wl12xx *wl, u8 bss_type, u8 dtim_interval,
257 u16 beacon_interval, u8 wait)
259 unsigned long timeout;
260 struct cmd_join *join;
264 join = kzalloc(sizeof(*join), GFP_KERNEL);
270 /* FIXME: this should be in main.c */
271 ret = wl12xx_acx_frame_rates(wl, DEFAULT_HW_GEN_TX_RATE,
272 DEFAULT_HW_GEN_MODULATION_TYPE,
273 wl->tx_mgmt_frm_rate,
274 wl->tx_mgmt_frm_mod);
278 wl12xx_debug(DEBUG_CMD, "cmd join");
280 /* Reverse order BSSID */
281 bssid = (u8 *) &join->bssid_lsb;
282 for (i = 0; i < ETH_ALEN; i++)
283 bssid[i] = wl->bssid[ETH_ALEN - i - 1];
285 join->rx_config_options = wl->rx_config;
286 join->rx_filter_options = wl->rx_filter;
288 join->basic_rate_set = RATE_MASK_1MBPS | RATE_MASK_2MBPS |
289 RATE_MASK_5_5MBPS | RATE_MASK_11MBPS;
291 join->beacon_interval = beacon_interval;
292 join->dtim_interval = dtim_interval;
293 join->bss_type = bss_type;
294 join->channel = wl->channel;
295 join->ctrl = JOIN_CMD_CTRL_TX_FLUSH;
297 ret = wl12xx_cmd_send(wl, CMD_START_JOIN, join, sizeof(*join));
299 wl12xx_error("failed to initiate cmd join");
303 timeout = msecs_to_jiffies(JOIN_TIMEOUT);
306 * ugly hack: we should wait for JOIN_EVENT_COMPLETE_ID but to
307 * simplify locking we just sleep instead, for now
317 int wl12xx_cmd_ps_mode(struct wl12xx *wl, u8 ps_mode)
319 struct wl12xx_cmd_ps_params *ps_params = NULL;
322 /* FIXME: this should be in ps.c */
323 ret = wl12xx_acx_wake_up_conditions(wl, WAKE_UP_EVENT_DTIM_BITMAP,
326 wl12xx_error("couldn't set wake up conditions");
330 wl12xx_debug(DEBUG_CMD, "cmd set ps mode");
332 ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
338 ps_params->ps_mode = ps_mode;
339 ps_params->send_null_data = 1;
340 ps_params->retries = 5;
341 ps_params->hang_over_period = 128;
342 ps_params->null_data_rate = 1; /* 1 Mbps */
344 ret = wl12xx_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
347 wl12xx_error("cmd set_ps_mode failed");
356 int wl12xx_cmd_read_memory(struct wl12xx *wl, u32 addr, void *answer,
359 struct cmd_read_write_memory *cmd;
362 wl12xx_debug(DEBUG_CMD, "cmd read memory");
364 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
370 WARN_ON(len > MAX_READ_SIZE);
371 len = min_t(size_t, len, MAX_READ_SIZE);
376 ret = wl12xx_cmd_send(wl, CMD_READ_MEMORY, cmd, sizeof(*cmd));
378 wl12xx_error("read memory command failed: %d", ret);
382 /* the read command got in, we can now read the answer */
383 wl12xx_spi_mem_read(wl, wl->cmd_box_addr, cmd, sizeof(*cmd));
385 if (cmd->header.status != CMD_STATUS_SUCCESS)
386 wl12xx_error("error in read command result: %d",
389 memcpy(answer, cmd->value, len);
396 int wl12xx_cmd_template_set(struct wl12xx *wl, u16 cmd_id,
397 void *buf, size_t buf_len)
399 struct wl12xx_cmd_packet_template *cmd;
403 wl12xx_debug(DEBUG_CMD, "cmd template %d", cmd_id);
405 WARN_ON(buf_len > WL12XX_MAX_TEMPLATE_SIZE);
406 buf_len = min_t(size_t, buf_len, WL12XX_MAX_TEMPLATE_SIZE);
407 cmd_len = ALIGN(sizeof(*cmd) + buf_len, 4);
409 cmd = kzalloc(cmd_len, GFP_KERNEL);
415 cmd->size = cpu_to_le16(buf_len);
418 memcpy(cmd->data, buf, buf_len);
420 ret = wl12xx_cmd_send(wl, cmd_id, cmd, cmd_len);
422 wl12xx_warning("cmd set_template failed: %d", ret);