2 * cros_ec_dev - expose the Chrome OS Embedded Controller to user-space
4 * Copyright (C) 2014 Google, Inc.
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.
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.
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/>.
21 #include <linux/mfd/core.h>
22 #include <linux/module.h>
23 #include <linux/platform_device.h>
25 #include <linux/slab.h>
26 #include <linux/uaccess.h>
28 #include "cros_ec_dev.h"
30 #define DRV_NAME "cros-ec-dev"
32 /* Device variables */
33 #define CROS_MAX_DEV 128
36 static const struct attribute_group *cros_ec_groups[] = {
38 &cros_ec_lightbar_attr_group,
39 &cros_ec_vbc_attr_group,
43 static struct class cros_class = {
46 .dev_groups = cros_ec_groups,
49 /* Basic communication */
50 static int ec_get_version(struct cros_ec_dev *ec, char *str, int maxlen)
52 struct ec_response_get_version *resp;
53 static const char * const current_image_name[] = {
54 "unknown", "read-only", "read-write", "invalid",
56 struct cros_ec_command *msg;
59 msg = kmalloc(sizeof(*msg) + sizeof(*resp), GFP_KERNEL);
64 msg->command = EC_CMD_GET_VERSION + ec->cmd_offset;
65 msg->insize = sizeof(*resp);
68 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
72 if (msg->result != EC_RES_SUCCESS) {
74 "%s\nUnknown EC version: EC returned %d\n",
75 CROS_EC_DEV_VERSION, msg->result);
80 resp = (struct ec_response_get_version *)msg->data;
81 if (resp->current_image >= ARRAY_SIZE(current_image_name))
82 resp->current_image = 3; /* invalid */
84 snprintf(str, maxlen, "%s\n%s\n%s\n%s\n", CROS_EC_DEV_VERSION,
85 resp->version_string_ro, resp->version_string_rw,
86 current_image_name[resp->current_image]);
94 static int cros_ec_check_features(struct cros_ec_dev *ec, int feature)
96 struct cros_ec_command *msg;
99 if (ec->features[0] == -1U && ec->features[1] == -1U) {
100 /* features bitmap not read yet */
102 msg = kmalloc(sizeof(*msg) + sizeof(ec->features), GFP_KERNEL);
107 msg->command = EC_CMD_GET_FEATURES + ec->cmd_offset;
108 msg->insize = sizeof(ec->features);
111 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
112 if (ret < 0 || msg->result != EC_RES_SUCCESS) {
113 dev_warn(ec->dev, "cannot get EC features: %d/%d\n",
115 memset(ec->features, 0, sizeof(ec->features));
117 memcpy(ec->features, msg->data, sizeof(ec->features));
120 dev_dbg(ec->dev, "EC features %08x %08x\n",
121 ec->features[0], ec->features[1]);
126 return ec->features[feature / 32] & EC_FEATURE_MASK_0(feature);
129 /* Device file ops */
130 static int ec_device_open(struct inode *inode, struct file *filp)
132 struct cros_ec_dev *ec = container_of(inode->i_cdev,
133 struct cros_ec_dev, cdev);
134 filp->private_data = ec;
135 nonseekable_open(inode, filp);
139 static int ec_device_release(struct inode *inode, struct file *filp)
144 static ssize_t ec_device_read(struct file *filp, char __user *buffer,
145 size_t length, loff_t *offset)
147 struct cros_ec_dev *ec = filp->private_data;
148 char msg[sizeof(struct ec_response_get_version) +
149 sizeof(CROS_EC_DEV_VERSION)];
156 ret = ec_get_version(ec, msg, sizeof(msg));
160 count = min(length, strlen(msg));
162 if (copy_to_user(buffer, msg, count))
170 static long ec_device_ioctl_xcmd(struct cros_ec_dev *ec, void __user *arg)
173 struct cros_ec_command u_cmd;
174 struct cros_ec_command *s_cmd;
176 if (copy_from_user(&u_cmd, arg, sizeof(u_cmd)))
179 if ((u_cmd.outsize > EC_MAX_MSG_BYTES) ||
180 (u_cmd.insize > EC_MAX_MSG_BYTES))
183 s_cmd = kmalloc(sizeof(*s_cmd) + max(u_cmd.outsize, u_cmd.insize),
188 if (copy_from_user(s_cmd, arg, sizeof(*s_cmd) + u_cmd.outsize)) {
193 if (u_cmd.outsize != s_cmd->outsize ||
194 u_cmd.insize != s_cmd->insize) {
199 s_cmd->command += ec->cmd_offset;
200 ret = cros_ec_cmd_xfer(ec->ec_dev, s_cmd);
201 /* Only copy data to userland if data was received. */
205 if (copy_to_user(arg, s_cmd, sizeof(*s_cmd) + s_cmd->insize))
212 static long ec_device_ioctl_readmem(struct cros_ec_dev *ec, void __user *arg)
214 struct cros_ec_device *ec_dev = ec->ec_dev;
215 struct cros_ec_readmem s_mem = { };
218 /* Not every platform supports direct reads */
219 if (!ec_dev->cmd_readmem)
222 if (copy_from_user(&s_mem, arg, sizeof(s_mem)))
225 num = ec_dev->cmd_readmem(ec_dev, s_mem.offset, s_mem.bytes,
230 if (copy_to_user((void __user *)arg, &s_mem, sizeof(s_mem)))
236 static long ec_device_ioctl(struct file *filp, unsigned int cmd,
239 struct cros_ec_dev *ec = filp->private_data;
241 if (_IOC_TYPE(cmd) != CROS_EC_DEV_IOC)
245 case CROS_EC_DEV_IOCXCMD:
246 return ec_device_ioctl_xcmd(ec, (void __user *)arg);
247 case CROS_EC_DEV_IOCRDMEM:
248 return ec_device_ioctl_readmem(ec, (void __user *)arg);
254 /* Module initialization */
255 static const struct file_operations fops = {
256 .open = ec_device_open,
257 .release = ec_device_release,
258 .read = ec_device_read,
259 .unlocked_ioctl = ec_device_ioctl,
261 .compat_ioctl = ec_device_ioctl,
265 static void cros_ec_sensors_register(struct cros_ec_dev *ec)
268 * Issue a command to get the number of sensor reported.
269 * Build an array of sensors driver and register them all.
271 int ret, i, id, sensor_num;
272 struct mfd_cell *sensor_cells;
273 struct cros_ec_sensor_platform *sensor_platforms;
274 int sensor_type[MOTIONSENSE_TYPE_MAX];
275 struct ec_params_motion_sense *params;
276 struct ec_response_motion_sense *resp;
277 struct cros_ec_command *msg;
279 msg = kzalloc(sizeof(struct cros_ec_command) +
280 max(sizeof(*params), sizeof(*resp)), GFP_KERNEL);
285 msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset;
286 msg->outsize = sizeof(*params);
287 msg->insize = sizeof(*resp);
289 params = (struct ec_params_motion_sense *)msg->data;
290 params->cmd = MOTIONSENSE_CMD_DUMP;
292 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
293 if (ret < 0 || msg->result != EC_RES_SUCCESS) {
294 dev_warn(ec->dev, "cannot get EC sensor information: %d/%d\n",
299 resp = (struct ec_response_motion_sense *)msg->data;
300 sensor_num = resp->dump.sensor_count;
301 /* Allocate 1 extra sensors in FIFO are needed */
302 sensor_cells = kcalloc(sensor_num + 1, sizeof(struct mfd_cell),
304 if (sensor_cells == NULL)
307 sensor_platforms = kcalloc(sensor_num + 1,
308 sizeof(struct cros_ec_sensor_platform),
310 if (sensor_platforms == NULL)
311 goto error_platforms;
313 memset(sensor_type, 0, sizeof(sensor_type));
315 for (i = 0; i < sensor_num; i++) {
316 params->cmd = MOTIONSENSE_CMD_INFO;
317 params->info.sensor_num = i;
318 ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
319 if (ret < 0 || msg->result != EC_RES_SUCCESS) {
320 dev_warn(ec->dev, "no info for EC sensor %d : %d/%d\n",
321 i, ret, msg->result);
324 switch (resp->info.type) {
325 case MOTIONSENSE_TYPE_ACCEL:
326 sensor_cells[id].name = "cros-ec-accel";
328 case MOTIONSENSE_TYPE_BARO:
329 sensor_cells[id].name = "cros-ec-baro";
331 case MOTIONSENSE_TYPE_GYRO:
332 sensor_cells[id].name = "cros-ec-gyro";
334 case MOTIONSENSE_TYPE_MAG:
335 sensor_cells[id].name = "cros-ec-mag";
337 case MOTIONSENSE_TYPE_PROX:
338 sensor_cells[id].name = "cros-ec-prox";
340 case MOTIONSENSE_TYPE_LIGHT:
341 sensor_cells[id].name = "cros-ec-light";
343 case MOTIONSENSE_TYPE_ACTIVITY:
344 sensor_cells[id].name = "cros-ec-activity";
347 dev_warn(ec->dev, "unknown type %d\n", resp->info.type);
350 sensor_platforms[id].sensor_num = i;
351 sensor_cells[id].id = sensor_type[resp->info.type];
352 sensor_cells[id].platform_data = &sensor_platforms[id];
353 sensor_cells[id].pdata_size =
354 sizeof(struct cros_ec_sensor_platform);
356 sensor_type[resp->info.type]++;
360 if (sensor_type[MOTIONSENSE_TYPE_ACCEL] >= 2)
361 ec->has_kb_wake_angle = true;
363 if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE_FIFO)) {
364 sensor_cells[id].name = "cros-ec-ring";
368 ret = mfd_add_devices(ec->dev, 0, sensor_cells, id,
371 dev_err(ec->dev, "failed to add EC sensors\n");
373 kfree(sensor_platforms);
380 static const struct mfd_cell cros_ec_rtc_cells[] = {
381 { .name = "cros-ec-rtc" }
384 static int ec_device_probe(struct platform_device *pdev)
386 int retval = -ENOMEM;
387 struct device *dev = &pdev->dev;
388 struct cros_ec_platform *ec_platform = dev_get_platdata(dev);
389 struct cros_ec_dev *ec = devm_kzalloc(dev, sizeof(*ec), GFP_KERNEL);
394 dev_set_drvdata(dev, ec);
395 ec->ec_dev = dev_get_drvdata(dev->parent);
397 ec->cmd_offset = ec_platform->cmd_offset;
398 ec->features[0] = -1U; /* Not cached yet */
399 ec->features[1] = -1U; /* Not cached yet */
400 device_initialize(&ec->class_dev);
401 cdev_init(&ec->cdev, &fops);
404 * Add the class device
405 * Link to the character device for creating the /dev entry
408 ec->class_dev.devt = MKDEV(ec_major, pdev->id);
409 ec->class_dev.class = &cros_class;
410 ec->class_dev.parent = dev;
412 retval = dev_set_name(&ec->class_dev, "%s", ec_platform->ec_name);
414 dev_err(dev, "dev_set_name failed => %d\n", retval);
418 /* check whether this EC is a sensor hub. */
419 if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE))
420 cros_ec_sensors_register(ec);
422 /* Check whether this EC instance has RTC host command support */
423 if (cros_ec_check_features(ec, EC_FEATURE_RTC)) {
424 retval = mfd_add_devices(ec->dev, PLATFORM_DEVID_AUTO,
426 ARRAY_SIZE(cros_ec_rtc_cells),
430 "failed to add cros-ec-rtc device: %d\n",
434 /* Take control of the lightbar from the EC. */
435 lb_manual_suspend_ctrl(ec, 1);
437 /* We can now add the sysfs class, we know which parameter to show */
438 retval = cdev_device_add(&ec->cdev, &ec->class_dev);
440 dev_err(dev, "cdev_device_add failed => %d\n", retval);
444 if (cros_ec_debugfs_init(ec))
445 dev_warn(dev, "failed to create debugfs directory\n");
450 put_device(&ec->class_dev);
454 static int ec_device_remove(struct platform_device *pdev)
456 struct cros_ec_dev *ec = dev_get_drvdata(&pdev->dev);
458 /* Let the EC take over the lightbar again. */
459 lb_manual_suspend_ctrl(ec, 0);
461 cros_ec_debugfs_remove(ec);
464 device_unregister(&ec->class_dev);
468 static void ec_device_shutdown(struct platform_device *pdev)
470 struct cros_ec_dev *ec = dev_get_drvdata(&pdev->dev);
472 /* Be sure to clear up debugfs delayed works */
473 cros_ec_debugfs_remove(ec);
476 static const struct platform_device_id cros_ec_id[] = {
480 MODULE_DEVICE_TABLE(platform, cros_ec_id);
482 static __maybe_unused int ec_device_suspend(struct device *dev)
484 struct cros_ec_dev *ec = dev_get_drvdata(dev);
486 cros_ec_debugfs_suspend(ec);
493 static __maybe_unused int ec_device_resume(struct device *dev)
495 struct cros_ec_dev *ec = dev_get_drvdata(dev);
497 cros_ec_debugfs_resume(ec);
504 static const struct dev_pm_ops cros_ec_dev_pm_ops = {
505 #ifdef CONFIG_PM_SLEEP
506 .suspend = ec_device_suspend,
507 .resume = ec_device_resume,
511 static struct platform_driver cros_ec_dev_driver = {
514 .pm = &cros_ec_dev_pm_ops,
516 .probe = ec_device_probe,
517 .remove = ec_device_remove,
518 .shutdown = ec_device_shutdown,
521 static int __init cros_ec_dev_init(void)
526 ret = class_register(&cros_class);
528 pr_err(CROS_EC_DEV_NAME ": failed to register device class\n");
532 /* Get a range of minor numbers (starting with 0) to work with */
533 ret = alloc_chrdev_region(&dev, 0, CROS_MAX_DEV, CROS_EC_DEV_NAME);
535 pr_err(CROS_EC_DEV_NAME ": alloc_chrdev_region() failed\n");
536 goto failed_chrdevreg;
538 ec_major = MAJOR(dev);
540 /* Register the driver */
541 ret = platform_driver_register(&cros_ec_dev_driver);
543 pr_warn(CROS_EC_DEV_NAME ": can't register driver: %d\n", ret);
549 unregister_chrdev_region(MKDEV(ec_major, 0), CROS_MAX_DEV);
551 class_unregister(&cros_class);
555 static void __exit cros_ec_dev_exit(void)
557 platform_driver_unregister(&cros_ec_dev_driver);
558 unregister_chrdev(ec_major, CROS_EC_DEV_NAME);
559 class_unregister(&cros_class);
562 module_init(cros_ec_dev_init);
563 module_exit(cros_ec_dev_exit);
565 MODULE_ALIAS("platform:" DRV_NAME);
567 MODULE_DESCRIPTION("Userspace interface to the Chrome OS Embedded Controller");
568 MODULE_VERSION("1.0");
569 MODULE_LICENSE("GPL");