testmode.c 7.2 KB

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