Merge with Linux 2.5.48.
[linux-2.6/linux-mips.git] / drivers / acpi / fan.c
blob77a79ee887341cb39385d98459e01ce94a953536
1 /*
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/compatmac.h>
31 #include <linux/proc_fs.h>
32 #include "acpi_bus.h"
33 #include "acpi_drivers.h"
36 #define ACPI_FAN_COMPONENT 0x00200000
37 #define ACPI_FAN_CLASS "fan"
38 #define ACPI_FAN_HID "PNP0C0B"
39 #define ACPI_FAN_DRIVER_NAME "ACPI Fan Driver"
40 #define ACPI_FAN_DEVICE_NAME "Fan"
41 #define ACPI_FAN_FILE_STATE "state"
42 #define ACPI_FAN_NOTIFY_STATUS 0x80
44 #define _COMPONENT ACPI_FAN_COMPONENT
45 ACPI_MODULE_NAME ("acpi_fan")
47 MODULE_AUTHOR("Paul Diefenbaugh");
48 MODULE_DESCRIPTION(ACPI_FAN_DRIVER_NAME);
49 MODULE_LICENSE("GPL");
51 int acpi_fan_add (struct acpi_device *device);
52 int acpi_fan_remove (struct acpi_device *device, int type);
54 static struct acpi_driver acpi_fan_driver = {
55 .name = ACPI_FAN_DRIVER_NAME,
56 .class = ACPI_FAN_CLASS,
57 .ids = ACPI_FAN_HID,
58 .ops = {
59 .add = acpi_fan_add,
60 .remove = acpi_fan_remove,
64 struct acpi_fan {
65 acpi_handle handle;
69 /* --------------------------------------------------------------------------
70 FS Interface (/proc)
71 -------------------------------------------------------------------------- */
73 struct proc_dir_entry *acpi_fan_dir = NULL;
76 static int
77 acpi_fan_read_state (
78 char *page,
79 char **start,
80 off_t off,
81 int count,
82 int *eof,
83 void *data)
85 struct acpi_fan *fan = (struct acpi_fan *) data;
86 char *p = page;
87 int len = 0;
88 int state = 0;
90 ACPI_FUNCTION_TRACE("acpi_fan_read_state");
92 if (!fan || (off != 0))
93 goto end;
95 if (acpi_bus_get_power(fan->handle, &state))
96 goto end;
98 p += sprintf(p, "status: %s\n",
99 !state?"on":"off");
101 end:
102 len = (p - page);
103 if (len <= off+count) *eof = 1;
104 *start = page + off;
105 len -= off;
106 if (len>count) len = count;
107 if (len<0) len = 0;
109 return_VALUE(len);
113 static int
114 acpi_fan_write_state (
115 struct file *file,
116 const char *buffer,
117 unsigned long count,
118 void *data)
120 int result = 0;
121 struct acpi_fan *fan = (struct acpi_fan *) data;
122 char state_string[12] = {'\0'};
124 ACPI_FUNCTION_TRACE("acpi_fan_write_state");
126 if (!fan || (count > sizeof(state_string) - 1))
127 return_VALUE(-EINVAL);
129 if (copy_from_user(state_string, buffer, count))
130 return_VALUE(-EFAULT);
132 state_string[count] = '\0';
134 result = acpi_bus_set_power(fan->handle,
135 simple_strtoul(state_string, NULL, 0));
136 if (result)
137 return_VALUE(result);
139 return_VALUE(count);
143 static int
144 acpi_fan_add_fs (
145 struct acpi_device *device)
147 struct proc_dir_entry *entry = NULL;
149 ACPI_FUNCTION_TRACE("acpi_fan_add_fs");
151 if (!device)
152 return_VALUE(-EINVAL);
154 if (!acpi_device_dir(device)) {
155 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
156 acpi_fan_dir);
157 if (!acpi_device_dir(device))
158 return_VALUE(-ENODEV);
161 /* 'status' [R/W] */
162 entry = create_proc_entry(ACPI_FAN_FILE_STATE,
163 S_IFREG|S_IRUGO|S_IWUSR, acpi_device_dir(device));
164 if (!entry)
165 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
166 "Unable to create '%s' fs entry\n",
167 ACPI_FAN_FILE_STATE));
168 else {
169 entry->read_proc = acpi_fan_read_state;
170 entry->write_proc = acpi_fan_write_state;
171 entry->data = acpi_driver_data(device);
174 return_VALUE(0);
178 static int
179 acpi_fan_remove_fs (
180 struct acpi_device *device)
182 ACPI_FUNCTION_TRACE("acpi_fan_remove_fs");
184 if (acpi_device_dir(device))
185 remove_proc_entry(acpi_device_bid(device), acpi_fan_dir);
187 return_VALUE(0);
191 /* --------------------------------------------------------------------------
192 Driver Interface
193 -------------------------------------------------------------------------- */
196 acpi_fan_add (
197 struct acpi_device *device)
199 int result = 0;
200 struct acpi_fan *fan = NULL;
201 int state = 0;
203 ACPI_FUNCTION_TRACE("acpi_fan_add");
205 if (!device)
206 return_VALUE(-EINVAL);
208 fan = kmalloc(sizeof(struct acpi_fan), GFP_KERNEL);
209 if (!fan)
210 return_VALUE(-ENOMEM);
211 memset(fan, 0, sizeof(struct acpi_fan));
213 fan->handle = device->handle;
214 sprintf(acpi_device_name(device), "%s", ACPI_FAN_DEVICE_NAME);
215 sprintf(acpi_device_class(device), "%s", ACPI_FAN_CLASS);
216 acpi_driver_data(device) = fan;
218 result = acpi_bus_get_power(fan->handle, &state);
219 if (result) {
220 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
221 "Error reading power state\n"));
222 goto end;
225 result = acpi_fan_add_fs(device);
226 if (result)
227 goto end;
229 printk(KERN_INFO PREFIX "%s [%s] (%s)\n",
230 acpi_device_name(device), acpi_device_bid(device),
231 !device->power.state?"on":"off");
233 end:
234 if (result)
235 kfree(fan);
237 return_VALUE(result);
242 acpi_fan_remove (
243 struct acpi_device *device,
244 int type)
246 struct acpi_fan *fan = NULL;
248 ACPI_FUNCTION_TRACE("acpi_fan_remove");
250 if (!device || !acpi_driver_data(device))
251 return_VALUE(-EINVAL);
253 fan = (struct acpi_fan *) acpi_driver_data(device);
255 acpi_fan_remove_fs(device);
257 kfree(fan);
259 return_VALUE(0);
263 int __init
264 acpi_fan_init (void)
266 int result = 0;
268 ACPI_FUNCTION_TRACE("acpi_fan_init");
270 acpi_fan_dir = proc_mkdir(ACPI_FAN_CLASS, acpi_root_dir);
271 if (!acpi_fan_dir)
272 return_VALUE(-ENODEV);
274 result = acpi_bus_register_driver(&acpi_fan_driver);
275 if (result < 0) {
276 remove_proc_entry(ACPI_FAN_CLASS, acpi_root_dir);
277 return_VALUE(-ENODEV);
280 return_VALUE(0);
284 void __exit
285 acpi_fan_exit (void)
287 ACPI_FUNCTION_TRACE("acpi_fan_exit");
289 acpi_bus_unregister_driver(&acpi_fan_driver);
291 remove_proc_entry(ACPI_FAN_CLASS, acpi_root_dir);
293 return_VOID;
297 module_init(acpi_fan_init);
298 module_exit(acpi_fan_exit);