wl1251_cmd.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. #include "wl1251_cmd.h"
  2. #include <linux/module.h>
  3. #include <linux/crc7.h>
  4. #include "wl1251.h"
  5. #include "reg.h"
  6. #include "wl1251_io.h"
  7. #include "wl1251_ps.h"
  8. #include "wl1251_acx.h"
  9. /**
  10. * send command to firmware
  11. *
  12. * @wl: wl struct
  13. * @id: command id
  14. * @buf: buffer containing the command, must work with dma
  15. * @len: length of the buffer
  16. */
  17. int wl1251_cmd_send(struct wl1251 *wl, u16 id, void *buf, size_t len)
  18. {
  19. struct wl1251_cmd_header *cmd;
  20. unsigned long timeout;
  21. u32 intr;
  22. int ret = 0;
  23. cmd = buf;
  24. cmd->id = id;
  25. cmd->status = 0;
  26. WARN_ON(len % 4 != 0);
  27. wl1251_mem_write(wl, wl->cmd_box_addr, buf, len);
  28. wl1251_reg_write32(wl, ACX_REG_INTERRUPT_TRIG, INTR_TRIG_CMD);
  29. timeout = jiffies + msecs_to_jiffies(WL1251_COMMAND_TIMEOUT);
  30. intr = wl1251_reg_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
  31. while (!(intr & wl->chip.intr_cmd_complete)) {
  32. if (time_after(jiffies, timeout)) {
  33. wl1251_error("command complete timeout");
  34. ret = -ETIMEDOUT;
  35. goto out;
  36. }
  37. msleep(1);
  38. intr = wl1251_reg_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
  39. }
  40. wl1251_reg_write32(wl, ACX_REG_INTERRUPT_ACK,
  41. wl->chip.intr_cmd_complete);
  42. out:
  43. return ret;
  44. }
  45. /**
  46. * send test command to firmware
  47. *
  48. * @wl: wl struct
  49. * @buf: buffer containing the command, with all headers, must work with dma
  50. * @len: length of the buffer
  51. * @answer: is answer needed
  52. */
  53. int wl1251_cmd_test(struct wl1251 *wl, void *buf, size_t buf_len, u8 answer)
  54. {
  55. int ret;
  56. wl1251_debug(DEBUG_CMD, "cmd test");
  57. ret = wl1251_cmd_send(wl, CMD_TEST, buf, buf_len);
  58. if (ret < 0) {
  59. wl1251_warning("TEST command failed");
  60. return ret;
  61. }
  62. if (answer) {
  63. struct wl1251_command *cmd_answer;
  64. /*
  65. * The test command got in, we can read the answer.
  66. * The answer would be a wl1251_command, where the
  67. * parameter array contains the actual answer.
  68. */
  69. wl1251_mem_read(wl, wl->cmd_box_addr, buf, buf_len);
  70. cmd_answer = buf;
  71. if (cmd_answer->header.status != CMD_STATUS_SUCCESS)
  72. wl1251_error("TEST command answer error: %d",
  73. cmd_answer->header.status);
  74. }
  75. return 0;
  76. }
  77. /**
  78. * read acx from firmware
  79. *
  80. * @wl: wl struct
  81. * @id: acx id
  82. * @buf: buffer for the response, including all headers, must work with dma
  83. * @len: lenght of buf
  84. */
  85. int wl1251_cmd_interrogate(struct wl1251 *wl, u16 id, void *buf, size_t len)
  86. {
  87. struct acx_header *acx = buf;
  88. int ret;
  89. wl1251_debug(DEBUG_CMD, "cmd interrogate");
  90. acx->id = id;
  91. /* payload length, does not include any headers */
  92. acx->len = len - sizeof(*acx);
  93. ret = wl1251_cmd_send(wl, CMD_INTERROGATE, acx, sizeof(*acx));
  94. if (ret < 0) {
  95. wl1251_error("INTERROGATE command failed");
  96. goto out;
  97. }
  98. /* the interrogate command got in, we can read the answer */
  99. wl1251_mem_read(wl, wl->cmd_box_addr, buf, len);
  100. acx = buf;
  101. if (acx->cmd.status != CMD_STATUS_SUCCESS)
  102. wl1251_error("INTERROGATE command error: %d",
  103. acx->cmd.status);
  104. out:
  105. return ret;
  106. }
  107. /**
  108. * write acx value to firmware
  109. *
  110. * @wl: wl struct
  111. * @id: acx id
  112. * @buf: buffer containing acx, including all headers, must work with dma
  113. * @len: length of buf
  114. */
  115. int wl1251_cmd_configure(struct wl1251 *wl, u16 id, void *buf, size_t len)
  116. {
  117. struct acx_header *acx = buf;
  118. int ret;
  119. wl1251_debug(DEBUG_CMD, "cmd configure");
  120. acx->id = id;
  121. /* payload length, does not include any headers */
  122. acx->len = len - sizeof(*acx);
  123. ret = wl1251_cmd_send(wl, CMD_CONFIGURE, acx, len);
  124. if (ret < 0) {
  125. wl1251_warning("CONFIGURE command NOK");
  126. return ret;
  127. }
  128. return 0;
  129. }
  130. int wl1251_cmd_vbm(struct wl1251 *wl, u8 identity,
  131. void *bitmap, u16 bitmap_len, u8 bitmap_control)
  132. {
  133. struct wl1251_cmd_vbm_update *vbm;
  134. int ret;
  135. wl1251_debug(DEBUG_CMD, "cmd vbm");
  136. vbm = kzalloc(sizeof(*vbm), GFP_KERNEL);
  137. if (!vbm) {
  138. ret = -ENOMEM;
  139. goto out;
  140. }
  141. /* Count and period will be filled by the target */
  142. vbm->tim.bitmap_ctrl = bitmap_control;
  143. if (bitmap_len > PARTIAL_VBM_MAX) {
  144. wl1251_warning("cmd vbm len is %d B, truncating to %d",
  145. bitmap_len, PARTIAL_VBM_MAX);
  146. bitmap_len = PARTIAL_VBM_MAX;
  147. }
  148. memcpy(vbm->tim.pvb_field, bitmap, bitmap_len);
  149. vbm->tim.identity = identity;
  150. vbm->tim.length = bitmap_len + 3;
  151. vbm->len = cpu_to_le16(bitmap_len + 5);
  152. ret = wl1251_cmd_send(wl, CMD_VBM, vbm, sizeof(*vbm));
  153. if (ret < 0) {
  154. wl1251_error("VBM command failed");
  155. goto out;
  156. }
  157. out:
  158. kfree(vbm);
  159. return 0;
  160. }
  161. int wl1251_cmd_data_path(struct wl1251 *wl, u8 channel, bool enable)
  162. {
  163. struct cmd_enabledisable_path *cmd;
  164. int ret;
  165. u16 cmd_rx, cmd_tx;
  166. wl1251_debug(DEBUG_CMD, "cmd data path");
  167. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  168. if (!cmd) {
  169. ret = -ENOMEM;
  170. goto out;
  171. }
  172. cmd->channel = channel;
  173. if (enable) {
  174. cmd_rx = CMD_ENABLE_RX;
  175. cmd_tx = CMD_ENABLE_TX;
  176. } else {
  177. cmd_rx = CMD_DISABLE_RX;
  178. cmd_tx = CMD_DISABLE_TX;
  179. }
  180. ret = wl1251_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd));
  181. if (ret < 0) {
  182. wl1251_error("rx %s cmd for channel %d failed",
  183. enable ? "start" : "stop", channel);
  184. goto out;
  185. }
  186. wl1251_debug(DEBUG_BOOT, "rx %s cmd channel %d",
  187. enable ? "start" : "stop", channel);
  188. ret = wl1251_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd));
  189. if (ret < 0) {
  190. wl1251_error("tx %s cmd for channel %d failed",
  191. enable ? "start" : "stop", channel);
  192. return ret;
  193. }
  194. wl1251_debug(DEBUG_BOOT, "tx %s cmd channel %d",
  195. enable ? "start" : "stop", channel);
  196. out:
  197. kfree(cmd);
  198. return ret;
  199. }
  200. int wl1251_cmd_join(struct wl1251 *wl, u8 bss_type, u8 dtim_interval,
  201. u16 beacon_interval, u8 wait)
  202. {
  203. unsigned long timeout;
  204. struct cmd_join *join;
  205. int ret, i;
  206. u8 *bssid;
  207. join = kzalloc(sizeof(*join), GFP_KERNEL);
  208. if (!join) {
  209. ret = -ENOMEM;
  210. goto out;
  211. }
  212. /* FIXME: this should be in main.c */
  213. ret = wl1251_acx_frame_rates(wl, DEFAULT_HW_GEN_TX_RATE,
  214. DEFAULT_HW_GEN_MODULATION_TYPE,
  215. wl->tx_mgmt_frm_rate,
  216. wl->tx_mgmt_frm_mod);
  217. if (ret < 0)
  218. goto out;
  219. wl1251_debug(DEBUG_CMD, "cmd join");
  220. /* Reverse order BSSID */
  221. bssid = (u8 *) &join->bssid_lsb;
  222. for (i = 0; i < ETH_ALEN; i++)
  223. bssid[i] = wl->bssid[ETH_ALEN - i - 1];
  224. join->rx_config_options = wl->rx_config;
  225. join->rx_filter_options = wl->rx_filter;
  226. join->basic_rate_set = RATE_MASK_1MBPS | RATE_MASK_2MBPS |
  227. RATE_MASK_5_5MBPS | RATE_MASK_11MBPS;
  228. join->beacon_interval = beacon_interval;
  229. join->dtim_interval = dtim_interval;
  230. join->bss_type = bss_type;
  231. join->channel = wl->channel;
  232. join->ctrl = JOIN_CMD_CTRL_TX_FLUSH;
  233. ret = wl1251_cmd_send(wl, CMD_START_JOIN, join, sizeof(*join));
  234. if (ret < 0) {
  235. wl1251_error("failed to initiate cmd join");
  236. goto out;
  237. }
  238. timeout = msecs_to_jiffies(JOIN_TIMEOUT);
  239. /*
  240. * ugly hack: we should wait for JOIN_EVENT_COMPLETE_ID but to
  241. * simplify locking we just sleep instead, for now
  242. */
  243. if (wait)
  244. msleep(10);
  245. out:
  246. kfree(join);
  247. return ret;
  248. }
  249. int wl1251_cmd_ps_mode(struct wl1251 *wl, u8 ps_mode)
  250. {
  251. struct wl1251_cmd_ps_params *ps_params = NULL;
  252. int ret = 0;
  253. /* FIXME: this should be in ps.c */
  254. ret = wl1251_acx_wake_up_conditions(wl, WAKE_UP_EVENT_DTIM_BITMAP,
  255. wl->listen_int);
  256. if (ret < 0) {
  257. wl1251_error("couldn't set wake up conditions");
  258. goto out;
  259. }
  260. wl1251_debug(DEBUG_CMD, "cmd set ps mode");
  261. ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
  262. if (!ps_params) {
  263. ret = -ENOMEM;
  264. goto out;
  265. }
  266. ps_params->ps_mode = ps_mode;
  267. ps_params->send_null_data = 1;
  268. ps_params->retries = 5;
  269. ps_params->hang_over_period = 128;
  270. ps_params->null_data_rate = 1; /* 1 Mbps */
  271. ret = wl1251_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
  272. sizeof(*ps_params));
  273. if (ret < 0) {
  274. wl1251_error("cmd set_ps_mode failed");
  275. goto out;
  276. }
  277. out:
  278. kfree(ps_params);
  279. return ret;
  280. }
  281. int wl1251_cmd_read_memory(struct wl1251 *wl, u32 addr, void *answer,
  282. size_t len)
  283. {
  284. struct cmd_read_write_memory *cmd;
  285. int ret = 0;
  286. wl1251_debug(DEBUG_CMD, "cmd read memory");
  287. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  288. if (!cmd) {
  289. ret = -ENOMEM;
  290. goto out;
  291. }
  292. WARN_ON(len > MAX_READ_SIZE);
  293. len = min_t(size_t, len, MAX_READ_SIZE);
  294. cmd->addr = addr;
  295. cmd->size = len;
  296. ret = wl1251_cmd_send(wl, CMD_READ_MEMORY, cmd, sizeof(*cmd));
  297. if (ret < 0) {
  298. wl1251_error("read memory command failed: %d", ret);
  299. goto out;
  300. }
  301. /* the read command got in, we can now read the answer */
  302. wl1251_mem_read(wl, wl->cmd_box_addr, cmd, sizeof(*cmd));
  303. if (cmd->header.status != CMD_STATUS_SUCCESS)
  304. wl1251_error("error in read command result: %d",
  305. cmd->header.status);
  306. memcpy(answer, cmd->value, len);
  307. out:
  308. kfree(cmd);
  309. return ret;
  310. }
  311. int wl1251_cmd_template_set(struct wl1251 *wl, u16 cmd_id,
  312. void *buf, size_t buf_len)
  313. {
  314. struct wl1251_cmd_packet_template *cmd;
  315. size_t cmd_len;
  316. int ret = 0;
  317. wl1251_debug(DEBUG_CMD, "cmd template %d", cmd_id);
  318. WARN_ON(buf_len > WL1251_MAX_TEMPLATE_SIZE);
  319. buf_len = min_t(size_t, buf_len, WL1251_MAX_TEMPLATE_SIZE);
  320. cmd_len = ALIGN(sizeof(*cmd) + buf_len, 4);
  321. cmd = kzalloc(cmd_len, GFP_KERNEL);
  322. if (!cmd) {
  323. ret = -ENOMEM;
  324. goto out;
  325. }
  326. cmd->size = cpu_to_le16(buf_len);
  327. if (buf)
  328. memcpy(cmd->data, buf, buf_len);
  329. ret = wl1251_cmd_send(wl, cmd_id, cmd, cmd_len);
  330. if (ret < 0) {
  331. wl1251_warning("cmd set_template failed: %d", ret);
  332. goto out;
  333. }
  334. out:
  335. kfree(cmd);
  336. return ret;
  337. }