xref: /linux/drivers/acpi/ec_sys.c (revision 234e340582901211f40d8c732afc49f0630ecf05)
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>
14cc4b859cSPaul Gortmaker #include <linux/module.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;
22500de3ddSThomas Renninger module_param(write_support, bool, 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 
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 	u8 *data = (u8 *) buf;
389827886dSThomas Renninger 	loff_t init_off = *off;
399827886dSThomas Renninger 	int err = 0;
409827886dSThomas Renninger 
419827886dSThomas Renninger 	if (*off >= size)
429827886dSThomas Renninger 		return 0;
439827886dSThomas Renninger 	if (*off + count >= size) {
449827886dSThomas Renninger 		size -= *off;
459827886dSThomas Renninger 		count = size;
469827886dSThomas Renninger 	} else
479827886dSThomas Renninger 		size = count;
489827886dSThomas Renninger 
499827886dSThomas Renninger 	while (size) {
509827886dSThomas Renninger 		err = ec_read(*off, &data[*off - init_off]);
519827886dSThomas Renninger 		if (err)
529827886dSThomas Renninger 			return err;
539827886dSThomas Renninger 		*off += 1;
549827886dSThomas Renninger 		size--;
559827886dSThomas Renninger 	}
569827886dSThomas Renninger 	return count;
579827886dSThomas Renninger }
589827886dSThomas Renninger 
599827886dSThomas Renninger static ssize_t acpi_ec_write_io(struct file *f, const char __user *buf,
609827886dSThomas Renninger 				size_t count, loff_t *off)
619827886dSThomas Renninger {
629827886dSThomas Renninger 	/* Use this if support reading/writing multiple ECs exists in ec.c:
639827886dSThomas Renninger 	 * struct acpi_ec *ec = ((struct seq_file *)f->private_data)->private;
649827886dSThomas Renninger 	 */
659827886dSThomas Renninger 
669827886dSThomas Renninger 	unsigned int size = count;
679827886dSThomas Renninger 	loff_t init_off = *off;
689827886dSThomas Renninger 	u8 *data = (u8 *) buf;
699827886dSThomas Renninger 	int err = 0;
709827886dSThomas Renninger 
719827886dSThomas Renninger 	if (*off >= EC_SPACE_SIZE)
729827886dSThomas Renninger 		return 0;
739827886dSThomas Renninger 	if (*off + count >= EC_SPACE_SIZE) {
749827886dSThomas Renninger 		size = EC_SPACE_SIZE - *off;
759827886dSThomas Renninger 		count = size;
769827886dSThomas Renninger 	}
779827886dSThomas Renninger 
789827886dSThomas Renninger 	while (size) {
799827886dSThomas Renninger 		u8 byte_write = data[*off - init_off];
809827886dSThomas Renninger 		err = ec_write(*off, byte_write);
819827886dSThomas Renninger 		if (err)
829827886dSThomas Renninger 			return err;
839827886dSThomas Renninger 
849827886dSThomas Renninger 		*off += 1;
859827886dSThomas Renninger 		size--;
869827886dSThomas Renninger 	}
879827886dSThomas Renninger 	return count;
889827886dSThomas Renninger }
899827886dSThomas Renninger 
909c8b04beSVasiliy Kulikov static const struct file_operations acpi_ec_io_ops = {
919827886dSThomas Renninger 	.owner = THIS_MODULE,
92*234e3405SStephen Boyd 	.open  = simple_open,
939827886dSThomas Renninger 	.read  = acpi_ec_read_io,
949827886dSThomas Renninger 	.write = acpi_ec_write_io,
956038f373SArnd Bergmann 	.llseek = default_llseek,
969827886dSThomas Renninger };
979827886dSThomas Renninger 
981195a098SThomas Renninger int acpi_ec_add_debugfs(struct acpi_ec *ec, unsigned int ec_device_count)
991195a098SThomas Renninger {
1001195a098SThomas Renninger 	struct dentry *dev_dir;
1011195a098SThomas Renninger 	char name[64];
102f4ae40a6SAl Viro 	umode_t mode = 0400;
103500de3ddSThomas Renninger 
1041195a098SThomas Renninger 	if (ec_device_count == 0) {
1051195a098SThomas Renninger 		acpi_ec_debugfs_dir = debugfs_create_dir("ec", NULL);
1061195a098SThomas Renninger 		if (!acpi_ec_debugfs_dir)
1071195a098SThomas Renninger 			return -ENOMEM;
1081195a098SThomas Renninger 	}
1091195a098SThomas Renninger 
1101195a098SThomas Renninger 	sprintf(name, "ec%u", ec_device_count);
1111195a098SThomas Renninger 	dev_dir = debugfs_create_dir(name, acpi_ec_debugfs_dir);
1121195a098SThomas Renninger 	if (!dev_dir) {
113500de3ddSThomas Renninger 		if (ec_device_count != 0)
114500de3ddSThomas Renninger 			goto error;
1151195a098SThomas Renninger 		return -ENOMEM;
1161195a098SThomas Renninger 	}
1171195a098SThomas Renninger 
118500de3ddSThomas Renninger 	if (!debugfs_create_x32("gpe", 0444, dev_dir, (u32 *)&first_ec->gpe))
119500de3ddSThomas Renninger 		goto error;
120500de3ddSThomas Renninger 	if (!debugfs_create_bool("use_global_lock", 0444, dev_dir,
121500de3ddSThomas Renninger 				 (u32 *)&first_ec->global_lock))
122500de3ddSThomas Renninger 		goto error;
123500de3ddSThomas Renninger 
124500de3ddSThomas Renninger 	if (write_support)
125500de3ddSThomas Renninger 		mode = 0600;
126500de3ddSThomas Renninger 	if (!debugfs_create_file("io", mode, dev_dir, ec, &acpi_ec_io_ops))
127500de3ddSThomas Renninger 		goto error;
128500de3ddSThomas Renninger 
1291195a098SThomas Renninger 	return 0;
130500de3ddSThomas Renninger 
131500de3ddSThomas Renninger error:
132500de3ddSThomas Renninger 	debugfs_remove_recursive(acpi_ec_debugfs_dir);
133500de3ddSThomas Renninger 	return -ENOMEM;
1341195a098SThomas Renninger }
1351195a098SThomas Renninger 
1361195a098SThomas Renninger static int __init acpi_ec_sys_init(void)
1371195a098SThomas Renninger {
1381195a098SThomas Renninger 	int err = 0;
1391195a098SThomas Renninger 	if (first_ec)
1401195a098SThomas Renninger 		err = acpi_ec_add_debugfs(first_ec, 0);
1411195a098SThomas Renninger 	else
1421195a098SThomas Renninger 		err = -ENODEV;
1431195a098SThomas Renninger 	return err;
1441195a098SThomas Renninger }
1451195a098SThomas Renninger 
1461195a098SThomas Renninger static void __exit acpi_ec_sys_exit(void)
1471195a098SThomas Renninger {
1481195a098SThomas Renninger 	debugfs_remove_recursive(acpi_ec_debugfs_dir);
1491195a098SThomas Renninger }
1501195a098SThomas Renninger 
1511195a098SThomas Renninger module_init(acpi_ec_sys_init);
1521195a098SThomas Renninger module_exit(acpi_ec_sys_exit);
153