blob: 2f9ebff2ded3ae007ddf37dd3ff7636350849544 [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,
Kalle Valoc8c90872010-02-18 13:25:53 +020041 WL1271_TM_CMD_SET_PLT_MODE,
Eliad Pellere285a522010-10-27 14:09:58 +020042 WL1271_TM_CMD_RECOVER,
Kalle Valoc8c90872010-02-18 13:25:53 +020043
44 __WL1271_TM_CMD_AFTER_LAST
45};
46#define WL1271_TM_CMD_MAX (__WL1271_TM_CMD_AFTER_LAST - 1)
47
48enum wl1271_tm_attrs {
49 WL1271_TM_ATTR_UNSPEC,
50 WL1271_TM_ATTR_CMD_ID,
51 WL1271_TM_ATTR_ANSWER,
52 WL1271_TM_ATTR_DATA,
53 WL1271_TM_ATTR_IE_ID,
54 WL1271_TM_ATTR_PLT_MODE,
55
56 __WL1271_TM_ATTR_AFTER_LAST
57};
58#define WL1271_TM_ATTR_MAX (__WL1271_TM_ATTR_AFTER_LAST - 1)
59
60static struct nla_policy wl1271_tm_policy[WL1271_TM_ATTR_MAX + 1] = {
61 [WL1271_TM_ATTR_CMD_ID] = { .type = NLA_U32 },
62 [WL1271_TM_ATTR_ANSWER] = { .type = NLA_U8 },
63 [WL1271_TM_ATTR_DATA] = { .type = NLA_BINARY,
64 .len = WL1271_TM_MAX_DATA_LENGTH },
65 [WL1271_TM_ATTR_IE_ID] = { .type = NLA_U32 },
66 [WL1271_TM_ATTR_PLT_MODE] = { .type = NLA_U32 },
67};
68
69
70static int wl1271_tm_cmd_test(struct wl1271 *wl, struct nlattr *tb[])
71{
72 int buf_len, ret, len;
73 struct sk_buff *skb;
74 void *buf;
75 u8 answer = 0;
76
77 wl1271_debug(DEBUG_TESTMODE, "testmode cmd test");
78
79 if (!tb[WL1271_TM_ATTR_DATA])
80 return -EINVAL;
81
82 buf = nla_data(tb[WL1271_TM_ATTR_DATA]);
83 buf_len = nla_len(tb[WL1271_TM_ATTR_DATA]);
84
85 if (tb[WL1271_TM_ATTR_ANSWER])
86 answer = nla_get_u8(tb[WL1271_TM_ATTR_ANSWER]);
87
88 if (buf_len > sizeof(struct wl1271_command))
89 return -EMSGSIZE;
90
91 mutex_lock(&wl->mutex);
Kalle Valoc8c90872010-02-18 13:25:53 +020092
Eliad Pellerabc47472011-12-06 12:15:05 +020093 if (wl->state == WL1271_STATE_OFF) {
94 ret = -EINVAL;
95 goto out;
96 }
97
98 ret = wl1271_ps_elp_wakeup(wl);
99 if (ret < 0)
100 goto out;
101
102 ret = wl1271_cmd_test(wl, buf, buf_len, answer);
Kalle Valoc8c90872010-02-18 13:25:53 +0200103 if (ret < 0) {
104 wl1271_warning("testmode cmd test failed: %d", ret);
Eliad Pellerabc47472011-12-06 12:15:05 +0200105 goto out_sleep;
Kalle Valoc8c90872010-02-18 13:25:53 +0200106 }
107
108 if (answer) {
109 len = nla_total_size(buf_len);
110 skb = cfg80211_testmode_alloc_reply_skb(wl->hw->wiphy, len);
Eliad Pellerabc47472011-12-06 12:15:05 +0200111 if (!skb) {
112 ret = -ENOMEM;
113 goto out_sleep;
114 }
Kalle Valoc8c90872010-02-18 13:25:53 +0200115
116 NLA_PUT(skb, WL1271_TM_ATTR_DATA, buf_len, buf);
117 ret = cfg80211_testmode_reply(skb);
118 if (ret < 0)
Eliad Pellerabc47472011-12-06 12:15:05 +0200119 goto out_sleep;
Kalle Valoc8c90872010-02-18 13:25:53 +0200120 }
121
Eliad Pellerabc47472011-12-06 12:15:05 +0200122out_sleep:
123 wl1271_ps_elp_sleep(wl);
124out:
125 mutex_unlock(&wl->mutex);
126
127 return ret;
Kalle Valoc8c90872010-02-18 13:25:53 +0200128
129nla_put_failure:
130 kfree_skb(skb);
Eliad Pellerabc47472011-12-06 12:15:05 +0200131 ret = -EMSGSIZE;
132 goto out_sleep;
Kalle Valoc8c90872010-02-18 13:25:53 +0200133}
134
135static int wl1271_tm_cmd_interrogate(struct wl1271 *wl, struct nlattr *tb[])
136{
137 int ret;
138 struct wl1271_command *cmd;
139 struct sk_buff *skb;
140 u8 ie_id;
141
142 wl1271_debug(DEBUG_TESTMODE, "testmode cmd interrogate");
143
144 if (!tb[WL1271_TM_ATTR_IE_ID])
145 return -EINVAL;
146
147 ie_id = nla_get_u8(tb[WL1271_TM_ATTR_IE_ID]);
148
Kalle Valoc8c90872010-02-18 13:25:53 +0200149 mutex_lock(&wl->mutex);
Kalle Valoc8c90872010-02-18 13:25:53 +0200150
Eliad Pellerabc47472011-12-06 12:15:05 +0200151 if (wl->state == WL1271_STATE_OFF) {
152 ret = -EINVAL;
153 goto out;
154 }
155
156 ret = wl1271_ps_elp_wakeup(wl);
157 if (ret < 0)
158 goto out;
159
160 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
161 if (!cmd) {
162 ret = -ENOMEM;
163 goto out_sleep;
164 }
165
166 ret = wl1271_cmd_interrogate(wl, ie_id, cmd, sizeof(*cmd));
Kalle Valoc8c90872010-02-18 13:25:53 +0200167 if (ret < 0) {
168 wl1271_warning("testmode cmd interrogate failed: %d", ret);
Eliad Pellerabc47472011-12-06 12:15:05 +0200169 goto out_free;
Kalle Valoc8c90872010-02-18 13:25:53 +0200170 }
171
172 skb = cfg80211_testmode_alloc_reply_skb(wl->hw->wiphy, sizeof(*cmd));
Julia Lawallf8afdf42011-08-12 13:31:30 -0400173 if (!skb) {
Eliad Pellerabc47472011-12-06 12:15:05 +0200174 ret = -ENOMEM;
175 goto out_free;
Julia Lawallf8afdf42011-08-12 13:31:30 -0400176 }
Kalle Valoc8c90872010-02-18 13:25:53 +0200177
178 NLA_PUT(skb, WL1271_TM_ATTR_DATA, sizeof(*cmd), cmd);
179
Eliad Pellerabc47472011-12-06 12:15:05 +0200180out_free:
181 kfree(cmd);
182out_sleep:
183 wl1271_ps_elp_sleep(wl);
184out:
185 mutex_unlock(&wl->mutex);
186
187 return ret;
Kalle Valoc8c90872010-02-18 13:25:53 +0200188
189nla_put_failure:
190 kfree_skb(skb);
Eliad Pellerabc47472011-12-06 12:15:05 +0200191 ret = -EMSGSIZE;
192 goto out_free;
Kalle Valoc8c90872010-02-18 13:25:53 +0200193}
194
195static int wl1271_tm_cmd_configure(struct wl1271 *wl, struct nlattr *tb[])
196{
197 int buf_len, ret;
198 void *buf;
199 u8 ie_id;
200
201 wl1271_debug(DEBUG_TESTMODE, "testmode cmd configure");
202
203 if (!tb[WL1271_TM_ATTR_DATA])
204 return -EINVAL;
205 if (!tb[WL1271_TM_ATTR_IE_ID])
206 return -EINVAL;
207
208 ie_id = nla_get_u8(tb[WL1271_TM_ATTR_IE_ID]);
209 buf = nla_data(tb[WL1271_TM_ATTR_DATA]);
210 buf_len = nla_len(tb[WL1271_TM_ATTR_DATA]);
211
212 if (buf_len > sizeof(struct wl1271_command))
213 return -EMSGSIZE;
214
215 mutex_lock(&wl->mutex);
216 ret = wl1271_cmd_configure(wl, ie_id, buf, buf_len);
217 mutex_unlock(&wl->mutex);
218
219 if (ret < 0) {
220 wl1271_warning("testmode cmd configure failed: %d", ret);
221 return ret;
222 }
223
224 return 0;
225}
226
Kalle Valoc8c90872010-02-18 13:25:53 +0200227static int wl1271_tm_cmd_set_plt_mode(struct wl1271 *wl, struct nlattr *tb[])
228{
229 u32 val;
230 int ret;
231
232 wl1271_debug(DEBUG_TESTMODE, "testmode cmd set plt mode");
233
234 if (!tb[WL1271_TM_ATTR_PLT_MODE])
235 return -EINVAL;
236
237 val = nla_get_u32(tb[WL1271_TM_ATTR_PLT_MODE]);
238
239 switch (val) {
240 case 0:
241 ret = wl1271_plt_stop(wl);
242 break;
243 case 1:
244 ret = wl1271_plt_start(wl);
245 break;
246 default:
247 ret = -EINVAL;
248 break;
249 }
250
251 return ret;
252}
253
Eliad Pellere285a522010-10-27 14:09:58 +0200254static int wl1271_tm_cmd_recover(struct wl1271 *wl, struct nlattr *tb[])
255{
256 wl1271_debug(DEBUG_TESTMODE, "testmode cmd recover");
257
Ido Yarivbaacb9a2011-06-06 14:57:05 +0300258 wl12xx_queue_recovery_work(wl);
Eliad Pellere285a522010-10-27 14:09:58 +0200259
260 return 0;
261}
262
Kalle Valoc8c90872010-02-18 13:25:53 +0200263int wl1271_tm_cmd(struct ieee80211_hw *hw, void *data, int len)
264{
265 struct wl1271 *wl = hw->priv;
266 struct nlattr *tb[WL1271_TM_ATTR_MAX + 1];
267 int err;
268
269 err = nla_parse(tb, WL1271_TM_ATTR_MAX, data, len, wl1271_tm_policy);
270 if (err)
271 return err;
272
273 if (!tb[WL1271_TM_ATTR_CMD_ID])
274 return -EINVAL;
275
276 switch (nla_get_u32(tb[WL1271_TM_ATTR_CMD_ID])) {
277 case WL1271_TM_CMD_TEST:
278 return wl1271_tm_cmd_test(wl, tb);
279 case WL1271_TM_CMD_INTERROGATE:
280 return wl1271_tm_cmd_interrogate(wl, tb);
281 case WL1271_TM_CMD_CONFIGURE:
282 return wl1271_tm_cmd_configure(wl, tb);
Kalle Valoc8c90872010-02-18 13:25:53 +0200283 case WL1271_TM_CMD_SET_PLT_MODE:
284 return wl1271_tm_cmd_set_plt_mode(wl, tb);
Eliad Pellere285a522010-10-27 14:09:58 +0200285 case WL1271_TM_CMD_RECOVER:
286 return wl1271_tm_cmd_recover(wl, tb);
Kalle Valoc8c90872010-02-18 13:25:53 +0200287 default:
288 return -EOPNOTSUPP;
289 }
290}