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_debugfs.h"
29 #include "cros_ec_dev.h"
31 /* Device variables */
32 #define CROS_MAX_DEV 128
35 static const struct attribute_group
*cros_ec_groups
[] = {
37 &cros_ec_lightbar_attr_group
,
38 &cros_ec_vbc_attr_group
,
42 static struct class cros_class
= {
45 .dev_groups
= cros_ec_groups
,
48 /* Basic communication */
49 static int ec_get_version(struct cros_ec_dev
*ec
, char *str
, int maxlen
)
51 struct ec_response_get_version
*resp
;
52 static const char * const current_image_name
[] = {
53 "unknown", "read-only", "read-write", "invalid",
55 struct cros_ec_command
*msg
;
58 msg
= kmalloc(sizeof(*msg
) + sizeof(*resp
), GFP_KERNEL
);
63 msg
->command
= EC_CMD_GET_VERSION
+ ec
->cmd_offset
;
64 msg
->insize
= sizeof(*resp
);
67 ret
= cros_ec_cmd_xfer(ec
->ec_dev
, msg
);
71 if (msg
->result
!= EC_RES_SUCCESS
) {
73 "%s\nUnknown EC version: EC returned %d\n",
74 CROS_EC_DEV_VERSION
, msg
->result
);
79 resp
= (struct ec_response_get_version
*)msg
->data
;
80 if (resp
->current_image
>= ARRAY_SIZE(current_image_name
))
81 resp
->current_image
= 3; /* invalid */
83 snprintf(str
, maxlen
, "%s\n%s\n%s\n%s\n", CROS_EC_DEV_VERSION
,
84 resp
->version_string_ro
, resp
->version_string_rw
,
85 current_image_name
[resp
->current_image
]);
93 static int cros_ec_check_features(struct cros_ec_dev
*ec
, int feature
)
95 struct cros_ec_command
*msg
;
98 if (ec
->features
[0] == -1U && ec
->features
[1] == -1U) {
99 /* features bitmap not read yet */
101 msg
= kmalloc(sizeof(*msg
) + sizeof(ec
->features
), GFP_KERNEL
);
106 msg
->command
= EC_CMD_GET_FEATURES
+ ec
->cmd_offset
;
107 msg
->insize
= sizeof(ec
->features
);
110 ret
= cros_ec_cmd_xfer(ec
->ec_dev
, msg
);
111 if (ret
< 0 || msg
->result
!= EC_RES_SUCCESS
) {
112 dev_warn(ec
->dev
, "cannot get EC features: %d/%d\n",
114 memset(ec
->features
, 0, sizeof(ec
->features
));
117 memcpy(ec
->features
, msg
->data
, sizeof(ec
->features
));
119 dev_dbg(ec
->dev
, "EC features %08x %08x\n",
120 ec
->features
[0], ec
->features
[1]);
125 return ec
->features
[feature
/ 32] & EC_FEATURE_MASK_0(feature
);
128 /* Device file ops */
129 static int ec_device_open(struct inode
*inode
, struct file
*filp
)
131 struct cros_ec_dev
*ec
= container_of(inode
->i_cdev
,
132 struct cros_ec_dev
, cdev
);
133 filp
->private_data
= ec
;
134 nonseekable_open(inode
, filp
);
138 static int ec_device_release(struct inode
*inode
, struct file
*filp
)
143 static ssize_t
ec_device_read(struct file
*filp
, char __user
*buffer
,
144 size_t length
, loff_t
*offset
)
146 struct cros_ec_dev
*ec
= filp
->private_data
;
147 char msg
[sizeof(struct ec_response_get_version
) +
148 sizeof(CROS_EC_DEV_VERSION
)];
155 ret
= ec_get_version(ec
, msg
, sizeof(msg
));
159 count
= min(length
, strlen(msg
));
161 if (copy_to_user(buffer
, msg
, count
))
169 static long ec_device_ioctl_xcmd(struct cros_ec_dev
*ec
, void __user
*arg
)
172 struct cros_ec_command u_cmd
;
173 struct cros_ec_command
*s_cmd
;
175 if (copy_from_user(&u_cmd
, arg
, sizeof(u_cmd
)))
178 if ((u_cmd
.outsize
> EC_MAX_MSG_BYTES
) ||
179 (u_cmd
.insize
> EC_MAX_MSG_BYTES
))
182 s_cmd
= kmalloc(sizeof(*s_cmd
) + max(u_cmd
.outsize
, u_cmd
.insize
),
187 if (copy_from_user(s_cmd
, arg
, sizeof(*s_cmd
) + u_cmd
.outsize
)) {
192 if (u_cmd
.outsize
!= s_cmd
->outsize
||
193 u_cmd
.insize
!= s_cmd
->insize
) {
198 s_cmd
->command
+= ec
->cmd_offset
;
199 ret
= cros_ec_cmd_xfer(ec
->ec_dev
, s_cmd
);
200 /* Only copy data to userland if data was received. */
204 if (copy_to_user(arg
, s_cmd
, sizeof(*s_cmd
) + s_cmd
->insize
))
211 static long ec_device_ioctl_readmem(struct cros_ec_dev
*ec
, void __user
*arg
)
213 struct cros_ec_device
*ec_dev
= ec
->ec_dev
;
214 struct cros_ec_readmem s_mem
= { };
217 /* Not every platform supports direct reads */
218 if (!ec_dev
->cmd_readmem
)
221 if (copy_from_user(&s_mem
, arg
, sizeof(s_mem
)))
224 num
= ec_dev
->cmd_readmem(ec_dev
, s_mem
.offset
, s_mem
.bytes
,
229 if (copy_to_user((void __user
*)arg
, &s_mem
, sizeof(s_mem
)))
235 static long ec_device_ioctl(struct file
*filp
, unsigned int cmd
,
238 struct cros_ec_dev
*ec
= filp
->private_data
;
240 if (_IOC_TYPE(cmd
) != CROS_EC_DEV_IOC
)
244 case CROS_EC_DEV_IOCXCMD
:
245 return ec_device_ioctl_xcmd(ec
, (void __user
*)arg
);
246 case CROS_EC_DEV_IOCRDMEM
:
247 return ec_device_ioctl_readmem(ec
, (void __user
*)arg
);
253 /* Module initialization */
254 static const struct file_operations fops
= {
255 .open
= ec_device_open
,
256 .release
= ec_device_release
,
257 .read
= ec_device_read
,
258 .unlocked_ioctl
= ec_device_ioctl
,
260 .compat_ioctl
= ec_device_ioctl
,
264 static void __remove(struct device
*dev
)
266 struct cros_ec_dev
*ec
= container_of(dev
, struct cros_ec_dev
,
271 static void cros_ec_sensors_register(struct cros_ec_dev
*ec
)
274 * Issue a command to get the number of sensor reported.
275 * Build an array of sensors driver and register them all.
277 int ret
, i
, id
, sensor_num
;
278 struct mfd_cell
*sensor_cells
;
279 struct cros_ec_sensor_platform
*sensor_platforms
;
280 int sensor_type
[MOTIONSENSE_TYPE_MAX
];
281 struct ec_params_motion_sense
*params
;
282 struct ec_response_motion_sense
*resp
;
283 struct cros_ec_command
*msg
;
285 msg
= kzalloc(sizeof(struct cros_ec_command
) +
286 max(sizeof(*params
), sizeof(*resp
)), GFP_KERNEL
);
291 msg
->command
= EC_CMD_MOTION_SENSE_CMD
+ ec
->cmd_offset
;
292 msg
->outsize
= sizeof(*params
);
293 msg
->insize
= sizeof(*resp
);
295 params
= (struct ec_params_motion_sense
*)msg
->data
;
296 params
->cmd
= MOTIONSENSE_CMD_DUMP
;
298 ret
= cros_ec_cmd_xfer(ec
->ec_dev
, msg
);
299 if (ret
< 0 || msg
->result
!= EC_RES_SUCCESS
) {
300 dev_warn(ec
->dev
, "cannot get EC sensor information: %d/%d\n",
305 resp
= (struct ec_response_motion_sense
*)msg
->data
;
306 sensor_num
= resp
->dump
.sensor_count
;
307 /* Allocate 2 extra sensors in case lid angle or FIFO are needed */
308 sensor_cells
= kzalloc(sizeof(struct mfd_cell
) * (sensor_num
+ 2),
310 if (sensor_cells
== NULL
)
313 sensor_platforms
= kzalloc(sizeof(struct cros_ec_sensor_platform
) *
314 (sensor_num
+ 1), GFP_KERNEL
);
315 if (sensor_platforms
== NULL
)
316 goto error_platforms
;
318 memset(sensor_type
, 0, sizeof(sensor_type
));
320 for (i
= 0; i
< sensor_num
; i
++) {
321 params
->cmd
= MOTIONSENSE_CMD_INFO
;
322 params
->info
.sensor_num
= i
;
323 ret
= cros_ec_cmd_xfer(ec
->ec_dev
, msg
);
324 if (ret
< 0 || msg
->result
!= EC_RES_SUCCESS
) {
325 dev_warn(ec
->dev
, "no info for EC sensor %d : %d/%d\n",
326 i
, ret
, msg
->result
);
329 switch (resp
->info
.type
) {
330 case MOTIONSENSE_TYPE_ACCEL
:
331 sensor_cells
[id
].name
= "cros-ec-accel";
333 case MOTIONSENSE_TYPE_BARO
:
334 sensor_cells
[id
].name
= "cros-ec-baro";
336 case MOTIONSENSE_TYPE_GYRO
:
337 sensor_cells
[id
].name
= "cros-ec-gyro";
339 case MOTIONSENSE_TYPE_MAG
:
340 sensor_cells
[id
].name
= "cros-ec-mag";
342 case MOTIONSENSE_TYPE_PROX
:
343 sensor_cells
[id
].name
= "cros-ec-prox";
345 case MOTIONSENSE_TYPE_LIGHT
:
346 sensor_cells
[id
].name
= "cros-ec-light";
348 case MOTIONSENSE_TYPE_ACTIVITY
:
349 sensor_cells
[id
].name
= "cros-ec-activity";
352 dev_warn(ec
->dev
, "unknown type %d\n", resp
->info
.type
);
355 sensor_platforms
[id
].sensor_num
= i
;
356 sensor_cells
[id
].id
= sensor_type
[resp
->info
.type
];
357 sensor_cells
[id
].platform_data
= &sensor_platforms
[id
];
358 sensor_cells
[id
].pdata_size
=
359 sizeof(struct cros_ec_sensor_platform
);
361 sensor_type
[resp
->info
.type
]++;
364 if (sensor_type
[MOTIONSENSE_TYPE_ACCEL
] >= 2) {
365 sensor_platforms
[id
].sensor_num
= sensor_num
;
367 sensor_cells
[id
].name
= "cros-ec-angle";
368 sensor_cells
[id
].id
= 0;
369 sensor_cells
[id
].platform_data
= &sensor_platforms
[id
];
370 sensor_cells
[id
].pdata_size
=
371 sizeof(struct cros_ec_sensor_platform
);
374 if (cros_ec_check_features(ec
, EC_FEATURE_MOTION_SENSE_FIFO
)) {
375 sensor_cells
[id
].name
= "cros-ec-ring";
379 ret
= mfd_add_devices(ec
->dev
, 0, sensor_cells
, id
,
382 dev_err(ec
->dev
, "failed to add EC sensors\n");
384 kfree(sensor_platforms
);
391 static int ec_device_probe(struct platform_device
*pdev
)
393 int retval
= -ENOMEM
;
394 struct device
*dev
= &pdev
->dev
;
395 struct cros_ec_platform
*ec_platform
= dev_get_platdata(dev
);
396 struct cros_ec_dev
*ec
= kzalloc(sizeof(*ec
), GFP_KERNEL
);
401 dev_set_drvdata(dev
, ec
);
402 ec
->ec_dev
= dev_get_drvdata(dev
->parent
);
404 ec
->cmd_offset
= ec_platform
->cmd_offset
;
405 ec
->features
[0] = -1U; /* Not cached yet */
406 ec
->features
[1] = -1U; /* Not cached yet */
407 device_initialize(&ec
->class_dev
);
408 cdev_init(&ec
->cdev
, &fops
);
411 * Add the class device
412 * Link to the character device for creating the /dev entry
415 ec
->class_dev
.devt
= MKDEV(ec_major
, pdev
->id
);
416 ec
->class_dev
.class = &cros_class
;
417 ec
->class_dev
.parent
= dev
;
418 ec
->class_dev
.release
= __remove
;
420 retval
= dev_set_name(&ec
->class_dev
, "%s", ec_platform
->ec_name
);
422 dev_err(dev
, "dev_set_name failed => %d\n", retval
);
426 retval
= cdev_device_add(&ec
->cdev
, &ec
->class_dev
);
428 dev_err(dev
, "cdev_device_add failed => %d\n", retval
);
432 if (cros_ec_debugfs_init(ec
))
433 dev_warn(dev
, "failed to create debugfs directory\n");
435 /* check whether this EC is a sensor hub. */
436 if (cros_ec_check_features(ec
, EC_FEATURE_MOTION_SENSE
))
437 cros_ec_sensors_register(ec
);
439 /* Take control of the lightbar from the EC. */
440 lb_manual_suspend_ctrl(ec
, 1);
445 put_device(&ec
->class_dev
);
449 static int ec_device_remove(struct platform_device
*pdev
)
451 struct cros_ec_dev
*ec
= dev_get_drvdata(&pdev
->dev
);
453 /* Let the EC take over the lightbar again. */
454 lb_manual_suspend_ctrl(ec
, 0);
456 cros_ec_debugfs_remove(ec
);
459 device_unregister(&ec
->class_dev
);
463 static const struct platform_device_id cros_ec_id
[] = {
464 { "cros-ec-ctl", 0 },
467 MODULE_DEVICE_TABLE(platform
, cros_ec_id
);
469 static __maybe_unused
int ec_device_suspend(struct device
*dev
)
471 struct cros_ec_dev
*ec
= dev_get_drvdata(dev
);
478 static __maybe_unused
int ec_device_resume(struct device
*dev
)
480 struct cros_ec_dev
*ec
= dev_get_drvdata(dev
);
487 static const struct dev_pm_ops cros_ec_dev_pm_ops
= {
488 #ifdef CONFIG_PM_SLEEP
489 .suspend
= ec_device_suspend
,
490 .resume
= ec_device_resume
,
494 static struct platform_driver cros_ec_dev_driver
= {
496 .name
= "cros-ec-ctl",
497 .pm
= &cros_ec_dev_pm_ops
,
499 .probe
= ec_device_probe
,
500 .remove
= ec_device_remove
,
503 static int __init
cros_ec_dev_init(void)
508 ret
= class_register(&cros_class
);
510 pr_err(CROS_EC_DEV_NAME
": failed to register device class\n");
514 /* Get a range of minor numbers (starting with 0) to work with */
515 ret
= alloc_chrdev_region(&dev
, 0, CROS_MAX_DEV
, CROS_EC_DEV_NAME
);
517 pr_err(CROS_EC_DEV_NAME
": alloc_chrdev_region() failed\n");
518 goto failed_chrdevreg
;
520 ec_major
= MAJOR(dev
);
522 /* Register the driver */
523 ret
= platform_driver_register(&cros_ec_dev_driver
);
525 pr_warn(CROS_EC_DEV_NAME
": can't register driver: %d\n", ret
);
531 unregister_chrdev_region(MKDEV(ec_major
, 0), CROS_MAX_DEV
);
533 class_unregister(&cros_class
);
537 static void __exit
cros_ec_dev_exit(void)
539 platform_driver_unregister(&cros_ec_dev_driver
);
540 unregister_chrdev(ec_major
, CROS_EC_DEV_NAME
);
541 class_unregister(&cros_class
);
544 module_init(cros_ec_dev_init
);
545 module_exit(cros_ec_dev_exit
);
547 MODULE_AUTHOR("Bill Richardson <wfrichar@chromium.org>");
548 MODULE_DESCRIPTION("Userspace interface to the Chrome OS Embedded Controller");
549 MODULE_VERSION("1.0");
550 MODULE_LICENSE("GPL");