blob: fcc13ac0aa1870009ad0fd9dc41712795e582aab [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#define _COMPONENT ACPI_BATTERY_COMPONENT
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +030059
Len Brownf52fd662007-02-12 22:42:12 -050060ACPI_MODULE_NAME("battery");
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Len Brownf52fd662007-02-12 22:42:12 -050062MODULE_AUTHOR("Paul Diefenbaugh");
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +040063MODULE_AUTHOR("Alexey Starikovskiy <astarikovskiy@suse.de>");
Len Brown7cda93e2007-02-12 23:50:02 -050064MODULE_DESCRIPTION("ACPI Battery Driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070065MODULE_LICENSE("GPL");
66
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +040067static unsigned int cache_time = 1000;
68module_param(cache_time, uint, 0644);
69MODULE_PARM_DESC(cache_time, "cache time in milliseconds");
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +030070
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +030071#ifdef CONFIG_ACPI_PROCFS_POWER
Rich Townsend3f86b832006-07-01 11:36:54 -040072extern struct proc_dir_entry *acpi_lock_battery_dir(void);
73extern void *acpi_unlock_battery_dir(struct proc_dir_entry *acpi_battery_dir);
74
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +040075enum acpi_battery_files {
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +040076 info_tag = 0,
77 state_tag,
78 alarm_tag,
Alexey Starikovskiy78490d82007-05-11 13:18:55 -040079 ACPI_BATTERY_NUMFILES,
80};
81
Alexey Starikovskiyd7380962007-09-26 19:43:04 +040082#endif
83
84static const struct acpi_device_id battery_device_ids[] = {
85 {"PNP0C0A", 0},
86 {"", 0},
87};
88
89MODULE_DEVICE_TABLE(acpi, battery_device_ids);
90
Alexey Starikovskiy7b3bcc42009-10-15 14:31:24 +040091enum {
92 ACPI_BATTERY_ALARM_PRESENT,
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +040093 ACPI_BATTERY_XINFO_PRESENT,
94 /* For buggy DSDTs that report negative 16-bit values for either
95 * charging or discharging current and/or report 0 as 65536
96 * due to bad math.
97 */
Alexey Starikovskiy7b3bcc42009-10-15 14:31:24 +040098 ACPI_BATTERY_QUIRK_SIGNED16_CURRENT,
Zhang Rui557d5862010-10-22 10:02:06 +080099 ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY,
Alexey Starikovskiy7b3bcc42009-10-15 14:31:24 +0400100};
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400101
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400102struct acpi_battery {
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400103 struct mutex lock;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400104 struct power_supply bat;
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400105 struct acpi_device *device;
Kyle McMartin25be5822011-03-22 16:19:50 -0400106 struct notifier_block pm_nb;
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400107 unsigned long update_time;
Alexey Starikovskiy7faa1442009-03-27 22:23:52 -0400108 int rate_now;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400109 int capacity_now;
110 int voltage_now;
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400111 int design_capacity;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400112 int full_charge_capacity;
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400113 int technology;
114 int design_voltage;
115 int design_capacity_warning;
116 int design_capacity_low;
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400117 int cycle_count;
118 int measurement_accuracy;
119 int max_sampling_time;
120 int min_sampling_time;
121 int max_averaging_interval;
122 int min_averaging_interval;
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400123 int capacity_granularity_1;
124 int capacity_granularity_2;
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400125 int alarm;
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400126 char model_number[32];
127 char serial_number[32];
128 char type[32];
129 char oem_info[32];
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400130 int state;
131 int power_unit;
Alexey Starikovskiy7b3bcc42009-10-15 14:31:24 +0400132 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133};
134
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400135#define to_acpi_battery(x) container_of(x, struct acpi_battery, bat);
136
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400137inline int acpi_battery_present(struct acpi_battery *battery)
138{
139 return battery->device->status.battery_present;
140}
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400141
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400142static int acpi_battery_technology(struct acpi_battery *battery)
143{
144 if (!strcasecmp("NiCd", battery->type))
145 return POWER_SUPPLY_TECHNOLOGY_NiCd;
146 if (!strcasecmp("NiMH", battery->type))
147 return POWER_SUPPLY_TECHNOLOGY_NiMH;
148 if (!strcasecmp("LION", battery->type))
149 return POWER_SUPPLY_TECHNOLOGY_LION;
Andrey Borzenkovad40e682007-11-10 20:02:49 +0300150 if (!strncasecmp("LI-ION", battery->type, 6))
Alexey Starikovskiy0bde7ee2007-10-28 15:33:10 +0300151 return POWER_SUPPLY_TECHNOLOGY_LION;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400152 if (!strcasecmp("LiP", battery->type))
153 return POWER_SUPPLY_TECHNOLOGY_LIPO;
154 return POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
155}
156
Alexey Starikovskiy91044762007-11-13 12:23:06 +0300157static int acpi_battery_get_state(struct acpi_battery *battery);
Alexey Starikovskiyb19073a2007-10-25 17:10:47 -0400158
Richard Hughes56f382a2009-01-25 15:05:50 +0000159static int acpi_battery_is_charged(struct acpi_battery *battery)
160{
161 /* either charging or discharging */
162 if (battery->state != 0)
163 return 0;
164
165 /* battery not reporting charge */
166 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN ||
167 battery->capacity_now == 0)
168 return 0;
169
170 /* good batteries update full_charge as the batteries degrade */
171 if (battery->full_charge_capacity == battery->capacity_now)
172 return 1;
173
174 /* fallback to using design values for broken batteries */
175 if (battery->design_capacity == battery->capacity_now)
176 return 1;
177
178 /* we don't do any sort of metric based on percentages */
179 return 0;
180}
181
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400182static int acpi_battery_get_property(struct power_supply *psy,
183 enum power_supply_property psp,
184 union power_supply_propval *val)
185{
Rafael J. Wysockia1b4bd62010-10-23 19:35:15 +0200186 int ret = 0;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400187 struct acpi_battery *battery = to_acpi_battery(psy);
188
Alexey Starikovskiy91044762007-11-13 12:23:06 +0300189 if (acpi_battery_present(battery)) {
190 /* run battery update only if it is present */
191 acpi_battery_get_state(battery);
192 } else if (psp != POWER_SUPPLY_PROP_PRESENT)
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400193 return -ENODEV;
194 switch (psp) {
195 case POWER_SUPPLY_PROP_STATUS:
196 if (battery->state & 0x01)
197 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
198 else if (battery->state & 0x02)
199 val->intval = POWER_SUPPLY_STATUS_CHARGING;
Richard Hughes56f382a2009-01-25 15:05:50 +0000200 else if (acpi_battery_is_charged(battery))
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400201 val->intval = POWER_SUPPLY_STATUS_FULL;
Roland Dreier4c41d3a2007-11-07 15:09:09 -0800202 else
203 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400204 break;
205 case POWER_SUPPLY_PROP_PRESENT:
206 val->intval = acpi_battery_present(battery);
207 break;
208 case POWER_SUPPLY_PROP_TECHNOLOGY:
209 val->intval = acpi_battery_technology(battery);
210 break;
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400211 case POWER_SUPPLY_PROP_CYCLE_COUNT:
212 val->intval = battery->cycle_count;
213 break;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400214 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
Rafael J. Wysockia1b4bd62010-10-23 19:35:15 +0200215 if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
216 ret = -ENODEV;
217 else
218 val->intval = battery->design_voltage * 1000;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400219 break;
220 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
Rafael J. Wysockia1b4bd62010-10-23 19:35:15 +0200221 if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN)
222 ret = -ENODEV;
223 else
224 val->intval = battery->voltage_now * 1000;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400225 break;
226 case POWER_SUPPLY_PROP_CURRENT_NOW:
Alexey Starikovskiy7faa1442009-03-27 22:23:52 -0400227 case POWER_SUPPLY_PROP_POWER_NOW:
Rafael J. Wysockia1b4bd62010-10-23 19:35:15 +0200228 if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN)
229 ret = -ENODEV;
230 else
231 val->intval = battery->rate_now * 1000;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400232 break;
233 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
234 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
Rafael J. Wysockia1b4bd62010-10-23 19:35:15 +0200235 if (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
236 ret = -ENODEV;
237 else
238 val->intval = battery->design_capacity * 1000;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400239 break;
240 case POWER_SUPPLY_PROP_CHARGE_FULL:
241 case POWER_SUPPLY_PROP_ENERGY_FULL:
Rafael J. Wysockia1b4bd62010-10-23 19:35:15 +0200242 if (battery->full_charge_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
243 ret = -ENODEV;
244 else
245 val->intval = battery->full_charge_capacity * 1000;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400246 break;
247 case POWER_SUPPLY_PROP_CHARGE_NOW:
248 case POWER_SUPPLY_PROP_ENERGY_NOW:
Rafael J. Wysockia1b4bd62010-10-23 19:35:15 +0200249 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN)
250 ret = -ENODEV;
251 else
252 val->intval = battery->capacity_now * 1000;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400253 break;
254 case POWER_SUPPLY_PROP_MODEL_NAME:
255 val->strval = battery->model_number;
256 break;
257 case POWER_SUPPLY_PROP_MANUFACTURER:
258 val->strval = battery->oem_info;
259 break;
maximilian attems7c2670b2008-01-22 18:46:50 +0100260 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
261 val->strval = battery->serial_number;
262 break;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400263 default:
Rafael J. Wysockia1b4bd62010-10-23 19:35:15 +0200264 ret = -EINVAL;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400265 }
Rafael J. Wysockia1b4bd62010-10-23 19:35:15 +0200266 return ret;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400267}
268
269static enum power_supply_property charge_battery_props[] = {
270 POWER_SUPPLY_PROP_STATUS,
271 POWER_SUPPLY_PROP_PRESENT,
272 POWER_SUPPLY_PROP_TECHNOLOGY,
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400273 POWER_SUPPLY_PROP_CYCLE_COUNT,
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400274 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
275 POWER_SUPPLY_PROP_VOLTAGE_NOW,
276 POWER_SUPPLY_PROP_CURRENT_NOW,
277 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
278 POWER_SUPPLY_PROP_CHARGE_FULL,
279 POWER_SUPPLY_PROP_CHARGE_NOW,
280 POWER_SUPPLY_PROP_MODEL_NAME,
281 POWER_SUPPLY_PROP_MANUFACTURER,
maximilian attems7c2670b2008-01-22 18:46:50 +0100282 POWER_SUPPLY_PROP_SERIAL_NUMBER,
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400283};
284
285static enum power_supply_property energy_battery_props[] = {
286 POWER_SUPPLY_PROP_STATUS,
287 POWER_SUPPLY_PROP_PRESENT,
288 POWER_SUPPLY_PROP_TECHNOLOGY,
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400289 POWER_SUPPLY_PROP_CYCLE_COUNT,
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400290 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
291 POWER_SUPPLY_PROP_VOLTAGE_NOW,
Alexey Starikovskiy7faa1442009-03-27 22:23:52 -0400292 POWER_SUPPLY_PROP_POWER_NOW,
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400293 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
294 POWER_SUPPLY_PROP_ENERGY_FULL,
295 POWER_SUPPLY_PROP_ENERGY_NOW,
296 POWER_SUPPLY_PROP_MODEL_NAME,
297 POWER_SUPPLY_PROP_MANUFACTURER,
maximilian attems7c2670b2008-01-22 18:46:50 +0100298 POWER_SUPPLY_PROP_SERIAL_NUMBER,
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400299};
300
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +0300301#ifdef CONFIG_ACPI_PROCFS_POWER
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400302inline char *acpi_battery_units(struct acpi_battery *battery)
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400303{
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400304 return (battery->power_unit)?"mA":"mW";
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400305}
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400306#endif
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308/* --------------------------------------------------------------------------
309 Battery Management
310 -------------------------------------------------------------------------- */
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400311struct acpi_offsets {
312 size_t offset; /* offset inside struct acpi_sbs_battery */
313 u8 mode; /* int or string? */
314};
315
316static struct acpi_offsets state_offsets[] = {
317 {offsetof(struct acpi_battery, state), 0},
Alexey Starikovskiy7faa1442009-03-27 22:23:52 -0400318 {offsetof(struct acpi_battery, rate_now), 0},
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400319 {offsetof(struct acpi_battery, capacity_now), 0},
320 {offsetof(struct acpi_battery, voltage_now), 0},
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400321};
322
323static struct acpi_offsets info_offsets[] = {
324 {offsetof(struct acpi_battery, power_unit), 0},
325 {offsetof(struct acpi_battery, design_capacity), 0},
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400326 {offsetof(struct acpi_battery, full_charge_capacity), 0},
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400327 {offsetof(struct acpi_battery, technology), 0},
328 {offsetof(struct acpi_battery, design_voltage), 0},
329 {offsetof(struct acpi_battery, design_capacity_warning), 0},
330 {offsetof(struct acpi_battery, design_capacity_low), 0},
331 {offsetof(struct acpi_battery, capacity_granularity_1), 0},
332 {offsetof(struct acpi_battery, capacity_granularity_2), 0},
333 {offsetof(struct acpi_battery, model_number), 1},
334 {offsetof(struct acpi_battery, serial_number), 1},
335 {offsetof(struct acpi_battery, type), 1},
336 {offsetof(struct acpi_battery, oem_info), 1},
337};
338
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400339static struct acpi_offsets extended_info_offsets[] = {
340 {offsetof(struct acpi_battery, power_unit), 0},
341 {offsetof(struct acpi_battery, design_capacity), 0},
342 {offsetof(struct acpi_battery, full_charge_capacity), 0},
343 {offsetof(struct acpi_battery, technology), 0},
344 {offsetof(struct acpi_battery, design_voltage), 0},
345 {offsetof(struct acpi_battery, design_capacity_warning), 0},
346 {offsetof(struct acpi_battery, design_capacity_low), 0},
347 {offsetof(struct acpi_battery, cycle_count), 0},
348 {offsetof(struct acpi_battery, measurement_accuracy), 0},
349 {offsetof(struct acpi_battery, max_sampling_time), 0},
350 {offsetof(struct acpi_battery, min_sampling_time), 0},
351 {offsetof(struct acpi_battery, max_averaging_interval), 0},
352 {offsetof(struct acpi_battery, min_averaging_interval), 0},
353 {offsetof(struct acpi_battery, capacity_granularity_1), 0},
354 {offsetof(struct acpi_battery, capacity_granularity_2), 0},
355 {offsetof(struct acpi_battery, model_number), 1},
356 {offsetof(struct acpi_battery, serial_number), 1},
357 {offsetof(struct acpi_battery, type), 1},
358 {offsetof(struct acpi_battery, oem_info), 1},
359};
360
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400361static int extract_package(struct acpi_battery *battery,
362 union acpi_object *package,
363 struct acpi_offsets *offsets, int num)
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300364{
Alexey Starikovskiy106449e2007-10-29 23:29:40 +0300365 int i;
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400366 union acpi_object *element;
367 if (package->type != ACPI_TYPE_PACKAGE)
368 return -EFAULT;
369 for (i = 0; i < num; ++i) {
370 if (package->package.count <= i)
371 return -EFAULT;
372 element = &package->package.elements[i];
373 if (offsets[i].mode) {
Alexey Starikovskiy106449e2007-10-29 23:29:40 +0300374 u8 *ptr = (u8 *)battery + offsets[i].offset;
375 if (element->type == ACPI_TYPE_STRING ||
376 element->type == ACPI_TYPE_BUFFER)
377 strncpy(ptr, element->string.pointer, 32);
378 else if (element->type == ACPI_TYPE_INTEGER) {
379 strncpy(ptr, (u8 *)&element->integer.value,
Lin Ming439913f2010-01-28 10:53:19 +0800380 sizeof(u64));
381 ptr[sizeof(u64)] = 0;
Alexey Starikovskiyb8a1bdb2008-03-17 22:37:42 -0400382 } else
383 *ptr = 0; /* don't have value */
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400384 } else {
Alexey Starikovskiyb8a1bdb2008-03-17 22:37:42 -0400385 int *x = (int *)((u8 *)battery + offsets[i].offset);
386 *x = (element->type == ACPI_TYPE_INTEGER) ?
387 element->integer.value : -1;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300388 }
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300389 }
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300390 return 0;
391}
392
393static int acpi_battery_get_status(struct acpi_battery *battery)
394{
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400395 if (acpi_bus_get_status(battery->device)) {
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300396 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Evaluating _STA"));
397 return -ENODEV;
398 }
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400399 return 0;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300400}
401
402static int acpi_battery_get_info(struct acpi_battery *battery)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400404 int result = -EFAULT;
Len Brown4be44fc2005-08-05 00:44:28 -0400405 acpi_status status = 0;
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400406 char *name = test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags)?
407 "_BIX" : "_BIF";
408
Len Brown4be44fc2005-08-05 00:44:28 -0400409 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300411 if (!acpi_battery_present(battery))
412 return 0;
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400413 mutex_lock(&battery->lock);
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400414 status = acpi_evaluate_object(battery->device->handle, name,
415 NULL, &buffer);
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400416 mutex_unlock(&battery->lock);
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 if (ACPI_FAILURE(status)) {
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400419 ACPI_EXCEPTION((AE_INFO, status, "Evaluating %s", name));
Patrick Mocheld550d982006-06-27 00:41:40 -0400420 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 }
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400422 if (test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags))
423 result = extract_package(battery, buffer.pointer,
424 extended_info_offsets,
425 ARRAY_SIZE(extended_info_offsets));
426 else
427 result = extract_package(battery, buffer.pointer,
428 info_offsets, ARRAY_SIZE(info_offsets));
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400429 kfree(buffer.pointer);
Zhang Rui557d5862010-10-22 10:02:06 +0800430 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags))
431 battery->full_charge_capacity = battery->design_capacity;
Patrick Mocheld550d982006-06-27 00:41:40 -0400432 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
434
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300435static int acpi_battery_get_state(struct acpi_battery *battery)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
Len Brown4be44fc2005-08-05 00:44:28 -0400437 int result = 0;
438 acpi_status status = 0;
439 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300441 if (!acpi_battery_present(battery))
442 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400444 if (battery->update_time &&
445 time_before(jiffies, battery->update_time +
446 msecs_to_jiffies(cache_time)))
447 return 0;
448
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400449 mutex_lock(&battery->lock);
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400450 status = acpi_evaluate_object(battery->device->handle, "_BST",
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400451 NULL, &buffer);
452 mutex_unlock(&battery->lock);
Len Brown5b31d892007-08-15 00:19:26 -0400453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400455 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BST"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400456 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 }
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400458
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400459 result = extract_package(battery, buffer.pointer,
460 state_offsets, ARRAY_SIZE(state_offsets));
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400461 battery->update_time = jiffies;
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400462 kfree(buffer.pointer);
Hector Martinbc76f902009-08-06 15:57:48 -0700463
Alexey Starikovskiy7b3bcc42009-10-15 14:31:24 +0400464 if (test_bit(ACPI_BATTERY_QUIRK_SIGNED16_CURRENT, &battery->flags) &&
Hector Martinbc76f902009-08-06 15:57:48 -0700465 battery->rate_now != -1)
466 battery->rate_now = abs((s16)battery->rate_now);
467
Zhang Rui557d5862010-10-22 10:02:06 +0800468 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags)
469 && battery->capacity_now >= 0 && battery->capacity_now <= 100)
470 battery->capacity_now = (battery->capacity_now *
471 battery->full_charge_capacity) / 100;
Patrick Mocheld550d982006-06-27 00:41:40 -0400472 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473}
474
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400475static int acpi_battery_set_alarm(struct acpi_battery *battery)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
Len Brown4be44fc2005-08-05 00:44:28 -0400477 acpi_status status = 0;
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400478 union acpi_object arg0 = { .type = ACPI_TYPE_INTEGER };
Len Brown4be44fc2005-08-05 00:44:28 -0400479 struct acpi_object_list arg_list = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400481 if (!acpi_battery_present(battery) ||
Alexey Starikovskiy7b3bcc42009-10-15 14:31:24 +0400482 !test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags))
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300483 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400485 arg0.integer.value = battery->alarm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400487 mutex_lock(&battery->lock);
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400488 status = acpi_evaluate_object(battery->device->handle, "_BTP",
489 &arg_list, NULL);
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400490 mutex_unlock(&battery->lock);
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400493 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400495 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", battery->alarm));
Patrick Mocheld550d982006-06-27 00:41:40 -0400496 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
498
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300499static int acpi_battery_init_alarm(struct acpi_battery *battery)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
Len Brown4be44fc2005-08-05 00:44:28 -0400501 acpi_status status = AE_OK;
502 acpi_handle handle = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300504 /* See if alarms are supported, and if so, set default */
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400505 status = acpi_get_handle(battery->device->handle, "_BTP", &handle);
506 if (ACPI_FAILURE(status)) {
Alexey Starikovskiy7b3bcc42009-10-15 14:31:24 +0400507 clear_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400508 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 }
Alexey Starikovskiy7b3bcc42009-10-15 14:31:24 +0400510 set_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400511 if (!battery->alarm)
512 battery->alarm = battery->design_capacity_warning;
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400513 return acpi_battery_set_alarm(battery);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514}
515
Andrey Borzenkov508df922007-10-28 12:50:09 +0300516static ssize_t acpi_battery_alarm_show(struct device *dev,
517 struct device_attribute *attr,
518 char *buf)
519{
520 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
521 return sprintf(buf, "%d\n", battery->alarm * 1000);
522}
523
524static ssize_t acpi_battery_alarm_store(struct device *dev,
525 struct device_attribute *attr,
526 const char *buf, size_t count)
527{
528 unsigned long x;
529 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
530 if (sscanf(buf, "%ld\n", &x) == 1)
531 battery->alarm = x/1000;
532 if (acpi_battery_present(battery))
533 acpi_battery_set_alarm(battery);
534 return count;
535}
536
537static struct device_attribute alarm_attr = {
Parag Warudkar01e8ef12008-10-18 20:28:50 -0700538 .attr = {.name = "alarm", .mode = 0644},
Andrey Borzenkov508df922007-10-28 12:50:09 +0300539 .show = acpi_battery_alarm_show,
540 .store = acpi_battery_alarm_store,
541};
542
543static int sysfs_add_battery(struct acpi_battery *battery)
544{
545 int result;
546
Andrey Borzenkov508df922007-10-28 12:50:09 +0300547 if (battery->power_unit) {
548 battery->bat.properties = charge_battery_props;
549 battery->bat.num_properties =
550 ARRAY_SIZE(charge_battery_props);
551 } else {
552 battery->bat.properties = energy_battery_props;
553 battery->bat.num_properties =
554 ARRAY_SIZE(energy_battery_props);
555 }
556
557 battery->bat.name = acpi_device_bid(battery->device);
558 battery->bat.type = POWER_SUPPLY_TYPE_BATTERY;
559 battery->bat.get_property = acpi_battery_get_property;
560
561 result = power_supply_register(&battery->device->dev, &battery->bat);
562 if (result)
563 return result;
564 return device_create_file(battery->bat.dev, &alarm_attr);
565}
566
567static void sysfs_remove_battery(struct acpi_battery *battery)
568{
569 if (!battery->bat.dev)
570 return;
571 device_remove_file(battery->bat.dev, &alarm_attr);
572 power_supply_unregister(&battery->bat);
Alexey Starikovskiy91044762007-11-13 12:23:06 +0300573 battery->bat.dev = NULL;
Andrey Borzenkov508df922007-10-28 12:50:09 +0300574}
575
Hector Martinbc76f902009-08-06 15:57:48 -0700576static void acpi_battery_quirks(struct acpi_battery *battery)
577{
Hector Martinbc76f902009-08-06 15:57:48 -0700578 if (dmi_name_in_vendors("Acer") && battery->power_unit) {
Alexey Starikovskiy7b3bcc42009-10-15 14:31:24 +0400579 set_bit(ACPI_BATTERY_QUIRK_SIGNED16_CURRENT, &battery->flags);
Hector Martinbc76f902009-08-06 15:57:48 -0700580 }
581}
582
Zhang Rui557d5862010-10-22 10:02:06 +0800583/*
584 * According to the ACPI spec, some kinds of primary batteries can
585 * report percentage battery remaining capacity directly to OS.
586 * In this case, it reports the Last Full Charged Capacity == 100
587 * and BatteryPresentRate == 0xFFFFFFFF.
588 *
589 * Now we found some battery reports percentage remaining capacity
590 * even if it's rechargeable.
591 * https://bugzilla.kernel.org/show_bug.cgi?id=15979
592 *
593 * Handle this correctly so that they won't break userspace.
594 */
595static void acpi_battery_quirks2(struct acpi_battery *battery)
596{
597 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags))
598 return ;
599
600 if (battery->full_charge_capacity == 100 &&
601 battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN &&
602 battery->capacity_now >=0 && battery->capacity_now <= 100) {
603 set_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags);
604 battery->full_charge_capacity = battery->design_capacity;
605 battery->capacity_now = (battery->capacity_now *
606 battery->full_charge_capacity) / 100;
607 }
608}
609
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400610static int acpi_battery_update(struct acpi_battery *battery)
Vladimir Lebedev4bd35cd2007-02-10 01:43:48 -0500611{
Alexey Starikovskiy50b17852008-12-23 02:44:54 +0300612 int result, old_present = acpi_battery_present(battery);
Alexey Starikovskiy97749cd2008-01-01 14:27:24 -0500613 result = acpi_battery_get_status(battery);
Andrey Borzenkov508df922007-10-28 12:50:09 +0300614 if (result)
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300615 return result;
Andrey Borzenkov508df922007-10-28 12:50:09 +0300616 if (!acpi_battery_present(battery)) {
617 sysfs_remove_battery(battery);
Alexey Starikovskiy97749cd2008-01-01 14:27:24 -0500618 battery->update_time = 0;
Andrey Borzenkov508df922007-10-28 12:50:09 +0300619 return 0;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300620 }
Alexey Starikovskiy50b17852008-12-23 02:44:54 +0300621 if (!battery->update_time ||
622 old_present != acpi_battery_present(battery)) {
Alexey Starikovskiy97749cd2008-01-01 14:27:24 -0500623 result = acpi_battery_get_info(battery);
624 if (result)
625 return result;
Hector Martinbc76f902009-08-06 15:57:48 -0700626 acpi_battery_quirks(battery);
Alexey Starikovskiy97749cd2008-01-01 14:27:24 -0500627 acpi_battery_init_alarm(battery);
628 }
Andrey Borzenkov508df922007-10-28 12:50:09 +0300629 if (!battery->bat.dev)
630 sysfs_add_battery(battery);
Zhang Rui557d5862010-10-22 10:02:06 +0800631 result = acpi_battery_get_state(battery);
632 acpi_battery_quirks2(battery);
633 return result;
Vladimir Lebedev4bd35cd2007-02-10 01:43:48 -0500634}
635
Rafael J. Wysockida8aeb92011-01-06 23:42:27 +0100636static void acpi_battery_refresh(struct acpi_battery *battery)
637{
638 if (!battery->bat.dev)
639 return;
640
641 acpi_battery_get_info(battery);
642 /* The battery may have changed its reporting units. */
643 sysfs_remove_battery(battery);
644 sysfs_add_battery(battery);
645}
646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647/* --------------------------------------------------------------------------
648 FS Interface (/proc)
649 -------------------------------------------------------------------------- */
650
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +0300651#ifdef CONFIG_ACPI_PROCFS_POWER
Len Brown4be44fc2005-08-05 00:44:28 -0400652static struct proc_dir_entry *acpi_battery_dir;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300653
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400654static int acpi_battery_print_info(struct seq_file *seq, int result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200656 struct acpi_battery *battery = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300658 if (result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 goto end;
660
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400661 seq_printf(seq, "present: %s\n",
662 acpi_battery_present(battery)?"yes":"no");
663 if (!acpi_battery_present(battery))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 goto end;
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400665 if (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 seq_printf(seq, "design capacity: unknown\n");
667 else
668 seq_printf(seq, "design capacity: %d %sh\n",
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400669 battery->design_capacity,
670 acpi_battery_units(battery));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400672 if (battery->full_charge_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 seq_printf(seq, "last full capacity: unknown\n");
674 else
675 seq_printf(seq, "last full capacity: %d %sh\n",
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400676 battery->full_charge_capacity,
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400677 acpi_battery_units(battery));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400679 seq_printf(seq, "battery technology: %srechargeable\n",
680 (!battery->technology)?"non-":"");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400682 if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 seq_printf(seq, "design voltage: unknown\n");
684 else
685 seq_printf(seq, "design voltage: %d mV\n",
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400686 battery->design_voltage);
Len Brown4be44fc2005-08-05 00:44:28 -0400687 seq_printf(seq, "design capacity warning: %d %sh\n",
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400688 battery->design_capacity_warning,
689 acpi_battery_units(battery));
Len Brown4be44fc2005-08-05 00:44:28 -0400690 seq_printf(seq, "design capacity low: %d %sh\n",
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400691 battery->design_capacity_low,
692 acpi_battery_units(battery));
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400693 seq_printf(seq, "cycle count: %i\n", battery->cycle_count);
Len Brown4be44fc2005-08-05 00:44:28 -0400694 seq_printf(seq, "capacity granularity 1: %d %sh\n",
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400695 battery->capacity_granularity_1,
696 acpi_battery_units(battery));
Len Brown4be44fc2005-08-05 00:44:28 -0400697 seq_printf(seq, "capacity granularity 2: %d %sh\n",
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400698 battery->capacity_granularity_2,
699 acpi_battery_units(battery));
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400700 seq_printf(seq, "model number: %s\n", battery->model_number);
701 seq_printf(seq, "serial number: %s\n", battery->serial_number);
702 seq_printf(seq, "battery type: %s\n", battery->type);
703 seq_printf(seq, "OEM info: %s\n", battery->oem_info);
Len Brown4be44fc2005-08-05 00:44:28 -0400704 end:
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300705 if (result)
706 seq_printf(seq, "ERROR: Unable to read battery info\n");
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300707 return result;
708}
709
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400710static int acpi_battery_print_state(struct seq_file *seq, int result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200712 struct acpi_battery *battery = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300714 if (result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 goto end;
716
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400717 seq_printf(seq, "present: %s\n",
718 acpi_battery_present(battery)?"yes":"no");
719 if (!acpi_battery_present(battery))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400722 seq_printf(seq, "capacity state: %s\n",
723 (battery->state & 0x04)?"critical":"ok");
724 if ((battery->state & 0x01) && (battery->state & 0x02))
Len Brown4be44fc2005-08-05 00:44:28 -0400725 seq_printf(seq,
726 "charging state: charging/discharging\n");
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400727 else if (battery->state & 0x01)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 seq_printf(seq, "charging state: discharging\n");
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400729 else if (battery->state & 0x02)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 seq_printf(seq, "charging state: charging\n");
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400731 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 seq_printf(seq, "charging state: charged\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Alexey Starikovskiy7faa1442009-03-27 22:23:52 -0400734 if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 seq_printf(seq, "present rate: unknown\n");
736 else
737 seq_printf(seq, "present rate: %d %s\n",
Alexey Starikovskiy7faa1442009-03-27 22:23:52 -0400738 battery->rate_now, acpi_battery_units(battery));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400740 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 seq_printf(seq, "remaining capacity: unknown\n");
742 else
743 seq_printf(seq, "remaining capacity: %d %sh\n",
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400744 battery->capacity_now, acpi_battery_units(battery));
745 if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 seq_printf(seq, "present voltage: unknown\n");
747 else
748 seq_printf(seq, "present voltage: %d mV\n",
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400749 battery->voltage_now);
Len Brown4be44fc2005-08-05 00:44:28 -0400750 end:
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400751 if (result)
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300752 seq_printf(seq, "ERROR: Unable to read battery state\n");
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300753
754 return result;
755}
756
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400757static int acpi_battery_print_alarm(struct seq_file *seq, int result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200759 struct acpi_battery *battery = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300761 if (result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 goto end;
763
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300764 if (!acpi_battery_present(battery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 seq_printf(seq, "present: no\n");
766 goto end;
767 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 seq_printf(seq, "alarm: ");
769 if (!battery->alarm)
770 seq_printf(seq, "unsupported\n");
771 else
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400772 seq_printf(seq, "%u %sh\n", battery->alarm,
773 acpi_battery_units(battery));
Len Brown4be44fc2005-08-05 00:44:28 -0400774 end:
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300775 if (result)
776 seq_printf(seq, "ERROR: Unable to read battery alarm\n");
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300777 return result;
778}
779
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400780static ssize_t acpi_battery_write_alarm(struct file *file,
781 const char __user * buffer,
782 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
Len Brown4be44fc2005-08-05 00:44:28 -0400784 int result = 0;
785 char alarm_string[12] = { '\0' };
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200786 struct seq_file *m = file->private_data;
787 struct acpi_battery *battery = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 if (!battery || (count > sizeof(alarm_string) - 1))
Patrick Mocheld550d982006-06-27 00:41:40 -0400790 return -EINVAL;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300791 if (!acpi_battery_present(battery)) {
792 result = -ENODEV;
793 goto end;
794 }
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300795 if (copy_from_user(alarm_string, buffer, count)) {
796 result = -EFAULT;
797 goto end;
798 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 alarm_string[count] = '\0';
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400800 battery->alarm = simple_strtol(alarm_string, NULL, 0);
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400801 result = acpi_battery_set_alarm(battery);
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300802 end:
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300803 if (!result)
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400804 return count;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300805 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806}
807
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400808typedef int(*print_func)(struct seq_file *seq, int result);
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400809
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400810static print_func acpi_print_funcs[ACPI_BATTERY_NUMFILES] = {
811 acpi_battery_print_info,
812 acpi_battery_print_state,
813 acpi_battery_print_alarm,
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400814};
815
816static int acpi_battery_read(int fid, struct seq_file *seq)
817{
818 struct acpi_battery *battery = seq->private;
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400819 int result = acpi_battery_update(battery);
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400820 return acpi_print_funcs[fid](seq, result);
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400821}
822
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400823#define DECLARE_FILE_FUNCTIONS(_name) \
824static int acpi_battery_read_##_name(struct seq_file *seq, void *offset) \
825{ \
826 return acpi_battery_read(_name##_tag, seq); \
827} \
828static int acpi_battery_##_name##_open_fs(struct inode *inode, struct file *file) \
829{ \
830 return single_open(file, acpi_battery_read_##_name, PDE(inode)->data); \
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400831}
832
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400833DECLARE_FILE_FUNCTIONS(info);
834DECLARE_FILE_FUNCTIONS(state);
835DECLARE_FILE_FUNCTIONS(alarm);
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400836
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400837#undef DECLARE_FILE_FUNCTIONS
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400838
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400839#define FILE_DESCRIPTION_RO(_name) \
840 { \
841 .name = __stringify(_name), \
842 .mode = S_IRUGO, \
843 .ops = { \
844 .open = acpi_battery_##_name##_open_fs, \
845 .read = seq_read, \
846 .llseek = seq_lseek, \
847 .release = single_release, \
848 .owner = THIS_MODULE, \
849 }, \
850 }
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400851
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400852#define FILE_DESCRIPTION_RW(_name) \
853 { \
854 .name = __stringify(_name), \
855 .mode = S_IFREG | S_IRUGO | S_IWUSR, \
856 .ops = { \
857 .open = acpi_battery_##_name##_open_fs, \
858 .read = seq_read, \
859 .llseek = seq_lseek, \
860 .write = acpi_battery_write_##_name, \
861 .release = single_release, \
862 .owner = THIS_MODULE, \
863 }, \
864 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400866static struct battery_file {
867 struct file_operations ops;
868 mode_t mode;
Jan Engelhardt070d8eb2009-01-12 00:07:55 +0100869 const char *name;
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400870} acpi_battery_file[] = {
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400871 FILE_DESCRIPTION_RO(info),
872 FILE_DESCRIPTION_RO(state),
873 FILE_DESCRIPTION_RW(alarm),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874};
875
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400876#undef FILE_DESCRIPTION_RO
877#undef FILE_DESCRIPTION_RW
878
Len Brown4be44fc2005-08-05 00:44:28 -0400879static int acpi_battery_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
Len Brown4be44fc2005-08-05 00:44:28 -0400881 struct proc_dir_entry *entry = NULL;
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400882 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Zhang Rui6d855fc2011-01-10 11:16:30 +0800884 printk(KERN_WARNING PREFIX "Deprecated procfs I/F for battery is loaded,"
885 " please retry with CONFIG_ACPI_PROCFS_POWER cleared\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 if (!acpi_device_dir(device)) {
887 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown4be44fc2005-08-05 00:44:28 -0400888 acpi_battery_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400890 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 }
892
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400893 for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i) {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700894 entry = proc_create_data(acpi_battery_file[i].name,
895 acpi_battery_file[i].mode,
896 acpi_device_dir(device),
897 &acpi_battery_file[i].ops,
898 acpi_driver_data(device));
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400899 if (!entry)
900 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 }
Patrick Mocheld550d982006-06-27 00:41:40 -0400902 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903}
904
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400905static void acpi_battery_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400907 int i;
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400908 if (!acpi_device_dir(device))
909 return;
910 for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i)
911 remove_proc_entry(acpi_battery_file[i].name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400914 remove_proc_entry(acpi_device_bid(device), acpi_battery_dir);
915 acpi_device_dir(device) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916}
917
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400918#endif
Alexey Starikovskiy3e58ea02007-09-26 19:43:11 +0400919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920/* --------------------------------------------------------------------------
921 Driver Interface
922 -------------------------------------------------------------------------- */
923
Bjorn Helgaasd9406692009-04-30 09:35:47 -0600924static void acpi_battery_notify(struct acpi_device *device, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
Bjorn Helgaasd9406692009-04-30 09:35:47 -0600926 struct acpi_battery *battery = acpi_driver_data(device);
Zhang Rui153e5002010-07-07 09:11:57 +0800927 struct device *old;
Bjorn Helgaasd9406692009-04-30 09:35:47 -0600928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 if (!battery)
Patrick Mocheld550d982006-06-27 00:41:40 -0400930 return;
Zhang Rui153e5002010-07-07 09:11:57 +0800931 old = battery->bat.dev;
Rafael J. Wysockida8aeb92011-01-06 23:42:27 +0100932 if (event == ACPI_BATTERY_NOTIFY_INFO)
933 acpi_battery_refresh(battery);
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400934 acpi_battery_update(battery);
935 acpi_bus_generate_proc_event(device, event,
936 acpi_battery_present(battery));
937 acpi_bus_generate_netlink_event(device->pnp.device_class,
Kay Sievers07944692008-10-30 01:18:59 +0100938 dev_name(&device->dev), event,
Vladimir Lebedev9ea7d572007-02-20 15:48:06 +0300939 acpi_battery_present(battery));
Justin P. Mattock2345baf2009-12-13 14:42:36 -0800940 /* acpi_battery_update could remove power_supply object */
Zhang Rui153e5002010-07-07 09:11:57 +0800941 if (old && battery->bat.dev)
Alan Jenkinsf79e1ce2009-06-30 14:36:16 +0000942 power_supply_changed(&battery->bat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943}
944
Kyle McMartin25be5822011-03-22 16:19:50 -0400945static int battery_notify(struct notifier_block *nb,
946 unsigned long mode, void *_unused)
947{
948 struct acpi_battery *battery = container_of(nb, struct acpi_battery,
949 pm_nb);
950 switch (mode) {
951 case PM_POST_SUSPEND:
952 sysfs_remove_battery(battery);
953 sysfs_add_battery(battery);
954 break;
955 }
956
957 return 0;
958}
959
Len Brown4be44fc2005-08-05 00:44:28 -0400960static int acpi_battery_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961{
Len Brown4be44fc2005-08-05 00:44:28 -0400962 int result = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400963 struct acpi_battery *battery = NULL;
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400964 acpi_handle handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400966 return -EINVAL;
Burman Yan36bcbec2006-12-19 12:56:11 -0800967 battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 if (!battery)
Patrick Mocheld550d982006-06-27 00:41:40 -0400969 return -ENOMEM;
Patrick Mochel145def82006-05-19 16:54:39 -0400970 battery->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME);
972 strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700973 device->driver_data = battery;
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400974 mutex_init(&battery->lock);
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400975 if (ACPI_SUCCESS(acpi_get_handle(battery->device->handle,
976 "_BIX", &handle)))
977 set_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags);
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400978 acpi_battery_update(battery);
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +0300979#ifdef CONFIG_ACPI_PROCFS_POWER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 result = acpi_battery_add_fs(device);
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400981#endif
Len Brown586caae2009-06-18 00:38:27 -0400982 if (!result) {
983 printk(KERN_INFO PREFIX "%s Slot [%s] (battery %s)\n",
984 ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device),
985 device->status.battery_present ? "present" : "absent");
986 } else {
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +0300987#ifdef CONFIG_ACPI_PROCFS_POWER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 acpi_battery_remove_fs(device);
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400989#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 kfree(battery);
991 }
Kyle McMartin25be5822011-03-22 16:19:50 -0400992
993 battery->pm_nb.notifier_call = battery_notify;
994 register_pm_notifier(&battery->pm_nb);
995
Patrick Mocheld550d982006-06-27 00:41:40 -0400996 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997}
998
Len Brown4be44fc2005-08-05 00:44:28 -0400999static int acpi_battery_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000{
Len Brown4be44fc2005-08-05 00:44:28 -04001001 struct acpi_battery *battery = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001004 return -EINVAL;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001005 battery = acpi_driver_data(device);
Kyle McMartin25be5822011-03-22 16:19:50 -04001006 unregister_pm_notifier(&battery->pm_nb);
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +03001007#ifdef CONFIG_ACPI_PROCFS_POWER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 acpi_battery_remove_fs(device);
Alexey Starikovskiyd7380962007-09-26 19:43:04 +04001009#endif
Andrey Borzenkov508df922007-10-28 12:50:09 +03001010 sysfs_remove_battery(battery);
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +04001011 mutex_destroy(&battery->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 kfree(battery);
Patrick Mocheld550d982006-06-27 00:41:40 -04001013 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014}
1015
Jiri Kosina34c44152006-10-10 14:20:41 -07001016/* this is needed to learn about changes made in suspended state */
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001017static int acpi_battery_resume(struct acpi_device *device)
Jiri Kosina34c44152006-10-10 14:20:41 -07001018{
1019 struct acpi_battery *battery;
Jiri Kosina34c44152006-10-10 14:20:41 -07001020 if (!device)
1021 return -EINVAL;
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +04001022 battery = acpi_driver_data(device);
1023 battery->update_time = 0;
Andrey Borzenkov508df922007-10-28 12:50:09 +03001024 acpi_battery_update(battery);
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +03001025 return 0;
Jiri Kosina34c44152006-10-10 14:20:41 -07001026}
1027
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +04001028static struct acpi_driver acpi_battery_driver = {
1029 .name = "battery",
1030 .class = ACPI_BATTERY_CLASS,
1031 .ids = battery_device_ids,
Bjorn Helgaasd9406692009-04-30 09:35:47 -06001032 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +04001033 .ops = {
1034 .add = acpi_battery_add,
1035 .resume = acpi_battery_resume,
1036 .remove = acpi_battery_remove,
Bjorn Helgaasd9406692009-04-30 09:35:47 -06001037 .notify = acpi_battery_notify,
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +04001038 },
1039};
1040
Linus Torvaldsb0cbc862009-04-11 12:45:20 -07001041static void __init acpi_battery_init_async(void *unused, async_cookie_t cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
Pavel Machek4d8316d2006-08-14 22:37:22 -07001043 if (acpi_disabled)
Arjan van de Ven0f66af52009-01-10 14:19:05 -05001044 return;
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +03001045#ifdef CONFIG_ACPI_PROCFS_POWER
Rich Townsend3f86b832006-07-01 11:36:54 -04001046 acpi_battery_dir = acpi_lock_battery_dir();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 if (!acpi_battery_dir)
Arjan van de Ven0f66af52009-01-10 14:19:05 -05001048 return;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +04001049#endif
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +04001050 if (acpi_bus_register_driver(&acpi_battery_driver) < 0) {
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +03001051#ifdef CONFIG_ACPI_PROCFS_POWER
Rich Townsend3f86b832006-07-01 11:36:54 -04001052 acpi_unlock_battery_dir(acpi_battery_dir);
Alexey Starikovskiyd7380962007-09-26 19:43:04 +04001053#endif
Arjan van de Ven0f66af52009-01-10 14:19:05 -05001054 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 }
Arjan van de Ven0f66af52009-01-10 14:19:05 -05001056 return;
1057}
1058
1059static int __init acpi_battery_init(void)
1060{
1061 async_schedule(acpi_battery_init_async, NULL);
Patrick Mocheld550d982006-06-27 00:41:40 -04001062 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063}
1064
Len Brown4be44fc2005-08-05 00:44:28 -04001065static void __exit acpi_battery_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 acpi_bus_unregister_driver(&acpi_battery_driver);
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +03001068#ifdef CONFIG_ACPI_PROCFS_POWER
Rich Townsend3f86b832006-07-01 11:36:54 -04001069 acpi_unlock_battery_dir(acpi_battery_dir);
Alexey Starikovskiyd7380962007-09-26 19:43:04 +04001070#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071}
1072
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073module_init(acpi_battery_init);
1074module_exit(acpi_battery_exit);