ACPI: Schedule /proc/acpi/event for removal
[deliverable/linux.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"
1da177e4 44#define ACPI_BATTERY_DEVICE_NAME "Battery"
1da177e4
LT
45#define ACPI_BATTERY_NOTIFY_STATUS 0x80
46#define ACPI_BATTERY_NOTIFY_INFO 0x81
47#define ACPI_BATTERY_UNITS_WATTS "mW"
48#define ACPI_BATTERY_UNITS_AMPS "mA"
49
1da177e4 50#define _COMPONENT ACPI_BATTERY_COMPONENT
b6ce4083
VL
51
52#define ACPI_BATTERY_UPDATE_TIME 0
53
54#define ACPI_BATTERY_NONE_UPDATE 0
55#define ACPI_BATTERY_EASY_UPDATE 1
56#define ACPI_BATTERY_INIT_UPDATE 2
57
f52fd66d 58ACPI_MODULE_NAME("battery");
1da177e4 59
f52fd66d 60MODULE_AUTHOR("Paul Diefenbaugh");
7cda93e0 61MODULE_DESCRIPTION("ACPI Battery Driver");
1da177e4
LT
62MODULE_LICENSE("GPL");
63
b6ce4083
VL
64static unsigned int update_time = ACPI_BATTERY_UPDATE_TIME;
65
66/* 0 - every time, > 0 - by update_time */
67module_param(update_time, uint, 0644);
68
3f86b832
RT
69extern struct proc_dir_entry *acpi_lock_battery_dir(void);
70extern void *acpi_unlock_battery_dir(struct proc_dir_entry *acpi_battery_dir);
71
4be44fcd
LB
72static int acpi_battery_add(struct acpi_device *device);
73static int acpi_battery_remove(struct acpi_device *device, int type);
5d9464a4 74static int acpi_battery_resume(struct acpi_device *device);
1da177e4 75
1ba90e3a
TR
76static const struct acpi_device_id battery_device_ids[] = {
77 {"PNP0C0A", 0},
78 {"", 0},
79};
80MODULE_DEVICE_TABLE(acpi, battery_device_ids);
81
1da177e4 82static struct acpi_driver acpi_battery_driver = {
c2b6705b 83 .name = "battery",
4be44fcd 84 .class = ACPI_BATTERY_CLASS,
1ba90e3a 85 .ids = battery_device_ids,
4be44fcd
LB
86 .ops = {
87 .add = acpi_battery_add,
34c4415a 88 .resume = acpi_battery_resume,
4be44fcd
LB
89 .remove = acpi_battery_remove,
90 },
1da177e4
LT
91};
92
a1f0eff2 93struct acpi_battery_state {
4be44fcd
LB
94 acpi_integer state;
95 acpi_integer present_rate;
96 acpi_integer remaining_capacity;
97 acpi_integer present_voltage;
1da177e4
LT
98};
99
100struct acpi_battery_info {
4be44fcd
LB
101 acpi_integer power_unit;
102 acpi_integer design_capacity;
103 acpi_integer last_full_capacity;
104 acpi_integer battery_technology;
105 acpi_integer design_voltage;
106 acpi_integer design_capacity_warning;
107 acpi_integer design_capacity_low;
108 acpi_integer battery_capacity_granularity_1;
109 acpi_integer battery_capacity_granularity_2;
110 acpi_string model_number;
111 acpi_string serial_number;
112 acpi_string battery_type;
113 acpi_string oem_info;
1da177e4
LT
114};
115
3bd92ba1 116enum acpi_battery_files {
78490d82
AS
117 ACPI_BATTERY_INFO = 0,
118 ACPI_BATTERY_STATE,
119 ACPI_BATTERY_ALARM,
120 ACPI_BATTERY_NUMFILES,
121};
122
1da177e4 123struct acpi_battery_flags {
b6ce4083
VL
124 u8 battery_present_prev;
125 u8 alarm_present;
126 u8 init_update;
78490d82 127 u8 update[ACPI_BATTERY_NUMFILES];
b6ce4083 128 u8 power_unit;
1da177e4
LT
129};
130
131struct acpi_battery {
9ea7d575 132 struct acpi_device *device;
1da177e4 133 struct acpi_battery_flags flags;
b6ce4083
VL
134 struct acpi_buffer bif_data;
135 struct acpi_buffer bst_data;
3bd92ba1 136 struct mutex lock;
4be44fcd 137 unsigned long alarm;
78490d82 138 unsigned long update_time[ACPI_BATTERY_NUMFILES];
3bd92ba1 139
1da177e4
LT
140};
141
78490d82 142inline int acpi_battery_present(struct acpi_battery *battery)
b6ce4083 143{
78490d82
AS
144 return battery->device->status.battery_present;
145}
146inline char *acpi_battery_power_units(struct acpi_battery *battery)
147{
148 if (battery->flags.power_unit)
149 return ACPI_BATTERY_UNITS_AMPS;
150 else
151 return ACPI_BATTERY_UNITS_WATTS;
b6ce4083
VL
152}
153
78490d82 154inline acpi_handle acpi_battery_handle(struct acpi_battery *battery)
b6ce4083 155{
78490d82 156 return battery->device->handle;
b6ce4083
VL
157}
158
78490d82
AS
159/* --------------------------------------------------------------------------
160 Battery Management
161 -------------------------------------------------------------------------- */
162
b6ce4083
VL
163static void acpi_battery_check_result(struct acpi_battery *battery, int result)
164{
165 if (!battery)
166 return;
167
168 if (result) {
78490d82 169 battery->flags.init_update = 1;
b6ce4083
VL
170 }
171}
172
173static int acpi_battery_extract_package(struct acpi_battery *battery,
174 union acpi_object *package,
175 struct acpi_buffer *format,
176 struct acpi_buffer *data,
177 char *package_name)
178{
179 acpi_status status = AE_OK;
180 struct acpi_buffer data_null = { 0, NULL };
181
182 status = acpi_extract_package(package, format, &data_null);
183 if (status != AE_BUFFER_OVERFLOW) {
184 ACPI_EXCEPTION((AE_INFO, status, "Extracting size %s",
185 package_name));
186 return -ENODEV;
187 }
188
189 if (data_null.length != data->length) {
78490d82 190 kfree(data->pointer);
b6ce4083
VL
191 data->pointer = kzalloc(data_null.length, GFP_KERNEL);
192 if (!data->pointer) {
193 ACPI_EXCEPTION((AE_INFO, AE_NO_MEMORY, "kzalloc()"));
194 return -ENOMEM;
195 }
196 data->length = data_null.length;
197 }
198
199 status = acpi_extract_package(package, format, data);
200 if (ACPI_FAILURE(status)) {
201 ACPI_EXCEPTION((AE_INFO, status, "Extracting %s",
202 package_name));
203 return -ENODEV;
204 }
205
206 return 0;
207}
208
209static int acpi_battery_get_status(struct acpi_battery *battery)
210{
211 int result = 0;
212
213 result = acpi_bus_get_status(battery->device);
214 if (result) {
215 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Evaluating _STA"));
216 return -ENODEV;
217 }
218 return result;
219}
220
221static int acpi_battery_get_info(struct acpi_battery *battery)
1da177e4 222{
4be44fcd
LB
223 int result = 0;
224 acpi_status status = 0;
225 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
226 struct acpi_buffer format = { sizeof(ACPI_BATTERY_FORMAT_BIF),
227 ACPI_BATTERY_FORMAT_BIF
228 };
4be44fcd 229 union acpi_object *package = NULL;
b6ce4083
VL
230 struct acpi_buffer *data = NULL;
231 struct acpi_battery_info *bif = NULL;
1da177e4 232
78490d82 233 battery->update_time[ACPI_BATTERY_INFO] = get_seconds();
1da177e4 234
b6ce4083
VL
235 if (!acpi_battery_present(battery))
236 return 0;
1da177e4 237
78490d82 238 /* Evaluate _BIF */
3bd92ba1
AS
239 mutex_lock(&battery->lock);
240 status = acpi_evaluate_object(acpi_battery_handle(battery), "_BIF",
241 NULL, &buffer);
242 mutex_unlock(&battery->lock);
1da177e4 243 if (ACPI_FAILURE(status)) {
a6fc6720 244 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BIF"));
d550d98d 245 return -ENODEV;
1da177e4
LT
246 }
247
50dd0969 248 package = buffer.pointer;
1da177e4 249
b6ce4083
VL
250 data = &battery->bif_data;
251
1da177e4
LT
252 /* Extract Package Data */
253
9ea7d575
VL
254 result =
255 acpi_battery_extract_package(battery, package, &format, data,
256 "_BIF");
b6ce4083 257 if (result)
1da177e4 258 goto end;
1da177e4 259
b6ce4083 260 end:
1da177e4 261
78490d82 262 kfree(buffer.pointer);
1da177e4 263
b6ce4083
VL
264 if (!result) {
265 bif = data->pointer;
266 battery->flags.power_unit = bif->power_unit;
267 }
1da177e4 268
d550d98d 269 return result;
1da177e4
LT
270}
271
b6ce4083 272static int acpi_battery_get_state(struct acpi_battery *battery)
1da177e4 273{
4be44fcd
LB
274 int result = 0;
275 acpi_status status = 0;
276 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
277 struct acpi_buffer format = { sizeof(ACPI_BATTERY_FORMAT_BST),
278 ACPI_BATTERY_FORMAT_BST
279 };
4be44fcd 280 union acpi_object *package = NULL;
b6ce4083 281 struct acpi_buffer *data = NULL;
1da177e4 282
78490d82 283 battery->update_time[ACPI_BATTERY_STATE] = get_seconds();
1da177e4 284
b6ce4083
VL
285 if (!acpi_battery_present(battery))
286 return 0;
1da177e4 287
78490d82 288 /* Evaluate _BST */
3bd92ba1
AS
289 mutex_lock(&battery->lock);
290 status = acpi_evaluate_object(acpi_battery_handle(battery), "_BST",
291 NULL, &buffer);
292 mutex_unlock(&battery->lock);
1da177e4 293 if (ACPI_FAILURE(status)) {
a6fc6720 294 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BST"));
d550d98d 295 return -ENODEV;
1da177e4
LT
296 }
297
50dd0969 298 package = buffer.pointer;
1da177e4 299
b6ce4083 300 data = &battery->bst_data;
1da177e4 301
b6ce4083 302 /* Extract Package Data */
1da177e4 303
9ea7d575
VL
304 result =
305 acpi_battery_extract_package(battery, package, &format, data,
306 "_BST");
b6ce4083 307 if (result)
1da177e4 308 goto end;
1da177e4 309
b6ce4083 310 end:
78490d82 311 kfree(buffer.pointer);
1da177e4 312
b6ce4083
VL
313 return result;
314}
1da177e4 315
b6ce4083
VL
316static int acpi_battery_get_alarm(struct acpi_battery *battery)
317{
78490d82 318 battery->update_time[ACPI_BATTERY_ALARM] = get_seconds();
1da177e4 319
b6ce4083 320 return 0;
1da177e4
LT
321}
322
9ea7d575
VL
323static int acpi_battery_set_alarm(struct acpi_battery *battery,
324 unsigned long alarm)
1da177e4 325{
4be44fcd
LB
326 acpi_status status = 0;
327 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
328 struct acpi_object_list arg_list = { 1, &arg0 };
1da177e4 329
78490d82 330 battery->update_time[ACPI_BATTERY_ALARM] = get_seconds();
1da177e4 331
b6ce4083
VL
332 if (!acpi_battery_present(battery))
333 return -ENODEV;
1da177e4 334
78490d82 335 if (!battery->flags.alarm_present)
d550d98d 336 return -ENODEV;
1da177e4
LT
337
338 arg0.integer.value = alarm;
339
3bd92ba1
AS
340 mutex_lock(&battery->lock);
341 status = acpi_evaluate_object(acpi_battery_handle(battery), "_BTP",
9ea7d575 342 &arg_list, NULL);
3bd92ba1 343 mutex_unlock(&battery->lock);
1da177e4 344 if (ACPI_FAILURE(status))
d550d98d 345 return -ENODEV;
1da177e4
LT
346
347 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", (u32) alarm));
348
349 battery->alarm = alarm;
350
d550d98d 351 return 0;
1da177e4
LT
352}
353
b6ce4083 354static int acpi_battery_init_alarm(struct acpi_battery *battery)
1da177e4 355{
4be44fcd
LB
356 int result = 0;
357 acpi_status status = AE_OK;
358 acpi_handle handle = NULL;
b6ce4083
VL
359 struct acpi_battery_info *bif = battery->bif_data.pointer;
360 unsigned long alarm = battery->alarm;
4be44fcd 361
b6ce4083 362 /* See if alarms are supported, and if so, set default */
1da177e4 363
b6ce4083
VL
364 status = acpi_get_handle(acpi_battery_handle(battery), "_BTP", &handle);
365 if (ACPI_SUCCESS(status)) {
78490d82 366 battery->flags.alarm_present = 1;
b6ce4083
VL
367 if (!alarm && bif) {
368 alarm = bif->design_capacity_warning;
369 }
370 result = acpi_battery_set_alarm(battery, alarm);
371 if (result)
372 goto end;
373 } else {
78490d82 374 battery->flags.alarm_present = 0;
b6ce4083 375 }
1da177e4 376
b6ce4083 377 end:
1da177e4 378
b6ce4083
VL
379 return result;
380}
1da177e4 381
b6ce4083
VL
382static int acpi_battery_init_update(struct acpi_battery *battery)
383{
384 int result = 0;
1da177e4 385
b6ce4083
VL
386 result = acpi_battery_get_status(battery);
387 if (result)
388 return result;
1da177e4 389
78490d82 390 battery->flags.battery_present_prev = acpi_battery_present(battery);
1da177e4 391
b6ce4083
VL
392 if (acpi_battery_present(battery)) {
393 result = acpi_battery_get_info(battery);
394 if (result)
395 return result;
396 result = acpi_battery_get_state(battery);
1da177e4 397 if (result)
d550d98d 398 return result;
1da177e4 399
b6ce4083
VL
400 acpi_battery_init_alarm(battery);
401 }
402
403 return result;
404}
405
406static int acpi_battery_update(struct acpi_battery *battery,
9ea7d575 407 int update, int *update_result_ptr)
b6ce4083
VL
408{
409 int result = 0;
410 int update_result = ACPI_BATTERY_NONE_UPDATE;
1da177e4 411
b6ce4083
VL
412 if (!acpi_battery_present(battery)) {
413 update = 1;
414 }
1da177e4 415
78490d82 416 if (battery->flags.init_update) {
b6ce4083
VL
417 result = acpi_battery_init_update(battery);
418 if (result)
78490d82 419 goto end;
b6ce4083
VL
420 update_result = ACPI_BATTERY_INIT_UPDATE;
421 } else if (update) {
422 result = acpi_battery_get_status(battery);
423 if (result)
78490d82
AS
424 goto end;
425 if ((!battery->flags.battery_present_prev & acpi_battery_present(battery))
426 || (battery->flags.battery_present_prev & !acpi_battery_present(battery))) {
b6ce4083
VL
427 result = acpi_battery_init_update(battery);
428 if (result)
78490d82 429 goto end;
b6ce4083
VL
430 update_result = ACPI_BATTERY_INIT_UPDATE;
431 } else {
432 update_result = ACPI_BATTERY_EASY_UPDATE;
1da177e4
LT
433 }
434 }
435
b6ce4083 436 end:
1da177e4 437
78490d82 438 battery->flags.init_update = (result != 0);
1da177e4 439
b6ce4083 440 *update_result_ptr = update_result;
1da177e4 441
d550d98d 442 return result;
1da177e4
LT
443}
444
b6ce4083 445static void acpi_battery_notify_update(struct acpi_battery *battery)
4bd35cdb 446{
b6ce4083
VL
447 acpi_battery_get_status(battery);
448
78490d82 449 if (battery->flags.init_update) {
b6ce4083
VL
450 return;
451 }
452
78490d82
AS
453 if ((!battery->flags.battery_present_prev &
454 acpi_battery_present(battery)) ||
455 (battery->flags.battery_present_prev &
456 !acpi_battery_present(battery))) {
457 battery->flags.init_update = 1;
b6ce4083 458 } else {
78490d82
AS
459 battery->flags.update[ACPI_BATTERY_INFO] = 1;
460 battery->flags.update[ACPI_BATTERY_STATE] = 1;
461 battery->flags.update[ACPI_BATTERY_ALARM] = 1;
4bd35cdb
VL
462 }
463}
464
1da177e4
LT
465/* --------------------------------------------------------------------------
466 FS Interface (/proc)
467 -------------------------------------------------------------------------- */
468
4be44fcd 469static struct proc_dir_entry *acpi_battery_dir;
b6ce4083 470
78490d82 471static int acpi_battery_print_info(struct seq_file *seq, int result)
1da177e4 472{
50dd0969 473 struct acpi_battery *battery = seq->private;
1da177e4 474 struct acpi_battery_info *bif = NULL;
4be44fcd 475 char *units = "?";
1da177e4 476
b6ce4083 477 if (result)
1da177e4
LT
478 goto end;
479
b6ce4083 480 if (acpi_battery_present(battery))
1da177e4
LT
481 seq_printf(seq, "present: yes\n");
482 else {
483 seq_printf(seq, "present: no\n");
484 goto end;
485 }
486
b6ce4083
VL
487 bif = battery->bif_data.pointer;
488 if (!bif) {
489 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "BIF buffer is NULL"));
490 result = -ENODEV;
1da177e4
LT
491 goto end;
492 }
493
b6ce4083
VL
494 /* Battery Units */
495
496 units = acpi_battery_power_units(battery);
4be44fcd 497
1da177e4
LT
498 if (bif->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
499 seq_printf(seq, "design capacity: unknown\n");
500 else
501 seq_printf(seq, "design capacity: %d %sh\n",
4be44fcd 502 (u32) bif->design_capacity, units);
1da177e4
LT
503
504 if (bif->last_full_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
505 seq_printf(seq, "last full capacity: unknown\n");
506 else
507 seq_printf(seq, "last full capacity: %d %sh\n",
4be44fcd 508 (u32) bif->last_full_capacity, units);
1da177e4
LT
509
510 switch ((u32) bif->battery_technology) {
511 case 0:
512 seq_printf(seq, "battery technology: non-rechargeable\n");
513 break;
514 case 1:
515 seq_printf(seq, "battery technology: rechargeable\n");
516 break;
517 default:
518 seq_printf(seq, "battery technology: unknown\n");
519 break;
520 }
521
522 if (bif->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
523 seq_printf(seq, "design voltage: unknown\n");
524 else
525 seq_printf(seq, "design voltage: %d mV\n",
4be44fcd 526 (u32) bif->design_voltage);
1da177e4 527 seq_printf(seq, "design capacity warning: %d %sh\n",
4be44fcd 528 (u32) bif->design_capacity_warning, units);
1da177e4 529 seq_printf(seq, "design capacity low: %d %sh\n",
4be44fcd 530 (u32) bif->design_capacity_low, units);
1da177e4 531 seq_printf(seq, "capacity granularity 1: %d %sh\n",
4be44fcd 532 (u32) bif->battery_capacity_granularity_1, units);
1da177e4 533 seq_printf(seq, "capacity granularity 2: %d %sh\n",
4be44fcd
LB
534 (u32) bif->battery_capacity_granularity_2, units);
535 seq_printf(seq, "model number: %s\n", bif->model_number);
536 seq_printf(seq, "serial number: %s\n", bif->serial_number);
537 seq_printf(seq, "battery type: %s\n", bif->battery_type);
538 seq_printf(seq, "OEM info: %s\n", bif->oem_info);
539
540 end:
1da177e4 541
b6ce4083
VL
542 if (result)
543 seq_printf(seq, "ERROR: Unable to read battery info\n");
544
545 return result;
546}
547
78490d82 548static int acpi_battery_print_state(struct seq_file *seq, int result)
1da177e4 549{
50dd0969 550 struct acpi_battery *battery = seq->private;
a1f0eff2 551 struct acpi_battery_state *bst = NULL;
4be44fcd 552 char *units = "?";
1da177e4 553
b6ce4083 554 if (result)
1da177e4
LT
555 goto end;
556
b6ce4083 557 if (acpi_battery_present(battery))
1da177e4
LT
558 seq_printf(seq, "present: yes\n");
559 else {
560 seq_printf(seq, "present: no\n");
561 goto end;
562 }
563
b6ce4083
VL
564 bst = battery->bst_data.pointer;
565 if (!bst) {
566 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "BST buffer is NULL"));
567 result = -ENODEV;
1da177e4
LT
568 goto end;
569 }
570
b6ce4083
VL
571 /* Battery Units */
572
573 units = acpi_battery_power_units(battery);
574
1da177e4
LT
575 if (!(bst->state & 0x04))
576 seq_printf(seq, "capacity state: ok\n");
577 else
578 seq_printf(seq, "capacity state: critical\n");
579
4be44fcd
LB
580 if ((bst->state & 0x01) && (bst->state & 0x02)) {
581 seq_printf(seq,
582 "charging state: charging/discharging\n");
4be44fcd 583 } else if (bst->state & 0x01)
1da177e4
LT
584 seq_printf(seq, "charging state: discharging\n");
585 else if (bst->state & 0x02)
586 seq_printf(seq, "charging state: charging\n");
587 else {
588 seq_printf(seq, "charging state: charged\n");
589 }
590
591 if (bst->present_rate == ACPI_BATTERY_VALUE_UNKNOWN)
592 seq_printf(seq, "present rate: unknown\n");
593 else
594 seq_printf(seq, "present rate: %d %s\n",
4be44fcd 595 (u32) bst->present_rate, units);
1da177e4
LT
596
597 if (bst->remaining_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
598 seq_printf(seq, "remaining capacity: unknown\n");
599 else
600 seq_printf(seq, "remaining capacity: %d %sh\n",
4be44fcd 601 (u32) bst->remaining_capacity, units);
1da177e4
LT
602
603 if (bst->present_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
604 seq_printf(seq, "present voltage: unknown\n");
605 else
606 seq_printf(seq, "present voltage: %d mV\n",
4be44fcd 607 (u32) bst->present_voltage);
1da177e4 608
4be44fcd 609 end:
1da177e4 610
b6ce4083
VL
611 if (result) {
612 seq_printf(seq, "ERROR: Unable to read battery state\n");
613 }
614
615 return result;
616}
617
78490d82 618static int acpi_battery_print_alarm(struct seq_file *seq, int result)
1da177e4 619{
50dd0969 620 struct acpi_battery *battery = seq->private;
4be44fcd 621 char *units = "?";
1da177e4 622
b6ce4083 623 if (result)
1da177e4
LT
624 goto end;
625
b6ce4083 626 if (!acpi_battery_present(battery)) {
1da177e4
LT
627 seq_printf(seq, "present: no\n");
628 goto end;
629 }
630
631 /* Battery Units */
4be44fcd 632
b6ce4083 633 units = acpi_battery_power_units(battery);
1da177e4
LT
634
635 seq_printf(seq, "alarm: ");
636 if (!battery->alarm)
637 seq_printf(seq, "unsupported\n");
638 else
b6ce4083 639 seq_printf(seq, "%lu %sh\n", battery->alarm, units);
1da177e4 640
4be44fcd 641 end:
b6ce4083
VL
642
643 if (result)
644 seq_printf(seq, "ERROR: Unable to read battery alarm\n");
645
646 return result;
647}
648
1da177e4 649static ssize_t
4be44fcd
LB
650acpi_battery_write_alarm(struct file *file,
651 const char __user * buffer,
652 size_t count, loff_t * ppos)
1da177e4 653{
4be44fcd
LB
654 int result = 0;
655 char alarm_string[12] = { '\0' };
50dd0969
JE
656 struct seq_file *m = file->private_data;
657 struct acpi_battery *battery = m->private;
b6ce4083 658 int update_result = ACPI_BATTERY_NONE_UPDATE;
1da177e4 659
1da177e4 660 if (!battery || (count > sizeof(alarm_string) - 1))
d550d98d 661 return -EINVAL;
1da177e4 662
b6ce4083
VL
663 result = acpi_battery_update(battery, 1, &update_result);
664 if (result) {
665 result = -ENODEV;
666 goto end;
667 }
1da177e4 668
b6ce4083
VL
669 if (!acpi_battery_present(battery)) {
670 result = -ENODEV;
671 goto end;
672 }
673
674 if (copy_from_user(alarm_string, buffer, count)) {
675 result = -EFAULT;
676 goto end;
677 }
4be44fcd 678
1da177e4
LT
679 alarm_string[count] = '\0';
680
4be44fcd
LB
681 result = acpi_battery_set_alarm(battery,
682 simple_strtoul(alarm_string, NULL, 0));
1da177e4 683 if (result)
b6ce4083
VL
684 goto end;
685
686 end:
687
688 acpi_battery_check_result(battery, result);
1da177e4 689
b6ce4083 690 if (!result)
3bd92ba1 691 return count;
78490d82
AS
692
693 return result;
694}
695
696typedef int(*print_func)(struct seq_file *seq, int result);
697typedef int(*get_func)(struct acpi_battery *battery);
698
699static struct acpi_read_mux {
700 print_func print;
701 get_func get;
702} acpi_read_funcs[ACPI_BATTERY_NUMFILES] = {
703 {.get = acpi_battery_get_info, .print = acpi_battery_print_info},
704 {.get = acpi_battery_get_state, .print = acpi_battery_print_state},
705 {.get = acpi_battery_get_alarm, .print = acpi_battery_print_alarm},
706};
b6ce4083 707
78490d82
AS
708static int acpi_battery_read(int fid, struct seq_file *seq)
709{
710 struct acpi_battery *battery = seq->private;
711 int result = 0;
712 int update_result = ACPI_BATTERY_NONE_UPDATE;
713 int update = 0;
714
78490d82
AS
715 update = (get_seconds() - battery->update_time[fid] >= update_time);
716 update = (update | battery->flags.update[fid]);
717
718 result = acpi_battery_update(battery, update, &update_result);
719 if (result)
720 goto end;
721
722 if (update_result == ACPI_BATTERY_EASY_UPDATE) {
723 result = acpi_read_funcs[fid].get(battery);
724 if (result)
725 goto end;
726 }
727
728 end:
729 result = acpi_read_funcs[fid].print(seq, result);
730 acpi_battery_check_result(battery, result);
731 battery->flags.update[fid] = result;
b6ce4083 732 return result;
1da177e4
LT
733}
734
78490d82
AS
735static int acpi_battery_read_info(struct seq_file *seq, void *offset)
736{
737 return acpi_battery_read(ACPI_BATTERY_INFO, seq);
738}
739
740static int acpi_battery_read_state(struct seq_file *seq, void *offset)
741{
742 return acpi_battery_read(ACPI_BATTERY_STATE, seq);
743}
744
745static int acpi_battery_read_alarm(struct seq_file *seq, void *offset)
746{
747 return acpi_battery_read(ACPI_BATTERY_ALARM, seq);
748}
749
750static int acpi_battery_info_open_fs(struct inode *inode, struct file *file)
751{
752 return single_open(file, acpi_battery_read_info, PDE(inode)->data);
753}
754
755static int acpi_battery_state_open_fs(struct inode *inode, struct file *file)
756{
757 return single_open(file, acpi_battery_read_state, PDE(inode)->data);
758}
759
1da177e4
LT
760static int acpi_battery_alarm_open_fs(struct inode *inode, struct file *file)
761{
762 return single_open(file, acpi_battery_read_alarm, PDE(inode)->data);
763}
764
78490d82
AS
765static struct battery_file {
766 struct file_operations ops;
767 mode_t mode;
768 char *name;
769} acpi_battery_file[] = {
770 {
771 .name = "info",
772 .mode = S_IRUGO,
773 .ops = {
4be44fcd
LB
774 .open = acpi_battery_info_open_fs,
775 .read = seq_read,
776 .llseek = seq_lseek,
777 .release = single_release,
1da177e4 778 .owner = THIS_MODULE,
78490d82
AS
779 },
780 },
781 {
782 .name = "state",
783 .mode = S_IRUGO,
784 .ops = {
4be44fcd
LB
785 .open = acpi_battery_state_open_fs,
786 .read = seq_read,
787 .llseek = seq_lseek,
788 .release = single_release,
1da177e4 789 .owner = THIS_MODULE,
78490d82
AS
790 },
791 },
792 {
793 .name = "alarm",
794 .mode = S_IFREG | S_IRUGO | S_IWUSR,
795 .ops = {
4be44fcd
LB
796 .open = acpi_battery_alarm_open_fs,
797 .read = seq_read,
798 .write = acpi_battery_write_alarm,
799 .llseek = seq_lseek,
800 .release = single_release,
1da177e4 801 .owner = THIS_MODULE,
78490d82
AS
802 },
803 },
1da177e4
LT
804};
805
4be44fcd 806static int acpi_battery_add_fs(struct acpi_device *device)
1da177e4 807{
4be44fcd 808 struct proc_dir_entry *entry = NULL;
78490d82 809 int i;
1da177e4 810
1da177e4
LT
811 if (!acpi_device_dir(device)) {
812 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
4be44fcd 813 acpi_battery_dir);
1da177e4 814 if (!acpi_device_dir(device))
d550d98d 815 return -ENODEV;
1da177e4
LT
816 acpi_device_dir(device)->owner = THIS_MODULE;
817 }
818
78490d82
AS
819 for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i) {
820 entry = create_proc_entry(acpi_battery_file[i].name,
821 acpi_battery_file[i].mode, acpi_device_dir(device));
822 if (!entry)
823 return -ENODEV;
824 else {
825 entry->proc_fops = &acpi_battery_file[i].ops;
826 entry->data = acpi_driver_data(device);
827 entry->owner = THIS_MODULE;
828 }
1da177e4
LT
829 }
830
d550d98d 831 return 0;
1da177e4
LT
832}
833
4be44fcd 834static int acpi_battery_remove_fs(struct acpi_device *device)
1da177e4 835{
78490d82 836 int i;
1da177e4 837 if (acpi_device_dir(device)) {
78490d82
AS
838 for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i) {
839 remove_proc_entry(acpi_battery_file[i].name,
1da177e4 840 acpi_device_dir(device));
78490d82 841 }
1da177e4
LT
842 remove_proc_entry(acpi_device_bid(device), acpi_battery_dir);
843 acpi_device_dir(device) = NULL;
844 }
845
d550d98d 846 return 0;
1da177e4
LT
847}
848
1da177e4
LT
849/* --------------------------------------------------------------------------
850 Driver Interface
851 -------------------------------------------------------------------------- */
852
4be44fcd 853static void acpi_battery_notify(acpi_handle handle, u32 event, void *data)
1da177e4 854{
50dd0969 855 struct acpi_battery *battery = data;
4be44fcd 856 struct acpi_device *device = NULL;
1da177e4 857
1da177e4 858 if (!battery)
d550d98d 859 return;
1da177e4 860
145def84 861 device = battery->device;
1da177e4
LT
862
863 switch (event) {
864 case ACPI_BATTERY_NOTIFY_STATUS:
865 case ACPI_BATTERY_NOTIFY_INFO:
9fdae727
VL
866 case ACPI_NOTIFY_BUS_CHECK:
867 case ACPI_NOTIFY_DEVICE_CHECK:
b6ce4083
VL
868 device = battery->device;
869 acpi_battery_notify_update(battery);
14e04fb3 870 acpi_bus_generate_proc_event(device, event,
9ea7d575 871 acpi_battery_present(battery));
962ce8ca
ZR
872 acpi_bus_generate_netlink_event(device->pnp.device_class,
873 device->dev.bus_id, event,
874 acpi_battery_present(battery));
1da177e4
LT
875 break;
876 default:
877 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
4be44fcd 878 "Unsupported event [0x%x]\n", event));
1da177e4
LT
879 break;
880 }
881
d550d98d 882 return;
1da177e4
LT
883}
884
4be44fcd 885static int acpi_battery_add(struct acpi_device *device)
1da177e4 886{
4be44fcd
LB
887 int result = 0;
888 acpi_status status = 0;
889 struct acpi_battery *battery = NULL;
1da177e4 890
1da177e4 891 if (!device)
d550d98d 892 return -EINVAL;
1da177e4 893
36bcbec7 894 battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL);
1da177e4 895 if (!battery)
d550d98d 896 return -ENOMEM;
1da177e4 897
3bd92ba1 898 mutex_init(&battery->lock);
145def84 899 battery->device = device;
1da177e4
LT
900 strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME);
901 strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS);
902 acpi_driver_data(device) = battery;
903
b6ce4083 904 result = acpi_battery_get_status(battery);
1da177e4
LT
905 if (result)
906 goto end;
907
78490d82 908 battery->flags.init_update = 1;
b6ce4083 909
1da177e4
LT
910 result = acpi_battery_add_fs(device);
911 if (result)
912 goto end;
913
3b073ec3 914 status = acpi_install_notify_handler(device->handle,
9fdae727 915 ACPI_ALL_NOTIFY,
4be44fcd 916 acpi_battery_notify, battery);
1da177e4 917 if (ACPI_FAILURE(status)) {
b6ce4083 918 ACPI_EXCEPTION((AE_INFO, status, "Installing notify handler"));
1da177e4
LT
919 result = -ENODEV;
920 goto end;
921 }
922
923 printk(KERN_INFO PREFIX "%s Slot [%s] (battery %s)\n",
4be44fcd
LB
924 ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device),
925 device->status.battery_present ? "present" : "absent");
926
927 end:
b6ce4083 928
1da177e4
LT
929 if (result) {
930 acpi_battery_remove_fs(device);
931 kfree(battery);
932 }
933
b6ce4083 934
d550d98d 935 return result;
1da177e4
LT
936}
937
4be44fcd 938static int acpi_battery_remove(struct acpi_device *device, int type)
1da177e4 939{
4be44fcd
LB
940 acpi_status status = 0;
941 struct acpi_battery *battery = NULL;
1da177e4 942
1da177e4 943 if (!device || !acpi_driver_data(device))
d550d98d 944 return -EINVAL;
1da177e4 945
50dd0969 946 battery = acpi_driver_data(device);
1da177e4 947
3b073ec3 948 status = acpi_remove_notify_handler(device->handle,
9fdae727 949 ACPI_ALL_NOTIFY,
4be44fcd 950 acpi_battery_notify);
1da177e4
LT
951
952 acpi_battery_remove_fs(device);
953
78490d82 954 kfree(battery->bif_data.pointer);
b6ce4083 955
78490d82 956 kfree(battery->bst_data.pointer);
b6ce4083 957
3bd92ba1 958 mutex_destroy(&battery->lock);
b6ce4083 959
1da177e4
LT
960 kfree(battery);
961
d550d98d 962 return 0;
1da177e4
LT
963}
964
34c4415a 965/* this is needed to learn about changes made in suspended state */
5d9464a4 966static int acpi_battery_resume(struct acpi_device *device)
34c4415a
JK
967{
968 struct acpi_battery *battery;
969
970 if (!device)
971 return -EINVAL;
972
973 battery = device->driver_data;
b6ce4083 974
78490d82 975 battery->flags.init_update = 1;
b6ce4083
VL
976
977 return 0;
34c4415a
JK
978}
979
4be44fcd 980static int __init acpi_battery_init(void)
1da177e4 981{
3f86b832 982 int result;
1da177e4 983
4d8316d5
PM
984 if (acpi_disabled)
985 return -ENODEV;
986
3f86b832 987 acpi_battery_dir = acpi_lock_battery_dir();
1da177e4 988 if (!acpi_battery_dir)
d550d98d 989 return -ENODEV;
1da177e4
LT
990
991 result = acpi_bus_register_driver(&acpi_battery_driver);
992 if (result < 0) {
3f86b832 993 acpi_unlock_battery_dir(acpi_battery_dir);
d550d98d 994 return -ENODEV;
1da177e4
LT
995 }
996
d550d98d 997 return 0;
1da177e4
LT
998}
999
4be44fcd 1000static void __exit acpi_battery_exit(void)
1da177e4 1001{
1da177e4
LT
1002 acpi_bus_unregister_driver(&acpi_battery_driver);
1003
3f86b832 1004 acpi_unlock_battery_dir(acpi_battery_dir);
1da177e4 1005
d550d98d 1006 return;
1da177e4
LT
1007}
1008
1da177e4
LT
1009module_init(acpi_battery_init);
1010module_exit(acpi_battery_exit);
This page took 0.351363 seconds and 5 git commands to generate.