]>
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> | |
5a0e3ad6 | 28 | #include <linux/slab.h> |
1da177e4 LT |
29 | #include <linux/init.h> |
30 | #include <linux/types.h> | |
fdcedbba | 31 | #ifdef CONFIG_ACPI_PROCFS_POWER |
1da177e4 LT |
32 | #include <linux/proc_fs.h> |
33 | #include <linux/seq_file.h> | |
8a246ee4 | 34 | #endif |
d5b4a3d0 | 35 | #include <linux/power_supply.h> |
1da177e4 LT |
36 | #include <acpi/acpi_bus.h> |
37 | #include <acpi/acpi_drivers.h> | |
38 | ||
a192a958 LB |
39 | #define PREFIX "ACPI: " |
40 | ||
1da177e4 | 41 | #define ACPI_AC_CLASS "ac_adapter" |
1da177e4 LT |
42 | #define ACPI_AC_DEVICE_NAME "AC Adapter" |
43 | #define ACPI_AC_FILE_STATE "state" | |
44 | #define ACPI_AC_NOTIFY_STATUS 0x80 | |
45 | #define ACPI_AC_STATUS_OFFLINE 0x00 | |
46 | #define ACPI_AC_STATUS_ONLINE 0x01 | |
47 | #define ACPI_AC_STATUS_UNKNOWN 0xFF | |
48 | ||
49 | #define _COMPONENT ACPI_AC_COMPONENT | |
f52fd66d | 50 | ACPI_MODULE_NAME("ac"); |
1da177e4 | 51 | |
f52fd66d | 52 | MODULE_AUTHOR("Paul Diefenbaugh"); |
7cda93e0 | 53 | MODULE_DESCRIPTION("ACPI AC Adapter Driver"); |
1da177e4 LT |
54 | MODULE_LICENSE("GPL"); |
55 | ||
fdcedbba | 56 | #ifdef CONFIG_ACPI_PROCFS_POWER |
3f86b832 RT |
57 | extern struct proc_dir_entry *acpi_lock_ac_dir(void); |
58 | extern void *acpi_unlock_ac_dir(struct proc_dir_entry *acpi_ac_dir); | |
8a246ee4 AB |
59 | static int acpi_ac_open_fs(struct inode *inode, struct file *file); |
60 | #endif | |
3f86b832 | 61 | |
4be44fcd LB |
62 | static int acpi_ac_add(struct acpi_device *device); |
63 | static int acpi_ac_remove(struct acpi_device *device, int type); | |
48fe1127 | 64 | static void acpi_ac_notify(struct acpi_device *device, u32 event); |
1da177e4 | 65 | |
b299c22c | 66 | static const struct acpi_device_id ac_device_ids[] = { |
1ba90e3a TR |
67 | {"ACPI0003", 0}, |
68 | {"", 0}, | |
69 | }; | |
70 | MODULE_DEVICE_TABLE(acpi, ac_device_ids); | |
71 | ||
ccda7069 RW |
72 | static int acpi_ac_resume(struct device *dev); |
73 | static SIMPLE_DEV_PM_OPS(acpi_ac_pm, NULL, acpi_ac_resume); | |
74 | ||
1da177e4 | 75 | static struct acpi_driver acpi_ac_driver = { |
c2b6705b | 76 | .name = "ac", |
4be44fcd | 77 | .class = ACPI_AC_CLASS, |
1ba90e3a | 78 | .ids = ac_device_ids, |
48fe1127 | 79 | .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS, |
4be44fcd LB |
80 | .ops = { |
81 | .add = acpi_ac_add, | |
82 | .remove = acpi_ac_remove, | |
48fe1127 | 83 | .notify = acpi_ac_notify, |
4be44fcd | 84 | }, |
ccda7069 | 85 | .drv.pm = &acpi_ac_pm, |
1da177e4 LT |
86 | }; |
87 | ||
88 | struct acpi_ac { | |
d5b4a3d0 | 89 | struct power_supply charger; |
af96179a | 90 | struct acpi_device * device; |
27663c58 | 91 | unsigned long long state; |
1da177e4 LT |
92 | }; |
93 | ||
497888cf | 94 | #define to_acpi_ac(x) container_of(x, struct acpi_ac, charger) |
d5b4a3d0 | 95 | |
fdcedbba | 96 | #ifdef CONFIG_ACPI_PROCFS_POWER |
d7508032 | 97 | static const struct file_operations acpi_ac_fops = { |
cf7acfab | 98 | .owner = THIS_MODULE, |
4be44fcd LB |
99 | .open = acpi_ac_open_fs, |
100 | .read = seq_read, | |
101 | .llseek = seq_lseek, | |
102 | .release = single_release, | |
1da177e4 | 103 | }; |
8a246ee4 | 104 | #endif |
d5b4a3d0 | 105 | |
1da177e4 LT |
106 | /* -------------------------------------------------------------------------- |
107 | AC Adapter Management | |
108 | -------------------------------------------------------------------------- */ | |
109 | ||
4be44fcd | 110 | static int acpi_ac_get_state(struct acpi_ac *ac) |
1da177e4 | 111 | { |
4be44fcd | 112 | acpi_status status = AE_OK; |
1da177e4 | 113 | |
1da177e4 LT |
114 | |
115 | if (!ac) | |
d550d98d | 116 | return -EINVAL; |
1da177e4 | 117 | |
a6ba5ebe | 118 | status = acpi_evaluate_integer(ac->device->handle, "_PSR", NULL, &ac->state); |
1da177e4 | 119 | if (ACPI_FAILURE(status)) { |
a6fc6720 | 120 | ACPI_EXCEPTION((AE_INFO, status, "Error reading AC Adapter state")); |
1da177e4 | 121 | ac->state = ACPI_AC_STATUS_UNKNOWN; |
d550d98d | 122 | return -ENODEV; |
1da177e4 | 123 | } |
4be44fcd | 124 | |
d550d98d | 125 | return 0; |
1da177e4 LT |
126 | } |
127 | ||
3151dbb0 ZR |
128 | /* -------------------------------------------------------------------------- |
129 | sysfs I/F | |
130 | -------------------------------------------------------------------------- */ | |
131 | static int get_ac_property(struct power_supply *psy, | |
132 | enum power_supply_property psp, | |
133 | union power_supply_propval *val) | |
134 | { | |
135 | struct acpi_ac *ac = to_acpi_ac(psy); | |
136 | ||
137 | if (!ac) | |
138 | return -ENODEV; | |
139 | ||
140 | if (acpi_ac_get_state(ac)) | |
141 | return -ENODEV; | |
142 | ||
143 | switch (psp) { | |
144 | case POWER_SUPPLY_PROP_ONLINE: | |
145 | val->intval = ac->state; | |
146 | break; | |
147 | default: | |
148 | return -EINVAL; | |
149 | } | |
150 | return 0; | |
151 | } | |
152 | ||
153 | static enum power_supply_property ac_props[] = { | |
154 | POWER_SUPPLY_PROP_ONLINE, | |
155 | }; | |
156 | ||
fdcedbba | 157 | #ifdef CONFIG_ACPI_PROCFS_POWER |
1da177e4 LT |
158 | /* -------------------------------------------------------------------------- |
159 | FS Interface (/proc) | |
160 | -------------------------------------------------------------------------- */ | |
161 | ||
4be44fcd | 162 | static struct proc_dir_entry *acpi_ac_dir; |
1da177e4 LT |
163 | |
164 | static int acpi_ac_seq_show(struct seq_file *seq, void *offset) | |
165 | { | |
50dd0969 | 166 | struct acpi_ac *ac = seq->private; |
1da177e4 | 167 | |
1da177e4 LT |
168 | |
169 | if (!ac) | |
d550d98d | 170 | return 0; |
1da177e4 LT |
171 | |
172 | if (acpi_ac_get_state(ac)) { | |
173 | seq_puts(seq, "ERROR: Unable to read AC Adapter state\n"); | |
d550d98d | 174 | return 0; |
1da177e4 LT |
175 | } |
176 | ||
177 | seq_puts(seq, "state: "); | |
178 | switch (ac->state) { | |
179 | case ACPI_AC_STATUS_OFFLINE: | |
180 | seq_puts(seq, "off-line\n"); | |
181 | break; | |
182 | case ACPI_AC_STATUS_ONLINE: | |
183 | seq_puts(seq, "on-line\n"); | |
184 | break; | |
185 | default: | |
186 | seq_puts(seq, "unknown\n"); | |
187 | break; | |
188 | } | |
189 | ||
d550d98d | 190 | return 0; |
1da177e4 | 191 | } |
4be44fcd | 192 | |
1da177e4 LT |
193 | static int acpi_ac_open_fs(struct inode *inode, struct file *file) |
194 | { | |
195 | return single_open(file, acpi_ac_seq_show, PDE(inode)->data); | |
196 | } | |
197 | ||
4be44fcd | 198 | static int acpi_ac_add_fs(struct acpi_device *device) |
1da177e4 | 199 | { |
4be44fcd | 200 | struct proc_dir_entry *entry = NULL; |
1da177e4 | 201 | |
6d855fcd ZR |
202 | printk(KERN_WARNING PREFIX "Deprecated procfs I/F for AC is loaded," |
203 | " please retry with CONFIG_ACPI_PROCFS_POWER cleared\n"); | |
1da177e4 LT |
204 | if (!acpi_device_dir(device)) { |
205 | acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), | |
4be44fcd | 206 | acpi_ac_dir); |
1da177e4 | 207 | if (!acpi_device_dir(device)) |
d550d98d | 208 | return -ENODEV; |
1da177e4 LT |
209 | } |
210 | ||
211 | /* 'state' [R] */ | |
cf7acfab DL |
212 | entry = proc_create_data(ACPI_AC_FILE_STATE, |
213 | S_IRUGO, acpi_device_dir(device), | |
214 | &acpi_ac_fops, acpi_driver_data(device)); | |
1da177e4 | 215 | if (!entry) |
d550d98d | 216 | return -ENODEV; |
d550d98d | 217 | return 0; |
1da177e4 LT |
218 | } |
219 | ||
4be44fcd | 220 | static int acpi_ac_remove_fs(struct acpi_device *device) |
1da177e4 | 221 | { |
1da177e4 LT |
222 | |
223 | if (acpi_device_dir(device)) { | |
4be44fcd | 224 | remove_proc_entry(ACPI_AC_FILE_STATE, acpi_device_dir(device)); |
1da177e4 LT |
225 | |
226 | remove_proc_entry(acpi_device_bid(device), acpi_ac_dir); | |
227 | acpi_device_dir(device) = NULL; | |
228 | } | |
229 | ||
d550d98d | 230 | return 0; |
1da177e4 | 231 | } |
8a246ee4 | 232 | #endif |
1da177e4 | 233 | |
1da177e4 LT |
234 | /* -------------------------------------------------------------------------- |
235 | Driver Model | |
236 | -------------------------------------------------------------------------- */ | |
237 | ||
48fe1127 | 238 | static void acpi_ac_notify(struct acpi_device *device, u32 event) |
1da177e4 | 239 | { |
48fe1127 | 240 | struct acpi_ac *ac = acpi_driver_data(device); |
1da177e4 | 241 | |
1da177e4 LT |
242 | |
243 | if (!ac) | |
d550d98d | 244 | return; |
1da177e4 | 245 | |
1da177e4 | 246 | switch (event) { |
f163ff51 LB |
247 | default: |
248 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, | |
249 | "Unsupported event [0x%x]\n", event)); | |
1da177e4 | 250 | case ACPI_AC_NOTIFY_STATUS: |
03d78252 CL |
251 | case ACPI_NOTIFY_BUS_CHECK: |
252 | case ACPI_NOTIFY_DEVICE_CHECK: | |
1da177e4 | 253 | acpi_ac_get_state(ac); |
14e04fb3 | 254 | acpi_bus_generate_proc_event(device, event, (u32) ac->state); |
962ce8ca | 255 | acpi_bus_generate_netlink_event(device->pnp.device_class, |
0794469d | 256 | dev_name(&device->dev), event, |
962ce8ca | 257 | (u32) ac->state); |
68b92b56 | 258 | acpi_notifier_call_chain(device, event, (u32) ac->state); |
d5b4a3d0 | 259 | kobject_uevent(&ac->charger.dev->kobj, KOBJ_CHANGE); |
1da177e4 LT |
260 | } |
261 | ||
d550d98d | 262 | return; |
1da177e4 LT |
263 | } |
264 | ||
4be44fcd | 265 | static int acpi_ac_add(struct acpi_device *device) |
1da177e4 | 266 | { |
4be44fcd | 267 | int result = 0; |
4be44fcd | 268 | struct acpi_ac *ac = NULL; |
1da177e4 | 269 | |
1da177e4 LT |
270 | |
271 | if (!device) | |
d550d98d | 272 | return -EINVAL; |
1da177e4 | 273 | |
36bcbec7 | 274 | ac = kzalloc(sizeof(struct acpi_ac), GFP_KERNEL); |
1da177e4 | 275 | if (!ac) |
d550d98d | 276 | return -ENOMEM; |
1da177e4 | 277 | |
af96179a | 278 | ac->device = device; |
1da177e4 LT |
279 | strcpy(acpi_device_name(device), ACPI_AC_DEVICE_NAME); |
280 | strcpy(acpi_device_class(device), ACPI_AC_CLASS); | |
db89b4f0 | 281 | device->driver_data = ac; |
1da177e4 LT |
282 | |
283 | result = acpi_ac_get_state(ac); | |
284 | if (result) | |
285 | goto end; | |
286 | ||
fdcedbba | 287 | #ifdef CONFIG_ACPI_PROCFS_POWER |
1da177e4 | 288 | result = acpi_ac_add_fs(device); |
8a246ee4 | 289 | #endif |
1da177e4 LT |
290 | if (result) |
291 | goto end; | |
d5b4a3d0 AS |
292 | ac->charger.name = acpi_device_bid(device); |
293 | ac->charger.type = POWER_SUPPLY_TYPE_MAINS; | |
294 | ac->charger.properties = ac_props; | |
295 | ac->charger.num_properties = ARRAY_SIZE(ac_props); | |
296 | ac->charger.get_property = get_ac_property; | |
f197ac13 LT |
297 | result = power_supply_register(&ac->device->dev, &ac->charger); |
298 | if (result) | |
299 | goto end; | |
1da177e4 | 300 | |
4be44fcd LB |
301 | printk(KERN_INFO PREFIX "%s [%s] (%s)\n", |
302 | acpi_device_name(device), acpi_device_bid(device), | |
303 | ac->state ? "on-line" : "off-line"); | |
1da177e4 | 304 | |
4be44fcd | 305 | end: |
1da177e4 | 306 | if (result) { |
fdcedbba | 307 | #ifdef CONFIG_ACPI_PROCFS_POWER |
1da177e4 | 308 | acpi_ac_remove_fs(device); |
8a246ee4 | 309 | #endif |
1da177e4 LT |
310 | kfree(ac); |
311 | } | |
312 | ||
d550d98d | 313 | return result; |
1da177e4 LT |
314 | } |
315 | ||
ccda7069 | 316 | static int acpi_ac_resume(struct device *dev) |
5bfeca31 AS |
317 | { |
318 | struct acpi_ac *ac; | |
319 | unsigned old_state; | |
ccda7069 RW |
320 | |
321 | if (!dev) | |
322 | return -EINVAL; | |
323 | ||
324 | ac = acpi_driver_data(to_acpi_device(dev)); | |
325 | if (!ac) | |
5bfeca31 | 326 | return -EINVAL; |
ccda7069 | 327 | |
5bfeca31 AS |
328 | old_state = ac->state; |
329 | if (acpi_ac_get_state(ac)) | |
330 | return 0; | |
331 | if (old_state != ac->state) | |
332 | kobject_uevent(&ac->charger.dev->kobj, KOBJ_CHANGE); | |
333 | return 0; | |
334 | } | |
335 | ||
4be44fcd | 336 | static int acpi_ac_remove(struct acpi_device *device, int type) |
1da177e4 | 337 | { |
4be44fcd | 338 | struct acpi_ac *ac = NULL; |
1da177e4 | 339 | |
1da177e4 LT |
340 | |
341 | if (!device || !acpi_driver_data(device)) | |
d550d98d | 342 | return -EINVAL; |
1da177e4 | 343 | |
50dd0969 | 344 | ac = acpi_driver_data(device); |
1da177e4 | 345 | |
d5b4a3d0 AS |
346 | if (ac->charger.dev) |
347 | power_supply_unregister(&ac->charger); | |
fdcedbba | 348 | #ifdef CONFIG_ACPI_PROCFS_POWER |
1da177e4 | 349 | acpi_ac_remove_fs(device); |
8a246ee4 | 350 | #endif |
1da177e4 LT |
351 | |
352 | kfree(ac); | |
353 | ||
d550d98d | 354 | return 0; |
1da177e4 LT |
355 | } |
356 | ||
4be44fcd | 357 | static int __init acpi_ac_init(void) |
1da177e4 | 358 | { |
3f86b832 | 359 | int result; |
1da177e4 | 360 | |
4d8316d5 PM |
361 | if (acpi_disabled) |
362 | return -ENODEV; | |
1da177e4 | 363 | |
fdcedbba | 364 | #ifdef CONFIG_ACPI_PROCFS_POWER |
3f86b832 | 365 | acpi_ac_dir = acpi_lock_ac_dir(); |
1da177e4 | 366 | if (!acpi_ac_dir) |
d550d98d | 367 | return -ENODEV; |
8a246ee4 | 368 | #endif |
1da177e4 LT |
369 | |
370 | result = acpi_bus_register_driver(&acpi_ac_driver); | |
371 | if (result < 0) { | |
fdcedbba | 372 | #ifdef CONFIG_ACPI_PROCFS_POWER |
3f86b832 | 373 | acpi_unlock_ac_dir(acpi_ac_dir); |
8a246ee4 | 374 | #endif |
d550d98d | 375 | return -ENODEV; |
1da177e4 LT |
376 | } |
377 | ||
d550d98d | 378 | return 0; |
1da177e4 LT |
379 | } |
380 | ||
4be44fcd | 381 | static void __exit acpi_ac_exit(void) |
1da177e4 | 382 | { |
1da177e4 LT |
383 | |
384 | acpi_bus_unregister_driver(&acpi_ac_driver); | |
385 | ||
fdcedbba | 386 | #ifdef CONFIG_ACPI_PROCFS_POWER |
3f86b832 | 387 | acpi_unlock_ac_dir(acpi_ac_dir); |
8a246ee4 | 388 | #endif |
1da177e4 | 389 | |
d550d98d | 390 | return; |
1da177e4 LT |
391 | } |
392 | ||
1da177e4 LT |
393 | module_init(acpi_ac_init); |
394 | module_exit(acpi_ac_exit); |