cmd.c 9.5 KB

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