xref: /linux/drivers/acpi/ec_sys.c (revision 500de3dd46ac9f9ae9d124634c68907b7d50d2cb)
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 
20*500de3ddSThomas Renninger static bool write_support;
21*500de3ddSThomas Renninger module_param(write_support, bool, 0644);
22*500de3ddSThomas Renninger MODULE_PARM_DESC(write_support, "Dangerous, reboot and removal of battery may "
23*500de3ddSThomas Renninger 		 "be needed.");
24*500de3ddSThomas Renninger 
259827886dSThomas Renninger #define EC_SPACE_SIZE 256
269827886dSThomas Renninger 
271195a098SThomas Renninger struct sysdev_class acpi_ec_sysdev_class = {
281195a098SThomas Renninger 	.name = "ec",
291195a098SThomas Renninger };
301195a098SThomas Renninger 
311195a098SThomas Renninger static struct dentry *acpi_ec_debugfs_dir;
321195a098SThomas Renninger 
339827886dSThomas Renninger static int acpi_ec_open_io(struct inode *i, struct file *f)
349827886dSThomas Renninger {
359827886dSThomas Renninger 	f->private_data = i->i_private;
369827886dSThomas Renninger 	return 0;
379827886dSThomas Renninger }
389827886dSThomas Renninger 
399827886dSThomas Renninger static ssize_t acpi_ec_read_io(struct file *f, char __user *buf,
409827886dSThomas Renninger 			       size_t count, loff_t *off)
419827886dSThomas Renninger {
429827886dSThomas Renninger 	/* Use this if support reading/writing multiple ECs exists in ec.c:
439827886dSThomas Renninger 	 * struct acpi_ec *ec = ((struct seq_file *)f->private_data)->private;
449827886dSThomas Renninger 	 */
459827886dSThomas Renninger 	unsigned int size = EC_SPACE_SIZE;
469827886dSThomas Renninger 	u8 *data = (u8 *) buf;
479827886dSThomas Renninger 	loff_t init_off = *off;
489827886dSThomas Renninger 	int err = 0;
499827886dSThomas Renninger 
509827886dSThomas Renninger 	if (*off >= size)
519827886dSThomas Renninger 		return 0;
529827886dSThomas Renninger 	if (*off + count >= size) {
539827886dSThomas Renninger 		size -= *off;
549827886dSThomas Renninger 		count = size;
559827886dSThomas Renninger 	} else
569827886dSThomas Renninger 		size = count;
579827886dSThomas Renninger 
589827886dSThomas Renninger 	while (size) {
599827886dSThomas Renninger 		err = ec_read(*off, &data[*off - init_off]);
609827886dSThomas Renninger 		if (err)
619827886dSThomas Renninger 			return err;
629827886dSThomas Renninger 		*off += 1;
639827886dSThomas Renninger 		size--;
649827886dSThomas Renninger 	}
659827886dSThomas Renninger 	return count;
669827886dSThomas Renninger }
679827886dSThomas Renninger 
689827886dSThomas Renninger static ssize_t acpi_ec_write_io(struct file *f, const char __user *buf,
699827886dSThomas Renninger 				size_t count, loff_t *off)
709827886dSThomas Renninger {
719827886dSThomas Renninger 	/* Use this if support reading/writing multiple ECs exists in ec.c:
729827886dSThomas Renninger 	 * struct acpi_ec *ec = ((struct seq_file *)f->private_data)->private;
739827886dSThomas Renninger 	 */
749827886dSThomas Renninger 
759827886dSThomas Renninger 	unsigned int size = count;
769827886dSThomas Renninger 	loff_t init_off = *off;
779827886dSThomas Renninger 	u8 *data = (u8 *) buf;
789827886dSThomas Renninger 	int err = 0;
799827886dSThomas Renninger 
809827886dSThomas Renninger 	if (*off >= EC_SPACE_SIZE)
819827886dSThomas Renninger 		return 0;
829827886dSThomas Renninger 	if (*off + count >= EC_SPACE_SIZE) {
839827886dSThomas Renninger 		size = EC_SPACE_SIZE - *off;
849827886dSThomas Renninger 		count = size;
859827886dSThomas Renninger 	}
869827886dSThomas Renninger 
879827886dSThomas Renninger 	while (size) {
889827886dSThomas Renninger 		u8 byte_write = data[*off - init_off];
899827886dSThomas Renninger 		err = ec_write(*off, byte_write);
909827886dSThomas Renninger 		if (err)
919827886dSThomas Renninger 			return err;
929827886dSThomas Renninger 
939827886dSThomas Renninger 		*off += 1;
949827886dSThomas Renninger 		size--;
959827886dSThomas Renninger 	}
969827886dSThomas Renninger 	return count;
979827886dSThomas Renninger }
989827886dSThomas Renninger 
999827886dSThomas Renninger static struct file_operations acpi_ec_io_ops = {
1009827886dSThomas Renninger 	.owner = THIS_MODULE,
1019827886dSThomas Renninger 	.open  = acpi_ec_open_io,
1029827886dSThomas Renninger 	.read  = acpi_ec_read_io,
1039827886dSThomas Renninger 	.write = acpi_ec_write_io,
1049827886dSThomas Renninger };
1059827886dSThomas Renninger 
1061195a098SThomas Renninger int acpi_ec_add_debugfs(struct acpi_ec *ec, unsigned int ec_device_count)
1071195a098SThomas Renninger {
1081195a098SThomas Renninger 	struct dentry *dev_dir;
1091195a098SThomas Renninger 	char name[64];
110*500de3ddSThomas Renninger 	mode_t mode = 0400;
111*500de3ddSThomas Renninger 
1121195a098SThomas Renninger 	if (ec_device_count == 0) {
1131195a098SThomas Renninger 		acpi_ec_debugfs_dir = debugfs_create_dir("ec", NULL);
1141195a098SThomas Renninger 		if (!acpi_ec_debugfs_dir)
1151195a098SThomas Renninger 			return -ENOMEM;
1161195a098SThomas Renninger 	}
1171195a098SThomas Renninger 
1181195a098SThomas Renninger 	sprintf(name, "ec%u", ec_device_count);
1191195a098SThomas Renninger 	dev_dir = debugfs_create_dir(name, acpi_ec_debugfs_dir);
1201195a098SThomas Renninger 	if (!dev_dir) {
121*500de3ddSThomas Renninger 		if (ec_device_count != 0)
122*500de3ddSThomas Renninger 			goto error;
1231195a098SThomas Renninger 		return -ENOMEM;
1241195a098SThomas Renninger 	}
1251195a098SThomas Renninger 
126*500de3ddSThomas Renninger 	if (!debugfs_create_x32("gpe", 0444, dev_dir, (u32 *)&first_ec->gpe))
127*500de3ddSThomas Renninger 		goto error;
128*500de3ddSThomas Renninger 	if (!debugfs_create_bool("use_global_lock", 0444, dev_dir,
129*500de3ddSThomas Renninger 				 (u32 *)&first_ec->global_lock))
130*500de3ddSThomas Renninger 		goto error;
131*500de3ddSThomas Renninger 
132*500de3ddSThomas Renninger 	if (write_support)
133*500de3ddSThomas Renninger 		mode = 0600;
134*500de3ddSThomas Renninger 	if (!debugfs_create_file("io", mode, dev_dir, ec, &acpi_ec_io_ops))
135*500de3ddSThomas Renninger 		goto error;
136*500de3ddSThomas Renninger 
1371195a098SThomas Renninger 	return 0;
138*500de3ddSThomas Renninger 
139*500de3ddSThomas Renninger error:
140*500de3ddSThomas Renninger 	debugfs_remove_recursive(acpi_ec_debugfs_dir);
141*500de3ddSThomas Renninger 	return -ENOMEM;
1421195a098SThomas Renninger }
1431195a098SThomas Renninger 
1441195a098SThomas Renninger static int __init acpi_ec_sys_init(void)
1451195a098SThomas Renninger {
1461195a098SThomas Renninger 	int err = 0;
1471195a098SThomas Renninger 	if (first_ec)
1481195a098SThomas Renninger 		err = acpi_ec_add_debugfs(first_ec, 0);
1491195a098SThomas Renninger 	else
1501195a098SThomas Renninger 		err = -ENODEV;
1511195a098SThomas Renninger 	return err;
1521195a098SThomas Renninger }
1531195a098SThomas Renninger 
1541195a098SThomas Renninger static void __exit acpi_ec_sys_exit(void)
1551195a098SThomas Renninger {
1561195a098SThomas Renninger 	debugfs_remove_recursive(acpi_ec_debugfs_dir);
1571195a098SThomas Renninger }
1581195a098SThomas Renninger 
1591195a098SThomas Renninger module_init(acpi_ec_sys_init);
1601195a098SThomas Renninger module_exit(acpi_ec_sys_exit);
161