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