Linux 4.19.133
[linux/fpc-iii.git] / drivers / acpi / ec_sys.c
blobdd70d6c2bca03eb227fe834fa14e33636f842d48
1 /*
2 * ec_sys.c
4 * Copyright (C) 2010 SUSE Products GmbH/Novell
5 * Author:
6 * Thomas Renninger <trenn@suse.de>
8 * This work is licensed under the terms of the GNU GPL, version 2.
9 */
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>
16 #include "internal.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 "
25 "be needed.");
27 #define EC_SPACE_SIZE 256
29 static struct dentry *acpi_ec_debugfs_dir;
31 static ssize_t acpi_ec_read_io(struct file *f, char __user *buf,
32 size_t count, loff_t *off)
34 /* Use this if support reading/writing multiple ECs exists in ec.c:
35 * struct acpi_ec *ec = ((struct seq_file *)f->private_data)->private;
37 unsigned int size = EC_SPACE_SIZE;
38 loff_t init_off = *off;
39 int err = 0;
41 if (*off >= size)
42 return 0;
43 if (*off + count >= size) {
44 size -= *off;
45 count = size;
46 } else
47 size = count;
49 while (size) {
50 u8 byte_read;
51 err = ec_read(*off, &byte_read);
52 if (err)
53 return err;
54 if (put_user(byte_read, buf + *off - init_off)) {
55 if (*off - init_off)
56 return *off - init_off; /* partial read */
57 return -EFAULT;
59 *off += 1;
60 size--;
62 return count;
65 static ssize_t acpi_ec_write_io(struct file *f, const char __user *buf,
66 size_t count, loff_t *off)
68 /* Use this if support reading/writing multiple ECs exists in ec.c:
69 * struct acpi_ec *ec = ((struct seq_file *)f->private_data)->private;
72 unsigned int size = count;
73 loff_t init_off = *off;
74 int err = 0;
76 if (!write_support)
77 return -EINVAL;
79 if (*off >= EC_SPACE_SIZE)
80 return 0;
81 if (*off + count >= EC_SPACE_SIZE) {
82 size = EC_SPACE_SIZE - *off;
83 count = size;
86 while (size) {
87 u8 byte_write;
88 if (get_user(byte_write, buf + *off - init_off)) {
89 if (*off - init_off)
90 return *off - init_off; /* partial write */
91 return -EFAULT;
93 err = ec_write(*off, byte_write);
94 if (err)
95 return err;
97 *off += 1;
98 size--;
100 return count;
103 static const struct file_operations acpi_ec_io_ops = {
104 .owner = THIS_MODULE,
105 .open = simple_open,
106 .read = acpi_ec_read_io,
107 .write = acpi_ec_write_io,
108 .llseek = default_llseek,
111 static int acpi_ec_add_debugfs(struct acpi_ec *ec, unsigned int ec_device_count)
113 struct dentry *dev_dir;
114 char name[64];
115 umode_t mode = 0400;
117 if (ec_device_count == 0) {
118 acpi_ec_debugfs_dir = debugfs_create_dir("ec", NULL);
119 if (!acpi_ec_debugfs_dir)
120 return -ENOMEM;
123 sprintf(name, "ec%u", ec_device_count);
124 dev_dir = debugfs_create_dir(name, acpi_ec_debugfs_dir);
125 if (!dev_dir) {
126 if (ec_device_count != 0)
127 goto error;
128 return -ENOMEM;
131 if (!debugfs_create_x32("gpe", 0444, dev_dir, &first_ec->gpe))
132 goto error;
133 if (!debugfs_create_bool("use_global_lock", 0444, dev_dir,
134 &first_ec->global_lock))
135 goto error;
137 if (write_support)
138 mode = 0600;
139 if (!debugfs_create_file("io", mode, dev_dir, ec, &acpi_ec_io_ops))
140 goto error;
142 return 0;
144 error:
145 debugfs_remove_recursive(acpi_ec_debugfs_dir);
146 return -ENOMEM;
149 static int __init acpi_ec_sys_init(void)
151 int err = 0;
152 if (first_ec)
153 err = acpi_ec_add_debugfs(first_ec, 0);
154 else
155 err = -ENODEV;
156 return err;
159 static void __exit acpi_ec_sys_exit(void)
161 debugfs_remove_recursive(acpi_ec_debugfs_dir);
164 module_init(acpi_ec_sys_init);
165 module_exit(acpi_ec_sys_exit);