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