blob: 81651032791b32364e3ed3b4cc45c0341a9db355 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * acpi_battery.c - ACPI Battery Driver ($Revision: 37 $)
3 *
4 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
6 *
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or (at
12 * your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 *
23 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24 */
25
26#include <linux/kernel.h>
27#include <linux/module.h>
28#include <linux/init.h>
29#include <linux/types.h>
30#include <linux/proc_fs.h>
31#include <linux/seq_file.h>
32#include <asm/uaccess.h>
33
34#include <acpi/acpi_bus.h>
35#include <acpi/acpi_drivers.h>
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#define ACPI_BATTERY_VALUE_UNKNOWN 0xFFFFFFFF
38
39#define ACPI_BATTERY_FORMAT_BIF "NNNNNNNNNSSSS"
40#define ACPI_BATTERY_FORMAT_BST "NNNN"
41
42#define ACPI_BATTERY_COMPONENT 0x00040000
43#define ACPI_BATTERY_CLASS "battery"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#define ACPI_BATTERY_DEVICE_NAME "Battery"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#define ACPI_BATTERY_NOTIFY_STATUS 0x80
46#define ACPI_BATTERY_NOTIFY_INFO 0x81
47#define ACPI_BATTERY_UNITS_WATTS "mW"
48#define ACPI_BATTERY_UNITS_AMPS "mA"
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#define _COMPONENT ACPI_BATTERY_COMPONENT
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +030051
52#define ACPI_BATTERY_UPDATE_TIME 0
53
54#define ACPI_BATTERY_NONE_UPDATE 0
55#define ACPI_BATTERY_EASY_UPDATE 1
56#define ACPI_BATTERY_INIT_UPDATE 2
57
Len Brownf52fd662007-02-12 22:42:12 -050058ACPI_MODULE_NAME("battery");
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Len Brownf52fd662007-02-12 22:42:12 -050060MODULE_AUTHOR("Paul Diefenbaugh");
Len Brown7cda93e2007-02-12 23:50:02 -050061MODULE_DESCRIPTION("ACPI Battery Driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070062MODULE_LICENSE("GPL");
63
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +030064static unsigned int update_time = ACPI_BATTERY_UPDATE_TIME;
65
66/* 0 - every time, > 0 - by update_time */
67module_param(update_time, uint, 0644);
68
Rich Townsend3f86b832006-07-01 11:36:54 -040069extern struct proc_dir_entry *acpi_lock_battery_dir(void);
70extern void *acpi_unlock_battery_dir(struct proc_dir_entry *acpi_battery_dir);
71
Len Brown4be44fc2005-08-05 00:44:28 -040072static int acpi_battery_add(struct acpi_device *device);
73static int acpi_battery_remove(struct acpi_device *device, int type);
Patrick Mochel5d9464a2006-12-07 20:56:27 +080074static int acpi_battery_resume(struct acpi_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Thomas Renninger1ba90e32007-07-23 14:44:41 +020076static const struct acpi_device_id battery_device_ids[] = {
77 {"PNP0C0A", 0},
78 {"", 0},
79};
80MODULE_DEVICE_TABLE(acpi, battery_device_ids);
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082static struct acpi_driver acpi_battery_driver = {
Len Brownc2b67052007-02-12 23:33:40 -050083 .name = "battery",
Len Brown4be44fc2005-08-05 00:44:28 -040084 .class = ACPI_BATTERY_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +020085 .ids = battery_device_ids,
Len Brown4be44fc2005-08-05 00:44:28 -040086 .ops = {
87 .add = acpi_battery_add,
Jiri Kosina34c44152006-10-10 14:20:41 -070088 .resume = acpi_battery_resume,
Len Brown4be44fc2005-08-05 00:44:28 -040089 .remove = acpi_battery_remove,
90 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070091};
92
Vladimir Lebedeva1f0eff2007-02-20 15:48:06 +030093struct acpi_battery_state {
Len Brown4be44fc2005-08-05 00:44:28 -040094 acpi_integer state;
95 acpi_integer present_rate;
96 acpi_integer remaining_capacity;
97 acpi_integer present_voltage;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098};
99
100struct acpi_battery_info {
Len Brown4be44fc2005-08-05 00:44:28 -0400101 acpi_integer power_unit;
102 acpi_integer design_capacity;
103 acpi_integer last_full_capacity;
104 acpi_integer battery_technology;
105 acpi_integer design_voltage;
106 acpi_integer design_capacity_warning;
107 acpi_integer design_capacity_low;
108 acpi_integer battery_capacity_granularity_1;
109 acpi_integer battery_capacity_granularity_2;
110 acpi_string model_number;
111 acpi_string serial_number;
112 acpi_string battery_type;
113 acpi_string oem_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114};
115
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400116enum acpi_battery_files{
117 ACPI_BATTERY_INFO = 0,
118 ACPI_BATTERY_STATE,
119 ACPI_BATTERY_ALARM,
120 ACPI_BATTERY_NUMFILES,
121};
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123struct acpi_battery_flags {
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300124 u8 battery_present_prev;
125 u8 alarm_present;
126 u8 init_update;
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400127 u8 update[ACPI_BATTERY_NUMFILES];
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300128 u8 power_unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129};
130
131struct acpi_battery {
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300132 struct mutex mutex;
Vladimir Lebedev9ea7d572007-02-20 15:48:06 +0300133 struct acpi_device *device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 struct acpi_battery_flags flags;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300135 struct acpi_buffer bif_data;
136 struct acpi_buffer bst_data;
Len Brown4be44fc2005-08-05 00:44:28 -0400137 unsigned long alarm;
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400138 unsigned long update_time[ACPI_BATTERY_NUMFILES];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139};
140
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400141inline int acpi_battery_present(struct acpi_battery *battery)
142{
143 return battery->device->status.battery_present;
144}
145inline char *acpi_battery_power_units(struct acpi_battery *battery)
146{
147 if (battery->flags.power_unit)
148 return ACPI_BATTERY_UNITS_AMPS;
149 else
150 return ACPI_BATTERY_UNITS_WATTS;
151}
152
153inline acpi_handle acpi_battery_handle(struct acpi_battery *battery)
154{
155 return battery->device->handle;
156}
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158/* --------------------------------------------------------------------------
159 Battery Management
160 -------------------------------------------------------------------------- */
161
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300162static void acpi_battery_check_result(struct acpi_battery *battery, int result)
163{
164 if (!battery)
165 return;
166
167 if (result) {
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400168 battery->flags.init_update = 1;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300169 }
170}
171
172static int acpi_battery_extract_package(struct acpi_battery *battery,
173 union acpi_object *package,
174 struct acpi_buffer *format,
175 struct acpi_buffer *data,
176 char *package_name)
177{
178 acpi_status status = AE_OK;
179 struct acpi_buffer data_null = { 0, NULL };
180
181 status = acpi_extract_package(package, format, &data_null);
182 if (status != AE_BUFFER_OVERFLOW) {
183 ACPI_EXCEPTION((AE_INFO, status, "Extracting size %s",
184 package_name));
185 return -ENODEV;
186 }
187
188 if (data_null.length != data->length) {
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400189 kfree(data->pointer);
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300190 data->pointer = kzalloc(data_null.length, GFP_KERNEL);
191 if (!data->pointer) {
192 ACPI_EXCEPTION((AE_INFO, AE_NO_MEMORY, "kzalloc()"));
193 return -ENOMEM;
194 }
195 data->length = data_null.length;
196 }
197
198 status = acpi_extract_package(package, format, data);
199 if (ACPI_FAILURE(status)) {
200 ACPI_EXCEPTION((AE_INFO, status, "Extracting %s",
201 package_name));
202 return -ENODEV;
203 }
204
205 return 0;
206}
207
208static int acpi_battery_get_status(struct acpi_battery *battery)
209{
210 int result = 0;
211
212 result = acpi_bus_get_status(battery->device);
213 if (result) {
214 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Evaluating _STA"));
215 return -ENODEV;
216 }
217 return result;
218}
219
220static int acpi_battery_get_info(struct acpi_battery *battery)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
Len Brown4be44fc2005-08-05 00:44:28 -0400222 int result = 0;
223 acpi_status status = 0;
224 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
225 struct acpi_buffer format = { sizeof(ACPI_BATTERY_FORMAT_BIF),
226 ACPI_BATTERY_FORMAT_BIF
227 };
Len Brown4be44fc2005-08-05 00:44:28 -0400228 union acpi_object *package = NULL;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300229 struct acpi_buffer *data = NULL;
230 struct acpi_battery_info *bif = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400232 battery->update_time[ACPI_BATTERY_INFO] = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300234 if (!acpi_battery_present(battery))
235 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400237 /* Evaluate _BIF */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Vladimir Lebedev9ea7d572007-02-20 15:48:06 +0300239 status =
240 acpi_evaluate_object(acpi_battery_handle(battery), "_BIF", NULL,
241 &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400243 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BIF"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400244 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 }
246
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200247 package = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300249 data = &battery->bif_data;
250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 /* Extract Package Data */
252
Vladimir Lebedev9ea7d572007-02-20 15:48:06 +0300253 result =
254 acpi_battery_extract_package(battery, package, &format, data,
255 "_BIF");
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300256 if (result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Len Brown4be44fc2005-08-05 00:44:28 -0400259 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400261 kfree(buffer.pointer);
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300262
263 if (!result) {
264 bif = data->pointer;
265 battery->flags.power_unit = bif->power_unit;
266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Patrick Mocheld550d982006-06-27 00:41:40 -0400268 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269}
270
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300271static int acpi_battery_get_state(struct acpi_battery *battery)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
Len Brown4be44fc2005-08-05 00:44:28 -0400273 int result = 0;
274 acpi_status status = 0;
275 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
276 struct acpi_buffer format = { sizeof(ACPI_BATTERY_FORMAT_BST),
277 ACPI_BATTERY_FORMAT_BST
278 };
Len Brown4be44fc2005-08-05 00:44:28 -0400279 union acpi_object *package = NULL;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300280 struct acpi_buffer *data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400282 battery->update_time[ACPI_BATTERY_STATE] = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300284 if (!acpi_battery_present(battery))
285 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400287 /* Evaluate _BST */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Vladimir Lebedev9ea7d572007-02-20 15:48:06 +0300289 status =
290 acpi_evaluate_object(acpi_battery_handle(battery), "_BST", NULL,
291 &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400293 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BST"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400294 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 }
296
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200297 package = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300299 data = &battery->bst_data;
300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 /* Extract Package Data */
302
Vladimir Lebedev9ea7d572007-02-20 15:48:06 +0300303 result =
304 acpi_battery_extract_package(battery, package, &format, data,
305 "_BST");
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300306 if (result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Len Brown4be44fc2005-08-05 00:44:28 -0400309 end:
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400310 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Patrick Mocheld550d982006-06-27 00:41:40 -0400312 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
314
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300315static int acpi_battery_get_alarm(struct acpi_battery *battery)
316{
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400317 battery->update_time[ACPI_BATTERY_ALARM] = get_seconds();
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300318
319 return 0;
320}
321
Vladimir Lebedev9ea7d572007-02-20 15:48:06 +0300322static int acpi_battery_set_alarm(struct acpi_battery *battery,
323 unsigned long alarm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
Len Brown4be44fc2005-08-05 00:44:28 -0400325 acpi_status status = 0;
326 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
327 struct acpi_object_list arg_list = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400329 battery->update_time[ACPI_BATTERY_ALARM] = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300331 if (!acpi_battery_present(battery))
332 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400334 if (!battery->flags.alarm_present)
Patrick Mocheld550d982006-06-27 00:41:40 -0400335 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 arg0.integer.value = alarm;
338
Vladimir Lebedev9ea7d572007-02-20 15:48:06 +0300339 status =
340 acpi_evaluate_object(acpi_battery_handle(battery), "_BTP",
341 &arg_list, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400343 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", (u32) alarm));
346
347 battery->alarm = alarm;
348
Patrick Mocheld550d982006-06-27 00:41:40 -0400349 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350}
351
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300352static int acpi_battery_init_alarm(struct acpi_battery *battery)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
Len Brown4be44fc2005-08-05 00:44:28 -0400354 int result = 0;
355 acpi_status status = AE_OK;
356 acpi_handle handle = NULL;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300357 struct acpi_battery_info *bif = battery->bif_data.pointer;
358 unsigned long alarm = battery->alarm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300360 /* See if alarms are supported, and if so, set default */
Len Brown4be44fc2005-08-05 00:44:28 -0400361
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300362 status = acpi_get_handle(acpi_battery_handle(battery), "_BTP", &handle);
363 if (ACPI_SUCCESS(status)) {
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400364 battery->flags.alarm_present = 1;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300365 if (!alarm && bif) {
366 alarm = bif->design_capacity_warning;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 }
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300368 result = acpi_battery_set_alarm(battery, alarm);
369 if (result)
370 goto end;
371 } else {
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400372 battery->flags.alarm_present = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 }
374
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300375 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Patrick Mocheld550d982006-06-27 00:41:40 -0400377 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378}
379
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300380static int acpi_battery_init_update(struct acpi_battery *battery)
Vladimir Lebedev4bd35cd2007-02-10 01:43:48 -0500381{
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300382 int result = 0;
383
384 result = acpi_battery_get_status(battery);
385 if (result)
386 return result;
387
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400388 battery->flags.battery_present_prev = acpi_battery_present(battery);
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300389
390 if (acpi_battery_present(battery)) {
391 result = acpi_battery_get_info(battery);
392 if (result)
393 return result;
394 result = acpi_battery_get_state(battery);
395 if (result)
396 return result;
397
398 acpi_battery_init_alarm(battery);
399 }
400
401 return result;
402}
403
404static int acpi_battery_update(struct acpi_battery *battery,
Vladimir Lebedev9ea7d572007-02-20 15:48:06 +0300405 int update, int *update_result_ptr)
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300406{
407 int result = 0;
408 int update_result = ACPI_BATTERY_NONE_UPDATE;
409
410 if (!acpi_battery_present(battery)) {
411 update = 1;
412 }
413
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400414 if (battery->flags.init_update) {
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300415 result = acpi_battery_init_update(battery);
416 if (result)
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400417 goto end;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300418 update_result = ACPI_BATTERY_INIT_UPDATE;
419 } else if (update) {
420 result = acpi_battery_get_status(battery);
421 if (result)
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400422 goto end;
423 if ((!battery->flags.battery_present_prev & acpi_battery_present(battery))
424 || (battery->flags.battery_present_prev & !acpi_battery_present(battery))) {
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300425 result = acpi_battery_init_update(battery);
426 if (result)
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400427 goto end;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300428 update_result = ACPI_BATTERY_INIT_UPDATE;
429 } else {
430 update_result = ACPI_BATTERY_EASY_UPDATE;
431 }
432 }
433
434 end:
435
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400436 battery->flags.init_update = (result != 0);
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300437
438 *update_result_ptr = update_result;
439
440 return result;
441}
442
443static void acpi_battery_notify_update(struct acpi_battery *battery)
444{
445 acpi_battery_get_status(battery);
446
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400447 if (battery->flags.init_update) {
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300448 return;
449 }
450
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400451 if ((!battery->flags.battery_present_prev &
452 acpi_battery_present(battery)) ||
453 (battery->flags.battery_present_prev &
454 !acpi_battery_present(battery))) {
455 battery->flags.init_update = 1;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300456 } else {
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400457 battery->flags.update[ACPI_BATTERY_INFO] = 1;
458 battery->flags.update[ACPI_BATTERY_STATE] = 1;
459 battery->flags.update[ACPI_BATTERY_ALARM] = 1;
Vladimir Lebedev4bd35cd2007-02-10 01:43:48 -0500460 }
461}
462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463/* --------------------------------------------------------------------------
464 FS Interface (/proc)
465 -------------------------------------------------------------------------- */
466
Len Brown4be44fc2005-08-05 00:44:28 -0400467static struct proc_dir_entry *acpi_battery_dir;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300468
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400469static int acpi_battery_print_info(struct seq_file *seq, int result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200471 struct acpi_battery *battery = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 struct acpi_battery_info *bif = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400473 char *units = "?";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300475 if (result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 goto end;
477
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300478 if (acpi_battery_present(battery))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 seq_printf(seq, "present: yes\n");
480 else {
481 seq_printf(seq, "present: no\n");
482 goto end;
483 }
484
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300485 bif = battery->bif_data.pointer;
486 if (!bif) {
487 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "BIF buffer is NULL"));
488 result = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 goto end;
490 }
491
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300492 /* Battery Units */
493
494 units = acpi_battery_power_units(battery);
Len Brown4be44fc2005-08-05 00:44:28 -0400495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 if (bif->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
497 seq_printf(seq, "design capacity: unknown\n");
498 else
499 seq_printf(seq, "design capacity: %d %sh\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400500 (u32) bif->design_capacity, units);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 if (bif->last_full_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
503 seq_printf(seq, "last full capacity: unknown\n");
504 else
505 seq_printf(seq, "last full capacity: %d %sh\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400506 (u32) bif->last_full_capacity, units);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508 switch ((u32) bif->battery_technology) {
509 case 0:
510 seq_printf(seq, "battery technology: non-rechargeable\n");
511 break;
512 case 1:
513 seq_printf(seq, "battery technology: rechargeable\n");
514 break;
515 default:
516 seq_printf(seq, "battery technology: unknown\n");
517 break;
518 }
519
520 if (bif->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
521 seq_printf(seq, "design voltage: unknown\n");
522 else
523 seq_printf(seq, "design voltage: %d mV\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400524 (u32) bif->design_voltage);
Len Brown4be44fc2005-08-05 00:44:28 -0400525 seq_printf(seq, "design capacity warning: %d %sh\n",
526 (u32) bif->design_capacity_warning, units);
527 seq_printf(seq, "design capacity low: %d %sh\n",
528 (u32) bif->design_capacity_low, units);
529 seq_printf(seq, "capacity granularity 1: %d %sh\n",
530 (u32) bif->battery_capacity_granularity_1, units);
531 seq_printf(seq, "capacity granularity 2: %d %sh\n",
532 (u32) bif->battery_capacity_granularity_2, units);
533 seq_printf(seq, "model number: %s\n", bif->model_number);
534 seq_printf(seq, "serial number: %s\n", bif->serial_number);
535 seq_printf(seq, "battery type: %s\n", bif->battery_type);
536 seq_printf(seq, "OEM info: %s\n", bif->oem_info);
537
538 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300540 if (result)
541 seq_printf(seq, "ERROR: Unable to read battery info\n");
542
543 return result;
544}
545
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400546static int acpi_battery_print_state(struct seq_file *seq, int result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200548 struct acpi_battery *battery = seq->private;
Vladimir Lebedeva1f0eff2007-02-20 15:48:06 +0300549 struct acpi_battery_state *bst = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400550 char *units = "?";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300552 if (result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 goto end;
554
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300555 if (acpi_battery_present(battery))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 seq_printf(seq, "present: yes\n");
557 else {
558 seq_printf(seq, "present: no\n");
559 goto end;
560 }
561
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300562 bst = battery->bst_data.pointer;
563 if (!bst) {
564 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "BST buffer is NULL"));
565 result = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 goto end;
567 }
568
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300569 /* Battery Units */
570
571 units = acpi_battery_power_units(battery);
572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 if (!(bst->state & 0x04))
574 seq_printf(seq, "capacity state: ok\n");
575 else
576 seq_printf(seq, "capacity state: critical\n");
577
Len Brown4be44fc2005-08-05 00:44:28 -0400578 if ((bst->state & 0x01) && (bst->state & 0x02)) {
579 seq_printf(seq,
580 "charging state: charging/discharging\n");
Len Brown4be44fc2005-08-05 00:44:28 -0400581 } else if (bst->state & 0x01)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 seq_printf(seq, "charging state: discharging\n");
583 else if (bst->state & 0x02)
584 seq_printf(seq, "charging state: charging\n");
585 else {
586 seq_printf(seq, "charging state: charged\n");
587 }
588
589 if (bst->present_rate == ACPI_BATTERY_VALUE_UNKNOWN)
590 seq_printf(seq, "present rate: unknown\n");
591 else
592 seq_printf(seq, "present rate: %d %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400593 (u32) bst->present_rate, units);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 if (bst->remaining_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
596 seq_printf(seq, "remaining capacity: unknown\n");
597 else
598 seq_printf(seq, "remaining capacity: %d %sh\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400599 (u32) bst->remaining_capacity, units);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601 if (bst->present_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
602 seq_printf(seq, "present voltage: unknown\n");
603 else
604 seq_printf(seq, "present voltage: %d mV\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400605 (u32) bst->present_voltage);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Len Brown4be44fc2005-08-05 00:44:28 -0400607 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300609 if (result) {
610 seq_printf(seq, "ERROR: Unable to read battery state\n");
611 }
612
613 return result;
614}
615
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400616static int acpi_battery_print_alarm(struct seq_file *seq, int result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200618 struct acpi_battery *battery = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -0400619 char *units = "?";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300621 if (result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 goto end;
623
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300624 if (!acpi_battery_present(battery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 seq_printf(seq, "present: no\n");
626 goto end;
627 }
628
629 /* Battery Units */
Len Brown4be44fc2005-08-05 00:44:28 -0400630
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300631 units = acpi_battery_power_units(battery);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633 seq_printf(seq, "alarm: ");
634 if (!battery->alarm)
635 seq_printf(seq, "unsupported\n");
636 else
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300637 seq_printf(seq, "%lu %sh\n", battery->alarm, units);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Len Brown4be44fc2005-08-05 00:44:28 -0400639 end:
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300640
641 if (result)
642 seq_printf(seq, "ERROR: Unable to read battery alarm\n");
643
644 return result;
645}
646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -0400648acpi_battery_write_alarm(struct file *file,
649 const char __user * buffer,
650 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
Len Brown4be44fc2005-08-05 00:44:28 -0400652 int result = 0;
653 char alarm_string[12] = { '\0' };
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200654 struct seq_file *m = file->private_data;
655 struct acpi_battery *battery = m->private;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300656 int update_result = ACPI_BATTERY_NONE_UPDATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 if (!battery || (count > sizeof(alarm_string) - 1))
Patrick Mocheld550d982006-06-27 00:41:40 -0400659 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400661 mutex_lock(&battery->mutex);
Vladimir Lebedev4bd35cd2007-02-10 01:43:48 -0500662
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300663 result = acpi_battery_update(battery, 1, &update_result);
664 if (result) {
665 result = -ENODEV;
666 goto end;
667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300669 if (!acpi_battery_present(battery)) {
670 result = -ENODEV;
671 goto end;
672 }
673
674 if (copy_from_user(alarm_string, buffer, count)) {
675 result = -EFAULT;
676 goto end;
677 }
Len Brown4be44fc2005-08-05 00:44:28 -0400678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 alarm_string[count] = '\0';
680
Len Brown4be44fc2005-08-05 00:44:28 -0400681 result = acpi_battery_set_alarm(battery,
682 simple_strtoul(alarm_string, NULL, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 if (result)
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300684 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300686 end:
687
688 acpi_battery_check_result(battery, result);
689
690 if (!result)
691 result = count;
692
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400693 mutex_unlock(&battery->mutex);
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300694
695 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696}
697
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400698typedef int(*print_func)(struct seq_file *seq, int result);
699typedef int(*get_func)(struct acpi_battery *battery);
700
701static struct acpi_read_mux {
702 print_func print;
703 get_func get;
704} acpi_read_funcs[ACPI_BATTERY_NUMFILES] = {
705 {.get = acpi_battery_get_info, .print = acpi_battery_print_info},
706 {.get = acpi_battery_get_state, .print = acpi_battery_print_state},
707 {.get = acpi_battery_get_alarm, .print = acpi_battery_print_alarm},
708};
709
710static int acpi_battery_read(int fid, struct seq_file *seq)
711{
712 struct acpi_battery *battery = seq->private;
713 int result = 0;
714 int update_result = ACPI_BATTERY_NONE_UPDATE;
715 int update = 0;
716
717 mutex_lock(&battery->mutex);
718
719 update = (get_seconds() - battery->update_time[fid] >= update_time);
720 update = (update | battery->flags.update[fid]);
721
722 result = acpi_battery_update(battery, update, &update_result);
723 if (result)
724 goto end;
725
726 if (update_result == ACPI_BATTERY_EASY_UPDATE) {
727 result = acpi_read_funcs[fid].get(battery);
728 if (result)
729 goto end;
730 }
731
732 end:
733 result = acpi_read_funcs[fid].print(seq, result);
734 acpi_battery_check_result(battery, result);
735 battery->flags.update[fid] = result;
736 mutex_unlock(&battery->mutex);
737 return result;
738}
739
740static int acpi_battery_read_info(struct seq_file *seq, void *offset)
741{
742 return acpi_battery_read(ACPI_BATTERY_INFO, seq);
743}
744
745static int acpi_battery_read_state(struct seq_file *seq, void *offset)
746{
747 return acpi_battery_read(ACPI_BATTERY_STATE, seq);
748}
749
750static int acpi_battery_read_alarm(struct seq_file *seq, void *offset)
751{
752 return acpi_battery_read(ACPI_BATTERY_ALARM, seq);
753}
754
755static int acpi_battery_info_open_fs(struct inode *inode, struct file *file)
756{
757 return single_open(file, acpi_battery_read_info, PDE(inode)->data);
758}
759
760static int acpi_battery_state_open_fs(struct inode *inode, struct file *file)
761{
762 return single_open(file, acpi_battery_read_state, PDE(inode)->data);
763}
764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765static int acpi_battery_alarm_open_fs(struct inode *inode, struct file *file)
766{
767 return single_open(file, acpi_battery_read_alarm, PDE(inode)->data);
768}
769
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400770static struct battery_file {
771 struct file_operations ops;
772 mode_t mode;
773 char *name;
774} acpi_battery_file[] = {
775 {
776 .name = "info",
777 .mode = S_IRUGO,
778 .ops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400779 .open = acpi_battery_info_open_fs,
780 .read = seq_read,
781 .llseek = seq_lseek,
782 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 .owner = THIS_MODULE,
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400784 },
785 },
786 {
787 .name = "state",
788 .mode = S_IRUGO,
789 .ops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400790 .open = acpi_battery_state_open_fs,
791 .read = seq_read,
792 .llseek = seq_lseek,
793 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 .owner = THIS_MODULE,
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400795 },
796 },
797 {
798 .name = "alarm",
799 .mode = S_IFREG | S_IRUGO | S_IWUSR,
800 .ops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400801 .open = acpi_battery_alarm_open_fs,
802 .read = seq_read,
803 .write = acpi_battery_write_alarm,
804 .llseek = seq_lseek,
805 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 .owner = THIS_MODULE,
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400807 },
808 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809};
810
Len Brown4be44fc2005-08-05 00:44:28 -0400811static int acpi_battery_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
Len Brown4be44fc2005-08-05 00:44:28 -0400813 struct proc_dir_entry *entry = NULL;
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400814 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 if (!acpi_device_dir(device)) {
817 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown4be44fc2005-08-05 00:44:28 -0400818 acpi_battery_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400820 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 acpi_device_dir(device)->owner = THIS_MODULE;
822 }
823
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400824 for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i) {
825 entry = create_proc_entry(acpi_battery_file[i].name,
826 acpi_battery_file[i].mode, acpi_device_dir(device));
827 if (!entry)
828 return -ENODEV;
829 else {
830 entry->proc_fops = &acpi_battery_file[i].ops;
831 entry->data = acpi_driver_data(device);
832 entry->owner = THIS_MODULE;
833 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 }
835
Patrick Mocheld550d982006-06-27 00:41:40 -0400836 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837}
838
Len Brown4be44fc2005-08-05 00:44:28 -0400839static int acpi_battery_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400841 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 if (acpi_device_dir(device)) {
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400843 for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i) {
844 remove_proc_entry(acpi_battery_file[i].name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 acpi_device_dir(device));
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 remove_proc_entry(acpi_device_bid(device), acpi_battery_dir);
848 acpi_device_dir(device) = NULL;
849 }
850
Patrick Mocheld550d982006-06-27 00:41:40 -0400851 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852}
853
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854/* --------------------------------------------------------------------------
855 Driver Interface
856 -------------------------------------------------------------------------- */
857
Len Brown4be44fc2005-08-05 00:44:28 -0400858static void acpi_battery_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200860 struct acpi_battery *battery = data;
Len Brown4be44fc2005-08-05 00:44:28 -0400861 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 if (!battery)
Patrick Mocheld550d982006-06-27 00:41:40 -0400864 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Patrick Mochel145def82006-05-19 16:54:39 -0400866 device = battery->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
868 switch (event) {
869 case ACPI_BATTERY_NOTIFY_STATUS:
870 case ACPI_BATTERY_NOTIFY_INFO:
Vladimir Lebedev9fdae722006-06-27 04:49:00 -0400871 case ACPI_NOTIFY_BUS_CHECK:
872 case ACPI_NOTIFY_DEVICE_CHECK:
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300873 device = battery->device;
874 acpi_battery_notify_update(battery);
Vladimir Lebedev9ea7d572007-02-20 15:48:06 +0300875 acpi_bus_generate_event(device, event,
876 acpi_battery_present(battery));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 break;
878 default:
879 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400880 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 break;
882 }
883
Patrick Mocheld550d982006-06-27 00:41:40 -0400884 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885}
886
Len Brown4be44fc2005-08-05 00:44:28 -0400887static int acpi_battery_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
Len Brown4be44fc2005-08-05 00:44:28 -0400889 int result = 0;
890 acpi_status status = 0;
891 struct acpi_battery *battery = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400894 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Burman Yan36bcbec2006-12-19 12:56:11 -0800896 battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 if (!battery)
Patrick Mocheld550d982006-06-27 00:41:40 -0400898 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300900 mutex_init(&battery->mutex);
901
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400902 mutex_lock(&battery->mutex);
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300903
Patrick Mochel145def82006-05-19 16:54:39 -0400904 battery->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME);
906 strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS);
907 acpi_driver_data(device) = battery;
908
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300909 result = acpi_battery_get_status(battery);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 if (result)
911 goto end;
912
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400913 battery->flags.init_update = 1;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300914
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 result = acpi_battery_add_fs(device);
916 if (result)
917 goto end;
918
Patrick Mochel3b073ec2006-05-19 16:54:41 -0400919 status = acpi_install_notify_handler(device->handle,
Vladimir Lebedev9fdae722006-06-27 04:49:00 -0400920 ACPI_ALL_NOTIFY,
Len Brown4be44fc2005-08-05 00:44:28 -0400921 acpi_battery_notify, battery);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 if (ACPI_FAILURE(status)) {
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300923 ACPI_EXCEPTION((AE_INFO, status, "Installing notify handler"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 result = -ENODEV;
925 goto end;
926 }
927
928 printk(KERN_INFO PREFIX "%s Slot [%s] (battery %s)\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400929 ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device),
930 device->status.battery_present ? "present" : "absent");
931
932 end:
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 if (result) {
935 acpi_battery_remove_fs(device);
936 kfree(battery);
937 }
938
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400939 mutex_unlock(&battery->mutex);
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300940
Patrick Mocheld550d982006-06-27 00:41:40 -0400941 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942}
943
Len Brown4be44fc2005-08-05 00:44:28 -0400944static int acpi_battery_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945{
Len Brown4be44fc2005-08-05 00:44:28 -0400946 acpi_status status = 0;
947 struct acpi_battery *battery = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400950 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200952 battery = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400954 mutex_lock(&battery->mutex);
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300955
Patrick Mochel3b073ec2006-05-19 16:54:41 -0400956 status = acpi_remove_notify_handler(device->handle,
Vladimir Lebedev9fdae722006-06-27 04:49:00 -0400957 ACPI_ALL_NOTIFY,
Len Brown4be44fc2005-08-05 00:44:28 -0400958 acpi_battery_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
960 acpi_battery_remove_fs(device);
961
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400962 kfree(battery->bif_data.pointer);
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300963
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400964 kfree(battery->bst_data.pointer);
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300965
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400966 mutex_unlock(&battery->mutex);
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300967
968 mutex_destroy(&battery->mutex);
969
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 kfree(battery);
971
Patrick Mocheld550d982006-06-27 00:41:40 -0400972 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973}
974
Jiri Kosina34c44152006-10-10 14:20:41 -0700975/* this is needed to learn about changes made in suspended state */
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800976static int acpi_battery_resume(struct acpi_device *device)
Jiri Kosina34c44152006-10-10 14:20:41 -0700977{
978 struct acpi_battery *battery;
979
980 if (!device)
981 return -EINVAL;
982
983 battery = device->driver_data;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300984
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400985 battery->flags.init_update = 1;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300986
987 return 0;
Jiri Kosina34c44152006-10-10 14:20:41 -0700988}
989
Len Brown4be44fc2005-08-05 00:44:28 -0400990static int __init acpi_battery_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991{
Rich Townsend3f86b832006-07-01 11:36:54 -0400992 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Pavel Machek4d8316d2006-08-14 22:37:22 -0700994 if (acpi_disabled)
995 return -ENODEV;
996
Rich Townsend3f86b832006-07-01 11:36:54 -0400997 acpi_battery_dir = acpi_lock_battery_dir();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 if (!acpi_battery_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -0400999 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
1001 result = acpi_bus_register_driver(&acpi_battery_driver);
1002 if (result < 0) {
Rich Townsend3f86b832006-07-01 11:36:54 -04001003 acpi_unlock_battery_dir(acpi_battery_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -04001004 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 }
1006
Patrick Mocheld550d982006-06-27 00:41:40 -04001007 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008}
1009
Len Brown4be44fc2005-08-05 00:44:28 -04001010static void __exit acpi_battery_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 acpi_bus_unregister_driver(&acpi_battery_driver);
1013
Rich Townsend3f86b832006-07-01 11:36:54 -04001014 acpi_unlock_battery_dir(acpi_battery_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Patrick Mocheld550d982006-06-27 00:41:40 -04001016 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017}
1018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019module_init(acpi_battery_init);
1020module_exit(acpi_battery_exit);