blob: 7711d94a04091442fcf7bf261c09d7db93186ebc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +04002 * battery.c - ACPI Battery Driver (Revision: 2.0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +04004 * Copyright (C) 2007 Alexey Starikovskiy <astarikovskiy@suse.de>
5 * Copyright (C) 2004-2007 Vladimir Lebedev <vladimir.p.lebedev@intel.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
7 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
8 *
9 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or (at
14 * your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
24 *
25 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
26 */
27
28#include <linux/kernel.h>
29#include <linux/module.h>
30#include <linux/init.h>
31#include <linux/types.h>
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +040032#include <linux/jiffies.h>
Arjan van de Ven0f66af52009-01-10 14:19:05 -050033#include <linux/async.h>
Hector Martinbc76f902009-08-06 15:57:48 -070034#include <linux/dmi.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Kyle McMartin25be5822011-03-22 16:19:50 -040036#include <linux/suspend.h>
Alexey Starikovskiyd7380962007-09-26 19:43:04 +040037
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +030038#ifdef CONFIG_ACPI_PROCFS_POWER
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/proc_fs.h>
40#include <linux/seq_file.h>
41#include <asm/uaccess.h>
Alexey Starikovskiyd7380962007-09-26 19:43:04 +040042#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44#include <acpi/acpi_bus.h>
45#include <acpi/acpi_drivers.h>
Alexey Starikovskiyd7380962007-09-26 19:43:04 +040046#include <linux/power_supply.h>
47
Len Browna192a952009-07-28 16:45:54 -040048#define PREFIX "ACPI: "
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#define ACPI_BATTERY_VALUE_UNKNOWN 0xFFFFFFFF
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#define ACPI_BATTERY_CLASS "battery"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#define ACPI_BATTERY_DEVICE_NAME "Battery"
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#define ACPI_BATTERY_NOTIFY_STATUS 0x80
55#define ACPI_BATTERY_NOTIFY_INFO 0x81
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +040056#define ACPI_BATTERY_NOTIFY_THRESHOLD 0x82
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Lan Tianyuae6f6182011-06-30 11:32:40 +080058/* Battery power unit: 0 means mW, 1 means mA */
59#define ACPI_BATTERY_POWER_UNIT_MA 1
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#define _COMPONENT ACPI_BATTERY_COMPONENT
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +030062
Len Brownf52fd662007-02-12 22:42:12 -050063ACPI_MODULE_NAME("battery");
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Len Brownf52fd662007-02-12 22:42:12 -050065MODULE_AUTHOR("Paul Diefenbaugh");
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +040066MODULE_AUTHOR("Alexey Starikovskiy <astarikovskiy@suse.de>");
Len Brown7cda93e2007-02-12 23:50:02 -050067MODULE_DESCRIPTION("ACPI Battery Driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070068MODULE_LICENSE("GPL");
69
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +040070static unsigned int cache_time = 1000;
71module_param(cache_time, uint, 0644);
72MODULE_PARM_DESC(cache_time, "cache time in milliseconds");
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +030073
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +030074#ifdef CONFIG_ACPI_PROCFS_POWER
Rich Townsend3f86b832006-07-01 11:36:54 -040075extern struct proc_dir_entry *acpi_lock_battery_dir(void);
76extern void *acpi_unlock_battery_dir(struct proc_dir_entry *acpi_battery_dir);
77
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +040078enum acpi_battery_files {
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +040079 info_tag = 0,
80 state_tag,
81 alarm_tag,
Alexey Starikovskiy78490d82007-05-11 13:18:55 -040082 ACPI_BATTERY_NUMFILES,
83};
84
Alexey Starikovskiyd7380962007-09-26 19:43:04 +040085#endif
86
87static const struct acpi_device_id battery_device_ids[] = {
88 {"PNP0C0A", 0},
89 {"", 0},
90};
91
92MODULE_DEVICE_TABLE(acpi, battery_device_ids);
93
Alexey Starikovskiy7b3bcc42009-10-15 14:31:24 +040094enum {
95 ACPI_BATTERY_ALARM_PRESENT,
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +040096 ACPI_BATTERY_XINFO_PRESENT,
Zhang Rui557d5862010-10-22 10:02:06 +080097 ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY,
Alexey Starikovskiy7b3bcc42009-10-15 14:31:24 +040098};
Alexey Starikovskiyd7380962007-09-26 19:43:04 +040099
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400100struct acpi_battery {
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400101 struct mutex lock;
Sergey Senozhatsky69d94ec2011-08-06 01:34:08 +0300102 struct mutex sysfs_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;
Kyle McMartin25be5822011-03-22 16:19:50 -0400105 struct notifier_block pm_nb;
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400106 unsigned long update_time;
Alexey Starikovskiy7faa1442009-03-27 22:23:52 -0400107 int rate_now;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400108 int capacity_now;
109 int voltage_now;
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400110 int design_capacity;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400111 int full_charge_capacity;
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400112 int technology;
113 int design_voltage;
114 int design_capacity_warning;
115 int design_capacity_low;
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400116 int cycle_count;
117 int measurement_accuracy;
118 int max_sampling_time;
119 int min_sampling_time;
120 int max_averaging_interval;
121 int min_averaging_interval;
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400122 int capacity_granularity_1;
123 int capacity_granularity_2;
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400124 int alarm;
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400125 char model_number[32];
126 char serial_number[32];
127 char type[32];
128 char oem_info[32];
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400129 int state;
130 int power_unit;
Alexey Starikovskiy7b3bcc42009-10-15 14:31:24 +0400131 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132};
133
Phil Carmody497888c2011-07-14 15:07:13 +0300134#define to_acpi_battery(x) container_of(x, struct acpi_battery, bat)
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400135
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400136inline int acpi_battery_present(struct acpi_battery *battery)
137{
138 return battery->device->status.battery_present;
139}
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400140
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400141static int acpi_battery_technology(struct acpi_battery *battery)
142{
143 if (!strcasecmp("NiCd", battery->type))
144 return POWER_SUPPLY_TECHNOLOGY_NiCd;
145 if (!strcasecmp("NiMH", battery->type))
146 return POWER_SUPPLY_TECHNOLOGY_NiMH;
147 if (!strcasecmp("LION", battery->type))
148 return POWER_SUPPLY_TECHNOLOGY_LION;
Andrey Borzenkovad40e682007-11-10 20:02:49 +0300149 if (!strncasecmp("LI-ION", battery->type, 6))
Alexey Starikovskiy0bde7ee2007-10-28 15:33:10 +0300150 return POWER_SUPPLY_TECHNOLOGY_LION;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400151 if (!strcasecmp("LiP", battery->type))
152 return POWER_SUPPLY_TECHNOLOGY_LIPO;
153 return POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
154}
155
Alexey Starikovskiy91044762007-11-13 12:23:06 +0300156static int acpi_battery_get_state(struct acpi_battery *battery);
Alexey Starikovskiyb19073a2007-10-25 17:10:47 -0400157
Richard Hughes56f382a2009-01-25 15:05:50 +0000158static int acpi_battery_is_charged(struct acpi_battery *battery)
159{
160 /* either charging or discharging */
161 if (battery->state != 0)
162 return 0;
163
164 /* battery not reporting charge */
165 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN ||
166 battery->capacity_now == 0)
167 return 0;
168
169 /* good batteries update full_charge as the batteries degrade */
170 if (battery->full_charge_capacity == battery->capacity_now)
171 return 1;
172
173 /* fallback to using design values for broken batteries */
174 if (battery->design_capacity == battery->capacity_now)
175 return 1;
176
177 /* we don't do any sort of metric based on percentages */
178 return 0;
179}
180
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400181static int acpi_battery_get_property(struct power_supply *psy,
182 enum power_supply_property psp,
183 union power_supply_propval *val)
184{
Rafael J. Wysockia1b4bd62010-10-23 19:35:15 +0200185 int ret = 0;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400186 struct acpi_battery *battery = to_acpi_battery(psy);
187
Alexey Starikovskiy91044762007-11-13 12:23:06 +0300188 if (acpi_battery_present(battery)) {
189 /* run battery update only if it is present */
190 acpi_battery_get_state(battery);
191 } else if (psp != POWER_SUPPLY_PROP_PRESENT)
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400192 return -ENODEV;
193 switch (psp) {
194 case POWER_SUPPLY_PROP_STATUS:
195 if (battery->state & 0x01)
196 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
197 else if (battery->state & 0x02)
198 val->intval = POWER_SUPPLY_STATUS_CHARGING;
Richard Hughes56f382a2009-01-25 15:05:50 +0000199 else if (acpi_battery_is_charged(battery))
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400200 val->intval = POWER_SUPPLY_STATUS_FULL;
Roland Dreier4c41d3a2007-11-07 15:09:09 -0800201 else
202 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400203 break;
204 case POWER_SUPPLY_PROP_PRESENT:
205 val->intval = acpi_battery_present(battery);
206 break;
207 case POWER_SUPPLY_PROP_TECHNOLOGY:
208 val->intval = acpi_battery_technology(battery);
209 break;
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400210 case POWER_SUPPLY_PROP_CYCLE_COUNT:
211 val->intval = battery->cycle_count;
212 break;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400213 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
Rafael J. Wysockia1b4bd62010-10-23 19:35:15 +0200214 if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
215 ret = -ENODEV;
216 else
217 val->intval = battery->design_voltage * 1000;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400218 break;
219 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
Rafael J. Wysockia1b4bd62010-10-23 19:35:15 +0200220 if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN)
221 ret = -ENODEV;
222 else
223 val->intval = battery->voltage_now * 1000;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400224 break;
225 case POWER_SUPPLY_PROP_CURRENT_NOW:
Alexey Starikovskiy7faa1442009-03-27 22:23:52 -0400226 case POWER_SUPPLY_PROP_POWER_NOW:
Rafael J. Wysockia1b4bd62010-10-23 19:35:15 +0200227 if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN)
228 ret = -ENODEV;
229 else
230 val->intval = battery->rate_now * 1000;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400231 break;
232 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
233 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
Rafael J. Wysockia1b4bd62010-10-23 19:35:15 +0200234 if (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
235 ret = -ENODEV;
236 else
237 val->intval = battery->design_capacity * 1000;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400238 break;
239 case POWER_SUPPLY_PROP_CHARGE_FULL:
240 case POWER_SUPPLY_PROP_ENERGY_FULL:
Rafael J. Wysockia1b4bd62010-10-23 19:35:15 +0200241 if (battery->full_charge_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
242 ret = -ENODEV;
243 else
244 val->intval = battery->full_charge_capacity * 1000;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400245 break;
246 case POWER_SUPPLY_PROP_CHARGE_NOW:
247 case POWER_SUPPLY_PROP_ENERGY_NOW:
Rafael J. Wysockia1b4bd62010-10-23 19:35:15 +0200248 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN)
249 ret = -ENODEV;
250 else
251 val->intval = battery->capacity_now * 1000;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400252 break;
253 case POWER_SUPPLY_PROP_MODEL_NAME:
254 val->strval = battery->model_number;
255 break;
256 case POWER_SUPPLY_PROP_MANUFACTURER:
257 val->strval = battery->oem_info;
258 break;
maximilian attems7c2670b2008-01-22 18:46:50 +0100259 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
260 val->strval = battery->serial_number;
261 break;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400262 default:
Rafael J. Wysockia1b4bd62010-10-23 19:35:15 +0200263 ret = -EINVAL;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400264 }
Rafael J. Wysockia1b4bd62010-10-23 19:35:15 +0200265 return ret;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400266}
267
268static enum power_supply_property charge_battery_props[] = {
269 POWER_SUPPLY_PROP_STATUS,
270 POWER_SUPPLY_PROP_PRESENT,
271 POWER_SUPPLY_PROP_TECHNOLOGY,
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400272 POWER_SUPPLY_PROP_CYCLE_COUNT,
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400273 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
274 POWER_SUPPLY_PROP_VOLTAGE_NOW,
275 POWER_SUPPLY_PROP_CURRENT_NOW,
276 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
277 POWER_SUPPLY_PROP_CHARGE_FULL,
278 POWER_SUPPLY_PROP_CHARGE_NOW,
279 POWER_SUPPLY_PROP_MODEL_NAME,
280 POWER_SUPPLY_PROP_MANUFACTURER,
maximilian attems7c2670b2008-01-22 18:46:50 +0100281 POWER_SUPPLY_PROP_SERIAL_NUMBER,
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400282};
283
284static enum power_supply_property energy_battery_props[] = {
285 POWER_SUPPLY_PROP_STATUS,
286 POWER_SUPPLY_PROP_PRESENT,
287 POWER_SUPPLY_PROP_TECHNOLOGY,
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400288 POWER_SUPPLY_PROP_CYCLE_COUNT,
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400289 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
290 POWER_SUPPLY_PROP_VOLTAGE_NOW,
Alexey Starikovskiy7faa1442009-03-27 22:23:52 -0400291 POWER_SUPPLY_PROP_POWER_NOW,
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400292 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
293 POWER_SUPPLY_PROP_ENERGY_FULL,
294 POWER_SUPPLY_PROP_ENERGY_NOW,
295 POWER_SUPPLY_PROP_MODEL_NAME,
296 POWER_SUPPLY_PROP_MANUFACTURER,
maximilian attems7c2670b2008-01-22 18:46:50 +0100297 POWER_SUPPLY_PROP_SERIAL_NUMBER,
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400298};
299
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +0300300#ifdef CONFIG_ACPI_PROCFS_POWER
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400301inline char *acpi_battery_units(struct acpi_battery *battery)
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400302{
Lan Tianyuae6f6182011-06-30 11:32:40 +0800303 return (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA) ?
304 "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
Lan Tianyu55003b22011-06-30 11:33:12 +0800464 /* For buggy DSDTs that report negative 16-bit values for either
465 * charging or discharging current and/or report 0 as 65536
466 * due to bad math.
467 */
468 if (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA &&
469 battery->rate_now != ACPI_BATTERY_VALUE_UNKNOWN &&
470 (s16)(battery->rate_now) < 0) {
Hector Martinbc76f902009-08-06 15:57:48 -0700471 battery->rate_now = abs((s16)battery->rate_now);
Lan Tianyu55003b22011-06-30 11:33:12 +0800472 printk_once(KERN_WARNING FW_BUG "battery: (dis)charge rate"
473 " invalid.\n");
474 }
Hector Martinbc76f902009-08-06 15:57:48 -0700475
Zhang Rui557d5862010-10-22 10:02:06 +0800476 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags)
477 && battery->capacity_now >= 0 && battery->capacity_now <= 100)
478 battery->capacity_now = (battery->capacity_now *
479 battery->full_charge_capacity) / 100;
Patrick Mocheld550d982006-06-27 00:41:40 -0400480 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481}
482
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400483static int acpi_battery_set_alarm(struct acpi_battery *battery)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
Len Brown4be44fc2005-08-05 00:44:28 -0400485 acpi_status status = 0;
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400486 union acpi_object arg0 = { .type = ACPI_TYPE_INTEGER };
Len Brown4be44fc2005-08-05 00:44:28 -0400487 struct acpi_object_list arg_list = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400489 if (!acpi_battery_present(battery) ||
Alexey Starikovskiy7b3bcc42009-10-15 14:31:24 +0400490 !test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags))
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300491 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400493 arg0.integer.value = battery->alarm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400495 mutex_lock(&battery->lock);
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400496 status = acpi_evaluate_object(battery->device->handle, "_BTP",
497 &arg_list, NULL);
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400498 mutex_unlock(&battery->lock);
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400501 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400503 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", battery->alarm));
Patrick Mocheld550d982006-06-27 00:41:40 -0400504 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505}
506
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300507static int acpi_battery_init_alarm(struct acpi_battery *battery)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
Len Brown4be44fc2005-08-05 00:44:28 -0400509 acpi_status status = AE_OK;
510 acpi_handle handle = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300512 /* See if alarms are supported, and if so, set default */
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400513 status = acpi_get_handle(battery->device->handle, "_BTP", &handle);
514 if (ACPI_FAILURE(status)) {
Alexey Starikovskiy7b3bcc42009-10-15 14:31:24 +0400515 clear_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400516 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 }
Alexey Starikovskiy7b3bcc42009-10-15 14:31:24 +0400518 set_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400519 if (!battery->alarm)
520 battery->alarm = battery->design_capacity_warning;
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400521 return acpi_battery_set_alarm(battery);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522}
523
Andrey Borzenkov508df922007-10-28 12:50:09 +0300524static ssize_t acpi_battery_alarm_show(struct device *dev,
525 struct device_attribute *attr,
526 char *buf)
527{
528 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
529 return sprintf(buf, "%d\n", battery->alarm * 1000);
530}
531
532static ssize_t acpi_battery_alarm_store(struct device *dev,
533 struct device_attribute *attr,
534 const char *buf, size_t count)
535{
536 unsigned long x;
537 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
538 if (sscanf(buf, "%ld\n", &x) == 1)
539 battery->alarm = x/1000;
540 if (acpi_battery_present(battery))
541 acpi_battery_set_alarm(battery);
542 return count;
543}
544
545static struct device_attribute alarm_attr = {
Parag Warudkar01e8ef12008-10-18 20:28:50 -0700546 .attr = {.name = "alarm", .mode = 0644},
Andrey Borzenkov508df922007-10-28 12:50:09 +0300547 .show = acpi_battery_alarm_show,
548 .store = acpi_battery_alarm_store,
549};
550
551static int sysfs_add_battery(struct acpi_battery *battery)
552{
553 int result;
554
Lan Tianyuae6f6182011-06-30 11:32:40 +0800555 if (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA) {
Andrey Borzenkov508df922007-10-28 12:50:09 +0300556 battery->bat.properties = charge_battery_props;
557 battery->bat.num_properties =
558 ARRAY_SIZE(charge_battery_props);
559 } else {
560 battery->bat.properties = energy_battery_props;
561 battery->bat.num_properties =
562 ARRAY_SIZE(energy_battery_props);
563 }
564
565 battery->bat.name = acpi_device_bid(battery->device);
566 battery->bat.type = POWER_SUPPLY_TYPE_BATTERY;
567 battery->bat.get_property = acpi_battery_get_property;
568
569 result = power_supply_register(&battery->device->dev, &battery->bat);
570 if (result)
571 return result;
572 return device_create_file(battery->bat.dev, &alarm_attr);
573}
574
575static void sysfs_remove_battery(struct acpi_battery *battery)
576{
Sergey Senozhatsky69d94ec2011-08-06 01:34:08 +0300577 mutex_lock(&battery->sysfs_lock);
Lan Tianyu9c921c222011-06-30 11:34:12 +0800578 if (!battery->bat.dev) {
Sergey Senozhatsky69d94ec2011-08-06 01:34:08 +0300579 mutex_unlock(&battery->sysfs_lock);
Andrey Borzenkov508df922007-10-28 12:50:09 +0300580 return;
Lan Tianyu9c921c222011-06-30 11:34:12 +0800581 }
582
Andrey Borzenkov508df922007-10-28 12:50:09 +0300583 device_remove_file(battery->bat.dev, &alarm_attr);
584 power_supply_unregister(&battery->bat);
Alexey Starikovskiy91044762007-11-13 12:23:06 +0300585 battery->bat.dev = NULL;
Sergey Senozhatsky69d94ec2011-08-06 01:34:08 +0300586 mutex_unlock(&battery->sysfs_lock);
Hector Martinbc76f902009-08-06 15:57:48 -0700587}
588
Zhang Rui557d5862010-10-22 10:02:06 +0800589/*
590 * According to the ACPI spec, some kinds of primary batteries can
591 * report percentage battery remaining capacity directly to OS.
592 * In this case, it reports the Last Full Charged Capacity == 100
593 * and BatteryPresentRate == 0xFFFFFFFF.
594 *
595 * Now we found some battery reports percentage remaining capacity
596 * even if it's rechargeable.
597 * https://bugzilla.kernel.org/show_bug.cgi?id=15979
598 *
599 * Handle this correctly so that they won't break userspace.
600 */
Lan Tianyu7b786222011-06-30 11:33:27 +0800601static void acpi_battery_quirks(struct acpi_battery *battery)
Zhang Rui557d5862010-10-22 10:02:06 +0800602{
603 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags))
604 return ;
605
606 if (battery->full_charge_capacity == 100 &&
607 battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN &&
608 battery->capacity_now >=0 && battery->capacity_now <= 100) {
609 set_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags);
610 battery->full_charge_capacity = battery->design_capacity;
611 battery->capacity_now = (battery->capacity_now *
612 battery->full_charge_capacity) / 100;
613 }
614}
615
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400616static int acpi_battery_update(struct acpi_battery *battery)
Vladimir Lebedev4bd35cd2007-02-10 01:43:48 -0500617{
Alexey Starikovskiy50b17852008-12-23 02:44:54 +0300618 int result, old_present = acpi_battery_present(battery);
Alexey Starikovskiy97749cd2008-01-01 14:27:24 -0500619 result = acpi_battery_get_status(battery);
Andrey Borzenkov508df922007-10-28 12:50:09 +0300620 if (result)
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300621 return result;
Andrey Borzenkov508df922007-10-28 12:50:09 +0300622 if (!acpi_battery_present(battery)) {
623 sysfs_remove_battery(battery);
Alexey Starikovskiy97749cd2008-01-01 14:27:24 -0500624 battery->update_time = 0;
Andrey Borzenkov508df922007-10-28 12:50:09 +0300625 return 0;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300626 }
Alexey Starikovskiy50b17852008-12-23 02:44:54 +0300627 if (!battery->update_time ||
628 old_present != acpi_battery_present(battery)) {
Alexey Starikovskiy97749cd2008-01-01 14:27:24 -0500629 result = acpi_battery_get_info(battery);
630 if (result)
631 return result;
632 acpi_battery_init_alarm(battery);
633 }
Stefan Hajnoczieb03cb02011-07-12 09:03:29 +0100634 if (!battery->bat.dev) {
635 result = sysfs_add_battery(battery);
636 if (result)
637 return result;
638 }
Zhang Rui557d5862010-10-22 10:02:06 +0800639 result = acpi_battery_get_state(battery);
Lan Tianyu7b786222011-06-30 11:33:27 +0800640 acpi_battery_quirks(battery);
Zhang Rui557d5862010-10-22 10:02:06 +0800641 return result;
Vladimir Lebedev4bd35cd2007-02-10 01:43:48 -0500642}
643
Rafael J. Wysockida8aeb92011-01-06 23:42:27 +0100644static void acpi_battery_refresh(struct acpi_battery *battery)
645{
646 if (!battery->bat.dev)
647 return;
648
649 acpi_battery_get_info(battery);
650 /* The battery may have changed its reporting units. */
651 sysfs_remove_battery(battery);
652 sysfs_add_battery(battery);
653}
654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655/* --------------------------------------------------------------------------
656 FS Interface (/proc)
657 -------------------------------------------------------------------------- */
658
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +0300659#ifdef CONFIG_ACPI_PROCFS_POWER
Len Brown4be44fc2005-08-05 00:44:28 -0400660static struct proc_dir_entry *acpi_battery_dir;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300661
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400662static int acpi_battery_print_info(struct seq_file *seq, int result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200664 struct acpi_battery *battery = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300666 if (result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 goto end;
668
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400669 seq_printf(seq, "present: %s\n",
670 acpi_battery_present(battery)?"yes":"no");
671 if (!acpi_battery_present(battery))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 goto end;
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400673 if (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 seq_printf(seq, "design capacity: unknown\n");
675 else
676 seq_printf(seq, "design capacity: %d %sh\n",
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400677 battery->design_capacity,
678 acpi_battery_units(battery));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400680 if (battery->full_charge_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 seq_printf(seq, "last full capacity: unknown\n");
682 else
683 seq_printf(seq, "last full capacity: %d %sh\n",
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400684 battery->full_charge_capacity,
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400685 acpi_battery_units(battery));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400687 seq_printf(seq, "battery technology: %srechargeable\n",
688 (!battery->technology)?"non-":"");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400690 if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 seq_printf(seq, "design voltage: unknown\n");
692 else
693 seq_printf(seq, "design voltage: %d mV\n",
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400694 battery->design_voltage);
Len Brown4be44fc2005-08-05 00:44:28 -0400695 seq_printf(seq, "design capacity warning: %d %sh\n",
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400696 battery->design_capacity_warning,
697 acpi_battery_units(battery));
Len Brown4be44fc2005-08-05 00:44:28 -0400698 seq_printf(seq, "design capacity low: %d %sh\n",
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400699 battery->design_capacity_low,
700 acpi_battery_units(battery));
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400701 seq_printf(seq, "cycle count: %i\n", battery->cycle_count);
Len Brown4be44fc2005-08-05 00:44:28 -0400702 seq_printf(seq, "capacity granularity 1: %d %sh\n",
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400703 battery->capacity_granularity_1,
704 acpi_battery_units(battery));
Len Brown4be44fc2005-08-05 00:44:28 -0400705 seq_printf(seq, "capacity granularity 2: %d %sh\n",
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400706 battery->capacity_granularity_2,
707 acpi_battery_units(battery));
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400708 seq_printf(seq, "model number: %s\n", battery->model_number);
709 seq_printf(seq, "serial number: %s\n", battery->serial_number);
710 seq_printf(seq, "battery type: %s\n", battery->type);
711 seq_printf(seq, "OEM info: %s\n", battery->oem_info);
Len Brown4be44fc2005-08-05 00:44:28 -0400712 end:
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300713 if (result)
714 seq_printf(seq, "ERROR: Unable to read battery info\n");
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300715 return result;
716}
717
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400718static int acpi_battery_print_state(struct seq_file *seq, int result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200720 struct acpi_battery *battery = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300722 if (result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 goto end;
724
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400725 seq_printf(seq, "present: %s\n",
726 acpi_battery_present(battery)?"yes":"no");
727 if (!acpi_battery_present(battery))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400730 seq_printf(seq, "capacity state: %s\n",
731 (battery->state & 0x04)?"critical":"ok");
732 if ((battery->state & 0x01) && (battery->state & 0x02))
Len Brown4be44fc2005-08-05 00:44:28 -0400733 seq_printf(seq,
734 "charging state: charging/discharging\n");
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400735 else if (battery->state & 0x01)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 seq_printf(seq, "charging state: discharging\n");
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400737 else if (battery->state & 0x02)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 seq_printf(seq, "charging state: charging\n");
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400739 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 seq_printf(seq, "charging state: charged\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Alexey Starikovskiy7faa1442009-03-27 22:23:52 -0400742 if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 seq_printf(seq, "present rate: unknown\n");
744 else
745 seq_printf(seq, "present rate: %d %s\n",
Alexey Starikovskiy7faa1442009-03-27 22:23:52 -0400746 battery->rate_now, acpi_battery_units(battery));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400748 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 seq_printf(seq, "remaining capacity: unknown\n");
750 else
751 seq_printf(seq, "remaining capacity: %d %sh\n",
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400752 battery->capacity_now, acpi_battery_units(battery));
753 if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 seq_printf(seq, "present voltage: unknown\n");
755 else
756 seq_printf(seq, "present voltage: %d mV\n",
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400757 battery->voltage_now);
Len Brown4be44fc2005-08-05 00:44:28 -0400758 end:
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400759 if (result)
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300760 seq_printf(seq, "ERROR: Unable to read battery state\n");
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300761
762 return result;
763}
764
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400765static int acpi_battery_print_alarm(struct seq_file *seq, int result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200767 struct acpi_battery *battery = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300769 if (result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 goto end;
771
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300772 if (!acpi_battery_present(battery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 seq_printf(seq, "present: no\n");
774 goto end;
775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 seq_printf(seq, "alarm: ");
777 if (!battery->alarm)
778 seq_printf(seq, "unsupported\n");
779 else
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400780 seq_printf(seq, "%u %sh\n", battery->alarm,
781 acpi_battery_units(battery));
Len Brown4be44fc2005-08-05 00:44:28 -0400782 end:
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300783 if (result)
784 seq_printf(seq, "ERROR: Unable to read battery alarm\n");
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300785 return result;
786}
787
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400788static ssize_t acpi_battery_write_alarm(struct file *file,
789 const char __user * buffer,
790 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
Len Brown4be44fc2005-08-05 00:44:28 -0400792 int result = 0;
793 char alarm_string[12] = { '\0' };
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200794 struct seq_file *m = file->private_data;
795 struct acpi_battery *battery = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 if (!battery || (count > sizeof(alarm_string) - 1))
Patrick Mocheld550d982006-06-27 00:41:40 -0400798 return -EINVAL;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300799 if (!acpi_battery_present(battery)) {
800 result = -ENODEV;
801 goto end;
802 }
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300803 if (copy_from_user(alarm_string, buffer, count)) {
804 result = -EFAULT;
805 goto end;
806 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 alarm_string[count] = '\0';
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400808 battery->alarm = simple_strtol(alarm_string, NULL, 0);
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400809 result = acpi_battery_set_alarm(battery);
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300810 end:
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300811 if (!result)
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400812 return count;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300813 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814}
815
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400816typedef int(*print_func)(struct seq_file *seq, int result);
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400817
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400818static print_func acpi_print_funcs[ACPI_BATTERY_NUMFILES] = {
819 acpi_battery_print_info,
820 acpi_battery_print_state,
821 acpi_battery_print_alarm,
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400822};
823
824static int acpi_battery_read(int fid, struct seq_file *seq)
825{
826 struct acpi_battery *battery = seq->private;
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400827 int result = acpi_battery_update(battery);
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400828 return acpi_print_funcs[fid](seq, result);
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400829}
830
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400831#define DECLARE_FILE_FUNCTIONS(_name) \
832static int acpi_battery_read_##_name(struct seq_file *seq, void *offset) \
833{ \
834 return acpi_battery_read(_name##_tag, seq); \
835} \
836static int acpi_battery_##_name##_open_fs(struct inode *inode, struct file *file) \
837{ \
838 return single_open(file, acpi_battery_read_##_name, PDE(inode)->data); \
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400839}
840
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400841DECLARE_FILE_FUNCTIONS(info);
842DECLARE_FILE_FUNCTIONS(state);
843DECLARE_FILE_FUNCTIONS(alarm);
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400844
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400845#undef DECLARE_FILE_FUNCTIONS
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400846
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400847#define FILE_DESCRIPTION_RO(_name) \
848 { \
849 .name = __stringify(_name), \
850 .mode = S_IRUGO, \
851 .ops = { \
852 .open = acpi_battery_##_name##_open_fs, \
853 .read = seq_read, \
854 .llseek = seq_lseek, \
855 .release = single_release, \
856 .owner = THIS_MODULE, \
857 }, \
858 }
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400859
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400860#define FILE_DESCRIPTION_RW(_name) \
861 { \
862 .name = __stringify(_name), \
863 .mode = S_IFREG | S_IRUGO | S_IWUSR, \
864 .ops = { \
865 .open = acpi_battery_##_name##_open_fs, \
866 .read = seq_read, \
867 .llseek = seq_lseek, \
868 .write = acpi_battery_write_##_name, \
869 .release = single_release, \
870 .owner = THIS_MODULE, \
871 }, \
872 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Vasiliy Kulikov9c8b04b2011-06-25 21:07:52 +0400874static const struct battery_file {
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400875 struct file_operations ops;
876 mode_t mode;
Jan Engelhardt070d8eb2009-01-12 00:07:55 +0100877 const char *name;
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400878} acpi_battery_file[] = {
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400879 FILE_DESCRIPTION_RO(info),
880 FILE_DESCRIPTION_RO(state),
881 FILE_DESCRIPTION_RW(alarm),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882};
883
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400884#undef FILE_DESCRIPTION_RO
885#undef FILE_DESCRIPTION_RW
886
Len Brown4be44fc2005-08-05 00:44:28 -0400887static int acpi_battery_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
Len Brown4be44fc2005-08-05 00:44:28 -0400889 struct proc_dir_entry *entry = NULL;
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400890 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Zhang Rui6d855fc2011-01-10 11:16:30 +0800892 printk(KERN_WARNING PREFIX "Deprecated procfs I/F for battery is loaded,"
893 " please retry with CONFIG_ACPI_PROCFS_POWER cleared\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 if (!acpi_device_dir(device)) {
895 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown4be44fc2005-08-05 00:44:28 -0400896 acpi_battery_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400898 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 }
900
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400901 for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i) {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700902 entry = proc_create_data(acpi_battery_file[i].name,
903 acpi_battery_file[i].mode,
904 acpi_device_dir(device),
905 &acpi_battery_file[i].ops,
906 acpi_driver_data(device));
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400907 if (!entry)
908 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 }
Patrick Mocheld550d982006-06-27 00:41:40 -0400910 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911}
912
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400913static void acpi_battery_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914{
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400915 int i;
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400916 if (!acpi_device_dir(device))
917 return;
918 for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i)
919 remove_proc_entry(acpi_battery_file[i].name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400922 remove_proc_entry(acpi_device_bid(device), acpi_battery_dir);
923 acpi_device_dir(device) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924}
925
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400926#endif
Alexey Starikovskiy3e58ea02007-09-26 19:43:11 +0400927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928/* --------------------------------------------------------------------------
929 Driver Interface
930 -------------------------------------------------------------------------- */
931
Bjorn Helgaasd9406692009-04-30 09:35:47 -0600932static void acpi_battery_notify(struct acpi_device *device, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
Bjorn Helgaasd9406692009-04-30 09:35:47 -0600934 struct acpi_battery *battery = acpi_driver_data(device);
Zhang Rui153e5002010-07-07 09:11:57 +0800935 struct device *old;
Bjorn Helgaasd9406692009-04-30 09:35:47 -0600936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 if (!battery)
Patrick Mocheld550d982006-06-27 00:41:40 -0400938 return;
Zhang Rui153e5002010-07-07 09:11:57 +0800939 old = battery->bat.dev;
Rafael J. Wysockida8aeb92011-01-06 23:42:27 +0100940 if (event == ACPI_BATTERY_NOTIFY_INFO)
941 acpi_battery_refresh(battery);
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400942 acpi_battery_update(battery);
943 acpi_bus_generate_proc_event(device, event,
944 acpi_battery_present(battery));
945 acpi_bus_generate_netlink_event(device->pnp.device_class,
Kay Sievers07944692008-10-30 01:18:59 +0100946 dev_name(&device->dev), event,
Vladimir Lebedev9ea7d572007-02-20 15:48:06 +0300947 acpi_battery_present(battery));
Justin P. Mattock2345baf2009-12-13 14:42:36 -0800948 /* acpi_battery_update could remove power_supply object */
Zhang Rui153e5002010-07-07 09:11:57 +0800949 if (old && battery->bat.dev)
Alan Jenkinsf79e1ce2009-06-30 14:36:16 +0000950 power_supply_changed(&battery->bat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951}
952
Kyle McMartin25be5822011-03-22 16:19:50 -0400953static int battery_notify(struct notifier_block *nb,
954 unsigned long mode, void *_unused)
955{
956 struct acpi_battery *battery = container_of(nb, struct acpi_battery,
957 pm_nb);
958 switch (mode) {
Lan Tianyud5a59112011-06-30 11:33:40 +0800959 case PM_POST_HIBERNATION:
Kyle McMartin25be5822011-03-22 16:19:50 -0400960 case PM_POST_SUSPEND:
Lan Tianyu6e17fb62011-06-30 11:33:58 +0800961 if (battery->bat.dev) {
962 sysfs_remove_battery(battery);
963 sysfs_add_battery(battery);
964 }
Kyle McMartin25be5822011-03-22 16:19:50 -0400965 break;
966 }
967
968 return 0;
969}
970
Len Brown4be44fc2005-08-05 00:44:28 -0400971static int acpi_battery_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
Len Brown4be44fc2005-08-05 00:44:28 -0400973 int result = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400974 struct acpi_battery *battery = NULL;
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400975 acpi_handle handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400977 return -EINVAL;
Burman Yan36bcbec2006-12-19 12:56:11 -0800978 battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 if (!battery)
Patrick Mocheld550d982006-06-27 00:41:40 -0400980 return -ENOMEM;
Patrick Mochel145def82006-05-19 16:54:39 -0400981 battery->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME);
983 strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700984 device->driver_data = battery;
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400985 mutex_init(&battery->lock);
Sergey Senozhatsky69d94ec2011-08-06 01:34:08 +0300986 mutex_init(&battery->sysfs_lock);
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400987 if (ACPI_SUCCESS(acpi_get_handle(battery->device->handle,
988 "_BIX", &handle)))
989 set_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags);
Stefan Hajnoczieb03cb02011-07-12 09:03:29 +0100990 result = acpi_battery_update(battery);
991 if (result)
992 goto fail;
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +0300993#ifdef CONFIG_ACPI_PROCFS_POWER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 result = acpi_battery_add_fs(device);
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400995#endif
Stefan Hajnoczie80bba42011-07-12 09:03:28 +0100996 if (result) {
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +0300997#ifdef CONFIG_ACPI_PROCFS_POWER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 acpi_battery_remove_fs(device);
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400999#endif
Stefan Hajnoczie80bba42011-07-12 09:03:28 +01001000 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 }
Kyle McMartin25be5822011-03-22 16:19:50 -04001002
Stefan Hajnoczie80bba42011-07-12 09:03:28 +01001003 printk(KERN_INFO PREFIX "%s Slot [%s] (battery %s)\n",
1004 ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device),
1005 device->status.battery_present ? "present" : "absent");
1006
Kyle McMartin25be5822011-03-22 16:19:50 -04001007 battery->pm_nb.notifier_call = battery_notify;
1008 register_pm_notifier(&battery->pm_nb);
1009
Patrick Mocheld550d982006-06-27 00:41:40 -04001010 return result;
Stefan Hajnoczie80bba42011-07-12 09:03:28 +01001011
1012fail:
1013 sysfs_remove_battery(battery);
1014 mutex_destroy(&battery->lock);
Sergey Senozhatsky69d94ec2011-08-06 01:34:08 +03001015 mutex_destroy(&battery->sysfs_lock);
Stefan Hajnoczie80bba42011-07-12 09:03:28 +01001016 kfree(battery);
1017 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018}
1019
Len Brown4be44fc2005-08-05 00:44:28 -04001020static int acpi_battery_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021{
Len Brown4be44fc2005-08-05 00:44:28 -04001022 struct acpi_battery *battery = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001025 return -EINVAL;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001026 battery = acpi_driver_data(device);
Kyle McMartin25be5822011-03-22 16:19:50 -04001027 unregister_pm_notifier(&battery->pm_nb);
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +03001028#ifdef CONFIG_ACPI_PROCFS_POWER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 acpi_battery_remove_fs(device);
Alexey Starikovskiyd7380962007-09-26 19:43:04 +04001030#endif
Andrey Borzenkov508df922007-10-28 12:50:09 +03001031 sysfs_remove_battery(battery);
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +04001032 mutex_destroy(&battery->lock);
Sergey Senozhatsky69d94ec2011-08-06 01:34:08 +03001033 mutex_destroy(&battery->sysfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 kfree(battery);
Patrick Mocheld550d982006-06-27 00:41:40 -04001035 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036}
1037
Jiri Kosina34c44152006-10-10 14:20:41 -07001038/* this is needed to learn about changes made in suspended state */
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001039static int acpi_battery_resume(struct acpi_device *device)
Jiri Kosina34c44152006-10-10 14:20:41 -07001040{
1041 struct acpi_battery *battery;
Jiri Kosina34c44152006-10-10 14:20:41 -07001042 if (!device)
1043 return -EINVAL;
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +04001044 battery = acpi_driver_data(device);
1045 battery->update_time = 0;
Andrey Borzenkov508df922007-10-28 12:50:09 +03001046 acpi_battery_update(battery);
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +03001047 return 0;
Jiri Kosina34c44152006-10-10 14:20:41 -07001048}
1049
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +04001050static struct acpi_driver acpi_battery_driver = {
1051 .name = "battery",
1052 .class = ACPI_BATTERY_CLASS,
1053 .ids = battery_device_ids,
Bjorn Helgaasd9406692009-04-30 09:35:47 -06001054 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +04001055 .ops = {
1056 .add = acpi_battery_add,
1057 .resume = acpi_battery_resume,
1058 .remove = acpi_battery_remove,
Bjorn Helgaasd9406692009-04-30 09:35:47 -06001059 .notify = acpi_battery_notify,
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +04001060 },
1061};
1062
Linus Torvaldsb0cbc862009-04-11 12:45:20 -07001063static void __init acpi_battery_init_async(void *unused, async_cookie_t cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
Pavel Machek4d8316d2006-08-14 22:37:22 -07001065 if (acpi_disabled)
Arjan van de Ven0f66af52009-01-10 14:19:05 -05001066 return;
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +03001067#ifdef CONFIG_ACPI_PROCFS_POWER
Rich Townsend3f86b832006-07-01 11:36:54 -04001068 acpi_battery_dir = acpi_lock_battery_dir();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 if (!acpi_battery_dir)
Arjan van de Ven0f66af52009-01-10 14:19:05 -05001070 return;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +04001071#endif
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +04001072 if (acpi_bus_register_driver(&acpi_battery_driver) < 0) {
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +03001073#ifdef CONFIG_ACPI_PROCFS_POWER
Rich Townsend3f86b832006-07-01 11:36:54 -04001074 acpi_unlock_battery_dir(acpi_battery_dir);
Alexey Starikovskiyd7380962007-09-26 19:43:04 +04001075#endif
Arjan van de Ven0f66af52009-01-10 14:19:05 -05001076 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 }
Arjan van de Ven0f66af52009-01-10 14:19:05 -05001078 return;
1079}
1080
1081static int __init acpi_battery_init(void)
1082{
1083 async_schedule(acpi_battery_init_async, NULL);
Patrick Mocheld550d982006-06-27 00:41:40 -04001084 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085}
1086
Len Brown4be44fc2005-08-05 00:44:28 -04001087static void __exit acpi_battery_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 acpi_bus_unregister_driver(&acpi_battery_driver);
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +03001090#ifdef CONFIG_ACPI_PROCFS_POWER
Rich Townsend3f86b832006-07-01 11:36:54 -04001091 acpi_unlock_battery_dir(acpi_battery_dir);
Alexey Starikovskiyd7380962007-09-26 19:43:04 +04001092#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093}
1094
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095module_init(acpi_battery_init);
1096module_exit(acpi_battery_exit);