WIP FPC-III support
[linux/fpc-iii.git] / drivers / net / wireless / ti / wlcore / testmode.c
blob3a17b9a8207e2cb7ad051cfe908d54bd5198112d
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * This file is part of wl1271
5 * Copyright (C) 2010 Nokia Corporation
7 * Contact: Luciano Coelho <luciano.coelho@nokia.com>
8 */
9 #include "testmode.h"
11 #include <linux/pm_runtime.h>
12 #include <linux/slab.h>
13 #include <net/genetlink.h>
15 #include "wlcore.h"
16 #include "debug.h"
17 #include "acx.h"
18 #include "io.h"
20 #define WL1271_TM_MAX_DATA_LENGTH 1024
22 enum wl1271_tm_commands {
23 WL1271_TM_CMD_UNSPEC,
24 WL1271_TM_CMD_TEST,
25 WL1271_TM_CMD_INTERROGATE,
26 WL1271_TM_CMD_CONFIGURE,
27 WL1271_TM_CMD_NVS_PUSH, /* Not in use. Keep to not break ABI */
28 WL1271_TM_CMD_SET_PLT_MODE,
29 WL1271_TM_CMD_RECOVER, /* Not in use. Keep to not break ABI */
30 WL1271_TM_CMD_GET_MAC,
32 __WL1271_TM_CMD_AFTER_LAST
34 #define WL1271_TM_CMD_MAX (__WL1271_TM_CMD_AFTER_LAST - 1)
36 enum wl1271_tm_attrs {
37 WL1271_TM_ATTR_UNSPEC,
38 WL1271_TM_ATTR_CMD_ID,
39 WL1271_TM_ATTR_ANSWER,
40 WL1271_TM_ATTR_DATA,
41 WL1271_TM_ATTR_IE_ID,
42 WL1271_TM_ATTR_PLT_MODE,
44 __WL1271_TM_ATTR_AFTER_LAST
46 #define WL1271_TM_ATTR_MAX (__WL1271_TM_ATTR_AFTER_LAST - 1)
48 static struct nla_policy wl1271_tm_policy[WL1271_TM_ATTR_MAX + 1] = {
49 [WL1271_TM_ATTR_CMD_ID] = { .type = NLA_U32 },
50 [WL1271_TM_ATTR_ANSWER] = { .type = NLA_U8 },
51 [WL1271_TM_ATTR_DATA] = { .type = NLA_BINARY,
52 .len = WL1271_TM_MAX_DATA_LENGTH },
53 [WL1271_TM_ATTR_IE_ID] = { .type = NLA_U32 },
54 [WL1271_TM_ATTR_PLT_MODE] = { .type = NLA_U32 },
58 static int wl1271_tm_cmd_test(struct wl1271 *wl, struct nlattr *tb[])
60 int buf_len, ret, len;
61 struct sk_buff *skb;
62 void *buf;
63 u8 answer = 0;
65 wl1271_debug(DEBUG_TESTMODE, "testmode cmd test");
67 if (!tb[WL1271_TM_ATTR_DATA])
68 return -EINVAL;
70 buf = nla_data(tb[WL1271_TM_ATTR_DATA]);
71 buf_len = nla_len(tb[WL1271_TM_ATTR_DATA]);
73 if (tb[WL1271_TM_ATTR_ANSWER])
74 answer = nla_get_u8(tb[WL1271_TM_ATTR_ANSWER]);
76 if (buf_len > sizeof(struct wl1271_command))
77 return -EMSGSIZE;
79 mutex_lock(&wl->mutex);
81 if (unlikely(wl->state != WLCORE_STATE_ON)) {
82 ret = -EINVAL;
83 goto out;
86 ret = pm_runtime_get_sync(wl->dev);
87 if (ret < 0) {
88 pm_runtime_put_noidle(wl->dev);
89 goto out;
92 ret = wl1271_cmd_test(wl, buf, buf_len, answer);
93 if (ret < 0) {
94 wl1271_warning("testmode cmd test failed: %d", ret);
95 goto out_sleep;
98 if (answer) {
99 /* If we got bip calibration answer print radio status */
100 struct wl1271_cmd_cal_p2g *params =
101 (struct wl1271_cmd_cal_p2g *) buf;
103 s16 radio_status = (s16) le16_to_cpu(params->radio_status);
105 if (params->test.id == TEST_CMD_P2G_CAL &&
106 radio_status < 0)
107 wl1271_warning("testmode cmd: radio status=%d",
108 radio_status);
109 else
110 wl1271_info("testmode cmd: radio status=%d",
111 radio_status);
113 len = nla_total_size(buf_len);
114 skb = cfg80211_testmode_alloc_reply_skb(wl->hw->wiphy, len);
115 if (!skb) {
116 ret = -ENOMEM;
117 goto out_sleep;
120 if (nla_put(skb, WL1271_TM_ATTR_DATA, buf_len, buf)) {
121 kfree_skb(skb);
122 ret = -EMSGSIZE;
123 goto out_sleep;
126 ret = cfg80211_testmode_reply(skb);
127 if (ret < 0)
128 goto out_sleep;
131 out_sleep:
132 pm_runtime_mark_last_busy(wl->dev);
133 pm_runtime_put_autosuspend(wl->dev);
134 out:
135 mutex_unlock(&wl->mutex);
137 return ret;
140 static int wl1271_tm_cmd_interrogate(struct wl1271 *wl, struct nlattr *tb[])
142 int ret;
143 struct wl1271_command *cmd;
144 struct sk_buff *skb;
145 u8 ie_id;
147 wl1271_debug(DEBUG_TESTMODE, "testmode cmd interrogate");
149 if (!tb[WL1271_TM_ATTR_IE_ID])
150 return -EINVAL;
152 ie_id = nla_get_u8(tb[WL1271_TM_ATTR_IE_ID]);
154 mutex_lock(&wl->mutex);
156 if (unlikely(wl->state != WLCORE_STATE_ON)) {
157 ret = -EINVAL;
158 goto out;
161 ret = pm_runtime_get_sync(wl->dev);
162 if (ret < 0) {
163 pm_runtime_put_noidle(wl->dev);
164 goto out;
167 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
168 if (!cmd) {
169 ret = -ENOMEM;
170 goto out_sleep;
173 ret = wl1271_cmd_interrogate(wl, ie_id, cmd,
174 sizeof(struct acx_header), sizeof(*cmd));
175 if (ret < 0) {
176 wl1271_warning("testmode cmd interrogate failed: %d", ret);
177 goto out_free;
180 skb = cfg80211_testmode_alloc_reply_skb(wl->hw->wiphy, sizeof(*cmd));
181 if (!skb) {
182 ret = -ENOMEM;
183 goto out_free;
186 if (nla_put(skb, WL1271_TM_ATTR_DATA, sizeof(*cmd), cmd)) {
187 kfree_skb(skb);
188 ret = -EMSGSIZE;
189 goto out_free;
192 ret = cfg80211_testmode_reply(skb);
193 if (ret < 0)
194 goto out_free;
196 out_free:
197 kfree(cmd);
198 out_sleep:
199 pm_runtime_mark_last_busy(wl->dev);
200 pm_runtime_put_autosuspend(wl->dev);
201 out:
202 mutex_unlock(&wl->mutex);
204 return ret;
207 static int wl1271_tm_cmd_configure(struct wl1271 *wl, struct nlattr *tb[])
209 int buf_len, ret;
210 void *buf;
211 u8 ie_id;
213 wl1271_debug(DEBUG_TESTMODE, "testmode cmd configure");
215 if (!tb[WL1271_TM_ATTR_DATA])
216 return -EINVAL;
217 if (!tb[WL1271_TM_ATTR_IE_ID])
218 return -EINVAL;
220 ie_id = nla_get_u8(tb[WL1271_TM_ATTR_IE_ID]);
221 buf = nla_data(tb[WL1271_TM_ATTR_DATA]);
222 buf_len = nla_len(tb[WL1271_TM_ATTR_DATA]);
224 if (buf_len > sizeof(struct wl1271_command))
225 return -EMSGSIZE;
227 mutex_lock(&wl->mutex);
228 ret = wl1271_cmd_configure(wl, ie_id, buf, buf_len);
229 mutex_unlock(&wl->mutex);
231 if (ret < 0) {
232 wl1271_warning("testmode cmd configure failed: %d", ret);
233 return ret;
236 return 0;
239 static int wl1271_tm_detect_fem(struct wl1271 *wl, struct nlattr *tb[])
241 /* return FEM type */
242 int ret, len;
243 struct sk_buff *skb;
245 ret = wl1271_plt_start(wl, PLT_FEM_DETECT);
246 if (ret < 0)
247 goto out;
249 mutex_lock(&wl->mutex);
251 len = nla_total_size(sizeof(wl->fem_manuf));
252 skb = cfg80211_testmode_alloc_reply_skb(wl->hw->wiphy, len);
253 if (!skb) {
254 ret = -ENOMEM;
255 goto out_mutex;
258 if (nla_put(skb, WL1271_TM_ATTR_DATA, sizeof(wl->fem_manuf),
259 &wl->fem_manuf)) {
260 kfree_skb(skb);
261 ret = -EMSGSIZE;
262 goto out_mutex;
265 ret = cfg80211_testmode_reply(skb);
267 out_mutex:
268 mutex_unlock(&wl->mutex);
270 /* We always stop plt after DETECT mode */
271 wl1271_plt_stop(wl);
272 out:
273 return ret;
276 static int wl1271_tm_cmd_set_plt_mode(struct wl1271 *wl, struct nlattr *tb[])
278 u32 val;
279 int ret;
281 wl1271_debug(DEBUG_TESTMODE, "testmode cmd set plt mode");
283 if (!tb[WL1271_TM_ATTR_PLT_MODE])
284 return -EINVAL;
286 val = nla_get_u32(tb[WL1271_TM_ATTR_PLT_MODE]);
288 switch (val) {
289 case PLT_OFF:
290 ret = wl1271_plt_stop(wl);
291 break;
292 case PLT_ON:
293 case PLT_CHIP_AWAKE:
294 ret = wl1271_plt_start(wl, val);
295 break;
296 case PLT_FEM_DETECT:
297 ret = wl1271_tm_detect_fem(wl, tb);
298 break;
299 default:
300 ret = -EINVAL;
301 break;
304 return ret;
307 static int wl12xx_tm_cmd_get_mac(struct wl1271 *wl, struct nlattr *tb[])
309 struct sk_buff *skb;
310 u8 mac_addr[ETH_ALEN];
311 int ret = 0;
313 mutex_lock(&wl->mutex);
315 if (!wl->plt) {
316 ret = -EINVAL;
317 goto out;
320 if (wl->fuse_oui_addr == 0 && wl->fuse_nic_addr == 0) {
321 ret = -EOPNOTSUPP;
322 goto out;
325 mac_addr[0] = (u8)(wl->fuse_oui_addr >> 16);
326 mac_addr[1] = (u8)(wl->fuse_oui_addr >> 8);
327 mac_addr[2] = (u8) wl->fuse_oui_addr;
328 mac_addr[3] = (u8)(wl->fuse_nic_addr >> 16);
329 mac_addr[4] = (u8)(wl->fuse_nic_addr >> 8);
330 mac_addr[5] = (u8) wl->fuse_nic_addr;
332 skb = cfg80211_testmode_alloc_reply_skb(wl->hw->wiphy, ETH_ALEN);
333 if (!skb) {
334 ret = -ENOMEM;
335 goto out;
338 if (nla_put(skb, WL1271_TM_ATTR_DATA, ETH_ALEN, mac_addr)) {
339 kfree_skb(skb);
340 ret = -EMSGSIZE;
341 goto out;
344 ret = cfg80211_testmode_reply(skb);
345 if (ret < 0)
346 goto out;
348 out:
349 mutex_unlock(&wl->mutex);
350 return ret;
353 int wl1271_tm_cmd(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
354 void *data, int len)
356 struct wl1271 *wl = hw->priv;
357 struct nlattr *tb[WL1271_TM_ATTR_MAX + 1];
358 u32 nla_cmd;
359 int err;
361 err = nla_parse_deprecated(tb, WL1271_TM_ATTR_MAX, data, len,
362 wl1271_tm_policy, NULL);
363 if (err)
364 return err;
366 if (!tb[WL1271_TM_ATTR_CMD_ID])
367 return -EINVAL;
369 nla_cmd = nla_get_u32(tb[WL1271_TM_ATTR_CMD_ID]);
371 /* Only SET_PLT_MODE is allowed in case of mode PLT_CHIP_AWAKE */
372 if (wl->plt_mode == PLT_CHIP_AWAKE &&
373 nla_cmd != WL1271_TM_CMD_SET_PLT_MODE)
374 return -EOPNOTSUPP;
376 switch (nla_cmd) {
377 case WL1271_TM_CMD_TEST:
378 return wl1271_tm_cmd_test(wl, tb);
379 case WL1271_TM_CMD_INTERROGATE:
380 return wl1271_tm_cmd_interrogate(wl, tb);
381 case WL1271_TM_CMD_CONFIGURE:
382 return wl1271_tm_cmd_configure(wl, tb);
383 case WL1271_TM_CMD_SET_PLT_MODE:
384 return wl1271_tm_cmd_set_plt_mode(wl, tb);
385 case WL1271_TM_CMD_GET_MAC:
386 return wl12xx_tm_cmd_get_mac(wl, tb);
387 default:
388 return -EOPNOTSUPP;