Blame view
drivers/acpi/fan.c
9 KB
1da177e4c
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
/* * acpi_fan.c - ACPI Fan Driver ($Revision: 29 $) * * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com> * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com> * * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or (at * your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ #include <linux/kernel.h> #include <linux/module.h> #include <linux/init.h> #include <linux/types.h> #include <linux/proc_fs.h> #include <linux/seq_file.h> #include <asm/uaccess.h> |
05a83d972
|
33 |
#include <linux/thermal.h> |
1da177e4c
|
34 35 |
#include <acpi/acpi_bus.h> #include <acpi/acpi_drivers.h> |
a192a9580
|
36 |
#define PREFIX "ACPI: " |
1da177e4c
|
37 |
#define ACPI_FAN_CLASS "fan" |
1da177e4c
|
38 |
#define ACPI_FAN_FILE_STATE "state" |
1da177e4c
|
39 40 |
#define _COMPONENT ACPI_FAN_COMPONENT |
f52fd66d2
|
41 |
ACPI_MODULE_NAME("fan"); |
1da177e4c
|
42 |
|
f52fd66d2
|
43 |
MODULE_AUTHOR("Paul Diefenbaugh"); |
7cda93e00
|
44 |
MODULE_DESCRIPTION("ACPI Fan Driver"); |
1da177e4c
|
45 |
MODULE_LICENSE("GPL"); |
4be44fcd3
|
46 47 |
static int acpi_fan_add(struct acpi_device *device); static int acpi_fan_remove(struct acpi_device *device, int type); |
ec68373c0
|
48 49 |
static int acpi_fan_suspend(struct acpi_device *device, pm_message_t state); static int acpi_fan_resume(struct acpi_device *device); |
1da177e4c
|
50 |
|
1ba90e3a8
|
51 52 53 54 55 |
static const struct acpi_device_id fan_device_ids[] = { {"PNP0C0B", 0}, {"", 0}, }; MODULE_DEVICE_TABLE(acpi, fan_device_ids); |
1da177e4c
|
56 |
static struct acpi_driver acpi_fan_driver = { |
c2b6705b7
|
57 |
.name = "fan", |
4be44fcd3
|
58 |
.class = ACPI_FAN_CLASS, |
1ba90e3a8
|
59 |
.ids = fan_device_ids, |
4be44fcd3
|
60 61 62 |
.ops = { .add = acpi_fan_add, .remove = acpi_fan_remove, |
ec68373c0
|
63 64 |
.suspend = acpi_fan_suspend, .resume = acpi_fan_resume, |
4be44fcd3
|
65 |
}, |
1da177e4c
|
66 |
}; |
05a83d972
|
67 |
/* thermal cooling device callbacks */ |
6503e5df0
|
68 69 |
static int fan_get_max_state(struct thermal_cooling_device *cdev, unsigned long *state) |
05a83d972
|
70 71 |
{ /* ACPI fan device only support two states: ON/OFF */ |
6503e5df0
|
72 73 |
*state = 1; return 0; |
05a83d972
|
74 |
} |
6503e5df0
|
75 76 |
static int fan_get_cur_state(struct thermal_cooling_device *cdev, unsigned long *state) |
05a83d972
|
77 78 |
{ struct acpi_device *device = cdev->devdata; |
05a83d972
|
79 |
int result; |
6503e5df0
|
80 |
int acpi_state; |
05a83d972
|
81 82 83 |
if (!device) return -EINVAL; |
6503e5df0
|
84 |
result = acpi_bus_get_power(device->handle, &acpi_state); |
05a83d972
|
85 86 |
if (result) return result; |
6503e5df0
|
87 88 89 |
*state = (acpi_state == ACPI_STATE_D3 ? 0 : (acpi_state == ACPI_STATE_D0 ? 1 : -1)); return 0; |
05a83d972
|
90 91 92 |
} static int |
6503e5df0
|
93 |
fan_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state) |
05a83d972
|
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
{ struct acpi_device *device = cdev->devdata; int result; if (!device || (state != 0 && state != 1)) return -EINVAL; result = acpi_bus_set_power(device->handle, state ? ACPI_STATE_D0 : ACPI_STATE_D3); return result; } static struct thermal_cooling_device_ops fan_cooling_ops = { .get_max_state = fan_get_max_state, .get_cur_state = fan_get_cur_state, .set_cur_state = fan_set_cur_state, }; |
1da177e4c
|
112 113 114 |
/* -------------------------------------------------------------------------- FS Interface (/proc) -------------------------------------------------------------------------- */ |
05a83d972
|
115 |
#ifdef CONFIG_ACPI_PROCFS |
1da177e4c
|
116 |
|
4be44fcd3
|
117 |
static struct proc_dir_entry *acpi_fan_dir; |
1da177e4c
|
118 |
|
4be44fcd3
|
119 |
static int acpi_fan_read_state(struct seq_file *seq, void *offset) |
1da177e4c
|
120 |
{ |
968fc5dc2
|
121 |
struct acpi_device *device = seq->private; |
4be44fcd3
|
122 |
int state = 0; |
1da177e4c
|
123 |
|
1da177e4c
|
124 |
|
968fc5dc2
|
125 126 |
if (device) { if (acpi_bus_get_power(device->handle, &state)) |
1da177e4c
|
127 128 129 130 131 |
seq_printf(seq, "status: ERROR "); else seq_printf(seq, "status: %s ", |
4be44fcd3
|
132 |
!state ? "on" : "off"); |
1da177e4c
|
133 |
} |
d550d98d3
|
134 |
return 0; |
1da177e4c
|
135 136 137 138 139 140 141 142 |
} static int acpi_fan_state_open_fs(struct inode *inode, struct file *file) { return single_open(file, acpi_fan_read_state, PDE(inode)->data); } static ssize_t |
4be44fcd3
|
143 144 |
acpi_fan_write_state(struct file *file, const char __user * buffer, size_t count, loff_t * ppos) |
1da177e4c
|
145 |
{ |
4be44fcd3
|
146 |
int result = 0; |
50dd09697
|
147 |
struct seq_file *m = file->private_data; |
968fc5dc2
|
148 |
struct acpi_device *device = m->private; |
6594d87eb
|
149 |
char state_string[3] = { '\0' }; |
1da177e4c
|
150 |
|
968fc5dc2
|
151 |
if (count > sizeof(state_string) - 1) |
d550d98d3
|
152 |
return -EINVAL; |
4be44fcd3
|
153 |
|
1da177e4c
|
154 |
if (copy_from_user(state_string, buffer, count)) |
d550d98d3
|
155 |
return -EFAULT; |
4be44fcd3
|
156 |
|
1da177e4c
|
157 |
state_string[count] = '\0'; |
6594d87eb
|
158 159 160 161 162 163 164 |
if ((state_string[0] < '0') || (state_string[0] > '3')) return -EINVAL; if (state_string[1] == ' ') state_string[1] = '\0'; if (state_string[1] != '\0') return -EINVAL; |
4be44fcd3
|
165 |
|
968fc5dc2
|
166 |
result = acpi_bus_set_power(device->handle, |
4be44fcd3
|
167 |
simple_strtoul(state_string, NULL, 0)); |
1da177e4c
|
168 |
if (result) |
d550d98d3
|
169 |
return result; |
1da177e4c
|
170 |
|
d550d98d3
|
171 |
return count; |
1da177e4c
|
172 |
} |
d75080328
|
173 |
static const struct file_operations acpi_fan_state_ops = { |
4be44fcd3
|
174 175 176 177 178 |
.open = acpi_fan_state_open_fs, .read = seq_read, .write = acpi_fan_write_state, .llseek = seq_lseek, .release = single_release, |
1da177e4c
|
179 180 |
.owner = THIS_MODULE, }; |
4be44fcd3
|
181 |
static int acpi_fan_add_fs(struct acpi_device *device) |
1da177e4c
|
182 |
{ |
4be44fcd3
|
183 |
struct proc_dir_entry *entry = NULL; |
1da177e4c
|
184 |
|
1da177e4c
|
185 186 |
if (!device) |
d550d98d3
|
187 |
return -EINVAL; |
1da177e4c
|
188 189 190 |
if (!acpi_device_dir(device)) { acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), |
4be44fcd3
|
191 |
acpi_fan_dir); |
1da177e4c
|
192 |
if (!acpi_device_dir(device)) |
d550d98d3
|
193 |
return -ENODEV; |
1da177e4c
|
194 195 196 |
} /* 'status' [R/W] */ |
cf7acfab0
|
197 198 199 200 201 |
entry = proc_create_data(ACPI_FAN_FILE_STATE, S_IFREG | S_IRUGO | S_IWUSR, acpi_device_dir(device), &acpi_fan_state_ops, device); |
1da177e4c
|
202 |
if (!entry) |
d550d98d3
|
203 |
return -ENODEV; |
d550d98d3
|
204 |
return 0; |
1da177e4c
|
205 |
} |
4be44fcd3
|
206 |
static int acpi_fan_remove_fs(struct acpi_device *device) |
1da177e4c
|
207 |
{ |
1da177e4c
|
208 209 |
if (acpi_device_dir(device)) { |
4be44fcd3
|
210 |
remove_proc_entry(ACPI_FAN_FILE_STATE, acpi_device_dir(device)); |
1da177e4c
|
211 212 213 |
remove_proc_entry(acpi_device_bid(device), acpi_fan_dir); acpi_device_dir(device) = NULL; } |
d550d98d3
|
214 |
return 0; |
1da177e4c
|
215 |
} |
05a83d972
|
216 217 218 219 220 |
#else static int acpi_fan_add_fs(struct acpi_device *device) { return 0; } |
1da177e4c
|
221 |
|
05a83d972
|
222 223 224 225 226 |
static int acpi_fan_remove_fs(struct acpi_device *device) { return 0; } #endif |
1da177e4c
|
227 228 229 |
/* -------------------------------------------------------------------------- Driver Interface -------------------------------------------------------------------------- */ |
4be44fcd3
|
230 |
static int acpi_fan_add(struct acpi_device *device) |
1da177e4c
|
231 |
{ |
4be44fcd3
|
232 |
int result = 0; |
4be44fcd3
|
233 |
int state = 0; |
05a83d972
|
234 |
struct thermal_cooling_device *cdev; |
1da177e4c
|
235 236 |
if (!device) |
d550d98d3
|
237 |
return -EINVAL; |
1da177e4c
|
238 |
|
c65ade4dc
|
239 |
strcpy(acpi_device_name(device), "Fan"); |
1da177e4c
|
240 |
strcpy(acpi_device_class(device), ACPI_FAN_CLASS); |
1da177e4c
|
241 |
|
dc8c2b274
|
242 |
result = acpi_bus_get_power(device->handle, &state); |
1da177e4c
|
243 |
if (result) { |
6468463ab
|
244 245 |
printk(KERN_ERR PREFIX "Reading power state "); |
1da177e4c
|
246 247 |
goto end; } |
ec68373c0
|
248 249 250 |
device->flags.force_power_state = 1; acpi_bus_set_power(device->handle, state); device->flags.force_power_state = 0; |
05a83d972
|
251 252 |
cdev = thermal_cooling_device_register("Fan", device, &fan_cooling_ops); |
19b36780e
|
253 254 255 256 |
if (IS_ERR(cdev)) { result = PTR_ERR(cdev); goto end; } |
9030062f3
|
257 |
|
13c411570
|
258 259 |
dev_dbg(&device->dev, "registered as cooling_device%d ", cdev->id); |
9030062f3
|
260 |
|
db89b4f0d
|
261 |
device->driver_data = cdev; |
9030062f3
|
262 263 264 265 |
result = sysfs_create_link(&device->dev.kobj, &cdev->device.kobj, "thermal_cooling"); if (result) |
fc3a8828b
|
266 267 268 |
dev_err(&device->dev, "Failed to create sysfs link " "'thermal_cooling' "); |
9030062f3
|
269 270 271 272 273 |
result = sysfs_create_link(&cdev->device.kobj, &device->dev.kobj, "device"); if (result) |
fc3a8828b
|
274 275 276 |
dev_err(&device->dev, "Failed to create sysfs link " "'device' "); |
05a83d972
|
277 |
|
1da177e4c
|
278 279 280 281 282 283 |
result = acpi_fan_add_fs(device); if (result) goto end; printk(KERN_INFO PREFIX "%s [%s] (%s) ", |
4be44fcd3
|
284 285 |
acpi_device_name(device), acpi_device_bid(device), !device->power.state ? "on" : "off"); |
1da177e4c
|
286 |
|
4be44fcd3
|
287 |
end: |
d550d98d3
|
288 |
return result; |
1da177e4c
|
289 |
} |
4be44fcd3
|
290 |
static int acpi_fan_remove(struct acpi_device *device, int type) |
1da177e4c
|
291 |
{ |
05a83d972
|
292 293 294 |
struct thermal_cooling_device *cdev = acpi_driver_data(device); if (!device || !cdev) |
d550d98d3
|
295 |
return -EINVAL; |
1da177e4c
|
296 |
|
1da177e4c
|
297 |
acpi_fan_remove_fs(device); |
05a83d972
|
298 299 300 |
sysfs_remove_link(&device->dev.kobj, "thermal_cooling"); sysfs_remove_link(&cdev->device.kobj, "device"); thermal_cooling_device_unregister(cdev); |
1da177e4c
|
301 |
|
d550d98d3
|
302 |
return 0; |
1da177e4c
|
303 |
} |
ec68373c0
|
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
static int acpi_fan_suspend(struct acpi_device *device, pm_message_t state) { if (!device) return -EINVAL; acpi_bus_set_power(device->handle, ACPI_STATE_D0); return AE_OK; } static int acpi_fan_resume(struct acpi_device *device) { int result = 0; int power_state = 0; if (!device) return -EINVAL; result = acpi_bus_get_power(device->handle, &power_state); if (result) { |
55ac9a018
|
324 325 326 |
printk(KERN_ERR PREFIX "Error reading fan power state "); |
ec68373c0
|
327 328 329 330 331 332 333 334 335 |
return result; } device->flags.force_power_state = 1; acpi_bus_set_power(device->handle, power_state); device->flags.force_power_state = 0; return result; } |
4be44fcd3
|
336 |
static int __init acpi_fan_init(void) |
1da177e4c
|
337 |
{ |
c65ade4dc
|
338 |
int result = 0; |
1da177e4c
|
339 |
|
3c6028405
|
340 |
#ifdef CONFIG_ACPI_PROCFS |
1da177e4c
|
341 342 |
acpi_fan_dir = proc_mkdir(ACPI_FAN_CLASS, acpi_root_dir); if (!acpi_fan_dir) |
d550d98d3
|
343 |
return -ENODEV; |
3c6028405
|
344 |
#endif |
1da177e4c
|
345 346 347 |
result = acpi_bus_register_driver(&acpi_fan_driver); if (result < 0) { |
934231de7
|
348 |
#ifdef CONFIG_ACPI_PROCFS |
1da177e4c
|
349 |
remove_proc_entry(ACPI_FAN_CLASS, acpi_root_dir); |
934231de7
|
350 |
#endif |
d550d98d3
|
351 |
return -ENODEV; |
1da177e4c
|
352 |
} |
d550d98d3
|
353 |
return 0; |
1da177e4c
|
354 |
} |
4be44fcd3
|
355 |
static void __exit acpi_fan_exit(void) |
1da177e4c
|
356 |
{ |
1da177e4c
|
357 358 359 360 |
acpi_bus_unregister_driver(&acpi_fan_driver); remove_proc_entry(ACPI_FAN_CLASS, acpi_root_dir); |
d550d98d3
|
361 |
return; |
1da177e4c
|
362 |
} |
1da177e4c
|
363 364 |
module_init(acpi_fan_init); module_exit(acpi_fan_exit); |