GNU Linux-libre 4.9.304-gnu1
[releases.git] / drivers / acpi / custom_method.c
1 /*
2  * custom_method.c - debugfs interface for customizing ACPI control method
3  */
4
5 #include <linux/init.h>
6 #include <linux/module.h>
7 #include <linux/kernel.h>
8 #include <linux/uaccess.h>
9 #include <linux/debugfs.h>
10 #include <linux/acpi.h>
11
12 #include "internal.h"
13
14 #define _COMPONENT              ACPI_SYSTEM_COMPONENT
15 ACPI_MODULE_NAME("custom_method");
16 MODULE_LICENSE("GPL");
17
18 static struct dentry *cm_dentry;
19
20 /* /sys/kernel/debug/acpi/custom_method */
21
22 static ssize_t cm_write(struct file *file, const char __user * user_buf,
23                         size_t count, loff_t *ppos)
24 {
25         static char *buf;
26         static u32 max_size;
27         static u32 uncopied_bytes;
28
29         struct acpi_table_header table;
30         acpi_status status;
31
32         if (!(*ppos)) {
33                 /* parse the table header to get the table length */
34                 if (count <= sizeof(struct acpi_table_header))
35                         return -EINVAL;
36                 if (copy_from_user(&table, user_buf,
37                                    sizeof(struct acpi_table_header)))
38                         return -EFAULT;
39                 uncopied_bytes = max_size = table.length;
40                 /* make sure the buf is not allocated */
41                 kfree(buf);
42                 buf = kzalloc(max_size, GFP_KERNEL);
43                 if (!buf)
44                         return -ENOMEM;
45         }
46
47         if (buf == NULL)
48                 return -EINVAL;
49
50         if ((*ppos > max_size) ||
51             (*ppos + count > max_size) ||
52             (*ppos + count < count) ||
53             (count > uncopied_bytes)) {
54                 kfree(buf);
55                 buf = NULL;
56                 return -EINVAL;
57         }
58
59         if (copy_from_user(buf + (*ppos), user_buf, count)) {
60                 kfree(buf);
61                 buf = NULL;
62                 return -EFAULT;
63         }
64
65         uncopied_bytes -= count;
66         *ppos += count;
67
68         if (!uncopied_bytes) {
69                 status = acpi_install_method(buf);
70                 kfree(buf);
71                 buf = NULL;
72                 if (ACPI_FAILURE(status))
73                         return -EINVAL;
74                 add_taint(TAINT_OVERRIDDEN_ACPI_TABLE, LOCKDEP_NOW_UNRELIABLE);
75         }
76
77         return count;
78 }
79
80 static const struct file_operations cm_fops = {
81         .write = cm_write,
82         .llseek = default_llseek,
83 };
84
85 static int __init acpi_custom_method_init(void)
86 {
87         if (acpi_debugfs_dir == NULL)
88                 return -ENOENT;
89
90         cm_dentry = debugfs_create_file("custom_method", S_IWUSR,
91                                         acpi_debugfs_dir, NULL, &cm_fops);
92         if (cm_dentry == NULL)
93                 return -ENODEV;
94
95         return 0;
96 }
97
98 static void __exit acpi_custom_method_exit(void)
99 {
100         if (cm_dentry)
101                 debugfs_remove(cm_dentry);
102  }
103
104 module_init(acpi_custom_method_init);
105 module_exit(acpi_custom_method_exit);