cros_ec_dev.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * cros_ec_dev - expose the Chrome OS Embedded Controller to user-space
  3. *
  4. * Copyright (C) 2014 Google, Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <linux/fs.h>
  20. #include <linux/module.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/uaccess.h>
  23. #include "cros_ec_dev.h"
  24. /* Device variables */
  25. #define CROS_MAX_DEV 128
  26. static struct class *cros_class;
  27. static int ec_major;
  28. /* Basic communication */
  29. static int ec_get_version(struct cros_ec_device *ec, char *str, int maxlen)
  30. {
  31. struct ec_response_get_version *resp;
  32. static const char * const current_image_name[] = {
  33. "unknown", "read-only", "read-write", "invalid",
  34. };
  35. struct cros_ec_command msg = {
  36. .version = 0,
  37. .command = EC_CMD_GET_VERSION,
  38. .outdata = { 0 },
  39. .outsize = 0,
  40. .indata = { 0 },
  41. .insize = sizeof(*resp),
  42. };
  43. int ret;
  44. ret = cros_ec_cmd_xfer(ec, &msg);
  45. if (ret < 0)
  46. return ret;
  47. if (msg.result != EC_RES_SUCCESS) {
  48. snprintf(str, maxlen,
  49. "%s\nUnknown EC version: EC returned %d\n",
  50. CROS_EC_DEV_VERSION, msg.result);
  51. return 0;
  52. }
  53. resp = (struct ec_response_get_version *)msg.indata;
  54. if (resp->current_image >= ARRAY_SIZE(current_image_name))
  55. resp->current_image = 3; /* invalid */
  56. snprintf(str, maxlen, "%s\n%s\n%s\n%s\n", CROS_EC_DEV_VERSION,
  57. resp->version_string_ro, resp->version_string_rw,
  58. current_image_name[resp->current_image]);
  59. return 0;
  60. }
  61. /* Device file ops */
  62. static int ec_device_open(struct inode *inode, struct file *filp)
  63. {
  64. filp->private_data = container_of(inode->i_cdev,
  65. struct cros_ec_device, cdev);
  66. return 0;
  67. }
  68. static int ec_device_release(struct inode *inode, struct file *filp)
  69. {
  70. return 0;
  71. }
  72. static ssize_t ec_device_read(struct file *filp, char __user *buffer,
  73. size_t length, loff_t *offset)
  74. {
  75. struct cros_ec_device *ec = filp->private_data;
  76. char msg[sizeof(struct ec_response_get_version) +
  77. sizeof(CROS_EC_DEV_VERSION)];
  78. size_t count;
  79. int ret;
  80. if (*offset != 0)
  81. return 0;
  82. ret = ec_get_version(ec, msg, sizeof(msg));
  83. if (ret)
  84. return ret;
  85. count = min(length, strlen(msg));
  86. if (copy_to_user(buffer, msg, count))
  87. return -EFAULT;
  88. *offset = count;
  89. return count;
  90. }
  91. /* Ioctls */
  92. static long ec_device_ioctl_xcmd(struct cros_ec_device *ec, void __user *arg)
  93. {
  94. long ret;
  95. struct cros_ec_command s_cmd = { };
  96. if (copy_from_user(&s_cmd, arg, sizeof(s_cmd)))
  97. return -EFAULT;
  98. ret = cros_ec_cmd_xfer(ec, &s_cmd);
  99. /* Only copy data to userland if data was received. */
  100. if (ret < 0)
  101. return ret;
  102. if (copy_to_user(arg, &s_cmd, sizeof(s_cmd)))
  103. return -EFAULT;
  104. return 0;
  105. }
  106. static long ec_device_ioctl_readmem(struct cros_ec_device *ec, void __user *arg)
  107. {
  108. struct cros_ec_readmem s_mem = { };
  109. long num;
  110. /* Not every platform supports direct reads */
  111. if (!ec->cmd_readmem)
  112. return -ENOTTY;
  113. if (copy_from_user(&s_mem, arg, sizeof(s_mem)))
  114. return -EFAULT;
  115. num = ec->cmd_readmem(ec, s_mem.offset, s_mem.bytes, s_mem.buffer);
  116. if (num <= 0)
  117. return num;
  118. if (copy_to_user((void __user *)arg, &s_mem, sizeof(s_mem)))
  119. return -EFAULT;
  120. return 0;
  121. }
  122. static long ec_device_ioctl(struct file *filp, unsigned int cmd,
  123. unsigned long arg)
  124. {
  125. struct cros_ec_device *ec = filp->private_data;
  126. if (_IOC_TYPE(cmd) != CROS_EC_DEV_IOC)
  127. return -ENOTTY;
  128. switch (cmd) {
  129. case CROS_EC_DEV_IOCXCMD:
  130. return ec_device_ioctl_xcmd(ec, (void __user *)arg);
  131. case CROS_EC_DEV_IOCRDMEM:
  132. return ec_device_ioctl_readmem(ec, (void __user *)arg);
  133. }
  134. return -ENOTTY;
  135. }
  136. /* Module initialization */
  137. static const struct file_operations fops = {
  138. .open = ec_device_open,
  139. .release = ec_device_release,
  140. .read = ec_device_read,
  141. .unlocked_ioctl = ec_device_ioctl,
  142. };
  143. static int ec_device_probe(struct platform_device *pdev)
  144. {
  145. struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent);
  146. int retval = -ENOTTY;
  147. dev_t devno = MKDEV(ec_major, 0);
  148. /* Instantiate it (and remember the EC) */
  149. cdev_init(&ec->cdev, &fops);
  150. retval = cdev_add(&ec->cdev, devno, 1);
  151. if (retval) {
  152. dev_err(&pdev->dev, ": failed to add character device\n");
  153. return retval;
  154. }
  155. ec->vdev = device_create(cros_class, NULL, devno, ec,
  156. CROS_EC_DEV_NAME);
  157. if (IS_ERR(ec->vdev)) {
  158. retval = PTR_ERR(ec->vdev);
  159. dev_err(&pdev->dev, ": failed to create device\n");
  160. cdev_del(&ec->cdev);
  161. return retval;
  162. }
  163. /* Initialize extra interfaces */
  164. ec_dev_sysfs_init(ec);
  165. ec_dev_lightbar_init(ec);
  166. return 0;
  167. }
  168. static int ec_device_remove(struct platform_device *pdev)
  169. {
  170. struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent);
  171. ec_dev_lightbar_remove(ec);
  172. ec_dev_sysfs_remove(ec);
  173. device_destroy(cros_class, MKDEV(ec_major, 0));
  174. cdev_del(&ec->cdev);
  175. return 0;
  176. }
  177. static struct platform_driver cros_ec_dev_driver = {
  178. .driver = {
  179. .name = "cros-ec-ctl",
  180. },
  181. .probe = ec_device_probe,
  182. .remove = ec_device_remove,
  183. };
  184. static int __init cros_ec_dev_init(void)
  185. {
  186. int ret;
  187. dev_t dev = 0;
  188. cros_class = class_create(THIS_MODULE, "chromeos");
  189. if (IS_ERR(cros_class)) {
  190. pr_err(CROS_EC_DEV_NAME ": failed to register device class\n");
  191. return PTR_ERR(cros_class);
  192. }
  193. /* Get a range of minor numbers (starting with 0) to work with */
  194. ret = alloc_chrdev_region(&dev, 0, CROS_MAX_DEV, CROS_EC_DEV_NAME);
  195. if (ret < 0) {
  196. pr_err(CROS_EC_DEV_NAME ": alloc_chrdev_region() failed\n");
  197. goto failed_chrdevreg;
  198. }
  199. ec_major = MAJOR(dev);
  200. /* Register the driver */
  201. ret = platform_driver_register(&cros_ec_dev_driver);
  202. if (ret < 0) {
  203. pr_warn(CROS_EC_DEV_NAME ": can't register driver: %d\n", ret);
  204. goto failed_devreg;
  205. }
  206. return 0;
  207. failed_devreg:
  208. unregister_chrdev_region(MKDEV(ec_major, 0), CROS_MAX_DEV);
  209. failed_chrdevreg:
  210. class_destroy(cros_class);
  211. return ret;
  212. }
  213. static void __exit cros_ec_dev_exit(void)
  214. {
  215. platform_driver_unregister(&cros_ec_dev_driver);
  216. unregister_chrdev(ec_major, CROS_EC_DEV_NAME);
  217. class_destroy(cros_class);
  218. }
  219. module_init(cros_ec_dev_init);
  220. module_exit(cros_ec_dev_exit);
  221. MODULE_AUTHOR("Bill Richardson <wfrichar@chromium.org>");
  222. MODULE_DESCRIPTION("Userspace interface to the Chrome OS Embedded Controller");
  223. MODULE_VERSION("1.0");
  224. MODULE_LICENSE("GPL");