cros_ec_dev.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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/slab.h>
  23. #include <linux/uaccess.h>
  24. #include "cros_ec_dev.h"
  25. /* Device variables */
  26. #define CROS_MAX_DEV 128
  27. static int ec_major;
  28. static const struct attribute_group *cros_ec_groups[] = {
  29. &cros_ec_attr_group,
  30. &cros_ec_lightbar_attr_group,
  31. &cros_ec_vbc_attr_group,
  32. NULL,
  33. };
  34. static struct class cros_class = {
  35. .owner = THIS_MODULE,
  36. .name = "chromeos",
  37. .dev_groups = cros_ec_groups,
  38. };
  39. /* Basic communication */
  40. static int ec_get_version(struct cros_ec_dev *ec, char *str, int maxlen)
  41. {
  42. struct ec_response_get_version *resp;
  43. static const char * const current_image_name[] = {
  44. "unknown", "read-only", "read-write", "invalid",
  45. };
  46. struct cros_ec_command *msg;
  47. int ret;
  48. msg = kmalloc(sizeof(*msg) + sizeof(*resp), GFP_KERNEL);
  49. if (!msg)
  50. return -ENOMEM;
  51. msg->version = 0;
  52. msg->command = EC_CMD_GET_VERSION + ec->cmd_offset;
  53. msg->insize = sizeof(*resp);
  54. msg->outsize = 0;
  55. ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
  56. if (ret < 0)
  57. goto exit;
  58. if (msg->result != EC_RES_SUCCESS) {
  59. snprintf(str, maxlen,
  60. "%s\nUnknown EC version: EC returned %d\n",
  61. CROS_EC_DEV_VERSION, msg->result);
  62. ret = -EINVAL;
  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. /* Device file ops */
  77. static int ec_device_open(struct inode *inode, struct file *filp)
  78. {
  79. struct cros_ec_dev *ec = container_of(inode->i_cdev,
  80. struct cros_ec_dev, cdev);
  81. filp->private_data = ec;
  82. nonseekable_open(inode, filp);
  83. return 0;
  84. }
  85. static int ec_device_release(struct inode *inode, struct file *filp)
  86. {
  87. return 0;
  88. }
  89. static ssize_t ec_device_read(struct file *filp, char __user *buffer,
  90. size_t length, loff_t *offset)
  91. {
  92. struct cros_ec_dev *ec = filp->private_data;
  93. char msg[sizeof(struct ec_response_get_version) +
  94. sizeof(CROS_EC_DEV_VERSION)];
  95. size_t count;
  96. int ret;
  97. if (*offset != 0)
  98. return 0;
  99. ret = ec_get_version(ec, msg, sizeof(msg));
  100. if (ret)
  101. return ret;
  102. count = min(length, strlen(msg));
  103. if (copy_to_user(buffer, msg, count))
  104. return -EFAULT;
  105. *offset = count;
  106. return count;
  107. }
  108. /* Ioctls */
  109. static long ec_device_ioctl_xcmd(struct cros_ec_dev *ec, void __user *arg)
  110. {
  111. long ret;
  112. struct cros_ec_command u_cmd;
  113. struct cros_ec_command *s_cmd;
  114. if (copy_from_user(&u_cmd, arg, sizeof(u_cmd)))
  115. return -EFAULT;
  116. if ((u_cmd.outsize > EC_MAX_MSG_BYTES) ||
  117. (u_cmd.insize > EC_MAX_MSG_BYTES))
  118. return -EINVAL;
  119. s_cmd = kmalloc(sizeof(*s_cmd) + max(u_cmd.outsize, u_cmd.insize),
  120. GFP_KERNEL);
  121. if (!s_cmd)
  122. return -ENOMEM;
  123. if (copy_from_user(s_cmd, arg, sizeof(*s_cmd) + u_cmd.outsize)) {
  124. ret = -EFAULT;
  125. goto exit;
  126. }
  127. if (u_cmd.outsize != s_cmd->outsize ||
  128. u_cmd.insize != s_cmd->insize) {
  129. ret = -EINVAL;
  130. goto exit;
  131. }
  132. s_cmd->command += ec->cmd_offset;
  133. ret = cros_ec_cmd_xfer(ec->ec_dev, s_cmd);
  134. /* Only copy data to userland if data was received. */
  135. if (ret < 0)
  136. goto exit;
  137. if (copy_to_user(arg, s_cmd, sizeof(*s_cmd) + s_cmd->insize))
  138. ret = -EFAULT;
  139. exit:
  140. kfree(s_cmd);
  141. return ret;
  142. }
  143. static long ec_device_ioctl_readmem(struct cros_ec_dev *ec, void __user *arg)
  144. {
  145. struct cros_ec_device *ec_dev = ec->ec_dev;
  146. struct cros_ec_readmem s_mem = { };
  147. long num;
  148. /* Not every platform supports direct reads */
  149. if (!ec_dev->cmd_readmem)
  150. return -ENOTTY;
  151. if (copy_from_user(&s_mem, arg, sizeof(s_mem)))
  152. return -EFAULT;
  153. num = ec_dev->cmd_readmem(ec_dev, s_mem.offset, s_mem.bytes,
  154. s_mem.buffer);
  155. if (num <= 0)
  156. return num;
  157. if (copy_to_user((void __user *)arg, &s_mem, sizeof(s_mem)))
  158. return -EFAULT;
  159. return 0;
  160. }
  161. static long ec_device_ioctl(struct file *filp, unsigned int cmd,
  162. unsigned long arg)
  163. {
  164. struct cros_ec_dev *ec = filp->private_data;
  165. if (_IOC_TYPE(cmd) != CROS_EC_DEV_IOC)
  166. return -ENOTTY;
  167. switch (cmd) {
  168. case CROS_EC_DEV_IOCXCMD:
  169. return ec_device_ioctl_xcmd(ec, (void __user *)arg);
  170. case CROS_EC_DEV_IOCRDMEM:
  171. return ec_device_ioctl_readmem(ec, (void __user *)arg);
  172. }
  173. return -ENOTTY;
  174. }
  175. /* Module initialization */
  176. static const struct file_operations fops = {
  177. .open = ec_device_open,
  178. .release = ec_device_release,
  179. .read = ec_device_read,
  180. .unlocked_ioctl = ec_device_ioctl,
  181. #ifdef CONFIG_COMPAT
  182. .compat_ioctl = ec_device_ioctl,
  183. #endif
  184. };
  185. static void __remove(struct device *dev)
  186. {
  187. struct cros_ec_dev *ec = container_of(dev, struct cros_ec_dev,
  188. class_dev);
  189. kfree(ec);
  190. }
  191. static int ec_device_probe(struct platform_device *pdev)
  192. {
  193. int retval = -ENOMEM;
  194. struct device *dev = &pdev->dev;
  195. struct cros_ec_platform *ec_platform = dev_get_platdata(dev);
  196. dev_t devno = MKDEV(ec_major, pdev->id);
  197. struct cros_ec_dev *ec = kzalloc(sizeof(*ec), GFP_KERNEL);
  198. if (!ec)
  199. return retval;
  200. dev_set_drvdata(dev, ec);
  201. ec->ec_dev = dev_get_drvdata(dev->parent);
  202. ec->dev = dev;
  203. ec->cmd_offset = ec_platform->cmd_offset;
  204. device_initialize(&ec->class_dev);
  205. cdev_init(&ec->cdev, &fops);
  206. /*
  207. * Add the character device
  208. * Link cdev to the class device to be sure device is not used
  209. * before unbinding it.
  210. */
  211. ec->cdev.kobj.parent = &ec->class_dev.kobj;
  212. retval = cdev_add(&ec->cdev, devno, 1);
  213. if (retval) {
  214. dev_err(dev, ": failed to add character device\n");
  215. goto cdev_add_failed;
  216. }
  217. /*
  218. * Add the class device
  219. * Link to the character device for creating the /dev entry
  220. * in devtmpfs.
  221. */
  222. ec->class_dev.devt = ec->cdev.dev;
  223. ec->class_dev.class = &cros_class;
  224. ec->class_dev.parent = dev;
  225. ec->class_dev.release = __remove;
  226. retval = dev_set_name(&ec->class_dev, "%s", ec_platform->ec_name);
  227. if (retval) {
  228. dev_err(dev, "dev_set_name failed => %d\n", retval);
  229. goto set_named_failed;
  230. }
  231. retval = device_add(&ec->class_dev);
  232. if (retval) {
  233. dev_err(dev, "device_register failed => %d\n", retval);
  234. goto dev_reg_failed;
  235. }
  236. return 0;
  237. dev_reg_failed:
  238. set_named_failed:
  239. dev_set_drvdata(dev, NULL);
  240. cdev_del(&ec->cdev);
  241. cdev_add_failed:
  242. kfree(ec);
  243. return retval;
  244. }
  245. static int ec_device_remove(struct platform_device *pdev)
  246. {
  247. struct cros_ec_dev *ec = dev_get_drvdata(&pdev->dev);
  248. cdev_del(&ec->cdev);
  249. device_unregister(&ec->class_dev);
  250. return 0;
  251. }
  252. static const struct platform_device_id cros_ec_id[] = {
  253. { "cros-ec-ctl", 0 },
  254. { /* sentinel */ },
  255. };
  256. MODULE_DEVICE_TABLE(platform, cros_ec_id);
  257. static struct platform_driver cros_ec_dev_driver = {
  258. .driver = {
  259. .name = "cros-ec-ctl",
  260. },
  261. .probe = ec_device_probe,
  262. .remove = ec_device_remove,
  263. };
  264. static int __init cros_ec_dev_init(void)
  265. {
  266. int ret;
  267. dev_t dev = 0;
  268. ret = class_register(&cros_class);
  269. if (ret) {
  270. pr_err(CROS_EC_DEV_NAME ": failed to register device class\n");
  271. return ret;
  272. }
  273. /* Get a range of minor numbers (starting with 0) to work with */
  274. ret = alloc_chrdev_region(&dev, 0, CROS_MAX_DEV, CROS_EC_DEV_NAME);
  275. if (ret < 0) {
  276. pr_err(CROS_EC_DEV_NAME ": alloc_chrdev_region() failed\n");
  277. goto failed_chrdevreg;
  278. }
  279. ec_major = MAJOR(dev);
  280. /* Register the driver */
  281. ret = platform_driver_register(&cros_ec_dev_driver);
  282. if (ret < 0) {
  283. pr_warn(CROS_EC_DEV_NAME ": can't register driver: %d\n", ret);
  284. goto failed_devreg;
  285. }
  286. return 0;
  287. failed_devreg:
  288. unregister_chrdev_region(MKDEV(ec_major, 0), CROS_MAX_DEV);
  289. failed_chrdevreg:
  290. class_unregister(&cros_class);
  291. return ret;
  292. }
  293. static void __exit cros_ec_dev_exit(void)
  294. {
  295. platform_driver_unregister(&cros_ec_dev_driver);
  296. unregister_chrdev(ec_major, CROS_EC_DEV_NAME);
  297. class_unregister(&cros_class);
  298. }
  299. module_init(cros_ec_dev_init);
  300. module_exit(cros_ec_dev_exit);
  301. MODULE_AUTHOR("Bill Richardson <wfrichar@chromium.org>");
  302. MODULE_DESCRIPTION("Userspace interface to the Chrome OS Embedded Controller");
  303. MODULE_VERSION("1.0");
  304. MODULE_LICENSE("GPL");