blob: e03de37a750d547d7e69d91264b224500dc063b8 [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>
Alexey Starikovskiyd5b4a3d2007-09-26 19:44:06 +040032#include <linux/power_supply.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <acpi/acpi_bus.h>
34#include <acpi/acpi_drivers.h>
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#define ACPI_AC_COMPONENT 0x00020000
37#define ACPI_AC_CLASS "ac_adapter"
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
Thomas Renninger1ba90e32007-07-23 14:44:41 +020059const static struct acpi_device_id ac_device_ids[] = {
60 {"ACPI0003", 0},
61 {"", 0},
62};
63MODULE_DEVICE_TABLE(acpi, ac_device_ids);
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065static struct acpi_driver acpi_ac_driver = {
Len Brownc2b6705b2007-02-12 23:33:40 -050066 .name = "ac",
Len Brown4be44fc2005-08-05 00:44:28 -040067 .class = ACPI_AC_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +020068 .ids = ac_device_ids,
Len Brown4be44fc2005-08-05 00:44:28 -040069 .ops = {
70 .add = acpi_ac_add,
71 .remove = acpi_ac_remove,
72 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070073};
74
75struct acpi_ac {
Alexey Starikovskiyd5b4a3d2007-09-26 19:44:06 +040076 struct power_supply charger;
Patrick Mochelaf961792006-05-19 16:54:32 -040077 struct acpi_device * device;
Len Brown4be44fc2005-08-05 00:44:28 -040078 unsigned long state;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079};
80
Alexey Starikovskiyd5b4a3d2007-09-26 19:44:06 +040081#define to_acpi_ac(x) container_of(x, struct acpi_ac, charger);
82
Arjan van de Vend7508032006-07-04 13:06:00 -040083static const struct file_operations acpi_ac_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -040084 .open = acpi_ac_open_fs,
85 .read = seq_read,
86 .llseek = seq_lseek,
87 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -070088};
Alexey Starikovskiyd5b4a3d2007-09-26 19:44:06 +040089static int get_ac_property(struct power_supply *psy,
90 enum power_supply_property psp,
91 union power_supply_propval *val)
92{
93 struct acpi_ac *ac = to_acpi_ac(psy);
94 switch (psp) {
95 case POWER_SUPPLY_PROP_ONLINE:
96 val->intval = ac->state;
97 break;
98 default:
99 return -EINVAL;
100 }
101 return 0;
102}
103
104static enum power_supply_property ac_props[] = {
105 POWER_SUPPLY_PROP_ONLINE,
106};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108/* --------------------------------------------------------------------------
109 AC Adapter Management
110 -------------------------------------------------------------------------- */
111
Len Brown4be44fc2005-08-05 00:44:28 -0400112static int acpi_ac_get_state(struct acpi_ac *ac)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
Len Brown4be44fc2005-08-05 00:44:28 -0400114 acpi_status status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117 if (!ac)
Patrick Mocheld550d982006-06-27 00:41:40 -0400118 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Patrick Mochela6ba5eb2006-05-19 16:54:41 -0400120 status = acpi_evaluate_integer(ac->device->handle, "_PSR", NULL, &ac->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400122 ACPI_EXCEPTION((AE_INFO, status, "Error reading AC Adapter state"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 ac->state = ACPI_AC_STATUS_UNKNOWN;
Patrick Mocheld550d982006-06-27 00:41:40 -0400124 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 }
Len Brown4be44fc2005-08-05 00:44:28 -0400126
Patrick Mocheld550d982006-06-27 00:41:40 -0400127 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130/* --------------------------------------------------------------------------
131 FS Interface (/proc)
132 -------------------------------------------------------------------------- */
133
Len Brown4be44fc2005-08-05 00:44:28 -0400134static struct proc_dir_entry *acpi_ac_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136static int acpi_ac_seq_show(struct seq_file *seq, void *offset)
137{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200138 struct acpi_ac *ac = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 if (!ac)
Patrick Mocheld550d982006-06-27 00:41:40 -0400142 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 if (acpi_ac_get_state(ac)) {
145 seq_puts(seq, "ERROR: Unable to read AC Adapter state\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400146 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 }
148
149 seq_puts(seq, "state: ");
150 switch (ac->state) {
151 case ACPI_AC_STATUS_OFFLINE:
152 seq_puts(seq, "off-line\n");
153 break;
154 case ACPI_AC_STATUS_ONLINE:
155 seq_puts(seq, "on-line\n");
156 break;
157 default:
158 seq_puts(seq, "unknown\n");
159 break;
160 }
161
Patrick Mocheld550d982006-06-27 00:41:40 -0400162 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
Len Brown4be44fc2005-08-05 00:44:28 -0400164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165static int acpi_ac_open_fs(struct inode *inode, struct file *file)
166{
167 return single_open(file, acpi_ac_seq_show, PDE(inode)->data);
168}
169
Len Brown4be44fc2005-08-05 00:44:28 -0400170static int acpi_ac_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
Len Brown4be44fc2005-08-05 00:44:28 -0400172 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 if (!acpi_device_dir(device)) {
176 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown4be44fc2005-08-05 00:44:28 -0400177 acpi_ac_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400179 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 acpi_device_dir(device)->owner = THIS_MODULE;
181 }
182
183 /* 'state' [R] */
184 entry = create_proc_entry(ACPI_AC_FILE_STATE,
Len Brown4be44fc2005-08-05 00:44:28 -0400185 S_IRUGO, acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400187 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 else {
189 entry->proc_fops = &acpi_ac_fops;
190 entry->data = acpi_driver_data(device);
191 entry->owner = THIS_MODULE;
192 }
193
Patrick Mocheld550d982006-06-27 00:41:40 -0400194 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
196
Len Brown4be44fc2005-08-05 00:44:28 -0400197static int acpi_ac_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200 if (acpi_device_dir(device)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400201 remove_proc_entry(ACPI_AC_FILE_STATE, acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 remove_proc_entry(acpi_device_bid(device), acpi_ac_dir);
204 acpi_device_dir(device) = NULL;
205 }
206
Patrick Mocheld550d982006-06-27 00:41:40 -0400207 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208}
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210/* --------------------------------------------------------------------------
211 Driver Model
212 -------------------------------------------------------------------------- */
213
Len Brown4be44fc2005-08-05 00:44:28 -0400214static void acpi_ac_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200216 struct acpi_ac *ac = data;
Len Brown4be44fc2005-08-05 00:44:28 -0400217 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 if (!ac)
Patrick Mocheld550d982006-06-27 00:41:40 -0400221 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Patrick Mochelaf961792006-05-19 16:54:32 -0400223 device = ac->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 switch (event) {
225 case ACPI_AC_NOTIFY_STATUS:
Christian Lupien03d78252004-08-19 01:26:00 -0400226 case ACPI_NOTIFY_BUS_CHECK:
227 case ACPI_NOTIFY_DEVICE_CHECK:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 acpi_ac_get_state(ac);
Len Brown14e04fb2007-08-23 15:20:26 -0400229 acpi_bus_generate_proc_event(device, event, (u32) ac->state);
Zhang Rui962ce8c2007-08-23 01:24:31 +0800230 acpi_bus_generate_netlink_event(device->pnp.device_class,
231 device->dev.bus_id, event,
232 (u32) ac->state);
Alexey Starikovskiyd5b4a3d2007-09-26 19:44:06 +0400233 kobject_uevent(&ac->charger.dev->kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 break;
235 default:
236 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400237 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 break;
239 }
240
Patrick Mocheld550d982006-06-27 00:41:40 -0400241 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242}
243
Len Brown4be44fc2005-08-05 00:44:28 -0400244static int acpi_ac_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
Len Brown4be44fc2005-08-05 00:44:28 -0400246 int result = 0;
247 acpi_status status = AE_OK;
248 struct acpi_ac *ac = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400252 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Burman Yan36bcbec2006-12-19 12:56:11 -0800254 ac = kzalloc(sizeof(struct acpi_ac), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 if (!ac)
Patrick Mocheld550d982006-06-27 00:41:40 -0400256 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Patrick Mochelaf961792006-05-19 16:54:32 -0400258 ac->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 strcpy(acpi_device_name(device), ACPI_AC_DEVICE_NAME);
260 strcpy(acpi_device_class(device), ACPI_AC_CLASS);
261 acpi_driver_data(device) = ac;
262
263 result = acpi_ac_get_state(ac);
264 if (result)
265 goto end;
266
267 result = acpi_ac_add_fs(device);
268 if (result)
269 goto end;
Alexey Starikovskiyd5b4a3d2007-09-26 19:44:06 +0400270 ac->charger.name = acpi_device_bid(device);
271 ac->charger.type = POWER_SUPPLY_TYPE_MAINS;
272 ac->charger.properties = ac_props;
273 ac->charger.num_properties = ARRAY_SIZE(ac_props);
274 ac->charger.get_property = get_ac_property;
275 power_supply_register(&ac->device->dev, &ac->charger);
Patrick Mochela6ba5eb2006-05-19 16:54:41 -0400276 status = acpi_install_notify_handler(device->handle,
Christian Lupien03d78252004-08-19 01:26:00 -0400277 ACPI_ALL_NOTIFY, acpi_ac_notify,
Len Brown4be44fc2005-08-05 00:44:28 -0400278 ac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 result = -ENODEV;
281 goto end;
282 }
283
Len Brown4be44fc2005-08-05 00:44:28 -0400284 printk(KERN_INFO PREFIX "%s [%s] (%s)\n",
285 acpi_device_name(device), acpi_device_bid(device),
286 ac->state ? "on-line" : "off-line");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Len Brown4be44fc2005-08-05 00:44:28 -0400288 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 if (result) {
290 acpi_ac_remove_fs(device);
291 kfree(ac);
292 }
293
Patrick Mocheld550d982006-06-27 00:41:40 -0400294 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
296
Len Brown4be44fc2005-08-05 00:44:28 -0400297static int acpi_ac_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
Len Brown4be44fc2005-08-05 00:44:28 -0400299 acpi_status status = AE_OK;
300 struct acpi_ac *ac = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400304 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200306 ac = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Patrick Mochela6ba5eb2006-05-19 16:54:41 -0400308 status = acpi_remove_notify_handler(device->handle,
Christian Lupien03d78252004-08-19 01:26:00 -0400309 ACPI_ALL_NOTIFY, acpi_ac_notify);
Alexey Starikovskiyd5b4a3d2007-09-26 19:44:06 +0400310 if (ac->charger.dev)
311 power_supply_unregister(&ac->charger);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 acpi_ac_remove_fs(device);
313
314 kfree(ac);
315
Patrick Mocheld550d982006-06-27 00:41:40 -0400316 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
318
Len Brown4be44fc2005-08-05 00:44:28 -0400319static int __init acpi_ac_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
Rich Townsend3f86b832006-07-01 11:36:54 -0400321 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Pavel Machek4d8316d2006-08-14 22:37:22 -0700323 if (acpi_disabled)
324 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Rich Townsend3f86b832006-07-01 11:36:54 -0400326 acpi_ac_dir = acpi_lock_ac_dir();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 if (!acpi_ac_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -0400328 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 result = acpi_bus_register_driver(&acpi_ac_driver);
331 if (result < 0) {
Rich Townsend3f86b832006-07-01 11:36:54 -0400332 acpi_unlock_ac_dir(acpi_ac_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -0400333 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 }
335
Patrick Mocheld550d982006-06-27 00:41:40 -0400336 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
338
Len Brown4be44fc2005-08-05 00:44:28 -0400339static void __exit acpi_ac_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 acpi_bus_unregister_driver(&acpi_ac_driver);
343
Rich Townsend3f86b832006-07-01 11:36:54 -0400344 acpi_unlock_ac_dir(acpi_ac_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Patrick Mocheld550d982006-06-27 00:41:40 -0400346 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347}
348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349module_init(acpi_ac_init);
350module_exit(acpi_ac_exit);