testmode.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. if (nla_put(skb, WL1271_TM_ATTR_DATA, buf_len, buf))
  99. goto nla_put_failure;
  100. ret = cfg80211_testmode_reply(skb);
  101. if (ret < 0)
  102. goto out_sleep;
  103. }
  104. out_sleep:
  105. wl1271_ps_elp_sleep(wl);
  106. out:
  107. mutex_unlock(&wl->mutex);
  108. return ret;
  109. nla_put_failure:
  110. kfree_skb(skb);
  111. ret = -EMSGSIZE;
  112. goto out_sleep;
  113. }
  114. static int wl1271_tm_cmd_interrogate(struct wl1271 *wl, struct nlattr *tb[])
  115. {
  116. int ret;
  117. struct wl1271_command *cmd;
  118. struct sk_buff *skb;
  119. u8 ie_id;
  120. wl1271_debug(DEBUG_TESTMODE, "testmode cmd interrogate");
  121. if (!tb[WL1271_TM_ATTR_IE_ID])
  122. return -EINVAL;
  123. ie_id = nla_get_u8(tb[WL1271_TM_ATTR_IE_ID]);
  124. mutex_lock(&wl->mutex);
  125. if (wl->state == WL1271_STATE_OFF) {
  126. ret = -EINVAL;
  127. goto out;
  128. }
  129. ret = wl1271_ps_elp_wakeup(wl);
  130. if (ret < 0)
  131. goto out;
  132. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  133. if (!cmd) {
  134. ret = -ENOMEM;
  135. goto out_sleep;
  136. }
  137. ret = wl1271_cmd_interrogate(wl, ie_id, cmd, sizeof(*cmd));
  138. if (ret < 0) {
  139. wl1271_warning("testmode cmd interrogate failed: %d", ret);
  140. goto out_free;
  141. }
  142. skb = cfg80211_testmode_alloc_reply_skb(wl->hw->wiphy, sizeof(*cmd));
  143. if (!skb) {
  144. ret = -ENOMEM;
  145. goto out_free;
  146. }
  147. if (nla_put(skb, WL1271_TM_ATTR_DATA, sizeof(*cmd), cmd))
  148. goto nla_put_failure;
  149. ret = cfg80211_testmode_reply(skb);
  150. if (ret < 0)
  151. goto out_free;
  152. out_free:
  153. kfree(cmd);
  154. out_sleep:
  155. wl1271_ps_elp_sleep(wl);
  156. out:
  157. mutex_unlock(&wl->mutex);
  158. return ret;
  159. nla_put_failure:
  160. kfree_skb(skb);
  161. ret = -EMSGSIZE;
  162. goto out_free;
  163. }
  164. static int wl1271_tm_cmd_configure(struct wl1271 *wl, struct nlattr *tb[])
  165. {
  166. int buf_len, ret;
  167. void *buf;
  168. u8 ie_id;
  169. wl1271_debug(DEBUG_TESTMODE, "testmode cmd configure");
  170. if (!tb[WL1271_TM_ATTR_DATA])
  171. return -EINVAL;
  172. if (!tb[WL1271_TM_ATTR_IE_ID])
  173. return -EINVAL;
  174. ie_id = nla_get_u8(tb[WL1271_TM_ATTR_IE_ID]);
  175. buf = nla_data(tb[WL1271_TM_ATTR_DATA]);
  176. buf_len = nla_len(tb[WL1271_TM_ATTR_DATA]);
  177. if (buf_len > sizeof(struct wl1271_command))
  178. return -EMSGSIZE;
  179. mutex_lock(&wl->mutex);
  180. ret = wl1271_cmd_configure(wl, ie_id, buf, buf_len);
  181. mutex_unlock(&wl->mutex);
  182. if (ret < 0) {
  183. wl1271_warning("testmode cmd configure failed: %d", ret);
  184. return ret;
  185. }
  186. return 0;
  187. }
  188. static int wl1271_tm_cmd_set_plt_mode(struct wl1271 *wl, struct nlattr *tb[])
  189. {
  190. u32 val;
  191. int ret;
  192. wl1271_debug(DEBUG_TESTMODE, "testmode cmd set plt mode");
  193. if (!tb[WL1271_TM_ATTR_PLT_MODE])
  194. return -EINVAL;
  195. val = nla_get_u32(tb[WL1271_TM_ATTR_PLT_MODE]);
  196. switch (val) {
  197. case 0:
  198. ret = wl1271_plt_stop(wl);
  199. break;
  200. case 1:
  201. ret = wl1271_plt_start(wl);
  202. break;
  203. default:
  204. ret = -EINVAL;
  205. break;
  206. }
  207. return ret;
  208. }
  209. static int wl1271_tm_cmd_recover(struct wl1271 *wl, struct nlattr *tb[])
  210. {
  211. wl1271_debug(DEBUG_TESTMODE, "testmode cmd recover");
  212. wl12xx_queue_recovery_work(wl);
  213. return 0;
  214. }
  215. static int wl12xx_tm_cmd_get_mac(struct wl1271 *wl, struct nlattr *tb[])
  216. {
  217. struct sk_buff *skb;
  218. u8 mac_addr[ETH_ALEN];
  219. int ret = 0;
  220. mutex_lock(&wl->mutex);
  221. if (!wl->plt) {
  222. ret = -EINVAL;
  223. goto out;
  224. }
  225. if (wl->fuse_oui_addr == 0 && wl->fuse_nic_addr == 0) {
  226. ret = -EOPNOTSUPP;
  227. goto out;
  228. }
  229. mac_addr[0] = (u8)(wl->fuse_oui_addr >> 16);
  230. mac_addr[1] = (u8)(wl->fuse_oui_addr >> 8);
  231. mac_addr[2] = (u8) wl->fuse_oui_addr;
  232. mac_addr[3] = (u8)(wl->fuse_nic_addr >> 16);
  233. mac_addr[4] = (u8)(wl->fuse_nic_addr >> 8);
  234. mac_addr[5] = (u8) wl->fuse_nic_addr;
  235. skb = cfg80211_testmode_alloc_reply_skb(wl->hw->wiphy, ETH_ALEN);
  236. if (!skb) {
  237. ret = -ENOMEM;
  238. goto out;
  239. }
  240. if (nla_put(skb, WL1271_TM_ATTR_DATA, ETH_ALEN, mac_addr))
  241. goto nla_put_failure;
  242. ret = cfg80211_testmode_reply(skb);
  243. if (ret < 0)
  244. goto out;
  245. out:
  246. mutex_unlock(&wl->mutex);
  247. return ret;
  248. nla_put_failure:
  249. kfree_skb(skb);
  250. ret = -EMSGSIZE;
  251. goto out;
  252. }
  253. int wl1271_tm_cmd(struct ieee80211_hw *hw, void *data, int len)
  254. {
  255. struct wl1271 *wl = hw->priv;
  256. struct nlattr *tb[WL1271_TM_ATTR_MAX + 1];
  257. int err;
  258. err = nla_parse(tb, WL1271_TM_ATTR_MAX, data, len, wl1271_tm_policy);
  259. if (err)
  260. return err;
  261. if (!tb[WL1271_TM_ATTR_CMD_ID])
  262. return -EINVAL;
  263. switch (nla_get_u32(tb[WL1271_TM_ATTR_CMD_ID])) {
  264. case WL1271_TM_CMD_TEST:
  265. return wl1271_tm_cmd_test(wl, tb);
  266. case WL1271_TM_CMD_INTERROGATE:
  267. return wl1271_tm_cmd_interrogate(wl, tb);
  268. case WL1271_TM_CMD_CONFIGURE:
  269. return wl1271_tm_cmd_configure(wl, tb);
  270. case WL1271_TM_CMD_SET_PLT_MODE:
  271. return wl1271_tm_cmd_set_plt_mode(wl, tb);
  272. case WL1271_TM_CMD_RECOVER:
  273. return wl1271_tm_cmd_recover(wl, tb);
  274. case WL1271_TM_CMD_GET_MAC:
  275. return wl12xx_tm_cmd_get_mac(wl, tb);
  276. default:
  277. return -EOPNOTSUPP;
  278. }
  279. }