1
0

cros_ec_chardev.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Miscellaneous character driver for ChromeOS Embedded Controller
  4. *
  5. * Copyright 2014 Google, Inc.
  6. * Copyright 2019 Google LLC
  7. *
  8. * This file is a rework and part of the code is ported from
  9. * drivers/mfd/cros_ec_dev.c that was originally written by
  10. * Bill Richardson.
  11. */
  12. #include <linux/init.h>
  13. #include <linux/device.h>
  14. #include <linux/fs.h>
  15. #include <linux/miscdevice.h>
  16. #include <linux/mod_devicetable.h>
  17. #include <linux/module.h>
  18. #include <linux/notifier.h>
  19. #include <linux/platform_data/cros_ec_chardev.h>
  20. #include <linux/platform_data/cros_ec_commands.h>
  21. #include <linux/platform_data/cros_ec_proto.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/poll.h>
  24. #include <linux/slab.h>
  25. #include <linux/types.h>
  26. #include <linux/uaccess.h>
  27. #define DRV_NAME "cros-ec-chardev"
  28. /* Arbitrary bounded size for the event queue */
  29. #define CROS_MAX_EVENT_LEN PAGE_SIZE
  30. struct chardev_priv {
  31. struct cros_ec_device *ec_dev;
  32. struct notifier_block notifier;
  33. wait_queue_head_t wait_event;
  34. unsigned long event_mask;
  35. struct list_head events;
  36. size_t event_len;
  37. u16 cmd_offset;
  38. };
  39. struct ec_event {
  40. struct list_head node;
  41. size_t size;
  42. u8 event_type;
  43. u8 data[];
  44. };
  45. static int ec_get_version(struct chardev_priv *priv, char *str, int maxlen)
  46. {
  47. static const char * const current_image_name[] = {
  48. "unknown", "read-only", "read-write", "invalid",
  49. };
  50. struct ec_response_get_version *resp;
  51. struct cros_ec_command *msg;
  52. int ret;
  53. msg = kzalloc(sizeof(*msg) + sizeof(*resp), GFP_KERNEL);
  54. if (!msg)
  55. return -ENOMEM;
  56. msg->command = EC_CMD_GET_VERSION + priv->cmd_offset;
  57. msg->insize = sizeof(*resp);
  58. ret = cros_ec_cmd_xfer_status(priv->ec_dev, msg);
  59. if (ret < 0) {
  60. snprintf(str, maxlen,
  61. "Unknown EC version, returned error: %d\n",
  62. msg->result);
  63. goto exit;
  64. }
  65. resp = (struct ec_response_get_version *)msg->data;
  66. if (resp->current_image >= ARRAY_SIZE(current_image_name))
  67. resp->current_image = 3; /* invalid */
  68. snprintf(str, maxlen, "%s\n%s\n%s\n%s\n", CROS_EC_DEV_VERSION,
  69. resp->version_string_ro, resp->version_string_rw,
  70. current_image_name[resp->current_image]);
  71. ret = 0;
  72. exit:
  73. kfree(msg);
  74. return ret;
  75. }
  76. static int cros_ec_chardev_mkbp_event(struct notifier_block *nb,
  77. unsigned long queued_during_suspend,
  78. void *_notify)
  79. {
  80. struct chardev_priv *priv = container_of(nb, struct chardev_priv,
  81. notifier);
  82. struct cros_ec_device *ec_dev = priv->ec_dev;
  83. struct ec_event *event;
  84. unsigned long event_bit = 1 << ec_dev->event_data.event_type;
  85. int total_size = sizeof(*event) + ec_dev->event_size;
  86. if (!(event_bit & priv->event_mask) ||
  87. (priv->event_len + total_size) > CROS_MAX_EVENT_LEN)
  88. return NOTIFY_DONE;
  89. event = kzalloc(total_size, GFP_KERNEL);
  90. if (!event)
  91. return NOTIFY_DONE;
  92. event->size = ec_dev->event_size;
  93. event->event_type = ec_dev->event_data.event_type;
  94. memcpy(event->data, &ec_dev->event_data.data, ec_dev->event_size);
  95. spin_lock(&priv->wait_event.lock);
  96. list_add_tail(&event->node, &priv->events);
  97. priv->event_len += total_size;
  98. wake_up_locked(&priv->wait_event);
  99. spin_unlock(&priv->wait_event.lock);
  100. return NOTIFY_OK;
  101. }
  102. static struct ec_event *cros_ec_chardev_fetch_event(struct chardev_priv *priv,
  103. bool fetch, bool block)
  104. {
  105. struct ec_event *event;
  106. int err;
  107. spin_lock(&priv->wait_event.lock);
  108. if (!block && list_empty(&priv->events)) {
  109. event = ERR_PTR(-EWOULDBLOCK);
  110. goto out;
  111. }
  112. if (!fetch) {
  113. event = NULL;
  114. goto out;
  115. }
  116. err = wait_event_interruptible_locked(priv->wait_event,
  117. !list_empty(&priv->events));
  118. if (err) {
  119. event = ERR_PTR(err);
  120. goto out;
  121. }
  122. event = list_first_entry(&priv->events, struct ec_event, node);
  123. list_del(&event->node);
  124. priv->event_len -= sizeof(*event) + event->size;
  125. out:
  126. spin_unlock(&priv->wait_event.lock);
  127. return event;
  128. }
  129. /*
  130. * Device file ops
  131. */
  132. static int cros_ec_chardev_open(struct inode *inode, struct file *filp)
  133. {
  134. struct miscdevice *mdev = filp->private_data;
  135. struct cros_ec_dev *ec = dev_get_drvdata(mdev->parent);
  136. struct cros_ec_device *ec_dev = ec->ec_dev;
  137. struct chardev_priv *priv;
  138. int ret;
  139. priv = kzalloc_obj(*priv);
  140. if (!priv)
  141. return -ENOMEM;
  142. priv->ec_dev = ec_dev;
  143. priv->cmd_offset = ec->cmd_offset;
  144. filp->private_data = priv;
  145. INIT_LIST_HEAD(&priv->events);
  146. init_waitqueue_head(&priv->wait_event);
  147. nonseekable_open(inode, filp);
  148. priv->notifier.notifier_call = cros_ec_chardev_mkbp_event;
  149. ret = blocking_notifier_chain_register(&ec_dev->event_notifier,
  150. &priv->notifier);
  151. if (ret) {
  152. dev_err(ec_dev->dev, "failed to register event notifier\n");
  153. kfree(priv);
  154. }
  155. return ret;
  156. }
  157. static __poll_t cros_ec_chardev_poll(struct file *filp, poll_table *wait)
  158. {
  159. struct chardev_priv *priv = filp->private_data;
  160. poll_wait(filp, &priv->wait_event, wait);
  161. if (list_empty(&priv->events))
  162. return 0;
  163. return EPOLLIN | EPOLLRDNORM;
  164. }
  165. static ssize_t cros_ec_chardev_read(struct file *filp, char __user *buffer,
  166. size_t length, loff_t *offset)
  167. {
  168. char msg[sizeof(struct ec_response_get_version) +
  169. sizeof(CROS_EC_DEV_VERSION)];
  170. struct chardev_priv *priv = filp->private_data;
  171. size_t count;
  172. int ret;
  173. if (priv->event_mask) { /* queued MKBP event */
  174. struct ec_event *event;
  175. event = cros_ec_chardev_fetch_event(priv, length != 0,
  176. !(filp->f_flags & O_NONBLOCK));
  177. if (IS_ERR(event))
  178. return PTR_ERR(event);
  179. /*
  180. * length == 0 is special - no IO is done but we check
  181. * for error conditions.
  182. */
  183. if (length == 0)
  184. return 0;
  185. /* The event is 1 byte of type plus the payload */
  186. count = min(length, event->size + 1);
  187. ret = copy_to_user(buffer, &event->event_type, count);
  188. kfree(event);
  189. if (ret) /* the copy failed */
  190. return -EFAULT;
  191. *offset = count;
  192. return count;
  193. }
  194. /*
  195. * Legacy behavior if no event mask is defined
  196. */
  197. if (*offset != 0)
  198. return 0;
  199. ret = ec_get_version(priv, msg, sizeof(msg));
  200. if (ret)
  201. return ret;
  202. count = min(length, strlen(msg));
  203. if (copy_to_user(buffer, msg, count))
  204. return -EFAULT;
  205. *offset = count;
  206. return count;
  207. }
  208. static int cros_ec_chardev_release(struct inode *inode, struct file *filp)
  209. {
  210. struct chardev_priv *priv = filp->private_data;
  211. struct cros_ec_device *ec_dev = priv->ec_dev;
  212. struct ec_event *event, *e;
  213. blocking_notifier_chain_unregister(&ec_dev->event_notifier,
  214. &priv->notifier);
  215. list_for_each_entry_safe(event, e, &priv->events, node) {
  216. list_del(&event->node);
  217. kfree(event);
  218. }
  219. kfree(priv);
  220. return 0;
  221. }
  222. /*
  223. * Ioctls
  224. */
  225. static long cros_ec_chardev_ioctl_xcmd(struct chardev_priv *priv, void __user *arg)
  226. {
  227. struct cros_ec_command *s_cmd;
  228. struct cros_ec_command u_cmd;
  229. long ret;
  230. if (copy_from_user(&u_cmd, arg, sizeof(u_cmd)))
  231. return -EFAULT;
  232. if (u_cmd.outsize > EC_MAX_MSG_BYTES ||
  233. u_cmd.insize > EC_MAX_MSG_BYTES)
  234. return -EINVAL;
  235. s_cmd = kzalloc(sizeof(*s_cmd) + max(u_cmd.outsize, u_cmd.insize),
  236. GFP_KERNEL);
  237. if (!s_cmd)
  238. return -ENOMEM;
  239. if (copy_from_user(s_cmd, arg, sizeof(*s_cmd) + u_cmd.outsize)) {
  240. ret = -EFAULT;
  241. goto exit;
  242. }
  243. if (u_cmd.outsize != s_cmd->outsize ||
  244. u_cmd.insize != s_cmd->insize) {
  245. ret = -EINVAL;
  246. goto exit;
  247. }
  248. s_cmd->command += priv->cmd_offset;
  249. ret = cros_ec_cmd_xfer(priv->ec_dev, s_cmd);
  250. /* Only copy data to userland if data was received. */
  251. if (ret < 0)
  252. goto exit;
  253. if (copy_to_user(arg, s_cmd, sizeof(*s_cmd) + s_cmd->insize))
  254. ret = -EFAULT;
  255. exit:
  256. kfree(s_cmd);
  257. return ret;
  258. }
  259. static long cros_ec_chardev_ioctl_readmem(struct chardev_priv *priv, void __user *arg)
  260. {
  261. struct cros_ec_device *ec_dev = priv->ec_dev;
  262. struct cros_ec_readmem s_mem = { };
  263. long num;
  264. /* Not every platform supports direct reads */
  265. if (!ec_dev->cmd_readmem)
  266. return -ENOTTY;
  267. if (copy_from_user(&s_mem, arg, sizeof(s_mem)))
  268. return -EFAULT;
  269. if (s_mem.bytes > sizeof(s_mem.buffer))
  270. return -EINVAL;
  271. num = ec_dev->cmd_readmem(ec_dev, s_mem.offset, s_mem.bytes,
  272. s_mem.buffer);
  273. if (num <= 0)
  274. return num;
  275. if (copy_to_user((void __user *)arg, &s_mem, sizeof(s_mem)))
  276. return -EFAULT;
  277. return num;
  278. }
  279. static long cros_ec_chardev_ioctl(struct file *filp, unsigned int cmd,
  280. unsigned long arg)
  281. {
  282. struct chardev_priv *priv = filp->private_data;
  283. if (_IOC_TYPE(cmd) != CROS_EC_DEV_IOC)
  284. return -ENOTTY;
  285. switch (cmd) {
  286. case CROS_EC_DEV_IOCXCMD:
  287. return cros_ec_chardev_ioctl_xcmd(priv, (void __user *)arg);
  288. case CROS_EC_DEV_IOCRDMEM:
  289. return cros_ec_chardev_ioctl_readmem(priv, (void __user *)arg);
  290. case CROS_EC_DEV_IOCEVENTMASK:
  291. priv->event_mask = arg;
  292. return 0;
  293. }
  294. return -ENOTTY;
  295. }
  296. static const struct file_operations chardev_fops = {
  297. .open = cros_ec_chardev_open,
  298. .poll = cros_ec_chardev_poll,
  299. .read = cros_ec_chardev_read,
  300. .release = cros_ec_chardev_release,
  301. .unlocked_ioctl = cros_ec_chardev_ioctl,
  302. #ifdef CONFIG_COMPAT
  303. .compat_ioctl = cros_ec_chardev_ioctl,
  304. #endif
  305. };
  306. static int cros_ec_chardev_probe(struct platform_device *pdev)
  307. {
  308. struct cros_ec_dev *ec = dev_get_drvdata(pdev->dev.parent);
  309. struct cros_ec_platform *ec_platform = dev_get_platdata(ec->dev);
  310. struct miscdevice *misc;
  311. /* Create a char device: we want to create it anew */
  312. misc = devm_kzalloc(&pdev->dev, sizeof(*misc), GFP_KERNEL);
  313. if (!misc)
  314. return -ENOMEM;
  315. misc->minor = MISC_DYNAMIC_MINOR;
  316. misc->fops = &chardev_fops;
  317. misc->name = ec_platform->ec_name;
  318. misc->parent = pdev->dev.parent;
  319. dev_set_drvdata(&pdev->dev, misc);
  320. return misc_register(misc);
  321. }
  322. static void cros_ec_chardev_remove(struct platform_device *pdev)
  323. {
  324. struct miscdevice *misc = dev_get_drvdata(&pdev->dev);
  325. misc_deregister(misc);
  326. }
  327. static const struct platform_device_id cros_ec_chardev_id[] = {
  328. { DRV_NAME, 0 },
  329. {}
  330. };
  331. MODULE_DEVICE_TABLE(platform, cros_ec_chardev_id);
  332. static struct platform_driver cros_ec_chardev_driver = {
  333. .driver = {
  334. .name = DRV_NAME,
  335. },
  336. .probe = cros_ec_chardev_probe,
  337. .remove = cros_ec_chardev_remove,
  338. .id_table = cros_ec_chardev_id,
  339. };
  340. module_platform_driver(cros_ec_chardev_driver);
  341. MODULE_AUTHOR("Enric Balletbo i Serra <enric.balletbo@collabora.com>");
  342. MODULE_DESCRIPTION("ChromeOS EC Miscellaneous Character Driver");
  343. MODULE_LICENSE("GPL");