xref: /linux/drivers/acpi/ec_sys.c (revision 03ab8e6297acd1bc0eedaa050e2a1635c576fd11)
17a338472SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
29827886dSThomas Renninger /*
39827886dSThomas Renninger  * ec_sys.c
49827886dSThomas Renninger  *
59827886dSThomas Renninger  * Copyright (C) 2010 SUSE Products GmbH/Novell
69827886dSThomas Renninger  * Author:
79827886dSThomas Renninger  *      Thomas Renninger <trenn@suse.de>
89827886dSThomas Renninger  */
99827886dSThomas Renninger 
101195a098SThomas Renninger #include <linux/kernel.h>
111195a098SThomas Renninger #include <linux/acpi.h>
121195a098SThomas Renninger #include <linux/debugfs.h>
13cc4b859cSPaul Gortmaker #include <linux/module.h>
14ecde3003SVasiliy Kulikov #include <linux/uaccess.h>
151195a098SThomas Renninger #include "internal.h"
161195a098SThomas Renninger 
171195a098SThomas Renninger MODULE_AUTHOR("Thomas Renninger <trenn@suse.de>");
181195a098SThomas Renninger MODULE_DESCRIPTION("ACPI EC sysfs access driver");
191195a098SThomas Renninger MODULE_LICENSE("GPL");
201195a098SThomas Renninger 
21500de3ddSThomas Renninger static bool write_support;
22*b66f8684SHans de Goede module_param_hw(write_support, bool, other, 0644);
23500de3ddSThomas Renninger MODULE_PARM_DESC(write_support, "Dangerous, reboot and removal of battery may "
24500de3ddSThomas Renninger 		 "be needed.");
25500de3ddSThomas Renninger 
269827886dSThomas Renninger #define EC_SPACE_SIZE 256
279827886dSThomas Renninger 
281195a098SThomas Renninger static struct dentry *acpi_ec_debugfs_dir;
291195a098SThomas Renninger 
acpi_ec_read_io(struct file * f,char __user * buf,size_t count,loff_t * off)309827886dSThomas Renninger static ssize_t acpi_ec_read_io(struct file *f, char __user *buf,
319827886dSThomas Renninger 			       size_t count, loff_t *off)
329827886dSThomas Renninger {
339827886dSThomas Renninger 	/* Use this if support reading/writing multiple ECs exists in ec.c:
349827886dSThomas Renninger 	 * struct acpi_ec *ec = ((struct seq_file *)f->private_data)->private;
359827886dSThomas Renninger 	 */
369827886dSThomas Renninger 	unsigned int size = EC_SPACE_SIZE;
379827886dSThomas Renninger 	loff_t init_off = *off;
389827886dSThomas Renninger 	int err = 0;
399827886dSThomas Renninger 
409827886dSThomas Renninger 	if (*off >= size)
419827886dSThomas Renninger 		return 0;
429827886dSThomas Renninger 	if (*off + count >= size) {
439827886dSThomas Renninger 		size -= *off;
449827886dSThomas Renninger 		count = size;
459827886dSThomas Renninger 	} else
469827886dSThomas Renninger 		size = count;
479827886dSThomas Renninger 
489827886dSThomas Renninger 	while (size) {
49ecde3003SVasiliy Kulikov 		u8 byte_read;
50ecde3003SVasiliy Kulikov 		err = ec_read(*off, &byte_read);
519827886dSThomas Renninger 		if (err)
529827886dSThomas Renninger 			return err;
53ecde3003SVasiliy Kulikov 		if (put_user(byte_read, buf + *off - init_off)) {
54ecde3003SVasiliy Kulikov 			if (*off - init_off)
55ecde3003SVasiliy Kulikov 				return *off - init_off; /* partial read */
56ecde3003SVasiliy Kulikov 			return -EFAULT;
57ecde3003SVasiliy Kulikov 		}
589827886dSThomas Renninger 		*off += 1;
599827886dSThomas Renninger 		size--;
609827886dSThomas Renninger 	}
619827886dSThomas Renninger 	return count;
629827886dSThomas Renninger }
639827886dSThomas Renninger 
acpi_ec_write_io(struct file * f,const char __user * buf,size_t count,loff_t * off)649827886dSThomas Renninger static ssize_t acpi_ec_write_io(struct file *f, const char __user *buf,
659827886dSThomas Renninger 				size_t count, loff_t *off)
669827886dSThomas Renninger {
679827886dSThomas Renninger 	/* Use this if support reading/writing multiple ECs exists in ec.c:
689827886dSThomas Renninger 	 * struct acpi_ec *ec = ((struct seq_file *)f->private_data)->private;
699827886dSThomas Renninger 	 */
709827886dSThomas Renninger 
719827886dSThomas Renninger 	unsigned int size = count;
729827886dSThomas Renninger 	loff_t init_off = *off;
739827886dSThomas Renninger 	int err = 0;
749827886dSThomas Renninger 
751b6e75eeSOleg Drokin 	if (!write_support)
761b6e75eeSOleg Drokin 		return -EINVAL;
771b6e75eeSOleg Drokin 
789827886dSThomas Renninger 	if (*off >= EC_SPACE_SIZE)
799827886dSThomas Renninger 		return 0;
809827886dSThomas Renninger 	if (*off + count >= EC_SPACE_SIZE) {
819827886dSThomas Renninger 		size = EC_SPACE_SIZE - *off;
829827886dSThomas Renninger 		count = size;
839827886dSThomas Renninger 	}
849827886dSThomas Renninger 
859827886dSThomas Renninger 	while (size) {
86ecde3003SVasiliy Kulikov 		u8 byte_write;
87ecde3003SVasiliy Kulikov 		if (get_user(byte_write, buf + *off - init_off)) {
88ecde3003SVasiliy Kulikov 			if (*off - init_off)
89ecde3003SVasiliy Kulikov 				return *off - init_off; /* partial write */
90ecde3003SVasiliy Kulikov 			return -EFAULT;
91ecde3003SVasiliy Kulikov 		}
929827886dSThomas Renninger 		err = ec_write(*off, byte_write);
939827886dSThomas Renninger 		if (err)
949827886dSThomas Renninger 			return err;
959827886dSThomas Renninger 
969827886dSThomas Renninger 		*off += 1;
979827886dSThomas Renninger 		size--;
989827886dSThomas Renninger 	}
999827886dSThomas Renninger 	return count;
1009827886dSThomas Renninger }
1019827886dSThomas Renninger 
1029c8b04beSVasiliy Kulikov static const struct file_operations acpi_ec_io_ops = {
1039827886dSThomas Renninger 	.owner = THIS_MODULE,
104234e3405SStephen Boyd 	.open  = simple_open,
1059827886dSThomas Renninger 	.read  = acpi_ec_read_io,
1069827886dSThomas Renninger 	.write = acpi_ec_write_io,
1076038f373SArnd Bergmann 	.llseek = default_llseek,
1089827886dSThomas Renninger };
1099827886dSThomas Renninger 
acpi_ec_add_debugfs(struct acpi_ec * ec,unsigned int ec_device_count)1109ec6dbfbSGreg Kroah-Hartman static void acpi_ec_add_debugfs(struct acpi_ec *ec, unsigned int ec_device_count)
1111195a098SThomas Renninger {
1121195a098SThomas Renninger 	struct dentry *dev_dir;
1131195a098SThomas Renninger 	char name[64];
114f4ae40a6SAl Viro 	umode_t mode = 0400;
115500de3ddSThomas Renninger 
1169ec6dbfbSGreg Kroah-Hartman 	if (ec_device_count == 0)
1171195a098SThomas Renninger 		acpi_ec_debugfs_dir = debugfs_create_dir("ec", NULL);
1181195a098SThomas Renninger 
1191195a098SThomas Renninger 	sprintf(name, "ec%u", ec_device_count);
1201195a098SThomas Renninger 	dev_dir = debugfs_create_dir(name, acpi_ec_debugfs_dir);
1211195a098SThomas Renninger 
1229ec6dbfbSGreg Kroah-Hartman 	debugfs_create_x32("gpe", 0444, dev_dir, &first_ec->gpe);
1239ec6dbfbSGreg Kroah-Hartman 	debugfs_create_bool("use_global_lock", 0444, dev_dir,
1249ec6dbfbSGreg Kroah-Hartman 			    &first_ec->global_lock);
125500de3ddSThomas Renninger 
126500de3ddSThomas Renninger 	if (write_support)
127500de3ddSThomas Renninger 		mode = 0600;
1289ec6dbfbSGreg Kroah-Hartman 	debugfs_create_file("io", mode, dev_dir, ec, &acpi_ec_io_ops);
1291195a098SThomas Renninger }
1301195a098SThomas Renninger 
acpi_ec_sys_init(void)1311195a098SThomas Renninger static int __init acpi_ec_sys_init(void)
1321195a098SThomas Renninger {
1331195a098SThomas Renninger 	if (first_ec)
1349ec6dbfbSGreg Kroah-Hartman 		acpi_ec_add_debugfs(first_ec, 0);
1359ec6dbfbSGreg Kroah-Hartman 	return 0;
1361195a098SThomas Renninger }
1371195a098SThomas Renninger 
acpi_ec_sys_exit(void)1381195a098SThomas Renninger static void __exit acpi_ec_sys_exit(void)
1391195a098SThomas Renninger {
1401195a098SThomas Renninger 	debugfs_remove_recursive(acpi_ec_debugfs_dir);
1411195a098SThomas Renninger }
1421195a098SThomas Renninger 
1431195a098SThomas Renninger module_init(acpi_ec_sys_init);
1441195a098SThomas Renninger module_exit(acpi_ec_sys_exit);
145