blob: dc58402b0a177a4e03e8dc54e7a094804edc1d36 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +04002 * battery.c - ACPI Battery Driver (Revision: 2.0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +04004 * Copyright (C) 2007 Alexey Starikovskiy <astarikovskiy@suse.de>
5 * Copyright (C) 2004-2007 Vladimir Lebedev <vladimir.p.lebedev@intel.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * 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>
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +040032#include <linux/jiffies.h>
Arjan van de Ven0f66af52009-01-10 14:19:05 -050033#include <linux/async.h>
Hector Martinbc76f902009-08-06 15:57:48 -070034#include <linux/dmi.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Alexey Starikovskiyd7380962007-09-26 19:43:04 +040036
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +030037#ifdef CONFIG_ACPI_PROCFS_POWER
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/proc_fs.h>
39#include <linux/seq_file.h>
40#include <asm/uaccess.h>
Alexey Starikovskiyd7380962007-09-26 19:43:04 +040041#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include <acpi/acpi_bus.h>
44#include <acpi/acpi_drivers.h>
45
Alexey Starikovskiy97749cd2008-01-01 14:27:24 -050046#ifdef CONFIG_ACPI_SYSFS_POWER
Alexey Starikovskiyd7380962007-09-26 19:43:04 +040047#include <linux/power_supply.h>
Alexey Starikovskiy97749cd2008-01-01 14:27:24 -050048#endif
Alexey Starikovskiyd7380962007-09-26 19:43:04 +040049
Len Browna192a952009-07-28 16:45:54 -040050#define PREFIX "ACPI: "
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#define ACPI_BATTERY_VALUE_UNKNOWN 0xFFFFFFFF
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#define ACPI_BATTERY_CLASS "battery"
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#define ACPI_BATTERY_DEVICE_NAME "Battery"
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#define ACPI_BATTERY_NOTIFY_STATUS 0x80
57#define ACPI_BATTERY_NOTIFY_INFO 0x81
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +040058#define ACPI_BATTERY_NOTIFY_THRESHOLD 0x82
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#define _COMPONENT ACPI_BATTERY_COMPONENT
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +030061
Len Brownf52fd662007-02-12 22:42:12 -050062ACPI_MODULE_NAME("battery");
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Len Brownf52fd662007-02-12 22:42:12 -050064MODULE_AUTHOR("Paul Diefenbaugh");
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +040065MODULE_AUTHOR("Alexey Starikovskiy <astarikovskiy@suse.de>");
Len Brown7cda93e2007-02-12 23:50:02 -050066MODULE_DESCRIPTION("ACPI Battery Driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070067MODULE_LICENSE("GPL");
68
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +040069static unsigned int cache_time = 1000;
70module_param(cache_time, uint, 0644);
71MODULE_PARM_DESC(cache_time, "cache time in milliseconds");
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +030072
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +030073#ifdef CONFIG_ACPI_PROCFS_POWER
Rich Townsend3f86b832006-07-01 11:36:54 -040074extern struct proc_dir_entry *acpi_lock_battery_dir(void);
75extern void *acpi_unlock_battery_dir(struct proc_dir_entry *acpi_battery_dir);
76
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +040077enum acpi_battery_files {
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +040078 info_tag = 0,
79 state_tag,
80 alarm_tag,
Alexey Starikovskiy78490d82007-05-11 13:18:55 -040081 ACPI_BATTERY_NUMFILES,
82};
83
Alexey Starikovskiyd7380962007-09-26 19:43:04 +040084#endif
85
86static const struct acpi_device_id battery_device_ids[] = {
87 {"PNP0C0A", 0},
88 {"", 0},
89};
90
91MODULE_DEVICE_TABLE(acpi, battery_device_ids);
92
Alexey Starikovskiy7b3bcc42009-10-15 14:31:24 +040093enum {
94 ACPI_BATTERY_ALARM_PRESENT,
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +040095 ACPI_BATTERY_XINFO_PRESENT,
96 /* For buggy DSDTs that report negative 16-bit values for either
97 * charging or discharging current and/or report 0 as 65536
98 * due to bad math.
99 */
Alexey Starikovskiy7b3bcc42009-10-15 14:31:24 +0400100 ACPI_BATTERY_QUIRK_SIGNED16_CURRENT,
101};
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400102
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400103struct acpi_battery {
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400104 struct mutex lock;
Alexey Starikovskiy97749cd2008-01-01 14:27:24 -0500105#ifdef CONFIG_ACPI_SYSFS_POWER
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400106 struct power_supply bat;
Alexey Starikovskiy97749cd2008-01-01 14:27:24 -0500107#endif
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400108 struct acpi_device *device;
109 unsigned long update_time;
Alexey Starikovskiy7faa1442009-03-27 22:23:52 -0400110 int rate_now;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400111 int capacity_now;
112 int voltage_now;
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400113 int design_capacity;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400114 int full_charge_capacity;
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400115 int technology;
116 int design_voltage;
117 int design_capacity_warning;
118 int design_capacity_low;
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400119 int cycle_count;
120 int measurement_accuracy;
121 int max_sampling_time;
122 int min_sampling_time;
123 int max_averaging_interval;
124 int min_averaging_interval;
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400125 int capacity_granularity_1;
126 int capacity_granularity_2;
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400127 int alarm;
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400128 char model_number[32];
129 char serial_number[32];
130 char type[32];
131 char oem_info[32];
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400132 int state;
133 int power_unit;
Alexey Starikovskiy7b3bcc42009-10-15 14:31:24 +0400134 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135};
136
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400137#define to_acpi_battery(x) container_of(x, struct acpi_battery, bat);
138
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400139inline int acpi_battery_present(struct acpi_battery *battery)
140{
141 return battery->device->status.battery_present;
142}
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400143
Alexey Starikovskiy97749cd2008-01-01 14:27:24 -0500144#ifdef CONFIG_ACPI_SYSFS_POWER
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400145static int acpi_battery_technology(struct acpi_battery *battery)
146{
147 if (!strcasecmp("NiCd", battery->type))
148 return POWER_SUPPLY_TECHNOLOGY_NiCd;
149 if (!strcasecmp("NiMH", battery->type))
150 return POWER_SUPPLY_TECHNOLOGY_NiMH;
151 if (!strcasecmp("LION", battery->type))
152 return POWER_SUPPLY_TECHNOLOGY_LION;
Andrey Borzenkovad40e682007-11-10 20:02:49 +0300153 if (!strncasecmp("LI-ION", battery->type, 6))
Alexey Starikovskiy0bde7ee2007-10-28 15:33:10 +0300154 return POWER_SUPPLY_TECHNOLOGY_LION;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400155 if (!strcasecmp("LiP", battery->type))
156 return POWER_SUPPLY_TECHNOLOGY_LIPO;
157 return POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
158}
159
Alexey Starikovskiy91044762007-11-13 12:23:06 +0300160static int acpi_battery_get_state(struct acpi_battery *battery);
Alexey Starikovskiyb19073a2007-10-25 17:10:47 -0400161
Richard Hughes56f382a2009-01-25 15:05:50 +0000162static int acpi_battery_is_charged(struct acpi_battery *battery)
163{
164 /* either charging or discharging */
165 if (battery->state != 0)
166 return 0;
167
168 /* battery not reporting charge */
169 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN ||
170 battery->capacity_now == 0)
171 return 0;
172
173 /* good batteries update full_charge as the batteries degrade */
174 if (battery->full_charge_capacity == battery->capacity_now)
175 return 1;
176
177 /* fallback to using design values for broken batteries */
178 if (battery->design_capacity == battery->capacity_now)
179 return 1;
180
181 /* we don't do any sort of metric based on percentages */
182 return 0;
183}
184
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400185static int acpi_battery_get_property(struct power_supply *psy,
186 enum power_supply_property psp,
187 union power_supply_propval *val)
188{
189 struct acpi_battery *battery = to_acpi_battery(psy);
190
Alexey Starikovskiy91044762007-11-13 12:23:06 +0300191 if (acpi_battery_present(battery)) {
192 /* run battery update only if it is present */
193 acpi_battery_get_state(battery);
194 } else if (psp != POWER_SUPPLY_PROP_PRESENT)
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400195 return -ENODEV;
196 switch (psp) {
197 case POWER_SUPPLY_PROP_STATUS:
198 if (battery->state & 0x01)
199 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
200 else if (battery->state & 0x02)
201 val->intval = POWER_SUPPLY_STATUS_CHARGING;
Richard Hughes56f382a2009-01-25 15:05:50 +0000202 else if (acpi_battery_is_charged(battery))
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400203 val->intval = POWER_SUPPLY_STATUS_FULL;
Roland Dreier4c41d3a2007-11-07 15:09:09 -0800204 else
205 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400206 break;
207 case POWER_SUPPLY_PROP_PRESENT:
208 val->intval = acpi_battery_present(battery);
209 break;
210 case POWER_SUPPLY_PROP_TECHNOLOGY:
211 val->intval = acpi_battery_technology(battery);
212 break;
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400213 case POWER_SUPPLY_PROP_CYCLE_COUNT:
214 val->intval = battery->cycle_count;
215 break;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400216 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
217 val->intval = battery->design_voltage * 1000;
218 break;
219 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
220 val->intval = battery->voltage_now * 1000;
221 break;
222 case POWER_SUPPLY_PROP_CURRENT_NOW:
Alexey Starikovskiy7faa1442009-03-27 22:23:52 -0400223 case POWER_SUPPLY_PROP_POWER_NOW:
224 val->intval = battery->rate_now * 1000;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400225 break;
226 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
227 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
228 val->intval = battery->design_capacity * 1000;
229 break;
230 case POWER_SUPPLY_PROP_CHARGE_FULL:
231 case POWER_SUPPLY_PROP_ENERGY_FULL:
232 val->intval = battery->full_charge_capacity * 1000;
233 break;
234 case POWER_SUPPLY_PROP_CHARGE_NOW:
235 case POWER_SUPPLY_PROP_ENERGY_NOW:
236 val->intval = battery->capacity_now * 1000;
237 break;
238 case POWER_SUPPLY_PROP_MODEL_NAME:
239 val->strval = battery->model_number;
240 break;
241 case POWER_SUPPLY_PROP_MANUFACTURER:
242 val->strval = battery->oem_info;
243 break;
maximilian attems7c2670b2008-01-22 18:46:50 +0100244 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
245 val->strval = battery->serial_number;
246 break;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400247 default:
248 return -EINVAL;
249 }
250 return 0;
251}
252
253static enum power_supply_property charge_battery_props[] = {
254 POWER_SUPPLY_PROP_STATUS,
255 POWER_SUPPLY_PROP_PRESENT,
256 POWER_SUPPLY_PROP_TECHNOLOGY,
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400257 POWER_SUPPLY_PROP_CYCLE_COUNT,
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400258 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
259 POWER_SUPPLY_PROP_VOLTAGE_NOW,
260 POWER_SUPPLY_PROP_CURRENT_NOW,
261 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
262 POWER_SUPPLY_PROP_CHARGE_FULL,
263 POWER_SUPPLY_PROP_CHARGE_NOW,
264 POWER_SUPPLY_PROP_MODEL_NAME,
265 POWER_SUPPLY_PROP_MANUFACTURER,
maximilian attems7c2670b2008-01-22 18:46:50 +0100266 POWER_SUPPLY_PROP_SERIAL_NUMBER,
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400267};
268
269static enum power_supply_property energy_battery_props[] = {
270 POWER_SUPPLY_PROP_STATUS,
271 POWER_SUPPLY_PROP_PRESENT,
272 POWER_SUPPLY_PROP_TECHNOLOGY,
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400273 POWER_SUPPLY_PROP_CYCLE_COUNT,
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400274 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
275 POWER_SUPPLY_PROP_VOLTAGE_NOW,
276 POWER_SUPPLY_PROP_CURRENT_NOW,
Alexey Starikovskiy7faa1442009-03-27 22:23:52 -0400277 POWER_SUPPLY_PROP_POWER_NOW,
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400278 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
279 POWER_SUPPLY_PROP_ENERGY_FULL,
280 POWER_SUPPLY_PROP_ENERGY_NOW,
281 POWER_SUPPLY_PROP_MODEL_NAME,
282 POWER_SUPPLY_PROP_MANUFACTURER,
maximilian attems7c2670b2008-01-22 18:46:50 +0100283 POWER_SUPPLY_PROP_SERIAL_NUMBER,
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400284};
Alexey Starikovskiy97749cd2008-01-01 14:27:24 -0500285#endif
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400286
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +0300287#ifdef CONFIG_ACPI_PROCFS_POWER
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400288inline char *acpi_battery_units(struct acpi_battery *battery)
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400289{
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400290 return (battery->power_unit)?"mA":"mW";
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400291}
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400292#endif
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294/* --------------------------------------------------------------------------
295 Battery Management
296 -------------------------------------------------------------------------- */
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400297struct acpi_offsets {
298 size_t offset; /* offset inside struct acpi_sbs_battery */
299 u8 mode; /* int or string? */
300};
301
302static struct acpi_offsets state_offsets[] = {
303 {offsetof(struct acpi_battery, state), 0},
Alexey Starikovskiy7faa1442009-03-27 22:23:52 -0400304 {offsetof(struct acpi_battery, rate_now), 0},
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400305 {offsetof(struct acpi_battery, capacity_now), 0},
306 {offsetof(struct acpi_battery, voltage_now), 0},
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400307};
308
309static struct acpi_offsets info_offsets[] = {
310 {offsetof(struct acpi_battery, power_unit), 0},
311 {offsetof(struct acpi_battery, design_capacity), 0},
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400312 {offsetof(struct acpi_battery, full_charge_capacity), 0},
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400313 {offsetof(struct acpi_battery, technology), 0},
314 {offsetof(struct acpi_battery, design_voltage), 0},
315 {offsetof(struct acpi_battery, design_capacity_warning), 0},
316 {offsetof(struct acpi_battery, design_capacity_low), 0},
317 {offsetof(struct acpi_battery, capacity_granularity_1), 0},
318 {offsetof(struct acpi_battery, capacity_granularity_2), 0},
319 {offsetof(struct acpi_battery, model_number), 1},
320 {offsetof(struct acpi_battery, serial_number), 1},
321 {offsetof(struct acpi_battery, type), 1},
322 {offsetof(struct acpi_battery, oem_info), 1},
323};
324
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400325static struct acpi_offsets extended_info_offsets[] = {
326 {offsetof(struct acpi_battery, power_unit), 0},
327 {offsetof(struct acpi_battery, design_capacity), 0},
328 {offsetof(struct acpi_battery, full_charge_capacity), 0},
329 {offsetof(struct acpi_battery, technology), 0},
330 {offsetof(struct acpi_battery, design_voltage), 0},
331 {offsetof(struct acpi_battery, design_capacity_warning), 0},
332 {offsetof(struct acpi_battery, design_capacity_low), 0},
333 {offsetof(struct acpi_battery, cycle_count), 0},
334 {offsetof(struct acpi_battery, measurement_accuracy), 0},
335 {offsetof(struct acpi_battery, max_sampling_time), 0},
336 {offsetof(struct acpi_battery, min_sampling_time), 0},
337 {offsetof(struct acpi_battery, max_averaging_interval), 0},
338 {offsetof(struct acpi_battery, min_averaging_interval), 0},
339 {offsetof(struct acpi_battery, capacity_granularity_1), 0},
340 {offsetof(struct acpi_battery, capacity_granularity_2), 0},
341 {offsetof(struct acpi_battery, model_number), 1},
342 {offsetof(struct acpi_battery, serial_number), 1},
343 {offsetof(struct acpi_battery, type), 1},
344 {offsetof(struct acpi_battery, oem_info), 1},
345};
346
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400347static int extract_package(struct acpi_battery *battery,
348 union acpi_object *package,
349 struct acpi_offsets *offsets, int num)
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300350{
Alexey Starikovskiy106449e2007-10-29 23:29:40 +0300351 int i;
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400352 union acpi_object *element;
353 if (package->type != ACPI_TYPE_PACKAGE)
354 return -EFAULT;
355 for (i = 0; i < num; ++i) {
356 if (package->package.count <= i)
357 return -EFAULT;
358 element = &package->package.elements[i];
359 if (offsets[i].mode) {
Alexey Starikovskiy106449e2007-10-29 23:29:40 +0300360 u8 *ptr = (u8 *)battery + offsets[i].offset;
361 if (element->type == ACPI_TYPE_STRING ||
362 element->type == ACPI_TYPE_BUFFER)
363 strncpy(ptr, element->string.pointer, 32);
364 else if (element->type == ACPI_TYPE_INTEGER) {
365 strncpy(ptr, (u8 *)&element->integer.value,
Lin Ming439913f2010-01-28 10:53:19 +0800366 sizeof(u64));
367 ptr[sizeof(u64)] = 0;
Alexey Starikovskiyb8a1bdb2008-03-17 22:37:42 -0400368 } else
369 *ptr = 0; /* don't have value */
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400370 } else {
Alexey Starikovskiyb8a1bdb2008-03-17 22:37:42 -0400371 int *x = (int *)((u8 *)battery + offsets[i].offset);
372 *x = (element->type == ACPI_TYPE_INTEGER) ?
373 element->integer.value : -1;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300374 }
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300375 }
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300376 return 0;
377}
378
379static int acpi_battery_get_status(struct acpi_battery *battery)
380{
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400381 if (acpi_bus_get_status(battery->device)) {
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300382 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Evaluating _STA"));
383 return -ENODEV;
384 }
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400385 return 0;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300386}
387
388static int acpi_battery_get_info(struct acpi_battery *battery)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400390 int result = -EFAULT;
Len Brown4be44fc2005-08-05 00:44:28 -0400391 acpi_status status = 0;
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400392 char *name = test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags)?
393 "_BIX" : "_BIF";
394
Len Brown4be44fc2005-08-05 00:44:28 -0400395 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300397 if (!acpi_battery_present(battery))
398 return 0;
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400399 mutex_lock(&battery->lock);
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400400 status = acpi_evaluate_object(battery->device->handle, name,
401 NULL, &buffer);
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400402 mutex_unlock(&battery->lock);
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 if (ACPI_FAILURE(status)) {
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400405 ACPI_EXCEPTION((AE_INFO, status, "Evaluating %s", name));
Patrick Mocheld550d982006-06-27 00:41:40 -0400406 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 }
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400408 if (test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags))
409 result = extract_package(battery, buffer.pointer,
410 extended_info_offsets,
411 ARRAY_SIZE(extended_info_offsets));
412 else
413 result = extract_package(battery, buffer.pointer,
414 info_offsets, ARRAY_SIZE(info_offsets));
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400415 kfree(buffer.pointer);
Patrick Mocheld550d982006-06-27 00:41:40 -0400416 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417}
418
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300419static int acpi_battery_get_state(struct acpi_battery *battery)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
Len Brown4be44fc2005-08-05 00:44:28 -0400421 int result = 0;
422 acpi_status status = 0;
423 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300425 if (!acpi_battery_present(battery))
426 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400428 if (battery->update_time &&
429 time_before(jiffies, battery->update_time +
430 msecs_to_jiffies(cache_time)))
431 return 0;
432
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400433 mutex_lock(&battery->lock);
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400434 status = acpi_evaluate_object(battery->device->handle, "_BST",
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400435 NULL, &buffer);
436 mutex_unlock(&battery->lock);
Len Brown5b31d892007-08-15 00:19:26 -0400437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400439 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BST"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400440 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 }
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400442
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400443 result = extract_package(battery, buffer.pointer,
444 state_offsets, ARRAY_SIZE(state_offsets));
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400445 battery->update_time = jiffies;
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400446 kfree(buffer.pointer);
Hector Martinbc76f902009-08-06 15:57:48 -0700447
Alexey Starikovskiy7b3bcc42009-10-15 14:31:24 +0400448 if (test_bit(ACPI_BATTERY_QUIRK_SIGNED16_CURRENT, &battery->flags) &&
Hector Martinbc76f902009-08-06 15:57:48 -0700449 battery->rate_now != -1)
450 battery->rate_now = abs((s16)battery->rate_now);
451
Patrick Mocheld550d982006-06-27 00:41:40 -0400452 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453}
454
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400455static int acpi_battery_set_alarm(struct acpi_battery *battery)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
Len Brown4be44fc2005-08-05 00:44:28 -0400457 acpi_status status = 0;
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400458 union acpi_object arg0 = { .type = ACPI_TYPE_INTEGER };
Len Brown4be44fc2005-08-05 00:44:28 -0400459 struct acpi_object_list arg_list = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400461 if (!acpi_battery_present(battery) ||
Alexey Starikovskiy7b3bcc42009-10-15 14:31:24 +0400462 !test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags))
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300463 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400465 arg0.integer.value = battery->alarm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400467 mutex_lock(&battery->lock);
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400468 status = acpi_evaluate_object(battery->device->handle, "_BTP",
469 &arg_list, NULL);
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400470 mutex_unlock(&battery->lock);
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400473 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400475 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", battery->alarm));
Patrick Mocheld550d982006-06-27 00:41:40 -0400476 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477}
478
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300479static int acpi_battery_init_alarm(struct acpi_battery *battery)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
Len Brown4be44fc2005-08-05 00:44:28 -0400481 acpi_status status = AE_OK;
482 acpi_handle handle = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300484 /* See if alarms are supported, and if so, set default */
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400485 status = acpi_get_handle(battery->device->handle, "_BTP", &handle);
486 if (ACPI_FAILURE(status)) {
Alexey Starikovskiy7b3bcc42009-10-15 14:31:24 +0400487 clear_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400488 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 }
Alexey Starikovskiy7b3bcc42009-10-15 14:31:24 +0400490 set_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400491 if (!battery->alarm)
492 battery->alarm = battery->design_capacity_warning;
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400493 return acpi_battery_set_alarm(battery);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494}
495
Alexey Starikovskiy97749cd2008-01-01 14:27:24 -0500496#ifdef CONFIG_ACPI_SYSFS_POWER
Andrey Borzenkov508df922007-10-28 12:50:09 +0300497static ssize_t acpi_battery_alarm_show(struct device *dev,
498 struct device_attribute *attr,
499 char *buf)
500{
501 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
502 return sprintf(buf, "%d\n", battery->alarm * 1000);
503}
504
505static ssize_t acpi_battery_alarm_store(struct device *dev,
506 struct device_attribute *attr,
507 const char *buf, size_t count)
508{
509 unsigned long x;
510 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
511 if (sscanf(buf, "%ld\n", &x) == 1)
512 battery->alarm = x/1000;
513 if (acpi_battery_present(battery))
514 acpi_battery_set_alarm(battery);
515 return count;
516}
517
518static struct device_attribute alarm_attr = {
Parag Warudkar01e8ef12008-10-18 20:28:50 -0700519 .attr = {.name = "alarm", .mode = 0644},
Andrey Borzenkov508df922007-10-28 12:50:09 +0300520 .show = acpi_battery_alarm_show,
521 .store = acpi_battery_alarm_store,
522};
523
524static int sysfs_add_battery(struct acpi_battery *battery)
525{
526 int result;
527
Andrey Borzenkov508df922007-10-28 12:50:09 +0300528 if (battery->power_unit) {
529 battery->bat.properties = charge_battery_props;
530 battery->bat.num_properties =
531 ARRAY_SIZE(charge_battery_props);
532 } else {
533 battery->bat.properties = energy_battery_props;
534 battery->bat.num_properties =
535 ARRAY_SIZE(energy_battery_props);
536 }
537
538 battery->bat.name = acpi_device_bid(battery->device);
539 battery->bat.type = POWER_SUPPLY_TYPE_BATTERY;
540 battery->bat.get_property = acpi_battery_get_property;
541
542 result = power_supply_register(&battery->device->dev, &battery->bat);
543 if (result)
544 return result;
545 return device_create_file(battery->bat.dev, &alarm_attr);
546}
547
548static void sysfs_remove_battery(struct acpi_battery *battery)
549{
550 if (!battery->bat.dev)
551 return;
552 device_remove_file(battery->bat.dev, &alarm_attr);
553 power_supply_unregister(&battery->bat);
Alexey Starikovskiy91044762007-11-13 12:23:06 +0300554 battery->bat.dev = NULL;
Andrey Borzenkov508df922007-10-28 12:50:09 +0300555}
Alexey Starikovskiy97749cd2008-01-01 14:27:24 -0500556#endif
Andrey Borzenkov508df922007-10-28 12:50:09 +0300557
Hector Martinbc76f902009-08-06 15:57:48 -0700558static void acpi_battery_quirks(struct acpi_battery *battery)
559{
Hector Martinbc76f902009-08-06 15:57:48 -0700560 if (dmi_name_in_vendors("Acer") && battery->power_unit) {
Alexey Starikovskiy7b3bcc42009-10-15 14:31:24 +0400561 set_bit(ACPI_BATTERY_QUIRK_SIGNED16_CURRENT, &battery->flags);
Hector Martinbc76f902009-08-06 15:57:48 -0700562 }
563}
564
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400565static int acpi_battery_update(struct acpi_battery *battery)
Vladimir Lebedev4bd35cd2007-02-10 01:43:48 -0500566{
Alexey Starikovskiy50b17852008-12-23 02:44:54 +0300567 int result, old_present = acpi_battery_present(battery);
Alexey Starikovskiy97749cd2008-01-01 14:27:24 -0500568 result = acpi_battery_get_status(battery);
Andrey Borzenkov508df922007-10-28 12:50:09 +0300569 if (result)
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300570 return result;
Andrey Borzenkov508df922007-10-28 12:50:09 +0300571 if (!acpi_battery_present(battery)) {
Alan Jenkinse363a752009-06-30 14:35:05 +0000572#ifdef CONFIG_ACPI_SYSFS_POWER
Andrey Borzenkov508df922007-10-28 12:50:09 +0300573 sysfs_remove_battery(battery);
Alan Jenkinse363a752009-06-30 14:35:05 +0000574#endif
Alexey Starikovskiy97749cd2008-01-01 14:27:24 -0500575 battery->update_time = 0;
Andrey Borzenkov508df922007-10-28 12:50:09 +0300576 return 0;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300577 }
Alexey Starikovskiy50b17852008-12-23 02:44:54 +0300578 if (!battery->update_time ||
579 old_present != acpi_battery_present(battery)) {
Alexey Starikovskiy97749cd2008-01-01 14:27:24 -0500580 result = acpi_battery_get_info(battery);
581 if (result)
582 return result;
Hector Martinbc76f902009-08-06 15:57:48 -0700583 acpi_battery_quirks(battery);
Alexey Starikovskiy97749cd2008-01-01 14:27:24 -0500584 acpi_battery_init_alarm(battery);
585 }
586#ifdef CONFIG_ACPI_SYSFS_POWER
Andrey Borzenkov508df922007-10-28 12:50:09 +0300587 if (!battery->bat.dev)
588 sysfs_add_battery(battery);
Alexey Starikovskiy97749cd2008-01-01 14:27:24 -0500589#endif
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400590 return acpi_battery_get_state(battery);
Vladimir Lebedev4bd35cd2007-02-10 01:43:48 -0500591}
592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593/* --------------------------------------------------------------------------
594 FS Interface (/proc)
595 -------------------------------------------------------------------------- */
596
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +0300597#ifdef CONFIG_ACPI_PROCFS_POWER
Len Brown4be44fc2005-08-05 00:44:28 -0400598static struct proc_dir_entry *acpi_battery_dir;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300599
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400600static int acpi_battery_print_info(struct seq_file *seq, int result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200602 struct acpi_battery *battery = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300604 if (result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 goto end;
606
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400607 seq_printf(seq, "present: %s\n",
608 acpi_battery_present(battery)?"yes":"no");
609 if (!acpi_battery_present(battery))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 goto end;
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400611 if (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 seq_printf(seq, "design capacity: unknown\n");
613 else
614 seq_printf(seq, "design capacity: %d %sh\n",
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400615 battery->design_capacity,
616 acpi_battery_units(battery));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400618 if (battery->full_charge_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 seq_printf(seq, "last full capacity: unknown\n");
620 else
621 seq_printf(seq, "last full capacity: %d %sh\n",
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400622 battery->full_charge_capacity,
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400623 acpi_battery_units(battery));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400625 seq_printf(seq, "battery technology: %srechargeable\n",
626 (!battery->technology)?"non-":"");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400628 if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 seq_printf(seq, "design voltage: unknown\n");
630 else
631 seq_printf(seq, "design voltage: %d mV\n",
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400632 battery->design_voltage);
Len Brown4be44fc2005-08-05 00:44:28 -0400633 seq_printf(seq, "design capacity warning: %d %sh\n",
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400634 battery->design_capacity_warning,
635 acpi_battery_units(battery));
Len Brown4be44fc2005-08-05 00:44:28 -0400636 seq_printf(seq, "design capacity low: %d %sh\n",
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400637 battery->design_capacity_low,
638 acpi_battery_units(battery));
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400639 seq_printf(seq, "cycle count: %i\n", battery->cycle_count);
Len Brown4be44fc2005-08-05 00:44:28 -0400640 seq_printf(seq, "capacity granularity 1: %d %sh\n",
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400641 battery->capacity_granularity_1,
642 acpi_battery_units(battery));
Len Brown4be44fc2005-08-05 00:44:28 -0400643 seq_printf(seq, "capacity granularity 2: %d %sh\n",
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400644 battery->capacity_granularity_2,
645 acpi_battery_units(battery));
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400646 seq_printf(seq, "model number: %s\n", battery->model_number);
647 seq_printf(seq, "serial number: %s\n", battery->serial_number);
648 seq_printf(seq, "battery type: %s\n", battery->type);
649 seq_printf(seq, "OEM info: %s\n", battery->oem_info);
Len Brown4be44fc2005-08-05 00:44:28 -0400650 end:
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300651 if (result)
652 seq_printf(seq, "ERROR: Unable to read battery info\n");
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300653 return result;
654}
655
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400656static int acpi_battery_print_state(struct seq_file *seq, int result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200658 struct acpi_battery *battery = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300660 if (result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 goto end;
662
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400663 seq_printf(seq, "present: %s\n",
664 acpi_battery_present(battery)?"yes":"no");
665 if (!acpi_battery_present(battery))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400668 seq_printf(seq, "capacity state: %s\n",
669 (battery->state & 0x04)?"critical":"ok");
670 if ((battery->state & 0x01) && (battery->state & 0x02))
Len Brown4be44fc2005-08-05 00:44:28 -0400671 seq_printf(seq,
672 "charging state: charging/discharging\n");
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400673 else if (battery->state & 0x01)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 seq_printf(seq, "charging state: discharging\n");
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400675 else if (battery->state & 0x02)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 seq_printf(seq, "charging state: charging\n");
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400677 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 seq_printf(seq, "charging state: charged\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Alexey Starikovskiy7faa1442009-03-27 22:23:52 -0400680 if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 seq_printf(seq, "present rate: unknown\n");
682 else
683 seq_printf(seq, "present rate: %d %s\n",
Alexey Starikovskiy7faa1442009-03-27 22:23:52 -0400684 battery->rate_now, acpi_battery_units(battery));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400686 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 seq_printf(seq, "remaining capacity: unknown\n");
688 else
689 seq_printf(seq, "remaining capacity: %d %sh\n",
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400690 battery->capacity_now, acpi_battery_units(battery));
691 if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 seq_printf(seq, "present voltage: unknown\n");
693 else
694 seq_printf(seq, "present voltage: %d mV\n",
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400695 battery->voltage_now);
Len Brown4be44fc2005-08-05 00:44:28 -0400696 end:
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400697 if (result)
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300698 seq_printf(seq, "ERROR: Unable to read battery state\n");
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300699
700 return result;
701}
702
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400703static int acpi_battery_print_alarm(struct seq_file *seq, int result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200705 struct acpi_battery *battery = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300707 if (result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 goto end;
709
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300710 if (!acpi_battery_present(battery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 seq_printf(seq, "present: no\n");
712 goto end;
713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 seq_printf(seq, "alarm: ");
715 if (!battery->alarm)
716 seq_printf(seq, "unsupported\n");
717 else
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400718 seq_printf(seq, "%u %sh\n", battery->alarm,
719 acpi_battery_units(battery));
Len Brown4be44fc2005-08-05 00:44:28 -0400720 end:
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300721 if (result)
722 seq_printf(seq, "ERROR: Unable to read battery alarm\n");
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300723 return result;
724}
725
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400726static ssize_t acpi_battery_write_alarm(struct file *file,
727 const char __user * buffer,
728 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729{
Len Brown4be44fc2005-08-05 00:44:28 -0400730 int result = 0;
731 char alarm_string[12] = { '\0' };
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200732 struct seq_file *m = file->private_data;
733 struct acpi_battery *battery = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 if (!battery || (count > sizeof(alarm_string) - 1))
Patrick Mocheld550d982006-06-27 00:41:40 -0400736 return -EINVAL;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300737 if (!acpi_battery_present(battery)) {
738 result = -ENODEV;
739 goto end;
740 }
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300741 if (copy_from_user(alarm_string, buffer, count)) {
742 result = -EFAULT;
743 goto end;
744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 alarm_string[count] = '\0';
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400746 battery->alarm = simple_strtol(alarm_string, NULL, 0);
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400747 result = acpi_battery_set_alarm(battery);
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300748 end:
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300749 if (!result)
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400750 return count;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300751 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752}
753
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400754typedef int(*print_func)(struct seq_file *seq, int result);
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400755
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400756static print_func acpi_print_funcs[ACPI_BATTERY_NUMFILES] = {
757 acpi_battery_print_info,
758 acpi_battery_print_state,
759 acpi_battery_print_alarm,
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400760};
761
762static int acpi_battery_read(int fid, struct seq_file *seq)
763{
764 struct acpi_battery *battery = seq->private;
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400765 int result = acpi_battery_update(battery);
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400766 return acpi_print_funcs[fid](seq, result);
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400767}
768
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400769#define DECLARE_FILE_FUNCTIONS(_name) \
770static int acpi_battery_read_##_name(struct seq_file *seq, void *offset) \
771{ \
772 return acpi_battery_read(_name##_tag, seq); \
773} \
774static int acpi_battery_##_name##_open_fs(struct inode *inode, struct file *file) \
775{ \
776 return single_open(file, acpi_battery_read_##_name, PDE(inode)->data); \
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400777}
778
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400779DECLARE_FILE_FUNCTIONS(info);
780DECLARE_FILE_FUNCTIONS(state);
781DECLARE_FILE_FUNCTIONS(alarm);
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400782
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400783#undef DECLARE_FILE_FUNCTIONS
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400784
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400785#define FILE_DESCRIPTION_RO(_name) \
786 { \
787 .name = __stringify(_name), \
788 .mode = S_IRUGO, \
789 .ops = { \
790 .open = acpi_battery_##_name##_open_fs, \
791 .read = seq_read, \
792 .llseek = seq_lseek, \
793 .release = single_release, \
794 .owner = THIS_MODULE, \
795 }, \
796 }
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400797
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400798#define FILE_DESCRIPTION_RW(_name) \
799 { \
800 .name = __stringify(_name), \
801 .mode = S_IFREG | S_IRUGO | S_IWUSR, \
802 .ops = { \
803 .open = acpi_battery_##_name##_open_fs, \
804 .read = seq_read, \
805 .llseek = seq_lseek, \
806 .write = acpi_battery_write_##_name, \
807 .release = single_release, \
808 .owner = THIS_MODULE, \
809 }, \
810 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400812static struct battery_file {
813 struct file_operations ops;
814 mode_t mode;
Jan Engelhardt070d8eb2009-01-12 00:07:55 +0100815 const char *name;
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400816} acpi_battery_file[] = {
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400817 FILE_DESCRIPTION_RO(info),
818 FILE_DESCRIPTION_RO(state),
819 FILE_DESCRIPTION_RW(alarm),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820};
821
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400822#undef FILE_DESCRIPTION_RO
823#undef FILE_DESCRIPTION_RW
824
Len Brown4be44fc2005-08-05 00:44:28 -0400825static int acpi_battery_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
Len Brown4be44fc2005-08-05 00:44:28 -0400827 struct proc_dir_entry *entry = NULL;
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400828 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 if (!acpi_device_dir(device)) {
831 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown4be44fc2005-08-05 00:44:28 -0400832 acpi_battery_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400834 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 }
836
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400837 for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i) {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700838 entry = proc_create_data(acpi_battery_file[i].name,
839 acpi_battery_file[i].mode,
840 acpi_device_dir(device),
841 &acpi_battery_file[i].ops,
842 acpi_driver_data(device));
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400843 if (!entry)
844 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 }
Patrick Mocheld550d982006-06-27 00:41:40 -0400846 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847}
848
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400849static void acpi_battery_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850{
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400851 int i;
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400852 if (!acpi_device_dir(device))
853 return;
854 for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i)
855 remove_proc_entry(acpi_battery_file[i].name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400858 remove_proc_entry(acpi_device_bid(device), acpi_battery_dir);
859 acpi_device_dir(device) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860}
861
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400862#endif
Alexey Starikovskiy3e58ea02007-09-26 19:43:11 +0400863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864/* --------------------------------------------------------------------------
865 Driver Interface
866 -------------------------------------------------------------------------- */
867
Bjorn Helgaasd9406692009-04-30 09:35:47 -0600868static void acpi_battery_notify(struct acpi_device *device, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
Bjorn Helgaasd9406692009-04-30 09:35:47 -0600870 struct acpi_battery *battery = acpi_driver_data(device);
Zhang Rui153e5002010-07-07 09:11:57 +0800871#ifdef CONFIG_ACPI_SYSFS_POWER
872 struct device *old;
873#endif
Bjorn Helgaasd9406692009-04-30 09:35:47 -0600874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 if (!battery)
Patrick Mocheld550d982006-06-27 00:41:40 -0400876 return;
Zhang Rui153e5002010-07-07 09:11:57 +0800877#ifdef CONFIG_ACPI_SYSFS_POWER
878 old = battery->bat.dev;
879#endif
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400880 acpi_battery_update(battery);
881 acpi_bus_generate_proc_event(device, event,
882 acpi_battery_present(battery));
883 acpi_bus_generate_netlink_event(device->pnp.device_class,
Kay Sievers07944692008-10-30 01:18:59 +0100884 dev_name(&device->dev), event,
Vladimir Lebedev9ea7d572007-02-20 15:48:06 +0300885 acpi_battery_present(battery));
Alexey Starikovskiy97749cd2008-01-01 14:27:24 -0500886#ifdef CONFIG_ACPI_SYSFS_POWER
Justin P. Mattock2345baf2009-12-13 14:42:36 -0800887 /* acpi_battery_update could remove power_supply object */
Zhang Rui153e5002010-07-07 09:11:57 +0800888 if (old && battery->bat.dev)
Alan Jenkinsf79e1ce2009-06-30 14:36:16 +0000889 power_supply_changed(&battery->bat);
Alexey Starikovskiy97749cd2008-01-01 14:27:24 -0500890#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891}
892
Len Brown4be44fc2005-08-05 00:44:28 -0400893static int acpi_battery_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
Len Brown4be44fc2005-08-05 00:44:28 -0400895 int result = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400896 struct acpi_battery *battery = NULL;
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400897 acpi_handle handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400899 return -EINVAL;
Burman Yan36bcbec2006-12-19 12:56:11 -0800900 battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 if (!battery)
Patrick Mocheld550d982006-06-27 00:41:40 -0400902 return -ENOMEM;
Patrick Mochel145def82006-05-19 16:54:39 -0400903 battery->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME);
905 strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700906 device->driver_data = battery;
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400907 mutex_init(&battery->lock);
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400908 if (ACPI_SUCCESS(acpi_get_handle(battery->device->handle,
909 "_BIX", &handle)))
910 set_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags);
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400911 acpi_battery_update(battery);
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +0300912#ifdef CONFIG_ACPI_PROCFS_POWER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 result = acpi_battery_add_fs(device);
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400914#endif
Len Brown586caae2009-06-18 00:38:27 -0400915 if (!result) {
916 printk(KERN_INFO PREFIX "%s Slot [%s] (battery %s)\n",
917 ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device),
918 device->status.battery_present ? "present" : "absent");
919 } else {
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +0300920#ifdef CONFIG_ACPI_PROCFS_POWER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 acpi_battery_remove_fs(device);
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400922#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 kfree(battery);
924 }
Patrick Mocheld550d982006-06-27 00:41:40 -0400925 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926}
927
Len Brown4be44fc2005-08-05 00:44:28 -0400928static int acpi_battery_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929{
Len Brown4be44fc2005-08-05 00:44:28 -0400930 struct acpi_battery *battery = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400933 return -EINVAL;
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200934 battery = acpi_driver_data(device);
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +0300935#ifdef CONFIG_ACPI_PROCFS_POWER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 acpi_battery_remove_fs(device);
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400937#endif
Alexey Starikovskiy97749cd2008-01-01 14:27:24 -0500938#ifdef CONFIG_ACPI_SYSFS_POWER
Andrey Borzenkov508df922007-10-28 12:50:09 +0300939 sysfs_remove_battery(battery);
Alexey Starikovskiy97749cd2008-01-01 14:27:24 -0500940#endif
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400941 mutex_destroy(&battery->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 kfree(battery);
Patrick Mocheld550d982006-06-27 00:41:40 -0400943 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944}
945
Jiri Kosina34c44152006-10-10 14:20:41 -0700946/* this is needed to learn about changes made in suspended state */
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800947static int acpi_battery_resume(struct acpi_device *device)
Jiri Kosina34c44152006-10-10 14:20:41 -0700948{
949 struct acpi_battery *battery;
Jiri Kosina34c44152006-10-10 14:20:41 -0700950 if (!device)
951 return -EINVAL;
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400952 battery = acpi_driver_data(device);
953 battery->update_time = 0;
Andrey Borzenkov508df922007-10-28 12:50:09 +0300954 acpi_battery_update(battery);
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300955 return 0;
Jiri Kosina34c44152006-10-10 14:20:41 -0700956}
957
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400958static struct acpi_driver acpi_battery_driver = {
959 .name = "battery",
960 .class = ACPI_BATTERY_CLASS,
961 .ids = battery_device_ids,
Bjorn Helgaasd9406692009-04-30 09:35:47 -0600962 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400963 .ops = {
964 .add = acpi_battery_add,
965 .resume = acpi_battery_resume,
966 .remove = acpi_battery_remove,
Bjorn Helgaasd9406692009-04-30 09:35:47 -0600967 .notify = acpi_battery_notify,
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400968 },
969};
970
Linus Torvaldsb0cbc862009-04-11 12:45:20 -0700971static void __init acpi_battery_init_async(void *unused, async_cookie_t cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
Pavel Machek4d8316d2006-08-14 22:37:22 -0700973 if (acpi_disabled)
Arjan van de Ven0f66af52009-01-10 14:19:05 -0500974 return;
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +0300975#ifdef CONFIG_ACPI_PROCFS_POWER
Rich Townsend3f86b832006-07-01 11:36:54 -0400976 acpi_battery_dir = acpi_lock_battery_dir();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 if (!acpi_battery_dir)
Arjan van de Ven0f66af52009-01-10 14:19:05 -0500978 return;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400979#endif
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400980 if (acpi_bus_register_driver(&acpi_battery_driver) < 0) {
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +0300981#ifdef CONFIG_ACPI_PROCFS_POWER
Rich Townsend3f86b832006-07-01 11:36:54 -0400982 acpi_unlock_battery_dir(acpi_battery_dir);
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400983#endif
Arjan van de Ven0f66af52009-01-10 14:19:05 -0500984 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 }
Arjan van de Ven0f66af52009-01-10 14:19:05 -0500986 return;
987}
988
989static int __init acpi_battery_init(void)
990{
991 async_schedule(acpi_battery_init_async, NULL);
Patrick Mocheld550d982006-06-27 00:41:40 -0400992 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993}
994
Len Brown4be44fc2005-08-05 00:44:28 -0400995static void __exit acpi_battery_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 acpi_bus_unregister_driver(&acpi_battery_driver);
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +0300998#ifdef CONFIG_ACPI_PROCFS_POWER
Rich Townsend3f86b832006-07-01 11:36:54 -0400999 acpi_unlock_battery_dir(acpi_battery_dir);
Alexey Starikovskiyd7380962007-09-26 19:43:04 +04001000#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001}
1002
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003module_init(acpi_battery_init);
1004module_exit(acpi_battery_exit);