]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * i8k.c -- Linux driver for accessing the SMM BIOS on Dell laptops. | |
1da177e4 LT |
3 | * |
4 | * Copyright (C) 2001 Massimo Dal Zotto <[email protected]> | |
5 | * | |
949a9d70 | 6 | * Hwmon integration: |
7c81c60f | 7 | * Copyright (C) 2011 Jean Delvare <[email protected]> |
f5a7f827 | 8 | * Copyright (C) 2013 Guenter Roeck <[email protected]> |
949a9d70 | 9 | * |
1da177e4 LT |
10 | * This program is free software; you can redistribute it and/or modify it |
11 | * under the terms of the GNU General Public License as published by the | |
12 | * Free Software Foundation; either version 2, or (at your option) any | |
13 | * later version. | |
14 | * | |
15 | * This program is distributed in the hope that it will be useful, but | |
16 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
18 | * General Public License for more details. | |
19 | */ | |
20 | ||
60e71aaf GR |
21 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
22 | ||
1da177e4 LT |
23 | #include <linux/module.h> |
24 | #include <linux/types.h> | |
25 | #include <linux/init.h> | |
26 | #include <linux/proc_fs.h> | |
352f8f8b | 27 | #include <linux/seq_file.h> |
e70c9d5e | 28 | #include <linux/dmi.h> |
7e80d0d0 | 29 | #include <linux/capability.h> |
613655fa | 30 | #include <linux/mutex.h> |
949a9d70 JD |
31 | #include <linux/hwmon.h> |
32 | #include <linux/hwmon-sysfs.h> | |
12186f51 GR |
33 | #include <linux/uaccess.h> |
34 | #include <linux/io.h> | |
f36fdb9f | 35 | #include <linux/sched.h> |
1da177e4 LT |
36 | |
37 | #include <linux/i8k.h> | |
38 | ||
1da177e4 LT |
39 | #define I8K_SMM_FN_STATUS 0x0025 |
40 | #define I8K_SMM_POWER_STATUS 0x0069 | |
41 | #define I8K_SMM_SET_FAN 0x01a3 | |
42 | #define I8K_SMM_GET_FAN 0x00a3 | |
43 | #define I8K_SMM_GET_SPEED 0x02a3 | |
44 | #define I8K_SMM_GET_TEMP 0x10a3 | |
7e0fa31d DT |
45 | #define I8K_SMM_GET_DELL_SIG1 0xfea3 |
46 | #define I8K_SMM_GET_DELL_SIG2 0xffa3 | |
1da177e4 LT |
47 | |
48 | #define I8K_FAN_MULT 30 | |
49 | #define I8K_MAX_TEMP 127 | |
50 | ||
51 | #define I8K_FN_NONE 0x00 | |
52 | #define I8K_FN_UP 0x01 | |
53 | #define I8K_FN_DOWN 0x02 | |
54 | #define I8K_FN_MUTE 0x04 | |
55 | #define I8K_FN_MASK 0x07 | |
56 | #define I8K_FN_SHIFT 8 | |
57 | ||
58 | #define I8K_POWER_AC 0x05 | |
59 | #define I8K_POWER_BATTERY 0x01 | |
60 | ||
61 | #define I8K_TEMPERATURE_BUG 1 | |
62 | ||
613655fa | 63 | static DEFINE_MUTEX(i8k_mutex); |
e70c9d5e | 64 | static char bios_version[4]; |
949a9d70 | 65 | static struct device *i8k_hwmon_dev; |
82ba1d3f | 66 | static u32 i8k_hwmon_flags; |
26d09382 | 67 | static int i8k_fan_mult; |
81474fc2 GR |
68 | static int i8k_pwm_mult; |
69 | static int i8k_fan_max = I8K_FAN_HIGH; | |
82ba1d3f GR |
70 | |
71 | #define I8K_HWMON_HAVE_TEMP1 (1 << 0) | |
d83c39de GR |
72 | #define I8K_HWMON_HAVE_TEMP2 (1 << 1) |
73 | #define I8K_HWMON_HAVE_TEMP3 (1 << 2) | |
74 | #define I8K_HWMON_HAVE_TEMP4 (1 << 3) | |
75 | #define I8K_HWMON_HAVE_FAN1 (1 << 4) | |
76 | #define I8K_HWMON_HAVE_FAN2 (1 << 5) | |
1da177e4 LT |
77 | |
78 | MODULE_AUTHOR("Massimo Dal Zotto ([email protected])"); | |
79 | MODULE_DESCRIPTION("Driver for accessing SMM BIOS on Dell laptops"); | |
80 | MODULE_LICENSE("GPL"); | |
81 | ||
90ab5ee9 | 82 | static bool force; |
1da177e4 LT |
83 | module_param(force, bool, 0); |
84 | MODULE_PARM_DESC(force, "Force loading without checking for supported models"); | |
85 | ||
90ab5ee9 | 86 | static bool ignore_dmi; |
e70c9d5e DT |
87 | module_param(ignore_dmi, bool, 0); |
88 | MODULE_PARM_DESC(ignore_dmi, "Continue probing hardware even if DMI data does not match"); | |
89 | ||
90ab5ee9 | 90 | static bool restricted; |
1da177e4 LT |
91 | module_param(restricted, bool, 0); |
92 | MODULE_PARM_DESC(restricted, "Allow fan control if SYS_ADMIN capability set"); | |
93 | ||
90ab5ee9 | 94 | static bool power_status; |
1da177e4 LT |
95 | module_param(power_status, bool, 0600); |
96 | MODULE_PARM_DESC(power_status, "Report power status in /proc/i8k"); | |
97 | ||
4ed99a27 JE |
98 | static int fan_mult = I8K_FAN_MULT; |
99 | module_param(fan_mult, int, 0); | |
100 | MODULE_PARM_DESC(fan_mult, "Factor to multiply fan speed with"); | |
101 | ||
81474fc2 GR |
102 | static int fan_max = I8K_FAN_HIGH; |
103 | module_param(fan_max, int, 0); | |
104 | MODULE_PARM_DESC(fan_max, "Maximum configurable fan speed"); | |
105 | ||
352f8f8b | 106 | static int i8k_open_fs(struct inode *inode, struct file *file); |
d79b6f4d | 107 | static long i8k_ioctl(struct file *, unsigned int, unsigned long); |
1da177e4 | 108 | |
62322d25 | 109 | static const struct file_operations i8k_fops = { |
1b502217 | 110 | .owner = THIS_MODULE, |
352f8f8b DT |
111 | .open = i8k_open_fs, |
112 | .read = seq_read, | |
113 | .llseek = seq_lseek, | |
114 | .release = single_release, | |
d79b6f4d | 115 | .unlocked_ioctl = i8k_ioctl, |
1da177e4 LT |
116 | }; |
117 | ||
8378b924 | 118 | struct smm_regs { |
dec63ec3 | 119 | unsigned int eax; |
12186f51 GR |
120 | unsigned int ebx __packed; |
121 | unsigned int ecx __packed; | |
122 | unsigned int edx __packed; | |
123 | unsigned int esi __packed; | |
124 | unsigned int edi __packed; | |
8378b924 | 125 | }; |
1da177e4 | 126 | |
1855256c | 127 | static inline const char *i8k_get_dmi_data(int field) |
e70c9d5e | 128 | { |
1855256c | 129 | const char *dmi_data = dmi_get_system_info(field); |
4f005551 DT |
130 | |
131 | return dmi_data && *dmi_data ? dmi_data : "?"; | |
e70c9d5e | 132 | } |
1da177e4 LT |
133 | |
134 | /* | |
135 | * Call the System Management Mode BIOS. Code provided by Jonathan Buzzard. | |
136 | */ | |
8378b924 | 137 | static int i8k_smm(struct smm_regs *regs) |
1da177e4 | 138 | { |
dec63ec3 DT |
139 | int rc; |
140 | int eax = regs->eax; | |
f36fdb9f GR |
141 | cpumask_var_t old_mask; |
142 | ||
143 | /* SMM requires CPU 0 */ | |
144 | if (!alloc_cpumask_var(&old_mask, GFP_KERNEL)) | |
145 | return -ENOMEM; | |
146 | cpumask_copy(old_mask, ¤t->cpus_allowed); | |
6d827fbc GR |
147 | rc = set_cpus_allowed_ptr(current, cpumask_of(0)); |
148 | if (rc) | |
149 | goto out; | |
f36fdb9f GR |
150 | if (smp_processor_id() != 0) { |
151 | rc = -EBUSY; | |
152 | goto out; | |
153 | } | |
dec63ec3 | 154 | |
fe04f22f | 155 | #if defined(CONFIG_X86_64) |
22d3243d | 156 | asm volatile("pushq %%rax\n\t" |
fe04f22f BS |
157 | "movl 0(%%rax),%%edx\n\t" |
158 | "pushq %%rdx\n\t" | |
159 | "movl 4(%%rax),%%ebx\n\t" | |
160 | "movl 8(%%rax),%%ecx\n\t" | |
161 | "movl 12(%%rax),%%edx\n\t" | |
162 | "movl 16(%%rax),%%esi\n\t" | |
163 | "movl 20(%%rax),%%edi\n\t" | |
164 | "popq %%rax\n\t" | |
165 | "out %%al,$0xb2\n\t" | |
166 | "out %%al,$0x84\n\t" | |
167 | "xchgq %%rax,(%%rsp)\n\t" | |
168 | "movl %%ebx,4(%%rax)\n\t" | |
169 | "movl %%ecx,8(%%rax)\n\t" | |
170 | "movl %%edx,12(%%rax)\n\t" | |
171 | "movl %%esi,16(%%rax)\n\t" | |
172 | "movl %%edi,20(%%rax)\n\t" | |
173 | "popq %%rdx\n\t" | |
174 | "movl %%edx,0(%%rax)\n\t" | |
bc1f419c LT |
175 | "pushfq\n\t" |
176 | "popq %%rax\n\t" | |
fe04f22f | 177 | "andl $1,%%eax\n" |
12186f51 | 178 | : "=a"(rc) |
fe04f22f BS |
179 | : "a"(regs) |
180 | : "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory"); | |
181 | #else | |
22d3243d | 182 | asm volatile("pushl %%eax\n\t" |
dec63ec3 DT |
183 | "movl 0(%%eax),%%edx\n\t" |
184 | "push %%edx\n\t" | |
185 | "movl 4(%%eax),%%ebx\n\t" | |
186 | "movl 8(%%eax),%%ecx\n\t" | |
187 | "movl 12(%%eax),%%edx\n\t" | |
188 | "movl 16(%%eax),%%esi\n\t" | |
189 | "movl 20(%%eax),%%edi\n\t" | |
190 | "popl %%eax\n\t" | |
191 | "out %%al,$0xb2\n\t" | |
192 | "out %%al,$0x84\n\t" | |
193 | "xchgl %%eax,(%%esp)\n\t" | |
194 | "movl %%ebx,4(%%eax)\n\t" | |
195 | "movl %%ecx,8(%%eax)\n\t" | |
196 | "movl %%edx,12(%%eax)\n\t" | |
197 | "movl %%esi,16(%%eax)\n\t" | |
198 | "movl %%edi,20(%%eax)\n\t" | |
199 | "popl %%edx\n\t" | |
200 | "movl %%edx,0(%%eax)\n\t" | |
201 | "lahf\n\t" | |
202 | "shrl $8,%%eax\n\t" | |
6b4e81db | 203 | "andl $1,%%eax\n" |
12186f51 | 204 | : "=a"(rc) |
dec63ec3 DT |
205 | : "a"(regs) |
206 | : "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory"); | |
fe04f22f | 207 | #endif |
8378b924 | 208 | if (rc != 0 || (regs->eax & 0xffff) == 0xffff || regs->eax == eax) |
f36fdb9f | 209 | rc = -EINVAL; |
dec63ec3 | 210 | |
f36fdb9f GR |
211 | out: |
212 | set_cpus_allowed_ptr(current, old_mask); | |
213 | free_cpumask_var(old_mask); | |
214 | return rc; | |
1da177e4 LT |
215 | } |
216 | ||
1da177e4 LT |
217 | /* |
218 | * Read the Fn key status. | |
219 | */ | |
220 | static int i8k_get_fn_status(void) | |
221 | { | |
8378b924 | 222 | struct smm_regs regs = { .eax = I8K_SMM_FN_STATUS, }; |
dec63ec3 DT |
223 | int rc; |
224 | ||
12186f51 GR |
225 | rc = i8k_smm(®s); |
226 | if (rc < 0) | |
dec63ec3 | 227 | return rc; |
dec63ec3 DT |
228 | |
229 | switch ((regs.eax >> I8K_FN_SHIFT) & I8K_FN_MASK) { | |
230 | case I8K_FN_UP: | |
231 | return I8K_VOL_UP; | |
232 | case I8K_FN_DOWN: | |
233 | return I8K_VOL_DOWN; | |
234 | case I8K_FN_MUTE: | |
235 | return I8K_VOL_MUTE; | |
236 | default: | |
237 | return 0; | |
238 | } | |
1da177e4 LT |
239 | } |
240 | ||
241 | /* | |
242 | * Read the power status. | |
243 | */ | |
244 | static int i8k_get_power_status(void) | |
245 | { | |
8378b924 | 246 | struct smm_regs regs = { .eax = I8K_SMM_POWER_STATUS, }; |
dec63ec3 DT |
247 | int rc; |
248 | ||
12186f51 GR |
249 | rc = i8k_smm(®s); |
250 | if (rc < 0) | |
dec63ec3 | 251 | return rc; |
dec63ec3 | 252 | |
8378b924 | 253 | return (regs.eax & 0xff) == I8K_POWER_AC ? I8K_AC : I8K_BATTERY; |
1da177e4 LT |
254 | } |
255 | ||
256 | /* | |
257 | * Read the fan status. | |
258 | */ | |
259 | static int i8k_get_fan_status(int fan) | |
260 | { | |
8378b924 | 261 | struct smm_regs regs = { .eax = I8K_SMM_GET_FAN, }; |
1da177e4 | 262 | |
dec63ec3 | 263 | regs.ebx = fan & 0xff; |
8378b924 | 264 | return i8k_smm(®s) ? : regs.eax & 0xff; |
1da177e4 LT |
265 | } |
266 | ||
267 | /* | |
268 | * Read the fan speed in RPM. | |
269 | */ | |
270 | static int i8k_get_fan_speed(int fan) | |
271 | { | |
8378b924 | 272 | struct smm_regs regs = { .eax = I8K_SMM_GET_SPEED, }; |
1da177e4 | 273 | |
dec63ec3 | 274 | regs.ebx = fan & 0xff; |
26d09382 | 275 | return i8k_smm(®s) ? : (regs.eax & 0xffff) * i8k_fan_mult; |
1da177e4 LT |
276 | } |
277 | ||
278 | /* | |
279 | * Set the fan speed (off, low, high). Returns the new fan status. | |
280 | */ | |
281 | static int i8k_set_fan(int fan, int speed) | |
282 | { | |
8378b924 | 283 | struct smm_regs regs = { .eax = I8K_SMM_SET_FAN, }; |
1da177e4 | 284 | |
81474fc2 | 285 | speed = (speed < 0) ? 0 : ((speed > i8k_fan_max) ? i8k_fan_max : speed); |
dec63ec3 | 286 | regs.ebx = (fan & 0xff) | (speed << 8); |
1da177e4 | 287 | |
8378b924 | 288 | return i8k_smm(®s) ? : i8k_get_fan_status(fan); |
1da177e4 LT |
289 | } |
290 | ||
291 | /* | |
292 | * Read the cpu temperature. | |
293 | */ | |
7e0fa31d | 294 | static int i8k_get_temp(int sensor) |
1da177e4 | 295 | { |
8378b924 | 296 | struct smm_regs regs = { .eax = I8K_SMM_GET_TEMP, }; |
dec63ec3 DT |
297 | int rc; |
298 | int temp; | |
1da177e4 LT |
299 | |
300 | #ifdef I8K_TEMPERATURE_BUG | |
723493ca | 301 | static int prev[4] = { I8K_MAX_TEMP+1, I8K_MAX_TEMP+1, I8K_MAX_TEMP+1, I8K_MAX_TEMP+1 }; |
1da177e4 | 302 | #endif |
7e0fa31d | 303 | regs.ebx = sensor & 0xff; |
12186f51 GR |
304 | rc = i8k_smm(®s); |
305 | if (rc < 0) | |
dec63ec3 | 306 | return rc; |
8378b924 | 307 | |
dec63ec3 | 308 | temp = regs.eax & 0xff; |
1da177e4 LT |
309 | |
310 | #ifdef I8K_TEMPERATURE_BUG | |
dec63ec3 DT |
311 | /* |
312 | * Sometimes the temperature sensor returns 0x99, which is out of range. | |
313 | * In this case we return (once) the previous cached value. For example: | |
314 | # 1003655137 00000058 00005a4b | |
315 | # 1003655138 00000099 00003a80 <--- 0x99 = 153 degrees | |
316 | # 1003655139 00000054 00005c52 | |
317 | */ | |
318 | if (temp > I8K_MAX_TEMP) { | |
d83c39de | 319 | temp = prev[sensor]; |
723493ca | 320 | prev[sensor] = I8K_MAX_TEMP+1; |
dec63ec3 | 321 | } else { |
d83c39de | 322 | prev[sensor] = temp; |
dec63ec3 | 323 | } |
723493ca PR |
324 | if (temp > I8K_MAX_TEMP) |
325 | return -ERANGE; | |
1da177e4 LT |
326 | #endif |
327 | ||
dec63ec3 | 328 | return temp; |
1da177e4 LT |
329 | } |
330 | ||
7e0fa31d | 331 | static int i8k_get_dell_signature(int req_fn) |
1da177e4 | 332 | { |
7e0fa31d | 333 | struct smm_regs regs = { .eax = req_fn, }; |
dec63ec3 | 334 | int rc; |
1da177e4 | 335 | |
12186f51 GR |
336 | rc = i8k_smm(®s); |
337 | if (rc < 0) | |
dec63ec3 | 338 | return rc; |
1da177e4 | 339 | |
8378b924 | 340 | return regs.eax == 1145651527 && regs.edx == 1145392204 ? 0 : -1; |
1da177e4 LT |
341 | } |
342 | ||
d79b6f4d FW |
343 | static int |
344 | i8k_ioctl_unlocked(struct file *fp, unsigned int cmd, unsigned long arg) | |
1da177e4 | 345 | { |
e70c9d5e | 346 | int val = 0; |
dec63ec3 DT |
347 | int speed; |
348 | unsigned char buff[16]; | |
349 | int __user *argp = (int __user *)arg; | |
350 | ||
351 | if (!argp) | |
352 | return -EINVAL; | |
353 | ||
354 | switch (cmd) { | |
355 | case I8K_BIOS_VERSION: | |
e551b152 GR |
356 | val = (bios_version[0] << 16) | |
357 | (bios_version[1] << 8) | bios_version[2]; | |
dec63ec3 DT |
358 | break; |
359 | ||
360 | case I8K_MACHINE_ID: | |
361 | memset(buff, 0, 16); | |
12186f51 GR |
362 | strlcpy(buff, i8k_get_dmi_data(DMI_PRODUCT_SERIAL), |
363 | sizeof(buff)); | |
dec63ec3 DT |
364 | break; |
365 | ||
366 | case I8K_FN_STATUS: | |
367 | val = i8k_get_fn_status(); | |
368 | break; | |
369 | ||
370 | case I8K_POWER_STATUS: | |
371 | val = i8k_get_power_status(); | |
372 | break; | |
373 | ||
374 | case I8K_GET_TEMP: | |
7e0fa31d | 375 | val = i8k_get_temp(0); |
dec63ec3 DT |
376 | break; |
377 | ||
378 | case I8K_GET_SPEED: | |
8378b924 | 379 | if (copy_from_user(&val, argp, sizeof(int))) |
dec63ec3 | 380 | return -EFAULT; |
8378b924 | 381 | |
dec63ec3 DT |
382 | val = i8k_get_fan_speed(val); |
383 | break; | |
1da177e4 | 384 | |
dec63ec3 | 385 | case I8K_GET_FAN: |
8378b924 | 386 | if (copy_from_user(&val, argp, sizeof(int))) |
dec63ec3 | 387 | return -EFAULT; |
8378b924 | 388 | |
dec63ec3 DT |
389 | val = i8k_get_fan_status(val); |
390 | break; | |
1da177e4 | 391 | |
dec63ec3 | 392 | case I8K_SET_FAN: |
8378b924 | 393 | if (restricted && !capable(CAP_SYS_ADMIN)) |
dec63ec3 | 394 | return -EPERM; |
8378b924 DT |
395 | |
396 | if (copy_from_user(&val, argp, sizeof(int))) | |
dec63ec3 | 397 | return -EFAULT; |
8378b924 DT |
398 | |
399 | if (copy_from_user(&speed, argp + 1, sizeof(int))) | |
dec63ec3 | 400 | return -EFAULT; |
8378b924 | 401 | |
dec63ec3 DT |
402 | val = i8k_set_fan(val, speed); |
403 | break; | |
1da177e4 | 404 | |
dec63ec3 DT |
405 | default: |
406 | return -EINVAL; | |
1da177e4 | 407 | } |
1da177e4 | 408 | |
8378b924 | 409 | if (val < 0) |
dec63ec3 | 410 | return val; |
1da177e4 | 411 | |
dec63ec3 DT |
412 | switch (cmd) { |
413 | case I8K_BIOS_VERSION: | |
8378b924 | 414 | if (copy_to_user(argp, &val, 4)) |
dec63ec3 | 415 | return -EFAULT; |
8378b924 | 416 | |
dec63ec3 DT |
417 | break; |
418 | case I8K_MACHINE_ID: | |
8378b924 | 419 | if (copy_to_user(argp, buff, 16)) |
dec63ec3 | 420 | return -EFAULT; |
8378b924 | 421 | |
dec63ec3 DT |
422 | break; |
423 | default: | |
8378b924 | 424 | if (copy_to_user(argp, &val, sizeof(int))) |
dec63ec3 | 425 | return -EFAULT; |
8378b924 | 426 | |
dec63ec3 | 427 | break; |
1da177e4 | 428 | } |
1da177e4 | 429 | |
dec63ec3 | 430 | return 0; |
1da177e4 LT |
431 | } |
432 | ||
d79b6f4d FW |
433 | static long i8k_ioctl(struct file *fp, unsigned int cmd, unsigned long arg) |
434 | { | |
435 | long ret; | |
436 | ||
613655fa | 437 | mutex_lock(&i8k_mutex); |
d79b6f4d | 438 | ret = i8k_ioctl_unlocked(fp, cmd, arg); |
613655fa | 439 | mutex_unlock(&i8k_mutex); |
d79b6f4d FW |
440 | |
441 | return ret; | |
442 | } | |
443 | ||
1da177e4 LT |
444 | /* |
445 | * Print the information for /proc/i8k. | |
446 | */ | |
352f8f8b | 447 | static int i8k_proc_show(struct seq_file *seq, void *offset) |
1da177e4 | 448 | { |
352f8f8b | 449 | int fn_key, cpu_temp, ac_power; |
dec63ec3 DT |
450 | int left_fan, right_fan, left_speed, right_speed; |
451 | ||
96de0e25 JE |
452 | cpu_temp = i8k_get_temp(0); /* 11100 µs */ |
453 | left_fan = i8k_get_fan_status(I8K_FAN_LEFT); /* 580 µs */ | |
454 | right_fan = i8k_get_fan_status(I8K_FAN_RIGHT); /* 580 µs */ | |
455 | left_speed = i8k_get_fan_speed(I8K_FAN_LEFT); /* 580 µs */ | |
456 | right_speed = i8k_get_fan_speed(I8K_FAN_RIGHT); /* 580 µs */ | |
457 | fn_key = i8k_get_fn_status(); /* 750 µs */ | |
8378b924 | 458 | if (power_status) |
96de0e25 | 459 | ac_power = i8k_get_power_status(); /* 14700 µs */ |
8378b924 | 460 | else |
dec63ec3 | 461 | ac_power = -1; |
dec63ec3 DT |
462 | |
463 | /* | |
464 | * Info: | |
465 | * | |
466 | * 1) Format version (this will change if format changes) | |
467 | * 2) BIOS version | |
468 | * 3) BIOS machine ID | |
469 | * 4) Cpu temperature | |
470 | * 5) Left fan status | |
471 | * 6) Right fan status | |
472 | * 7) Left fan speed | |
473 | * 8) Right fan speed | |
474 | * 9) AC power | |
475 | * 10) Fn Key status | |
476 | */ | |
352f8f8b DT |
477 | return seq_printf(seq, "%s %s %s %d %d %d %d %d %d %d\n", |
478 | I8K_PROC_FMT, | |
479 | bios_version, | |
4f005551 | 480 | i8k_get_dmi_data(DMI_PRODUCT_SERIAL), |
352f8f8b DT |
481 | cpu_temp, |
482 | left_fan, right_fan, left_speed, right_speed, | |
483 | ac_power, fn_key); | |
1da177e4 LT |
484 | } |
485 | ||
352f8f8b | 486 | static int i8k_open_fs(struct inode *inode, struct file *file) |
1da177e4 | 487 | { |
352f8f8b | 488 | return single_open(file, i8k_proc_show, NULL); |
1da177e4 LT |
489 | } |
490 | ||
949a9d70 JD |
491 | |
492 | /* | |
493 | * Hwmon interface | |
494 | */ | |
495 | ||
496 | static ssize_t i8k_hwmon_show_temp(struct device *dev, | |
497 | struct device_attribute *devattr, | |
498 | char *buf) | |
499 | { | |
d83c39de GR |
500 | int index = to_sensor_dev_attr(devattr)->index; |
501 | int temp; | |
949a9d70 | 502 | |
d83c39de | 503 | temp = i8k_get_temp(index); |
723493ca PR |
504 | if (temp == -ERANGE) |
505 | return -EINVAL; | |
d83c39de GR |
506 | if (temp < 0) |
507 | return temp; | |
508 | return sprintf(buf, "%d\n", temp * 1000); | |
949a9d70 JD |
509 | } |
510 | ||
511 | static ssize_t i8k_hwmon_show_fan(struct device *dev, | |
512 | struct device_attribute *devattr, | |
513 | char *buf) | |
514 | { | |
515 | int index = to_sensor_dev_attr(devattr)->index; | |
516 | int fan_speed; | |
517 | ||
518 | fan_speed = i8k_get_fan_speed(index); | |
519 | if (fan_speed < 0) | |
520 | return fan_speed; | |
521 | return sprintf(buf, "%d\n", fan_speed); | |
522 | } | |
523 | ||
e7f5f275 GR |
524 | static ssize_t i8k_hwmon_show_pwm(struct device *dev, |
525 | struct device_attribute *devattr, | |
526 | char *buf) | |
527 | { | |
528 | int index = to_sensor_dev_attr(devattr)->index; | |
529 | int status; | |
530 | ||
531 | status = i8k_get_fan_status(index); | |
532 | if (status < 0) | |
533 | return -EIO; | |
81474fc2 | 534 | return sprintf(buf, "%d\n", clamp_val(status * i8k_pwm_mult, 0, 255)); |
e7f5f275 GR |
535 | } |
536 | ||
537 | static ssize_t i8k_hwmon_set_pwm(struct device *dev, | |
538 | struct device_attribute *attr, | |
539 | const char *buf, size_t count) | |
540 | { | |
541 | int index = to_sensor_dev_attr(attr)->index; | |
542 | unsigned long val; | |
543 | int err; | |
544 | ||
545 | err = kstrtoul(buf, 10, &val); | |
546 | if (err) | |
547 | return err; | |
81474fc2 | 548 | val = clamp_val(DIV_ROUND_CLOSEST(val, i8k_pwm_mult), 0, i8k_fan_max); |
e7f5f275 GR |
549 | |
550 | mutex_lock(&i8k_mutex); | |
551 | err = i8k_set_fan(index, val); | |
552 | mutex_unlock(&i8k_mutex); | |
553 | ||
554 | return err < 0 ? -EIO : count; | |
555 | } | |
556 | ||
d83c39de GR |
557 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 0); |
558 | static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 1); | |
559 | static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 2); | |
560 | static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 3); | |
949a9d70 JD |
561 | static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, i8k_hwmon_show_fan, NULL, |
562 | I8K_FAN_LEFT); | |
e7f5f275 GR |
563 | static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, i8k_hwmon_show_pwm, |
564 | i8k_hwmon_set_pwm, I8K_FAN_LEFT); | |
949a9d70 JD |
565 | static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, i8k_hwmon_show_fan, NULL, |
566 | I8K_FAN_RIGHT); | |
e7f5f275 GR |
567 | static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, i8k_hwmon_show_pwm, |
568 | i8k_hwmon_set_pwm, I8K_FAN_RIGHT); | |
82ba1d3f GR |
569 | |
570 | static struct attribute *i8k_attrs[] = { | |
d83c39de | 571 | &sensor_dev_attr_temp1_input.dev_attr.attr, /* 0 */ |
b12ce5f2 GR |
572 | &sensor_dev_attr_temp2_input.dev_attr.attr, /* 1 */ |
573 | &sensor_dev_attr_temp3_input.dev_attr.attr, /* 2 */ | |
574 | &sensor_dev_attr_temp4_input.dev_attr.attr, /* 3 */ | |
575 | &sensor_dev_attr_fan1_input.dev_attr.attr, /* 4 */ | |
576 | &sensor_dev_attr_pwm1.dev_attr.attr, /* 5 */ | |
577 | &sensor_dev_attr_fan2_input.dev_attr.attr, /* 6 */ | |
578 | &sensor_dev_attr_pwm2.dev_attr.attr, /* 7 */ | |
82ba1d3f GR |
579 | NULL |
580 | }; | |
949a9d70 | 581 | |
82ba1d3f GR |
582 | static umode_t i8k_is_visible(struct kobject *kobj, struct attribute *attr, |
583 | int index) | |
949a9d70 | 584 | { |
b12ce5f2 | 585 | if (index == 0 && !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP1)) |
82ba1d3f | 586 | return 0; |
b12ce5f2 | 587 | if (index == 1 && !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP2)) |
d83c39de | 588 | return 0; |
b12ce5f2 | 589 | if (index == 2 && !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP3)) |
d83c39de | 590 | return 0; |
b12ce5f2 | 591 | if (index == 3 && !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP4)) |
d83c39de | 592 | return 0; |
b12ce5f2 | 593 | if (index >= 4 && index <= 5 && |
82ba1d3f GR |
594 | !(i8k_hwmon_flags & I8K_HWMON_HAVE_FAN1)) |
595 | return 0; | |
b12ce5f2 | 596 | if (index >= 6 && index <= 7 && |
82ba1d3f GR |
597 | !(i8k_hwmon_flags & I8K_HWMON_HAVE_FAN2)) |
598 | return 0; | |
599 | ||
600 | return attr->mode; | |
949a9d70 JD |
601 | } |
602 | ||
82ba1d3f GR |
603 | static const struct attribute_group i8k_group = { |
604 | .attrs = i8k_attrs, | |
605 | .is_visible = i8k_is_visible, | |
606 | }; | |
607 | __ATTRIBUTE_GROUPS(i8k); | |
608 | ||
949a9d70 JD |
609 | static int __init i8k_init_hwmon(void) |
610 | { | |
611 | int err; | |
612 | ||
82ba1d3f | 613 | i8k_hwmon_flags = 0; |
949a9d70 JD |
614 | |
615 | /* CPU temperature attributes, if temperature reading is OK */ | |
616 | err = i8k_get_temp(0); | |
723493ca | 617 | if (err >= 0 || err == -ERANGE) |
82ba1d3f | 618 | i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP1; |
d83c39de GR |
619 | /* check for additional temperature sensors */ |
620 | err = i8k_get_temp(1); | |
723493ca | 621 | if (err >= 0 || err == -ERANGE) |
d83c39de GR |
622 | i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP2; |
623 | err = i8k_get_temp(2); | |
723493ca | 624 | if (err >= 0 || err == -ERANGE) |
d83c39de GR |
625 | i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP3; |
626 | err = i8k_get_temp(3); | |
723493ca | 627 | if (err >= 0 || err == -ERANGE) |
d83c39de | 628 | i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP4; |
949a9d70 JD |
629 | |
630 | /* Left fan attributes, if left fan is present */ | |
631 | err = i8k_get_fan_status(I8K_FAN_LEFT); | |
82ba1d3f GR |
632 | if (err >= 0) |
633 | i8k_hwmon_flags |= I8K_HWMON_HAVE_FAN1; | |
949a9d70 JD |
634 | |
635 | /* Right fan attributes, if right fan is present */ | |
636 | err = i8k_get_fan_status(I8K_FAN_RIGHT); | |
82ba1d3f GR |
637 | if (err >= 0) |
638 | i8k_hwmon_flags |= I8K_HWMON_HAVE_FAN2; | |
949a9d70 | 639 | |
82ba1d3f GR |
640 | i8k_hwmon_dev = hwmon_device_register_with_groups(NULL, "i8k", NULL, |
641 | i8k_groups); | |
642 | if (IS_ERR(i8k_hwmon_dev)) { | |
643 | err = PTR_ERR(i8k_hwmon_dev); | |
644 | i8k_hwmon_dev = NULL; | |
645 | pr_err("hwmon registration failed (%d)\n", err); | |
646 | return err; | |
647 | } | |
949a9d70 | 648 | return 0; |
949a9d70 JD |
649 | } |
650 | ||
81474fc2 GR |
651 | struct i8k_config_data { |
652 | int fan_mult; | |
653 | int fan_max; | |
654 | }; | |
655 | ||
656 | enum i8k_configs { | |
7b883446 | 657 | DELL_LATITUDE_D520, |
06c88b0d | 658 | DELL_LATITUDE_E6540, |
7b883446 | 659 | DELL_PRECISION_490, |
81474fc2 GR |
660 | DELL_STUDIO, |
661 | DELL_XPS_M140, | |
662 | }; | |
663 | ||
664 | static const struct i8k_config_data i8k_config_data[] = { | |
7b883446 GR |
665 | [DELL_LATITUDE_D520] = { |
666 | .fan_mult = 1, | |
667 | .fan_max = I8K_FAN_TURBO, | |
668 | }, | |
06c88b0d SH |
669 | [DELL_LATITUDE_E6540] = { |
670 | .fan_mult = 1, | |
671 | .fan_max = I8K_FAN_HIGH, | |
672 | }, | |
7b883446 GR |
673 | [DELL_PRECISION_490] = { |
674 | .fan_mult = 1, | |
675 | .fan_max = I8K_FAN_TURBO, | |
676 | }, | |
81474fc2 GR |
677 | [DELL_STUDIO] = { |
678 | .fan_mult = 1, | |
679 | .fan_max = I8K_FAN_HIGH, | |
680 | }, | |
681 | [DELL_XPS_M140] = { | |
682 | .fan_mult = 1, | |
683 | .fan_max = I8K_FAN_HIGH, | |
684 | }, | |
685 | }; | |
686 | ||
12186f51 | 687 | static struct dmi_system_id i8k_dmi_table[] __initdata = { |
e70c9d5e DT |
688 | { |
689 | .ident = "Dell Inspiron", | |
690 | .matches = { | |
691 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer"), | |
692 | DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron"), | |
693 | }, | |
694 | }, | |
695 | { | |
696 | .ident = "Dell Latitude", | |
697 | .matches = { | |
698 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer"), | |
699 | DMI_MATCH(DMI_PRODUCT_NAME, "Latitude"), | |
700 | }, | |
701 | }, | |
7e0fa31d DT |
702 | { |
703 | .ident = "Dell Inspiron 2", | |
704 | .matches = { | |
705 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), | |
706 | DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron"), | |
707 | }, | |
708 | }, | |
7b883446 GR |
709 | { |
710 | .ident = "Dell Latitude D520", | |
711 | .matches = { | |
712 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), | |
713 | DMI_MATCH(DMI_PRODUCT_NAME, "Latitude D520"), | |
714 | }, | |
715 | .driver_data = (void *)&i8k_config_data[DELL_LATITUDE_D520], | |
716 | }, | |
0f352239 PR |
717 | { |
718 | .ident = "Dell Latitude E6440", | |
719 | .matches = { | |
720 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), | |
721 | DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E6440"), | |
722 | }, | |
723 | .driver_data = (void *)&i8k_config_data[DELL_LATITUDE_E6540], | |
724 | }, | |
06c88b0d SH |
725 | { |
726 | .ident = "Dell Latitude E6540", | |
727 | .matches = { | |
728 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), | |
729 | DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E6540"), | |
730 | }, | |
731 | .driver_data = (void *)&i8k_config_data[DELL_LATITUDE_E6540], | |
732 | }, | |
7e0fa31d DT |
733 | { |
734 | .ident = "Dell Latitude 2", | |
735 | .matches = { | |
736 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), | |
737 | DMI_MATCH(DMI_PRODUCT_NAME, "Latitude"), | |
738 | }, | |
739 | }, | |
a9000d03 NW |
740 | { /* UK Inspiron 6400 */ |
741 | .ident = "Dell Inspiron 3", | |
742 | .matches = { | |
743 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), | |
744 | DMI_MATCH(DMI_PRODUCT_NAME, "MM061"), | |
745 | }, | |
746 | }, | |
48103c52 FS |
747 | { |
748 | .ident = "Dell Inspiron 3", | |
749 | .matches = { | |
750 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), | |
751 | DMI_MATCH(DMI_PRODUCT_NAME, "MP061"), | |
752 | }, | |
753 | }, | |
7b883446 GR |
754 | { |
755 | .ident = "Dell Precision 490", | |
756 | .matches = { | |
757 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), | |
758 | DMI_MATCH(DMI_PRODUCT_NAME, | |
759 | "Precision WorkStation 490"), | |
760 | }, | |
761 | .driver_data = (void *)&i8k_config_data[DELL_PRECISION_490], | |
762 | }, | |
7ab21a86 AS |
763 | { |
764 | .ident = "Dell Precision", | |
765 | .matches = { | |
766 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), | |
767 | DMI_MATCH(DMI_PRODUCT_NAME, "Precision"), | |
768 | }, | |
769 | }, | |
bef2a508 FH |
770 | { |
771 | .ident = "Dell Vostro", | |
772 | .matches = { | |
773 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), | |
774 | DMI_MATCH(DMI_PRODUCT_NAME, "Vostro"), | |
775 | }, | |
776 | }, | |
9aa5b018 AC |
777 | { |
778 | .ident = "Dell XPS421", | |
779 | .matches = { | |
780 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), | |
781 | DMI_MATCH(DMI_PRODUCT_NAME, "XPS L421X"), | |
782 | }, | |
783 | }, | |
ff457bde GR |
784 | { |
785 | .ident = "Dell Studio", | |
786 | .matches = { | |
787 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), | |
788 | DMI_MATCH(DMI_PRODUCT_NAME, "Studio"), | |
789 | }, | |
81474fc2 | 790 | .driver_data = (void *)&i8k_config_data[DELL_STUDIO], |
ff457bde | 791 | }, |
919a0304 GR |
792 | { |
793 | .ident = "Dell XPS M140", | |
794 | .matches = { | |
795 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), | |
796 | DMI_MATCH(DMI_PRODUCT_NAME, "MXC051"), | |
797 | }, | |
81474fc2 | 798 | .driver_data = (void *)&i8k_config_data[DELL_XPS_M140], |
919a0304 | 799 | }, |
12186f51 | 800 | { } |
e70c9d5e | 801 | }; |
1da177e4 | 802 | |
148b1fda PR |
803 | MODULE_DEVICE_TABLE(dmi, i8k_dmi_table); |
804 | ||
1da177e4 LT |
805 | /* |
806 | * Probe for the presence of a supported laptop. | |
807 | */ | |
808 | static int __init i8k_probe(void) | |
809 | { | |
26d09382 | 810 | const struct dmi_system_id *id; |
dec63ec3 | 811 | |
1da177e4 | 812 | /* |
dec63ec3 | 813 | * Get DMI information |
1da177e4 | 814 | */ |
e70c9d5e DT |
815 | if (!dmi_check_system(i8k_dmi_table)) { |
816 | if (!ignore_dmi && !force) | |
817 | return -ENODEV; | |
818 | ||
60e71aaf GR |
819 | pr_info("not running on a supported Dell system.\n"); |
820 | pr_info("vendor=%s, model=%s, version=%s\n", | |
e70c9d5e DT |
821 | i8k_get_dmi_data(DMI_SYS_VENDOR), |
822 | i8k_get_dmi_data(DMI_PRODUCT_NAME), | |
823 | i8k_get_dmi_data(DMI_BIOS_VERSION)); | |
1da177e4 | 824 | } |
dec63ec3 | 825 | |
12186f51 GR |
826 | strlcpy(bios_version, i8k_get_dmi_data(DMI_BIOS_VERSION), |
827 | sizeof(bios_version)); | |
e70c9d5e | 828 | |
1da177e4 | 829 | /* |
dec63ec3 | 830 | * Get SMM Dell signature |
1da177e4 | 831 | */ |
7e0fa31d DT |
832 | if (i8k_get_dell_signature(I8K_SMM_GET_DELL_SIG1) && |
833 | i8k_get_dell_signature(I8K_SMM_GET_DELL_SIG2)) { | |
60e71aaf | 834 | pr_err("unable to get SMM Dell signature\n"); |
e70c9d5e DT |
835 | if (!force) |
836 | return -ENODEV; | |
1da177e4 | 837 | } |
1da177e4 | 838 | |
26d09382 | 839 | i8k_fan_mult = fan_mult; |
81474fc2 | 840 | i8k_fan_max = fan_max ? : I8K_FAN_HIGH; /* Must not be 0 */ |
26d09382 | 841 | id = dmi_first_match(i8k_dmi_table); |
81474fc2 GR |
842 | if (id && id->driver_data) { |
843 | const struct i8k_config_data *conf = id->driver_data; | |
844 | ||
845 | if (fan_mult == I8K_FAN_MULT && conf->fan_mult) | |
846 | i8k_fan_mult = conf->fan_mult; | |
847 | if (fan_max == I8K_FAN_HIGH && conf->fan_max) | |
848 | i8k_fan_max = conf->fan_max; | |
849 | } | |
850 | i8k_pwm_mult = DIV_ROUND_UP(255, i8k_fan_max); | |
26d09382 | 851 | |
dec63ec3 | 852 | return 0; |
1da177e4 LT |
853 | } |
854 | ||
8378b924 | 855 | static int __init i8k_init(void) |
1da177e4 | 856 | { |
dec63ec3 | 857 | struct proc_dir_entry *proc_i8k; |
949a9d70 | 858 | int err; |
dec63ec3 DT |
859 | |
860 | /* Are we running on an supported laptop? */ | |
e70c9d5e | 861 | if (i8k_probe()) |
dec63ec3 | 862 | return -ENODEV; |
dec63ec3 DT |
863 | |
864 | /* Register the proc entry */ | |
1b502217 | 865 | proc_i8k = proc_create("i8k", 0, NULL, &i8k_fops); |
352f8f8b | 866 | if (!proc_i8k) |
dec63ec3 | 867 | return -ENOENT; |
352f8f8b | 868 | |
949a9d70 JD |
869 | err = i8k_init_hwmon(); |
870 | if (err) | |
871 | goto exit_remove_proc; | |
872 | ||
dec63ec3 | 873 | return 0; |
949a9d70 JD |
874 | |
875 | exit_remove_proc: | |
876 | remove_proc_entry("i8k", NULL); | |
877 | return err; | |
1da177e4 LT |
878 | } |
879 | ||
8378b924 | 880 | static void __exit i8k_exit(void) |
1da177e4 | 881 | { |
82ba1d3f | 882 | hwmon_device_unregister(i8k_hwmon_dev); |
dec63ec3 | 883 | remove_proc_entry("i8k", NULL); |
1da177e4 | 884 | } |
1da177e4 | 885 | |
8378b924 DT |
886 | module_init(i8k_init); |
887 | module_exit(i8k_exit); |