testmode.c 6.5 KB

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