]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * acpi_ac.c - ACPI AC Adapter Driver ($Revision: 27 $) | |
3 | * | |
4 | * Copyright (C) 2001, 2002 Andy Grover <[email protected]> | |
5 | * Copyright (C) 2001, 2002 Paul Diefenbaugh <[email protected]> | |
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 | ||
1da177e4 LT |
35 | #define ACPI_AC_COMPONENT 0x00020000 |
36 | #define ACPI_AC_CLASS "ac_adapter" | |
1da177e4 LT |
37 | #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 | |
f52fd66d | 45 | ACPI_MODULE_NAME("ac"); |
1da177e4 | 46 | |
f52fd66d | 47 | MODULE_AUTHOR("Paul Diefenbaugh"); |
7cda93e0 | 48 | MODULE_DESCRIPTION("ACPI AC Adapter Driver"); |
1da177e4 LT |
49 | MODULE_LICENSE("GPL"); |
50 | ||
3f86b832 RT |
51 | extern struct proc_dir_entry *acpi_lock_ac_dir(void); |
52 | extern void *acpi_unlock_ac_dir(struct proc_dir_entry *acpi_ac_dir); | |
53 | ||
4be44fcd LB |
54 | static int acpi_ac_add(struct acpi_device *device); |
55 | static int acpi_ac_remove(struct acpi_device *device, int type); | |
1da177e4 LT |
56 | static int acpi_ac_open_fs(struct inode *inode, struct file *file); |
57 | ||
1ba90e3a TR |
58 | const static struct acpi_device_id ac_device_ids[] = { |
59 | {"ACPI0003", 0}, | |
60 | {"", 0}, | |
61 | }; | |
62 | MODULE_DEVICE_TABLE(acpi, ac_device_ids); | |
63 | ||
1da177e4 | 64 | static struct acpi_driver acpi_ac_driver = { |
c2b6705b | 65 | .name = "ac", |
4be44fcd | 66 | .class = ACPI_AC_CLASS, |
1ba90e3a | 67 | .ids = ac_device_ids, |
4be44fcd LB |
68 | .ops = { |
69 | .add = acpi_ac_add, | |
70 | .remove = acpi_ac_remove, | |
71 | }, | |
1da177e4 LT |
72 | }; |
73 | ||
74 | struct acpi_ac { | |
af96179a | 75 | struct acpi_device * device; |
4be44fcd | 76 | unsigned long state; |
1da177e4 LT |
77 | }; |
78 | ||
d7508032 | 79 | static const struct file_operations acpi_ac_fops = { |
4be44fcd LB |
80 | .open = acpi_ac_open_fs, |
81 | .read = seq_read, | |
82 | .llseek = seq_lseek, | |
83 | .release = single_release, | |
1da177e4 LT |
84 | }; |
85 | ||
86 | /* -------------------------------------------------------------------------- | |
87 | AC Adapter Management | |
88 | -------------------------------------------------------------------------- */ | |
89 | ||
4be44fcd | 90 | static int acpi_ac_get_state(struct acpi_ac *ac) |
1da177e4 | 91 | { |
4be44fcd | 92 | acpi_status status = AE_OK; |
1da177e4 | 93 | |
1da177e4 LT |
94 | |
95 | if (!ac) | |
d550d98d | 96 | return -EINVAL; |
1da177e4 | 97 | |
a6ba5ebe | 98 | status = acpi_evaluate_integer(ac->device->handle, "_PSR", NULL, &ac->state); |
1da177e4 | 99 | if (ACPI_FAILURE(status)) { |
a6fc6720 | 100 | ACPI_EXCEPTION((AE_INFO, status, "Error reading AC Adapter state")); |
1da177e4 | 101 | ac->state = ACPI_AC_STATUS_UNKNOWN; |
d550d98d | 102 | return -ENODEV; |
1da177e4 | 103 | } |
4be44fcd | 104 | |
d550d98d | 105 | return 0; |
1da177e4 LT |
106 | } |
107 | ||
1da177e4 LT |
108 | /* -------------------------------------------------------------------------- |
109 | FS Interface (/proc) | |
110 | -------------------------------------------------------------------------- */ | |
111 | ||
4be44fcd | 112 | static struct proc_dir_entry *acpi_ac_dir; |
1da177e4 LT |
113 | |
114 | static int acpi_ac_seq_show(struct seq_file *seq, void *offset) | |
115 | { | |
50dd0969 | 116 | struct acpi_ac *ac = seq->private; |
1da177e4 | 117 | |
1da177e4 LT |
118 | |
119 | if (!ac) | |
d550d98d | 120 | return 0; |
1da177e4 LT |
121 | |
122 | if (acpi_ac_get_state(ac)) { | |
123 | seq_puts(seq, "ERROR: Unable to read AC Adapter state\n"); | |
d550d98d | 124 | return 0; |
1da177e4 LT |
125 | } |
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 | ||
d550d98d | 140 | return 0; |
1da177e4 | 141 | } |
4be44fcd | 142 | |
1da177e4 LT |
143 | static 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 | ||
4be44fcd | 148 | static int acpi_ac_add_fs(struct acpi_device *device) |
1da177e4 | 149 | { |
4be44fcd | 150 | struct proc_dir_entry *entry = NULL; |
1da177e4 | 151 | |
1da177e4 LT |
152 | |
153 | if (!acpi_device_dir(device)) { | |
154 | acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), | |
4be44fcd | 155 | acpi_ac_dir); |
1da177e4 | 156 | if (!acpi_device_dir(device)) |
d550d98d | 157 | return -ENODEV; |
1da177e4 LT |
158 | acpi_device_dir(device)->owner = THIS_MODULE; |
159 | } | |
160 | ||
161 | /* 'state' [R] */ | |
162 | entry = create_proc_entry(ACPI_AC_FILE_STATE, | |
4be44fcd | 163 | S_IRUGO, acpi_device_dir(device)); |
1da177e4 | 164 | if (!entry) |
d550d98d | 165 | return -ENODEV; |
1da177e4 LT |
166 | else { |
167 | entry->proc_fops = &acpi_ac_fops; | |
168 | entry->data = acpi_driver_data(device); | |
169 | entry->owner = THIS_MODULE; | |
170 | } | |
171 | ||
d550d98d | 172 | return 0; |
1da177e4 LT |
173 | } |
174 | ||
4be44fcd | 175 | static int acpi_ac_remove_fs(struct acpi_device *device) |
1da177e4 | 176 | { |
1da177e4 LT |
177 | |
178 | if (acpi_device_dir(device)) { | |
4be44fcd | 179 | remove_proc_entry(ACPI_AC_FILE_STATE, acpi_device_dir(device)); |
1da177e4 LT |
180 | |
181 | remove_proc_entry(acpi_device_bid(device), acpi_ac_dir); | |
182 | acpi_device_dir(device) = NULL; | |
183 | } | |
184 | ||
d550d98d | 185 | return 0; |
1da177e4 LT |
186 | } |
187 | ||
1da177e4 LT |
188 | /* -------------------------------------------------------------------------- |
189 | Driver Model | |
190 | -------------------------------------------------------------------------- */ | |
191 | ||
4be44fcd | 192 | static void acpi_ac_notify(acpi_handle handle, u32 event, void *data) |
1da177e4 | 193 | { |
50dd0969 | 194 | struct acpi_ac *ac = data; |
4be44fcd | 195 | struct acpi_device *device = NULL; |
1da177e4 | 196 | |
1da177e4 LT |
197 | |
198 | if (!ac) | |
d550d98d | 199 | return; |
1da177e4 | 200 | |
af96179a | 201 | device = ac->device; |
1da177e4 LT |
202 | switch (event) { |
203 | case ACPI_AC_NOTIFY_STATUS: | |
03d78252 CL |
204 | case ACPI_NOTIFY_BUS_CHECK: |
205 | case ACPI_NOTIFY_DEVICE_CHECK: | |
1da177e4 LT |
206 | acpi_ac_get_state(ac); |
207 | acpi_bus_generate_event(device, event, (u32) ac->state); | |
962ce8ca ZR |
208 | acpi_bus_generate_netlink_event(device->pnp.device_class, |
209 | device->dev.bus_id, event, | |
210 | (u32) ac->state); | |
1da177e4 LT |
211 | break; |
212 | default: | |
213 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, | |
4be44fcd | 214 | "Unsupported event [0x%x]\n", event)); |
1da177e4 LT |
215 | break; |
216 | } | |
217 | ||
d550d98d | 218 | return; |
1da177e4 LT |
219 | } |
220 | ||
4be44fcd | 221 | static int acpi_ac_add(struct acpi_device *device) |
1da177e4 | 222 | { |
4be44fcd LB |
223 | int result = 0; |
224 | acpi_status status = AE_OK; | |
225 | struct acpi_ac *ac = NULL; | |
1da177e4 | 226 | |
1da177e4 LT |
227 | |
228 | if (!device) | |
d550d98d | 229 | return -EINVAL; |
1da177e4 | 230 | |
36bcbec7 | 231 | ac = kzalloc(sizeof(struct acpi_ac), GFP_KERNEL); |
1da177e4 | 232 | if (!ac) |
d550d98d | 233 | return -ENOMEM; |
1da177e4 | 234 | |
af96179a | 235 | ac->device = device; |
1da177e4 LT |
236 | strcpy(acpi_device_name(device), ACPI_AC_DEVICE_NAME); |
237 | strcpy(acpi_device_class(device), ACPI_AC_CLASS); | |
238 | acpi_driver_data(device) = ac; | |
239 | ||
240 | result = acpi_ac_get_state(ac); | |
241 | if (result) | |
242 | goto end; | |
243 | ||
244 | result = acpi_ac_add_fs(device); | |
245 | if (result) | |
246 | goto end; | |
247 | ||
a6ba5ebe | 248 | status = acpi_install_notify_handler(device->handle, |
03d78252 | 249 | ACPI_ALL_NOTIFY, acpi_ac_notify, |
4be44fcd | 250 | ac); |
1da177e4 | 251 | if (ACPI_FAILURE(status)) { |
1da177e4 LT |
252 | result = -ENODEV; |
253 | goto end; | |
254 | } | |
255 | ||
4be44fcd LB |
256 | printk(KERN_INFO PREFIX "%s [%s] (%s)\n", |
257 | acpi_device_name(device), acpi_device_bid(device), | |
258 | ac->state ? "on-line" : "off-line"); | |
1da177e4 | 259 | |
4be44fcd | 260 | end: |
1da177e4 LT |
261 | if (result) { |
262 | acpi_ac_remove_fs(device); | |
263 | kfree(ac); | |
264 | } | |
265 | ||
d550d98d | 266 | return result; |
1da177e4 LT |
267 | } |
268 | ||
4be44fcd | 269 | static int acpi_ac_remove(struct acpi_device *device, int type) |
1da177e4 | 270 | { |
4be44fcd LB |
271 | acpi_status status = AE_OK; |
272 | struct acpi_ac *ac = NULL; | |
1da177e4 | 273 | |
1da177e4 LT |
274 | |
275 | if (!device || !acpi_driver_data(device)) | |
d550d98d | 276 | return -EINVAL; |
1da177e4 | 277 | |
50dd0969 | 278 | ac = acpi_driver_data(device); |
1da177e4 | 279 | |
a6ba5ebe | 280 | status = acpi_remove_notify_handler(device->handle, |
03d78252 | 281 | ACPI_ALL_NOTIFY, acpi_ac_notify); |
1da177e4 LT |
282 | |
283 | acpi_ac_remove_fs(device); | |
284 | ||
285 | kfree(ac); | |
286 | ||
d550d98d | 287 | return 0; |
1da177e4 LT |
288 | } |
289 | ||
4be44fcd | 290 | static int __init acpi_ac_init(void) |
1da177e4 | 291 | { |
3f86b832 | 292 | int result; |
1da177e4 | 293 | |
4d8316d5 PM |
294 | if (acpi_disabled) |
295 | return -ENODEV; | |
1da177e4 | 296 | |
3f86b832 | 297 | acpi_ac_dir = acpi_lock_ac_dir(); |
1da177e4 | 298 | if (!acpi_ac_dir) |
d550d98d | 299 | return -ENODEV; |
1da177e4 LT |
300 | |
301 | result = acpi_bus_register_driver(&acpi_ac_driver); | |
302 | if (result < 0) { | |
3f86b832 | 303 | acpi_unlock_ac_dir(acpi_ac_dir); |
d550d98d | 304 | return -ENODEV; |
1da177e4 LT |
305 | } |
306 | ||
d550d98d | 307 | return 0; |
1da177e4 LT |
308 | } |
309 | ||
4be44fcd | 310 | static void __exit acpi_ac_exit(void) |
1da177e4 | 311 | { |
1da177e4 LT |
312 | |
313 | acpi_bus_unregister_driver(&acpi_ac_driver); | |
314 | ||
3f86b832 | 315 | acpi_unlock_ac_dir(acpi_ac_dir); |
1da177e4 | 316 | |
d550d98d | 317 | return; |
1da177e4 LT |
318 | } |
319 | ||
1da177e4 LT |
320 | module_init(acpi_ac_init); |
321 | module_exit(acpi_ac_exit); |