blob: d7b499fe0cd932fec5f8c6a9c7237cd38b962acc [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 Brownc2b6705b2007-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 Starikovskiy3bd92ba2007-08-03 17:38:20 -0400116enum acpi_battery_files {
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400117 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 Lebedev9ea7d572007-02-20 15:48:06 +0300132 struct acpi_device *device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 struct acpi_battery_flags flags;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300134 struct acpi_buffer bif_data;
135 struct acpi_buffer bst_data;
Alexey Starikovskiy3bd92ba2007-08-03 17:38:20 -0400136 struct mutex lock;
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];
Alexey Starikovskiy3bd92ba2007-08-03 17:38:20 -0400139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140};
141
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400142inline int acpi_battery_present(struct acpi_battery *battery)
143{
144 return battery->device->status.battery_present;
145}
146inline char *acpi_battery_power_units(struct acpi_battery *battery)
147{
148 if (battery->flags.power_unit)
149 return ACPI_BATTERY_UNITS_AMPS;
150 else
151 return ACPI_BATTERY_UNITS_WATTS;
152}
153
154inline acpi_handle acpi_battery_handle(struct acpi_battery *battery)
155{
156 return battery->device->handle;
157}
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159/* --------------------------------------------------------------------------
160 Battery Management
161 -------------------------------------------------------------------------- */
162
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300163static void acpi_battery_check_result(struct acpi_battery *battery, int result)
164{
165 if (!battery)
166 return;
167
168 if (result) {
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400169 battery->flags.init_update = 1;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300170 }
171}
172
173static int acpi_battery_extract_package(struct acpi_battery *battery,
174 union acpi_object *package,
175 struct acpi_buffer *format,
176 struct acpi_buffer *data,
177 char *package_name)
178{
179 acpi_status status = AE_OK;
180 struct acpi_buffer data_null = { 0, NULL };
181
182 status = acpi_extract_package(package, format, &data_null);
183 if (status != AE_BUFFER_OVERFLOW) {
184 ACPI_EXCEPTION((AE_INFO, status, "Extracting size %s",
185 package_name));
186 return -ENODEV;
187 }
188
189 if (data_null.length != data->length) {
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400190 kfree(data->pointer);
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300191 data->pointer = kzalloc(data_null.length, GFP_KERNEL);
192 if (!data->pointer) {
193 ACPI_EXCEPTION((AE_INFO, AE_NO_MEMORY, "kzalloc()"));
194 return -ENOMEM;
195 }
196 data->length = data_null.length;
197 }
198
199 status = acpi_extract_package(package, format, data);
200 if (ACPI_FAILURE(status)) {
201 ACPI_EXCEPTION((AE_INFO, status, "Extracting %s",
202 package_name));
203 return -ENODEV;
204 }
205
206 return 0;
207}
208
209static int acpi_battery_get_status(struct acpi_battery *battery)
210{
211 int result = 0;
212
213 result = acpi_bus_get_status(battery->device);
214 if (result) {
215 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Evaluating _STA"));
216 return -ENODEV;
217 }
218 return result;
219}
220
221static int acpi_battery_get_info(struct acpi_battery *battery)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
Len Brown4be44fc2005-08-05 00:44:28 -0400223 int result = 0;
224 acpi_status status = 0;
225 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
226 struct acpi_buffer format = { sizeof(ACPI_BATTERY_FORMAT_BIF),
227 ACPI_BATTERY_FORMAT_BIF
228 };
Len Brown4be44fc2005-08-05 00:44:28 -0400229 union acpi_object *package = NULL;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300230 struct acpi_buffer *data = NULL;
231 struct acpi_battery_info *bif = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400233 battery->update_time[ACPI_BATTERY_INFO] = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300235 if (!acpi_battery_present(battery))
236 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400238 /* Evaluate _BIF */
Alexey Starikovskiy3bd92ba2007-08-03 17:38:20 -0400239 mutex_lock(&battery->lock);
240 status = acpi_evaluate_object(acpi_battery_handle(battery), "_BIF",
241 NULL, &buffer);
242 mutex_unlock(&battery->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400244 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BIF"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400245 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 }
247
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200248 package = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300250 data = &battery->bif_data;
251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 /* Extract Package Data */
253
Vladimir Lebedev9ea7d572007-02-20 15:48:06 +0300254 result =
255 acpi_battery_extract_package(battery, package, &format, data,
256 "_BIF");
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300257 if (result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Len Brown4be44fc2005-08-05 00:44:28 -0400260 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400262 kfree(buffer.pointer);
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300263
264 if (!result) {
265 bif = data->pointer;
266 battery->flags.power_unit = bif->power_unit;
267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Patrick Mocheld550d982006-06-27 00:41:40 -0400269 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270}
271
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300272static int acpi_battery_get_state(struct acpi_battery *battery)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
Len Brown4be44fc2005-08-05 00:44:28 -0400274 int result = 0;
275 acpi_status status = 0;
276 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
277 struct acpi_buffer format = { sizeof(ACPI_BATTERY_FORMAT_BST),
278 ACPI_BATTERY_FORMAT_BST
279 };
Len Brown4be44fc2005-08-05 00:44:28 -0400280 union acpi_object *package = NULL;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300281 struct acpi_buffer *data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400283 battery->update_time[ACPI_BATTERY_STATE] = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300285 if (!acpi_battery_present(battery))
286 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400288 /* Evaluate _BST */
Alexey Starikovskiy3bd92ba2007-08-03 17:38:20 -0400289 mutex_lock(&battery->lock);
290 status = acpi_evaluate_object(acpi_battery_handle(battery), "_BST",
291 NULL, &buffer);
292 mutex_unlock(&battery->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400294 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BST"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400295 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 }
297
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200298 package = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300300 data = &battery->bst_data;
301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 /* Extract Package Data */
303
Vladimir Lebedev9ea7d572007-02-20 15:48:06 +0300304 result =
305 acpi_battery_extract_package(battery, package, &format, data,
306 "_BST");
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300307 if (result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Len Brown4be44fc2005-08-05 00:44:28 -0400310 end:
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400311 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Patrick Mocheld550d982006-06-27 00:41:40 -0400313 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314}
315
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300316static int acpi_battery_get_alarm(struct acpi_battery *battery)
317{
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400318 battery->update_time[ACPI_BATTERY_ALARM] = get_seconds();
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300319
320 return 0;
321}
322
Vladimir Lebedev9ea7d572007-02-20 15:48:06 +0300323static int acpi_battery_set_alarm(struct acpi_battery *battery,
324 unsigned long alarm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
Len Brown4be44fc2005-08-05 00:44:28 -0400326 acpi_status status = 0;
327 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
328 struct acpi_object_list arg_list = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400330 battery->update_time[ACPI_BATTERY_ALARM] = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300332 if (!acpi_battery_present(battery))
333 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400335 if (!battery->flags.alarm_present)
Patrick Mocheld550d982006-06-27 00:41:40 -0400336 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 arg0.integer.value = alarm;
339
Alexey Starikovskiy3bd92ba2007-08-03 17:38:20 -0400340 mutex_lock(&battery->lock);
341 status = acpi_evaluate_object(acpi_battery_handle(battery), "_BTP",
Vladimir Lebedev9ea7d572007-02-20 15:48:06 +0300342 &arg_list, NULL);
Alexey Starikovskiy3bd92ba2007-08-03 17:38:20 -0400343 mutex_unlock(&battery->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400345 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", (u32) alarm));
348
349 battery->alarm = alarm;
350
Patrick Mocheld550d982006-06-27 00:41:40 -0400351 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352}
353
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300354static int acpi_battery_init_alarm(struct acpi_battery *battery)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
Len Brown4be44fc2005-08-05 00:44:28 -0400356 int result = 0;
357 acpi_status status = AE_OK;
358 acpi_handle handle = NULL;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300359 struct acpi_battery_info *bif = battery->bif_data.pointer;
360 unsigned long alarm = battery->alarm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300362 /* See if alarms are supported, and if so, set default */
Len Brown4be44fc2005-08-05 00:44:28 -0400363
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300364 status = acpi_get_handle(acpi_battery_handle(battery), "_BTP", &handle);
365 if (ACPI_SUCCESS(status)) {
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400366 battery->flags.alarm_present = 1;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300367 if (!alarm && bif) {
368 alarm = bif->design_capacity_warning;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 }
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300370 result = acpi_battery_set_alarm(battery, alarm);
371 if (result)
372 goto end;
373 } else {
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400374 battery->flags.alarm_present = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 }
376
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300377 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Patrick Mocheld550d982006-06-27 00:41:40 -0400379 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380}
381
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300382static int acpi_battery_init_update(struct acpi_battery *battery)
Vladimir Lebedev4bd35cd2007-02-10 01:43:48 -0500383{
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300384 int result = 0;
385
386 result = acpi_battery_get_status(battery);
387 if (result)
388 return result;
389
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400390 battery->flags.battery_present_prev = acpi_battery_present(battery);
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300391
392 if (acpi_battery_present(battery)) {
393 result = acpi_battery_get_info(battery);
394 if (result)
395 return result;
396 result = acpi_battery_get_state(battery);
397 if (result)
398 return result;
399
400 acpi_battery_init_alarm(battery);
401 }
402
403 return result;
404}
405
406static int acpi_battery_update(struct acpi_battery *battery,
Vladimir Lebedev9ea7d572007-02-20 15:48:06 +0300407 int update, int *update_result_ptr)
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300408{
409 int result = 0;
410 int update_result = ACPI_BATTERY_NONE_UPDATE;
411
412 if (!acpi_battery_present(battery)) {
413 update = 1;
414 }
415
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400416 if (battery->flags.init_update) {
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300417 result = acpi_battery_init_update(battery);
418 if (result)
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400419 goto end;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300420 update_result = ACPI_BATTERY_INIT_UPDATE;
421 } else if (update) {
422 result = acpi_battery_get_status(battery);
423 if (result)
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400424 goto end;
425 if ((!battery->flags.battery_present_prev & acpi_battery_present(battery))
426 || (battery->flags.battery_present_prev & !acpi_battery_present(battery))) {
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300427 result = acpi_battery_init_update(battery);
428 if (result)
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400429 goto end;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300430 update_result = ACPI_BATTERY_INIT_UPDATE;
431 } else {
432 update_result = ACPI_BATTERY_EASY_UPDATE;
433 }
434 }
435
436 end:
437
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400438 battery->flags.init_update = (result != 0);
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300439
440 *update_result_ptr = update_result;
441
442 return result;
443}
444
445static void acpi_battery_notify_update(struct acpi_battery *battery)
446{
447 acpi_battery_get_status(battery);
448
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400449 if (battery->flags.init_update) {
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300450 return;
451 }
452
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400453 if ((!battery->flags.battery_present_prev &
454 acpi_battery_present(battery)) ||
455 (battery->flags.battery_present_prev &
456 !acpi_battery_present(battery))) {
457 battery->flags.init_update = 1;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300458 } else {
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400459 battery->flags.update[ACPI_BATTERY_INFO] = 1;
460 battery->flags.update[ACPI_BATTERY_STATE] = 1;
461 battery->flags.update[ACPI_BATTERY_ALARM] = 1;
Vladimir Lebedev4bd35cd2007-02-10 01:43:48 -0500462 }
463}
464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465/* --------------------------------------------------------------------------
466 FS Interface (/proc)
467 -------------------------------------------------------------------------- */
468
Len Brown4be44fc2005-08-05 00:44:28 -0400469static struct proc_dir_entry *acpi_battery_dir;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300470
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400471static int acpi_battery_print_info(struct seq_file *seq, int result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200473 struct acpi_battery *battery = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 struct acpi_battery_info *bif = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400475 char *units = "?";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300477 if (result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 goto end;
479
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300480 if (acpi_battery_present(battery))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 seq_printf(seq, "present: yes\n");
482 else {
483 seq_printf(seq, "present: no\n");
484 goto end;
485 }
486
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300487 bif = battery->bif_data.pointer;
488 if (!bif) {
489 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "BIF buffer is NULL"));
490 result = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 goto end;
492 }
493
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300494 /* Battery Units */
495
496 units = acpi_battery_power_units(battery);
Len Brown4be44fc2005-08-05 00:44:28 -0400497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 if (bif->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
499 seq_printf(seq, "design capacity: unknown\n");
500 else
501 seq_printf(seq, "design capacity: %d %sh\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400502 (u32) bif->design_capacity, units);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504 if (bif->last_full_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
505 seq_printf(seq, "last full capacity: unknown\n");
506 else
507 seq_printf(seq, "last full capacity: %d %sh\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400508 (u32) bif->last_full_capacity, units);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
510 switch ((u32) bif->battery_technology) {
511 case 0:
512 seq_printf(seq, "battery technology: non-rechargeable\n");
513 break;
514 case 1:
515 seq_printf(seq, "battery technology: rechargeable\n");
516 break;
517 default:
518 seq_printf(seq, "battery technology: unknown\n");
519 break;
520 }
521
522 if (bif->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
523 seq_printf(seq, "design voltage: unknown\n");
524 else
525 seq_printf(seq, "design voltage: %d mV\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400526 (u32) bif->design_voltage);
Len Brown4be44fc2005-08-05 00:44:28 -0400527 seq_printf(seq, "design capacity warning: %d %sh\n",
528 (u32) bif->design_capacity_warning, units);
529 seq_printf(seq, "design capacity low: %d %sh\n",
530 (u32) bif->design_capacity_low, units);
531 seq_printf(seq, "capacity granularity 1: %d %sh\n",
532 (u32) bif->battery_capacity_granularity_1, units);
533 seq_printf(seq, "capacity granularity 2: %d %sh\n",
534 (u32) bif->battery_capacity_granularity_2, units);
535 seq_printf(seq, "model number: %s\n", bif->model_number);
536 seq_printf(seq, "serial number: %s\n", bif->serial_number);
537 seq_printf(seq, "battery type: %s\n", bif->battery_type);
538 seq_printf(seq, "OEM info: %s\n", bif->oem_info);
539
540 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300542 if (result)
543 seq_printf(seq, "ERROR: Unable to read battery info\n");
544
545 return result;
546}
547
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400548static int acpi_battery_print_state(struct seq_file *seq, int result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200550 struct acpi_battery *battery = seq->private;
Vladimir Lebedeva1f0eff2007-02-20 15:48:06 +0300551 struct acpi_battery_state *bst = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400552 char *units = "?";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300554 if (result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 goto end;
556
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300557 if (acpi_battery_present(battery))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 seq_printf(seq, "present: yes\n");
559 else {
560 seq_printf(seq, "present: no\n");
561 goto end;
562 }
563
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300564 bst = battery->bst_data.pointer;
565 if (!bst) {
566 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "BST buffer is NULL"));
567 result = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 goto end;
569 }
570
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300571 /* Battery Units */
572
573 units = acpi_battery_power_units(battery);
574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 if (!(bst->state & 0x04))
576 seq_printf(seq, "capacity state: ok\n");
577 else
578 seq_printf(seq, "capacity state: critical\n");
579
Len Brown4be44fc2005-08-05 00:44:28 -0400580 if ((bst->state & 0x01) && (bst->state & 0x02)) {
581 seq_printf(seq,
582 "charging state: charging/discharging\n");
Len Brown4be44fc2005-08-05 00:44:28 -0400583 } else if (bst->state & 0x01)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 seq_printf(seq, "charging state: discharging\n");
585 else if (bst->state & 0x02)
586 seq_printf(seq, "charging state: charging\n");
587 else {
588 seq_printf(seq, "charging state: charged\n");
589 }
590
591 if (bst->present_rate == ACPI_BATTERY_VALUE_UNKNOWN)
592 seq_printf(seq, "present rate: unknown\n");
593 else
594 seq_printf(seq, "present rate: %d %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400595 (u32) bst->present_rate, units);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597 if (bst->remaining_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
598 seq_printf(seq, "remaining capacity: unknown\n");
599 else
600 seq_printf(seq, "remaining capacity: %d %sh\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400601 (u32) bst->remaining_capacity, units);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603 if (bst->present_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
604 seq_printf(seq, "present voltage: unknown\n");
605 else
606 seq_printf(seq, "present voltage: %d mV\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400607 (u32) bst->present_voltage);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Len Brown4be44fc2005-08-05 00:44:28 -0400609 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300611 if (result) {
612 seq_printf(seq, "ERROR: Unable to read battery state\n");
613 }
614
615 return result;
616}
617
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400618static int acpi_battery_print_alarm(struct seq_file *seq, int result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200620 struct acpi_battery *battery = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -0400621 char *units = "?";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300623 if (result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 goto end;
625
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300626 if (!acpi_battery_present(battery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 seq_printf(seq, "present: no\n");
628 goto end;
629 }
630
631 /* Battery Units */
Len Brown4be44fc2005-08-05 00:44:28 -0400632
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300633 units = acpi_battery_power_units(battery);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635 seq_printf(seq, "alarm: ");
636 if (!battery->alarm)
637 seq_printf(seq, "unsupported\n");
638 else
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300639 seq_printf(seq, "%lu %sh\n", battery->alarm, units);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Len Brown4be44fc2005-08-05 00:44:28 -0400641 end:
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300642
643 if (result)
644 seq_printf(seq, "ERROR: Unable to read battery alarm\n");
645
646 return result;
647}
648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -0400650acpi_battery_write_alarm(struct file *file,
651 const char __user * buffer,
652 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
Len Brown4be44fc2005-08-05 00:44:28 -0400654 int result = 0;
655 char alarm_string[12] = { '\0' };
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200656 struct seq_file *m = file->private_data;
657 struct acpi_battery *battery = m->private;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300658 int update_result = ACPI_BATTERY_NONE_UPDATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 if (!battery || (count > sizeof(alarm_string) - 1))
Patrick Mocheld550d982006-06-27 00:41:40 -0400661 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
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)
Alexey Starikovskiy3bd92ba2007-08-03 17:38:20 -0400691 return count;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300692
693 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694}
695
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400696typedef int(*print_func)(struct seq_file *seq, int result);
697typedef int(*get_func)(struct acpi_battery *battery);
698
699static struct acpi_read_mux {
700 print_func print;
701 get_func get;
702} acpi_read_funcs[ACPI_BATTERY_NUMFILES] = {
703 {.get = acpi_battery_get_info, .print = acpi_battery_print_info},
704 {.get = acpi_battery_get_state, .print = acpi_battery_print_state},
705 {.get = acpi_battery_get_alarm, .print = acpi_battery_print_alarm},
706};
707
708static int acpi_battery_read(int fid, struct seq_file *seq)
709{
710 struct acpi_battery *battery = seq->private;
711 int result = 0;
712 int update_result = ACPI_BATTERY_NONE_UPDATE;
713 int update = 0;
714
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400715 update = (get_seconds() - battery->update_time[fid] >= update_time);
716 update = (update | battery->flags.update[fid]);
717
718 result = acpi_battery_update(battery, update, &update_result);
719 if (result)
720 goto end;
721
722 if (update_result == ACPI_BATTERY_EASY_UPDATE) {
723 result = acpi_read_funcs[fid].get(battery);
724 if (result)
725 goto end;
726 }
727
728 end:
729 result = acpi_read_funcs[fid].print(seq, result);
730 acpi_battery_check_result(battery, result);
731 battery->flags.update[fid] = result;
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400732 return result;
733}
734
735static int acpi_battery_read_info(struct seq_file *seq, void *offset)
736{
737 return acpi_battery_read(ACPI_BATTERY_INFO, seq);
738}
739
740static int acpi_battery_read_state(struct seq_file *seq, void *offset)
741{
742 return acpi_battery_read(ACPI_BATTERY_STATE, seq);
743}
744
745static int acpi_battery_read_alarm(struct seq_file *seq, void *offset)
746{
747 return acpi_battery_read(ACPI_BATTERY_ALARM, seq);
748}
749
750static int acpi_battery_info_open_fs(struct inode *inode, struct file *file)
751{
752 return single_open(file, acpi_battery_read_info, PDE(inode)->data);
753}
754
755static int acpi_battery_state_open_fs(struct inode *inode, struct file *file)
756{
757 return single_open(file, acpi_battery_read_state, PDE(inode)->data);
758}
759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760static int acpi_battery_alarm_open_fs(struct inode *inode, struct file *file)
761{
762 return single_open(file, acpi_battery_read_alarm, PDE(inode)->data);
763}
764
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400765static struct battery_file {
766 struct file_operations ops;
767 mode_t mode;
768 char *name;
769} acpi_battery_file[] = {
770 {
771 .name = "info",
772 .mode = S_IRUGO,
773 .ops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400774 .open = acpi_battery_info_open_fs,
775 .read = seq_read,
776 .llseek = seq_lseek,
777 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 .owner = THIS_MODULE,
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400779 },
780 },
781 {
782 .name = "state",
783 .mode = S_IRUGO,
784 .ops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400785 .open = acpi_battery_state_open_fs,
786 .read = seq_read,
787 .llseek = seq_lseek,
788 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 .owner = THIS_MODULE,
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400790 },
791 },
792 {
793 .name = "alarm",
794 .mode = S_IFREG | S_IRUGO | S_IWUSR,
795 .ops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400796 .open = acpi_battery_alarm_open_fs,
797 .read = seq_read,
798 .write = acpi_battery_write_alarm,
799 .llseek = seq_lseek,
800 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 .owner = THIS_MODULE,
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400802 },
803 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804};
805
Len Brown4be44fc2005-08-05 00:44:28 -0400806static int acpi_battery_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
Len Brown4be44fc2005-08-05 00:44:28 -0400808 struct proc_dir_entry *entry = NULL;
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400809 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 if (!acpi_device_dir(device)) {
812 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown4be44fc2005-08-05 00:44:28 -0400813 acpi_battery_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400815 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 acpi_device_dir(device)->owner = THIS_MODULE;
817 }
818
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400819 for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i) {
820 entry = create_proc_entry(acpi_battery_file[i].name,
821 acpi_battery_file[i].mode, acpi_device_dir(device));
822 if (!entry)
823 return -ENODEV;
824 else {
825 entry->proc_fops = &acpi_battery_file[i].ops;
826 entry->data = acpi_driver_data(device);
827 entry->owner = THIS_MODULE;
828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 }
830
Patrick Mocheld550d982006-06-27 00:41:40 -0400831 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832}
833
Len Brown4be44fc2005-08-05 00:44:28 -0400834static int acpi_battery_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400836 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 if (acpi_device_dir(device)) {
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400838 for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i) {
839 remove_proc_entry(acpi_battery_file[i].name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 acpi_device_dir(device));
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400841 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 remove_proc_entry(acpi_device_bid(device), acpi_battery_dir);
843 acpi_device_dir(device) = NULL;
844 }
845
Patrick Mocheld550d982006-06-27 00:41:40 -0400846 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847}
848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849/* --------------------------------------------------------------------------
850 Driver Interface
851 -------------------------------------------------------------------------- */
852
Len Brown4be44fc2005-08-05 00:44:28 -0400853static void acpi_battery_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200855 struct acpi_battery *battery = data;
Len Brown4be44fc2005-08-05 00:44:28 -0400856 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 if (!battery)
Patrick Mocheld550d982006-06-27 00:41:40 -0400859 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Patrick Mochel145def82006-05-19 16:54:39 -0400861 device = battery->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
863 switch (event) {
864 case ACPI_BATTERY_NOTIFY_STATUS:
865 case ACPI_BATTERY_NOTIFY_INFO:
Vladimir Lebedev9fdae722006-06-27 04:49:00 -0400866 case ACPI_NOTIFY_BUS_CHECK:
867 case ACPI_NOTIFY_DEVICE_CHECK:
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300868 device = battery->device;
869 acpi_battery_notify_update(battery);
Vladimir Lebedev9ea7d572007-02-20 15:48:06 +0300870 acpi_bus_generate_event(device, event,
871 acpi_battery_present(battery));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 break;
873 default:
874 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400875 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 break;
877 }
878
Patrick Mocheld550d982006-06-27 00:41:40 -0400879 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880}
881
Len Brown4be44fc2005-08-05 00:44:28 -0400882static int acpi_battery_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883{
Len Brown4be44fc2005-08-05 00:44:28 -0400884 int result = 0;
885 acpi_status status = 0;
886 struct acpi_battery *battery = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400889 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Burman Yan36bcbec2006-12-19 12:56:11 -0800891 battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 if (!battery)
Patrick Mocheld550d982006-06-27 00:41:40 -0400893 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Alexey Starikovskiy3bd92ba2007-08-03 17:38:20 -0400895 mutex_init(&battery->lock);
Patrick Mochel145def82006-05-19 16:54:39 -0400896 battery->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME);
898 strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS);
899 acpi_driver_data(device) = battery;
900
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300901 result = acpi_battery_get_status(battery);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 if (result)
903 goto end;
904
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400905 battery->flags.init_update = 1;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 result = acpi_battery_add_fs(device);
908 if (result)
909 goto end;
910
Patrick Mochel3b073ec2006-05-19 16:54:41 -0400911 status = acpi_install_notify_handler(device->handle,
Vladimir Lebedev9fdae722006-06-27 04:49:00 -0400912 ACPI_ALL_NOTIFY,
Len Brown4be44fc2005-08-05 00:44:28 -0400913 acpi_battery_notify, battery);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 if (ACPI_FAILURE(status)) {
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300915 ACPI_EXCEPTION((AE_INFO, status, "Installing notify handler"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 result = -ENODEV;
917 goto end;
918 }
919
920 printk(KERN_INFO PREFIX "%s Slot [%s] (battery %s)\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400921 ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device),
922 device->status.battery_present ? "present" : "absent");
923
924 end:
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 if (result) {
927 acpi_battery_remove_fs(device);
928 kfree(battery);
929 }
930
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300931
Patrick Mocheld550d982006-06-27 00:41:40 -0400932 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933}
934
Len Brown4be44fc2005-08-05 00:44:28 -0400935static int acpi_battery_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936{
Len Brown4be44fc2005-08-05 00:44:28 -0400937 acpi_status status = 0;
938 struct acpi_battery *battery = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400941 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200943 battery = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Patrick Mochel3b073ec2006-05-19 16:54:41 -0400945 status = acpi_remove_notify_handler(device->handle,
Vladimir Lebedev9fdae722006-06-27 04:49:00 -0400946 ACPI_ALL_NOTIFY,
Len Brown4be44fc2005-08-05 00:44:28 -0400947 acpi_battery_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
949 acpi_battery_remove_fs(device);
950
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400951 kfree(battery->bif_data.pointer);
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300952
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400953 kfree(battery->bst_data.pointer);
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300954
Alexey Starikovskiy3bd92ba2007-08-03 17:38:20 -0400955 mutex_destroy(&battery->lock);
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300956
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 kfree(battery);
958
Patrick Mocheld550d982006-06-27 00:41:40 -0400959 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960}
961
Jiri Kosina34c44152006-10-10 14:20:41 -0700962/* this is needed to learn about changes made in suspended state */
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800963static int acpi_battery_resume(struct acpi_device *device)
Jiri Kosina34c44152006-10-10 14:20:41 -0700964{
965 struct acpi_battery *battery;
966
967 if (!device)
968 return -EINVAL;
969
970 battery = device->driver_data;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300971
Alexey Starikovskiy78490d82007-05-11 13:18:55 -0400972 battery->flags.init_update = 1;
Vladimir Lebedevb6ce4082007-02-20 15:48:06 +0300973
974 return 0;
Jiri Kosina34c44152006-10-10 14:20:41 -0700975}
976
Len Brown4be44fc2005-08-05 00:44:28 -0400977static int __init acpi_battery_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978{
Rich Townsend3f86b832006-07-01 11:36:54 -0400979 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Pavel Machek4d8316d2006-08-14 22:37:22 -0700981 if (acpi_disabled)
982 return -ENODEV;
983
Rich Townsend3f86b832006-07-01 11:36:54 -0400984 acpi_battery_dir = acpi_lock_battery_dir();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 if (!acpi_battery_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -0400986 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
988 result = acpi_bus_register_driver(&acpi_battery_driver);
989 if (result < 0) {
Rich Townsend3f86b832006-07-01 11:36:54 -0400990 acpi_unlock_battery_dir(acpi_battery_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -0400991 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 }
993
Patrick Mocheld550d982006-06-27 00:41:40 -0400994 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995}
996
Len Brown4be44fc2005-08-05 00:44:28 -0400997static void __exit acpi_battery_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 acpi_bus_unregister_driver(&acpi_battery_driver);
1000
Rich Townsend3f86b832006-07-01 11:36:54 -04001001 acpi_unlock_battery_dir(acpi_battery_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Patrick Mocheld550d982006-06-27 00:41:40 -04001003 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004}
1005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006module_init(acpi_battery_init);
1007module_exit(acpi_battery_exit);