PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/drivers/net/wireless/wl1251/cmd.c

https://github.com/mstsirkin/linux
C | 496 lines | 344 code | 103 blank | 49 comment | 40 complexity | 686c8fed451e8a996a1b0a0aff315804 MD5 | raw file
  1. #include "cmd.h"
  2. #include <linux/module.h>
  3. #include <linux/slab.h>
  4. #include <linux/crc7.h>
  5. #include "wl1251.h"
  6. #include "reg.h"
  7. #include "io.h"
  8. #include "ps.h"
  9. #include "acx.h"
  10. /**
  11. * send command to firmware
  12. *
  13. * @wl: wl struct
  14. * @id: command id
  15. * @buf: buffer containing the command, must work with dma
  16. * @len: length of the buffer
  17. */
  18. int wl1251_cmd_send(struct wl1251 *wl, u16 id, void *buf, size_t len)
  19. {
  20. struct wl1251_cmd_header *cmd;
  21. unsigned long timeout;
  22. u32 intr;
  23. int ret = 0;
  24. cmd = buf;
  25. cmd->id = id;
  26. cmd->status = 0;
  27. WARN_ON(len % 4 != 0);
  28. wl1251_mem_write(wl, wl->cmd_box_addr, buf, len);
  29. wl1251_reg_write32(wl, ACX_REG_INTERRUPT_TRIG, INTR_TRIG_CMD);
  30. timeout = jiffies + msecs_to_jiffies(WL1251_COMMAND_TIMEOUT);
  31. intr = wl1251_reg_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
  32. while (!(intr & WL1251_ACX_INTR_CMD_COMPLETE)) {
  33. if (time_after(jiffies, timeout)) {
  34. wl1251_error("command complete timeout");
  35. ret = -ETIMEDOUT;
  36. goto out;
  37. }
  38. msleep(1);
  39. intr = wl1251_reg_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
  40. }
  41. wl1251_reg_write32(wl, ACX_REG_INTERRUPT_ACK,
  42. WL1251_ACX_INTR_CMD_COMPLETE);
  43. out:
  44. return ret;
  45. }
  46. /**
  47. * send test command to firmware
  48. *
  49. * @wl: wl struct
  50. * @buf: buffer containing the command, with all headers, must work with dma
  51. * @len: length of the buffer
  52. * @answer: is answer needed
  53. */
  54. int wl1251_cmd_test(struct wl1251 *wl, void *buf, size_t buf_len, u8 answer)
  55. {
  56. int ret;
  57. wl1251_debug(DEBUG_CMD, "cmd test");
  58. ret = wl1251_cmd_send(wl, CMD_TEST, buf, buf_len);
  59. if (ret < 0) {
  60. wl1251_warning("TEST command failed");
  61. return ret;
  62. }
  63. if (answer) {
  64. struct wl1251_command *cmd_answer;
  65. /*
  66. * The test command got in, we can read the answer.
  67. * The answer would be a wl1251_command, where the
  68. * parameter array contains the actual answer.
  69. */
  70. wl1251_mem_read(wl, wl->cmd_box_addr, buf, buf_len);
  71. cmd_answer = buf;
  72. if (cmd_answer->header.status != CMD_STATUS_SUCCESS)
  73. wl1251_error("TEST command answer error: %d",
  74. cmd_answer->header.status);
  75. }
  76. return 0;
  77. }
  78. /**
  79. * read acx from firmware
  80. *
  81. * @wl: wl struct
  82. * @id: acx id
  83. * @buf: buffer for the response, including all headers, must work with dma
  84. * @len: length of buf
  85. */
  86. int wl1251_cmd_interrogate(struct wl1251 *wl, u16 id, void *buf, size_t len)
  87. {
  88. struct acx_header *acx = buf;
  89. int ret;
  90. wl1251_debug(DEBUG_CMD, "cmd interrogate");
  91. acx->id = id;
  92. /* payload length, does not include any headers */
  93. acx->len = len - sizeof(*acx);
  94. ret = wl1251_cmd_send(wl, CMD_INTERROGATE, acx, sizeof(*acx));
  95. if (ret < 0) {
  96. wl1251_error("INTERROGATE command failed");
  97. goto out;
  98. }
  99. /* the interrogate command got in, we can read the answer */
  100. wl1251_mem_read(wl, wl->cmd_box_addr, buf, len);
  101. acx = buf;
  102. if (acx->cmd.status != CMD_STATUS_SUCCESS)
  103. wl1251_error("INTERROGATE command error: %d",
  104. acx->cmd.status);
  105. out:
  106. return ret;
  107. }
  108. /**
  109. * write acx value to firmware
  110. *
  111. * @wl: wl struct
  112. * @id: acx id
  113. * @buf: buffer containing acx, including all headers, must work with dma
  114. * @len: length of buf
  115. */
  116. int wl1251_cmd_configure(struct wl1251 *wl, u16 id, void *buf, size_t len)
  117. {
  118. struct acx_header *acx = buf;
  119. int ret;
  120. wl1251_debug(DEBUG_CMD, "cmd configure");
  121. acx->id = id;
  122. /* payload length, does not include any headers */
  123. acx->len = len - sizeof(*acx);
  124. ret = wl1251_cmd_send(wl, CMD_CONFIGURE, acx, len);
  125. if (ret < 0) {
  126. wl1251_warning("CONFIGURE command NOK");
  127. return ret;
  128. }
  129. return 0;
  130. }
  131. int wl1251_cmd_vbm(struct wl1251 *wl, u8 identity,
  132. void *bitmap, u16 bitmap_len, u8 bitmap_control)
  133. {
  134. struct wl1251_cmd_vbm_update *vbm;
  135. int ret;
  136. wl1251_debug(DEBUG_CMD, "cmd vbm");
  137. vbm = kzalloc(sizeof(*vbm), GFP_KERNEL);
  138. if (!vbm) {
  139. ret = -ENOMEM;
  140. goto out;
  141. }
  142. /* Count and period will be filled by the target */
  143. vbm->tim.bitmap_ctrl = bitmap_control;
  144. if (bitmap_len > PARTIAL_VBM_MAX) {
  145. wl1251_warning("cmd vbm len is %d B, truncating to %d",
  146. bitmap_len, PARTIAL_VBM_MAX);
  147. bitmap_len = PARTIAL_VBM_MAX;
  148. }
  149. memcpy(vbm->tim.pvb_field, bitmap, bitmap_len);
  150. vbm->tim.identity = identity;
  151. vbm->tim.length = bitmap_len + 3;
  152. vbm->len = cpu_to_le16(bitmap_len + 5);
  153. ret = wl1251_cmd_send(wl, CMD_VBM, vbm, sizeof(*vbm));
  154. if (ret < 0) {
  155. wl1251_error("VBM command failed");
  156. goto out;
  157. }
  158. out:
  159. kfree(vbm);
  160. return ret;
  161. }
  162. int wl1251_cmd_data_path(struct wl1251 *wl, u8 channel, bool enable)
  163. {
  164. struct cmd_enabledisable_path *cmd;
  165. int ret;
  166. u16 cmd_rx, cmd_tx;
  167. wl1251_debug(DEBUG_CMD, "cmd data path");
  168. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  169. if (!cmd) {
  170. ret = -ENOMEM;
  171. goto out;
  172. }
  173. cmd->channel = channel;
  174. if (enable) {
  175. cmd_rx = CMD_ENABLE_RX;
  176. cmd_tx = CMD_ENABLE_TX;
  177. } else {
  178. cmd_rx = CMD_DISABLE_RX;
  179. cmd_tx = CMD_DISABLE_TX;
  180. }
  181. ret = wl1251_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd));
  182. if (ret < 0) {
  183. wl1251_error("rx %s cmd for channel %d failed",
  184. enable ? "start" : "stop", channel);
  185. goto out;
  186. }
  187. wl1251_debug(DEBUG_BOOT, "rx %s cmd channel %d",
  188. enable ? "start" : "stop", channel);
  189. ret = wl1251_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd));
  190. if (ret < 0) {
  191. wl1251_error("tx %s cmd for channel %d failed",
  192. enable ? "start" : "stop", channel);
  193. goto out;
  194. }
  195. wl1251_debug(DEBUG_BOOT, "tx %s cmd channel %d",
  196. enable ? "start" : "stop", channel);
  197. out:
  198. kfree(cmd);
  199. return ret;
  200. }
  201. int wl1251_cmd_join(struct wl1251 *wl, u8 bss_type, u8 channel,
  202. u16 beacon_interval, u8 dtim_interval)
  203. {
  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. wl1251_debug(DEBUG_CMD, "cmd join%s ch %d %d/%d",
  213. bss_type == BSS_TYPE_IBSS ? " ibss" : "",
  214. channel, beacon_interval, dtim_interval);
  215. /* Reverse order BSSID */
  216. bssid = (u8 *) &join->bssid_lsb;
  217. for (i = 0; i < ETH_ALEN; i++)
  218. bssid[i] = wl->bssid[ETH_ALEN - i - 1];
  219. join->rx_config_options = wl->rx_config;
  220. join->rx_filter_options = wl->rx_filter;
  221. /*
  222. * FIXME: disable temporarily all filters because after commit
  223. * 9cef8737 "mac80211: fix managed mode BSSID handling" broke
  224. * association. The filter logic needs to be implemented properly
  225. * and once that is done, this hack can be removed.
  226. */
  227. join->rx_config_options = 0;
  228. join->rx_filter_options = WL1251_DEFAULT_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 = 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. out:
  242. kfree(join);
  243. return ret;
  244. }
  245. int wl1251_cmd_ps_mode(struct wl1251 *wl, u8 ps_mode)
  246. {
  247. struct wl1251_cmd_ps_params *ps_params = NULL;
  248. int ret = 0;
  249. wl1251_debug(DEBUG_CMD, "cmd set ps mode");
  250. ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
  251. if (!ps_params) {
  252. ret = -ENOMEM;
  253. goto out;
  254. }
  255. ps_params->ps_mode = ps_mode;
  256. ps_params->send_null_data = 1;
  257. ps_params->retries = 5;
  258. ps_params->hang_over_period = 128;
  259. ps_params->null_data_rate = 1; /* 1 Mbps */
  260. ret = wl1251_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
  261. sizeof(*ps_params));
  262. if (ret < 0) {
  263. wl1251_error("cmd set_ps_mode failed");
  264. goto out;
  265. }
  266. out:
  267. kfree(ps_params);
  268. return ret;
  269. }
  270. int wl1251_cmd_read_memory(struct wl1251 *wl, u32 addr, void *answer,
  271. size_t len)
  272. {
  273. struct cmd_read_write_memory *cmd;
  274. int ret = 0;
  275. wl1251_debug(DEBUG_CMD, "cmd read memory");
  276. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  277. if (!cmd) {
  278. ret = -ENOMEM;
  279. goto out;
  280. }
  281. WARN_ON(len > MAX_READ_SIZE);
  282. len = min_t(size_t, len, MAX_READ_SIZE);
  283. cmd->addr = addr;
  284. cmd->size = len;
  285. ret = wl1251_cmd_send(wl, CMD_READ_MEMORY, cmd, sizeof(*cmd));
  286. if (ret < 0) {
  287. wl1251_error("read memory command failed: %d", ret);
  288. goto out;
  289. }
  290. /* the read command got in, we can now read the answer */
  291. wl1251_mem_read(wl, wl->cmd_box_addr, cmd, sizeof(*cmd));
  292. if (cmd->header.status != CMD_STATUS_SUCCESS)
  293. wl1251_error("error in read command result: %d",
  294. cmd->header.status);
  295. memcpy(answer, cmd->value, len);
  296. out:
  297. kfree(cmd);
  298. return ret;
  299. }
  300. int wl1251_cmd_template_set(struct wl1251 *wl, u16 cmd_id,
  301. void *buf, size_t buf_len)
  302. {
  303. struct wl1251_cmd_packet_template *cmd;
  304. size_t cmd_len;
  305. int ret = 0;
  306. wl1251_debug(DEBUG_CMD, "cmd template %d", cmd_id);
  307. WARN_ON(buf_len > WL1251_MAX_TEMPLATE_SIZE);
  308. buf_len = min_t(size_t, buf_len, WL1251_MAX_TEMPLATE_SIZE);
  309. cmd_len = ALIGN(sizeof(*cmd) + buf_len, 4);
  310. cmd = kzalloc(cmd_len, GFP_KERNEL);
  311. if (!cmd) {
  312. ret = -ENOMEM;
  313. goto out;
  314. }
  315. cmd->size = cpu_to_le16(buf_len);
  316. if (buf)
  317. memcpy(cmd->data, buf, buf_len);
  318. ret = wl1251_cmd_send(wl, cmd_id, cmd, cmd_len);
  319. if (ret < 0) {
  320. wl1251_warning("cmd set_template failed: %d", ret);
  321. goto out;
  322. }
  323. out:
  324. kfree(cmd);
  325. return ret;
  326. }
  327. int wl1251_cmd_scan(struct wl1251 *wl, u8 *ssid, size_t ssid_len,
  328. struct ieee80211_channel *channels[],
  329. unsigned int n_channels, unsigned int n_probes)
  330. {
  331. struct wl1251_cmd_scan *cmd;
  332. int i, ret = 0;
  333. wl1251_debug(DEBUG_CMD, "cmd scan");
  334. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  335. if (!cmd)
  336. return -ENOMEM;
  337. cmd->params.rx_config_options = cpu_to_le32(CFG_RX_ALL_GOOD);
  338. cmd->params.rx_filter_options = cpu_to_le32(CFG_RX_PRSP_EN |
  339. CFG_RX_MGMT_EN |
  340. CFG_RX_BCN_EN);
  341. cmd->params.scan_options = 0;
  342. cmd->params.num_channels = n_channels;
  343. cmd->params.num_probe_requests = n_probes;
  344. cmd->params.tx_rate = cpu_to_le16(1 << 1); /* 2 Mbps */
  345. cmd->params.tid_trigger = 0;
  346. for (i = 0; i < n_channels; i++) {
  347. cmd->channels[i].min_duration =
  348. cpu_to_le32(WL1251_SCAN_MIN_DURATION);
  349. cmd->channels[i].max_duration =
  350. cpu_to_le32(WL1251_SCAN_MAX_DURATION);
  351. memset(&cmd->channels[i].bssid_lsb, 0xff, 4);
  352. memset(&cmd->channels[i].bssid_msb, 0xff, 2);
  353. cmd->channels[i].early_termination = 0;
  354. cmd->channels[i].tx_power_att = 0;
  355. cmd->channels[i].channel = channels[i]->hw_value;
  356. }
  357. cmd->params.ssid_len = ssid_len;
  358. if (ssid)
  359. memcpy(cmd->params.ssid, ssid, ssid_len);
  360. ret = wl1251_cmd_send(wl, CMD_SCAN, cmd, sizeof(*cmd));
  361. if (ret < 0) {
  362. wl1251_error("cmd scan failed: %d", ret);
  363. goto out;
  364. }
  365. wl1251_mem_read(wl, wl->cmd_box_addr, cmd, sizeof(*cmd));
  366. if (cmd->header.status != CMD_STATUS_SUCCESS) {
  367. wl1251_error("cmd scan status wasn't success: %d",
  368. cmd->header.status);
  369. ret = -EIO;
  370. goto out;
  371. }
  372. out:
  373. kfree(cmd);
  374. return ret;
  375. }
  376. int wl1251_cmd_trigger_scan_to(struct wl1251 *wl, u32 timeout)
  377. {
  378. struct wl1251_cmd_trigger_scan_to *cmd;
  379. int ret;
  380. wl1251_debug(DEBUG_CMD, "cmd trigger scan to");
  381. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  382. if (!cmd)
  383. return -ENOMEM;
  384. cmd->timeout = timeout;
  385. ret = wl1251_cmd_send(wl, CMD_TRIGGER_SCAN_TO, cmd, sizeof(*cmd));
  386. if (ret < 0) {
  387. wl1251_error("cmd trigger scan to failed: %d", ret);
  388. goto out;
  389. }
  390. out:
  391. kfree(cmd);
  392. return ret;
  393. }