Pull asus_acpi-0.30 into release branch
[deliverable/linux.git] / drivers / acpi / ac.c
CommitLineData
1da177e4
LT
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
1da177e4
LT
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
4be44fcd 47ACPI_MODULE_NAME("acpi_ac")
1da177e4 48
4be44fcd 49 MODULE_AUTHOR("Paul Diefenbaugh");
1da177e4
LT
50MODULE_DESCRIPTION(ACPI_AC_DRIVER_NAME);
51MODULE_LICENSE("GPL");
52
4be44fcd
LB
53static int acpi_ac_add(struct acpi_device *device);
54static int acpi_ac_remove(struct acpi_device *device, int type);
1da177e4
LT
55static int acpi_ac_open_fs(struct inode *inode, struct file *file);
56
57static struct acpi_driver acpi_ac_driver = {
4be44fcd
LB
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 },
1da177e4
LT
65};
66
67struct acpi_ac {
af96179a 68 struct acpi_device * device;
4be44fcd 69 unsigned long state;
1da177e4
LT
70};
71
72static struct file_operations acpi_ac_fops = {
4be44fcd
LB
73 .open = acpi_ac_open_fs,
74 .read = seq_read,
75 .llseek = seq_lseek,
76 .release = single_release,
1da177e4
LT
77};
78
79/* --------------------------------------------------------------------------
80 AC Adapter Management
81 -------------------------------------------------------------------------- */
82
4be44fcd 83static int acpi_ac_get_state(struct acpi_ac *ac)
1da177e4 84{
4be44fcd 85 acpi_status status = AE_OK;
1da177e4 86
1da177e4
LT
87
88 if (!ac)
d550d98d 89 return -EINVAL;
1da177e4 90
a6ba5ebe 91 status = acpi_evaluate_integer(ac->device->handle, "_PSR", NULL, &ac->state);
1da177e4 92 if (ACPI_FAILURE(status)) {
a6fc6720 93 ACPI_EXCEPTION((AE_INFO, status, "Error reading AC Adapter state"));
1da177e4 94 ac->state = ACPI_AC_STATUS_UNKNOWN;
d550d98d 95 return -ENODEV;
1da177e4 96 }
4be44fcd 97
d550d98d 98 return 0;
1da177e4
LT
99}
100
1da177e4
LT
101/* --------------------------------------------------------------------------
102 FS Interface (/proc)
103 -------------------------------------------------------------------------- */
104
4be44fcd 105static struct proc_dir_entry *acpi_ac_dir;
1da177e4
LT
106
107static int acpi_ac_seq_show(struct seq_file *seq, void *offset)
108{
4be44fcd 109 struct acpi_ac *ac = (struct acpi_ac *)seq->private;
1da177e4 110
1da177e4
LT
111
112 if (!ac)
d550d98d 113 return 0;
1da177e4
LT
114
115 if (acpi_ac_get_state(ac)) {
116 seq_puts(seq, "ERROR: Unable to read AC Adapter state\n");
d550d98d 117 return 0;
1da177e4
LT
118 }
119
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;
131 }
132
d550d98d 133 return 0;
1da177e4 134}
4be44fcd 135
1da177e4
LT
136static int acpi_ac_open_fs(struct inode *inode, struct file *file)
137{
138 return single_open(file, acpi_ac_seq_show, PDE(inode)->data);
139}
140
4be44fcd 141static int acpi_ac_add_fs(struct acpi_device *device)
1da177e4 142{
4be44fcd 143 struct proc_dir_entry *entry = NULL;
1da177e4 144
1da177e4
LT
145
146 if (!acpi_device_dir(device)) {
147 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
4be44fcd 148 acpi_ac_dir);
1da177e4 149 if (!acpi_device_dir(device))
d550d98d 150 return -ENODEV;
1da177e4
LT
151 acpi_device_dir(device)->owner = THIS_MODULE;
152 }
153
154 /* 'state' [R] */
155 entry = create_proc_entry(ACPI_AC_FILE_STATE,
4be44fcd 156 S_IRUGO, acpi_device_dir(device));
1da177e4 157 if (!entry)
d550d98d 158 return -ENODEV;
1da177e4
LT
159 else {
160 entry->proc_fops = &acpi_ac_fops;
161 entry->data = acpi_driver_data(device);
162 entry->owner = THIS_MODULE;
163 }
164
d550d98d 165 return 0;
1da177e4
LT
166}
167
4be44fcd 168static int acpi_ac_remove_fs(struct acpi_device *device)
1da177e4 169{
1da177e4
LT
170
171 if (acpi_device_dir(device)) {
4be44fcd 172 remove_proc_entry(ACPI_AC_FILE_STATE, acpi_device_dir(device));
1da177e4
LT
173
174 remove_proc_entry(acpi_device_bid(device), acpi_ac_dir);
175 acpi_device_dir(device) = NULL;
176 }
177
d550d98d 178 return 0;
1da177e4
LT
179}
180
1da177e4
LT
181/* --------------------------------------------------------------------------
182 Driver Model
183 -------------------------------------------------------------------------- */
184
4be44fcd 185static void acpi_ac_notify(acpi_handle handle, u32 event, void *data)
1da177e4 186{
4be44fcd
LB
187 struct acpi_ac *ac = (struct acpi_ac *)data;
188 struct acpi_device *device = NULL;
1da177e4 189
1da177e4
LT
190
191 if (!ac)
d550d98d 192 return;
1da177e4 193
af96179a 194 device = ac->device;
1da177e4
LT
195 switch (event) {
196 case ACPI_AC_NOTIFY_STATUS:
197 acpi_ac_get_state(ac);
198 acpi_bus_generate_event(device, event, (u32) ac->state);
199 break;
200 default:
201 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
4be44fcd 202 "Unsupported event [0x%x]\n", event));
1da177e4
LT
203 break;
204 }
205
d550d98d 206 return;
1da177e4
LT
207}
208
4be44fcd 209static int acpi_ac_add(struct acpi_device *device)
1da177e4 210{
4be44fcd
LB
211 int result = 0;
212 acpi_status status = AE_OK;
213 struct acpi_ac *ac = NULL;
1da177e4 214
1da177e4
LT
215
216 if (!device)
d550d98d 217 return -EINVAL;
1da177e4
LT
218
219 ac = kmalloc(sizeof(struct acpi_ac), GFP_KERNEL);
220 if (!ac)
d550d98d 221 return -ENOMEM;
1da177e4
LT
222 memset(ac, 0, sizeof(struct acpi_ac));
223
af96179a 224 ac->device = device;
1da177e4
LT
225 strcpy(acpi_device_name(device), ACPI_AC_DEVICE_NAME);
226 strcpy(acpi_device_class(device), ACPI_AC_CLASS);
227 acpi_driver_data(device) = ac;
228
229 result = acpi_ac_get_state(ac);
230 if (result)
231 goto end;
232
233 result = acpi_ac_add_fs(device);
234 if (result)
235 goto end;
236
a6ba5ebe 237 status = acpi_install_notify_handler(device->handle,
4be44fcd
LB
238 ACPI_DEVICE_NOTIFY, acpi_ac_notify,
239 ac);
1da177e4 240 if (ACPI_FAILURE(status)) {
1da177e4
LT
241 result = -ENODEV;
242 goto end;
243 }
244
4be44fcd
LB
245 printk(KERN_INFO PREFIX "%s [%s] (%s)\n",
246 acpi_device_name(device), acpi_device_bid(device),
247 ac->state ? "on-line" : "off-line");
1da177e4 248
4be44fcd 249 end:
1da177e4
LT
250 if (result) {
251 acpi_ac_remove_fs(device);
252 kfree(ac);
253 }
254
d550d98d 255 return result;
1da177e4
LT
256}
257
4be44fcd 258static int acpi_ac_remove(struct acpi_device *device, int type)
1da177e4 259{
4be44fcd
LB
260 acpi_status status = AE_OK;
261 struct acpi_ac *ac = NULL;
1da177e4 262
1da177e4
LT
263
264 if (!device || !acpi_driver_data(device))
d550d98d 265 return -EINVAL;
1da177e4 266
4be44fcd 267 ac = (struct acpi_ac *)acpi_driver_data(device);
1da177e4 268
a6ba5ebe 269 status = acpi_remove_notify_handler(device->handle,
4be44fcd 270 ACPI_DEVICE_NOTIFY, acpi_ac_notify);
1da177e4
LT
271
272 acpi_ac_remove_fs(device);
273
274 kfree(ac);
275
d550d98d 276 return 0;
1da177e4
LT
277}
278
4be44fcd 279static int __init acpi_ac_init(void)
1da177e4 280{
4be44fcd 281 int result = 0;
1da177e4 282
1da177e4
LT
283
284 acpi_ac_dir = proc_mkdir(ACPI_AC_CLASS, acpi_root_dir);
285 if (!acpi_ac_dir)
d550d98d 286 return -ENODEV;
1da177e4
LT
287 acpi_ac_dir->owner = THIS_MODULE;
288
289 result = acpi_bus_register_driver(&acpi_ac_driver);
290 if (result < 0) {
291 remove_proc_entry(ACPI_AC_CLASS, acpi_root_dir);
d550d98d 292 return -ENODEV;
1da177e4
LT
293 }
294
d550d98d 295 return 0;
1da177e4
LT
296}
297
4be44fcd 298static void __exit acpi_ac_exit(void)
1da177e4 299{
1da177e4
LT
300
301 acpi_bus_unregister_driver(&acpi_ac_driver);
302
303 remove_proc_entry(ACPI_AC_CLASS, acpi_root_dir);
304
d550d98d 305 return;
1da177e4
LT
306}
307
1da177e4
LT
308module_init(acpi_ac_init);
309module_exit(acpi_ac_exit);
This page took 0.132714 seconds and 5 git commands to generate.