blob: 978cf2de713dce858d45cfef119eace285a0c37e [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);
Eliad Peller3dbb5842011-12-06 12:15:07 +0200179 ret = cfg80211_testmode_reply(skb);
180 if (ret < 0)
181 goto out_free;
Kalle Valoc8c90872010-02-18 13:25:53 +0200182
Eliad Pellerabc47472011-12-06 12:15:05 +0200183out_free:
184 kfree(cmd);
185out_sleep:
186 wl1271_ps_elp_sleep(wl);
187out:
188 mutex_unlock(&wl->mutex);
189
190 return ret;
Kalle Valoc8c90872010-02-18 13:25:53 +0200191
192nla_put_failure:
193 kfree_skb(skb);
Eliad Pellerabc47472011-12-06 12:15:05 +0200194 ret = -EMSGSIZE;
195 goto out_free;
Kalle Valoc8c90872010-02-18 13:25:53 +0200196}
197
198static int wl1271_tm_cmd_configure(struct wl1271 *wl, struct nlattr *tb[])
199{
200 int buf_len, ret;
201 void *buf;
202 u8 ie_id;
203
204 wl1271_debug(DEBUG_TESTMODE, "testmode cmd configure");
205
206 if (!tb[WL1271_TM_ATTR_DATA])
207 return -EINVAL;
208 if (!tb[WL1271_TM_ATTR_IE_ID])
209 return -EINVAL;
210
211 ie_id = nla_get_u8(tb[WL1271_TM_ATTR_IE_ID]);
212 buf = nla_data(tb[WL1271_TM_ATTR_DATA]);
213 buf_len = nla_len(tb[WL1271_TM_ATTR_DATA]);
214
215 if (buf_len > sizeof(struct wl1271_command))
216 return -EMSGSIZE;
217
218 mutex_lock(&wl->mutex);
219 ret = wl1271_cmd_configure(wl, ie_id, buf, buf_len);
220 mutex_unlock(&wl->mutex);
221
222 if (ret < 0) {
223 wl1271_warning("testmode cmd configure failed: %d", ret);
224 return ret;
225 }
226
227 return 0;
228}
229
Kalle Valoc8c90872010-02-18 13:25:53 +0200230static int wl1271_tm_cmd_set_plt_mode(struct wl1271 *wl, struct nlattr *tb[])
231{
232 u32 val;
233 int ret;
234
235 wl1271_debug(DEBUG_TESTMODE, "testmode cmd set plt mode");
236
237 if (!tb[WL1271_TM_ATTR_PLT_MODE])
238 return -EINVAL;
239
240 val = nla_get_u32(tb[WL1271_TM_ATTR_PLT_MODE]);
241
242 switch (val) {
243 case 0:
244 ret = wl1271_plt_stop(wl);
245 break;
246 case 1:
247 ret = wl1271_plt_start(wl);
248 break;
249 default:
250 ret = -EINVAL;
251 break;
252 }
253
254 return ret;
255}
256
Eliad Pellere285a522010-10-27 14:09:58 +0200257static int wl1271_tm_cmd_recover(struct wl1271 *wl, struct nlattr *tb[])
258{
259 wl1271_debug(DEBUG_TESTMODE, "testmode cmd recover");
260
Ido Yarivbaacb9a2011-06-06 14:57:05 +0300261 wl12xx_queue_recovery_work(wl);
Eliad Pellere285a522010-10-27 14:09:58 +0200262
263 return 0;
264}
265
Kalle Valoc8c90872010-02-18 13:25:53 +0200266int wl1271_tm_cmd(struct ieee80211_hw *hw, void *data, int len)
267{
268 struct wl1271 *wl = hw->priv;
269 struct nlattr *tb[WL1271_TM_ATTR_MAX + 1];
270 int err;
271
272 err = nla_parse(tb, WL1271_TM_ATTR_MAX, data, len, wl1271_tm_policy);
273 if (err)
274 return err;
275
276 if (!tb[WL1271_TM_ATTR_CMD_ID])
277 return -EINVAL;
278
279 switch (nla_get_u32(tb[WL1271_TM_ATTR_CMD_ID])) {
280 case WL1271_TM_CMD_TEST:
281 return wl1271_tm_cmd_test(wl, tb);
282 case WL1271_TM_CMD_INTERROGATE:
283 return wl1271_tm_cmd_interrogate(wl, tb);
284 case WL1271_TM_CMD_CONFIGURE:
285 return wl1271_tm_cmd_configure(wl, tb);
Kalle Valoc8c90872010-02-18 13:25:53 +0200286 case WL1271_TM_CMD_SET_PLT_MODE:
287 return wl1271_tm_cmd_set_plt_mode(wl, tb);
Eliad Pellere285a522010-10-27 14:09:58 +0200288 case WL1271_TM_CMD_RECOVER:
289 return wl1271_tm_cmd_recover(wl, tb);
Kalle Valoc8c90872010-02-18 13:25:53 +0200290 default:
291 return -EOPNOTSUPP;
292 }
293}