USB: Allow autosuspend delay to equal 0
[linux-2.6.git] / drivers / acpi / ac.c
blob37c7dc4f9fe5db6d15b7bba43803790f0c0ee588
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_DEVICE_NAME "AC Adapter"
39 #define ACPI_AC_FILE_STATE "state"
40 #define ACPI_AC_NOTIFY_STATUS 0x80
41 #define ACPI_AC_STATUS_OFFLINE 0x00
42 #define ACPI_AC_STATUS_ONLINE 0x01
43 #define ACPI_AC_STATUS_UNKNOWN 0xFF
45 #define _COMPONENT ACPI_AC_COMPONENT
46 ACPI_MODULE_NAME("ac");
48 MODULE_AUTHOR("Paul Diefenbaugh");
49 MODULE_DESCRIPTION("ACPI AC Adapter Driver");
50 MODULE_LICENSE("GPL");
52 extern struct proc_dir_entry *acpi_lock_ac_dir(void);
53 extern void *acpi_unlock_ac_dir(struct proc_dir_entry *acpi_ac_dir);
55 static int acpi_ac_add(struct acpi_device *device);
56 static int acpi_ac_remove(struct acpi_device *device, int type);
57 static int acpi_ac_open_fs(struct inode *inode, struct file *file);
59 static struct acpi_driver acpi_ac_driver = {
60 .name = "ac",
61 .class = ACPI_AC_CLASS,
62 .ids = ACPI_AC_HID,
63 .ops = {
64 .add = acpi_ac_add,
65 .remove = acpi_ac_remove,
69 struct acpi_ac {
70 struct acpi_device * device;
71 unsigned long state;
74 static const struct file_operations acpi_ac_fops = {
75 .open = acpi_ac_open_fs,
76 .read = seq_read,
77 .llseek = seq_lseek,
78 .release = single_release,
81 /* --------------------------------------------------------------------------
82 AC Adapter Management
83 -------------------------------------------------------------------------- */
85 static int acpi_ac_get_state(struct acpi_ac *ac)
87 acpi_status status = AE_OK;
90 if (!ac)
91 return -EINVAL;
93 status = acpi_evaluate_integer(ac->device->handle, "_PSR", NULL, &ac->state);
94 if (ACPI_FAILURE(status)) {
95 ACPI_EXCEPTION((AE_INFO, status, "Error reading AC Adapter state"));
96 ac->state = ACPI_AC_STATUS_UNKNOWN;
97 return -ENODEV;
100 return 0;
103 /* --------------------------------------------------------------------------
104 FS Interface (/proc)
105 -------------------------------------------------------------------------- */
107 static struct proc_dir_entry *acpi_ac_dir;
109 static int acpi_ac_seq_show(struct seq_file *seq, void *offset)
111 struct acpi_ac *ac = seq->private;
114 if (!ac)
115 return 0;
117 if (acpi_ac_get_state(ac)) {
118 seq_puts(seq, "ERROR: Unable to read AC Adapter state\n");
119 return 0;
122 seq_puts(seq, "state: ");
123 switch (ac->state) {
124 case ACPI_AC_STATUS_OFFLINE:
125 seq_puts(seq, "off-line\n");
126 break;
127 case ACPI_AC_STATUS_ONLINE:
128 seq_puts(seq, "on-line\n");
129 break;
130 default:
131 seq_puts(seq, "unknown\n");
132 break;
135 return 0;
138 static int acpi_ac_open_fs(struct inode *inode, struct file *file)
140 return single_open(file, acpi_ac_seq_show, PDE(inode)->data);
143 static int acpi_ac_add_fs(struct acpi_device *device)
145 struct proc_dir_entry *entry = NULL;
148 if (!acpi_device_dir(device)) {
149 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
150 acpi_ac_dir);
151 if (!acpi_device_dir(device))
152 return -ENODEV;
153 acpi_device_dir(device)->owner = THIS_MODULE;
156 /* 'state' [R] */
157 entry = create_proc_entry(ACPI_AC_FILE_STATE,
158 S_IRUGO, acpi_device_dir(device));
159 if (!entry)
160 return -ENODEV;
161 else {
162 entry->proc_fops = &acpi_ac_fops;
163 entry->data = acpi_driver_data(device);
164 entry->owner = THIS_MODULE;
167 return 0;
170 static int acpi_ac_remove_fs(struct acpi_device *device)
173 if (acpi_device_dir(device)) {
174 remove_proc_entry(ACPI_AC_FILE_STATE, acpi_device_dir(device));
176 remove_proc_entry(acpi_device_bid(device), acpi_ac_dir);
177 acpi_device_dir(device) = NULL;
180 return 0;
183 /* --------------------------------------------------------------------------
184 Driver Model
185 -------------------------------------------------------------------------- */
187 static void acpi_ac_notify(acpi_handle handle, u32 event, void *data)
189 struct acpi_ac *ac = data;
190 struct acpi_device *device = NULL;
193 if (!ac)
194 return;
196 device = ac->device;
197 switch (event) {
198 case ACPI_AC_NOTIFY_STATUS:
199 case ACPI_NOTIFY_BUS_CHECK:
200 case ACPI_NOTIFY_DEVICE_CHECK:
201 acpi_ac_get_state(ac);
202 acpi_bus_generate_event(device, event, (u32) ac->state);
203 break;
204 default:
205 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
206 "Unsupported event [0x%x]\n", event));
207 break;
210 return;
213 static int acpi_ac_add(struct acpi_device *device)
215 int result = 0;
216 acpi_status status = AE_OK;
217 struct acpi_ac *ac = NULL;
220 if (!device)
221 return -EINVAL;
223 ac = kzalloc(sizeof(struct acpi_ac), GFP_KERNEL);
224 if (!ac)
225 return -ENOMEM;
227 ac->device = device;
228 strcpy(acpi_device_name(device), ACPI_AC_DEVICE_NAME);
229 strcpy(acpi_device_class(device), ACPI_AC_CLASS);
230 acpi_driver_data(device) = ac;
232 result = acpi_ac_get_state(ac);
233 if (result)
234 goto end;
236 result = acpi_ac_add_fs(device);
237 if (result)
238 goto end;
240 status = acpi_install_notify_handler(device->handle,
241 ACPI_ALL_NOTIFY, acpi_ac_notify,
242 ac);
243 if (ACPI_FAILURE(status)) {
244 result = -ENODEV;
245 goto end;
248 printk(KERN_INFO PREFIX "%s [%s] (%s)\n",
249 acpi_device_name(device), acpi_device_bid(device),
250 ac->state ? "on-line" : "off-line");
252 end:
253 if (result) {
254 acpi_ac_remove_fs(device);
255 kfree(ac);
258 return result;
261 static int acpi_ac_remove(struct acpi_device *device, int type)
263 acpi_status status = AE_OK;
264 struct acpi_ac *ac = NULL;
267 if (!device || !acpi_driver_data(device))
268 return -EINVAL;
270 ac = acpi_driver_data(device);
272 status = acpi_remove_notify_handler(device->handle,
273 ACPI_ALL_NOTIFY, acpi_ac_notify);
275 acpi_ac_remove_fs(device);
277 kfree(ac);
279 return 0;
282 static int __init acpi_ac_init(void)
284 int result;
286 if (acpi_disabled)
287 return -ENODEV;
289 acpi_ac_dir = acpi_lock_ac_dir();
290 if (!acpi_ac_dir)
291 return -ENODEV;
293 result = acpi_bus_register_driver(&acpi_ac_driver);
294 if (result < 0) {
295 acpi_unlock_ac_dir(acpi_ac_dir);
296 return -ENODEV;
299 return 0;
302 static void __exit acpi_ac_exit(void)
305 acpi_bus_unregister_driver(&acpi_ac_driver);
307 acpi_unlock_ac_dir(acpi_ac_dir);
309 return;
312 module_init(acpi_ac_init);
313 module_exit(acpi_ac_exit);