• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
61 static DEFINE_MUTEX(i8k_mutex);
62 static char bios_version[4];
63 static char bios_machineid[16];
64 static struct device *i8k_hwmon_dev;
65 static u32 i8k_hwmon_flags;
66 static uint i8k_fan_mult = I8K_FAN_MULT;
67 static uint i8k_pwm_mult;
68 static uint i8k_fan_max = I8K_FAN_HIGH;
69 static bool disallow_fan_type_call;
70 static bool disallow_fan_support;
71 static unsigned int manual_fan;
72 static 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 
88 MODULE_AUTHOR("Massimo Dal Zotto (dz@debian.org)");
89 MODULE_AUTHOR("Pali Rohár <pali@kernel.org>");
90 MODULE_DESCRIPTION("Dell laptop SMM BIOS hwmon driver");
91 MODULE_LICENSE("GPL");
92 MODULE_ALIAS("i8k");
93 
94 static bool force;
95 module_param(force, bool, 0);
96 MODULE_PARM_DESC(force, "Force loading without checking for supported models");
97 
98 static bool ignore_dmi;
99 module_param(ignore_dmi, bool, 0);
100 MODULE_PARM_DESC(ignore_dmi, "Continue probing hardware even if DMI data does not match");
101 
102 #if IS_ENABLED(CONFIG_I8K)
103 static bool restricted = true;
104 module_param(restricted, bool, 0);
105 MODULE_PARM_DESC(restricted, "Restrict fan control and serial number to CAP_SYS_ADMIN (default: 1)");
106 
107 static bool power_status;
108 module_param(power_status, bool, 0600);
109 MODULE_PARM_DESC(power_status, "Report power status in /proc/i8k (default: 0)");
110 #endif
111 
112 static uint fan_mult;
113 module_param(fan_mult, uint, 0);
114 MODULE_PARM_DESC(fan_mult, "Factor to multiply fan speed with (default: autodetect)");
115 
116 static uint fan_max;
117 module_param(fan_max, uint, 0);
118 MODULE_PARM_DESC(fan_max, "Maximum configurable fan speed (default: autodetect)");
119 
120 struct 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 
i8k_get_dmi_data(int field)129 static 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  */
i8k_smm_func(void * par)139 static 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  */
i8k_smm(struct smm_regs * regs)227 static 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  */
i8k_get_fan_status(int fan)241 static 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  */
i8k_get_fan_speed(int fan)255 static 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  */
_i8k_get_fan_type(int fan)269 static 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 
i8k_get_fan_type(int fan)280 static 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  */
i8k_get_fan_nominal_speed(int fan,int speed)294 static 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  */
i8k_enable_fan_auto_mode(bool enable)308 static 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  */
i8k_set_fan(int fan,int speed)322 static 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 
i8k_get_temp_type(int sensor)335 static 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  */
_i8k_get_temp(int sensor)346 static 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 
i8k_get_temp(int sensor)356 static 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 
i8k_get_dell_signature(int req_fn)385 static 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  */
i8k_get_fn_status(void)402 static 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  */
i8k_get_power_status(void)426 static 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 
442 static int
i8k_ioctl_unlocked(struct file * fp,unsigned int cmd,unsigned long arg)443 i8k_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 
i8k_ioctl(struct file * fp,unsigned int cmd,unsigned long arg)538 static 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  */
i8k_proc_show(struct seq_file * seq,void * offset)552 static 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 
i8k_open_fs(struct inode * inode,struct file * file)593 static int i8k_open_fs(struct inode *inode, struct file *file)
594 {
595 	return single_open(file, i8k_proc_show, NULL);
596 }
597 
598 static 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 
606 static struct proc_dir_entry *entry;
607 
i8k_init_procfs(void)608 static void __init i8k_init_procfs(void)
609 {
610 	/* Register the proc entry */
611 	entry = proc_create("i8k", 0, NULL, &i8k_proc_ops);
612 }
613 
i8k_exit_procfs(void)614 static void __exit i8k_exit_procfs(void)
615 {
616 	if (entry)
617 		remove_proc_entry("i8k", NULL);
618 }
619 
620 #else
621 
i8k_init_procfs(void)622 static inline void __init i8k_init_procfs(void)
623 {
624 }
625 
i8k_exit_procfs(void)626 static inline void __exit i8k_exit_procfs(void)
627 {
628 }
629 
630 #endif
631 
632 /*
633  * Hwmon interface
634  */
635 
i8k_hwmon_temp_label_show(struct device * dev,struct device_attribute * devattr,char * buf)636 static ssize_t i8k_hwmon_temp_label_show(struct device *dev,
637 					 struct device_attribute *devattr,
638 					 char *buf)
639 {
640 	static const char * const labels[] = {
641 		"CPU",
642 		"GPU",
643 		"SODIMM",
644 		"Other",
645 		"Ambient",
646 		"Other",
647 	};
648 	int index = to_sensor_dev_attr(devattr)->index;
649 	int type;
650 
651 	type = i8k_get_temp_type(index);
652 	if (type < 0)
653 		return type;
654 	if (type >= ARRAY_SIZE(labels))
655 		type = ARRAY_SIZE(labels) - 1;
656 	return sprintf(buf, "%s\n", labels[type]);
657 }
658 
i8k_hwmon_temp_show(struct device * dev,struct device_attribute * devattr,char * buf)659 static ssize_t i8k_hwmon_temp_show(struct device *dev,
660 				   struct device_attribute *devattr,
661 				   char *buf)
662 {
663 	int index = to_sensor_dev_attr(devattr)->index;
664 	int temp;
665 
666 	temp = i8k_get_temp(index);
667 	if (temp < 0)
668 		return temp;
669 	return sprintf(buf, "%d\n", temp * 1000);
670 }
671 
i8k_hwmon_fan_label_show(struct device * dev,struct device_attribute * devattr,char * buf)672 static ssize_t i8k_hwmon_fan_label_show(struct device *dev,
673 					struct device_attribute *devattr,
674 					char *buf)
675 {
676 	static const char * const labels[] = {
677 		"Processor Fan",
678 		"Motherboard Fan",
679 		"Video Fan",
680 		"Power Supply Fan",
681 		"Chipset Fan",
682 		"Other Fan",
683 	};
684 	int index = to_sensor_dev_attr(devattr)->index;
685 	bool dock = false;
686 	int type;
687 
688 	type = i8k_get_fan_type(index);
689 	if (type < 0)
690 		return type;
691 
692 	if (type & 0x10) {
693 		dock = true;
694 		type &= 0x0F;
695 	}
696 
697 	if (type >= ARRAY_SIZE(labels))
698 		type = (ARRAY_SIZE(labels) - 1);
699 
700 	return sprintf(buf, "%s%s\n", (dock ? "Docking " : ""), labels[type]);
701 }
702 
i8k_hwmon_fan_show(struct device * dev,struct device_attribute * devattr,char * buf)703 static ssize_t i8k_hwmon_fan_show(struct device *dev,
704 				  struct device_attribute *devattr, char *buf)
705 {
706 	int index = to_sensor_dev_attr(devattr)->index;
707 	int fan_speed;
708 
709 	fan_speed = i8k_get_fan_speed(index);
710 	if (fan_speed < 0)
711 		return fan_speed;
712 	return sprintf(buf, "%d\n", fan_speed);
713 }
714 
i8k_hwmon_pwm_show(struct device * dev,struct device_attribute * devattr,char * buf)715 static ssize_t i8k_hwmon_pwm_show(struct device *dev,
716 				  struct device_attribute *devattr, char *buf)
717 {
718 	int index = to_sensor_dev_attr(devattr)->index;
719 	int status;
720 
721 	status = i8k_get_fan_status(index);
722 	if (status < 0)
723 		return -EIO;
724 	return sprintf(buf, "%d\n", clamp_val(status * i8k_pwm_mult, 0, 255));
725 }
726 
i8k_hwmon_pwm_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)727 static ssize_t i8k_hwmon_pwm_store(struct device *dev,
728 				   struct device_attribute *attr,
729 				   const char *buf, size_t count)
730 {
731 	int index = to_sensor_dev_attr(attr)->index;
732 	unsigned long val;
733 	int err;
734 
735 	err = kstrtoul(buf, 10, &val);
736 	if (err)
737 		return err;
738 	val = clamp_val(DIV_ROUND_CLOSEST(val, i8k_pwm_mult), 0, i8k_fan_max);
739 
740 	mutex_lock(&i8k_mutex);
741 	err = i8k_set_fan(index, val);
742 	mutex_unlock(&i8k_mutex);
743 
744 	return err < 0 ? -EIO : count;
745 }
746 
i8k_hwmon_pwm_enable_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)747 static ssize_t i8k_hwmon_pwm_enable_store(struct device *dev,
748 					  struct device_attribute *attr,
749 					  const char *buf, size_t count)
750 {
751 	int err;
752 	bool enable;
753 	unsigned long val;
754 
755 	if (!auto_fan)
756 		return -ENODEV;
757 
758 	err = kstrtoul(buf, 10, &val);
759 	if (err)
760 		return err;
761 
762 	if (val == 1)
763 		enable = false;
764 	else if (val == 2)
765 		enable = true;
766 	else
767 		return -EINVAL;
768 
769 	mutex_lock(&i8k_mutex);
770 	err = i8k_enable_fan_auto_mode(enable);
771 	mutex_unlock(&i8k_mutex);
772 
773 	return err ? err : count;
774 }
775 
776 static SENSOR_DEVICE_ATTR_RO(temp1_input, i8k_hwmon_temp, 0);
777 static SENSOR_DEVICE_ATTR_RO(temp1_label, i8k_hwmon_temp_label, 0);
778 static SENSOR_DEVICE_ATTR_RO(temp2_input, i8k_hwmon_temp, 1);
779 static SENSOR_DEVICE_ATTR_RO(temp2_label, i8k_hwmon_temp_label, 1);
780 static SENSOR_DEVICE_ATTR_RO(temp3_input, i8k_hwmon_temp, 2);
781 static SENSOR_DEVICE_ATTR_RO(temp3_label, i8k_hwmon_temp_label, 2);
782 static SENSOR_DEVICE_ATTR_RO(temp4_input, i8k_hwmon_temp, 3);
783 static SENSOR_DEVICE_ATTR_RO(temp4_label, i8k_hwmon_temp_label, 3);
784 static SENSOR_DEVICE_ATTR_RO(temp5_input, i8k_hwmon_temp, 4);
785 static SENSOR_DEVICE_ATTR_RO(temp5_label, i8k_hwmon_temp_label, 4);
786 static SENSOR_DEVICE_ATTR_RO(temp6_input, i8k_hwmon_temp, 5);
787 static SENSOR_DEVICE_ATTR_RO(temp6_label, i8k_hwmon_temp_label, 5);
788 static SENSOR_DEVICE_ATTR_RO(temp7_input, i8k_hwmon_temp, 6);
789 static SENSOR_DEVICE_ATTR_RO(temp7_label, i8k_hwmon_temp_label, 6);
790 static SENSOR_DEVICE_ATTR_RO(temp8_input, i8k_hwmon_temp, 7);
791 static SENSOR_DEVICE_ATTR_RO(temp8_label, i8k_hwmon_temp_label, 7);
792 static SENSOR_DEVICE_ATTR_RO(temp9_input, i8k_hwmon_temp, 8);
793 static SENSOR_DEVICE_ATTR_RO(temp9_label, i8k_hwmon_temp_label, 8);
794 static SENSOR_DEVICE_ATTR_RO(temp10_input, i8k_hwmon_temp, 9);
795 static SENSOR_DEVICE_ATTR_RO(temp10_label, i8k_hwmon_temp_label, 9);
796 static SENSOR_DEVICE_ATTR_RO(fan1_input, i8k_hwmon_fan, 0);
797 static SENSOR_DEVICE_ATTR_RO(fan1_label, i8k_hwmon_fan_label, 0);
798 static SENSOR_DEVICE_ATTR_RW(pwm1, i8k_hwmon_pwm, 0);
799 static SENSOR_DEVICE_ATTR_WO(pwm1_enable, i8k_hwmon_pwm_enable, 0);
800 static SENSOR_DEVICE_ATTR_RO(fan2_input, i8k_hwmon_fan, 1);
801 static SENSOR_DEVICE_ATTR_RO(fan2_label, i8k_hwmon_fan_label, 1);
802 static SENSOR_DEVICE_ATTR_RW(pwm2, i8k_hwmon_pwm, 1);
803 static SENSOR_DEVICE_ATTR_RO(fan3_input, i8k_hwmon_fan, 2);
804 static SENSOR_DEVICE_ATTR_RO(fan3_label, i8k_hwmon_fan_label, 2);
805 static SENSOR_DEVICE_ATTR_RW(pwm3, i8k_hwmon_pwm, 2);
806 
807 static struct attribute *i8k_attrs[] = {
808 	&sensor_dev_attr_temp1_input.dev_attr.attr,	/* 0 */
809 	&sensor_dev_attr_temp1_label.dev_attr.attr,	/* 1 */
810 	&sensor_dev_attr_temp2_input.dev_attr.attr,	/* 2 */
811 	&sensor_dev_attr_temp2_label.dev_attr.attr,	/* 3 */
812 	&sensor_dev_attr_temp3_input.dev_attr.attr,	/* 4 */
813 	&sensor_dev_attr_temp3_label.dev_attr.attr,	/* 5 */
814 	&sensor_dev_attr_temp4_input.dev_attr.attr,	/* 6 */
815 	&sensor_dev_attr_temp4_label.dev_attr.attr,	/* 7 */
816 	&sensor_dev_attr_temp5_input.dev_attr.attr,	/* 8 */
817 	&sensor_dev_attr_temp5_label.dev_attr.attr,	/* 9 */
818 	&sensor_dev_attr_temp6_input.dev_attr.attr,	/* 10 */
819 	&sensor_dev_attr_temp6_label.dev_attr.attr,	/* 11 */
820 	&sensor_dev_attr_temp7_input.dev_attr.attr,	/* 12 */
821 	&sensor_dev_attr_temp7_label.dev_attr.attr,	/* 13 */
822 	&sensor_dev_attr_temp8_input.dev_attr.attr,	/* 14 */
823 	&sensor_dev_attr_temp8_label.dev_attr.attr,	/* 15 */
824 	&sensor_dev_attr_temp9_input.dev_attr.attr,	/* 16 */
825 	&sensor_dev_attr_temp9_label.dev_attr.attr,	/* 17 */
826 	&sensor_dev_attr_temp10_input.dev_attr.attr,	/* 18 */
827 	&sensor_dev_attr_temp10_label.dev_attr.attr,	/* 19 */
828 	&sensor_dev_attr_fan1_input.dev_attr.attr,	/* 20 */
829 	&sensor_dev_attr_fan1_label.dev_attr.attr,	/* 21 */
830 	&sensor_dev_attr_pwm1.dev_attr.attr,		/* 22 */
831 	&sensor_dev_attr_pwm1_enable.dev_attr.attr,	/* 23 */
832 	&sensor_dev_attr_fan2_input.dev_attr.attr,	/* 24 */
833 	&sensor_dev_attr_fan2_label.dev_attr.attr,	/* 25 */
834 	&sensor_dev_attr_pwm2.dev_attr.attr,		/* 26 */
835 	&sensor_dev_attr_fan3_input.dev_attr.attr,	/* 27 */
836 	&sensor_dev_attr_fan3_label.dev_attr.attr,	/* 28 */
837 	&sensor_dev_attr_pwm3.dev_attr.attr,		/* 29 */
838 	NULL
839 };
840 
i8k_is_visible(struct kobject * kobj,struct attribute * attr,int index)841 static umode_t i8k_is_visible(struct kobject *kobj, struct attribute *attr,
842 			      int index)
843 {
844 	if (disallow_fan_support && index >= 20)
845 		return 0;
846 	if (disallow_fan_type_call &&
847 	    (index == 21 || index == 25 || index == 28))
848 		return 0;
849 	if (index >= 0 && index <= 1 &&
850 	    !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP1))
851 		return 0;
852 	if (index >= 2 && index <= 3 &&
853 	    !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP2))
854 		return 0;
855 	if (index >= 4 && index <= 5 &&
856 	    !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP3))
857 		return 0;
858 	if (index >= 6 && index <= 7 &&
859 	    !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP4))
860 		return 0;
861 	if (index >= 8 && index <= 9 &&
862 	    !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP5))
863 		return 0;
864 	if (index >= 10 && index <= 11 &&
865 	    !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP6))
866 		return 0;
867 	if (index >= 12 && index <= 13 &&
868 	    !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP7))
869 		return 0;
870 	if (index >= 14 && index <= 15 &&
871 	    !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP8))
872 		return 0;
873 	if (index >= 16 && index <= 17 &&
874 	    !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP9))
875 		return 0;
876 	if (index >= 18 && index <= 19 &&
877 	    !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP10))
878 		return 0;
879 
880 	if (index >= 20 && index <= 23 &&
881 	    !(i8k_hwmon_flags & I8K_HWMON_HAVE_FAN1))
882 		return 0;
883 	if (index >= 24 && index <= 26 &&
884 	    !(i8k_hwmon_flags & I8K_HWMON_HAVE_FAN2))
885 		return 0;
886 	if (index >= 27 && index <= 29 &&
887 	    !(i8k_hwmon_flags & I8K_HWMON_HAVE_FAN3))
888 		return 0;
889 
890 	if (index == 23 && !auto_fan)
891 		return 0;
892 
893 	return attr->mode;
894 }
895 
896 static const struct attribute_group i8k_group = {
897 	.attrs = i8k_attrs,
898 	.is_visible = i8k_is_visible,
899 };
900 __ATTRIBUTE_GROUPS(i8k);
901 
i8k_init_hwmon(void)902 static int __init i8k_init_hwmon(void)
903 {
904 	int err;
905 
906 	i8k_hwmon_flags = 0;
907 
908 	/* CPU temperature attributes, if temperature type is OK */
909 	err = i8k_get_temp_type(0);
910 	if (err >= 0)
911 		i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP1;
912 	/* check for additional temperature sensors */
913 	err = i8k_get_temp_type(1);
914 	if (err >= 0)
915 		i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP2;
916 	err = i8k_get_temp_type(2);
917 	if (err >= 0)
918 		i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP3;
919 	err = i8k_get_temp_type(3);
920 	if (err >= 0)
921 		i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP4;
922 	err = i8k_get_temp_type(4);
923 	if (err >= 0)
924 		i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP5;
925 	err = i8k_get_temp_type(5);
926 	if (err >= 0)
927 		i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP6;
928 	err = i8k_get_temp_type(6);
929 	if (err >= 0)
930 		i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP7;
931 	err = i8k_get_temp_type(7);
932 	if (err >= 0)
933 		i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP8;
934 	err = i8k_get_temp_type(8);
935 	if (err >= 0)
936 		i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP9;
937 	err = i8k_get_temp_type(9);
938 	if (err >= 0)
939 		i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP10;
940 
941 	/* First fan attributes, if fan status or type is OK */
942 	err = i8k_get_fan_status(0);
943 	if (err < 0)
944 		err = i8k_get_fan_type(0);
945 	if (err >= 0)
946 		i8k_hwmon_flags |= I8K_HWMON_HAVE_FAN1;
947 
948 	/* Second fan attributes, if fan status or type is OK */
949 	err = i8k_get_fan_status(1);
950 	if (err < 0)
951 		err = i8k_get_fan_type(1);
952 	if (err >= 0)
953 		i8k_hwmon_flags |= I8K_HWMON_HAVE_FAN2;
954 
955 	/* Third fan attributes, if fan status or type is OK */
956 	err = i8k_get_fan_status(2);
957 	if (err < 0)
958 		err = i8k_get_fan_type(2);
959 	if (err >= 0)
960 		i8k_hwmon_flags |= I8K_HWMON_HAVE_FAN3;
961 
962 	i8k_hwmon_dev = hwmon_device_register_with_groups(NULL, "dell_smm",
963 							  NULL, i8k_groups);
964 	if (IS_ERR(i8k_hwmon_dev)) {
965 		err = PTR_ERR(i8k_hwmon_dev);
966 		i8k_hwmon_dev = NULL;
967 		pr_err("hwmon registration failed (%d)\n", err);
968 		return err;
969 	}
970 	return 0;
971 }
972 
973 struct i8k_config_data {
974 	uint fan_mult;
975 	uint fan_max;
976 };
977 
978 enum i8k_configs {
979 	DELL_LATITUDE_D520,
980 	DELL_PRECISION_490,
981 	DELL_STUDIO,
982 	DELL_XPS,
983 };
984 
985 static const struct i8k_config_data i8k_config_data[] = {
986 	[DELL_LATITUDE_D520] = {
987 		.fan_mult = 1,
988 		.fan_max = I8K_FAN_TURBO,
989 	},
990 	[DELL_PRECISION_490] = {
991 		.fan_mult = 1,
992 		.fan_max = I8K_FAN_TURBO,
993 	},
994 	[DELL_STUDIO] = {
995 		.fan_mult = 1,
996 		.fan_max = I8K_FAN_HIGH,
997 	},
998 	[DELL_XPS] = {
999 		.fan_mult = 1,
1000 		.fan_max = I8K_FAN_HIGH,
1001 	},
1002 };
1003 
1004 static const struct dmi_system_id i8k_dmi_table[] __initconst = {
1005 	{
1006 		.ident = "Dell Inspiron",
1007 		.matches = {
1008 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer"),
1009 			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron"),
1010 		},
1011 	},
1012 	{
1013 		.ident = "Dell Latitude",
1014 		.matches = {
1015 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer"),
1016 			DMI_MATCH(DMI_PRODUCT_NAME, "Latitude"),
1017 		},
1018 	},
1019 	{
1020 		.ident = "Dell Inspiron 2",
1021 		.matches = {
1022 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1023 			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron"),
1024 		},
1025 	},
1026 	{
1027 		.ident = "Dell Latitude D520",
1028 		.matches = {
1029 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1030 			DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D520"),
1031 		},
1032 		.driver_data = (void *)&i8k_config_data[DELL_LATITUDE_D520],
1033 	},
1034 	{
1035 		.ident = "Dell Latitude 2",
1036 		.matches = {
1037 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1038 			DMI_MATCH(DMI_PRODUCT_NAME, "Latitude"),
1039 		},
1040 	},
1041 	{	/* UK Inspiron 6400  */
1042 		.ident = "Dell Inspiron 3",
1043 		.matches = {
1044 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1045 			DMI_MATCH(DMI_PRODUCT_NAME, "MM061"),
1046 		},
1047 	},
1048 	{
1049 		.ident = "Dell Inspiron 3",
1050 		.matches = {
1051 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1052 			DMI_MATCH(DMI_PRODUCT_NAME, "MP061"),
1053 		},
1054 	},
1055 	{
1056 		.ident = "Dell Precision 490",
1057 		.matches = {
1058 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1059 			DMI_MATCH(DMI_PRODUCT_NAME,
1060 				  "Precision WorkStation 490"),
1061 		},
1062 		.driver_data = (void *)&i8k_config_data[DELL_PRECISION_490],
1063 	},
1064 	{
1065 		.ident = "Dell Precision",
1066 		.matches = {
1067 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1068 			DMI_MATCH(DMI_PRODUCT_NAME, "Precision"),
1069 		},
1070 	},
1071 	{
1072 		.ident = "Dell Vostro",
1073 		.matches = {
1074 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1075 			DMI_MATCH(DMI_PRODUCT_NAME, "Vostro"),
1076 		},
1077 	},
1078 	{
1079 		.ident = "Dell Studio",
1080 		.matches = {
1081 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1082 			DMI_MATCH(DMI_PRODUCT_NAME, "Studio"),
1083 		},
1084 		.driver_data = (void *)&i8k_config_data[DELL_STUDIO],
1085 	},
1086 	{
1087 		.ident = "Dell XPS M140",
1088 		.matches = {
1089 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1090 			DMI_MATCH(DMI_PRODUCT_NAME, "MXC051"),
1091 		},
1092 		.driver_data = (void *)&i8k_config_data[DELL_XPS],
1093 	},
1094 	{
1095 		.ident = "Dell XPS",
1096 		.matches = {
1097 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1098 			DMI_MATCH(DMI_PRODUCT_NAME, "XPS"),
1099 		},
1100 	},
1101 	{ }
1102 };
1103 
1104 MODULE_DEVICE_TABLE(dmi, i8k_dmi_table);
1105 
1106 /*
1107  * On some machines once I8K_SMM_GET_FAN_TYPE is issued then CPU fan speed
1108  * randomly going up and down due to bug in Dell SMM or BIOS. Here is blacklist
1109  * of affected Dell machines for which we disallow I8K_SMM_GET_FAN_TYPE call.
1110  * See bug: https://bugzilla.kernel.org/show_bug.cgi?id=100121
1111  */
1112 static const struct dmi_system_id i8k_blacklist_fan_type_dmi_table[] __initconst = {
1113 	{
1114 		.ident = "Dell Studio XPS 8000",
1115 		.matches = {
1116 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1117 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Studio XPS 8000"),
1118 		},
1119 	},
1120 	{
1121 		.ident = "Dell Studio XPS 8100",
1122 		.matches = {
1123 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1124 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Studio XPS 8100"),
1125 		},
1126 	},
1127 	{
1128 		.ident = "Dell Inspiron 580",
1129 		.matches = {
1130 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1131 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Inspiron 580 "),
1132 		},
1133 	},
1134 	{ }
1135 };
1136 
1137 /*
1138  * On some machines all fan related SMM functions implemented by Dell BIOS
1139  * firmware freeze kernel for about 500ms. Until Dell fixes these problems fan
1140  * support for affected blacklisted Dell machines stay disabled.
1141  * See bug: https://bugzilla.kernel.org/show_bug.cgi?id=195751
1142  */
1143 static struct dmi_system_id i8k_blacklist_fan_support_dmi_table[] __initdata = {
1144 	{
1145 		.ident = "Dell Inspiron 7720",
1146 		.matches = {
1147 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1148 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Inspiron 7720"),
1149 		},
1150 	},
1151 	{
1152 		.ident = "Dell Vostro 3360",
1153 		.matches = {
1154 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1155 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Vostro 3360"),
1156 		},
1157 	},
1158 	{
1159 		.ident = "Dell XPS13 9333",
1160 		.matches = {
1161 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1162 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "XPS13 9333"),
1163 		},
1164 	},
1165 	{
1166 		.ident = "Dell XPS 15 L502X",
1167 		.matches = {
1168 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1169 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Dell System XPS L502X"),
1170 		},
1171 	},
1172 	{ }
1173 };
1174 
1175 struct i8k_fan_control_data {
1176 	unsigned int manual_fan;
1177 	unsigned int auto_fan;
1178 };
1179 
1180 enum i8k_fan_controls {
1181 	I8K_FAN_34A3_35A3,
1182 };
1183 
1184 static const struct i8k_fan_control_data i8k_fan_control_data[] = {
1185 	[I8K_FAN_34A3_35A3] = {
1186 		.manual_fan = 0x34a3,
1187 		.auto_fan = 0x35a3,
1188 	},
1189 };
1190 
1191 static struct dmi_system_id i8k_whitelist_fan_control[] __initdata = {
1192 	{
1193 		.ident = "Dell Precision 5530",
1194 		.matches = {
1195 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1196 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Precision 5530"),
1197 		},
1198 		.driver_data = (void *)&i8k_fan_control_data[I8K_FAN_34A3_35A3],
1199 	},
1200 	{
1201 		.ident = "Dell Latitude 5480",
1202 		.matches = {
1203 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1204 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Latitude 5480"),
1205 		},
1206 		.driver_data = (void *)&i8k_fan_control_data[I8K_FAN_34A3_35A3],
1207 	},
1208 	{
1209 		.ident = "Dell Latitude E6440",
1210 		.matches = {
1211 			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
1212 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Latitude E6440"),
1213 		},
1214 		.driver_data = (void *)&i8k_fan_control_data[I8K_FAN_34A3_35A3],
1215 	},
1216 	{ }
1217 };
1218 
1219 /*
1220  * Probe for the presence of a supported laptop.
1221  */
i8k_probe(void)1222 static int __init i8k_probe(void)
1223 {
1224 	const struct dmi_system_id *id, *fan_control;
1225 	int fan, ret;
1226 
1227 	/*
1228 	 * Get DMI information
1229 	 */
1230 	if (!dmi_check_system(i8k_dmi_table)) {
1231 		if (!ignore_dmi && !force)
1232 			return -ENODEV;
1233 
1234 		pr_info("not running on a supported Dell system.\n");
1235 		pr_info("vendor=%s, model=%s, version=%s\n",
1236 			i8k_get_dmi_data(DMI_SYS_VENDOR),
1237 			i8k_get_dmi_data(DMI_PRODUCT_NAME),
1238 			i8k_get_dmi_data(DMI_BIOS_VERSION));
1239 	}
1240 
1241 	if (dmi_check_system(i8k_blacklist_fan_support_dmi_table)) {
1242 		pr_warn("broken Dell BIOS detected, disallow fan support\n");
1243 		if (!force)
1244 			disallow_fan_support = true;
1245 	}
1246 
1247 	if (dmi_check_system(i8k_blacklist_fan_type_dmi_table)) {
1248 		pr_warn("broken Dell BIOS detected, disallow fan type call\n");
1249 		if (!force)
1250 			disallow_fan_type_call = true;
1251 	}
1252 
1253 	strlcpy(bios_version, i8k_get_dmi_data(DMI_BIOS_VERSION),
1254 		sizeof(bios_version));
1255 	strlcpy(bios_machineid, i8k_get_dmi_data(DMI_PRODUCT_SERIAL),
1256 		sizeof(bios_machineid));
1257 
1258 	/*
1259 	 * Get SMM Dell signature
1260 	 */
1261 	if (i8k_get_dell_signature(I8K_SMM_GET_DELL_SIG1) &&
1262 	    i8k_get_dell_signature(I8K_SMM_GET_DELL_SIG2)) {
1263 		pr_err("unable to get SMM Dell signature\n");
1264 		if (!force)
1265 			return -ENODEV;
1266 	}
1267 
1268 	/*
1269 	 * Set fan multiplier and maximal fan speed from dmi config
1270 	 * Values specified in module parameters override values from dmi
1271 	 */
1272 	id = dmi_first_match(i8k_dmi_table);
1273 	if (id && id->driver_data) {
1274 		const struct i8k_config_data *conf = id->driver_data;
1275 		if (!fan_mult && conf->fan_mult)
1276 			fan_mult = conf->fan_mult;
1277 		if (!fan_max && conf->fan_max)
1278 			fan_max = conf->fan_max;
1279 	}
1280 
1281 	i8k_fan_max = fan_max ? : I8K_FAN_HIGH;	/* Must not be 0 */
1282 	i8k_pwm_mult = DIV_ROUND_UP(255, i8k_fan_max);
1283 
1284 	fan_control = dmi_first_match(i8k_whitelist_fan_control);
1285 	if (fan_control && fan_control->driver_data) {
1286 		const struct i8k_fan_control_data *data = fan_control->driver_data;
1287 
1288 		manual_fan = data->manual_fan;
1289 		auto_fan = data->auto_fan;
1290 		pr_info("enabling support for setting automatic/manual fan control\n");
1291 	}
1292 
1293 	if (!fan_mult) {
1294 		/*
1295 		 * Autodetect fan multiplier based on nominal rpm
1296 		 * If fan reports rpm value too high then set multiplier to 1
1297 		 */
1298 		for (fan = 0; fan < 2; ++fan) {
1299 			ret = i8k_get_fan_nominal_speed(fan, i8k_fan_max);
1300 			if (ret < 0)
1301 				continue;
1302 			if (ret > I8K_FAN_MAX_RPM)
1303 				i8k_fan_mult = 1;
1304 			break;
1305 		}
1306 	} else {
1307 		/* Fan multiplier was specified in module param or in dmi */
1308 		i8k_fan_mult = fan_mult;
1309 	}
1310 
1311 	return 0;
1312 }
1313 
i8k_init(void)1314 static int __init i8k_init(void)
1315 {
1316 	int err;
1317 
1318 	/* Are we running on an supported laptop? */
1319 	if (i8k_probe())
1320 		return -ENODEV;
1321 
1322 	err = i8k_init_hwmon();
1323 	if (err)
1324 		return err;
1325 
1326 	i8k_init_procfs();
1327 	return 0;
1328 }
1329 
i8k_exit(void)1330 static void __exit i8k_exit(void)
1331 {
1332 	hwmon_device_unregister(i8k_hwmon_dev);
1333 	i8k_exit_procfs();
1334 }
1335 
1336 module_init(i8k_init);
1337 module_exit(i8k_exit);
1338