blob: 2229d44cb92c271fdf5e5e61d0aad739433fea8d [file] [log] [blame]
Michał Kępień2f9f26b2016-01-22 15:27:13 +01001/*
2 * Common functions for kernel modules using Dell SMBIOS
3 *
4 * Copyright (c) Red Hat <mjg@redhat.com>
5 * Copyright (c) 2014 Gabriele Mazzotta <gabriele.mzt@gmail.com>
6 * Copyright (c) 2014 Pali Rohár <pali.rohar@gmail.com>
7 *
8 * Based on documentation in the libsmbios package:
9 * Copyright (C) 2005-2014 Dell Inc.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
14 */
Mario Limonciello7b4dd2162017-09-26 13:50:02 -050015#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Michał Kępień2f9f26b2016-01-22 15:27:13 +010016
17#include <linux/kernel.h>
18#include <linux/module.h>
Mario Limonciello33b9ca12017-11-01 14:25:30 -050019#include <linux/capability.h>
Michał Kępień2f9f26b2016-01-22 15:27:13 +010020#include <linux/dmi.h>
Michał Kępieńe8edf532016-03-04 14:09:06 +010021#include <linux/err.h>
Michał Kępień2f9f26b2016-01-22 15:27:13 +010022#include <linux/mutex.h>
Mario Limonciello33b9ca12017-11-01 14:25:30 -050023#include <linux/platform_device.h>
Michał Kępień2f9f26b2016-01-22 15:27:13 +010024#include <linux/slab.h>
Michał Kępień2f9f26b2016-01-22 15:27:13 +010025#include "dell-smbios.h"
26
Michał Kępień2f9f26b2016-01-22 15:27:13 +010027static int da_num_tokens;
Mario Limonciello33b9ca12017-11-01 14:25:30 -050028static struct platform_device *platform_device;
Michał Kępieńb7bca2d2016-01-22 15:27:26 +010029static struct calling_interface_token *da_tokens;
Mario Limonciello33b9ca12017-11-01 14:25:30 -050030static struct device_attribute *token_location_attrs;
31static struct device_attribute *token_value_attrs;
32static struct attribute **token_attrs;
Mario Limonciello549b4932017-11-01 14:25:31 -050033static DEFINE_MUTEX(smbios_mutex);
34
35struct smbios_device {
36 struct list_head list;
37 struct device *device;
38 int (*call_fn)(struct calling_interface_buffer *);
39};
40
41static LIST_HEAD(smbios_device_list);
Michał Kępień2f9f26b2016-01-22 15:27:13 +010042
Michał Kępień0db21802016-03-04 14:09:07 +010043int dell_smbios_error(int value)
Michał Kępieńe8edf532016-03-04 14:09:06 +010044{
45 switch (value) {
46 case 0: /* Completed successfully */
47 return 0;
48 case -1: /* Completed with error */
49 return -EIO;
50 case -2: /* Function not supported */
51 return -ENXIO;
52 default: /* Unknown error */
53 return -EINVAL;
54 }
55}
Michał Kępień0db21802016-03-04 14:09:07 +010056EXPORT_SYMBOL_GPL(dell_smbios_error);
Michał Kępieńe8edf532016-03-04 14:09:06 +010057
Mario Limonciello549b4932017-11-01 14:25:31 -050058int dell_smbios_register_device(struct device *d, void *call_fn)
Michał Kępień2f9f26b2016-01-22 15:27:13 +010059{
Mario Limonciello549b4932017-11-01 14:25:31 -050060 struct smbios_device *priv;
Michał Kępień2f9f26b2016-01-22 15:27:13 +010061
Mario Limonciello549b4932017-11-01 14:25:31 -050062 priv = devm_kzalloc(d, sizeof(struct smbios_device), GFP_KERNEL);
63 if (!priv)
64 return -ENOMEM;
65 get_device(d);
66 priv->device = d;
67 priv->call_fn = call_fn;
68 mutex_lock(&smbios_mutex);
69 list_add_tail(&priv->list, &smbios_device_list);
70 mutex_unlock(&smbios_mutex);
71 dev_dbg(d, "Added device: %s\n", d->driver->name);
72 return 0;
73}
74EXPORT_SYMBOL_GPL(dell_smbios_register_device);
75
76void dell_smbios_unregister_device(struct device *d)
Michał Kępień2f9f26b2016-01-22 15:27:13 +010077{
Mario Limonciello549b4932017-11-01 14:25:31 -050078 struct smbios_device *priv;
Michał Kępień2f9f26b2016-01-22 15:27:13 +010079
Mario Limonciello549b4932017-11-01 14:25:31 -050080 mutex_lock(&smbios_mutex);
81 list_for_each_entry(priv, &smbios_device_list, list) {
82 if (priv->device == d) {
83 list_del(&priv->list);
84 put_device(d);
85 break;
86 }
87 }
88 mutex_unlock(&smbios_mutex);
89 dev_dbg(d, "Remove device: %s\n", d->driver->name);
90}
91EXPORT_SYMBOL_GPL(dell_smbios_unregister_device);
92
93int dell_smbios_call(struct calling_interface_buffer *buffer)
Michał Kępień2f9f26b2016-01-22 15:27:13 +010094{
Mario Limonciello549b4932017-11-01 14:25:31 -050095 int (*call_fn)(struct calling_interface_buffer *) = NULL;
96 struct device *selected_dev = NULL;
97 struct smbios_device *priv;
98 int ret;
99
100 mutex_lock(&smbios_mutex);
101 list_for_each_entry(priv, &smbios_device_list, list) {
102 if (!selected_dev || priv->device->id >= selected_dev->id) {
103 dev_dbg(priv->device, "Trying device ID: %d\n",
104 priv->device->id);
105 call_fn = priv->call_fn;
106 selected_dev = priv->device;
107 }
108 }
109
110 if (!selected_dev) {
111 ret = -ENODEV;
112 pr_err("No dell-smbios drivers are loaded\n");
113 goto out_smbios_call;
114 }
115
116 ret = call_fn(buffer);
117
118out_smbios_call:
119 mutex_unlock(&smbios_mutex);
120 return ret;
Michał Kępień2f9f26b2016-01-22 15:27:13 +0100121}
Mario Limonciello549b4932017-11-01 14:25:31 -0500122EXPORT_SYMBOL_GPL(dell_smbios_call);
Michał Kępień2f9f26b2016-01-22 15:27:13 +0100123
Michał Kępień96f7ef92016-01-22 15:27:22 +0100124struct calling_interface_token *dell_smbios_find_token(int tokenid)
125{
126 int i;
127
128 for (i = 0; i < da_num_tokens; i++) {
129 if (da_tokens[i].tokenID == tokenid)
130 return &da_tokens[i];
131 }
132
133 return NULL;
134}
135EXPORT_SYMBOL_GPL(dell_smbios_find_token);
136
Hans de Goede504b0252017-03-16 11:55:32 +0100137static BLOCKING_NOTIFIER_HEAD(dell_laptop_chain_head);
138
139int dell_laptop_register_notifier(struct notifier_block *nb)
140{
141 return blocking_notifier_chain_register(&dell_laptop_chain_head, nb);
142}
143EXPORT_SYMBOL_GPL(dell_laptop_register_notifier);
144
145int dell_laptop_unregister_notifier(struct notifier_block *nb)
146{
147 return blocking_notifier_chain_unregister(&dell_laptop_chain_head, nb);
148}
149EXPORT_SYMBOL_GPL(dell_laptop_unregister_notifier);
150
151void dell_laptop_call_notifier(unsigned long action, void *data)
152{
153 blocking_notifier_call_chain(&dell_laptop_chain_head, action, data);
154}
155EXPORT_SYMBOL_GPL(dell_laptop_call_notifier);
156
Michał Kępień2f9f26b2016-01-22 15:27:13 +0100157static void __init parse_da_table(const struct dmi_header *dm)
158{
159 /* Final token is a terminator, so we don't want to copy it */
160 int tokens = (dm->length-11)/sizeof(struct calling_interface_token)-1;
161 struct calling_interface_token *new_da_tokens;
162 struct calling_interface_structure *table =
163 container_of(dm, struct calling_interface_structure, header);
164
165 /* 4 bytes of table header, plus 7 bytes of Dell header, plus at least
166 6 bytes of entry */
167
168 if (dm->length < 17)
169 return;
170
Michał Kępień2f9f26b2016-01-22 15:27:13 +0100171 new_da_tokens = krealloc(da_tokens, (da_num_tokens + tokens) *
172 sizeof(struct calling_interface_token),
173 GFP_KERNEL);
174
175 if (!new_da_tokens)
176 return;
177 da_tokens = new_da_tokens;
178
179 memcpy(da_tokens+da_num_tokens, table->tokens,
180 sizeof(struct calling_interface_token) * tokens);
181
182 da_num_tokens += tokens;
183}
184
Mario Limonciello33b9ca12017-11-01 14:25:30 -0500185static void zero_duplicates(struct device *dev)
186{
187 int i, j;
188
189 for (i = 0; i < da_num_tokens; i++) {
190 if (da_tokens[i].tokenID == 0)
191 continue;
192 for (j = i+1; j < da_num_tokens; j++) {
193 if (da_tokens[j].tokenID == 0)
194 continue;
195 if (da_tokens[i].tokenID == da_tokens[j].tokenID) {
196 dev_dbg(dev, "Zeroing dup token ID %x(%x/%x)\n",
197 da_tokens[j].tokenID,
198 da_tokens[j].location,
199 da_tokens[j].value);
200 da_tokens[j].tokenID = 0;
201 }
202 }
203 }
204}
205
Michał Kępień2f9f26b2016-01-22 15:27:13 +0100206static void __init find_tokens(const struct dmi_header *dm, void *dummy)
207{
208 switch (dm->type) {
209 case 0xd4: /* Indexed IO */
210 case 0xd5: /* Protected Area Type 1 */
211 case 0xd6: /* Protected Area Type 2 */
212 break;
213 case 0xda: /* Calling interface */
214 parse_da_table(dm);
215 break;
216 }
217}
218
Mario Limonciello33b9ca12017-11-01 14:25:30 -0500219static int match_attribute(struct device *dev,
220 struct device_attribute *attr)
221{
222 int i;
223
224 for (i = 0; i < da_num_tokens * 2; i++) {
225 if (!token_attrs[i])
226 continue;
227 if (strcmp(token_attrs[i]->name, attr->attr.name) == 0)
228 return i/2;
229 }
230 dev_dbg(dev, "couldn't match: %s\n", attr->attr.name);
231 return -EINVAL;
232}
233
234static ssize_t location_show(struct device *dev,
235 struct device_attribute *attr, char *buf)
236{
237 int i;
238
239 if (!capable(CAP_SYS_ADMIN))
240 return -EPERM;
241
242 i = match_attribute(dev, attr);
243 if (i > 0)
244 return scnprintf(buf, PAGE_SIZE, "%08x", da_tokens[i].location);
245 return 0;
246}
247
248static ssize_t value_show(struct device *dev,
249 struct device_attribute *attr, char *buf)
250{
251 int i;
252
253 if (!capable(CAP_SYS_ADMIN))
254 return -EPERM;
255
256 i = match_attribute(dev, attr);
257 if (i > 0)
258 return scnprintf(buf, PAGE_SIZE, "%08x", da_tokens[i].value);
259 return 0;
260}
261
262static struct attribute_group smbios_attribute_group = {
263 .name = "tokens"
264};
265
266static struct platform_driver platform_driver = {
267 .driver = {
268 .name = "dell-smbios",
269 },
270};
271
272static int build_tokens_sysfs(struct platform_device *dev)
273{
274 char buffer_location[13];
275 char buffer_value[10];
276 char *location_name;
277 char *value_name;
278 size_t size;
279 int ret;
280 int i, j;
281
282 /* (number of tokens + 1 for null terminated */
283 size = sizeof(struct device_attribute) * (da_num_tokens + 1);
284 token_location_attrs = kzalloc(size, GFP_KERNEL);
285 if (!token_location_attrs)
286 return -ENOMEM;
287 token_value_attrs = kzalloc(size, GFP_KERNEL);
288 if (!token_value_attrs)
289 goto out_allocate_value;
290
291 /* need to store both location and value + terminator*/
292 size = sizeof(struct attribute *) * ((2 * da_num_tokens) + 1);
293 token_attrs = kzalloc(size, GFP_KERNEL);
294 if (!token_attrs)
295 goto out_allocate_attrs;
296
297 for (i = 0, j = 0; i < da_num_tokens; i++) {
298 /* skip empty */
299 if (da_tokens[i].tokenID == 0)
300 continue;
301 /* add location */
302 sprintf(buffer_location, "%04x_location",
303 da_tokens[i].tokenID);
304 location_name = kstrdup(buffer_location, GFP_KERNEL);
305 if (location_name == NULL)
306 goto out_unwind_strings;
307 sysfs_attr_init(&token_location_attrs[i].attr);
308 token_location_attrs[i].attr.name = location_name;
309 token_location_attrs[i].attr.mode = 0444;
310 token_location_attrs[i].show = location_show;
311 token_attrs[j++] = &token_location_attrs[i].attr;
312
313 /* add value */
314 sprintf(buffer_value, "%04x_value",
315 da_tokens[i].tokenID);
316 value_name = kstrdup(buffer_value, GFP_KERNEL);
317 if (value_name == NULL)
318 goto loop_fail_create_value;
319 sysfs_attr_init(&token_value_attrs[i].attr);
320 token_value_attrs[i].attr.name = value_name;
321 token_value_attrs[i].attr.mode = 0444;
322 token_value_attrs[i].show = value_show;
323 token_attrs[j++] = &token_value_attrs[i].attr;
324 continue;
325
326loop_fail_create_value:
327 kfree(value_name);
328 goto out_unwind_strings;
329 }
330 smbios_attribute_group.attrs = token_attrs;
331
332 ret = sysfs_create_group(&dev->dev.kobj, &smbios_attribute_group);
333 if (ret)
334 goto out_unwind_strings;
335 return 0;
336
337out_unwind_strings:
338 for (i = i-1; i > 0; i--) {
339 kfree(token_location_attrs[i].attr.name);
340 kfree(token_value_attrs[i].attr.name);
341 }
342 kfree(token_attrs);
343out_allocate_attrs:
344 kfree(token_value_attrs);
345out_allocate_value:
346 kfree(token_location_attrs);
347
348 return -ENOMEM;
349}
350
351static void free_group(struct platform_device *pdev)
352{
353 int i;
354
355 sysfs_remove_group(&pdev->dev.kobj,
356 &smbios_attribute_group);
357 for (i = 0; i < da_num_tokens; i++) {
358 kfree(token_location_attrs[i].attr.name);
359 kfree(token_value_attrs[i].attr.name);
360 }
361 kfree(token_attrs);
362 kfree(token_value_attrs);
363 kfree(token_location_attrs);
364}
365
Michał Kępień2f9f26b2016-01-22 15:27:13 +0100366static int __init dell_smbios_init(void)
367{
Mario Limonciello980f4812017-11-01 14:25:29 -0500368 const struct dmi_device *valid;
Michał Kępień2f9f26b2016-01-22 15:27:13 +0100369 int ret;
370
Mario Limonciello980f4812017-11-01 14:25:29 -0500371 valid = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, "Dell System", NULL);
372 if (!valid) {
373 pr_err("Unable to run on non-Dell system\n");
374 return -ENODEV;
375 }
376
Michał Kępień2f9f26b2016-01-22 15:27:13 +0100377 dmi_walk(find_tokens, NULL);
378
379 if (!da_tokens) {
380 pr_info("Unable to find dmi tokens\n");
381 return -ENODEV;
382 }
383
Mario Limonciello33b9ca12017-11-01 14:25:30 -0500384 ret = platform_driver_register(&platform_driver);
385 if (ret)
386 goto fail_platform_driver;
387
388 platform_device = platform_device_alloc("dell-smbios", 0);
389 if (!platform_device) {
390 ret = -ENOMEM;
391 goto fail_platform_device_alloc;
392 }
393 ret = platform_device_add(platform_device);
394 if (ret)
395 goto fail_platform_device_add;
396
397 /* duplicate tokens will cause problems building sysfs files */
398 zero_duplicates(&platform_device->dev);
399
400 ret = build_tokens_sysfs(platform_device);
401 if (ret)
402 goto fail_create_group;
Michał Kępień2f9f26b2016-01-22 15:27:13 +0100403
404 return 0;
405
Mario Limonciello33b9ca12017-11-01 14:25:30 -0500406fail_create_group:
407 platform_device_del(platform_device);
408
409fail_platform_device_add:
410 platform_device_put(platform_device);
411
412fail_platform_device_alloc:
413 platform_driver_unregister(&platform_driver);
414
415fail_platform_driver:
Michał Kępień2f9f26b2016-01-22 15:27:13 +0100416 kfree(da_tokens);
417 return ret;
418}
419
420static void __exit dell_smbios_exit(void)
421{
Mario Limonciello549b4932017-11-01 14:25:31 -0500422 mutex_lock(&smbios_mutex);
Mario Limonciello33b9ca12017-11-01 14:25:30 -0500423 if (platform_device) {
424 free_group(platform_device);
425 platform_device_unregister(platform_device);
426 platform_driver_unregister(&platform_driver);
427 }
Mario Limonciello33b9ca12017-11-01 14:25:30 -0500428 kfree(da_tokens);
Mario Limonciello549b4932017-11-01 14:25:31 -0500429 mutex_unlock(&smbios_mutex);
Michał Kępień2f9f26b2016-01-22 15:27:13 +0100430}
431
432subsys_initcall(dell_smbios_init);
433module_exit(dell_smbios_exit);
434
435MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
436MODULE_AUTHOR("Gabriele Mazzotta <gabriele.mzt@gmail.com>");
437MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
438MODULE_DESCRIPTION("Common functions for kernel modules using Dell SMBIOS");
439MODULE_LICENSE("GPL");