RT-AC56 3.0.0.4.374.37 core
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / net / wireless / wl12xx / wl1271_cmd.c
blob4f5e7408008c710cd7d52446d6e6d44a10a184a8
1 /*
2 * This file is part of wl1271
4 * Copyright (C) 2009-2010 Nokia Corporation
6 * Contact: Luciano Coelho <luciano.coelho@nokia.com>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA
24 #include <linux/module.h>
25 #include <linux/platform_device.h>
26 #include <linux/crc7.h>
27 #include <linux/spi/spi.h>
28 #include <linux/etherdevice.h>
29 #include <linux/ieee80211.h>
30 #include <linux/slab.h>
32 #include "wl1271.h"
33 #include "wl1271_reg.h"
34 #include "wl1271_io.h"
35 #include "wl1271_acx.h"
36 #include "wl12xx_80211.h"
37 #include "wl1271_cmd.h"
38 #include "wl1271_event.h"
40 #define WL1271_CMD_FAST_POLL_COUNT 50
43 * send command to firmware
45 * @wl: wl struct
46 * @id: command id
47 * @buf: buffer containing the command, must work with dma
48 * @len: length of the buffer
50 int wl1271_cmd_send(struct wl1271 *wl, u16 id, void *buf, size_t len,
51 size_t res_len)
53 struct wl1271_cmd_header *cmd;
54 unsigned long timeout;
55 u32 intr;
56 int ret = 0;
57 u16 status;
58 u16 poll_count = 0;
60 cmd = buf;
61 cmd->id = cpu_to_le16(id);
62 cmd->status = 0;
64 WARN_ON(len % 4 != 0);
66 wl1271_write(wl, wl->cmd_box_addr, buf, len, false);
68 wl1271_write32(wl, ACX_REG_INTERRUPT_TRIG, INTR_TRIG_CMD);
70 timeout = jiffies + msecs_to_jiffies(WL1271_COMMAND_TIMEOUT);
72 intr = wl1271_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
73 while (!(intr & WL1271_ACX_INTR_CMD_COMPLETE)) {
74 if (time_after(jiffies, timeout)) {
75 wl1271_error("command complete timeout");
76 ret = -ETIMEDOUT;
77 goto out;
80 poll_count++;
81 if (poll_count < WL1271_CMD_FAST_POLL_COUNT)
82 udelay(10);
83 else
84 msleep(1);
86 intr = wl1271_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
89 /* read back the status code of the command */
90 if (res_len == 0)
91 res_len = sizeof(struct wl1271_cmd_header);
92 wl1271_read(wl, wl->cmd_box_addr, cmd, res_len, false);
94 status = le16_to_cpu(cmd->status);
95 if (status != CMD_STATUS_SUCCESS) {
96 wl1271_error("command execute failure %d", status);
97 ret = -EIO;
100 wl1271_write32(wl, ACX_REG_INTERRUPT_ACK,
101 WL1271_ACX_INTR_CMD_COMPLETE);
103 out:
104 return ret;
107 int wl1271_cmd_general_parms(struct wl1271 *wl)
109 struct wl1271_general_parms_cmd *gen_parms;
110 int ret;
112 if (!wl->nvs)
113 return -ENODEV;
115 gen_parms = kzalloc(sizeof(*gen_parms), GFP_KERNEL);
116 if (!gen_parms)
117 return -ENOMEM;
119 gen_parms->test.id = TEST_CMD_INI_FILE_GENERAL_PARAM;
121 memcpy(&gen_parms->general_params, &wl->nvs->general_params,
122 sizeof(struct wl1271_ini_general_params));
124 ret = wl1271_cmd_test(wl, gen_parms, sizeof(*gen_parms), 0);
125 if (ret < 0)
126 wl1271_warning("CMD_INI_FILE_GENERAL_PARAM failed");
128 kfree(gen_parms);
129 return ret;
132 int wl1271_cmd_radio_parms(struct wl1271 *wl)
134 struct wl1271_radio_parms_cmd *radio_parms;
135 struct wl1271_ini_general_params *gp = &wl->nvs->general_params;
136 int ret;
138 if (!wl->nvs)
139 return -ENODEV;
141 radio_parms = kzalloc(sizeof(*radio_parms), GFP_KERNEL);
142 if (!radio_parms)
143 return -ENOMEM;
145 radio_parms->test.id = TEST_CMD_INI_FILE_RADIO_PARAM;
147 /* 2.4GHz parameters */
148 memcpy(&radio_parms->static_params_2, &wl->nvs->stat_radio_params_2,
149 sizeof(struct wl1271_ini_band_params_2));
150 memcpy(&radio_parms->dyn_params_2,
151 &wl->nvs->dyn_radio_params_2[gp->tx_bip_fem_manufacturer].params,
152 sizeof(struct wl1271_ini_fem_params_2));
154 /* 5GHz parameters */
155 memcpy(&radio_parms->static_params_5,
156 &wl->nvs->stat_radio_params_5,
157 sizeof(struct wl1271_ini_band_params_5));
158 memcpy(&radio_parms->dyn_params_5,
159 &wl->nvs->dyn_radio_params_5[gp->tx_bip_fem_manufacturer].params,
160 sizeof(struct wl1271_ini_fem_params_5));
162 wl1271_dump(DEBUG_CMD, "TEST_CMD_INI_FILE_RADIO_PARAM: ",
163 radio_parms, sizeof(*radio_parms));
165 ret = wl1271_cmd_test(wl, radio_parms, sizeof(*radio_parms), 0);
166 if (ret < 0)
167 wl1271_warning("CMD_INI_FILE_RADIO_PARAM failed");
169 kfree(radio_parms);
170 return ret;
174 * Poll the mailbox event field until any of the bits in the mask is set or a
175 * timeout occurs (WL1271_EVENT_TIMEOUT in msecs)
177 static int wl1271_cmd_wait_for_event(struct wl1271 *wl, u32 mask)
179 u32 events_vector, event;
180 unsigned long timeout;
182 timeout = jiffies + msecs_to_jiffies(WL1271_EVENT_TIMEOUT);
184 do {
185 if (time_after(jiffies, timeout))
186 return -ETIMEDOUT;
188 msleep(1);
190 /* read from both event fields */
191 wl1271_read(wl, wl->mbox_ptr[0], &events_vector,
192 sizeof(events_vector), false);
193 event = events_vector & mask;
194 wl1271_read(wl, wl->mbox_ptr[1], &events_vector,
195 sizeof(events_vector), false);
196 event |= events_vector & mask;
197 } while (!event);
199 return 0;
202 int wl1271_cmd_join(struct wl1271 *wl, u8 bss_type)
204 struct wl1271_cmd_join *join;
205 int ret, i;
206 u8 *bssid;
208 join = kzalloc(sizeof(*join), GFP_KERNEL);
209 if (!join) {
210 ret = -ENOMEM;
211 goto out;
214 wl1271_debug(DEBUG_CMD, "cmd join");
216 /* Reverse order BSSID */
217 bssid = (u8 *) &join->bssid_lsb;
218 for (i = 0; i < ETH_ALEN; i++)
219 bssid[i] = wl->bssid[ETH_ALEN - i - 1];
221 join->rx_config_options = cpu_to_le32(wl->rx_config);
222 join->rx_filter_options = cpu_to_le32(wl->rx_filter);
223 join->bss_type = bss_type;
224 join->basic_rate_set = cpu_to_le32(wl->basic_rate_set);
226 if (wl->band == IEEE80211_BAND_5GHZ)
227 join->bss_type |= WL1271_JOIN_CMD_BSS_TYPE_5GHZ;
229 join->beacon_interval = cpu_to_le16(wl->beacon_int);
230 join->dtim_interval = WL1271_DEFAULT_DTIM_PERIOD;
232 join->channel = wl->channel;
233 join->ssid_len = wl->ssid_len;
234 memcpy(join->ssid, wl->ssid, wl->ssid_len);
236 join->ctrl |= wl->session_counter << WL1271_JOIN_CMD_TX_SESSION_OFFSET;
238 /* reset TX security counters */
239 wl->tx_security_last_seq = 0;
240 wl->tx_security_seq = 0;
242 ret = wl1271_cmd_send(wl, CMD_START_JOIN, join, sizeof(*join), 0);
243 if (ret < 0) {
244 wl1271_error("failed to initiate cmd join");
245 goto out_free;
248 ret = wl1271_cmd_wait_for_event(wl, JOIN_EVENT_COMPLETE_ID);
249 if (ret < 0)
250 wl1271_error("cmd join event completion error");
252 out_free:
253 kfree(join);
255 out:
256 return ret;
260 * send test command to firmware
262 * @wl: wl struct
263 * @buf: buffer containing the command, with all headers, must work with dma
264 * @len: length of the buffer
265 * @answer: is answer needed
267 int wl1271_cmd_test(struct wl1271 *wl, void *buf, size_t buf_len, u8 answer)
269 int ret;
270 size_t res_len = 0;
272 wl1271_debug(DEBUG_CMD, "cmd test");
274 if (answer)
275 res_len = buf_len;
277 ret = wl1271_cmd_send(wl, CMD_TEST, buf, buf_len, res_len);
279 if (ret < 0) {
280 wl1271_warning("TEST command failed");
281 return ret;
284 return ret;
288 * read acx from firmware
290 * @wl: wl struct
291 * @id: acx id
292 * @buf: buffer for the response, including all headers, must work with dma
293 * @len: lenght of buf
295 int wl1271_cmd_interrogate(struct wl1271 *wl, u16 id, void *buf, size_t len)
297 struct acx_header *acx = buf;
298 int ret;
300 wl1271_debug(DEBUG_CMD, "cmd interrogate");
302 acx->id = cpu_to_le16(id);
304 /* payload length, does not include any headers */
305 acx->len = cpu_to_le16(len - sizeof(*acx));
307 ret = wl1271_cmd_send(wl, CMD_INTERROGATE, acx, sizeof(*acx), len);
308 if (ret < 0)
309 wl1271_error("INTERROGATE command failed");
311 return ret;
315 * write acx value to firmware
317 * @wl: wl struct
318 * @id: acx id
319 * @buf: buffer containing acx, including all headers, must work with dma
320 * @len: length of buf
322 int wl1271_cmd_configure(struct wl1271 *wl, u16 id, void *buf, size_t len)
324 struct acx_header *acx = buf;
325 int ret;
327 wl1271_debug(DEBUG_CMD, "cmd configure");
329 acx->id = cpu_to_le16(id);
331 /* payload length, does not include any headers */
332 acx->len = cpu_to_le16(len - sizeof(*acx));
334 ret = wl1271_cmd_send(wl, CMD_CONFIGURE, acx, len, 0);
335 if (ret < 0) {
336 wl1271_warning("CONFIGURE command NOK");
337 return ret;
340 return 0;
343 int wl1271_cmd_data_path(struct wl1271 *wl, bool enable)
345 struct cmd_enabledisable_path *cmd;
346 int ret;
347 u16 cmd_rx, cmd_tx;
349 wl1271_debug(DEBUG_CMD, "cmd data path");
351 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
352 if (!cmd) {
353 ret = -ENOMEM;
354 goto out;
357 /* the channel here is only used for calibration, so hardcoded to 1 */
358 cmd->channel = 1;
360 if (enable) {
361 cmd_rx = CMD_ENABLE_RX;
362 cmd_tx = CMD_ENABLE_TX;
363 } else {
364 cmd_rx = CMD_DISABLE_RX;
365 cmd_tx = CMD_DISABLE_TX;
368 ret = wl1271_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd), 0);
369 if (ret < 0) {
370 wl1271_error("rx %s cmd for channel %d failed",
371 enable ? "start" : "stop", cmd->channel);
372 goto out;
375 wl1271_debug(DEBUG_BOOT, "rx %s cmd channel %d",
376 enable ? "start" : "stop", cmd->channel);
378 ret = wl1271_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd), 0);
379 if (ret < 0) {
380 wl1271_error("tx %s cmd for channel %d failed",
381 enable ? "start" : "stop", cmd->channel);
382 goto out;
385 wl1271_debug(DEBUG_BOOT, "tx %s cmd channel %d",
386 enable ? "start" : "stop", cmd->channel);
388 out:
389 kfree(cmd);
390 return ret;
393 int wl1271_cmd_ps_mode(struct wl1271 *wl, u8 ps_mode, bool send)
395 struct wl1271_cmd_ps_params *ps_params = NULL;
396 int ret = 0;
398 ret = wl1271_acx_wake_up_conditions(wl);
399 if (ret < 0) {
400 wl1271_error("couldn't set wake up conditions");
401 goto out;
404 wl1271_debug(DEBUG_CMD, "cmd set ps mode");
406 ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
407 if (!ps_params) {
408 ret = -ENOMEM;
409 goto out;
412 ps_params->ps_mode = ps_mode;
413 ps_params->send_null_data = send;
414 ps_params->retries = 5;
415 ps_params->hang_over_period = 1;
416 ps_params->null_data_rate = cpu_to_le32(wl->basic_rate_set);
418 ret = wl1271_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
419 sizeof(*ps_params), 0);
420 if (ret < 0) {
421 wl1271_error("cmd set_ps_mode failed");
422 goto out;
425 out:
426 kfree(ps_params);
427 return ret;
430 int wl1271_cmd_read_memory(struct wl1271 *wl, u32 addr, void *answer,
431 size_t len)
433 struct cmd_read_write_memory *cmd;
434 int ret = 0;
436 wl1271_debug(DEBUG_CMD, "cmd read memory");
438 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
439 if (!cmd) {
440 ret = -ENOMEM;
441 goto out;
444 WARN_ON(len > MAX_READ_SIZE);
445 len = min_t(size_t, len, MAX_READ_SIZE);
447 cmd->addr = cpu_to_le32(addr);
448 cmd->size = cpu_to_le32(len);
450 ret = wl1271_cmd_send(wl, CMD_READ_MEMORY, cmd, sizeof(*cmd),
451 sizeof(*cmd));
452 if (ret < 0) {
453 wl1271_error("read memory command failed: %d", ret);
454 goto out;
457 /* the read command got in */
458 memcpy(answer, cmd->value, len);
460 out:
461 kfree(cmd);
462 return ret;
465 int wl1271_cmd_template_set(struct wl1271 *wl, u16 template_id,
466 void *buf, size_t buf_len, int index, u32 rates)
468 struct wl1271_cmd_template_set *cmd;
469 int ret = 0;
471 wl1271_debug(DEBUG_CMD, "cmd template_set %d", template_id);
473 WARN_ON(buf_len > WL1271_CMD_TEMPL_MAX_SIZE);
474 buf_len = min_t(size_t, buf_len, WL1271_CMD_TEMPL_MAX_SIZE);
476 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
477 if (!cmd) {
478 ret = -ENOMEM;
479 goto out;
482 cmd->len = cpu_to_le16(buf_len);
483 cmd->template_type = template_id;
484 cmd->enabled_rates = cpu_to_le32(rates);
485 cmd->short_retry_limit = wl->conf.tx.rc_conf.short_retry_limit;
486 cmd->long_retry_limit = wl->conf.tx.rc_conf.long_retry_limit;
487 cmd->index = index;
489 if (buf)
490 memcpy(cmd->template_data, buf, buf_len);
492 ret = wl1271_cmd_send(wl, CMD_SET_TEMPLATE, cmd, sizeof(*cmd), 0);
493 if (ret < 0) {
494 wl1271_warning("cmd set_template failed: %d", ret);
495 goto out_free;
498 out_free:
499 kfree(cmd);
501 out:
502 return ret;
505 int wl1271_cmd_build_null_data(struct wl1271 *wl)
507 struct sk_buff *skb = NULL;
508 int size;
509 void *ptr;
510 int ret = -ENOMEM;
513 if (wl->bss_type == BSS_TYPE_IBSS) {
514 size = sizeof(struct wl12xx_null_data_template);
515 ptr = NULL;
516 } else {
517 skb = ieee80211_nullfunc_get(wl->hw, wl->vif);
518 if (!skb)
519 goto out;
520 size = skb->len;
521 ptr = skb->data;
524 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_NULL_DATA, ptr, size, 0,
525 WL1271_RATE_AUTOMATIC);
527 out:
528 dev_kfree_skb(skb);
529 if (ret)
530 wl1271_warning("cmd buld null data failed %d", ret);
532 return ret;
536 int wl1271_cmd_build_klv_null_data(struct wl1271 *wl)
538 struct sk_buff *skb = NULL;
539 int ret = -ENOMEM;
541 skb = ieee80211_nullfunc_get(wl->hw, wl->vif);
542 if (!skb)
543 goto out;
545 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_KLV,
546 skb->data, skb->len,
547 CMD_TEMPL_KLV_IDX_NULL_DATA,
548 WL1271_RATE_AUTOMATIC);
550 out:
551 dev_kfree_skb(skb);
552 if (ret)
553 wl1271_warning("cmd build klv null data failed %d", ret);
555 return ret;
559 int wl1271_cmd_build_ps_poll(struct wl1271 *wl, u16 aid)
561 struct sk_buff *skb;
562 int ret = 0;
564 skb = ieee80211_pspoll_get(wl->hw, wl->vif);
565 if (!skb)
566 goto out;
568 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_PS_POLL, skb->data,
569 skb->len, 0, wl->basic_rate_set);
571 out:
572 dev_kfree_skb(skb);
573 return ret;
576 int wl1271_cmd_build_probe_req(struct wl1271 *wl,
577 const u8 *ssid, size_t ssid_len,
578 const u8 *ie, size_t ie_len, u8 band)
580 struct sk_buff *skb;
581 int ret;
583 skb = ieee80211_probereq_get(wl->hw, wl->vif, ssid, ssid_len,
584 ie, ie_len);
585 if (!skb) {
586 ret = -ENOMEM;
587 goto out;
590 wl1271_dump(DEBUG_SCAN, "PROBE REQ: ", skb->data, skb->len);
592 if (band == IEEE80211_BAND_2GHZ)
593 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_2_4,
594 skb->data, skb->len, 0,
595 wl->conf.tx.basic_rate);
596 else
597 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_5,
598 skb->data, skb->len, 0,
599 wl->conf.tx.basic_rate_5);
601 out:
602 dev_kfree_skb(skb);
603 return ret;
606 int wl1271_build_qos_null_data(struct wl1271 *wl)
608 struct ieee80211_qos_hdr template;
610 memset(&template, 0, sizeof(template));
612 memcpy(template.addr1, wl->bssid, ETH_ALEN);
613 memcpy(template.addr2, wl->mac_addr, ETH_ALEN);
614 memcpy(template.addr3, wl->bssid, ETH_ALEN);
616 template.frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
617 IEEE80211_STYPE_QOS_NULLFUNC |
618 IEEE80211_FCTL_TODS);
620 template.qos_ctrl = cpu_to_le16(0);
622 return wl1271_cmd_template_set(wl, CMD_TEMPL_QOS_NULL_DATA, &template,
623 sizeof(template), 0,
624 WL1271_RATE_AUTOMATIC);
627 int wl1271_cmd_set_default_wep_key(struct wl1271 *wl, u8 id)
629 struct wl1271_cmd_set_keys *cmd;
630 int ret = 0;
632 wl1271_debug(DEBUG_CMD, "cmd set_default_wep_key %d", id);
634 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
635 if (!cmd) {
636 ret = -ENOMEM;
637 goto out;
640 cmd->id = id;
641 cmd->key_action = cpu_to_le16(KEY_SET_ID);
642 cmd->key_type = KEY_WEP;
644 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
645 if (ret < 0) {
646 wl1271_warning("cmd set_default_wep_key failed: %d", ret);
647 goto out;
650 out:
651 kfree(cmd);
653 return ret;
656 int wl1271_cmd_set_key(struct wl1271 *wl, u16 action, u8 id, u8 key_type,
657 u8 key_size, const u8 *key, const u8 *addr,
658 u32 tx_seq_32, u16 tx_seq_16)
660 struct wl1271_cmd_set_keys *cmd;
661 int ret = 0;
663 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
664 if (!cmd) {
665 ret = -ENOMEM;
666 goto out;
669 if (key_type != KEY_WEP)
670 memcpy(cmd->addr, addr, ETH_ALEN);
672 cmd->key_action = cpu_to_le16(action);
673 cmd->key_size = key_size;
674 cmd->key_type = key_type;
676 cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
677 cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
679 /* we have only one SSID profile */
680 cmd->ssid_profile = 0;
682 cmd->id = id;
684 if (key_type == KEY_TKIP) {
686 * We get the key in the following form:
687 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
688 * but the target is expecting:
689 * TKIP - RX MIC - TX MIC
691 memcpy(cmd->key, key, 16);
692 memcpy(cmd->key + 16, key + 24, 8);
693 memcpy(cmd->key + 24, key + 16, 8);
695 } else {
696 memcpy(cmd->key, key, key_size);
699 wl1271_dump(DEBUG_CRYPT, "TARGET KEY: ", cmd, sizeof(*cmd));
701 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
702 if (ret < 0) {
703 wl1271_warning("could not set keys");
704 goto out;
707 out:
708 kfree(cmd);
710 return ret;
713 int wl1271_cmd_disconnect(struct wl1271 *wl)
715 struct wl1271_cmd_disconnect *cmd;
716 int ret = 0;
718 wl1271_debug(DEBUG_CMD, "cmd disconnect");
720 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
721 if (!cmd) {
722 ret = -ENOMEM;
723 goto out;
726 cmd->rx_config_options = cpu_to_le32(wl->rx_config);
727 cmd->rx_filter_options = cpu_to_le32(wl->rx_filter);
728 /* disconnect reason is not used in immediate disconnections */
729 cmd->type = DISCONNECT_IMMEDIATE;
731 ret = wl1271_cmd_send(wl, CMD_DISCONNECT, cmd, sizeof(*cmd), 0);
732 if (ret < 0) {
733 wl1271_error("failed to send disconnect command");
734 goto out_free;
737 ret = wl1271_cmd_wait_for_event(wl, DISCONNECT_EVENT_COMPLETE_ID);
738 if (ret < 0)
739 wl1271_error("cmd disconnect event completion error");
741 out_free:
742 kfree(cmd);
744 out:
745 return ret;