Merge branch 'master' into next
[linux-2.6-block.git] / drivers / acpi / battery.c
CommitLineData
1da177e4 1/*
aa650bbd 2 * battery.c - ACPI Battery Driver (Revision: 2.0)
1da177e4 3 *
aa650bbd
AS
4 * Copyright (C) 2007 Alexey Starikovskiy <astarikovskiy@suse.de>
5 * Copyright (C) 2004-2007 Vladimir Lebedev <vladimir.p.lebedev@intel.com>
1da177e4
LT
6 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
7 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
8 *
9 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or (at
14 * your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
24 *
25 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
26 */
27
28#include <linux/kernel.h>
29#include <linux/module.h>
30#include <linux/init.h>
31#include <linux/types.h>
f1d4661a 32#include <linux/jiffies.h>
d7380965 33
fdcedbba 34#ifdef CONFIG_ACPI_PROCFS_POWER
1da177e4
LT
35#include <linux/proc_fs.h>
36#include <linux/seq_file.h>
37#include <asm/uaccess.h>
d7380965 38#endif
1da177e4
LT
39
40#include <acpi/acpi_bus.h>
41#include <acpi/acpi_drivers.h>
42
97749cd9 43#ifdef CONFIG_ACPI_SYSFS_POWER
d7380965 44#include <linux/power_supply.h>
97749cd9 45#endif
d7380965 46
1da177e4
LT
47#define ACPI_BATTERY_VALUE_UNKNOWN 0xFFFFFFFF
48
1da177e4 49#define ACPI_BATTERY_CLASS "battery"
1da177e4 50#define ACPI_BATTERY_DEVICE_NAME "Battery"
1da177e4
LT
51#define ACPI_BATTERY_NOTIFY_STATUS 0x80
52#define ACPI_BATTERY_NOTIFY_INFO 0x81
1da177e4 53
1da177e4 54#define _COMPONENT ACPI_BATTERY_COMPONENT
b6ce4083 55
f52fd66d 56ACPI_MODULE_NAME("battery");
1da177e4 57
f52fd66d 58MODULE_AUTHOR("Paul Diefenbaugh");
aa650bbd 59MODULE_AUTHOR("Alexey Starikovskiy <astarikovskiy@suse.de>");
7cda93e0 60MODULE_DESCRIPTION("ACPI Battery Driver");
1da177e4
LT
61MODULE_LICENSE("GPL");
62
f1d4661a
AS
63static unsigned int cache_time = 1000;
64module_param(cache_time, uint, 0644);
65MODULE_PARM_DESC(cache_time, "cache time in milliseconds");
b6ce4083 66
fdcedbba 67#ifdef CONFIG_ACPI_PROCFS_POWER
3f86b832
RT
68extern struct proc_dir_entry *acpi_lock_battery_dir(void);
69extern void *acpi_unlock_battery_dir(struct proc_dir_entry *acpi_battery_dir);
70
d7380965
AS
71enum acpi_battery_files {
72 info_tag = 0,
73 state_tag,
74 alarm_tag,
75 ACPI_BATTERY_NUMFILES,
76};
77
78#endif
79
1ba90e3a
TR
80static const struct acpi_device_id battery_device_ids[] = {
81 {"PNP0C0A", 0},
82 {"", 0},
83};
1ba90e3a 84
aa650bbd 85MODULE_DEVICE_TABLE(acpi, battery_device_ids);
1da177e4 86
78490d82 87
1da177e4 88struct acpi_battery {
038fdea2 89 struct mutex lock;
97749cd9 90#ifdef CONFIG_ACPI_SYSFS_POWER
d7380965 91 struct power_supply bat;
97749cd9 92#endif
f1d4661a
AS
93 struct acpi_device *device;
94 unsigned long update_time;
d7380965
AS
95 int current_now;
96 int capacity_now;
97 int voltage_now;
038fdea2 98 int design_capacity;
d7380965 99 int full_charge_capacity;
038fdea2
AS
100 int technology;
101 int design_voltage;
102 int design_capacity_warning;
103 int design_capacity_low;
104 int capacity_granularity_1;
105 int capacity_granularity_2;
f1d4661a 106 int alarm;
038fdea2
AS
107 char model_number[32];
108 char serial_number[32];
109 char type[32];
110 char oem_info[32];
f1d4661a
AS
111 int state;
112 int power_unit;
038fdea2 113 u8 alarm_present;
1da177e4
LT
114};
115
d7380965
AS
116#define to_acpi_battery(x) container_of(x, struct acpi_battery, bat);
117
78490d82 118inline int acpi_battery_present(struct acpi_battery *battery)
b6ce4083 119{
78490d82
AS
120 return battery->device->status.battery_present;
121}
038fdea2 122
97749cd9 123#ifdef CONFIG_ACPI_SYSFS_POWER
d7380965
AS
124static int acpi_battery_technology(struct acpi_battery *battery)
125{
126 if (!strcasecmp("NiCd", battery->type))
127 return POWER_SUPPLY_TECHNOLOGY_NiCd;
128 if (!strcasecmp("NiMH", battery->type))
129 return POWER_SUPPLY_TECHNOLOGY_NiMH;
130 if (!strcasecmp("LION", battery->type))
131 return POWER_SUPPLY_TECHNOLOGY_LION;
ad40e68b 132 if (!strncasecmp("LI-ION", battery->type, 6))
0bde7eee 133 return POWER_SUPPLY_TECHNOLOGY_LION;
d7380965
AS
134 if (!strcasecmp("LiP", battery->type))
135 return POWER_SUPPLY_TECHNOLOGY_LIPO;
136 return POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
137}
138
9104476e 139static int acpi_battery_get_state(struct acpi_battery *battery);
b19073a0 140
d7380965
AS
141static int acpi_battery_get_property(struct power_supply *psy,
142 enum power_supply_property psp,
143 union power_supply_propval *val)
144{
145 struct acpi_battery *battery = to_acpi_battery(psy);
146
9104476e
AS
147 if (acpi_battery_present(battery)) {
148 /* run battery update only if it is present */
149 acpi_battery_get_state(battery);
150 } else if (psp != POWER_SUPPLY_PROP_PRESENT)
d7380965
AS
151 return -ENODEV;
152 switch (psp) {
153 case POWER_SUPPLY_PROP_STATUS:
154 if (battery->state & 0x01)
155 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
156 else if (battery->state & 0x02)
157 val->intval = POWER_SUPPLY_STATUS_CHARGING;
158 else if (battery->state == 0)
159 val->intval = POWER_SUPPLY_STATUS_FULL;
4c41d3ad
RD
160 else
161 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
d7380965
AS
162 break;
163 case POWER_SUPPLY_PROP_PRESENT:
164 val->intval = acpi_battery_present(battery);
165 break;
166 case POWER_SUPPLY_PROP_TECHNOLOGY:
167 val->intval = acpi_battery_technology(battery);
168 break;
169 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
170 val->intval = battery->design_voltage * 1000;
171 break;
172 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
173 val->intval = battery->voltage_now * 1000;
174 break;
175 case POWER_SUPPLY_PROP_CURRENT_NOW:
176 val->intval = battery->current_now * 1000;
558073dd
AS
177 /* if power units are mW, convert to mA by
178 dividing by current voltage (mV/1000) */
179 if (!battery->power_unit) {
180 if (battery->voltage_now) {
181 val->intval /= battery->voltage_now;
182 val->intval *= 1000;
183 } else
184 val->intval = -1;
185 }
d7380965
AS
186 break;
187 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
188 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
189 val->intval = battery->design_capacity * 1000;
190 break;
191 case POWER_SUPPLY_PROP_CHARGE_FULL:
192 case POWER_SUPPLY_PROP_ENERGY_FULL:
193 val->intval = battery->full_charge_capacity * 1000;
194 break;
195 case POWER_SUPPLY_PROP_CHARGE_NOW:
196 case POWER_SUPPLY_PROP_ENERGY_NOW:
197 val->intval = battery->capacity_now * 1000;
198 break;
199 case POWER_SUPPLY_PROP_MODEL_NAME:
200 val->strval = battery->model_number;
201 break;
202 case POWER_SUPPLY_PROP_MANUFACTURER:
203 val->strval = battery->oem_info;
204 break;
7c2670bb 205 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
206 val->strval = battery->serial_number;
207 break;
d7380965
AS
208 default:
209 return -EINVAL;
210 }
211 return 0;
212}
213
214static enum power_supply_property charge_battery_props[] = {
215 POWER_SUPPLY_PROP_STATUS,
216 POWER_SUPPLY_PROP_PRESENT,
217 POWER_SUPPLY_PROP_TECHNOLOGY,
218 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
219 POWER_SUPPLY_PROP_VOLTAGE_NOW,
220 POWER_SUPPLY_PROP_CURRENT_NOW,
221 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
222 POWER_SUPPLY_PROP_CHARGE_FULL,
223 POWER_SUPPLY_PROP_CHARGE_NOW,
224 POWER_SUPPLY_PROP_MODEL_NAME,
225 POWER_SUPPLY_PROP_MANUFACTURER,
7c2670bb 226 POWER_SUPPLY_PROP_SERIAL_NUMBER,
d7380965
AS
227};
228
229static enum power_supply_property energy_battery_props[] = {
230 POWER_SUPPLY_PROP_STATUS,
231 POWER_SUPPLY_PROP_PRESENT,
232 POWER_SUPPLY_PROP_TECHNOLOGY,
233 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
234 POWER_SUPPLY_PROP_VOLTAGE_NOW,
235 POWER_SUPPLY_PROP_CURRENT_NOW,
236 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
237 POWER_SUPPLY_PROP_ENERGY_FULL,
238 POWER_SUPPLY_PROP_ENERGY_NOW,
239 POWER_SUPPLY_PROP_MODEL_NAME,
240 POWER_SUPPLY_PROP_MANUFACTURER,
7c2670bb 241 POWER_SUPPLY_PROP_SERIAL_NUMBER,
d7380965 242};
97749cd9 243#endif
d7380965 244
fdcedbba 245#ifdef CONFIG_ACPI_PROCFS_POWER
f1d4661a 246inline char *acpi_battery_units(struct acpi_battery *battery)
78490d82 247{
f1d4661a 248 return (battery->power_unit)?"mA":"mW";
b6ce4083 249}
d7380965 250#endif
b6ce4083 251
78490d82
AS
252/* --------------------------------------------------------------------------
253 Battery Management
254 -------------------------------------------------------------------------- */
038fdea2
AS
255struct acpi_offsets {
256 size_t offset; /* offset inside struct acpi_sbs_battery */
257 u8 mode; /* int or string? */
258};
b6ce4083 259
038fdea2
AS
260static struct acpi_offsets state_offsets[] = {
261 {offsetof(struct acpi_battery, state), 0},
d7380965
AS
262 {offsetof(struct acpi_battery, current_now), 0},
263 {offsetof(struct acpi_battery, capacity_now), 0},
264 {offsetof(struct acpi_battery, voltage_now), 0},
038fdea2 265};
b6ce4083 266
038fdea2
AS
267static struct acpi_offsets info_offsets[] = {
268 {offsetof(struct acpi_battery, power_unit), 0},
269 {offsetof(struct acpi_battery, design_capacity), 0},
d7380965 270 {offsetof(struct acpi_battery, full_charge_capacity), 0},
038fdea2
AS
271 {offsetof(struct acpi_battery, technology), 0},
272 {offsetof(struct acpi_battery, design_voltage), 0},
273 {offsetof(struct acpi_battery, design_capacity_warning), 0},
274 {offsetof(struct acpi_battery, design_capacity_low), 0},
275 {offsetof(struct acpi_battery, capacity_granularity_1), 0},
276 {offsetof(struct acpi_battery, capacity_granularity_2), 0},
277 {offsetof(struct acpi_battery, model_number), 1},
278 {offsetof(struct acpi_battery, serial_number), 1},
279 {offsetof(struct acpi_battery, type), 1},
280 {offsetof(struct acpi_battery, oem_info), 1},
281};
b6ce4083 282
038fdea2
AS
283static int extract_package(struct acpi_battery *battery,
284 union acpi_object *package,
285 struct acpi_offsets *offsets, int num)
286{
106449e8 287 int i;
038fdea2
AS
288 union acpi_object *element;
289 if (package->type != ACPI_TYPE_PACKAGE)
290 return -EFAULT;
291 for (i = 0; i < num; ++i) {
292 if (package->package.count <= i)
293 return -EFAULT;
294 element = &package->package.elements[i];
295 if (offsets[i].mode) {
106449e8
AS
296 u8 *ptr = (u8 *)battery + offsets[i].offset;
297 if (element->type == ACPI_TYPE_STRING ||
298 element->type == ACPI_TYPE_BUFFER)
299 strncpy(ptr, element->string.pointer, 32);
300 else if (element->type == ACPI_TYPE_INTEGER) {
301 strncpy(ptr, (u8 *)&element->integer.value,
302 sizeof(acpi_integer));
303 ptr[sizeof(acpi_integer)] = 0;
b8a1bdb1
AS
304 } else
305 *ptr = 0; /* don't have value */
038fdea2 306 } else {
b8a1bdb1
AS
307 int *x = (int *)((u8 *)battery + offsets[i].offset);
308 *x = (element->type == ACPI_TYPE_INTEGER) ?
309 element->integer.value : -1;
038fdea2 310 }
b6ce4083 311 }
b6ce4083
VL
312 return 0;
313}
314
315static int acpi_battery_get_status(struct acpi_battery *battery)
316{
aa650bbd 317 if (acpi_bus_get_status(battery->device)) {
b6ce4083
VL
318 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Evaluating _STA"));
319 return -ENODEV;
320 }
aa650bbd 321 return 0;
b6ce4083
VL
322}
323
324static int acpi_battery_get_info(struct acpi_battery *battery)
1da177e4 325{
aa650bbd 326 int result = -EFAULT;
4be44fcd
LB
327 acpi_status status = 0;
328 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1da177e4 329
b6ce4083
VL
330 if (!acpi_battery_present(battery))
331 return 0;
038fdea2 332 mutex_lock(&battery->lock);
f1d4661a 333 status = acpi_evaluate_object(battery->device->handle, "_BIF",
038fdea2
AS
334 NULL, &buffer);
335 mutex_unlock(&battery->lock);
aa650bbd 336
1da177e4 337 if (ACPI_FAILURE(status)) {
a6fc6720 338 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BIF"));
d550d98d 339 return -ENODEV;
1da177e4 340 }
aa650bbd 341
038fdea2
AS
342 result = extract_package(battery, buffer.pointer,
343 info_offsets, ARRAY_SIZE(info_offsets));
78490d82 344 kfree(buffer.pointer);
d550d98d 345 return result;
1da177e4
LT
346}
347
b6ce4083 348static int acpi_battery_get_state(struct acpi_battery *battery)
1da177e4 349{
4be44fcd
LB
350 int result = 0;
351 acpi_status status = 0;
352 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1da177e4 353
b6ce4083
VL
354 if (!acpi_battery_present(battery))
355 return 0;
1da177e4 356
f1d4661a
AS
357 if (battery->update_time &&
358 time_before(jiffies, battery->update_time +
359 msecs_to_jiffies(cache_time)))
360 return 0;
361
038fdea2 362 mutex_lock(&battery->lock);
f1d4661a 363 status = acpi_evaluate_object(battery->device->handle, "_BST",
038fdea2
AS
364 NULL, &buffer);
365 mutex_unlock(&battery->lock);
5b31d895 366
1da177e4 367 if (ACPI_FAILURE(status)) {
a6fc6720 368 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BST"));
d550d98d 369 return -ENODEV;
1da177e4 370 }
aa650bbd 371
038fdea2
AS
372 result = extract_package(battery, buffer.pointer,
373 state_offsets, ARRAY_SIZE(state_offsets));
f1d4661a 374 battery->update_time = jiffies;
78490d82 375 kfree(buffer.pointer);
b6ce4083
VL
376 return result;
377}
1da177e4 378
aa650bbd 379static int acpi_battery_set_alarm(struct acpi_battery *battery)
1da177e4 380{
4be44fcd 381 acpi_status status = 0;
aa650bbd 382 union acpi_object arg0 = { .type = ACPI_TYPE_INTEGER };
4be44fcd 383 struct acpi_object_list arg_list = { 1, &arg0 };
1da177e4 384
aa650bbd 385 if (!acpi_battery_present(battery)|| !battery->alarm_present)
d550d98d 386 return -ENODEV;
1da177e4 387
aa650bbd 388 arg0.integer.value = battery->alarm;
1da177e4 389
038fdea2 390 mutex_lock(&battery->lock);
f1d4661a
AS
391 status = acpi_evaluate_object(battery->device->handle, "_BTP",
392 &arg_list, NULL);
038fdea2 393 mutex_unlock(&battery->lock);
aa650bbd 394
1da177e4 395 if (ACPI_FAILURE(status))
d550d98d 396 return -ENODEV;
1da177e4 397
aa650bbd 398 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", battery->alarm));
d550d98d 399 return 0;
1da177e4
LT
400}
401
b6ce4083 402static int acpi_battery_init_alarm(struct acpi_battery *battery)
1da177e4 403{
4be44fcd
LB
404 acpi_status status = AE_OK;
405 acpi_handle handle = NULL;
4be44fcd 406
b6ce4083 407 /* See if alarms are supported, and if so, set default */
f1d4661a
AS
408 status = acpi_get_handle(battery->device->handle, "_BTP", &handle);
409 if (ACPI_FAILURE(status)) {
038fdea2 410 battery->alarm_present = 0;
f1d4661a 411 return 0;
b6ce4083 412 }
f1d4661a
AS
413 battery->alarm_present = 1;
414 if (!battery->alarm)
415 battery->alarm = battery->design_capacity_warning;
aa650bbd 416 return acpi_battery_set_alarm(battery);
b6ce4083 417}
1da177e4 418
97749cd9 419#ifdef CONFIG_ACPI_SYSFS_POWER
508df92d
AB
420static ssize_t acpi_battery_alarm_show(struct device *dev,
421 struct device_attribute *attr,
422 char *buf)
423{
424 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
425 return sprintf(buf, "%d\n", battery->alarm * 1000);
426}
427
428static ssize_t acpi_battery_alarm_store(struct device *dev,
429 struct device_attribute *attr,
430 const char *buf, size_t count)
431{
432 unsigned long x;
433 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
434 if (sscanf(buf, "%ld\n", &x) == 1)
435 battery->alarm = x/1000;
436 if (acpi_battery_present(battery))
437 acpi_battery_set_alarm(battery);
438 return count;
439}
440
441static struct device_attribute alarm_attr = {
01e8ef11 442 .attr = {.name = "alarm", .mode = 0644},
508df92d
AB
443 .show = acpi_battery_alarm_show,
444 .store = acpi_battery_alarm_store,
445};
446
447static int sysfs_add_battery(struct acpi_battery *battery)
448{
449 int result;
450
508df92d
AB
451 if (battery->power_unit) {
452 battery->bat.properties = charge_battery_props;
453 battery->bat.num_properties =
454 ARRAY_SIZE(charge_battery_props);
455 } else {
456 battery->bat.properties = energy_battery_props;
457 battery->bat.num_properties =
458 ARRAY_SIZE(energy_battery_props);
459 }
460
461 battery->bat.name = acpi_device_bid(battery->device);
462 battery->bat.type = POWER_SUPPLY_TYPE_BATTERY;
463 battery->bat.get_property = acpi_battery_get_property;
464
465 result = power_supply_register(&battery->device->dev, &battery->bat);
466 if (result)
467 return result;
468 return device_create_file(battery->bat.dev, &alarm_attr);
469}
470
471static void sysfs_remove_battery(struct acpi_battery *battery)
472{
473 if (!battery->bat.dev)
474 return;
475 device_remove_file(battery->bat.dev, &alarm_attr);
476 power_supply_unregister(&battery->bat);
9104476e 477 battery->bat.dev = NULL;
508df92d 478}
97749cd9 479#endif
508df92d 480
f1d4661a 481static int acpi_battery_update(struct acpi_battery *battery)
b6ce4083 482{
97749cd9
AS
483 int result;
484 result = acpi_battery_get_status(battery);
508df92d 485 if (result)
b6ce4083 486 return result;
97749cd9 487#ifdef CONFIG_ACPI_SYSFS_POWER
508df92d
AB
488 if (!acpi_battery_present(battery)) {
489 sysfs_remove_battery(battery);
97749cd9 490 battery->update_time = 0;
508df92d 491 return 0;
b6ce4083 492 }
97749cd9
AS
493#endif
494 if (!battery->update_time) {
495 result = acpi_battery_get_info(battery);
496 if (result)
497 return result;
498 acpi_battery_init_alarm(battery);
499 }
500#ifdef CONFIG_ACPI_SYSFS_POWER
508df92d
AB
501 if (!battery->bat.dev)
502 sysfs_add_battery(battery);
97749cd9 503#endif
f1d4661a 504 return acpi_battery_get_state(battery);
4bd35cdb
VL
505}
506
1da177e4
LT
507/* --------------------------------------------------------------------------
508 FS Interface (/proc)
509 -------------------------------------------------------------------------- */
510
fdcedbba 511#ifdef CONFIG_ACPI_PROCFS_POWER
4be44fcd 512static struct proc_dir_entry *acpi_battery_dir;
b6ce4083 513
78490d82 514static int acpi_battery_print_info(struct seq_file *seq, int result)
1da177e4 515{
50dd0969 516 struct acpi_battery *battery = seq->private;
1da177e4 517
b6ce4083 518 if (result)
1da177e4
LT
519 goto end;
520
aa650bbd
AS
521 seq_printf(seq, "present: %s\n",
522 acpi_battery_present(battery)?"yes":"no");
523 if (!acpi_battery_present(battery))
1da177e4 524 goto end;
038fdea2 525 if (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
1da177e4
LT
526 seq_printf(seq, "design capacity: unknown\n");
527 else
528 seq_printf(seq, "design capacity: %d %sh\n",
aa650bbd
AS
529 battery->design_capacity,
530 acpi_battery_units(battery));
1da177e4 531
d7380965 532 if (battery->full_charge_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
1da177e4
LT
533 seq_printf(seq, "last full capacity: unknown\n");
534 else
535 seq_printf(seq, "last full capacity: %d %sh\n",
d7380965 536 battery->full_charge_capacity,
aa650bbd
AS
537 acpi_battery_units(battery));
538
539 seq_printf(seq, "battery technology: %srechargeable\n",
540 (!battery->technology)?"non-":"");
1da177e4 541
038fdea2 542 if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
1da177e4
LT
543 seq_printf(seq, "design voltage: unknown\n");
544 else
545 seq_printf(seq, "design voltage: %d mV\n",
aa650bbd 546 battery->design_voltage);
1da177e4 547 seq_printf(seq, "design capacity warning: %d %sh\n",
aa650bbd
AS
548 battery->design_capacity_warning,
549 acpi_battery_units(battery));
1da177e4 550 seq_printf(seq, "design capacity low: %d %sh\n",
aa650bbd
AS
551 battery->design_capacity_low,
552 acpi_battery_units(battery));
1da177e4 553 seq_printf(seq, "capacity granularity 1: %d %sh\n",
aa650bbd
AS
554 battery->capacity_granularity_1,
555 acpi_battery_units(battery));
1da177e4 556 seq_printf(seq, "capacity granularity 2: %d %sh\n",
aa650bbd
AS
557 battery->capacity_granularity_2,
558 acpi_battery_units(battery));
038fdea2
AS
559 seq_printf(seq, "model number: %s\n", battery->model_number);
560 seq_printf(seq, "serial number: %s\n", battery->serial_number);
561 seq_printf(seq, "battery type: %s\n", battery->type);
562 seq_printf(seq, "OEM info: %s\n", battery->oem_info);
4be44fcd 563 end:
b6ce4083
VL
564 if (result)
565 seq_printf(seq, "ERROR: Unable to read battery info\n");
b6ce4083
VL
566 return result;
567}
568
78490d82 569static int acpi_battery_print_state(struct seq_file *seq, int result)
1da177e4 570{
50dd0969 571 struct acpi_battery *battery = seq->private;
1da177e4 572
b6ce4083 573 if (result)
1da177e4
LT
574 goto end;
575
aa650bbd
AS
576 seq_printf(seq, "present: %s\n",
577 acpi_battery_present(battery)?"yes":"no");
578 if (!acpi_battery_present(battery))
1da177e4 579 goto end;
b6ce4083 580
aa650bbd
AS
581 seq_printf(seq, "capacity state: %s\n",
582 (battery->state & 0x04)?"critical":"ok");
583 if ((battery->state & 0x01) && (battery->state & 0x02))
4be44fcd
LB
584 seq_printf(seq,
585 "charging state: charging/discharging\n");
aa650bbd 586 else if (battery->state & 0x01)
1da177e4 587 seq_printf(seq, "charging state: discharging\n");
038fdea2 588 else if (battery->state & 0x02)
1da177e4 589 seq_printf(seq, "charging state: charging\n");
aa650bbd 590 else
1da177e4 591 seq_printf(seq, "charging state: charged\n");
1da177e4 592
d7380965 593 if (battery->current_now == ACPI_BATTERY_VALUE_UNKNOWN)
1da177e4
LT
594 seq_printf(seq, "present rate: unknown\n");
595 else
596 seq_printf(seq, "present rate: %d %s\n",
d7380965 597 battery->current_now, acpi_battery_units(battery));
1da177e4 598
d7380965 599 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN)
1da177e4
LT
600 seq_printf(seq, "remaining capacity: unknown\n");
601 else
602 seq_printf(seq, "remaining capacity: %d %sh\n",
d7380965
AS
603 battery->capacity_now, acpi_battery_units(battery));
604 if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN)
1da177e4
LT
605 seq_printf(seq, "present voltage: unknown\n");
606 else
607 seq_printf(seq, "present voltage: %d mV\n",
d7380965 608 battery->voltage_now);
4be44fcd 609 end:
aa650bbd 610 if (result)
b6ce4083 611 seq_printf(seq, "ERROR: Unable to read battery state\n");
b6ce4083
VL
612
613 return result;
614}
615
78490d82 616static int acpi_battery_print_alarm(struct seq_file *seq, int result)
1da177e4 617{
50dd0969 618 struct acpi_battery *battery = seq->private;
1da177e4 619
b6ce4083 620 if (result)
1da177e4
LT
621 goto end;
622
b6ce4083 623 if (!acpi_battery_present(battery)) {
1da177e4
LT
624 seq_printf(seq, "present: no\n");
625 goto end;
626 }
1da177e4
LT
627 seq_printf(seq, "alarm: ");
628 if (!battery->alarm)
629 seq_printf(seq, "unsupported\n");
630 else
aa650bbd
AS
631 seq_printf(seq, "%u %sh\n", battery->alarm,
632 acpi_battery_units(battery));
4be44fcd 633 end:
b6ce4083
VL
634 if (result)
635 seq_printf(seq, "ERROR: Unable to read battery alarm\n");
b6ce4083
VL
636 return result;
637}
638
f1d4661a
AS
639static ssize_t acpi_battery_write_alarm(struct file *file,
640 const char __user * buffer,
641 size_t count, loff_t * ppos)
1da177e4 642{
4be44fcd
LB
643 int result = 0;
644 char alarm_string[12] = { '\0' };
50dd0969
JE
645 struct seq_file *m = file->private_data;
646 struct acpi_battery *battery = m->private;
1da177e4 647
1da177e4 648 if (!battery || (count > sizeof(alarm_string) - 1))
d550d98d 649 return -EINVAL;
b6ce4083
VL
650 if (!acpi_battery_present(battery)) {
651 result = -ENODEV;
652 goto end;
653 }
b6ce4083
VL
654 if (copy_from_user(alarm_string, buffer, count)) {
655 result = -EFAULT;
656 goto end;
657 }
1da177e4 658 alarm_string[count] = '\0';
f1d4661a 659 battery->alarm = simple_strtol(alarm_string, NULL, 0);
aa650bbd 660 result = acpi_battery_set_alarm(battery);
b6ce4083 661 end:
b6ce4083 662 if (!result)
f1d4661a 663 return count;
78490d82
AS
664 return result;
665}
666
667typedef int(*print_func)(struct seq_file *seq, int result);
aa650bbd
AS
668
669static print_func acpi_print_funcs[ACPI_BATTERY_NUMFILES] = {
670 acpi_battery_print_info,
671 acpi_battery_print_state,
672 acpi_battery_print_alarm,
78490d82 673};
b6ce4083 674
78490d82
AS
675static int acpi_battery_read(int fid, struct seq_file *seq)
676{
677 struct acpi_battery *battery = seq->private;
f1d4661a 678 int result = acpi_battery_update(battery);
aa650bbd 679 return acpi_print_funcs[fid](seq, result);
78490d82
AS
680}
681
aa650bbd
AS
682#define DECLARE_FILE_FUNCTIONS(_name) \
683static int acpi_battery_read_##_name(struct seq_file *seq, void *offset) \
684{ \
685 return acpi_battery_read(_name##_tag, seq); \
686} \
687static int acpi_battery_##_name##_open_fs(struct inode *inode, struct file *file) \
688{ \
689 return single_open(file, acpi_battery_read_##_name, PDE(inode)->data); \
78490d82
AS
690}
691
aa650bbd
AS
692DECLARE_FILE_FUNCTIONS(info);
693DECLARE_FILE_FUNCTIONS(state);
694DECLARE_FILE_FUNCTIONS(alarm);
695
696#undef DECLARE_FILE_FUNCTIONS
697
698#define FILE_DESCRIPTION_RO(_name) \
699 { \
700 .name = __stringify(_name), \
701 .mode = S_IRUGO, \
702 .ops = { \
703 .open = acpi_battery_##_name##_open_fs, \
704 .read = seq_read, \
705 .llseek = seq_lseek, \
706 .release = single_release, \
707 .owner = THIS_MODULE, \
708 }, \
709 }
78490d82 710
aa650bbd
AS
711#define FILE_DESCRIPTION_RW(_name) \
712 { \
713 .name = __stringify(_name), \
714 .mode = S_IFREG | S_IRUGO | S_IWUSR, \
715 .ops = { \
716 .open = acpi_battery_##_name##_open_fs, \
717 .read = seq_read, \
718 .llseek = seq_lseek, \
719 .write = acpi_battery_write_##_name, \
720 .release = single_release, \
721 .owner = THIS_MODULE, \
722 }, \
723 }
1da177e4 724
78490d82
AS
725static struct battery_file {
726 struct file_operations ops;
727 mode_t mode;
728 char *name;
729} acpi_battery_file[] = {
aa650bbd
AS
730 FILE_DESCRIPTION_RO(info),
731 FILE_DESCRIPTION_RO(state),
732 FILE_DESCRIPTION_RW(alarm),
1da177e4
LT
733};
734
aa650bbd
AS
735#undef FILE_DESCRIPTION_RO
736#undef FILE_DESCRIPTION_RW
737
4be44fcd 738static int acpi_battery_add_fs(struct acpi_device *device)
1da177e4 739{
4be44fcd 740 struct proc_dir_entry *entry = NULL;
78490d82 741 int i;
1da177e4 742
1da177e4
LT
743 if (!acpi_device_dir(device)) {
744 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
4be44fcd 745 acpi_battery_dir);
1da177e4 746 if (!acpi_device_dir(device))
d550d98d 747 return -ENODEV;
1da177e4
LT
748 acpi_device_dir(device)->owner = THIS_MODULE;
749 }
750
78490d82 751 for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i) {
cf7acfab
DL
752 entry = proc_create_data(acpi_battery_file[i].name,
753 acpi_battery_file[i].mode,
754 acpi_device_dir(device),
755 &acpi_battery_file[i].ops,
756 acpi_driver_data(device));
78490d82
AS
757 if (!entry)
758 return -ENODEV;
1da177e4 759 }
d550d98d 760 return 0;
1da177e4
LT
761}
762
aa650bbd 763static void acpi_battery_remove_fs(struct acpi_device *device)
1da177e4 764{
78490d82 765 int i;
aa650bbd
AS
766 if (!acpi_device_dir(device))
767 return;
768 for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i)
769 remove_proc_entry(acpi_battery_file[i].name,
1da177e4 770 acpi_device_dir(device));
1da177e4 771
aa650bbd
AS
772 remove_proc_entry(acpi_device_bid(device), acpi_battery_dir);
773 acpi_device_dir(device) = NULL;
1da177e4
LT
774}
775
d7380965 776#endif
3e58ea0d 777
1da177e4
LT
778/* --------------------------------------------------------------------------
779 Driver Interface
780 -------------------------------------------------------------------------- */
781
4be44fcd 782static void acpi_battery_notify(acpi_handle handle, u32 event, void *data)
1da177e4 783{
50dd0969 784 struct acpi_battery *battery = data;
f1d4661a 785 struct acpi_device *device;
1da177e4 786 if (!battery)
d550d98d 787 return;
145def84 788 device = battery->device;
f1d4661a
AS
789 acpi_battery_update(battery);
790 acpi_bus_generate_proc_event(device, event,
791 acpi_battery_present(battery));
792 acpi_bus_generate_netlink_event(device->pnp.device_class,
0794469d 793 dev_name(&device->dev), event,
9ea7d575 794 acpi_battery_present(battery));
97749cd9 795#ifdef CONFIG_ACPI_SYSFS_POWER
508df92d
AB
796 /* acpi_batter_update could remove power_supply object */
797 if (battery->bat.dev)
798 kobject_uevent(&battery->bat.dev->kobj, KOBJ_CHANGE);
97749cd9 799#endif
1da177e4
LT
800}
801
4be44fcd 802static int acpi_battery_add(struct acpi_device *device)
1da177e4 803{
4be44fcd
LB
804 int result = 0;
805 acpi_status status = 0;
806 struct acpi_battery *battery = NULL;
1da177e4 807 if (!device)
d550d98d 808 return -EINVAL;
36bcbec7 809 battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL);
1da177e4 810 if (!battery)
d550d98d 811 return -ENOMEM;
145def84 812 battery->device = device;
1da177e4
LT
813 strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME);
814 strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS);
db89b4f0 815 device->driver_data = battery;
038fdea2 816 mutex_init(&battery->lock);
f1d4661a 817 acpi_battery_update(battery);
fdcedbba 818#ifdef CONFIG_ACPI_PROCFS_POWER
1da177e4
LT
819 result = acpi_battery_add_fs(device);
820 if (result)
821 goto end;
d7380965 822#endif
3b073ec3 823 status = acpi_install_notify_handler(device->handle,
9fdae727 824 ACPI_ALL_NOTIFY,
4be44fcd 825 acpi_battery_notify, battery);
1da177e4 826 if (ACPI_FAILURE(status)) {
b6ce4083 827 ACPI_EXCEPTION((AE_INFO, status, "Installing notify handler"));
1da177e4
LT
828 result = -ENODEV;
829 goto end;
830 }
1da177e4 831 printk(KERN_INFO PREFIX "%s Slot [%s] (battery %s)\n",
4be44fcd
LB
832 ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device),
833 device->status.battery_present ? "present" : "absent");
4be44fcd 834 end:
1da177e4 835 if (result) {
fdcedbba 836#ifdef CONFIG_ACPI_PROCFS_POWER
1da177e4 837 acpi_battery_remove_fs(device);
d7380965 838#endif
1da177e4
LT
839 kfree(battery);
840 }
d550d98d 841 return result;
1da177e4
LT
842}
843
4be44fcd 844static int acpi_battery_remove(struct acpi_device *device, int type)
1da177e4 845{
4be44fcd
LB
846 acpi_status status = 0;
847 struct acpi_battery *battery = NULL;
1da177e4 848
1da177e4 849 if (!device || !acpi_driver_data(device))
d550d98d 850 return -EINVAL;
50dd0969 851 battery = acpi_driver_data(device);
3b073ec3 852 status = acpi_remove_notify_handler(device->handle,
9fdae727 853 ACPI_ALL_NOTIFY,
4be44fcd 854 acpi_battery_notify);
fdcedbba 855#ifdef CONFIG_ACPI_PROCFS_POWER
1da177e4 856 acpi_battery_remove_fs(device);
d7380965 857#endif
97749cd9 858#ifdef CONFIG_ACPI_SYSFS_POWER
508df92d 859 sysfs_remove_battery(battery);
97749cd9 860#endif
038fdea2 861 mutex_destroy(&battery->lock);
1da177e4 862 kfree(battery);
d550d98d 863 return 0;
1da177e4
LT
864}
865
34c4415a 866/* this is needed to learn about changes made in suspended state */
5d9464a4 867static int acpi_battery_resume(struct acpi_device *device)
34c4415a
JK
868{
869 struct acpi_battery *battery;
34c4415a
JK
870 if (!device)
871 return -EINVAL;
f1d4661a
AS
872 battery = acpi_driver_data(device);
873 battery->update_time = 0;
508df92d 874 acpi_battery_update(battery);
b6ce4083 875 return 0;
34c4415a
JK
876}
877
aa650bbd
AS
878static struct acpi_driver acpi_battery_driver = {
879 .name = "battery",
880 .class = ACPI_BATTERY_CLASS,
881 .ids = battery_device_ids,
882 .ops = {
883 .add = acpi_battery_add,
884 .resume = acpi_battery_resume,
885 .remove = acpi_battery_remove,
886 },
887};
888
4be44fcd 889static int __init acpi_battery_init(void)
1da177e4 890{
4d8316d5
PM
891 if (acpi_disabled)
892 return -ENODEV;
fdcedbba 893#ifdef CONFIG_ACPI_PROCFS_POWER
3f86b832 894 acpi_battery_dir = acpi_lock_battery_dir();
1da177e4 895 if (!acpi_battery_dir)
d550d98d 896 return -ENODEV;
d7380965 897#endif
aa650bbd 898 if (acpi_bus_register_driver(&acpi_battery_driver) < 0) {
fdcedbba 899#ifdef CONFIG_ACPI_PROCFS_POWER
3f86b832 900 acpi_unlock_battery_dir(acpi_battery_dir);
d7380965 901#endif
d550d98d 902 return -ENODEV;
1da177e4 903 }
d550d98d 904 return 0;
1da177e4
LT
905}
906
4be44fcd 907static void __exit acpi_battery_exit(void)
1da177e4 908{
1da177e4 909 acpi_bus_unregister_driver(&acpi_battery_driver);
fdcedbba 910#ifdef CONFIG_ACPI_PROCFS_POWER
3f86b832 911 acpi_unlock_battery_dir(acpi_battery_dir);
d7380965 912#endif
1da177e4
LT
913}
914
1da177e4
LT
915module_init(acpi_battery_init);
916module_exit(acpi_battery_exit);