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