1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Linux driver for WMI sensor information on Dell notebooks. 4 * 5 * Copyright (C) 2022 Armin Wolf <W_Armin@gmx.de> 6 */ 7 8 #define pr_format(fmt) KBUILD_MODNAME ": " fmt 9 10 #include <linux/acpi.h> 11 #include <linux/debugfs.h> 12 #include <linux/device.h> 13 #include <linux/device/driver.h> 14 #include <linux/dev_printk.h> 15 #include <linux/errno.h> 16 #include <linux/kconfig.h> 17 #include <linux/kernel.h> 18 #include <linux/hwmon.h> 19 #include <linux/kstrtox.h> 20 #include <linux/math.h> 21 #include <linux/math64.h> 22 #include <linux/module.h> 23 #include <linux/mutex.h> 24 #include <linux/limits.h> 25 #include <linux/pm.h> 26 #include <linux/power_supply.h> 27 #include <linux/printk.h> 28 #include <linux/seq_file.h> 29 #include <linux/sysfs.h> 30 #include <linux/types.h> 31 #include <linux/wmi.h> 32 33 #include <acpi/battery.h> 34 35 #include <asm/unaligned.h> 36 37 #define DRIVER_NAME "dell-wmi-ddv" 38 39 #define DELL_DDV_SUPPORTED_VERSION_MIN 2 40 #define DELL_DDV_SUPPORTED_VERSION_MAX 3 41 #define DELL_DDV_GUID "8A42EA14-4F2A-FD45-6422-0087F7A7E608" 42 43 #define DELL_EPPID_LENGTH 20 44 #define DELL_EPPID_EXT_LENGTH 23 45 46 static bool force; 47 module_param_unsafe(force, bool, 0); 48 MODULE_PARM_DESC(force, "Force loading without checking for supported WMI interface versions"); 49 50 enum dell_ddv_method { 51 DELL_DDV_BATTERY_DESIGN_CAPACITY = 0x01, 52 DELL_DDV_BATTERY_FULL_CHARGE_CAPACITY = 0x02, 53 DELL_DDV_BATTERY_MANUFACTURE_NAME = 0x03, 54 DELL_DDV_BATTERY_MANUFACTURE_DATE = 0x04, 55 DELL_DDV_BATTERY_SERIAL_NUMBER = 0x05, 56 DELL_DDV_BATTERY_CHEMISTRY_VALUE = 0x06, 57 DELL_DDV_BATTERY_TEMPERATURE = 0x07, 58 DELL_DDV_BATTERY_CURRENT = 0x08, 59 DELL_DDV_BATTERY_VOLTAGE = 0x09, 60 DELL_DDV_BATTERY_MANUFACTURER_ACCESS = 0x0A, 61 DELL_DDV_BATTERY_RELATIVE_CHARGE_STATE = 0x0B, 62 DELL_DDV_BATTERY_CYCLE_COUNT = 0x0C, 63 DELL_DDV_BATTERY_EPPID = 0x0D, 64 DELL_DDV_BATTERY_RAW_ANALYTICS_START = 0x0E, 65 DELL_DDV_BATTERY_RAW_ANALYTICS = 0x0F, 66 DELL_DDV_BATTERY_DESIGN_VOLTAGE = 0x10, 67 DELL_DDV_BATTERY_RAW_ANALYTICS_A_BLOCK = 0x11, /* version 3 */ 68 69 DELL_DDV_INTERFACE_VERSION = 0x12, 70 71 DELL_DDV_FAN_SENSOR_INFORMATION = 0x20, 72 DELL_DDV_THERMAL_SENSOR_INFORMATION = 0x22, 73 }; 74 75 struct fan_sensor_entry { 76 u8 type; 77 __le16 rpm; 78 } __packed; 79 80 struct thermal_sensor_entry { 81 u8 type; 82 s8 now; 83 s8 min; 84 s8 max; 85 u8 unknown; 86 } __packed; 87 88 struct combined_channel_info { 89 struct hwmon_channel_info info; 90 u32 config[]; 91 }; 92 93 struct combined_chip_info { 94 struct hwmon_chip_info chip; 95 const struct hwmon_channel_info *info[]; 96 }; 97 98 struct dell_wmi_ddv_sensors { 99 struct mutex lock; /* protect caching */ 100 unsigned long timestamp; 101 union acpi_object *obj; 102 u64 entries; 103 }; 104 105 struct dell_wmi_ddv_data { 106 struct acpi_battery_hook hook; 107 struct device_attribute temp_attr; 108 struct device_attribute eppid_attr; 109 struct dell_wmi_ddv_sensors fans; 110 struct dell_wmi_ddv_sensors temps; 111 struct wmi_device *wdev; 112 }; 113 114 static const char * const fan_labels[] = { 115 "CPU Fan", 116 "Chassis Motherboard Fan", 117 "Video Fan", 118 "Power Supply Fan", 119 "Chipset Fan", 120 "Memory Fan", 121 "PCI Fan", 122 "HDD Fan", 123 }; 124 125 static const char * const fan_dock_labels[] = { 126 "Docking Chassis/Motherboard Fan", 127 "Docking Video Fan", 128 "Docking Power Supply Fan", 129 "Docking Chipset Fan", 130 }; 131 132 static int dell_wmi_ddv_query_type(struct wmi_device *wdev, enum dell_ddv_method method, u32 arg, 133 union acpi_object **result, acpi_object_type type) 134 { 135 struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL }; 136 const struct acpi_buffer in = { 137 .length = sizeof(arg), 138 .pointer = &arg, 139 }; 140 union acpi_object *obj; 141 acpi_status ret; 142 143 ret = wmidev_evaluate_method(wdev, 0x0, method, &in, &out); 144 if (ACPI_FAILURE(ret)) 145 return -EIO; 146 147 obj = out.pointer; 148 if (!obj) 149 return -ENODATA; 150 151 if (obj->type != type) { 152 kfree(obj); 153 return -ENOMSG; 154 } 155 156 *result = obj; 157 158 return 0; 159 } 160 161 static int dell_wmi_ddv_query_integer(struct wmi_device *wdev, enum dell_ddv_method method, 162 u32 arg, u32 *res) 163 { 164 union acpi_object *obj; 165 int ret; 166 167 ret = dell_wmi_ddv_query_type(wdev, method, arg, &obj, ACPI_TYPE_INTEGER); 168 if (ret < 0) 169 return ret; 170 171 if (obj->integer.value <= U32_MAX) 172 *res = (u32)obj->integer.value; 173 else 174 ret = -ERANGE; 175 176 kfree(obj); 177 178 return ret; 179 } 180 181 static int dell_wmi_ddv_query_buffer(struct wmi_device *wdev, enum dell_ddv_method method, 182 u32 arg, union acpi_object **result) 183 { 184 union acpi_object *obj; 185 u64 buffer_size; 186 int ret; 187 188 ret = dell_wmi_ddv_query_type(wdev, method, arg, &obj, ACPI_TYPE_PACKAGE); 189 if (ret < 0) 190 return ret; 191 192 if (obj->package.count != 2 || 193 obj->package.elements[0].type != ACPI_TYPE_INTEGER || 194 obj->package.elements[1].type != ACPI_TYPE_BUFFER) { 195 ret = -ENOMSG; 196 197 goto err_free; 198 } 199 200 buffer_size = obj->package.elements[0].integer.value; 201 202 if (!buffer_size) { 203 ret = -ENODATA; 204 205 goto err_free; 206 } 207 208 if (buffer_size > obj->package.elements[1].buffer.length) { 209 dev_warn(&wdev->dev, 210 FW_WARN "WMI buffer size (%llu) exceeds ACPI buffer size (%d)\n", 211 buffer_size, obj->package.elements[1].buffer.length); 212 ret = -EMSGSIZE; 213 214 goto err_free; 215 } 216 217 *result = obj; 218 219 return 0; 220 221 err_free: 222 kfree(obj); 223 224 return ret; 225 } 226 227 static int dell_wmi_ddv_query_string(struct wmi_device *wdev, enum dell_ddv_method method, 228 u32 arg, union acpi_object **result) 229 { 230 return dell_wmi_ddv_query_type(wdev, method, arg, result, ACPI_TYPE_STRING); 231 } 232 233 /* 234 * Needs to be called with lock held, except during initialization. 235 */ 236 static int dell_wmi_ddv_update_sensors(struct wmi_device *wdev, enum dell_ddv_method method, 237 struct dell_wmi_ddv_sensors *sensors, size_t entry_size) 238 { 239 u64 buffer_size, rem, entries; 240 union acpi_object *obj; 241 u8 *buffer; 242 int ret; 243 244 if (sensors->obj) { 245 if (time_before(jiffies, sensors->timestamp + HZ)) 246 return 0; 247 248 kfree(sensors->obj); 249 sensors->obj = NULL; 250 } 251 252 ret = dell_wmi_ddv_query_buffer(wdev, method, 0, &obj); 253 if (ret < 0) 254 return ret; 255 256 /* buffer format sanity check */ 257 buffer_size = obj->package.elements[0].integer.value; 258 buffer = obj->package.elements[1].buffer.pointer; 259 entries = div64_u64_rem(buffer_size, entry_size, &rem); 260 if (rem != 1 || buffer[buffer_size - 1] != 0xff) { 261 ret = -ENOMSG; 262 goto err_free; 263 } 264 265 if (!entries) { 266 ret = -ENODATA; 267 goto err_free; 268 } 269 270 sensors->obj = obj; 271 sensors->entries = entries; 272 sensors->timestamp = jiffies; 273 274 return 0; 275 276 err_free: 277 kfree(obj); 278 279 return ret; 280 } 281 282 static umode_t dell_wmi_ddv_is_visible(const void *drvdata, enum hwmon_sensor_types type, u32 attr, 283 int channel) 284 { 285 return 0444; 286 } 287 288 static int dell_wmi_ddv_fan_read_channel(struct dell_wmi_ddv_data *data, u32 attr, int channel, 289 long *val) 290 { 291 struct fan_sensor_entry *entry; 292 int ret; 293 294 ret = dell_wmi_ddv_update_sensors(data->wdev, DELL_DDV_FAN_SENSOR_INFORMATION, 295 &data->fans, sizeof(*entry)); 296 if (ret < 0) 297 return ret; 298 299 if (channel >= data->fans.entries) 300 return -ENXIO; 301 302 entry = (struct fan_sensor_entry *)data->fans.obj->package.elements[1].buffer.pointer; 303 switch (attr) { 304 case hwmon_fan_input: 305 *val = get_unaligned_le16(&entry[channel].rpm); 306 return 0; 307 default: 308 break; 309 } 310 311 return -EOPNOTSUPP; 312 } 313 314 static int dell_wmi_ddv_temp_read_channel(struct dell_wmi_ddv_data *data, u32 attr, int channel, 315 long *val) 316 { 317 struct thermal_sensor_entry *entry; 318 int ret; 319 320 ret = dell_wmi_ddv_update_sensors(data->wdev, DELL_DDV_THERMAL_SENSOR_INFORMATION, 321 &data->temps, sizeof(*entry)); 322 if (ret < 0) 323 return ret; 324 325 if (channel >= data->temps.entries) 326 return -ENXIO; 327 328 entry = (struct thermal_sensor_entry *)data->temps.obj->package.elements[1].buffer.pointer; 329 switch (attr) { 330 case hwmon_temp_input: 331 *val = entry[channel].now * 1000; 332 return 0; 333 case hwmon_temp_min: 334 *val = entry[channel].min * 1000; 335 return 0; 336 case hwmon_temp_max: 337 *val = entry[channel].max * 1000; 338 return 0; 339 default: 340 break; 341 } 342 343 return -EOPNOTSUPP; 344 } 345 346 static int dell_wmi_ddv_read(struct device *dev, enum hwmon_sensor_types type, u32 attr, 347 int channel, long *val) 348 { 349 struct dell_wmi_ddv_data *data = dev_get_drvdata(dev); 350 int ret; 351 352 switch (type) { 353 case hwmon_fan: 354 mutex_lock(&data->fans.lock); 355 ret = dell_wmi_ddv_fan_read_channel(data, attr, channel, val); 356 mutex_unlock(&data->fans.lock); 357 return ret; 358 case hwmon_temp: 359 mutex_lock(&data->temps.lock); 360 ret = dell_wmi_ddv_temp_read_channel(data, attr, channel, val); 361 mutex_unlock(&data->temps.lock); 362 return ret; 363 default: 364 break; 365 } 366 367 return -EOPNOTSUPP; 368 } 369 370 static int dell_wmi_ddv_fan_read_string(struct dell_wmi_ddv_data *data, int channel, 371 const char **str) 372 { 373 struct fan_sensor_entry *entry; 374 int ret; 375 u8 type; 376 377 ret = dell_wmi_ddv_update_sensors(data->wdev, DELL_DDV_FAN_SENSOR_INFORMATION, 378 &data->fans, sizeof(*entry)); 379 if (ret < 0) 380 return ret; 381 382 if (channel >= data->fans.entries) 383 return -ENXIO; 384 385 entry = (struct fan_sensor_entry *)data->fans.obj->package.elements[1].buffer.pointer; 386 type = entry[channel].type; 387 switch (type) { 388 case 0x00 ... 0x07: 389 *str = fan_labels[type]; 390 break; 391 case 0x11 ... 0x14: 392 *str = fan_dock_labels[type - 0x11]; 393 break; 394 default: 395 *str = "Unknown Fan"; 396 break; 397 } 398 399 return 0; 400 } 401 402 static int dell_wmi_ddv_temp_read_string(struct dell_wmi_ddv_data *data, int channel, 403 const char **str) 404 { 405 struct thermal_sensor_entry *entry; 406 int ret; 407 408 ret = dell_wmi_ddv_update_sensors(data->wdev, DELL_DDV_THERMAL_SENSOR_INFORMATION, 409 &data->temps, sizeof(*entry)); 410 if (ret < 0) 411 return ret; 412 413 if (channel >= data->temps.entries) 414 return -ENXIO; 415 416 entry = (struct thermal_sensor_entry *)data->temps.obj->package.elements[1].buffer.pointer; 417 switch (entry[channel].type) { 418 case 0x00: 419 *str = "CPU"; 420 break; 421 case 0x11: 422 *str = "Video"; 423 break; 424 case 0x22: 425 *str = "Memory"; /* sometimes called DIMM */ 426 break; 427 case 0x33: 428 *str = "Other"; 429 break; 430 case 0x44: 431 *str = "Ambient"; /* sometimes called SKIN */ 432 break; 433 case 0x52: 434 *str = "SODIMM"; 435 break; 436 case 0x55: 437 *str = "HDD"; 438 break; 439 case 0x62: 440 *str = "SODIMM 2"; 441 break; 442 case 0x73: 443 *str = "NB"; 444 break; 445 case 0x83: 446 *str = "Charger"; 447 break; 448 case 0xbb: 449 *str = "Memory 3"; 450 break; 451 default: 452 *str = "Unknown"; 453 break; 454 } 455 456 return 0; 457 } 458 459 static int dell_wmi_ddv_read_string(struct device *dev, enum hwmon_sensor_types type, u32 attr, 460 int channel, const char **str) 461 { 462 struct dell_wmi_ddv_data *data = dev_get_drvdata(dev); 463 int ret; 464 465 switch (type) { 466 case hwmon_fan: 467 switch (attr) { 468 case hwmon_fan_label: 469 mutex_lock(&data->fans.lock); 470 ret = dell_wmi_ddv_fan_read_string(data, channel, str); 471 mutex_unlock(&data->fans.lock); 472 return ret; 473 default: 474 break; 475 } 476 break; 477 case hwmon_temp: 478 switch (attr) { 479 case hwmon_temp_label: 480 mutex_lock(&data->temps.lock); 481 ret = dell_wmi_ddv_temp_read_string(data, channel, str); 482 mutex_unlock(&data->temps.lock); 483 return ret; 484 default: 485 break; 486 } 487 break; 488 default: 489 break; 490 } 491 492 return -EOPNOTSUPP; 493 } 494 495 static const struct hwmon_ops dell_wmi_ddv_ops = { 496 .is_visible = dell_wmi_ddv_is_visible, 497 .read = dell_wmi_ddv_read, 498 .read_string = dell_wmi_ddv_read_string, 499 }; 500 501 static struct hwmon_channel_info *dell_wmi_ddv_channel_create(struct device *dev, u64 count, 502 enum hwmon_sensor_types type, 503 u32 config) 504 { 505 struct combined_channel_info *cinfo; 506 int i; 507 508 cinfo = devm_kzalloc(dev, struct_size(cinfo, config, count + 1), GFP_KERNEL); 509 if (!cinfo) 510 return ERR_PTR(-ENOMEM); 511 512 cinfo->info.type = type; 513 cinfo->info.config = cinfo->config; 514 515 for (i = 0; i < count; i++) 516 cinfo->config[i] = config; 517 518 return &cinfo->info; 519 } 520 521 static void dell_wmi_ddv_hwmon_cache_invalidate(struct dell_wmi_ddv_sensors *sensors) 522 { 523 mutex_lock(&sensors->lock); 524 kfree(sensors->obj); 525 sensors->obj = NULL; 526 mutex_unlock(&sensors->lock); 527 } 528 529 static void dell_wmi_ddv_hwmon_cache_destroy(void *data) 530 { 531 struct dell_wmi_ddv_sensors *sensors = data; 532 533 mutex_destroy(&sensors->lock); 534 kfree(sensors->obj); 535 } 536 537 static struct hwmon_channel_info *dell_wmi_ddv_channel_init(struct wmi_device *wdev, 538 enum dell_ddv_method method, 539 struct dell_wmi_ddv_sensors *sensors, 540 size_t entry_size, 541 enum hwmon_sensor_types type, 542 u32 config) 543 { 544 struct hwmon_channel_info *info; 545 int ret; 546 547 ret = dell_wmi_ddv_update_sensors(wdev, method, sensors, entry_size); 548 if (ret < 0) 549 return ERR_PTR(ret); 550 551 mutex_init(&sensors->lock); 552 553 ret = devm_add_action_or_reset(&wdev->dev, dell_wmi_ddv_hwmon_cache_destroy, sensors); 554 if (ret < 0) 555 return ERR_PTR(ret); 556 557 info = dell_wmi_ddv_channel_create(&wdev->dev, sensors->entries, type, config); 558 if (IS_ERR(info)) 559 devm_release_action(&wdev->dev, dell_wmi_ddv_hwmon_cache_destroy, sensors); 560 561 return info; 562 } 563 564 static int dell_wmi_ddv_hwmon_add(struct dell_wmi_ddv_data *data) 565 { 566 struct wmi_device *wdev = data->wdev; 567 struct combined_chip_info *cinfo; 568 struct hwmon_channel_info *info; 569 struct device *hdev; 570 int index = 0; 571 int ret; 572 573 if (!devres_open_group(&wdev->dev, dell_wmi_ddv_hwmon_add, GFP_KERNEL)) 574 return -ENOMEM; 575 576 cinfo = devm_kzalloc(&wdev->dev, struct_size(cinfo, info, 4), GFP_KERNEL); 577 if (!cinfo) { 578 ret = -ENOMEM; 579 580 goto err_release; 581 } 582 583 cinfo->chip.ops = &dell_wmi_ddv_ops; 584 cinfo->chip.info = cinfo->info; 585 586 info = dell_wmi_ddv_channel_create(&wdev->dev, 1, hwmon_chip, HWMON_C_REGISTER_TZ); 587 if (IS_ERR(info)) { 588 ret = PTR_ERR(info); 589 590 goto err_release; 591 } 592 593 cinfo->info[index] = info; 594 index++; 595 596 info = dell_wmi_ddv_channel_init(wdev, DELL_DDV_FAN_SENSOR_INFORMATION, &data->fans, 597 sizeof(struct fan_sensor_entry), hwmon_fan, 598 (HWMON_F_INPUT | HWMON_F_LABEL)); 599 if (!IS_ERR(info)) { 600 cinfo->info[index] = info; 601 index++; 602 } 603 604 info = dell_wmi_ddv_channel_init(wdev, DELL_DDV_THERMAL_SENSOR_INFORMATION, &data->temps, 605 sizeof(struct thermal_sensor_entry), hwmon_temp, 606 (HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX | 607 HWMON_T_LABEL)); 608 if (!IS_ERR(info)) { 609 cinfo->info[index] = info; 610 index++; 611 } 612 613 if (index < 2) { 614 ret = -ENODEV; 615 616 goto err_release; 617 } 618 619 hdev = devm_hwmon_device_register_with_info(&wdev->dev, "dell_ddv", data, &cinfo->chip, 620 NULL); 621 if (IS_ERR(hdev)) { 622 ret = PTR_ERR(hdev); 623 624 goto err_release; 625 } 626 627 devres_close_group(&wdev->dev, dell_wmi_ddv_hwmon_add); 628 629 return 0; 630 631 err_release: 632 devres_release_group(&wdev->dev, dell_wmi_ddv_hwmon_add); 633 634 return ret; 635 } 636 637 static int dell_wmi_ddv_battery_index(struct acpi_device *acpi_dev, u32 *index) 638 { 639 const char *uid_str; 640 641 uid_str = acpi_device_uid(acpi_dev); 642 if (!uid_str) 643 return -ENODEV; 644 645 return kstrtou32(uid_str, 10, index); 646 } 647 648 static ssize_t temp_show(struct device *dev, struct device_attribute *attr, char *buf) 649 { 650 struct dell_wmi_ddv_data *data = container_of(attr, struct dell_wmi_ddv_data, temp_attr); 651 u32 index, value; 652 int ret; 653 654 ret = dell_wmi_ddv_battery_index(to_acpi_device(dev->parent), &index); 655 if (ret < 0) 656 return ret; 657 658 ret = dell_wmi_ddv_query_integer(data->wdev, DELL_DDV_BATTERY_TEMPERATURE, index, &value); 659 if (ret < 0) 660 return ret; 661 662 return sysfs_emit(buf, "%d\n", DIV_ROUND_CLOSEST(value, 10)); 663 } 664 665 static ssize_t eppid_show(struct device *dev, struct device_attribute *attr, char *buf) 666 { 667 struct dell_wmi_ddv_data *data = container_of(attr, struct dell_wmi_ddv_data, eppid_attr); 668 union acpi_object *obj; 669 u32 index; 670 int ret; 671 672 ret = dell_wmi_ddv_battery_index(to_acpi_device(dev->parent), &index); 673 if (ret < 0) 674 return ret; 675 676 ret = dell_wmi_ddv_query_string(data->wdev, DELL_DDV_BATTERY_EPPID, index, &obj); 677 if (ret < 0) 678 return ret; 679 680 if (obj->string.length != DELL_EPPID_LENGTH && obj->string.length != DELL_EPPID_EXT_LENGTH) 681 dev_info_once(&data->wdev->dev, FW_INFO "Suspicious ePPID length (%d)\n", 682 obj->string.length); 683 684 ret = sysfs_emit(buf, "%s\n", obj->string.pointer); 685 686 kfree(obj); 687 688 return ret; 689 } 690 691 static int dell_wmi_ddv_add_battery(struct power_supply *battery, struct acpi_battery_hook *hook) 692 { 693 struct dell_wmi_ddv_data *data = container_of(hook, struct dell_wmi_ddv_data, hook); 694 u32 index; 695 int ret; 696 697 /* Return 0 instead of error to avoid being unloaded */ 698 ret = dell_wmi_ddv_battery_index(to_acpi_device(battery->dev.parent), &index); 699 if (ret < 0) 700 return 0; 701 702 ret = device_create_file(&battery->dev, &data->temp_attr); 703 if (ret < 0) 704 return ret; 705 706 ret = device_create_file(&battery->dev, &data->eppid_attr); 707 if (ret < 0) { 708 device_remove_file(&battery->dev, &data->temp_attr); 709 710 return ret; 711 } 712 713 return 0; 714 } 715 716 static int dell_wmi_ddv_remove_battery(struct power_supply *battery, struct acpi_battery_hook *hook) 717 { 718 struct dell_wmi_ddv_data *data = container_of(hook, struct dell_wmi_ddv_data, hook); 719 720 device_remove_file(&battery->dev, &data->temp_attr); 721 device_remove_file(&battery->dev, &data->eppid_attr); 722 723 return 0; 724 } 725 726 static void dell_wmi_ddv_battery_remove(void *data) 727 { 728 struct acpi_battery_hook *hook = data; 729 730 battery_hook_unregister(hook); 731 } 732 733 static int dell_wmi_ddv_battery_add(struct dell_wmi_ddv_data *data) 734 { 735 data->hook.name = "Dell DDV Battery Extension"; 736 data->hook.add_battery = dell_wmi_ddv_add_battery; 737 data->hook.remove_battery = dell_wmi_ddv_remove_battery; 738 739 sysfs_attr_init(&data->temp_attr.attr); 740 data->temp_attr.attr.name = "temp"; 741 data->temp_attr.attr.mode = 0444; 742 data->temp_attr.show = temp_show; 743 744 sysfs_attr_init(&data->eppid_attr.attr); 745 data->eppid_attr.attr.name = "eppid"; 746 data->eppid_attr.attr.mode = 0444; 747 data->eppid_attr.show = eppid_show; 748 749 battery_hook_register(&data->hook); 750 751 return devm_add_action_or_reset(&data->wdev->dev, dell_wmi_ddv_battery_remove, &data->hook); 752 } 753 754 static int dell_wmi_ddv_buffer_read(struct seq_file *seq, enum dell_ddv_method method) 755 { 756 struct device *dev = seq->private; 757 struct dell_wmi_ddv_data *data = dev_get_drvdata(dev); 758 union acpi_object *obj; 759 u64 size; 760 u8 *buf; 761 int ret; 762 763 ret = dell_wmi_ddv_query_buffer(data->wdev, method, 0, &obj); 764 if (ret < 0) 765 return ret; 766 767 size = obj->package.elements[0].integer.value; 768 buf = obj->package.elements[1].buffer.pointer; 769 ret = seq_write(seq, buf, size); 770 kfree(obj); 771 772 return ret; 773 } 774 775 static int dell_wmi_ddv_fan_read(struct seq_file *seq, void *offset) 776 { 777 return dell_wmi_ddv_buffer_read(seq, DELL_DDV_FAN_SENSOR_INFORMATION); 778 } 779 780 static int dell_wmi_ddv_temp_read(struct seq_file *seq, void *offset) 781 { 782 return dell_wmi_ddv_buffer_read(seq, DELL_DDV_THERMAL_SENSOR_INFORMATION); 783 } 784 785 static void dell_wmi_ddv_debugfs_remove(void *data) 786 { 787 struct dentry *entry = data; 788 789 debugfs_remove(entry); 790 } 791 792 static void dell_wmi_ddv_debugfs_init(struct wmi_device *wdev) 793 { 794 struct dentry *entry; 795 char name[64]; 796 797 scnprintf(name, ARRAY_SIZE(name), "%s-%s", DRIVER_NAME, dev_name(&wdev->dev)); 798 entry = debugfs_create_dir(name, NULL); 799 800 debugfs_create_devm_seqfile(&wdev->dev, "fan_sensor_information", entry, 801 dell_wmi_ddv_fan_read); 802 debugfs_create_devm_seqfile(&wdev->dev, "thermal_sensor_information", entry, 803 dell_wmi_ddv_temp_read); 804 805 devm_add_action_or_reset(&wdev->dev, dell_wmi_ddv_debugfs_remove, entry); 806 } 807 808 static int dell_wmi_ddv_probe(struct wmi_device *wdev, const void *context) 809 { 810 struct dell_wmi_ddv_data *data; 811 u32 version; 812 int ret; 813 814 ret = dell_wmi_ddv_query_integer(wdev, DELL_DDV_INTERFACE_VERSION, 0, &version); 815 if (ret < 0) 816 return ret; 817 818 dev_dbg(&wdev->dev, "WMI interface version: %d\n", version); 819 if (version < DELL_DDV_SUPPORTED_VERSION_MIN || version > DELL_DDV_SUPPORTED_VERSION_MAX) { 820 if (!force) 821 return -ENODEV; 822 823 dev_warn(&wdev->dev, "Loading despite unsupported WMI interface version (%u)\n", 824 version); 825 } 826 827 data = devm_kzalloc(&wdev->dev, sizeof(*data), GFP_KERNEL); 828 if (!data) 829 return -ENOMEM; 830 831 dev_set_drvdata(&wdev->dev, data); 832 data->wdev = wdev; 833 834 dell_wmi_ddv_debugfs_init(wdev); 835 836 if (IS_REACHABLE(CONFIG_ACPI_BATTERY)) { 837 ret = dell_wmi_ddv_battery_add(data); 838 if (ret < 0 && ret != -ENODEV) 839 dev_warn(&wdev->dev, "Unable to register ACPI battery hook: %d\n", ret); 840 } 841 842 if (IS_REACHABLE(CONFIG_HWMON)) { 843 ret = dell_wmi_ddv_hwmon_add(data); 844 if (ret < 0 && ret != -ENODEV) 845 dev_warn(&wdev->dev, "Unable to register hwmon interface: %d\n", ret); 846 } 847 848 return 0; 849 } 850 851 static int dell_wmi_ddv_resume(struct device *dev) 852 { 853 struct dell_wmi_ddv_data *data = dev_get_drvdata(dev); 854 855 /* Force re-reading of all sensors */ 856 dell_wmi_ddv_hwmon_cache_invalidate(&data->fans); 857 dell_wmi_ddv_hwmon_cache_invalidate(&data->temps); 858 859 return 0; 860 } 861 862 static DEFINE_SIMPLE_DEV_PM_OPS(dell_wmi_ddv_dev_pm_ops, NULL, dell_wmi_ddv_resume); 863 864 static const struct wmi_device_id dell_wmi_ddv_id_table[] = { 865 { DELL_DDV_GUID, NULL }, 866 { } 867 }; 868 MODULE_DEVICE_TABLE(wmi, dell_wmi_ddv_id_table); 869 870 static struct wmi_driver dell_wmi_ddv_driver = { 871 .driver = { 872 .name = DRIVER_NAME, 873 .probe_type = PROBE_PREFER_ASYNCHRONOUS, 874 .pm = pm_sleep_ptr(&dell_wmi_ddv_dev_pm_ops), 875 }, 876 .id_table = dell_wmi_ddv_id_table, 877 .probe = dell_wmi_ddv_probe, 878 }; 879 module_wmi_driver(dell_wmi_ddv_driver); 880 881 MODULE_AUTHOR("Armin Wolf <W_Armin@gmx.de>"); 882 MODULE_DESCRIPTION("Dell WMI sensor driver"); 883 MODULE_LICENSE("GPL"); 884