Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * acpi_fan.c - ACPI Fan Driver ($Revision: 29 $) | |
3 | * | |
4 | * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com> | |
5 | * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com> | |
6 | * | |
7 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License as published by | |
11 | * the Free Software Foundation; either version 2 of the License, or (at | |
12 | * your option) any 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 | * You should have received a copy of the GNU General Public License along | |
20 | * with this program; if not, write to the Free Software Foundation, Inc., | |
21 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. | |
22 | * | |
23 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
24 | */ | |
25 | ||
26 | #include <linux/kernel.h> | |
27 | #include <linux/module.h> | |
28 | #include <linux/init.h> | |
29 | #include <linux/types.h> | |
30 | #include <linux/proc_fs.h> | |
31 | #include <linux/seq_file.h> | |
32 | #include <asm/uaccess.h> | |
33 | ||
34 | #include <acpi/acpi_bus.h> | |
35 | #include <acpi/acpi_drivers.h> | |
36 | ||
1da177e4 LT |
37 | #define ACPI_FAN_COMPONENT 0x00200000 |
38 | #define ACPI_FAN_CLASS "fan" | |
1da177e4 | 39 | #define ACPI_FAN_FILE_STATE "state" |
1da177e4 LT |
40 | |
41 | #define _COMPONENT ACPI_FAN_COMPONENT | |
f52fd66d | 42 | ACPI_MODULE_NAME("fan"); |
1da177e4 | 43 | |
f52fd66d | 44 | MODULE_AUTHOR("Paul Diefenbaugh"); |
7cda93e0 | 45 | MODULE_DESCRIPTION("ACPI Fan Driver"); |
1da177e4 LT |
46 | MODULE_LICENSE("GPL"); |
47 | ||
4be44fcd LB |
48 | static int acpi_fan_add(struct acpi_device *device); |
49 | static int acpi_fan_remove(struct acpi_device *device, int type); | |
5d9464a4 PM |
50 | static int acpi_fan_suspend(struct acpi_device *device, pm_message_t state); |
51 | static int acpi_fan_resume(struct acpi_device *device); | |
1da177e4 LT |
52 | |
53 | static struct acpi_driver acpi_fan_driver = { | |
c2b6705b | 54 | .name = "fan", |
4be44fcd LB |
55 | .class = ACPI_FAN_CLASS, |
56 | .ids = "PNP0C0B", | |
57 | .ops = { | |
58 | .add = acpi_fan_add, | |
59 | .remove = acpi_fan_remove, | |
0feabb01 KK |
60 | .suspend = acpi_fan_suspend, |
61 | .resume = acpi_fan_resume, | |
4be44fcd | 62 | }, |
1da177e4 LT |
63 | }; |
64 | ||
65 | struct acpi_fan { | |
74b142e0 | 66 | struct acpi_device * device; |
1da177e4 LT |
67 | }; |
68 | ||
1da177e4 LT |
69 | /* -------------------------------------------------------------------------- |
70 | FS Interface (/proc) | |
71 | -------------------------------------------------------------------------- */ | |
72 | ||
4be44fcd | 73 | static struct proc_dir_entry *acpi_fan_dir; |
1da177e4 | 74 | |
4be44fcd | 75 | static int acpi_fan_read_state(struct seq_file *seq, void *offset) |
1da177e4 | 76 | { |
4be44fcd LB |
77 | struct acpi_fan *fan = seq->private; |
78 | int state = 0; | |
1da177e4 | 79 | |
1da177e4 LT |
80 | |
81 | if (fan) { | |
dc8c2b27 | 82 | if (acpi_bus_get_power(fan->device->handle, &state)) |
1da177e4 LT |
83 | seq_printf(seq, "status: ERROR\n"); |
84 | else | |
85 | seq_printf(seq, "status: %s\n", | |
4be44fcd | 86 | !state ? "on" : "off"); |
1da177e4 | 87 | } |
d550d98d | 88 | return 0; |
1da177e4 LT |
89 | } |
90 | ||
91 | static int acpi_fan_state_open_fs(struct inode *inode, struct file *file) | |
92 | { | |
93 | return single_open(file, acpi_fan_read_state, PDE(inode)->data); | |
94 | } | |
95 | ||
96 | static ssize_t | |
4be44fcd LB |
97 | acpi_fan_write_state(struct file *file, const char __user * buffer, |
98 | size_t count, loff_t * ppos) | |
1da177e4 | 99 | { |
4be44fcd | 100 | int result = 0; |
50dd0969 JE |
101 | struct seq_file *m = file->private_data; |
102 | struct acpi_fan *fan = m->private; | |
4be44fcd | 103 | char state_string[12] = { '\0' }; |
1da177e4 | 104 | |
1da177e4 LT |
105 | |
106 | if (!fan || (count > sizeof(state_string) - 1)) | |
d550d98d | 107 | return -EINVAL; |
4be44fcd | 108 | |
1da177e4 | 109 | if (copy_from_user(state_string, buffer, count)) |
d550d98d | 110 | return -EFAULT; |
4be44fcd | 111 | |
1da177e4 | 112 | state_string[count] = '\0'; |
4be44fcd | 113 | |
dc8c2b27 | 114 | result = acpi_bus_set_power(fan->device->handle, |
4be44fcd | 115 | simple_strtoul(state_string, NULL, 0)); |
1da177e4 | 116 | if (result) |
d550d98d | 117 | return result; |
1da177e4 | 118 | |
d550d98d | 119 | return count; |
1da177e4 LT |
120 | } |
121 | ||
d7508032 | 122 | static const struct file_operations acpi_fan_state_ops = { |
4be44fcd LB |
123 | .open = acpi_fan_state_open_fs, |
124 | .read = seq_read, | |
125 | .write = acpi_fan_write_state, | |
126 | .llseek = seq_lseek, | |
127 | .release = single_release, | |
1da177e4 LT |
128 | .owner = THIS_MODULE, |
129 | }; | |
130 | ||
4be44fcd | 131 | static int acpi_fan_add_fs(struct acpi_device *device) |
1da177e4 | 132 | { |
4be44fcd | 133 | struct proc_dir_entry *entry = NULL; |
1da177e4 | 134 | |
1da177e4 LT |
135 | |
136 | if (!device) | |
d550d98d | 137 | return -EINVAL; |
1da177e4 LT |
138 | |
139 | if (!acpi_device_dir(device)) { | |
140 | acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), | |
4be44fcd | 141 | acpi_fan_dir); |
1da177e4 | 142 | if (!acpi_device_dir(device)) |
d550d98d | 143 | return -ENODEV; |
1da177e4 LT |
144 | acpi_device_dir(device)->owner = THIS_MODULE; |
145 | } | |
146 | ||
147 | /* 'status' [R/W] */ | |
148 | entry = create_proc_entry(ACPI_FAN_FILE_STATE, | |
4be44fcd LB |
149 | S_IFREG | S_IRUGO | S_IWUSR, |
150 | acpi_device_dir(device)); | |
1da177e4 | 151 | if (!entry) |
d550d98d | 152 | return -ENODEV; |
1da177e4 LT |
153 | else { |
154 | entry->proc_fops = &acpi_fan_state_ops; | |
155 | entry->data = acpi_driver_data(device); | |
156 | entry->owner = THIS_MODULE; | |
157 | } | |
158 | ||
d550d98d | 159 | return 0; |
1da177e4 LT |
160 | } |
161 | ||
4be44fcd | 162 | static int acpi_fan_remove_fs(struct acpi_device *device) |
1da177e4 | 163 | { |
1da177e4 LT |
164 | |
165 | if (acpi_device_dir(device)) { | |
4be44fcd | 166 | remove_proc_entry(ACPI_FAN_FILE_STATE, acpi_device_dir(device)); |
1da177e4 LT |
167 | remove_proc_entry(acpi_device_bid(device), acpi_fan_dir); |
168 | acpi_device_dir(device) = NULL; | |
169 | } | |
170 | ||
d550d98d | 171 | return 0; |
1da177e4 LT |
172 | } |
173 | ||
1da177e4 LT |
174 | /* -------------------------------------------------------------------------- |
175 | Driver Interface | |
176 | -------------------------------------------------------------------------- */ | |
177 | ||
4be44fcd | 178 | static int acpi_fan_add(struct acpi_device *device) |
1da177e4 | 179 | { |
4be44fcd LB |
180 | int result = 0; |
181 | struct acpi_fan *fan = NULL; | |
182 | int state = 0; | |
1da177e4 | 183 | |
1da177e4 LT |
184 | |
185 | if (!device) | |
d550d98d | 186 | return -EINVAL; |
1da177e4 | 187 | |
36bcbec7 | 188 | fan = kzalloc(sizeof(struct acpi_fan), GFP_KERNEL); |
1da177e4 | 189 | if (!fan) |
d550d98d | 190 | return -ENOMEM; |
1da177e4 | 191 | |
74b142e0 | 192 | fan->device = device; |
c65ade4d | 193 | strcpy(acpi_device_name(device), "Fan"); |
1da177e4 LT |
194 | strcpy(acpi_device_class(device), ACPI_FAN_CLASS); |
195 | acpi_driver_data(device) = fan; | |
196 | ||
dc8c2b27 | 197 | result = acpi_bus_get_power(device->handle, &state); |
1da177e4 | 198 | if (result) { |
6468463a | 199 | printk(KERN_ERR PREFIX "Reading power state\n"); |
1da177e4 LT |
200 | goto end; |
201 | } | |
202 | ||
0feabb01 KK |
203 | device->flags.force_power_state = 1; |
204 | acpi_bus_set_power(device->handle, state); | |
205 | device->flags.force_power_state = 0; | |
206 | ||
1da177e4 LT |
207 | result = acpi_fan_add_fs(device); |
208 | if (result) | |
209 | goto end; | |
210 | ||
211 | printk(KERN_INFO PREFIX "%s [%s] (%s)\n", | |
4be44fcd LB |
212 | acpi_device_name(device), acpi_device_bid(device), |
213 | !device->power.state ? "on" : "off"); | |
1da177e4 | 214 | |
4be44fcd | 215 | end: |
1da177e4 LT |
216 | if (result) |
217 | kfree(fan); | |
218 | ||
d550d98d | 219 | return result; |
1da177e4 LT |
220 | } |
221 | ||
4be44fcd | 222 | static int acpi_fan_remove(struct acpi_device *device, int type) |
1da177e4 | 223 | { |
c65ade4d | 224 | struct acpi_fan *fan = NULL; |
1da177e4 | 225 | |
1da177e4 LT |
226 | |
227 | if (!device || !acpi_driver_data(device)) | |
d550d98d | 228 | return -EINVAL; |
1da177e4 | 229 | |
50dd0969 | 230 | fan = acpi_driver_data(device); |
1da177e4 LT |
231 | |
232 | acpi_fan_remove_fs(device); | |
233 | ||
234 | kfree(fan); | |
235 | ||
d550d98d | 236 | return 0; |
1da177e4 LT |
237 | } |
238 | ||
5d9464a4 | 239 | static int acpi_fan_suspend(struct acpi_device *device, pm_message_t state) |
0feabb01 KK |
240 | { |
241 | if (!device) | |
242 | return -EINVAL; | |
243 | ||
244 | acpi_bus_set_power(device->handle, ACPI_STATE_D0); | |
245 | ||
246 | return AE_OK; | |
247 | } | |
248 | ||
5d9464a4 | 249 | static int acpi_fan_resume(struct acpi_device *device) |
0feabb01 KK |
250 | { |
251 | int result = 0; | |
252 | int power_state = 0; | |
253 | ||
254 | if (!device) | |
255 | return -EINVAL; | |
256 | ||
257 | result = acpi_bus_get_power(device->handle, &power_state); | |
258 | if (result) { | |
259 | ACPI_DEBUG_PRINT((ACPI_DB_ERROR, | |
260 | "Error reading fan power state\n")); | |
261 | return result; | |
262 | } | |
263 | ||
264 | device->flags.force_power_state = 1; | |
265 | acpi_bus_set_power(device->handle, power_state); | |
266 | device->flags.force_power_state = 0; | |
267 | ||
268 | return result; | |
269 | } | |
270 | ||
4be44fcd | 271 | static int __init acpi_fan_init(void) |
1da177e4 | 272 | { |
c65ade4d | 273 | int result = 0; |
1da177e4 | 274 | |
1da177e4 LT |
275 | |
276 | acpi_fan_dir = proc_mkdir(ACPI_FAN_CLASS, acpi_root_dir); | |
277 | if (!acpi_fan_dir) | |
d550d98d | 278 | return -ENODEV; |
1da177e4 LT |
279 | acpi_fan_dir->owner = THIS_MODULE; |
280 | ||
281 | result = acpi_bus_register_driver(&acpi_fan_driver); | |
282 | if (result < 0) { | |
283 | remove_proc_entry(ACPI_FAN_CLASS, acpi_root_dir); | |
d550d98d | 284 | return -ENODEV; |
1da177e4 LT |
285 | } |
286 | ||
d550d98d | 287 | return 0; |
1da177e4 LT |
288 | } |
289 | ||
4be44fcd | 290 | static void __exit acpi_fan_exit(void) |
1da177e4 | 291 | { |
1da177e4 LT |
292 | |
293 | acpi_bus_unregister_driver(&acpi_fan_driver); | |
294 | ||
295 | remove_proc_entry(ACPI_FAN_CLASS, acpi_root_dir); | |
296 | ||
d550d98d | 297 | return; |
1da177e4 LT |
298 | } |
299 | ||
1da177e4 LT |
300 | module_init(acpi_fan_init); |
301 | module_exit(acpi_fan_exit); |