patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / acpi / fan.c
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 <asm/uaccess.h>
32
33 #include <acpi/acpi_bus.h>
34 #include <acpi/acpi_drivers.h>
35
36
37 #define ACPI_FAN_COMPONENT              0x00200000
38 #define ACPI_FAN_CLASS                  "fan"
39 #define ACPI_FAN_HID                    "PNP0C0B"
40 #define ACPI_FAN_DRIVER_NAME            "ACPI Fan Driver"
41 #define ACPI_FAN_DEVICE_NAME            "Fan"
42 #define ACPI_FAN_FILE_STATE             "state"
43 #define ACPI_FAN_NOTIFY_STATUS          0x80
44
45 #define _COMPONENT              ACPI_FAN_COMPONENT
46 ACPI_MODULE_NAME                ("acpi_fan")
47
48 MODULE_AUTHOR("Paul Diefenbaugh");
49 MODULE_DESCRIPTION(ACPI_FAN_DRIVER_NAME);
50 MODULE_LICENSE("GPL");
51
52 int acpi_fan_add (struct acpi_device *device);
53 int acpi_fan_remove (struct acpi_device *device, int type);
54
55 static struct acpi_driver acpi_fan_driver = {
56         .name =         ACPI_FAN_DRIVER_NAME,
57         .class =        ACPI_FAN_CLASS,
58         .ids =          ACPI_FAN_HID,
59         .ops =          {
60                                 .add =          acpi_fan_add,
61                                 .remove =       acpi_fan_remove,
62                         },
63 };
64
65 struct acpi_fan {
66         acpi_handle             handle;
67 };
68
69
70 /* --------------------------------------------------------------------------
71                               FS Interface (/proc)
72    -------------------------------------------------------------------------- */
73
74 struct proc_dir_entry           *acpi_fan_dir;
75
76
77 static int
78 acpi_fan_read_state (
79         char                    *page,
80         char                    **start,
81         off_t                   off,
82         int                     count,
83         int                     *eof,
84         void                    *data)
85 {
86         struct acpi_fan         *fan = (struct acpi_fan *) data;
87         char                    *p = page;
88         int                     len = 0;
89         int                     state = 0;
90
91         ACPI_FUNCTION_TRACE("acpi_fan_read_state");
92
93         if (!fan || (off != 0))
94                 goto end;
95
96         if (acpi_bus_get_power(fan->handle, &state))
97                 goto end;
98
99         p += sprintf(p, "status:                  %s\n",
100                 !state?"on":"off");
101
102 end:
103         len = (p - page);
104         if (len <= off+count) *eof = 1;
105         *start = page + off;
106         len -= off;
107         if (len>count) len = count;
108         if (len<0) len = 0;
109
110         return_VALUE(len);
111 }
112
113
114 static int
115 acpi_fan_write_state (
116         struct file             *file,
117         const char              __user *buffer,
118         unsigned long           count,
119         void                    *data)
120 {
121         int                     result = 0;
122         struct acpi_fan         *fan = (struct acpi_fan *) data;
123         char                    state_string[12] = {'\0'};
124
125         ACPI_FUNCTION_TRACE("acpi_fan_write_state");
126
127         if (!fan || (count > sizeof(state_string) - 1))
128                 return_VALUE(-EINVAL);
129         
130         if (copy_from_user(state_string, buffer, count))
131                 return_VALUE(-EFAULT);
132         
133         state_string[count] = '\0';
134         
135         result = acpi_bus_set_power(fan->handle, 
136                 simple_strtoul(state_string, NULL, 0));
137         if (result)
138                 return_VALUE(result);
139
140         return_VALUE(count);
141 }
142
143
144 static int
145 acpi_fan_add_fs (
146         struct acpi_device      *device)
147 {
148         struct proc_dir_entry   *entry = NULL;
149
150         ACPI_FUNCTION_TRACE("acpi_fan_add_fs");
151
152         if (!device)
153                 return_VALUE(-EINVAL);
154
155         if (!acpi_device_dir(device)) {
156                 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
157                         acpi_fan_dir);
158                 if (!acpi_device_dir(device))
159                         return_VALUE(-ENODEV);
160                 acpi_device_dir(device)->owner = THIS_MODULE;
161         }
162
163         /* 'status' [R/W] */
164         entry = create_proc_entry(ACPI_FAN_FILE_STATE,
165                 S_IFREG|S_IRUGO|S_IWUSR, acpi_device_dir(device));
166         if (!entry)
167                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
168                         "Unable to create '%s' fs entry\n",
169                         ACPI_FAN_FILE_STATE));
170         else {
171                 entry->read_proc = acpi_fan_read_state;
172                 entry->write_proc = acpi_fan_write_state;
173                 entry->data = acpi_driver_data(device);
174                 entry->owner = THIS_MODULE;
175         }
176
177         return_VALUE(0);
178 }
179
180
181 static int
182 acpi_fan_remove_fs (
183         struct acpi_device      *device)
184 {
185         ACPI_FUNCTION_TRACE("acpi_fan_remove_fs");
186
187         if (acpi_device_dir(device)) {
188                 remove_proc_entry(ACPI_FAN_FILE_STATE,
189                                   acpi_device_dir(device));
190                 remove_proc_entry(acpi_device_bid(device), acpi_fan_dir);
191                 acpi_device_dir(device) = NULL;
192         }
193
194         return_VALUE(0);
195 }
196
197
198 /* --------------------------------------------------------------------------
199                                  Driver Interface
200    -------------------------------------------------------------------------- */
201
202 int
203 acpi_fan_add (
204         struct acpi_device      *device)
205 {
206         int                     result = 0;
207         struct acpi_fan         *fan = NULL;
208         int                     state = 0;
209
210         ACPI_FUNCTION_TRACE("acpi_fan_add");
211
212         if (!device)
213                 return_VALUE(-EINVAL);
214
215         fan = kmalloc(sizeof(struct acpi_fan), GFP_KERNEL);
216         if (!fan)
217                 return_VALUE(-ENOMEM);
218         memset(fan, 0, sizeof(struct acpi_fan));
219
220         fan->handle = device->handle;
221         strcpy(acpi_device_name(device), ACPI_FAN_DEVICE_NAME);
222         strcpy(acpi_device_class(device), ACPI_FAN_CLASS);
223         acpi_driver_data(device) = fan;
224
225         result = acpi_bus_get_power(fan->handle, &state);
226         if (result) {
227                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
228                         "Error reading power state\n"));
229                 goto end;
230         }
231
232         result = acpi_fan_add_fs(device);
233         if (result)
234                 goto end;
235
236         printk(KERN_INFO PREFIX "%s [%s] (%s)\n",
237                 acpi_device_name(device), acpi_device_bid(device),
238                 !device->power.state?"on":"off");
239
240 end:
241         if (result)
242                 kfree(fan);
243
244         return_VALUE(result);
245 }
246
247
248 int
249 acpi_fan_remove (
250         struct acpi_device      *device,
251         int                     type)
252 {
253         struct acpi_fan         *fan = NULL;
254
255         ACPI_FUNCTION_TRACE("acpi_fan_remove");
256
257         if (!device || !acpi_driver_data(device))
258                 return_VALUE(-EINVAL);
259
260         fan = (struct acpi_fan *) acpi_driver_data(device);
261
262         acpi_fan_remove_fs(device);
263
264         kfree(fan);
265
266         return_VALUE(0);
267 }
268
269
270 int __init
271 acpi_fan_init (void)
272 {
273         int                     result = 0;
274
275         ACPI_FUNCTION_TRACE("acpi_fan_init");
276
277         acpi_fan_dir = proc_mkdir(ACPI_FAN_CLASS, acpi_root_dir);
278         if (!acpi_fan_dir)
279                 return_VALUE(-ENODEV);
280         acpi_fan_dir->owner = THIS_MODULE;
281
282         result = acpi_bus_register_driver(&acpi_fan_driver);
283         if (result < 0) {
284                 remove_proc_entry(ACPI_FAN_CLASS, acpi_root_dir);
285                 return_VALUE(-ENODEV);
286         }
287
288         return_VALUE(0);
289 }
290
291
292 void __exit
293 acpi_fan_exit (void)
294 {
295         ACPI_FUNCTION_TRACE("acpi_fan_exit");
296
297         acpi_bus_unregister_driver(&acpi_fan_driver);
298
299         remove_proc_entry(ACPI_FAN_CLASS, acpi_root_dir);
300
301         return_VOID;
302 }
303
304
305 module_init(acpi_fan_init);
306 module_exit(acpi_fan_exit);
307