blob: 25093c0cb0edf654ad04f603e3c1b7eb47603a1b [file] [log] [blame]
Kalle Valoc8c90872010-02-18 13:25:53 +02001/*
2 * This file is part of wl1271
3 *
4 * Copyright (C) 2010 Nokia Corporation
5 *
6 * Contact: Luciano Coelho <luciano.coelho@nokia.com>
7 *
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.
11 *
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.
16 *
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
21 *
22 */
Shahar Levi00d20102010-11-08 11:20:10 +000023#include "testmode.h"
Kalle Valoc8c90872010-02-18 13:25:53 +020024
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Kalle Valoc8c90872010-02-18 13:25:53 +020026#include <net/genetlink.h>
27
Shahar Levi00d20102010-11-08 11:20:10 +000028#include "wl12xx.h"
Luciano Coelho0f4e3122011-10-07 11:02:42 +030029#include "debug.h"
Shahar Levi00d20102010-11-08 11:20:10 +000030#include "acx.h"
Shahar Levibc765bf2011-03-06 16:32:10 +020031#include "reg.h"
Eliad Pellerabc47472011-12-06 12:15:05 +020032#include "ps.h"
Kalle Valoc8c90872010-02-18 13:25:53 +020033
34#define WL1271_TM_MAX_DATA_LENGTH 1024
35
36enum wl1271_tm_commands {
37 WL1271_TM_CMD_UNSPEC,
38 WL1271_TM_CMD_TEST,
39 WL1271_TM_CMD_INTERROGATE,
40 WL1271_TM_CMD_CONFIGURE,
Pontus Fuchs3f176492011-12-01 12:13:44 +010041 WL1271_TM_CMD_NVS_PUSH, /* Not in use. Keep to not break ABI */
Kalle Valoc8c90872010-02-18 13:25:53 +020042 WL1271_TM_CMD_SET_PLT_MODE,
Eliad Pellere285a522010-10-27 14:09:58 +020043 WL1271_TM_CMD_RECOVER,
Kalle Valoc8c90872010-02-18 13:25:53 +020044
45 __WL1271_TM_CMD_AFTER_LAST
46};
47#define WL1271_TM_CMD_MAX (__WL1271_TM_CMD_AFTER_LAST - 1)
48
49enum wl1271_tm_attrs {
50 WL1271_TM_ATTR_UNSPEC,
51 WL1271_TM_ATTR_CMD_ID,
52 WL1271_TM_ATTR_ANSWER,
53 WL1271_TM_ATTR_DATA,
54 WL1271_TM_ATTR_IE_ID,
55 WL1271_TM_ATTR_PLT_MODE,
56
57 __WL1271_TM_ATTR_AFTER_LAST
58};
59#define WL1271_TM_ATTR_MAX (__WL1271_TM_ATTR_AFTER_LAST - 1)
60
61static struct nla_policy wl1271_tm_policy[WL1271_TM_ATTR_MAX + 1] = {
62 [WL1271_TM_ATTR_CMD_ID] = { .type = NLA_U32 },
63 [WL1271_TM_ATTR_ANSWER] = { .type = NLA_U8 },
64 [WL1271_TM_ATTR_DATA] = { .type = NLA_BINARY,
65 .len = WL1271_TM_MAX_DATA_LENGTH },
66 [WL1271_TM_ATTR_IE_ID] = { .type = NLA_U32 },
67 [WL1271_TM_ATTR_PLT_MODE] = { .type = NLA_U32 },
68};
69
70
71static int wl1271_tm_cmd_test(struct wl1271 *wl, struct nlattr *tb[])
72{
73 int buf_len, ret, len;
74 struct sk_buff *skb;
75 void *buf;
76 u8 answer = 0;
77
78 wl1271_debug(DEBUG_TESTMODE, "testmode cmd test");
79
80 if (!tb[WL1271_TM_ATTR_DATA])
81 return -EINVAL;
82
83 buf = nla_data(tb[WL1271_TM_ATTR_DATA]);
84 buf_len = nla_len(tb[WL1271_TM_ATTR_DATA]);
85
86 if (tb[WL1271_TM_ATTR_ANSWER])
87 answer = nla_get_u8(tb[WL1271_TM_ATTR_ANSWER]);
88
89 if (buf_len > sizeof(struct wl1271_command))
90 return -EMSGSIZE;
91
92 mutex_lock(&wl->mutex);
Kalle Valoc8c90872010-02-18 13:25:53 +020093
Eliad Pellerabc47472011-12-06 12:15:05 +020094 if (wl->state == WL1271_STATE_OFF) {
95 ret = -EINVAL;
96 goto out;
97 }
98
99 ret = wl1271_ps_elp_wakeup(wl);
100 if (ret < 0)
101 goto out;
102
103 ret = wl1271_cmd_test(wl, buf, buf_len, answer);
Kalle Valoc8c90872010-02-18 13:25:53 +0200104 if (ret < 0) {
105 wl1271_warning("testmode cmd test failed: %d", ret);
Eliad Pellerabc47472011-12-06 12:15:05 +0200106 goto out_sleep;
Kalle Valoc8c90872010-02-18 13:25:53 +0200107 }
108
109 if (answer) {
110 len = nla_total_size(buf_len);
111 skb = cfg80211_testmode_alloc_reply_skb(wl->hw->wiphy, len);
Eliad Pellerabc47472011-12-06 12:15:05 +0200112 if (!skb) {
113 ret = -ENOMEM;
114 goto out_sleep;
115 }
Kalle Valoc8c90872010-02-18 13:25:53 +0200116
117 NLA_PUT(skb, WL1271_TM_ATTR_DATA, buf_len, buf);
118 ret = cfg80211_testmode_reply(skb);
119 if (ret < 0)
Eliad Pellerabc47472011-12-06 12:15:05 +0200120 goto out_sleep;
Kalle Valoc8c90872010-02-18 13:25:53 +0200121 }
122
Eliad Pellerabc47472011-12-06 12:15:05 +0200123out_sleep:
124 wl1271_ps_elp_sleep(wl);
125out:
126 mutex_unlock(&wl->mutex);
127
128 return ret;
Kalle Valoc8c90872010-02-18 13:25:53 +0200129
130nla_put_failure:
131 kfree_skb(skb);
Eliad Pellerabc47472011-12-06 12:15:05 +0200132 ret = -EMSGSIZE;
133 goto out_sleep;
Kalle Valoc8c90872010-02-18 13:25:53 +0200134}
135
136static int wl1271_tm_cmd_interrogate(struct wl1271 *wl, struct nlattr *tb[])
137{
138 int ret;
139 struct wl1271_command *cmd;
140 struct sk_buff *skb;
141 u8 ie_id;
142
143 wl1271_debug(DEBUG_TESTMODE, "testmode cmd interrogate");
144
145 if (!tb[WL1271_TM_ATTR_IE_ID])
146 return -EINVAL;
147
148 ie_id = nla_get_u8(tb[WL1271_TM_ATTR_IE_ID]);
149
Kalle Valoc8c90872010-02-18 13:25:53 +0200150 mutex_lock(&wl->mutex);
Kalle Valoc8c90872010-02-18 13:25:53 +0200151
Eliad Pellerabc47472011-12-06 12:15:05 +0200152 if (wl->state == WL1271_STATE_OFF) {
153 ret = -EINVAL;
154 goto out;
155 }
156
157 ret = wl1271_ps_elp_wakeup(wl);
158 if (ret < 0)
159 goto out;
160
161 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
162 if (!cmd) {
163 ret = -ENOMEM;
164 goto out_sleep;
165 }
166
167 ret = wl1271_cmd_interrogate(wl, ie_id, cmd, sizeof(*cmd));
Kalle Valoc8c90872010-02-18 13:25:53 +0200168 if (ret < 0) {
169 wl1271_warning("testmode cmd interrogate failed: %d", ret);
Eliad Pellerabc47472011-12-06 12:15:05 +0200170 goto out_free;
Kalle Valoc8c90872010-02-18 13:25:53 +0200171 }
172
173 skb = cfg80211_testmode_alloc_reply_skb(wl->hw->wiphy, sizeof(*cmd));
Julia Lawallf8afdf42011-08-12 13:31:30 -0400174 if (!skb) {
Eliad Pellerabc47472011-12-06 12:15:05 +0200175 ret = -ENOMEM;
176 goto out_free;
Julia Lawallf8afdf42011-08-12 13:31:30 -0400177 }
Kalle Valoc8c90872010-02-18 13:25:53 +0200178
179 NLA_PUT(skb, WL1271_TM_ATTR_DATA, sizeof(*cmd), cmd);
Eliad Peller3dbb5842011-12-06 12:15:07 +0200180 ret = cfg80211_testmode_reply(skb);
181 if (ret < 0)
182 goto out_free;
Kalle Valoc8c90872010-02-18 13:25:53 +0200183
Eliad Pellerabc47472011-12-06 12:15:05 +0200184out_free:
185 kfree(cmd);
186out_sleep:
187 wl1271_ps_elp_sleep(wl);
188out:
189 mutex_unlock(&wl->mutex);
190
191 return ret;
Kalle Valoc8c90872010-02-18 13:25:53 +0200192
193nla_put_failure:
194 kfree_skb(skb);
Eliad Pellerabc47472011-12-06 12:15:05 +0200195 ret = -EMSGSIZE;
196 goto out_free;
Kalle Valoc8c90872010-02-18 13:25:53 +0200197}
198
199static int wl1271_tm_cmd_configure(struct wl1271 *wl, struct nlattr *tb[])
200{
201 int buf_len, ret;
202 void *buf;
203 u8 ie_id;
204
205 wl1271_debug(DEBUG_TESTMODE, "testmode cmd configure");
206
207 if (!tb[WL1271_TM_ATTR_DATA])
208 return -EINVAL;
209 if (!tb[WL1271_TM_ATTR_IE_ID])
210 return -EINVAL;
211
212 ie_id = nla_get_u8(tb[WL1271_TM_ATTR_IE_ID]);
213 buf = nla_data(tb[WL1271_TM_ATTR_DATA]);
214 buf_len = nla_len(tb[WL1271_TM_ATTR_DATA]);
215
216 if (buf_len > sizeof(struct wl1271_command))
217 return -EMSGSIZE;
218
219 mutex_lock(&wl->mutex);
220 ret = wl1271_cmd_configure(wl, ie_id, buf, buf_len);
221 mutex_unlock(&wl->mutex);
222
223 if (ret < 0) {
224 wl1271_warning("testmode cmd configure failed: %d", ret);
225 return ret;
226 }
227
228 return 0;
229}
230
Kalle Valoc8c90872010-02-18 13:25:53 +0200231static int wl1271_tm_cmd_set_plt_mode(struct wl1271 *wl, struct nlattr *tb[])
232{
233 u32 val;
234 int ret;
235
236 wl1271_debug(DEBUG_TESTMODE, "testmode cmd set plt mode");
237
238 if (!tb[WL1271_TM_ATTR_PLT_MODE])
239 return -EINVAL;
240
241 val = nla_get_u32(tb[WL1271_TM_ATTR_PLT_MODE]);
242
243 switch (val) {
244 case 0:
245 ret = wl1271_plt_stop(wl);
246 break;
247 case 1:
248 ret = wl1271_plt_start(wl);
249 break;
250 default:
251 ret = -EINVAL;
252 break;
253 }
254
255 return ret;
256}
257
Eliad Pellere285a522010-10-27 14:09:58 +0200258static int wl1271_tm_cmd_recover(struct wl1271 *wl, struct nlattr *tb[])
259{
260 wl1271_debug(DEBUG_TESTMODE, "testmode cmd recover");
261
Ido Yarivbaacb9ae2011-06-06 14:57:05 +0300262 wl12xx_queue_recovery_work(wl);
Eliad Pellere285a522010-10-27 14:09:58 +0200263
264 return 0;
265}
266
Kalle Valoc8c90872010-02-18 13:25:53 +0200267int wl1271_tm_cmd(struct ieee80211_hw *hw, void *data, int len)
268{
269 struct wl1271 *wl = hw->priv;
270 struct nlattr *tb[WL1271_TM_ATTR_MAX + 1];
271 int err;
272
273 err = nla_parse(tb, WL1271_TM_ATTR_MAX, data, len, wl1271_tm_policy);
274 if (err)
275 return err;
276
277 if (!tb[WL1271_TM_ATTR_CMD_ID])
278 return -EINVAL;
279
280 switch (nla_get_u32(tb[WL1271_TM_ATTR_CMD_ID])) {
281 case WL1271_TM_CMD_TEST:
282 return wl1271_tm_cmd_test(wl, tb);
283 case WL1271_TM_CMD_INTERROGATE:
284 return wl1271_tm_cmd_interrogate(wl, tb);
285 case WL1271_TM_CMD_CONFIGURE:
286 return wl1271_tm_cmd_configure(wl, tb);
Kalle Valoc8c90872010-02-18 13:25:53 +0200287 case WL1271_TM_CMD_SET_PLT_MODE:
288 return wl1271_tm_cmd_set_plt_mode(wl, tb);
Eliad Pellere285a522010-10-27 14:09:58 +0200289 case WL1271_TM_CMD_RECOVER:
290 return wl1271_tm_cmd_recover(wl, tb);
Kalle Valoc8c90872010-02-18 13:25:53 +0200291 default:
292 return -EOPNOTSUPP;
293 }
294}