]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
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 | ||
1da177e4 LT |
35 | #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 | |
4be44fcd | 47 | ACPI_MODULE_NAME("acpi_ac") |
1da177e4 | 48 | |
4be44fcd | 49 | MODULE_AUTHOR("Paul Diefenbaugh"); |
1da177e4 LT |
50 | MODULE_DESCRIPTION(ACPI_AC_DRIVER_NAME); |
51 | MODULE_LICENSE("GPL"); | |
52 | ||
4be44fcd LB |
53 | static int acpi_ac_add(struct acpi_device *device); |
54 | static int acpi_ac_remove(struct acpi_device *device, int type); | |
1da177e4 LT |
55 | static int acpi_ac_open_fs(struct inode *inode, struct file *file); |
56 | ||
57 | static struct acpi_driver acpi_ac_driver = { | |
4be44fcd LB |
58 | .name = ACPI_AC_DRIVER_NAME, |
59 | .class = ACPI_AC_CLASS, | |
60 | .ids = ACPI_AC_HID, | |
61 | .ops = { | |
62 | .add = acpi_ac_add, | |
63 | .remove = acpi_ac_remove, | |
64 | }, | |
1da177e4 LT |
65 | }; |
66 | ||
67 | struct acpi_ac { | |
4be44fcd LB |
68 | acpi_handle handle; |
69 | unsigned long state; | |
1da177e4 LT |
70 | }; |
71 | ||
72 | static struct file_operations acpi_ac_fops = { | |
4be44fcd LB |
73 | .open = acpi_ac_open_fs, |
74 | .read = seq_read, | |
75 | .llseek = seq_lseek, | |
76 | .release = single_release, | |
1da177e4 LT |
77 | }; |
78 | ||
79 | /* -------------------------------------------------------------------------- | |
80 | AC Adapter Management | |
81 | -------------------------------------------------------------------------- */ | |
82 | ||
4be44fcd | 83 | static int acpi_ac_get_state(struct acpi_ac *ac) |
1da177e4 | 84 | { |
4be44fcd | 85 | acpi_status status = AE_OK; |
1da177e4 LT |
86 | |
87 | ACPI_FUNCTION_TRACE("acpi_ac_get_state"); | |
88 | ||
89 | if (!ac) | |
90 | return_VALUE(-EINVAL); | |
91 | ||
92 | status = acpi_evaluate_integer(ac->handle, "_PSR", NULL, &ac->state); | |
93 | if (ACPI_FAILURE(status)) { | |
a6fc6720 | 94 | ACPI_EXCEPTION((AE_INFO, status, "Error reading AC Adapter state")); |
1da177e4 LT |
95 | ac->state = ACPI_AC_STATUS_UNKNOWN; |
96 | return_VALUE(-ENODEV); | |
97 | } | |
4be44fcd | 98 | |
1da177e4 LT |
99 | return_VALUE(0); |
100 | } | |
101 | ||
1da177e4 LT |
102 | /* -------------------------------------------------------------------------- |
103 | FS Interface (/proc) | |
104 | -------------------------------------------------------------------------- */ | |
105 | ||
4be44fcd | 106 | static struct proc_dir_entry *acpi_ac_dir; |
1da177e4 LT |
107 | |
108 | static int acpi_ac_seq_show(struct seq_file *seq, void *offset) | |
109 | { | |
4be44fcd | 110 | struct acpi_ac *ac = (struct acpi_ac *)seq->private; |
1da177e4 LT |
111 | |
112 | ACPI_FUNCTION_TRACE("acpi_ac_seq_show"); | |
113 | ||
114 | if (!ac) | |
115 | return_VALUE(0); | |
116 | ||
117 | if (acpi_ac_get_state(ac)) { | |
118 | seq_puts(seq, "ERROR: Unable to read AC Adapter state\n"); | |
119 | return_VALUE(0); | |
120 | } | |
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 | ||
135 | return_VALUE(0); | |
136 | } | |
4be44fcd | 137 | |
1da177e4 LT |
138 | static 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 | ||
4be44fcd | 143 | static int acpi_ac_add_fs(struct acpi_device *device) |
1da177e4 | 144 | { |
4be44fcd | 145 | struct proc_dir_entry *entry = NULL; |
1da177e4 LT |
146 | |
147 | ACPI_FUNCTION_TRACE("acpi_ac_add_fs"); | |
148 | ||
149 | if (!acpi_device_dir(device)) { | |
150 | acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), | |
4be44fcd | 151 | acpi_ac_dir); |
1da177e4 LT |
152 | if (!acpi_device_dir(device)) |
153 | return_VALUE(-ENODEV); | |
154 | acpi_device_dir(device)->owner = THIS_MODULE; | |
155 | } | |
156 | ||
157 | /* 'state' [R] */ | |
158 | entry = create_proc_entry(ACPI_AC_FILE_STATE, | |
4be44fcd | 159 | S_IRUGO, acpi_device_dir(device)); |
1da177e4 | 160 | if (!entry) |
a6fc6720 | 161 | return_VALUE(-ENODEV); |
1da177e4 LT |
162 | else { |
163 | entry->proc_fops = &acpi_ac_fops; | |
164 | entry->data = acpi_driver_data(device); | |
165 | entry->owner = THIS_MODULE; | |
166 | } | |
167 | ||
168 | return_VALUE(0); | |
169 | } | |
170 | ||
4be44fcd | 171 | static int acpi_ac_remove_fs(struct acpi_device *device) |
1da177e4 LT |
172 | { |
173 | ACPI_FUNCTION_TRACE("acpi_ac_remove_fs"); | |
174 | ||
175 | if (acpi_device_dir(device)) { | |
4be44fcd | 176 | remove_proc_entry(ACPI_AC_FILE_STATE, acpi_device_dir(device)); |
1da177e4 LT |
177 | |
178 | remove_proc_entry(acpi_device_bid(device), acpi_ac_dir); | |
179 | acpi_device_dir(device) = NULL; | |
180 | } | |
181 | ||
182 | return_VALUE(0); | |
183 | } | |
184 | ||
1da177e4 LT |
185 | /* -------------------------------------------------------------------------- |
186 | Driver Model | |
187 | -------------------------------------------------------------------------- */ | |
188 | ||
4be44fcd | 189 | static void acpi_ac_notify(acpi_handle handle, u32 event, void *data) |
1da177e4 | 190 | { |
4be44fcd LB |
191 | struct acpi_ac *ac = (struct acpi_ac *)data; |
192 | struct acpi_device *device = NULL; | |
1da177e4 LT |
193 | |
194 | ACPI_FUNCTION_TRACE("acpi_ac_notify"); | |
195 | ||
196 | if (!ac) | |
197 | return_VOID; | |
198 | ||
199 | if (acpi_bus_get_device(ac->handle, &device)) | |
200 | return_VOID; | |
201 | ||
202 | switch (event) { | |
203 | case ACPI_AC_NOTIFY_STATUS: | |
204 | acpi_ac_get_state(ac); | |
205 | acpi_bus_generate_event(device, event, (u32) ac->state); | |
206 | break; | |
207 | default: | |
208 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, | |
4be44fcd | 209 | "Unsupported event [0x%x]\n", event)); |
1da177e4 LT |
210 | break; |
211 | } | |
212 | ||
213 | return_VOID; | |
214 | } | |
215 | ||
4be44fcd | 216 | static int acpi_ac_add(struct acpi_device *device) |
1da177e4 | 217 | { |
4be44fcd LB |
218 | int result = 0; |
219 | acpi_status status = AE_OK; | |
220 | struct acpi_ac *ac = NULL; | |
1da177e4 LT |
221 | |
222 | ACPI_FUNCTION_TRACE("acpi_ac_add"); | |
223 | ||
224 | if (!device) | |
225 | return_VALUE(-EINVAL); | |
226 | ||
227 | ac = kmalloc(sizeof(struct acpi_ac), GFP_KERNEL); | |
228 | if (!ac) | |
229 | return_VALUE(-ENOMEM); | |
230 | memset(ac, 0, sizeof(struct acpi_ac)); | |
231 | ||
232 | ac->handle = device->handle; | |
233 | 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 | ||
245 | status = acpi_install_notify_handler(ac->handle, | |
4be44fcd LB |
246 | ACPI_DEVICE_NOTIFY, acpi_ac_notify, |
247 | ac); | |
1da177e4 | 248 | if (ACPI_FAILURE(status)) { |
1da177e4 LT |
249 | result = -ENODEV; |
250 | goto end; | |
251 | } | |
252 | ||
4be44fcd LB |
253 | printk(KERN_INFO PREFIX "%s [%s] (%s)\n", |
254 | acpi_device_name(device), acpi_device_bid(device), | |
255 | ac->state ? "on-line" : "off-line"); | |
1da177e4 | 256 | |
4be44fcd | 257 | end: |
1da177e4 LT |
258 | if (result) { |
259 | acpi_ac_remove_fs(device); | |
260 | kfree(ac); | |
261 | } | |
262 | ||
263 | return_VALUE(result); | |
264 | } | |
265 | ||
4be44fcd | 266 | static int acpi_ac_remove(struct acpi_device *device, int type) |
1da177e4 | 267 | { |
4be44fcd LB |
268 | acpi_status status = AE_OK; |
269 | struct acpi_ac *ac = NULL; | |
1da177e4 LT |
270 | |
271 | ACPI_FUNCTION_TRACE("acpi_ac_remove"); | |
272 | ||
273 | if (!device || !acpi_driver_data(device)) | |
274 | return_VALUE(-EINVAL); | |
275 | ||
4be44fcd | 276 | ac = (struct acpi_ac *)acpi_driver_data(device); |
1da177e4 LT |
277 | |
278 | status = acpi_remove_notify_handler(ac->handle, | |
4be44fcd | 279 | ACPI_DEVICE_NOTIFY, acpi_ac_notify); |
1da177e4 LT |
280 | |
281 | acpi_ac_remove_fs(device); | |
282 | ||
283 | kfree(ac); | |
284 | ||
285 | return_VALUE(0); | |
286 | } | |
287 | ||
4be44fcd | 288 | static int __init acpi_ac_init(void) |
1da177e4 | 289 | { |
4be44fcd | 290 | int result = 0; |
1da177e4 LT |
291 | |
292 | ACPI_FUNCTION_TRACE("acpi_ac_init"); | |
293 | ||
294 | acpi_ac_dir = proc_mkdir(ACPI_AC_CLASS, acpi_root_dir); | |
295 | if (!acpi_ac_dir) | |
296 | return_VALUE(-ENODEV); | |
297 | acpi_ac_dir->owner = THIS_MODULE; | |
298 | ||
299 | result = acpi_bus_register_driver(&acpi_ac_driver); | |
300 | if (result < 0) { | |
301 | remove_proc_entry(ACPI_AC_CLASS, acpi_root_dir); | |
302 | return_VALUE(-ENODEV); | |
303 | } | |
304 | ||
305 | return_VALUE(0); | |
306 | } | |
307 | ||
4be44fcd | 308 | static void __exit acpi_ac_exit(void) |
1da177e4 LT |
309 | { |
310 | ACPI_FUNCTION_TRACE("acpi_ac_exit"); | |
311 | ||
312 | acpi_bus_unregister_driver(&acpi_ac_driver); | |
313 | ||
314 | remove_proc_entry(ACPI_AC_CLASS, acpi_root_dir); | |
315 | ||
316 | return_VOID; | |
317 | } | |
318 | ||
1da177e4 LT |
319 | module_init(acpi_ac_init); |
320 | module_exit(acpi_ac_exit); |