ACPI: handle AC notify event on broken BIOS
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / acpi / ac.c
blob69a98da13913d0505b32791eb83c6a393b345ecf
1 /*
2 * acpi_ac.c - ACPI AC Adapter Driver ($Revision: 27 $)
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/proc_fs.h>
31 #include <linux/seq_file.h>
32 #include <acpi/acpi_bus.h>
33 #include <acpi/acpi_drivers.h>
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
46 #define _COMPONENT ACPI_AC_COMPONENT
47 ACPI_MODULE_NAME("acpi_ac")
49 MODULE_AUTHOR("Paul Diefenbaugh");
50 MODULE_DESCRIPTION(ACPI_AC_DRIVER_NAME);
51 MODULE_LICENSE("GPL");
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);
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,
67 struct acpi_ac {
68 struct acpi_device * device;
69 unsigned long state;
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,
79 /* --------------------------------------------------------------------------
80 AC Adapter Management
81 -------------------------------------------------------------------------- */
83 static int acpi_ac_get_state(struct acpi_ac *ac)
85 acpi_status status = AE_OK;
88 if (!ac)
89 return -EINVAL;
91 status = acpi_evaluate_integer(ac->device->handle, "_PSR", NULL, &ac->state);
92 if (ACPI_FAILURE(status)) {
93 ACPI_EXCEPTION((AE_INFO, status, "Error reading AC Adapter state"));
94 ac->state = ACPI_AC_STATUS_UNKNOWN;
95 return -ENODEV;
98 return 0;
101 /* --------------------------------------------------------------------------
102 FS Interface (/proc)
103 -------------------------------------------------------------------------- */
105 static struct proc_dir_entry *acpi_ac_dir;
107 static int acpi_ac_seq_show(struct seq_file *seq, void *offset)
109 struct acpi_ac *ac = (struct acpi_ac *)seq->private;
112 if (!ac)
113 return 0;
115 if (acpi_ac_get_state(ac)) {
116 seq_puts(seq, "ERROR: Unable to read AC Adapter state\n");
117 return 0;
120 seq_puts(seq, "state: ");
121 switch (ac->state) {
122 case ACPI_AC_STATUS_OFFLINE:
123 seq_puts(seq, "off-line\n");
124 break;
125 case ACPI_AC_STATUS_ONLINE:
126 seq_puts(seq, "on-line\n");
127 break;
128 default:
129 seq_puts(seq, "unknown\n");
130 break;
133 return 0;
136 static int acpi_ac_open_fs(struct inode *inode, struct file *file)
138 return single_open(file, acpi_ac_seq_show, PDE(inode)->data);
141 static int acpi_ac_add_fs(struct acpi_device *device)
143 struct proc_dir_entry *entry = NULL;
146 if (!acpi_device_dir(device)) {
147 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
148 acpi_ac_dir);
149 if (!acpi_device_dir(device))
150 return -ENODEV;
151 acpi_device_dir(device)->owner = THIS_MODULE;
154 /* 'state' [R] */
155 entry = create_proc_entry(ACPI_AC_FILE_STATE,
156 S_IRUGO, acpi_device_dir(device));
157 if (!entry)
158 return -ENODEV;
159 else {
160 entry->proc_fops = &acpi_ac_fops;
161 entry->data = acpi_driver_data(device);
162 entry->owner = THIS_MODULE;
165 return 0;
168 static int acpi_ac_remove_fs(struct acpi_device *device)
171 if (acpi_device_dir(device)) {
172 remove_proc_entry(ACPI_AC_FILE_STATE, acpi_device_dir(device));
174 remove_proc_entry(acpi_device_bid(device), acpi_ac_dir);
175 acpi_device_dir(device) = NULL;
178 return 0;
181 /* --------------------------------------------------------------------------
182 Driver Model
183 -------------------------------------------------------------------------- */
185 static void acpi_ac_notify(acpi_handle handle, u32 event, void *data)
187 struct acpi_ac *ac = (struct acpi_ac *)data;
188 struct acpi_device *device = NULL;
191 if (!ac)
192 return;
194 device = ac->device;
195 switch (event) {
196 case ACPI_AC_NOTIFY_STATUS:
197 case ACPI_NOTIFY_BUS_CHECK:
198 case ACPI_NOTIFY_DEVICE_CHECK:
199 acpi_ac_get_state(ac);
200 acpi_bus_generate_event(device, event, (u32) ac->state);
201 break;
202 default:
203 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
204 "Unsupported event [0x%x]\n", event));
205 break;
208 return;
211 static int acpi_ac_add(struct acpi_device *device)
213 int result = 0;
214 acpi_status status = AE_OK;
215 struct acpi_ac *ac = NULL;
218 if (!device)
219 return -EINVAL;
221 ac = kmalloc(sizeof(struct acpi_ac), GFP_KERNEL);
222 if (!ac)
223 return -ENOMEM;
224 memset(ac, 0, sizeof(struct acpi_ac));
226 ac->device = device;
227 strcpy(acpi_device_name(device), ACPI_AC_DEVICE_NAME);
228 strcpy(acpi_device_class(device), ACPI_AC_CLASS);
229 acpi_driver_data(device) = ac;
231 result = acpi_ac_get_state(ac);
232 if (result)
233 goto end;
235 result = acpi_ac_add_fs(device);
236 if (result)
237 goto end;
239 status = acpi_install_notify_handler(device->handle,
240 ACPI_ALL_NOTIFY, acpi_ac_notify,
241 ac);
242 if (ACPI_FAILURE(status)) {
243 result = -ENODEV;
244 goto end;
247 printk(KERN_INFO PREFIX "%s [%s] (%s)\n",
248 acpi_device_name(device), acpi_device_bid(device),
249 ac->state ? "on-line" : "off-line");
251 end:
252 if (result) {
253 acpi_ac_remove_fs(device);
254 kfree(ac);
257 return result;
260 static int acpi_ac_remove(struct acpi_device *device, int type)
262 acpi_status status = AE_OK;
263 struct acpi_ac *ac = NULL;
266 if (!device || !acpi_driver_data(device))
267 return -EINVAL;
269 ac = (struct acpi_ac *)acpi_driver_data(device);
271 status = acpi_remove_notify_handler(device->handle,
272 ACPI_ALL_NOTIFY, acpi_ac_notify);
274 acpi_ac_remove_fs(device);
276 kfree(ac);
278 return 0;
281 static int __init acpi_ac_init(void)
283 int result = 0;
286 acpi_ac_dir = proc_mkdir(ACPI_AC_CLASS, acpi_root_dir);
287 if (!acpi_ac_dir)
288 return -ENODEV;
289 acpi_ac_dir->owner = THIS_MODULE;
291 result = acpi_bus_register_driver(&acpi_ac_driver);
292 if (result < 0) {
293 remove_proc_entry(ACPI_AC_CLASS, acpi_root_dir);
294 return -ENODEV;
297 return 0;
300 static void __exit acpi_ac_exit(void)
303 acpi_bus_unregister_driver(&acpi_ac_driver);
305 remove_proc_entry(ACPI_AC_CLASS, acpi_root_dir);
307 return;
310 module_init(acpi_ac_init);
311 module_exit(acpi_ac_exit);