cmd.c 9.0 KB

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