blob: ac1a599f5147640cc58135391260f0f202a411b1 [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>
Alexey Starikovskiyd7380962007-09-26 19:43:04 +040045#include <linux/power_supply.h>
46
Len Browna192a952009-07-28 16:45:54 -040047#define PREFIX "ACPI: "
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#define ACPI_BATTERY_VALUE_UNKNOWN 0xFFFFFFFF
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#define ACPI_BATTERY_CLASS "battery"
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#define ACPI_BATTERY_DEVICE_NAME "Battery"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#define ACPI_BATTERY_NOTIFY_STATUS 0x80
54#define ACPI_BATTERY_NOTIFY_INFO 0x81
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +040055#define ACPI_BATTERY_NOTIFY_THRESHOLD 0x82
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#define _COMPONENT ACPI_BATTERY_COMPONENT
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +030058
Len Brownf52fd662007-02-12 22:42:12 -050059ACPI_MODULE_NAME("battery");
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Len Brownf52fd662007-02-12 22:42:12 -050061MODULE_AUTHOR("Paul Diefenbaugh");
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +040062MODULE_AUTHOR("Alexey Starikovskiy <astarikovskiy@suse.de>");
Len Brown7cda93e2007-02-12 23:50:02 -050063MODULE_DESCRIPTION("ACPI Battery Driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070064MODULE_LICENSE("GPL");
65
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +040066static unsigned int cache_time = 1000;
67module_param(cache_time, uint, 0644);
68MODULE_PARM_DESC(cache_time, "cache time in milliseconds");
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +030069
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +030070#ifdef CONFIG_ACPI_PROCFS_POWER
Rich Townsend3f86b832006-07-01 11:36:54 -040071extern struct proc_dir_entry *acpi_lock_battery_dir(void);
72extern void *acpi_unlock_battery_dir(struct proc_dir_entry *acpi_battery_dir);
73
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +040074enum acpi_battery_files {
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +040075 info_tag = 0,
76 state_tag,
77 alarm_tag,
Alexey Starikovskiy78490d82007-05-11 13:18:55 -040078 ACPI_BATTERY_NUMFILES,
79};
80
Alexey Starikovskiyd7380962007-09-26 19:43:04 +040081#endif
82
83static const struct acpi_device_id battery_device_ids[] = {
84 {"PNP0C0A", 0},
85 {"", 0},
86};
87
88MODULE_DEVICE_TABLE(acpi, battery_device_ids);
89
Alexey Starikovskiy7b3bcc42009-10-15 14:31:24 +040090enum {
91 ACPI_BATTERY_ALARM_PRESENT,
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +040092 ACPI_BATTERY_XINFO_PRESENT,
93 /* For buggy DSDTs that report negative 16-bit values for either
94 * charging or discharging current and/or report 0 as 65536
95 * due to bad math.
96 */
Alexey Starikovskiy7b3bcc42009-10-15 14:31:24 +040097 ACPI_BATTERY_QUIRK_SIGNED16_CURRENT,
Zhang Rui557d5862010-10-22 10:02:06 +080098 ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY,
Alexey Starikovskiy7b3bcc42009-10-15 14:31:24 +040099};
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400100
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400101struct acpi_battery {
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400102 struct mutex lock;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400103 struct power_supply bat;
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400104 struct acpi_device *device;
105 unsigned long update_time;
Alexey Starikovskiy7faa1442009-03-27 22:23:52 -0400106 int rate_now;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400107 int capacity_now;
108 int voltage_now;
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400109 int design_capacity;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400110 int full_charge_capacity;
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400111 int technology;
112 int design_voltage;
113 int design_capacity_warning;
114 int design_capacity_low;
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400115 int cycle_count;
116 int measurement_accuracy;
117 int max_sampling_time;
118 int min_sampling_time;
119 int max_averaging_interval;
120 int min_averaging_interval;
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400121 int capacity_granularity_1;
122 int capacity_granularity_2;
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400123 int alarm;
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400124 char model_number[32];
125 char serial_number[32];
126 char type[32];
127 char oem_info[32];
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400128 int state;
129 int power_unit;
Alexey Starikovskiy7b3bcc42009-10-15 14:31:24 +0400130 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131};
132
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400133#define to_acpi_battery(x) container_of(x, struct acpi_battery, bat);
134
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400135inline int acpi_battery_present(struct acpi_battery *battery)
136{
137 return battery->device->status.battery_present;
138}
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400139
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400140static int acpi_battery_technology(struct acpi_battery *battery)
141{
142 if (!strcasecmp("NiCd", battery->type))
143 return POWER_SUPPLY_TECHNOLOGY_NiCd;
144 if (!strcasecmp("NiMH", battery->type))
145 return POWER_SUPPLY_TECHNOLOGY_NiMH;
146 if (!strcasecmp("LION", battery->type))
147 return POWER_SUPPLY_TECHNOLOGY_LION;
Andrey Borzenkovad40e682007-11-10 20:02:49 +0300148 if (!strncasecmp("LI-ION", battery->type, 6))
Alexey Starikovskiy0bde7ee2007-10-28 15:33:10 +0300149 return POWER_SUPPLY_TECHNOLOGY_LION;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400150 if (!strcasecmp("LiP", battery->type))
151 return POWER_SUPPLY_TECHNOLOGY_LIPO;
152 return POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
153}
154
Alexey Starikovskiy91044762007-11-13 12:23:06 +0300155static int acpi_battery_get_state(struct acpi_battery *battery);
Alexey Starikovskiyb19073a2007-10-25 17:10:47 -0400156
Richard Hughes56f382a2009-01-25 15:05:50 +0000157static int acpi_battery_is_charged(struct acpi_battery *battery)
158{
159 /* either charging or discharging */
160 if (battery->state != 0)
161 return 0;
162
163 /* battery not reporting charge */
164 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN ||
165 battery->capacity_now == 0)
166 return 0;
167
168 /* good batteries update full_charge as the batteries degrade */
169 if (battery->full_charge_capacity == battery->capacity_now)
170 return 1;
171
172 /* fallback to using design values for broken batteries */
173 if (battery->design_capacity == battery->capacity_now)
174 return 1;
175
176 /* we don't do any sort of metric based on percentages */
177 return 0;
178}
179
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400180static int acpi_battery_get_property(struct power_supply *psy,
181 enum power_supply_property psp,
182 union power_supply_propval *val)
183{
Rafael J. Wysockia1b4bd62010-10-23 19:35:15 +0200184 int ret = 0;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400185 struct acpi_battery *battery = to_acpi_battery(psy);
186
Alexey Starikovskiy91044762007-11-13 12:23:06 +0300187 if (acpi_battery_present(battery)) {
188 /* run battery update only if it is present */
189 acpi_battery_get_state(battery);
190 } else if (psp != POWER_SUPPLY_PROP_PRESENT)
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400191 return -ENODEV;
192 switch (psp) {
193 case POWER_SUPPLY_PROP_STATUS:
194 if (battery->state & 0x01)
195 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
196 else if (battery->state & 0x02)
197 val->intval = POWER_SUPPLY_STATUS_CHARGING;
Richard Hughes56f382a2009-01-25 15:05:50 +0000198 else if (acpi_battery_is_charged(battery))
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400199 val->intval = POWER_SUPPLY_STATUS_FULL;
Roland Dreier4c41d3a2007-11-07 15:09:09 -0800200 else
201 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400202 break;
203 case POWER_SUPPLY_PROP_PRESENT:
204 val->intval = acpi_battery_present(battery);
205 break;
206 case POWER_SUPPLY_PROP_TECHNOLOGY:
207 val->intval = acpi_battery_technology(battery);
208 break;
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400209 case POWER_SUPPLY_PROP_CYCLE_COUNT:
210 val->intval = battery->cycle_count;
211 break;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400212 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
Rafael J. Wysockia1b4bd62010-10-23 19:35:15 +0200213 if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
214 ret = -ENODEV;
215 else
216 val->intval = battery->design_voltage * 1000;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400217 break;
218 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
Rafael J. Wysockia1b4bd62010-10-23 19:35:15 +0200219 if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN)
220 ret = -ENODEV;
221 else
222 val->intval = battery->voltage_now * 1000;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400223 break;
224 case POWER_SUPPLY_PROP_CURRENT_NOW:
Alexey Starikovskiy7faa1442009-03-27 22:23:52 -0400225 case POWER_SUPPLY_PROP_POWER_NOW:
Rafael J. Wysockia1b4bd62010-10-23 19:35:15 +0200226 if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN)
227 ret = -ENODEV;
228 else
229 val->intval = battery->rate_now * 1000;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400230 break;
231 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
232 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
Rafael J. Wysockia1b4bd62010-10-23 19:35:15 +0200233 if (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
234 ret = -ENODEV;
235 else
236 val->intval = battery->design_capacity * 1000;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400237 break;
238 case POWER_SUPPLY_PROP_CHARGE_FULL:
239 case POWER_SUPPLY_PROP_ENERGY_FULL:
Rafael J. Wysockia1b4bd62010-10-23 19:35:15 +0200240 if (battery->full_charge_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
241 ret = -ENODEV;
242 else
243 val->intval = battery->full_charge_capacity * 1000;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400244 break;
245 case POWER_SUPPLY_PROP_CHARGE_NOW:
246 case POWER_SUPPLY_PROP_ENERGY_NOW:
Rafael J. Wysockia1b4bd62010-10-23 19:35:15 +0200247 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN)
248 ret = -ENODEV;
249 else
250 val->intval = battery->capacity_now * 1000;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400251 break;
252 case POWER_SUPPLY_PROP_MODEL_NAME:
253 val->strval = battery->model_number;
254 break;
255 case POWER_SUPPLY_PROP_MANUFACTURER:
256 val->strval = battery->oem_info;
257 break;
maximilian attems7c2670b2008-01-22 18:46:50 +0100258 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
259 val->strval = battery->serial_number;
260 break;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400261 default:
Rafael J. Wysockia1b4bd62010-10-23 19:35:15 +0200262 ret = -EINVAL;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400263 }
Rafael J. Wysockia1b4bd62010-10-23 19:35:15 +0200264 return ret;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400265}
266
267static enum power_supply_property charge_battery_props[] = {
268 POWER_SUPPLY_PROP_STATUS,
269 POWER_SUPPLY_PROP_PRESENT,
270 POWER_SUPPLY_PROP_TECHNOLOGY,
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400271 POWER_SUPPLY_PROP_CYCLE_COUNT,
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400272 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
273 POWER_SUPPLY_PROP_VOLTAGE_NOW,
274 POWER_SUPPLY_PROP_CURRENT_NOW,
275 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
276 POWER_SUPPLY_PROP_CHARGE_FULL,
277 POWER_SUPPLY_PROP_CHARGE_NOW,
278 POWER_SUPPLY_PROP_MODEL_NAME,
279 POWER_SUPPLY_PROP_MANUFACTURER,
maximilian attems7c2670b2008-01-22 18:46:50 +0100280 POWER_SUPPLY_PROP_SERIAL_NUMBER,
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400281};
282
283static enum power_supply_property energy_battery_props[] = {
284 POWER_SUPPLY_PROP_STATUS,
285 POWER_SUPPLY_PROP_PRESENT,
286 POWER_SUPPLY_PROP_TECHNOLOGY,
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400287 POWER_SUPPLY_PROP_CYCLE_COUNT,
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400288 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
289 POWER_SUPPLY_PROP_VOLTAGE_NOW,
Alexey Starikovskiy7faa1442009-03-27 22:23:52 -0400290 POWER_SUPPLY_PROP_POWER_NOW,
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400291 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
292 POWER_SUPPLY_PROP_ENERGY_FULL,
293 POWER_SUPPLY_PROP_ENERGY_NOW,
294 POWER_SUPPLY_PROP_MODEL_NAME,
295 POWER_SUPPLY_PROP_MANUFACTURER,
maximilian attems7c2670b2008-01-22 18:46:50 +0100296 POWER_SUPPLY_PROP_SERIAL_NUMBER,
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400297};
298
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +0300299#ifdef CONFIG_ACPI_PROCFS_POWER
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400300inline char *acpi_battery_units(struct acpi_battery *battery)
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400301{
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400302 return (battery->power_unit)?"mA":"mW";
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400303}
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400304#endif
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306/* --------------------------------------------------------------------------
307 Battery Management
308 -------------------------------------------------------------------------- */
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400309struct acpi_offsets {
310 size_t offset; /* offset inside struct acpi_sbs_battery */
311 u8 mode; /* int or string? */
312};
313
314static struct acpi_offsets state_offsets[] = {
315 {offsetof(struct acpi_battery, state), 0},
Alexey Starikovskiy7faa1442009-03-27 22:23:52 -0400316 {offsetof(struct acpi_battery, rate_now), 0},
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400317 {offsetof(struct acpi_battery, capacity_now), 0},
318 {offsetof(struct acpi_battery, voltage_now), 0},
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400319};
320
321static struct acpi_offsets info_offsets[] = {
322 {offsetof(struct acpi_battery, power_unit), 0},
323 {offsetof(struct acpi_battery, design_capacity), 0},
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400324 {offsetof(struct acpi_battery, full_charge_capacity), 0},
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400325 {offsetof(struct acpi_battery, technology), 0},
326 {offsetof(struct acpi_battery, design_voltage), 0},
327 {offsetof(struct acpi_battery, design_capacity_warning), 0},
328 {offsetof(struct acpi_battery, design_capacity_low), 0},
329 {offsetof(struct acpi_battery, capacity_granularity_1), 0},
330 {offsetof(struct acpi_battery, capacity_granularity_2), 0},
331 {offsetof(struct acpi_battery, model_number), 1},
332 {offsetof(struct acpi_battery, serial_number), 1},
333 {offsetof(struct acpi_battery, type), 1},
334 {offsetof(struct acpi_battery, oem_info), 1},
335};
336
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400337static struct acpi_offsets extended_info_offsets[] = {
338 {offsetof(struct acpi_battery, power_unit), 0},
339 {offsetof(struct acpi_battery, design_capacity), 0},
340 {offsetof(struct acpi_battery, full_charge_capacity), 0},
341 {offsetof(struct acpi_battery, technology), 0},
342 {offsetof(struct acpi_battery, design_voltage), 0},
343 {offsetof(struct acpi_battery, design_capacity_warning), 0},
344 {offsetof(struct acpi_battery, design_capacity_low), 0},
345 {offsetof(struct acpi_battery, cycle_count), 0},
346 {offsetof(struct acpi_battery, measurement_accuracy), 0},
347 {offsetof(struct acpi_battery, max_sampling_time), 0},
348 {offsetof(struct acpi_battery, min_sampling_time), 0},
349 {offsetof(struct acpi_battery, max_averaging_interval), 0},
350 {offsetof(struct acpi_battery, min_averaging_interval), 0},
351 {offsetof(struct acpi_battery, capacity_granularity_1), 0},
352 {offsetof(struct acpi_battery, capacity_granularity_2), 0},
353 {offsetof(struct acpi_battery, model_number), 1},
354 {offsetof(struct acpi_battery, serial_number), 1},
355 {offsetof(struct acpi_battery, type), 1},
356 {offsetof(struct acpi_battery, oem_info), 1},
357};
358
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400359static int extract_package(struct acpi_battery *battery,
360 union acpi_object *package,
361 struct acpi_offsets *offsets, int num)
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300362{
Alexey Starikovskiy106449e2007-10-29 23:29:40 +0300363 int i;
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400364 union acpi_object *element;
365 if (package->type != ACPI_TYPE_PACKAGE)
366 return -EFAULT;
367 for (i = 0; i < num; ++i) {
368 if (package->package.count <= i)
369 return -EFAULT;
370 element = &package->package.elements[i];
371 if (offsets[i].mode) {
Alexey Starikovskiy106449e2007-10-29 23:29:40 +0300372 u8 *ptr = (u8 *)battery + offsets[i].offset;
373 if (element->type == ACPI_TYPE_STRING ||
374 element->type == ACPI_TYPE_BUFFER)
375 strncpy(ptr, element->string.pointer, 32);
376 else if (element->type == ACPI_TYPE_INTEGER) {
377 strncpy(ptr, (u8 *)&element->integer.value,
Lin Ming439913f2010-01-28 10:53:19 +0800378 sizeof(u64));
379 ptr[sizeof(u64)] = 0;
Alexey Starikovskiyb8a1bdb2008-03-17 22:37:42 -0400380 } else
381 *ptr = 0; /* don't have value */
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400382 } else {
Alexey Starikovskiyb8a1bdb2008-03-17 22:37:42 -0400383 int *x = (int *)((u8 *)battery + offsets[i].offset);
384 *x = (element->type == ACPI_TYPE_INTEGER) ?
385 element->integer.value : -1;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300386 }
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300387 }
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300388 return 0;
389}
390
391static int acpi_battery_get_status(struct acpi_battery *battery)
392{
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400393 if (acpi_bus_get_status(battery->device)) {
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300394 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Evaluating _STA"));
395 return -ENODEV;
396 }
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400397 return 0;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300398}
399
400static int acpi_battery_get_info(struct acpi_battery *battery)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400402 int result = -EFAULT;
Len Brown4be44fc2005-08-05 00:44:28 -0400403 acpi_status status = 0;
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400404 char *name = test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags)?
405 "_BIX" : "_BIF";
406
Len Brown4be44fc2005-08-05 00:44:28 -0400407 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300409 if (!acpi_battery_present(battery))
410 return 0;
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400411 mutex_lock(&battery->lock);
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400412 status = acpi_evaluate_object(battery->device->handle, name,
413 NULL, &buffer);
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400414 mutex_unlock(&battery->lock);
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 if (ACPI_FAILURE(status)) {
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400417 ACPI_EXCEPTION((AE_INFO, status, "Evaluating %s", name));
Patrick Mocheld550d982006-06-27 00:41:40 -0400418 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400420 if (test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags))
421 result = extract_package(battery, buffer.pointer,
422 extended_info_offsets,
423 ARRAY_SIZE(extended_info_offsets));
424 else
425 result = extract_package(battery, buffer.pointer,
426 info_offsets, ARRAY_SIZE(info_offsets));
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400427 kfree(buffer.pointer);
Zhang Rui557d5862010-10-22 10:02:06 +0800428 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags))
429 battery->full_charge_capacity = battery->design_capacity;
Patrick Mocheld550d982006-06-27 00:41:40 -0400430 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431}
432
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300433static int acpi_battery_get_state(struct acpi_battery *battery)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
Len Brown4be44fc2005-08-05 00:44:28 -0400435 int result = 0;
436 acpi_status status = 0;
437 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300439 if (!acpi_battery_present(battery))
440 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400442 if (battery->update_time &&
443 time_before(jiffies, battery->update_time +
444 msecs_to_jiffies(cache_time)))
445 return 0;
446
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400447 mutex_lock(&battery->lock);
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400448 status = acpi_evaluate_object(battery->device->handle, "_BST",
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400449 NULL, &buffer);
450 mutex_unlock(&battery->lock);
Len Brown5b31d892007-08-15 00:19:26 -0400451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400453 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BST"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400454 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 }
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400456
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400457 result = extract_package(battery, buffer.pointer,
458 state_offsets, ARRAY_SIZE(state_offsets));
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400459 battery->update_time = jiffies;
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400460 kfree(buffer.pointer);
Hector Martinbc76f902009-08-06 15:57:48 -0700461
Alexey Starikovskiy7b3bcc42009-10-15 14:31:24 +0400462 if (test_bit(ACPI_BATTERY_QUIRK_SIGNED16_CURRENT, &battery->flags) &&
Hector Martinbc76f902009-08-06 15:57:48 -0700463 battery->rate_now != -1)
464 battery->rate_now = abs((s16)battery->rate_now);
465
Zhang Rui557d5862010-10-22 10:02:06 +0800466 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags)
467 && battery->capacity_now >= 0 && battery->capacity_now <= 100)
468 battery->capacity_now = (battery->capacity_now *
469 battery->full_charge_capacity) / 100;
Patrick Mocheld550d982006-06-27 00:41:40 -0400470 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471}
472
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400473static int acpi_battery_set_alarm(struct acpi_battery *battery)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
Len Brown4be44fc2005-08-05 00:44:28 -0400475 acpi_status status = 0;
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400476 union acpi_object arg0 = { .type = ACPI_TYPE_INTEGER };
Len Brown4be44fc2005-08-05 00:44:28 -0400477 struct acpi_object_list arg_list = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400479 if (!acpi_battery_present(battery) ||
Alexey Starikovskiy7b3bcc42009-10-15 14:31:24 +0400480 !test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags))
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300481 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400483 arg0.integer.value = battery->alarm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400485 mutex_lock(&battery->lock);
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400486 status = acpi_evaluate_object(battery->device->handle, "_BTP",
487 &arg_list, NULL);
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400488 mutex_unlock(&battery->lock);
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400491 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400493 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", battery->alarm));
Patrick Mocheld550d982006-06-27 00:41:40 -0400494 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495}
496
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300497static int acpi_battery_init_alarm(struct acpi_battery *battery)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
Len Brown4be44fc2005-08-05 00:44:28 -0400499 acpi_status status = AE_OK;
500 acpi_handle handle = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300502 /* See if alarms are supported, and if so, set default */
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400503 status = acpi_get_handle(battery->device->handle, "_BTP", &handle);
504 if (ACPI_FAILURE(status)) {
Alexey Starikovskiy7b3bcc42009-10-15 14:31:24 +0400505 clear_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400506 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 }
Alexey Starikovskiy7b3bcc42009-10-15 14:31:24 +0400508 set_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400509 if (!battery->alarm)
510 battery->alarm = battery->design_capacity_warning;
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400511 return acpi_battery_set_alarm(battery);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512}
513
Andrey Borzenkov508df922007-10-28 12:50:09 +0300514static ssize_t acpi_battery_alarm_show(struct device *dev,
515 struct device_attribute *attr,
516 char *buf)
517{
518 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
519 return sprintf(buf, "%d\n", battery->alarm * 1000);
520}
521
522static ssize_t acpi_battery_alarm_store(struct device *dev,
523 struct device_attribute *attr,
524 const char *buf, size_t count)
525{
526 unsigned long x;
527 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
528 if (sscanf(buf, "%ld\n", &x) == 1)
529 battery->alarm = x/1000;
530 if (acpi_battery_present(battery))
531 acpi_battery_set_alarm(battery);
532 return count;
533}
534
535static struct device_attribute alarm_attr = {
Parag Warudkar01e8ef12008-10-18 20:28:50 -0700536 .attr = {.name = "alarm", .mode = 0644},
Andrey Borzenkov508df922007-10-28 12:50:09 +0300537 .show = acpi_battery_alarm_show,
538 .store = acpi_battery_alarm_store,
539};
540
541static int sysfs_add_battery(struct acpi_battery *battery)
542{
543 int result;
544
Andrey Borzenkov508df922007-10-28 12:50:09 +0300545 if (battery->power_unit) {
546 battery->bat.properties = charge_battery_props;
547 battery->bat.num_properties =
548 ARRAY_SIZE(charge_battery_props);
549 } else {
550 battery->bat.properties = energy_battery_props;
551 battery->bat.num_properties =
552 ARRAY_SIZE(energy_battery_props);
553 }
554
555 battery->bat.name = acpi_device_bid(battery->device);
556 battery->bat.type = POWER_SUPPLY_TYPE_BATTERY;
557 battery->bat.get_property = acpi_battery_get_property;
558
559 result = power_supply_register(&battery->device->dev, &battery->bat);
560 if (result)
561 return result;
562 return device_create_file(battery->bat.dev, &alarm_attr);
563}
564
565static void sysfs_remove_battery(struct acpi_battery *battery)
566{
567 if (!battery->bat.dev)
568 return;
569 device_remove_file(battery->bat.dev, &alarm_attr);
570 power_supply_unregister(&battery->bat);
Alexey Starikovskiy91044762007-11-13 12:23:06 +0300571 battery->bat.dev = NULL;
Andrey Borzenkov508df922007-10-28 12:50:09 +0300572}
573
Hector Martinbc76f902009-08-06 15:57:48 -0700574static void acpi_battery_quirks(struct acpi_battery *battery)
575{
Hector Martinbc76f902009-08-06 15:57:48 -0700576 if (dmi_name_in_vendors("Acer") && battery->power_unit) {
Alexey Starikovskiy7b3bcc42009-10-15 14:31:24 +0400577 set_bit(ACPI_BATTERY_QUIRK_SIGNED16_CURRENT, &battery->flags);
Hector Martinbc76f902009-08-06 15:57:48 -0700578 }
579}
580
Zhang Rui557d5862010-10-22 10:02:06 +0800581/*
582 * According to the ACPI spec, some kinds of primary batteries can
583 * report percentage battery remaining capacity directly to OS.
584 * In this case, it reports the Last Full Charged Capacity == 100
585 * and BatteryPresentRate == 0xFFFFFFFF.
586 *
587 * Now we found some battery reports percentage remaining capacity
588 * even if it's rechargeable.
589 * https://bugzilla.kernel.org/show_bug.cgi?id=15979
590 *
591 * Handle this correctly so that they won't break userspace.
592 */
593static void acpi_battery_quirks2(struct acpi_battery *battery)
594{
595 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags))
596 return ;
597
598 if (battery->full_charge_capacity == 100 &&
599 battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN &&
600 battery->capacity_now >=0 && battery->capacity_now <= 100) {
601 set_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags);
602 battery->full_charge_capacity = battery->design_capacity;
603 battery->capacity_now = (battery->capacity_now *
604 battery->full_charge_capacity) / 100;
605 }
606}
607
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400608static int acpi_battery_update(struct acpi_battery *battery)
Vladimir Lebedev4bd35cd2007-02-10 01:43:48 -0500609{
Alexey Starikovskiy50b17852008-12-23 02:44:54 +0300610 int result, old_present = acpi_battery_present(battery);
Alexey Starikovskiy97749cd2008-01-01 14:27:24 -0500611 result = acpi_battery_get_status(battery);
Andrey Borzenkov508df922007-10-28 12:50:09 +0300612 if (result)
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300613 return result;
Andrey Borzenkov508df922007-10-28 12:50:09 +0300614 if (!acpi_battery_present(battery)) {
615 sysfs_remove_battery(battery);
Alexey Starikovskiy97749cd2008-01-01 14:27:24 -0500616 battery->update_time = 0;
Andrey Borzenkov508df922007-10-28 12:50:09 +0300617 return 0;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300618 }
Alexey Starikovskiy50b17852008-12-23 02:44:54 +0300619 if (!battery->update_time ||
620 old_present != acpi_battery_present(battery)) {
Alexey Starikovskiy97749cd2008-01-01 14:27:24 -0500621 result = acpi_battery_get_info(battery);
622 if (result)
623 return result;
Hector Martinbc76f902009-08-06 15:57:48 -0700624 acpi_battery_quirks(battery);
Alexey Starikovskiy97749cd2008-01-01 14:27:24 -0500625 acpi_battery_init_alarm(battery);
626 }
Andrey Borzenkov508df922007-10-28 12:50:09 +0300627 if (!battery->bat.dev)
628 sysfs_add_battery(battery);
Zhang Rui557d5862010-10-22 10:02:06 +0800629 result = acpi_battery_get_state(battery);
630 acpi_battery_quirks2(battery);
631 return result;
Vladimir Lebedev4bd35cd2007-02-10 01:43:48 -0500632}
633
Rafael J. Wysockida8aeb92011-01-06 23:42:27 +0100634static void acpi_battery_refresh(struct acpi_battery *battery)
635{
636 if (!battery->bat.dev)
637 return;
638
639 acpi_battery_get_info(battery);
640 /* The battery may have changed its reporting units. */
641 sysfs_remove_battery(battery);
642 sysfs_add_battery(battery);
643}
644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645/* --------------------------------------------------------------------------
646 FS Interface (/proc)
647 -------------------------------------------------------------------------- */
648
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +0300649#ifdef CONFIG_ACPI_PROCFS_POWER
Len Brown4be44fc2005-08-05 00:44:28 -0400650static struct proc_dir_entry *acpi_battery_dir;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300651
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400652static int acpi_battery_print_info(struct seq_file *seq, int result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200654 struct acpi_battery *battery = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300656 if (result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 goto end;
658
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400659 seq_printf(seq, "present: %s\n",
660 acpi_battery_present(battery)?"yes":"no");
661 if (!acpi_battery_present(battery))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 goto end;
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400663 if (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 seq_printf(seq, "design capacity: unknown\n");
665 else
666 seq_printf(seq, "design capacity: %d %sh\n",
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400667 battery->design_capacity,
668 acpi_battery_units(battery));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400670 if (battery->full_charge_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 seq_printf(seq, "last full capacity: unknown\n");
672 else
673 seq_printf(seq, "last full capacity: %d %sh\n",
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400674 battery->full_charge_capacity,
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400675 acpi_battery_units(battery));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400677 seq_printf(seq, "battery technology: %srechargeable\n",
678 (!battery->technology)?"non-":"");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400680 if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 seq_printf(seq, "design voltage: unknown\n");
682 else
683 seq_printf(seq, "design voltage: %d mV\n",
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400684 battery->design_voltage);
Len Brown4be44fc2005-08-05 00:44:28 -0400685 seq_printf(seq, "design capacity warning: %d %sh\n",
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400686 battery->design_capacity_warning,
687 acpi_battery_units(battery));
Len Brown4be44fc2005-08-05 00:44:28 -0400688 seq_printf(seq, "design capacity low: %d %sh\n",
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400689 battery->design_capacity_low,
690 acpi_battery_units(battery));
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400691 seq_printf(seq, "cycle count: %i\n", battery->cycle_count);
Len Brown4be44fc2005-08-05 00:44:28 -0400692 seq_printf(seq, "capacity granularity 1: %d %sh\n",
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400693 battery->capacity_granularity_1,
694 acpi_battery_units(battery));
Len Brown4be44fc2005-08-05 00:44:28 -0400695 seq_printf(seq, "capacity granularity 2: %d %sh\n",
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400696 battery->capacity_granularity_2,
697 acpi_battery_units(battery));
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400698 seq_printf(seq, "model number: %s\n", battery->model_number);
699 seq_printf(seq, "serial number: %s\n", battery->serial_number);
700 seq_printf(seq, "battery type: %s\n", battery->type);
701 seq_printf(seq, "OEM info: %s\n", battery->oem_info);
Len Brown4be44fc2005-08-05 00:44:28 -0400702 end:
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300703 if (result)
704 seq_printf(seq, "ERROR: Unable to read battery info\n");
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300705 return result;
706}
707
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400708static int acpi_battery_print_state(struct seq_file *seq, int result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200710 struct acpi_battery *battery = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300712 if (result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 goto end;
714
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400715 seq_printf(seq, "present: %s\n",
716 acpi_battery_present(battery)?"yes":"no");
717 if (!acpi_battery_present(battery))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400720 seq_printf(seq, "capacity state: %s\n",
721 (battery->state & 0x04)?"critical":"ok");
722 if ((battery->state & 0x01) && (battery->state & 0x02))
Len Brown4be44fc2005-08-05 00:44:28 -0400723 seq_printf(seq,
724 "charging state: charging/discharging\n");
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400725 else if (battery->state & 0x01)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 seq_printf(seq, "charging state: discharging\n");
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400727 else if (battery->state & 0x02)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 seq_printf(seq, "charging state: charging\n");
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400729 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 seq_printf(seq, "charging state: charged\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Alexey Starikovskiy7faa1442009-03-27 22:23:52 -0400732 if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 seq_printf(seq, "present rate: unknown\n");
734 else
735 seq_printf(seq, "present rate: %d %s\n",
Alexey Starikovskiy7faa1442009-03-27 22:23:52 -0400736 battery->rate_now, acpi_battery_units(battery));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400738 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 seq_printf(seq, "remaining capacity: unknown\n");
740 else
741 seq_printf(seq, "remaining capacity: %d %sh\n",
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400742 battery->capacity_now, acpi_battery_units(battery));
743 if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 seq_printf(seq, "present voltage: unknown\n");
745 else
746 seq_printf(seq, "present voltage: %d mV\n",
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400747 battery->voltage_now);
Len Brown4be44fc2005-08-05 00:44:28 -0400748 end:
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400749 if (result)
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300750 seq_printf(seq, "ERROR: Unable to read battery state\n");
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300751
752 return result;
753}
754
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400755static int acpi_battery_print_alarm(struct seq_file *seq, int result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200757 struct acpi_battery *battery = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300759 if (result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 goto end;
761
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300762 if (!acpi_battery_present(battery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 seq_printf(seq, "present: no\n");
764 goto end;
765 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 seq_printf(seq, "alarm: ");
767 if (!battery->alarm)
768 seq_printf(seq, "unsupported\n");
769 else
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400770 seq_printf(seq, "%u %sh\n", battery->alarm,
771 acpi_battery_units(battery));
Len Brown4be44fc2005-08-05 00:44:28 -0400772 end:
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300773 if (result)
774 seq_printf(seq, "ERROR: Unable to read battery alarm\n");
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300775 return result;
776}
777
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400778static ssize_t acpi_battery_write_alarm(struct file *file,
779 const char __user * buffer,
780 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
Len Brown4be44fc2005-08-05 00:44:28 -0400782 int result = 0;
783 char alarm_string[12] = { '\0' };
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200784 struct seq_file *m = file->private_data;
785 struct acpi_battery *battery = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 if (!battery || (count > sizeof(alarm_string) - 1))
Patrick Mocheld550d982006-06-27 00:41:40 -0400788 return -EINVAL;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300789 if (!acpi_battery_present(battery)) {
790 result = -ENODEV;
791 goto end;
792 }
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300793 if (copy_from_user(alarm_string, buffer, count)) {
794 result = -EFAULT;
795 goto end;
796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 alarm_string[count] = '\0';
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400798 battery->alarm = simple_strtol(alarm_string, NULL, 0);
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400799 result = acpi_battery_set_alarm(battery);
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300800 end:
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300801 if (!result)
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400802 return count;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300803 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804}
805
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400806typedef int(*print_func)(struct seq_file *seq, int result);
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400807
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400808static print_func acpi_print_funcs[ACPI_BATTERY_NUMFILES] = {
809 acpi_battery_print_info,
810 acpi_battery_print_state,
811 acpi_battery_print_alarm,
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400812};
813
814static int acpi_battery_read(int fid, struct seq_file *seq)
815{
816 struct acpi_battery *battery = seq->private;
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400817 int result = acpi_battery_update(battery);
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400818 return acpi_print_funcs[fid](seq, result);
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400819}
820
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400821#define DECLARE_FILE_FUNCTIONS(_name) \
822static int acpi_battery_read_##_name(struct seq_file *seq, void *offset) \
823{ \
824 return acpi_battery_read(_name##_tag, seq); \
825} \
826static int acpi_battery_##_name##_open_fs(struct inode *inode, struct file *file) \
827{ \
828 return single_open(file, acpi_battery_read_##_name, PDE(inode)->data); \
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400829}
830
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400831DECLARE_FILE_FUNCTIONS(info);
832DECLARE_FILE_FUNCTIONS(state);
833DECLARE_FILE_FUNCTIONS(alarm);
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400834
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400835#undef DECLARE_FILE_FUNCTIONS
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400836
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400837#define FILE_DESCRIPTION_RO(_name) \
838 { \
839 .name = __stringify(_name), \
840 .mode = S_IRUGO, \
841 .ops = { \
842 .open = acpi_battery_##_name##_open_fs, \
843 .read = seq_read, \
844 .llseek = seq_lseek, \
845 .release = single_release, \
846 .owner = THIS_MODULE, \
847 }, \
848 }
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400849
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400850#define FILE_DESCRIPTION_RW(_name) \
851 { \
852 .name = __stringify(_name), \
853 .mode = S_IFREG | S_IRUGO | S_IWUSR, \
854 .ops = { \
855 .open = acpi_battery_##_name##_open_fs, \
856 .read = seq_read, \
857 .llseek = seq_lseek, \
858 .write = acpi_battery_write_##_name, \
859 .release = single_release, \
860 .owner = THIS_MODULE, \
861 }, \
862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400864static struct battery_file {
865 struct file_operations ops;
866 mode_t mode;
Jan Engelhardt070d8eb2009-01-12 00:07:55 +0100867 const char *name;
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400868} acpi_battery_file[] = {
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400869 FILE_DESCRIPTION_RO(info),
870 FILE_DESCRIPTION_RO(state),
871 FILE_DESCRIPTION_RW(alarm),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872};
873
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400874#undef FILE_DESCRIPTION_RO
875#undef FILE_DESCRIPTION_RW
876
Len Brown4be44fc2005-08-05 00:44:28 -0400877static int acpi_battery_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
Len Brown4be44fc2005-08-05 00:44:28 -0400879 struct proc_dir_entry *entry = NULL;
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400880 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
Zhang Rui6d855fc2011-01-10 11:16:30 +0800882 printk(KERN_WARNING PREFIX "Deprecated procfs I/F for battery is loaded,"
883 " please retry with CONFIG_ACPI_PROCFS_POWER cleared\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 if (!acpi_device_dir(device)) {
885 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown4be44fc2005-08-05 00:44:28 -0400886 acpi_battery_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400888 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 }
890
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400891 for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i) {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700892 entry = proc_create_data(acpi_battery_file[i].name,
893 acpi_battery_file[i].mode,
894 acpi_device_dir(device),
895 &acpi_battery_file[i].ops,
896 acpi_driver_data(device));
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400897 if (!entry)
898 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 }
Patrick Mocheld550d982006-06-27 00:41:40 -0400900 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901}
902
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400903static void acpi_battery_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400905 int i;
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400906 if (!acpi_device_dir(device))
907 return;
908 for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i)
909 remove_proc_entry(acpi_battery_file[i].name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400912 remove_proc_entry(acpi_device_bid(device), acpi_battery_dir);
913 acpi_device_dir(device) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914}
915
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400916#endif
Alexey Starikovskiy3e58ea02007-09-26 19:43:11 +0400917
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918/* --------------------------------------------------------------------------
919 Driver Interface
920 -------------------------------------------------------------------------- */
921
Bjorn Helgaasd9406692009-04-30 09:35:47 -0600922static void acpi_battery_notify(struct acpi_device *device, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923{
Bjorn Helgaasd9406692009-04-30 09:35:47 -0600924 struct acpi_battery *battery = acpi_driver_data(device);
Zhang Rui153e5002010-07-07 09:11:57 +0800925 struct device *old;
Bjorn Helgaasd9406692009-04-30 09:35:47 -0600926
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 if (!battery)
Patrick Mocheld550d982006-06-27 00:41:40 -0400928 return;
Zhang Rui153e5002010-07-07 09:11:57 +0800929 old = battery->bat.dev;
Rafael J. Wysockida8aeb92011-01-06 23:42:27 +0100930 if (event == ACPI_BATTERY_NOTIFY_INFO)
931 acpi_battery_refresh(battery);
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400932 acpi_battery_update(battery);
933 acpi_bus_generate_proc_event(device, event,
934 acpi_battery_present(battery));
935 acpi_bus_generate_netlink_event(device->pnp.device_class,
Kay Sievers07944692008-10-30 01:18:59 +0100936 dev_name(&device->dev), event,
Vladimir Lebedev9ea7d572007-02-20 15:48:06 +0300937 acpi_battery_present(battery));
Justin P. Mattock2345baf2009-12-13 14:42:36 -0800938 /* acpi_battery_update could remove power_supply object */
Zhang Rui153e5002010-07-07 09:11:57 +0800939 if (old && battery->bat.dev)
Alan Jenkinsf79e1ce2009-06-30 14:36:16 +0000940 power_supply_changed(&battery->bat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941}
942
Len Brown4be44fc2005-08-05 00:44:28 -0400943static int acpi_battery_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944{
Len Brown4be44fc2005-08-05 00:44:28 -0400945 int result = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400946 struct acpi_battery *battery = NULL;
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400947 acpi_handle handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400949 return -EINVAL;
Burman Yan36bcbec2006-12-19 12:56:11 -0800950 battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 if (!battery)
Patrick Mocheld550d982006-06-27 00:41:40 -0400952 return -ENOMEM;
Patrick Mochel145def82006-05-19 16:54:39 -0400953 battery->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME);
955 strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700956 device->driver_data = battery;
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400957 mutex_init(&battery->lock);
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400958 if (ACPI_SUCCESS(acpi_get_handle(battery->device->handle,
959 "_BIX", &handle)))
960 set_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags);
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400961 acpi_battery_update(battery);
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +0300962#ifdef CONFIG_ACPI_PROCFS_POWER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 result = acpi_battery_add_fs(device);
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400964#endif
Len Brown586caae2009-06-18 00:38:27 -0400965 if (!result) {
966 printk(KERN_INFO PREFIX "%s Slot [%s] (battery %s)\n",
967 ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device),
968 device->status.battery_present ? "present" : "absent");
969 } else {
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +0300970#ifdef CONFIG_ACPI_PROCFS_POWER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 acpi_battery_remove_fs(device);
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400972#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 kfree(battery);
974 }
Patrick Mocheld550d982006-06-27 00:41:40 -0400975 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976}
977
Len Brown4be44fc2005-08-05 00:44:28 -0400978static int acpi_battery_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979{
Len Brown4be44fc2005-08-05 00:44:28 -0400980 struct acpi_battery *battery = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400983 return -EINVAL;
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200984 battery = acpi_driver_data(device);
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +0300985#ifdef CONFIG_ACPI_PROCFS_POWER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 acpi_battery_remove_fs(device);
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400987#endif
Andrey Borzenkov508df922007-10-28 12:50:09 +0300988 sysfs_remove_battery(battery);
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400989 mutex_destroy(&battery->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 kfree(battery);
Patrick Mocheld550d982006-06-27 00:41:40 -0400991 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992}
993
Jiri Kosina34c44152006-10-10 14:20:41 -0700994/* this is needed to learn about changes made in suspended state */
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800995static int acpi_battery_resume(struct acpi_device *device)
Jiri Kosina34c44152006-10-10 14:20:41 -0700996{
997 struct acpi_battery *battery;
Jiri Kosina34c44152006-10-10 14:20:41 -0700998 if (!device)
999 return -EINVAL;
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +04001000 battery = acpi_driver_data(device);
1001 battery->update_time = 0;
Andrey Borzenkov508df922007-10-28 12:50:09 +03001002 acpi_battery_update(battery);
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +03001003 return 0;
Jiri Kosina34c44152006-10-10 14:20:41 -07001004}
1005
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +04001006static struct acpi_driver acpi_battery_driver = {
1007 .name = "battery",
1008 .class = ACPI_BATTERY_CLASS,
1009 .ids = battery_device_ids,
Bjorn Helgaasd9406692009-04-30 09:35:47 -06001010 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +04001011 .ops = {
1012 .add = acpi_battery_add,
1013 .resume = acpi_battery_resume,
1014 .remove = acpi_battery_remove,
Bjorn Helgaasd9406692009-04-30 09:35:47 -06001015 .notify = acpi_battery_notify,
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +04001016 },
1017};
1018
Linus Torvaldsb0cbc862009-04-11 12:45:20 -07001019static void __init acpi_battery_init_async(void *unused, async_cookie_t cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020{
Pavel Machek4d8316d2006-08-14 22:37:22 -07001021 if (acpi_disabled)
Arjan van de Ven0f66af52009-01-10 14:19:05 -05001022 return;
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +03001023#ifdef CONFIG_ACPI_PROCFS_POWER
Rich Townsend3f86b832006-07-01 11:36:54 -04001024 acpi_battery_dir = acpi_lock_battery_dir();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 if (!acpi_battery_dir)
Arjan van de Ven0f66af52009-01-10 14:19:05 -05001026 return;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +04001027#endif
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +04001028 if (acpi_bus_register_driver(&acpi_battery_driver) < 0) {
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +03001029#ifdef CONFIG_ACPI_PROCFS_POWER
Rich Townsend3f86b832006-07-01 11:36:54 -04001030 acpi_unlock_battery_dir(acpi_battery_dir);
Alexey Starikovskiyd7380962007-09-26 19:43:04 +04001031#endif
Arjan van de Ven0f66af52009-01-10 14:19:05 -05001032 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 }
Arjan van de Ven0f66af52009-01-10 14:19:05 -05001034 return;
1035}
1036
1037static int __init acpi_battery_init(void)
1038{
1039 async_schedule(acpi_battery_init_async, NULL);
Patrick Mocheld550d982006-06-27 00:41:40 -04001040 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041}
1042
Len Brown4be44fc2005-08-05 00:44:28 -04001043static void __exit acpi_battery_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 acpi_bus_unregister_driver(&acpi_battery_driver);
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +03001046#ifdef CONFIG_ACPI_PROCFS_POWER
Rich Townsend3f86b832006-07-01 11:36:54 -04001047 acpi_unlock_battery_dir(acpi_battery_dir);
Alexey Starikovskiyd7380962007-09-26 19:43:04 +04001048#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049}
1050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051module_init(acpi_battery_init);
1052module_exit(acpi_battery_exit);