testmode.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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 "wlcore.h"
  27. #include "debug.h"
  28. #include "acx.h"
  29. #include "ps.h"
  30. #include "io.h"
  31. #define WL1271_TM_MAX_DATA_LENGTH 1024
  32. enum wl1271_tm_commands {
  33. WL1271_TM_CMD_UNSPEC,
  34. WL1271_TM_CMD_TEST,
  35. WL1271_TM_CMD_INTERROGATE,
  36. WL1271_TM_CMD_CONFIGURE,
  37. WL1271_TM_CMD_NVS_PUSH, /* Not in use. Keep to not break ABI */
  38. WL1271_TM_CMD_SET_PLT_MODE,
  39. WL1271_TM_CMD_RECOVER, /* Not in use. Keep to not break ABI */
  40. WL1271_TM_CMD_GET_MAC,
  41. __WL1271_TM_CMD_AFTER_LAST
  42. };
  43. #define WL1271_TM_CMD_MAX (__WL1271_TM_CMD_AFTER_LAST - 1)
  44. enum wl1271_tm_attrs {
  45. WL1271_TM_ATTR_UNSPEC,
  46. WL1271_TM_ATTR_CMD_ID,
  47. WL1271_TM_ATTR_ANSWER,
  48. WL1271_TM_ATTR_DATA,
  49. WL1271_TM_ATTR_IE_ID,
  50. WL1271_TM_ATTR_PLT_MODE,
  51. __WL1271_TM_ATTR_AFTER_LAST
  52. };
  53. #define WL1271_TM_ATTR_MAX (__WL1271_TM_ATTR_AFTER_LAST - 1)
  54. static struct nla_policy wl1271_tm_policy[WL1271_TM_ATTR_MAX + 1] = {
  55. [WL1271_TM_ATTR_CMD_ID] = { .type = NLA_U32 },
  56. [WL1271_TM_ATTR_ANSWER] = { .type = NLA_U8 },
  57. [WL1271_TM_ATTR_DATA] = { .type = NLA_BINARY,
  58. .len = WL1271_TM_MAX_DATA_LENGTH },
  59. [WL1271_TM_ATTR_IE_ID] = { .type = NLA_U32 },
  60. [WL1271_TM_ATTR_PLT_MODE] = { .type = NLA_U32 },
  61. };
  62. static int wl1271_tm_cmd_test(struct wl1271 *wl, struct nlattr *tb[])
  63. {
  64. int buf_len, ret, len;
  65. struct sk_buff *skb;
  66. void *buf;
  67. u8 answer = 0;
  68. wl1271_debug(DEBUG_TESTMODE, "testmode cmd test");
  69. if (!tb[WL1271_TM_ATTR_DATA])
  70. return -EINVAL;
  71. buf = nla_data(tb[WL1271_TM_ATTR_DATA]);
  72. buf_len = nla_len(tb[WL1271_TM_ATTR_DATA]);
  73. if (tb[WL1271_TM_ATTR_ANSWER])
  74. answer = nla_get_u8(tb[WL1271_TM_ATTR_ANSWER]);
  75. if (buf_len > sizeof(struct wl1271_command))
  76. return -EMSGSIZE;
  77. mutex_lock(&wl->mutex);
  78. if (unlikely(wl->state != WLCORE_STATE_ON)) {
  79. ret = -EINVAL;
  80. goto out;
  81. }
  82. ret = wl1271_ps_elp_wakeup(wl);
  83. if (ret < 0)
  84. goto out;
  85. ret = wl1271_cmd_test(wl, buf, buf_len, answer);
  86. if (ret < 0) {
  87. wl1271_warning("testmode cmd test failed: %d", ret);
  88. goto out_sleep;
  89. }
  90. if (answer) {
  91. /* If we got bip calibration answer print radio status */
  92. struct wl1271_cmd_cal_p2g *params =
  93. (struct wl1271_cmd_cal_p2g *) buf;
  94. s16 radio_status = (s16) le16_to_cpu(params->radio_status);
  95. if (params->test.id == TEST_CMD_P2G_CAL &&
  96. radio_status < 0)
  97. wl1271_warning("testmode cmd: radio status=%d",
  98. radio_status);
  99. else
  100. wl1271_info("testmode cmd: radio status=%d",
  101. radio_status);
  102. len = nla_total_size(buf_len);
  103. skb = cfg80211_testmode_alloc_reply_skb(wl->hw->wiphy, len);
  104. if (!skb) {
  105. ret = -ENOMEM;
  106. goto out_sleep;
  107. }
  108. if (nla_put(skb, WL1271_TM_ATTR_DATA, buf_len, buf)) {
  109. kfree_skb(skb);
  110. ret = -EMSGSIZE;
  111. goto out_sleep;
  112. }
  113. ret = cfg80211_testmode_reply(skb);
  114. if (ret < 0)
  115. goto out_sleep;
  116. }
  117. out_sleep:
  118. wl1271_ps_elp_sleep(wl);
  119. out:
  120. mutex_unlock(&wl->mutex);
  121. return ret;
  122. }
  123. static int wl1271_tm_cmd_interrogate(struct wl1271 *wl, struct nlattr *tb[])
  124. {
  125. int ret;
  126. struct wl1271_command *cmd;
  127. struct sk_buff *skb;
  128. u8 ie_id;
  129. wl1271_debug(DEBUG_TESTMODE, "testmode cmd interrogate");
  130. if (!tb[WL1271_TM_ATTR_IE_ID])
  131. return -EINVAL;
  132. ie_id = nla_get_u8(tb[WL1271_TM_ATTR_IE_ID]);
  133. mutex_lock(&wl->mutex);
  134. if (unlikely(wl->state != WLCORE_STATE_ON)) {
  135. ret = -EINVAL;
  136. goto out;
  137. }
  138. ret = wl1271_ps_elp_wakeup(wl);
  139. if (ret < 0)
  140. goto out;
  141. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  142. if (!cmd) {
  143. ret = -ENOMEM;
  144. goto out_sleep;
  145. }
  146. ret = wl1271_cmd_interrogate(wl, ie_id, cmd,
  147. sizeof(struct acx_header), sizeof(*cmd));
  148. if (ret < 0) {
  149. wl1271_warning("testmode cmd interrogate failed: %d", ret);
  150. goto out_free;
  151. }
  152. skb = cfg80211_testmode_alloc_reply_skb(wl->hw->wiphy, sizeof(*cmd));
  153. if (!skb) {
  154. ret = -ENOMEM;
  155. goto out_free;
  156. }
  157. if (nla_put(skb, WL1271_TM_ATTR_DATA, sizeof(*cmd), cmd)) {
  158. kfree_skb(skb);
  159. ret = -EMSGSIZE;
  160. goto out_free;
  161. }
  162. ret = cfg80211_testmode_reply(skb);
  163. if (ret < 0)
  164. goto out_free;
  165. out_free:
  166. kfree(cmd);
  167. out_sleep:
  168. wl1271_ps_elp_sleep(wl);
  169. out:
  170. mutex_unlock(&wl->mutex);
  171. return ret;
  172. }
  173. static int wl1271_tm_cmd_configure(struct wl1271 *wl, struct nlattr *tb[])
  174. {
  175. int buf_len, ret;
  176. void *buf;
  177. u8 ie_id;
  178. wl1271_debug(DEBUG_TESTMODE, "testmode cmd configure");
  179. if (!tb[WL1271_TM_ATTR_DATA])
  180. return -EINVAL;
  181. if (!tb[WL1271_TM_ATTR_IE_ID])
  182. return -EINVAL;
  183. ie_id = nla_get_u8(tb[WL1271_TM_ATTR_IE_ID]);
  184. buf = nla_data(tb[WL1271_TM_ATTR_DATA]);
  185. buf_len = nla_len(tb[WL1271_TM_ATTR_DATA]);
  186. if (buf_len > sizeof(struct wl1271_command))
  187. return -EMSGSIZE;
  188. mutex_lock(&wl->mutex);
  189. ret = wl1271_cmd_configure(wl, ie_id, buf, buf_len);
  190. mutex_unlock(&wl->mutex);
  191. if (ret < 0) {
  192. wl1271_warning("testmode cmd configure failed: %d", ret);
  193. return ret;
  194. }
  195. return 0;
  196. }
  197. static int wl1271_tm_detect_fem(struct wl1271 *wl, struct nlattr *tb[])
  198. {
  199. /* return FEM type */
  200. int ret, len;
  201. struct sk_buff *skb;
  202. ret = wl1271_plt_start(wl, PLT_FEM_DETECT);
  203. if (ret < 0)
  204. goto out;
  205. mutex_lock(&wl->mutex);
  206. len = nla_total_size(sizeof(wl->fem_manuf));
  207. skb = cfg80211_testmode_alloc_reply_skb(wl->hw->wiphy, len);
  208. if (!skb) {
  209. ret = -ENOMEM;
  210. goto out_mutex;
  211. }
  212. if (nla_put(skb, WL1271_TM_ATTR_DATA, sizeof(wl->fem_manuf),
  213. &wl->fem_manuf)) {
  214. kfree_skb(skb);
  215. ret = -EMSGSIZE;
  216. goto out_mutex;
  217. }
  218. ret = cfg80211_testmode_reply(skb);
  219. out_mutex:
  220. mutex_unlock(&wl->mutex);
  221. /* We always stop plt after DETECT mode */
  222. wl1271_plt_stop(wl);
  223. out:
  224. return ret;
  225. }
  226. static int wl1271_tm_cmd_set_plt_mode(struct wl1271 *wl, struct nlattr *tb[])
  227. {
  228. u32 val;
  229. int ret;
  230. wl1271_debug(DEBUG_TESTMODE, "testmode cmd set plt mode");
  231. if (!tb[WL1271_TM_ATTR_PLT_MODE])
  232. return -EINVAL;
  233. val = nla_get_u32(tb[WL1271_TM_ATTR_PLT_MODE]);
  234. switch (val) {
  235. case PLT_OFF:
  236. ret = wl1271_plt_stop(wl);
  237. break;
  238. case PLT_ON:
  239. case PLT_CHIP_AWAKE:
  240. ret = wl1271_plt_start(wl, val);
  241. break;
  242. case PLT_FEM_DETECT:
  243. ret = wl1271_tm_detect_fem(wl, tb);
  244. break;
  245. default:
  246. ret = -EINVAL;
  247. break;
  248. }
  249. return ret;
  250. }
  251. static int wl12xx_tm_cmd_get_mac(struct wl1271 *wl, struct nlattr *tb[])
  252. {
  253. struct sk_buff *skb;
  254. u8 mac_addr[ETH_ALEN];
  255. int ret = 0;
  256. mutex_lock(&wl->mutex);
  257. if (!wl->plt) {
  258. ret = -EINVAL;
  259. goto out;
  260. }
  261. if (wl->fuse_oui_addr == 0 && wl->fuse_nic_addr == 0) {
  262. ret = -EOPNOTSUPP;
  263. goto out;
  264. }
  265. mac_addr[0] = (u8)(wl->fuse_oui_addr >> 16);
  266. mac_addr[1] = (u8)(wl->fuse_oui_addr >> 8);
  267. mac_addr[2] = (u8) wl->fuse_oui_addr;
  268. mac_addr[3] = (u8)(wl->fuse_nic_addr >> 16);
  269. mac_addr[4] = (u8)(wl->fuse_nic_addr >> 8);
  270. mac_addr[5] = (u8) wl->fuse_nic_addr;
  271. skb = cfg80211_testmode_alloc_reply_skb(wl->hw->wiphy, ETH_ALEN);
  272. if (!skb) {
  273. ret = -ENOMEM;
  274. goto out;
  275. }
  276. if (nla_put(skb, WL1271_TM_ATTR_DATA, ETH_ALEN, mac_addr)) {
  277. kfree_skb(skb);
  278. ret = -EMSGSIZE;
  279. goto out;
  280. }
  281. ret = cfg80211_testmode_reply(skb);
  282. if (ret < 0)
  283. goto out;
  284. out:
  285. mutex_unlock(&wl->mutex);
  286. return ret;
  287. }
  288. int wl1271_tm_cmd(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  289. void *data, int len)
  290. {
  291. struct wl1271 *wl = hw->priv;
  292. struct nlattr *tb[WL1271_TM_ATTR_MAX + 1];
  293. u32 nla_cmd;
  294. int err;
  295. err = nla_parse(tb, WL1271_TM_ATTR_MAX, data, len, wl1271_tm_policy);
  296. if (err)
  297. return err;
  298. if (!tb[WL1271_TM_ATTR_CMD_ID])
  299. return -EINVAL;
  300. nla_cmd = nla_get_u32(tb[WL1271_TM_ATTR_CMD_ID]);
  301. /* Only SET_PLT_MODE is allowed in case of mode PLT_CHIP_AWAKE */
  302. if (wl->plt_mode == PLT_CHIP_AWAKE &&
  303. nla_cmd != WL1271_TM_CMD_SET_PLT_MODE)
  304. return -EOPNOTSUPP;
  305. switch (nla_cmd) {
  306. case WL1271_TM_CMD_TEST:
  307. return wl1271_tm_cmd_test(wl, tb);
  308. case WL1271_TM_CMD_INTERROGATE:
  309. return wl1271_tm_cmd_interrogate(wl, tb);
  310. case WL1271_TM_CMD_CONFIGURE:
  311. return wl1271_tm_cmd_configure(wl, tb);
  312. case WL1271_TM_CMD_SET_PLT_MODE:
  313. return wl1271_tm_cmd_set_plt_mode(wl, tb);
  314. case WL1271_TM_CMD_GET_MAC:
  315. return wl12xx_tm_cmd_get_mac(wl, tb);
  316. default:
  317. return -EOPNOTSUPP;
  318. }
  319. }