4 * Copyright (C) 2010 SUSE Products GmbH/Novell
6 * Thomas Renninger <trenn@suse.de>
8 * This work is licensed under the terms of the GNU GPL, version 2.
11 #include <linux/kernel.h>
12 #include <linux/acpi.h>
13 #include <linux/debugfs.h>
14 #include <linux/module.h>
15 #include <linux/uaccess.h>
18 MODULE_AUTHOR("Thomas Renninger <trenn@suse.de>");
19 MODULE_DESCRIPTION("ACPI EC sysfs access driver");
20 MODULE_LICENSE("GPL");
22 static bool write_support;
23 module_param(write_support, bool, 0644);
24 MODULE_PARM_DESC(write_support, "Dangerous, reboot and removal of battery may "
27 #define EC_SPACE_SIZE 256
29 static struct dentry *acpi_ec_debugfs_dir;
31 static int acpi_ec_open_io(struct inode *i, struct file *f)
33 f->private_data = i->i_private;
37 static ssize_t acpi_ec_read_io(struct file *f, char __user *buf,
38 size_t count, loff_t *off)
40 /* Use this if support reading/writing multiple ECs exists in ec.c:
41 * struct acpi_ec *ec = ((struct seq_file *)f->private_data)->private;
43 unsigned int size = EC_SPACE_SIZE;
44 loff_t init_off = *off;
49 if (*off + count >= size) {
57 err = ec_read(*off, &byte_read);
60 if (put_user(byte_read, buf + *off - init_off)) {
62 return *off - init_off; /* partial read */
71 static ssize_t acpi_ec_write_io(struct file *f, const char __user *buf,
72 size_t count, loff_t *off)
74 /* Use this if support reading/writing multiple ECs exists in ec.c:
75 * struct acpi_ec *ec = ((struct seq_file *)f->private_data)->private;
78 unsigned int size = count;
79 loff_t init_off = *off;
82 if (*off >= EC_SPACE_SIZE)
84 if (*off + count >= EC_SPACE_SIZE) {
85 size = EC_SPACE_SIZE - *off;
91 if (get_user(byte_write, buf + *off - init_off)) {
93 return *off - init_off; /* partial write */
96 err = ec_write(*off, byte_write);
106 static const struct file_operations acpi_ec_io_ops = {
107 .owner = THIS_MODULE,
108 .open = acpi_ec_open_io,
109 .read = acpi_ec_read_io,
110 .write = acpi_ec_write_io,
111 .llseek = default_llseek,
114 int acpi_ec_add_debugfs(struct acpi_ec *ec, unsigned int ec_device_count)
116 struct dentry *dev_dir;
120 if (ec_device_count == 0) {
121 acpi_ec_debugfs_dir = debugfs_create_dir("ec", NULL);
122 if (!acpi_ec_debugfs_dir)
126 sprintf(name, "ec%u", ec_device_count);
127 dev_dir = debugfs_create_dir(name, acpi_ec_debugfs_dir);
129 if (ec_device_count != 0)
134 if (!debugfs_create_x32("gpe", 0444, dev_dir, (u32 *)&first_ec->gpe))
136 if (!debugfs_create_bool("use_global_lock", 0444, dev_dir,
137 (u32 *)&first_ec->global_lock))
142 if (!debugfs_create_file("io", mode, dev_dir, ec, &acpi_ec_io_ops))
148 debugfs_remove_recursive(acpi_ec_debugfs_dir);
152 static int __init acpi_ec_sys_init(void)
156 err = acpi_ec_add_debugfs(first_ec, 0);
162 static void __exit acpi_ec_sys_exit(void)
164 debugfs_remove_recursive(acpi_ec_debugfs_dir);
167 module_init(acpi_ec_sys_init);
168 module_exit(acpi_ec_sys_exit);