cmd.c 9.2 KB

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