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