blob: 8abf5ac17f671e48bd3323275383450b483e02e2 [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;
Patrick Mochel74b142e2006-05-19 16:54:39 -040068 struct acpi_device * device;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069};
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071/* --------------------------------------------------------------------------
72 FS Interface (/proc)
73 -------------------------------------------------------------------------- */
74
Len Brown4be44fc2005-08-05 00:44:28 -040075static struct proc_dir_entry *acpi_fan_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Len Brown4be44fc2005-08-05 00:44:28 -040077static int acpi_fan_read_state(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
Len Brown4be44fc2005-08-05 00:44:28 -040079 struct acpi_fan *fan = seq->private;
80 int state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
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 }
Patrick Mocheld550d982006-06-27 00:41:40 -040090 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 if (!fan || (count > sizeof(state_string) - 1))
Patrick Mocheld550d982006-06-27 00:41:40 -0400109 return -EINVAL;
Len Brown4be44fc2005-08-05 00:44:28 -0400110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 if (copy_from_user(state_string, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -0400112 return -EFAULT;
Len Brown4be44fc2005-08-05 00:44:28 -0400113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 state_string[count] = '\0';
Len Brown4be44fc2005-08-05 00:44:28 -0400115
116 result = acpi_bus_set_power(fan->handle,
117 simple_strtoul(state_string, NULL, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400119 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Patrick Mocheld550d982006-06-27 00:41:40 -0400121 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
124static struct file_operations acpi_fan_state_ops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400125 .open = acpi_fan_state_open_fs,
126 .read = seq_read,
127 .write = acpi_fan_write_state,
128 .llseek = seq_lseek,
129 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 .owner = THIS_MODULE,
131};
132
Len Brown4be44fc2005-08-05 00:44:28 -0400133static int acpi_fan_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
Len Brown4be44fc2005-08-05 00:44:28 -0400135 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400139 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 if (!acpi_device_dir(device)) {
142 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown4be44fc2005-08-05 00:44:28 -0400143 acpi_fan_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400145 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 acpi_device_dir(device)->owner = THIS_MODULE;
147 }
148
149 /* 'status' [R/W] */
150 entry = create_proc_entry(ACPI_FAN_FILE_STATE,
Len Brown4be44fc2005-08-05 00:44:28 -0400151 S_IFREG | S_IRUGO | S_IWUSR,
152 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400154 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 else {
156 entry->proc_fops = &acpi_fan_state_ops;
157 entry->data = acpi_driver_data(device);
158 entry->owner = THIS_MODULE;
159 }
160
Patrick Mocheld550d982006-06-27 00:41:40 -0400161 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
163
Len Brown4be44fc2005-08-05 00:44:28 -0400164static int acpi_fan_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 if (acpi_device_dir(device)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400168 remove_proc_entry(ACPI_FAN_FILE_STATE, acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 remove_proc_entry(acpi_device_bid(device), acpi_fan_dir);
170 acpi_device_dir(device) = NULL;
171 }
172
Patrick Mocheld550d982006-06-27 00:41:40 -0400173 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174}
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176/* --------------------------------------------------------------------------
177 Driver Interface
178 -------------------------------------------------------------------------- */
179
Len Brown4be44fc2005-08-05 00:44:28 -0400180static int acpi_fan_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
Len Brown4be44fc2005-08-05 00:44:28 -0400182 int result = 0;
183 struct acpi_fan *fan = NULL;
184 int state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400188 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190 fan = kmalloc(sizeof(struct acpi_fan), GFP_KERNEL);
191 if (!fan)
Patrick Mocheld550d982006-06-27 00:41:40 -0400192 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 memset(fan, 0, sizeof(struct acpi_fan));
194
195 fan->handle = device->handle;
Patrick Mochel74b142e2006-05-19 16:54:39 -0400196 fan->device = device;
Pavel Machekc65ade42005-08-05 00:37:45 -0400197 strcpy(acpi_device_name(device), "Fan");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 strcpy(acpi_device_class(device), ACPI_FAN_CLASS);
199 acpi_driver_data(device) = fan;
200
201 result = acpi_bus_get_power(fan->handle, &state);
202 if (result) {
Len Brown64684632006-06-26 23:41:38 -0400203 printk(KERN_ERR PREFIX "Reading power state\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 goto end;
205 }
206
Konstantin Karasyov0feabb02006-05-08 00:00:00 -0400207 device->flags.force_power_state = 1;
208 acpi_bus_set_power(device->handle, state);
209 device->flags.force_power_state = 0;
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 result = acpi_fan_add_fs(device);
212 if (result)
213 goto end;
214
215 printk(KERN_INFO PREFIX "%s [%s] (%s)\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400216 acpi_device_name(device), acpi_device_bid(device),
217 !device->power.state ? "on" : "off");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Len Brown4be44fc2005-08-05 00:44:28 -0400219 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 if (result)
221 kfree(fan);
222
Patrick Mocheld550d982006-06-27 00:41:40 -0400223 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224}
225
Len Brown4be44fc2005-08-05 00:44:28 -0400226static int acpi_fan_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
Pavel Machekc65ade42005-08-05 00:37:45 -0400228 struct acpi_fan *fan = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400232 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Len Brown4be44fc2005-08-05 00:44:28 -0400234 fan = (struct acpi_fan *)acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 acpi_fan_remove_fs(device);
237
238 kfree(fan);
239
Patrick Mocheld550d982006-06-27 00:41:40 -0400240 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
242
Konstantin Karasyov0feabb02006-05-08 00:00:00 -0400243static int acpi_fan_suspend(struct acpi_device *device, int state)
244{
245 if (!device)
246 return -EINVAL;
247
248 acpi_bus_set_power(device->handle, ACPI_STATE_D0);
249
250 return AE_OK;
251}
252
253static int acpi_fan_resume(struct acpi_device *device, int state)
254{
255 int result = 0;
256 int power_state = 0;
257
258 if (!device)
259 return -EINVAL;
260
261 result = acpi_bus_get_power(device->handle, &power_state);
262 if (result) {
263 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
264 "Error reading fan power state\n"));
265 return result;
266 }
267
268 device->flags.force_power_state = 1;
269 acpi_bus_set_power(device->handle, power_state);
270 device->flags.force_power_state = 0;
271
272 return result;
273}
274
Len Brown4be44fc2005-08-05 00:44:28 -0400275static int __init acpi_fan_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
Pavel Machekc65ade42005-08-05 00:37:45 -0400277 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 acpi_fan_dir = proc_mkdir(ACPI_FAN_CLASS, acpi_root_dir);
281 if (!acpi_fan_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -0400282 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 acpi_fan_dir->owner = THIS_MODULE;
284
285 result = acpi_bus_register_driver(&acpi_fan_driver);
286 if (result < 0) {
287 remove_proc_entry(ACPI_FAN_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -0400288 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 }
290
Patrick Mocheld550d982006-06-27 00:41:40 -0400291 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}
293
Len Brown4be44fc2005-08-05 00:44:28 -0400294static void __exit acpi_fan_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297 acpi_bus_unregister_driver(&acpi_fan_driver);
298
299 remove_proc_entry(ACPI_FAN_CLASS, acpi_root_dir);
300
Patrick Mocheld550d982006-06-27 00:41:40 -0400301 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302}
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304module_init(acpi_fan_init);
305module_exit(acpi_fan_exit);