blob: 611434f413dbf3feaadcdedd7fdd894ad5389e79 [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>
Kyle McMartin25be5822011-03-22 16:19:50 -040036#include <linux/suspend.h>
Alexey Starikovskiyd7380962007-09-26 19:43:04 +040037
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +030038#ifdef CONFIG_ACPI_PROCFS_POWER
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/proc_fs.h>
40#include <linux/seq_file.h>
41#include <asm/uaccess.h>
Alexey Starikovskiyd7380962007-09-26 19:43:04 +040042#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44#include <acpi/acpi_bus.h>
45#include <acpi/acpi_drivers.h>
Alexey Starikovskiyd7380962007-09-26 19:43:04 +040046#include <linux/power_supply.h>
47
Len Browna192a952009-07-28 16:45:54 -040048#define PREFIX "ACPI: "
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#define ACPI_BATTERY_VALUE_UNKNOWN 0xFFFFFFFF
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#define ACPI_BATTERY_CLASS "battery"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#define ACPI_BATTERY_DEVICE_NAME "Battery"
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#define ACPI_BATTERY_NOTIFY_STATUS 0x80
55#define ACPI_BATTERY_NOTIFY_INFO 0x81
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +040056#define ACPI_BATTERY_NOTIFY_THRESHOLD 0x82
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Lan Tianyuae6f6182011-06-30 11:32:40 +080058/* Battery power unit: 0 means mW, 1 means mA */
59#define ACPI_BATTERY_POWER_UNIT_MA 1
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#define _COMPONENT ACPI_BATTERY_COMPONENT
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +030062
Len Brownf52fd662007-02-12 22:42:12 -050063ACPI_MODULE_NAME("battery");
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Len Brownf52fd662007-02-12 22:42:12 -050065MODULE_AUTHOR("Paul Diefenbaugh");
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +040066MODULE_AUTHOR("Alexey Starikovskiy <astarikovskiy@suse.de>");
Len Brown7cda93e2007-02-12 23:50:02 -050067MODULE_DESCRIPTION("ACPI Battery Driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070068MODULE_LICENSE("GPL");
69
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +040070static unsigned int cache_time = 1000;
71module_param(cache_time, uint, 0644);
72MODULE_PARM_DESC(cache_time, "cache time in milliseconds");
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +030073
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +030074#ifdef CONFIG_ACPI_PROCFS_POWER
Rich Townsend3f86b832006-07-01 11:36:54 -040075extern struct proc_dir_entry *acpi_lock_battery_dir(void);
76extern void *acpi_unlock_battery_dir(struct proc_dir_entry *acpi_battery_dir);
77
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +040078enum acpi_battery_files {
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +040079 info_tag = 0,
80 state_tag,
81 alarm_tag,
Alexey Starikovskiy78490d82007-05-11 13:18:55 -040082 ACPI_BATTERY_NUMFILES,
83};
84
Alexey Starikovskiyd7380962007-09-26 19:43:04 +040085#endif
86
87static const struct acpi_device_id battery_device_ids[] = {
88 {"PNP0C0A", 0},
89 {"", 0},
90};
91
92MODULE_DEVICE_TABLE(acpi, battery_device_ids);
93
Alexey Starikovskiy7b3bcc42009-10-15 14:31:24 +040094enum {
95 ACPI_BATTERY_ALARM_PRESENT,
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +040096 ACPI_BATTERY_XINFO_PRESENT,
97 /* For buggy DSDTs that report negative 16-bit values for either
98 * charging or discharging current and/or report 0 as 65536
99 * due to bad math.
100 */
Alexey Starikovskiy7b3bcc42009-10-15 14:31:24 +0400101 ACPI_BATTERY_QUIRK_SIGNED16_CURRENT,
Zhang Rui557d5862010-10-22 10:02:06 +0800102 ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY,
Alexey Starikovskiy7b3bcc42009-10-15 14:31:24 +0400103};
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400104
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400105struct acpi_battery {
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400106 struct mutex lock;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400107 struct power_supply bat;
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400108 struct acpi_device *device;
Kyle McMartin25be5822011-03-22 16:19:50 -0400109 struct notifier_block pm_nb;
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400110 unsigned long update_time;
Alexey Starikovskiy7faa1442009-03-27 22:23:52 -0400111 int rate_now;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400112 int capacity_now;
113 int voltage_now;
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400114 int design_capacity;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400115 int full_charge_capacity;
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400116 int technology;
117 int design_voltage;
118 int design_capacity_warning;
119 int design_capacity_low;
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400120 int cycle_count;
121 int measurement_accuracy;
122 int max_sampling_time;
123 int min_sampling_time;
124 int max_averaging_interval;
125 int min_averaging_interval;
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400126 int capacity_granularity_1;
127 int capacity_granularity_2;
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400128 int alarm;
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400129 char model_number[32];
130 char serial_number[32];
131 char type[32];
132 char oem_info[32];
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400133 int state;
134 int power_unit;
Alexey Starikovskiy7b3bcc42009-10-15 14:31:24 +0400135 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136};
137
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400138#define to_acpi_battery(x) container_of(x, struct acpi_battery, bat);
139
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400140inline int acpi_battery_present(struct acpi_battery *battery)
141{
142 return battery->device->status.battery_present;
143}
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400144
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{
Rafael J. Wysockia1b4bd62010-10-23 19:35:15 +0200189 int ret = 0;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400190 struct acpi_battery *battery = to_acpi_battery(psy);
191
Alexey Starikovskiy91044762007-11-13 12:23:06 +0300192 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)
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400196 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;
Richard Hughes56f382a2009-01-25 15:05:50 +0000203 else if (acpi_battery_is_charged(battery))
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400204 val->intval = POWER_SUPPLY_STATUS_FULL;
Roland Dreier4c41d3a2007-11-07 15:09:09 -0800205 else
206 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400207 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;
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400214 case POWER_SUPPLY_PROP_CYCLE_COUNT:
215 val->intval = battery->cycle_count;
216 break;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400217 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
Rafael J. Wysockia1b4bd62010-10-23 19:35:15 +0200218 if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
219 ret = -ENODEV;
220 else
221 val->intval = battery->design_voltage * 1000;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400222 break;
223 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
Rafael J. Wysockia1b4bd62010-10-23 19:35:15 +0200224 if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN)
225 ret = -ENODEV;
226 else
227 val->intval = battery->voltage_now * 1000;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400228 break;
229 case POWER_SUPPLY_PROP_CURRENT_NOW:
Alexey Starikovskiy7faa1442009-03-27 22:23:52 -0400230 case POWER_SUPPLY_PROP_POWER_NOW:
Rafael J. Wysockia1b4bd62010-10-23 19:35:15 +0200231 if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN)
232 ret = -ENODEV;
233 else
234 val->intval = battery->rate_now * 1000;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400235 break;
236 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
237 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
Rafael J. Wysockia1b4bd62010-10-23 19:35:15 +0200238 if (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
239 ret = -ENODEV;
240 else
241 val->intval = battery->design_capacity * 1000;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400242 break;
243 case POWER_SUPPLY_PROP_CHARGE_FULL:
244 case POWER_SUPPLY_PROP_ENERGY_FULL:
Rafael J. Wysockia1b4bd62010-10-23 19:35:15 +0200245 if (battery->full_charge_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
246 ret = -ENODEV;
247 else
248 val->intval = battery->full_charge_capacity * 1000;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400249 break;
250 case POWER_SUPPLY_PROP_CHARGE_NOW:
251 case POWER_SUPPLY_PROP_ENERGY_NOW:
Rafael J. Wysockia1b4bd62010-10-23 19:35:15 +0200252 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN)
253 ret = -ENODEV;
254 else
255 val->intval = battery->capacity_now * 1000;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400256 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;
maximilian attems7c2670b2008-01-22 18:46:50 +0100263 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
264 val->strval = battery->serial_number;
265 break;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400266 default:
Rafael J. Wysockia1b4bd62010-10-23 19:35:15 +0200267 ret = -EINVAL;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400268 }
Rafael J. Wysockia1b4bd62010-10-23 19:35:15 +0200269 return ret;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400270}
271
272static enum power_supply_property charge_battery_props[] = {
273 POWER_SUPPLY_PROP_STATUS,
274 POWER_SUPPLY_PROP_PRESENT,
275 POWER_SUPPLY_PROP_TECHNOLOGY,
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400276 POWER_SUPPLY_PROP_CYCLE_COUNT,
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400277 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,
maximilian attems7c2670b2008-01-22 18:46:50 +0100285 POWER_SUPPLY_PROP_SERIAL_NUMBER,
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400286};
287
288static enum power_supply_property energy_battery_props[] = {
289 POWER_SUPPLY_PROP_STATUS,
290 POWER_SUPPLY_PROP_PRESENT,
291 POWER_SUPPLY_PROP_TECHNOLOGY,
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400292 POWER_SUPPLY_PROP_CYCLE_COUNT,
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400293 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
294 POWER_SUPPLY_PROP_VOLTAGE_NOW,
Alexey Starikovskiy7faa1442009-03-27 22:23:52 -0400295 POWER_SUPPLY_PROP_POWER_NOW,
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400296 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,
maximilian attems7c2670b2008-01-22 18:46:50 +0100301 POWER_SUPPLY_PROP_SERIAL_NUMBER,
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400302};
303
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +0300304#ifdef CONFIG_ACPI_PROCFS_POWER
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400305inline char *acpi_battery_units(struct acpi_battery *battery)
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400306{
Lan Tianyuae6f6182011-06-30 11:32:40 +0800307 return (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA) ?
308 "mA" : "mW";
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400309}
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400310#endif
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312/* --------------------------------------------------------------------------
313 Battery Management
314 -------------------------------------------------------------------------- */
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400315struct acpi_offsets {
316 size_t offset; /* offset inside struct acpi_sbs_battery */
317 u8 mode; /* int or string? */
318};
319
320static struct acpi_offsets state_offsets[] = {
321 {offsetof(struct acpi_battery, state), 0},
Alexey Starikovskiy7faa1442009-03-27 22:23:52 -0400322 {offsetof(struct acpi_battery, rate_now), 0},
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400323 {offsetof(struct acpi_battery, capacity_now), 0},
324 {offsetof(struct acpi_battery, voltage_now), 0},
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400325};
326
327static struct acpi_offsets info_offsets[] = {
328 {offsetof(struct acpi_battery, power_unit), 0},
329 {offsetof(struct acpi_battery, design_capacity), 0},
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400330 {offsetof(struct acpi_battery, full_charge_capacity), 0},
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400331 {offsetof(struct acpi_battery, technology), 0},
332 {offsetof(struct acpi_battery, design_voltage), 0},
333 {offsetof(struct acpi_battery, design_capacity_warning), 0},
334 {offsetof(struct acpi_battery, design_capacity_low), 0},
335 {offsetof(struct acpi_battery, capacity_granularity_1), 0},
336 {offsetof(struct acpi_battery, capacity_granularity_2), 0},
337 {offsetof(struct acpi_battery, model_number), 1},
338 {offsetof(struct acpi_battery, serial_number), 1},
339 {offsetof(struct acpi_battery, type), 1},
340 {offsetof(struct acpi_battery, oem_info), 1},
341};
342
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400343static struct acpi_offsets extended_info_offsets[] = {
344 {offsetof(struct acpi_battery, power_unit), 0},
345 {offsetof(struct acpi_battery, design_capacity), 0},
346 {offsetof(struct acpi_battery, full_charge_capacity), 0},
347 {offsetof(struct acpi_battery, technology), 0},
348 {offsetof(struct acpi_battery, design_voltage), 0},
349 {offsetof(struct acpi_battery, design_capacity_warning), 0},
350 {offsetof(struct acpi_battery, design_capacity_low), 0},
351 {offsetof(struct acpi_battery, cycle_count), 0},
352 {offsetof(struct acpi_battery, measurement_accuracy), 0},
353 {offsetof(struct acpi_battery, max_sampling_time), 0},
354 {offsetof(struct acpi_battery, min_sampling_time), 0},
355 {offsetof(struct acpi_battery, max_averaging_interval), 0},
356 {offsetof(struct acpi_battery, min_averaging_interval), 0},
357 {offsetof(struct acpi_battery, capacity_granularity_1), 0},
358 {offsetof(struct acpi_battery, capacity_granularity_2), 0},
359 {offsetof(struct acpi_battery, model_number), 1},
360 {offsetof(struct acpi_battery, serial_number), 1},
361 {offsetof(struct acpi_battery, type), 1},
362 {offsetof(struct acpi_battery, oem_info), 1},
363};
364
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400365static int extract_package(struct acpi_battery *battery,
366 union acpi_object *package,
367 struct acpi_offsets *offsets, int num)
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300368{
Alexey Starikovskiy106449e2007-10-29 23:29:40 +0300369 int i;
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400370 union acpi_object *element;
371 if (package->type != ACPI_TYPE_PACKAGE)
372 return -EFAULT;
373 for (i = 0; i < num; ++i) {
374 if (package->package.count <= i)
375 return -EFAULT;
376 element = &package->package.elements[i];
377 if (offsets[i].mode) {
Alexey Starikovskiy106449e2007-10-29 23:29:40 +0300378 u8 *ptr = (u8 *)battery + offsets[i].offset;
379 if (element->type == ACPI_TYPE_STRING ||
380 element->type == ACPI_TYPE_BUFFER)
381 strncpy(ptr, element->string.pointer, 32);
382 else if (element->type == ACPI_TYPE_INTEGER) {
383 strncpy(ptr, (u8 *)&element->integer.value,
Lin Ming439913f2010-01-28 10:53:19 +0800384 sizeof(u64));
385 ptr[sizeof(u64)] = 0;
Alexey Starikovskiyb8a1bdb2008-03-17 22:37:42 -0400386 } else
387 *ptr = 0; /* don't have value */
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400388 } else {
Alexey Starikovskiyb8a1bdb2008-03-17 22:37:42 -0400389 int *x = (int *)((u8 *)battery + offsets[i].offset);
390 *x = (element->type == ACPI_TYPE_INTEGER) ?
391 element->integer.value : -1;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300392 }
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300393 }
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300394 return 0;
395}
396
397static int acpi_battery_get_status(struct acpi_battery *battery)
398{
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400399 if (acpi_bus_get_status(battery->device)) {
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300400 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Evaluating _STA"));
401 return -ENODEV;
402 }
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400403 return 0;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300404}
405
406static int acpi_battery_get_info(struct acpi_battery *battery)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400408 int result = -EFAULT;
Len Brown4be44fc2005-08-05 00:44:28 -0400409 acpi_status status = 0;
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400410 char *name = test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags)?
411 "_BIX" : "_BIF";
412
Len Brown4be44fc2005-08-05 00:44:28 -0400413 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300415 if (!acpi_battery_present(battery))
416 return 0;
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400417 mutex_lock(&battery->lock);
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400418 status = acpi_evaluate_object(battery->device->handle, name,
419 NULL, &buffer);
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400420 mutex_unlock(&battery->lock);
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 if (ACPI_FAILURE(status)) {
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400423 ACPI_EXCEPTION((AE_INFO, status, "Evaluating %s", name));
Patrick Mocheld550d982006-06-27 00:41:40 -0400424 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 }
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400426 if (test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags))
427 result = extract_package(battery, buffer.pointer,
428 extended_info_offsets,
429 ARRAY_SIZE(extended_info_offsets));
430 else
431 result = extract_package(battery, buffer.pointer,
432 info_offsets, ARRAY_SIZE(info_offsets));
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400433 kfree(buffer.pointer);
Zhang Rui557d5862010-10-22 10:02:06 +0800434 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags))
435 battery->full_charge_capacity = battery->design_capacity;
Patrick Mocheld550d982006-06-27 00:41:40 -0400436 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437}
438
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300439static int acpi_battery_get_state(struct acpi_battery *battery)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
Len Brown4be44fc2005-08-05 00:44:28 -0400441 int result = 0;
442 acpi_status status = 0;
443 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300445 if (!acpi_battery_present(battery))
446 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400448 if (battery->update_time &&
449 time_before(jiffies, battery->update_time +
450 msecs_to_jiffies(cache_time)))
451 return 0;
452
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400453 mutex_lock(&battery->lock);
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400454 status = acpi_evaluate_object(battery->device->handle, "_BST",
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400455 NULL, &buffer);
456 mutex_unlock(&battery->lock);
Len Brown5b31d892007-08-15 00:19:26 -0400457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400459 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BST"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400460 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 }
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400462
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400463 result = extract_package(battery, buffer.pointer,
464 state_offsets, ARRAY_SIZE(state_offsets));
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400465 battery->update_time = jiffies;
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400466 kfree(buffer.pointer);
Hector Martinbc76f902009-08-06 15:57:48 -0700467
Alexey Starikovskiy7b3bcc42009-10-15 14:31:24 +0400468 if (test_bit(ACPI_BATTERY_QUIRK_SIGNED16_CURRENT, &battery->flags) &&
Hector Martinbc76f902009-08-06 15:57:48 -0700469 battery->rate_now != -1)
470 battery->rate_now = abs((s16)battery->rate_now);
471
Zhang Rui557d5862010-10-22 10:02:06 +0800472 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags)
473 && battery->capacity_now >= 0 && battery->capacity_now <= 100)
474 battery->capacity_now = (battery->capacity_now *
475 battery->full_charge_capacity) / 100;
Patrick Mocheld550d982006-06-27 00:41:40 -0400476 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477}
478
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400479static int acpi_battery_set_alarm(struct acpi_battery *battery)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
Len Brown4be44fc2005-08-05 00:44:28 -0400481 acpi_status status = 0;
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400482 union acpi_object arg0 = { .type = ACPI_TYPE_INTEGER };
Len Brown4be44fc2005-08-05 00:44:28 -0400483 struct acpi_object_list arg_list = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400485 if (!acpi_battery_present(battery) ||
Alexey Starikovskiy7b3bcc42009-10-15 14:31:24 +0400486 !test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags))
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300487 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400489 arg0.integer.value = battery->alarm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400491 mutex_lock(&battery->lock);
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400492 status = acpi_evaluate_object(battery->device->handle, "_BTP",
493 &arg_list, NULL);
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400494 mutex_unlock(&battery->lock);
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400497 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400499 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", battery->alarm));
Patrick Mocheld550d982006-06-27 00:41:40 -0400500 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501}
502
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300503static int acpi_battery_init_alarm(struct acpi_battery *battery)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
Len Brown4be44fc2005-08-05 00:44:28 -0400505 acpi_status status = AE_OK;
506 acpi_handle handle = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300508 /* See if alarms are supported, and if so, set default */
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400509 status = acpi_get_handle(battery->device->handle, "_BTP", &handle);
510 if (ACPI_FAILURE(status)) {
Alexey Starikovskiy7b3bcc42009-10-15 14:31:24 +0400511 clear_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400512 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 }
Alexey Starikovskiy7b3bcc42009-10-15 14:31:24 +0400514 set_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400515 if (!battery->alarm)
516 battery->alarm = battery->design_capacity_warning;
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400517 return acpi_battery_set_alarm(battery);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518}
519
Andrey Borzenkov508df922007-10-28 12:50:09 +0300520static ssize_t acpi_battery_alarm_show(struct device *dev,
521 struct device_attribute *attr,
522 char *buf)
523{
524 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
525 return sprintf(buf, "%d\n", battery->alarm * 1000);
526}
527
528static ssize_t acpi_battery_alarm_store(struct device *dev,
529 struct device_attribute *attr,
530 const char *buf, size_t count)
531{
532 unsigned long x;
533 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
534 if (sscanf(buf, "%ld\n", &x) == 1)
535 battery->alarm = x/1000;
536 if (acpi_battery_present(battery))
537 acpi_battery_set_alarm(battery);
538 return count;
539}
540
541static struct device_attribute alarm_attr = {
Parag Warudkar01e8ef12008-10-18 20:28:50 -0700542 .attr = {.name = "alarm", .mode = 0644},
Andrey Borzenkov508df922007-10-28 12:50:09 +0300543 .show = acpi_battery_alarm_show,
544 .store = acpi_battery_alarm_store,
545};
546
547static int sysfs_add_battery(struct acpi_battery *battery)
548{
549 int result;
550
Lan Tianyuae6f6182011-06-30 11:32:40 +0800551 if (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA) {
Andrey Borzenkov508df922007-10-28 12:50:09 +0300552 battery->bat.properties = charge_battery_props;
553 battery->bat.num_properties =
554 ARRAY_SIZE(charge_battery_props);
555 } else {
556 battery->bat.properties = energy_battery_props;
557 battery->bat.num_properties =
558 ARRAY_SIZE(energy_battery_props);
559 }
560
561 battery->bat.name = acpi_device_bid(battery->device);
562 battery->bat.type = POWER_SUPPLY_TYPE_BATTERY;
563 battery->bat.get_property = acpi_battery_get_property;
564
565 result = power_supply_register(&battery->device->dev, &battery->bat);
566 if (result)
567 return result;
568 return device_create_file(battery->bat.dev, &alarm_attr);
569}
570
571static void sysfs_remove_battery(struct acpi_battery *battery)
572{
573 if (!battery->bat.dev)
574 return;
575 device_remove_file(battery->bat.dev, &alarm_attr);
576 power_supply_unregister(&battery->bat);
Alexey Starikovskiy91044762007-11-13 12:23:06 +0300577 battery->bat.dev = NULL;
Andrey Borzenkov508df922007-10-28 12:50:09 +0300578}
579
Hector Martinbc76f902009-08-06 15:57:48 -0700580static void acpi_battery_quirks(struct acpi_battery *battery)
581{
Lan Tianyuae6f6182011-06-30 11:32:40 +0800582 if (dmi_name_in_vendors("Acer") &&
583 battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA) {
Alexey Starikovskiy7b3bcc42009-10-15 14:31:24 +0400584 set_bit(ACPI_BATTERY_QUIRK_SIGNED16_CURRENT, &battery->flags);
Hector Martinbc76f902009-08-06 15:57:48 -0700585 }
586}
587
Zhang Rui557d5862010-10-22 10:02:06 +0800588/*
589 * According to the ACPI spec, some kinds of primary batteries can
590 * report percentage battery remaining capacity directly to OS.
591 * In this case, it reports the Last Full Charged Capacity == 100
592 * and BatteryPresentRate == 0xFFFFFFFF.
593 *
594 * Now we found some battery reports percentage remaining capacity
595 * even if it's rechargeable.
596 * https://bugzilla.kernel.org/show_bug.cgi?id=15979
597 *
598 * Handle this correctly so that they won't break userspace.
599 */
600static void acpi_battery_quirks2(struct acpi_battery *battery)
601{
602 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags))
603 return ;
604
605 if (battery->full_charge_capacity == 100 &&
606 battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN &&
607 battery->capacity_now >=0 && battery->capacity_now <= 100) {
608 set_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags);
609 battery->full_charge_capacity = battery->design_capacity;
610 battery->capacity_now = (battery->capacity_now *
611 battery->full_charge_capacity) / 100;
612 }
613}
614
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400615static int acpi_battery_update(struct acpi_battery *battery)
Vladimir Lebedev4bd35cd2007-02-10 01:43:48 -0500616{
Alexey Starikovskiy50b17852008-12-23 02:44:54 +0300617 int result, old_present = acpi_battery_present(battery);
Alexey Starikovskiy97749cd2008-01-01 14:27:24 -0500618 result = acpi_battery_get_status(battery);
Andrey Borzenkov508df922007-10-28 12:50:09 +0300619 if (result)
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300620 return result;
Andrey Borzenkov508df922007-10-28 12:50:09 +0300621 if (!acpi_battery_present(battery)) {
622 sysfs_remove_battery(battery);
Alexey Starikovskiy97749cd2008-01-01 14:27:24 -0500623 battery->update_time = 0;
Andrey Borzenkov508df922007-10-28 12:50:09 +0300624 return 0;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300625 }
Alexey Starikovskiy50b17852008-12-23 02:44:54 +0300626 if (!battery->update_time ||
627 old_present != acpi_battery_present(battery)) {
Alexey Starikovskiy97749cd2008-01-01 14:27:24 -0500628 result = acpi_battery_get_info(battery);
629 if (result)
630 return result;
Hector Martinbc76f902009-08-06 15:57:48 -0700631 acpi_battery_quirks(battery);
Alexey Starikovskiy97749cd2008-01-01 14:27:24 -0500632 acpi_battery_init_alarm(battery);
633 }
Andrey Borzenkov508df922007-10-28 12:50:09 +0300634 if (!battery->bat.dev)
635 sysfs_add_battery(battery);
Zhang Rui557d5862010-10-22 10:02:06 +0800636 result = acpi_battery_get_state(battery);
637 acpi_battery_quirks2(battery);
638 return result;
Vladimir Lebedev4bd35cd2007-02-10 01:43:48 -0500639}
640
Rafael J. Wysockida8aeb92011-01-06 23:42:27 +0100641static void acpi_battery_refresh(struct acpi_battery *battery)
642{
643 if (!battery->bat.dev)
644 return;
645
646 acpi_battery_get_info(battery);
647 /* The battery may have changed its reporting units. */
648 sysfs_remove_battery(battery);
649 sysfs_add_battery(battery);
650}
651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652/* --------------------------------------------------------------------------
653 FS Interface (/proc)
654 -------------------------------------------------------------------------- */
655
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +0300656#ifdef CONFIG_ACPI_PROCFS_POWER
Len Brown4be44fc2005-08-05 00:44:28 -0400657static struct proc_dir_entry *acpi_battery_dir;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300658
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400659static int acpi_battery_print_info(struct seq_file *seq, int result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200661 struct acpi_battery *battery = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300663 if (result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 goto end;
665
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400666 seq_printf(seq, "present: %s\n",
667 acpi_battery_present(battery)?"yes":"no");
668 if (!acpi_battery_present(battery))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 goto end;
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400670 if (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 seq_printf(seq, "design capacity: unknown\n");
672 else
673 seq_printf(seq, "design capacity: %d %sh\n",
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400674 battery->design_capacity,
675 acpi_battery_units(battery));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400677 if (battery->full_charge_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 seq_printf(seq, "last full capacity: unknown\n");
679 else
680 seq_printf(seq, "last full capacity: %d %sh\n",
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400681 battery->full_charge_capacity,
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400682 acpi_battery_units(battery));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400684 seq_printf(seq, "battery technology: %srechargeable\n",
685 (!battery->technology)?"non-":"");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400687 if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 seq_printf(seq, "design voltage: unknown\n");
689 else
690 seq_printf(seq, "design voltage: %d mV\n",
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400691 battery->design_voltage);
Len Brown4be44fc2005-08-05 00:44:28 -0400692 seq_printf(seq, "design capacity warning: %d %sh\n",
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400693 battery->design_capacity_warning,
694 acpi_battery_units(battery));
Len Brown4be44fc2005-08-05 00:44:28 -0400695 seq_printf(seq, "design capacity low: %d %sh\n",
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400696 battery->design_capacity_low,
697 acpi_battery_units(battery));
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400698 seq_printf(seq, "cycle count: %i\n", battery->cycle_count);
Len Brown4be44fc2005-08-05 00:44:28 -0400699 seq_printf(seq, "capacity granularity 1: %d %sh\n",
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400700 battery->capacity_granularity_1,
701 acpi_battery_units(battery));
Len Brown4be44fc2005-08-05 00:44:28 -0400702 seq_printf(seq, "capacity granularity 2: %d %sh\n",
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400703 battery->capacity_granularity_2,
704 acpi_battery_units(battery));
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400705 seq_printf(seq, "model number: %s\n", battery->model_number);
706 seq_printf(seq, "serial number: %s\n", battery->serial_number);
707 seq_printf(seq, "battery type: %s\n", battery->type);
708 seq_printf(seq, "OEM info: %s\n", battery->oem_info);
Len Brown4be44fc2005-08-05 00:44:28 -0400709 end:
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300710 if (result)
711 seq_printf(seq, "ERROR: Unable to read battery info\n");
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300712 return result;
713}
714
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400715static int acpi_battery_print_state(struct seq_file *seq, int result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200717 struct acpi_battery *battery = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300719 if (result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 goto end;
721
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400722 seq_printf(seq, "present: %s\n",
723 acpi_battery_present(battery)?"yes":"no");
724 if (!acpi_battery_present(battery))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400727 seq_printf(seq, "capacity state: %s\n",
728 (battery->state & 0x04)?"critical":"ok");
729 if ((battery->state & 0x01) && (battery->state & 0x02))
Len Brown4be44fc2005-08-05 00:44:28 -0400730 seq_printf(seq,
731 "charging state: charging/discharging\n");
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400732 else if (battery->state & 0x01)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 seq_printf(seq, "charging state: discharging\n");
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400734 else if (battery->state & 0x02)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 seq_printf(seq, "charging state: charging\n");
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400736 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 seq_printf(seq, "charging state: charged\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Alexey Starikovskiy7faa1442009-03-27 22:23:52 -0400739 if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 seq_printf(seq, "present rate: unknown\n");
741 else
742 seq_printf(seq, "present rate: %d %s\n",
Alexey Starikovskiy7faa1442009-03-27 22:23:52 -0400743 battery->rate_now, acpi_battery_units(battery));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400745 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 seq_printf(seq, "remaining capacity: unknown\n");
747 else
748 seq_printf(seq, "remaining capacity: %d %sh\n",
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400749 battery->capacity_now, acpi_battery_units(battery));
750 if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 seq_printf(seq, "present voltage: unknown\n");
752 else
753 seq_printf(seq, "present voltage: %d mV\n",
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400754 battery->voltage_now);
Len Brown4be44fc2005-08-05 00:44:28 -0400755 end:
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400756 if (result)
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300757 seq_printf(seq, "ERROR: Unable to read battery state\n");
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300758
759 return result;
760}
761
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400762static int acpi_battery_print_alarm(struct seq_file *seq, int result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200764 struct acpi_battery *battery = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300766 if (result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 goto end;
768
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300769 if (!acpi_battery_present(battery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 seq_printf(seq, "present: no\n");
771 goto end;
772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 seq_printf(seq, "alarm: ");
774 if (!battery->alarm)
775 seq_printf(seq, "unsupported\n");
776 else
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400777 seq_printf(seq, "%u %sh\n", battery->alarm,
778 acpi_battery_units(battery));
Len Brown4be44fc2005-08-05 00:44:28 -0400779 end:
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300780 if (result)
781 seq_printf(seq, "ERROR: Unable to read battery alarm\n");
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300782 return result;
783}
784
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400785static ssize_t acpi_battery_write_alarm(struct file *file,
786 const char __user * buffer,
787 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
Len Brown4be44fc2005-08-05 00:44:28 -0400789 int result = 0;
790 char alarm_string[12] = { '\0' };
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200791 struct seq_file *m = file->private_data;
792 struct acpi_battery *battery = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 if (!battery || (count > sizeof(alarm_string) - 1))
Patrick Mocheld550d982006-06-27 00:41:40 -0400795 return -EINVAL;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300796 if (!acpi_battery_present(battery)) {
797 result = -ENODEV;
798 goto end;
799 }
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300800 if (copy_from_user(alarm_string, buffer, count)) {
801 result = -EFAULT;
802 goto end;
803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 alarm_string[count] = '\0';
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400805 battery->alarm = simple_strtol(alarm_string, NULL, 0);
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400806 result = acpi_battery_set_alarm(battery);
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300807 end:
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300808 if (!result)
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400809 return count;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300810 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811}
812
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400813typedef int(*print_func)(struct seq_file *seq, int result);
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400814
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400815static print_func acpi_print_funcs[ACPI_BATTERY_NUMFILES] = {
816 acpi_battery_print_info,
817 acpi_battery_print_state,
818 acpi_battery_print_alarm,
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400819};
820
821static int acpi_battery_read(int fid, struct seq_file *seq)
822{
823 struct acpi_battery *battery = seq->private;
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400824 int result = acpi_battery_update(battery);
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400825 return acpi_print_funcs[fid](seq, result);
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400826}
827
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400828#define DECLARE_FILE_FUNCTIONS(_name) \
829static int acpi_battery_read_##_name(struct seq_file *seq, void *offset) \
830{ \
831 return acpi_battery_read(_name##_tag, seq); \
832} \
833static int acpi_battery_##_name##_open_fs(struct inode *inode, struct file *file) \
834{ \
835 return single_open(file, acpi_battery_read_##_name, PDE(inode)->data); \
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400836}
837
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400838DECLARE_FILE_FUNCTIONS(info);
839DECLARE_FILE_FUNCTIONS(state);
840DECLARE_FILE_FUNCTIONS(alarm);
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400841
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400842#undef DECLARE_FILE_FUNCTIONS
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400843
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400844#define FILE_DESCRIPTION_RO(_name) \
845 { \
846 .name = __stringify(_name), \
847 .mode = S_IRUGO, \
848 .ops = { \
849 .open = acpi_battery_##_name##_open_fs, \
850 .read = seq_read, \
851 .llseek = seq_lseek, \
852 .release = single_release, \
853 .owner = THIS_MODULE, \
854 }, \
855 }
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400856
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400857#define FILE_DESCRIPTION_RW(_name) \
858 { \
859 .name = __stringify(_name), \
860 .mode = S_IFREG | S_IRUGO | S_IWUSR, \
861 .ops = { \
862 .open = acpi_battery_##_name##_open_fs, \
863 .read = seq_read, \
864 .llseek = seq_lseek, \
865 .write = acpi_battery_write_##_name, \
866 .release = single_release, \
867 .owner = THIS_MODULE, \
868 }, \
869 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400871static struct battery_file {
872 struct file_operations ops;
873 mode_t mode;
Jan Engelhardt070d8eb2009-01-12 00:07:55 +0100874 const char *name;
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400875} acpi_battery_file[] = {
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400876 FILE_DESCRIPTION_RO(info),
877 FILE_DESCRIPTION_RO(state),
878 FILE_DESCRIPTION_RW(alarm),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879};
880
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400881#undef FILE_DESCRIPTION_RO
882#undef FILE_DESCRIPTION_RW
883
Len Brown4be44fc2005-08-05 00:44:28 -0400884static int acpi_battery_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885{
Len Brown4be44fc2005-08-05 00:44:28 -0400886 struct proc_dir_entry *entry = NULL;
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400887 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Zhang Rui6d855fc2011-01-10 11:16:30 +0800889 printk(KERN_WARNING PREFIX "Deprecated procfs I/F for battery is loaded,"
890 " please retry with CONFIG_ACPI_PROCFS_POWER cleared\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 if (!acpi_device_dir(device)) {
892 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown4be44fc2005-08-05 00:44:28 -0400893 acpi_battery_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400895 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 }
897
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400898 for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i) {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700899 entry = proc_create_data(acpi_battery_file[i].name,
900 acpi_battery_file[i].mode,
901 acpi_device_dir(device),
902 &acpi_battery_file[i].ops,
903 acpi_driver_data(device));
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400904 if (!entry)
905 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 }
Patrick Mocheld550d982006-06-27 00:41:40 -0400907 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908}
909
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400910static void acpi_battery_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911{
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400912 int i;
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400913 if (!acpi_device_dir(device))
914 return;
915 for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i)
916 remove_proc_entry(acpi_battery_file[i].name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400919 remove_proc_entry(acpi_device_bid(device), acpi_battery_dir);
920 acpi_device_dir(device) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921}
922
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400923#endif
Alexey Starikovskiy3e58ea02007-09-26 19:43:11 +0400924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925/* --------------------------------------------------------------------------
926 Driver Interface
927 -------------------------------------------------------------------------- */
928
Bjorn Helgaasd9406692009-04-30 09:35:47 -0600929static void acpi_battery_notify(struct acpi_device *device, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
Bjorn Helgaasd9406692009-04-30 09:35:47 -0600931 struct acpi_battery *battery = acpi_driver_data(device);
Zhang Rui153e5002010-07-07 09:11:57 +0800932 struct device *old;
Bjorn Helgaasd9406692009-04-30 09:35:47 -0600933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 if (!battery)
Patrick Mocheld550d982006-06-27 00:41:40 -0400935 return;
Zhang Rui153e5002010-07-07 09:11:57 +0800936 old = battery->bat.dev;
Rafael J. Wysockida8aeb92011-01-06 23:42:27 +0100937 if (event == ACPI_BATTERY_NOTIFY_INFO)
938 acpi_battery_refresh(battery);
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400939 acpi_battery_update(battery);
940 acpi_bus_generate_proc_event(device, event,
941 acpi_battery_present(battery));
942 acpi_bus_generate_netlink_event(device->pnp.device_class,
Kay Sievers07944692008-10-30 01:18:59 +0100943 dev_name(&device->dev), event,
Vladimir Lebedev9ea7d572007-02-20 15:48:06 +0300944 acpi_battery_present(battery));
Justin P. Mattock2345baf2009-12-13 14:42:36 -0800945 /* acpi_battery_update could remove power_supply object */
Zhang Rui153e5002010-07-07 09:11:57 +0800946 if (old && battery->bat.dev)
Alan Jenkinsf79e1ce2009-06-30 14:36:16 +0000947 power_supply_changed(&battery->bat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948}
949
Kyle McMartin25be5822011-03-22 16:19:50 -0400950static int battery_notify(struct notifier_block *nb,
951 unsigned long mode, void *_unused)
952{
953 struct acpi_battery *battery = container_of(nb, struct acpi_battery,
954 pm_nb);
955 switch (mode) {
956 case PM_POST_SUSPEND:
957 sysfs_remove_battery(battery);
958 sysfs_add_battery(battery);
959 break;
960 }
961
962 return 0;
963}
964
Len Brown4be44fc2005-08-05 00:44:28 -0400965static int acpi_battery_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966{
Len Brown4be44fc2005-08-05 00:44:28 -0400967 int result = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400968 struct acpi_battery *battery = NULL;
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400969 acpi_handle handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400971 return -EINVAL;
Burman Yan36bcbec2006-12-19 12:56:11 -0800972 battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 if (!battery)
Patrick Mocheld550d982006-06-27 00:41:40 -0400974 return -ENOMEM;
Patrick Mochel145def82006-05-19 16:54:39 -0400975 battery->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME);
977 strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700978 device->driver_data = battery;
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400979 mutex_init(&battery->lock);
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400980 if (ACPI_SUCCESS(acpi_get_handle(battery->device->handle,
981 "_BIX", &handle)))
982 set_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags);
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400983 acpi_battery_update(battery);
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +0300984#ifdef CONFIG_ACPI_PROCFS_POWER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 result = acpi_battery_add_fs(device);
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400986#endif
Len Brown586caae2009-06-18 00:38:27 -0400987 if (!result) {
988 printk(KERN_INFO PREFIX "%s Slot [%s] (battery %s)\n",
989 ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device),
990 device->status.battery_present ? "present" : "absent");
991 } else {
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +0300992#ifdef CONFIG_ACPI_PROCFS_POWER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 acpi_battery_remove_fs(device);
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400994#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 kfree(battery);
996 }
Kyle McMartin25be5822011-03-22 16:19:50 -0400997
998 battery->pm_nb.notifier_call = battery_notify;
999 register_pm_notifier(&battery->pm_nb);
1000
Patrick Mocheld550d982006-06-27 00:41:40 -04001001 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002}
1003
Len Brown4be44fc2005-08-05 00:44:28 -04001004static int acpi_battery_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005{
Len Brown4be44fc2005-08-05 00:44:28 -04001006 struct acpi_battery *battery = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001009 return -EINVAL;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001010 battery = acpi_driver_data(device);
Kyle McMartin25be5822011-03-22 16:19:50 -04001011 unregister_pm_notifier(&battery->pm_nb);
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +03001012#ifdef CONFIG_ACPI_PROCFS_POWER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 acpi_battery_remove_fs(device);
Alexey Starikovskiyd7380962007-09-26 19:43:04 +04001014#endif
Andrey Borzenkov508df922007-10-28 12:50:09 +03001015 sysfs_remove_battery(battery);
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +04001016 mutex_destroy(&battery->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 kfree(battery);
Patrick Mocheld550d982006-06-27 00:41:40 -04001018 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019}
1020
Jiri Kosina34c44152006-10-10 14:20:41 -07001021/* this is needed to learn about changes made in suspended state */
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001022static int acpi_battery_resume(struct acpi_device *device)
Jiri Kosina34c44152006-10-10 14:20:41 -07001023{
1024 struct acpi_battery *battery;
Jiri Kosina34c44152006-10-10 14:20:41 -07001025 if (!device)
1026 return -EINVAL;
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +04001027 battery = acpi_driver_data(device);
1028 battery->update_time = 0;
Andrey Borzenkov508df922007-10-28 12:50:09 +03001029 acpi_battery_update(battery);
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +03001030 return 0;
Jiri Kosina34c44152006-10-10 14:20:41 -07001031}
1032
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +04001033static struct acpi_driver acpi_battery_driver = {
1034 .name = "battery",
1035 .class = ACPI_BATTERY_CLASS,
1036 .ids = battery_device_ids,
Bjorn Helgaasd9406692009-04-30 09:35:47 -06001037 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +04001038 .ops = {
1039 .add = acpi_battery_add,
1040 .resume = acpi_battery_resume,
1041 .remove = acpi_battery_remove,
Bjorn Helgaasd9406692009-04-30 09:35:47 -06001042 .notify = acpi_battery_notify,
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +04001043 },
1044};
1045
Linus Torvaldsb0cbc862009-04-11 12:45:20 -07001046static void __init acpi_battery_init_async(void *unused, async_cookie_t cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047{
Pavel Machek4d8316d2006-08-14 22:37:22 -07001048 if (acpi_disabled)
Arjan van de Ven0f66af52009-01-10 14:19:05 -05001049 return;
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +03001050#ifdef CONFIG_ACPI_PROCFS_POWER
Rich Townsend3f86b832006-07-01 11:36:54 -04001051 acpi_battery_dir = acpi_lock_battery_dir();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 if (!acpi_battery_dir)
Arjan van de Ven0f66af52009-01-10 14:19:05 -05001053 return;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +04001054#endif
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +04001055 if (acpi_bus_register_driver(&acpi_battery_driver) < 0) {
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +03001056#ifdef CONFIG_ACPI_PROCFS_POWER
Rich Townsend3f86b832006-07-01 11:36:54 -04001057 acpi_unlock_battery_dir(acpi_battery_dir);
Alexey Starikovskiyd7380962007-09-26 19:43:04 +04001058#endif
Arjan van de Ven0f66af52009-01-10 14:19:05 -05001059 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 }
Arjan van de Ven0f66af52009-01-10 14:19:05 -05001061 return;
1062}
1063
1064static int __init acpi_battery_init(void)
1065{
1066 async_schedule(acpi_battery_init_async, NULL);
Patrick Mocheld550d982006-06-27 00:41:40 -04001067 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068}
1069
Len Brown4be44fc2005-08-05 00:44:28 -04001070static void __exit acpi_battery_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 acpi_bus_unregister_driver(&acpi_battery_driver);
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +03001073#ifdef CONFIG_ACPI_PROCFS_POWER
Rich Townsend3f86b832006-07-01 11:36:54 -04001074 acpi_unlock_battery_dir(acpi_battery_dir);
Alexey Starikovskiyd7380962007-09-26 19:43:04 +04001075#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076}
1077
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078module_init(acpi_battery_init);
1079module_exit(acpi_battery_exit);