Kalle Valo | c8c9087 | 2010-02-18 13:25:53 +0200 | [diff] [blame] | 1 | /* |
| 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 Levi | 00d2010 | 2010-11-08 11:20:10 +0000 | [diff] [blame] | 23 | #include "testmode.h" |
Kalle Valo | c8c9087 | 2010-02-18 13:25:53 +0200 | [diff] [blame] | 24 | |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 25 | #include <linux/slab.h> |
Kalle Valo | c8c9087 | 2010-02-18 13:25:53 +0200 | [diff] [blame] | 26 | #include <net/genetlink.h> |
| 27 | |
Shahar Levi | 00d2010 | 2010-11-08 11:20:10 +0000 | [diff] [blame] | 28 | #include "wl12xx.h" |
Luciano Coelho | 0f4e312 | 2011-10-07 11:02:42 +0300 | [diff] [blame] | 29 | #include "debug.h" |
Shahar Levi | 00d2010 | 2010-11-08 11:20:10 +0000 | [diff] [blame] | 30 | #include "acx.h" |
Shahar Levi | bc765bf | 2011-03-06 16:32:10 +0200 | [diff] [blame] | 31 | #include "reg.h" |
Eliad Peller | abc4747 | 2011-12-06 12:15:05 +0200 | [diff] [blame^] | 32 | #include "ps.h" |
Kalle Valo | c8c9087 | 2010-02-18 13:25:53 +0200 | [diff] [blame] | 33 | |
| 34 | #define WL1271_TM_MAX_DATA_LENGTH 1024 |
| 35 | |
| 36 | enum wl1271_tm_commands { |
| 37 | WL1271_TM_CMD_UNSPEC, |
| 38 | WL1271_TM_CMD_TEST, |
| 39 | WL1271_TM_CMD_INTERROGATE, |
| 40 | WL1271_TM_CMD_CONFIGURE, |
Kalle Valo | c8c9087 | 2010-02-18 13:25:53 +0200 | [diff] [blame] | 41 | WL1271_TM_CMD_SET_PLT_MODE, |
Eliad Peller | e285a52 | 2010-10-27 14:09:58 +0200 | [diff] [blame] | 42 | WL1271_TM_CMD_RECOVER, |
Kalle Valo | c8c9087 | 2010-02-18 13:25:53 +0200 | [diff] [blame] | 43 | |
| 44 | __WL1271_TM_CMD_AFTER_LAST |
| 45 | }; |
| 46 | #define WL1271_TM_CMD_MAX (__WL1271_TM_CMD_AFTER_LAST - 1) |
| 47 | |
| 48 | enum 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 | |
| 60 | static 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 | |
| 70 | static 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 Valo | c8c9087 | 2010-02-18 13:25:53 +0200 | [diff] [blame] | 92 | |
Eliad Peller | abc4747 | 2011-12-06 12:15:05 +0200 | [diff] [blame^] | 93 | 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 Valo | c8c9087 | 2010-02-18 13:25:53 +0200 | [diff] [blame] | 103 | if (ret < 0) { |
| 104 | wl1271_warning("testmode cmd test failed: %d", ret); |
Eliad Peller | abc4747 | 2011-12-06 12:15:05 +0200 | [diff] [blame^] | 105 | goto out_sleep; |
Kalle Valo | c8c9087 | 2010-02-18 13:25:53 +0200 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | if (answer) { |
| 109 | len = nla_total_size(buf_len); |
| 110 | skb = cfg80211_testmode_alloc_reply_skb(wl->hw->wiphy, len); |
Eliad Peller | abc4747 | 2011-12-06 12:15:05 +0200 | [diff] [blame^] | 111 | if (!skb) { |
| 112 | ret = -ENOMEM; |
| 113 | goto out_sleep; |
| 114 | } |
Kalle Valo | c8c9087 | 2010-02-18 13:25:53 +0200 | [diff] [blame] | 115 | |
| 116 | NLA_PUT(skb, WL1271_TM_ATTR_DATA, buf_len, buf); |
| 117 | ret = cfg80211_testmode_reply(skb); |
| 118 | if (ret < 0) |
Eliad Peller | abc4747 | 2011-12-06 12:15:05 +0200 | [diff] [blame^] | 119 | goto out_sleep; |
Kalle Valo | c8c9087 | 2010-02-18 13:25:53 +0200 | [diff] [blame] | 120 | } |
| 121 | |
Eliad Peller | abc4747 | 2011-12-06 12:15:05 +0200 | [diff] [blame^] | 122 | out_sleep: |
| 123 | wl1271_ps_elp_sleep(wl); |
| 124 | out: |
| 125 | mutex_unlock(&wl->mutex); |
| 126 | |
| 127 | return ret; |
Kalle Valo | c8c9087 | 2010-02-18 13:25:53 +0200 | [diff] [blame] | 128 | |
| 129 | nla_put_failure: |
| 130 | kfree_skb(skb); |
Eliad Peller | abc4747 | 2011-12-06 12:15:05 +0200 | [diff] [blame^] | 131 | ret = -EMSGSIZE; |
| 132 | goto out_sleep; |
Kalle Valo | c8c9087 | 2010-02-18 13:25:53 +0200 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | static 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 Valo | c8c9087 | 2010-02-18 13:25:53 +0200 | [diff] [blame] | 149 | mutex_lock(&wl->mutex); |
Kalle Valo | c8c9087 | 2010-02-18 13:25:53 +0200 | [diff] [blame] | 150 | |
Eliad Peller | abc4747 | 2011-12-06 12:15:05 +0200 | [diff] [blame^] | 151 | 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 Valo | c8c9087 | 2010-02-18 13:25:53 +0200 | [diff] [blame] | 167 | if (ret < 0) { |
| 168 | wl1271_warning("testmode cmd interrogate failed: %d", ret); |
Eliad Peller | abc4747 | 2011-12-06 12:15:05 +0200 | [diff] [blame^] | 169 | goto out_free; |
Kalle Valo | c8c9087 | 2010-02-18 13:25:53 +0200 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | skb = cfg80211_testmode_alloc_reply_skb(wl->hw->wiphy, sizeof(*cmd)); |
Julia Lawall | f8afdf4 | 2011-08-12 13:31:30 -0400 | [diff] [blame] | 173 | if (!skb) { |
Eliad Peller | abc4747 | 2011-12-06 12:15:05 +0200 | [diff] [blame^] | 174 | ret = -ENOMEM; |
| 175 | goto out_free; |
Julia Lawall | f8afdf4 | 2011-08-12 13:31:30 -0400 | [diff] [blame] | 176 | } |
Kalle Valo | c8c9087 | 2010-02-18 13:25:53 +0200 | [diff] [blame] | 177 | |
| 178 | NLA_PUT(skb, WL1271_TM_ATTR_DATA, sizeof(*cmd), cmd); |
| 179 | |
Eliad Peller | abc4747 | 2011-12-06 12:15:05 +0200 | [diff] [blame^] | 180 | out_free: |
| 181 | kfree(cmd); |
| 182 | out_sleep: |
| 183 | wl1271_ps_elp_sleep(wl); |
| 184 | out: |
| 185 | mutex_unlock(&wl->mutex); |
| 186 | |
| 187 | return ret; |
Kalle Valo | c8c9087 | 2010-02-18 13:25:53 +0200 | [diff] [blame] | 188 | |
| 189 | nla_put_failure: |
| 190 | kfree_skb(skb); |
Eliad Peller | abc4747 | 2011-12-06 12:15:05 +0200 | [diff] [blame^] | 191 | ret = -EMSGSIZE; |
| 192 | goto out_free; |
Kalle Valo | c8c9087 | 2010-02-18 13:25:53 +0200 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | static 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 Valo | c8c9087 | 2010-02-18 13:25:53 +0200 | [diff] [blame] | 227 | static 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 Peller | e285a52 | 2010-10-27 14:09:58 +0200 | [diff] [blame] | 254 | static int wl1271_tm_cmd_recover(struct wl1271 *wl, struct nlattr *tb[]) |
| 255 | { |
| 256 | wl1271_debug(DEBUG_TESTMODE, "testmode cmd recover"); |
| 257 | |
Ido Yariv | baacb9a | 2011-06-06 14:57:05 +0300 | [diff] [blame] | 258 | wl12xx_queue_recovery_work(wl); |
Eliad Peller | e285a52 | 2010-10-27 14:09:58 +0200 | [diff] [blame] | 259 | |
| 260 | return 0; |
| 261 | } |
| 262 | |
Kalle Valo | c8c9087 | 2010-02-18 13:25:53 +0200 | [diff] [blame] | 263 | int 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 Valo | c8c9087 | 2010-02-18 13:25:53 +0200 | [diff] [blame] | 283 | case WL1271_TM_CMD_SET_PLT_MODE: |
| 284 | return wl1271_tm_cmd_set_plt_mode(wl, tb); |
Eliad Peller | e285a52 | 2010-10-27 14:09:58 +0200 | [diff] [blame] | 285 | case WL1271_TM_CMD_RECOVER: |
| 286 | return wl1271_tm_cmd_recover(wl, tb); |
Kalle Valo | c8c9087 | 2010-02-18 13:25:53 +0200 | [diff] [blame] | 287 | default: |
| 288 | return -EOPNOTSUPP; |
| 289 | } |
| 290 | } |