sfc: Don't use enums as a bitmask.
[zen-stable.git] / drivers / net / wireless / wl12xx / testmode.c
blobda351d7cd1f29ff3772f53ac27c2faad561e560d
1 /*
2 * This file is part of wl1271
4 * Copyright (C) 2010 Nokia Corporation
6 * Contact: Luciano Coelho <luciano.coelho@nokia.com>
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.
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.
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
23 #include "testmode.h"
25 #include <linux/slab.h>
26 #include <net/genetlink.h>
28 #include "wl12xx.h"
29 #include "acx.h"
30 #include "reg.h"
32 #define WL1271_TM_MAX_DATA_LENGTH 1024
34 enum wl1271_tm_commands {
35 WL1271_TM_CMD_UNSPEC,
36 WL1271_TM_CMD_TEST,
37 WL1271_TM_CMD_INTERROGATE,
38 WL1271_TM_CMD_CONFIGURE,
39 WL1271_TM_CMD_NVS_PUSH,
40 WL1271_TM_CMD_SET_PLT_MODE,
41 WL1271_TM_CMD_RECOVER,
43 __WL1271_TM_CMD_AFTER_LAST
45 #define WL1271_TM_CMD_MAX (__WL1271_TM_CMD_AFTER_LAST - 1)
47 enum wl1271_tm_attrs {
48 WL1271_TM_ATTR_UNSPEC,
49 WL1271_TM_ATTR_CMD_ID,
50 WL1271_TM_ATTR_ANSWER,
51 WL1271_TM_ATTR_DATA,
52 WL1271_TM_ATTR_IE_ID,
53 WL1271_TM_ATTR_PLT_MODE,
55 __WL1271_TM_ATTR_AFTER_LAST
57 #define WL1271_TM_ATTR_MAX (__WL1271_TM_ATTR_AFTER_LAST - 1)
59 static struct nla_policy wl1271_tm_policy[WL1271_TM_ATTR_MAX + 1] = {
60 [WL1271_TM_ATTR_CMD_ID] = { .type = NLA_U32 },
61 [WL1271_TM_ATTR_ANSWER] = { .type = NLA_U8 },
62 [WL1271_TM_ATTR_DATA] = { .type = NLA_BINARY,
63 .len = WL1271_TM_MAX_DATA_LENGTH },
64 [WL1271_TM_ATTR_IE_ID] = { .type = NLA_U32 },
65 [WL1271_TM_ATTR_PLT_MODE] = { .type = NLA_U32 },
69 static int wl1271_tm_cmd_test(struct wl1271 *wl, struct nlattr *tb[])
71 int buf_len, ret, len;
72 struct sk_buff *skb;
73 void *buf;
74 u8 answer = 0;
76 wl1271_debug(DEBUG_TESTMODE, "testmode cmd test");
78 if (!tb[WL1271_TM_ATTR_DATA])
79 return -EINVAL;
81 buf = nla_data(tb[WL1271_TM_ATTR_DATA]);
82 buf_len = nla_len(tb[WL1271_TM_ATTR_DATA]);
84 if (tb[WL1271_TM_ATTR_ANSWER])
85 answer = nla_get_u8(tb[WL1271_TM_ATTR_ANSWER]);
87 if (buf_len > sizeof(struct wl1271_command))
88 return -EMSGSIZE;
90 mutex_lock(&wl->mutex);
91 ret = wl1271_cmd_test(wl, buf, buf_len, answer);
92 mutex_unlock(&wl->mutex);
94 if (ret < 0) {
95 wl1271_warning("testmode cmd test failed: %d", ret);
96 return ret;
99 if (answer) {
100 len = nla_total_size(buf_len);
101 skb = cfg80211_testmode_alloc_reply_skb(wl->hw->wiphy, len);
102 if (!skb)
103 return -ENOMEM;
105 NLA_PUT(skb, WL1271_TM_ATTR_DATA, buf_len, buf);
106 ret = cfg80211_testmode_reply(skb);
107 if (ret < 0)
108 return ret;
111 return 0;
113 nla_put_failure:
114 kfree_skb(skb);
115 return -EMSGSIZE;
118 static int wl1271_tm_cmd_interrogate(struct wl1271 *wl, struct nlattr *tb[])
120 int ret;
121 struct wl1271_command *cmd;
122 struct sk_buff *skb;
123 u8 ie_id;
125 wl1271_debug(DEBUG_TESTMODE, "testmode cmd interrogate");
127 if (!tb[WL1271_TM_ATTR_IE_ID])
128 return -EINVAL;
130 ie_id = nla_get_u8(tb[WL1271_TM_ATTR_IE_ID]);
132 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
133 if (!cmd)
134 return -ENOMEM;
136 mutex_lock(&wl->mutex);
137 ret = wl1271_cmd_interrogate(wl, ie_id, cmd, sizeof(*cmd));
138 mutex_unlock(&wl->mutex);
140 if (ret < 0) {
141 wl1271_warning("testmode cmd interrogate failed: %d", ret);
142 return ret;
145 skb = cfg80211_testmode_alloc_reply_skb(wl->hw->wiphy, sizeof(*cmd));
146 if (!skb)
147 return -ENOMEM;
149 NLA_PUT(skb, WL1271_TM_ATTR_DATA, sizeof(*cmd), cmd);
151 return 0;
153 nla_put_failure:
154 kfree_skb(skb);
155 return -EMSGSIZE;
158 static int wl1271_tm_cmd_configure(struct wl1271 *wl, struct nlattr *tb[])
160 int buf_len, ret;
161 void *buf;
162 u8 ie_id;
164 wl1271_debug(DEBUG_TESTMODE, "testmode cmd configure");
166 if (!tb[WL1271_TM_ATTR_DATA])
167 return -EINVAL;
168 if (!tb[WL1271_TM_ATTR_IE_ID])
169 return -EINVAL;
171 ie_id = nla_get_u8(tb[WL1271_TM_ATTR_IE_ID]);
172 buf = nla_data(tb[WL1271_TM_ATTR_DATA]);
173 buf_len = nla_len(tb[WL1271_TM_ATTR_DATA]);
175 if (buf_len > sizeof(struct wl1271_command))
176 return -EMSGSIZE;
178 mutex_lock(&wl->mutex);
179 ret = wl1271_cmd_configure(wl, ie_id, buf, buf_len);
180 mutex_unlock(&wl->mutex);
182 if (ret < 0) {
183 wl1271_warning("testmode cmd configure failed: %d", ret);
184 return ret;
187 return 0;
190 static int wl1271_tm_cmd_nvs_push(struct wl1271 *wl, struct nlattr *tb[])
192 int ret = 0;
193 size_t len;
194 void *buf;
196 wl1271_debug(DEBUG_TESTMODE, "testmode cmd nvs push");
198 if (!tb[WL1271_TM_ATTR_DATA])
199 return -EINVAL;
201 buf = nla_data(tb[WL1271_TM_ATTR_DATA]);
202 len = nla_len(tb[WL1271_TM_ATTR_DATA]);
204 mutex_lock(&wl->mutex);
206 kfree(wl->nvs);
208 if ((wl->chip.id == CHIP_ID_1283_PG20) &&
209 (len != sizeof(struct wl128x_nvs_file)))
210 return -EINVAL;
211 else if (len != sizeof(struct wl1271_nvs_file))
212 return -EINVAL;
214 wl->nvs = kzalloc(len, GFP_KERNEL);
215 if (!wl->nvs) {
216 wl1271_error("could not allocate memory for the nvs file");
217 ret = -ENOMEM;
218 goto out;
221 memcpy(wl->nvs, buf, len);
222 wl->nvs_len = len;
224 wl1271_debug(DEBUG_TESTMODE, "testmode pushed nvs");
226 out:
227 mutex_unlock(&wl->mutex);
229 return ret;
232 static int wl1271_tm_cmd_set_plt_mode(struct wl1271 *wl, struct nlattr *tb[])
234 u32 val;
235 int ret;
237 wl1271_debug(DEBUG_TESTMODE, "testmode cmd set plt mode");
239 if (!tb[WL1271_TM_ATTR_PLT_MODE])
240 return -EINVAL;
242 val = nla_get_u32(tb[WL1271_TM_ATTR_PLT_MODE]);
244 switch (val) {
245 case 0:
246 ret = wl1271_plt_stop(wl);
247 break;
248 case 1:
249 ret = wl1271_plt_start(wl);
250 break;
251 default:
252 ret = -EINVAL;
253 break;
256 return ret;
259 static int wl1271_tm_cmd_recover(struct wl1271 *wl, struct nlattr *tb[])
261 wl1271_debug(DEBUG_TESTMODE, "testmode cmd recover");
263 ieee80211_queue_work(wl->hw, &wl->recovery_work);
265 return 0;
268 int wl1271_tm_cmd(struct ieee80211_hw *hw, void *data, int len)
270 struct wl1271 *wl = hw->priv;
271 struct nlattr *tb[WL1271_TM_ATTR_MAX + 1];
272 int err;
274 err = nla_parse(tb, WL1271_TM_ATTR_MAX, data, len, wl1271_tm_policy);
275 if (err)
276 return err;
278 if (!tb[WL1271_TM_ATTR_CMD_ID])
279 return -EINVAL;
281 switch (nla_get_u32(tb[WL1271_TM_ATTR_CMD_ID])) {
282 case WL1271_TM_CMD_TEST:
283 return wl1271_tm_cmd_test(wl, tb);
284 case WL1271_TM_CMD_INTERROGATE:
285 return wl1271_tm_cmd_interrogate(wl, tb);
286 case WL1271_TM_CMD_CONFIGURE:
287 return wl1271_tm_cmd_configure(wl, tb);
288 case WL1271_TM_CMD_NVS_PUSH:
289 return wl1271_tm_cmd_nvs_push(wl, tb);
290 case WL1271_TM_CMD_SET_PLT_MODE:
291 return wl1271_tm_cmd_set_plt_mode(wl, tb);
292 case WL1271_TM_CMD_RECOVER:
293 return wl1271_tm_cmd_recover(wl, tb);
294 default:
295 return -EOPNOTSUPP;