testmode.c 5.6 KB

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