xref: /linux/drivers/mfd/cros_ec_dev.c (revision 6fce0a2cf5a050e8a3326556d7d293e69be303be)
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 
20 #include <linux/fs.h>
21 #include <linux/mfd/core.h>
22 #include <linux/module.h>
23 #include <linux/mod_devicetable.h>
24 #include <linux/platform_device.h>
25 #include <linux/pm.h>
26 #include <linux/slab.h>
27 #include <linux/uaccess.h>
28 
29 #include "cros_ec_dev.h"
30 
31 #define DRV_NAME "cros-ec-dev"
32 
33 /* Device variables */
34 #define CROS_MAX_DEV 128
35 static int ec_major;
36 
37 static const struct attribute_group *cros_ec_groups[] = {
38 	&cros_ec_attr_group,
39 	NULL,
40 };
41 
42 static struct class cros_class = {
43 	.owner          = THIS_MODULE,
44 	.name           = "chromeos",
45 	.dev_groups     = cros_ec_groups,
46 };
47 
48 /* Basic communication */
49 static int ec_get_version(struct cros_ec_dev *ec, char *str, int maxlen)
50 {
51 	struct ec_response_get_version *resp;
52 	static const char * const current_image_name[] = {
53 		"unknown", "read-only", "read-write", "invalid",
54 	};
55 	struct cros_ec_command *msg;
56 	int ret;
57 
58 	msg = kmalloc(sizeof(*msg) + sizeof(*resp), GFP_KERNEL);
59 	if (!msg)
60 		return -ENOMEM;
61 
62 	msg->version = 0;
63 	msg->command = EC_CMD_GET_VERSION + ec->cmd_offset;
64 	msg->insize = sizeof(*resp);
65 	msg->outsize = 0;
66 
67 	ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
68 	if (ret < 0)
69 		goto exit;
70 
71 	if (msg->result != EC_RES_SUCCESS) {
72 		snprintf(str, maxlen,
73 			 "%s\nUnknown EC version: EC returned %d\n",
74 			 CROS_EC_DEV_VERSION, msg->result);
75 		ret = -EINVAL;
76 		goto exit;
77 	}
78 
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 */
82 
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]);
86 
87 	ret = 0;
88 exit:
89 	kfree(msg);
90 	return ret;
91 }
92 
93 static int cros_ec_check_features(struct cros_ec_dev *ec, int feature)
94 {
95 	struct cros_ec_command *msg;
96 	int ret;
97 
98 	if (ec->features[0] == -1U && ec->features[1] == -1U) {
99 		/* features bitmap not read yet */
100 
101 		msg = kmalloc(sizeof(*msg) + sizeof(ec->features), GFP_KERNEL);
102 		if (!msg)
103 			return -ENOMEM;
104 
105 		msg->version = 0;
106 		msg->command = EC_CMD_GET_FEATURES + ec->cmd_offset;
107 		msg->insize = sizeof(ec->features);
108 		msg->outsize = 0;
109 
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",
113 				 ret, msg->result);
114 			memset(ec->features, 0, sizeof(ec->features));
115 		} else {
116 			memcpy(ec->features, msg->data, sizeof(ec->features));
117 		}
118 
119 		dev_dbg(ec->dev, "EC features %08x %08x\n",
120 			ec->features[0], ec->features[1]);
121 
122 		kfree(msg);
123 	}
124 
125 	return ec->features[feature / 32] & EC_FEATURE_MASK_0(feature);
126 }
127 
128 /* Device file ops */
129 static int ec_device_open(struct inode *inode, struct file *filp)
130 {
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);
135 	return 0;
136 }
137 
138 static int ec_device_release(struct inode *inode, struct file *filp)
139 {
140 	return 0;
141 }
142 
143 static ssize_t ec_device_read(struct file *filp, char __user *buffer,
144 			      size_t length, loff_t *offset)
145 {
146 	struct cros_ec_dev *ec = filp->private_data;
147 	char msg[sizeof(struct ec_response_get_version) +
148 		 sizeof(CROS_EC_DEV_VERSION)];
149 	size_t count;
150 	int ret;
151 
152 	if (*offset != 0)
153 		return 0;
154 
155 	ret = ec_get_version(ec, msg, sizeof(msg));
156 	if (ret)
157 		return ret;
158 
159 	count = min(length, strlen(msg));
160 
161 	if (copy_to_user(buffer, msg, count))
162 		return -EFAULT;
163 
164 	*offset = count;
165 	return count;
166 }
167 
168 /* Ioctls */
169 static long ec_device_ioctl_xcmd(struct cros_ec_dev *ec, void __user *arg)
170 {
171 	long ret;
172 	struct cros_ec_command u_cmd;
173 	struct cros_ec_command *s_cmd;
174 
175 	if (copy_from_user(&u_cmd, arg, sizeof(u_cmd)))
176 		return -EFAULT;
177 
178 	if ((u_cmd.outsize > EC_MAX_MSG_BYTES) ||
179 	    (u_cmd.insize > EC_MAX_MSG_BYTES))
180 		return -EINVAL;
181 
182 	s_cmd = kmalloc(sizeof(*s_cmd) + max(u_cmd.outsize, u_cmd.insize),
183 			GFP_KERNEL);
184 	if (!s_cmd)
185 		return -ENOMEM;
186 
187 	if (copy_from_user(s_cmd, arg, sizeof(*s_cmd) + u_cmd.outsize)) {
188 		ret = -EFAULT;
189 		goto exit;
190 	}
191 
192 	if (u_cmd.outsize != s_cmd->outsize ||
193 	    u_cmd.insize != s_cmd->insize) {
194 		ret = -EINVAL;
195 		goto exit;
196 	}
197 
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. */
201 	if (ret < 0)
202 		goto exit;
203 
204 	if (copy_to_user(arg, s_cmd, sizeof(*s_cmd) + s_cmd->insize))
205 		ret = -EFAULT;
206 exit:
207 	kfree(s_cmd);
208 	return ret;
209 }
210 
211 static long ec_device_ioctl_readmem(struct cros_ec_dev *ec, void __user *arg)
212 {
213 	struct cros_ec_device *ec_dev = ec->ec_dev;
214 	struct cros_ec_readmem s_mem = { };
215 	long num;
216 
217 	/* Not every platform supports direct reads */
218 	if (!ec_dev->cmd_readmem)
219 		return -ENOTTY;
220 
221 	if (copy_from_user(&s_mem, arg, sizeof(s_mem)))
222 		return -EFAULT;
223 
224 	num = ec_dev->cmd_readmem(ec_dev, s_mem.offset, s_mem.bytes,
225 				  s_mem.buffer);
226 	if (num <= 0)
227 		return num;
228 
229 	if (copy_to_user((void __user *)arg, &s_mem, sizeof(s_mem)))
230 		return -EFAULT;
231 
232 	return 0;
233 }
234 
235 static long ec_device_ioctl(struct file *filp, unsigned int cmd,
236 			    unsigned long arg)
237 {
238 	struct cros_ec_dev *ec = filp->private_data;
239 
240 	if (_IOC_TYPE(cmd) != CROS_EC_DEV_IOC)
241 		return -ENOTTY;
242 
243 	switch (cmd) {
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);
248 	}
249 
250 	return -ENOTTY;
251 }
252 
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,
259 #ifdef CONFIG_COMPAT
260 	.compat_ioctl = ec_device_ioctl,
261 #endif
262 };
263 
264 static void cros_ec_class_release(struct device *dev)
265 {
266 	kfree(to_cros_ec_dev(dev));
267 }
268 
269 static void cros_ec_sensors_register(struct cros_ec_dev *ec)
270 {
271 	/*
272 	 * Issue a command to get the number of sensor reported.
273 	 * Build an array of sensors driver and register them all.
274 	 */
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;
282 
283 	msg = kzalloc(sizeof(struct cros_ec_command) +
284 		      max(sizeof(*params), sizeof(*resp)), GFP_KERNEL);
285 	if (msg == NULL)
286 		return;
287 
288 	msg->version = 2;
289 	msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset;
290 	msg->outsize = sizeof(*params);
291 	msg->insize = sizeof(*resp);
292 
293 	params = (struct ec_params_motion_sense *)msg->data;
294 	params->cmd = MOTIONSENSE_CMD_DUMP;
295 
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",
299 			 ret, msg->result);
300 		goto error;
301 	}
302 
303 	resp = (struct ec_response_motion_sense *)msg->data;
304 	sensor_num = resp->dump.sensor_count;
305 	/* Allocate 1 extra sensors in FIFO are needed */
306 	sensor_cells = kcalloc(sensor_num + 1, sizeof(struct mfd_cell),
307 			       GFP_KERNEL);
308 	if (sensor_cells == NULL)
309 		goto error;
310 
311 	sensor_platforms = kcalloc(sensor_num + 1,
312 				   sizeof(struct cros_ec_sensor_platform),
313 				   GFP_KERNEL);
314 	if (sensor_platforms == NULL)
315 		goto error_platforms;
316 
317 	memset(sensor_type, 0, sizeof(sensor_type));
318 	id = 0;
319 	for (i = 0; i < sensor_num; i++) {
320 		params->cmd = MOTIONSENSE_CMD_INFO;
321 		params->info.sensor_num = i;
322 		ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
323 		if (ret < 0 || msg->result != EC_RES_SUCCESS) {
324 			dev_warn(ec->dev, "no info for EC sensor %d : %d/%d\n",
325 				 i, ret, msg->result);
326 			continue;
327 		}
328 		switch (resp->info.type) {
329 		case MOTIONSENSE_TYPE_ACCEL:
330 			sensor_cells[id].name = "cros-ec-accel";
331 			break;
332 		case MOTIONSENSE_TYPE_BARO:
333 			sensor_cells[id].name = "cros-ec-baro";
334 			break;
335 		case MOTIONSENSE_TYPE_GYRO:
336 			sensor_cells[id].name = "cros-ec-gyro";
337 			break;
338 		case MOTIONSENSE_TYPE_MAG:
339 			sensor_cells[id].name = "cros-ec-mag";
340 			break;
341 		case MOTIONSENSE_TYPE_PROX:
342 			sensor_cells[id].name = "cros-ec-prox";
343 			break;
344 		case MOTIONSENSE_TYPE_LIGHT:
345 			sensor_cells[id].name = "cros-ec-light";
346 			break;
347 		case MOTIONSENSE_TYPE_ACTIVITY:
348 			sensor_cells[id].name = "cros-ec-activity";
349 			break;
350 		default:
351 			dev_warn(ec->dev, "unknown type %d\n", resp->info.type);
352 			continue;
353 		}
354 		sensor_platforms[id].sensor_num = i;
355 		sensor_cells[id].id = sensor_type[resp->info.type];
356 		sensor_cells[id].platform_data = &sensor_platforms[id];
357 		sensor_cells[id].pdata_size =
358 			sizeof(struct cros_ec_sensor_platform);
359 
360 		sensor_type[resp->info.type]++;
361 		id++;
362 	}
363 
364 	if (sensor_type[MOTIONSENSE_TYPE_ACCEL] >= 2)
365 		ec->has_kb_wake_angle = true;
366 
367 	if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE_FIFO)) {
368 		sensor_cells[id].name = "cros-ec-ring";
369 		id++;
370 	}
371 
372 	ret = mfd_add_devices(ec->dev, 0, sensor_cells, id,
373 			      NULL, 0, NULL);
374 	if (ret)
375 		dev_err(ec->dev, "failed to add EC sensors\n");
376 
377 	kfree(sensor_platforms);
378 error_platforms:
379 	kfree(sensor_cells);
380 error:
381 	kfree(msg);
382 }
383 
384 static const struct mfd_cell cros_ec_cec_cells[] = {
385 	{ .name = "cros-ec-cec" }
386 };
387 
388 static const struct mfd_cell cros_ec_rtc_cells[] = {
389 	{ .name = "cros-ec-rtc" }
390 };
391 
392 static const struct mfd_cell cros_usbpd_charger_cells[] = {
393 	{ .name = "cros-usbpd-charger" }
394 };
395 
396 static const struct mfd_cell cros_ec_platform_cells[] = {
397 	{ .name = "cros-ec-debugfs" },
398 	{ .name = "cros-ec-lightbar" },
399 	{ .name = "cros-ec-vbc" },
400 };
401 
402 static int ec_device_probe(struct platform_device *pdev)
403 {
404 	int retval = -ENOMEM;
405 	struct device *dev = &pdev->dev;
406 	struct cros_ec_platform *ec_platform = dev_get_platdata(dev);
407 	struct cros_ec_dev *ec = kzalloc(sizeof(*ec), GFP_KERNEL);
408 
409 	if (!ec)
410 		return retval;
411 
412 	dev_set_drvdata(dev, ec);
413 	ec->ec_dev = dev_get_drvdata(dev->parent);
414 	ec->dev = dev;
415 	ec->cmd_offset = ec_platform->cmd_offset;
416 	ec->features[0] = -1U; /* Not cached yet */
417 	ec->features[1] = -1U; /* Not cached yet */
418 	device_initialize(&ec->class_dev);
419 	cdev_init(&ec->cdev, &fops);
420 
421 	/*
422 	 * Add the class device
423 	 * Link to the character device for creating the /dev entry
424 	 * in devtmpfs.
425 	 */
426 	ec->class_dev.devt = MKDEV(ec_major, pdev->id);
427 	ec->class_dev.class = &cros_class;
428 	ec->class_dev.parent = dev;
429 	ec->class_dev.release = cros_ec_class_release;
430 
431 	retval = dev_set_name(&ec->class_dev, "%s", ec_platform->ec_name);
432 	if (retval) {
433 		dev_err(dev, "dev_set_name failed => %d\n", retval);
434 		goto failed;
435 	}
436 
437 	/* check whether this EC is a sensor hub. */
438 	if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE))
439 		cros_ec_sensors_register(ec);
440 
441 	/* Check whether this EC instance has CEC host command support */
442 	if (cros_ec_check_features(ec, EC_FEATURE_CEC)) {
443 		retval = mfd_add_devices(ec->dev, PLATFORM_DEVID_AUTO,
444 					 cros_ec_cec_cells,
445 					 ARRAY_SIZE(cros_ec_cec_cells),
446 					 NULL, 0, NULL);
447 		if (retval)
448 			dev_err(ec->dev,
449 				"failed to add cros-ec-cec device: %d\n",
450 				retval);
451 	}
452 
453 	/* Check whether this EC instance has RTC host command support */
454 	if (cros_ec_check_features(ec, EC_FEATURE_RTC)) {
455 		retval = mfd_add_devices(ec->dev, PLATFORM_DEVID_AUTO,
456 					 cros_ec_rtc_cells,
457 					 ARRAY_SIZE(cros_ec_rtc_cells),
458 					 NULL, 0, NULL);
459 		if (retval)
460 			dev_err(ec->dev,
461 				"failed to add cros-ec-rtc device: %d\n",
462 				retval);
463 	}
464 
465 	/* Check whether this EC instance has the PD charge manager */
466 	if (cros_ec_check_features(ec, EC_FEATURE_USB_PD)) {
467 		retval = mfd_add_devices(ec->dev, PLATFORM_DEVID_AUTO,
468 					 cros_usbpd_charger_cells,
469 					 ARRAY_SIZE(cros_usbpd_charger_cells),
470 					 NULL, 0, NULL);
471 		if (retval)
472 			dev_err(ec->dev,
473 				"failed to add cros-usbpd-charger device: %d\n",
474 				retval);
475 	}
476 
477 	/* We can now add the sysfs class, we know which parameter to show */
478 	retval = cdev_device_add(&ec->cdev, &ec->class_dev);
479 	if (retval) {
480 		dev_err(dev, "cdev_device_add failed => %d\n", retval);
481 		goto failed;
482 	}
483 
484 	retval = mfd_add_devices(ec->dev, PLATFORM_DEVID_AUTO,
485 				 cros_ec_platform_cells,
486 				 ARRAY_SIZE(cros_ec_platform_cells),
487 				 NULL, 0, NULL);
488 	if (retval)
489 		dev_warn(ec->dev,
490 			 "failed to add cros-ec platform devices: %d\n",
491 			 retval);
492 
493 	return 0;
494 
495 failed:
496 	put_device(&ec->class_dev);
497 	return retval;
498 }
499 
500 static int ec_device_remove(struct platform_device *pdev)
501 {
502 	struct cros_ec_dev *ec = dev_get_drvdata(&pdev->dev);
503 
504 	mfd_remove_devices(ec->dev);
505 	cdev_del(&ec->cdev);
506 	device_unregister(&ec->class_dev);
507 	return 0;
508 }
509 
510 static const struct platform_device_id cros_ec_id[] = {
511 	{ DRV_NAME, 0 },
512 	{ /* sentinel */ }
513 };
514 MODULE_DEVICE_TABLE(platform, cros_ec_id);
515 
516 static struct platform_driver cros_ec_dev_driver = {
517 	.driver = {
518 		.name = DRV_NAME,
519 	},
520 	.id_table = cros_ec_id,
521 	.probe = ec_device_probe,
522 	.remove = ec_device_remove,
523 };
524 
525 static int __init cros_ec_dev_init(void)
526 {
527 	int ret;
528 	dev_t dev = 0;
529 
530 	ret  = class_register(&cros_class);
531 	if (ret) {
532 		pr_err(CROS_EC_DEV_NAME ": failed to register device class\n");
533 		return ret;
534 	}
535 
536 	/* Get a range of minor numbers (starting with 0) to work with */
537 	ret = alloc_chrdev_region(&dev, 0, CROS_MAX_DEV, CROS_EC_DEV_NAME);
538 	if (ret < 0) {
539 		pr_err(CROS_EC_DEV_NAME ": alloc_chrdev_region() failed\n");
540 		goto failed_chrdevreg;
541 	}
542 	ec_major = MAJOR(dev);
543 
544 	/* Register the driver */
545 	ret = platform_driver_register(&cros_ec_dev_driver);
546 	if (ret < 0) {
547 		pr_warn(CROS_EC_DEV_NAME ": can't register driver: %d\n", ret);
548 		goto failed_devreg;
549 	}
550 	return 0;
551 
552 failed_devreg:
553 	unregister_chrdev_region(MKDEV(ec_major, 0), CROS_MAX_DEV);
554 failed_chrdevreg:
555 	class_unregister(&cros_class);
556 	return ret;
557 }
558 
559 static void __exit cros_ec_dev_exit(void)
560 {
561 	platform_driver_unregister(&cros_ec_dev_driver);
562 	unregister_chrdev(ec_major, CROS_EC_DEV_NAME);
563 	class_unregister(&cros_class);
564 }
565 
566 module_init(cros_ec_dev_init);
567 module_exit(cros_ec_dev_exit);
568 
569 MODULE_ALIAS("platform:" DRV_NAME);
570 MODULE_AUTHOR("Bill Richardson <wfrichar@chromium.org>");
571 MODULE_DESCRIPTION("Userspace interface to the Chrome OS Embedded Controller");
572 MODULE_VERSION("1.0");
573 MODULE_LICENSE("GPL");
574