File Index
Files and directories
Sign in
FILE

dell-smm-hwmon.c

File preview with syntax highlighting.

linux-6.19.14/drivers/hwmon/dell-smm-hwmon.c C
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 * dell-smm-hwmon.c -- Linux driver for accessing the SMM BIOS on Dell laptops.
   4 *
   5 * Copyright (C) 2001  Massimo Dal Zotto <dz@debian.org>
   6 *
   7 * Hwmon integration:
   8 * Copyright (C) 2011  Jean Delvare <jdelvare@suse.de>
   9 * Copyright (C) 2013, 2014  Guenter Roeck <linux@roeck-us.net>
  10 * Copyright (C) 2014, 2015  Pali Rohár <pali@kernel.org>
  11 */
  12
  13#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  14
  15#include <linux/acpi.h>
  16#include <linux/capability.h>
  17#include <linux/cpu.h>
  18#include <linux/ctype.h>
  19#include <linux/delay.h>
  20#include <linux/dmi.h>
  21#include <linux/err.h>
  22#include <linux/errno.h>
  23#include <linux/hwmon.h>
  24#include <linux/init.h>
  25#include <linux/kconfig.h>
  26#include <linux/kernel.h>
  27#include <linux/minmax.h>
  28#include <linux/module.h>
  29#include <linux/mutex.h>
  30#include <linux/platform_device.h>
  31#include <linux/proc_fs.h>
  32#include <linux/seq_file.h>
  33#include <linux/slab.h>
  34#include <linux/smp.h>
  35#include <linux/string.h>
  36#include <linux/thermal.h>
  37#include <linux/types.h>
  38#include <linux/uaccess.h>
  39#include <linux/wmi.h>
  40
  41#include <linux/i8k.h>
  42#include <linux/unaligned.h>
  43
  44#define I8K_SMM_FN_STATUS	0x0025
  45#define I8K_SMM_POWER_STATUS	0x0069
  46#define I8K_SMM_SET_FAN		0x01a3
  47#define I8K_SMM_GET_FAN		0x00a3
  48#define I8K_SMM_GET_SPEED	0x02a3
  49#define I8K_SMM_GET_FAN_TYPE	0x03a3
  50#define I8K_SMM_GET_NOM_SPEED	0x04a3
  51#define I8K_SMM_GET_TEMP	0x10a3
  52#define I8K_SMM_GET_TEMP_TYPE	0x11a3
  53#define I8K_SMM_GET_DELL_SIG1	0xfea3
  54#define I8K_SMM_GET_DELL_SIG2	0xffa3
  55
  56/* in usecs */
  57#define DELL_SMM_MAX_DURATION  250000
  58
  59#define I8K_FAN_MULT		30
  60#define I8K_FAN_RPM_THRESHOLD	1000
  61#define I8K_MAX_TEMP		127
  62
  63#define I8K_FN_NONE		0x00
  64#define I8K_FN_UP		0x01
  65#define I8K_FN_DOWN		0x02
  66#define I8K_FN_MUTE		0x04
  67#define I8K_FN_MASK		0x07
  68#define I8K_FN_SHIFT		8
  69
  70#define I8K_POWER_AC		0x05
  71#define I8K_POWER_BATTERY	0x01
  72
  73#define DELL_SMM_WMI_GUID	"F1DDEE52-063C-4784-A11E-8A06684B9B01"
  74#define DELL_SMM_LEGACY_EXECUTE	0x1
  75
  76#define DELL_SMM_NO_TEMP	10
  77#define DELL_SMM_NO_FANS	4
  78
  79/* limit fan multiplier to avoid overflow */
  80#define DELL_SMM_MAX_FAN_MULT (INT_MAX / U16_MAX)
  81
  82struct smm_regs {
  83	unsigned int eax;
  84	unsigned int ebx;
  85	unsigned int ecx;
  86	unsigned int edx;
  87	unsigned int esi;
  88	unsigned int edi;
  89};
  90
  91struct dell_smm_ops {
  92	struct device *smm_dev;
  93	int (*smm_call)(struct device *smm_dev, struct smm_regs *regs);
  94};
  95
  96struct dell_smm_data {
  97	struct mutex i8k_mutex; /* lock for sensors writes */
  98	char bios_version[4];
  99	char bios_machineid[16];
 100	uint i8k_fan_mult;
 101	uint i8k_pwm_mult;
 102	uint i8k_fan_max;
 103	int temp_type[DELL_SMM_NO_TEMP];
 104	bool fan[DELL_SMM_NO_FANS];
 105	int fan_type[DELL_SMM_NO_FANS];
 106	int *fan_nominal_speed[DELL_SMM_NO_FANS];
 107	const struct dell_smm_ops *ops;
 108};
 109
 110struct dell_smm_cooling_data {
 111	u8 fan_num;
 112	struct dell_smm_data *data;
 113};
 114
 115MODULE_AUTHOR("Massimo Dal Zotto <dz@debian.org>");
 116MODULE_AUTHOR("Pali Rohár <pali@kernel.org>");
 117MODULE_DESCRIPTION("Dell laptop SMM BIOS hwmon driver");
 118MODULE_LICENSE("GPL");
 119MODULE_ALIAS("i8k");
 120
 121static bool force;
 122module_param_unsafe(force, bool, 0);
 123MODULE_PARM_DESC(force, "Force loading without checking for supported models and features");
 124
 125static bool ignore_dmi;
 126module_param(ignore_dmi, bool, 0);
 127MODULE_PARM_DESC(ignore_dmi, "Continue probing hardware even if DMI data does not match");
 128
 129#if IS_ENABLED(CONFIG_I8K)
 130static bool restricted = true;
 131module_param(restricted, bool, 0);
 132MODULE_PARM_DESC(restricted, "Restrict fan control and serial number to CAP_SYS_ADMIN (default: 1)");
 133
 134static bool power_status;
 135module_param(power_status, bool, 0600);
 136MODULE_PARM_DESC(power_status, "Report power status in /proc/i8k (default: 0)");
 137#endif
 138
 139static uint fan_mult;
 140module_param(fan_mult, uint, 0);
 141MODULE_PARM_DESC(fan_mult, "Factor to multiply fan speed with (default: autodetect)");
 142
 143static uint fan_max;
 144module_param(fan_max, uint, 0);
 145MODULE_PARM_DESC(fan_max, "Maximum configurable fan speed (default: autodetect)");
 146
 147static bool disallow_fan_type_call, disallow_fan_support;
 148
 149static unsigned int manual_fan, auto_fan;
 150
 151static const char * const temp_labels[] = {
 152	"CPU",
 153	"GPU",
 154	"SODIMM",
 155	"Other",
 156	"Ambient",
 157	"Other",
 158};
 159
 160static const char * const fan_labels[] = {
 161	"Processor Fan",
 162	"Motherboard Fan",
 163	"Video Fan",
 164	"Power Supply Fan",
 165	"Chipset Fan",
 166	"Other Fan",
 167};
 168
 169static const char * const docking_labels[] = {
 170	"Docking Processor Fan",
 171	"Docking Motherboard Fan",
 172	"Docking Video Fan",
 173	"Docking Power Supply Fan",
 174	"Docking Chipset Fan",
 175	"Docking Other Fan",
 176};
 177
 178static inline const char __init *i8k_get_dmi_data(int field)
 179{
 180	const char *dmi_data = dmi_get_system_info(field);
 181
 182	return dmi_data && *dmi_data ? dmi_data : "?";
 183}
 184
 185/*
 186 * Call the System Management Mode BIOS. Code provided by Jonathan Buzzard.
 187 */
 188static int i8k_smm_func(void *par)
 189{
 190	struct smm_regs *regs = par;
 191	unsigned char carry;
 192
 193	/* SMM requires CPU 0 */
 194	if (smp_processor_id() != 0)
 195		return -EBUSY;
 196
 197	asm volatile("out %%al,$0xb2\n\t"
 198		     "out %%al,$0x84\n\t"
 199		     "setc %0\n"
 200		     : "=mr" (carry),
 201		       "+a" (regs->eax),
 202		       "+b" (regs->ebx),
 203		       "+c" (regs->ecx),
 204		       "+d" (regs->edx),
 205		       "+S" (regs->esi),
 206		       "+D" (regs->edi));
 207
 208	if (carry)
 209		return -EINVAL;
 210
 211	return 0;
 212}
 213
 214/*
 215 * Call the System Management Mode BIOS.
 216 */
 217static int i8k_smm_call(struct device *dummy, struct smm_regs *regs)
 218{
 219	int ret;
 220
 221	cpus_read_lock();
 222	ret = smp_call_on_cpu(0, i8k_smm_func, regs, true);
 223	cpus_read_unlock();
 224
 225	return ret;
 226}
 227
 228static const struct dell_smm_ops i8k_smm_ops = {
 229	.smm_call = i8k_smm_call,
 230};
 231
 232/*
 233 * Call the System Management Mode BIOS over WMI.
 234 */
 235static ssize_t wmi_parse_register(u8 *buffer, u32 length, unsigned int *reg)
 236{
 237	__le32 value;
 238	u32 reg_size;
 239
 240	if (length <= sizeof(reg_size))
 241		return -ENODATA;
 242
 243	reg_size = get_unaligned_le32(buffer);
 244	if (!reg_size || reg_size > sizeof(value))
 245		return -ENOMSG;
 246
 247	if (length < sizeof(reg_size) + reg_size)
 248		return -ENODATA;
 249
 250	memcpy_and_pad(&value, sizeof(value), buffer + sizeof(reg_size), reg_size, 0);
 251	*reg = le32_to_cpu(value);
 252
 253	return reg_size + sizeof(reg_size);
 254}
 255
 256static int wmi_parse_response(u8 *buffer, u32 length, struct smm_regs *regs)
 257{
 258	unsigned int *registers[] = {
 259		&regs->eax,
 260		&regs->ebx,
 261		&regs->ecx,
 262		&regs->edx
 263	};
 264	u32 offset = 0;
 265	ssize_t ret;
 266	int i;
 267
 268	for (i = 0; i < ARRAY_SIZE(registers); i++) {
 269		if (offset >= length)
 270			return -ENODATA;
 271
 272		ret = wmi_parse_register(buffer + offset, length - offset, registers[i]);
 273		if (ret < 0)
 274			return ret;
 275
 276		offset += ret;
 277	}
 278
 279	if (offset != length)
 280		return -ENOMSG;
 281
 282	return 0;
 283}
 284
 285static int wmi_smm_call(struct device *dev, struct smm_regs *regs)
 286{
 287	struct wmi_device *wdev = container_of(dev, struct wmi_device, dev);
 288	struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL };
 289	u32 wmi_payload[] = {
 290		sizeof(regs->eax),
 291		regs->eax,
 292		sizeof(regs->ebx),
 293		regs->ebx,
 294		sizeof(regs->ecx),
 295		regs->ecx,
 296		sizeof(regs->edx),
 297		regs->edx
 298	};
 299	const struct acpi_buffer in = {
 300		.length = sizeof(wmi_payload),
 301		.pointer = &wmi_payload,
 302	};
 303	union acpi_object *obj;
 304	acpi_status status;
 305	int ret;
 306
 307	status = wmidev_evaluate_method(wdev, 0x0, DELL_SMM_LEGACY_EXECUTE, &in, &out);
 308	if (ACPI_FAILURE(status))
 309		return -EIO;
 310
 311	obj = out.pointer;
 312	if (!obj)
 313		return -ENODATA;
 314
 315	if (obj->type != ACPI_TYPE_BUFFER) {
 316		ret = -ENOMSG;
 317
 318		goto err_free;
 319	}
 320
 321	ret = wmi_parse_response(obj->buffer.pointer, obj->buffer.length, regs);
 322
 323err_free:
 324	kfree(obj);
 325
 326	return ret;
 327}
 328
 329static int dell_smm_call(const struct dell_smm_ops *ops, struct smm_regs *regs)
 330{
 331	unsigned int eax = regs->eax;
 332	unsigned int ebx = regs->ebx;
 333	long long duration;
 334	ktime_t calltime;
 335	int ret;
 336
 337	calltime = ktime_get();
 338	ret = ops->smm_call(ops->smm_dev, regs);
 339	duration = ktime_us_delta(ktime_get(), calltime);
 340
 341	pr_debug("SMM(0x%.4x 0x%.4x) = 0x%.4x status: %d (took %7lld usecs)\n",
 342		 eax, ebx, regs->eax & 0xffff, ret, duration);
 343
 344	if (duration > DELL_SMM_MAX_DURATION)
 345		pr_warn_once("SMM call took %lld usecs!\n", duration);
 346
 347	if (ret < 0)
 348		return ret;
 349
 350	if ((regs->eax & 0xffff) == 0xffff || regs->eax == eax)
 351		return -EINVAL;
 352
 353	return 0;
 354}
 355
 356/*
 357 * Read the fan status.
 358 */
 359static int i8k_get_fan_status(const struct dell_smm_data *data, u8 fan)
 360{
 361	struct smm_regs regs = {
 362		.eax = I8K_SMM_GET_FAN,
 363		.ebx = fan,
 364	};
 365
 366	if (disallow_fan_support)
 367		return -EINVAL;
 368
 369	return dell_smm_call(data->ops, &regs) ? : regs.eax & 0xff;
 370}
 371
 372/*
 373 * Read the fan speed in RPM.
 374 */
 375static int i8k_get_fan_speed(const struct dell_smm_data *data, u8 fan)
 376{
 377	struct smm_regs regs = {
 378		.eax = I8K_SMM_GET_SPEED,
 379		.ebx = fan,
 380	};
 381
 382	if (disallow_fan_support)
 383		return -EINVAL;
 384
 385	return dell_smm_call(data->ops, &regs) ? : (regs.eax & 0xffff) * data->i8k_fan_mult;
 386}
 387
 388/*
 389 * Read the fan type.
 390 */
 391static int _i8k_get_fan_type(const struct dell_smm_data *data, u8 fan)
 392{
 393	struct smm_regs regs = {
 394		.eax = I8K_SMM_GET_FAN_TYPE,
 395		.ebx = fan,
 396	};
 397
 398	if (disallow_fan_support || disallow_fan_type_call)
 399		return -EINVAL;
 400
 401	return dell_smm_call(data->ops, &regs) ? : regs.eax & 0xff;
 402}
 403
 404static int i8k_get_fan_type(struct dell_smm_data *data, u8 fan)
 405{
 406	/* I8K_SMM_GET_FAN_TYPE SMM call is expensive, so cache values */
 407	if (data->fan_type[fan] == INT_MIN)
 408		data->fan_type[fan] = _i8k_get_fan_type(data, fan);
 409
 410	return data->fan_type[fan];
 411}
 412
 413/*
 414 * Read the fan nominal rpm for specific fan speed.
 415 */
 416static int i8k_get_fan_nominal_speed(const struct dell_smm_data *data, u8 fan, int speed)
 417{
 418	struct smm_regs regs = {
 419		.eax = I8K_SMM_GET_NOM_SPEED,
 420		.ebx = fan | (speed << 8),
 421	};
 422
 423	if (disallow_fan_support)
 424		return -EINVAL;
 425
 426	return dell_smm_call(data->ops, &regs) ? : (regs.eax & 0xffff);
 427}
 428
 429/*
 430 * Enable or disable automatic BIOS fan control support
 431 */
 432static int i8k_enable_fan_auto_mode(const struct dell_smm_data *data, bool enable)
 433{
 434	struct smm_regs regs = { };
 435
 436	if (disallow_fan_support)
 437		return -EINVAL;
 438
 439	regs.eax = enable ? auto_fan : manual_fan;
 440	return dell_smm_call(data->ops, &regs);
 441}
 442
 443/*
 444 * Set the fan speed (off, low, high, ...).
 445 */
 446static int i8k_set_fan(const struct dell_smm_data *data, u8 fan, int speed)
 447{
 448	struct smm_regs regs = { .eax = I8K_SMM_SET_FAN, };
 449
 450	if (disallow_fan_support)
 451		return -EINVAL;
 452
 453	regs.ebx = fan | (speed << 8);
 454
 455	return dell_smm_call(data->ops, &regs);
 456}
 457
 458static int i8k_get_temp_type(const struct dell_smm_data *data, u8 sensor)
 459{
 460	struct smm_regs regs = {
 461		.eax = I8K_SMM_GET_TEMP_TYPE,
 462		.ebx = sensor,
 463	};
 464
 465	return dell_smm_call(data->ops, &regs) ? : regs.eax & 0xff;
 466}
 467
 468/*
 469 * Read the cpu temperature.
 470 */
 471static int _i8k_get_temp(const struct dell_smm_data *data, u8 sensor)
 472{
 473	struct smm_regs regs = {
 474		.eax = I8K_SMM_GET_TEMP,
 475		.ebx = sensor,
 476	};
 477
 478	return dell_smm_call(data->ops, &regs) ? : regs.eax & 0xff;
 479}
 480
 481static int i8k_get_temp(const struct dell_smm_data *data, u8 sensor)
 482{
 483	int temp = _i8k_get_temp(data, sensor);
 484
 485	/*
 486	 * Sometimes the temperature sensor returns 0x99, which is out of range.
 487	 * In this case we retry (once) before returning an error.
 488	 # 1003655137 00000058 00005a4b
 489	 # 1003655138 00000099 00003a80 <--- 0x99 = 153 degrees
 490	 # 1003655139 00000054 00005c52
 491	 */
 492	if (temp == 0x99) {
 493		msleep(100);
 494		temp = _i8k_get_temp(data, sensor);
 495	}
 496	/*
 497	 * Return -ENODATA for all invalid temperatures.
 498	 *
 499	 * Known instances are the 0x99 value as seen above as well as
 500	 * 0xc1 (193), which may be returned when trying to read the GPU
 501	 * temperature if the system supports a GPU and it is currently
 502	 * turned off.
 503	 */
 504	if (temp > I8K_MAX_TEMP)
 505		return -ENODATA;
 506
 507	return temp;
 508}
 509
 510static int dell_smm_get_signature(const struct dell_smm_ops *ops, int req_fn)
 511{
 512	struct smm_regs regs = { .eax = req_fn, };
 513	int rc;
 514
 515	rc = dell_smm_call(ops, &regs);
 516	if (rc < 0)
 517		return rc;
 518
 519	return regs.eax == 1145651527 && regs.edx == 1145392204 ? 0 : -1;
 520}
 521
 522#if IS_ENABLED(CONFIG_I8K)
 523
 524/*
 525 * Read the Fn key status.
 526 */
 527static int i8k_get_fn_status(const struct dell_smm_data *data)
 528{
 529	struct smm_regs regs = { .eax = I8K_SMM_FN_STATUS, };
 530	int rc;
 531
 532	rc = dell_smm_call(data->ops, &regs);
 533	if (rc < 0)
 534		return rc;
 535
 536	switch ((regs.eax >> I8K_FN_SHIFT) & I8K_FN_MASK) {
 537	case I8K_FN_UP:
 538		return I8K_VOL_UP;
 539	case I8K_FN_DOWN:
 540		return I8K_VOL_DOWN;
 541	case I8K_FN_MUTE:
 542		return I8K_VOL_MUTE;
 543	default:
 544		return 0;
 545	}
 546}
 547
 548/*
 549 * Read the power status.
 550 */
 551static int i8k_get_power_status(const struct dell_smm_data *data)
 552{
 553	struct smm_regs regs = { .eax = I8K_SMM_POWER_STATUS, };
 554	int rc;
 555
 556	rc = dell_smm_call(data->ops, &regs);
 557	if (rc < 0)
 558		return rc;
 559
 560	return (regs.eax & 0xff) == I8K_POWER_AC ? I8K_AC : I8K_BATTERY;
 561}
 562
 563/*
 564 * Procfs interface
 565 */
 566
 567static long i8k_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)
 568{
 569	struct dell_smm_data *data = pde_data(file_inode(fp));
 570	int __user *argp = (int __user *)arg;
 571	int speed, err;
 572	int val = 0;
 573
 574	if (!argp)
 575		return -EINVAL;
 576
 577	switch (cmd) {
 578	case I8K_BIOS_VERSION:
 579		if (!isdigit(data->bios_version[0]) || !isdigit(data->bios_version[1]) ||
 580		    !isdigit(data->bios_version[2]))
 581			return -EINVAL;
 582
 583		val = (data->bios_version[0] << 16) |
 584				(data->bios_version[1] << 8) | data->bios_version[2];
 585
 586		if (copy_to_user(argp, &val, sizeof(val)))
 587			return -EFAULT;
 588
 589		return 0;
 590	case I8K_MACHINE_ID:
 591		if (restricted && !capable(CAP_SYS_ADMIN))
 592			return -EPERM;
 593
 594		if (copy_to_user(argp, data->bios_machineid, sizeof(data->bios_machineid)))
 595			return -EFAULT;
 596
 597		return 0;
 598	case I8K_FN_STATUS:
 599		val = i8k_get_fn_status(data);
 600		break;
 601
 602	case I8K_POWER_STATUS:
 603		val = i8k_get_power_status(data);
 604		break;
 605
 606	case I8K_GET_TEMP:
 607		val = i8k_get_temp(data, 0);
 608		break;
 609
 610	case I8K_GET_SPEED:
 611		if (copy_from_user(&val, argp, sizeof(int)))
 612			return -EFAULT;
 613
 614		if (val > U8_MAX || val < 0)
 615			return -EINVAL;
 616
 617		val = i8k_get_fan_speed(data, val);
 618		break;
 619
 620	case I8K_GET_FAN:
 621		if (copy_from_user(&val, argp, sizeof(int)))
 622			return -EFAULT;
 623
 624		if (val > U8_MAX || val < 0)
 625			return -EINVAL;
 626
 627		val = i8k_get_fan_status(data, val);
 628		break;
 629
 630	case I8K_SET_FAN:
 631		if (restricted && !capable(CAP_SYS_ADMIN))
 632			return -EPERM;
 633
 634		if (copy_from_user(&val, argp, sizeof(int)))
 635			return -EFAULT;
 636
 637		if (val > U8_MAX || val < 0)
 638			return -EINVAL;
 639
 640		if (copy_from_user(&speed, argp + 1, sizeof(int)))
 641			return -EFAULT;
 642
 643		speed = clamp_val(speed, 0, data->i8k_fan_max);
 644
 645		mutex_lock(&data->i8k_mutex);
 646		err = i8k_set_fan(data, val, speed);
 647		if (err < 0)
 648			val = err;
 649		else
 650			val = i8k_get_fan_status(data, val);
 651		mutex_unlock(&data->i8k_mutex);
 652		break;
 653
 654	default:
 655		return -ENOIOCTLCMD;
 656	}
 657
 658	if (val < 0)
 659		return val;
 660
 661	if (copy_to_user(argp, &val, sizeof(int)))
 662		return -EFAULT;
 663
 664	return 0;
 665}
 666
 667/*
 668 * Print the information for /proc/i8k.
 669 */
 670static int i8k_proc_show(struct seq_file *seq, void *offset)
 671{
 672	struct dell_smm_data *data = seq->private;
 673	int fn_key, cpu_temp, ac_power;
 674	int left_fan, right_fan, left_speed, right_speed;
 675
 676	cpu_temp	= i8k_get_temp(data, 0);			/* 11100 µs */
 677	left_fan	= i8k_get_fan_status(data, I8K_FAN_LEFT);	/*   580 µs */
 678	right_fan	= i8k_get_fan_status(data, I8K_FAN_RIGHT);	/*   580 µs */
 679	left_speed	= i8k_get_fan_speed(data, I8K_FAN_LEFT);	/*   580 µs */
 680	right_speed	= i8k_get_fan_speed(data, I8K_FAN_RIGHT);	/*   580 µs */
 681	fn_key		= i8k_get_fn_status(data);			/*   750 µs */
 682	if (power_status)
 683		ac_power = i8k_get_power_status(data);			/* 14700 µs */
 684	else
 685		ac_power = -1;
 686
 687	/*
 688	 * Info:
 689	 *
 690	 * 1)  Format version (this will change if format changes)
 691	 * 2)  BIOS version
 692	 * 3)  BIOS machine ID
 693	 * 4)  Cpu temperature
 694	 * 5)  Left fan status
 695	 * 6)  Right fan status
 696	 * 7)  Left fan speed
 697	 * 8)  Right fan speed
 698	 * 9)  AC power
 699	 * 10) Fn Key status
 700	 */
 701	seq_printf(seq, "%s %s %s %d %d %d %d %d %d %d\n",
 702		   I8K_PROC_FMT,
 703		   data->bios_version,
 704		   (restricted && !capable(CAP_SYS_ADMIN)) ? "-1" : data->bios_machineid,
 705		   cpu_temp,
 706		   left_fan, right_fan, left_speed, right_speed,
 707		   ac_power, fn_key);
 708
 709	return 0;
 710}
 711
 712static int i8k_open_fs(struct inode *inode, struct file *file)
 713{
 714	return single_open(file, i8k_proc_show, pde_data(inode));
 715}
 716
 717static const struct proc_ops i8k_proc_ops = {
 718	.proc_open	= i8k_open_fs,
 719	.proc_read	= seq_read,
 720	.proc_lseek	= seq_lseek,
 721	.proc_release	= single_release,
 722	.proc_ioctl	= i8k_ioctl,
 723};
 724
 725static void i8k_exit_procfs(void *param)
 726{
 727	remove_proc_entry("i8k", NULL);
 728}
 729
 730static void __init i8k_init_procfs(struct device *dev)
 731{
 732	struct dell_smm_data *data = dev_get_drvdata(dev);
 733
 734	strscpy(data->bios_version, i8k_get_dmi_data(DMI_BIOS_VERSION),
 735		sizeof(data->bios_version));
 736	strscpy(data->bios_machineid, i8k_get_dmi_data(DMI_PRODUCT_SERIAL),
 737		sizeof(data->bios_machineid));
 738
 739	/* Only register exit function if creation was successful */
 740	if (proc_create_data("i8k", 0, NULL, &i8k_proc_ops, data))
 741		devm_add_action_or_reset(dev, i8k_exit_procfs, NULL);
 742}
 743
 744#else
 745
 746static void __init i8k_init_procfs(struct device *dev)
 747{
 748}
 749
 750#endif
 751
 752static int dell_smm_get_max_state(struct thermal_cooling_device *dev, unsigned long *state)
 753{
 754	struct dell_smm_cooling_data *cdata = dev->devdata;
 755
 756	*state = cdata->data->i8k_fan_max;
 757
 758	return 0;
 759}
 760
 761static int dell_smm_get_cur_state(struct thermal_cooling_device *dev, unsigned long *state)
 762{
 763	struct dell_smm_cooling_data *cdata = dev->devdata;
 764	int ret;
 765
 766	ret = i8k_get_fan_status(cdata->data, cdata->fan_num);
 767	if (ret < 0)
 768		return ret;
 769
 770	/*
 771	 * A fan state bigger than i8k_fan_max might indicate that
 772	 * the fan is currently in automatic mode.
 773	 */
 774	if (ret > cdata->data->i8k_fan_max)
 775		return -ENODATA;
 776
 777	*state = ret;
 778
 779	return 0;
 780}
 781
 782static int dell_smm_set_cur_state(struct thermal_cooling_device *dev, unsigned long state)
 783{
 784	struct dell_smm_cooling_data *cdata = dev->devdata;
 785	struct dell_smm_data *data = cdata->data;
 786	int ret;
 787
 788	if (state > data->i8k_fan_max)
 789		return -EINVAL;
 790
 791	mutex_lock(&data->i8k_mutex);
 792	ret = i8k_set_fan(data, cdata->fan_num, (int)state);
 793	mutex_unlock(&data->i8k_mutex);
 794
 795	return ret;
 796}
 797
 798static const struct thermal_cooling_device_ops dell_smm_cooling_ops = {
 799	.get_max_state = dell_smm_get_max_state,
 800	.get_cur_state = dell_smm_get_cur_state,
 801	.set_cur_state = dell_smm_set_cur_state,
 802};
 803
 804static umode_t dell_smm_is_visible(const void *drvdata, enum hwmon_sensor_types type, u32 attr,
 805				   int channel)
 806{
 807	const struct dell_smm_data *data = drvdata;
 808
 809	switch (type) {
 810	case hwmon_temp:
 811		switch (attr) {
 812		case hwmon_temp_input:
 813			/* _i8k_get_temp() is fine since we do not care about the actual value */
 814			if (data->temp_type[channel] >= 0 || _i8k_get_temp(data, channel) >= 0)
 815				return 0444;
 816
 817			break;
 818		case hwmon_temp_label:
 819			if (data->temp_type[channel] >= 0)
 820				return 0444;
 821
 822			break;
 823		default:
 824			break;
 825		}
 826		break;
 827	case hwmon_fan:
 828		if (disallow_fan_support)
 829			break;
 830
 831		switch (attr) {
 832		case hwmon_fan_input:
 833			if (data->fan[channel])
 834				return 0444;
 835
 836			break;
 837		case hwmon_fan_label:
 838			if (data->fan[channel] && !disallow_fan_type_call)
 839				return 0444;
 840
 841			break;
 842		case hwmon_fan_min:
 843		case hwmon_fan_max:
 844		case hwmon_fan_target:
 845			if (data->fan_nominal_speed[channel])
 846				return 0444;
 847
 848			break;
 849		default:
 850			break;
 851		}
 852		break;
 853	case hwmon_pwm:
 854		if (disallow_fan_support)
 855			break;
 856
 857		switch (attr) {
 858		case hwmon_pwm_input:
 859			if (data->fan[channel])
 860				return 0644;
 861
 862			break;
 863		case hwmon_pwm_enable:
 864			if (auto_fan) {
 865				/*
 866				 * The setting affects all fans, so only create a
 867				 * single attribute for the first fan channel.
 868				 */
 869				if (channel != 0)
 870					return 0;
 871
 872				/*
 873				 * There is no command for retrieve the current status
 874				 * from BIOS, and userspace/firmware itself can change
 875				 * it.
 876				 * Thus we can only provide write-only access for now.
 877				 */
 878				return 0200;
 879			}
 880
 881			if (data->fan[channel] && data->i8k_fan_max < I8K_FAN_AUTO)
 882				return 0644;
 883
 884			break;
 885		default:
 886			break;
 887		}
 888		break;
 889	default:
 890		break;
 891	}
 892
 893	return 0;
 894}
 895
 896static int dell_smm_read(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel,
 897			 long *val)
 898{
 899	struct dell_smm_data *data = dev_get_drvdata(dev);
 900	int mult = data->i8k_fan_mult;
 901	int ret;
 902
 903	switch (type) {
 904	case hwmon_temp:
 905		switch (attr) {
 906		case hwmon_temp_input:
 907			ret = i8k_get_temp(data, channel);
 908			if (ret < 0)
 909				return ret;
 910
 911			*val = ret * 1000;
 912
 913			return 0;
 914		default:
 915			break;
 916		}
 917		break;
 918	case hwmon_fan:
 919		switch (attr) {
 920		case hwmon_fan_input:
 921			ret = i8k_get_fan_speed(data, channel);
 922			if (ret < 0)
 923				return ret;
 924
 925			*val = ret;
 926
 927			return 0;
 928		case hwmon_fan_min:
 929			*val = data->fan_nominal_speed[channel][0] * mult;
 930
 931			return 0;
 932		case hwmon_fan_max:
 933			*val = data->fan_nominal_speed[channel][data->i8k_fan_max] * mult;
 934
 935			return 0;
 936		case hwmon_fan_target:
 937			ret = i8k_get_fan_status(data, channel);
 938			if (ret < 0)
 939				return ret;
 940
 941			if (ret > data->i8k_fan_max)
 942				ret = data->i8k_fan_max;
 943
 944			*val = data->fan_nominal_speed[channel][ret] * mult;
 945
 946			return 0;
 947		default:
 948			break;
 949		}
 950		break;
 951	case hwmon_pwm:
 952		ret = i8k_get_fan_status(data, channel);
 953		if (ret < 0)
 954			return ret;
 955
 956		switch (attr) {
 957		case hwmon_pwm_input:
 958			/*
 959			 * A fan state bigger than i8k_fan_max might indicate that
 960			 * the fan is currently in automatic mode.
 961			 */
 962			if (ret > data->i8k_fan_max)
 963				return -ENODATA;
 964
 965			*val = clamp_val(ret * data->i8k_pwm_mult, 0, 255);
 966
 967			return 0;
 968		case hwmon_pwm_enable:
 969			if (ret == I8K_FAN_AUTO)
 970				*val = 2;
 971			else
 972				*val = 1;
 973
 974			return 0;
 975		default:
 976			break;
 977		}
 978		break;
 979	default:
 980		break;
 981	}
 982
 983	return -EOPNOTSUPP;
 984}
 985
 986static const char *dell_smm_fan_label(struct dell_smm_data *data, int channel)
 987{
 988	bool dock = false;
 989	int type = i8k_get_fan_type(data, channel);
 990
 991	if (type < 0)
 992		return ERR_PTR(type);
 993
 994	if (type & 0x10) {
 995		dock = true;
 996		type &= 0x0F;
 997	}
 998
 999	if (type >= ARRAY_SIZE(fan_labels))
1000		type = ARRAY_SIZE(fan_labels) - 1;
1001
1002	return dock ? docking_labels[type] : fan_labels[type];
1003}
1004
1005static int dell_smm_read_string(struct device *dev, enum hwmon_sensor_types type, u32 attr,
1006				int channel, const char **str)
1007{
1008	struct dell_smm_data *data = dev_get_drvdata(dev);
1009
1010	switch (type) {
1011	case hwmon_temp:
1012		switch (attr) {
1013		case hwmon_temp_label:
1014			*str = temp_labels[data->temp_type[channel]];
1015			return 0;
1016		default:
1017			break;
1018		}
1019		break;
1020	case hwmon_fan:
1021		switch (attr) {
1022		case hwmon_fan_label:
1023			*str = dell_smm_fan_label(data, channel);
1024			return PTR_ERR_OR_ZERO(*str);
1025		default:
1026			break;
1027		}
1028		break;
1029	default:
1030		break;
1031	}
1032
1033	return -EOPNOTSUPP;
1034}
1035
1036static int dell_smm_write(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel,
1037			  long val)
1038{
1039	struct dell_smm_data *data = dev_get_drvdata(dev);
1040	unsigned long pwm;
1041	bool enable;
1042	int err;
1043
1044	switch (type) {
1045	case hwmon_pwm:
1046		switch (attr) {
1047		case hwmon_pwm_input:
1048			pwm = clamp_val(DIV_ROUND_CLOSEST(val, data->i8k_pwm_mult), 0,
1049					data->i8k_fan_max);
1050
1051			mutex_lock(&data->i8k_mutex);
1052			err = i8k_set_fan(data, channel, pwm);
1053			mutex_unlock(&data->i8k_mutex);
1054
1055			if (err < 0)
1056				return err;
1057
1058			return 0;
1059		case hwmon_pwm_enable:
1060			switch (val) {
1061			case 1:
1062				enable = false;
1063				break;
1064			case 2:
1065				enable = true;
1066				break;
1067			default:
1068				return -EINVAL;
1069			}
1070
1071			mutex_lock(&data->i8k_mutex);
1072			if (auto_fan) {
1073				err = i8k_enable_fan_auto_mode(data, enable);
1074			} else {
1075				/*
1076				 * When putting the fan into manual control mode we have to ensure
1077				 * that the device does not overheat until the userspace fan control
1078				 * software takes over. Because of this we set the fan speed to
1079				 * i8k_fan_max when disabling automatic fan control.
1080				 */
1081				if (enable)
1082					err = i8k_set_fan(data, channel, I8K_FAN_AUTO);
1083				else
1084					err = i8k_set_fan(data, channel, data->i8k_fan_max);
1085			}
1086			mutex_unlock(&data->i8k_mutex);
1087
1088			if (err < 0)
1089				return err;
1090
1091			return 0;
1092		default:
1093			break;
1094		}
1095		break;
1096	default:
1097		break;
1098	}
1099
1100	return -EOPNOTSUPP;
1101}
1102
1103static const struct hwmon_ops dell_smm_ops = {
1104	.is_visible = dell_smm_is_visible,
1105	.read = dell_smm_read,
1106	.read_string = dell_smm_read_string,
1107	.write = dell_smm_write,
1108};
1109
1110static const struct hwmon_channel_info * const dell_smm_info[] = {
1111	HWMON_CHANNEL_INFO(chip, HWMON_C_REGISTER_TZ),
1112	HWMON_CHANNEL_INFO(temp,
1113			   HWMON_T_INPUT | HWMON_T_LABEL,
1114			   HWMON_T_INPUT | HWMON_T_LABEL,
1115			   HWMON_T_INPUT | HWMON_T_LABEL,
1116			   HWMON_T_INPUT | HWMON_T_LABEL,
1117			   HWMON_T_INPUT | HWMON_T_LABEL,
1118			   HWMON_T_INPUT | HWMON_T_LABEL,
1119			   HWMON_T_INPUT | HWMON_T_LABEL,
1120			   HWMON_T_INPUT | HWMON_T_LABEL,
1121			   HWMON_T_INPUT | HWMON_T_LABEL,
1122			   HWMON_T_INPUT | HWMON_T_LABEL
1123			   ),
1124	HWMON_CHANNEL_INFO(fan,
1125			   HWMON_F_INPUT | HWMON_F_LABEL | HWMON_F_MIN | HWMON_F_MAX |
1126			   HWMON_F_TARGET,
1127			   HWMON_F_INPUT | HWMON_F_LABEL | HWMON_F_MIN | HWMON_F_MAX |
1128			   HWMON_F_TARGET,
1129			   HWMON_F_INPUT | HWMON_F_LABEL | HWMON_F_MIN | HWMON_F_MAX |
1130			   HWMON_F_TARGET,
1131			   HWMON_F_INPUT | HWMON_F_LABEL | HWMON_F_MIN | HWMON_F_MAX |
1132			   HWMON_F_TARGET
1133			   ),
1134	HWMON_CHANNEL_INFO(pwm,
1135			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
1136			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
1137			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE,
1138			   HWMON_PWM_INPUT | HWMON_PWM_ENABLE
1139			   ),
1140	NULL
1141};
1142
1143static const struct hwmon_chip_info dell_smm_chip_info = {
1144	.ops = &dell_smm_ops,
1145	.info = dell_smm_info,
1146};
1147
1148static int dell_smm_init_cdev(struct device *dev, u8 fan_num)
1149{
1150	struct dell_smm_data *data = dev_get_drvdata(dev);
1151	struct thermal_cooling_device *cdev;
1152	struct dell_smm_cooling_data *cdata;
1153	int ret = 0;
1154	char *name;
1155
1156	name = kasprintf(GFP_KERNEL, "dell-smm-fan%u", fan_num + 1);
1157	if (!name)
1158		return -ENOMEM;
1159
1160	cdata = devm_kmalloc(dev, sizeof(*cdata), GFP_KERNEL);
1161	if (cdata) {
1162		cdata->fan_num = fan_num;
1163		cdata->data = data;
1164		cdev = devm_thermal_of_cooling_device_register(dev, NULL, name, cdata,
1165							       &dell_smm_cooling_ops);
1166		if (IS_ERR(cdev)) {
1167			devm_kfree(dev, cdata);
1168			ret = PTR_ERR(cdev);
1169		}
1170	} else {
1171		ret = -ENOMEM;
1172	}
1173
1174	kfree(name);
1175
1176	return ret;
1177}
1178
1179static int dell_smm_init_hwmon(struct device *dev)
1180{
1181	struct dell_smm_data *data = dev_get_drvdata(dev);
1182	struct device *dell_smm_hwmon_dev;
1183	int state, err;
1184	u8 i;
1185
1186	for (i = 0; i < DELL_SMM_NO_TEMP; i++) {
1187		data->temp_type[i] = i8k_get_temp_type(data, i);
1188		if (data->temp_type[i] < 0)
1189			continue;
1190
1191		if (data->temp_type[i] >= ARRAY_SIZE(temp_labels))
1192			data->temp_type[i] = ARRAY_SIZE(temp_labels) - 1;
1193	}
1194
1195	for (i = 0; i < DELL_SMM_NO_FANS; i++) {
1196		data->fan_type[i] = INT_MIN;
1197		err = i8k_get_fan_status(data, i);
1198		if (err < 0)
1199			err = i8k_get_fan_type(data, i);
1200
1201		if (err < 0)
1202			continue;
1203
1204		data->fan[i] = true;
1205
1206		/* the cooling device is not critical, ignore failures */
1207		if (IS_REACHABLE(CONFIG_THERMAL)) {
1208			err = dell_smm_init_cdev(dev, i);
1209			if (err < 0)
1210				dev_warn(dev, "Failed to register cooling device for fan %u\n",
1211					 i + 1);
1212		}
1213
1214		data->fan_nominal_speed[i] = devm_kmalloc_array(dev, data->i8k_fan_max + 1,
1215								sizeof(*data->fan_nominal_speed[i]),
1216								GFP_KERNEL);
1217		if (!data->fan_nominal_speed[i])
1218			continue;
1219
1220		for (state = 0; state <= data->i8k_fan_max; state++) {
1221			err = i8k_get_fan_nominal_speed(data, i, state);
1222			if (err < 0) {
1223				/* Mark nominal speed table as invalid in case of error */
1224				devm_kfree(dev, data->fan_nominal_speed[i]);
1225				data->fan_nominal_speed[i] = NULL;
1226				break;
1227			}
1228			data->fan_nominal_speed[i][state] = err;
1229			/*
1230			 * Autodetect fan multiplier based on nominal rpm if multiplier
1231			 * was not specified as module param or in DMI. If fan reports
1232			 * rpm value too high then set multiplier to 1.
1233			 */
1234			if (!fan_mult && err > I8K_FAN_RPM_THRESHOLD)
1235				data->i8k_fan_mult = 1;
1236		}
1237	}
1238
1239	dell_smm_hwmon_dev = devm_hwmon_device_register_with_info(dev, "dell_smm", data,
1240								  &dell_smm_chip_info, NULL);
1241
1242	return PTR_ERR_OR_ZERO(dell_smm_hwmon_dev);
1243}
1244
1245static int dell_smm_init_data(struct device *dev, const struct dell_smm_ops *ops)
1246{
1247	struct dell_smm_data *data;
1248
1249	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
1250	if (!data)
1251		return -ENOMEM;
1252
1253	mutex_init(&data->i8k_mutex);
1254	dev_set_drvdata(dev, data);
1255
1256	data->ops = ops;
1257	/* All options must not be 0 */
1258	data->i8k_fan_mult = fan_mult ? : I8K_FAN_MULT;
1259	if (data->i8k_fan_mult > DELL_SMM_MAX_FAN_MULT) {
1260		dev_err(dev,
1261			"fan multiplier %u is too large (max %u)\n",
1262			data->i8k_fan_mult, DELL_SMM_MAX_FAN_MULT);
1263		return -EINVAL;
1264	}
1265	data->i8k_fan_max = fan_max ? : I8K_FAN_HIGH;
1266	data->i8k_pwm_mult = DIV_ROUND_UP(255, data->i8k_fan_max);
1267
1268	return 0;
1269}
1270
1271static const struct dmi_system_id i8k_dmi_table[] __initconst = {
1272	{
1273		.ident = "Dell G5 5590",
1274		.matches = {
1275			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1276			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "G5 5590"),
1277		},
1278	},
1279	{
1280		.ident = "Dell G5 5505",
1281		.matches = {
1282			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1283			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "G5 5505"),
1284		},
1285	},
1286	{
1287		.ident = "Dell Inspiron",
1288		.matches = {
1289			DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer"),
1290			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron"),
1291		},
1292	},
1293	{
1294		.ident = "Dell Latitude",
1295		.matches = {
1296			DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer"),
1297			DMI_MATCH(DMI_PRODUCT_NAME, "Latitude"),
1298		},
1299	},
1300	{
1301		.ident = "Dell Inspiron 2",
1302		.matches = {
1303			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1304			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron"),
1305		},
1306	},
1307	{
1308		.ident = "Dell Latitude 2",
1309		.matches = {
1310			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1311			DMI_MATCH(DMI_PRODUCT_NAME, "Latitude"),
1312		},
1313	},
1314	{	/* UK Inspiron 6400  */
1315		.ident = "Dell Inspiron 3",
1316		.matches = {
1317			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1318			DMI_MATCH(DMI_PRODUCT_NAME, "MM061"),
1319		},
1320	},
1321	{
1322		.ident = "Dell Inspiron 3",
1323		.matches = {
1324			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1325			DMI_MATCH(DMI_PRODUCT_NAME, "MP061"),
1326		},
1327	},
1328	{
1329		.ident = "Dell OptiPlex 7080",
1330		.matches = {
1331			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1332			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "OptiPlex 7080"),
1333		},
1334	},
1335	{
1336		.ident = "Dell OptiPlex 7060",
1337		.matches = {
1338			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1339			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "OptiPlex 7060"),
1340		},
1341	},
1342	{
1343		.ident = "Dell OptiPlex 7050",
1344		.matches = {
1345			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1346			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "OptiPlex 7050"),
1347		},
1348	},
1349	{
1350		.ident = "Dell OptiPlex 7040",
1351		.matches = {
1352			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1353			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "OptiPlex 7040"),
1354		},
1355	},
1356	{
1357		.ident = "Dell Precision",
1358		.matches = {
1359			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1360			DMI_MATCH(DMI_PRODUCT_NAME, "Precision"),
1361		},
1362	},
1363	{
1364		.ident = "Dell Vostro",
1365		.matches = {
1366			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1367			DMI_MATCH(DMI_PRODUCT_NAME, "Vostro"),
1368		},
1369	},
1370	{
1371		.ident = "Dell Studio",
1372		.matches = {
1373			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1374			DMI_MATCH(DMI_PRODUCT_NAME, "Studio"),
1375		},
1376	},
1377	{
1378		.ident = "Dell XPS M140",
1379		.matches = {
1380			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1381			DMI_MATCH(DMI_PRODUCT_NAME, "MXC051"),
1382		},
1383	},
1384	{
1385		.ident = "Dell XPS",
1386		.matches = {
1387			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1388			DMI_MATCH(DMI_PRODUCT_NAME, "XPS"),
1389		},
1390	},
1391	{ }
1392};
1393
1394MODULE_DEVICE_TABLE(dmi, i8k_dmi_table);
1395
1396/*
1397 * Only use for machines which need some special configuration
1398 * in order to work correctly (e.g. if autoconfig fails on this machines).
1399 */
1400struct i8k_config_data {
1401	uint fan_mult;
1402	uint fan_max;
1403};
1404
1405enum i8k_configs {
1406	DELL_LATITUDE_D520,
1407	DELL_STUDIO,
1408	DELL_XPS,
1409};
1410
1411static const struct i8k_config_data i8k_config_data[] __initconst = {
1412	[DELL_LATITUDE_D520] = {
1413		.fan_mult = 1,
1414		.fan_max = I8K_FAN_TURBO,
1415	},
1416	[DELL_STUDIO] = {
1417		.fan_mult = 1,
1418		.fan_max = I8K_FAN_HIGH,
1419	},
1420	[DELL_XPS] = {
1421		.fan_mult = 1,
1422		.fan_max = I8K_FAN_HIGH,
1423	},
1424};
1425
1426static const struct dmi_system_id i8k_config_dmi_table[] __initconst = {
1427	{
1428		.ident = "Dell Latitude D520",
1429		.matches = {
1430			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1431			DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D520"),
1432		},
1433		.driver_data = (void *)&i8k_config_data[DELL_LATITUDE_D520],
1434	},
1435	{
1436		.ident = "Dell Studio",
1437		.matches = {
1438			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1439			DMI_MATCH(DMI_PRODUCT_NAME, "Studio"),
1440		},
1441		.driver_data = (void *)&i8k_config_data[DELL_STUDIO],
1442	},
1443	{
1444		.ident = "Dell XPS M140",
1445		.matches = {
1446			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1447			DMI_MATCH(DMI_PRODUCT_NAME, "MXC051"),
1448		},
1449		.driver_data = (void *)&i8k_config_data[DELL_XPS],
1450	},
1451	{ }
1452};
1453
1454/*
1455 * On some machines once I8K_SMM_GET_FAN_TYPE is issued then CPU fan speed
1456 * randomly going up and down due to bug in Dell SMM or BIOS. Here is blacklist
1457 * of affected Dell machines for which we disallow I8K_SMM_GET_FAN_TYPE call.
1458 * See bug: https://bugzilla.kernel.org/show_bug.cgi?id=100121
1459 */
1460static const struct dmi_system_id i8k_blacklist_fan_type_dmi_table[] __initconst = {
1461	{
1462		.ident = "Dell Studio XPS 8000",
1463		.matches = {
1464			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1465			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Studio XPS 8000"),
1466		},
1467	},
1468	{
1469		.ident = "Dell Studio XPS 8100",
1470		.matches = {
1471			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1472			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Studio XPS 8100"),
1473		},
1474	},
1475	{
1476		.ident = "Dell Inspiron 580",
1477		.matches = {
1478			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1479			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Inspiron 580 "),
1480		},
1481	},
1482	{
1483		.ident = "Dell Inspiron 3505",
1484		.matches = {
1485			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1486			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Inspiron 3505"),
1487		},
1488	},
1489	{ }
1490};
1491
1492/*
1493 * On some machines all fan related SMM functions implemented by Dell BIOS
1494 * firmware freeze kernel for about 500ms. Until Dell fixes these problems fan
1495 * support for affected blacklisted Dell machines stay disabled.
1496 * See bug: https://bugzilla.kernel.org/show_bug.cgi?id=195751
1497 */
1498static const struct dmi_system_id i8k_blacklist_fan_support_dmi_table[] __initconst = {
1499	{
1500		.ident = "Dell Inspiron 7720",
1501		.matches = {
1502			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1503			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Inspiron 7720"),
1504		},
1505	},
1506	{
1507		.ident = "Dell Vostro 3360",
1508		.matches = {
1509			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1510			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Vostro 3360"),
1511		},
1512	},
1513	{
1514		.ident = "Dell XPS13 9333",
1515		.matches = {
1516			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1517			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "XPS13 9333"),
1518		},
1519	},
1520	{
1521		.ident = "Dell XPS 15 L502X",
1522		.matches = {
1523			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1524			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Dell System XPS L502X"),
1525		},
1526	},
1527	{ }
1528};
1529
1530struct i8k_fan_control_data {
1531	unsigned int manual_fan;
1532	unsigned int auto_fan;
1533};
1534
1535enum i8k_fan_controls {
1536	I8K_FAN_30A3_31A3,
1537	I8K_FAN_34A3_35A3,
1538};
1539
1540static const struct i8k_fan_control_data i8k_fan_control_data[] __initconst = {
1541	[I8K_FAN_30A3_31A3] = {
1542		.manual_fan = 0x30a3,
1543		.auto_fan = 0x31a3,
1544	},
1545	[I8K_FAN_34A3_35A3] = {
1546		.manual_fan = 0x34a3,
1547		.auto_fan = 0x35a3,
1548	},
1549};
1550
1551static const struct dmi_system_id i8k_whitelist_fan_control[] __initconst = {
1552	{
1553		.ident = "Dell G5 5505",
1554		.matches = {
1555			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1556			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "G5 5505"),
1557
1558		},
1559		.driver_data = (void *)&i8k_fan_control_data[I8K_FAN_34A3_35A3],
1560	},
1561	{
1562		.ident = "Dell Latitude 5480",
1563		.matches = {
1564			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1565			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Latitude 5480"),
1566		},
1567		.driver_data = (void *)&i8k_fan_control_data[I8K_FAN_34A3_35A3],
1568	},
1569	{
1570		.ident = "Dell Latitude 7320",
1571		.matches = {
1572			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1573			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Latitude 7320"),
1574		},
1575		.driver_data = (void *)&i8k_fan_control_data[I8K_FAN_30A3_31A3],
1576	},
1577	{
1578		.ident = "Dell Latitude E6440",
1579		.matches = {
1580			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1581			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Latitude E6440"),
1582		},
1583		.driver_data = (void *)&i8k_fan_control_data[I8K_FAN_34A3_35A3],
1584	},
1585	{
1586		.ident = "Dell Latitude E7440",
1587		.matches = {
1588			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1589			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Latitude E7440"),
1590		},
1591		.driver_data = (void *)&i8k_fan_control_data[I8K_FAN_34A3_35A3],
1592	},
1593	{
1594		.ident = "Dell Precision 5530",
1595		.matches = {
1596			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1597			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Precision 5530"),
1598		},
1599		.driver_data = (void *)&i8k_fan_control_data[I8K_FAN_34A3_35A3],
1600	},
1601	{
1602		.ident = "Dell Precision 7510",
1603		.matches = {
1604			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1605			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Precision 7510"),
1606		},
1607		.driver_data = (void *)&i8k_fan_control_data[I8K_FAN_34A3_35A3],
1608	},
1609	{
1610		.ident = "Dell Precision 7540",
1611		.matches = {
1612			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1613			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Precision 7540"),
1614		},
1615		.driver_data = (void *)&i8k_fan_control_data[I8K_FAN_34A3_35A3],
1616	},
1617	{
1618		.ident = "Dell XPS 13 7390",
1619		.matches = {
1620			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1621			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "XPS 13 7390"),
1622		},
1623		.driver_data = (void *)&i8k_fan_control_data[I8K_FAN_34A3_35A3],
1624	},
1625	{
1626		.ident = "Dell XPS 13 9370",
1627		.matches = {
1628			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1629			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "XPS 13 9370"),
1630		},
1631		.driver_data = (void *)&i8k_fan_control_data[I8K_FAN_30A3_31A3],
1632	},
1633	{
1634		.ident = "Dell Optiplex 7000",
1635		.matches = {
1636			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1637			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "OptiPlex 7000"),
1638		},
1639		.driver_data = (void *)&i8k_fan_control_data[I8K_FAN_34A3_35A3],
1640	},
1641	{
1642		.ident = "Dell XPS 9315",
1643		.matches = {
1644			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1645			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "XPS 9315"),
1646		},
1647		.driver_data = (void *)&i8k_fan_control_data[I8K_FAN_30A3_31A3],
1648	},
1649	{
1650		.ident = "Dell G15 5510",
1651		.matches = {
1652			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1653			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Dell G15 5510"),
1654		},
1655		.driver_data = (void *)&i8k_fan_control_data[I8K_FAN_30A3_31A3],
1656	},
1657	{
1658		.ident = "Dell G15 5511",
1659		.matches = {
1660			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1661			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Dell G15 5511"),
1662		},
1663		.driver_data = (void *)&i8k_fan_control_data[I8K_FAN_30A3_31A3],
1664	},
1665	{ }
1666};
1667
1668/*
1669 * Legacy SMM backend driver.
1670 */
1671static int __init dell_smm_probe(struct platform_device *pdev)
1672{
1673	int ret;
1674
1675	ret = dell_smm_init_data(&pdev->dev, &i8k_smm_ops);
1676	if (ret < 0)
1677		return ret;
1678
1679	ret = dell_smm_init_hwmon(&pdev->dev);
1680	if (ret)
1681		return ret;
1682
1683	i8k_init_procfs(&pdev->dev);
1684
1685	return 0;
1686}
1687
1688static struct platform_driver dell_smm_driver = {
1689	.driver		= {
1690		.name	= KBUILD_MODNAME,
1691	},
1692};
1693
1694static struct platform_device *dell_smm_device;
1695
1696/*
1697 * WMI SMM backend driver.
1698 */
1699static int dell_smm_wmi_probe(struct wmi_device *wdev, const void *context)
1700{
1701	struct dell_smm_ops *ops;
1702	int ret;
1703
1704	ops = devm_kzalloc(&wdev->dev, sizeof(*ops), GFP_KERNEL);
1705	if (!ops)
1706		return -ENOMEM;
1707
1708	ops->smm_call = wmi_smm_call;
1709	ops->smm_dev = &wdev->dev;
1710
1711	if (dell_smm_get_signature(ops, I8K_SMM_GET_DELL_SIG1) &&
1712	    dell_smm_get_signature(ops, I8K_SMM_GET_DELL_SIG2))
1713		return -ENODEV;
1714
1715	ret = dell_smm_init_data(&wdev->dev, ops);
1716	if (ret < 0)
1717		return ret;
1718
1719	return dell_smm_init_hwmon(&wdev->dev);
1720}
1721
1722static const struct wmi_device_id dell_smm_wmi_id_table[] = {
1723	{ DELL_SMM_WMI_GUID, NULL },
1724	{ }
1725};
1726MODULE_DEVICE_TABLE(wmi, dell_smm_wmi_id_table);
1727
1728static struct wmi_driver dell_smm_wmi_driver = {
1729	.driver = {
1730		.name = KBUILD_MODNAME,
1731		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
1732	},
1733	.id_table = dell_smm_wmi_id_table,
1734	.probe = dell_smm_wmi_probe,
1735	.no_singleton = true,
1736};
1737
1738/*
1739 * Probe for the presence of a supported laptop.
1740 */
1741static void __init dell_smm_init_dmi(void)
1742{
1743	struct i8k_fan_control_data *control;
1744	struct i8k_config_data *config;
1745	const struct dmi_system_id *id;
1746
1747	if (dmi_check_system(i8k_blacklist_fan_support_dmi_table)) {
1748		if (!force) {
1749			pr_notice("Disabling fan support due to BIOS bugs\n");
1750			disallow_fan_support = true;
1751		} else {
1752			pr_warn("Enabling fan support despite BIOS bugs\n");
1753		}
1754	}
1755
1756	if (dmi_check_system(i8k_blacklist_fan_type_dmi_table)) {
1757		if (!force) {
1758			pr_notice("Disabling fan type call due to BIOS bugs\n");
1759			disallow_fan_type_call = true;
1760		} else {
1761			pr_warn("Enabling fan type call despite BIOS bugs\n");
1762		}
1763	}
1764
1765	/*
1766	 * Set fan multiplier and maximal fan speed from DMI config.
1767	 * Values specified in module parameters override values from DMI.
1768	 */
1769	id = dmi_first_match(i8k_config_dmi_table);
1770	if (id && id->driver_data) {
1771		config = id->driver_data;
1772		if (!fan_mult && config->fan_mult)
1773			fan_mult = config->fan_mult;
1774
1775		if (!fan_max && config->fan_max)
1776			fan_max = config->fan_max;
1777	}
1778
1779	id = dmi_first_match(i8k_whitelist_fan_control);
1780	if (id && id->driver_data) {
1781		control = id->driver_data;
1782		manual_fan = control->manual_fan;
1783		auto_fan = control->auto_fan;
1784
1785		pr_info("Enabling support for setting automatic/manual fan control\n");
1786	}
1787}
1788
1789static int __init dell_smm_legacy_check(void)
1790{
1791	if (!dmi_check_system(i8k_dmi_table)) {
1792		if (!ignore_dmi && !force)
1793			return -ENODEV;
1794
1795		pr_info("Probing for legacy SMM handler on unsupported machine\n");
1796		pr_info("vendor=%s, model=%s, version=%s\n",
1797			i8k_get_dmi_data(DMI_SYS_VENDOR),
1798			i8k_get_dmi_data(DMI_PRODUCT_NAME),
1799			i8k_get_dmi_data(DMI_BIOS_VERSION));
1800	}
1801
1802	if (dell_smm_get_signature(&i8k_smm_ops, I8K_SMM_GET_DELL_SIG1) &&
1803	    dell_smm_get_signature(&i8k_smm_ops, I8K_SMM_GET_DELL_SIG2)) {
1804		if (!force)
1805			return -ENODEV;
1806
1807		pr_warn("Forcing legacy SMM calls on a possibly incompatible machine\n");
1808	}
1809
1810	return 0;
1811}
1812
1813static int __init i8k_init(void)
1814{
1815	int ret;
1816
1817	dell_smm_init_dmi();
1818
1819	ret = dell_smm_legacy_check();
1820	if (ret < 0) {
1821		/*
1822		 * On modern machines, SMM communication happens over WMI, meaning
1823		 * the SMM handler might not react to legacy SMM calls.
1824		 */
1825		return wmi_driver_register(&dell_smm_wmi_driver);
1826	}
1827
1828	dell_smm_device = platform_create_bundle(&dell_smm_driver, dell_smm_probe, NULL, 0, NULL,
1829						 0);
1830
1831	return PTR_ERR_OR_ZERO(dell_smm_device);
1832}
1833
1834static void __exit i8k_exit(void)
1835{
1836	if (dell_smm_device) {
1837		platform_device_unregister(dell_smm_device);
1838		platform_driver_unregister(&dell_smm_driver);
1839	} else {
1840		wmi_driver_unregister(&dell_smm_wmi_driver);
1841	}
1842}
1843
1844module_init(i8k_init);
1845module_exit(i8k_exit);
~/ Read-only file preview.