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