3 #include <linux/module.h>
4 #include <linux/slab.h>
5 #include <linux/crc7.h>
14 * send command to firmware
18 * @buf: buffer containing the command, must work with dma
19 * @len: length of the buffer
21 int wl1251_cmd_send(struct wl1251 *wl, u16 id, void *buf, size_t len)
23 struct wl1251_cmd_header *cmd;
24 unsigned long timeout;
33 WARN_ON(len % 4 != 0);
35 wl1251_mem_write(wl, wl->cmd_box_addr, buf, len);
37 wl1251_reg_write32(wl, ACX_REG_INTERRUPT_TRIG, INTR_TRIG_CMD);
39 timeout = jiffies + msecs_to_jiffies(WL1251_COMMAND_TIMEOUT);
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");
55 intr = wl1251_reg_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
58 wl1251_mem_read(wl, wl->cmd_box_addr, cmd, sizeof(*cmd));
60 if (cmd->status != CMD_STATUS_SUCCESS)
61 wl1251_error("command %d returned %d", id, cmd->status);
63 wl1251_reg_write32(wl, ACX_REG_INTERRUPT_ACK,
64 WL1251_ACX_INTR_CMD_COMPLETE);
71 * send test command to firmware
74 * @buf: buffer containing the command, with all headers, must work with dma
75 * @len: length of the buffer
76 * @answer: is answer needed
78 int wl1251_cmd_test(struct wl1251 *wl, void *buf, size_t buf_len, u8 answer)
82 wl1251_debug(DEBUG_CMD, "cmd test");
84 ret = wl1251_cmd_send(wl, CMD_TEST, buf, buf_len);
87 wl1251_warning("TEST command failed");
92 struct wl1251_command *cmd_answer;
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.
99 wl1251_mem_read(wl, wl->cmd_box_addr, buf, buf_len);
103 if (cmd_answer->header.status != CMD_STATUS_SUCCESS)
104 wl1251_error("TEST command answer error: %d",
105 cmd_answer->header.status);
112 * read acx from firmware
116 * @buf: buffer for the response, including all headers, must work with dma
117 * @len: length of buf
119 int wl1251_cmd_interrogate(struct wl1251 *wl, u16 id, void *buf, size_t len)
121 struct acx_header *acx = buf;
124 wl1251_debug(DEBUG_CMD, "cmd interrogate");
128 /* payload length, does not include any headers */
129 acx->len = len - sizeof(*acx);
131 ret = wl1251_cmd_send(wl, CMD_INTERROGATE, acx, sizeof(*acx));
133 wl1251_error("INTERROGATE command failed");
137 /* the interrogate command got in, we can read the answer */
138 wl1251_mem_read(wl, wl->cmd_box_addr, buf, len);
141 if (acx->cmd.status != CMD_STATUS_SUCCESS)
142 wl1251_error("INTERROGATE command error: %d",
150 * write acx value to firmware
154 * @buf: buffer containing acx, including all headers, must work with dma
155 * @len: length of buf
157 int wl1251_cmd_configure(struct wl1251 *wl, u16 id, void *buf, size_t len)
159 struct acx_header *acx = buf;
162 wl1251_debug(DEBUG_CMD, "cmd configure");
166 /* payload length, does not include any headers */
167 acx->len = len - sizeof(*acx);
169 ret = wl1251_cmd_send(wl, CMD_CONFIGURE, acx, len);
171 wl1251_warning("CONFIGURE command NOK");
178 int wl1251_cmd_vbm(struct wl1251 *wl, u8 identity,
179 void *bitmap, u16 bitmap_len, u8 bitmap_control)
181 struct wl1251_cmd_vbm_update *vbm;
184 wl1251_debug(DEBUG_CMD, "cmd vbm");
186 vbm = kzalloc(sizeof(*vbm), GFP_KERNEL);
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;
199 memcpy(vbm->tim.pvb_field, bitmap, bitmap_len);
200 vbm->tim.identity = identity;
201 vbm->tim.length = bitmap_len + 3;
203 vbm->len = cpu_to_le16(bitmap_len + 5);
205 ret = wl1251_cmd_send(wl, CMD_VBM, vbm, sizeof(*vbm));
207 wl1251_error("VBM command failed");
216 int wl1251_cmd_data_path_rx(struct wl1251 *wl, u8 channel, bool enable)
218 struct cmd_enabledisable_path *cmd;
222 wl1251_debug(DEBUG_CMD, "cmd data path");
224 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
230 cmd->channel = channel;
233 cmd_rx = CMD_ENABLE_RX;
235 cmd_rx = CMD_DISABLE_RX;
237 ret = wl1251_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd));
239 wl1251_error("rx %s cmd for channel %d failed",
240 enable ? "start" : "stop", channel);
244 wl1251_debug(DEBUG_BOOT, "rx %s cmd channel %d",
245 enable ? "start" : "stop", channel);
252 int wl1251_cmd_data_path_tx(struct wl1251 *wl, u8 channel, bool enable)
254 struct cmd_enabledisable_path *cmd;
258 wl1251_debug(DEBUG_CMD, "cmd data path");
260 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
264 cmd->channel = channel;
267 cmd_tx = CMD_ENABLE_TX;
269 cmd_tx = CMD_DISABLE_TX;
271 ret = wl1251_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd));
273 wl1251_error("tx %s cmd for channel %d failed",
274 enable ? "start" : "stop", channel);
276 wl1251_debug(DEBUG_BOOT, "tx %s cmd channel %d",
277 enable ? "start" : "stop", channel);
283 int wl1251_cmd_join(struct wl1251 *wl, u8 bss_type, u8 channel,
284 u16 beacon_interval, u8 dtim_interval)
286 struct cmd_join *join;
290 join = kzalloc(sizeof(*join), GFP_KERNEL);
296 wl1251_debug(DEBUG_CMD, "cmd join%s ch %d %d/%d",
297 bss_type == BSS_TYPE_IBSS ? " ibss" : "",
298 channel, beacon_interval, dtim_interval);
300 /* Reverse order BSSID */
301 bssid = (u8 *) &join->bssid_lsb;
302 for (i = 0; i < ETH_ALEN; i++)
303 bssid[i] = wl->bssid[ETH_ALEN - i - 1];
305 join->rx_config_options = wl->rx_config;
306 join->rx_filter_options = wl->rx_filter;
308 join->basic_rate_set = RATE_MASK_1MBPS | RATE_MASK_2MBPS |
309 RATE_MASK_5_5MBPS | RATE_MASK_11MBPS;
311 join->beacon_interval = beacon_interval;
312 join->dtim_interval = dtim_interval;
313 join->bss_type = bss_type;
314 join->channel = channel;
315 join->ctrl = JOIN_CMD_CTRL_TX_FLUSH;
317 ret = wl1251_cmd_send(wl, CMD_START_JOIN, join, sizeof(*join));
319 wl1251_error("failed to initiate cmd join");
328 int wl1251_cmd_ps_mode(struct wl1251 *wl, u8 ps_mode)
330 struct wl1251_cmd_ps_params *ps_params = NULL;
333 wl1251_debug(DEBUG_CMD, "cmd set ps mode");
335 ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
341 ps_params->ps_mode = ps_mode;
342 ps_params->send_null_data = 1;
343 ps_params->retries = 5;
344 ps_params->hang_over_period = 128;
345 ps_params->null_data_rate = 1; /* 1 Mbps */
347 ret = wl1251_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
350 wl1251_error("cmd set_ps_mode failed");
359 int wl1251_cmd_read_memory(struct wl1251 *wl, u32 addr, void *answer,
362 struct cmd_read_write_memory *cmd;
365 wl1251_debug(DEBUG_CMD, "cmd read memory");
367 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
373 WARN_ON(len > MAX_READ_SIZE);
374 len = min_t(size_t, len, MAX_READ_SIZE);
379 ret = wl1251_cmd_send(wl, CMD_READ_MEMORY, cmd, sizeof(*cmd));
381 wl1251_error("read memory command failed: %d", ret);
385 /* the read command got in, we can now read the answer */
386 wl1251_mem_read(wl, wl->cmd_box_addr, cmd, sizeof(*cmd));
388 if (cmd->header.status != CMD_STATUS_SUCCESS)
389 wl1251_error("error in read command result: %d",
392 memcpy(answer, cmd->value, len);
399 int wl1251_cmd_template_set(struct wl1251 *wl, u16 cmd_id,
400 void *buf, size_t buf_len)
402 struct wl1251_cmd_packet_template *cmd;
406 wl1251_debug(DEBUG_CMD, "cmd template %d", cmd_id);
408 WARN_ON(buf_len > WL1251_MAX_TEMPLATE_SIZE);
409 buf_len = min_t(size_t, buf_len, WL1251_MAX_TEMPLATE_SIZE);
410 cmd_len = ALIGN(sizeof(*cmd) + buf_len, 4);
412 cmd = kzalloc(cmd_len, GFP_KERNEL);
418 cmd->size = cpu_to_le16(buf_len);
421 memcpy(cmd->data, buf, buf_len);
423 ret = wl1251_cmd_send(wl, cmd_id, cmd, cmd_len);
425 wl1251_warning("cmd set_template failed: %d", ret);
434 int wl1251_cmd_scan(struct wl1251 *wl, u8 *ssid, size_t ssid_len,
435 struct ieee80211_channel *channels[],
436 unsigned int n_channels, unsigned int n_probes)
438 struct wl1251_cmd_scan *cmd;
441 wl1251_debug(DEBUG_CMD, "cmd scan");
443 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
447 cmd->params.rx_config_options = cpu_to_le32(CFG_RX_ALL_GOOD);
448 cmd->params.rx_filter_options = cpu_to_le32(CFG_RX_PRSP_EN |
451 cmd->params.scan_options = 0;
452 cmd->params.num_channels = n_channels;
453 cmd->params.num_probe_requests = n_probes;
454 cmd->params.tx_rate = cpu_to_le16(1 << 1); /* 2 Mbps */
455 cmd->params.tid_trigger = 0;
457 for (i = 0; i < n_channels; i++) {
458 cmd->channels[i].min_duration =
459 cpu_to_le32(WL1251_SCAN_MIN_DURATION);
460 cmd->channels[i].max_duration =
461 cpu_to_le32(WL1251_SCAN_MAX_DURATION);
462 memset(&cmd->channels[i].bssid_lsb, 0xff, 4);
463 memset(&cmd->channels[i].bssid_msb, 0xff, 2);
464 cmd->channels[i].early_termination = 0;
465 cmd->channels[i].tx_power_att = 0;
466 cmd->channels[i].channel = channels[i]->hw_value;
469 cmd->params.ssid_len = ssid_len;
471 memcpy(cmd->params.ssid, ssid, ssid_len);
473 ret = wl1251_cmd_send(wl, CMD_SCAN, cmd, sizeof(*cmd));
475 wl1251_error("cmd scan failed: %d", ret);
479 wl1251_mem_read(wl, wl->cmd_box_addr, cmd, sizeof(*cmd));
481 if (cmd->header.status != CMD_STATUS_SUCCESS) {
482 wl1251_error("cmd scan status wasn't success: %d",
493 int wl1251_cmd_trigger_scan_to(struct wl1251 *wl, u32 timeout)
495 struct wl1251_cmd_trigger_scan_to *cmd;
498 wl1251_debug(DEBUG_CMD, "cmd trigger scan to");
500 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
504 cmd->timeout = timeout;
506 ret = wl1251_cmd_send(wl, CMD_TRIGGER_SCAN_TO, cmd, sizeof(*cmd));
508 wl1251_error("cmd trigger scan to failed: %d", ret);