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