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