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