wl1251_cmd.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. #include "wl1251_cmd.h"
  2. #include <linux/module.h>
  3. #include <linux/crc7.h>
  4. #include "wl1251.h"
  5. #include "wl1251_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 & WL1251_ACX_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. WL1251_ACX_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 channel,
  201. u16 beacon_interval, u8 dtim_interval)
  202. {
  203. struct cmd_join *join;
  204. int ret, i;
  205. u8 *bssid;
  206. join = kzalloc(sizeof(*join), GFP_KERNEL);
  207. if (!join) {
  208. ret = -ENOMEM;
  209. goto out;
  210. }
  211. wl1251_debug(DEBUG_CMD, "cmd join%s ch %d %d/%d",
  212. bss_type == BSS_TYPE_IBSS ? " ibss" : "",
  213. channel, beacon_interval, dtim_interval);
  214. /* Reverse order BSSID */
  215. bssid = (u8 *) &join->bssid_lsb;
  216. for (i = 0; i < ETH_ALEN; i++)
  217. bssid[i] = wl->bssid[ETH_ALEN - i - 1];
  218. join->rx_config_options = wl->rx_config;
  219. join->rx_filter_options = wl->rx_filter;
  220. /*
  221. * FIXME: disable temporarily all filters because after commit
  222. * 9cef8737 "mac80211: fix managed mode BSSID handling" broke
  223. * association. The filter logic needs to be implemented properly
  224. * and once that is done, this hack can be removed.
  225. */
  226. join->rx_config_options = 0;
  227. join->rx_filter_options = WL1251_DEFAULT_RX_FILTER;
  228. join->basic_rate_set = RATE_MASK_1MBPS | RATE_MASK_2MBPS |
  229. RATE_MASK_5_5MBPS | RATE_MASK_11MBPS;
  230. join->beacon_interval = beacon_interval;
  231. join->dtim_interval = dtim_interval;
  232. join->bss_type = bss_type;
  233. join->channel = channel;
  234. join->ctrl = JOIN_CMD_CTRL_TX_FLUSH;
  235. ret = wl1251_cmd_send(wl, CMD_START_JOIN, join, sizeof(*join));
  236. if (ret < 0) {
  237. wl1251_error("failed to initiate cmd join");
  238. goto out;
  239. }
  240. out:
  241. kfree(join);
  242. return ret;
  243. }
  244. int wl1251_cmd_ps_mode(struct wl1251 *wl, u8 ps_mode)
  245. {
  246. struct wl1251_cmd_ps_params *ps_params = NULL;
  247. int ret = 0;
  248. wl1251_debug(DEBUG_CMD, "cmd set ps mode");
  249. ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
  250. if (!ps_params) {
  251. ret = -ENOMEM;
  252. goto out;
  253. }
  254. ps_params->ps_mode = ps_mode;
  255. ps_params->send_null_data = 1;
  256. ps_params->retries = 5;
  257. ps_params->hang_over_period = 128;
  258. ps_params->null_data_rate = 1; /* 1 Mbps */
  259. ret = wl1251_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
  260. sizeof(*ps_params));
  261. if (ret < 0) {
  262. wl1251_error("cmd set_ps_mode failed");
  263. goto out;
  264. }
  265. out:
  266. kfree(ps_params);
  267. return ret;
  268. }
  269. int wl1251_cmd_read_memory(struct wl1251 *wl, u32 addr, void *answer,
  270. size_t len)
  271. {
  272. struct cmd_read_write_memory *cmd;
  273. int ret = 0;
  274. wl1251_debug(DEBUG_CMD, "cmd read memory");
  275. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  276. if (!cmd) {
  277. ret = -ENOMEM;
  278. goto out;
  279. }
  280. WARN_ON(len > MAX_READ_SIZE);
  281. len = min_t(size_t, len, MAX_READ_SIZE);
  282. cmd->addr = addr;
  283. cmd->size = len;
  284. ret = wl1251_cmd_send(wl, CMD_READ_MEMORY, cmd, sizeof(*cmd));
  285. if (ret < 0) {
  286. wl1251_error("read memory command failed: %d", ret);
  287. goto out;
  288. }
  289. /* the read command got in, we can now read the answer */
  290. wl1251_mem_read(wl, wl->cmd_box_addr, cmd, sizeof(*cmd));
  291. if (cmd->header.status != CMD_STATUS_SUCCESS)
  292. wl1251_error("error in read command result: %d",
  293. cmd->header.status);
  294. memcpy(answer, cmd->value, len);
  295. out:
  296. kfree(cmd);
  297. return ret;
  298. }
  299. int wl1251_cmd_template_set(struct wl1251 *wl, u16 cmd_id,
  300. void *buf, size_t buf_len)
  301. {
  302. struct wl1251_cmd_packet_template *cmd;
  303. size_t cmd_len;
  304. int ret = 0;
  305. wl1251_debug(DEBUG_CMD, "cmd template %d", cmd_id);
  306. WARN_ON(buf_len > WL1251_MAX_TEMPLATE_SIZE);
  307. buf_len = min_t(size_t, buf_len, WL1251_MAX_TEMPLATE_SIZE);
  308. cmd_len = ALIGN(sizeof(*cmd) + buf_len, 4);
  309. cmd = kzalloc(cmd_len, GFP_KERNEL);
  310. if (!cmd) {
  311. ret = -ENOMEM;
  312. goto out;
  313. }
  314. cmd->size = cpu_to_le16(buf_len);
  315. if (buf)
  316. memcpy(cmd->data, buf, buf_len);
  317. ret = wl1251_cmd_send(wl, cmd_id, cmd, cmd_len);
  318. if (ret < 0) {
  319. wl1251_warning("cmd set_template failed: %d", ret);
  320. goto out;
  321. }
  322. out:
  323. kfree(cmd);
  324. return ret;
  325. }