Merge tag 'trace-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt...
[linux/fpc-iii.git] / drivers / acpi / acpi_configfs.c
blobcf91f49101eac8d64ae16f47500cd177758baed9
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * ACPI configfs support
5 * Copyright (c) 2016 Intel Corporation
6 */
8 #define pr_fmt(fmt) "ACPI configfs: " fmt
10 #include <linux/init.h>
11 #include <linux/module.h>
12 #include <linux/configfs.h>
13 #include <linux/acpi.h>
14 #include <linux/security.h>
16 #include "acpica/accommon.h"
17 #include "acpica/actables.h"
19 static struct config_group *acpi_table_group;
21 struct acpi_table {
22 struct config_item cfg;
23 struct acpi_table_header *header;
24 u32 index;
27 static ssize_t acpi_table_aml_write(struct config_item *cfg,
28 const void *data, size_t size)
30 const struct acpi_table_header *header = data;
31 struct acpi_table *table;
32 int ret = security_locked_down(LOCKDOWN_ACPI_TABLES);
34 if (ret)
35 return ret;
37 table = container_of(cfg, struct acpi_table, cfg);
39 if (table->header) {
40 pr_err("table already loaded\n");
41 return -EBUSY;
44 if (header->length != size) {
45 pr_err("invalid table length\n");
46 return -EINVAL;
49 if (memcmp(header->signature, ACPI_SIG_SSDT, 4)) {
50 pr_err("invalid table signature\n");
51 return -EINVAL;
54 table = container_of(cfg, struct acpi_table, cfg);
56 table->header = kmemdup(header, header->length, GFP_KERNEL);
57 if (!table->header)
58 return -ENOMEM;
60 ret = acpi_load_table(table->header, &table->index);
61 if (ret) {
62 kfree(table->header);
63 table->header = NULL;
66 return ret;
69 static inline struct acpi_table_header *get_header(struct config_item *cfg)
71 struct acpi_table *table = container_of(cfg, struct acpi_table, cfg);
73 if (!table->header)
74 pr_err("table not loaded\n");
76 return table->header;
79 static ssize_t acpi_table_aml_read(struct config_item *cfg,
80 void *data, size_t size)
82 struct acpi_table_header *h = get_header(cfg);
84 if (!h)
85 return -EINVAL;
87 if (data)
88 memcpy(data, h, h->length);
90 return h->length;
93 #define MAX_ACPI_TABLE_SIZE (128 * 1024)
95 CONFIGFS_BIN_ATTR(acpi_table_, aml, NULL, MAX_ACPI_TABLE_SIZE);
97 static struct configfs_bin_attribute *acpi_table_bin_attrs[] = {
98 &acpi_table_attr_aml,
99 NULL,
102 static ssize_t acpi_table_signature_show(struct config_item *cfg, char *str)
104 struct acpi_table_header *h = get_header(cfg);
106 if (!h)
107 return -EINVAL;
109 return sprintf(str, "%.*s\n", ACPI_NAMESEG_SIZE, h->signature);
112 static ssize_t acpi_table_length_show(struct config_item *cfg, char *str)
114 struct acpi_table_header *h = get_header(cfg);
116 if (!h)
117 return -EINVAL;
119 return sprintf(str, "%d\n", h->length);
122 static ssize_t acpi_table_revision_show(struct config_item *cfg, char *str)
124 struct acpi_table_header *h = get_header(cfg);
126 if (!h)
127 return -EINVAL;
129 return sprintf(str, "%d\n", h->revision);
132 static ssize_t acpi_table_oem_id_show(struct config_item *cfg, char *str)
134 struct acpi_table_header *h = get_header(cfg);
136 if (!h)
137 return -EINVAL;
139 return sprintf(str, "%.*s\n", ACPI_OEM_ID_SIZE, h->oem_id);
142 static ssize_t acpi_table_oem_table_id_show(struct config_item *cfg, char *str)
144 struct acpi_table_header *h = get_header(cfg);
146 if (!h)
147 return -EINVAL;
149 return sprintf(str, "%.*s\n", ACPI_OEM_TABLE_ID_SIZE, h->oem_table_id);
152 static ssize_t acpi_table_oem_revision_show(struct config_item *cfg, char *str)
154 struct acpi_table_header *h = get_header(cfg);
156 if (!h)
157 return -EINVAL;
159 return sprintf(str, "%d\n", h->oem_revision);
162 static ssize_t acpi_table_asl_compiler_id_show(struct config_item *cfg,
163 char *str)
165 struct acpi_table_header *h = get_header(cfg);
167 if (!h)
168 return -EINVAL;
170 return sprintf(str, "%.*s\n", ACPI_NAMESEG_SIZE, h->asl_compiler_id);
173 static ssize_t acpi_table_asl_compiler_revision_show(struct config_item *cfg,
174 char *str)
176 struct acpi_table_header *h = get_header(cfg);
178 if (!h)
179 return -EINVAL;
181 return sprintf(str, "%d\n", h->asl_compiler_revision);
184 CONFIGFS_ATTR_RO(acpi_table_, signature);
185 CONFIGFS_ATTR_RO(acpi_table_, length);
186 CONFIGFS_ATTR_RO(acpi_table_, revision);
187 CONFIGFS_ATTR_RO(acpi_table_, oem_id);
188 CONFIGFS_ATTR_RO(acpi_table_, oem_table_id);
189 CONFIGFS_ATTR_RO(acpi_table_, oem_revision);
190 CONFIGFS_ATTR_RO(acpi_table_, asl_compiler_id);
191 CONFIGFS_ATTR_RO(acpi_table_, asl_compiler_revision);
193 static struct configfs_attribute *acpi_table_attrs[] = {
194 &acpi_table_attr_signature,
195 &acpi_table_attr_length,
196 &acpi_table_attr_revision,
197 &acpi_table_attr_oem_id,
198 &acpi_table_attr_oem_table_id,
199 &acpi_table_attr_oem_revision,
200 &acpi_table_attr_asl_compiler_id,
201 &acpi_table_attr_asl_compiler_revision,
202 NULL,
205 static const struct config_item_type acpi_table_type = {
206 .ct_owner = THIS_MODULE,
207 .ct_bin_attrs = acpi_table_bin_attrs,
208 .ct_attrs = acpi_table_attrs,
211 static struct config_item *acpi_table_make_item(struct config_group *group,
212 const char *name)
214 struct acpi_table *table;
216 table = kzalloc(sizeof(*table), GFP_KERNEL);
217 if (!table)
218 return ERR_PTR(-ENOMEM);
220 config_item_init_type_name(&table->cfg, name, &acpi_table_type);
221 return &table->cfg;
224 static void acpi_table_drop_item(struct config_group *group,
225 struct config_item *cfg)
227 struct acpi_table *table = container_of(cfg, struct acpi_table, cfg);
229 ACPI_INFO(("Host-directed Dynamic ACPI Table Unload"));
230 acpi_unload_table(table->index);
231 config_item_put(cfg);
234 static struct configfs_group_operations acpi_table_group_ops = {
235 .make_item = acpi_table_make_item,
236 .drop_item = acpi_table_drop_item,
239 static const struct config_item_type acpi_tables_type = {
240 .ct_owner = THIS_MODULE,
241 .ct_group_ops = &acpi_table_group_ops,
244 static const struct config_item_type acpi_root_group_type = {
245 .ct_owner = THIS_MODULE,
248 static struct configfs_subsystem acpi_configfs = {
249 .su_group = {
250 .cg_item = {
251 .ci_namebuf = "acpi",
252 .ci_type = &acpi_root_group_type,
255 .su_mutex = __MUTEX_INITIALIZER(acpi_configfs.su_mutex),
258 static int __init acpi_configfs_init(void)
260 int ret;
261 struct config_group *root = &acpi_configfs.su_group;
263 config_group_init(root);
265 ret = configfs_register_subsystem(&acpi_configfs);
266 if (ret)
267 return ret;
269 acpi_table_group = configfs_register_default_group(root, "table",
270 &acpi_tables_type);
271 return PTR_ERR_OR_ZERO(acpi_table_group);
273 module_init(acpi_configfs_init);
275 static void __exit acpi_configfs_exit(void)
277 configfs_unregister_default_group(acpi_table_group);
278 configfs_unregister_subsystem(&acpi_configfs);
280 module_exit(acpi_configfs_exit);
282 MODULE_AUTHOR("Octavian Purdila <octavian.purdila@intel.com>");
283 MODULE_DESCRIPTION("ACPI configfs support");
284 MODULE_LICENSE("GPL v2");