blob: 45e3e1759fb88c09ddd401921d2a4ea035431bff [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;
srinivas pandruvadaa58e1152012-04-05 17:38:54 -0700253 case POWER_SUPPLY_PROP_CAPACITY:
254 if (battery->capacity_now && battery->full_charge_capacity)
255 val->intval = battery->capacity_now * 100/
256 battery->full_charge_capacity;
257 else
258 val->intval = 0;
259 break;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400260 case POWER_SUPPLY_PROP_MODEL_NAME:
261 val->strval = battery->model_number;
262 break;
263 case POWER_SUPPLY_PROP_MANUFACTURER:
264 val->strval = battery->oem_info;
265 break;
maximilian attems7c2670b2008-01-22 18:46:50 +0100266 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
267 val->strval = battery->serial_number;
268 break;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400269 default:
Rafael J. Wysockia1b4bd62010-10-23 19:35:15 +0200270 ret = -EINVAL;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400271 }
Rafael J. Wysockia1b4bd62010-10-23 19:35:15 +0200272 return ret;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400273}
274
275static enum power_supply_property charge_battery_props[] = {
276 POWER_SUPPLY_PROP_STATUS,
277 POWER_SUPPLY_PROP_PRESENT,
278 POWER_SUPPLY_PROP_TECHNOLOGY,
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400279 POWER_SUPPLY_PROP_CYCLE_COUNT,
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400280 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
281 POWER_SUPPLY_PROP_VOLTAGE_NOW,
282 POWER_SUPPLY_PROP_CURRENT_NOW,
283 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
284 POWER_SUPPLY_PROP_CHARGE_FULL,
285 POWER_SUPPLY_PROP_CHARGE_NOW,
srinivas pandruvadaa58e1152012-04-05 17:38:54 -0700286 POWER_SUPPLY_PROP_CAPACITY,
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400287 POWER_SUPPLY_PROP_MODEL_NAME,
288 POWER_SUPPLY_PROP_MANUFACTURER,
maximilian attems7c2670b2008-01-22 18:46:50 +0100289 POWER_SUPPLY_PROP_SERIAL_NUMBER,
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400290};
291
292static enum power_supply_property energy_battery_props[] = {
293 POWER_SUPPLY_PROP_STATUS,
294 POWER_SUPPLY_PROP_PRESENT,
295 POWER_SUPPLY_PROP_TECHNOLOGY,
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400296 POWER_SUPPLY_PROP_CYCLE_COUNT,
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400297 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
298 POWER_SUPPLY_PROP_VOLTAGE_NOW,
Alexey Starikovskiy7faa1442009-03-27 22:23:52 -0400299 POWER_SUPPLY_PROP_POWER_NOW,
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400300 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
301 POWER_SUPPLY_PROP_ENERGY_FULL,
302 POWER_SUPPLY_PROP_ENERGY_NOW,
srinivas pandruvadaa58e1152012-04-05 17:38:54 -0700303 POWER_SUPPLY_PROP_CAPACITY,
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400304 POWER_SUPPLY_PROP_MODEL_NAME,
305 POWER_SUPPLY_PROP_MANUFACTURER,
maximilian attems7c2670b2008-01-22 18:46:50 +0100306 POWER_SUPPLY_PROP_SERIAL_NUMBER,
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400307};
308
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +0300309#ifdef CONFIG_ACPI_PROCFS_POWER
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400310inline char *acpi_battery_units(struct acpi_battery *battery)
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400311{
Lan Tianyuae6f6182011-06-30 11:32:40 +0800312 return (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA) ?
313 "mA" : "mW";
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400314}
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400315#endif
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317/* --------------------------------------------------------------------------
318 Battery Management
319 -------------------------------------------------------------------------- */
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400320struct acpi_offsets {
321 size_t offset; /* offset inside struct acpi_sbs_battery */
322 u8 mode; /* int or string? */
323};
324
325static struct acpi_offsets state_offsets[] = {
326 {offsetof(struct acpi_battery, state), 0},
Alexey Starikovskiy7faa1442009-03-27 22:23:52 -0400327 {offsetof(struct acpi_battery, rate_now), 0},
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400328 {offsetof(struct acpi_battery, capacity_now), 0},
329 {offsetof(struct acpi_battery, voltage_now), 0},
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400330};
331
332static struct acpi_offsets info_offsets[] = {
333 {offsetof(struct acpi_battery, power_unit), 0},
334 {offsetof(struct acpi_battery, design_capacity), 0},
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400335 {offsetof(struct acpi_battery, full_charge_capacity), 0},
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400336 {offsetof(struct acpi_battery, technology), 0},
337 {offsetof(struct acpi_battery, design_voltage), 0},
338 {offsetof(struct acpi_battery, design_capacity_warning), 0},
339 {offsetof(struct acpi_battery, design_capacity_low), 0},
340 {offsetof(struct acpi_battery, capacity_granularity_1), 0},
341 {offsetof(struct acpi_battery, capacity_granularity_2), 0},
342 {offsetof(struct acpi_battery, model_number), 1},
343 {offsetof(struct acpi_battery, serial_number), 1},
344 {offsetof(struct acpi_battery, type), 1},
345 {offsetof(struct acpi_battery, oem_info), 1},
346};
347
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400348static struct acpi_offsets extended_info_offsets[] = {
349 {offsetof(struct acpi_battery, power_unit), 0},
350 {offsetof(struct acpi_battery, design_capacity), 0},
351 {offsetof(struct acpi_battery, full_charge_capacity), 0},
352 {offsetof(struct acpi_battery, technology), 0},
353 {offsetof(struct acpi_battery, design_voltage), 0},
354 {offsetof(struct acpi_battery, design_capacity_warning), 0},
355 {offsetof(struct acpi_battery, design_capacity_low), 0},
356 {offsetof(struct acpi_battery, cycle_count), 0},
357 {offsetof(struct acpi_battery, measurement_accuracy), 0},
358 {offsetof(struct acpi_battery, max_sampling_time), 0},
359 {offsetof(struct acpi_battery, min_sampling_time), 0},
360 {offsetof(struct acpi_battery, max_averaging_interval), 0},
361 {offsetof(struct acpi_battery, min_averaging_interval), 0},
362 {offsetof(struct acpi_battery, capacity_granularity_1), 0},
363 {offsetof(struct acpi_battery, capacity_granularity_2), 0},
364 {offsetof(struct acpi_battery, model_number), 1},
365 {offsetof(struct acpi_battery, serial_number), 1},
366 {offsetof(struct acpi_battery, type), 1},
367 {offsetof(struct acpi_battery, oem_info), 1},
368};
369
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400370static int extract_package(struct acpi_battery *battery,
371 union acpi_object *package,
372 struct acpi_offsets *offsets, int num)
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300373{
Alexey Starikovskiy106449e2007-10-29 23:29:40 +0300374 int i;
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400375 union acpi_object *element;
376 if (package->type != ACPI_TYPE_PACKAGE)
377 return -EFAULT;
378 for (i = 0; i < num; ++i) {
379 if (package->package.count <= i)
380 return -EFAULT;
381 element = &package->package.elements[i];
382 if (offsets[i].mode) {
Alexey Starikovskiy106449e2007-10-29 23:29:40 +0300383 u8 *ptr = (u8 *)battery + offsets[i].offset;
384 if (element->type == ACPI_TYPE_STRING ||
385 element->type == ACPI_TYPE_BUFFER)
386 strncpy(ptr, element->string.pointer, 32);
387 else if (element->type == ACPI_TYPE_INTEGER) {
388 strncpy(ptr, (u8 *)&element->integer.value,
Lin Ming439913f2010-01-28 10:53:19 +0800389 sizeof(u64));
390 ptr[sizeof(u64)] = 0;
Alexey Starikovskiyb8a1bdb2008-03-17 22:37:42 -0400391 } else
392 *ptr = 0; /* don't have value */
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400393 } else {
Alexey Starikovskiyb8a1bdb2008-03-17 22:37:42 -0400394 int *x = (int *)((u8 *)battery + offsets[i].offset);
395 *x = (element->type == ACPI_TYPE_INTEGER) ?
396 element->integer.value : -1;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300397 }
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300398 }
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300399 return 0;
400}
401
402static int acpi_battery_get_status(struct acpi_battery *battery)
403{
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400404 if (acpi_bus_get_status(battery->device)) {
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300405 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Evaluating _STA"));
406 return -ENODEV;
407 }
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400408 return 0;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300409}
410
411static int acpi_battery_get_info(struct acpi_battery *battery)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400413 int result = -EFAULT;
Len Brown4be44fc2005-08-05 00:44:28 -0400414 acpi_status status = 0;
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400415 char *name = test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags)?
416 "_BIX" : "_BIF";
417
Len Brown4be44fc2005-08-05 00:44:28 -0400418 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300420 if (!acpi_battery_present(battery))
421 return 0;
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400422 mutex_lock(&battery->lock);
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400423 status = acpi_evaluate_object(battery->device->handle, name,
424 NULL, &buffer);
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400425 mutex_unlock(&battery->lock);
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 if (ACPI_FAILURE(status)) {
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400428 ACPI_EXCEPTION((AE_INFO, status, "Evaluating %s", name));
Patrick Mocheld550d982006-06-27 00:41:40 -0400429 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 }
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400431 if (test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags))
432 result = extract_package(battery, buffer.pointer,
433 extended_info_offsets,
434 ARRAY_SIZE(extended_info_offsets));
435 else
436 result = extract_package(battery, buffer.pointer,
437 info_offsets, ARRAY_SIZE(info_offsets));
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400438 kfree(buffer.pointer);
Zhang Rui557d5862010-10-22 10:02:06 +0800439 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags))
440 battery->full_charge_capacity = battery->design_capacity;
Patrick Mocheld550d982006-06-27 00:41:40 -0400441 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442}
443
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300444static int acpi_battery_get_state(struct acpi_battery *battery)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
Len Brown4be44fc2005-08-05 00:44:28 -0400446 int result = 0;
447 acpi_status status = 0;
448 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300450 if (!acpi_battery_present(battery))
451 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400453 if (battery->update_time &&
454 time_before(jiffies, battery->update_time +
455 msecs_to_jiffies(cache_time)))
456 return 0;
457
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400458 mutex_lock(&battery->lock);
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400459 status = acpi_evaluate_object(battery->device->handle, "_BST",
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400460 NULL, &buffer);
461 mutex_unlock(&battery->lock);
Len Brown5b31d892007-08-15 00:19:26 -0400462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400464 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BST"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400465 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 }
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400467
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400468 result = extract_package(battery, buffer.pointer,
469 state_offsets, ARRAY_SIZE(state_offsets));
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400470 battery->update_time = jiffies;
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400471 kfree(buffer.pointer);
Hector Martinbc76f902009-08-06 15:57:48 -0700472
Lan Tianyu55003b22011-06-30 11:33:12 +0800473 /* For buggy DSDTs that report negative 16-bit values for either
474 * charging or discharging current and/or report 0 as 65536
475 * due to bad math.
476 */
477 if (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA &&
478 battery->rate_now != ACPI_BATTERY_VALUE_UNKNOWN &&
479 (s16)(battery->rate_now) < 0) {
Hector Martinbc76f902009-08-06 15:57:48 -0700480 battery->rate_now = abs((s16)battery->rate_now);
Lan Tianyu55003b22011-06-30 11:33:12 +0800481 printk_once(KERN_WARNING FW_BUG "battery: (dis)charge rate"
482 " invalid.\n");
483 }
Hector Martinbc76f902009-08-06 15:57:48 -0700484
Zhang Rui557d5862010-10-22 10:02:06 +0800485 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags)
486 && battery->capacity_now >= 0 && battery->capacity_now <= 100)
487 battery->capacity_now = (battery->capacity_now *
488 battery->full_charge_capacity) / 100;
Patrick Mocheld550d982006-06-27 00:41:40 -0400489 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
491
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400492static int acpi_battery_set_alarm(struct acpi_battery *battery)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
Len Brown4be44fc2005-08-05 00:44:28 -0400494 acpi_status status = 0;
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400495 union acpi_object arg0 = { .type = ACPI_TYPE_INTEGER };
Len Brown4be44fc2005-08-05 00:44:28 -0400496 struct acpi_object_list arg_list = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400498 if (!acpi_battery_present(battery) ||
Alexey Starikovskiy7b3bcc42009-10-15 14:31:24 +0400499 !test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags))
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300500 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400502 arg0.integer.value = battery->alarm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400504 mutex_lock(&battery->lock);
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400505 status = acpi_evaluate_object(battery->device->handle, "_BTP",
506 &arg_list, NULL);
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400507 mutex_unlock(&battery->lock);
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400510 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400512 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", battery->alarm));
Patrick Mocheld550d982006-06-27 00:41:40 -0400513 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514}
515
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300516static int acpi_battery_init_alarm(struct acpi_battery *battery)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
Len Brown4be44fc2005-08-05 00:44:28 -0400518 acpi_status status = AE_OK;
519 acpi_handle handle = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300521 /* See if alarms are supported, and if so, set default */
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400522 status = acpi_get_handle(battery->device->handle, "_BTP", &handle);
523 if (ACPI_FAILURE(status)) {
Alexey Starikovskiy7b3bcc42009-10-15 14:31:24 +0400524 clear_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400525 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 }
Alexey Starikovskiy7b3bcc42009-10-15 14:31:24 +0400527 set_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400528 if (!battery->alarm)
529 battery->alarm = battery->design_capacity_warning;
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400530 return acpi_battery_set_alarm(battery);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531}
532
Andrey Borzenkov508df922007-10-28 12:50:09 +0300533static ssize_t acpi_battery_alarm_show(struct device *dev,
534 struct device_attribute *attr,
535 char *buf)
536{
537 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
538 return sprintf(buf, "%d\n", battery->alarm * 1000);
539}
540
541static ssize_t acpi_battery_alarm_store(struct device *dev,
542 struct device_attribute *attr,
543 const char *buf, size_t count)
544{
545 unsigned long x;
546 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
547 if (sscanf(buf, "%ld\n", &x) == 1)
548 battery->alarm = x/1000;
549 if (acpi_battery_present(battery))
550 acpi_battery_set_alarm(battery);
551 return count;
552}
553
554static struct device_attribute alarm_attr = {
Parag Warudkar01e8ef12008-10-18 20:28:50 -0700555 .attr = {.name = "alarm", .mode = 0644},
Andrey Borzenkov508df922007-10-28 12:50:09 +0300556 .show = acpi_battery_alarm_show,
557 .store = acpi_battery_alarm_store,
558};
559
560static int sysfs_add_battery(struct acpi_battery *battery)
561{
562 int result;
563
Lan Tianyuae6f6182011-06-30 11:32:40 +0800564 if (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA) {
Andrey Borzenkov508df922007-10-28 12:50:09 +0300565 battery->bat.properties = charge_battery_props;
566 battery->bat.num_properties =
567 ARRAY_SIZE(charge_battery_props);
568 } else {
569 battery->bat.properties = energy_battery_props;
570 battery->bat.num_properties =
571 ARRAY_SIZE(energy_battery_props);
572 }
573
574 battery->bat.name = acpi_device_bid(battery->device);
575 battery->bat.type = POWER_SUPPLY_TYPE_BATTERY;
576 battery->bat.get_property = acpi_battery_get_property;
577
578 result = power_supply_register(&battery->device->dev, &battery->bat);
579 if (result)
580 return result;
581 return device_create_file(battery->bat.dev, &alarm_attr);
582}
583
584static void sysfs_remove_battery(struct acpi_battery *battery)
585{
Sergey Senozhatsky69d94ec2011-08-06 01:34:08 +0300586 mutex_lock(&battery->sysfs_lock);
Lan Tianyu9c921c222011-06-30 11:34:12 +0800587 if (!battery->bat.dev) {
Sergey Senozhatsky69d94ec2011-08-06 01:34:08 +0300588 mutex_unlock(&battery->sysfs_lock);
Andrey Borzenkov508df922007-10-28 12:50:09 +0300589 return;
Lan Tianyu9c921c222011-06-30 11:34:12 +0800590 }
591
Andrey Borzenkov508df922007-10-28 12:50:09 +0300592 device_remove_file(battery->bat.dev, &alarm_attr);
593 power_supply_unregister(&battery->bat);
Alexey Starikovskiy91044762007-11-13 12:23:06 +0300594 battery->bat.dev = NULL;
Sergey Senozhatsky69d94ec2011-08-06 01:34:08 +0300595 mutex_unlock(&battery->sysfs_lock);
Hector Martinbc76f902009-08-06 15:57:48 -0700596}
597
Zhang Rui557d5862010-10-22 10:02:06 +0800598/*
599 * According to the ACPI spec, some kinds of primary batteries can
600 * report percentage battery remaining capacity directly to OS.
601 * In this case, it reports the Last Full Charged Capacity == 100
602 * and BatteryPresentRate == 0xFFFFFFFF.
603 *
604 * Now we found some battery reports percentage remaining capacity
605 * even if it's rechargeable.
606 * https://bugzilla.kernel.org/show_bug.cgi?id=15979
607 *
608 * Handle this correctly so that they won't break userspace.
609 */
Lan Tianyu7b786222011-06-30 11:33:27 +0800610static void acpi_battery_quirks(struct acpi_battery *battery)
Zhang Rui557d5862010-10-22 10:02:06 +0800611{
612 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags))
613 return ;
614
615 if (battery->full_charge_capacity == 100 &&
616 battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN &&
617 battery->capacity_now >=0 && battery->capacity_now <= 100) {
618 set_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags);
619 battery->full_charge_capacity = battery->design_capacity;
620 battery->capacity_now = (battery->capacity_now *
621 battery->full_charge_capacity) / 100;
622 }
623}
624
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400625static int acpi_battery_update(struct acpi_battery *battery)
Vladimir Lebedev4bd35cd2007-02-10 01:43:48 -0500626{
Alexey Starikovskiy50b17852008-12-23 02:44:54 +0300627 int result, old_present = acpi_battery_present(battery);
Alexey Starikovskiy97749cd2008-01-01 14:27:24 -0500628 result = acpi_battery_get_status(battery);
Andrey Borzenkov508df922007-10-28 12:50:09 +0300629 if (result)
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300630 return result;
Andrey Borzenkov508df922007-10-28 12:50:09 +0300631 if (!acpi_battery_present(battery)) {
632 sysfs_remove_battery(battery);
Alexey Starikovskiy97749cd2008-01-01 14:27:24 -0500633 battery->update_time = 0;
Andrey Borzenkov508df922007-10-28 12:50:09 +0300634 return 0;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300635 }
Alexey Starikovskiy50b17852008-12-23 02:44:54 +0300636 if (!battery->update_time ||
637 old_present != acpi_battery_present(battery)) {
Alexey Starikovskiy97749cd2008-01-01 14:27:24 -0500638 result = acpi_battery_get_info(battery);
639 if (result)
640 return result;
641 acpi_battery_init_alarm(battery);
642 }
Stefan Hajnoczieb03cb02011-07-12 09:03:29 +0100643 if (!battery->bat.dev) {
644 result = sysfs_add_battery(battery);
645 if (result)
646 return result;
647 }
Zhang Rui557d5862010-10-22 10:02:06 +0800648 result = acpi_battery_get_state(battery);
Lan Tianyu7b786222011-06-30 11:33:27 +0800649 acpi_battery_quirks(battery);
Zhang Rui557d5862010-10-22 10:02:06 +0800650 return result;
Vladimir Lebedev4bd35cd2007-02-10 01:43:48 -0500651}
652
Rafael J. Wysockida8aeb92011-01-06 23:42:27 +0100653static void acpi_battery_refresh(struct acpi_battery *battery)
654{
Andy Whitcroftc5971452012-05-03 14:48:26 +0100655 int power_unit;
656
Rafael J. Wysockida8aeb92011-01-06 23:42:27 +0100657 if (!battery->bat.dev)
658 return;
659
Andy Whitcroftc5971452012-05-03 14:48:26 +0100660 power_unit = battery->power_unit;
661
Rafael J. Wysockida8aeb92011-01-06 23:42:27 +0100662 acpi_battery_get_info(battery);
Andy Whitcroftc5971452012-05-03 14:48:26 +0100663
664 if (power_unit == battery->power_unit)
665 return;
666
667 /* The battery has changed its reporting units. */
Rafael J. Wysockida8aeb92011-01-06 23:42:27 +0100668 sysfs_remove_battery(battery);
669 sysfs_add_battery(battery);
670}
671
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672/* --------------------------------------------------------------------------
673 FS Interface (/proc)
674 -------------------------------------------------------------------------- */
675
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +0300676#ifdef CONFIG_ACPI_PROCFS_POWER
Len Brown4be44fc2005-08-05 00:44:28 -0400677static struct proc_dir_entry *acpi_battery_dir;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300678
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400679static int acpi_battery_print_info(struct seq_file *seq, int result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200681 struct acpi_battery *battery = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300683 if (result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 goto end;
685
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400686 seq_printf(seq, "present: %s\n",
687 acpi_battery_present(battery)?"yes":"no");
688 if (!acpi_battery_present(battery))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 goto end;
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400690 if (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 seq_printf(seq, "design capacity: unknown\n");
692 else
693 seq_printf(seq, "design capacity: %d %sh\n",
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400694 battery->design_capacity,
695 acpi_battery_units(battery));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400697 if (battery->full_charge_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 seq_printf(seq, "last full capacity: unknown\n");
699 else
700 seq_printf(seq, "last full capacity: %d %sh\n",
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400701 battery->full_charge_capacity,
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400702 acpi_battery_units(battery));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400704 seq_printf(seq, "battery technology: %srechargeable\n",
705 (!battery->technology)?"non-":"");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400707 if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 seq_printf(seq, "design voltage: unknown\n");
709 else
710 seq_printf(seq, "design voltage: %d mV\n",
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400711 battery->design_voltage);
Len Brown4be44fc2005-08-05 00:44:28 -0400712 seq_printf(seq, "design capacity warning: %d %sh\n",
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400713 battery->design_capacity_warning,
714 acpi_battery_units(battery));
Len Brown4be44fc2005-08-05 00:44:28 -0400715 seq_printf(seq, "design capacity low: %d %sh\n",
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400716 battery->design_capacity_low,
717 acpi_battery_units(battery));
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400718 seq_printf(seq, "cycle count: %i\n", battery->cycle_count);
Len Brown4be44fc2005-08-05 00:44:28 -0400719 seq_printf(seq, "capacity granularity 1: %d %sh\n",
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400720 battery->capacity_granularity_1,
721 acpi_battery_units(battery));
Len Brown4be44fc2005-08-05 00:44:28 -0400722 seq_printf(seq, "capacity granularity 2: %d %sh\n",
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400723 battery->capacity_granularity_2,
724 acpi_battery_units(battery));
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400725 seq_printf(seq, "model number: %s\n", battery->model_number);
726 seq_printf(seq, "serial number: %s\n", battery->serial_number);
727 seq_printf(seq, "battery type: %s\n", battery->type);
728 seq_printf(seq, "OEM info: %s\n", battery->oem_info);
Len Brown4be44fc2005-08-05 00:44:28 -0400729 end:
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300730 if (result)
731 seq_printf(seq, "ERROR: Unable to read battery info\n");
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300732 return result;
733}
734
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400735static int acpi_battery_print_state(struct seq_file *seq, int result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200737 struct acpi_battery *battery = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300739 if (result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 goto end;
741
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400742 seq_printf(seq, "present: %s\n",
743 acpi_battery_present(battery)?"yes":"no");
744 if (!acpi_battery_present(battery))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400747 seq_printf(seq, "capacity state: %s\n",
748 (battery->state & 0x04)?"critical":"ok");
749 if ((battery->state & 0x01) && (battery->state & 0x02))
Len Brown4be44fc2005-08-05 00:44:28 -0400750 seq_printf(seq,
751 "charging state: charging/discharging\n");
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400752 else if (battery->state & 0x01)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 seq_printf(seq, "charging state: discharging\n");
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400754 else if (battery->state & 0x02)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 seq_printf(seq, "charging state: charging\n");
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400756 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 seq_printf(seq, "charging state: charged\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Alexey Starikovskiy7faa1442009-03-27 22:23:52 -0400759 if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 seq_printf(seq, "present rate: unknown\n");
761 else
762 seq_printf(seq, "present rate: %d %s\n",
Alexey Starikovskiy7faa1442009-03-27 22:23:52 -0400763 battery->rate_now, acpi_battery_units(battery));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400765 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 seq_printf(seq, "remaining capacity: unknown\n");
767 else
768 seq_printf(seq, "remaining capacity: %d %sh\n",
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400769 battery->capacity_now, acpi_battery_units(battery));
770 if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 seq_printf(seq, "present voltage: unknown\n");
772 else
773 seq_printf(seq, "present voltage: %d mV\n",
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400774 battery->voltage_now);
Len Brown4be44fc2005-08-05 00:44:28 -0400775 end:
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400776 if (result)
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300777 seq_printf(seq, "ERROR: Unable to read battery state\n");
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300778
779 return result;
780}
781
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400782static int acpi_battery_print_alarm(struct seq_file *seq, int result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200784 struct acpi_battery *battery = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300786 if (result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 goto end;
788
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300789 if (!acpi_battery_present(battery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 seq_printf(seq, "present: no\n");
791 goto end;
792 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 seq_printf(seq, "alarm: ");
794 if (!battery->alarm)
795 seq_printf(seq, "unsupported\n");
796 else
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400797 seq_printf(seq, "%u %sh\n", battery->alarm,
798 acpi_battery_units(battery));
Len Brown4be44fc2005-08-05 00:44:28 -0400799 end:
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300800 if (result)
801 seq_printf(seq, "ERROR: Unable to read battery alarm\n");
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300802 return result;
803}
804
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400805static ssize_t acpi_battery_write_alarm(struct file *file,
806 const char __user * buffer,
807 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
Len Brown4be44fc2005-08-05 00:44:28 -0400809 int result = 0;
810 char alarm_string[12] = { '\0' };
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200811 struct seq_file *m = file->private_data;
812 struct acpi_battery *battery = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 if (!battery || (count > sizeof(alarm_string) - 1))
Patrick Mocheld550d982006-06-27 00:41:40 -0400815 return -EINVAL;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300816 if (!acpi_battery_present(battery)) {
817 result = -ENODEV;
818 goto end;
819 }
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300820 if (copy_from_user(alarm_string, buffer, count)) {
821 result = -EFAULT;
822 goto end;
823 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 alarm_string[count] = '\0';
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400825 battery->alarm = simple_strtol(alarm_string, NULL, 0);
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400826 result = acpi_battery_set_alarm(battery);
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300827 end:
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300828 if (!result)
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400829 return count;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300830 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831}
832
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400833typedef int(*print_func)(struct seq_file *seq, int result);
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400834
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400835static print_func acpi_print_funcs[ACPI_BATTERY_NUMFILES] = {
836 acpi_battery_print_info,
837 acpi_battery_print_state,
838 acpi_battery_print_alarm,
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400839};
840
841static int acpi_battery_read(int fid, struct seq_file *seq)
842{
843 struct acpi_battery *battery = seq->private;
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400844 int result = acpi_battery_update(battery);
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400845 return acpi_print_funcs[fid](seq, result);
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400846}
847
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400848#define DECLARE_FILE_FUNCTIONS(_name) \
849static int acpi_battery_read_##_name(struct seq_file *seq, void *offset) \
850{ \
851 return acpi_battery_read(_name##_tag, seq); \
852} \
853static int acpi_battery_##_name##_open_fs(struct inode *inode, struct file *file) \
854{ \
855 return single_open(file, acpi_battery_read_##_name, PDE(inode)->data); \
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400856}
857
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400858DECLARE_FILE_FUNCTIONS(info);
859DECLARE_FILE_FUNCTIONS(state);
860DECLARE_FILE_FUNCTIONS(alarm);
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400861
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400862#undef DECLARE_FILE_FUNCTIONS
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400863
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400864#define FILE_DESCRIPTION_RO(_name) \
865 { \
866 .name = __stringify(_name), \
867 .mode = S_IRUGO, \
868 .ops = { \
869 .open = acpi_battery_##_name##_open_fs, \
870 .read = seq_read, \
871 .llseek = seq_lseek, \
872 .release = single_release, \
873 .owner = THIS_MODULE, \
874 }, \
875 }
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400876
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400877#define FILE_DESCRIPTION_RW(_name) \
878 { \
879 .name = __stringify(_name), \
880 .mode = S_IFREG | S_IRUGO | S_IWUSR, \
881 .ops = { \
882 .open = acpi_battery_##_name##_open_fs, \
883 .read = seq_read, \
884 .llseek = seq_lseek, \
885 .write = acpi_battery_write_##_name, \
886 .release = single_release, \
887 .owner = THIS_MODULE, \
888 }, \
889 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Vasiliy Kulikov9c8b04b2011-06-25 21:07:52 +0400891static const struct battery_file {
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400892 struct file_operations ops;
Al Virod161a132011-07-24 03:36:29 -0400893 umode_t mode;
Jan Engelhardt070d8eb2009-01-12 00:07:55 +0100894 const char *name;
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400895} acpi_battery_file[] = {
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400896 FILE_DESCRIPTION_RO(info),
897 FILE_DESCRIPTION_RO(state),
898 FILE_DESCRIPTION_RW(alarm),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899};
900
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400901#undef FILE_DESCRIPTION_RO
902#undef FILE_DESCRIPTION_RW
903
Len Brown4be44fc2005-08-05 00:44:28 -0400904static int acpi_battery_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
Len Brown4be44fc2005-08-05 00:44:28 -0400906 struct proc_dir_entry *entry = NULL;
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400907 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Zhang Rui6d855fc2011-01-10 11:16:30 +0800909 printk(KERN_WARNING PREFIX "Deprecated procfs I/F for battery is loaded,"
910 " please retry with CONFIG_ACPI_PROCFS_POWER cleared\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 if (!acpi_device_dir(device)) {
912 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown4be44fc2005-08-05 00:44:28 -0400913 acpi_battery_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400915 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 }
917
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400918 for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i) {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700919 entry = proc_create_data(acpi_battery_file[i].name,
920 acpi_battery_file[i].mode,
921 acpi_device_dir(device),
922 &acpi_battery_file[i].ops,
923 acpi_driver_data(device));
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400924 if (!entry)
925 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 }
Patrick Mocheld550d982006-06-27 00:41:40 -0400927 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928}
929
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400930static void acpi_battery_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931{
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400932 int i;
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400933 if (!acpi_device_dir(device))
934 return;
935 for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i)
936 remove_proc_entry(acpi_battery_file[i].name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400939 remove_proc_entry(acpi_device_bid(device), acpi_battery_dir);
940 acpi_device_dir(device) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941}
942
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400943#endif
Alexey Starikovskiy3e58ea02007-09-26 19:43:11 +0400944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945/* --------------------------------------------------------------------------
946 Driver Interface
947 -------------------------------------------------------------------------- */
948
Bjorn Helgaasd9406692009-04-30 09:35:47 -0600949static void acpi_battery_notify(struct acpi_device *device, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950{
Bjorn Helgaasd9406692009-04-30 09:35:47 -0600951 struct acpi_battery *battery = acpi_driver_data(device);
Zhang Rui153e5002010-07-07 09:11:57 +0800952 struct device *old;
Bjorn Helgaasd9406692009-04-30 09:35:47 -0600953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 if (!battery)
Patrick Mocheld550d982006-06-27 00:41:40 -0400955 return;
Zhang Rui153e5002010-07-07 09:11:57 +0800956 old = battery->bat.dev;
Rafael J. Wysockida8aeb92011-01-06 23:42:27 +0100957 if (event == ACPI_BATTERY_NOTIFY_INFO)
958 acpi_battery_refresh(battery);
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400959 acpi_battery_update(battery);
960 acpi_bus_generate_proc_event(device, event,
961 acpi_battery_present(battery));
962 acpi_bus_generate_netlink_event(device->pnp.device_class,
Kay Sievers07944692008-10-30 01:18:59 +0100963 dev_name(&device->dev), event,
Vladimir Lebedev9ea7d572007-02-20 15:48:06 +0300964 acpi_battery_present(battery));
Justin P. Mattock2345baf2009-12-13 14:42:36 -0800965 /* acpi_battery_update could remove power_supply object */
Zhang Rui153e5002010-07-07 09:11:57 +0800966 if (old && battery->bat.dev)
Alan Jenkinsf79e1ce2009-06-30 14:36:16 +0000967 power_supply_changed(&battery->bat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968}
969
Kyle McMartin25be5822011-03-22 16:19:50 -0400970static int battery_notify(struct notifier_block *nb,
971 unsigned long mode, void *_unused)
972{
973 struct acpi_battery *battery = container_of(nb, struct acpi_battery,
974 pm_nb);
975 switch (mode) {
Lan Tianyud5a59112011-06-30 11:33:40 +0800976 case PM_POST_HIBERNATION:
Kyle McMartin25be5822011-03-22 16:19:50 -0400977 case PM_POST_SUSPEND:
Lan Tianyu6e17fb62011-06-30 11:33:58 +0800978 if (battery->bat.dev) {
979 sysfs_remove_battery(battery);
980 sysfs_add_battery(battery);
981 }
Kyle McMartin25be5822011-03-22 16:19:50 -0400982 break;
983 }
984
985 return 0;
986}
987
Len Brown4be44fc2005-08-05 00:44:28 -0400988static int acpi_battery_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989{
Len Brown4be44fc2005-08-05 00:44:28 -0400990 int result = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400991 struct acpi_battery *battery = NULL;
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400992 acpi_handle handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400994 return -EINVAL;
Burman Yan36bcbec2006-12-19 12:56:11 -0800995 battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 if (!battery)
Patrick Mocheld550d982006-06-27 00:41:40 -0400997 return -ENOMEM;
Patrick Mochel145def82006-05-19 16:54:39 -0400998 battery->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME);
1000 strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07001001 device->driver_data = battery;
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +04001002 mutex_init(&battery->lock);
Sergey Senozhatsky69d94ec2011-08-06 01:34:08 +03001003 mutex_init(&battery->sysfs_lock);
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +04001004 if (ACPI_SUCCESS(acpi_get_handle(battery->device->handle,
1005 "_BIX", &handle)))
1006 set_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags);
Stefan Hajnoczieb03cb02011-07-12 09:03:29 +01001007 result = acpi_battery_update(battery);
1008 if (result)
1009 goto fail;
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +03001010#ifdef CONFIG_ACPI_PROCFS_POWER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 result = acpi_battery_add_fs(device);
Alexey Starikovskiyd7380962007-09-26 19:43:04 +04001012#endif
Stefan Hajnoczie80bba42011-07-12 09:03:28 +01001013 if (result) {
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +03001014#ifdef CONFIG_ACPI_PROCFS_POWER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 acpi_battery_remove_fs(device);
Alexey Starikovskiyd7380962007-09-26 19:43:04 +04001016#endif
Stefan Hajnoczie80bba42011-07-12 09:03:28 +01001017 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 }
Kyle McMartin25be5822011-03-22 16:19:50 -04001019
Stefan Hajnoczie80bba42011-07-12 09:03:28 +01001020 printk(KERN_INFO PREFIX "%s Slot [%s] (battery %s)\n",
1021 ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device),
1022 device->status.battery_present ? "present" : "absent");
1023
Kyle McMartin25be5822011-03-22 16:19:50 -04001024 battery->pm_nb.notifier_call = battery_notify;
1025 register_pm_notifier(&battery->pm_nb);
1026
Patrick Mocheld550d982006-06-27 00:41:40 -04001027 return result;
Stefan Hajnoczie80bba42011-07-12 09:03:28 +01001028
1029fail:
1030 sysfs_remove_battery(battery);
1031 mutex_destroy(&battery->lock);
Sergey Senozhatsky69d94ec2011-08-06 01:34:08 +03001032 mutex_destroy(&battery->sysfs_lock);
Stefan Hajnoczie80bba42011-07-12 09:03:28 +01001033 kfree(battery);
1034 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035}
1036
Len Brown4be44fc2005-08-05 00:44:28 -04001037static int acpi_battery_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038{
Len Brown4be44fc2005-08-05 00:44:28 -04001039 struct acpi_battery *battery = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001042 return -EINVAL;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001043 battery = acpi_driver_data(device);
Kyle McMartin25be5822011-03-22 16:19:50 -04001044 unregister_pm_notifier(&battery->pm_nb);
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +03001045#ifdef CONFIG_ACPI_PROCFS_POWER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 acpi_battery_remove_fs(device);
Alexey Starikovskiyd7380962007-09-26 19:43:04 +04001047#endif
Andrey Borzenkov508df922007-10-28 12:50:09 +03001048 sysfs_remove_battery(battery);
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +04001049 mutex_destroy(&battery->lock);
Sergey Senozhatsky69d94ec2011-08-06 01:34:08 +03001050 mutex_destroy(&battery->sysfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 kfree(battery);
Patrick Mocheld550d982006-06-27 00:41:40 -04001052 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053}
1054
Rafael J. Wysocki90692402012-08-09 23:00:02 +02001055#ifdef CONFIG_PM_SLEEP
Jiri Kosina34c44152006-10-10 14:20:41 -07001056/* this is needed to learn about changes made in suspended state */
Rafael J. Wysockia6f50dc2012-06-27 23:26:43 +02001057static int acpi_battery_resume(struct device *dev)
Jiri Kosina34c44152006-10-10 14:20:41 -07001058{
1059 struct acpi_battery *battery;
Rafael J. Wysockia6f50dc2012-06-27 23:26:43 +02001060
1061 if (!dev)
Jiri Kosina34c44152006-10-10 14:20:41 -07001062 return -EINVAL;
Rafael J. Wysockia6f50dc2012-06-27 23:26:43 +02001063
1064 battery = acpi_driver_data(to_acpi_device(dev));
1065 if (!battery)
1066 return -EINVAL;
1067
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +04001068 battery->update_time = 0;
Andrey Borzenkov508df922007-10-28 12:50:09 +03001069 acpi_battery_update(battery);
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +03001070 return 0;
Jiri Kosina34c44152006-10-10 14:20:41 -07001071}
Rafael J. Wysocki90692402012-08-09 23:00:02 +02001072#endif
Jiri Kosina34c44152006-10-10 14:20:41 -07001073
Rafael J. Wysockia6f50dc2012-06-27 23:26:43 +02001074static SIMPLE_DEV_PM_OPS(acpi_battery_pm, NULL, acpi_battery_resume);
1075
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +04001076static struct acpi_driver acpi_battery_driver = {
1077 .name = "battery",
1078 .class = ACPI_BATTERY_CLASS,
1079 .ids = battery_device_ids,
Bjorn Helgaasd9406692009-04-30 09:35:47 -06001080 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +04001081 .ops = {
1082 .add = acpi_battery_add,
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +04001083 .remove = acpi_battery_remove,
Bjorn Helgaasd9406692009-04-30 09:35:47 -06001084 .notify = acpi_battery_notify,
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +04001085 },
Rafael J. Wysockia6f50dc2012-06-27 23:26:43 +02001086 .drv.pm = &acpi_battery_pm,
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +04001087};
1088
Linus Torvaldsb0cbc862009-04-11 12:45:20 -07001089static void __init acpi_battery_init_async(void *unused, async_cookie_t cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
Pavel Machek4d8316d2006-08-14 22:37:22 -07001091 if (acpi_disabled)
Arjan van de Ven0f66af52009-01-10 14:19:05 -05001092 return;
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +03001093#ifdef CONFIG_ACPI_PROCFS_POWER
Rich Townsend3f86b832006-07-01 11:36:54 -04001094 acpi_battery_dir = acpi_lock_battery_dir();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 if (!acpi_battery_dir)
Arjan van de Ven0f66af52009-01-10 14:19:05 -05001096 return;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +04001097#endif
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +04001098 if (acpi_bus_register_driver(&acpi_battery_driver) < 0) {
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +03001099#ifdef CONFIG_ACPI_PROCFS_POWER
Rich Townsend3f86b832006-07-01 11:36:54 -04001100 acpi_unlock_battery_dir(acpi_battery_dir);
Alexey Starikovskiyd7380962007-09-26 19:43:04 +04001101#endif
Arjan van de Ven0f66af52009-01-10 14:19:05 -05001102 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 }
Arjan van de Ven0f66af52009-01-10 14:19:05 -05001104 return;
1105}
1106
1107static int __init acpi_battery_init(void)
1108{
1109 async_schedule(acpi_battery_init_async, NULL);
Patrick Mocheld550d982006-06-27 00:41:40 -04001110 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111}
1112
Len Brown4be44fc2005-08-05 00:44:28 -04001113static void __exit acpi_battery_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 acpi_bus_unregister_driver(&acpi_battery_driver);
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +03001116#ifdef CONFIG_ACPI_PROCFS_POWER
Rich Townsend3f86b832006-07-01 11:36:54 -04001117 acpi_unlock_battery_dir(acpi_battery_dir);
Alexey Starikovskiyd7380962007-09-26 19:43:04 +04001118#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119}
1120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121module_init(acpi_battery_init);
1122module_exit(acpi_battery_exit);