Blame view
drivers/acpi/ac.c
9.09 KB
1da177e4c
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
/* * acpi_ac.c - ACPI AC Adapter Driver ($Revision: 27 $) * * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com> * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com> * * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or (at * your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ #include <linux/kernel.h> #include <linux/module.h> |
5a0e3ad6a
|
28 |
#include <linux/slab.h> |
1da177e4c
|
29 30 |
#include <linux/init.h> #include <linux/types.h> |
fdcedbba2
|
31 |
#ifdef CONFIG_ACPI_PROCFS_POWER |
1da177e4c
|
32 33 |
#include <linux/proc_fs.h> #include <linux/seq_file.h> |
8a246ee43
|
34 |
#endif |
97749cd9a
|
35 |
#ifdef CONFIG_ACPI_SYSFS_POWER |
d5b4a3d0e
|
36 |
#include <linux/power_supply.h> |
97749cd9a
|
37 |
#endif |
1da177e4c
|
38 39 |
#include <acpi/acpi_bus.h> #include <acpi/acpi_drivers.h> |
a192a9580
|
40 |
#define PREFIX "ACPI: " |
1da177e4c
|
41 |
#define ACPI_AC_CLASS "ac_adapter" |
1da177e4c
|
42 43 44 45 46 47 48 49 |
#define ACPI_AC_DEVICE_NAME "AC Adapter" #define ACPI_AC_FILE_STATE "state" #define ACPI_AC_NOTIFY_STATUS 0x80 #define ACPI_AC_STATUS_OFFLINE 0x00 #define ACPI_AC_STATUS_ONLINE 0x01 #define ACPI_AC_STATUS_UNKNOWN 0xFF #define _COMPONENT ACPI_AC_COMPONENT |
f52fd66d2
|
50 |
ACPI_MODULE_NAME("ac"); |
1da177e4c
|
51 |
|
f52fd66d2
|
52 |
MODULE_AUTHOR("Paul Diefenbaugh"); |
7cda93e00
|
53 |
MODULE_DESCRIPTION("ACPI AC Adapter Driver"); |
1da177e4c
|
54 |
MODULE_LICENSE("GPL"); |
fdcedbba2
|
55 |
#ifdef CONFIG_ACPI_PROCFS_POWER |
3f86b8324
|
56 57 |
extern struct proc_dir_entry *acpi_lock_ac_dir(void); extern void *acpi_unlock_ac_dir(struct proc_dir_entry *acpi_ac_dir); |
8a246ee43
|
58 59 |
static int acpi_ac_open_fs(struct inode *inode, struct file *file); #endif |
3f86b8324
|
60 |
|
4be44fcd3
|
61 62 |
static int acpi_ac_add(struct acpi_device *device); static int acpi_ac_remove(struct acpi_device *device, int type); |
5bfeca313
|
63 |
static int acpi_ac_resume(struct acpi_device *device); |
48fe11274
|
64 |
static void acpi_ac_notify(struct acpi_device *device, u32 event); |
1da177e4c
|
65 |
|
b299c22c8
|
66 |
static const struct acpi_device_id ac_device_ids[] = { |
1ba90e3a8
|
67 68 69 70 |
{"ACPI0003", 0}, {"", 0}, }; MODULE_DEVICE_TABLE(acpi, ac_device_ids); |
1da177e4c
|
71 |
static struct acpi_driver acpi_ac_driver = { |
c2b6705b7
|
72 |
.name = "ac", |
4be44fcd3
|
73 |
.class = ACPI_AC_CLASS, |
1ba90e3a8
|
74 |
.ids = ac_device_ids, |
48fe11274
|
75 |
.flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS, |
4be44fcd3
|
76 77 78 |
.ops = { .add = acpi_ac_add, .remove = acpi_ac_remove, |
5bfeca313
|
79 |
.resume = acpi_ac_resume, |
48fe11274
|
80 |
.notify = acpi_ac_notify, |
4be44fcd3
|
81 |
}, |
1da177e4c
|
82 83 84 |
}; struct acpi_ac { |
97749cd9a
|
85 |
#ifdef CONFIG_ACPI_SYSFS_POWER |
d5b4a3d0e
|
86 |
struct power_supply charger; |
97749cd9a
|
87 |
#endif |
af96179a8
|
88 |
struct acpi_device * device; |
27663c585
|
89 |
unsigned long long state; |
1da177e4c
|
90 |
}; |
d5b4a3d0e
|
91 |
#define to_acpi_ac(x) container_of(x, struct acpi_ac, charger); |
fdcedbba2
|
92 |
#ifdef CONFIG_ACPI_PROCFS_POWER |
d75080328
|
93 |
static const struct file_operations acpi_ac_fops = { |
cf7acfab0
|
94 |
.owner = THIS_MODULE, |
4be44fcd3
|
95 96 97 98 |
.open = acpi_ac_open_fs, .read = seq_read, .llseek = seq_lseek, .release = single_release, |
1da177e4c
|
99 |
}; |
8a246ee43
|
100 |
#endif |
97749cd9a
|
101 |
#ifdef CONFIG_ACPI_SYSFS_POWER |
d5b4a3d0e
|
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
static int get_ac_property(struct power_supply *psy, enum power_supply_property psp, union power_supply_propval *val) { struct acpi_ac *ac = to_acpi_ac(psy); switch (psp) { case POWER_SUPPLY_PROP_ONLINE: val->intval = ac->state; break; default: return -EINVAL; } return 0; } static enum power_supply_property ac_props[] = { POWER_SUPPLY_PROP_ONLINE, }; |
97749cd9a
|
120 |
#endif |
1da177e4c
|
121 122 123 |
/* -------------------------------------------------------------------------- AC Adapter Management -------------------------------------------------------------------------- */ |
4be44fcd3
|
124 |
static int acpi_ac_get_state(struct acpi_ac *ac) |
1da177e4c
|
125 |
{ |
4be44fcd3
|
126 |
acpi_status status = AE_OK; |
1da177e4c
|
127 |
|
1da177e4c
|
128 129 |
if (!ac) |
d550d98d3
|
130 |
return -EINVAL; |
1da177e4c
|
131 |
|
a6ba5ebef
|
132 |
status = acpi_evaluate_integer(ac->device->handle, "_PSR", NULL, &ac->state); |
1da177e4c
|
133 |
if (ACPI_FAILURE(status)) { |
a6fc67202
|
134 |
ACPI_EXCEPTION((AE_INFO, status, "Error reading AC Adapter state")); |
1da177e4c
|
135 |
ac->state = ACPI_AC_STATUS_UNKNOWN; |
d550d98d3
|
136 |
return -ENODEV; |
1da177e4c
|
137 |
} |
4be44fcd3
|
138 |
|
d550d98d3
|
139 |
return 0; |
1da177e4c
|
140 |
} |
fdcedbba2
|
141 |
#ifdef CONFIG_ACPI_PROCFS_POWER |
1da177e4c
|
142 143 144 |
/* -------------------------------------------------------------------------- FS Interface (/proc) -------------------------------------------------------------------------- */ |
4be44fcd3
|
145 |
static struct proc_dir_entry *acpi_ac_dir; |
1da177e4c
|
146 147 148 |
static int acpi_ac_seq_show(struct seq_file *seq, void *offset) { |
50dd09697
|
149 |
struct acpi_ac *ac = seq->private; |
1da177e4c
|
150 |
|
1da177e4c
|
151 152 |
if (!ac) |
d550d98d3
|
153 |
return 0; |
1da177e4c
|
154 155 156 157 |
if (acpi_ac_get_state(ac)) { seq_puts(seq, "ERROR: Unable to read AC Adapter state "); |
d550d98d3
|
158 |
return 0; |
1da177e4c
|
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
} seq_puts(seq, "state: "); switch (ac->state) { case ACPI_AC_STATUS_OFFLINE: seq_puts(seq, "off-line "); break; case ACPI_AC_STATUS_ONLINE: seq_puts(seq, "on-line "); break; default: seq_puts(seq, "unknown "); break; } |
d550d98d3
|
176 |
return 0; |
1da177e4c
|
177 |
} |
4be44fcd3
|
178 |
|
1da177e4c
|
179 180 181 182 |
static int acpi_ac_open_fs(struct inode *inode, struct file *file) { return single_open(file, acpi_ac_seq_show, PDE(inode)->data); } |
4be44fcd3
|
183 |
static int acpi_ac_add_fs(struct acpi_device *device) |
1da177e4c
|
184 |
{ |
4be44fcd3
|
185 |
struct proc_dir_entry *entry = NULL; |
1da177e4c
|
186 |
|
1da177e4c
|
187 188 189 |
if (!acpi_device_dir(device)) { acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), |
4be44fcd3
|
190 |
acpi_ac_dir); |
1da177e4c
|
191 |
if (!acpi_device_dir(device)) |
d550d98d3
|
192 |
return -ENODEV; |
1da177e4c
|
193 194 195 |
} /* 'state' [R] */ |
cf7acfab0
|
196 197 198 |
entry = proc_create_data(ACPI_AC_FILE_STATE, S_IRUGO, acpi_device_dir(device), &acpi_ac_fops, acpi_driver_data(device)); |
1da177e4c
|
199 |
if (!entry) |
d550d98d3
|
200 |
return -ENODEV; |
d550d98d3
|
201 |
return 0; |
1da177e4c
|
202 |
} |
4be44fcd3
|
203 |
static int acpi_ac_remove_fs(struct acpi_device *device) |
1da177e4c
|
204 |
{ |
1da177e4c
|
205 206 |
if (acpi_device_dir(device)) { |
4be44fcd3
|
207 |
remove_proc_entry(ACPI_AC_FILE_STATE, acpi_device_dir(device)); |
1da177e4c
|
208 209 210 211 |
remove_proc_entry(acpi_device_bid(device), acpi_ac_dir); acpi_device_dir(device) = NULL; } |
d550d98d3
|
212 |
return 0; |
1da177e4c
|
213 |
} |
8a246ee43
|
214 |
#endif |
1da177e4c
|
215 |
|
1da177e4c
|
216 217 218 |
/* -------------------------------------------------------------------------- Driver Model -------------------------------------------------------------------------- */ |
48fe11274
|
219 |
static void acpi_ac_notify(struct acpi_device *device, u32 event) |
1da177e4c
|
220 |
{ |
48fe11274
|
221 |
struct acpi_ac *ac = acpi_driver_data(device); |
1da177e4c
|
222 |
|
1da177e4c
|
223 224 |
if (!ac) |
d550d98d3
|
225 |
return; |
1da177e4c
|
226 |
|
1da177e4c
|
227 |
switch (event) { |
f163ff517
|
228 229 230 231 |
default: ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Unsupported event [0x%x] ", event)); |
1da177e4c
|
232 |
case ACPI_AC_NOTIFY_STATUS: |
03d782524
|
233 234 |
case ACPI_NOTIFY_BUS_CHECK: case ACPI_NOTIFY_DEVICE_CHECK: |
1da177e4c
|
235 |
acpi_ac_get_state(ac); |
14e04fb34
|
236 |
acpi_bus_generate_proc_event(device, event, (u32) ac->state); |
962ce8ca0
|
237 |
acpi_bus_generate_netlink_event(device->pnp.device_class, |
0794469da
|
238 |
dev_name(&device->dev), event, |
962ce8ca0
|
239 |
(u32) ac->state); |
68b92b567
|
240 |
acpi_notifier_call_chain(device, event, (u32) ac->state); |
97749cd9a
|
241 |
#ifdef CONFIG_ACPI_SYSFS_POWER |
d5b4a3d0e
|
242 |
kobject_uevent(&ac->charger.dev->kobj, KOBJ_CHANGE); |
97749cd9a
|
243 |
#endif |
1da177e4c
|
244 |
} |
d550d98d3
|
245 |
return; |
1da177e4c
|
246 |
} |
4be44fcd3
|
247 |
static int acpi_ac_add(struct acpi_device *device) |
1da177e4c
|
248 |
{ |
4be44fcd3
|
249 |
int result = 0; |
4be44fcd3
|
250 |
struct acpi_ac *ac = NULL; |
1da177e4c
|
251 |
|
1da177e4c
|
252 253 |
if (!device) |
d550d98d3
|
254 |
return -EINVAL; |
1da177e4c
|
255 |
|
36bcbec7c
|
256 |
ac = kzalloc(sizeof(struct acpi_ac), GFP_KERNEL); |
1da177e4c
|
257 |
if (!ac) |
d550d98d3
|
258 |
return -ENOMEM; |
1da177e4c
|
259 |
|
af96179a8
|
260 |
ac->device = device; |
1da177e4c
|
261 262 |
strcpy(acpi_device_name(device), ACPI_AC_DEVICE_NAME); strcpy(acpi_device_class(device), ACPI_AC_CLASS); |
db89b4f0d
|
263 |
device->driver_data = ac; |
1da177e4c
|
264 265 266 267 |
result = acpi_ac_get_state(ac); if (result) goto end; |
fdcedbba2
|
268 |
#ifdef CONFIG_ACPI_PROCFS_POWER |
1da177e4c
|
269 |
result = acpi_ac_add_fs(device); |
8a246ee43
|
270 |
#endif |
1da177e4c
|
271 272 |
if (result) goto end; |
97749cd9a
|
273 |
#ifdef CONFIG_ACPI_SYSFS_POWER |
d5b4a3d0e
|
274 275 276 277 278 279 |
ac->charger.name = acpi_device_bid(device); ac->charger.type = POWER_SUPPLY_TYPE_MAINS; ac->charger.properties = ac_props; ac->charger.num_properties = ARRAY_SIZE(ac_props); ac->charger.get_property = get_ac_property; power_supply_register(&ac->device->dev, &ac->charger); |
97749cd9a
|
280 |
#endif |
1da177e4c
|
281 |
|
4be44fcd3
|
282 283 284 285 |
printk(KERN_INFO PREFIX "%s [%s] (%s) ", acpi_device_name(device), acpi_device_bid(device), ac->state ? "on-line" : "off-line"); |
1da177e4c
|
286 |
|
4be44fcd3
|
287 |
end: |
1da177e4c
|
288 |
if (result) { |
fdcedbba2
|
289 |
#ifdef CONFIG_ACPI_PROCFS_POWER |
1da177e4c
|
290 |
acpi_ac_remove_fs(device); |
8a246ee43
|
291 |
#endif |
1da177e4c
|
292 293 |
kfree(ac); } |
d550d98d3
|
294 |
return result; |
1da177e4c
|
295 |
} |
5bfeca313
|
296 297 298 299 300 301 302 303 304 305 |
static int acpi_ac_resume(struct acpi_device *device) { struct acpi_ac *ac; unsigned old_state; if (!device || !acpi_driver_data(device)) return -EINVAL; ac = acpi_driver_data(device); old_state = ac->state; if (acpi_ac_get_state(ac)) return 0; |
97749cd9a
|
306 |
#ifdef CONFIG_ACPI_SYSFS_POWER |
5bfeca313
|
307 308 |
if (old_state != ac->state) kobject_uevent(&ac->charger.dev->kobj, KOBJ_CHANGE); |
97749cd9a
|
309 |
#endif |
5bfeca313
|
310 311 |
return 0; } |
4be44fcd3
|
312 |
static int acpi_ac_remove(struct acpi_device *device, int type) |
1da177e4c
|
313 |
{ |
4be44fcd3
|
314 |
struct acpi_ac *ac = NULL; |
1da177e4c
|
315 |
|
1da177e4c
|
316 317 |
if (!device || !acpi_driver_data(device)) |
d550d98d3
|
318 |
return -EINVAL; |
1da177e4c
|
319 |
|
50dd09697
|
320 |
ac = acpi_driver_data(device); |
1da177e4c
|
321 |
|
97749cd9a
|
322 |
#ifdef CONFIG_ACPI_SYSFS_POWER |
d5b4a3d0e
|
323 324 |
if (ac->charger.dev) power_supply_unregister(&ac->charger); |
97749cd9a
|
325 |
#endif |
fdcedbba2
|
326 |
#ifdef CONFIG_ACPI_PROCFS_POWER |
1da177e4c
|
327 |
acpi_ac_remove_fs(device); |
8a246ee43
|
328 |
#endif |
1da177e4c
|
329 330 |
kfree(ac); |
d550d98d3
|
331 |
return 0; |
1da177e4c
|
332 |
} |
4be44fcd3
|
333 |
static int __init acpi_ac_init(void) |
1da177e4c
|
334 |
{ |
3f86b8324
|
335 |
int result; |
1da177e4c
|
336 |
|
4d8316d5e
|
337 338 |
if (acpi_disabled) return -ENODEV; |
1da177e4c
|
339 |
|
fdcedbba2
|
340 |
#ifdef CONFIG_ACPI_PROCFS_POWER |
3f86b8324
|
341 |
acpi_ac_dir = acpi_lock_ac_dir(); |
1da177e4c
|
342 |
if (!acpi_ac_dir) |
d550d98d3
|
343 |
return -ENODEV; |
8a246ee43
|
344 |
#endif |
1da177e4c
|
345 346 347 |
result = acpi_bus_register_driver(&acpi_ac_driver); if (result < 0) { |
fdcedbba2
|
348 |
#ifdef CONFIG_ACPI_PROCFS_POWER |
3f86b8324
|
349 |
acpi_unlock_ac_dir(acpi_ac_dir); |
8a246ee43
|
350 |
#endif |
d550d98d3
|
351 |
return -ENODEV; |
1da177e4c
|
352 |
} |
d550d98d3
|
353 |
return 0; |
1da177e4c
|
354 |
} |
4be44fcd3
|
355 |
static void __exit acpi_ac_exit(void) |
1da177e4c
|
356 |
{ |
1da177e4c
|
357 358 |
acpi_bus_unregister_driver(&acpi_ac_driver); |
fdcedbba2
|
359 |
#ifdef CONFIG_ACPI_PROCFS_POWER |
3f86b8324
|
360 |
acpi_unlock_ac_dir(acpi_ac_dir); |
8a246ee43
|
361 |
#endif |
1da177e4c
|
362 |
|
d550d98d3
|
363 |
return; |
1da177e4c
|
364 |
} |
1da177e4c
|
365 366 |
module_init(acpi_ac_init); module_exit(acpi_ac_exit); |