blob: 4ba339d0ea1920ef9bf4d16dc8d190cc27c3dda6 [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;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400102 struct power_supply bat;
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400103 struct acpi_device *device;
Kyle McMartin25be5822011-03-22 16:19:50 -0400104 struct notifier_block pm_nb;
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400105 unsigned long update_time;
Alexey Starikovskiy7faa1442009-03-27 22:23:52 -0400106 int rate_now;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400107 int capacity_now;
108 int voltage_now;
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400109 int design_capacity;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400110 int full_charge_capacity;
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400111 int technology;
112 int design_voltage;
113 int design_capacity_warning;
114 int design_capacity_low;
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400115 int cycle_count;
116 int measurement_accuracy;
117 int max_sampling_time;
118 int min_sampling_time;
119 int max_averaging_interval;
120 int min_averaging_interval;
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400121 int capacity_granularity_1;
122 int capacity_granularity_2;
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400123 int alarm;
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400124 char model_number[32];
125 char serial_number[32];
126 char type[32];
127 char oem_info[32];
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400128 int state;
129 int power_unit;
Alexey Starikovskiy7b3bcc42009-10-15 14:31:24 +0400130 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131};
132
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400133#define to_acpi_battery(x) container_of(x, struct acpi_battery, bat);
134
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400135inline int acpi_battery_present(struct acpi_battery *battery)
136{
137 return battery->device->status.battery_present;
138}
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400139
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400140static int acpi_battery_technology(struct acpi_battery *battery)
141{
142 if (!strcasecmp("NiCd", battery->type))
143 return POWER_SUPPLY_TECHNOLOGY_NiCd;
144 if (!strcasecmp("NiMH", battery->type))
145 return POWER_SUPPLY_TECHNOLOGY_NiMH;
146 if (!strcasecmp("LION", battery->type))
147 return POWER_SUPPLY_TECHNOLOGY_LION;
Andrey Borzenkovad40e682007-11-10 20:02:49 +0300148 if (!strncasecmp("LI-ION", battery->type, 6))
Alexey Starikovskiy0bde7ee2007-10-28 15:33:10 +0300149 return POWER_SUPPLY_TECHNOLOGY_LION;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400150 if (!strcasecmp("LiP", battery->type))
151 return POWER_SUPPLY_TECHNOLOGY_LIPO;
152 return POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
153}
154
Alexey Starikovskiy91044762007-11-13 12:23:06 +0300155static int acpi_battery_get_state(struct acpi_battery *battery);
Alexey Starikovskiyb19073a2007-10-25 17:10:47 -0400156
Richard Hughes56f382a2009-01-25 15:05:50 +0000157static int acpi_battery_is_charged(struct acpi_battery *battery)
158{
159 /* either charging or discharging */
160 if (battery->state != 0)
161 return 0;
162
163 /* battery not reporting charge */
164 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN ||
165 battery->capacity_now == 0)
166 return 0;
167
168 /* good batteries update full_charge as the batteries degrade */
169 if (battery->full_charge_capacity == battery->capacity_now)
170 return 1;
171
172 /* fallback to using design values for broken batteries */
173 if (battery->design_capacity == battery->capacity_now)
174 return 1;
175
176 /* we don't do any sort of metric based on percentages */
177 return 0;
178}
179
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400180static int acpi_battery_get_property(struct power_supply *psy,
181 enum power_supply_property psp,
182 union power_supply_propval *val)
183{
Rafael J. Wysockia1b4bd62010-10-23 19:35:15 +0200184 int ret = 0;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400185 struct acpi_battery *battery = to_acpi_battery(psy);
186
Alexey Starikovskiy91044762007-11-13 12:23:06 +0300187 if (acpi_battery_present(battery)) {
188 /* run battery update only if it is present */
189 acpi_battery_get_state(battery);
190 } else if (psp != POWER_SUPPLY_PROP_PRESENT)
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400191 return -ENODEV;
192 switch (psp) {
193 case POWER_SUPPLY_PROP_STATUS:
194 if (battery->state & 0x01)
195 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
196 else if (battery->state & 0x02)
197 val->intval = POWER_SUPPLY_STATUS_CHARGING;
Richard Hughes56f382a2009-01-25 15:05:50 +0000198 else if (acpi_battery_is_charged(battery))
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400199 val->intval = POWER_SUPPLY_STATUS_FULL;
Roland Dreier4c41d3a2007-11-07 15:09:09 -0800200 else
201 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400202 break;
203 case POWER_SUPPLY_PROP_PRESENT:
204 val->intval = acpi_battery_present(battery);
205 break;
206 case POWER_SUPPLY_PROP_TECHNOLOGY:
207 val->intval = acpi_battery_technology(battery);
208 break;
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400209 case POWER_SUPPLY_PROP_CYCLE_COUNT:
210 val->intval = battery->cycle_count;
211 break;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400212 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
Rafael J. Wysockia1b4bd62010-10-23 19:35:15 +0200213 if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
214 ret = -ENODEV;
215 else
216 val->intval = battery->design_voltage * 1000;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400217 break;
218 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
Rafael J. Wysockia1b4bd62010-10-23 19:35:15 +0200219 if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN)
220 ret = -ENODEV;
221 else
222 val->intval = battery->voltage_now * 1000;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400223 break;
224 case POWER_SUPPLY_PROP_CURRENT_NOW:
Alexey Starikovskiy7faa1442009-03-27 22:23:52 -0400225 case POWER_SUPPLY_PROP_POWER_NOW:
Rafael J. Wysockia1b4bd62010-10-23 19:35:15 +0200226 if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN)
227 ret = -ENODEV;
228 else
229 val->intval = battery->rate_now * 1000;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400230 break;
231 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
232 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
Rafael J. Wysockia1b4bd62010-10-23 19:35:15 +0200233 if (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
234 ret = -ENODEV;
235 else
236 val->intval = battery->design_capacity * 1000;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400237 break;
238 case POWER_SUPPLY_PROP_CHARGE_FULL:
239 case POWER_SUPPLY_PROP_ENERGY_FULL:
Rafael J. Wysockia1b4bd62010-10-23 19:35:15 +0200240 if (battery->full_charge_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
241 ret = -ENODEV;
242 else
243 val->intval = battery->full_charge_capacity * 1000;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400244 break;
245 case POWER_SUPPLY_PROP_CHARGE_NOW:
246 case POWER_SUPPLY_PROP_ENERGY_NOW:
Rafael J. Wysockia1b4bd62010-10-23 19:35:15 +0200247 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN)
248 ret = -ENODEV;
249 else
250 val->intval = battery->capacity_now * 1000;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400251 break;
252 case POWER_SUPPLY_PROP_MODEL_NAME:
253 val->strval = battery->model_number;
254 break;
255 case POWER_SUPPLY_PROP_MANUFACTURER:
256 val->strval = battery->oem_info;
257 break;
maximilian attems7c2670b2008-01-22 18:46:50 +0100258 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
259 val->strval = battery->serial_number;
260 break;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400261 default:
Rafael J. Wysockia1b4bd62010-10-23 19:35:15 +0200262 ret = -EINVAL;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400263 }
Rafael J. Wysockia1b4bd62010-10-23 19:35:15 +0200264 return ret;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400265}
266
267static enum power_supply_property charge_battery_props[] = {
268 POWER_SUPPLY_PROP_STATUS,
269 POWER_SUPPLY_PROP_PRESENT,
270 POWER_SUPPLY_PROP_TECHNOLOGY,
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400271 POWER_SUPPLY_PROP_CYCLE_COUNT,
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400272 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
273 POWER_SUPPLY_PROP_VOLTAGE_NOW,
274 POWER_SUPPLY_PROP_CURRENT_NOW,
275 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
276 POWER_SUPPLY_PROP_CHARGE_FULL,
277 POWER_SUPPLY_PROP_CHARGE_NOW,
278 POWER_SUPPLY_PROP_MODEL_NAME,
279 POWER_SUPPLY_PROP_MANUFACTURER,
maximilian attems7c2670b2008-01-22 18:46:50 +0100280 POWER_SUPPLY_PROP_SERIAL_NUMBER,
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400281};
282
283static enum power_supply_property energy_battery_props[] = {
284 POWER_SUPPLY_PROP_STATUS,
285 POWER_SUPPLY_PROP_PRESENT,
286 POWER_SUPPLY_PROP_TECHNOLOGY,
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400287 POWER_SUPPLY_PROP_CYCLE_COUNT,
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400288 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
289 POWER_SUPPLY_PROP_VOLTAGE_NOW,
Alexey Starikovskiy7faa1442009-03-27 22:23:52 -0400290 POWER_SUPPLY_PROP_POWER_NOW,
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400291 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
292 POWER_SUPPLY_PROP_ENERGY_FULL,
293 POWER_SUPPLY_PROP_ENERGY_NOW,
294 POWER_SUPPLY_PROP_MODEL_NAME,
295 POWER_SUPPLY_PROP_MANUFACTURER,
maximilian attems7c2670b2008-01-22 18:46:50 +0100296 POWER_SUPPLY_PROP_SERIAL_NUMBER,
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400297};
298
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +0300299#ifdef CONFIG_ACPI_PROCFS_POWER
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400300inline char *acpi_battery_units(struct acpi_battery *battery)
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400301{
Lan Tianyuae6f6182011-06-30 11:32:40 +0800302 return (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA) ?
303 "mA" : "mW";
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400304}
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400305#endif
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307/* --------------------------------------------------------------------------
308 Battery Management
309 -------------------------------------------------------------------------- */
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400310struct acpi_offsets {
311 size_t offset; /* offset inside struct acpi_sbs_battery */
312 u8 mode; /* int or string? */
313};
314
315static struct acpi_offsets state_offsets[] = {
316 {offsetof(struct acpi_battery, state), 0},
Alexey Starikovskiy7faa1442009-03-27 22:23:52 -0400317 {offsetof(struct acpi_battery, rate_now), 0},
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400318 {offsetof(struct acpi_battery, capacity_now), 0},
319 {offsetof(struct acpi_battery, voltage_now), 0},
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400320};
321
322static struct acpi_offsets info_offsets[] = {
323 {offsetof(struct acpi_battery, power_unit), 0},
324 {offsetof(struct acpi_battery, design_capacity), 0},
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400325 {offsetof(struct acpi_battery, full_charge_capacity), 0},
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400326 {offsetof(struct acpi_battery, technology), 0},
327 {offsetof(struct acpi_battery, design_voltage), 0},
328 {offsetof(struct acpi_battery, design_capacity_warning), 0},
329 {offsetof(struct acpi_battery, design_capacity_low), 0},
330 {offsetof(struct acpi_battery, capacity_granularity_1), 0},
331 {offsetof(struct acpi_battery, capacity_granularity_2), 0},
332 {offsetof(struct acpi_battery, model_number), 1},
333 {offsetof(struct acpi_battery, serial_number), 1},
334 {offsetof(struct acpi_battery, type), 1},
335 {offsetof(struct acpi_battery, oem_info), 1},
336};
337
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400338static struct acpi_offsets extended_info_offsets[] = {
339 {offsetof(struct acpi_battery, power_unit), 0},
340 {offsetof(struct acpi_battery, design_capacity), 0},
341 {offsetof(struct acpi_battery, full_charge_capacity), 0},
342 {offsetof(struct acpi_battery, technology), 0},
343 {offsetof(struct acpi_battery, design_voltage), 0},
344 {offsetof(struct acpi_battery, design_capacity_warning), 0},
345 {offsetof(struct acpi_battery, design_capacity_low), 0},
346 {offsetof(struct acpi_battery, cycle_count), 0},
347 {offsetof(struct acpi_battery, measurement_accuracy), 0},
348 {offsetof(struct acpi_battery, max_sampling_time), 0},
349 {offsetof(struct acpi_battery, min_sampling_time), 0},
350 {offsetof(struct acpi_battery, max_averaging_interval), 0},
351 {offsetof(struct acpi_battery, min_averaging_interval), 0},
352 {offsetof(struct acpi_battery, capacity_granularity_1), 0},
353 {offsetof(struct acpi_battery, capacity_granularity_2), 0},
354 {offsetof(struct acpi_battery, model_number), 1},
355 {offsetof(struct acpi_battery, serial_number), 1},
356 {offsetof(struct acpi_battery, type), 1},
357 {offsetof(struct acpi_battery, oem_info), 1},
358};
359
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400360static int extract_package(struct acpi_battery *battery,
361 union acpi_object *package,
362 struct acpi_offsets *offsets, int num)
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300363{
Alexey Starikovskiy106449e2007-10-29 23:29:40 +0300364 int i;
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400365 union acpi_object *element;
366 if (package->type != ACPI_TYPE_PACKAGE)
367 return -EFAULT;
368 for (i = 0; i < num; ++i) {
369 if (package->package.count <= i)
370 return -EFAULT;
371 element = &package->package.elements[i];
372 if (offsets[i].mode) {
Alexey Starikovskiy106449e2007-10-29 23:29:40 +0300373 u8 *ptr = (u8 *)battery + offsets[i].offset;
374 if (element->type == ACPI_TYPE_STRING ||
375 element->type == ACPI_TYPE_BUFFER)
376 strncpy(ptr, element->string.pointer, 32);
377 else if (element->type == ACPI_TYPE_INTEGER) {
378 strncpy(ptr, (u8 *)&element->integer.value,
Lin Ming439913f2010-01-28 10:53:19 +0800379 sizeof(u64));
380 ptr[sizeof(u64)] = 0;
Alexey Starikovskiyb8a1bdb2008-03-17 22:37:42 -0400381 } else
382 *ptr = 0; /* don't have value */
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400383 } else {
Alexey Starikovskiyb8a1bdb2008-03-17 22:37:42 -0400384 int *x = (int *)((u8 *)battery + offsets[i].offset);
385 *x = (element->type == ACPI_TYPE_INTEGER) ?
386 element->integer.value : -1;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300387 }
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300388 }
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300389 return 0;
390}
391
392static int acpi_battery_get_status(struct acpi_battery *battery)
393{
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400394 if (acpi_bus_get_status(battery->device)) {
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300395 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Evaluating _STA"));
396 return -ENODEV;
397 }
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400398 return 0;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300399}
400
401static int acpi_battery_get_info(struct acpi_battery *battery)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400403 int result = -EFAULT;
Len Brown4be44fc2005-08-05 00:44:28 -0400404 acpi_status status = 0;
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400405 char *name = test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags)?
406 "_BIX" : "_BIF";
407
Len Brown4be44fc2005-08-05 00:44:28 -0400408 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300410 if (!acpi_battery_present(battery))
411 return 0;
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400412 mutex_lock(&battery->lock);
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400413 status = acpi_evaluate_object(battery->device->handle, name,
414 NULL, &buffer);
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400415 mutex_unlock(&battery->lock);
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 if (ACPI_FAILURE(status)) {
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400418 ACPI_EXCEPTION((AE_INFO, status, "Evaluating %s", name));
Patrick Mocheld550d982006-06-27 00:41:40 -0400419 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 }
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400421 if (test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags))
422 result = extract_package(battery, buffer.pointer,
423 extended_info_offsets,
424 ARRAY_SIZE(extended_info_offsets));
425 else
426 result = extract_package(battery, buffer.pointer,
427 info_offsets, ARRAY_SIZE(info_offsets));
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400428 kfree(buffer.pointer);
Zhang Rui557d5862010-10-22 10:02:06 +0800429 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags))
430 battery->full_charge_capacity = battery->design_capacity;
Patrick Mocheld550d982006-06-27 00:41:40 -0400431 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}
433
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300434static int acpi_battery_get_state(struct acpi_battery *battery)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
Len Brown4be44fc2005-08-05 00:44:28 -0400436 int result = 0;
437 acpi_status status = 0;
438 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300440 if (!acpi_battery_present(battery))
441 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400443 if (battery->update_time &&
444 time_before(jiffies, battery->update_time +
445 msecs_to_jiffies(cache_time)))
446 return 0;
447
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400448 mutex_lock(&battery->lock);
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400449 status = acpi_evaluate_object(battery->device->handle, "_BST",
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400450 NULL, &buffer);
451 mutex_unlock(&battery->lock);
Len Brown5b31d892007-08-15 00:19:26 -0400452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400454 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BST"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400455 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 }
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400457
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400458 result = extract_package(battery, buffer.pointer,
459 state_offsets, ARRAY_SIZE(state_offsets));
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400460 battery->update_time = jiffies;
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400461 kfree(buffer.pointer);
Hector Martinbc76f902009-08-06 15:57:48 -0700462
Lan Tianyu55003b22011-06-30 11:33:12 +0800463 /* For buggy DSDTs that report negative 16-bit values for either
464 * charging or discharging current and/or report 0 as 65536
465 * due to bad math.
466 */
467 if (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA &&
468 battery->rate_now != ACPI_BATTERY_VALUE_UNKNOWN &&
469 (s16)(battery->rate_now) < 0) {
Hector Martinbc76f902009-08-06 15:57:48 -0700470 battery->rate_now = abs((s16)battery->rate_now);
Lan Tianyu55003b22011-06-30 11:33:12 +0800471 printk_once(KERN_WARNING FW_BUG "battery: (dis)charge rate"
472 " invalid.\n");
473 }
Hector Martinbc76f902009-08-06 15:57:48 -0700474
Zhang Rui557d5862010-10-22 10:02:06 +0800475 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags)
476 && battery->capacity_now >= 0 && battery->capacity_now <= 100)
477 battery->capacity_now = (battery->capacity_now *
478 battery->full_charge_capacity) / 100;
Patrick Mocheld550d982006-06-27 00:41:40 -0400479 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480}
481
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400482static int acpi_battery_set_alarm(struct acpi_battery *battery)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
Len Brown4be44fc2005-08-05 00:44:28 -0400484 acpi_status status = 0;
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400485 union acpi_object arg0 = { .type = ACPI_TYPE_INTEGER };
Len Brown4be44fc2005-08-05 00:44:28 -0400486 struct acpi_object_list arg_list = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400488 if (!acpi_battery_present(battery) ||
Alexey Starikovskiy7b3bcc42009-10-15 14:31:24 +0400489 !test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags))
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300490 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400492 arg0.integer.value = battery->alarm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400494 mutex_lock(&battery->lock);
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400495 status = acpi_evaluate_object(battery->device->handle, "_BTP",
496 &arg_list, NULL);
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400497 mutex_unlock(&battery->lock);
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400500 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400502 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", battery->alarm));
Patrick Mocheld550d982006-06-27 00:41:40 -0400503 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504}
505
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300506static int acpi_battery_init_alarm(struct acpi_battery *battery)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
Len Brown4be44fc2005-08-05 00:44:28 -0400508 acpi_status status = AE_OK;
509 acpi_handle handle = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300511 /* See if alarms are supported, and if so, set default */
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400512 status = acpi_get_handle(battery->device->handle, "_BTP", &handle);
513 if (ACPI_FAILURE(status)) {
Alexey Starikovskiy7b3bcc42009-10-15 14:31:24 +0400514 clear_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400515 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 }
Alexey Starikovskiy7b3bcc42009-10-15 14:31:24 +0400517 set_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400518 if (!battery->alarm)
519 battery->alarm = battery->design_capacity_warning;
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400520 return acpi_battery_set_alarm(battery);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521}
522
Andrey Borzenkov508df922007-10-28 12:50:09 +0300523static ssize_t acpi_battery_alarm_show(struct device *dev,
524 struct device_attribute *attr,
525 char *buf)
526{
527 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
528 return sprintf(buf, "%d\n", battery->alarm * 1000);
529}
530
531static ssize_t acpi_battery_alarm_store(struct device *dev,
532 struct device_attribute *attr,
533 const char *buf, size_t count)
534{
535 unsigned long x;
536 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
537 if (sscanf(buf, "%ld\n", &x) == 1)
538 battery->alarm = x/1000;
539 if (acpi_battery_present(battery))
540 acpi_battery_set_alarm(battery);
541 return count;
542}
543
544static struct device_attribute alarm_attr = {
Parag Warudkar01e8ef12008-10-18 20:28:50 -0700545 .attr = {.name = "alarm", .mode = 0644},
Andrey Borzenkov508df922007-10-28 12:50:09 +0300546 .show = acpi_battery_alarm_show,
547 .store = acpi_battery_alarm_store,
548};
549
550static int sysfs_add_battery(struct acpi_battery *battery)
551{
552 int result;
553
Lan Tianyuae6f6182011-06-30 11:32:40 +0800554 if (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA) {
Andrey Borzenkov508df922007-10-28 12:50:09 +0300555 battery->bat.properties = charge_battery_props;
556 battery->bat.num_properties =
557 ARRAY_SIZE(charge_battery_props);
558 } else {
559 battery->bat.properties = energy_battery_props;
560 battery->bat.num_properties =
561 ARRAY_SIZE(energy_battery_props);
562 }
563
564 battery->bat.name = acpi_device_bid(battery->device);
565 battery->bat.type = POWER_SUPPLY_TYPE_BATTERY;
566 battery->bat.get_property = acpi_battery_get_property;
567
568 result = power_supply_register(&battery->device->dev, &battery->bat);
569 if (result)
570 return result;
571 return device_create_file(battery->bat.dev, &alarm_attr);
572}
573
574static void sysfs_remove_battery(struct acpi_battery *battery)
575{
576 if (!battery->bat.dev)
577 return;
578 device_remove_file(battery->bat.dev, &alarm_attr);
579 power_supply_unregister(&battery->bat);
Alexey Starikovskiy91044762007-11-13 12:23:06 +0300580 battery->bat.dev = NULL;
Andrey Borzenkov508df922007-10-28 12:50:09 +0300581}
582
Zhang Rui557d5862010-10-22 10:02:06 +0800583/*
584 * According to the ACPI spec, some kinds of primary batteries can
585 * report percentage battery remaining capacity directly to OS.
586 * In this case, it reports the Last Full Charged Capacity == 100
587 * and BatteryPresentRate == 0xFFFFFFFF.
588 *
589 * Now we found some battery reports percentage remaining capacity
590 * even if it's rechargeable.
591 * https://bugzilla.kernel.org/show_bug.cgi?id=15979
592 *
593 * Handle this correctly so that they won't break userspace.
594 */
Lan Tianyu7b786222011-06-30 11:33:27 +0800595static void acpi_battery_quirks(struct acpi_battery *battery)
Zhang Rui557d5862010-10-22 10:02:06 +0800596{
597 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags))
598 return ;
599
600 if (battery->full_charge_capacity == 100 &&
601 battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN &&
602 battery->capacity_now >=0 && battery->capacity_now <= 100) {
603 set_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags);
604 battery->full_charge_capacity = battery->design_capacity;
605 battery->capacity_now = (battery->capacity_now *
606 battery->full_charge_capacity) / 100;
607 }
608}
609
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400610static int acpi_battery_update(struct acpi_battery *battery)
Vladimir Lebedev4bd35cd2007-02-10 01:43:48 -0500611{
Alexey Starikovskiy50b17852008-12-23 02:44:54 +0300612 int result, old_present = acpi_battery_present(battery);
Alexey Starikovskiy97749cd2008-01-01 14:27:24 -0500613 result = acpi_battery_get_status(battery);
Andrey Borzenkov508df922007-10-28 12:50:09 +0300614 if (result)
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300615 return result;
Andrey Borzenkov508df922007-10-28 12:50:09 +0300616 if (!acpi_battery_present(battery)) {
617 sysfs_remove_battery(battery);
Alexey Starikovskiy97749cd2008-01-01 14:27:24 -0500618 battery->update_time = 0;
Andrey Borzenkov508df922007-10-28 12:50:09 +0300619 return 0;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300620 }
Alexey Starikovskiy50b17852008-12-23 02:44:54 +0300621 if (!battery->update_time ||
622 old_present != acpi_battery_present(battery)) {
Alexey Starikovskiy97749cd2008-01-01 14:27:24 -0500623 result = acpi_battery_get_info(battery);
624 if (result)
625 return result;
626 acpi_battery_init_alarm(battery);
627 }
Andrey Borzenkov508df922007-10-28 12:50:09 +0300628 if (!battery->bat.dev)
629 sysfs_add_battery(battery);
Zhang Rui557d5862010-10-22 10:02:06 +0800630 result = acpi_battery_get_state(battery);
Lan Tianyu7b786222011-06-30 11:33:27 +0800631 acpi_battery_quirks(battery);
Zhang Rui557d5862010-10-22 10:02:06 +0800632 return result;
Vladimir Lebedev4bd35cd2007-02-10 01:43:48 -0500633}
634
Rafael J. Wysockida8aeb92011-01-06 23:42:27 +0100635static void acpi_battery_refresh(struct acpi_battery *battery)
636{
637 if (!battery->bat.dev)
638 return;
639
640 acpi_battery_get_info(battery);
641 /* The battery may have changed its reporting units. */
642 sysfs_remove_battery(battery);
643 sysfs_add_battery(battery);
644}
645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646/* --------------------------------------------------------------------------
647 FS Interface (/proc)
648 -------------------------------------------------------------------------- */
649
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +0300650#ifdef CONFIG_ACPI_PROCFS_POWER
Len Brown4be44fc2005-08-05 00:44:28 -0400651static struct proc_dir_entry *acpi_battery_dir;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300652
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400653static int acpi_battery_print_info(struct seq_file *seq, int result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200655 struct acpi_battery *battery = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300657 if (result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 goto end;
659
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400660 seq_printf(seq, "present: %s\n",
661 acpi_battery_present(battery)?"yes":"no");
662 if (!acpi_battery_present(battery))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 goto end;
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400664 if (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 seq_printf(seq, "design capacity: unknown\n");
666 else
667 seq_printf(seq, "design capacity: %d %sh\n",
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400668 battery->design_capacity,
669 acpi_battery_units(battery));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400671 if (battery->full_charge_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 seq_printf(seq, "last full capacity: unknown\n");
673 else
674 seq_printf(seq, "last full capacity: %d %sh\n",
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400675 battery->full_charge_capacity,
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400676 acpi_battery_units(battery));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400678 seq_printf(seq, "battery technology: %srechargeable\n",
679 (!battery->technology)?"non-":"");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400681 if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 seq_printf(seq, "design voltage: unknown\n");
683 else
684 seq_printf(seq, "design voltage: %d mV\n",
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400685 battery->design_voltage);
Len Brown4be44fc2005-08-05 00:44:28 -0400686 seq_printf(seq, "design capacity warning: %d %sh\n",
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400687 battery->design_capacity_warning,
688 acpi_battery_units(battery));
Len Brown4be44fc2005-08-05 00:44:28 -0400689 seq_printf(seq, "design capacity low: %d %sh\n",
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400690 battery->design_capacity_low,
691 acpi_battery_units(battery));
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400692 seq_printf(seq, "cycle count: %i\n", battery->cycle_count);
Len Brown4be44fc2005-08-05 00:44:28 -0400693 seq_printf(seq, "capacity granularity 1: %d %sh\n",
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400694 battery->capacity_granularity_1,
695 acpi_battery_units(battery));
Len Brown4be44fc2005-08-05 00:44:28 -0400696 seq_printf(seq, "capacity granularity 2: %d %sh\n",
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400697 battery->capacity_granularity_2,
698 acpi_battery_units(battery));
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400699 seq_printf(seq, "model number: %s\n", battery->model_number);
700 seq_printf(seq, "serial number: %s\n", battery->serial_number);
701 seq_printf(seq, "battery type: %s\n", battery->type);
702 seq_printf(seq, "OEM info: %s\n", battery->oem_info);
Len Brown4be44fc2005-08-05 00:44:28 -0400703 end:
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300704 if (result)
705 seq_printf(seq, "ERROR: Unable to read battery info\n");
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300706 return result;
707}
708
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400709static int acpi_battery_print_state(struct seq_file *seq, int result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200711 struct acpi_battery *battery = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300713 if (result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 goto end;
715
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400716 seq_printf(seq, "present: %s\n",
717 acpi_battery_present(battery)?"yes":"no");
718 if (!acpi_battery_present(battery))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400721 seq_printf(seq, "capacity state: %s\n",
722 (battery->state & 0x04)?"critical":"ok");
723 if ((battery->state & 0x01) && (battery->state & 0x02))
Len Brown4be44fc2005-08-05 00:44:28 -0400724 seq_printf(seq,
725 "charging state: charging/discharging\n");
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400726 else if (battery->state & 0x01)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 seq_printf(seq, "charging state: discharging\n");
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400728 else if (battery->state & 0x02)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 seq_printf(seq, "charging state: charging\n");
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400730 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 seq_printf(seq, "charging state: charged\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Alexey Starikovskiy7faa1442009-03-27 22:23:52 -0400733 if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 seq_printf(seq, "present rate: unknown\n");
735 else
736 seq_printf(seq, "present rate: %d %s\n",
Alexey Starikovskiy7faa1442009-03-27 22:23:52 -0400737 battery->rate_now, acpi_battery_units(battery));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400739 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 seq_printf(seq, "remaining capacity: unknown\n");
741 else
742 seq_printf(seq, "remaining capacity: %d %sh\n",
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400743 battery->capacity_now, acpi_battery_units(battery));
744 if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 seq_printf(seq, "present voltage: unknown\n");
746 else
747 seq_printf(seq, "present voltage: %d mV\n",
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400748 battery->voltage_now);
Len Brown4be44fc2005-08-05 00:44:28 -0400749 end:
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400750 if (result)
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300751 seq_printf(seq, "ERROR: Unable to read battery state\n");
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300752
753 return result;
754}
755
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400756static int acpi_battery_print_alarm(struct seq_file *seq, int result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200758 struct acpi_battery *battery = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300760 if (result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 goto end;
762
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300763 if (!acpi_battery_present(battery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 seq_printf(seq, "present: no\n");
765 goto end;
766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 seq_printf(seq, "alarm: ");
768 if (!battery->alarm)
769 seq_printf(seq, "unsupported\n");
770 else
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400771 seq_printf(seq, "%u %sh\n", battery->alarm,
772 acpi_battery_units(battery));
Len Brown4be44fc2005-08-05 00:44:28 -0400773 end:
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300774 if (result)
775 seq_printf(seq, "ERROR: Unable to read battery alarm\n");
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300776 return result;
777}
778
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400779static ssize_t acpi_battery_write_alarm(struct file *file,
780 const char __user * buffer,
781 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
Len Brown4be44fc2005-08-05 00:44:28 -0400783 int result = 0;
784 char alarm_string[12] = { '\0' };
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200785 struct seq_file *m = file->private_data;
786 struct acpi_battery *battery = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 if (!battery || (count > sizeof(alarm_string) - 1))
Patrick Mocheld550d982006-06-27 00:41:40 -0400789 return -EINVAL;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300790 if (!acpi_battery_present(battery)) {
791 result = -ENODEV;
792 goto end;
793 }
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300794 if (copy_from_user(alarm_string, buffer, count)) {
795 result = -EFAULT;
796 goto end;
797 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 alarm_string[count] = '\0';
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400799 battery->alarm = simple_strtol(alarm_string, NULL, 0);
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400800 result = acpi_battery_set_alarm(battery);
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300801 end:
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300802 if (!result)
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400803 return count;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300804 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805}
806
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400807typedef int(*print_func)(struct seq_file *seq, int result);
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400808
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400809static print_func acpi_print_funcs[ACPI_BATTERY_NUMFILES] = {
810 acpi_battery_print_info,
811 acpi_battery_print_state,
812 acpi_battery_print_alarm,
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400813};
814
815static int acpi_battery_read(int fid, struct seq_file *seq)
816{
817 struct acpi_battery *battery = seq->private;
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400818 int result = acpi_battery_update(battery);
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400819 return acpi_print_funcs[fid](seq, result);
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400820}
821
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400822#define DECLARE_FILE_FUNCTIONS(_name) \
823static int acpi_battery_read_##_name(struct seq_file *seq, void *offset) \
824{ \
825 return acpi_battery_read(_name##_tag, seq); \
826} \
827static int acpi_battery_##_name##_open_fs(struct inode *inode, struct file *file) \
828{ \
829 return single_open(file, acpi_battery_read_##_name, PDE(inode)->data); \
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400830}
831
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400832DECLARE_FILE_FUNCTIONS(info);
833DECLARE_FILE_FUNCTIONS(state);
834DECLARE_FILE_FUNCTIONS(alarm);
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400835
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400836#undef DECLARE_FILE_FUNCTIONS
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400837
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400838#define FILE_DESCRIPTION_RO(_name) \
839 { \
840 .name = __stringify(_name), \
841 .mode = S_IRUGO, \
842 .ops = { \
843 .open = acpi_battery_##_name##_open_fs, \
844 .read = seq_read, \
845 .llseek = seq_lseek, \
846 .release = single_release, \
847 .owner = THIS_MODULE, \
848 }, \
849 }
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400850
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400851#define FILE_DESCRIPTION_RW(_name) \
852 { \
853 .name = __stringify(_name), \
854 .mode = S_IFREG | S_IRUGO | S_IWUSR, \
855 .ops = { \
856 .open = acpi_battery_##_name##_open_fs, \
857 .read = seq_read, \
858 .llseek = seq_lseek, \
859 .write = acpi_battery_write_##_name, \
860 .release = single_release, \
861 .owner = THIS_MODULE, \
862 }, \
863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400865static struct battery_file {
866 struct file_operations ops;
867 mode_t mode;
Jan Engelhardt070d8eb2009-01-12 00:07:55 +0100868 const char *name;
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400869} acpi_battery_file[] = {
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400870 FILE_DESCRIPTION_RO(info),
871 FILE_DESCRIPTION_RO(state),
872 FILE_DESCRIPTION_RW(alarm),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873};
874
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400875#undef FILE_DESCRIPTION_RO
876#undef FILE_DESCRIPTION_RW
877
Len Brown4be44fc2005-08-05 00:44:28 -0400878static int acpi_battery_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
Len Brown4be44fc2005-08-05 00:44:28 -0400880 struct proc_dir_entry *entry = NULL;
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400881 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Zhang Rui6d855fc2011-01-10 11:16:30 +0800883 printk(KERN_WARNING PREFIX "Deprecated procfs I/F for battery is loaded,"
884 " please retry with CONFIG_ACPI_PROCFS_POWER cleared\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 if (!acpi_device_dir(device)) {
886 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown4be44fc2005-08-05 00:44:28 -0400887 acpi_battery_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400889 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 }
891
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400892 for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i) {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700893 entry = proc_create_data(acpi_battery_file[i].name,
894 acpi_battery_file[i].mode,
895 acpi_device_dir(device),
896 &acpi_battery_file[i].ops,
897 acpi_driver_data(device));
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400898 if (!entry)
899 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 }
Patrick Mocheld550d982006-06-27 00:41:40 -0400901 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902}
903
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400904static void acpi_battery_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400906 int i;
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400907 if (!acpi_device_dir(device))
908 return;
909 for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i)
910 remove_proc_entry(acpi_battery_file[i].name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +0400913 remove_proc_entry(acpi_device_bid(device), acpi_battery_dir);
914 acpi_device_dir(device) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915}
916
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400917#endif
Alexey Starikovskiy3e58ea02007-09-26 19:43:11 +0400918
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919/* --------------------------------------------------------------------------
920 Driver Interface
921 -------------------------------------------------------------------------- */
922
Bjorn Helgaasd9406692009-04-30 09:35:47 -0600923static void acpi_battery_notify(struct acpi_device *device, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924{
Bjorn Helgaasd9406692009-04-30 09:35:47 -0600925 struct acpi_battery *battery = acpi_driver_data(device);
Zhang Rui153e5002010-07-07 09:11:57 +0800926 struct device *old;
Bjorn Helgaasd9406692009-04-30 09:35:47 -0600927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 if (!battery)
Patrick Mocheld550d982006-06-27 00:41:40 -0400929 return;
Zhang Rui153e5002010-07-07 09:11:57 +0800930 old = battery->bat.dev;
Rafael J. Wysockida8aeb92011-01-06 23:42:27 +0100931 if (event == ACPI_BATTERY_NOTIFY_INFO)
932 acpi_battery_refresh(battery);
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400933 acpi_battery_update(battery);
934 acpi_bus_generate_proc_event(device, event,
935 acpi_battery_present(battery));
936 acpi_bus_generate_netlink_event(device->pnp.device_class,
Kay Sievers07944692008-10-30 01:18:59 +0100937 dev_name(&device->dev), event,
Vladimir Lebedev9ea7d572007-02-20 15:48:06 +0300938 acpi_battery_present(battery));
Justin P. Mattock2345baf2009-12-13 14:42:36 -0800939 /* acpi_battery_update could remove power_supply object */
Zhang Rui153e5002010-07-07 09:11:57 +0800940 if (old && battery->bat.dev)
Alan Jenkinsf79e1ce2009-06-30 14:36:16 +0000941 power_supply_changed(&battery->bat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942}
943
Kyle McMartin25be5822011-03-22 16:19:50 -0400944static int battery_notify(struct notifier_block *nb,
945 unsigned long mode, void *_unused)
946{
947 struct acpi_battery *battery = container_of(nb, struct acpi_battery,
948 pm_nb);
949 switch (mode) {
Lan Tianyud5a59112011-06-30 11:33:40 +0800950 case PM_POST_HIBERNATION:
Kyle McMartin25be5822011-03-22 16:19:50 -0400951 case PM_POST_SUSPEND:
Lan Tianyu6e17fb62011-06-30 11:33:58 +0800952 if (battery->bat.dev) {
953 sysfs_remove_battery(battery);
954 sysfs_add_battery(battery);
955 }
Kyle McMartin25be5822011-03-22 16:19:50 -0400956 break;
957 }
958
959 return 0;
960}
961
Len Brown4be44fc2005-08-05 00:44:28 -0400962static int acpi_battery_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963{
Len Brown4be44fc2005-08-05 00:44:28 -0400964 int result = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400965 struct acpi_battery *battery = NULL;
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400966 acpi_handle handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400968 return -EINVAL;
Burman Yan36bcbec2006-12-19 12:56:11 -0800969 battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 if (!battery)
Patrick Mocheld550d982006-06-27 00:41:40 -0400971 return -ENOMEM;
Patrick Mochel145def82006-05-19 16:54:39 -0400972 battery->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME);
974 strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700975 device->driver_data = battery;
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +0400976 mutex_init(&battery->lock);
Alexey Starikovskiyc67fcd62009-10-15 14:31:44 +0400977 if (ACPI_SUCCESS(acpi_get_handle(battery->device->handle,
978 "_BIX", &handle)))
979 set_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags);
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +0400980 acpi_battery_update(battery);
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +0300981#ifdef CONFIG_ACPI_PROCFS_POWER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 result = acpi_battery_add_fs(device);
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400983#endif
Len Brown586caae2009-06-18 00:38:27 -0400984 if (!result) {
985 printk(KERN_INFO PREFIX "%s Slot [%s] (battery %s)\n",
986 ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device),
987 device->status.battery_present ? "present" : "absent");
988 } else {
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +0300989#ifdef CONFIG_ACPI_PROCFS_POWER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 acpi_battery_remove_fs(device);
Alexey Starikovskiyd7380962007-09-26 19:43:04 +0400991#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 kfree(battery);
993 }
Kyle McMartin25be5822011-03-22 16:19:50 -0400994
995 battery->pm_nb.notifier_call = battery_notify;
996 register_pm_notifier(&battery->pm_nb);
997
Patrick Mocheld550d982006-06-27 00:41:40 -0400998 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999}
1000
Len Brown4be44fc2005-08-05 00:44:28 -04001001static int acpi_battery_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002{
Len Brown4be44fc2005-08-05 00:44:28 -04001003 struct acpi_battery *battery = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001006 return -EINVAL;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001007 battery = acpi_driver_data(device);
Kyle McMartin25be5822011-03-22 16:19:50 -04001008 unregister_pm_notifier(&battery->pm_nb);
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +03001009#ifdef CONFIG_ACPI_PROCFS_POWER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 acpi_battery_remove_fs(device);
Alexey Starikovskiyd7380962007-09-26 19:43:04 +04001011#endif
Andrey Borzenkov508df922007-10-28 12:50:09 +03001012 sysfs_remove_battery(battery);
Alexey Starikovskiy038fdea2007-09-26 19:42:46 +04001013 mutex_destroy(&battery->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 kfree(battery);
Patrick Mocheld550d982006-06-27 00:41:40 -04001015 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016}
1017
Jiri Kosina34c44152006-10-10 14:20:41 -07001018/* this is needed to learn about changes made in suspended state */
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001019static int acpi_battery_resume(struct acpi_device *device)
Jiri Kosina34c44152006-10-10 14:20:41 -07001020{
1021 struct acpi_battery *battery;
Jiri Kosina34c44152006-10-10 14:20:41 -07001022 if (!device)
1023 return -EINVAL;
Alexey Starikovskiyf1d46612007-09-26 19:42:52 +04001024 battery = acpi_driver_data(device);
1025 battery->update_time = 0;
Andrey Borzenkov508df922007-10-28 12:50:09 +03001026 acpi_battery_update(battery);
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +03001027 return 0;
Jiri Kosina34c44152006-10-10 14:20:41 -07001028}
1029
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +04001030static struct acpi_driver acpi_battery_driver = {
1031 .name = "battery",
1032 .class = ACPI_BATTERY_CLASS,
1033 .ids = battery_device_ids,
Bjorn Helgaasd9406692009-04-30 09:35:47 -06001034 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +04001035 .ops = {
1036 .add = acpi_battery_add,
1037 .resume = acpi_battery_resume,
1038 .remove = acpi_battery_remove,
Bjorn Helgaasd9406692009-04-30 09:35:47 -06001039 .notify = acpi_battery_notify,
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +04001040 },
1041};
1042
Linus Torvaldsb0cbc862009-04-11 12:45:20 -07001043static void __init acpi_battery_init_async(void *unused, async_cookie_t cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
Pavel Machek4d8316d2006-08-14 22:37:22 -07001045 if (acpi_disabled)
Arjan van de Ven0f66af52009-01-10 14:19:05 -05001046 return;
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +03001047#ifdef CONFIG_ACPI_PROCFS_POWER
Rich Townsend3f86b832006-07-01 11:36:54 -04001048 acpi_battery_dir = acpi_lock_battery_dir();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 if (!acpi_battery_dir)
Arjan van de Ven0f66af52009-01-10 14:19:05 -05001050 return;
Alexey Starikovskiyd7380962007-09-26 19:43:04 +04001051#endif
Alexey Starikovskiyaa650bb2007-09-26 19:42:58 +04001052 if (acpi_bus_register_driver(&acpi_battery_driver) < 0) {
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +03001053#ifdef CONFIG_ACPI_PROCFS_POWER
Rich Townsend3f86b832006-07-01 11:36:54 -04001054 acpi_unlock_battery_dir(acpi_battery_dir);
Alexey Starikovskiyd7380962007-09-26 19:43:04 +04001055#endif
Arjan van de Ven0f66af52009-01-10 14:19:05 -05001056 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 }
Arjan van de Ven0f66af52009-01-10 14:19:05 -05001058 return;
1059}
1060
1061static int __init acpi_battery_init(void)
1062{
1063 async_schedule(acpi_battery_init_async, NULL);
Patrick Mocheld550d982006-06-27 00:41:40 -04001064 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065}
1066
Len Brown4be44fc2005-08-05 00:44:28 -04001067static void __exit acpi_battery_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 acpi_bus_unregister_driver(&acpi_battery_driver);
Alexey Starikovskiyfdcedbb2007-11-19 16:33:45 +03001070#ifdef CONFIG_ACPI_PROCFS_POWER
Rich Townsend3f86b832006-07-01 11:36:54 -04001071 acpi_unlock_battery_dir(acpi_battery_dir);
Alexey Starikovskiyd7380962007-09-26 19:43:04 +04001072#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073}
1074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075module_init(acpi_battery_init);
1076module_exit(acpi_battery_exit);