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