0001
0002
0003
0004
0005
0006
0007
0008 #define pr_fmt(fmt) "ACPI configfs: " fmt
0009
0010 #include <linux/init.h>
0011 #include <linux/module.h>
0012 #include <linux/configfs.h>
0013 #include <linux/acpi.h>
0014 #include <linux/security.h>
0015
0016 static struct config_group *acpi_table_group;
0017
0018 struct acpi_table {
0019 struct config_item cfg;
0020 struct acpi_table_header *header;
0021 u32 index;
0022 };
0023
0024 static ssize_t acpi_table_aml_write(struct config_item *cfg,
0025 const void *data, size_t size)
0026 {
0027 const struct acpi_table_header *header = data;
0028 struct acpi_table *table;
0029 int ret = security_locked_down(LOCKDOWN_ACPI_TABLES);
0030
0031 if (ret)
0032 return ret;
0033
0034 table = container_of(cfg, struct acpi_table, cfg);
0035
0036 if (table->header) {
0037 pr_err("table already loaded\n");
0038 return -EBUSY;
0039 }
0040
0041 if (header->length != size) {
0042 pr_err("invalid table length\n");
0043 return -EINVAL;
0044 }
0045
0046 if (memcmp(header->signature, ACPI_SIG_SSDT, 4)) {
0047 pr_err("invalid table signature\n");
0048 return -EINVAL;
0049 }
0050
0051 table = container_of(cfg, struct acpi_table, cfg);
0052
0053 table->header = kmemdup(header, header->length, GFP_KERNEL);
0054 if (!table->header)
0055 return -ENOMEM;
0056
0057 ret = acpi_load_table(table->header, &table->index);
0058 if (ret) {
0059 kfree(table->header);
0060 table->header = NULL;
0061 }
0062
0063 return ret;
0064 }
0065
0066 static inline struct acpi_table_header *get_header(struct config_item *cfg)
0067 {
0068 struct acpi_table *table = container_of(cfg, struct acpi_table, cfg);
0069
0070 if (!table->header)
0071 pr_err("table not loaded\n");
0072
0073 return table->header ?: ERR_PTR(-EINVAL);
0074 }
0075
0076 static ssize_t acpi_table_aml_read(struct config_item *cfg,
0077 void *data, size_t size)
0078 {
0079 struct acpi_table_header *h = get_header(cfg);
0080
0081 if (IS_ERR(h))
0082 return PTR_ERR(h);
0083
0084 if (data)
0085 memcpy(data, h, h->length);
0086
0087 return h->length;
0088 }
0089
0090 #define MAX_ACPI_TABLE_SIZE (128 * 1024)
0091
0092 CONFIGFS_BIN_ATTR(acpi_table_, aml, NULL, MAX_ACPI_TABLE_SIZE);
0093
0094 static struct configfs_bin_attribute *acpi_table_bin_attrs[] = {
0095 &acpi_table_attr_aml,
0096 NULL,
0097 };
0098
0099 static ssize_t acpi_table_signature_show(struct config_item *cfg, char *str)
0100 {
0101 struct acpi_table_header *h = get_header(cfg);
0102
0103 if (IS_ERR(h))
0104 return PTR_ERR(h);
0105
0106 return sysfs_emit(str, "%.*s\n", ACPI_NAMESEG_SIZE, h->signature);
0107 }
0108
0109 static ssize_t acpi_table_length_show(struct config_item *cfg, char *str)
0110 {
0111 struct acpi_table_header *h = get_header(cfg);
0112
0113 if (IS_ERR(h))
0114 return PTR_ERR(h);
0115
0116 return sysfs_emit(str, "%d\n", h->length);
0117 }
0118
0119 static ssize_t acpi_table_revision_show(struct config_item *cfg, char *str)
0120 {
0121 struct acpi_table_header *h = get_header(cfg);
0122
0123 if (IS_ERR(h))
0124 return PTR_ERR(h);
0125
0126 return sysfs_emit(str, "%d\n", h->revision);
0127 }
0128
0129 static ssize_t acpi_table_oem_id_show(struct config_item *cfg, char *str)
0130 {
0131 struct acpi_table_header *h = get_header(cfg);
0132
0133 if (IS_ERR(h))
0134 return PTR_ERR(h);
0135
0136 return sysfs_emit(str, "%.*s\n", ACPI_OEM_ID_SIZE, h->oem_id);
0137 }
0138
0139 static ssize_t acpi_table_oem_table_id_show(struct config_item *cfg, char *str)
0140 {
0141 struct acpi_table_header *h = get_header(cfg);
0142
0143 if (IS_ERR(h))
0144 return PTR_ERR(h);
0145
0146 return sysfs_emit(str, "%.*s\n", ACPI_OEM_TABLE_ID_SIZE, h->oem_table_id);
0147 }
0148
0149 static ssize_t acpi_table_oem_revision_show(struct config_item *cfg, char *str)
0150 {
0151 struct acpi_table_header *h = get_header(cfg);
0152
0153 if (IS_ERR(h))
0154 return PTR_ERR(h);
0155
0156 return sysfs_emit(str, "%d\n", h->oem_revision);
0157 }
0158
0159 static ssize_t acpi_table_asl_compiler_id_show(struct config_item *cfg,
0160 char *str)
0161 {
0162 struct acpi_table_header *h = get_header(cfg);
0163
0164 if (IS_ERR(h))
0165 return PTR_ERR(h);
0166
0167 return sysfs_emit(str, "%.*s\n", ACPI_NAMESEG_SIZE, h->asl_compiler_id);
0168 }
0169
0170 static ssize_t acpi_table_asl_compiler_revision_show(struct config_item *cfg,
0171 char *str)
0172 {
0173 struct acpi_table_header *h = get_header(cfg);
0174
0175 if (IS_ERR(h))
0176 return PTR_ERR(h);
0177
0178 return sysfs_emit(str, "%d\n", h->asl_compiler_revision);
0179 }
0180
0181 CONFIGFS_ATTR_RO(acpi_table_, signature);
0182 CONFIGFS_ATTR_RO(acpi_table_, length);
0183 CONFIGFS_ATTR_RO(acpi_table_, revision);
0184 CONFIGFS_ATTR_RO(acpi_table_, oem_id);
0185 CONFIGFS_ATTR_RO(acpi_table_, oem_table_id);
0186 CONFIGFS_ATTR_RO(acpi_table_, oem_revision);
0187 CONFIGFS_ATTR_RO(acpi_table_, asl_compiler_id);
0188 CONFIGFS_ATTR_RO(acpi_table_, asl_compiler_revision);
0189
0190 static struct configfs_attribute *acpi_table_attrs[] = {
0191 &acpi_table_attr_signature,
0192 &acpi_table_attr_length,
0193 &acpi_table_attr_revision,
0194 &acpi_table_attr_oem_id,
0195 &acpi_table_attr_oem_table_id,
0196 &acpi_table_attr_oem_revision,
0197 &acpi_table_attr_asl_compiler_id,
0198 &acpi_table_attr_asl_compiler_revision,
0199 NULL,
0200 };
0201
0202 static const struct config_item_type acpi_table_type = {
0203 .ct_owner = THIS_MODULE,
0204 .ct_bin_attrs = acpi_table_bin_attrs,
0205 .ct_attrs = acpi_table_attrs,
0206 };
0207
0208 static struct config_item *acpi_table_make_item(struct config_group *group,
0209 const char *name)
0210 {
0211 struct acpi_table *table;
0212
0213 table = kzalloc(sizeof(*table), GFP_KERNEL);
0214 if (!table)
0215 return ERR_PTR(-ENOMEM);
0216
0217 config_item_init_type_name(&table->cfg, name, &acpi_table_type);
0218 return &table->cfg;
0219 }
0220
0221 static void acpi_table_drop_item(struct config_group *group,
0222 struct config_item *cfg)
0223 {
0224 struct acpi_table *table = container_of(cfg, struct acpi_table, cfg);
0225
0226 pr_debug("Host-directed Dynamic ACPI Table Unload\n");
0227 acpi_unload_table(table->index);
0228 config_item_put(cfg);
0229 }
0230
0231 static struct configfs_group_operations acpi_table_group_ops = {
0232 .make_item = acpi_table_make_item,
0233 .drop_item = acpi_table_drop_item,
0234 };
0235
0236 static const struct config_item_type acpi_tables_type = {
0237 .ct_owner = THIS_MODULE,
0238 .ct_group_ops = &acpi_table_group_ops,
0239 };
0240
0241 static const struct config_item_type acpi_root_group_type = {
0242 .ct_owner = THIS_MODULE,
0243 };
0244
0245 static struct configfs_subsystem acpi_configfs = {
0246 .su_group = {
0247 .cg_item = {
0248 .ci_namebuf = "acpi",
0249 .ci_type = &acpi_root_group_type,
0250 },
0251 },
0252 .su_mutex = __MUTEX_INITIALIZER(acpi_configfs.su_mutex),
0253 };
0254
0255 static int __init acpi_configfs_init(void)
0256 {
0257 int ret;
0258 struct config_group *root = &acpi_configfs.su_group;
0259
0260 config_group_init(root);
0261
0262 ret = configfs_register_subsystem(&acpi_configfs);
0263 if (ret)
0264 return ret;
0265
0266 acpi_table_group = configfs_register_default_group(root, "table",
0267 &acpi_tables_type);
0268 if (IS_ERR(acpi_table_group)) {
0269 configfs_unregister_subsystem(&acpi_configfs);
0270 return PTR_ERR(acpi_table_group);
0271 }
0272
0273 return 0;
0274 }
0275 module_init(acpi_configfs_init);
0276
0277 static void __exit acpi_configfs_exit(void)
0278 {
0279 configfs_unregister_default_group(acpi_table_group);
0280 configfs_unregister_subsystem(&acpi_configfs);
0281 }
0282 module_exit(acpi_configfs_exit);
0283
0284 MODULE_AUTHOR("Octavian Purdila <octavian.purdila@intel.com>");
0285 MODULE_DESCRIPTION("ACPI configfs support");
0286 MODULE_LICENSE("GPL v2");