wl1271_testmode.c 6.1 KB

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