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