2 * acpi_fan.c - ACPI Fan Driver ($Revision: 29 $)
4 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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.
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.
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.
23 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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>
34 #include <acpi/acpi_bus.h>
35 #include <acpi/acpi_drivers.h>
37 #define ACPI_FAN_COMPONENT 0x00200000
38 #define ACPI_FAN_CLASS "fan"
39 #define ACPI_FAN_DRIVER_NAME "ACPI Fan Driver"
40 #define ACPI_FAN_FILE_STATE "state"
42 #define _COMPONENT ACPI_FAN_COMPONENT
43 ACPI_MODULE_NAME("acpi_fan")
45 MODULE_AUTHOR("Paul Diefenbaugh");
46 MODULE_DESCRIPTION(ACPI_FAN_DRIVER_NAME);
47 MODULE_LICENSE("GPL");
49 static int acpi_fan_add(struct acpi_device *device);
50 static int acpi_fan_remove(struct acpi_device *device, int type);
51 static int acpi_fan_suspend(struct acpi_device *device, int state);
52 static int acpi_fan_resume(struct acpi_device *device, int state);
54 static struct acpi_driver acpi_fan_driver = {
55 .name = ACPI_FAN_DRIVER_NAME,
56 .class = ACPI_FAN_CLASS,
60 .remove = acpi_fan_remove,
61 .suspend = acpi_fan_suspend,
62 .resume = acpi_fan_resume,
70 /* --------------------------------------------------------------------------
72 -------------------------------------------------------------------------- */
74 static struct proc_dir_entry *acpi_fan_dir;
76 static int acpi_fan_read_state(struct seq_file *seq, void *offset)
78 struct acpi_fan *fan = seq->private;
81 ACPI_FUNCTION_TRACE("acpi_fan_read_state");
84 if (acpi_bus_get_power(fan->handle, &state))
85 seq_printf(seq, "status: ERROR\n");
87 seq_printf(seq, "status: %s\n",
88 !state ? "on" : "off");
93 static int acpi_fan_state_open_fs(struct inode *inode, struct file *file)
95 return single_open(file, acpi_fan_read_state, PDE(inode)->data);
99 acpi_fan_write_state(struct file *file, const char __user * buffer,
100 size_t count, loff_t * ppos)
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' };
107 ACPI_FUNCTION_TRACE("acpi_fan_write_state");
109 if (!fan || (count > sizeof(state_string) - 1))
110 return_VALUE(-EINVAL);
112 if (copy_from_user(state_string, buffer, count))
113 return_VALUE(-EFAULT);
115 state_string[count] = '\0';
117 result = acpi_bus_set_power(fan->handle,
118 simple_strtoul(state_string, NULL, 0));
120 return_VALUE(result);
125 static struct file_operations acpi_fan_state_ops = {
126 .open = acpi_fan_state_open_fs,
128 .write = acpi_fan_write_state,
130 .release = single_release,
131 .owner = THIS_MODULE,
134 static int acpi_fan_add_fs(struct acpi_device *device)
136 struct proc_dir_entry *entry = NULL;
138 ACPI_FUNCTION_TRACE("acpi_fan_add_fs");
141 return_VALUE(-EINVAL);
143 if (!acpi_device_dir(device)) {
144 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
146 if (!acpi_device_dir(device))
147 return_VALUE(-ENODEV);
148 acpi_device_dir(device)->owner = THIS_MODULE;
152 entry = create_proc_entry(ACPI_FAN_FILE_STATE,
153 S_IFREG | S_IRUGO | S_IWUSR,
154 acpi_device_dir(device));
156 return_VALUE(-ENODEV);
158 entry->proc_fops = &acpi_fan_state_ops;
159 entry->data = acpi_driver_data(device);
160 entry->owner = THIS_MODULE;
166 static int acpi_fan_remove_fs(struct acpi_device *device)
168 ACPI_FUNCTION_TRACE("acpi_fan_remove_fs");
170 if (acpi_device_dir(device)) {
171 remove_proc_entry(ACPI_FAN_FILE_STATE, acpi_device_dir(device));
172 remove_proc_entry(acpi_device_bid(device), acpi_fan_dir);
173 acpi_device_dir(device) = NULL;
179 /* --------------------------------------------------------------------------
181 -------------------------------------------------------------------------- */
183 static int acpi_fan_add(struct acpi_device *device)
186 struct acpi_fan *fan = NULL;
189 ACPI_FUNCTION_TRACE("acpi_fan_add");
192 return_VALUE(-EINVAL);
194 fan = kmalloc(sizeof(struct acpi_fan), GFP_KERNEL);
196 return_VALUE(-ENOMEM);
197 memset(fan, 0, sizeof(struct acpi_fan));
199 fan->handle = device->handle;
200 strcpy(acpi_device_name(device), "Fan");
201 strcpy(acpi_device_class(device), ACPI_FAN_CLASS);
202 acpi_driver_data(device) = fan;
204 result = acpi_bus_get_power(fan->handle, &state);
206 printk(KERN_ERR PREFIX "Reading power state\n");
210 device->flags.force_power_state = 1;
211 acpi_bus_set_power(device->handle, state);
212 device->flags.force_power_state = 0;
214 result = acpi_fan_add_fs(device);
218 printk(KERN_INFO PREFIX "%s [%s] (%s)\n",
219 acpi_device_name(device), acpi_device_bid(device),
220 !device->power.state ? "on" : "off");
226 return_VALUE(result);
229 static int acpi_fan_remove(struct acpi_device *device, int type)
231 struct acpi_fan *fan = NULL;
233 ACPI_FUNCTION_TRACE("acpi_fan_remove");
235 if (!device || !acpi_driver_data(device))
236 return_VALUE(-EINVAL);
238 fan = (struct acpi_fan *)acpi_driver_data(device);
240 acpi_fan_remove_fs(device);
247 static int acpi_fan_suspend(struct acpi_device *device, int state)
252 acpi_bus_set_power(device->handle, ACPI_STATE_D0);
257 static int acpi_fan_resume(struct acpi_device *device, int state)
265 result = acpi_bus_get_power(device->handle, &power_state);
267 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
268 "Error reading fan power state\n"));
272 device->flags.force_power_state = 1;
273 acpi_bus_set_power(device->handle, power_state);
274 device->flags.force_power_state = 0;
279 static int __init acpi_fan_init(void)
283 ACPI_FUNCTION_TRACE("acpi_fan_init");
285 acpi_fan_dir = proc_mkdir(ACPI_FAN_CLASS, acpi_root_dir);
287 return_VALUE(-ENODEV);
288 acpi_fan_dir->owner = THIS_MODULE;
290 result = acpi_bus_register_driver(&acpi_fan_driver);
292 remove_proc_entry(ACPI_FAN_CLASS, acpi_root_dir);
293 return_VALUE(-ENODEV);
299 static void __exit acpi_fan_exit(void)
301 ACPI_FUNCTION_TRACE("acpi_fan_exit");
303 acpi_bus_unregister_driver(&acpi_fan_driver);
305 remove_proc_entry(ACPI_FAN_CLASS, acpi_root_dir);
310 module_init(acpi_fan_init);
311 module_exit(acpi_fan_exit);