blob: 37c7dc4f9fe5db6d15b7bba43803790f0c0ee588 [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#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
44
45#define _COMPONENT ACPI_AC_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050046ACPI_MODULE_NAME("ac");
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Len Brownf52fd662007-02-12 22:42:12 -050048MODULE_AUTHOR("Paul Diefenbaugh");
Len Brown7cda93e2007-02-12 23:50:02 -050049MODULE_DESCRIPTION("ACPI AC Adapter Driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070050MODULE_LICENSE("GPL");
51
Rich Townsend3f86b832006-07-01 11:36:54 -040052extern struct proc_dir_entry *acpi_lock_ac_dir(void);
53extern void *acpi_unlock_ac_dir(struct proc_dir_entry *acpi_ac_dir);
54
Len Brown4be44fc2005-08-05 00:44:28 -040055static int acpi_ac_add(struct acpi_device *device);
56static int acpi_ac_remove(struct acpi_device *device, int type);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057static int acpi_ac_open_fs(struct inode *inode, struct file *file);
58
59static struct acpi_driver acpi_ac_driver = {
Len Brownc2b67052007-02-12 23:33:40 -050060 .name = "ac",
Len Brown4be44fc2005-08-05 00:44:28 -040061 .class = ACPI_AC_CLASS,
62 .ids = ACPI_AC_HID,
63 .ops = {
64 .add = acpi_ac_add,
65 .remove = acpi_ac_remove,
66 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070067};
68
69struct acpi_ac {
Patrick Mochelaf961792006-05-19 16:54:32 -040070 struct acpi_device * device;
Len Brown4be44fc2005-08-05 00:44:28 -040071 unsigned long state;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072};
73
Arjan van de Vend7508032006-07-04 13:06:00 -040074static const struct file_operations acpi_ac_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -040075 .open = acpi_ac_open_fs,
76 .read = seq_read,
77 .llseek = seq_lseek,
78 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -070079};
80
81/* --------------------------------------------------------------------------
82 AC Adapter Management
83 -------------------------------------------------------------------------- */
84
Len Brown4be44fc2005-08-05 00:44:28 -040085static int acpi_ac_get_state(struct acpi_ac *ac)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
Len Brown4be44fc2005-08-05 00:44:28 -040087 acpi_status status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90 if (!ac)
Patrick Mocheld550d982006-06-27 00:41:40 -040091 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Patrick Mochela6ba5eb2006-05-19 16:54:41 -040093 status = acpi_evaluate_integer(ac->device->handle, "_PSR", NULL, &ac->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -040095 ACPI_EXCEPTION((AE_INFO, status, "Error reading AC Adapter state"));
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 ac->state = ACPI_AC_STATUS_UNKNOWN;
Patrick Mocheld550d982006-06-27 00:41:40 -040097 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 }
Len Brown4be44fc2005-08-05 00:44:28 -040099
Patrick Mocheld550d982006-06-27 00:41:40 -0400100 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101}
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103/* --------------------------------------------------------------------------
104 FS Interface (/proc)
105 -------------------------------------------------------------------------- */
106
Len Brown4be44fc2005-08-05 00:44:28 -0400107static struct proc_dir_entry *acpi_ac_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109static int acpi_ac_seq_show(struct seq_file *seq, void *offset)
110{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200111 struct acpi_ac *ac = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114 if (!ac)
Patrick Mocheld550d982006-06-27 00:41:40 -0400115 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117 if (acpi_ac_get_state(ac)) {
118 seq_puts(seq, "ERROR: Unable to read AC Adapter state\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400119 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 }
121
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;
133 }
134
Patrick Mocheld550d982006-06-27 00:41:40 -0400135 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136}
Len Brown4be44fc2005-08-05 00:44:28 -0400137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138static int acpi_ac_open_fs(struct inode *inode, struct file *file)
139{
140 return single_open(file, acpi_ac_seq_show, PDE(inode)->data);
141}
142
Len Brown4be44fc2005-08-05 00:44:28 -0400143static int acpi_ac_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
Len Brown4be44fc2005-08-05 00:44:28 -0400145 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 if (!acpi_device_dir(device)) {
149 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown4be44fc2005-08-05 00:44:28 -0400150 acpi_ac_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400152 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 acpi_device_dir(device)->owner = THIS_MODULE;
154 }
155
156 /* 'state' [R] */
157 entry = create_proc_entry(ACPI_AC_FILE_STATE,
Len Brown4be44fc2005-08-05 00:44:28 -0400158 S_IRUGO, acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400160 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 else {
162 entry->proc_fops = &acpi_ac_fops;
163 entry->data = acpi_driver_data(device);
164 entry->owner = THIS_MODULE;
165 }
166
Patrick Mocheld550d982006-06-27 00:41:40 -0400167 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168}
169
Len Brown4be44fc2005-08-05 00:44:28 -0400170static int acpi_ac_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 if (acpi_device_dir(device)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400174 remove_proc_entry(ACPI_AC_FILE_STATE, acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176 remove_proc_entry(acpi_device_bid(device), acpi_ac_dir);
177 acpi_device_dir(device) = NULL;
178 }
179
Patrick Mocheld550d982006-06-27 00:41:40 -0400180 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181}
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183/* --------------------------------------------------------------------------
184 Driver Model
185 -------------------------------------------------------------------------- */
186
Len Brown4be44fc2005-08-05 00:44:28 -0400187static void acpi_ac_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200189 struct acpi_ac *ac = data;
Len Brown4be44fc2005-08-05 00:44:28 -0400190 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193 if (!ac)
Patrick Mocheld550d982006-06-27 00:41:40 -0400194 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Patrick Mochelaf961792006-05-19 16:54:32 -0400196 device = ac->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 switch (event) {
198 case ACPI_AC_NOTIFY_STATUS:
Christian Lupien03d78252004-08-19 01:26:00 -0400199 case ACPI_NOTIFY_BUS_CHECK:
200 case ACPI_NOTIFY_DEVICE_CHECK:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 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,
Len Brown4be44fc2005-08-05 00:44:28 -0400206 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 break;
208 }
209
Patrick Mocheld550d982006-06-27 00:41:40 -0400210 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211}
212
Len Brown4be44fc2005-08-05 00:44:28 -0400213static int acpi_ac_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
Len Brown4be44fc2005-08-05 00:44:28 -0400215 int result = 0;
216 acpi_status status = AE_OK;
217 struct acpi_ac *ac = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400221 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Burman Yan36bcbec2006-12-19 12:56:11 -0800223 ac = kzalloc(sizeof(struct acpi_ac), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 if (!ac)
Patrick Mocheld550d982006-06-27 00:41:40 -0400225 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Patrick Mochelaf961792006-05-19 16:54:32 -0400227 ac->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 strcpy(acpi_device_name(device), ACPI_AC_DEVICE_NAME);
229 strcpy(acpi_device_class(device), ACPI_AC_CLASS);
230 acpi_driver_data(device) = ac;
231
232 result = acpi_ac_get_state(ac);
233 if (result)
234 goto end;
235
236 result = acpi_ac_add_fs(device);
237 if (result)
238 goto end;
239
Patrick Mochela6ba5eb2006-05-19 16:54:41 -0400240 status = acpi_install_notify_handler(device->handle,
Christian Lupien03d78252004-08-19 01:26:00 -0400241 ACPI_ALL_NOTIFY, acpi_ac_notify,
Len Brown4be44fc2005-08-05 00:44:28 -0400242 ac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 result = -ENODEV;
245 goto end;
246 }
247
Len Brown4be44fc2005-08-05 00:44:28 -0400248 printk(KERN_INFO PREFIX "%s [%s] (%s)\n",
249 acpi_device_name(device), acpi_device_bid(device),
250 ac->state ? "on-line" : "off-line");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Len Brown4be44fc2005-08-05 00:44:28 -0400252 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 if (result) {
254 acpi_ac_remove_fs(device);
255 kfree(ac);
256 }
257
Patrick Mocheld550d982006-06-27 00:41:40 -0400258 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
Len Brown4be44fc2005-08-05 00:44:28 -0400261static int acpi_ac_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
Len Brown4be44fc2005-08-05 00:44:28 -0400263 acpi_status status = AE_OK;
264 struct acpi_ac *ac = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400268 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200270 ac = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Patrick Mochela6ba5eb2006-05-19 16:54:41 -0400272 status = acpi_remove_notify_handler(device->handle,
Christian Lupien03d78252004-08-19 01:26:00 -0400273 ACPI_ALL_NOTIFY, acpi_ac_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275 acpi_ac_remove_fs(device);
276
277 kfree(ac);
278
Patrick Mocheld550d982006-06-27 00:41:40 -0400279 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
281
Len Brown4be44fc2005-08-05 00:44:28 -0400282static int __init acpi_ac_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
Rich Townsend3f86b832006-07-01 11:36:54 -0400284 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Pavel Machek4d8316d2006-08-14 22:37:22 -0700286 if (acpi_disabled)
287 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Rich Townsend3f86b832006-07-01 11:36:54 -0400289 acpi_ac_dir = acpi_lock_ac_dir();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 if (!acpi_ac_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -0400291 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293 result = acpi_bus_register_driver(&acpi_ac_driver);
294 if (result < 0) {
Rich Townsend3f86b832006-07-01 11:36:54 -0400295 acpi_unlock_ac_dir(acpi_ac_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -0400296 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 }
298
Patrick Mocheld550d982006-06-27 00:41:40 -0400299 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300}
301
Len Brown4be44fc2005-08-05 00:44:28 -0400302static void __exit acpi_ac_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 acpi_bus_unregister_driver(&acpi_ac_driver);
306
Rich Townsend3f86b832006-07-01 11:36:54 -0400307 acpi_unlock_ac_dir(acpi_ac_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Patrick Mocheld550d982006-06-27 00:41:40 -0400309 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310}
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312module_init(acpi_ac_init);
313module_exit(acpi_ac_exit);