blob: d8b35093527a3bbb8962c180b1b7792f5a2eb48b [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#define ACPI_AC_DEVICE_NAME "AC Adapter"
38#define ACPI_AC_FILE_STATE "state"
39#define ACPI_AC_NOTIFY_STATUS 0x80
40#define ACPI_AC_STATUS_OFFLINE 0x00
41#define ACPI_AC_STATUS_ONLINE 0x01
42#define ACPI_AC_STATUS_UNKNOWN 0xFF
43
44#define _COMPONENT ACPI_AC_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050045ACPI_MODULE_NAME("ac");
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Len Brownf52fd662007-02-12 22:42:12 -050047MODULE_AUTHOR("Paul Diefenbaugh");
Len Brown7cda93e2007-02-12 23:50:02 -050048MODULE_DESCRIPTION("ACPI AC Adapter Driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070049MODULE_LICENSE("GPL");
50
Rich Townsend3f86b832006-07-01 11:36:54 -040051extern struct proc_dir_entry *acpi_lock_ac_dir(void);
52extern void *acpi_unlock_ac_dir(struct proc_dir_entry *acpi_ac_dir);
53
Len Brown4be44fc2005-08-05 00:44:28 -040054static int acpi_ac_add(struct acpi_device *device);
55static int acpi_ac_remove(struct acpi_device *device, int type);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056static int acpi_ac_open_fs(struct inode *inode, struct file *file);
57
Thomas Renninger1ba90e32007-07-23 14:44:41 +020058const static struct acpi_device_id ac_device_ids[] = {
59 {"ACPI0003", 0},
60 {"", 0},
61};
62MODULE_DEVICE_TABLE(acpi, ac_device_ids);
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static struct acpi_driver acpi_ac_driver = {
Len Brownc2b67052007-02-12 23:33:40 -050065 .name = "ac",
Len Brown4be44fc2005-08-05 00:44:28 -040066 .class = ACPI_AC_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +020067 .ids = ac_device_ids,
Len Brown4be44fc2005-08-05 00:44:28 -040068 .ops = {
69 .add = acpi_ac_add,
70 .remove = acpi_ac_remove,
71 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070072};
73
74struct acpi_ac {
Patrick Mochelaf961792006-05-19 16:54:32 -040075 struct acpi_device * device;
Len Brown4be44fc2005-08-05 00:44:28 -040076 unsigned long state;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077};
78
Arjan van de Vend7508032006-07-04 13:06:00 -040079static const struct file_operations acpi_ac_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -040080 .open = acpi_ac_open_fs,
81 .read = seq_read,
82 .llseek = seq_lseek,
83 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -070084};
85
86/* --------------------------------------------------------------------------
87 AC Adapter Management
88 -------------------------------------------------------------------------- */
89
Len Brown4be44fc2005-08-05 00:44:28 -040090static int acpi_ac_get_state(struct acpi_ac *ac)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Len Brown4be44fc2005-08-05 00:44:28 -040092 acpi_status status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95 if (!ac)
Patrick Mocheld550d982006-06-27 00:41:40 -040096 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Patrick Mochela6ba5eb2006-05-19 16:54:41 -040098 status = acpi_evaluate_integer(ac->device->handle, "_PSR", NULL, &ac->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400100 ACPI_EXCEPTION((AE_INFO, status, "Error reading AC Adapter state"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 ac->state = ACPI_AC_STATUS_UNKNOWN;
Patrick Mocheld550d982006-06-27 00:41:40 -0400102 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 }
Len Brown4be44fc2005-08-05 00:44:28 -0400104
Patrick Mocheld550d982006-06-27 00:41:40 -0400105 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108/* --------------------------------------------------------------------------
109 FS Interface (/proc)
110 -------------------------------------------------------------------------- */
111
Len Brown4be44fc2005-08-05 00:44:28 -0400112static struct proc_dir_entry *acpi_ac_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114static int acpi_ac_seq_show(struct seq_file *seq, void *offset)
115{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200116 struct acpi_ac *ac = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119 if (!ac)
Patrick Mocheld550d982006-06-27 00:41:40 -0400120 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 if (acpi_ac_get_state(ac)) {
123 seq_puts(seq, "ERROR: Unable to read AC Adapter state\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400124 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 }
126
127 seq_puts(seq, "state: ");
128 switch (ac->state) {
129 case ACPI_AC_STATUS_OFFLINE:
130 seq_puts(seq, "off-line\n");
131 break;
132 case ACPI_AC_STATUS_ONLINE:
133 seq_puts(seq, "on-line\n");
134 break;
135 default:
136 seq_puts(seq, "unknown\n");
137 break;
138 }
139
Patrick Mocheld550d982006-06-27 00:41:40 -0400140 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
Len Brown4be44fc2005-08-05 00:44:28 -0400142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143static int acpi_ac_open_fs(struct inode *inode, struct file *file)
144{
145 return single_open(file, acpi_ac_seq_show, PDE(inode)->data);
146}
147
Len Brown4be44fc2005-08-05 00:44:28 -0400148static int acpi_ac_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
Len Brown4be44fc2005-08-05 00:44:28 -0400150 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153 if (!acpi_device_dir(device)) {
154 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown4be44fc2005-08-05 00:44:28 -0400155 acpi_ac_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400157 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 acpi_device_dir(device)->owner = THIS_MODULE;
159 }
160
161 /* 'state' [R] */
162 entry = create_proc_entry(ACPI_AC_FILE_STATE,
Len Brown4be44fc2005-08-05 00:44:28 -0400163 S_IRUGO, acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400165 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 else {
167 entry->proc_fops = &acpi_ac_fops;
168 entry->data = acpi_driver_data(device);
169 entry->owner = THIS_MODULE;
170 }
171
Patrick Mocheld550d982006-06-27 00:41:40 -0400172 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
Len Brown4be44fc2005-08-05 00:44:28 -0400175static int acpi_ac_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178 if (acpi_device_dir(device)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400179 remove_proc_entry(ACPI_AC_FILE_STATE, acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181 remove_proc_entry(acpi_device_bid(device), acpi_ac_dir);
182 acpi_device_dir(device) = NULL;
183 }
184
Patrick Mocheld550d982006-06-27 00:41:40 -0400185 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188/* --------------------------------------------------------------------------
189 Driver Model
190 -------------------------------------------------------------------------- */
191
Len Brown4be44fc2005-08-05 00:44:28 -0400192static void acpi_ac_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200194 struct acpi_ac *ac = data;
Len Brown4be44fc2005-08-05 00:44:28 -0400195 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198 if (!ac)
Patrick Mocheld550d982006-06-27 00:41:40 -0400199 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Patrick Mochelaf961792006-05-19 16:54:32 -0400201 device = ac->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 switch (event) {
203 case ACPI_AC_NOTIFY_STATUS:
Christian Lupien03d782522004-08-19 01:26:00 -0400204 case ACPI_NOTIFY_BUS_CHECK:
205 case ACPI_NOTIFY_DEVICE_CHECK:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 acpi_ac_get_state(ac);
207 acpi_bus_generate_event(device, event, (u32) ac->state);
208 break;
209 default:
210 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400211 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 break;
213 }
214
Patrick Mocheld550d982006-06-27 00:41:40 -0400215 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216}
217
Len Brown4be44fc2005-08-05 00:44:28 -0400218static int acpi_ac_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
Len Brown4be44fc2005-08-05 00:44:28 -0400220 int result = 0;
221 acpi_status status = AE_OK;
222 struct acpi_ac *ac = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400226 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Burman Yan36bcbec2006-12-19 12:56:11 -0800228 ac = kzalloc(sizeof(struct acpi_ac), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 if (!ac)
Patrick Mocheld550d982006-06-27 00:41:40 -0400230 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Patrick Mochelaf961792006-05-19 16:54:32 -0400232 ac->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 strcpy(acpi_device_name(device), ACPI_AC_DEVICE_NAME);
234 strcpy(acpi_device_class(device), ACPI_AC_CLASS);
235 acpi_driver_data(device) = ac;
236
237 result = acpi_ac_get_state(ac);
238 if (result)
239 goto end;
240
241 result = acpi_ac_add_fs(device);
242 if (result)
243 goto end;
244
Patrick Mochela6ba5eb2006-05-19 16:54:41 -0400245 status = acpi_install_notify_handler(device->handle,
Christian Lupien03d782522004-08-19 01:26:00 -0400246 ACPI_ALL_NOTIFY, acpi_ac_notify,
Len Brown4be44fc2005-08-05 00:44:28 -0400247 ac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 result = -ENODEV;
250 goto end;
251 }
252
Len Brown4be44fc2005-08-05 00:44:28 -0400253 printk(KERN_INFO PREFIX "%s [%s] (%s)\n",
254 acpi_device_name(device), acpi_device_bid(device),
255 ac->state ? "on-line" : "off-line");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Len Brown4be44fc2005-08-05 00:44:28 -0400257 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 if (result) {
259 acpi_ac_remove_fs(device);
260 kfree(ac);
261 }
262
Patrick Mocheld550d982006-06-27 00:41:40 -0400263 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
265
Len Brown4be44fc2005-08-05 00:44:28 -0400266static int acpi_ac_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Len Brown4be44fc2005-08-05 00:44:28 -0400268 acpi_status status = AE_OK;
269 struct acpi_ac *ac = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400273 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200275 ac = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Patrick Mochela6ba5eb2006-05-19 16:54:41 -0400277 status = acpi_remove_notify_handler(device->handle,
Christian Lupien03d782522004-08-19 01:26:00 -0400278 ACPI_ALL_NOTIFY, acpi_ac_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 acpi_ac_remove_fs(device);
281
282 kfree(ac);
283
Patrick Mocheld550d982006-06-27 00:41:40 -0400284 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
Len Brown4be44fc2005-08-05 00:44:28 -0400287static int __init acpi_ac_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
Rich Townsend3f86b832006-07-01 11:36:54 -0400289 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Pavel Machek4d8316d2006-08-14 22:37:22 -0700291 if (acpi_disabled)
292 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Rich Townsend3f86b832006-07-01 11:36:54 -0400294 acpi_ac_dir = acpi_lock_ac_dir();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 if (!acpi_ac_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -0400296 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298 result = acpi_bus_register_driver(&acpi_ac_driver);
299 if (result < 0) {
Rich Townsend3f86b832006-07-01 11:36:54 -0400300 acpi_unlock_ac_dir(acpi_ac_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -0400301 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 }
303
Patrick Mocheld550d982006-06-27 00:41:40 -0400304 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305}
306
Len Brown4be44fc2005-08-05 00:44:28 -0400307static void __exit acpi_ac_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310 acpi_bus_unregister_driver(&acpi_ac_driver);
311
Rich Townsend3f86b832006-07-01 11:36:54 -0400312 acpi_unlock_ac_dir(acpi_ac_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Patrick Mocheld550d982006-06-27 00:41:40 -0400314 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315}
316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317module_init(acpi_ac_init);
318module_exit(acpi_ac_exit);