Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at v4.17-rc5 648 lines 18 kB view raw
1/* 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 */ 15#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 16 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" 26 27static u32 da_supported_commands; 28static int da_num_tokens; 29static struct platform_device *platform_device; 30static struct calling_interface_token *da_tokens; 31static struct device_attribute *token_location_attrs; 32static struct device_attribute *token_value_attrs; 33static struct attribute **token_attrs; 34static DEFINE_MUTEX(smbios_mutex); 35 36struct smbios_device { 37 struct list_head list; 38 struct device *device; 39 int (*call_fn)(struct calling_interface_buffer *arg); 40}; 41 42struct smbios_call { 43 u32 need_capability; 44 int cmd_class; 45 int cmd_select; 46}; 47 48/* calls that are whitelisted for given capabilities */ 49static struct smbios_call call_whitelist[] = { 50 /* generally tokens are allowed, but may be further filtered or 51 * restricted by token blacklist or whitelist 52 */ 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}, 64}; 65 66/* calls that are explicitly blacklisted */ 67static 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}, 77}; 78 79struct token_range { 80 u32 need_capability; 81 u16 min; 82 u16 max; 83}; 84 85/* tokens that are whitelisted for given capabilities */ 86static 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} 91}; 92 93/* tokens that are explicitly blacklisted */ 94static 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}, 125}; 126 127static LIST_HEAD(smbios_device_list); 128 129int dell_smbios_error(int value) 130{ 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; 140 } 141} 142EXPORT_SYMBOL_GPL(dell_smbios_error); 143 144int dell_smbios_register_device(struct device *d, void *call_fn) 145{ 146 struct smbios_device *priv; 147 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; 159} 160EXPORT_SYMBOL_GPL(dell_smbios_register_device); 161 162void dell_smbios_unregister_device(struct device *d) 163{ 164 struct smbios_device *priv; 165 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; 172 } 173 } 174 mutex_unlock(&smbios_mutex); 175 dev_dbg(d, "Remove device: %s\n", d->driver->name); 176} 177EXPORT_SYMBOL_GPL(dell_smbios_unregister_device); 178 179int dell_smbios_call_filter(struct device *d, 180 struct calling_interface_buffer *buffer) 181{ 182 u16 t = 0; 183 int i; 184 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; 189 } 190 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; 196 } 197 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; 208 } 209 210 /* if a token call, find token ID */ 211 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; 221 } 222 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; 228 } 229 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; 237 } 238 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; 250 } 251 252 } 253 } 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; 265 } 266 dev_dbg(d, "missing capability %d for %u/%u\n", 267 call_whitelist[i].need_capability, 268 buffer->cmd_class, buffer->cmd_select); 269 270 } 271 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; 277 } 278 279 return -EACCES; 280} 281EXPORT_SYMBOL_GPL(dell_smbios_call_filter); 282 283int dell_smbios_call(struct calling_interface_buffer *buffer) 284{ 285 int (*call_fn)(struct calling_interface_buffer *) = NULL; 286 struct device *selected_dev = NULL; 287 struct smbios_device *priv; 288 int ret; 289 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; 297 } 298 } 299 300 if (!selected_dev) { 301 ret = -ENODEV; 302 pr_err("No dell-smbios drivers are loaded\n"); 303 goto out_smbios_call; 304 } 305 306 ret = call_fn(buffer); 307 308out_smbios_call: 309 mutex_unlock(&smbios_mutex); 310 return ret; 311} 312EXPORT_SYMBOL_GPL(dell_smbios_call); 313 314struct calling_interface_token *dell_smbios_find_token(int tokenid) 315{ 316 int i; 317 318 for (i = 0; i < da_num_tokens; i++) { 319 if (da_tokens[i].tokenID == tokenid) 320 return &da_tokens[i]; 321 } 322 323 return NULL; 324} 325EXPORT_SYMBOL_GPL(dell_smbios_find_token); 326 327static BLOCKING_NOTIFIER_HEAD(dell_laptop_chain_head); 328 329int dell_laptop_register_notifier(struct notifier_block *nb) 330{ 331 return blocking_notifier_chain_register(&dell_laptop_chain_head, nb); 332} 333EXPORT_SYMBOL_GPL(dell_laptop_register_notifier); 334 335int dell_laptop_unregister_notifier(struct notifier_block *nb) 336{ 337 return blocking_notifier_chain_unregister(&dell_laptop_chain_head, nb); 338} 339EXPORT_SYMBOL_GPL(dell_laptop_unregister_notifier); 340 341void dell_laptop_call_notifier(unsigned long action, void *data) 342{ 343 blocking_notifier_call_chain(&dell_laptop_chain_head, action, data); 344} 345EXPORT_SYMBOL_GPL(dell_laptop_call_notifier); 346 347static void __init parse_da_table(const struct dmi_header *dm) 348{ 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); 354 355 /* 356 * 4 bytes of table header, plus 7 bytes of Dell header 357 * plus at least 6 bytes of entry 358 */ 359 360 if (dm->length < 17) 361 return; 362 363 da_supported_commands = table->supportedCmds; 364 365 new_da_tokens = krealloc(da_tokens, (da_num_tokens + tokens) * 366 sizeof(struct calling_interface_token), 367 GFP_KERNEL); 368 369 if (!new_da_tokens) 370 return; 371 da_tokens = new_da_tokens; 372 373 memcpy(da_tokens+da_num_tokens, table->tokens, 374 sizeof(struct calling_interface_token) * tokens); 375 376 da_num_tokens += tokens; 377} 378 379static void zero_duplicates(struct device *dev) 380{ 381 int i, j; 382 383 for (i = 0; i < da_num_tokens; i++) { 384 if (da_tokens[i].tokenID == 0) 385 continue; 386 for (j = i+1; j < da_num_tokens; j++) { 387 if (da_tokens[j].tokenID == 0) 388 continue; 389 if (da_tokens[i].tokenID == da_tokens[j].tokenID) { 390 dev_dbg(dev, "Zeroing dup token ID %x(%x/%x)\n", 391 da_tokens[j].tokenID, 392 da_tokens[j].location, 393 da_tokens[j].value); 394 da_tokens[j].tokenID = 0; 395 } 396 } 397 } 398} 399 400static void __init find_tokens(const struct dmi_header *dm, void *dummy) 401{ 402 switch (dm->type) { 403 case 0xd4: /* Indexed IO */ 404 case 0xd5: /* Protected Area Type 1 */ 405 case 0xd6: /* Protected Area Type 2 */ 406 break; 407 case 0xda: /* Calling interface */ 408 parse_da_table(dm); 409 break; 410 } 411} 412 413static int match_attribute(struct device *dev, 414 struct device_attribute *attr) 415{ 416 int i; 417 418 for (i = 0; i < da_num_tokens * 2; i++) { 419 if (!token_attrs[i]) 420 continue; 421 if (strcmp(token_attrs[i]->name, attr->attr.name) == 0) 422 return i/2; 423 } 424 dev_dbg(dev, "couldn't match: %s\n", attr->attr.name); 425 return -EINVAL; 426} 427 428static ssize_t location_show(struct device *dev, 429 struct device_attribute *attr, char *buf) 430{ 431 int i; 432 433 if (!capable(CAP_SYS_ADMIN)) 434 return -EPERM; 435 436 i = match_attribute(dev, attr); 437 if (i > 0) 438 return scnprintf(buf, PAGE_SIZE, "%08x", da_tokens[i].location); 439 return 0; 440} 441 442static ssize_t value_show(struct device *dev, 443 struct device_attribute *attr, char *buf) 444{ 445 int i; 446 447 if (!capable(CAP_SYS_ADMIN)) 448 return -EPERM; 449 450 i = match_attribute(dev, attr); 451 if (i > 0) 452 return scnprintf(buf, PAGE_SIZE, "%08x", da_tokens[i].value); 453 return 0; 454} 455 456static struct attribute_group smbios_attribute_group = { 457 .name = "tokens" 458}; 459 460static struct platform_driver platform_driver = { 461 .driver = { 462 .name = "dell-smbios", 463 }, 464}; 465 466static int build_tokens_sysfs(struct platform_device *dev) 467{ 468 char *location_name; 469 char *value_name; 470 size_t size; 471 int ret; 472 int i, j; 473 474 /* (number of tokens + 1 for null terminated */ 475 size = sizeof(struct device_attribute) * (da_num_tokens + 1); 476 token_location_attrs = kzalloc(size, GFP_KERNEL); 477 if (!token_location_attrs) 478 return -ENOMEM; 479 token_value_attrs = kzalloc(size, GFP_KERNEL); 480 if (!token_value_attrs) 481 goto out_allocate_value; 482 483 /* need to store both location and value + terminator*/ 484 size = sizeof(struct attribute *) * ((2 * da_num_tokens) + 1); 485 token_attrs = kzalloc(size, GFP_KERNEL); 486 if (!token_attrs) 487 goto out_allocate_attrs; 488 489 for (i = 0, j = 0; i < da_num_tokens; i++) { 490 /* skip empty */ 491 if (da_tokens[i].tokenID == 0) 492 continue; 493 /* add location */ 494 location_name = kasprintf(GFP_KERNEL, "%04x_location", 495 da_tokens[i].tokenID); 496 if (location_name == NULL) 497 goto out_unwind_strings; 498 sysfs_attr_init(&token_location_attrs[i].attr); 499 token_location_attrs[i].attr.name = location_name; 500 token_location_attrs[i].attr.mode = 0444; 501 token_location_attrs[i].show = location_show; 502 token_attrs[j++] = &token_location_attrs[i].attr; 503 504 /* add value */ 505 value_name = kasprintf(GFP_KERNEL, "%04x_value", 506 da_tokens[i].tokenID); 507 if (value_name == NULL) 508 goto loop_fail_create_value; 509 sysfs_attr_init(&token_value_attrs[i].attr); 510 token_value_attrs[i].attr.name = value_name; 511 token_value_attrs[i].attr.mode = 0444; 512 token_value_attrs[i].show = value_show; 513 token_attrs[j++] = &token_value_attrs[i].attr; 514 continue; 515 516loop_fail_create_value: 517 kfree(location_name); 518 goto out_unwind_strings; 519 } 520 smbios_attribute_group.attrs = token_attrs; 521 522 ret = sysfs_create_group(&dev->dev.kobj, &smbios_attribute_group); 523 if (ret) 524 goto out_unwind_strings; 525 return 0; 526 527out_unwind_strings: 528 while (i--) { 529 kfree(token_location_attrs[i].attr.name); 530 kfree(token_value_attrs[i].attr.name); 531 } 532 kfree(token_attrs); 533out_allocate_attrs: 534 kfree(token_value_attrs); 535out_allocate_value: 536 kfree(token_location_attrs); 537 538 return -ENOMEM; 539} 540 541static void free_group(struct platform_device *pdev) 542{ 543 int i; 544 545 sysfs_remove_group(&pdev->dev.kobj, 546 &smbios_attribute_group); 547 for (i = 0; i < da_num_tokens; i++) { 548 kfree(token_location_attrs[i].attr.name); 549 kfree(token_value_attrs[i].attr.name); 550 } 551 kfree(token_attrs); 552 kfree(token_value_attrs); 553 kfree(token_location_attrs); 554} 555 556static int __init dell_smbios_init(void) 557{ 558 const struct dmi_device *valid; 559 int ret, wmi, smm; 560 561 valid = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, "Dell System", NULL); 562 if (!valid) { 563 pr_err("Unable to run on non-Dell system\n"); 564 return -ENODEV; 565 } 566 567 dmi_walk(find_tokens, NULL); 568 569 if (!da_tokens) { 570 pr_info("Unable to find dmi tokens\n"); 571 return -ENODEV; 572 } 573 574 ret = platform_driver_register(&platform_driver); 575 if (ret) 576 goto fail_platform_driver; 577 578 platform_device = platform_device_alloc("dell-smbios", 0); 579 if (!platform_device) { 580 ret = -ENOMEM; 581 goto fail_platform_device_alloc; 582 } 583 ret = platform_device_add(platform_device); 584 if (ret) 585 goto fail_platform_device_add; 586 587 /* duplicate tokens will cause problems building sysfs files */ 588 zero_duplicates(&platform_device->dev); 589 590 ret = build_tokens_sysfs(platform_device); 591 if (ret) 592 goto fail_create_group; 593 594 /* register backends */ 595 wmi = init_dell_smbios_wmi(); 596 if (wmi) 597 pr_debug("Failed to initialize WMI backend: %d\n", wmi); 598 smm = init_dell_smbios_smm(); 599 if (smm) 600 pr_debug("Failed to initialize SMM backend: %d\n", smm); 601 if (wmi && smm) { 602 pr_err("No SMBIOS backends available (wmi: %d, smm: %d)\n", 603 wmi, smm); 604 goto fail_sysfs; 605 } 606 607 return 0; 608 609fail_sysfs: 610 free_group(platform_device); 611 612fail_create_group: 613 platform_device_del(platform_device); 614 615fail_platform_device_add: 616 platform_device_put(platform_device); 617 618fail_platform_device_alloc: 619 platform_driver_unregister(&platform_driver); 620 621fail_platform_driver: 622 kfree(da_tokens); 623 return ret; 624} 625 626static void __exit dell_smbios_exit(void) 627{ 628 exit_dell_smbios_wmi(); 629 exit_dell_smbios_smm(); 630 mutex_lock(&smbios_mutex); 631 if (platform_device) { 632 free_group(platform_device); 633 platform_device_unregister(platform_device); 634 platform_driver_unregister(&platform_driver); 635 } 636 kfree(da_tokens); 637 mutex_unlock(&smbios_mutex); 638} 639 640module_init(dell_smbios_init); 641module_exit(dell_smbios_exit); 642 643MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>"); 644MODULE_AUTHOR("Gabriele Mazzotta <gabriele.mzt@gmail.com>"); 645MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>"); 646MODULE_AUTHOR("Mario Limonciello <mario.limonciello@dell.com>"); 647MODULE_DESCRIPTION("Common functions for kernel modules using Dell SMBIOS"); 648MODULE_LICENSE("GPL");