blob: 36ca365bcead4bca3baae6610ef83eb5dcd1aeec [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#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
Len Brown4be44fc2005-08-05 00:44:28 -040047ACPI_MODULE_NAME("acpi_ac")
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Len Brown4be44fc2005-08-05 00:44:28 -040049 MODULE_AUTHOR("Paul Diefenbaugh");
Linus Torvalds1da177e2005-04-16 15:20:36 -070050MODULE_DESCRIPTION(ACPI_AC_DRIVER_NAME);
51MODULE_LICENSE("GPL");
52
Len Brown4be44fc2005-08-05 00:44:28 -040053static int acpi_ac_add(struct acpi_device *device);
54static int acpi_ac_remove(struct acpi_device *device, int type);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055static int acpi_ac_open_fs(struct inode *inode, struct file *file);
56
57static struct acpi_driver acpi_ac_driver = {
Len Brown4be44fc2005-08-05 00:44:28 -040058 .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 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070065};
66
67struct acpi_ac {
Len Brown4be44fc2005-08-05 00:44:28 -040068 acpi_handle handle;
69 unsigned long state;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070};
71
72static struct file_operations acpi_ac_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -040073 .open = acpi_ac_open_fs,
74 .read = seq_read,
75 .llseek = seq_lseek,
76 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -070077};
78
79/* --------------------------------------------------------------------------
80 AC Adapter Management
81 -------------------------------------------------------------------------- */
82
Len Brown4be44fc2005-08-05 00:44:28 -040083static int acpi_ac_get_state(struct acpi_ac *ac)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
Len Brown4be44fc2005-08-05 00:44:28 -040085 acpi_status status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88 if (!ac)
Patrick Mocheld550d982006-06-27 00:41:40 -040089 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91 status = acpi_evaluate_integer(ac->handle, "_PSR", NULL, &ac->state);
92 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -040093 ACPI_EXCEPTION((AE_INFO, status, "Error reading AC Adapter state"));
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 ac->state = ACPI_AC_STATUS_UNKNOWN;
Patrick Mocheld550d982006-06-27 00:41:40 -040095 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 }
Len Brown4be44fc2005-08-05 00:44:28 -040097
Patrick Mocheld550d982006-06-27 00:41:40 -040098 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101/* --------------------------------------------------------------------------
102 FS Interface (/proc)
103 -------------------------------------------------------------------------- */
104
Len Brown4be44fc2005-08-05 00:44:28 -0400105static struct proc_dir_entry *acpi_ac_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107static int acpi_ac_seq_show(struct seq_file *seq, void *offset)
108{
Len Brown4be44fc2005-08-05 00:44:28 -0400109 struct acpi_ac *ac = (struct acpi_ac *)seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112 if (!ac)
Patrick Mocheld550d982006-06-27 00:41:40 -0400113 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115 if (acpi_ac_get_state(ac)) {
116 seq_puts(seq, "ERROR: Unable to read AC Adapter state\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400117 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 }
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
Patrick Mocheld550d982006-06-27 00:41:40 -0400133 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134}
Len Brown4be44fc2005-08-05 00:44:28 -0400135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136static 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
Len Brown4be44fc2005-08-05 00:44:28 -0400141static int acpi_ac_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
Len Brown4be44fc2005-08-05 00:44:28 -0400143 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 if (!acpi_device_dir(device)) {
147 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown4be44fc2005-08-05 00:44:28 -0400148 acpi_ac_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400150 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 acpi_device_dir(device)->owner = THIS_MODULE;
152 }
153
154 /* 'state' [R] */
155 entry = create_proc_entry(ACPI_AC_FILE_STATE,
Len Brown4be44fc2005-08-05 00:44:28 -0400156 S_IRUGO, acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400158 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 else {
160 entry->proc_fops = &acpi_ac_fops;
161 entry->data = acpi_driver_data(device);
162 entry->owner = THIS_MODULE;
163 }
164
Patrick Mocheld550d982006-06-27 00:41:40 -0400165 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
167
Len Brown4be44fc2005-08-05 00:44:28 -0400168static int acpi_ac_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 if (acpi_device_dir(device)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400172 remove_proc_entry(ACPI_AC_FILE_STATE, acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 remove_proc_entry(acpi_device_bid(device), acpi_ac_dir);
175 acpi_device_dir(device) = NULL;
176 }
177
Patrick Mocheld550d982006-06-27 00:41:40 -0400178 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181/* --------------------------------------------------------------------------
182 Driver Model
183 -------------------------------------------------------------------------- */
184
Len Brown4be44fc2005-08-05 00:44:28 -0400185static void acpi_ac_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
Len Brown4be44fc2005-08-05 00:44:28 -0400187 struct acpi_ac *ac = (struct acpi_ac *)data;
188 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191 if (!ac)
Patrick Mocheld550d982006-06-27 00:41:40 -0400192 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 if (acpi_bus_get_device(ac->handle, &device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400195 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 switch (event) {
198 case ACPI_AC_NOTIFY_STATUS:
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,
Len Brown4be44fc2005-08-05 00:44:28 -0400204 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 break;
206 }
207
Patrick Mocheld550d982006-06-27 00:41:40 -0400208 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
Len Brown4be44fc2005-08-05 00:44:28 -0400211static int acpi_ac_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
Len Brown4be44fc2005-08-05 00:44:28 -0400213 int result = 0;
214 acpi_status status = AE_OK;
215 struct acpi_ac *ac = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400219 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221 ac = kmalloc(sizeof(struct acpi_ac), GFP_KERNEL);
222 if (!ac)
Patrick Mocheld550d982006-06-27 00:41:40 -0400223 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 memset(ac, 0, sizeof(struct acpi_ac));
225
226 ac->handle = device->handle;
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;
230
231 result = acpi_ac_get_state(ac);
232 if (result)
233 goto end;
234
235 result = acpi_ac_add_fs(device);
236 if (result)
237 goto end;
238
239 status = acpi_install_notify_handler(ac->handle,
Len Brown4be44fc2005-08-05 00:44:28 -0400240 ACPI_DEVICE_NOTIFY, acpi_ac_notify,
241 ac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 result = -ENODEV;
244 goto end;
245 }
246
Len Brown4be44fc2005-08-05 00:44:28 -0400247 printk(KERN_INFO PREFIX "%s [%s] (%s)\n",
248 acpi_device_name(device), acpi_device_bid(device),
249 ac->state ? "on-line" : "off-line");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Len Brown4be44fc2005-08-05 00:44:28 -0400251 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 if (result) {
253 acpi_ac_remove_fs(device);
254 kfree(ac);
255 }
256
Patrick Mocheld550d982006-06-27 00:41:40 -0400257 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258}
259
Len Brown4be44fc2005-08-05 00:44:28 -0400260static int acpi_ac_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
Len Brown4be44fc2005-08-05 00:44:28 -0400262 acpi_status status = AE_OK;
263 struct acpi_ac *ac = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400267 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Len Brown4be44fc2005-08-05 00:44:28 -0400269 ac = (struct acpi_ac *)acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271 status = acpi_remove_notify_handler(ac->handle,
Len Brown4be44fc2005-08-05 00:44:28 -0400272 ACPI_DEVICE_NOTIFY, acpi_ac_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274 acpi_ac_remove_fs(device);
275
276 kfree(ac);
277
Patrick Mocheld550d982006-06-27 00:41:40 -0400278 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279}
280
Len Brown4be44fc2005-08-05 00:44:28 -0400281static int __init acpi_ac_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
Len Brown4be44fc2005-08-05 00:44:28 -0400283 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 acpi_ac_dir = proc_mkdir(ACPI_AC_CLASS, acpi_root_dir);
287 if (!acpi_ac_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -0400288 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 acpi_ac_dir->owner = THIS_MODULE;
290
291 result = acpi_bus_register_driver(&acpi_ac_driver);
292 if (result < 0) {
293 remove_proc_entry(ACPI_AC_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -0400294 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 }
296
Patrick Mocheld550d982006-06-27 00:41:40 -0400297 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
Len Brown4be44fc2005-08-05 00:44:28 -0400300static void __exit acpi_ac_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303 acpi_bus_unregister_driver(&acpi_ac_driver);
304
305 remove_proc_entry(ACPI_AC_CLASS, acpi_root_dir);
306
Patrick Mocheld550d982006-06-27 00:41:40 -0400307 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308}
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310module_init(acpi_ac_init);
311module_exit(acpi_ac_exit);