blob: 2f4521a48fe7b6e3f66726d389cbe5c436540ef6 [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"
45#define ACPI_BATTERY_DRIVER_NAME "ACPI Battery Driver"
46#define ACPI_BATTERY_DEVICE_NAME "Battery"
47#define ACPI_BATTERY_FILE_INFO "info"
48#define ACPI_BATTERY_FILE_STATUS "state"
49#define ACPI_BATTERY_FILE_ALARM "alarm"
50#define ACPI_BATTERY_NOTIFY_STATUS 0x80
51#define ACPI_BATTERY_NOTIFY_INFO 0x81
52#define ACPI_BATTERY_UNITS_WATTS "mW"
53#define ACPI_BATTERY_UNITS_AMPS "mA"
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#define _COMPONENT ACPI_BATTERY_COMPONENT
Len Brown4be44fc2005-08-05 00:44:28 -040056ACPI_MODULE_NAME("acpi_battery")
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Len Brown4be44fc2005-08-05 00:44:28 -040058 MODULE_AUTHOR("Paul Diefenbaugh");
Linus Torvalds1da177e2005-04-16 15:20:36 -070059MODULE_DESCRIPTION(ACPI_BATTERY_DRIVER_NAME);
60MODULE_LICENSE("GPL");
61
Rich Townsend3f86b832006-07-01 11:36:54 -040062extern struct proc_dir_entry *acpi_lock_battery_dir(void);
63extern void *acpi_unlock_battery_dir(struct proc_dir_entry *acpi_battery_dir);
64
Len Brown4be44fc2005-08-05 00:44:28 -040065static int acpi_battery_add(struct acpi_device *device);
66static int acpi_battery_remove(struct acpi_device *device, int type);
Patrick Mochel5d9464a2006-12-07 20:56:27 +080067static int acpi_battery_resume(struct acpi_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69static struct acpi_driver acpi_battery_driver = {
Len Brown4be44fc2005-08-05 00:44:28 -040070 .name = ACPI_BATTERY_DRIVER_NAME,
71 .class = ACPI_BATTERY_CLASS,
72 .ids = ACPI_BATTERY_HID,
73 .ops = {
74 .add = acpi_battery_add,
Jiri Kosina34c44152006-10-10 14:20:41 -070075 .resume = acpi_battery_resume,
Len Brown4be44fc2005-08-05 00:44:28 -040076 .remove = acpi_battery_remove,
77 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070078};
79
80struct acpi_battery_status {
Len Brown4be44fc2005-08-05 00:44:28 -040081 acpi_integer state;
82 acpi_integer present_rate;
83 acpi_integer remaining_capacity;
84 acpi_integer present_voltage;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085};
86
87struct acpi_battery_info {
Len Brown4be44fc2005-08-05 00:44:28 -040088 acpi_integer power_unit;
89 acpi_integer design_capacity;
90 acpi_integer last_full_capacity;
91 acpi_integer battery_technology;
92 acpi_integer design_voltage;
93 acpi_integer design_capacity_warning;
94 acpi_integer design_capacity_low;
95 acpi_integer battery_capacity_granularity_1;
96 acpi_integer battery_capacity_granularity_2;
97 acpi_string model_number;
98 acpi_string serial_number;
99 acpi_string battery_type;
100 acpi_string oem_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101};
102
103struct acpi_battery_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400104 u8 present:1; /* Bay occupied? */
105 u8 power_unit:1; /* 0=watts, 1=apms */
106 u8 alarm:1; /* _BTP present? */
107 u8 reserved:5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108};
109
110struct acpi_battery_trips {
Len Brown4be44fc2005-08-05 00:44:28 -0400111 unsigned long warning;
112 unsigned long low;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113};
114
115struct acpi_battery {
Patrick Mochel145def82006-05-19 16:54:39 -0400116 struct acpi_device * device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 struct acpi_battery_flags flags;
118 struct acpi_battery_trips trips;
Len Brown4be44fc2005-08-05 00:44:28 -0400119 unsigned long alarm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 struct acpi_battery_info *info;
121};
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123/* --------------------------------------------------------------------------
124 Battery Management
125 -------------------------------------------------------------------------- */
126
127static int
Len Brown4be44fc2005-08-05 00:44:28 -0400128acpi_battery_get_info(struct acpi_battery *battery,
129 struct acpi_battery_info **bif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
Len Brown4be44fc2005-08-05 00:44:28 -0400131 int result = 0;
132 acpi_status status = 0;
133 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
134 struct acpi_buffer format = { sizeof(ACPI_BATTERY_FORMAT_BIF),
135 ACPI_BATTERY_FORMAT_BIF
136 };
137 struct acpi_buffer data = { 0, NULL };
138 union acpi_object *package = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 if (!battery || !bif)
Patrick Mocheld550d982006-06-27 00:41:40 -0400142 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 /* Evalute _BIF */
145
Patrick Mochel3b073ec2006-05-19 16:54:41 -0400146 status = acpi_evaluate_object(battery->device->handle, "_BIF", NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400148 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BIF"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400149 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 }
151
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200152 package = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154 /* Extract Package Data */
155
156 status = acpi_extract_package(package, &format, &data);
157 if (status != AE_BUFFER_OVERFLOW) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400158 ACPI_EXCEPTION((AE_INFO, status, "Extracting _BIF"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 result = -ENODEV;
160 goto end;
161 }
162
Burman Yan36bcbec2006-12-19 12:56:11 -0800163 data.pointer = kzalloc(data.length, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 if (!data.pointer) {
165 result = -ENOMEM;
166 goto end;
167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169 status = acpi_extract_package(package, &format, &data);
170 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400171 ACPI_EXCEPTION((AE_INFO, status, "Extracting _BIF"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 kfree(data.pointer);
173 result = -ENODEV;
174 goto end;
175 }
176
Len Brown4be44fc2005-08-05 00:44:28 -0400177 end:
Len Brown02438d82006-06-30 03:19:10 -0400178 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 if (!result)
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200181 (*bif) = data.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Patrick Mocheld550d982006-06-27 00:41:40 -0400183 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
185
186static int
Len Brown4be44fc2005-08-05 00:44:28 -0400187acpi_battery_get_status(struct acpi_battery *battery,
188 struct acpi_battery_status **bst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
Len Brown4be44fc2005-08-05 00:44:28 -0400190 int result = 0;
191 acpi_status status = 0;
192 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
193 struct acpi_buffer format = { sizeof(ACPI_BATTERY_FORMAT_BST),
194 ACPI_BATTERY_FORMAT_BST
195 };
196 struct acpi_buffer data = { 0, NULL };
197 union acpi_object *package = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200 if (!battery || !bst)
Patrick Mocheld550d982006-06-27 00:41:40 -0400201 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 /* Evalute _BST */
204
Patrick Mochel3b073ec2006-05-19 16:54:41 -0400205 status = acpi_evaluate_object(battery->device->handle, "_BST", NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400207 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BST"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400208 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 }
210
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200211 package = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213 /* Extract Package Data */
214
215 status = acpi_extract_package(package, &format, &data);
216 if (status != AE_BUFFER_OVERFLOW) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400217 ACPI_EXCEPTION((AE_INFO, status, "Extracting _BST"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 result = -ENODEV;
219 goto end;
220 }
221
Burman Yan36bcbec2006-12-19 12:56:11 -0800222 data.pointer = kzalloc(data.length, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 if (!data.pointer) {
224 result = -ENOMEM;
225 goto end;
226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 status = acpi_extract_package(package, &format, &data);
229 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400230 ACPI_EXCEPTION((AE_INFO, status, "Extracting _BST"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 kfree(data.pointer);
232 result = -ENODEV;
233 goto end;
234 }
235
Len Brown4be44fc2005-08-05 00:44:28 -0400236 end:
Len Brown02438d82006-06-30 03:19:10 -0400237 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239 if (!result)
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200240 (*bst) = data.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Patrick Mocheld550d982006-06-27 00:41:40 -0400242 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245static int
Len Brown4be44fc2005-08-05 00:44:28 -0400246acpi_battery_set_alarm(struct acpi_battery *battery, unsigned long alarm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
Len Brown4be44fc2005-08-05 00:44:28 -0400248 acpi_status status = 0;
249 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
250 struct acpi_object_list arg_list = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 if (!battery)
Patrick Mocheld550d982006-06-27 00:41:40 -0400254 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 if (!battery->flags.alarm)
Patrick Mocheld550d982006-06-27 00:41:40 -0400257 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 arg0.integer.value = alarm;
260
Patrick Mochel3b073ec2006-05-19 16:54:41 -0400261 status = acpi_evaluate_object(battery->device->handle, "_BTP", &arg_list, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400263 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", (u32) alarm));
266
267 battery->alarm = alarm;
268
Patrick Mocheld550d982006-06-27 00:41:40 -0400269 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270}
271
Len Brown4be44fc2005-08-05 00:44:28 -0400272static int acpi_battery_check(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 = AE_OK;
276 acpi_handle handle = NULL;
277 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 struct acpi_battery_info *bif = NULL;
279
Len Brown4be44fc2005-08-05 00:44:28 -0400280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 if (!battery)
Patrick Mocheld550d982006-06-27 00:41:40 -0400282 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Patrick Mochel145def82006-05-19 16:54:39 -0400284 device = battery->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 result = acpi_bus_get_status(device);
287 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400288 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290 /* Insertion? */
291
292 if (!battery->flags.present && device->status.battery_present) {
293
294 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Battery inserted\n"));
295
296 /* Evalute _BIF to get certain static information */
297
298 result = acpi_battery_get_info(battery, &bif);
299 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400300 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 battery->flags.power_unit = bif->power_unit;
303 battery->trips.warning = bif->design_capacity_warning;
304 battery->trips.low = bif->design_capacity_low;
305 kfree(bif);
306
307 /* See if alarms are supported, and if so, set default */
308
Patrick Mochel3b073ec2006-05-19 16:54:41 -0400309 status = acpi_get_handle(battery->device->handle, "_BTP", &handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 if (ACPI_SUCCESS(status)) {
311 battery->flags.alarm = 1;
312 acpi_battery_set_alarm(battery, battery->trips.warning);
313 }
314 }
315
316 /* Removal? */
317
318 else if (battery->flags.present && !device->status.battery_present) {
319 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Battery removed\n"));
320 }
321
322 battery->flags.present = device->status.battery_present;
323
Patrick Mocheld550d982006-06-27 00:41:40 -0400324 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325}
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327/* --------------------------------------------------------------------------
328 FS Interface (/proc)
329 -------------------------------------------------------------------------- */
330
Len Brown4be44fc2005-08-05 00:44:28 -0400331static struct proc_dir_entry *acpi_battery_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332static int acpi_battery_read_info(struct seq_file *seq, void *offset)
333{
Len Brown4be44fc2005-08-05 00:44:28 -0400334 int result = 0;
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200335 struct acpi_battery *battery = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 struct acpi_battery_info *bif = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400337 char *units = "?";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 if (!battery)
341 goto end;
342
343 if (battery->flags.present)
344 seq_printf(seq, "present: yes\n");
345 else {
346 seq_printf(seq, "present: no\n");
347 goto end;
348 }
349
350 /* Battery Info (_BIF) */
351
352 result = acpi_battery_get_info(battery, &bif);
353 if (result || !bif) {
354 seq_printf(seq, "ERROR: Unable to read battery information\n");
355 goto end;
356 }
357
Len Brown4be44fc2005-08-05 00:44:28 -0400358 units =
359 bif->
360 power_unit ? ACPI_BATTERY_UNITS_AMPS : ACPI_BATTERY_UNITS_WATTS;
361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 if (bif->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
363 seq_printf(seq, "design capacity: unknown\n");
364 else
365 seq_printf(seq, "design capacity: %d %sh\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400366 (u32) bif->design_capacity, units);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 if (bif->last_full_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
369 seq_printf(seq, "last full capacity: unknown\n");
370 else
371 seq_printf(seq, "last full capacity: %d %sh\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400372 (u32) bif->last_full_capacity, units);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 switch ((u32) bif->battery_technology) {
375 case 0:
376 seq_printf(seq, "battery technology: non-rechargeable\n");
377 break;
378 case 1:
379 seq_printf(seq, "battery technology: rechargeable\n");
380 break;
381 default:
382 seq_printf(seq, "battery technology: unknown\n");
383 break;
384 }
385
386 if (bif->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
387 seq_printf(seq, "design voltage: unknown\n");
388 else
389 seq_printf(seq, "design voltage: %d mV\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400390 (u32) bif->design_voltage);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Len Brown4be44fc2005-08-05 00:44:28 -0400392 seq_printf(seq, "design capacity warning: %d %sh\n",
393 (u32) bif->design_capacity_warning, units);
394 seq_printf(seq, "design capacity low: %d %sh\n",
395 (u32) bif->design_capacity_low, units);
396 seq_printf(seq, "capacity granularity 1: %d %sh\n",
397 (u32) bif->battery_capacity_granularity_1, units);
398 seq_printf(seq, "capacity granularity 2: %d %sh\n",
399 (u32) bif->battery_capacity_granularity_2, units);
400 seq_printf(seq, "model number: %s\n", bif->model_number);
401 seq_printf(seq, "serial number: %s\n", bif->serial_number);
402 seq_printf(seq, "battery type: %s\n", bif->battery_type);
403 seq_printf(seq, "OEM info: %s\n", bif->oem_info);
404
405 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 kfree(bif);
407
Patrick Mocheld550d982006-06-27 00:41:40 -0400408 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409}
410
411static int acpi_battery_info_open_fs(struct inode *inode, struct file *file)
412{
413 return single_open(file, acpi_battery_read_info, PDE(inode)->data);
414}
415
Len Brown4be44fc2005-08-05 00:44:28 -0400416static int acpi_battery_read_state(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
Len Brown4be44fc2005-08-05 00:44:28 -0400418 int result = 0;
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200419 struct acpi_battery *battery = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 struct acpi_battery_status *bst = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400421 char *units = "?";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424 if (!battery)
425 goto end;
426
427 if (battery->flags.present)
428 seq_printf(seq, "present: yes\n");
429 else {
430 seq_printf(seq, "present: no\n");
431 goto end;
432 }
433
434 /* Battery Units */
435
Len Brown4be44fc2005-08-05 00:44:28 -0400436 units =
437 battery->flags.
438 power_unit ? ACPI_BATTERY_UNITS_AMPS : ACPI_BATTERY_UNITS_WATTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 /* Battery Status (_BST) */
441
442 result = acpi_battery_get_status(battery, &bst);
443 if (result || !bst) {
444 seq_printf(seq, "ERROR: Unable to read battery status\n");
445 goto end;
446 }
447
448 if (!(bst->state & 0x04))
449 seq_printf(seq, "capacity state: ok\n");
450 else
451 seq_printf(seq, "capacity state: critical\n");
452
Len Brown4be44fc2005-08-05 00:44:28 -0400453 if ((bst->state & 0x01) && (bst->state & 0x02)) {
454 seq_printf(seq,
455 "charging state: charging/discharging\n");
Len Brown4be44fc2005-08-05 00:44:28 -0400456 } else if (bst->state & 0x01)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 seq_printf(seq, "charging state: discharging\n");
458 else if (bst->state & 0x02)
459 seq_printf(seq, "charging state: charging\n");
460 else {
461 seq_printf(seq, "charging state: charged\n");
462 }
463
464 if (bst->present_rate == ACPI_BATTERY_VALUE_UNKNOWN)
465 seq_printf(seq, "present rate: unknown\n");
466 else
467 seq_printf(seq, "present rate: %d %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400468 (u32) bst->present_rate, units);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
470 if (bst->remaining_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
471 seq_printf(seq, "remaining capacity: unknown\n");
472 else
473 seq_printf(seq, "remaining capacity: %d %sh\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400474 (u32) bst->remaining_capacity, units);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476 if (bst->present_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
477 seq_printf(seq, "present voltage: unknown\n");
478 else
479 seq_printf(seq, "present voltage: %d mV\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400480 (u32) bst->present_voltage);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Len Brown4be44fc2005-08-05 00:44:28 -0400482 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 kfree(bst);
484
Patrick Mocheld550d982006-06-27 00:41:40 -0400485 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
487
488static int acpi_battery_state_open_fs(struct inode *inode, struct file *file)
489{
490 return single_open(file, acpi_battery_read_state, PDE(inode)->data);
491}
492
Len Brown4be44fc2005-08-05 00:44:28 -0400493static int acpi_battery_read_alarm(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200495 struct acpi_battery *battery = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -0400496 char *units = "?";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 if (!battery)
500 goto end;
501
502 if (!battery->flags.present) {
503 seq_printf(seq, "present: no\n");
504 goto end;
505 }
506
507 /* Battery Units */
Len Brown4be44fc2005-08-05 00:44:28 -0400508
509 units =
510 battery->flags.
511 power_unit ? ACPI_BATTERY_UNITS_AMPS : ACPI_BATTERY_UNITS_WATTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513 /* Battery Alarm */
514
515 seq_printf(seq, "alarm: ");
516 if (!battery->alarm)
517 seq_printf(seq, "unsupported\n");
518 else
519 seq_printf(seq, "%d %sh\n", (u32) battery->alarm, units);
520
Len Brown4be44fc2005-08-05 00:44:28 -0400521 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400522 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523}
524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -0400526acpi_battery_write_alarm(struct file *file,
527 const char __user * buffer,
528 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
Len Brown4be44fc2005-08-05 00:44:28 -0400530 int result = 0;
531 char alarm_string[12] = { '\0' };
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200532 struct seq_file *m = file->private_data;
533 struct acpi_battery *battery = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536 if (!battery || (count > sizeof(alarm_string) - 1))
Patrick Mocheld550d982006-06-27 00:41:40 -0400537 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
539 if (!battery->flags.present)
Patrick Mocheld550d982006-06-27 00:41:40 -0400540 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542 if (copy_from_user(alarm_string, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -0400543 return -EFAULT;
Len Brown4be44fc2005-08-05 00:44:28 -0400544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 alarm_string[count] = '\0';
546
Len Brown4be44fc2005-08-05 00:44:28 -0400547 result = acpi_battery_set_alarm(battery,
548 simple_strtoul(alarm_string, NULL, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400550 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Patrick Mocheld550d982006-06-27 00:41:40 -0400552 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553}
554
555static int acpi_battery_alarm_open_fs(struct inode *inode, struct file *file)
556{
557 return single_open(file, acpi_battery_read_alarm, PDE(inode)->data);
558}
559
Arjan van de Vend7508032006-07-04 13:06:00 -0400560static const struct file_operations acpi_battery_info_ops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400561 .open = acpi_battery_info_open_fs,
562 .read = seq_read,
563 .llseek = seq_lseek,
564 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 .owner = THIS_MODULE,
566};
567
Arjan van de Vend7508032006-07-04 13:06:00 -0400568static const struct file_operations acpi_battery_state_ops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400569 .open = acpi_battery_state_open_fs,
570 .read = seq_read,
571 .llseek = seq_lseek,
572 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 .owner = THIS_MODULE,
574};
575
Arjan van de Vend7508032006-07-04 13:06:00 -0400576static const struct file_operations acpi_battery_alarm_ops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400577 .open = acpi_battery_alarm_open_fs,
578 .read = seq_read,
579 .write = acpi_battery_write_alarm,
580 .llseek = seq_lseek,
581 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 .owner = THIS_MODULE,
583};
584
Len Brown4be44fc2005-08-05 00:44:28 -0400585static int acpi_battery_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
Len Brown4be44fc2005-08-05 00:44:28 -0400587 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
590 if (!acpi_device_dir(device)) {
591 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown4be44fc2005-08-05 00:44:28 -0400592 acpi_battery_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400594 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 acpi_device_dir(device)->owner = THIS_MODULE;
596 }
597
598 /* 'info' [R] */
599 entry = create_proc_entry(ACPI_BATTERY_FILE_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400600 S_IRUGO, acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400602 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 else {
Len Brown4be44fc2005-08-05 00:44:28 -0400604 entry->proc_fops = &acpi_battery_info_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 entry->data = acpi_driver_data(device);
606 entry->owner = THIS_MODULE;
607 }
608
609 /* 'status' [R] */
610 entry = create_proc_entry(ACPI_BATTERY_FILE_STATUS,
Len Brown4be44fc2005-08-05 00:44:28 -0400611 S_IRUGO, acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400613 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 else {
615 entry->proc_fops = &acpi_battery_state_ops;
616 entry->data = acpi_driver_data(device);
617 entry->owner = THIS_MODULE;
618 }
619
620 /* 'alarm' [R/W] */
621 entry = create_proc_entry(ACPI_BATTERY_FILE_ALARM,
Len Brown4be44fc2005-08-05 00:44:28 -0400622 S_IFREG | S_IRUGO | S_IWUSR,
623 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400625 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 else {
627 entry->proc_fops = &acpi_battery_alarm_ops;
628 entry->data = acpi_driver_data(device);
629 entry->owner = THIS_MODULE;
630 }
631
Patrick Mocheld550d982006-06-27 00:41:40 -0400632 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633}
634
Len Brown4be44fc2005-08-05 00:44:28 -0400635static int acpi_battery_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638 if (acpi_device_dir(device)) {
639 remove_proc_entry(ACPI_BATTERY_FILE_ALARM,
640 acpi_device_dir(device));
641 remove_proc_entry(ACPI_BATTERY_FILE_STATUS,
642 acpi_device_dir(device));
643 remove_proc_entry(ACPI_BATTERY_FILE_INFO,
644 acpi_device_dir(device));
645
646 remove_proc_entry(acpi_device_bid(device), acpi_battery_dir);
647 acpi_device_dir(device) = NULL;
648 }
649
Patrick Mocheld550d982006-06-27 00:41:40 -0400650 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651}
652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653/* --------------------------------------------------------------------------
654 Driver Interface
655 -------------------------------------------------------------------------- */
656
Len Brown4be44fc2005-08-05 00:44:28 -0400657static void acpi_battery_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200659 struct acpi_battery *battery = data;
Len Brown4be44fc2005-08-05 00:44:28 -0400660 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
663 if (!battery)
Patrick Mocheld550d982006-06-27 00:41:40 -0400664 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Patrick Mochel145def82006-05-19 16:54:39 -0400666 device = battery->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668 switch (event) {
669 case ACPI_BATTERY_NOTIFY_STATUS:
670 case ACPI_BATTERY_NOTIFY_INFO:
Vladimir Lebedev9fdae722006-06-27 04:49:00 -0400671 case ACPI_NOTIFY_BUS_CHECK:
672 case ACPI_NOTIFY_DEVICE_CHECK:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 acpi_battery_check(battery);
674 acpi_bus_generate_event(device, event, battery->flags.present);
675 break;
676 default:
677 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400678 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 break;
680 }
681
Patrick Mocheld550d982006-06-27 00:41:40 -0400682 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683}
684
Len Brown4be44fc2005-08-05 00:44:28 -0400685static int acpi_battery_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
Len Brown4be44fc2005-08-05 00:44:28 -0400687 int result = 0;
688 acpi_status status = 0;
689 struct acpi_battery *battery = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Len Brown4be44fc2005-08-05 00:44:28 -0400691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400693 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Burman Yan36bcbec2006-12-19 12:56:11 -0800695 battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 if (!battery)
Patrick Mocheld550d982006-06-27 00:41:40 -0400697 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Patrick Mochel145def82006-05-19 16:54:39 -0400699 battery->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME);
701 strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS);
702 acpi_driver_data(device) = battery;
703
704 result = acpi_battery_check(battery);
705 if (result)
706 goto end;
707
708 result = acpi_battery_add_fs(device);
709 if (result)
710 goto end;
711
Patrick Mochel3b073ec2006-05-19 16:54:41 -0400712 status = acpi_install_notify_handler(device->handle,
Vladimir Lebedev9fdae722006-06-27 04:49:00 -0400713 ACPI_ALL_NOTIFY,
Len Brown4be44fc2005-08-05 00:44:28 -0400714 acpi_battery_notify, battery);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 result = -ENODEV;
717 goto end;
718 }
719
720 printk(KERN_INFO PREFIX "%s Slot [%s] (battery %s)\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400721 ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device),
722 device->status.battery_present ? "present" : "absent");
723
724 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 if (result) {
726 acpi_battery_remove_fs(device);
727 kfree(battery);
728 }
729
Patrick Mocheld550d982006-06-27 00:41:40 -0400730 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731}
732
Len Brown4be44fc2005-08-05 00:44:28 -0400733static int acpi_battery_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
Len Brown4be44fc2005-08-05 00:44:28 -0400735 acpi_status status = 0;
736 struct acpi_battery *battery = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400740 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200742 battery = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Patrick Mochel3b073ec2006-05-19 16:54:41 -0400744 status = acpi_remove_notify_handler(device->handle,
Vladimir Lebedev9fdae722006-06-27 04:49:00 -0400745 ACPI_ALL_NOTIFY,
Len Brown4be44fc2005-08-05 00:44:28 -0400746 acpi_battery_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748 acpi_battery_remove_fs(device);
749
750 kfree(battery);
751
Patrick Mocheld550d982006-06-27 00:41:40 -0400752 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753}
754
Jiri Kosina34c44152006-10-10 14:20:41 -0700755/* this is needed to learn about changes made in suspended state */
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800756static int acpi_battery_resume(struct acpi_device *device)
Jiri Kosina34c44152006-10-10 14:20:41 -0700757{
758 struct acpi_battery *battery;
759
760 if (!device)
761 return -EINVAL;
762
763 battery = device->driver_data;
764 return acpi_battery_check(battery);
765}
766
Len Brown4be44fc2005-08-05 00:44:28 -0400767static int __init acpi_battery_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
Rich Townsend3f86b832006-07-01 11:36:54 -0400769 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Pavel Machek4d8316d2006-08-14 22:37:22 -0700771 if (acpi_disabled)
772 return -ENODEV;
773
Rich Townsend3f86b832006-07-01 11:36:54 -0400774 acpi_battery_dir = acpi_lock_battery_dir();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 if (!acpi_battery_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -0400776 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
778 result = acpi_bus_register_driver(&acpi_battery_driver);
779 if (result < 0) {
Rich Townsend3f86b832006-07-01 11:36:54 -0400780 acpi_unlock_battery_dir(acpi_battery_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -0400781 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 }
783
Patrick Mocheld550d982006-06-27 00:41:40 -0400784 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785}
786
Len Brown4be44fc2005-08-05 00:44:28 -0400787static void __exit acpi_battery_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790 acpi_bus_unregister_driver(&acpi_battery_driver);
791
Rich Townsend3f86b832006-07-01 11:36:54 -0400792 acpi_unlock_battery_dir(acpi_battery_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Patrick Mocheld550d982006-06-27 00:41:40 -0400794 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795}
796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797module_init(acpi_battery_init);
798module_exit(acpi_battery_exit);