0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019 #include <linux/kernel.h>
0020 #include <linux/init.h>
0021 #include <linux/module.h>
0022 #include <linux/types.h>
0023 #include <linux/kobject.h>
0024 #include <linux/dmi.h>
0025 #include <linux/capability.h>
0026 #include <linux/slab.h>
0027 #include <linux/list.h>
0028 #include <linux/io.h>
0029 #include <asm/dmi.h>
0030
0031 #define MAX_ENTRY_TYPE 255
0032
0033
0034 struct dmi_sysfs_entry {
0035 struct dmi_header dh;
0036 struct kobject kobj;
0037 int instance;
0038 int position;
0039 struct list_head list;
0040 struct kobject *child;
0041 };
0042
0043
0044
0045
0046
0047
0048 static LIST_HEAD(entry_list);
0049 static DEFINE_SPINLOCK(entry_list_lock);
0050
0051
0052 struct dmi_sysfs_attribute {
0053 struct attribute attr;
0054 ssize_t (*show)(struct dmi_sysfs_entry *entry, char *buf);
0055 };
0056
0057 #define DMI_SYSFS_ATTR(_entry, _name) \
0058 struct dmi_sysfs_attribute dmi_sysfs_attr_##_entry##_##_name = { \
0059 .attr = {.name = __stringify(_name), .mode = 0400}, \
0060 .show = dmi_sysfs_##_entry##_##_name, \
0061 }
0062
0063
0064
0065
0066
0067 struct dmi_sysfs_mapped_attribute {
0068 struct attribute attr;
0069 ssize_t (*show)(struct dmi_sysfs_entry *entry,
0070 const struct dmi_header *dh,
0071 char *buf);
0072 };
0073
0074 #define DMI_SYSFS_MAPPED_ATTR(_entry, _name) \
0075 struct dmi_sysfs_mapped_attribute dmi_sysfs_attr_##_entry##_##_name = { \
0076 .attr = {.name = __stringify(_name), .mode = 0400}, \
0077 .show = dmi_sysfs_##_entry##_##_name, \
0078 }
0079
0080
0081
0082
0083 static void dmi_entry_free(struct kobject *kobj)
0084 {
0085 kfree(kobj);
0086 }
0087
0088 static struct dmi_sysfs_entry *to_entry(struct kobject *kobj)
0089 {
0090 return container_of(kobj, struct dmi_sysfs_entry, kobj);
0091 }
0092
0093 static struct dmi_sysfs_attribute *to_attr(struct attribute *attr)
0094 {
0095 return container_of(attr, struct dmi_sysfs_attribute, attr);
0096 }
0097
0098 static ssize_t dmi_sysfs_attr_show(struct kobject *kobj,
0099 struct attribute *_attr, char *buf)
0100 {
0101 struct dmi_sysfs_entry *entry = to_entry(kobj);
0102 struct dmi_sysfs_attribute *attr = to_attr(_attr);
0103
0104
0105 if (!capable(CAP_SYS_ADMIN))
0106 return -EACCES;
0107
0108 return attr->show(entry, buf);
0109 }
0110
0111 static const struct sysfs_ops dmi_sysfs_attr_ops = {
0112 .show = dmi_sysfs_attr_show,
0113 };
0114
0115 typedef ssize_t (*dmi_callback)(struct dmi_sysfs_entry *,
0116 const struct dmi_header *dh, void *);
0117
0118 struct find_dmi_data {
0119 struct dmi_sysfs_entry *entry;
0120 dmi_callback callback;
0121 void *private;
0122 int instance_countdown;
0123 ssize_t ret;
0124 };
0125
0126 static void find_dmi_entry_helper(const struct dmi_header *dh,
0127 void *_data)
0128 {
0129 struct find_dmi_data *data = _data;
0130 struct dmi_sysfs_entry *entry = data->entry;
0131
0132
0133 if (dh->type != entry->dh.type)
0134 return;
0135
0136 if (data->instance_countdown != 0) {
0137
0138 data->instance_countdown--;
0139 return;
0140 }
0141
0142
0143
0144
0145
0146 data->instance_countdown--;
0147
0148
0149 data->ret = data->callback(entry, dh, data->private);
0150 }
0151
0152
0153 struct dmi_read_state {
0154 char *buf;
0155 loff_t pos;
0156 size_t count;
0157 };
0158
0159 static ssize_t find_dmi_entry(struct dmi_sysfs_entry *entry,
0160 dmi_callback callback, void *private)
0161 {
0162 struct find_dmi_data data = {
0163 .entry = entry,
0164 .callback = callback,
0165 .private = private,
0166 .instance_countdown = entry->instance,
0167 .ret = -EIO,
0168 };
0169 int ret;
0170
0171 ret = dmi_walk(find_dmi_entry_helper, &data);
0172
0173 if (ret)
0174 return -EINVAL;
0175 return data.ret;
0176 }
0177
0178
0179
0180
0181
0182
0183 static size_t dmi_entry_length(const struct dmi_header *dh)
0184 {
0185 const char *p = (const char *)dh;
0186
0187 p += dh->length;
0188
0189 while (p[0] || p[1])
0190 p++;
0191
0192 return 2 + p - (const char *)dh;
0193 }
0194
0195
0196
0197
0198 struct dmi_entry_attr_show_data {
0199 struct attribute *attr;
0200 char *buf;
0201 };
0202
0203 static ssize_t dmi_entry_attr_show_helper(struct dmi_sysfs_entry *entry,
0204 const struct dmi_header *dh,
0205 void *_data)
0206 {
0207 struct dmi_entry_attr_show_data *data = _data;
0208 struct dmi_sysfs_mapped_attribute *attr;
0209
0210 attr = container_of(data->attr,
0211 struct dmi_sysfs_mapped_attribute, attr);
0212 return attr->show(entry, dh, data->buf);
0213 }
0214
0215 static ssize_t dmi_entry_attr_show(struct kobject *kobj,
0216 struct attribute *attr,
0217 char *buf)
0218 {
0219 struct dmi_entry_attr_show_data data = {
0220 .attr = attr,
0221 .buf = buf,
0222 };
0223
0224
0225 return find_dmi_entry(to_entry(kobj->parent),
0226 dmi_entry_attr_show_helper, &data);
0227 }
0228
0229 static const struct sysfs_ops dmi_sysfs_specialize_attr_ops = {
0230 .show = dmi_entry_attr_show,
0231 };
0232
0233
0234
0235
0236
0237
0238
0239 #define DMI_SEL_ACCESS_METHOD_IO8 0x00
0240 #define DMI_SEL_ACCESS_METHOD_IO2x8 0x01
0241 #define DMI_SEL_ACCESS_METHOD_IO16 0x02
0242 #define DMI_SEL_ACCESS_METHOD_PHYS32 0x03
0243 #define DMI_SEL_ACCESS_METHOD_GPNV 0x04
0244
0245 struct dmi_system_event_log {
0246 struct dmi_header header;
0247 u16 area_length;
0248 u16 header_start_offset;
0249 u16 data_start_offset;
0250 u8 access_method;
0251 u8 status;
0252 u32 change_token;
0253 union {
0254 struct {
0255 u16 index_addr;
0256 u16 data_addr;
0257 } io;
0258 u32 phys_addr32;
0259 u16 gpnv_handle;
0260 u32 access_method_address;
0261 };
0262 u8 header_format;
0263 u8 type_descriptors_supported_count;
0264 u8 per_log_type_descriptor_length;
0265 u8 supported_log_type_descriptos[];
0266 } __packed;
0267
0268 #define DMI_SYSFS_SEL_FIELD(_field) \
0269 static ssize_t dmi_sysfs_sel_##_field(struct dmi_sysfs_entry *entry, \
0270 const struct dmi_header *dh, \
0271 char *buf) \
0272 { \
0273 struct dmi_system_event_log sel; \
0274 if (sizeof(sel) > dmi_entry_length(dh)) \
0275 return -EIO; \
0276 memcpy(&sel, dh, sizeof(sel)); \
0277 return sprintf(buf, "%u\n", sel._field); \
0278 } \
0279 static DMI_SYSFS_MAPPED_ATTR(sel, _field)
0280
0281 DMI_SYSFS_SEL_FIELD(area_length);
0282 DMI_SYSFS_SEL_FIELD(header_start_offset);
0283 DMI_SYSFS_SEL_FIELD(data_start_offset);
0284 DMI_SYSFS_SEL_FIELD(access_method);
0285 DMI_SYSFS_SEL_FIELD(status);
0286 DMI_SYSFS_SEL_FIELD(change_token);
0287 DMI_SYSFS_SEL_FIELD(access_method_address);
0288 DMI_SYSFS_SEL_FIELD(header_format);
0289 DMI_SYSFS_SEL_FIELD(type_descriptors_supported_count);
0290 DMI_SYSFS_SEL_FIELD(per_log_type_descriptor_length);
0291
0292 static struct attribute *dmi_sysfs_sel_attrs[] = {
0293 &dmi_sysfs_attr_sel_area_length.attr,
0294 &dmi_sysfs_attr_sel_header_start_offset.attr,
0295 &dmi_sysfs_attr_sel_data_start_offset.attr,
0296 &dmi_sysfs_attr_sel_access_method.attr,
0297 &dmi_sysfs_attr_sel_status.attr,
0298 &dmi_sysfs_attr_sel_change_token.attr,
0299 &dmi_sysfs_attr_sel_access_method_address.attr,
0300 &dmi_sysfs_attr_sel_header_format.attr,
0301 &dmi_sysfs_attr_sel_type_descriptors_supported_count.attr,
0302 &dmi_sysfs_attr_sel_per_log_type_descriptor_length.attr,
0303 NULL,
0304 };
0305 ATTRIBUTE_GROUPS(dmi_sysfs_sel);
0306
0307 static struct kobj_type dmi_system_event_log_ktype = {
0308 .release = dmi_entry_free,
0309 .sysfs_ops = &dmi_sysfs_specialize_attr_ops,
0310 .default_groups = dmi_sysfs_sel_groups,
0311 };
0312
0313 typedef u8 (*sel_io_reader)(const struct dmi_system_event_log *sel,
0314 loff_t offset);
0315
0316 static DEFINE_MUTEX(io_port_lock);
0317
0318 static u8 read_sel_8bit_indexed_io(const struct dmi_system_event_log *sel,
0319 loff_t offset)
0320 {
0321 u8 ret;
0322
0323 mutex_lock(&io_port_lock);
0324 outb((u8)offset, sel->io.index_addr);
0325 ret = inb(sel->io.data_addr);
0326 mutex_unlock(&io_port_lock);
0327 return ret;
0328 }
0329
0330 static u8 read_sel_2x8bit_indexed_io(const struct dmi_system_event_log *sel,
0331 loff_t offset)
0332 {
0333 u8 ret;
0334
0335 mutex_lock(&io_port_lock);
0336 outb((u8)offset, sel->io.index_addr);
0337 outb((u8)(offset >> 8), sel->io.index_addr + 1);
0338 ret = inb(sel->io.data_addr);
0339 mutex_unlock(&io_port_lock);
0340 return ret;
0341 }
0342
0343 static u8 read_sel_16bit_indexed_io(const struct dmi_system_event_log *sel,
0344 loff_t offset)
0345 {
0346 u8 ret;
0347
0348 mutex_lock(&io_port_lock);
0349 outw((u16)offset, sel->io.index_addr);
0350 ret = inb(sel->io.data_addr);
0351 mutex_unlock(&io_port_lock);
0352 return ret;
0353 }
0354
0355 static sel_io_reader sel_io_readers[] = {
0356 [DMI_SEL_ACCESS_METHOD_IO8] = read_sel_8bit_indexed_io,
0357 [DMI_SEL_ACCESS_METHOD_IO2x8] = read_sel_2x8bit_indexed_io,
0358 [DMI_SEL_ACCESS_METHOD_IO16] = read_sel_16bit_indexed_io,
0359 };
0360
0361 static ssize_t dmi_sel_raw_read_io(struct dmi_sysfs_entry *entry,
0362 const struct dmi_system_event_log *sel,
0363 char *buf, loff_t pos, size_t count)
0364 {
0365 ssize_t wrote = 0;
0366
0367 sel_io_reader io_reader = sel_io_readers[sel->access_method];
0368
0369 while (count && pos < sel->area_length) {
0370 count--;
0371 *(buf++) = io_reader(sel, pos++);
0372 wrote++;
0373 }
0374
0375 return wrote;
0376 }
0377
0378 static ssize_t dmi_sel_raw_read_phys32(struct dmi_sysfs_entry *entry,
0379 const struct dmi_system_event_log *sel,
0380 char *buf, loff_t pos, size_t count)
0381 {
0382 u8 __iomem *mapped;
0383 ssize_t wrote = 0;
0384
0385 mapped = dmi_remap(sel->access_method_address, sel->area_length);
0386 if (!mapped)
0387 return -EIO;
0388
0389 while (count && pos < sel->area_length) {
0390 count--;
0391 *(buf++) = readb(mapped + pos++);
0392 wrote++;
0393 }
0394
0395 dmi_unmap(mapped);
0396 return wrote;
0397 }
0398
0399 static ssize_t dmi_sel_raw_read_helper(struct dmi_sysfs_entry *entry,
0400 const struct dmi_header *dh,
0401 void *_state)
0402 {
0403 struct dmi_read_state *state = _state;
0404 struct dmi_system_event_log sel;
0405
0406 if (sizeof(sel) > dmi_entry_length(dh))
0407 return -EIO;
0408
0409 memcpy(&sel, dh, sizeof(sel));
0410
0411 switch (sel.access_method) {
0412 case DMI_SEL_ACCESS_METHOD_IO8:
0413 case DMI_SEL_ACCESS_METHOD_IO2x8:
0414 case DMI_SEL_ACCESS_METHOD_IO16:
0415 return dmi_sel_raw_read_io(entry, &sel, state->buf,
0416 state->pos, state->count);
0417 case DMI_SEL_ACCESS_METHOD_PHYS32:
0418 return dmi_sel_raw_read_phys32(entry, &sel, state->buf,
0419 state->pos, state->count);
0420 case DMI_SEL_ACCESS_METHOD_GPNV:
0421 pr_info("dmi-sysfs: GPNV support missing.\n");
0422 return -EIO;
0423 default:
0424 pr_info("dmi-sysfs: Unknown access method %02x\n",
0425 sel.access_method);
0426 return -EIO;
0427 }
0428 }
0429
0430 static ssize_t dmi_sel_raw_read(struct file *filp, struct kobject *kobj,
0431 struct bin_attribute *bin_attr,
0432 char *buf, loff_t pos, size_t count)
0433 {
0434 struct dmi_sysfs_entry *entry = to_entry(kobj->parent);
0435 struct dmi_read_state state = {
0436 .buf = buf,
0437 .pos = pos,
0438 .count = count,
0439 };
0440
0441 return find_dmi_entry(entry, dmi_sel_raw_read_helper, &state);
0442 }
0443
0444 static struct bin_attribute dmi_sel_raw_attr = {
0445 .attr = {.name = "raw_event_log", .mode = 0400},
0446 .read = dmi_sel_raw_read,
0447 };
0448
0449 static int dmi_system_event_log(struct dmi_sysfs_entry *entry)
0450 {
0451 int ret;
0452
0453 entry->child = kzalloc(sizeof(*entry->child), GFP_KERNEL);
0454 if (!entry->child)
0455 return -ENOMEM;
0456 ret = kobject_init_and_add(entry->child,
0457 &dmi_system_event_log_ktype,
0458 &entry->kobj,
0459 "system_event_log");
0460 if (ret)
0461 goto out_free;
0462
0463 ret = sysfs_create_bin_file(entry->child, &dmi_sel_raw_attr);
0464 if (ret)
0465 goto out_del;
0466
0467 return 0;
0468
0469 out_del:
0470 kobject_del(entry->child);
0471 out_free:
0472 kfree(entry->child);
0473 return ret;
0474 }
0475
0476
0477
0478
0479
0480 static ssize_t dmi_sysfs_entry_length(struct dmi_sysfs_entry *entry, char *buf)
0481 {
0482 return sprintf(buf, "%d\n", entry->dh.length);
0483 }
0484
0485 static ssize_t dmi_sysfs_entry_handle(struct dmi_sysfs_entry *entry, char *buf)
0486 {
0487 return sprintf(buf, "%d\n", entry->dh.handle);
0488 }
0489
0490 static ssize_t dmi_sysfs_entry_type(struct dmi_sysfs_entry *entry, char *buf)
0491 {
0492 return sprintf(buf, "%d\n", entry->dh.type);
0493 }
0494
0495 static ssize_t dmi_sysfs_entry_instance(struct dmi_sysfs_entry *entry,
0496 char *buf)
0497 {
0498 return sprintf(buf, "%d\n", entry->instance);
0499 }
0500
0501 static ssize_t dmi_sysfs_entry_position(struct dmi_sysfs_entry *entry,
0502 char *buf)
0503 {
0504 return sprintf(buf, "%d\n", entry->position);
0505 }
0506
0507 static DMI_SYSFS_ATTR(entry, length);
0508 static DMI_SYSFS_ATTR(entry, handle);
0509 static DMI_SYSFS_ATTR(entry, type);
0510 static DMI_SYSFS_ATTR(entry, instance);
0511 static DMI_SYSFS_ATTR(entry, position);
0512
0513 static struct attribute *dmi_sysfs_entry_attrs[] = {
0514 &dmi_sysfs_attr_entry_length.attr,
0515 &dmi_sysfs_attr_entry_handle.attr,
0516 &dmi_sysfs_attr_entry_type.attr,
0517 &dmi_sysfs_attr_entry_instance.attr,
0518 &dmi_sysfs_attr_entry_position.attr,
0519 NULL,
0520 };
0521 ATTRIBUTE_GROUPS(dmi_sysfs_entry);
0522
0523 static ssize_t dmi_entry_raw_read_helper(struct dmi_sysfs_entry *entry,
0524 const struct dmi_header *dh,
0525 void *_state)
0526 {
0527 struct dmi_read_state *state = _state;
0528 size_t entry_length;
0529
0530 entry_length = dmi_entry_length(dh);
0531
0532 return memory_read_from_buffer(state->buf, state->count,
0533 &state->pos, dh, entry_length);
0534 }
0535
0536 static ssize_t dmi_entry_raw_read(struct file *filp,
0537 struct kobject *kobj,
0538 struct bin_attribute *bin_attr,
0539 char *buf, loff_t pos, size_t count)
0540 {
0541 struct dmi_sysfs_entry *entry = to_entry(kobj);
0542 struct dmi_read_state state = {
0543 .buf = buf,
0544 .pos = pos,
0545 .count = count,
0546 };
0547
0548 return find_dmi_entry(entry, dmi_entry_raw_read_helper, &state);
0549 }
0550
0551 static const struct bin_attribute dmi_entry_raw_attr = {
0552 .attr = {.name = "raw", .mode = 0400},
0553 .read = dmi_entry_raw_read,
0554 };
0555
0556 static void dmi_sysfs_entry_release(struct kobject *kobj)
0557 {
0558 struct dmi_sysfs_entry *entry = to_entry(kobj);
0559
0560 spin_lock(&entry_list_lock);
0561 list_del(&entry->list);
0562 spin_unlock(&entry_list_lock);
0563 kfree(entry);
0564 }
0565
0566 static struct kobj_type dmi_sysfs_entry_ktype = {
0567 .release = dmi_sysfs_entry_release,
0568 .sysfs_ops = &dmi_sysfs_attr_ops,
0569 .default_groups = dmi_sysfs_entry_groups,
0570 };
0571
0572 static struct kset *dmi_kset;
0573
0574
0575 static int __initdata instance_counts[MAX_ENTRY_TYPE + 1];
0576
0577
0578 static int __initdata position_count;
0579
0580 static void __init dmi_sysfs_register_handle(const struct dmi_header *dh,
0581 void *_ret)
0582 {
0583 struct dmi_sysfs_entry *entry;
0584 int *ret = _ret;
0585
0586
0587 if (*ret)
0588 return;
0589
0590
0591 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
0592 if (!entry) {
0593 *ret = -ENOMEM;
0594 return;
0595 }
0596
0597
0598 memcpy(&entry->dh, dh, sizeof(*dh));
0599 entry->instance = instance_counts[dh->type]++;
0600 entry->position = position_count++;
0601
0602 entry->kobj.kset = dmi_kset;
0603 *ret = kobject_init_and_add(&entry->kobj, &dmi_sysfs_entry_ktype, NULL,
0604 "%d-%d", dh->type, entry->instance);
0605
0606 if (*ret) {
0607 kobject_put(&entry->kobj);
0608 return;
0609 }
0610
0611
0612 spin_lock(&entry_list_lock);
0613 list_add_tail(&entry->list, &entry_list);
0614 spin_unlock(&entry_list_lock);
0615
0616
0617 switch (dh->type) {
0618 case DMI_ENTRY_SYSTEM_EVENT_LOG:
0619 *ret = dmi_system_event_log(entry);
0620 break;
0621 default:
0622
0623 break;
0624 }
0625 if (*ret)
0626 goto out_err;
0627
0628
0629 *ret = sysfs_create_bin_file(&entry->kobj, &dmi_entry_raw_attr);
0630 if (*ret)
0631 goto out_err;
0632
0633 return;
0634 out_err:
0635 kobject_put(entry->child);
0636 kobject_put(&entry->kobj);
0637 return;
0638 }
0639
0640 static void cleanup_entry_list(void)
0641 {
0642 struct dmi_sysfs_entry *entry, *next;
0643
0644
0645 list_for_each_entry_safe(entry, next, &entry_list, list) {
0646 kobject_put(entry->child);
0647 kobject_put(&entry->kobj);
0648 }
0649 }
0650
0651 static int __init dmi_sysfs_init(void)
0652 {
0653 int error;
0654 int val;
0655
0656 if (!dmi_kobj) {
0657 pr_debug("dmi-sysfs: dmi entry is absent.\n");
0658 error = -ENODATA;
0659 goto err;
0660 }
0661
0662 dmi_kset = kset_create_and_add("entries", NULL, dmi_kobj);
0663 if (!dmi_kset) {
0664 error = -ENOMEM;
0665 goto err;
0666 }
0667
0668 val = 0;
0669 error = dmi_walk(dmi_sysfs_register_handle, &val);
0670 if (error)
0671 goto err;
0672 if (val) {
0673 error = val;
0674 goto err;
0675 }
0676
0677 pr_debug("dmi-sysfs: loaded.\n");
0678
0679 return 0;
0680 err:
0681 cleanup_entry_list();
0682 kset_unregister(dmi_kset);
0683 return error;
0684 }
0685
0686
0687 static void __exit dmi_sysfs_exit(void)
0688 {
0689 pr_debug("dmi-sysfs: unloading.\n");
0690 cleanup_entry_list();
0691 kset_unregister(dmi_kset);
0692 }
0693
0694 module_init(dmi_sysfs_init);
0695 module_exit(dmi_sysfs_exit);
0696
0697 MODULE_AUTHOR("Mike Waychison <mikew@google.com>");
0698 MODULE_DESCRIPTION("DMI sysfs support");
0699 MODULE_LICENSE("GPL");