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