[SCSI] scsi_transport_spi.c: make print_nego() static
[linux-2.6] / drivers / acpi / ac.c
1 /*
2  *  acpi_ac.c - ACPI AC Adapter Driver ($Revision: 27 $)
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 <acpi/acpi_bus.h>
33 #include <acpi/acpi_drivers.h>
34
35 #define ACPI_AC_COMPONENT               0x00020000
36 #define ACPI_AC_CLASS                   "ac_adapter"
37 #define ACPI_AC_HID                     "ACPI0003"
38 #define ACPI_AC_DRIVER_NAME             "ACPI AC Adapter Driver"
39 #define ACPI_AC_DEVICE_NAME             "AC Adapter"
40 #define ACPI_AC_FILE_STATE              "state"
41 #define ACPI_AC_NOTIFY_STATUS           0x80
42 #define ACPI_AC_STATUS_OFFLINE          0x00
43 #define ACPI_AC_STATUS_ONLINE           0x01
44 #define ACPI_AC_STATUS_UNKNOWN          0xFF
45
46 #define _COMPONENT              ACPI_AC_COMPONENT
47 ACPI_MODULE_NAME("acpi_ac")
48
49     MODULE_AUTHOR("Paul Diefenbaugh");
50 MODULE_DESCRIPTION(ACPI_AC_DRIVER_NAME);
51 MODULE_LICENSE("GPL");
52
53 static int acpi_ac_add(struct acpi_device *device);
54 static int acpi_ac_remove(struct acpi_device *device, int type);
55 static int acpi_ac_open_fs(struct inode *inode, struct file *file);
56
57 static struct acpi_driver acpi_ac_driver = {
58         .name = ACPI_AC_DRIVER_NAME,
59         .class = ACPI_AC_CLASS,
60         .ids = ACPI_AC_HID,
61         .ops = {
62                 .add = acpi_ac_add,
63                 .remove = acpi_ac_remove,
64                 },
65 };
66
67 struct acpi_ac {
68         acpi_handle handle;
69         unsigned long state;
70 };
71
72 static struct file_operations acpi_ac_fops = {
73         .open = acpi_ac_open_fs,
74         .read = seq_read,
75         .llseek = seq_lseek,
76         .release = single_release,
77 };
78
79 /* --------------------------------------------------------------------------
80                                AC Adapter Management
81    -------------------------------------------------------------------------- */
82
83 static int acpi_ac_get_state(struct acpi_ac *ac)
84 {
85         acpi_status status = AE_OK;
86
87         ACPI_FUNCTION_TRACE("acpi_ac_get_state");
88
89         if (!ac)
90                 return_VALUE(-EINVAL);
91
92         status = acpi_evaluate_integer(ac->handle, "_PSR", NULL, &ac->state);
93         if (ACPI_FAILURE(status)) {
94                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
95                                   "Error reading AC Adapter state\n"));
96                 ac->state = ACPI_AC_STATUS_UNKNOWN;
97                 return_VALUE(-ENODEV);
98         }
99
100         return_VALUE(0);
101 }
102
103 /* --------------------------------------------------------------------------
104                               FS Interface (/proc)
105    -------------------------------------------------------------------------- */
106
107 static struct proc_dir_entry *acpi_ac_dir;
108
109 static int acpi_ac_seq_show(struct seq_file *seq, void *offset)
110 {
111         struct acpi_ac *ac = (struct acpi_ac *)seq->private;
112
113         ACPI_FUNCTION_TRACE("acpi_ac_seq_show");
114
115         if (!ac)
116                 return_VALUE(0);
117
118         if (acpi_ac_get_state(ac)) {
119                 seq_puts(seq, "ERROR: Unable to read AC Adapter state\n");
120                 return_VALUE(0);
121         }
122
123         seq_puts(seq, "state:                   ");
124         switch (ac->state) {
125         case ACPI_AC_STATUS_OFFLINE:
126                 seq_puts(seq, "off-line\n");
127                 break;
128         case ACPI_AC_STATUS_ONLINE:
129                 seq_puts(seq, "on-line\n");
130                 break;
131         default:
132                 seq_puts(seq, "unknown\n");
133                 break;
134         }
135
136         return_VALUE(0);
137 }
138
139 static int acpi_ac_open_fs(struct inode *inode, struct file *file)
140 {
141         return single_open(file, acpi_ac_seq_show, PDE(inode)->data);
142 }
143
144 static int acpi_ac_add_fs(struct acpi_device *device)
145 {
146         struct proc_dir_entry *entry = NULL;
147
148         ACPI_FUNCTION_TRACE("acpi_ac_add_fs");
149
150         if (!acpi_device_dir(device)) {
151                 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
152                                                      acpi_ac_dir);
153                 if (!acpi_device_dir(device))
154                         return_VALUE(-ENODEV);
155                 acpi_device_dir(device)->owner = THIS_MODULE;
156         }
157
158         /* 'state' [R] */
159         entry = create_proc_entry(ACPI_AC_FILE_STATE,
160                                   S_IRUGO, acpi_device_dir(device));
161         if (!entry)
162                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
163                                   "Unable to create '%s' fs entry\n",
164                                   ACPI_AC_FILE_STATE));
165         else {
166                 entry->proc_fops = &acpi_ac_fops;
167                 entry->data = acpi_driver_data(device);
168                 entry->owner = THIS_MODULE;
169         }
170
171         return_VALUE(0);
172 }
173
174 static int acpi_ac_remove_fs(struct acpi_device *device)
175 {
176         ACPI_FUNCTION_TRACE("acpi_ac_remove_fs");
177
178         if (acpi_device_dir(device)) {
179                 remove_proc_entry(ACPI_AC_FILE_STATE, acpi_device_dir(device));
180
181                 remove_proc_entry(acpi_device_bid(device), acpi_ac_dir);
182                 acpi_device_dir(device) = NULL;
183         }
184
185         return_VALUE(0);
186 }
187
188 /* --------------------------------------------------------------------------
189                                    Driver Model
190    -------------------------------------------------------------------------- */
191
192 static void acpi_ac_notify(acpi_handle handle, u32 event, void *data)
193 {
194         struct acpi_ac *ac = (struct acpi_ac *)data;
195         struct acpi_device *device = NULL;
196
197         ACPI_FUNCTION_TRACE("acpi_ac_notify");
198
199         if (!ac)
200                 return_VOID;
201
202         if (acpi_bus_get_device(ac->handle, &device))
203                 return_VOID;
204
205         switch (event) {
206         case ACPI_AC_NOTIFY_STATUS:
207                 acpi_ac_get_state(ac);
208                 acpi_bus_generate_event(device, event, (u32) ac->state);
209                 break;
210         default:
211                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
212                                   "Unsupported event [0x%x]\n", event));
213                 break;
214         }
215
216         return_VOID;
217 }
218
219 static int acpi_ac_add(struct acpi_device *device)
220 {
221         int result = 0;
222         acpi_status status = AE_OK;
223         struct acpi_ac *ac = NULL;
224
225         ACPI_FUNCTION_TRACE("acpi_ac_add");
226
227         if (!device)
228                 return_VALUE(-EINVAL);
229
230         ac = kmalloc(sizeof(struct acpi_ac), GFP_KERNEL);
231         if (!ac)
232                 return_VALUE(-ENOMEM);
233         memset(ac, 0, sizeof(struct acpi_ac));
234
235         ac->handle = device->handle;
236         strcpy(acpi_device_name(device), ACPI_AC_DEVICE_NAME);
237         strcpy(acpi_device_class(device), ACPI_AC_CLASS);
238         acpi_driver_data(device) = ac;
239
240         result = acpi_ac_get_state(ac);
241         if (result)
242                 goto end;
243
244         result = acpi_ac_add_fs(device);
245         if (result)
246                 goto end;
247
248         status = acpi_install_notify_handler(ac->handle,
249                                              ACPI_DEVICE_NOTIFY, acpi_ac_notify,
250                                              ac);
251         if (ACPI_FAILURE(status)) {
252                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
253                                   "Error installing notify handler\n"));
254                 result = -ENODEV;
255                 goto end;
256         }
257
258         printk(KERN_INFO PREFIX "%s [%s] (%s)\n",
259                acpi_device_name(device), acpi_device_bid(device),
260                ac->state ? "on-line" : "off-line");
261
262       end:
263         if (result) {
264                 acpi_ac_remove_fs(device);
265                 kfree(ac);
266         }
267
268         return_VALUE(result);
269 }
270
271 static int acpi_ac_remove(struct acpi_device *device, int type)
272 {
273         acpi_status status = AE_OK;
274         struct acpi_ac *ac = NULL;
275
276         ACPI_FUNCTION_TRACE("acpi_ac_remove");
277
278         if (!device || !acpi_driver_data(device))
279                 return_VALUE(-EINVAL);
280
281         ac = (struct acpi_ac *)acpi_driver_data(device);
282
283         status = acpi_remove_notify_handler(ac->handle,
284                                             ACPI_DEVICE_NOTIFY, acpi_ac_notify);
285         if (ACPI_FAILURE(status))
286                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
287                                   "Error removing notify handler\n"));
288
289         acpi_ac_remove_fs(device);
290
291         kfree(ac);
292
293         return_VALUE(0);
294 }
295
296 static int __init acpi_ac_init(void)
297 {
298         int result = 0;
299
300         ACPI_FUNCTION_TRACE("acpi_ac_init");
301
302         acpi_ac_dir = proc_mkdir(ACPI_AC_CLASS, acpi_root_dir);
303         if (!acpi_ac_dir)
304                 return_VALUE(-ENODEV);
305         acpi_ac_dir->owner = THIS_MODULE;
306
307         result = acpi_bus_register_driver(&acpi_ac_driver);
308         if (result < 0) {
309                 remove_proc_entry(ACPI_AC_CLASS, acpi_root_dir);
310                 return_VALUE(-ENODEV);
311         }
312
313         return_VALUE(0);
314 }
315
316 static void __exit acpi_ac_exit(void)
317 {
318         ACPI_FUNCTION_TRACE("acpi_ac_exit");
319
320         acpi_bus_unregister_driver(&acpi_ac_driver);
321
322         remove_proc_entry(ACPI_AC_CLASS, acpi_root_dir);
323
324         return_VOID;
325 }
326
327 module_init(acpi_ac_init);
328 module_exit(acpi_ac_exit);