xref: /linux/drivers/acpi/ec_sys.c (revision 9c8b04be443b33939f374a811c82abeebe0a61d1)
19827886dSThomas Renninger /*
29827886dSThomas Renninger  * ec_sys.c
39827886dSThomas Renninger  *
49827886dSThomas Renninger  * Copyright (C) 2010 SUSE Products GmbH/Novell
59827886dSThomas Renninger  * Author:
69827886dSThomas Renninger  *      Thomas Renninger <trenn@suse.de>
79827886dSThomas Renninger  *
89827886dSThomas Renninger  * This work is licensed under the terms of the GNU GPL, version 2.
99827886dSThomas Renninger  */
109827886dSThomas Renninger 
111195a098SThomas Renninger #include <linux/kernel.h>
121195a098SThomas Renninger #include <linux/acpi.h>
131195a098SThomas Renninger #include <linux/debugfs.h>
141195a098SThomas Renninger #include "internal.h"
151195a098SThomas Renninger 
161195a098SThomas Renninger MODULE_AUTHOR("Thomas Renninger <trenn@suse.de>");
171195a098SThomas Renninger MODULE_DESCRIPTION("ACPI EC sysfs access driver");
181195a098SThomas Renninger MODULE_LICENSE("GPL");
191195a098SThomas Renninger 
20500de3ddSThomas Renninger static bool write_support;
21500de3ddSThomas Renninger module_param(write_support, bool, 0644);
22500de3ddSThomas Renninger MODULE_PARM_DESC(write_support, "Dangerous, reboot and removal of battery may "
23500de3ddSThomas Renninger 		 "be needed.");
24500de3ddSThomas Renninger 
259827886dSThomas Renninger #define EC_SPACE_SIZE 256
269827886dSThomas Renninger 
271195a098SThomas Renninger static struct dentry *acpi_ec_debugfs_dir;
281195a098SThomas Renninger 
299827886dSThomas Renninger static int acpi_ec_open_io(struct inode *i, struct file *f)
309827886dSThomas Renninger {
319827886dSThomas Renninger 	f->private_data = i->i_private;
329827886dSThomas Renninger 	return 0;
339827886dSThomas Renninger }
349827886dSThomas Renninger 
359827886dSThomas Renninger static ssize_t acpi_ec_read_io(struct file *f, char __user *buf,
369827886dSThomas Renninger 			       size_t count, loff_t *off)
379827886dSThomas Renninger {
389827886dSThomas Renninger 	/* Use this if support reading/writing multiple ECs exists in ec.c:
399827886dSThomas Renninger 	 * struct acpi_ec *ec = ((struct seq_file *)f->private_data)->private;
409827886dSThomas Renninger 	 */
419827886dSThomas Renninger 	unsigned int size = EC_SPACE_SIZE;
429827886dSThomas Renninger 	u8 *data = (u8 *) buf;
439827886dSThomas Renninger 	loff_t init_off = *off;
449827886dSThomas Renninger 	int err = 0;
459827886dSThomas Renninger 
469827886dSThomas Renninger 	if (*off >= size)
479827886dSThomas Renninger 		return 0;
489827886dSThomas Renninger 	if (*off + count >= size) {
499827886dSThomas Renninger 		size -= *off;
509827886dSThomas Renninger 		count = size;
519827886dSThomas Renninger 	} else
529827886dSThomas Renninger 		size = count;
539827886dSThomas Renninger 
549827886dSThomas Renninger 	while (size) {
559827886dSThomas Renninger 		err = ec_read(*off, &data[*off - init_off]);
569827886dSThomas Renninger 		if (err)
579827886dSThomas Renninger 			return err;
589827886dSThomas Renninger 		*off += 1;
599827886dSThomas Renninger 		size--;
609827886dSThomas Renninger 	}
619827886dSThomas Renninger 	return count;
629827886dSThomas Renninger }
639827886dSThomas Renninger 
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 	u8 *data = (u8 *) buf;
749827886dSThomas Renninger 	int err = 0;
759827886dSThomas Renninger 
769827886dSThomas Renninger 	if (*off >= EC_SPACE_SIZE)
779827886dSThomas Renninger 		return 0;
789827886dSThomas Renninger 	if (*off + count >= EC_SPACE_SIZE) {
799827886dSThomas Renninger 		size = EC_SPACE_SIZE - *off;
809827886dSThomas Renninger 		count = size;
819827886dSThomas Renninger 	}
829827886dSThomas Renninger 
839827886dSThomas Renninger 	while (size) {
849827886dSThomas Renninger 		u8 byte_write = data[*off - init_off];
859827886dSThomas Renninger 		err = ec_write(*off, byte_write);
869827886dSThomas Renninger 		if (err)
879827886dSThomas Renninger 			return err;
889827886dSThomas Renninger 
899827886dSThomas Renninger 		*off += 1;
909827886dSThomas Renninger 		size--;
919827886dSThomas Renninger 	}
929827886dSThomas Renninger 	return count;
939827886dSThomas Renninger }
949827886dSThomas Renninger 
95*9c8b04beSVasiliy Kulikov static const struct file_operations acpi_ec_io_ops = {
969827886dSThomas Renninger 	.owner = THIS_MODULE,
979827886dSThomas Renninger 	.open  = acpi_ec_open_io,
989827886dSThomas Renninger 	.read  = acpi_ec_read_io,
999827886dSThomas Renninger 	.write = acpi_ec_write_io,
1006038f373SArnd Bergmann 	.llseek = default_llseek,
1019827886dSThomas Renninger };
1029827886dSThomas Renninger 
1031195a098SThomas Renninger int acpi_ec_add_debugfs(struct acpi_ec *ec, unsigned int ec_device_count)
1041195a098SThomas Renninger {
1051195a098SThomas Renninger 	struct dentry *dev_dir;
1061195a098SThomas Renninger 	char name[64];
107500de3ddSThomas Renninger 	mode_t mode = 0400;
108500de3ddSThomas Renninger 
1091195a098SThomas Renninger 	if (ec_device_count == 0) {
1101195a098SThomas Renninger 		acpi_ec_debugfs_dir = debugfs_create_dir("ec", NULL);
1111195a098SThomas Renninger 		if (!acpi_ec_debugfs_dir)
1121195a098SThomas Renninger 			return -ENOMEM;
1131195a098SThomas Renninger 	}
1141195a098SThomas Renninger 
1151195a098SThomas Renninger 	sprintf(name, "ec%u", ec_device_count);
1161195a098SThomas Renninger 	dev_dir = debugfs_create_dir(name, acpi_ec_debugfs_dir);
1171195a098SThomas Renninger 	if (!dev_dir) {
118500de3ddSThomas Renninger 		if (ec_device_count != 0)
119500de3ddSThomas Renninger 			goto error;
1201195a098SThomas Renninger 		return -ENOMEM;
1211195a098SThomas Renninger 	}
1221195a098SThomas Renninger 
123500de3ddSThomas Renninger 	if (!debugfs_create_x32("gpe", 0444, dev_dir, (u32 *)&first_ec->gpe))
124500de3ddSThomas Renninger 		goto error;
125500de3ddSThomas Renninger 	if (!debugfs_create_bool("use_global_lock", 0444, dev_dir,
126500de3ddSThomas Renninger 				 (u32 *)&first_ec->global_lock))
127500de3ddSThomas Renninger 		goto error;
128500de3ddSThomas Renninger 
129500de3ddSThomas Renninger 	if (write_support)
130500de3ddSThomas Renninger 		mode = 0600;
131500de3ddSThomas Renninger 	if (!debugfs_create_file("io", mode, dev_dir, ec, &acpi_ec_io_ops))
132500de3ddSThomas Renninger 		goto error;
133500de3ddSThomas Renninger 
1341195a098SThomas Renninger 	return 0;
135500de3ddSThomas Renninger 
136500de3ddSThomas Renninger error:
137500de3ddSThomas Renninger 	debugfs_remove_recursive(acpi_ec_debugfs_dir);
138500de3ddSThomas Renninger 	return -ENOMEM;
1391195a098SThomas Renninger }
1401195a098SThomas Renninger 
1411195a098SThomas Renninger static int __init acpi_ec_sys_init(void)
1421195a098SThomas Renninger {
1431195a098SThomas Renninger 	int err = 0;
1441195a098SThomas Renninger 	if (first_ec)
1451195a098SThomas Renninger 		err = acpi_ec_add_debugfs(first_ec, 0);
1461195a098SThomas Renninger 	else
1471195a098SThomas Renninger 		err = -ENODEV;
1481195a098SThomas Renninger 	return err;
1491195a098SThomas Renninger }
1501195a098SThomas Renninger 
1511195a098SThomas Renninger static void __exit acpi_ec_sys_exit(void)
1521195a098SThomas Renninger {
1531195a098SThomas Renninger 	debugfs_remove_recursive(acpi_ec_debugfs_dir);
1541195a098SThomas Renninger }
1551195a098SThomas Renninger 
1561195a098SThomas Renninger module_init(acpi_ec_sys_init);
1571195a098SThomas Renninger module_exit(acpi_ec_sys_exit);
158