xref: /linux/drivers/acpi/ec_sys.c (revision f4ae40a6a50a98ac23d4b285f739455e926a473e)
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 int acpi_ec_open_io(struct inode *i, struct file *f)
319827886dSThomas Renninger {
329827886dSThomas Renninger 	f->private_data = i->i_private;
339827886dSThomas Renninger 	return 0;
349827886dSThomas Renninger }
359827886dSThomas Renninger 
369827886dSThomas Renninger static ssize_t acpi_ec_read_io(struct file *f, char __user *buf,
379827886dSThomas Renninger 			       size_t count, loff_t *off)
389827886dSThomas Renninger {
399827886dSThomas Renninger 	/* Use this if support reading/writing multiple ECs exists in ec.c:
409827886dSThomas Renninger 	 * struct acpi_ec *ec = ((struct seq_file *)f->private_data)->private;
419827886dSThomas Renninger 	 */
429827886dSThomas Renninger 	unsigned int size = EC_SPACE_SIZE;
439827886dSThomas Renninger 	u8 *data = (u8 *) buf;
449827886dSThomas Renninger 	loff_t init_off = *off;
459827886dSThomas Renninger 	int err = 0;
469827886dSThomas Renninger 
479827886dSThomas Renninger 	if (*off >= size)
489827886dSThomas Renninger 		return 0;
499827886dSThomas Renninger 	if (*off + count >= size) {
509827886dSThomas Renninger 		size -= *off;
519827886dSThomas Renninger 		count = size;
529827886dSThomas Renninger 	} else
539827886dSThomas Renninger 		size = count;
549827886dSThomas Renninger 
559827886dSThomas Renninger 	while (size) {
569827886dSThomas Renninger 		err = ec_read(*off, &data[*off - init_off]);
579827886dSThomas Renninger 		if (err)
589827886dSThomas Renninger 			return err;
599827886dSThomas Renninger 		*off += 1;
609827886dSThomas Renninger 		size--;
619827886dSThomas Renninger 	}
629827886dSThomas Renninger 	return count;
639827886dSThomas Renninger }
649827886dSThomas Renninger 
659827886dSThomas Renninger static ssize_t acpi_ec_write_io(struct file *f, const char __user *buf,
669827886dSThomas Renninger 				size_t count, loff_t *off)
679827886dSThomas Renninger {
689827886dSThomas Renninger 	/* Use this if support reading/writing multiple ECs exists in ec.c:
699827886dSThomas Renninger 	 * struct acpi_ec *ec = ((struct seq_file *)f->private_data)->private;
709827886dSThomas Renninger 	 */
719827886dSThomas Renninger 
729827886dSThomas Renninger 	unsigned int size = count;
739827886dSThomas Renninger 	loff_t init_off = *off;
749827886dSThomas Renninger 	u8 *data = (u8 *) buf;
759827886dSThomas Renninger 	int err = 0;
769827886dSThomas Renninger 
779827886dSThomas Renninger 	if (*off >= EC_SPACE_SIZE)
789827886dSThomas Renninger 		return 0;
799827886dSThomas Renninger 	if (*off + count >= EC_SPACE_SIZE) {
809827886dSThomas Renninger 		size = EC_SPACE_SIZE - *off;
819827886dSThomas Renninger 		count = size;
829827886dSThomas Renninger 	}
839827886dSThomas Renninger 
849827886dSThomas Renninger 	while (size) {
859827886dSThomas Renninger 		u8 byte_write = data[*off - init_off];
869827886dSThomas Renninger 		err = ec_write(*off, byte_write);
879827886dSThomas Renninger 		if (err)
889827886dSThomas Renninger 			return err;
899827886dSThomas Renninger 
909827886dSThomas Renninger 		*off += 1;
919827886dSThomas Renninger 		size--;
929827886dSThomas Renninger 	}
939827886dSThomas Renninger 	return count;
949827886dSThomas Renninger }
959827886dSThomas Renninger 
969c8b04beSVasiliy Kulikov static const struct file_operations acpi_ec_io_ops = {
979827886dSThomas Renninger 	.owner = THIS_MODULE,
989827886dSThomas Renninger 	.open  = acpi_ec_open_io,
999827886dSThomas Renninger 	.read  = acpi_ec_read_io,
1009827886dSThomas Renninger 	.write = acpi_ec_write_io,
1016038f373SArnd Bergmann 	.llseek = default_llseek,
1029827886dSThomas Renninger };
1039827886dSThomas Renninger 
1041195a098SThomas Renninger int acpi_ec_add_debugfs(struct acpi_ec *ec, unsigned int ec_device_count)
1051195a098SThomas Renninger {
1061195a098SThomas Renninger 	struct dentry *dev_dir;
1071195a098SThomas Renninger 	char name[64];
108*f4ae40a6SAl Viro 	umode_t mode = 0400;
109500de3ddSThomas Renninger 
1101195a098SThomas Renninger 	if (ec_device_count == 0) {
1111195a098SThomas Renninger 		acpi_ec_debugfs_dir = debugfs_create_dir("ec", NULL);
1121195a098SThomas Renninger 		if (!acpi_ec_debugfs_dir)
1131195a098SThomas Renninger 			return -ENOMEM;
1141195a098SThomas Renninger 	}
1151195a098SThomas Renninger 
1161195a098SThomas Renninger 	sprintf(name, "ec%u", ec_device_count);
1171195a098SThomas Renninger 	dev_dir = debugfs_create_dir(name, acpi_ec_debugfs_dir);
1181195a098SThomas Renninger 	if (!dev_dir) {
119500de3ddSThomas Renninger 		if (ec_device_count != 0)
120500de3ddSThomas Renninger 			goto error;
1211195a098SThomas Renninger 		return -ENOMEM;
1221195a098SThomas Renninger 	}
1231195a098SThomas Renninger 
124500de3ddSThomas Renninger 	if (!debugfs_create_x32("gpe", 0444, dev_dir, (u32 *)&first_ec->gpe))
125500de3ddSThomas Renninger 		goto error;
126500de3ddSThomas Renninger 	if (!debugfs_create_bool("use_global_lock", 0444, dev_dir,
127500de3ddSThomas Renninger 				 (u32 *)&first_ec->global_lock))
128500de3ddSThomas Renninger 		goto error;
129500de3ddSThomas Renninger 
130500de3ddSThomas Renninger 	if (write_support)
131500de3ddSThomas Renninger 		mode = 0600;
132500de3ddSThomas Renninger 	if (!debugfs_create_file("io", mode, dev_dir, ec, &acpi_ec_io_ops))
133500de3ddSThomas Renninger 		goto error;
134500de3ddSThomas Renninger 
1351195a098SThomas Renninger 	return 0;
136500de3ddSThomas Renninger 
137500de3ddSThomas Renninger error:
138500de3ddSThomas Renninger 	debugfs_remove_recursive(acpi_ec_debugfs_dir);
139500de3ddSThomas Renninger 	return -ENOMEM;
1401195a098SThomas Renninger }
1411195a098SThomas Renninger 
1421195a098SThomas Renninger static int __init acpi_ec_sys_init(void)
1431195a098SThomas Renninger {
1441195a098SThomas Renninger 	int err = 0;
1451195a098SThomas Renninger 	if (first_ec)
1461195a098SThomas Renninger 		err = acpi_ec_add_debugfs(first_ec, 0);
1471195a098SThomas Renninger 	else
1481195a098SThomas Renninger 		err = -ENODEV;
1491195a098SThomas Renninger 	return err;
1501195a098SThomas Renninger }
1511195a098SThomas Renninger 
1521195a098SThomas Renninger static void __exit acpi_ec_sys_exit(void)
1531195a098SThomas Renninger {
1541195a098SThomas Renninger 	debugfs_remove_recursive(acpi_ec_debugfs_dir);
1551195a098SThomas Renninger }
1561195a098SThomas Renninger 
1571195a098SThomas Renninger module_init(acpi_ec_sys_init);
1581195a098SThomas Renninger module_exit(acpi_ec_sys_exit);
159