cmd.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. int wl12xx_cmd_send(struct wl12xx *wl, u16 type, void *buf, size_t buf_len)
  11. {
  12. struct wl12xx_command cmd;
  13. unsigned long timeout;
  14. size_t cmd_len;
  15. u32 intr;
  16. int ret = 0;
  17. memset(&cmd, 0, sizeof(cmd));
  18. cmd.id = type;
  19. cmd.status = 0;
  20. memcpy(cmd.parameters, buf, buf_len);
  21. cmd_len = ALIGN(buf_len, 4) + CMDMBOX_HEADER_LEN;
  22. wl12xx_ps_elp_wakeup(wl);
  23. wl12xx_spi_mem_write(wl, wl->cmd_box_addr, &cmd, cmd_len);
  24. wl12xx_reg_write32(wl, ACX_REG_INTERRUPT_TRIG, INTR_TRIG_CMD);
  25. timeout = jiffies + msecs_to_jiffies(WL12XX_COMMAND_TIMEOUT);
  26. intr = wl12xx_reg_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
  27. while (!(intr & wl->chip.intr_cmd_complete)) {
  28. if (time_after(jiffies, timeout)) {
  29. wl12xx_error("command complete timeout");
  30. ret = -ETIMEDOUT;
  31. goto out;
  32. }
  33. msleep(1);
  34. intr = wl12xx_reg_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
  35. }
  36. wl12xx_reg_write32(wl, ACX_REG_INTERRUPT_ACK,
  37. wl->chip.intr_cmd_complete);
  38. out:
  39. wl12xx_ps_elp_sleep(wl);
  40. return ret;
  41. }
  42. int wl12xx_cmd_test(struct wl12xx *wl, void *buf, size_t buf_len, u8 answer)
  43. {
  44. int ret;
  45. wl12xx_debug(DEBUG_CMD, "cmd test");
  46. ret = wl12xx_cmd_send(wl, CMD_TEST, buf, buf_len);
  47. if (ret < 0) {
  48. wl12xx_warning("TEST command failed");
  49. return ret;
  50. }
  51. if (answer) {
  52. struct wl12xx_command *cmd_answer;
  53. /*
  54. * The test command got in, we can read the answer.
  55. * The answer would be a wl12xx_command, where the
  56. * parameter array contains the actual answer.
  57. */
  58. wl12xx_ps_elp_wakeup(wl);
  59. wl12xx_spi_mem_read(wl, wl->cmd_box_addr, buf, buf_len);
  60. wl12xx_ps_elp_sleep(wl);
  61. cmd_answer = buf;
  62. if (cmd_answer->status != CMD_STATUS_SUCCESS)
  63. wl12xx_error("TEST command answer error: %d",
  64. cmd_answer->status);
  65. }
  66. return 0;
  67. }
  68. int wl12xx_cmd_interrogate(struct wl12xx *wl, u16 ie_id, u16 ie_len,
  69. void *answer)
  70. {
  71. struct wl12xx_command *cmd;
  72. struct acx_header header;
  73. int ret;
  74. wl12xx_debug(DEBUG_CMD, "cmd interrogate");
  75. header.id = ie_id;
  76. header.len = ie_len - sizeof(header);
  77. ret = wl12xx_cmd_send(wl, CMD_INTERROGATE, &header, sizeof(header));
  78. if (ret < 0) {
  79. wl12xx_error("INTERROGATE command failed");
  80. return ret;
  81. }
  82. wl12xx_ps_elp_wakeup(wl);
  83. /* the interrogate command got in, we can read the answer */
  84. wl12xx_spi_mem_read(wl, wl->cmd_box_addr, answer,
  85. CMDMBOX_HEADER_LEN + ie_len);
  86. wl12xx_ps_elp_sleep(wl);
  87. cmd = answer;
  88. if (cmd->status != CMD_STATUS_SUCCESS)
  89. wl12xx_error("INTERROGATE command error: %d",
  90. cmd->status);
  91. return 0;
  92. }
  93. int wl12xx_cmd_configure(struct wl12xx *wl, void *ie, int ie_len)
  94. {
  95. int ret;
  96. wl12xx_debug(DEBUG_CMD, "cmd configure");
  97. ret = wl12xx_cmd_send(wl, CMD_CONFIGURE, ie,
  98. ie_len);
  99. if (ret < 0) {
  100. wl12xx_warning("CONFIGURE command NOK");
  101. return ret;
  102. }
  103. return 0;
  104. }
  105. int wl12xx_cmd_vbm(struct wl12xx *wl, u8 identity,
  106. void *bitmap, u16 bitmap_len, u8 bitmap_control)
  107. {
  108. struct vbm_update_request vbm;
  109. int ret;
  110. wl12xx_debug(DEBUG_CMD, "cmd vbm");
  111. /* Count and period will be filled by the target */
  112. vbm.tim.bitmap_ctrl = bitmap_control;
  113. if (bitmap_len > PARTIAL_VBM_MAX) {
  114. wl12xx_warning("cmd vbm len is %d B, truncating to %d",
  115. bitmap_len, PARTIAL_VBM_MAX);
  116. bitmap_len = PARTIAL_VBM_MAX;
  117. }
  118. memcpy(vbm.tim.pvb_field, bitmap, bitmap_len);
  119. vbm.tim.identity = identity;
  120. vbm.tim.length = bitmap_len + 3;
  121. vbm.len = cpu_to_le16(bitmap_len + 5);
  122. ret = wl12xx_cmd_send(wl, CMD_VBM, &vbm, sizeof(vbm));
  123. if (ret < 0) {
  124. wl12xx_error("VBM command failed");
  125. return ret;
  126. }
  127. return 0;
  128. }
  129. int wl12xx_cmd_data_path(struct wl12xx *wl, u8 channel, u8 enable)
  130. {
  131. int ret;
  132. u16 cmd_rx, cmd_tx;
  133. wl12xx_debug(DEBUG_CMD, "cmd data path");
  134. if (enable) {
  135. cmd_rx = CMD_ENABLE_RX;
  136. cmd_tx = CMD_ENABLE_TX;
  137. } else {
  138. cmd_rx = CMD_DISABLE_RX;
  139. cmd_tx = CMD_DISABLE_TX;
  140. }
  141. ret = wl12xx_cmd_send(wl, cmd_rx, &channel, sizeof(channel));
  142. if (ret < 0) {
  143. wl12xx_error("rx %s cmd for channel %d failed",
  144. enable ? "start" : "stop", channel);
  145. return ret;
  146. }
  147. wl12xx_debug(DEBUG_BOOT, "rx %s cmd channel %d",
  148. enable ? "start" : "stop", channel);
  149. ret = wl12xx_cmd_send(wl, cmd_tx, &channel, sizeof(channel));
  150. if (ret < 0) {
  151. wl12xx_error("tx %s cmd for channel %d failed",
  152. enable ? "start" : "stop", channel);
  153. return ret;
  154. }
  155. wl12xx_debug(DEBUG_BOOT, "tx %s cmd channel %d",
  156. enable ? "start" : "stop", channel);
  157. return 0;
  158. }
  159. int wl12xx_cmd_join(struct wl12xx *wl, u8 bss_type, u8 dtim_interval,
  160. u16 beacon_interval, u8 wait)
  161. {
  162. unsigned long timeout;
  163. struct cmd_join join = {};
  164. int ret, i;
  165. u8 *bssid;
  166. /* FIXME: this should be in main.c */
  167. ret = wl12xx_acx_frame_rates(wl, DEFAULT_HW_GEN_TX_RATE,
  168. DEFAULT_HW_GEN_MODULATION_TYPE,
  169. wl->tx_mgmt_frm_rate,
  170. wl->tx_mgmt_frm_mod);
  171. if (ret < 0)
  172. return ret;
  173. wl12xx_debug(DEBUG_CMD, "cmd join");
  174. /* Reverse order BSSID */
  175. bssid = (u8 *)&join.bssid_lsb;
  176. for (i = 0; i < ETH_ALEN; i++)
  177. bssid[i] = wl->bssid[ETH_ALEN - i - 1];
  178. join.rx_config_options = wl->rx_config;
  179. join.rx_filter_options = wl->rx_filter;
  180. join.basic_rate_set = RATE_MASK_1MBPS | RATE_MASK_2MBPS |
  181. RATE_MASK_5_5MBPS | RATE_MASK_11MBPS;
  182. join.beacon_interval = beacon_interval;
  183. join.dtim_interval = dtim_interval;
  184. join.bss_type = bss_type;
  185. join.channel = wl->channel;
  186. join.ctrl = JOIN_CMD_CTRL_TX_FLUSH;
  187. ret = wl12xx_cmd_send(wl, CMD_START_JOIN, &join, sizeof(join));
  188. if (ret < 0) {
  189. wl12xx_error("failed to initiate cmd join");
  190. return ret;
  191. }
  192. timeout = msecs_to_jiffies(JOIN_TIMEOUT);
  193. /*
  194. * ugly hack: we should wait for JOIN_EVENT_COMPLETE_ID but to
  195. * simplify locking we just sleep instead, for now
  196. */
  197. if (wait)
  198. msleep(10);
  199. return 0;
  200. }
  201. int wl12xx_cmd_ps_mode(struct wl12xx *wl, u8 ps_mode)
  202. {
  203. int ret;
  204. struct acx_ps_params ps_params;
  205. /* FIXME: this should be in ps.c */
  206. ret = wl12xx_acx_wake_up_conditions(wl, wl->listen_int);
  207. if (ret < 0) {
  208. wl12xx_error("Couldnt set wake up conditions");
  209. return ret;
  210. }
  211. wl12xx_debug(DEBUG_CMD, "cmd set ps mode");
  212. ps_params.ps_mode = ps_mode;
  213. ps_params.send_null_data = 1;
  214. ps_params.retries = 5;
  215. ps_params.hang_over_period = 128;
  216. ps_params.null_data_rate = 1; /* 1 Mbps */
  217. ret = wl12xx_cmd_send(wl, CMD_SET_PS_MODE, &ps_params,
  218. sizeof(ps_params));
  219. if (ret < 0) {
  220. wl12xx_error("cmd set_ps_mode failed");
  221. return ret;
  222. }
  223. return 0;
  224. }
  225. int wl12xx_cmd_read_memory(struct wl12xx *wl, u32 addr, u32 len, void *answer)
  226. {
  227. struct cmd_read_write_memory mem_cmd, *mem_answer;
  228. struct wl12xx_command cmd;
  229. int ret;
  230. wl12xx_debug(DEBUG_CMD, "cmd read memory");
  231. memset(&mem_cmd, 0, sizeof(mem_cmd));
  232. mem_cmd.addr = addr;
  233. mem_cmd.size = len;
  234. ret = wl12xx_cmd_send(wl, CMD_READ_MEMORY, &mem_cmd, sizeof(mem_cmd));
  235. if (ret < 0) {
  236. wl12xx_error("read memory command failed: %d", ret);
  237. return ret;
  238. }
  239. /* the read command got in, we can now read the answer */
  240. wl12xx_spi_mem_read(wl, wl->cmd_box_addr, &cmd,
  241. CMDMBOX_HEADER_LEN + sizeof(mem_cmd));
  242. if (cmd.status != CMD_STATUS_SUCCESS)
  243. wl12xx_error("error in read command result: %d", cmd.status);
  244. mem_answer = (struct cmd_read_write_memory *) cmd.parameters;
  245. memcpy(answer, mem_answer->value, len);
  246. return 0;
  247. }
  248. int wl12xx_cmd_template_set(struct wl12xx *wl, u16 cmd_id,
  249. void *buf, size_t buf_len)
  250. {
  251. struct wl12xx_cmd_packet_template template;
  252. int ret;
  253. wl12xx_debug(DEBUG_CMD, "cmd template %d", cmd_id);
  254. memset(&template, 0, sizeof(template));
  255. WARN_ON(buf_len > WL12XX_MAX_TEMPLATE_SIZE);
  256. buf_len = min_t(size_t, buf_len, WL12XX_MAX_TEMPLATE_SIZE);
  257. template.size = cpu_to_le16(buf_len);
  258. if (buf)
  259. memcpy(template.template, buf, buf_len);
  260. ret = wl12xx_cmd_send(wl, cmd_id, &template,
  261. sizeof(template.size) + buf_len);
  262. if (ret < 0) {
  263. wl12xx_warning("cmd set_template failed: %d", ret);
  264. return ret;
  265. }
  266. return 0;
  267. }