ACPI: fan: add struct acpi_device to struct acpi_fan.
[safe/jmp/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 #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"
41
42 #define _COMPONENT              ACPI_FAN_COMPONENT
43 ACPI_MODULE_NAME("acpi_fan")
44
45     MODULE_AUTHOR("Paul Diefenbaugh");
46 MODULE_DESCRIPTION(ACPI_FAN_DRIVER_NAME);
47 MODULE_LICENSE("GPL");
48
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);
53
54 static struct acpi_driver acpi_fan_driver = {
55         .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,
61                 .suspend = acpi_fan_suspend,
62                 .resume = acpi_fan_resume,
63                 },
64 };
65
66 struct acpi_fan {
67         acpi_handle handle;
68         struct acpi_device * device;
69 };
70
71 /* --------------------------------------------------------------------------
72                               FS Interface (/proc)
73    -------------------------------------------------------------------------- */
74
75 static struct proc_dir_entry *acpi_fan_dir;
76
77 static int acpi_fan_read_state(struct seq_file *seq, void *offset)
78 {
79         struct acpi_fan *fan = seq->private;
80         int state = 0;
81
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 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
108         if (!fan || (count > sizeof(state_string) - 1))
109                 return -EINVAL;
110
111         if (copy_from_user(state_string, buffer, count))
112                 return -EFAULT;
113
114         state_string[count] = '\0';
115
116         result = acpi_bus_set_power(fan->handle,
117                                     simple_strtoul(state_string, NULL, 0));
118         if (result)
119                 return result;
120
121         return count;
122 }
123
124 static struct file_operations acpi_fan_state_ops = {
125         .open = acpi_fan_state_open_fs,
126         .read = seq_read,
127         .write = acpi_fan_write_state,
128         .llseek = seq_lseek,
129         .release = single_release,
130         .owner = THIS_MODULE,
131 };
132
133 static int acpi_fan_add_fs(struct acpi_device *device)
134 {
135         struct proc_dir_entry *entry = NULL;
136
137
138         if (!device)
139                 return -EINVAL;
140
141         if (!acpi_device_dir(device)) {
142                 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
143                                                      acpi_fan_dir);
144                 if (!acpi_device_dir(device))
145                         return -ENODEV;
146                 acpi_device_dir(device)->owner = THIS_MODULE;
147         }
148
149         /* 'status' [R/W] */
150         entry = create_proc_entry(ACPI_FAN_FILE_STATE,
151                                   S_IFREG | S_IRUGO | S_IWUSR,
152                                   acpi_device_dir(device));
153         if (!entry)
154                 return -ENODEV;
155         else {
156                 entry->proc_fops = &acpi_fan_state_ops;
157                 entry->data = acpi_driver_data(device);
158                 entry->owner = THIS_MODULE;
159         }
160
161         return 0;
162 }
163
164 static int acpi_fan_remove_fs(struct acpi_device *device)
165 {
166
167         if (acpi_device_dir(device)) {
168                 remove_proc_entry(ACPI_FAN_FILE_STATE, acpi_device_dir(device));
169                 remove_proc_entry(acpi_device_bid(device), acpi_fan_dir);
170                 acpi_device_dir(device) = NULL;
171         }
172
173         return 0;
174 }
175
176 /* --------------------------------------------------------------------------
177                                  Driver Interface
178    -------------------------------------------------------------------------- */
179
180 static int acpi_fan_add(struct acpi_device *device)
181 {
182         int result = 0;
183         struct acpi_fan *fan = NULL;
184         int state = 0;
185
186
187         if (!device)
188                 return -EINVAL;
189
190         fan = kmalloc(sizeof(struct acpi_fan), GFP_KERNEL);
191         if (!fan)
192                 return -ENOMEM;
193         memset(fan, 0, sizeof(struct acpi_fan));
194
195         fan->handle = device->handle;
196         fan->device = device;
197         strcpy(acpi_device_name(device), "Fan");
198         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) {
203                 printk(KERN_ERR PREFIX "Reading power state\n");
204                 goto end;
205         }
206
207         device->flags.force_power_state = 1;
208         acpi_bus_set_power(device->handle, state);
209         device->flags.force_power_state = 0;
210
211         result = acpi_fan_add_fs(device);
212         if (result)
213                 goto end;
214
215         printk(KERN_INFO PREFIX "%s [%s] (%s)\n",
216                acpi_device_name(device), acpi_device_bid(device),
217                !device->power.state ? "on" : "off");
218
219       end:
220         if (result)
221                 kfree(fan);
222
223         return result;
224 }
225
226 static int acpi_fan_remove(struct acpi_device *device, int type)
227 {
228         struct acpi_fan *fan = NULL;
229
230
231         if (!device || !acpi_driver_data(device))
232                 return -EINVAL;
233
234         fan = (struct acpi_fan *)acpi_driver_data(device);
235
236         acpi_fan_remove_fs(device);
237
238         kfree(fan);
239
240         return 0;
241 }
242
243 static 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
253 static 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
275 static int __init acpi_fan_init(void)
276 {
277         int result = 0;
278
279
280         acpi_fan_dir = proc_mkdir(ACPI_FAN_CLASS, acpi_root_dir);
281         if (!acpi_fan_dir)
282                 return -ENODEV;
283         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);
288                 return -ENODEV;
289         }
290
291         return 0;
292 }
293
294 static void __exit acpi_fan_exit(void)
295 {
296
297         acpi_bus_unregister_driver(&acpi_fan_driver);
298
299         remove_proc_entry(ACPI_FAN_CLASS, acpi_root_dir);
300
301         return;
302 }
303
304 module_init(acpi_fan_init);
305 module_exit(acpi_fan_exit);