2 * Copyright (c) 2014 Qualcomm Atheros, Inc.
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <net/netlink.h>
20 #include <linux/firmware.h>
27 #include "testmode_i.h"
29 static const struct nla_policy ath10k_tm_policy[ATH10K_TM_ATTR_MAX + 1] = {
30 [ATH10K_TM_ATTR_CMD] = { .type = NLA_U32 },
31 [ATH10K_TM_ATTR_DATA] = { .type = NLA_BINARY,
32 .len = ATH10K_TM_DATA_MAX_LEN },
33 [ATH10K_TM_ATTR_WMI_CMDID] = { .type = NLA_U32 },
34 [ATH10K_TM_ATTR_VERSION_MAJOR] = { .type = NLA_U32 },
35 [ATH10K_TM_ATTR_VERSION_MINOR] = { .type = NLA_U32 },
38 /* Returns true if callee consumes the skb and the skb should be discarded.
39 * Returns false if skb is not used. Does not sleep.
41 bool ath10k_tm_event_wmi(struct ath10k *ar, u32 cmd_id, struct sk_buff *skb)
43 struct sk_buff *nl_skb;
47 ath10k_dbg(ar, ATH10K_DBG_TESTMODE,
48 "testmode event wmi cmd_id %d skb %p skb->len %d\n",
49 cmd_id, skb, skb->len);
51 ath10k_dbg_dump(ar, ATH10K_DBG_TESTMODE, NULL, "", skb->data, skb->len);
53 spin_lock_bh(&ar->data_lock);
55 if (!ar->testmode.utf_monitor) {
60 /* Only testmode.c should be handling events from utf firmware,
61 * otherwise all sort of problems will arise as mac80211 operations
62 * are not initialised.
66 nl_skb = cfg80211_testmode_alloc_event_skb(ar->hw->wiphy,
67 2 * sizeof(u32) + skb->len,
71 "failed to allocate skb for testmode wmi event\n");
75 ret = nla_put_u32(nl_skb, ATH10K_TM_ATTR_CMD, ATH10K_TM_CMD_WMI);
78 "failed to to put testmode wmi event cmd attribute: %d\n",
84 ret = nla_put_u32(nl_skb, ATH10K_TM_ATTR_WMI_CMDID, cmd_id);
87 "failed to to put testmode wmi even cmd_id: %d\n",
93 ret = nla_put(nl_skb, ATH10K_TM_ATTR_DATA, skb->len, skb->data);
96 "failed to copy skb to testmode wmi event: %d\n",
102 cfg80211_testmode_event(nl_skb, GFP_ATOMIC);
105 spin_unlock_bh(&ar->data_lock);
110 static int ath10k_tm_cmd_get_version(struct ath10k *ar, struct nlattr *tb[])
115 ath10k_dbg(ar, ATH10K_DBG_TESTMODE,
116 "testmode cmd get version_major %d version_minor %d\n",
117 ATH10K_TESTMODE_VERSION_MAJOR,
118 ATH10K_TESTMODE_VERSION_MINOR);
120 skb = cfg80211_testmode_alloc_reply_skb(ar->hw->wiphy,
121 nla_total_size(sizeof(u32)));
125 ret = nla_put_u32(skb, ATH10K_TM_ATTR_VERSION_MAJOR,
126 ATH10K_TESTMODE_VERSION_MAJOR);
132 ret = nla_put_u32(skb, ATH10K_TM_ATTR_VERSION_MINOR,
133 ATH10K_TESTMODE_VERSION_MINOR);
139 return cfg80211_testmode_reply(skb);
142 static int ath10k_tm_cmd_utf_start(struct ath10k *ar, struct nlattr *tb[])
147 ath10k_dbg(ar, ATH10K_DBG_TESTMODE, "testmode cmd utf start\n");
149 mutex_lock(&ar->conf_mutex);
151 if (ar->state == ATH10K_STATE_UTF) {
156 /* start utf only when the driver is not in use */
157 if (ar->state != ATH10K_STATE_OFF) {
162 if (WARN_ON(ar->testmode.utf != NULL)) {
163 /* utf image is already downloaded, it shouldn't be */
168 snprintf(filename, sizeof(filename), "%s/%s",
169 ar->hw_params.fw.dir, ATH10K_FW_UTF_FILE);
171 /* load utf firmware image */
172 ret = request_firmware(&ar->testmode.utf, filename, ar->dev);
174 ath10k_warn(ar, "failed to retrieve utf firmware '%s': %d\n",
179 spin_lock_bh(&ar->data_lock);
181 ar->testmode.utf_monitor = true;
183 spin_unlock_bh(&ar->data_lock);
185 BUILD_BUG_ON(sizeof(ar->fw_features) !=
186 sizeof(ar->testmode.orig_fw_features));
188 memcpy(ar->testmode.orig_fw_features, ar->fw_features,
189 sizeof(ar->fw_features));
191 /* utf.bin firmware image does not advertise firmware features. Do
192 * an ugly hack where we force the firmware features so that wmi.c
193 * will use the correct WMI interface.
195 memset(ar->fw_features, 0, sizeof(ar->fw_features));
196 __set_bit(ATH10K_FW_FEATURE_WMI_10X, ar->fw_features);
198 ret = ath10k_hif_power_up(ar);
200 ath10k_err(ar, "failed to power up hif (testmode): %d\n", ret);
201 ar->state = ATH10K_STATE_OFF;
202 goto err_fw_features;
205 ret = ath10k_core_start(ar, ATH10K_FIRMWARE_MODE_UTF);
207 ath10k_err(ar, "failed to start core (testmode): %d\n", ret);
208 ar->state = ATH10K_STATE_OFF;
212 ar->state = ATH10K_STATE_UTF;
214 ath10k_info(ar, "UTF firmware started\n");
216 mutex_unlock(&ar->conf_mutex);
221 ath10k_hif_power_down(ar);
224 /* return the original firmware features */
225 memcpy(ar->fw_features, ar->testmode.orig_fw_features,
226 sizeof(ar->fw_features));
228 release_firmware(ar->testmode.utf);
229 ar->testmode.utf = NULL;
232 mutex_unlock(&ar->conf_mutex);
237 static void __ath10k_tm_cmd_utf_stop(struct ath10k *ar)
239 lockdep_assert_held(&ar->conf_mutex);
241 ath10k_core_stop(ar);
242 ath10k_hif_power_down(ar);
244 spin_lock_bh(&ar->data_lock);
246 ar->testmode.utf_monitor = false;
248 spin_unlock_bh(&ar->data_lock);
250 /* return the original firmware features */
251 memcpy(ar->fw_features, ar->testmode.orig_fw_features,
252 sizeof(ar->fw_features));
254 release_firmware(ar->testmode.utf);
255 ar->testmode.utf = NULL;
257 ar->state = ATH10K_STATE_OFF;
260 static int ath10k_tm_cmd_utf_stop(struct ath10k *ar, struct nlattr *tb[])
264 ath10k_dbg(ar, ATH10K_DBG_TESTMODE, "testmode cmd utf stop\n");
266 mutex_lock(&ar->conf_mutex);
268 if (ar->state != ATH10K_STATE_UTF) {
273 __ath10k_tm_cmd_utf_stop(ar);
277 ath10k_info(ar, "UTF firmware stopped\n");
280 mutex_unlock(&ar->conf_mutex);
284 static int ath10k_tm_cmd_wmi(struct ath10k *ar, struct nlattr *tb[])
291 mutex_lock(&ar->conf_mutex);
293 if (ar->state != ATH10K_STATE_UTF) {
298 if (!tb[ATH10K_TM_ATTR_DATA]) {
303 if (!tb[ATH10K_TM_ATTR_WMI_CMDID]) {
308 buf = nla_data(tb[ATH10K_TM_ATTR_DATA]);
309 buf_len = nla_len(tb[ATH10K_TM_ATTR_DATA]);
310 cmd_id = nla_get_u32(tb[ATH10K_TM_ATTR_WMI_CMDID]);
312 ath10k_dbg(ar, ATH10K_DBG_TESTMODE,
313 "testmode cmd wmi cmd_id %d buf %p buf_len %d\n",
314 cmd_id, buf, buf_len);
316 ath10k_dbg_dump(ar, ATH10K_DBG_TESTMODE, NULL, "", buf, buf_len);
318 skb = ath10k_wmi_alloc_skb(ar, buf_len);
324 memcpy(skb->data, buf, buf_len);
326 ret = ath10k_wmi_cmd_send(ar, skb, cmd_id);
328 ath10k_warn(ar, "failed to transmit wmi command (testmode): %d\n",
336 mutex_unlock(&ar->conf_mutex);
340 int ath10k_tm_cmd(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
343 struct ath10k *ar = hw->priv;
344 struct nlattr *tb[ATH10K_TM_ATTR_MAX + 1];
347 ret = nla_parse(tb, ATH10K_TM_ATTR_MAX, data, len,
352 if (!tb[ATH10K_TM_ATTR_CMD])
355 switch (nla_get_u32(tb[ATH10K_TM_ATTR_CMD])) {
356 case ATH10K_TM_CMD_GET_VERSION:
357 return ath10k_tm_cmd_get_version(ar, tb);
358 case ATH10K_TM_CMD_UTF_START:
359 return ath10k_tm_cmd_utf_start(ar, tb);
360 case ATH10K_TM_CMD_UTF_STOP:
361 return ath10k_tm_cmd_utf_stop(ar, tb);
362 case ATH10K_TM_CMD_WMI:
363 return ath10k_tm_cmd_wmi(ar, tb);
369 void ath10k_testmode_destroy(struct ath10k *ar)
371 mutex_lock(&ar->conf_mutex);
373 if (ar->state != ATH10K_STATE_UTF) {
374 /* utf firmware is not running, nothing to do */
378 __ath10k_tm_cmd_utf_stop(ar);
381 mutex_unlock(&ar->conf_mutex);