blob: e64c76c8b7268a56562969d48b6c940bbda09bfc [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"
44#define ACPI_BATTERY_HID "PNP0C0A"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#define ACPI_BATTERY_DEVICE_NAME "Battery"
46#define ACPI_BATTERY_FILE_INFO "info"
47#define ACPI_BATTERY_FILE_STATUS "state"
48#define ACPI_BATTERY_FILE_ALARM "alarm"
49#define ACPI_BATTERY_NOTIFY_STATUS 0x80
50#define ACPI_BATTERY_NOTIFY_INFO 0x81
51#define ACPI_BATTERY_UNITS_WATTS "mW"
52#define ACPI_BATTERY_UNITS_AMPS "mA"
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#define _COMPONENT ACPI_BATTERY_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050055ACPI_MODULE_NAME("battery");
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Len Brownf52fd662007-02-12 22:42:12 -050057MODULE_AUTHOR("Paul Diefenbaugh");
Len Brown7cda93e2007-02-12 23:50:02 -050058MODULE_DESCRIPTION("ACPI Battery Driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070059MODULE_LICENSE("GPL");
60
Rich Townsend3f86b832006-07-01 11:36:54 -040061extern struct proc_dir_entry *acpi_lock_battery_dir(void);
62extern void *acpi_unlock_battery_dir(struct proc_dir_entry *acpi_battery_dir);
63
Len Brown4be44fc2005-08-05 00:44:28 -040064static int acpi_battery_add(struct acpi_device *device);
65static int acpi_battery_remove(struct acpi_device *device, int type);
Patrick Mochel5d9464a2006-12-07 20:56:27 +080066static int acpi_battery_resume(struct acpi_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68static struct acpi_driver acpi_battery_driver = {
Len Brownc2b67052007-02-12 23:33:40 -050069 .name = "battery",
Len Brown4be44fc2005-08-05 00:44:28 -040070 .class = ACPI_BATTERY_CLASS,
71 .ids = ACPI_BATTERY_HID,
72 .ops = {
73 .add = acpi_battery_add,
Jiri Kosina34c44152006-10-10 14:20:41 -070074 .resume = acpi_battery_resume,
Len Brown4be44fc2005-08-05 00:44:28 -040075 .remove = acpi_battery_remove,
76 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070077};
78
79struct acpi_battery_status {
Len Brown4be44fc2005-08-05 00:44:28 -040080 acpi_integer state;
81 acpi_integer present_rate;
82 acpi_integer remaining_capacity;
83 acpi_integer present_voltage;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084};
85
86struct acpi_battery_info {
Len Brown4be44fc2005-08-05 00:44:28 -040087 acpi_integer power_unit;
88 acpi_integer design_capacity;
89 acpi_integer last_full_capacity;
90 acpi_integer battery_technology;
91 acpi_integer design_voltage;
92 acpi_integer design_capacity_warning;
93 acpi_integer design_capacity_low;
94 acpi_integer battery_capacity_granularity_1;
95 acpi_integer battery_capacity_granularity_2;
96 acpi_string model_number;
97 acpi_string serial_number;
98 acpi_string battery_type;
99 acpi_string oem_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100};
101
102struct acpi_battery_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400103 u8 present:1; /* Bay occupied? */
104 u8 power_unit:1; /* 0=watts, 1=apms */
105 u8 alarm:1; /* _BTP present? */
106 u8 reserved:5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107};
108
109struct acpi_battery_trips {
Len Brown4be44fc2005-08-05 00:44:28 -0400110 unsigned long warning;
111 unsigned long low;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112};
113
114struct acpi_battery {
Patrick Mochel145def82006-05-19 16:54:39 -0400115 struct acpi_device * device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 struct acpi_battery_flags flags;
117 struct acpi_battery_trips trips;
Len Brown4be44fc2005-08-05 00:44:28 -0400118 unsigned long alarm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 struct acpi_battery_info *info;
120};
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122/* --------------------------------------------------------------------------
123 Battery Management
124 -------------------------------------------------------------------------- */
125
126static int
Len Brown4be44fc2005-08-05 00:44:28 -0400127acpi_battery_get_info(struct acpi_battery *battery,
128 struct acpi_battery_info **bif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
Len Brown4be44fc2005-08-05 00:44:28 -0400130 int result = 0;
131 acpi_status status = 0;
132 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
133 struct acpi_buffer format = { sizeof(ACPI_BATTERY_FORMAT_BIF),
134 ACPI_BATTERY_FORMAT_BIF
135 };
136 struct acpi_buffer data = { 0, NULL };
137 union acpi_object *package = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140 if (!battery || !bif)
Patrick Mocheld550d982006-06-27 00:41:40 -0400141 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143 /* Evalute _BIF */
144
Patrick Mochel3b073ec2006-05-19 16:54:41 -0400145 status = acpi_evaluate_object(battery->device->handle, "_BIF", NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400147 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BIF"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400148 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 }
150
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200151 package = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153 /* Extract Package Data */
154
155 status = acpi_extract_package(package, &format, &data);
156 if (status != AE_BUFFER_OVERFLOW) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400157 ACPI_EXCEPTION((AE_INFO, status, "Extracting _BIF"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 result = -ENODEV;
159 goto end;
160 }
161
Burman Yan36bcbec2006-12-19 12:56:11 -0800162 data.pointer = kzalloc(data.length, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 if (!data.pointer) {
164 result = -ENOMEM;
165 goto end;
166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168 status = acpi_extract_package(package, &format, &data);
169 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400170 ACPI_EXCEPTION((AE_INFO, status, "Extracting _BIF"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 kfree(data.pointer);
172 result = -ENODEV;
173 goto end;
174 }
175
Len Brown4be44fc2005-08-05 00:44:28 -0400176 end:
Len Brown02438d82006-06-30 03:19:10 -0400177 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179 if (!result)
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200180 (*bif) = data.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Patrick Mocheld550d982006-06-27 00:41:40 -0400182 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183}
184
185static int
Len Brown4be44fc2005-08-05 00:44:28 -0400186acpi_battery_get_status(struct acpi_battery *battery,
187 struct acpi_battery_status **bst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
Len Brown4be44fc2005-08-05 00:44:28 -0400189 int result = 0;
190 acpi_status status = 0;
191 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
192 struct acpi_buffer format = { sizeof(ACPI_BATTERY_FORMAT_BST),
193 ACPI_BATTERY_FORMAT_BST
194 };
195 struct acpi_buffer data = { 0, NULL };
196 union acpi_object *package = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 if (!battery || !bst)
Patrick Mocheld550d982006-06-27 00:41:40 -0400200 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 /* Evalute _BST */
203
Patrick Mochel3b073ec2006-05-19 16:54:41 -0400204 status = acpi_evaluate_object(battery->device->handle, "_BST", NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400206 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BST"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400207 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 }
209
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200210 package = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212 /* Extract Package Data */
213
214 status = acpi_extract_package(package, &format, &data);
215 if (status != AE_BUFFER_OVERFLOW) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400216 ACPI_EXCEPTION((AE_INFO, status, "Extracting _BST"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 result = -ENODEV;
218 goto end;
219 }
220
Burman Yan36bcbec2006-12-19 12:56:11 -0800221 data.pointer = kzalloc(data.length, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 if (!data.pointer) {
223 result = -ENOMEM;
224 goto end;
225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 status = acpi_extract_package(package, &format, &data);
228 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400229 ACPI_EXCEPTION((AE_INFO, status, "Extracting _BST"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 kfree(data.pointer);
231 result = -ENODEV;
232 goto end;
233 }
234
Len Brown4be44fc2005-08-05 00:44:28 -0400235 end:
Len Brown02438d82006-06-30 03:19:10 -0400236 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238 if (!result)
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200239 (*bst) = data.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Patrick Mocheld550d982006-06-27 00:41:40 -0400241 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242}
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244static int
Len Brown4be44fc2005-08-05 00:44:28 -0400245acpi_battery_set_alarm(struct acpi_battery *battery, unsigned long alarm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
Len Brown4be44fc2005-08-05 00:44:28 -0400247 acpi_status status = 0;
248 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
249 struct acpi_object_list arg_list = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 if (!battery)
Patrick Mocheld550d982006-06-27 00:41:40 -0400253 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 if (!battery->flags.alarm)
Patrick Mocheld550d982006-06-27 00:41:40 -0400256 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 arg0.integer.value = alarm;
259
Patrick Mochel3b073ec2006-05-19 16:54:41 -0400260 status = acpi_evaluate_object(battery->device->handle, "_BTP", &arg_list, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400262 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", (u32) alarm));
265
266 battery->alarm = alarm;
267
Patrick Mocheld550d982006-06-27 00:41:40 -0400268 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269}
270
Len Brown4be44fc2005-08-05 00:44:28 -0400271static int acpi_battery_check(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 = AE_OK;
275 acpi_handle handle = NULL;
276 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 struct acpi_battery_info *bif = NULL;
278
Len Brown4be44fc2005-08-05 00:44:28 -0400279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 if (!battery)
Patrick Mocheld550d982006-06-27 00:41:40 -0400281 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Patrick Mochel145def82006-05-19 16:54:39 -0400283 device = battery->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 result = acpi_bus_get_status(device);
286 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400287 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289 /* Insertion? */
290
291 if (!battery->flags.present && device->status.battery_present) {
292
293 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Battery inserted\n"));
294
295 /* Evalute _BIF to get certain static information */
296
297 result = acpi_battery_get_info(battery, &bif);
298 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400299 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 battery->flags.power_unit = bif->power_unit;
302 battery->trips.warning = bif->design_capacity_warning;
303 battery->trips.low = bif->design_capacity_low;
304 kfree(bif);
305
306 /* See if alarms are supported, and if so, set default */
307
Patrick Mochel3b073ec2006-05-19 16:54:41 -0400308 status = acpi_get_handle(battery->device->handle, "_BTP", &handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 if (ACPI_SUCCESS(status)) {
310 battery->flags.alarm = 1;
311 acpi_battery_set_alarm(battery, battery->trips.warning);
312 }
313 }
314
315 /* Removal? */
316
317 else if (battery->flags.present && !device->status.battery_present) {
318 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Battery removed\n"));
319 }
320
321 battery->flags.present = device->status.battery_present;
322
Patrick Mocheld550d982006-06-27 00:41:40 -0400323 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324}
325
Vladimir Lebedev4bd35cd2007-02-10 01:43:48 -0500326static void acpi_battery_check_present(struct acpi_battery *battery)
327{
328 if (!battery->flags.present) {
329 acpi_battery_check(battery);
330 }
331}
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333/* --------------------------------------------------------------------------
334 FS Interface (/proc)
335 -------------------------------------------------------------------------- */
336
Len Brown4be44fc2005-08-05 00:44:28 -0400337static struct proc_dir_entry *acpi_battery_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338static int acpi_battery_read_info(struct seq_file *seq, void *offset)
339{
Len Brown4be44fc2005-08-05 00:44:28 -0400340 int result = 0;
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200341 struct acpi_battery *battery = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 struct acpi_battery_info *bif = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400343 char *units = "?";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 if (!battery)
347 goto end;
348
Vladimir Lebedev4bd35cd2007-02-10 01:43:48 -0500349 acpi_battery_check_present(battery);
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 if (battery->flags.present)
352 seq_printf(seq, "present: yes\n");
353 else {
354 seq_printf(seq, "present: no\n");
355 goto end;
356 }
357
358 /* Battery Info (_BIF) */
359
360 result = acpi_battery_get_info(battery, &bif);
361 if (result || !bif) {
362 seq_printf(seq, "ERROR: Unable to read battery information\n");
363 goto end;
364 }
365
Len Brown4be44fc2005-08-05 00:44:28 -0400366 units =
367 bif->
368 power_unit ? ACPI_BATTERY_UNITS_AMPS : ACPI_BATTERY_UNITS_WATTS;
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 if (bif->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
371 seq_printf(seq, "design capacity: unknown\n");
372 else
373 seq_printf(seq, "design capacity: %d %sh\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400374 (u32) bif->design_capacity, units);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 if (bif->last_full_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
377 seq_printf(seq, "last full capacity: unknown\n");
378 else
379 seq_printf(seq, "last full capacity: %d %sh\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400380 (u32) bif->last_full_capacity, units);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382 switch ((u32) bif->battery_technology) {
383 case 0:
384 seq_printf(seq, "battery technology: non-rechargeable\n");
385 break;
386 case 1:
387 seq_printf(seq, "battery technology: rechargeable\n");
388 break;
389 default:
390 seq_printf(seq, "battery technology: unknown\n");
391 break;
392 }
393
394 if (bif->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
395 seq_printf(seq, "design voltage: unknown\n");
396 else
397 seq_printf(seq, "design voltage: %d mV\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400398 (u32) bif->design_voltage);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Len Brown4be44fc2005-08-05 00:44:28 -0400400 seq_printf(seq, "design capacity warning: %d %sh\n",
401 (u32) bif->design_capacity_warning, units);
402 seq_printf(seq, "design capacity low: %d %sh\n",
403 (u32) bif->design_capacity_low, units);
404 seq_printf(seq, "capacity granularity 1: %d %sh\n",
405 (u32) bif->battery_capacity_granularity_1, units);
406 seq_printf(seq, "capacity granularity 2: %d %sh\n",
407 (u32) bif->battery_capacity_granularity_2, units);
408 seq_printf(seq, "model number: %s\n", bif->model_number);
409 seq_printf(seq, "serial number: %s\n", bif->serial_number);
410 seq_printf(seq, "battery type: %s\n", bif->battery_type);
411 seq_printf(seq, "OEM info: %s\n", bif->oem_info);
412
413 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 kfree(bif);
415
Patrick Mocheld550d982006-06-27 00:41:40 -0400416 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417}
418
419static int acpi_battery_info_open_fs(struct inode *inode, struct file *file)
420{
421 return single_open(file, acpi_battery_read_info, PDE(inode)->data);
422}
423
Len Brown4be44fc2005-08-05 00:44:28 -0400424static int acpi_battery_read_state(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
Len Brown4be44fc2005-08-05 00:44:28 -0400426 int result = 0;
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200427 struct acpi_battery *battery = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 struct acpi_battery_status *bst = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400429 char *units = "?";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 if (!battery)
433 goto end;
434
Vladimir Lebedev4bd35cd2007-02-10 01:43:48 -0500435 acpi_battery_check_present(battery);
436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 if (battery->flags.present)
438 seq_printf(seq, "present: yes\n");
439 else {
440 seq_printf(seq, "present: no\n");
441 goto end;
442 }
443
444 /* Battery Units */
445
Len Brown4be44fc2005-08-05 00:44:28 -0400446 units =
447 battery->flags.
448 power_unit ? ACPI_BATTERY_UNITS_AMPS : ACPI_BATTERY_UNITS_WATTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450 /* Battery Status (_BST) */
451
452 result = acpi_battery_get_status(battery, &bst);
453 if (result || !bst) {
454 seq_printf(seq, "ERROR: Unable to read battery status\n");
455 goto end;
456 }
457
458 if (!(bst->state & 0x04))
459 seq_printf(seq, "capacity state: ok\n");
460 else
461 seq_printf(seq, "capacity state: critical\n");
462
Len Brown4be44fc2005-08-05 00:44:28 -0400463 if ((bst->state & 0x01) && (bst->state & 0x02)) {
464 seq_printf(seq,
465 "charging state: charging/discharging\n");
Len Brown4be44fc2005-08-05 00:44:28 -0400466 } else if (bst->state & 0x01)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 seq_printf(seq, "charging state: discharging\n");
468 else if (bst->state & 0x02)
469 seq_printf(seq, "charging state: charging\n");
470 else {
471 seq_printf(seq, "charging state: charged\n");
472 }
473
474 if (bst->present_rate == ACPI_BATTERY_VALUE_UNKNOWN)
475 seq_printf(seq, "present rate: unknown\n");
476 else
477 seq_printf(seq, "present rate: %d %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400478 (u32) bst->present_rate, units);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
480 if (bst->remaining_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
481 seq_printf(seq, "remaining capacity: unknown\n");
482 else
483 seq_printf(seq, "remaining capacity: %d %sh\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400484 (u32) bst->remaining_capacity, units);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 if (bst->present_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
487 seq_printf(seq, "present voltage: unknown\n");
488 else
489 seq_printf(seq, "present voltage: %d mV\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400490 (u32) bst->present_voltage);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Len Brown4be44fc2005-08-05 00:44:28 -0400492 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 kfree(bst);
494
Patrick Mocheld550d982006-06-27 00:41:40 -0400495 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496}
497
498static int acpi_battery_state_open_fs(struct inode *inode, struct file *file)
499{
500 return single_open(file, acpi_battery_read_state, PDE(inode)->data);
501}
502
Len Brown4be44fc2005-08-05 00:44:28 -0400503static int acpi_battery_read_alarm(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200505 struct acpi_battery *battery = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -0400506 char *units = "?";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509 if (!battery)
510 goto end;
511
Vladimir Lebedev4bd35cd2007-02-10 01:43:48 -0500512 acpi_battery_check_present(battery);
513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 if (!battery->flags.present) {
515 seq_printf(seq, "present: no\n");
516 goto end;
517 }
518
519 /* Battery Units */
Len Brown4be44fc2005-08-05 00:44:28 -0400520
521 units =
522 battery->flags.
523 power_unit ? ACPI_BATTERY_UNITS_AMPS : ACPI_BATTERY_UNITS_WATTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525 /* Battery Alarm */
526
527 seq_printf(seq, "alarm: ");
528 if (!battery->alarm)
529 seq_printf(seq, "unsupported\n");
530 else
531 seq_printf(seq, "%d %sh\n", (u32) battery->alarm, units);
532
Len Brown4be44fc2005-08-05 00:44:28 -0400533 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400534 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535}
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -0400538acpi_battery_write_alarm(struct file *file,
539 const char __user * buffer,
540 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
Len Brown4be44fc2005-08-05 00:44:28 -0400542 int result = 0;
543 char alarm_string[12] = { '\0' };
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200544 struct seq_file *m = file->private_data;
545 struct acpi_battery *battery = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548 if (!battery || (count > sizeof(alarm_string) - 1))
Patrick Mocheld550d982006-06-27 00:41:40 -0400549 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Vladimir Lebedev4bd35cd2007-02-10 01:43:48 -0500551 acpi_battery_check_present(battery);
552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 if (!battery->flags.present)
Patrick Mocheld550d982006-06-27 00:41:40 -0400554 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556 if (copy_from_user(alarm_string, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -0400557 return -EFAULT;
Len Brown4be44fc2005-08-05 00:44:28 -0400558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 alarm_string[count] = '\0';
560
Len Brown4be44fc2005-08-05 00:44:28 -0400561 result = acpi_battery_set_alarm(battery,
562 simple_strtoul(alarm_string, NULL, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400564 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Patrick Mocheld550d982006-06-27 00:41:40 -0400566 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567}
568
569static int acpi_battery_alarm_open_fs(struct inode *inode, struct file *file)
570{
571 return single_open(file, acpi_battery_read_alarm, PDE(inode)->data);
572}
573
Arjan van de Vend7508032006-07-04 13:06:00 -0400574static const struct file_operations acpi_battery_info_ops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400575 .open = acpi_battery_info_open_fs,
576 .read = seq_read,
577 .llseek = seq_lseek,
578 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 .owner = THIS_MODULE,
580};
581
Arjan van de Vend7508032006-07-04 13:06:00 -0400582static const struct file_operations acpi_battery_state_ops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400583 .open = acpi_battery_state_open_fs,
584 .read = seq_read,
585 .llseek = seq_lseek,
586 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 .owner = THIS_MODULE,
588};
589
Arjan van de Vend7508032006-07-04 13:06:00 -0400590static const struct file_operations acpi_battery_alarm_ops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400591 .open = acpi_battery_alarm_open_fs,
592 .read = seq_read,
593 .write = acpi_battery_write_alarm,
594 .llseek = seq_lseek,
595 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 .owner = THIS_MODULE,
597};
598
Len Brown4be44fc2005-08-05 00:44:28 -0400599static int acpi_battery_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
Len Brown4be44fc2005-08-05 00:44:28 -0400601 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 if (!acpi_device_dir(device)) {
605 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown4be44fc2005-08-05 00:44:28 -0400606 acpi_battery_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400608 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 acpi_device_dir(device)->owner = THIS_MODULE;
610 }
611
612 /* 'info' [R] */
613 entry = create_proc_entry(ACPI_BATTERY_FILE_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400614 S_IRUGO, acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400616 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 else {
Len Brown4be44fc2005-08-05 00:44:28 -0400618 entry->proc_fops = &acpi_battery_info_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 entry->data = acpi_driver_data(device);
620 entry->owner = THIS_MODULE;
621 }
622
623 /* 'status' [R] */
624 entry = create_proc_entry(ACPI_BATTERY_FILE_STATUS,
Len Brown4be44fc2005-08-05 00:44:28 -0400625 S_IRUGO, acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400627 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 else {
629 entry->proc_fops = &acpi_battery_state_ops;
630 entry->data = acpi_driver_data(device);
631 entry->owner = THIS_MODULE;
632 }
633
634 /* 'alarm' [R/W] */
635 entry = create_proc_entry(ACPI_BATTERY_FILE_ALARM,
Len Brown4be44fc2005-08-05 00:44:28 -0400636 S_IFREG | S_IRUGO | S_IWUSR,
637 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400639 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 else {
641 entry->proc_fops = &acpi_battery_alarm_ops;
642 entry->data = acpi_driver_data(device);
643 entry->owner = THIS_MODULE;
644 }
645
Patrick Mocheld550d982006-06-27 00:41:40 -0400646 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647}
648
Len Brown4be44fc2005-08-05 00:44:28 -0400649static int acpi_battery_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652 if (acpi_device_dir(device)) {
653 remove_proc_entry(ACPI_BATTERY_FILE_ALARM,
654 acpi_device_dir(device));
655 remove_proc_entry(ACPI_BATTERY_FILE_STATUS,
656 acpi_device_dir(device));
657 remove_proc_entry(ACPI_BATTERY_FILE_INFO,
658 acpi_device_dir(device));
659
660 remove_proc_entry(acpi_device_bid(device), acpi_battery_dir);
661 acpi_device_dir(device) = NULL;
662 }
663
Patrick Mocheld550d982006-06-27 00:41:40 -0400664 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665}
666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667/* --------------------------------------------------------------------------
668 Driver Interface
669 -------------------------------------------------------------------------- */
670
Len Brown4be44fc2005-08-05 00:44:28 -0400671static void acpi_battery_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200673 struct acpi_battery *battery = data;
Len Brown4be44fc2005-08-05 00:44:28 -0400674 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677 if (!battery)
Patrick Mocheld550d982006-06-27 00:41:40 -0400678 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Patrick Mochel145def82006-05-19 16:54:39 -0400680 device = battery->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682 switch (event) {
683 case ACPI_BATTERY_NOTIFY_STATUS:
684 case ACPI_BATTERY_NOTIFY_INFO:
Vladimir Lebedev9fdae722006-06-27 04:49:00 -0400685 case ACPI_NOTIFY_BUS_CHECK:
686 case ACPI_NOTIFY_DEVICE_CHECK:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 acpi_battery_check(battery);
688 acpi_bus_generate_event(device, event, battery->flags.present);
689 break;
690 default:
691 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400692 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 break;
694 }
695
Patrick Mocheld550d982006-06-27 00:41:40 -0400696 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697}
698
Len Brown4be44fc2005-08-05 00:44:28 -0400699static int acpi_battery_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
Len Brown4be44fc2005-08-05 00:44:28 -0400701 int result = 0;
702 acpi_status status = 0;
703 struct acpi_battery *battery = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Len Brown4be44fc2005-08-05 00:44:28 -0400705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400707 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Burman Yan36bcbec2006-12-19 12:56:11 -0800709 battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 if (!battery)
Patrick Mocheld550d982006-06-27 00:41:40 -0400711 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Patrick Mochel145def82006-05-19 16:54:39 -0400713 battery->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME);
715 strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS);
716 acpi_driver_data(device) = battery;
717
718 result = acpi_battery_check(battery);
719 if (result)
720 goto end;
721
722 result = acpi_battery_add_fs(device);
723 if (result)
724 goto end;
725
Patrick Mochel3b073ec2006-05-19 16:54:41 -0400726 status = acpi_install_notify_handler(device->handle,
Vladimir Lebedev9fdae722006-06-27 04:49:00 -0400727 ACPI_ALL_NOTIFY,
Len Brown4be44fc2005-08-05 00:44:28 -0400728 acpi_battery_notify, battery);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 result = -ENODEV;
731 goto end;
732 }
733
734 printk(KERN_INFO PREFIX "%s Slot [%s] (battery %s)\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400735 ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device),
736 device->status.battery_present ? "present" : "absent");
737
738 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 if (result) {
740 acpi_battery_remove_fs(device);
741 kfree(battery);
742 }
743
Patrick Mocheld550d982006-06-27 00:41:40 -0400744 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745}
746
Len Brown4be44fc2005-08-05 00:44:28 -0400747static int acpi_battery_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
Len Brown4be44fc2005-08-05 00:44:28 -0400749 acpi_status status = 0;
750 struct acpi_battery *battery = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400754 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200756 battery = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Patrick Mochel3b073ec2006-05-19 16:54:41 -0400758 status = acpi_remove_notify_handler(device->handle,
Vladimir Lebedev9fdae722006-06-27 04:49:00 -0400759 ACPI_ALL_NOTIFY,
Len Brown4be44fc2005-08-05 00:44:28 -0400760 acpi_battery_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762 acpi_battery_remove_fs(device);
763
764 kfree(battery);
765
Patrick Mocheld550d982006-06-27 00:41:40 -0400766 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767}
768
Jiri Kosina34c44152006-10-10 14:20:41 -0700769/* this is needed to learn about changes made in suspended state */
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800770static int acpi_battery_resume(struct acpi_device *device)
Jiri Kosina34c44152006-10-10 14:20:41 -0700771{
772 struct acpi_battery *battery;
773
774 if (!device)
775 return -EINVAL;
776
777 battery = device->driver_data;
778 return acpi_battery_check(battery);
779}
780
Len Brown4be44fc2005-08-05 00:44:28 -0400781static int __init acpi_battery_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
Rich Townsend3f86b832006-07-01 11:36:54 -0400783 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Pavel Machek4d8316d2006-08-14 22:37:22 -0700785 if (acpi_disabled)
786 return -ENODEV;
787
Rich Townsend3f86b832006-07-01 11:36:54 -0400788 acpi_battery_dir = acpi_lock_battery_dir();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 if (!acpi_battery_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -0400790 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792 result = acpi_bus_register_driver(&acpi_battery_driver);
793 if (result < 0) {
Rich Townsend3f86b832006-07-01 11:36:54 -0400794 acpi_unlock_battery_dir(acpi_battery_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -0400795 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 }
797
Patrick Mocheld550d982006-06-27 00:41:40 -0400798 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799}
800
Len Brown4be44fc2005-08-05 00:44:28 -0400801static void __exit acpi_battery_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
804 acpi_bus_unregister_driver(&acpi_battery_driver);
805
Rich Townsend3f86b832006-07-01 11:36:54 -0400806 acpi_unlock_battery_dir(acpi_battery_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Patrick Mocheld550d982006-06-27 00:41:40 -0400808 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809}
810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811module_init(acpi_battery_init);
812module_exit(acpi_battery_exit);