cros_ec_rpmsg.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Copyright 2018 Google LLC.
  4. #include <linux/completion.h>
  5. #include <linux/delay.h>
  6. #include <linux/kernel.h>
  7. #include <linux/module.h>
  8. #include <linux/of.h>
  9. #include <linux/platform_data/cros_ec_commands.h>
  10. #include <linux/platform_data/cros_ec_proto.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/rpmsg.h>
  13. #include <linux/slab.h>
  14. #include "cros_ec.h"
  15. #define EC_MSG_TIMEOUT_MS 200
  16. #define HOST_COMMAND_MARK 1
  17. #define HOST_EVENT_MARK 2
  18. /**
  19. * struct cros_ec_rpmsg_response - rpmsg message format from from EC.
  20. *
  21. * @type: The type of message, should be either HOST_COMMAND_MARK or
  22. * HOST_EVENT_MARK, representing that the message is a response to
  23. * host command, or a host event.
  24. * @data: ec_host_response for host command.
  25. */
  26. struct cros_ec_rpmsg_response {
  27. u8 type;
  28. u8 data[] __aligned(4);
  29. };
  30. /**
  31. * struct cros_ec_rpmsg - information about a EC over rpmsg.
  32. *
  33. * @rpdev: rpmsg device we are connected to
  34. * @xfer_ack: completion for host command transfer.
  35. * @host_event_work: Work struct for pending host event.
  36. * @ept: The rpmsg endpoint of this channel.
  37. * @has_pending_host_event: Boolean used to check if there is a pending event.
  38. * @probe_done: Flag to indicate that probe is done.
  39. */
  40. struct cros_ec_rpmsg {
  41. struct rpmsg_device *rpdev;
  42. struct completion xfer_ack;
  43. struct work_struct host_event_work;
  44. struct rpmsg_endpoint *ept;
  45. bool has_pending_host_event;
  46. bool probe_done;
  47. };
  48. /**
  49. * cros_ec_cmd_xfer_rpmsg - Transfer a message over rpmsg and receive the reply
  50. *
  51. * @ec_dev: ChromeOS EC device
  52. * @ec_msg: Message to transfer
  53. *
  54. * This is only used for old EC proto version, and is not supported for this
  55. * driver.
  56. *
  57. * Return: -EINVAL
  58. */
  59. static int cros_ec_cmd_xfer_rpmsg(struct cros_ec_device *ec_dev,
  60. struct cros_ec_command *ec_msg)
  61. {
  62. return -EINVAL;
  63. }
  64. /**
  65. * cros_ec_pkt_xfer_rpmsg - Transfer a packet over rpmsg and receive the reply
  66. *
  67. * @ec_dev: ChromeOS EC device
  68. * @ec_msg: Message to transfer
  69. *
  70. * Return: number of bytes of the reply on success or negative error code.
  71. */
  72. static int cros_ec_pkt_xfer_rpmsg(struct cros_ec_device *ec_dev,
  73. struct cros_ec_command *ec_msg)
  74. {
  75. struct cros_ec_rpmsg *ec_rpmsg = ec_dev->priv;
  76. struct ec_host_response *response;
  77. unsigned long timeout;
  78. int len;
  79. int ret;
  80. u8 sum;
  81. int i;
  82. ec_msg->result = 0;
  83. len = cros_ec_prepare_tx(ec_dev, ec_msg);
  84. if (len < 0)
  85. return len;
  86. dev_dbg(ec_dev->dev, "prepared, len=%d\n", len);
  87. reinit_completion(&ec_rpmsg->xfer_ack);
  88. ret = rpmsg_send(ec_rpmsg->ept, ec_dev->dout, len);
  89. if (ret) {
  90. dev_err(ec_dev->dev, "rpmsg send failed\n");
  91. return ret;
  92. }
  93. timeout = msecs_to_jiffies(EC_MSG_TIMEOUT_MS);
  94. ret = wait_for_completion_timeout(&ec_rpmsg->xfer_ack, timeout);
  95. if (!ret) {
  96. dev_err(ec_dev->dev, "rpmsg send timeout\n");
  97. return -EIO;
  98. }
  99. /* check response error code */
  100. response = (struct ec_host_response *)ec_dev->din;
  101. ec_msg->result = response->result;
  102. ret = cros_ec_check_result(ec_dev, ec_msg);
  103. if (ret)
  104. goto exit;
  105. if (response->data_len > ec_msg->insize) {
  106. dev_err(ec_dev->dev, "packet too long (%d bytes, expected %d)",
  107. response->data_len, ec_msg->insize);
  108. ret = -EMSGSIZE;
  109. goto exit;
  110. }
  111. /* copy response packet payload and compute checksum */
  112. memcpy(ec_msg->data, ec_dev->din + sizeof(*response),
  113. response->data_len);
  114. sum = 0;
  115. for (i = 0; i < sizeof(*response) + response->data_len; i++)
  116. sum += ec_dev->din[i];
  117. if (sum) {
  118. dev_err(ec_dev->dev, "bad packet checksum, calculated %x\n",
  119. sum);
  120. ret = -EBADMSG;
  121. goto exit;
  122. }
  123. ret = response->data_len;
  124. exit:
  125. if (ec_msg->command == EC_CMD_REBOOT_EC)
  126. msleep(EC_REBOOT_DELAY_MS);
  127. return ret;
  128. }
  129. static void
  130. cros_ec_rpmsg_host_event_function(struct work_struct *host_event_work)
  131. {
  132. struct cros_ec_rpmsg *ec_rpmsg = container_of(host_event_work,
  133. struct cros_ec_rpmsg,
  134. host_event_work);
  135. cros_ec_irq_thread(0, dev_get_drvdata(&ec_rpmsg->rpdev->dev));
  136. }
  137. static int cros_ec_rpmsg_callback(struct rpmsg_device *rpdev, void *data,
  138. int len, void *priv, u32 src)
  139. {
  140. struct cros_ec_device *ec_dev = dev_get_drvdata(&rpdev->dev);
  141. struct cros_ec_rpmsg *ec_rpmsg = ec_dev->priv;
  142. struct cros_ec_rpmsg_response *resp;
  143. if (!len) {
  144. dev_warn(ec_dev->dev, "rpmsg received empty response");
  145. return -EINVAL;
  146. }
  147. resp = data;
  148. len -= offsetof(struct cros_ec_rpmsg_response, data);
  149. if (resp->type == HOST_COMMAND_MARK) {
  150. if (len > ec_dev->din_size) {
  151. dev_warn(ec_dev->dev,
  152. "received length %d > din_size %d, truncating",
  153. len, ec_dev->din_size);
  154. len = ec_dev->din_size;
  155. }
  156. memcpy(ec_dev->din, resp->data, len);
  157. complete(&ec_rpmsg->xfer_ack);
  158. } else if (resp->type == HOST_EVENT_MARK) {
  159. /*
  160. * If the host event is sent before cros_ec_register is
  161. * finished, queue the host event.
  162. */
  163. if (ec_rpmsg->probe_done)
  164. schedule_work(&ec_rpmsg->host_event_work);
  165. else
  166. ec_rpmsg->has_pending_host_event = true;
  167. } else {
  168. dev_warn(ec_dev->dev, "rpmsg received invalid type = %d",
  169. resp->type);
  170. return -EINVAL;
  171. }
  172. return 0;
  173. }
  174. static struct rpmsg_endpoint *
  175. cros_ec_rpmsg_create_ept(struct rpmsg_device *rpdev)
  176. {
  177. struct rpmsg_channel_info chinfo = {};
  178. strscpy(chinfo.name, rpdev->id.name, RPMSG_NAME_SIZE);
  179. chinfo.src = rpdev->src;
  180. chinfo.dst = RPMSG_ADDR_ANY;
  181. return rpmsg_create_ept(rpdev, cros_ec_rpmsg_callback, NULL, chinfo);
  182. }
  183. static int cros_ec_rpmsg_probe(struct rpmsg_device *rpdev)
  184. {
  185. struct device *dev = &rpdev->dev;
  186. struct cros_ec_rpmsg *ec_rpmsg;
  187. struct cros_ec_device *ec_dev;
  188. int ret;
  189. ec_dev = devm_kzalloc(dev, sizeof(*ec_dev), GFP_KERNEL);
  190. if (!ec_dev)
  191. return -ENOMEM;
  192. ec_rpmsg = devm_kzalloc(dev, sizeof(*ec_rpmsg), GFP_KERNEL);
  193. if (!ec_rpmsg)
  194. return -ENOMEM;
  195. ec_dev->dev = dev;
  196. ec_dev->priv = ec_rpmsg;
  197. ec_dev->cmd_xfer = cros_ec_cmd_xfer_rpmsg;
  198. ec_dev->pkt_xfer = cros_ec_pkt_xfer_rpmsg;
  199. ec_dev->phys_name = dev_name(&rpdev->dev);
  200. ec_dev->din_size = sizeof(struct ec_host_response) +
  201. sizeof(struct ec_response_get_protocol_info);
  202. ec_dev->dout_size = sizeof(struct ec_host_request);
  203. dev_set_drvdata(dev, ec_dev);
  204. ec_rpmsg->rpdev = rpdev;
  205. init_completion(&ec_rpmsg->xfer_ack);
  206. INIT_WORK(&ec_rpmsg->host_event_work,
  207. cros_ec_rpmsg_host_event_function);
  208. ec_rpmsg->ept = cros_ec_rpmsg_create_ept(rpdev);
  209. if (!ec_rpmsg->ept)
  210. return -ENOMEM;
  211. ret = cros_ec_register(ec_dev);
  212. if (ret < 0) {
  213. rpmsg_destroy_ept(ec_rpmsg->ept);
  214. cancel_work_sync(&ec_rpmsg->host_event_work);
  215. return ret;
  216. }
  217. ec_rpmsg->probe_done = true;
  218. if (ec_rpmsg->has_pending_host_event)
  219. schedule_work(&ec_rpmsg->host_event_work);
  220. return 0;
  221. }
  222. static void cros_ec_rpmsg_remove(struct rpmsg_device *rpdev)
  223. {
  224. struct cros_ec_device *ec_dev = dev_get_drvdata(&rpdev->dev);
  225. struct cros_ec_rpmsg *ec_rpmsg = ec_dev->priv;
  226. cros_ec_unregister(ec_dev);
  227. rpmsg_destroy_ept(ec_rpmsg->ept);
  228. cancel_work_sync(&ec_rpmsg->host_event_work);
  229. }
  230. #ifdef CONFIG_PM_SLEEP
  231. static int cros_ec_rpmsg_suspend(struct device *dev)
  232. {
  233. struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
  234. return cros_ec_suspend(ec_dev);
  235. }
  236. static int cros_ec_rpmsg_resume(struct device *dev)
  237. {
  238. struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
  239. return cros_ec_resume(ec_dev);
  240. }
  241. #endif
  242. static SIMPLE_DEV_PM_OPS(cros_ec_rpmsg_pm_ops, cros_ec_rpmsg_suspend,
  243. cros_ec_rpmsg_resume);
  244. static const struct of_device_id cros_ec_rpmsg_of_match[] = {
  245. { .compatible = "google,cros-ec-rpmsg", },
  246. { }
  247. };
  248. MODULE_DEVICE_TABLE(of, cros_ec_rpmsg_of_match);
  249. static struct rpmsg_driver cros_ec_driver_rpmsg = {
  250. .drv = {
  251. .name = "cros-ec-rpmsg",
  252. .of_match_table = cros_ec_rpmsg_of_match,
  253. .pm = &cros_ec_rpmsg_pm_ops,
  254. },
  255. .probe = cros_ec_rpmsg_probe,
  256. .remove = cros_ec_rpmsg_remove,
  257. };
  258. module_rpmsg_driver(cros_ec_driver_rpmsg);
  259. MODULE_LICENSE("GPL v2");
  260. MODULE_DESCRIPTION("ChromeOS EC multi function device (rpmsg)");