blob: 5a76d056b9d0d0b1fe9770d9d272413d054afb8b [file] [log] [blame]
Lennart Poettering4f5c7912007-05-08 22:07:02 +02001/*
2 * Export SMBIOS/DMI info via sysfs to userspace
3 *
4 * Copyright 2007, Lennart Poettering
5 *
6 * Licensed under GPLv2
7 */
8
9#include <linux/module.h>
10#include <linux/kernel.h>
11#include <linux/init.h>
12#include <linux/dmi.h>
13#include <linux/device.h>
Lennart Poettering4f5c7912007-05-08 22:07:02 +020014
Jean Delvare63744752007-08-13 11:24:33 +020015struct dmi_device_attribute{
16 struct device_attribute dev_attr;
17 int field;
18};
19#define to_dmi_dev_attr(_dev_attr) \
20 container_of(_dev_attr, struct dmi_device_attribute, dev_attr)
21
Jean Delvare63744752007-08-13 11:24:33 +020022static ssize_t sys_dmi_field_show(struct device *dev,
23 struct device_attribute *attr,
24 char *page)
25{
26 int field = to_dmi_dev_attr(attr)->field;
27 ssize_t len;
28 len = scnprintf(page, PAGE_SIZE, "%s\n", dmi_get_system_info(field));
29 page[len-1] = '\n';
30 return len;
31}
32
33#define DMI_ATTR(_name, _mode, _show, _field) \
34 { .dev_attr = __ATTR(_name, _mode, _show, NULL), \
35 .field = _field }
36
37#define DEFINE_DMI_ATTR_WITH_SHOW(_name, _mode, _field) \
38static struct dmi_device_attribute sys_dmi_##_name##_attr = \
39 DMI_ATTR(_name, _mode, sys_dmi_field_show, _field);
Lennart Poettering4f5c7912007-05-08 22:07:02 +020040
41DEFINE_DMI_ATTR_WITH_SHOW(bios_vendor, 0444, DMI_BIOS_VENDOR);
42DEFINE_DMI_ATTR_WITH_SHOW(bios_version, 0444, DMI_BIOS_VERSION);
43DEFINE_DMI_ATTR_WITH_SHOW(bios_date, 0444, DMI_BIOS_DATE);
44DEFINE_DMI_ATTR_WITH_SHOW(sys_vendor, 0444, DMI_SYS_VENDOR);
45DEFINE_DMI_ATTR_WITH_SHOW(product_name, 0444, DMI_PRODUCT_NAME);
46DEFINE_DMI_ATTR_WITH_SHOW(product_version, 0444, DMI_PRODUCT_VERSION);
47DEFINE_DMI_ATTR_WITH_SHOW(product_serial, 0400, DMI_PRODUCT_SERIAL);
48DEFINE_DMI_ATTR_WITH_SHOW(product_uuid, 0400, DMI_PRODUCT_UUID);
49DEFINE_DMI_ATTR_WITH_SHOW(board_vendor, 0444, DMI_BOARD_VENDOR);
50DEFINE_DMI_ATTR_WITH_SHOW(board_name, 0444, DMI_BOARD_NAME);
51DEFINE_DMI_ATTR_WITH_SHOW(board_version, 0444, DMI_BOARD_VERSION);
52DEFINE_DMI_ATTR_WITH_SHOW(board_serial, 0400, DMI_BOARD_SERIAL);
53DEFINE_DMI_ATTR_WITH_SHOW(board_asset_tag, 0444, DMI_BOARD_ASSET_TAG);
54DEFINE_DMI_ATTR_WITH_SHOW(chassis_vendor, 0444, DMI_CHASSIS_VENDOR);
55DEFINE_DMI_ATTR_WITH_SHOW(chassis_type, 0444, DMI_CHASSIS_TYPE);
56DEFINE_DMI_ATTR_WITH_SHOW(chassis_version, 0444, DMI_CHASSIS_VERSION);
57DEFINE_DMI_ATTR_WITH_SHOW(chassis_serial, 0400, DMI_CHASSIS_SERIAL);
58DEFINE_DMI_ATTR_WITH_SHOW(chassis_asset_tag, 0444, DMI_CHASSIS_ASSET_TAG);
59
60static void ascii_filter(char *d, const char *s)
61{
62 /* Filter out characters we don't want to see in the modalias string */
63 for (; *s; s++)
64 if (*s > ' ' && *s < 127 && *s != ':')
65 *(d++) = *s;
66
67 *d = 0;
68}
69
70static ssize_t get_modalias(char *buffer, size_t buffer_size)
71{
72 static const struct mafield {
73 const char *prefix;
74 int field;
75 } fields[] = {
76 { "bvn", DMI_BIOS_VENDOR },
77 { "bvr", DMI_BIOS_VERSION },
78 { "bd", DMI_BIOS_DATE },
79 { "svn", DMI_SYS_VENDOR },
80 { "pn", DMI_PRODUCT_NAME },
81 { "pvr", DMI_PRODUCT_VERSION },
82 { "rvn", DMI_BOARD_VENDOR },
83 { "rn", DMI_BOARD_NAME },
84 { "rvr", DMI_BOARD_VERSION },
85 { "cvn", DMI_CHASSIS_VENDOR },
86 { "ct", DMI_CHASSIS_TYPE },
87 { "cvr", DMI_CHASSIS_VERSION },
88 { NULL, DMI_NONE }
89 };
90
91 ssize_t l, left;
92 char *p;
93 const struct mafield *f;
94
95 strcpy(buffer, "dmi");
96 p = buffer + 3; left = buffer_size - 4;
97
98 for (f = fields; f->prefix && left > 0; f++) {
99 const char *c;
100 char *t;
101
102 c = dmi_get_system_info(f->field);
103 if (!c)
104 continue;
105
106 t = kmalloc(strlen(c) + 1, GFP_KERNEL);
107 if (!t)
108 break;
109 ascii_filter(t, c);
110 l = scnprintf(p, left, ":%s%s", f->prefix, t);
111 kfree(t);
112
113 p += l;
114 left -= l;
115 }
116
117 p[0] = ':';
118 p[1] = 0;
119
120 return p - buffer + 1;
121}
122
123static ssize_t sys_dmi_modalias_show(struct device *dev,
124 struct device_attribute *attr, char *page)
125{
126 ssize_t r;
127 r = get_modalias(page, PAGE_SIZE-1);
128 page[r] = '\n';
129 page[r+1] = 0;
130 return r+1;
131}
132
Jean Delvare34b51f32007-08-13 11:26:26 +0200133static struct device_attribute sys_dmi_modalias_attr =
134 __ATTR(modalias, 0444, sys_dmi_modalias_show, NULL);
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200135
136static struct attribute *sys_dmi_attributes[DMI_STRING_MAX+2];
137
138static struct attribute_group sys_dmi_attribute_group = {
139 .attrs = sys_dmi_attributes,
140};
141
142static struct attribute_group* sys_dmi_attribute_groups[] = {
143 &sys_dmi_attribute_group,
144 NULL
145};
146
Kay Sievers7eff2e72007-08-14 15:15:12 +0200147static int dmi_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200148{
Kay Sievers7eff2e72007-08-14 15:15:12 +0200149 ssize_t len;
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200150
Kay Sievers7eff2e72007-08-14 15:15:12 +0200151 if (add_uevent_var(env, "MODALIAS="))
152 return -ENOMEM;
153 len = get_modalias(&env->buf[env->buflen - 1],
154 sizeof(env->buf) - env->buflen);
155 if (len >= (sizeof(env->buf) - env->buflen))
156 return -ENOMEM;
157 env->buflen += len;
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200158 return 0;
159}
160
161static struct class dmi_class = {
162 .name = "dmi",
163 .dev_release = (void(*)(struct device *)) kfree,
164 .dev_uevent = dmi_dev_uevent,
165};
166
167static struct device *dmi_dev;
168
169/* Initialization */
170
171#define ADD_DMI_ATTR(_name, _field) \
172 if (dmi_get_system_info(_field)) \
Jean Delvare63744752007-08-13 11:24:33 +0200173 sys_dmi_attributes[i++] = &sys_dmi_##_name##_attr.dev_attr.attr;
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200174
Jean Delvarece8c6282008-01-08 15:33:08 -0800175/* In a separate function to keep gcc 3.2 happy - do NOT merge this in
176 dmi_id_init! */
177static void __init dmi_id_init_attr_table(void)
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200178{
Jean Delvarece8c6282008-01-08 15:33:08 -0800179 int i;
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200180
181 /* Not necessarily all DMI fields are available on all
182 * systems, hence let's built an attribute table of just
183 * what's available */
184 i = 0;
185 ADD_DMI_ATTR(bios_vendor, DMI_BIOS_VENDOR);
186 ADD_DMI_ATTR(bios_version, DMI_BIOS_VERSION);
187 ADD_DMI_ATTR(bios_date, DMI_BIOS_DATE);
188 ADD_DMI_ATTR(sys_vendor, DMI_SYS_VENDOR);
189 ADD_DMI_ATTR(product_name, DMI_PRODUCT_NAME);
190 ADD_DMI_ATTR(product_version, DMI_PRODUCT_VERSION);
191 ADD_DMI_ATTR(product_serial, DMI_PRODUCT_SERIAL);
192 ADD_DMI_ATTR(product_uuid, DMI_PRODUCT_UUID);
193 ADD_DMI_ATTR(board_vendor, DMI_BOARD_VENDOR);
194 ADD_DMI_ATTR(board_name, DMI_BOARD_NAME);
195 ADD_DMI_ATTR(board_version, DMI_BOARD_VERSION);
196 ADD_DMI_ATTR(board_serial, DMI_BOARD_SERIAL);
197 ADD_DMI_ATTR(board_asset_tag, DMI_BOARD_ASSET_TAG);
198 ADD_DMI_ATTR(chassis_vendor, DMI_CHASSIS_VENDOR);
199 ADD_DMI_ATTR(chassis_type, DMI_CHASSIS_TYPE);
200 ADD_DMI_ATTR(chassis_version, DMI_CHASSIS_VERSION);
201 ADD_DMI_ATTR(chassis_serial, DMI_CHASSIS_SERIAL);
202 ADD_DMI_ATTR(chassis_asset_tag, DMI_CHASSIS_ASSET_TAG);
203 sys_dmi_attributes[i++] = &sys_dmi_modalias_attr.attr;
Jean Delvarece8c6282008-01-08 15:33:08 -0800204}
205
206static int __init dmi_id_init(void)
207{
208 int ret;
209
210 if (!dmi_available)
211 return -ENODEV;
212
213 dmi_id_init_attr_table();
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200214
215 ret = class_register(&dmi_class);
216 if (ret)
217 return ret;
218
219 dmi_dev = kzalloc(sizeof(*dmi_dev), GFP_KERNEL);
220 if (!dmi_dev) {
221 ret = -ENOMEM;
222 goto fail_class_unregister;
223 }
224
225 dmi_dev->class = &dmi_class;
Kay Sievers6ff4dd32009-01-06 10:44:42 -0800226 dev_set_name(dmi_dev, "id");
Lennart Poettering4f5c7912007-05-08 22:07:02 +0200227 dmi_dev->groups = sys_dmi_attribute_groups;
228
229 ret = device_register(dmi_dev);
230 if (ret)
231 goto fail_class_unregister;
232
233 return 0;
234
235fail_class_unregister:
236
237 class_unregister(&dmi_class);
238
239 return ret;
240}
241
242arch_initcall(dmi_id_init);