blob: 6daeace796a86c2b3e7892c517b5bab107ceb8a7 [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
Rich Townsend3f86b832006-07-01 11:36:54 -040053extern struct proc_dir_entry *acpi_lock_ac_dir(void);
54extern void *acpi_unlock_ac_dir(struct proc_dir_entry *acpi_ac_dir);
55
Len Brown4be44fc2005-08-05 00:44:28 -040056static int acpi_ac_add(struct acpi_device *device);
57static int acpi_ac_remove(struct acpi_device *device, int type);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058static int acpi_ac_open_fs(struct inode *inode, struct file *file);
59
60static struct acpi_driver acpi_ac_driver = {
Len Brown4be44fc2005-08-05 00:44:28 -040061 .name = ACPI_AC_DRIVER_NAME,
62 .class = ACPI_AC_CLASS,
63 .ids = ACPI_AC_HID,
64 .ops = {
65 .add = acpi_ac_add,
66 .remove = acpi_ac_remove,
67 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070068};
69
70struct acpi_ac {
Patrick Mochelaf961792006-05-19 16:54:32 -040071 struct acpi_device * device;
Len Brown4be44fc2005-08-05 00:44:28 -040072 unsigned long state;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073};
74
Arjan van de Vend7508032006-07-04 13:06:00 -040075static const struct file_operations acpi_ac_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -040076 .open = acpi_ac_open_fs,
77 .read = seq_read,
78 .llseek = seq_lseek,
79 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -070080};
81
82/* --------------------------------------------------------------------------
83 AC Adapter Management
84 -------------------------------------------------------------------------- */
85
Len Brown4be44fc2005-08-05 00:44:28 -040086static int acpi_ac_get_state(struct acpi_ac *ac)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
Len Brown4be44fc2005-08-05 00:44:28 -040088 acpi_status status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91 if (!ac)
Patrick Mocheld550d982006-06-27 00:41:40 -040092 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Patrick Mochela6ba5eb2006-05-19 16:54:41 -040094 status = acpi_evaluate_integer(ac->device->handle, "_PSR", NULL, &ac->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -040096 ACPI_EXCEPTION((AE_INFO, status, "Error reading AC Adapter state"));
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 ac->state = ACPI_AC_STATUS_UNKNOWN;
Patrick Mocheld550d982006-06-27 00:41:40 -040098 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 }
Len Brown4be44fc2005-08-05 00:44:28 -0400100
Patrick Mocheld550d982006-06-27 00:41:40 -0400101 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104/* --------------------------------------------------------------------------
105 FS Interface (/proc)
106 -------------------------------------------------------------------------- */
107
Len Brown4be44fc2005-08-05 00:44:28 -0400108static struct proc_dir_entry *acpi_ac_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110static int acpi_ac_seq_show(struct seq_file *seq, void *offset)
111{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200112 struct acpi_ac *ac = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115 if (!ac)
Patrick Mocheld550d982006-06-27 00:41:40 -0400116 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118 if (acpi_ac_get_state(ac)) {
119 seq_puts(seq, "ERROR: Unable to read AC Adapter state\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400120 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 }
122
123 seq_puts(seq, "state: ");
124 switch (ac->state) {
125 case ACPI_AC_STATUS_OFFLINE:
126 seq_puts(seq, "off-line\n");
127 break;
128 case ACPI_AC_STATUS_ONLINE:
129 seq_puts(seq, "on-line\n");
130 break;
131 default:
132 seq_puts(seq, "unknown\n");
133 break;
134 }
135
Patrick Mocheld550d982006-06-27 00:41:40 -0400136 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
Len Brown4be44fc2005-08-05 00:44:28 -0400138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139static int acpi_ac_open_fs(struct inode *inode, struct file *file)
140{
141 return single_open(file, acpi_ac_seq_show, PDE(inode)->data);
142}
143
Len Brown4be44fc2005-08-05 00:44:28 -0400144static int acpi_ac_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
Len Brown4be44fc2005-08-05 00:44:28 -0400146 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149 if (!acpi_device_dir(device)) {
150 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown4be44fc2005-08-05 00:44:28 -0400151 acpi_ac_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400153 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 acpi_device_dir(device)->owner = THIS_MODULE;
155 }
156
157 /* 'state' [R] */
158 entry = create_proc_entry(ACPI_AC_FILE_STATE,
Len Brown4be44fc2005-08-05 00:44:28 -0400159 S_IRUGO, acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400161 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 else {
163 entry->proc_fops = &acpi_ac_fops;
164 entry->data = acpi_driver_data(device);
165 entry->owner = THIS_MODULE;
166 }
167
Patrick Mocheld550d982006-06-27 00:41:40 -0400168 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
170
Len Brown4be44fc2005-08-05 00:44:28 -0400171static int acpi_ac_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 if (acpi_device_dir(device)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400175 remove_proc_entry(ACPI_AC_FILE_STATE, acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177 remove_proc_entry(acpi_device_bid(device), acpi_ac_dir);
178 acpi_device_dir(device) = NULL;
179 }
180
Patrick Mocheld550d982006-06-27 00:41:40 -0400181 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182}
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184/* --------------------------------------------------------------------------
185 Driver Model
186 -------------------------------------------------------------------------- */
187
Len Brown4be44fc2005-08-05 00:44:28 -0400188static void acpi_ac_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200190 struct acpi_ac *ac = data;
Len Brown4be44fc2005-08-05 00:44:28 -0400191 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 if (!ac)
Patrick Mocheld550d982006-06-27 00:41:40 -0400195 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Patrick Mochelaf961792006-05-19 16:54:32 -0400197 device = ac->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 switch (event) {
199 case ACPI_AC_NOTIFY_STATUS:
Christian Lupien03d782522004-08-19 01:26:00 -0400200 case ACPI_NOTIFY_BUS_CHECK:
201 case ACPI_NOTIFY_DEVICE_CHECK:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 acpi_ac_get_state(ac);
203 acpi_bus_generate_event(device, event, (u32) ac->state);
204 break;
205 default:
206 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400207 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 break;
209 }
210
Patrick Mocheld550d982006-06-27 00:41:40 -0400211 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
Len Brown4be44fc2005-08-05 00:44:28 -0400214static int acpi_ac_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
Len Brown4be44fc2005-08-05 00:44:28 -0400216 int result = 0;
217 acpi_status status = AE_OK;
218 struct acpi_ac *ac = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400222 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Burman Yan36bcbec2006-12-19 12:56:11 -0800224 ac = kzalloc(sizeof(struct acpi_ac), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 if (!ac)
Patrick Mocheld550d982006-06-27 00:41:40 -0400226 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Patrick Mochelaf961792006-05-19 16:54:32 -0400228 ac->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 strcpy(acpi_device_name(device), ACPI_AC_DEVICE_NAME);
230 strcpy(acpi_device_class(device), ACPI_AC_CLASS);
231 acpi_driver_data(device) = ac;
232
233 result = acpi_ac_get_state(ac);
234 if (result)
235 goto end;
236
237 result = acpi_ac_add_fs(device);
238 if (result)
239 goto end;
240
Patrick Mochela6ba5eb2006-05-19 16:54:41 -0400241 status = acpi_install_notify_handler(device->handle,
Christian Lupien03d782522004-08-19 01:26:00 -0400242 ACPI_ALL_NOTIFY, acpi_ac_notify,
Len Brown4be44fc2005-08-05 00:44:28 -0400243 ac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 result = -ENODEV;
246 goto end;
247 }
248
Len Brown4be44fc2005-08-05 00:44:28 -0400249 printk(KERN_INFO PREFIX "%s [%s] (%s)\n",
250 acpi_device_name(device), acpi_device_bid(device),
251 ac->state ? "on-line" : "off-line");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Len Brown4be44fc2005-08-05 00:44:28 -0400253 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 if (result) {
255 acpi_ac_remove_fs(device);
256 kfree(ac);
257 }
258
Patrick Mocheld550d982006-06-27 00:41:40 -0400259 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
261
Len Brown4be44fc2005-08-05 00:44:28 -0400262static int acpi_ac_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
Len Brown4be44fc2005-08-05 00:44:28 -0400264 acpi_status status = AE_OK;
265 struct acpi_ac *ac = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400269 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200271 ac = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Patrick Mochela6ba5eb2006-05-19 16:54:41 -0400273 status = acpi_remove_notify_handler(device->handle,
Christian Lupien03d782522004-08-19 01:26:00 -0400274 ACPI_ALL_NOTIFY, acpi_ac_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 acpi_ac_remove_fs(device);
277
278 kfree(ac);
279
Patrick Mocheld550d982006-06-27 00:41:40 -0400280 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281}
282
Len Brown4be44fc2005-08-05 00:44:28 -0400283static int __init acpi_ac_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
Rich Townsend3f86b832006-07-01 11:36:54 -0400285 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Pavel Machek4d8316d2006-08-14 22:37:22 -0700287 if (acpi_disabled)
288 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Rich Townsend3f86b832006-07-01 11:36:54 -0400290 acpi_ac_dir = acpi_lock_ac_dir();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 if (!acpi_ac_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -0400292 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294 result = acpi_bus_register_driver(&acpi_ac_driver);
295 if (result < 0) {
Rich Townsend3f86b832006-07-01 11:36:54 -0400296 acpi_unlock_ac_dir(acpi_ac_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -0400297 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 }
299
Patrick Mocheld550d982006-06-27 00:41:40 -0400300 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301}
302
Len Brown4be44fc2005-08-05 00:44:28 -0400303static void __exit acpi_ac_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 acpi_bus_unregister_driver(&acpi_ac_driver);
307
Rich Townsend3f86b832006-07-01 11:36:54 -0400308 acpi_unlock_ac_dir(acpi_ac_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Patrick Mocheld550d982006-06-27 00:41:40 -0400310 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313module_init(acpi_ac_init);
314module_exit(acpi_ac_exit);