blob: e91ced1cb8cefc39f1498f54740ade234325dcb2 [file] [log] [blame]
Bill Richardsone7c256f2015-02-02 12:26:25 +01001/*
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
20#include <linux/fs.h>
21#include <linux/module.h>
22#include <linux/platform_device.h>
Javier Martinez Canillasa8411782015-06-09 13:04:42 +020023#include <linux/slab.h>
Bill Richardsone7c256f2015-02-02 12:26:25 +010024#include <linux/uaccess.h>
25
26#include "cros_ec_dev.h"
27
28/* Device variables */
29#define CROS_MAX_DEV 128
30static struct class *cros_class;
31static int ec_major;
32
33/* Basic communication */
34static int ec_get_version(struct cros_ec_device *ec, char *str, int maxlen)
35{
36 struct ec_response_get_version *resp;
37 static const char * const current_image_name[] = {
38 "unknown", "read-only", "read-write", "invalid",
39 };
Javier Martinez Canillasa8411782015-06-09 13:04:42 +020040 struct cros_ec_command *msg;
Bill Richardsone7c256f2015-02-02 12:26:25 +010041 int ret;
42
Javier Martinez Canillasa8411782015-06-09 13:04:42 +020043 msg = kmalloc(sizeof(*msg) + sizeof(*resp), GFP_KERNEL);
44 if (!msg)
45 return -ENOMEM;
Bill Richardsone7c256f2015-02-02 12:26:25 +010046
Javier Martinez Canillasa8411782015-06-09 13:04:42 +020047 msg->version = 0;
48 msg->command = EC_CMD_GET_VERSION;
49 msg->insize = sizeof(*resp);
50 msg->outsize = 0;
51
52 ret = cros_ec_cmd_xfer(ec, msg);
53 if (ret < 0)
54 goto exit;
55
56 if (msg->result != EC_RES_SUCCESS) {
Bill Richardsone7c256f2015-02-02 12:26:25 +010057 snprintf(str, maxlen,
58 "%s\nUnknown EC version: EC returned %d\n",
Javier Martinez Canillasa8411782015-06-09 13:04:42 +020059 CROS_EC_DEV_VERSION, msg->result);
60 ret = -EINVAL;
61 goto exit;
Bill Richardsone7c256f2015-02-02 12:26:25 +010062 }
63
Javier Martinez Canillasa8411782015-06-09 13:04:42 +020064 resp = (struct ec_response_get_version *)msg->data;
Bill Richardsone7c256f2015-02-02 12:26:25 +010065 if (resp->current_image >= ARRAY_SIZE(current_image_name))
66 resp->current_image = 3; /* invalid */
67
Olof Johanssonef59c252015-03-03 13:22:32 +010068 snprintf(str, maxlen, "%s\n%s\n%s\n%s\n", CROS_EC_DEV_VERSION,
Bill Richardsone7c256f2015-02-02 12:26:25 +010069 resp->version_string_ro, resp->version_string_rw,
70 current_image_name[resp->current_image]);
71
Javier Martinez Canillasa8411782015-06-09 13:04:42 +020072 ret = 0;
73exit:
74 kfree(msg);
75 return ret;
Bill Richardsone7c256f2015-02-02 12:26:25 +010076}
77
78/* Device file ops */
79static int ec_device_open(struct inode *inode, struct file *filp)
80{
81 filp->private_data = container_of(inode->i_cdev,
82 struct cros_ec_device, cdev);
83 return 0;
84}
85
86static int ec_device_release(struct inode *inode, struct file *filp)
87{
88 return 0;
89}
90
91static ssize_t ec_device_read(struct file *filp, char __user *buffer,
92 size_t length, loff_t *offset)
93{
94 struct cros_ec_device *ec = filp->private_data;
95 char msg[sizeof(struct ec_response_get_version) +
96 sizeof(CROS_EC_DEV_VERSION)];
97 size_t count;
98 int ret;
99
100 if (*offset != 0)
101 return 0;
102
103 ret = ec_get_version(ec, msg, sizeof(msg));
104 if (ret)
105 return ret;
106
107 count = min(length, strlen(msg));
108
109 if (copy_to_user(buffer, msg, count))
110 return -EFAULT;
111
112 *offset = count;
113 return count;
114}
115
116/* Ioctls */
117static long ec_device_ioctl_xcmd(struct cros_ec_device *ec, void __user *arg)
118{
119 long ret;
Javier Martinez Canillasa8411782015-06-09 13:04:42 +0200120 struct cros_ec_command u_cmd;
121 struct cros_ec_command *s_cmd;
Bill Richardsone7c256f2015-02-02 12:26:25 +0100122
Javier Martinez Canillasa8411782015-06-09 13:04:42 +0200123 if (copy_from_user(&u_cmd, arg, sizeof(u_cmd)))
Bill Richardsone7c256f2015-02-02 12:26:25 +0100124 return -EFAULT;
125
Javier Martinez Canillasa8411782015-06-09 13:04:42 +0200126 s_cmd = kmalloc(sizeof(*s_cmd) + max(u_cmd.outsize, u_cmd.insize),
127 GFP_KERNEL);
128 if (!s_cmd)
129 return -ENOMEM;
130
131 if (copy_from_user(s_cmd, arg, sizeof(*s_cmd) + u_cmd.outsize)) {
132 ret = -EFAULT;
133 goto exit;
134 }
135
136 ret = cros_ec_cmd_xfer(ec, s_cmd);
Bill Richardsone7c256f2015-02-02 12:26:25 +0100137 /* Only copy data to userland if data was received. */
138 if (ret < 0)
Javier Martinez Canillasa8411782015-06-09 13:04:42 +0200139 goto exit;
Bill Richardsone7c256f2015-02-02 12:26:25 +0100140
Javier Martinez Canillasa8411782015-06-09 13:04:42 +0200141 if (copy_to_user(arg, s_cmd, sizeof(*s_cmd) + u_cmd.insize))
142 ret = -EFAULT;
143exit:
144 kfree(s_cmd);
145 return ret;
Bill Richardsone7c256f2015-02-02 12:26:25 +0100146}
147
148static long ec_device_ioctl_readmem(struct cros_ec_device *ec, void __user *arg)
149{
150 struct cros_ec_readmem s_mem = { };
151 long num;
152
153 /* Not every platform supports direct reads */
154 if (!ec->cmd_readmem)
155 return -ENOTTY;
156
157 if (copy_from_user(&s_mem, arg, sizeof(s_mem)))
158 return -EFAULT;
159
160 num = ec->cmd_readmem(ec, s_mem.offset, s_mem.bytes, s_mem.buffer);
161 if (num <= 0)
162 return num;
163
164 if (copy_to_user((void __user *)arg, &s_mem, sizeof(s_mem)))
165 return -EFAULT;
166
167 return 0;
168}
169
170static long ec_device_ioctl(struct file *filp, unsigned int cmd,
171 unsigned long arg)
172{
173 struct cros_ec_device *ec = filp->private_data;
174
175 if (_IOC_TYPE(cmd) != CROS_EC_DEV_IOC)
176 return -ENOTTY;
177
178 switch (cmd) {
179 case CROS_EC_DEV_IOCXCMD:
180 return ec_device_ioctl_xcmd(ec, (void __user *)arg);
181 case CROS_EC_DEV_IOCRDMEM:
182 return ec_device_ioctl_readmem(ec, (void __user *)arg);
183 }
184
185 return -ENOTTY;
186}
187
188/* Module initialization */
189static const struct file_operations fops = {
190 .open = ec_device_open,
191 .release = ec_device_release,
192 .read = ec_device_read,
193 .unlocked_ioctl = ec_device_ioctl,
194};
195
196static int ec_device_probe(struct platform_device *pdev)
197{
198 struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent);
199 int retval = -ENOTTY;
200 dev_t devno = MKDEV(ec_major, 0);
201
202 /* Instantiate it (and remember the EC) */
203 cdev_init(&ec->cdev, &fops);
204
205 retval = cdev_add(&ec->cdev, devno, 1);
206 if (retval) {
207 dev_err(&pdev->dev, ": failed to add character device\n");
208 return retval;
209 }
210
211 ec->vdev = device_create(cros_class, NULL, devno, ec,
212 CROS_EC_DEV_NAME);
213 if (IS_ERR(ec->vdev)) {
214 retval = PTR_ERR(ec->vdev);
215 dev_err(&pdev->dev, ": failed to create device\n");
216 cdev_del(&ec->cdev);
217 return retval;
218 }
219
Bill Richardson71af4b52015-02-02 12:26:27 +0100220 /* Initialize extra interfaces */
221 ec_dev_sysfs_init(ec);
Bill Richardsonf3f837e2015-02-02 12:26:28 +0100222 ec_dev_lightbar_init(ec);
Bill Richardson71af4b52015-02-02 12:26:27 +0100223
Bill Richardsone7c256f2015-02-02 12:26:25 +0100224 return 0;
225}
226
227static int ec_device_remove(struct platform_device *pdev)
228{
229 struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent);
230
Bill Richardsonf3f837e2015-02-02 12:26:28 +0100231 ec_dev_lightbar_remove(ec);
Bill Richardson71af4b52015-02-02 12:26:27 +0100232 ec_dev_sysfs_remove(ec);
Bill Richardsone7c256f2015-02-02 12:26:25 +0100233 device_destroy(cros_class, MKDEV(ec_major, 0));
234 cdev_del(&ec->cdev);
235 return 0;
236}
237
238static struct platform_driver cros_ec_dev_driver = {
239 .driver = {
240 .name = "cros-ec-ctl",
241 },
242 .probe = ec_device_probe,
243 .remove = ec_device_remove,
244};
245
246static int __init cros_ec_dev_init(void)
247{
248 int ret;
249 dev_t dev = 0;
250
251 cros_class = class_create(THIS_MODULE, "chromeos");
252 if (IS_ERR(cros_class)) {
253 pr_err(CROS_EC_DEV_NAME ": failed to register device class\n");
254 return PTR_ERR(cros_class);
255 }
256
257 /* Get a range of minor numbers (starting with 0) to work with */
258 ret = alloc_chrdev_region(&dev, 0, CROS_MAX_DEV, CROS_EC_DEV_NAME);
259 if (ret < 0) {
260 pr_err(CROS_EC_DEV_NAME ": alloc_chrdev_region() failed\n");
261 goto failed_chrdevreg;
262 }
263 ec_major = MAJOR(dev);
264
265 /* Register the driver */
266 ret = platform_driver_register(&cros_ec_dev_driver);
267 if (ret < 0) {
268 pr_warn(CROS_EC_DEV_NAME ": can't register driver: %d\n", ret);
269 goto failed_devreg;
270 }
271 return 0;
272
273failed_devreg:
274 unregister_chrdev_region(MKDEV(ec_major, 0), CROS_MAX_DEV);
275failed_chrdevreg:
276 class_destroy(cros_class);
277 return ret;
278}
279
280static void __exit cros_ec_dev_exit(void)
281{
282 platform_driver_unregister(&cros_ec_dev_driver);
283 unregister_chrdev(ec_major, CROS_EC_DEV_NAME);
284 class_destroy(cros_class);
285}
286
287module_init(cros_ec_dev_init);
288module_exit(cros_ec_dev_exit);
289
290MODULE_AUTHOR("Bill Richardson <wfrichar@chromium.org>");
291MODULE_DESCRIPTION("Userspace interface to the Chrome OS Embedded Controller");
292MODULE_VERSION("1.0");
293MODULE_LICENSE("GPL");