testmode.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 "wl12xx.h"
  27. #include "debug.h"
  28. #include "acx.h"
  29. #include "reg.h"
  30. #include "ps.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_SET_PLT_MODE,
  38. WL1271_TM_CMD_RECOVER,
  39. __WL1271_TM_CMD_AFTER_LAST
  40. };
  41. #define WL1271_TM_CMD_MAX (__WL1271_TM_CMD_AFTER_LAST - 1)
  42. enum wl1271_tm_attrs {
  43. WL1271_TM_ATTR_UNSPEC,
  44. WL1271_TM_ATTR_CMD_ID,
  45. WL1271_TM_ATTR_ANSWER,
  46. WL1271_TM_ATTR_DATA,
  47. WL1271_TM_ATTR_IE_ID,
  48. WL1271_TM_ATTR_PLT_MODE,
  49. __WL1271_TM_ATTR_AFTER_LAST
  50. };
  51. #define WL1271_TM_ATTR_MAX (__WL1271_TM_ATTR_AFTER_LAST - 1)
  52. static struct nla_policy wl1271_tm_policy[WL1271_TM_ATTR_MAX + 1] = {
  53. [WL1271_TM_ATTR_CMD_ID] = { .type = NLA_U32 },
  54. [WL1271_TM_ATTR_ANSWER] = { .type = NLA_U8 },
  55. [WL1271_TM_ATTR_DATA] = { .type = NLA_BINARY,
  56. .len = WL1271_TM_MAX_DATA_LENGTH },
  57. [WL1271_TM_ATTR_IE_ID] = { .type = NLA_U32 },
  58. [WL1271_TM_ATTR_PLT_MODE] = { .type = NLA_U32 },
  59. };
  60. static int wl1271_tm_cmd_test(struct wl1271 *wl, struct nlattr *tb[])
  61. {
  62. int buf_len, ret, len;
  63. struct sk_buff *skb;
  64. void *buf;
  65. u8 answer = 0;
  66. wl1271_debug(DEBUG_TESTMODE, "testmode cmd test");
  67. if (!tb[WL1271_TM_ATTR_DATA])
  68. return -EINVAL;
  69. buf = nla_data(tb[WL1271_TM_ATTR_DATA]);
  70. buf_len = nla_len(tb[WL1271_TM_ATTR_DATA]);
  71. if (tb[WL1271_TM_ATTR_ANSWER])
  72. answer = nla_get_u8(tb[WL1271_TM_ATTR_ANSWER]);
  73. if (buf_len > sizeof(struct wl1271_command))
  74. return -EMSGSIZE;
  75. mutex_lock(&wl->mutex);
  76. if (wl->state == WL1271_STATE_OFF) {
  77. ret = -EINVAL;
  78. goto out;
  79. }
  80. ret = wl1271_ps_elp_wakeup(wl);
  81. if (ret < 0)
  82. goto out;
  83. ret = wl1271_cmd_test(wl, buf, buf_len, answer);
  84. if (ret < 0) {
  85. wl1271_warning("testmode cmd test failed: %d", ret);
  86. goto out_sleep;
  87. }
  88. if (answer) {
  89. len = nla_total_size(buf_len);
  90. skb = cfg80211_testmode_alloc_reply_skb(wl->hw->wiphy, len);
  91. if (!skb) {
  92. ret = -ENOMEM;
  93. goto out_sleep;
  94. }
  95. NLA_PUT(skb, WL1271_TM_ATTR_DATA, buf_len, buf);
  96. ret = cfg80211_testmode_reply(skb);
  97. if (ret < 0)
  98. goto out_sleep;
  99. }
  100. out_sleep:
  101. wl1271_ps_elp_sleep(wl);
  102. out:
  103. mutex_unlock(&wl->mutex);
  104. return ret;
  105. nla_put_failure:
  106. kfree_skb(skb);
  107. ret = -EMSGSIZE;
  108. goto out_sleep;
  109. }
  110. static int wl1271_tm_cmd_interrogate(struct wl1271 *wl, struct nlattr *tb[])
  111. {
  112. int ret;
  113. struct wl1271_command *cmd;
  114. struct sk_buff *skb;
  115. u8 ie_id;
  116. wl1271_debug(DEBUG_TESTMODE, "testmode cmd interrogate");
  117. if (!tb[WL1271_TM_ATTR_IE_ID])
  118. return -EINVAL;
  119. ie_id = nla_get_u8(tb[WL1271_TM_ATTR_IE_ID]);
  120. mutex_lock(&wl->mutex);
  121. if (wl->state == WL1271_STATE_OFF) {
  122. ret = -EINVAL;
  123. goto out;
  124. }
  125. ret = wl1271_ps_elp_wakeup(wl);
  126. if (ret < 0)
  127. goto out;
  128. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  129. if (!cmd) {
  130. ret = -ENOMEM;
  131. goto out_sleep;
  132. }
  133. ret = wl1271_cmd_interrogate(wl, ie_id, cmd, sizeof(*cmd));
  134. if (ret < 0) {
  135. wl1271_warning("testmode cmd interrogate failed: %d", ret);
  136. goto out_free;
  137. }
  138. skb = cfg80211_testmode_alloc_reply_skb(wl->hw->wiphy, sizeof(*cmd));
  139. if (!skb) {
  140. ret = -ENOMEM;
  141. goto out_free;
  142. }
  143. NLA_PUT(skb, WL1271_TM_ATTR_DATA, sizeof(*cmd), cmd);
  144. ret = cfg80211_testmode_reply(skb);
  145. if (ret < 0)
  146. goto out_free;
  147. out_free:
  148. kfree(cmd);
  149. out_sleep:
  150. wl1271_ps_elp_sleep(wl);
  151. out:
  152. mutex_unlock(&wl->mutex);
  153. return ret;
  154. nla_put_failure:
  155. kfree_skb(skb);
  156. ret = -EMSGSIZE;
  157. goto out_free;
  158. }
  159. static int wl1271_tm_cmd_configure(struct wl1271 *wl, struct nlattr *tb[])
  160. {
  161. int buf_len, ret;
  162. void *buf;
  163. u8 ie_id;
  164. wl1271_debug(DEBUG_TESTMODE, "testmode cmd configure");
  165. if (!tb[WL1271_TM_ATTR_DATA])
  166. return -EINVAL;
  167. if (!tb[WL1271_TM_ATTR_IE_ID])
  168. return -EINVAL;
  169. ie_id = nla_get_u8(tb[WL1271_TM_ATTR_IE_ID]);
  170. buf = nla_data(tb[WL1271_TM_ATTR_DATA]);
  171. buf_len = nla_len(tb[WL1271_TM_ATTR_DATA]);
  172. if (buf_len > sizeof(struct wl1271_command))
  173. return -EMSGSIZE;
  174. mutex_lock(&wl->mutex);
  175. ret = wl1271_cmd_configure(wl, ie_id, buf, buf_len);
  176. mutex_unlock(&wl->mutex);
  177. if (ret < 0) {
  178. wl1271_warning("testmode cmd configure failed: %d", ret);
  179. return ret;
  180. }
  181. return 0;
  182. }
  183. static int wl1271_tm_cmd_set_plt_mode(struct wl1271 *wl, struct nlattr *tb[])
  184. {
  185. u32 val;
  186. int ret;
  187. wl1271_debug(DEBUG_TESTMODE, "testmode cmd set plt mode");
  188. if (!tb[WL1271_TM_ATTR_PLT_MODE])
  189. return -EINVAL;
  190. val = nla_get_u32(tb[WL1271_TM_ATTR_PLT_MODE]);
  191. switch (val) {
  192. case 0:
  193. ret = wl1271_plt_stop(wl);
  194. break;
  195. case 1:
  196. ret = wl1271_plt_start(wl);
  197. break;
  198. default:
  199. ret = -EINVAL;
  200. break;
  201. }
  202. return ret;
  203. }
  204. static int wl1271_tm_cmd_recover(struct wl1271 *wl, struct nlattr *tb[])
  205. {
  206. wl1271_debug(DEBUG_TESTMODE, "testmode cmd recover");
  207. wl12xx_queue_recovery_work(wl);
  208. return 0;
  209. }
  210. int wl1271_tm_cmd(struct ieee80211_hw *hw, void *data, int len)
  211. {
  212. struct wl1271 *wl = hw->priv;
  213. struct nlattr *tb[WL1271_TM_ATTR_MAX + 1];
  214. int err;
  215. err = nla_parse(tb, WL1271_TM_ATTR_MAX, data, len, wl1271_tm_policy);
  216. if (err)
  217. return err;
  218. if (!tb[WL1271_TM_ATTR_CMD_ID])
  219. return -EINVAL;
  220. switch (nla_get_u32(tb[WL1271_TM_ATTR_CMD_ID])) {
  221. case WL1271_TM_CMD_TEST:
  222. return wl1271_tm_cmd_test(wl, tb);
  223. case WL1271_TM_CMD_INTERROGATE:
  224. return wl1271_tm_cmd_interrogate(wl, tb);
  225. case WL1271_TM_CMD_CONFIGURE:
  226. return wl1271_tm_cmd_configure(wl, tb);
  227. case WL1271_TM_CMD_SET_PLT_MODE:
  228. return wl1271_tm_cmd_set_plt_mode(wl, tb);
  229. case WL1271_TM_CMD_RECOVER:
  230. return wl1271_tm_cmd_recover(wl, tb);
  231. default:
  232. return -EOPNOTSUPP;
  233. }
  234. }