xref: /linux/drivers/mfd/cros_ec_dev.c (revision acb9900f9e8074858738f48bee9a705138961258)
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-lightbar" },
398 	{ .name = "cros-ec-vbc" },
399 };
400 
401 static int ec_device_probe(struct platform_device *pdev)
402 {
403 	int retval = -ENOMEM;
404 	struct device *dev = &pdev->dev;
405 	struct cros_ec_platform *ec_platform = dev_get_platdata(dev);
406 	struct cros_ec_dev *ec = kzalloc(sizeof(*ec), GFP_KERNEL);
407 
408 	if (!ec)
409 		return retval;
410 
411 	dev_set_drvdata(dev, ec);
412 	ec->ec_dev = dev_get_drvdata(dev->parent);
413 	ec->dev = dev;
414 	ec->cmd_offset = ec_platform->cmd_offset;
415 	ec->features[0] = -1U; /* Not cached yet */
416 	ec->features[1] = -1U; /* Not cached yet */
417 	device_initialize(&ec->class_dev);
418 	cdev_init(&ec->cdev, &fops);
419 
420 	/*
421 	 * Add the class device
422 	 * Link to the character device for creating the /dev entry
423 	 * in devtmpfs.
424 	 */
425 	ec->class_dev.devt = MKDEV(ec_major, pdev->id);
426 	ec->class_dev.class = &cros_class;
427 	ec->class_dev.parent = dev;
428 	ec->class_dev.release = cros_ec_class_release;
429 
430 	retval = dev_set_name(&ec->class_dev, "%s", ec_platform->ec_name);
431 	if (retval) {
432 		dev_err(dev, "dev_set_name failed => %d\n", retval);
433 		goto failed;
434 	}
435 
436 	/* check whether this EC is a sensor hub. */
437 	if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE))
438 		cros_ec_sensors_register(ec);
439 
440 	/* Check whether this EC instance has CEC host command support */
441 	if (cros_ec_check_features(ec, EC_FEATURE_CEC)) {
442 		retval = mfd_add_devices(ec->dev, PLATFORM_DEVID_AUTO,
443 					 cros_ec_cec_cells,
444 					 ARRAY_SIZE(cros_ec_cec_cells),
445 					 NULL, 0, NULL);
446 		if (retval)
447 			dev_err(ec->dev,
448 				"failed to add cros-ec-cec device: %d\n",
449 				retval);
450 	}
451 
452 	/* Check whether this EC instance has RTC host command support */
453 	if (cros_ec_check_features(ec, EC_FEATURE_RTC)) {
454 		retval = mfd_add_devices(ec->dev, PLATFORM_DEVID_AUTO,
455 					 cros_ec_rtc_cells,
456 					 ARRAY_SIZE(cros_ec_rtc_cells),
457 					 NULL, 0, NULL);
458 		if (retval)
459 			dev_err(ec->dev,
460 				"failed to add cros-ec-rtc device: %d\n",
461 				retval);
462 	}
463 
464 	/* Check whether this EC instance has the PD charge manager */
465 	if (cros_ec_check_features(ec, EC_FEATURE_USB_PD)) {
466 		retval = mfd_add_devices(ec->dev, PLATFORM_DEVID_AUTO,
467 					 cros_usbpd_charger_cells,
468 					 ARRAY_SIZE(cros_usbpd_charger_cells),
469 					 NULL, 0, NULL);
470 		if (retval)
471 			dev_err(ec->dev,
472 				"failed to add cros-usbpd-charger device: %d\n",
473 				retval);
474 	}
475 
476 	/* We can now add the sysfs class, we know which parameter to show */
477 	retval = cdev_device_add(&ec->cdev, &ec->class_dev);
478 	if (retval) {
479 		dev_err(dev, "cdev_device_add failed => %d\n", retval);
480 		goto failed;
481 	}
482 
483 	retval = mfd_add_devices(ec->dev, PLATFORM_DEVID_AUTO,
484 				 cros_ec_platform_cells,
485 				 ARRAY_SIZE(cros_ec_platform_cells),
486 				 NULL, 0, NULL);
487 	if (retval)
488 		dev_warn(ec->dev,
489 			 "failed to add cros-ec platform devices: %d\n",
490 			 retval);
491 
492 	if (cros_ec_debugfs_init(ec))
493 		dev_warn(dev, "failed to create debugfs directory\n");
494 
495 	return 0;
496 
497 failed:
498 	put_device(&ec->class_dev);
499 	return retval;
500 }
501 
502 static int ec_device_remove(struct platform_device *pdev)
503 {
504 	struct cros_ec_dev *ec = dev_get_drvdata(&pdev->dev);
505 
506 	cros_ec_debugfs_remove(ec);
507 
508 	mfd_remove_devices(ec->dev);
509 	cdev_del(&ec->cdev);
510 	device_unregister(&ec->class_dev);
511 	return 0;
512 }
513 
514 static void ec_device_shutdown(struct platform_device *pdev)
515 {
516 	struct cros_ec_dev *ec = dev_get_drvdata(&pdev->dev);
517 
518 	/* Be sure to clear up debugfs delayed works */
519 	cros_ec_debugfs_remove(ec);
520 }
521 
522 static const struct platform_device_id cros_ec_id[] = {
523 	{ DRV_NAME, 0 },
524 	{ /* sentinel */ }
525 };
526 MODULE_DEVICE_TABLE(platform, cros_ec_id);
527 
528 static __maybe_unused int ec_device_suspend(struct device *dev)
529 {
530 	struct cros_ec_dev *ec = dev_get_drvdata(dev);
531 
532 	cros_ec_debugfs_suspend(ec);
533 
534 	return 0;
535 }
536 
537 static __maybe_unused int ec_device_resume(struct device *dev)
538 {
539 	struct cros_ec_dev *ec = dev_get_drvdata(dev);
540 
541 	cros_ec_debugfs_resume(ec);
542 
543 	return 0;
544 }
545 
546 static const struct dev_pm_ops cros_ec_dev_pm_ops = {
547 #ifdef CONFIG_PM_SLEEP
548 	.suspend = ec_device_suspend,
549 	.resume = ec_device_resume,
550 #endif
551 };
552 
553 static struct platform_driver cros_ec_dev_driver = {
554 	.driver = {
555 		.name = DRV_NAME,
556 		.pm = &cros_ec_dev_pm_ops,
557 	},
558 	.id_table = cros_ec_id,
559 	.probe = ec_device_probe,
560 	.remove = ec_device_remove,
561 	.shutdown = ec_device_shutdown,
562 };
563 
564 static int __init cros_ec_dev_init(void)
565 {
566 	int ret;
567 	dev_t dev = 0;
568 
569 	ret  = class_register(&cros_class);
570 	if (ret) {
571 		pr_err(CROS_EC_DEV_NAME ": failed to register device class\n");
572 		return ret;
573 	}
574 
575 	/* Get a range of minor numbers (starting with 0) to work with */
576 	ret = alloc_chrdev_region(&dev, 0, CROS_MAX_DEV, CROS_EC_DEV_NAME);
577 	if (ret < 0) {
578 		pr_err(CROS_EC_DEV_NAME ": alloc_chrdev_region() failed\n");
579 		goto failed_chrdevreg;
580 	}
581 	ec_major = MAJOR(dev);
582 
583 	/* Register the driver */
584 	ret = platform_driver_register(&cros_ec_dev_driver);
585 	if (ret < 0) {
586 		pr_warn(CROS_EC_DEV_NAME ": can't register driver: %d\n", ret);
587 		goto failed_devreg;
588 	}
589 	return 0;
590 
591 failed_devreg:
592 	unregister_chrdev_region(MKDEV(ec_major, 0), CROS_MAX_DEV);
593 failed_chrdevreg:
594 	class_unregister(&cros_class);
595 	return ret;
596 }
597 
598 static void __exit cros_ec_dev_exit(void)
599 {
600 	platform_driver_unregister(&cros_ec_dev_driver);
601 	unregister_chrdev(ec_major, CROS_EC_DEV_NAME);
602 	class_unregister(&cros_class);
603 }
604 
605 module_init(cros_ec_dev_init);
606 module_exit(cros_ec_dev_exit);
607 
608 MODULE_ALIAS("platform:" DRV_NAME);
609 MODULE_AUTHOR("Bill Richardson <wfrichar@chromium.org>");
610 MODULE_DESCRIPTION("Userspace interface to the Chrome OS Embedded Controller");
611 MODULE_VERSION("1.0");
612 MODULE_LICENSE("GPL");
613