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>
24 #include <linux/slab.h>
25 #include <linux/uaccess.h>
27 #include "cros_ec_dev.h"
29 /* Device variables */
30 #define CROS_MAX_DEV 128
33 static const struct attribute_group
*cros_ec_groups
[] = {
35 &cros_ec_lightbar_attr_group
,
36 &cros_ec_vbc_attr_group
,
40 static struct class cros_class
= {
43 .dev_groups
= cros_ec_groups
,
46 /* Basic communication */
47 static int ec_get_version(struct cros_ec_dev
*ec
, char *str
, int maxlen
)
49 struct ec_response_get_version
*resp
;
50 static const char * const current_image_name
[] = {
51 "unknown", "read-only", "read-write", "invalid",
53 struct cros_ec_command
*msg
;
56 msg
= kmalloc(sizeof(*msg
) + sizeof(*resp
), GFP_KERNEL
);
61 msg
->command
= EC_CMD_GET_VERSION
+ ec
->cmd_offset
;
62 msg
->insize
= sizeof(*resp
);
65 ret
= cros_ec_cmd_xfer(ec
->ec_dev
, msg
);
69 if (msg
->result
!= EC_RES_SUCCESS
) {
71 "%s\nUnknown EC version: EC returned %d\n",
72 CROS_EC_DEV_VERSION
, msg
->result
);
77 resp
= (struct ec_response_get_version
*)msg
->data
;
78 if (resp
->current_image
>= ARRAY_SIZE(current_image_name
))
79 resp
->current_image
= 3; /* invalid */
81 snprintf(str
, maxlen
, "%s\n%s\n%s\n%s\n", CROS_EC_DEV_VERSION
,
82 resp
->version_string_ro
, resp
->version_string_rw
,
83 current_image_name
[resp
->current_image
]);
91 static int cros_ec_check_features(struct cros_ec_dev
*ec
, int feature
)
93 struct cros_ec_command
*msg
;
96 if (ec
->features
[0] == -1U && ec
->features
[1] == -1U) {
97 /* features bitmap not read yet */
99 msg
= kmalloc(sizeof(*msg
) + sizeof(ec
->features
), GFP_KERNEL
);
104 msg
->command
= EC_CMD_GET_FEATURES
+ ec
->cmd_offset
;
105 msg
->insize
= sizeof(ec
->features
);
108 ret
= cros_ec_cmd_xfer(ec
->ec_dev
, msg
);
109 if (ret
< 0 || msg
->result
!= EC_RES_SUCCESS
) {
110 dev_warn(ec
->dev
, "cannot get EC features: %d/%d\n",
112 memset(ec
->features
, 0, sizeof(ec
->features
));
115 memcpy(ec
->features
, msg
->data
, sizeof(ec
->features
));
117 dev_dbg(ec
->dev
, "EC features %08x %08x\n",
118 ec
->features
[0], ec
->features
[1]);
123 return ec
->features
[feature
/ 32] & EC_FEATURE_MASK_0(feature
);
126 /* Device file ops */
127 static int ec_device_open(struct inode
*inode
, struct file
*filp
)
129 struct cros_ec_dev
*ec
= container_of(inode
->i_cdev
,
130 struct cros_ec_dev
, cdev
);
131 filp
->private_data
= ec
;
132 nonseekable_open(inode
, filp
);
136 static int ec_device_release(struct inode
*inode
, struct file
*filp
)
141 static ssize_t
ec_device_read(struct file
*filp
, char __user
*buffer
,
142 size_t length
, loff_t
*offset
)
144 struct cros_ec_dev
*ec
= filp
->private_data
;
145 char msg
[sizeof(struct ec_response_get_version
) +
146 sizeof(CROS_EC_DEV_VERSION
)];
153 ret
= ec_get_version(ec
, msg
, sizeof(msg
));
157 count
= min(length
, strlen(msg
));
159 if (copy_to_user(buffer
, msg
, count
))
167 static long ec_device_ioctl_xcmd(struct cros_ec_dev
*ec
, void __user
*arg
)
170 struct cros_ec_command u_cmd
;
171 struct cros_ec_command
*s_cmd
;
173 if (copy_from_user(&u_cmd
, arg
, sizeof(u_cmd
)))
176 if ((u_cmd
.outsize
> EC_MAX_MSG_BYTES
) ||
177 (u_cmd
.insize
> EC_MAX_MSG_BYTES
))
180 s_cmd
= kmalloc(sizeof(*s_cmd
) + max(u_cmd
.outsize
, u_cmd
.insize
),
185 if (copy_from_user(s_cmd
, arg
, sizeof(*s_cmd
) + u_cmd
.outsize
)) {
190 if (u_cmd
.outsize
!= s_cmd
->outsize
||
191 u_cmd
.insize
!= s_cmd
->insize
) {
196 s_cmd
->command
+= ec
->cmd_offset
;
197 ret
= cros_ec_cmd_xfer(ec
->ec_dev
, s_cmd
);
198 /* Only copy data to userland if data was received. */
202 if (copy_to_user(arg
, s_cmd
, sizeof(*s_cmd
) + s_cmd
->insize
))
209 static long ec_device_ioctl_readmem(struct cros_ec_dev
*ec
, void __user
*arg
)
211 struct cros_ec_device
*ec_dev
= ec
->ec_dev
;
212 struct cros_ec_readmem s_mem
= { };
215 /* Not every platform supports direct reads */
216 if (!ec_dev
->cmd_readmem
)
219 if (copy_from_user(&s_mem
, arg
, sizeof(s_mem
)))
222 num
= ec_dev
->cmd_readmem(ec_dev
, s_mem
.offset
, s_mem
.bytes
,
227 if (copy_to_user((void __user
*)arg
, &s_mem
, sizeof(s_mem
)))
233 static long ec_device_ioctl(struct file
*filp
, unsigned int cmd
,
236 struct cros_ec_dev
*ec
= filp
->private_data
;
238 if (_IOC_TYPE(cmd
) != CROS_EC_DEV_IOC
)
242 case CROS_EC_DEV_IOCXCMD
:
243 return ec_device_ioctl_xcmd(ec
, (void __user
*)arg
);
244 case CROS_EC_DEV_IOCRDMEM
:
245 return ec_device_ioctl_readmem(ec
, (void __user
*)arg
);
251 /* Module initialization */
252 static const struct file_operations fops
= {
253 .open
= ec_device_open
,
254 .release
= ec_device_release
,
255 .read
= ec_device_read
,
256 .unlocked_ioctl
= ec_device_ioctl
,
258 .compat_ioctl
= ec_device_ioctl
,
262 static void __remove(struct device
*dev
)
264 struct cros_ec_dev
*ec
= container_of(dev
, struct cros_ec_dev
,
269 static void cros_ec_sensors_register(struct cros_ec_dev
*ec
)
272 * Issue a command to get the number of sensor reported.
273 * Build an array of sensors driver and register them all.
275 int ret
, i
, id
, sensor_num
;
276 struct mfd_cell
*sensor_cells
;
277 struct cros_ec_sensor_platform
*sensor_platforms
;
278 int sensor_type
[MOTIONSENSE_TYPE_MAX
];
279 struct ec_params_motion_sense
*params
;
280 struct ec_response_motion_sense
*resp
;
281 struct cros_ec_command
*msg
;
283 msg
= kzalloc(sizeof(struct cros_ec_command
) +
284 max(sizeof(*params
), sizeof(*resp
)), GFP_KERNEL
);
289 msg
->command
= EC_CMD_MOTION_SENSE_CMD
+ ec
->cmd_offset
;
290 msg
->outsize
= sizeof(*params
);
291 msg
->insize
= sizeof(*resp
);
293 params
= (struct ec_params_motion_sense
*)msg
->data
;
294 params
->cmd
= MOTIONSENSE_CMD_DUMP
;
296 ret
= cros_ec_cmd_xfer(ec
->ec_dev
, msg
);
297 if (ret
< 0 || msg
->result
!= EC_RES_SUCCESS
) {
298 dev_warn(ec
->dev
, "cannot get EC sensor information: %d/%d\n",
303 resp
= (struct ec_response_motion_sense
*)msg
->data
;
304 sensor_num
= resp
->dump
.sensor_count
;
305 /* Allocate 2 extra sensors in case lid angle or FIFO are needed */
306 sensor_cells
= kzalloc(sizeof(struct mfd_cell
) * (sensor_num
+ 2),
308 if (sensor_cells
== NULL
)
311 sensor_platforms
= kzalloc(sizeof(struct cros_ec_sensor_platform
) *
312 (sensor_num
+ 1), GFP_KERNEL
);
313 if (sensor_platforms
== NULL
)
314 goto error_platforms
;
316 memset(sensor_type
, 0, sizeof(sensor_type
));
318 for (i
= 0; i
< sensor_num
; i
++) {
319 params
->cmd
= MOTIONSENSE_CMD_INFO
;
320 params
->info
.sensor_num
= i
;
321 ret
= cros_ec_cmd_xfer(ec
->ec_dev
, msg
);
322 if (ret
< 0 || msg
->result
!= EC_RES_SUCCESS
) {
323 dev_warn(ec
->dev
, "no info for EC sensor %d : %d/%d\n",
324 i
, ret
, msg
->result
);
327 switch (resp
->info
.type
) {
328 case MOTIONSENSE_TYPE_ACCEL
:
329 sensor_cells
[id
].name
= "cros-ec-accel";
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
]++;
359 if (sensor_type
[MOTIONSENSE_TYPE_ACCEL
] >= 2) {
360 sensor_platforms
[id
].sensor_num
= sensor_num
;
362 sensor_cells
[id
].name
= "cros-ec-angle";
363 sensor_cells
[id
].id
= 0;
364 sensor_cells
[id
].platform_data
= &sensor_platforms
[id
];
365 sensor_cells
[id
].pdata_size
=
366 sizeof(struct cros_ec_sensor_platform
);
369 if (cros_ec_check_features(ec
, EC_FEATURE_MOTION_SENSE_FIFO
)) {
370 sensor_cells
[id
].name
= "cros-ec-ring";
374 ret
= mfd_add_devices(ec
->dev
, 0, sensor_cells
, id
,
377 dev_err(ec
->dev
, "failed to add EC sensors\n");
379 kfree(sensor_platforms
);
386 static int ec_device_probe(struct platform_device
*pdev
)
388 int retval
= -ENOMEM
;
389 struct device
*dev
= &pdev
->dev
;
390 struct cros_ec_platform
*ec_platform
= dev_get_platdata(dev
);
391 dev_t devno
= MKDEV(ec_major
, pdev
->id
);
392 struct cros_ec_dev
*ec
= kzalloc(sizeof(*ec
), GFP_KERNEL
);
397 dev_set_drvdata(dev
, ec
);
398 ec
->ec_dev
= dev_get_drvdata(dev
->parent
);
400 ec
->cmd_offset
= ec_platform
->cmd_offset
;
401 ec
->features
[0] = -1U; /* Not cached yet */
402 ec
->features
[1] = -1U; /* Not cached yet */
403 device_initialize(&ec
->class_dev
);
404 cdev_init(&ec
->cdev
, &fops
);
407 * Add the character device
408 * Link cdev to the class device to be sure device is not used
409 * before unbinding it.
411 ec
->cdev
.kobj
.parent
= &ec
->class_dev
.kobj
;
412 retval
= cdev_add(&ec
->cdev
, devno
, 1);
414 dev_err(dev
, ": failed to add character device\n");
415 goto cdev_add_failed
;
419 * Add the class device
420 * Link to the character device for creating the /dev entry
423 ec
->class_dev
.devt
= ec
->cdev
.dev
;
424 ec
->class_dev
.class = &cros_class
;
425 ec
->class_dev
.parent
= dev
;
426 ec
->class_dev
.release
= __remove
;
428 retval
= dev_set_name(&ec
->class_dev
, "%s", ec_platform
->ec_name
);
430 dev_err(dev
, "dev_set_name failed => %d\n", retval
);
431 goto set_named_failed
;
434 retval
= device_add(&ec
->class_dev
);
436 dev_err(dev
, "device_register failed => %d\n", retval
);
440 /* check whether this EC is a sensor hub. */
441 if (cros_ec_check_features(ec
, EC_FEATURE_MOTION_SENSE
))
442 cros_ec_sensors_register(ec
);
448 dev_set_drvdata(dev
, NULL
);
455 static int ec_device_remove(struct platform_device
*pdev
)
457 struct cros_ec_dev
*ec
= dev_get_drvdata(&pdev
->dev
);
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 struct platform_driver cros_ec_dev_driver
= {
471 .name
= "cros-ec-ctl",
473 .probe
= ec_device_probe
,
474 .remove
= ec_device_remove
,
477 static int __init
cros_ec_dev_init(void)
482 ret
= class_register(&cros_class
);
484 pr_err(CROS_EC_DEV_NAME
": failed to register device class\n");
488 /* Get a range of minor numbers (starting with 0) to work with */
489 ret
= alloc_chrdev_region(&dev
, 0, CROS_MAX_DEV
, CROS_EC_DEV_NAME
);
491 pr_err(CROS_EC_DEV_NAME
": alloc_chrdev_region() failed\n");
492 goto failed_chrdevreg
;
494 ec_major
= MAJOR(dev
);
496 /* Register the driver */
497 ret
= platform_driver_register(&cros_ec_dev_driver
);
499 pr_warn(CROS_EC_DEV_NAME
": can't register driver: %d\n", ret
);
505 unregister_chrdev_region(MKDEV(ec_major
, 0), CROS_MAX_DEV
);
507 class_unregister(&cros_class
);
511 static void __exit
cros_ec_dev_exit(void)
513 platform_driver_unregister(&cros_ec_dev_driver
);
514 unregister_chrdev(ec_major
, CROS_EC_DEV_NAME
);
515 class_unregister(&cros_class
);
518 module_init(cros_ec_dev_init
);
519 module_exit(cros_ec_dev_exit
);
521 MODULE_AUTHOR("Bill Richardson <wfrichar@chromium.org>");
522 MODULE_DESCRIPTION("Userspace interface to the Chrome OS Embedded Controller");
523 MODULE_VERSION("1.0");
524 MODULE_LICENSE("GPL");