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
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>
36 #include "wl12xx_80211.h"
40 #define WL1271_CMD_FAST_POLL_COUNT 50
43 * send command to firmware
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,
53 struct wl1271_cmd_header *cmd;
54 unsigned long timeout;
61 cmd->id = cpu_to_le16(id);
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");
81 if (poll_count < WL1271_CMD_FAST_POLL_COUNT)
86 intr = wl1271_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
89 /* read back the status code of the command */
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 ieee80211_queue_work(wl->hw, &wl->recovery_work);
101 wl1271_write32(wl, ACX_REG_INTERRUPT_ACK,
102 WL1271_ACX_INTR_CMD_COMPLETE);
108 int wl1271_cmd_general_parms(struct wl1271 *wl)
110 struct wl1271_general_parms_cmd *gen_parms;
111 struct wl1271_ini_general_params *gp = &wl->nvs->general_params;
118 gen_parms = kzalloc(sizeof(*gen_parms), GFP_KERNEL);
122 gen_parms->test.id = TEST_CMD_INI_FILE_GENERAL_PARAM;
124 memcpy(&gen_parms->general_params, gp, sizeof(*gp));
126 if (gp->tx_bip_fem_auto_detect)
129 ret = wl1271_cmd_test(wl, gen_parms, sizeof(*gen_parms), answer);
131 wl1271_warning("CMD_INI_FILE_GENERAL_PARAM failed");
135 gp->tx_bip_fem_manufacturer =
136 gen_parms->general_params.tx_bip_fem_manufacturer;
138 wl1271_debug(DEBUG_CMD, "FEM autodetect: %s, manufacturer: %d\n",
139 answer ? "auto" : "manual", gp->tx_bip_fem_manufacturer);
146 int wl1271_cmd_radio_parms(struct wl1271 *wl)
148 struct wl1271_radio_parms_cmd *radio_parms;
149 struct wl1271_ini_general_params *gp = &wl->nvs->general_params;
155 radio_parms = kzalloc(sizeof(*radio_parms), GFP_KERNEL);
159 radio_parms->test.id = TEST_CMD_INI_FILE_RADIO_PARAM;
161 /* 2.4GHz parameters */
162 memcpy(&radio_parms->static_params_2, &wl->nvs->stat_radio_params_2,
163 sizeof(struct wl1271_ini_band_params_2));
164 memcpy(&radio_parms->dyn_params_2,
165 &wl->nvs->dyn_radio_params_2[gp->tx_bip_fem_manufacturer].params,
166 sizeof(struct wl1271_ini_fem_params_2));
168 /* 5GHz parameters */
169 memcpy(&radio_parms->static_params_5,
170 &wl->nvs->stat_radio_params_5,
171 sizeof(struct wl1271_ini_band_params_5));
172 memcpy(&radio_parms->dyn_params_5,
173 &wl->nvs->dyn_radio_params_5[gp->tx_bip_fem_manufacturer].params,
174 sizeof(struct wl1271_ini_fem_params_5));
176 wl1271_dump(DEBUG_CMD, "TEST_CMD_INI_FILE_RADIO_PARAM: ",
177 radio_parms, sizeof(*radio_parms));
179 ret = wl1271_cmd_test(wl, radio_parms, sizeof(*radio_parms), 0);
181 wl1271_warning("CMD_INI_FILE_RADIO_PARAM failed");
187 int wl1271_cmd_ext_radio_parms(struct wl1271 *wl)
189 struct wl1271_ext_radio_parms_cmd *ext_radio_parms;
190 struct conf_rf_settings *rf = &wl->conf.rf;
196 ext_radio_parms = kzalloc(sizeof(*ext_radio_parms), GFP_KERNEL);
197 if (!ext_radio_parms)
200 ext_radio_parms->test.id = TEST_CMD_INI_FILE_RF_EXTENDED_PARAM;
202 memcpy(ext_radio_parms->tx_per_channel_power_compensation_2,
203 rf->tx_per_channel_power_compensation_2,
204 CONF_TX_PWR_COMPENSATION_LEN_2);
205 memcpy(ext_radio_parms->tx_per_channel_power_compensation_5,
206 rf->tx_per_channel_power_compensation_5,
207 CONF_TX_PWR_COMPENSATION_LEN_5);
209 wl1271_dump(DEBUG_CMD, "TEST_CMD_INI_FILE_EXT_RADIO_PARAM: ",
210 ext_radio_parms, sizeof(*ext_radio_parms));
212 ret = wl1271_cmd_test(wl, ext_radio_parms, sizeof(*ext_radio_parms), 0);
214 wl1271_warning("TEST_CMD_INI_FILE_RF_EXTENDED_PARAM failed");
216 kfree(ext_radio_parms);
221 * Poll the mailbox event field until any of the bits in the mask is set or a
222 * timeout occurs (WL1271_EVENT_TIMEOUT in msecs)
224 static int wl1271_cmd_wait_for_event(struct wl1271 *wl, u32 mask)
226 u32 events_vector, event;
227 unsigned long timeout;
229 timeout = jiffies + msecs_to_jiffies(WL1271_EVENT_TIMEOUT);
232 if (time_after(jiffies, timeout)) {
233 ieee80211_queue_work(wl->hw, &wl->recovery_work);
239 /* read from both event fields */
240 wl1271_read(wl, wl->mbox_ptr[0], &events_vector,
241 sizeof(events_vector), false);
242 event = events_vector & mask;
243 wl1271_read(wl, wl->mbox_ptr[1], &events_vector,
244 sizeof(events_vector), false);
245 event |= events_vector & mask;
251 int wl1271_cmd_join(struct wl1271 *wl, u8 bss_type)
253 struct wl1271_cmd_join *join;
257 join = kzalloc(sizeof(*join), GFP_KERNEL);
263 wl1271_debug(DEBUG_CMD, "cmd join");
265 /* Reverse order BSSID */
266 bssid = (u8 *) &join->bssid_lsb;
267 for (i = 0; i < ETH_ALEN; i++)
268 bssid[i] = wl->bssid[ETH_ALEN - i - 1];
270 join->rx_config_options = cpu_to_le32(wl->rx_config);
271 join->rx_filter_options = cpu_to_le32(wl->rx_filter);
272 join->bss_type = bss_type;
273 join->basic_rate_set = cpu_to_le32(wl->basic_rate_set);
275 if (wl->band == IEEE80211_BAND_5GHZ)
276 join->bss_type |= WL1271_JOIN_CMD_BSS_TYPE_5GHZ;
278 join->beacon_interval = cpu_to_le16(wl->beacon_int);
279 join->dtim_interval = WL1271_DEFAULT_DTIM_PERIOD;
281 join->channel = wl->channel;
282 join->ssid_len = wl->ssid_len;
283 memcpy(join->ssid, wl->ssid, wl->ssid_len);
285 join->ctrl |= wl->session_counter << WL1271_JOIN_CMD_TX_SESSION_OFFSET;
287 /* reset TX security counters */
288 wl->tx_security_last_seq = 0;
289 wl->tx_security_seq = 0;
291 ret = wl1271_cmd_send(wl, CMD_START_JOIN, join, sizeof(*join), 0);
293 wl1271_error("failed to initiate cmd join");
297 ret = wl1271_cmd_wait_for_event(wl, JOIN_EVENT_COMPLETE_ID);
299 wl1271_error("cmd join event completion error");
309 * send test command to firmware
312 * @buf: buffer containing the command, with all headers, must work with dma
313 * @len: length of the buffer
314 * @answer: is answer needed
316 int wl1271_cmd_test(struct wl1271 *wl, void *buf, size_t buf_len, u8 answer)
321 wl1271_debug(DEBUG_CMD, "cmd test");
326 ret = wl1271_cmd_send(wl, CMD_TEST, buf, buf_len, res_len);
329 wl1271_warning("TEST command failed");
337 * read acx from firmware
341 * @buf: buffer for the response, including all headers, must work with dma
342 * @len: lenght of buf
344 int wl1271_cmd_interrogate(struct wl1271 *wl, u16 id, void *buf, size_t len)
346 struct acx_header *acx = buf;
349 wl1271_debug(DEBUG_CMD, "cmd interrogate");
351 acx->id = cpu_to_le16(id);
353 /* payload length, does not include any headers */
354 acx->len = cpu_to_le16(len - sizeof(*acx));
356 ret = wl1271_cmd_send(wl, CMD_INTERROGATE, acx, sizeof(*acx), len);
358 wl1271_error("INTERROGATE command failed");
364 * write acx value to firmware
368 * @buf: buffer containing acx, including all headers, must work with dma
369 * @len: length of buf
371 int wl1271_cmd_configure(struct wl1271 *wl, u16 id, void *buf, size_t len)
373 struct acx_header *acx = buf;
376 wl1271_debug(DEBUG_CMD, "cmd configure");
378 acx->id = cpu_to_le16(id);
380 /* payload length, does not include any headers */
381 acx->len = cpu_to_le16(len - sizeof(*acx));
383 ret = wl1271_cmd_send(wl, CMD_CONFIGURE, acx, len, 0);
385 wl1271_warning("CONFIGURE command NOK");
392 int wl1271_cmd_data_path(struct wl1271 *wl, bool enable)
394 struct cmd_enabledisable_path *cmd;
398 wl1271_debug(DEBUG_CMD, "cmd data path");
400 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
406 /* the channel here is only used for calibration, so hardcoded to 1 */
410 cmd_rx = CMD_ENABLE_RX;
411 cmd_tx = CMD_ENABLE_TX;
413 cmd_rx = CMD_DISABLE_RX;
414 cmd_tx = CMD_DISABLE_TX;
417 ret = wl1271_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd), 0);
419 wl1271_error("rx %s cmd for channel %d failed",
420 enable ? "start" : "stop", cmd->channel);
424 wl1271_debug(DEBUG_BOOT, "rx %s cmd channel %d",
425 enable ? "start" : "stop", cmd->channel);
427 ret = wl1271_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd), 0);
429 wl1271_error("tx %s cmd for channel %d failed",
430 enable ? "start" : "stop", cmd->channel);
434 wl1271_debug(DEBUG_BOOT, "tx %s cmd channel %d",
435 enable ? "start" : "stop", cmd->channel);
442 int wl1271_cmd_ps_mode(struct wl1271 *wl, u8 ps_mode, u32 rates, bool send)
444 struct wl1271_cmd_ps_params *ps_params = NULL;
447 wl1271_debug(DEBUG_CMD, "cmd set ps mode");
449 ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
455 ps_params->ps_mode = ps_mode;
456 ps_params->send_null_data = send;
457 ps_params->retries = wl->conf.conn.psm_entry_nullfunc_retries;
458 ps_params->hang_over_period = wl->conf.conn.psm_entry_hangover_period;
459 ps_params->null_data_rate = cpu_to_le32(rates);
461 ret = wl1271_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
462 sizeof(*ps_params), 0);
464 wl1271_error("cmd set_ps_mode failed");
473 int wl1271_cmd_template_set(struct wl1271 *wl, u16 template_id,
474 void *buf, size_t buf_len, int index, u32 rates)
476 struct wl1271_cmd_template_set *cmd;
479 wl1271_debug(DEBUG_CMD, "cmd template_set %d", template_id);
481 WARN_ON(buf_len > WL1271_CMD_TEMPL_MAX_SIZE);
482 buf_len = min_t(size_t, buf_len, WL1271_CMD_TEMPL_MAX_SIZE);
484 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
490 cmd->len = cpu_to_le16(buf_len);
491 cmd->template_type = template_id;
492 cmd->enabled_rates = cpu_to_le32(rates);
493 cmd->short_retry_limit = wl->conf.tx.rc_conf.short_retry_limit;
494 cmd->long_retry_limit = wl->conf.tx.rc_conf.long_retry_limit;
498 memcpy(cmd->template_data, buf, buf_len);
500 ret = wl1271_cmd_send(wl, CMD_SET_TEMPLATE, cmd, sizeof(*cmd), 0);
502 wl1271_warning("cmd set_template failed: %d", ret);
513 int wl1271_cmd_build_null_data(struct wl1271 *wl)
515 struct sk_buff *skb = NULL;
521 if (wl->bss_type == BSS_TYPE_IBSS) {
522 size = sizeof(struct wl12xx_null_data_template);
525 skb = ieee80211_nullfunc_get(wl->hw, wl->vif);
532 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_NULL_DATA, ptr, size, 0,
538 wl1271_warning("cmd buld null data failed %d", ret);
544 int wl1271_cmd_build_klv_null_data(struct wl1271 *wl)
546 struct sk_buff *skb = NULL;
549 skb = ieee80211_nullfunc_get(wl->hw, wl->vif);
553 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_KLV,
555 CMD_TEMPL_KLV_IDX_NULL_DATA,
561 wl1271_warning("cmd build klv null data failed %d", ret);
567 int wl1271_cmd_build_ps_poll(struct wl1271 *wl, u16 aid)
572 skb = ieee80211_pspoll_get(wl->hw, wl->vif);
576 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_PS_POLL, skb->data,
577 skb->len, 0, wl->basic_rate_set);
584 int wl1271_cmd_build_probe_req(struct wl1271 *wl,
585 const u8 *ssid, size_t ssid_len,
586 const u8 *ie, size_t ie_len, u8 band)
591 skb = ieee80211_probereq_get(wl->hw, wl->vif, ssid, ssid_len,
598 wl1271_dump(DEBUG_SCAN, "PROBE REQ: ", skb->data, skb->len);
600 if (band == IEEE80211_BAND_2GHZ)
601 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_2_4,
602 skb->data, skb->len, 0,
603 wl->conf.tx.basic_rate);
605 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_5,
606 skb->data, skb->len, 0,
607 wl->conf.tx.basic_rate_5);
614 struct sk_buff *wl1271_cmd_build_ap_probe_req(struct wl1271 *wl,
620 skb = ieee80211_ap_probereq_get(wl->hw, wl->vif);
624 wl1271_dump(DEBUG_SCAN, "AP PROBE REQ: ", skb->data, skb->len);
626 if (wl->band == IEEE80211_BAND_2GHZ)
627 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_2_4,
628 skb->data, skb->len, 0,
629 wl->conf.tx.basic_rate);
631 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_5,
632 skb->data, skb->len, 0,
633 wl->conf.tx.basic_rate_5);
636 wl1271_error("Unable to set ap probe request template.");
642 int wl1271_cmd_build_arp_rsp(struct wl1271 *wl, __be32 ip_addr)
645 struct wl12xx_arp_rsp_template tmpl;
646 struct ieee80211_hdr_3addr *hdr;
647 struct arphdr *arp_hdr;
649 memset(&tmpl, 0, sizeof(tmpl));
651 /* mac80211 header */
653 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
654 IEEE80211_STYPE_DATA |
655 IEEE80211_FCTL_TODS);
656 memcpy(hdr->addr1, wl->vif->bss_conf.bssid, ETH_ALEN);
657 memcpy(hdr->addr2, wl->vif->addr, ETH_ALEN);
658 memset(hdr->addr3, 0xff, ETH_ALEN);
661 memcpy(tmpl.llc_hdr, rfc1042_header, sizeof(rfc1042_header));
662 tmpl.llc_type = htons(ETH_P_ARP);
665 arp_hdr = &tmpl.arp_hdr;
666 arp_hdr->ar_hrd = htons(ARPHRD_ETHER);
667 arp_hdr->ar_pro = htons(ETH_P_IP);
668 arp_hdr->ar_hln = ETH_ALEN;
670 arp_hdr->ar_op = htons(ARPOP_REPLY);
673 memcpy(tmpl.sender_hw, wl->vif->addr, ETH_ALEN);
674 tmpl.sender_ip = ip_addr;
676 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_ARP_RSP,
677 &tmpl, sizeof(tmpl), 0,
683 int wl1271_build_qos_null_data(struct wl1271 *wl)
685 struct ieee80211_qos_hdr template;
687 memset(&template, 0, sizeof(template));
689 memcpy(template.addr1, wl->bssid, ETH_ALEN);
690 memcpy(template.addr2, wl->mac_addr, ETH_ALEN);
691 memcpy(template.addr3, wl->bssid, ETH_ALEN);
693 template.frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
694 IEEE80211_STYPE_QOS_NULLFUNC |
695 IEEE80211_FCTL_TODS);
697 /* FIXME: not sure what priority to use here */
698 template.qos_ctrl = cpu_to_le16(0);
700 return wl1271_cmd_template_set(wl, CMD_TEMPL_QOS_NULL_DATA, &template,
705 int wl1271_cmd_set_default_wep_key(struct wl1271 *wl, u8 id)
707 struct wl1271_cmd_set_keys *cmd;
710 wl1271_debug(DEBUG_CMD, "cmd set_default_wep_key %d", id);
712 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
719 cmd->key_action = cpu_to_le16(KEY_SET_ID);
720 cmd->key_type = KEY_WEP;
722 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
724 wl1271_warning("cmd set_default_wep_key failed: %d", ret);
734 int wl1271_cmd_set_key(struct wl1271 *wl, u16 action, u8 id, u8 key_type,
735 u8 key_size, const u8 *key, const u8 *addr,
736 u32 tx_seq_32, u16 tx_seq_16)
738 struct wl1271_cmd_set_keys *cmd;
741 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
747 if (key_type != KEY_WEP)
748 memcpy(cmd->addr, addr, ETH_ALEN);
750 cmd->key_action = cpu_to_le16(action);
751 cmd->key_size = key_size;
752 cmd->key_type = key_type;
754 cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
755 cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
757 /* we have only one SSID profile */
758 cmd->ssid_profile = 0;
762 if (key_type == KEY_TKIP) {
764 * We get the key in the following form:
765 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
766 * but the target is expecting:
767 * TKIP - RX MIC - TX MIC
769 memcpy(cmd->key, key, 16);
770 memcpy(cmd->key + 16, key + 24, 8);
771 memcpy(cmd->key + 24, key + 16, 8);
774 memcpy(cmd->key, key, key_size);
777 wl1271_dump(DEBUG_CRYPT, "TARGET KEY: ", cmd, sizeof(*cmd));
779 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
781 wl1271_warning("could not set keys");
791 int wl1271_cmd_disconnect(struct wl1271 *wl)
793 struct wl1271_cmd_disconnect *cmd;
796 wl1271_debug(DEBUG_CMD, "cmd disconnect");
798 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
804 cmd->rx_config_options = cpu_to_le32(wl->rx_config);
805 cmd->rx_filter_options = cpu_to_le32(wl->rx_filter);
806 /* disconnect reason is not used in immediate disconnections */
807 cmd->type = DISCONNECT_IMMEDIATE;
809 ret = wl1271_cmd_send(wl, CMD_DISCONNECT, cmd, sizeof(*cmd), 0);
811 wl1271_error("failed to send disconnect command");
815 ret = wl1271_cmd_wait_for_event(wl, DISCONNECT_EVENT_COMPLETE_ID);
817 wl1271_error("cmd disconnect event completion error");
826 int wl1271_cmd_set_sta_state(struct wl1271 *wl)
828 struct wl1271_cmd_set_sta_state *cmd;
831 wl1271_debug(DEBUG_CMD, "cmd set sta state");
833 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
839 cmd->state = WL1271_CMD_STA_STATE_CONNECTED;
841 ret = wl1271_cmd_send(wl, CMD_SET_STA_STATE, cmd, sizeof(*cmd), 0);
843 wl1271_error("failed to send set STA state command");