blob: 435bd0ffc8c023a203e98f8eede528b2c5172061 [file] [log] [blame]
Thomas Renninger526b4af2011-05-26 12:26:24 +02001/*
Zhang Rui5baa1be2013-01-17 14:24:05 +01002 * custom_method.c - debugfs interface for customizing ACPI control method
Thomas Renninger526b4af2011-05-26 12:26:24 +02003 */
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>
Lv Zheng8b484632013-12-03 08:49:16 +080010#include <linux/acpi.h>
Thomas Renninger526b4af2011-05-26 12:26:24 +020011
12#include "internal.h"
13
14#define _COMPONENT ACPI_SYSTEM_COMPONENT
15ACPI_MODULE_NAME("custom_method");
16MODULE_LICENSE("GPL");
17
18static struct dentry *cm_dentry;
19
20/* /sys/kernel/debug/acpi/custom_method */
21
22static 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 buf = kzalloc(max_size, GFP_KERNEL);
41 if (!buf)
42 return -ENOMEM;
43 }
44
45 if (buf == NULL)
46 return -EINVAL;
47
48 if ((*ppos > max_size) ||
49 (*ppos + count > max_size) ||
50 (*ppos + count < count) ||
Wenwen Wang5c12dad2019-08-16 00:08:27 -050051 (count > uncopied_bytes)) {
52 kfree(buf);
Thomas Renninger526b4af2011-05-26 12:26:24 +020053 return -EINVAL;
Wenwen Wang5c12dad2019-08-16 00:08:27 -050054 }
Thomas Renninger526b4af2011-05-26 12:26:24 +020055
56 if (copy_from_user(buf + (*ppos), user_buf, count)) {
57 kfree(buf);
58 buf = NULL;
59 return -EFAULT;
60 }
61
62 uncopied_bytes -= count;
63 *ppos += count;
64
65 if (!uncopied_bytes) {
66 status = acpi_install_method(buf);
67 kfree(buf);
68 buf = NULL;
69 if (ACPI_FAILURE(status))
70 return -EINVAL;
Rusty Russell373d4d02013-01-21 17:17:39 +103071 add_taint(TAINT_OVERRIDDEN_ACPI_TABLE, LOCKDEP_NOW_UNRELIABLE);
Thomas Renninger526b4af2011-05-26 12:26:24 +020072 }
73
Wenwen Wang5c12dad2019-08-16 00:08:27 -050074 kfree(buf);
Thomas Renninger526b4af2011-05-26 12:26:24 +020075 return count;
76}
77
78static const struct file_operations cm_fops = {
79 .write = cm_write,
80 .llseek = default_llseek,
81};
82
83static int __init acpi_custom_method_init(void)
84{
85 if (acpi_debugfs_dir == NULL)
86 return -ENOENT;
87
88 cm_dentry = debugfs_create_file("custom_method", S_IWUSR,
89 acpi_debugfs_dir, NULL, &cm_fops);
90 if (cm_dentry == NULL)
91 return -ENODEV;
92
93 return 0;
94}
95
96static void __exit acpi_custom_method_exit(void)
97{
98 if (cm_dentry)
99 debugfs_remove(cm_dentry);
100 }
101
102module_init(acpi_custom_method_init);
103module_exit(acpi_custom_method_exit);