Linux Audio

Check our new training course

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