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