testmode.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. */
  23. #include "testmode.h"
  24. #include <linux/slab.h>
  25. #include <net/genetlink.h>
  26. #include "wlcore.h"
  27. #include "debug.h"
  28. #include "acx.h"
  29. #include "ps.h"
  30. #include "io.h"
  31. #define WL1271_TM_MAX_DATA_LENGTH 1024
  32. enum wl1271_tm_commands {
  33. WL1271_TM_CMD_UNSPEC,
  34. WL1271_TM_CMD_TEST,
  35. WL1271_TM_CMD_INTERROGATE,
  36. WL1271_TM_CMD_CONFIGURE,
  37. WL1271_TM_CMD_NVS_PUSH, /* Not in use. Keep to not break ABI */
  38. WL1271_TM_CMD_SET_PLT_MODE,
  39. WL1271_TM_CMD_RECOVER,
  40. WL1271_TM_CMD_GET_MAC,
  41. __WL1271_TM_CMD_AFTER_LAST
  42. };
  43. #define WL1271_TM_CMD_MAX (__WL1271_TM_CMD_AFTER_LAST - 1)
  44. enum wl1271_tm_attrs {
  45. WL1271_TM_ATTR_UNSPEC,
  46. WL1271_TM_ATTR_CMD_ID,
  47. WL1271_TM_ATTR_ANSWER,
  48. WL1271_TM_ATTR_DATA,
  49. WL1271_TM_ATTR_IE_ID,
  50. WL1271_TM_ATTR_PLT_MODE,
  51. __WL1271_TM_ATTR_AFTER_LAST
  52. };
  53. #define WL1271_TM_ATTR_MAX (__WL1271_TM_ATTR_AFTER_LAST - 1)
  54. static struct nla_policy wl1271_tm_policy[WL1271_TM_ATTR_MAX + 1] = {
  55. [WL1271_TM_ATTR_CMD_ID] = { .type = NLA_U32 },
  56. [WL1271_TM_ATTR_ANSWER] = { .type = NLA_U8 },
  57. [WL1271_TM_ATTR_DATA] = { .type = NLA_BINARY,
  58. .len = WL1271_TM_MAX_DATA_LENGTH },
  59. [WL1271_TM_ATTR_IE_ID] = { .type = NLA_U32 },
  60. [WL1271_TM_ATTR_PLT_MODE] = { .type = NLA_U32 },
  61. };
  62. static int wl1271_tm_cmd_test(struct wl1271 *wl, struct nlattr *tb[])
  63. {
  64. int buf_len, ret, len;
  65. struct sk_buff *skb;
  66. void *buf;
  67. u8 answer = 0;
  68. wl1271_debug(DEBUG_TESTMODE, "testmode cmd test");
  69. if (!tb[WL1271_TM_ATTR_DATA])
  70. return -EINVAL;
  71. buf = nla_data(tb[WL1271_TM_ATTR_DATA]);
  72. buf_len = nla_len(tb[WL1271_TM_ATTR_DATA]);
  73. if (tb[WL1271_TM_ATTR_ANSWER])
  74. answer = nla_get_u8(tb[WL1271_TM_ATTR_ANSWER]);
  75. if (buf_len > sizeof(struct wl1271_command))
  76. return -EMSGSIZE;
  77. mutex_lock(&wl->mutex);
  78. if (wl->state == WL1271_STATE_OFF) {
  79. ret = -EINVAL;
  80. goto out;
  81. }
  82. ret = wl1271_ps_elp_wakeup(wl);
  83. if (ret < 0)
  84. goto out;
  85. ret = wl1271_cmd_test(wl, buf, buf_len, answer);
  86. if (ret < 0) {
  87. wl1271_warning("testmode cmd test failed: %d", ret);
  88. goto out_sleep;
  89. }
  90. if (answer) {
  91. len = nla_total_size(buf_len);
  92. skb = cfg80211_testmode_alloc_reply_skb(wl->hw->wiphy, len);
  93. if (!skb) {
  94. ret = -ENOMEM;
  95. goto out_sleep;
  96. }
  97. NLA_PUT(skb, WL1271_TM_ATTR_DATA, buf_len, buf);
  98. ret = cfg80211_testmode_reply(skb);
  99. if (ret < 0)
  100. goto out_sleep;
  101. }
  102. out_sleep:
  103. wl1271_ps_elp_sleep(wl);
  104. out:
  105. mutex_unlock(&wl->mutex);
  106. return ret;
  107. nla_put_failure:
  108. kfree_skb(skb);
  109. ret = -EMSGSIZE;
  110. goto out_sleep;
  111. }
  112. static int wl1271_tm_cmd_interrogate(struct wl1271 *wl, struct nlattr *tb[])
  113. {
  114. int ret;
  115. struct wl1271_command *cmd;
  116. struct sk_buff *skb;
  117. u8 ie_id;
  118. wl1271_debug(DEBUG_TESTMODE, "testmode cmd interrogate");
  119. if (!tb[WL1271_TM_ATTR_IE_ID])
  120. return -EINVAL;
  121. ie_id = nla_get_u8(tb[WL1271_TM_ATTR_IE_ID]);
  122. mutex_lock(&wl->mutex);
  123. if (wl->state == WL1271_STATE_OFF) {
  124. ret = -EINVAL;
  125. goto out;
  126. }
  127. ret = wl1271_ps_elp_wakeup(wl);
  128. if (ret < 0)
  129. goto out;
  130. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  131. if (!cmd) {
  132. ret = -ENOMEM;
  133. goto out_sleep;
  134. }
  135. ret = wl1271_cmd_interrogate(wl, ie_id, cmd, sizeof(*cmd));
  136. if (ret < 0) {
  137. wl1271_warning("testmode cmd interrogate failed: %d", ret);
  138. goto out_free;
  139. }
  140. skb = cfg80211_testmode_alloc_reply_skb(wl->hw->wiphy, sizeof(*cmd));
  141. if (!skb) {
  142. ret = -ENOMEM;
  143. goto out_free;
  144. }
  145. NLA_PUT(skb, WL1271_TM_ATTR_DATA, sizeof(*cmd), cmd);
  146. ret = cfg80211_testmode_reply(skb);
  147. if (ret < 0)
  148. goto out_free;
  149. out_free:
  150. kfree(cmd);
  151. out_sleep:
  152. wl1271_ps_elp_sleep(wl);
  153. out:
  154. mutex_unlock(&wl->mutex);
  155. return ret;
  156. nla_put_failure:
  157. kfree_skb(skb);
  158. ret = -EMSGSIZE;
  159. goto out_free;
  160. }
  161. static int wl1271_tm_cmd_configure(struct wl1271 *wl, struct nlattr *tb[])
  162. {
  163. int buf_len, ret;
  164. void *buf;
  165. u8 ie_id;
  166. wl1271_debug(DEBUG_TESTMODE, "testmode cmd configure");
  167. if (!tb[WL1271_TM_ATTR_DATA])
  168. return -EINVAL;
  169. if (!tb[WL1271_TM_ATTR_IE_ID])
  170. 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]);
  174. if (buf_len > sizeof(struct wl1271_command))
  175. return -EMSGSIZE;
  176. mutex_lock(&wl->mutex);
  177. ret = wl1271_cmd_configure(wl, ie_id, buf, buf_len);
  178. mutex_unlock(&wl->mutex);
  179. if (ret < 0) {
  180. wl1271_warning("testmode cmd configure failed: %d", ret);
  181. return ret;
  182. }
  183. return 0;
  184. }
  185. static int wl1271_tm_cmd_set_plt_mode(struct wl1271 *wl, struct nlattr *tb[])
  186. {
  187. u32 val;
  188. int ret;
  189. wl1271_debug(DEBUG_TESTMODE, "testmode cmd set plt mode");
  190. if (!tb[WL1271_TM_ATTR_PLT_MODE])
  191. return -EINVAL;
  192. val = nla_get_u32(tb[WL1271_TM_ATTR_PLT_MODE]);
  193. switch (val) {
  194. case 0:
  195. ret = wl1271_plt_stop(wl);
  196. break;
  197. case 1:
  198. ret = wl1271_plt_start(wl);
  199. break;
  200. default:
  201. ret = -EINVAL;
  202. break;
  203. }
  204. return ret;
  205. }
  206. static int wl1271_tm_cmd_recover(struct wl1271 *wl, struct nlattr *tb[])
  207. {
  208. wl1271_debug(DEBUG_TESTMODE, "testmode cmd recover");
  209. wl12xx_queue_recovery_work(wl);
  210. return 0;
  211. }
  212. static int wl12xx_tm_cmd_get_mac(struct wl1271 *wl, struct nlattr *tb[])
  213. {
  214. struct sk_buff *skb;
  215. u8 mac_addr[ETH_ALEN];
  216. int ret = 0;
  217. mutex_lock(&wl->mutex);
  218. if (!wl->plt) {
  219. ret = -EINVAL;
  220. goto out;
  221. }
  222. if (wl->fuse_oui_addr == 0 && wl->fuse_nic_addr == 0) {
  223. ret = -EOPNOTSUPP;
  224. goto out;
  225. }
  226. mac_addr[0] = (u8)(wl->fuse_oui_addr >> 16);
  227. mac_addr[1] = (u8)(wl->fuse_oui_addr >> 8);
  228. mac_addr[2] = (u8) wl->fuse_oui_addr;
  229. mac_addr[3] = (u8)(wl->fuse_nic_addr >> 16);
  230. mac_addr[4] = (u8)(wl->fuse_nic_addr >> 8);
  231. mac_addr[5] = (u8) wl->fuse_nic_addr;
  232. skb = cfg80211_testmode_alloc_reply_skb(wl->hw->wiphy, ETH_ALEN);
  233. if (!skb) {
  234. ret = -ENOMEM;
  235. goto out;
  236. }
  237. NLA_PUT(skb, WL1271_TM_ATTR_DATA, ETH_ALEN, mac_addr);
  238. ret = cfg80211_testmode_reply(skb);
  239. if (ret < 0)
  240. goto out;
  241. out:
  242. mutex_unlock(&wl->mutex);
  243. return ret;
  244. nla_put_failure:
  245. kfree_skb(skb);
  246. ret = -EMSGSIZE;
  247. goto out;
  248. }
  249. int wl1271_tm_cmd(struct ieee80211_hw *hw, void *data, int len)
  250. {
  251. struct wl1271 *wl = hw->priv;
  252. struct nlattr *tb[WL1271_TM_ATTR_MAX + 1];
  253. int err;
  254. err = nla_parse(tb, WL1271_TM_ATTR_MAX, data, len, wl1271_tm_policy);
  255. if (err)
  256. return err;
  257. if (!tb[WL1271_TM_ATTR_CMD_ID])
  258. return -EINVAL;
  259. switch (nla_get_u32(tb[WL1271_TM_ATTR_CMD_ID])) {
  260. case WL1271_TM_CMD_TEST:
  261. return wl1271_tm_cmd_test(wl, tb);
  262. case WL1271_TM_CMD_INTERROGATE:
  263. return wl1271_tm_cmd_interrogate(wl, tb);
  264. case WL1271_TM_CMD_CONFIGURE:
  265. return wl1271_tm_cmd_configure(wl, tb);
  266. case WL1271_TM_CMD_SET_PLT_MODE:
  267. return wl1271_tm_cmd_set_plt_mode(wl, tb);
  268. case WL1271_TM_CMD_RECOVER:
  269. return wl1271_tm_cmd_recover(wl, tb);
  270. case WL1271_TM_CMD_GET_MAC:
  271. return wl12xx_tm_cmd_get_mac(wl, tb);
  272. default:
  273. return -EOPNOTSUPP;
  274. }
  275. }