x86/topology: Fix function name in documentation
[cris-mirror.git] / drivers / platform / x86 / dell-smbios.c
blob8541cde4cb7d3c4febde205b639428b89215bdc0
1 /*
2 * Common functions for kernel modules using Dell SMBIOS
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>
8 * Based on documentation in the libsmbios package:
9 * Copyright (C) 2005-2014 Dell Inc.
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.
15 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/capability.h>
20 #include <linux/dmi.h>
21 #include <linux/err.h>
22 #include <linux/mutex.h>
23 #include <linux/platform_device.h>
24 #include <linux/slab.h>
25 #include "dell-smbios.h"
27 static u32 da_supported_commands;
28 static int da_num_tokens;
29 static struct platform_device *platform_device;
30 static struct calling_interface_token *da_tokens;
31 static struct device_attribute *token_location_attrs;
32 static struct device_attribute *token_value_attrs;
33 static struct attribute **token_attrs;
34 static DEFINE_MUTEX(smbios_mutex);
36 struct smbios_device {
37 struct list_head list;
38 struct device *device;
39 int (*call_fn)(struct calling_interface_buffer *);
42 struct smbios_call {
43 u32 need_capability;
44 int cmd_class;
45 int cmd_select;
48 /* calls that are whitelisted for given capabilities */
49 static struct smbios_call call_whitelist[] = {
50 /* generally tokens are allowed, but may be further filtered or
51 * restricted by token blacklist or whitelist
53 {CAP_SYS_ADMIN, CLASS_TOKEN_READ, SELECT_TOKEN_STD},
54 {CAP_SYS_ADMIN, CLASS_TOKEN_READ, SELECT_TOKEN_AC},
55 {CAP_SYS_ADMIN, CLASS_TOKEN_READ, SELECT_TOKEN_BAT},
56 {CAP_SYS_ADMIN, CLASS_TOKEN_WRITE, SELECT_TOKEN_STD},
57 {CAP_SYS_ADMIN, CLASS_TOKEN_WRITE, SELECT_TOKEN_AC},
58 {CAP_SYS_ADMIN, CLASS_TOKEN_WRITE, SELECT_TOKEN_BAT},
59 /* used by userspace: fwupdate */
60 {CAP_SYS_ADMIN, CLASS_ADMIN_PROP, SELECT_ADMIN_PROP},
61 /* used by userspace: fwupd */
62 {CAP_SYS_ADMIN, CLASS_INFO, SELECT_DOCK},
63 {CAP_SYS_ADMIN, CLASS_FLASH_INTERFACE, SELECT_FLASH_INTERFACE},
66 /* calls that are explicitly blacklisted */
67 static struct smbios_call call_blacklist[] = {
68 {0x0000, 1, 7}, /* manufacturing use */
69 {0x0000, 6, 5}, /* manufacturing use */
70 {0x0000, 11, 3}, /* write once */
71 {0x0000, 11, 7}, /* write once */
72 {0x0000, 11, 11}, /* write once */
73 {0x0000, 19, -1}, /* diagnostics */
74 /* handled by kernel: dell-laptop */
75 {0x0000, CLASS_INFO, SELECT_RFKILL},
76 {0x0000, CLASS_KBD_BACKLIGHT, SELECT_KBD_BACKLIGHT},
79 struct token_range {
80 u32 need_capability;
81 u16 min;
82 u16 max;
85 /* tokens that are whitelisted for given capabilities */
86 static struct token_range token_whitelist[] = {
87 /* used by userspace: fwupdate */
88 {CAP_SYS_ADMIN, CAPSULE_EN_TOKEN, CAPSULE_DIS_TOKEN},
89 /* can indicate to userspace that WMI is needed */
90 {0x0000, WSMT_EN_TOKEN, WSMT_DIS_TOKEN}
93 /* tokens that are explicitly blacklisted */
94 static struct token_range token_blacklist[] = {
95 {0x0000, 0x0058, 0x0059}, /* ME use */
96 {0x0000, 0x00CD, 0x00D0}, /* raid shadow copy */
97 {0x0000, 0x013A, 0x01FF}, /* sata shadow copy */
98 {0x0000, 0x0175, 0x0176}, /* write once */
99 {0x0000, 0x0195, 0x0197}, /* diagnostics */
100 {0x0000, 0x01DC, 0x01DD}, /* manufacturing use */
101 {0x0000, 0x027D, 0x0284}, /* diagnostics */
102 {0x0000, 0x02E3, 0x02E3}, /* manufacturing use */
103 {0x0000, 0x02FF, 0x02FF}, /* manufacturing use */
104 {0x0000, 0x0300, 0x0302}, /* manufacturing use */
105 {0x0000, 0x0325, 0x0326}, /* manufacturing use */
106 {0x0000, 0x0332, 0x0335}, /* fan control */
107 {0x0000, 0x0350, 0x0350}, /* manufacturing use */
108 {0x0000, 0x0363, 0x0363}, /* manufacturing use */
109 {0x0000, 0x0368, 0x0368}, /* manufacturing use */
110 {0x0000, 0x03F6, 0x03F7}, /* manufacturing use */
111 {0x0000, 0x049E, 0x049F}, /* manufacturing use */
112 {0x0000, 0x04A0, 0x04A3}, /* disagnostics */
113 {0x0000, 0x04E6, 0x04E7}, /* manufacturing use */
114 {0x0000, 0x4000, 0x7FFF}, /* internal BIOS use */
115 {0x0000, 0x9000, 0x9001}, /* internal BIOS use */
116 {0x0000, 0xA000, 0xBFFF}, /* write only */
117 {0x0000, 0xEFF0, 0xEFFF}, /* internal BIOS use */
118 /* handled by kernel: dell-laptop */
119 {0x0000, BRIGHTNESS_TOKEN, BRIGHTNESS_TOKEN},
120 {0x0000, KBD_LED_OFF_TOKEN, KBD_LED_AUTO_TOKEN},
121 {0x0000, KBD_LED_AC_TOKEN, KBD_LED_AC_TOKEN},
122 {0x0000, KBD_LED_AUTO_25_TOKEN, KBD_LED_AUTO_75_TOKEN},
123 {0x0000, KBD_LED_AUTO_100_TOKEN, KBD_LED_AUTO_100_TOKEN},
124 {0x0000, GLOBAL_MIC_MUTE_ENABLE, GLOBAL_MIC_MUTE_DISABLE},
127 static LIST_HEAD(smbios_device_list);
129 int dell_smbios_error(int value)
131 switch (value) {
132 case 0: /* Completed successfully */
133 return 0;
134 case -1: /* Completed with error */
135 return -EIO;
136 case -2: /* Function not supported */
137 return -ENXIO;
138 default: /* Unknown error */
139 return -EINVAL;
142 EXPORT_SYMBOL_GPL(dell_smbios_error);
144 int dell_smbios_register_device(struct device *d, void *call_fn)
146 struct smbios_device *priv;
148 priv = devm_kzalloc(d, sizeof(struct smbios_device), GFP_KERNEL);
149 if (!priv)
150 return -ENOMEM;
151 get_device(d);
152 priv->device = d;
153 priv->call_fn = call_fn;
154 mutex_lock(&smbios_mutex);
155 list_add_tail(&priv->list, &smbios_device_list);
156 mutex_unlock(&smbios_mutex);
157 dev_dbg(d, "Added device: %s\n", d->driver->name);
158 return 0;
160 EXPORT_SYMBOL_GPL(dell_smbios_register_device);
162 void dell_smbios_unregister_device(struct device *d)
164 struct smbios_device *priv;
166 mutex_lock(&smbios_mutex);
167 list_for_each_entry(priv, &smbios_device_list, list) {
168 if (priv->device == d) {
169 list_del(&priv->list);
170 put_device(d);
171 break;
174 mutex_unlock(&smbios_mutex);
175 dev_dbg(d, "Remove device: %s\n", d->driver->name);
177 EXPORT_SYMBOL_GPL(dell_smbios_unregister_device);
179 int dell_smbios_call_filter(struct device *d,
180 struct calling_interface_buffer *buffer)
182 u16 t = 0;
183 int i;
185 /* can't make calls over 30 */
186 if (buffer->cmd_class > 30) {
187 dev_dbg(d, "class too big: %u\n", buffer->cmd_class);
188 return -EINVAL;
191 /* supported calls on the particular system */
192 if (!(da_supported_commands & (1 << buffer->cmd_class))) {
193 dev_dbg(d, "invalid command, supported commands: 0x%8x\n",
194 da_supported_commands);
195 return -EINVAL;
198 /* match against call blacklist */
199 for (i = 0; i < ARRAY_SIZE(call_blacklist); i++) {
200 if (buffer->cmd_class != call_blacklist[i].cmd_class)
201 continue;
202 if (buffer->cmd_select != call_blacklist[i].cmd_select &&
203 call_blacklist[i].cmd_select != -1)
204 continue;
205 dev_dbg(d, "blacklisted command: %u/%u\n",
206 buffer->cmd_class, buffer->cmd_select);
207 return -EINVAL;
210 /* if a token call, find token ID */
212 if ((buffer->cmd_class == CLASS_TOKEN_READ ||
213 buffer->cmd_class == CLASS_TOKEN_WRITE) &&
214 buffer->cmd_select < 3) {
215 /* find the matching token ID */
216 for (i = 0; i < da_num_tokens; i++) {
217 if (da_tokens[i].location != buffer->input[0])
218 continue;
219 t = da_tokens[i].tokenID;
220 break;
223 /* token call; but token didn't exist */
224 if (!t) {
225 dev_dbg(d, "token at location %04x doesn't exist\n",
226 buffer->input[0]);
227 return -EINVAL;
230 /* match against token blacklist */
231 for (i = 0; i < ARRAY_SIZE(token_blacklist); i++) {
232 if (!token_blacklist[i].min || !token_blacklist[i].max)
233 continue;
234 if (t >= token_blacklist[i].min &&
235 t <= token_blacklist[i].max)
236 return -EINVAL;
239 /* match against token whitelist */
240 for (i = 0; i < ARRAY_SIZE(token_whitelist); i++) {
241 if (!token_whitelist[i].min || !token_whitelist[i].max)
242 continue;
243 if (t < token_whitelist[i].min ||
244 t > token_whitelist[i].max)
245 continue;
246 if (!token_whitelist[i].need_capability ||
247 capable(token_whitelist[i].need_capability)) {
248 dev_dbg(d, "whitelisted token: %x\n", t);
249 return 0;
254 /* match against call whitelist */
255 for (i = 0; i < ARRAY_SIZE(call_whitelist); i++) {
256 if (buffer->cmd_class != call_whitelist[i].cmd_class)
257 continue;
258 if (buffer->cmd_select != call_whitelist[i].cmd_select)
259 continue;
260 if (!call_whitelist[i].need_capability ||
261 capable(call_whitelist[i].need_capability)) {
262 dev_dbg(d, "whitelisted capable command: %u/%u\n",
263 buffer->cmd_class, buffer->cmd_select);
264 return 0;
266 dev_dbg(d, "missing capability %d for %u/%u\n",
267 call_whitelist[i].need_capability,
268 buffer->cmd_class, buffer->cmd_select);
272 /* not in a whitelist, only allow processes with capabilities */
273 if (capable(CAP_SYS_RAWIO)) {
274 dev_dbg(d, "Allowing %u/%u due to CAP_SYS_RAWIO\n",
275 buffer->cmd_class, buffer->cmd_select);
276 return 0;
279 return -EACCES;
281 EXPORT_SYMBOL_GPL(dell_smbios_call_filter);
283 int dell_smbios_call(struct calling_interface_buffer *buffer)
285 int (*call_fn)(struct calling_interface_buffer *) = NULL;
286 struct device *selected_dev = NULL;
287 struct smbios_device *priv;
288 int ret;
290 mutex_lock(&smbios_mutex);
291 list_for_each_entry(priv, &smbios_device_list, list) {
292 if (!selected_dev || priv->device->id >= selected_dev->id) {
293 dev_dbg(priv->device, "Trying device ID: %d\n",
294 priv->device->id);
295 call_fn = priv->call_fn;
296 selected_dev = priv->device;
300 if (!selected_dev) {
301 ret = -ENODEV;
302 pr_err("No dell-smbios drivers are loaded\n");
303 goto out_smbios_call;
306 ret = call_fn(buffer);
308 out_smbios_call:
309 mutex_unlock(&smbios_mutex);
310 return ret;
312 EXPORT_SYMBOL_GPL(dell_smbios_call);
314 struct calling_interface_token *dell_smbios_find_token(int tokenid)
316 int i;
318 for (i = 0; i < da_num_tokens; i++) {
319 if (da_tokens[i].tokenID == tokenid)
320 return &da_tokens[i];
323 return NULL;
325 EXPORT_SYMBOL_GPL(dell_smbios_find_token);
327 static BLOCKING_NOTIFIER_HEAD(dell_laptop_chain_head);
329 int dell_laptop_register_notifier(struct notifier_block *nb)
331 return blocking_notifier_chain_register(&dell_laptop_chain_head, nb);
333 EXPORT_SYMBOL_GPL(dell_laptop_register_notifier);
335 int dell_laptop_unregister_notifier(struct notifier_block *nb)
337 return blocking_notifier_chain_unregister(&dell_laptop_chain_head, nb);
339 EXPORT_SYMBOL_GPL(dell_laptop_unregister_notifier);
341 void dell_laptop_call_notifier(unsigned long action, void *data)
343 blocking_notifier_call_chain(&dell_laptop_chain_head, action, data);
345 EXPORT_SYMBOL_GPL(dell_laptop_call_notifier);
347 static void __init parse_da_table(const struct dmi_header *dm)
349 /* Final token is a terminator, so we don't want to copy it */
350 int tokens = (dm->length-11)/sizeof(struct calling_interface_token)-1;
351 struct calling_interface_token *new_da_tokens;
352 struct calling_interface_structure *table =
353 container_of(dm, struct calling_interface_structure, header);
355 /* 4 bytes of table header, plus 7 bytes of Dell header, plus at least
356 6 bytes of entry */
358 if (dm->length < 17)
359 return;
361 da_supported_commands = table->supportedCmds;
363 new_da_tokens = krealloc(da_tokens, (da_num_tokens + tokens) *
364 sizeof(struct calling_interface_token),
365 GFP_KERNEL);
367 if (!new_da_tokens)
368 return;
369 da_tokens = new_da_tokens;
371 memcpy(da_tokens+da_num_tokens, table->tokens,
372 sizeof(struct calling_interface_token) * tokens);
374 da_num_tokens += tokens;
377 static void zero_duplicates(struct device *dev)
379 int i, j;
381 for (i = 0; i < da_num_tokens; i++) {
382 if (da_tokens[i].tokenID == 0)
383 continue;
384 for (j = i+1; j < da_num_tokens; j++) {
385 if (da_tokens[j].tokenID == 0)
386 continue;
387 if (da_tokens[i].tokenID == da_tokens[j].tokenID) {
388 dev_dbg(dev, "Zeroing dup token ID %x(%x/%x)\n",
389 da_tokens[j].tokenID,
390 da_tokens[j].location,
391 da_tokens[j].value);
392 da_tokens[j].tokenID = 0;
398 static void __init find_tokens(const struct dmi_header *dm, void *dummy)
400 switch (dm->type) {
401 case 0xd4: /* Indexed IO */
402 case 0xd5: /* Protected Area Type 1 */
403 case 0xd6: /* Protected Area Type 2 */
404 break;
405 case 0xda: /* Calling interface */
406 parse_da_table(dm);
407 break;
411 static int match_attribute(struct device *dev,
412 struct device_attribute *attr)
414 int i;
416 for (i = 0; i < da_num_tokens * 2; i++) {
417 if (!token_attrs[i])
418 continue;
419 if (strcmp(token_attrs[i]->name, attr->attr.name) == 0)
420 return i/2;
422 dev_dbg(dev, "couldn't match: %s\n", attr->attr.name);
423 return -EINVAL;
426 static ssize_t location_show(struct device *dev,
427 struct device_attribute *attr, char *buf)
429 int i;
431 if (!capable(CAP_SYS_ADMIN))
432 return -EPERM;
434 i = match_attribute(dev, attr);
435 if (i > 0)
436 return scnprintf(buf, PAGE_SIZE, "%08x", da_tokens[i].location);
437 return 0;
440 static ssize_t value_show(struct device *dev,
441 struct device_attribute *attr, char *buf)
443 int i;
445 if (!capable(CAP_SYS_ADMIN))
446 return -EPERM;
448 i = match_attribute(dev, attr);
449 if (i > 0)
450 return scnprintf(buf, PAGE_SIZE, "%08x", da_tokens[i].value);
451 return 0;
454 static struct attribute_group smbios_attribute_group = {
455 .name = "tokens"
458 static struct platform_driver platform_driver = {
459 .driver = {
460 .name = "dell-smbios",
464 static int build_tokens_sysfs(struct platform_device *dev)
466 char *location_name;
467 char *value_name;
468 size_t size;
469 int ret;
470 int i, j;
472 /* (number of tokens + 1 for null terminated */
473 size = sizeof(struct device_attribute) * (da_num_tokens + 1);
474 token_location_attrs = kzalloc(size, GFP_KERNEL);
475 if (!token_location_attrs)
476 return -ENOMEM;
477 token_value_attrs = kzalloc(size, GFP_KERNEL);
478 if (!token_value_attrs)
479 goto out_allocate_value;
481 /* need to store both location and value + terminator*/
482 size = sizeof(struct attribute *) * ((2 * da_num_tokens) + 1);
483 token_attrs = kzalloc(size, GFP_KERNEL);
484 if (!token_attrs)
485 goto out_allocate_attrs;
487 for (i = 0, j = 0; i < da_num_tokens; i++) {
488 /* skip empty */
489 if (da_tokens[i].tokenID == 0)
490 continue;
491 /* add location */
492 location_name = kasprintf(GFP_KERNEL, "%04x_location",
493 da_tokens[i].tokenID);
494 if (location_name == NULL)
495 goto out_unwind_strings;
496 sysfs_attr_init(&token_location_attrs[i].attr);
497 token_location_attrs[i].attr.name = location_name;
498 token_location_attrs[i].attr.mode = 0444;
499 token_location_attrs[i].show = location_show;
500 token_attrs[j++] = &token_location_attrs[i].attr;
502 /* add value */
503 value_name = kasprintf(GFP_KERNEL, "%04x_value",
504 da_tokens[i].tokenID);
505 if (value_name == NULL)
506 goto loop_fail_create_value;
507 sysfs_attr_init(&token_value_attrs[i].attr);
508 token_value_attrs[i].attr.name = value_name;
509 token_value_attrs[i].attr.mode = 0444;
510 token_value_attrs[i].show = value_show;
511 token_attrs[j++] = &token_value_attrs[i].attr;
512 continue;
514 loop_fail_create_value:
515 kfree(value_name);
516 goto out_unwind_strings;
518 smbios_attribute_group.attrs = token_attrs;
520 ret = sysfs_create_group(&dev->dev.kobj, &smbios_attribute_group);
521 if (ret)
522 goto out_unwind_strings;
523 return 0;
525 out_unwind_strings:
526 for (i = i-1; i > 0; i--) {
527 kfree(token_location_attrs[i].attr.name);
528 kfree(token_value_attrs[i].attr.name);
530 kfree(token_attrs);
531 out_allocate_attrs:
532 kfree(token_value_attrs);
533 out_allocate_value:
534 kfree(token_location_attrs);
536 return -ENOMEM;
539 static void free_group(struct platform_device *pdev)
541 int i;
543 sysfs_remove_group(&pdev->dev.kobj,
544 &smbios_attribute_group);
545 for (i = 0; i < da_num_tokens; i++) {
546 kfree(token_location_attrs[i].attr.name);
547 kfree(token_value_attrs[i].attr.name);
549 kfree(token_attrs);
550 kfree(token_value_attrs);
551 kfree(token_location_attrs);
554 static int __init dell_smbios_init(void)
556 const struct dmi_device *valid;
557 int ret;
559 valid = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, "Dell System", NULL);
560 if (!valid) {
561 pr_err("Unable to run on non-Dell system\n");
562 return -ENODEV;
565 dmi_walk(find_tokens, NULL);
567 if (!da_tokens) {
568 pr_info("Unable to find dmi tokens\n");
569 return -ENODEV;
572 ret = platform_driver_register(&platform_driver);
573 if (ret)
574 goto fail_platform_driver;
576 platform_device = platform_device_alloc("dell-smbios", 0);
577 if (!platform_device) {
578 ret = -ENOMEM;
579 goto fail_platform_device_alloc;
581 ret = platform_device_add(platform_device);
582 if (ret)
583 goto fail_platform_device_add;
585 /* duplicate tokens will cause problems building sysfs files */
586 zero_duplicates(&platform_device->dev);
588 ret = build_tokens_sysfs(platform_device);
589 if (ret)
590 goto fail_create_group;
592 return 0;
594 fail_create_group:
595 platform_device_del(platform_device);
597 fail_platform_device_add:
598 platform_device_put(platform_device);
600 fail_platform_device_alloc:
601 platform_driver_unregister(&platform_driver);
603 fail_platform_driver:
604 kfree(da_tokens);
605 return ret;
608 static void __exit dell_smbios_exit(void)
610 mutex_lock(&smbios_mutex);
611 if (platform_device) {
612 free_group(platform_device);
613 platform_device_unregister(platform_device);
614 platform_driver_unregister(&platform_driver);
616 kfree(da_tokens);
617 mutex_unlock(&smbios_mutex);
620 subsys_initcall(dell_smbios_init);
621 module_exit(dell_smbios_exit);
623 MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
624 MODULE_AUTHOR("Gabriele Mazzotta <gabriele.mzt@gmail.com>");
625 MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
626 MODULE_DESCRIPTION("Common functions for kernel modules using Dell SMBIOS");
627 MODULE_LICENSE("GPL");