wl1251_cmd.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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 & 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, u16 beacon_interval,
  201. u8 dtim_interval, bool 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%s %d %d%s",
  220. bss_type == BSS_TYPE_IBSS ? " ibss" : "",
  221. beacon_interval, dtim_interval,
  222. wait ? " wait" : "");
  223. /* Reverse order BSSID */
  224. bssid = (u8 *) &join->bssid_lsb;
  225. for (i = 0; i < ETH_ALEN; i++)
  226. bssid[i] = wl->bssid[ETH_ALEN - i - 1];
  227. join->rx_config_options = wl->rx_config;
  228. join->rx_filter_options = wl->rx_filter;
  229. join->basic_rate_set = RATE_MASK_1MBPS | RATE_MASK_2MBPS |
  230. RATE_MASK_5_5MBPS | RATE_MASK_11MBPS;
  231. join->beacon_interval = beacon_interval;
  232. join->dtim_interval = dtim_interval;
  233. join->bss_type = bss_type;
  234. join->channel = wl->channel;
  235. join->ctrl = JOIN_CMD_CTRL_TX_FLUSH;
  236. ret = wl1251_cmd_send(wl, CMD_START_JOIN, join, sizeof(*join));
  237. if (ret < 0) {
  238. wl1251_error("failed to initiate cmd join");
  239. goto out;
  240. }
  241. timeout = msecs_to_jiffies(JOIN_TIMEOUT);
  242. /*
  243. * ugly hack: we should wait for JOIN_EVENT_COMPLETE_ID but to
  244. * simplify locking we just sleep instead, for now
  245. */
  246. if (wait)
  247. msleep(10);
  248. out:
  249. kfree(join);
  250. return ret;
  251. }
  252. int wl1251_cmd_ps_mode(struct wl1251 *wl, u8 ps_mode)
  253. {
  254. struct wl1251_cmd_ps_params *ps_params = NULL;
  255. int ret = 0;
  256. /* FIXME: this should be in ps.c */
  257. ret = wl1251_acx_wake_up_conditions(wl, WAKE_UP_EVENT_DTIM_BITMAP,
  258. wl->listen_int);
  259. if (ret < 0) {
  260. wl1251_error("couldn't set wake up conditions");
  261. goto out;
  262. }
  263. wl1251_debug(DEBUG_CMD, "cmd set ps mode");
  264. ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
  265. if (!ps_params) {
  266. ret = -ENOMEM;
  267. goto out;
  268. }
  269. ps_params->ps_mode = ps_mode;
  270. ps_params->send_null_data = 1;
  271. ps_params->retries = 5;
  272. ps_params->hang_over_period = 128;
  273. ps_params->null_data_rate = 1; /* 1 Mbps */
  274. ret = wl1251_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
  275. sizeof(*ps_params));
  276. if (ret < 0) {
  277. wl1251_error("cmd set_ps_mode failed");
  278. goto out;
  279. }
  280. out:
  281. kfree(ps_params);
  282. return ret;
  283. }
  284. int wl1251_cmd_read_memory(struct wl1251 *wl, u32 addr, void *answer,
  285. size_t len)
  286. {
  287. struct cmd_read_write_memory *cmd;
  288. int ret = 0;
  289. wl1251_debug(DEBUG_CMD, "cmd read memory");
  290. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  291. if (!cmd) {
  292. ret = -ENOMEM;
  293. goto out;
  294. }
  295. WARN_ON(len > MAX_READ_SIZE);
  296. len = min_t(size_t, len, MAX_READ_SIZE);
  297. cmd->addr = addr;
  298. cmd->size = len;
  299. ret = wl1251_cmd_send(wl, CMD_READ_MEMORY, cmd, sizeof(*cmd));
  300. if (ret < 0) {
  301. wl1251_error("read memory command failed: %d", ret);
  302. goto out;
  303. }
  304. /* the read command got in, we can now read the answer */
  305. wl1251_mem_read(wl, wl->cmd_box_addr, cmd, sizeof(*cmd));
  306. if (cmd->header.status != CMD_STATUS_SUCCESS)
  307. wl1251_error("error in read command result: %d",
  308. cmd->header.status);
  309. memcpy(answer, cmd->value, len);
  310. out:
  311. kfree(cmd);
  312. return ret;
  313. }
  314. int wl1251_cmd_template_set(struct wl1251 *wl, u16 cmd_id,
  315. void *buf, size_t buf_len)
  316. {
  317. struct wl1251_cmd_packet_template *cmd;
  318. size_t cmd_len;
  319. int ret = 0;
  320. wl1251_debug(DEBUG_CMD, "cmd template %d", cmd_id);
  321. WARN_ON(buf_len > WL1251_MAX_TEMPLATE_SIZE);
  322. buf_len = min_t(size_t, buf_len, WL1251_MAX_TEMPLATE_SIZE);
  323. cmd_len = ALIGN(sizeof(*cmd) + buf_len, 4);
  324. cmd = kzalloc(cmd_len, GFP_KERNEL);
  325. if (!cmd) {
  326. ret = -ENOMEM;
  327. goto out;
  328. }
  329. cmd->size = cpu_to_le16(buf_len);
  330. if (buf)
  331. memcpy(cmd->data, buf, buf_len);
  332. ret = wl1251_cmd_send(wl, cmd_id, cmd, cmd_len);
  333. if (ret < 0) {
  334. wl1251_warning("cmd set_template failed: %d", ret);
  335. goto out;
  336. }
  337. out:
  338. kfree(cmd);
  339. return ret;
  340. }