]>
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 <dz@debian.org> | |
5 | * | |
949a9d70 | 6 | * Hwmon integration: |
7c81c60f | 7 | * Copyright (C) 2011 Jean Delvare <jdelvare@suse.de> |
f5a7f827 | 8 | * Copyright (C) 2013 Guenter Roeck <linux@roeck-us.net> |
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 (dz@debian.org)"); | |
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); | |
147 | set_cpus_allowed_ptr(current, cpumask_of(0)); | |
148 | if (smp_processor_id() != 0) { | |
149 | rc = -EBUSY; | |
150 | goto out; | |
151 | } | |
dec63ec3 | 152 | |
fe04f22f | 153 | #if defined(CONFIG_X86_64) |
22d3243d | 154 | asm volatile("pushq %%rax\n\t" |
fe04f22f BS |
155 | "movl 0(%%rax),%%edx\n\t" |
156 | "pushq %%rdx\n\t" | |
157 | "movl 4(%%rax),%%ebx\n\t" | |
158 | "movl 8(%%rax),%%ecx\n\t" | |
159 | "movl 12(%%rax),%%edx\n\t" | |
160 | "movl 16(%%rax),%%esi\n\t" | |
161 | "movl 20(%%rax),%%edi\n\t" | |
162 | "popq %%rax\n\t" | |
163 | "out %%al,$0xb2\n\t" | |
164 | "out %%al,$0x84\n\t" | |
165 | "xchgq %%rax,(%%rsp)\n\t" | |
166 | "movl %%ebx,4(%%rax)\n\t" | |
167 | "movl %%ecx,8(%%rax)\n\t" | |
168 | "movl %%edx,12(%%rax)\n\t" | |
169 | "movl %%esi,16(%%rax)\n\t" | |
170 | "movl %%edi,20(%%rax)\n\t" | |
171 | "popq %%rdx\n\t" | |
172 | "movl %%edx,0(%%rax)\n\t" | |
bc1f419c LT |
173 | "pushfq\n\t" |
174 | "popq %%rax\n\t" | |
fe04f22f | 175 | "andl $1,%%eax\n" |
12186f51 | 176 | : "=a"(rc) |
fe04f22f BS |
177 | : "a"(regs) |
178 | : "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory"); | |
179 | #else | |
22d3243d | 180 | asm volatile("pushl %%eax\n\t" |
dec63ec3 DT |
181 | "movl 0(%%eax),%%edx\n\t" |
182 | "push %%edx\n\t" | |
183 | "movl 4(%%eax),%%ebx\n\t" | |
184 | "movl 8(%%eax),%%ecx\n\t" | |
185 | "movl 12(%%eax),%%edx\n\t" | |
186 | "movl 16(%%eax),%%esi\n\t" | |
187 | "movl 20(%%eax),%%edi\n\t" | |
188 | "popl %%eax\n\t" | |
189 | "out %%al,$0xb2\n\t" | |
190 | "out %%al,$0x84\n\t" | |
191 | "xchgl %%eax,(%%esp)\n\t" | |
192 | "movl %%ebx,4(%%eax)\n\t" | |
193 | "movl %%ecx,8(%%eax)\n\t" | |
194 | "movl %%edx,12(%%eax)\n\t" | |
195 | "movl %%esi,16(%%eax)\n\t" | |
196 | "movl %%edi,20(%%eax)\n\t" | |
197 | "popl %%edx\n\t" | |
198 | "movl %%edx,0(%%eax)\n\t" | |
199 | "lahf\n\t" | |
200 | "shrl $8,%%eax\n\t" | |
6b4e81db | 201 | "andl $1,%%eax\n" |
12186f51 | 202 | : "=a"(rc) |
dec63ec3 DT |
203 | : "a"(regs) |
204 | : "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory"); | |
fe04f22f | 205 | #endif |
8378b924 | 206 | if (rc != 0 || (regs->eax & 0xffff) == 0xffff || regs->eax == eax) |
f36fdb9f | 207 | rc = -EINVAL; |
dec63ec3 | 208 | |
f36fdb9f GR |
209 | out: |
210 | set_cpus_allowed_ptr(current, old_mask); | |
211 | free_cpumask_var(old_mask); | |
212 | return rc; | |
1da177e4 LT |
213 | } |
214 | ||
1da177e4 LT |
215 | /* |
216 | * Read the Fn key status. | |
217 | */ | |
218 | static int i8k_get_fn_status(void) | |
219 | { | |
8378b924 | 220 | struct smm_regs regs = { .eax = I8K_SMM_FN_STATUS, }; |
dec63ec3 DT |
221 | int rc; |
222 | ||
12186f51 GR |
223 | rc = i8k_smm(®s); |
224 | if (rc < 0) | |
dec63ec3 | 225 | return rc; |
dec63ec3 DT |
226 | |
227 | switch ((regs.eax >> I8K_FN_SHIFT) & I8K_FN_MASK) { | |
228 | case I8K_FN_UP: | |
229 | return I8K_VOL_UP; | |
230 | case I8K_FN_DOWN: | |
231 | return I8K_VOL_DOWN; | |
232 | case I8K_FN_MUTE: | |
233 | return I8K_VOL_MUTE; | |
234 | default: | |
235 | return 0; | |
236 | } | |
1da177e4 LT |
237 | } |
238 | ||
239 | /* | |
240 | * Read the power status. | |
241 | */ | |
242 | static int i8k_get_power_status(void) | |
243 | { | |
8378b924 | 244 | struct smm_regs regs = { .eax = I8K_SMM_POWER_STATUS, }; |
dec63ec3 DT |
245 | int rc; |
246 | ||
12186f51 GR |
247 | rc = i8k_smm(®s); |
248 | if (rc < 0) | |
dec63ec3 | 249 | return rc; |
dec63ec3 | 250 | |
8378b924 | 251 | return (regs.eax & 0xff) == I8K_POWER_AC ? I8K_AC : I8K_BATTERY; |
1da177e4 LT |
252 | } |
253 | ||
254 | /* | |
255 | * Read the fan status. | |
256 | */ | |
257 | static int i8k_get_fan_status(int fan) | |
258 | { | |
8378b924 | 259 | struct smm_regs regs = { .eax = I8K_SMM_GET_FAN, }; |
1da177e4 | 260 | |
dec63ec3 | 261 | regs.ebx = fan & 0xff; |
8378b924 | 262 | return i8k_smm(®s) ? : regs.eax & 0xff; |
1da177e4 LT |
263 | } |
264 | ||
265 | /* | |
266 | * Read the fan speed in RPM. | |
267 | */ | |
268 | static int i8k_get_fan_speed(int fan) | |
269 | { | |
8378b924 | 270 | struct smm_regs regs = { .eax = I8K_SMM_GET_SPEED, }; |
1da177e4 | 271 | |
dec63ec3 | 272 | regs.ebx = fan & 0xff; |
26d09382 | 273 | return i8k_smm(®s) ? : (regs.eax & 0xffff) * i8k_fan_mult; |
1da177e4 LT |
274 | } |
275 | ||
276 | /* | |
277 | * Set the fan speed (off, low, high). Returns the new fan status. | |
278 | */ | |
279 | static int i8k_set_fan(int fan, int speed) | |
280 | { | |
8378b924 | 281 | struct smm_regs regs = { .eax = I8K_SMM_SET_FAN, }; |
1da177e4 | 282 | |
81474fc2 | 283 | speed = (speed < 0) ? 0 : ((speed > i8k_fan_max) ? i8k_fan_max : speed); |
dec63ec3 | 284 | regs.ebx = (fan & 0xff) | (speed << 8); |
1da177e4 | 285 | |
8378b924 | 286 | return i8k_smm(®s) ? : i8k_get_fan_status(fan); |
1da177e4 LT |
287 | } |
288 | ||
289 | /* | |
290 | * Read the cpu temperature. | |
291 | */ | |
7e0fa31d | 292 | static int i8k_get_temp(int sensor) |
1da177e4 | 293 | { |
8378b924 | 294 | struct smm_regs regs = { .eax = I8K_SMM_GET_TEMP, }; |
dec63ec3 DT |
295 | int rc; |
296 | int temp; | |
1da177e4 LT |
297 | |
298 | #ifdef I8K_TEMPERATURE_BUG | |
d83c39de | 299 | static int prev[4]; |
1da177e4 | 300 | #endif |
7e0fa31d | 301 | regs.ebx = sensor & 0xff; |
12186f51 GR |
302 | rc = i8k_smm(®s); |
303 | if (rc < 0) | |
dec63ec3 | 304 | return rc; |
8378b924 | 305 | |
dec63ec3 | 306 | temp = regs.eax & 0xff; |
1da177e4 LT |
307 | |
308 | #ifdef I8K_TEMPERATURE_BUG | |
dec63ec3 DT |
309 | /* |
310 | * Sometimes the temperature sensor returns 0x99, which is out of range. | |
311 | * In this case we return (once) the previous cached value. For example: | |
312 | # 1003655137 00000058 00005a4b | |
313 | # 1003655138 00000099 00003a80 <--- 0x99 = 153 degrees | |
314 | # 1003655139 00000054 00005c52 | |
315 | */ | |
316 | if (temp > I8K_MAX_TEMP) { | |
d83c39de GR |
317 | temp = prev[sensor]; |
318 | prev[sensor] = I8K_MAX_TEMP; | |
dec63ec3 | 319 | } else { |
d83c39de | 320 | prev[sensor] = temp; |
dec63ec3 | 321 | } |
1da177e4 LT |
322 | #endif |
323 | ||
dec63ec3 | 324 | return temp; |
1da177e4 LT |
325 | } |
326 | ||
7e0fa31d | 327 | static int i8k_get_dell_signature(int req_fn) |
1da177e4 | 328 | { |
7e0fa31d | 329 | struct smm_regs regs = { .eax = req_fn, }; |
dec63ec3 | 330 | int rc; |
1da177e4 | 331 | |
12186f51 GR |
332 | rc = i8k_smm(®s); |
333 | if (rc < 0) | |
dec63ec3 | 334 | return rc; |
1da177e4 | 335 | |
8378b924 | 336 | return regs.eax == 1145651527 && regs.edx == 1145392204 ? 0 : -1; |
1da177e4 LT |
337 | } |
338 | ||
d79b6f4d FW |
339 | static int |
340 | i8k_ioctl_unlocked(struct file *fp, unsigned int cmd, unsigned long arg) | |
1da177e4 | 341 | { |
e70c9d5e | 342 | int val = 0; |
dec63ec3 DT |
343 | int speed; |
344 | unsigned char buff[16]; | |
345 | int __user *argp = (int __user *)arg; | |
346 | ||
347 | if (!argp) | |
348 | return -EINVAL; | |
349 | ||
350 | switch (cmd) { | |
351 | case I8K_BIOS_VERSION: | |
e551b152 GR |
352 | val = (bios_version[0] << 16) | |
353 | (bios_version[1] << 8) | bios_version[2]; | |
dec63ec3 DT |
354 | break; |
355 | ||
356 | case I8K_MACHINE_ID: | |
357 | memset(buff, 0, 16); | |
12186f51 GR |
358 | strlcpy(buff, i8k_get_dmi_data(DMI_PRODUCT_SERIAL), |
359 | sizeof(buff)); | |
dec63ec3 DT |
360 | break; |
361 | ||
362 | case I8K_FN_STATUS: | |
363 | val = i8k_get_fn_status(); | |
364 | break; | |
365 | ||
366 | case I8K_POWER_STATUS: | |
367 | val = i8k_get_power_status(); | |
368 | break; | |
369 | ||
370 | case I8K_GET_TEMP: | |
7e0fa31d | 371 | val = i8k_get_temp(0); |
dec63ec3 DT |
372 | break; |
373 | ||
374 | case I8K_GET_SPEED: | |
8378b924 | 375 | if (copy_from_user(&val, argp, sizeof(int))) |
dec63ec3 | 376 | return -EFAULT; |
8378b924 | 377 | |
dec63ec3 DT |
378 | val = i8k_get_fan_speed(val); |
379 | break; | |
1da177e4 | 380 | |
dec63ec3 | 381 | case I8K_GET_FAN: |
8378b924 | 382 | if (copy_from_user(&val, argp, sizeof(int))) |
dec63ec3 | 383 | return -EFAULT; |
8378b924 | 384 | |
dec63ec3 DT |
385 | val = i8k_get_fan_status(val); |
386 | break; | |
1da177e4 | 387 | |
dec63ec3 | 388 | case I8K_SET_FAN: |
8378b924 | 389 | if (restricted && !capable(CAP_SYS_ADMIN)) |
dec63ec3 | 390 | return -EPERM; |
8378b924 DT |
391 | |
392 | if (copy_from_user(&val, argp, sizeof(int))) | |
dec63ec3 | 393 | return -EFAULT; |
8378b924 DT |
394 | |
395 | if (copy_from_user(&speed, argp + 1, sizeof(int))) | |
dec63ec3 | 396 | return -EFAULT; |
8378b924 | 397 | |
dec63ec3 DT |
398 | val = i8k_set_fan(val, speed); |
399 | break; | |
1da177e4 | 400 | |
dec63ec3 DT |
401 | default: |
402 | return -EINVAL; | |
1da177e4 | 403 | } |
1da177e4 | 404 | |
8378b924 | 405 | if (val < 0) |
dec63ec3 | 406 | return val; |
1da177e4 | 407 | |
dec63ec3 DT |
408 | switch (cmd) { |
409 | case I8K_BIOS_VERSION: | |
8378b924 | 410 | if (copy_to_user(argp, &val, 4)) |
dec63ec3 | 411 | return -EFAULT; |
8378b924 | 412 | |
dec63ec3 DT |
413 | break; |
414 | case I8K_MACHINE_ID: | |
8378b924 | 415 | if (copy_to_user(argp, buff, 16)) |
dec63ec3 | 416 | return -EFAULT; |
8378b924 | 417 | |
dec63ec3 DT |
418 | break; |
419 | default: | |
8378b924 | 420 | if (copy_to_user(argp, &val, sizeof(int))) |
dec63ec3 | 421 | return -EFAULT; |
8378b924 | 422 | |
dec63ec3 | 423 | break; |
1da177e4 | 424 | } |
1da177e4 | 425 | |
dec63ec3 | 426 | return 0; |
1da177e4 LT |
427 | } |
428 | ||
d79b6f4d FW |
429 | static long i8k_ioctl(struct file *fp, unsigned int cmd, unsigned long arg) |
430 | { | |
431 | long ret; | |
432 | ||
613655fa | 433 | mutex_lock(&i8k_mutex); |
d79b6f4d | 434 | ret = i8k_ioctl_unlocked(fp, cmd, arg); |
613655fa | 435 | mutex_unlock(&i8k_mutex); |
d79b6f4d FW |
436 | |
437 | return ret; | |
438 | } | |
439 | ||
1da177e4 LT |
440 | /* |
441 | * Print the information for /proc/i8k. | |
442 | */ | |
352f8f8b | 443 | static int i8k_proc_show(struct seq_file *seq, void *offset) |
1da177e4 | 444 | { |
352f8f8b | 445 | int fn_key, cpu_temp, ac_power; |
dec63ec3 DT |
446 | int left_fan, right_fan, left_speed, right_speed; |
447 | ||
96de0e25 JE |
448 | cpu_temp = i8k_get_temp(0); /* 11100 µs */ |
449 | left_fan = i8k_get_fan_status(I8K_FAN_LEFT); /* 580 µs */ | |
450 | right_fan = i8k_get_fan_status(I8K_FAN_RIGHT); /* 580 µs */ | |
451 | left_speed = i8k_get_fan_speed(I8K_FAN_LEFT); /* 580 µs */ | |
452 | right_speed = i8k_get_fan_speed(I8K_FAN_RIGHT); /* 580 µs */ | |
453 | fn_key = i8k_get_fn_status(); /* 750 µs */ | |
8378b924 | 454 | if (power_status) |
96de0e25 | 455 | ac_power = i8k_get_power_status(); /* 14700 µs */ |
8378b924 | 456 | else |
dec63ec3 | 457 | ac_power = -1; |
dec63ec3 DT |
458 | |
459 | /* | |
460 | * Info: | |
461 | * | |
462 | * 1) Format version (this will change if format changes) | |
463 | * 2) BIOS version | |
464 | * 3) BIOS machine ID | |
465 | * 4) Cpu temperature | |
466 | * 5) Left fan status | |
467 | * 6) Right fan status | |
468 | * 7) Left fan speed | |
469 | * 8) Right fan speed | |
470 | * 9) AC power | |
471 | * 10) Fn Key status | |
472 | */ | |
352f8f8b DT |
473 | return seq_printf(seq, "%s %s %s %d %d %d %d %d %d %d\n", |
474 | I8K_PROC_FMT, | |
475 | bios_version, | |
4f005551 | 476 | i8k_get_dmi_data(DMI_PRODUCT_SERIAL), |
352f8f8b DT |
477 | cpu_temp, |
478 | left_fan, right_fan, left_speed, right_speed, | |
479 | ac_power, fn_key); | |
1da177e4 LT |
480 | } |
481 | ||
352f8f8b | 482 | static int i8k_open_fs(struct inode *inode, struct file *file) |
1da177e4 | 483 | { |
352f8f8b | 484 | return single_open(file, i8k_proc_show, NULL); |
1da177e4 LT |
485 | } |
486 | ||
949a9d70 JD |
487 | |
488 | /* | |
489 | * Hwmon interface | |
490 | */ | |
491 | ||
492 | static ssize_t i8k_hwmon_show_temp(struct device *dev, | |
493 | struct device_attribute *devattr, | |
494 | char *buf) | |
495 | { | |
d83c39de GR |
496 | int index = to_sensor_dev_attr(devattr)->index; |
497 | int temp; | |
949a9d70 | 498 | |
d83c39de GR |
499 | temp = i8k_get_temp(index); |
500 | if (temp < 0) | |
501 | return temp; | |
502 | return sprintf(buf, "%d\n", temp * 1000); | |
949a9d70 JD |
503 | } |
504 | ||
505 | static ssize_t i8k_hwmon_show_fan(struct device *dev, | |
506 | struct device_attribute *devattr, | |
507 | char *buf) | |
508 | { | |
509 | int index = to_sensor_dev_attr(devattr)->index; | |
510 | int fan_speed; | |
511 | ||
512 | fan_speed = i8k_get_fan_speed(index); | |
513 | if (fan_speed < 0) | |
514 | return fan_speed; | |
515 | return sprintf(buf, "%d\n", fan_speed); | |
516 | } | |
517 | ||
e7f5f275 GR |
518 | static ssize_t i8k_hwmon_show_pwm(struct device *dev, |
519 | struct device_attribute *devattr, | |
520 | char *buf) | |
521 | { | |
522 | int index = to_sensor_dev_attr(devattr)->index; | |
523 | int status; | |
524 | ||
525 | status = i8k_get_fan_status(index); | |
526 | if (status < 0) | |
527 | return -EIO; | |
81474fc2 | 528 | return sprintf(buf, "%d\n", clamp_val(status * i8k_pwm_mult, 0, 255)); |
e7f5f275 GR |
529 | } |
530 | ||
531 | static ssize_t i8k_hwmon_set_pwm(struct device *dev, | |
532 | struct device_attribute *attr, | |
533 | const char *buf, size_t count) | |
534 | { | |
535 | int index = to_sensor_dev_attr(attr)->index; | |
536 | unsigned long val; | |
537 | int err; | |
538 | ||
539 | err = kstrtoul(buf, 10, &val); | |
540 | if (err) | |
541 | return err; | |
81474fc2 | 542 | val = clamp_val(DIV_ROUND_CLOSEST(val, i8k_pwm_mult), 0, i8k_fan_max); |
e7f5f275 GR |
543 | |
544 | mutex_lock(&i8k_mutex); | |
545 | err = i8k_set_fan(index, val); | |
546 | mutex_unlock(&i8k_mutex); | |
547 | ||
548 | return err < 0 ? -EIO : count; | |
549 | } | |
550 | ||
d83c39de GR |
551 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 0); |
552 | static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 1); | |
553 | static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 2); | |
554 | static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, i8k_hwmon_show_temp, NULL, 3); | |
949a9d70 JD |
555 | static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, i8k_hwmon_show_fan, NULL, |
556 | I8K_FAN_LEFT); | |
e7f5f275 GR |
557 | static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, i8k_hwmon_show_pwm, |
558 | i8k_hwmon_set_pwm, I8K_FAN_LEFT); | |
949a9d70 JD |
559 | static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, i8k_hwmon_show_fan, NULL, |
560 | I8K_FAN_RIGHT); | |
e7f5f275 GR |
561 | static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, i8k_hwmon_show_pwm, |
562 | i8k_hwmon_set_pwm, I8K_FAN_RIGHT); | |
82ba1d3f GR |
563 | |
564 | static struct attribute *i8k_attrs[] = { | |
d83c39de | 565 | &sensor_dev_attr_temp1_input.dev_attr.attr, /* 0 */ |
b12ce5f2 GR |
566 | &sensor_dev_attr_temp2_input.dev_attr.attr, /* 1 */ |
567 | &sensor_dev_attr_temp3_input.dev_attr.attr, /* 2 */ | |
568 | &sensor_dev_attr_temp4_input.dev_attr.attr, /* 3 */ | |
569 | &sensor_dev_attr_fan1_input.dev_attr.attr, /* 4 */ | |
570 | &sensor_dev_attr_pwm1.dev_attr.attr, /* 5 */ | |
571 | &sensor_dev_attr_fan2_input.dev_attr.attr, /* 6 */ | |
572 | &sensor_dev_attr_pwm2.dev_attr.attr, /* 7 */ | |
82ba1d3f GR |
573 | NULL |
574 | }; | |
949a9d70 | 575 | |
82ba1d3f GR |
576 | static umode_t i8k_is_visible(struct kobject *kobj, struct attribute *attr, |
577 | int index) | |
949a9d70 | 578 | { |
b12ce5f2 | 579 | if (index == 0 && !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP1)) |
82ba1d3f | 580 | return 0; |
b12ce5f2 | 581 | if (index == 1 && !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP2)) |
d83c39de | 582 | return 0; |
b12ce5f2 | 583 | if (index == 2 && !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP3)) |
d83c39de | 584 | return 0; |
b12ce5f2 | 585 | if (index == 3 && !(i8k_hwmon_flags & I8K_HWMON_HAVE_TEMP4)) |
d83c39de | 586 | return 0; |
b12ce5f2 | 587 | if (index >= 4 && index <= 5 && |
82ba1d3f GR |
588 | !(i8k_hwmon_flags & I8K_HWMON_HAVE_FAN1)) |
589 | return 0; | |
b12ce5f2 | 590 | if (index >= 6 && index <= 7 && |
82ba1d3f GR |
591 | !(i8k_hwmon_flags & I8K_HWMON_HAVE_FAN2)) |
592 | return 0; | |
593 | ||
594 | return attr->mode; | |
949a9d70 JD |
595 | } |
596 | ||
82ba1d3f GR |
597 | static const struct attribute_group i8k_group = { |
598 | .attrs = i8k_attrs, | |
599 | .is_visible = i8k_is_visible, | |
600 | }; | |
601 | __ATTRIBUTE_GROUPS(i8k); | |
602 | ||
949a9d70 JD |
603 | static int __init i8k_init_hwmon(void) |
604 | { | |
605 | int err; | |
606 | ||
82ba1d3f | 607 | i8k_hwmon_flags = 0; |
949a9d70 JD |
608 | |
609 | /* CPU temperature attributes, if temperature reading is OK */ | |
610 | err = i8k_get_temp(0); | |
82ba1d3f GR |
611 | if (err >= 0) |
612 | i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP1; | |
d83c39de GR |
613 | /* check for additional temperature sensors */ |
614 | err = i8k_get_temp(1); | |
615 | if (err >= 0) | |
616 | i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP2; | |
617 | err = i8k_get_temp(2); | |
618 | if (err >= 0) | |
619 | i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP3; | |
620 | err = i8k_get_temp(3); | |
621 | if (err >= 0) | |
622 | i8k_hwmon_flags |= I8K_HWMON_HAVE_TEMP4; | |
949a9d70 JD |
623 | |
624 | /* Left fan attributes, if left fan is present */ | |
625 | err = i8k_get_fan_status(I8K_FAN_LEFT); | |
82ba1d3f GR |
626 | if (err >= 0) |
627 | i8k_hwmon_flags |= I8K_HWMON_HAVE_FAN1; | |
949a9d70 JD |
628 | |
629 | /* Right fan attributes, if right fan is present */ | |
630 | err = i8k_get_fan_status(I8K_FAN_RIGHT); | |
82ba1d3f GR |
631 | if (err >= 0) |
632 | i8k_hwmon_flags |= I8K_HWMON_HAVE_FAN2; | |
949a9d70 | 633 | |
82ba1d3f GR |
634 | i8k_hwmon_dev = hwmon_device_register_with_groups(NULL, "i8k", NULL, |
635 | i8k_groups); | |
636 | if (IS_ERR(i8k_hwmon_dev)) { | |
637 | err = PTR_ERR(i8k_hwmon_dev); | |
638 | i8k_hwmon_dev = NULL; | |
639 | pr_err("hwmon registration failed (%d)\n", err); | |
640 | return err; | |
641 | } | |
949a9d70 | 642 | return 0; |
949a9d70 JD |
643 | } |
644 | ||
81474fc2 GR |
645 | struct i8k_config_data { |
646 | int fan_mult; | |
647 | int fan_max; | |
648 | }; | |
649 | ||
650 | enum i8k_configs { | |
651 | DELL_STUDIO, | |
652 | DELL_XPS_M140, | |
653 | }; | |
654 | ||
655 | static const struct i8k_config_data i8k_config_data[] = { | |
656 | [DELL_STUDIO] = { | |
657 | .fan_mult = 1, | |
658 | .fan_max = I8K_FAN_HIGH, | |
659 | }, | |
660 | [DELL_XPS_M140] = { | |
661 | .fan_mult = 1, | |
662 | .fan_max = I8K_FAN_HIGH, | |
663 | }, | |
664 | }; | |
665 | ||
12186f51 | 666 | static struct dmi_system_id i8k_dmi_table[] __initdata = { |
e70c9d5e DT |
667 | { |
668 | .ident = "Dell Inspiron", | |
669 | .matches = { | |
670 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer"), | |
671 | DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron"), | |
672 | }, | |
673 | }, | |
674 | { | |
675 | .ident = "Dell Latitude", | |
676 | .matches = { | |
677 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer"), | |
678 | DMI_MATCH(DMI_PRODUCT_NAME, "Latitude"), | |
679 | }, | |
680 | }, | |
7e0fa31d DT |
681 | { |
682 | .ident = "Dell Inspiron 2", | |
683 | .matches = { | |
684 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), | |
685 | DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron"), | |
686 | }, | |
687 | }, | |
688 | { | |
689 | .ident = "Dell Latitude 2", | |
690 | .matches = { | |
691 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), | |
692 | DMI_MATCH(DMI_PRODUCT_NAME, "Latitude"), | |
693 | }, | |
694 | }, | |
a9000d03 NW |
695 | { /* UK Inspiron 6400 */ |
696 | .ident = "Dell Inspiron 3", | |
697 | .matches = { | |
698 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), | |
699 | DMI_MATCH(DMI_PRODUCT_NAME, "MM061"), | |
700 | }, | |
701 | }, | |
48103c52 FS |
702 | { |
703 | .ident = "Dell Inspiron 3", | |
704 | .matches = { | |
705 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), | |
706 | DMI_MATCH(DMI_PRODUCT_NAME, "MP061"), | |
707 | }, | |
708 | }, | |
7ab21a86 AS |
709 | { |
710 | .ident = "Dell Precision", | |
711 | .matches = { | |
712 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), | |
713 | DMI_MATCH(DMI_PRODUCT_NAME, "Precision"), | |
714 | }, | |
715 | }, | |
bef2a508 FH |
716 | { |
717 | .ident = "Dell Vostro", | |
718 | .matches = { | |
719 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), | |
720 | DMI_MATCH(DMI_PRODUCT_NAME, "Vostro"), | |
721 | }, | |
722 | }, | |
9aa5b018 AC |
723 | { |
724 | .ident = "Dell XPS421", | |
725 | .matches = { | |
726 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), | |
727 | DMI_MATCH(DMI_PRODUCT_NAME, "XPS L421X"), | |
728 | }, | |
729 | }, | |
ff457bde GR |
730 | { |
731 | .ident = "Dell Studio", | |
732 | .matches = { | |
733 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), | |
734 | DMI_MATCH(DMI_PRODUCT_NAME, "Studio"), | |
735 | }, | |
81474fc2 | 736 | .driver_data = (void *)&i8k_config_data[DELL_STUDIO], |
ff457bde | 737 | }, |
919a0304 GR |
738 | { |
739 | .ident = "Dell XPS M140", | |
740 | .matches = { | |
741 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), | |
742 | DMI_MATCH(DMI_PRODUCT_NAME, "MXC051"), | |
743 | }, | |
81474fc2 | 744 | .driver_data = (void *)&i8k_config_data[DELL_XPS_M140], |
919a0304 | 745 | }, |
12186f51 | 746 | { } |
e70c9d5e | 747 | }; |
1da177e4 LT |
748 | |
749 | /* | |
750 | * Probe for the presence of a supported laptop. | |
751 | */ | |
752 | static int __init i8k_probe(void) | |
753 | { | |
26d09382 | 754 | const struct dmi_system_id *id; |
dec63ec3 | 755 | |
1da177e4 | 756 | /* |
dec63ec3 | 757 | * Get DMI information |
1da177e4 | 758 | */ |
e70c9d5e DT |
759 | if (!dmi_check_system(i8k_dmi_table)) { |
760 | if (!ignore_dmi && !force) | |
761 | return -ENODEV; | |
762 | ||
60e71aaf GR |
763 | pr_info("not running on a supported Dell system.\n"); |
764 | pr_info("vendor=%s, model=%s, version=%s\n", | |
e70c9d5e DT |
765 | i8k_get_dmi_data(DMI_SYS_VENDOR), |
766 | i8k_get_dmi_data(DMI_PRODUCT_NAME), | |
767 | i8k_get_dmi_data(DMI_BIOS_VERSION)); | |
1da177e4 | 768 | } |
dec63ec3 | 769 | |
12186f51 GR |
770 | strlcpy(bios_version, i8k_get_dmi_data(DMI_BIOS_VERSION), |
771 | sizeof(bios_version)); | |
e70c9d5e | 772 | |
1da177e4 | 773 | /* |
dec63ec3 | 774 | * Get SMM Dell signature |
1da177e4 | 775 | */ |
7e0fa31d DT |
776 | if (i8k_get_dell_signature(I8K_SMM_GET_DELL_SIG1) && |
777 | i8k_get_dell_signature(I8K_SMM_GET_DELL_SIG2)) { | |
60e71aaf | 778 | pr_err("unable to get SMM Dell signature\n"); |
e70c9d5e DT |
779 | if (!force) |
780 | return -ENODEV; | |
1da177e4 | 781 | } |
1da177e4 | 782 | |
26d09382 | 783 | i8k_fan_mult = fan_mult; |
81474fc2 | 784 | i8k_fan_max = fan_max ? : I8K_FAN_HIGH; /* Must not be 0 */ |
26d09382 | 785 | id = dmi_first_match(i8k_dmi_table); |
81474fc2 GR |
786 | if (id && id->driver_data) { |
787 | const struct i8k_config_data *conf = id->driver_data; | |
788 | ||
789 | if (fan_mult == I8K_FAN_MULT && conf->fan_mult) | |
790 | i8k_fan_mult = conf->fan_mult; | |
791 | if (fan_max == I8K_FAN_HIGH && conf->fan_max) | |
792 | i8k_fan_max = conf->fan_max; | |
793 | } | |
794 | i8k_pwm_mult = DIV_ROUND_UP(255, i8k_fan_max); | |
26d09382 | 795 | |
dec63ec3 | 796 | return 0; |
1da177e4 LT |
797 | } |
798 | ||
8378b924 | 799 | static int __init i8k_init(void) |
1da177e4 | 800 | { |
dec63ec3 | 801 | struct proc_dir_entry *proc_i8k; |
949a9d70 | 802 | int err; |
dec63ec3 DT |
803 | |
804 | /* Are we running on an supported laptop? */ | |
e70c9d5e | 805 | if (i8k_probe()) |
dec63ec3 | 806 | return -ENODEV; |
dec63ec3 DT |
807 | |
808 | /* Register the proc entry */ | |
1b502217 | 809 | proc_i8k = proc_create("i8k", 0, NULL, &i8k_fops); |
352f8f8b | 810 | if (!proc_i8k) |
dec63ec3 | 811 | return -ENOENT; |
352f8f8b | 812 | |
949a9d70 JD |
813 | err = i8k_init_hwmon(); |
814 | if (err) | |
815 | goto exit_remove_proc; | |
816 | ||
dec63ec3 | 817 | return 0; |
949a9d70 JD |
818 | |
819 | exit_remove_proc: | |
820 | remove_proc_entry("i8k", NULL); | |
821 | return err; | |
1da177e4 LT |
822 | } |
823 | ||
8378b924 | 824 | static void __exit i8k_exit(void) |
1da177e4 | 825 | { |
82ba1d3f | 826 | hwmon_device_unregister(i8k_hwmon_dev); |
dec63ec3 | 827 | remove_proc_entry("i8k", NULL); |
1da177e4 | 828 | } |
1da177e4 | 829 | |
8378b924 DT |
830 | module_init(i8k_init); |
831 | module_exit(i8k_exit); |