cros_ec_chardev.c 10 KB

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