3 #include <linux/module.h>
4 #include <linux/slab.h>
5 #include <linux/crc7.h>
6 #include <linux/etherdevice.h>
15 * send command to firmware
19 * @buf: buffer containing the command, must work with dma
20 * @len: length of the buffer
22 int wl1251_cmd_send(struct wl1251 *wl, u16 id, void *buf, size_t len)
24 struct wl1251_cmd_header *cmd;
25 unsigned long timeout;
34 WARN_ON(len % 4 != 0);
36 wl1251_mem_write(wl, wl->cmd_box_addr, buf, len);
38 wl1251_reg_write32(wl, ACX_REG_INTERRUPT_TRIG, INTR_TRIG_CMD);
40 timeout = jiffies + msecs_to_jiffies(WL1251_COMMAND_TIMEOUT);
42 intr = wl1251_reg_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
43 while (!(intr & WL1251_ACX_INTR_CMD_COMPLETE)) {
44 if (time_after(jiffies, timeout)) {
45 wl1251_error("command complete timeout");
56 intr = wl1251_reg_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
59 wl1251_mem_read(wl, wl->cmd_box_addr, cmd, sizeof(*cmd));
61 if (cmd->status != CMD_STATUS_SUCCESS)
62 wl1251_error("command %d returned %d", id, cmd->status);
64 wl1251_reg_write32(wl, ACX_REG_INTERRUPT_ACK,
65 WL1251_ACX_INTR_CMD_COMPLETE);
72 * send test command to firmware
75 * @buf: buffer containing the command, with all headers, must work with dma
76 * @len: length of the buffer
77 * @answer: is answer needed
79 int wl1251_cmd_test(struct wl1251 *wl, void *buf, size_t buf_len, u8 answer)
83 wl1251_debug(DEBUG_CMD, "cmd test");
85 ret = wl1251_cmd_send(wl, CMD_TEST, buf, buf_len);
88 wl1251_warning("TEST command failed");
93 struct wl1251_command *cmd_answer;
96 * The test command got in, we can read the answer.
97 * The answer would be a wl1251_command, where the
98 * parameter array contains the actual answer.
100 wl1251_mem_read(wl, wl->cmd_box_addr, buf, buf_len);
104 if (cmd_answer->header.status != CMD_STATUS_SUCCESS)
105 wl1251_error("TEST command answer error: %d",
106 cmd_answer->header.status);
113 * read acx from firmware
117 * @buf: buffer for the response, including all headers, must work with dma
118 * @len: length of buf
120 int wl1251_cmd_interrogate(struct wl1251 *wl, u16 id, void *buf, size_t len)
122 struct acx_header *acx = buf;
125 wl1251_debug(DEBUG_CMD, "cmd interrogate");
129 /* payload length, does not include any headers */
130 acx->len = len - sizeof(*acx);
132 ret = wl1251_cmd_send(wl, CMD_INTERROGATE, acx, sizeof(*acx));
134 wl1251_error("INTERROGATE command failed");
138 /* the interrogate command got in, we can read the answer */
139 wl1251_mem_read(wl, wl->cmd_box_addr, buf, len);
142 if (acx->cmd.status != CMD_STATUS_SUCCESS)
143 wl1251_error("INTERROGATE command error: %d",
151 * write acx value to firmware
155 * @buf: buffer containing acx, including all headers, must work with dma
156 * @len: length of buf
158 int wl1251_cmd_configure(struct wl1251 *wl, u16 id, void *buf, size_t len)
160 struct acx_header *acx = buf;
163 wl1251_debug(DEBUG_CMD, "cmd configure");
167 /* payload length, does not include any headers */
168 acx->len = len - sizeof(*acx);
170 ret = wl1251_cmd_send(wl, CMD_CONFIGURE, acx, len);
172 wl1251_warning("CONFIGURE command NOK");
179 int wl1251_cmd_vbm(struct wl1251 *wl, u8 identity,
180 void *bitmap, u16 bitmap_len, u8 bitmap_control)
182 struct wl1251_cmd_vbm_update *vbm;
185 wl1251_debug(DEBUG_CMD, "cmd vbm");
187 vbm = kzalloc(sizeof(*vbm), GFP_KERNEL);
193 /* Count and period will be filled by the target */
194 vbm->tim.bitmap_ctrl = bitmap_control;
195 if (bitmap_len > PARTIAL_VBM_MAX) {
196 wl1251_warning("cmd vbm len is %d B, truncating to %d",
197 bitmap_len, PARTIAL_VBM_MAX);
198 bitmap_len = PARTIAL_VBM_MAX;
200 memcpy(vbm->tim.pvb_field, bitmap, bitmap_len);
201 vbm->tim.identity = identity;
202 vbm->tim.length = bitmap_len + 3;
204 vbm->len = cpu_to_le16(bitmap_len + 5);
206 ret = wl1251_cmd_send(wl, CMD_VBM, vbm, sizeof(*vbm));
208 wl1251_error("VBM command failed");
217 int wl1251_cmd_data_path_rx(struct wl1251 *wl, u8 channel, bool enable)
219 struct cmd_enabledisable_path *cmd;
223 wl1251_debug(DEBUG_CMD, "cmd data path");
225 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
231 cmd->channel = channel;
234 cmd_rx = CMD_ENABLE_RX;
236 cmd_rx = CMD_DISABLE_RX;
238 ret = wl1251_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd));
240 wl1251_error("rx %s cmd for channel %d failed",
241 enable ? "start" : "stop", channel);
245 wl1251_debug(DEBUG_BOOT, "rx %s cmd channel %d",
246 enable ? "start" : "stop", channel);
253 int wl1251_cmd_data_path_tx(struct wl1251 *wl, u8 channel, bool enable)
255 struct cmd_enabledisable_path *cmd;
259 wl1251_debug(DEBUG_CMD, "cmd data path");
261 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
265 cmd->channel = channel;
268 cmd_tx = CMD_ENABLE_TX;
270 cmd_tx = CMD_DISABLE_TX;
272 ret = wl1251_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd));
274 wl1251_error("tx %s cmd for channel %d failed",
275 enable ? "start" : "stop", channel);
277 wl1251_debug(DEBUG_BOOT, "tx %s cmd channel %d",
278 enable ? "start" : "stop", channel);
284 int wl1251_cmd_join(struct wl1251 *wl, u8 bss_type, u8 channel,
285 u16 beacon_interval, u8 dtim_interval)
287 struct cmd_join *join;
291 join = kzalloc(sizeof(*join), GFP_KERNEL);
297 wl1251_debug(DEBUG_CMD, "cmd join%s ch %d %d/%d",
298 bss_type == BSS_TYPE_IBSS ? " ibss" : "",
299 channel, beacon_interval, dtim_interval);
301 /* Reverse order BSSID */
302 bssid = (u8 *) &join->bssid_lsb;
303 for (i = 0; i < ETH_ALEN; i++)
304 bssid[i] = wl->bssid[ETH_ALEN - i - 1];
306 join->rx_config_options = wl->rx_config;
307 join->rx_filter_options = wl->rx_filter;
309 join->basic_rate_set = RATE_MASK_1MBPS | RATE_MASK_2MBPS |
310 RATE_MASK_5_5MBPS | RATE_MASK_11MBPS;
312 join->beacon_interval = beacon_interval;
313 join->dtim_interval = dtim_interval;
314 join->bss_type = bss_type;
315 join->channel = channel;
316 join->ctrl = JOIN_CMD_CTRL_TX_FLUSH;
318 ret = wl1251_cmd_send(wl, CMD_START_JOIN, join, sizeof(*join));
320 wl1251_error("failed to initiate cmd join");
329 int wl1251_cmd_disconnect(struct wl1251 *wl)
331 struct wl1251_cmd_disconnect *cmd;
334 wl1251_debug(DEBUG_CMD, "cmd disconnect");
336 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
340 cmd->rx_config_options = wl->rx_config;
341 cmd->rx_filter_options = 0;
343 ret = wl1251_cmd_send(wl, CMD_DISCONNECT, cmd, sizeof(*cmd));
345 wl1251_error("cmd disconnect failed: %d", ret);
351 int wl1251_cmd_ps_mode(struct wl1251 *wl, u8 ps_mode)
353 struct wl1251_cmd_ps_params *ps_params = NULL;
356 wl1251_debug(DEBUG_CMD, "cmd set ps mode");
358 ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
364 ps_params->ps_mode = ps_mode;
365 ps_params->send_null_data = 1;
366 ps_params->retries = 5;
367 ps_params->hang_over_period = 128;
368 ps_params->null_data_rate = 1; /* 1 Mbps */
370 ret = wl1251_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
373 wl1251_error("cmd set_ps_mode failed");
382 int wl1251_cmd_read_memory(struct wl1251 *wl, u32 addr, void *answer,
385 struct cmd_read_write_memory *cmd;
388 wl1251_debug(DEBUG_CMD, "cmd read memory");
390 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
396 WARN_ON(len > MAX_READ_SIZE);
397 len = min_t(size_t, len, MAX_READ_SIZE);
402 ret = wl1251_cmd_send(wl, CMD_READ_MEMORY, cmd, sizeof(*cmd));
404 wl1251_error("read memory command failed: %d", ret);
408 /* the read command got in, we can now read the answer */
409 wl1251_mem_read(wl, wl->cmd_box_addr, cmd, sizeof(*cmd));
411 if (cmd->header.status != CMD_STATUS_SUCCESS)
412 wl1251_error("error in read command result: %d",
415 memcpy(answer, cmd->value, len);
422 int wl1251_cmd_template_set(struct wl1251 *wl, u16 cmd_id,
423 void *buf, size_t buf_len)
425 struct wl1251_cmd_packet_template *cmd;
429 wl1251_debug(DEBUG_CMD, "cmd template %d", cmd_id);
431 WARN_ON(buf_len > WL1251_MAX_TEMPLATE_SIZE);
432 buf_len = min_t(size_t, buf_len, WL1251_MAX_TEMPLATE_SIZE);
433 cmd_len = ALIGN(sizeof(*cmd) + buf_len, 4);
435 cmd = kzalloc(cmd_len, GFP_KERNEL);
441 cmd->size = cpu_to_le16(buf_len);
444 memcpy(cmd->data, buf, buf_len);
446 ret = wl1251_cmd_send(wl, cmd_id, cmd, cmd_len);
448 wl1251_warning("cmd set_template failed: %d", ret);
457 int wl1251_cmd_scan(struct wl1251 *wl, u8 *ssid, size_t ssid_len,
458 struct ieee80211_channel *channels[],
459 unsigned int n_channels, unsigned int n_probes)
461 struct wl1251_cmd_scan *cmd;
464 wl1251_debug(DEBUG_CMD, "cmd scan channels %d", n_channels);
466 WARN_ON(n_channels > SCAN_MAX_NUM_OF_CHANNELS);
468 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
472 cmd->params.rx_config_options = cpu_to_le32(CFG_RX_ALL_GOOD);
473 cmd->params.rx_filter_options = cpu_to_le32(CFG_RX_PRSP_EN |
476 cmd->params.scan_options = 0;
478 * Use high priority scan when not associated to prevent fw issue
479 * causing never-ending scans (sometimes 20+ minutes).
480 * Note: This bug may be caused by the fw's DTIM handling.
482 if (is_zero_ether_addr(wl->bssid))
483 cmd->params.scan_options |= cpu_to_le16(WL1251_SCAN_OPT_PRIORITY_HIGH);
484 cmd->params.num_channels = n_channels;
485 cmd->params.num_probe_requests = n_probes;
486 cmd->params.tx_rate = cpu_to_le16(1 << 1); /* 2 Mbps */
487 cmd->params.tid_trigger = 0;
489 for (i = 0; i < n_channels; i++) {
490 cmd->channels[i].min_duration =
491 cpu_to_le32(WL1251_SCAN_MIN_DURATION);
492 cmd->channels[i].max_duration =
493 cpu_to_le32(WL1251_SCAN_MAX_DURATION);
494 memset(&cmd->channels[i].bssid_lsb, 0xff, 4);
495 memset(&cmd->channels[i].bssid_msb, 0xff, 2);
496 cmd->channels[i].early_termination = 0;
497 cmd->channels[i].tx_power_att = 0;
498 cmd->channels[i].channel = channels[i]->hw_value;
501 cmd->params.ssid_len = ssid_len;
503 memcpy(cmd->params.ssid, ssid, ssid_len);
505 ret = wl1251_cmd_send(wl, CMD_SCAN, cmd, sizeof(*cmd));
507 wl1251_error("cmd scan failed: %d", ret);
511 wl1251_mem_read(wl, wl->cmd_box_addr, cmd, sizeof(*cmd));
513 if (cmd->header.status != CMD_STATUS_SUCCESS) {
514 wl1251_error("cmd scan status wasn't success: %d",
525 int wl1251_cmd_trigger_scan_to(struct wl1251 *wl, u32 timeout)
527 struct wl1251_cmd_trigger_scan_to *cmd;
530 wl1251_debug(DEBUG_CMD, "cmd trigger scan to");
532 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
536 cmd->timeout = timeout;
538 ret = wl1251_cmd_send(wl, CMD_TRIGGER_SCAN_TO, cmd, sizeof(*cmd));
540 wl1251_error("cmd trigger scan to failed: %d", ret);