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