]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/acpi/battery.c
Pull bugzilla-5737 into release branch
[mirror_ubuntu-zesty-kernel.git] / drivers / acpi / battery.c
CommitLineData
1da177e4
LT
1/*
2 * acpi_battery.c - ACPI Battery Driver ($Revision: 37 $)
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 <asm/uaccess.h>
33
34#include <acpi/acpi_bus.h>
35#include <acpi/acpi_drivers.h>
36
1da177e4
LT
37#define ACPI_BATTERY_VALUE_UNKNOWN 0xFFFFFFFF
38
39#define ACPI_BATTERY_FORMAT_BIF "NNNNNNNNNSSSS"
40#define ACPI_BATTERY_FORMAT_BST "NNNN"
41
42#define ACPI_BATTERY_COMPONENT 0x00040000
43#define ACPI_BATTERY_CLASS "battery"
44#define ACPI_BATTERY_HID "PNP0C0A"
45#define ACPI_BATTERY_DRIVER_NAME "ACPI Battery Driver"
46#define ACPI_BATTERY_DEVICE_NAME "Battery"
47#define ACPI_BATTERY_FILE_INFO "info"
48#define ACPI_BATTERY_FILE_STATUS "state"
49#define ACPI_BATTERY_FILE_ALARM "alarm"
50#define ACPI_BATTERY_NOTIFY_STATUS 0x80
51#define ACPI_BATTERY_NOTIFY_INFO 0x81
52#define ACPI_BATTERY_UNITS_WATTS "mW"
53#define ACPI_BATTERY_UNITS_AMPS "mA"
54
1da177e4 55#define _COMPONENT ACPI_BATTERY_COMPONENT
4be44fcd 56ACPI_MODULE_NAME("acpi_battery")
1da177e4 57
4be44fcd 58 MODULE_AUTHOR("Paul Diefenbaugh");
1da177e4
LT
59MODULE_DESCRIPTION(ACPI_BATTERY_DRIVER_NAME);
60MODULE_LICENSE("GPL");
61
4be44fcd
LB
62static int acpi_battery_add(struct acpi_device *device);
63static int acpi_battery_remove(struct acpi_device *device, int type);
1da177e4
LT
64
65static struct acpi_driver acpi_battery_driver = {
4be44fcd
LB
66 .name = ACPI_BATTERY_DRIVER_NAME,
67 .class = ACPI_BATTERY_CLASS,
68 .ids = ACPI_BATTERY_HID,
69 .ops = {
70 .add = acpi_battery_add,
71 .remove = acpi_battery_remove,
72 },
1da177e4
LT
73};
74
75struct acpi_battery_status {
4be44fcd
LB
76 acpi_integer state;
77 acpi_integer present_rate;
78 acpi_integer remaining_capacity;
79 acpi_integer present_voltage;
1da177e4
LT
80};
81
82struct acpi_battery_info {
4be44fcd
LB
83 acpi_integer power_unit;
84 acpi_integer design_capacity;
85 acpi_integer last_full_capacity;
86 acpi_integer battery_technology;
87 acpi_integer design_voltage;
88 acpi_integer design_capacity_warning;
89 acpi_integer design_capacity_low;
90 acpi_integer battery_capacity_granularity_1;
91 acpi_integer battery_capacity_granularity_2;
92 acpi_string model_number;
93 acpi_string serial_number;
94 acpi_string battery_type;
95 acpi_string oem_info;
1da177e4
LT
96};
97
98struct acpi_battery_flags {
4be44fcd
LB
99 u8 present:1; /* Bay occupied? */
100 u8 power_unit:1; /* 0=watts, 1=apms */
101 u8 alarm:1; /* _BTP present? */
102 u8 reserved:5;
1da177e4
LT
103};
104
105struct acpi_battery_trips {
4be44fcd
LB
106 unsigned long warning;
107 unsigned long low;
1da177e4
LT
108};
109
110struct acpi_battery {
4be44fcd 111 acpi_handle handle;
1da177e4
LT
112 struct acpi_battery_flags flags;
113 struct acpi_battery_trips trips;
4be44fcd 114 unsigned long alarm;
1da177e4
LT
115 struct acpi_battery_info *info;
116};
117
1da177e4
LT
118/* --------------------------------------------------------------------------
119 Battery Management
120 -------------------------------------------------------------------------- */
121
122static int
4be44fcd
LB
123acpi_battery_get_info(struct acpi_battery *battery,
124 struct acpi_battery_info **bif)
1da177e4 125{
4be44fcd
LB
126 int result = 0;
127 acpi_status status = 0;
128 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
129 struct acpi_buffer format = { sizeof(ACPI_BATTERY_FORMAT_BIF),
130 ACPI_BATTERY_FORMAT_BIF
131 };
132 struct acpi_buffer data = { 0, NULL };
133 union acpi_object *package = NULL;
1da177e4
LT
134
135 ACPI_FUNCTION_TRACE("acpi_battery_get_info");
136
137 if (!battery || !bif)
138 return_VALUE(-EINVAL);
139
140 /* Evalute _BIF */
141
142 status = acpi_evaluate_object(battery->handle, "_BIF", NULL, &buffer);
143 if (ACPI_FAILURE(status)) {
a6fc6720 144 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BIF"));
1da177e4
LT
145 return_VALUE(-ENODEV);
146 }
147
4be44fcd 148 package = (union acpi_object *)buffer.pointer;
1da177e4
LT
149
150 /* Extract Package Data */
151
152 status = acpi_extract_package(package, &format, &data);
153 if (status != AE_BUFFER_OVERFLOW) {
a6fc6720 154 ACPI_EXCEPTION((AE_INFO, status, "Extracting _BIF"));
1da177e4
LT
155 result = -ENODEV;
156 goto end;
157 }
158
159 data.pointer = kmalloc(data.length, GFP_KERNEL);
160 if (!data.pointer) {
161 result = -ENOMEM;
162 goto end;
163 }
164 memset(data.pointer, 0, data.length);
165
166 status = acpi_extract_package(package, &format, &data);
167 if (ACPI_FAILURE(status)) {
a6fc6720 168 ACPI_EXCEPTION((AE_INFO, status, "Extracting _BIF"));
1da177e4
LT
169 kfree(data.pointer);
170 result = -ENODEV;
171 goto end;
172 }
173
4be44fcd 174 end:
1da177e4
LT
175 acpi_os_free(buffer.pointer);
176
177 if (!result)
4be44fcd 178 (*bif) = (struct acpi_battery_info *)data.pointer;
1da177e4
LT
179
180 return_VALUE(result);
181}
182
183static int
4be44fcd
LB
184acpi_battery_get_status(struct acpi_battery *battery,
185 struct acpi_battery_status **bst)
1da177e4 186{
4be44fcd
LB
187 int result = 0;
188 acpi_status status = 0;
189 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
190 struct acpi_buffer format = { sizeof(ACPI_BATTERY_FORMAT_BST),
191 ACPI_BATTERY_FORMAT_BST
192 };
193 struct acpi_buffer data = { 0, NULL };
194 union acpi_object *package = NULL;
1da177e4
LT
195
196 ACPI_FUNCTION_TRACE("acpi_battery_get_status");
197
198 if (!battery || !bst)
199 return_VALUE(-EINVAL);
200
201 /* Evalute _BST */
202
203 status = acpi_evaluate_object(battery->handle, "_BST", NULL, &buffer);
204 if (ACPI_FAILURE(status)) {
a6fc6720 205 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BST"));
1da177e4
LT
206 return_VALUE(-ENODEV);
207 }
208
4be44fcd 209 package = (union acpi_object *)buffer.pointer;
1da177e4
LT
210
211 /* Extract Package Data */
212
213 status = acpi_extract_package(package, &format, &data);
214 if (status != AE_BUFFER_OVERFLOW) {
a6fc6720 215 ACPI_EXCEPTION((AE_INFO, status, "Extracting _BST"));
1da177e4
LT
216 result = -ENODEV;
217 goto end;
218 }
219
220 data.pointer = kmalloc(data.length, GFP_KERNEL);
221 if (!data.pointer) {
222 result = -ENOMEM;
223 goto end;
224 }
225 memset(data.pointer, 0, data.length);
226
227 status = acpi_extract_package(package, &format, &data);
228 if (ACPI_FAILURE(status)) {
a6fc6720 229 ACPI_EXCEPTION((AE_INFO, status, "Extracting _BST"));
1da177e4
LT
230 kfree(data.pointer);
231 result = -ENODEV;
232 goto end;
233 }
234
4be44fcd 235 end:
1da177e4
LT
236 acpi_os_free(buffer.pointer);
237
238 if (!result)
4be44fcd 239 (*bst) = (struct acpi_battery_status *)data.pointer;
1da177e4
LT
240
241 return_VALUE(result);
242}
243
1da177e4 244static int
4be44fcd 245acpi_battery_set_alarm(struct acpi_battery *battery, unsigned long alarm)
1da177e4 246{
4be44fcd
LB
247 acpi_status status = 0;
248 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
249 struct acpi_object_list arg_list = { 1, &arg0 };
1da177e4
LT
250
251 ACPI_FUNCTION_TRACE("acpi_battery_set_alarm");
252
253 if (!battery)
254 return_VALUE(-EINVAL);
255
256 if (!battery->flags.alarm)
257 return_VALUE(-ENODEV);
258
259 arg0.integer.value = alarm;
260
261 status = acpi_evaluate_object(battery->handle, "_BTP", &arg_list, NULL);
262 if (ACPI_FAILURE(status))
263 return_VALUE(-ENODEV);
264
265 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", (u32) alarm));
266
267 battery->alarm = alarm;
268
269 return_VALUE(0);
270}
271
4be44fcd 272static int acpi_battery_check(struct acpi_battery *battery)
1da177e4 273{
4be44fcd
LB
274 int result = 0;
275 acpi_status status = AE_OK;
276 acpi_handle handle = NULL;
277 struct acpi_device *device = NULL;
1da177e4
LT
278 struct acpi_battery_info *bif = NULL;
279
280 ACPI_FUNCTION_TRACE("acpi_battery_check");
4be44fcd 281
1da177e4
LT
282 if (!battery)
283 return_VALUE(-EINVAL);
284
285 result = acpi_bus_get_device(battery->handle, &device);
286 if (result)
287 return_VALUE(result);
288
289 result = acpi_bus_get_status(device);
290 if (result)
291 return_VALUE(result);
292
293 /* Insertion? */
294
295 if (!battery->flags.present && device->status.battery_present) {
296
297 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Battery inserted\n"));
298
299 /* Evalute _BIF to get certain static information */
300
301 result = acpi_battery_get_info(battery, &bif);
302 if (result)
303 return_VALUE(result);
304
305 battery->flags.power_unit = bif->power_unit;
306 battery->trips.warning = bif->design_capacity_warning;
307 battery->trips.low = bif->design_capacity_low;
308 kfree(bif);
309
310 /* See if alarms are supported, and if so, set default */
311
312 status = acpi_get_handle(battery->handle, "_BTP", &handle);
313 if (ACPI_SUCCESS(status)) {
314 battery->flags.alarm = 1;
315 acpi_battery_set_alarm(battery, battery->trips.warning);
316 }
317 }
318
319 /* Removal? */
320
321 else if (battery->flags.present && !device->status.battery_present) {
322 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Battery removed\n"));
323 }
324
325 battery->flags.present = device->status.battery_present;
326
327 return_VALUE(result);
328}
329
1da177e4
LT
330/* --------------------------------------------------------------------------
331 FS Interface (/proc)
332 -------------------------------------------------------------------------- */
333
4be44fcd 334static struct proc_dir_entry *acpi_battery_dir;
1da177e4
LT
335static int acpi_battery_read_info(struct seq_file *seq, void *offset)
336{
4be44fcd
LB
337 int result = 0;
338 struct acpi_battery *battery = (struct acpi_battery *)seq->private;
1da177e4 339 struct acpi_battery_info *bif = NULL;
4be44fcd 340 char *units = "?";
1da177e4
LT
341
342 ACPI_FUNCTION_TRACE("acpi_battery_read_info");
343
344 if (!battery)
345 goto end;
346
347 if (battery->flags.present)
348 seq_printf(seq, "present: yes\n");
349 else {
350 seq_printf(seq, "present: no\n");
351 goto end;
352 }
353
354 /* Battery Info (_BIF) */
355
356 result = acpi_battery_get_info(battery, &bif);
357 if (result || !bif) {
358 seq_printf(seq, "ERROR: Unable to read battery information\n");
359 goto end;
360 }
361
4be44fcd
LB
362 units =
363 bif->
364 power_unit ? ACPI_BATTERY_UNITS_AMPS : ACPI_BATTERY_UNITS_WATTS;
365
1da177e4
LT
366 if (bif->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
367 seq_printf(seq, "design capacity: unknown\n");
368 else
369 seq_printf(seq, "design capacity: %d %sh\n",
4be44fcd 370 (u32) bif->design_capacity, units);
1da177e4
LT
371
372 if (bif->last_full_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
373 seq_printf(seq, "last full capacity: unknown\n");
374 else
375 seq_printf(seq, "last full capacity: %d %sh\n",
4be44fcd 376 (u32) bif->last_full_capacity, units);
1da177e4
LT
377
378 switch ((u32) bif->battery_technology) {
379 case 0:
380 seq_printf(seq, "battery technology: non-rechargeable\n");
381 break;
382 case 1:
383 seq_printf(seq, "battery technology: rechargeable\n");
384 break;
385 default:
386 seq_printf(seq, "battery technology: unknown\n");
387 break;
388 }
389
390 if (bif->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
391 seq_printf(seq, "design voltage: unknown\n");
392 else
393 seq_printf(seq, "design voltage: %d mV\n",
4be44fcd
LB
394 (u32) bif->design_voltage);
395
1da177e4 396 seq_printf(seq, "design capacity warning: %d %sh\n",
4be44fcd 397 (u32) bif->design_capacity_warning, units);
1da177e4 398 seq_printf(seq, "design capacity low: %d %sh\n",
4be44fcd 399 (u32) bif->design_capacity_low, units);
1da177e4 400 seq_printf(seq, "capacity granularity 1: %d %sh\n",
4be44fcd 401 (u32) bif->battery_capacity_granularity_1, units);
1da177e4 402 seq_printf(seq, "capacity granularity 2: %d %sh\n",
4be44fcd
LB
403 (u32) bif->battery_capacity_granularity_2, units);
404 seq_printf(seq, "model number: %s\n", bif->model_number);
405 seq_printf(seq, "serial number: %s\n", bif->serial_number);
406 seq_printf(seq, "battery type: %s\n", bif->battery_type);
407 seq_printf(seq, "OEM info: %s\n", bif->oem_info);
408
409 end:
1da177e4
LT
410 kfree(bif);
411
412 return_VALUE(0);
413}
414
415static int acpi_battery_info_open_fs(struct inode *inode, struct file *file)
416{
417 return single_open(file, acpi_battery_read_info, PDE(inode)->data);
418}
419
4be44fcd 420static int acpi_battery_read_state(struct seq_file *seq, void *offset)
1da177e4 421{
4be44fcd
LB
422 int result = 0;
423 struct acpi_battery *battery = (struct acpi_battery *)seq->private;
1da177e4 424 struct acpi_battery_status *bst = NULL;
4be44fcd 425 char *units = "?";
1da177e4
LT
426
427 ACPI_FUNCTION_TRACE("acpi_battery_read_state");
428
429 if (!battery)
430 goto end;
431
432 if (battery->flags.present)
433 seq_printf(seq, "present: yes\n");
434 else {
435 seq_printf(seq, "present: no\n");
436 goto end;
437 }
438
439 /* Battery Units */
440
4be44fcd
LB
441 units =
442 battery->flags.
443 power_unit ? ACPI_BATTERY_UNITS_AMPS : ACPI_BATTERY_UNITS_WATTS;
1da177e4
LT
444
445 /* Battery Status (_BST) */
446
447 result = acpi_battery_get_status(battery, &bst);
448 if (result || !bst) {
449 seq_printf(seq, "ERROR: Unable to read battery status\n");
450 goto end;
451 }
452
453 if (!(bst->state & 0x04))
454 seq_printf(seq, "capacity state: ok\n");
455 else
456 seq_printf(seq, "capacity state: critical\n");
457
4be44fcd
LB
458 if ((bst->state & 0x01) && (bst->state & 0x02)) {
459 seq_printf(seq,
460 "charging state: charging/discharging\n");
4be44fcd 461 } else if (bst->state & 0x01)
1da177e4
LT
462 seq_printf(seq, "charging state: discharging\n");
463 else if (bst->state & 0x02)
464 seq_printf(seq, "charging state: charging\n");
465 else {
466 seq_printf(seq, "charging state: charged\n");
467 }
468
469 if (bst->present_rate == ACPI_BATTERY_VALUE_UNKNOWN)
470 seq_printf(seq, "present rate: unknown\n");
471 else
472 seq_printf(seq, "present rate: %d %s\n",
4be44fcd 473 (u32) bst->present_rate, units);
1da177e4
LT
474
475 if (bst->remaining_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
476 seq_printf(seq, "remaining capacity: unknown\n");
477 else
478 seq_printf(seq, "remaining capacity: %d %sh\n",
4be44fcd 479 (u32) bst->remaining_capacity, units);
1da177e4
LT
480
481 if (bst->present_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
482 seq_printf(seq, "present voltage: unknown\n");
483 else
484 seq_printf(seq, "present voltage: %d mV\n",
4be44fcd 485 (u32) bst->present_voltage);
1da177e4 486
4be44fcd 487 end:
1da177e4
LT
488 kfree(bst);
489
490 return_VALUE(0);
491}
492
493static int acpi_battery_state_open_fs(struct inode *inode, struct file *file)
494{
495 return single_open(file, acpi_battery_read_state, PDE(inode)->data);
496}
497
4be44fcd 498static int acpi_battery_read_alarm(struct seq_file *seq, void *offset)
1da177e4 499{
4be44fcd
LB
500 struct acpi_battery *battery = (struct acpi_battery *)seq->private;
501 char *units = "?";
1da177e4
LT
502
503 ACPI_FUNCTION_TRACE("acpi_battery_read_alarm");
504
505 if (!battery)
506 goto end;
507
508 if (!battery->flags.present) {
509 seq_printf(seq, "present: no\n");
510 goto end;
511 }
512
513 /* Battery Units */
4be44fcd
LB
514
515 units =
516 battery->flags.
517 power_unit ? ACPI_BATTERY_UNITS_AMPS : ACPI_BATTERY_UNITS_WATTS;
1da177e4
LT
518
519 /* Battery Alarm */
520
521 seq_printf(seq, "alarm: ");
522 if (!battery->alarm)
523 seq_printf(seq, "unsupported\n");
524 else
525 seq_printf(seq, "%d %sh\n", (u32) battery->alarm, units);
526
4be44fcd 527 end:
1da177e4
LT
528 return_VALUE(0);
529}
530
1da177e4 531static ssize_t
4be44fcd
LB
532acpi_battery_write_alarm(struct file *file,
533 const char __user * buffer,
534 size_t count, loff_t * ppos)
1da177e4 535{
4be44fcd
LB
536 int result = 0;
537 char alarm_string[12] = { '\0' };
538 struct seq_file *m = (struct seq_file *)file->private_data;
539 struct acpi_battery *battery = (struct acpi_battery *)m->private;
1da177e4
LT
540
541 ACPI_FUNCTION_TRACE("acpi_battery_write_alarm");
542
543 if (!battery || (count > sizeof(alarm_string) - 1))
544 return_VALUE(-EINVAL);
545
546 if (!battery->flags.present)
547 return_VALUE(-ENODEV);
548
549 if (copy_from_user(alarm_string, buffer, count))
550 return_VALUE(-EFAULT);
4be44fcd 551
1da177e4
LT
552 alarm_string[count] = '\0';
553
4be44fcd
LB
554 result = acpi_battery_set_alarm(battery,
555 simple_strtoul(alarm_string, NULL, 0));
1da177e4
LT
556 if (result)
557 return_VALUE(result);
558
559 return_VALUE(count);
560}
561
562static int acpi_battery_alarm_open_fs(struct inode *inode, struct file *file)
563{
564 return single_open(file, acpi_battery_read_alarm, PDE(inode)->data);
565}
566
567static struct file_operations acpi_battery_info_ops = {
4be44fcd
LB
568 .open = acpi_battery_info_open_fs,
569 .read = seq_read,
570 .llseek = seq_lseek,
571 .release = single_release,
1da177e4
LT
572 .owner = THIS_MODULE,
573};
574
575static struct file_operations acpi_battery_state_ops = {
4be44fcd
LB
576 .open = acpi_battery_state_open_fs,
577 .read = seq_read,
578 .llseek = seq_lseek,
579 .release = single_release,
1da177e4
LT
580 .owner = THIS_MODULE,
581};
582
583static struct file_operations acpi_battery_alarm_ops = {
4be44fcd
LB
584 .open = acpi_battery_alarm_open_fs,
585 .read = seq_read,
586 .write = acpi_battery_write_alarm,
587 .llseek = seq_lseek,
588 .release = single_release,
1da177e4
LT
589 .owner = THIS_MODULE,
590};
591
4be44fcd 592static int acpi_battery_add_fs(struct acpi_device *device)
1da177e4 593{
4be44fcd 594 struct proc_dir_entry *entry = NULL;
1da177e4
LT
595
596 ACPI_FUNCTION_TRACE("acpi_battery_add_fs");
597
598 if (!acpi_device_dir(device)) {
599 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
4be44fcd 600 acpi_battery_dir);
1da177e4
LT
601 if (!acpi_device_dir(device))
602 return_VALUE(-ENODEV);
603 acpi_device_dir(device)->owner = THIS_MODULE;
604 }
605
606 /* 'info' [R] */
607 entry = create_proc_entry(ACPI_BATTERY_FILE_INFO,
4be44fcd 608 S_IRUGO, acpi_device_dir(device));
1da177e4 609 if (!entry)
a6fc6720 610 return_VALUE(-ENODEV);
1da177e4 611 else {
4be44fcd 612 entry->proc_fops = &acpi_battery_info_ops;
1da177e4
LT
613 entry->data = acpi_driver_data(device);
614 entry->owner = THIS_MODULE;
615 }
616
617 /* 'status' [R] */
618 entry = create_proc_entry(ACPI_BATTERY_FILE_STATUS,
4be44fcd 619 S_IRUGO, acpi_device_dir(device));
1da177e4 620 if (!entry)
a6fc6720 621 return_VALUE(-ENODEV);
1da177e4
LT
622 else {
623 entry->proc_fops = &acpi_battery_state_ops;
624 entry->data = acpi_driver_data(device);
625 entry->owner = THIS_MODULE;
626 }
627
628 /* 'alarm' [R/W] */
629 entry = create_proc_entry(ACPI_BATTERY_FILE_ALARM,
4be44fcd
LB
630 S_IFREG | S_IRUGO | S_IWUSR,
631 acpi_device_dir(device));
1da177e4 632 if (!entry)
a6fc6720 633 return_VALUE(-ENODEV);
1da177e4
LT
634 else {
635 entry->proc_fops = &acpi_battery_alarm_ops;
636 entry->data = acpi_driver_data(device);
637 entry->owner = THIS_MODULE;
638 }
639
640 return_VALUE(0);
641}
642
4be44fcd 643static int acpi_battery_remove_fs(struct acpi_device *device)
1da177e4
LT
644{
645 ACPI_FUNCTION_TRACE("acpi_battery_remove_fs");
646
647 if (acpi_device_dir(device)) {
648 remove_proc_entry(ACPI_BATTERY_FILE_ALARM,
649 acpi_device_dir(device));
650 remove_proc_entry(ACPI_BATTERY_FILE_STATUS,
651 acpi_device_dir(device));
652 remove_proc_entry(ACPI_BATTERY_FILE_INFO,
653 acpi_device_dir(device));
654
655 remove_proc_entry(acpi_device_bid(device), acpi_battery_dir);
656 acpi_device_dir(device) = NULL;
657 }
658
659 return_VALUE(0);
660}
661
1da177e4
LT
662/* --------------------------------------------------------------------------
663 Driver Interface
664 -------------------------------------------------------------------------- */
665
4be44fcd 666static void acpi_battery_notify(acpi_handle handle, u32 event, void *data)
1da177e4 667{
4be44fcd
LB
668 struct acpi_battery *battery = (struct acpi_battery *)data;
669 struct acpi_device *device = NULL;
1da177e4
LT
670
671 ACPI_FUNCTION_TRACE("acpi_battery_notify");
672
673 if (!battery)
674 return_VOID;
675
676 if (acpi_bus_get_device(handle, &device))
677 return_VOID;
678
679 switch (event) {
680 case ACPI_BATTERY_NOTIFY_STATUS:
681 case ACPI_BATTERY_NOTIFY_INFO:
682 acpi_battery_check(battery);
683 acpi_bus_generate_event(device, event, battery->flags.present);
684 break;
685 default:
686 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
4be44fcd 687 "Unsupported event [0x%x]\n", event));
1da177e4
LT
688 break;
689 }
690
691 return_VOID;
692}
693
4be44fcd 694static int acpi_battery_add(struct acpi_device *device)
1da177e4 695{
4be44fcd
LB
696 int result = 0;
697 acpi_status status = 0;
698 struct acpi_battery *battery = NULL;
1da177e4
LT
699
700 ACPI_FUNCTION_TRACE("acpi_battery_add");
4be44fcd 701
1da177e4
LT
702 if (!device)
703 return_VALUE(-EINVAL);
704
705 battery = kmalloc(sizeof(struct acpi_battery), GFP_KERNEL);
706 if (!battery)
707 return_VALUE(-ENOMEM);
708 memset(battery, 0, sizeof(struct acpi_battery));
709
710 battery->handle = device->handle;
711 strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME);
712 strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS);
713 acpi_driver_data(device) = battery;
714
715 result = acpi_battery_check(battery);
716 if (result)
717 goto end;
718
719 result = acpi_battery_add_fs(device);
720 if (result)
721 goto end;
722
723 status = acpi_install_notify_handler(battery->handle,
4be44fcd
LB
724 ACPI_DEVICE_NOTIFY,
725 acpi_battery_notify, battery);
1da177e4 726 if (ACPI_FAILURE(status)) {
1da177e4
LT
727 result = -ENODEV;
728 goto end;
729 }
730
731 printk(KERN_INFO PREFIX "%s Slot [%s] (battery %s)\n",
4be44fcd
LB
732 ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device),
733 device->status.battery_present ? "present" : "absent");
734
735 end:
1da177e4
LT
736 if (result) {
737 acpi_battery_remove_fs(device);
738 kfree(battery);
739 }
740
741 return_VALUE(result);
742}
743
4be44fcd 744static int acpi_battery_remove(struct acpi_device *device, int type)
1da177e4 745{
4be44fcd
LB
746 acpi_status status = 0;
747 struct acpi_battery *battery = NULL;
1da177e4
LT
748
749 ACPI_FUNCTION_TRACE("acpi_battery_remove");
750
751 if (!device || !acpi_driver_data(device))
752 return_VALUE(-EINVAL);
753
4be44fcd 754 battery = (struct acpi_battery *)acpi_driver_data(device);
1da177e4
LT
755
756 status = acpi_remove_notify_handler(battery->handle,
4be44fcd
LB
757 ACPI_DEVICE_NOTIFY,
758 acpi_battery_notify);
1da177e4
LT
759
760 acpi_battery_remove_fs(device);
761
762 kfree(battery);
763
764 return_VALUE(0);
765}
766
4be44fcd 767static int __init acpi_battery_init(void)
1da177e4 768{
4be44fcd 769 int result = 0;
1da177e4
LT
770
771 ACPI_FUNCTION_TRACE("acpi_battery_init");
772
773 acpi_battery_dir = proc_mkdir(ACPI_BATTERY_CLASS, acpi_root_dir);
774 if (!acpi_battery_dir)
775 return_VALUE(-ENODEV);
776 acpi_battery_dir->owner = THIS_MODULE;
777
778 result = acpi_bus_register_driver(&acpi_battery_driver);
779 if (result < 0) {
780 remove_proc_entry(ACPI_BATTERY_CLASS, acpi_root_dir);
781 return_VALUE(-ENODEV);
782 }
783
784 return_VALUE(0);
785}
786
4be44fcd 787static void __exit acpi_battery_exit(void)
1da177e4
LT
788{
789 ACPI_FUNCTION_TRACE("acpi_battery_exit");
790
791 acpi_bus_unregister_driver(&acpi_battery_driver);
792
793 remove_proc_entry(ACPI_BATTERY_CLASS, acpi_root_dir);
794
795 return_VOID;
796}
797
1da177e4
LT
798module_init(acpi_battery_init);
799module_exit(acpi_battery_exit);