blob: 65bdcde2c4e3a683988234c52d40ca34c3bceb7c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#define ACPI_FAN_COMPONENT 0x00200000
38#define ACPI_FAN_CLASS "fan"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#define ACPI_FAN_DRIVER_NAME "ACPI Fan Driver"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#define ACPI_FAN_FILE_STATE "state"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#define _COMPONENT ACPI_FAN_COMPONENT
Len Brown4be44fc2005-08-05 00:44:28 -040043ACPI_MODULE_NAME("acpi_fan")
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Len Brown4be44fc2005-08-05 00:44:28 -040045 MODULE_AUTHOR("Paul Diefenbaugh");
Linus Torvalds1da177e2005-04-16 15:20:36 -070046MODULE_DESCRIPTION(ACPI_FAN_DRIVER_NAME);
47MODULE_LICENSE("GPL");
48
Len Brown4be44fc2005-08-05 00:44:28 -040049static int acpi_fan_add(struct acpi_device *device);
50static int acpi_fan_remove(struct acpi_device *device, int type);
Konstantin Karasyov0feabb02006-05-08 00:00:00 -040051static int acpi_fan_suspend(struct acpi_device *device, int state);
52static int acpi_fan_resume(struct acpi_device *device, int state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54static struct acpi_driver acpi_fan_driver = {
Len Brown4be44fc2005-08-05 00:44:28 -040055 .name = ACPI_FAN_DRIVER_NAME,
56 .class = ACPI_FAN_CLASS,
57 .ids = "PNP0C0B",
58 .ops = {
59 .add = acpi_fan_add,
60 .remove = acpi_fan_remove,
Konstantin Karasyov0feabb02006-05-08 00:00:00 -040061 .suspend = acpi_fan_suspend,
62 .resume = acpi_fan_resume,
Len Brown4be44fc2005-08-05 00:44:28 -040063 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070064};
65
66struct acpi_fan {
Len Brown4be44fc2005-08-05 00:44:28 -040067 acpi_handle handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068};
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070/* --------------------------------------------------------------------------
71 FS Interface (/proc)
72 -------------------------------------------------------------------------- */
73
Len Brown4be44fc2005-08-05 00:44:28 -040074static struct proc_dir_entry *acpi_fan_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Len Brown4be44fc2005-08-05 00:44:28 -040076static int acpi_fan_read_state(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
Len Brown4be44fc2005-08-05 00:44:28 -040078 struct acpi_fan *fan = seq->private;
79 int state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81 ACPI_FUNCTION_TRACE("acpi_fan_read_state");
82
83 if (fan) {
84 if (acpi_bus_get_power(fan->handle, &state))
85 seq_printf(seq, "status: ERROR\n");
86 else
87 seq_printf(seq, "status: %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -040088 !state ? "on" : "off");
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 }
90 return_VALUE(0);
91}
92
93static int acpi_fan_state_open_fs(struct inode *inode, struct file *file)
94{
95 return single_open(file, acpi_fan_read_state, PDE(inode)->data);
96}
97
98static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -040099acpi_fan_write_state(struct file *file, const char __user * buffer,
100 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
Len Brown4be44fc2005-08-05 00:44:28 -0400102 int result = 0;
103 struct seq_file *m = (struct seq_file *)file->private_data;
104 struct acpi_fan *fan = (struct acpi_fan *)m->private;
105 char state_string[12] = { '\0' };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107 ACPI_FUNCTION_TRACE("acpi_fan_write_state");
108
109 if (!fan || (count > sizeof(state_string) - 1))
110 return_VALUE(-EINVAL);
Len Brown4be44fc2005-08-05 00:44:28 -0400111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 if (copy_from_user(state_string, buffer, count))
113 return_VALUE(-EFAULT);
Len Brown4be44fc2005-08-05 00:44:28 -0400114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 state_string[count] = '\0';
Len Brown4be44fc2005-08-05 00:44:28 -0400116
117 result = acpi_bus_set_power(fan->handle,
118 simple_strtoul(state_string, NULL, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 if (result)
120 return_VALUE(result);
121
122 return_VALUE(count);
123}
124
125static struct file_operations acpi_fan_state_ops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400126 .open = acpi_fan_state_open_fs,
127 .read = seq_read,
128 .write = acpi_fan_write_state,
129 .llseek = seq_lseek,
130 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 .owner = THIS_MODULE,
132};
133
Len Brown4be44fc2005-08-05 00:44:28 -0400134static int acpi_fan_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
Len Brown4be44fc2005-08-05 00:44:28 -0400136 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138 ACPI_FUNCTION_TRACE("acpi_fan_add_fs");
139
140 if (!device)
141 return_VALUE(-EINVAL);
142
143 if (!acpi_device_dir(device)) {
144 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown4be44fc2005-08-05 00:44:28 -0400145 acpi_fan_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 if (!acpi_device_dir(device))
147 return_VALUE(-ENODEV);
148 acpi_device_dir(device)->owner = THIS_MODULE;
149 }
150
151 /* 'status' [R/W] */
152 entry = create_proc_entry(ACPI_FAN_FILE_STATE,
Len Brown4be44fc2005-08-05 00:44:28 -0400153 S_IFREG | S_IRUGO | S_IWUSR,
154 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 if (!entry)
Thomas Renningera6fc6722006-06-26 23:58:43 -0400156 return_VALUE(-ENODEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 else {
158 entry->proc_fops = &acpi_fan_state_ops;
159 entry->data = acpi_driver_data(device);
160 entry->owner = THIS_MODULE;
161 }
162
163 return_VALUE(0);
164}
165
Len Brown4be44fc2005-08-05 00:44:28 -0400166static int acpi_fan_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
168 ACPI_FUNCTION_TRACE("acpi_fan_remove_fs");
169
170 if (acpi_device_dir(device)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400171 remove_proc_entry(ACPI_FAN_FILE_STATE, acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 remove_proc_entry(acpi_device_bid(device), acpi_fan_dir);
173 acpi_device_dir(device) = NULL;
174 }
175
176 return_VALUE(0);
177}
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179/* --------------------------------------------------------------------------
180 Driver Interface
181 -------------------------------------------------------------------------- */
182
Len Brown4be44fc2005-08-05 00:44:28 -0400183static int acpi_fan_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
Len Brown4be44fc2005-08-05 00:44:28 -0400185 int result = 0;
186 struct acpi_fan *fan = NULL;
187 int state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189 ACPI_FUNCTION_TRACE("acpi_fan_add");
190
191 if (!device)
192 return_VALUE(-EINVAL);
193
194 fan = kmalloc(sizeof(struct acpi_fan), GFP_KERNEL);
195 if (!fan)
196 return_VALUE(-ENOMEM);
197 memset(fan, 0, sizeof(struct acpi_fan));
198
199 fan->handle = device->handle;
Pavel Machekc65ade42005-08-05 00:37:45 -0400200 strcpy(acpi_device_name(device), "Fan");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 strcpy(acpi_device_class(device), ACPI_FAN_CLASS);
202 acpi_driver_data(device) = fan;
203
204 result = acpi_bus_get_power(fan->handle, &state);
205 if (result) {
Len Brown64684632006-06-26 23:41:38 -0400206 printk(KERN_ERR PREFIX "Reading power state\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 goto end;
208 }
209
Konstantin Karasyov0feabb02006-05-08 00:00:00 -0400210 device->flags.force_power_state = 1;
211 acpi_bus_set_power(device->handle, state);
212 device->flags.force_power_state = 0;
213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 result = acpi_fan_add_fs(device);
215 if (result)
216 goto end;
217
218 printk(KERN_INFO PREFIX "%s [%s] (%s)\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400219 acpi_device_name(device), acpi_device_bid(device),
220 !device->power.state ? "on" : "off");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Len Brown4be44fc2005-08-05 00:44:28 -0400222 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 if (result)
224 kfree(fan);
225
226 return_VALUE(result);
227}
228
Len Brown4be44fc2005-08-05 00:44:28 -0400229static int acpi_fan_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
Pavel Machekc65ade42005-08-05 00:37:45 -0400231 struct acpi_fan *fan = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 ACPI_FUNCTION_TRACE("acpi_fan_remove");
234
235 if (!device || !acpi_driver_data(device))
236 return_VALUE(-EINVAL);
237
Len Brown4be44fc2005-08-05 00:44:28 -0400238 fan = (struct acpi_fan *)acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 acpi_fan_remove_fs(device);
241
242 kfree(fan);
243
244 return_VALUE(0);
245}
246
Konstantin Karasyov0feabb02006-05-08 00:00:00 -0400247static int acpi_fan_suspend(struct acpi_device *device, int state)
248{
249 if (!device)
250 return -EINVAL;
251
252 acpi_bus_set_power(device->handle, ACPI_STATE_D0);
253
254 return AE_OK;
255}
256
257static int acpi_fan_resume(struct acpi_device *device, int state)
258{
259 int result = 0;
260 int power_state = 0;
261
262 if (!device)
263 return -EINVAL;
264
265 result = acpi_bus_get_power(device->handle, &power_state);
266 if (result) {
267 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
268 "Error reading fan power state\n"));
269 return result;
270 }
271
272 device->flags.force_power_state = 1;
273 acpi_bus_set_power(device->handle, power_state);
274 device->flags.force_power_state = 0;
275
276 return result;
277}
278
Len Brown4be44fc2005-08-05 00:44:28 -0400279static int __init acpi_fan_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
Pavel Machekc65ade42005-08-05 00:37:45 -0400281 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 ACPI_FUNCTION_TRACE("acpi_fan_init");
284
285 acpi_fan_dir = proc_mkdir(ACPI_FAN_CLASS, acpi_root_dir);
286 if (!acpi_fan_dir)
287 return_VALUE(-ENODEV);
288 acpi_fan_dir->owner = THIS_MODULE;
289
290 result = acpi_bus_register_driver(&acpi_fan_driver);
291 if (result < 0) {
292 remove_proc_entry(ACPI_FAN_CLASS, acpi_root_dir);
293 return_VALUE(-ENODEV);
294 }
295
296 return_VALUE(0);
297}
298
Len Brown4be44fc2005-08-05 00:44:28 -0400299static void __exit acpi_fan_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
301 ACPI_FUNCTION_TRACE("acpi_fan_exit");
302
303 acpi_bus_unregister_driver(&acpi_fan_driver);
304
305 remove_proc_entry(ACPI_FAN_CLASS, acpi_root_dir);
306
307 return_VOID;
308}
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310module_init(acpi_fan_init);
311module_exit(acpi_fan_exit);