blob: 07c3c276ad3425f0ff691426146348a1b7675542 [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
Len Brown4be44fc2005-08-05 00:44:28 -0400152 package = (union acpi_object *)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
163 data.pointer = kmalloc(data.length, GFP_KERNEL);
164 if (!data.pointer) {
165 result = -ENOMEM;
166 goto end;
167 }
168 memset(data.pointer, 0, data.length);
169
170 status = acpi_extract_package(package, &format, &data);
171 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400172 ACPI_EXCEPTION((AE_INFO, status, "Extracting _BIF"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 kfree(data.pointer);
174 result = -ENODEV;
175 goto end;
176 }
177
Len Brown4be44fc2005-08-05 00:44:28 -0400178 end:
Len Brown02438d82006-06-30 03:19:10 -0400179 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181 if (!result)
Len Brown4be44fc2005-08-05 00:44:28 -0400182 (*bif) = (struct acpi_battery_info *)data.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Patrick Mocheld550d982006-06-27 00:41:40 -0400184 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
187static int
Len Brown4be44fc2005-08-05 00:44:28 -0400188acpi_battery_get_status(struct acpi_battery *battery,
189 struct acpi_battery_status **bst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
Len Brown4be44fc2005-08-05 00:44:28 -0400191 int result = 0;
192 acpi_status status = 0;
193 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
194 struct acpi_buffer format = { sizeof(ACPI_BATTERY_FORMAT_BST),
195 ACPI_BATTERY_FORMAT_BST
196 };
197 struct acpi_buffer data = { 0, NULL };
198 union acpi_object *package = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 if (!battery || !bst)
Patrick Mocheld550d982006-06-27 00:41:40 -0400202 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204 /* Evalute _BST */
205
Patrick Mochel3b073ec2006-05-19 16:54:41 -0400206 status = acpi_evaluate_object(battery->device->handle, "_BST", NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400208 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BST"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400209 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 }
211
Len Brown4be44fc2005-08-05 00:44:28 -0400212 package = (union acpi_object *)buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 /* Extract Package Data */
215
216 status = acpi_extract_package(package, &format, &data);
217 if (status != AE_BUFFER_OVERFLOW) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400218 ACPI_EXCEPTION((AE_INFO, status, "Extracting _BST"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 result = -ENODEV;
220 goto end;
221 }
222
223 data.pointer = kmalloc(data.length, GFP_KERNEL);
224 if (!data.pointer) {
225 result = -ENOMEM;
226 goto end;
227 }
228 memset(data.pointer, 0, data.length);
229
230 status = acpi_extract_package(package, &format, &data);
231 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400232 ACPI_EXCEPTION((AE_INFO, status, "Extracting _BST"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 kfree(data.pointer);
234 result = -ENODEV;
235 goto end;
236 }
237
Len Brown4be44fc2005-08-05 00:44:28 -0400238 end:
Len Brown02438d82006-06-30 03:19:10 -0400239 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 if (!result)
Len Brown4be44fc2005-08-05 00:44:28 -0400242 (*bst) = (struct acpi_battery_status *)data.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Patrick Mocheld550d982006-06-27 00:41:40 -0400244 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245}
246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247static int
Len Brown4be44fc2005-08-05 00:44:28 -0400248acpi_battery_set_alarm(struct acpi_battery *battery, unsigned long alarm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
Len Brown4be44fc2005-08-05 00:44:28 -0400250 acpi_status status = 0;
251 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
252 struct acpi_object_list arg_list = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 if (!battery)
Patrick Mocheld550d982006-06-27 00:41:40 -0400256 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 if (!battery->flags.alarm)
Patrick Mocheld550d982006-06-27 00:41:40 -0400259 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 arg0.integer.value = alarm;
262
Patrick Mochel3b073ec2006-05-19 16:54:41 -0400263 status = acpi_evaluate_object(battery->device->handle, "_BTP", &arg_list, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400265 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", (u32) alarm));
268
269 battery->alarm = alarm;
270
Patrick Mocheld550d982006-06-27 00:41:40 -0400271 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272}
273
Len Brown4be44fc2005-08-05 00:44:28 -0400274static int acpi_battery_check(struct acpi_battery *battery)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
Len Brown4be44fc2005-08-05 00:44:28 -0400276 int result = 0;
277 acpi_status status = AE_OK;
278 acpi_handle handle = NULL;
279 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 struct acpi_battery_info *bif = NULL;
281
Len Brown4be44fc2005-08-05 00:44:28 -0400282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 if (!battery)
Patrick Mocheld550d982006-06-27 00:41:40 -0400284 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Patrick Mochel145def82006-05-19 16:54:39 -0400286 device = battery->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288 result = acpi_bus_get_status(device);
289 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400290 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 /* Insertion? */
293
294 if (!battery->flags.present && device->status.battery_present) {
295
296 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Battery inserted\n"));
297
298 /* Evalute _BIF to get certain static information */
299
300 result = acpi_battery_get_info(battery, &bif);
301 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400302 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 battery->flags.power_unit = bif->power_unit;
305 battery->trips.warning = bif->design_capacity_warning;
306 battery->trips.low = bif->design_capacity_low;
307 kfree(bif);
308
309 /* See if alarms are supported, and if so, set default */
310
Patrick Mochel3b073ec2006-05-19 16:54:41 -0400311 status = acpi_get_handle(battery->device->handle, "_BTP", &handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 if (ACPI_SUCCESS(status)) {
313 battery->flags.alarm = 1;
314 acpi_battery_set_alarm(battery, battery->trips.warning);
315 }
316 }
317
318 /* Removal? */
319
320 else if (battery->flags.present && !device->status.battery_present) {
321 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Battery removed\n"));
322 }
323
324 battery->flags.present = device->status.battery_present;
325
Patrick Mocheld550d982006-06-27 00:41:40 -0400326 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329/* --------------------------------------------------------------------------
330 FS Interface (/proc)
331 -------------------------------------------------------------------------- */
332
Len Brown4be44fc2005-08-05 00:44:28 -0400333static struct proc_dir_entry *acpi_battery_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334static int acpi_battery_read_info(struct seq_file *seq, void *offset)
335{
Len Brown4be44fc2005-08-05 00:44:28 -0400336 int result = 0;
337 struct acpi_battery *battery = (struct acpi_battery *)seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 struct acpi_battery_info *bif = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400339 char *units = "?";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 if (!battery)
343 goto end;
344
345 if (battery->flags.present)
346 seq_printf(seq, "present: yes\n");
347 else {
348 seq_printf(seq, "present: no\n");
349 goto end;
350 }
351
352 /* Battery Info (_BIF) */
353
354 result = acpi_battery_get_info(battery, &bif);
355 if (result || !bif) {
356 seq_printf(seq, "ERROR: Unable to read battery information\n");
357 goto end;
358 }
359
Len Brown4be44fc2005-08-05 00:44:28 -0400360 units =
361 bif->
362 power_unit ? ACPI_BATTERY_UNITS_AMPS : ACPI_BATTERY_UNITS_WATTS;
363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 if (bif->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
365 seq_printf(seq, "design capacity: unknown\n");
366 else
367 seq_printf(seq, "design capacity: %d %sh\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400368 (u32) bif->design_capacity, units);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 if (bif->last_full_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
371 seq_printf(seq, "last full capacity: unknown\n");
372 else
373 seq_printf(seq, "last full capacity: %d %sh\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400374 (u32) bif->last_full_capacity, units);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 switch ((u32) bif->battery_technology) {
377 case 0:
378 seq_printf(seq, "battery technology: non-rechargeable\n");
379 break;
380 case 1:
381 seq_printf(seq, "battery technology: rechargeable\n");
382 break;
383 default:
384 seq_printf(seq, "battery technology: unknown\n");
385 break;
386 }
387
388 if (bif->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
389 seq_printf(seq, "design voltage: unknown\n");
390 else
391 seq_printf(seq, "design voltage: %d mV\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400392 (u32) bif->design_voltage);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Len Brown4be44fc2005-08-05 00:44:28 -0400394 seq_printf(seq, "design capacity warning: %d %sh\n",
395 (u32) bif->design_capacity_warning, units);
396 seq_printf(seq, "design capacity low: %d %sh\n",
397 (u32) bif->design_capacity_low, units);
398 seq_printf(seq, "capacity granularity 1: %d %sh\n",
399 (u32) bif->battery_capacity_granularity_1, units);
400 seq_printf(seq, "capacity granularity 2: %d %sh\n",
401 (u32) bif->battery_capacity_granularity_2, units);
402 seq_printf(seq, "model number: %s\n", bif->model_number);
403 seq_printf(seq, "serial number: %s\n", bif->serial_number);
404 seq_printf(seq, "battery type: %s\n", bif->battery_type);
405 seq_printf(seq, "OEM info: %s\n", bif->oem_info);
406
407 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 kfree(bif);
409
Patrick Mocheld550d982006-06-27 00:41:40 -0400410 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411}
412
413static int acpi_battery_info_open_fs(struct inode *inode, struct file *file)
414{
415 return single_open(file, acpi_battery_read_info, PDE(inode)->data);
416}
417
Len Brown4be44fc2005-08-05 00:44:28 -0400418static int acpi_battery_read_state(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
Len Brown4be44fc2005-08-05 00:44:28 -0400420 int result = 0;
421 struct acpi_battery *battery = (struct acpi_battery *)seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 struct acpi_battery_status *bst = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400423 char *units = "?";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 if (!battery)
427 goto end;
428
429 if (battery->flags.present)
430 seq_printf(seq, "present: yes\n");
431 else {
432 seq_printf(seq, "present: no\n");
433 goto end;
434 }
435
436 /* Battery Units */
437
Len Brown4be44fc2005-08-05 00:44:28 -0400438 units =
439 battery->flags.
440 power_unit ? ACPI_BATTERY_UNITS_AMPS : ACPI_BATTERY_UNITS_WATTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442 /* Battery Status (_BST) */
443
444 result = acpi_battery_get_status(battery, &bst);
445 if (result || !bst) {
446 seq_printf(seq, "ERROR: Unable to read battery status\n");
447 goto end;
448 }
449
450 if (!(bst->state & 0x04))
451 seq_printf(seq, "capacity state: ok\n");
452 else
453 seq_printf(seq, "capacity state: critical\n");
454
Len Brown4be44fc2005-08-05 00:44:28 -0400455 if ((bst->state & 0x01) && (bst->state & 0x02)) {
456 seq_printf(seq,
457 "charging state: charging/discharging\n");
Len Brown4be44fc2005-08-05 00:44:28 -0400458 } else if (bst->state & 0x01)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 seq_printf(seq, "charging state: discharging\n");
460 else if (bst->state & 0x02)
461 seq_printf(seq, "charging state: charging\n");
462 else {
463 seq_printf(seq, "charging state: charged\n");
464 }
465
466 if (bst->present_rate == ACPI_BATTERY_VALUE_UNKNOWN)
467 seq_printf(seq, "present rate: unknown\n");
468 else
469 seq_printf(seq, "present rate: %d %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400470 (u32) bst->present_rate, units);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472 if (bst->remaining_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
473 seq_printf(seq, "remaining capacity: unknown\n");
474 else
475 seq_printf(seq, "remaining capacity: %d %sh\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400476 (u32) bst->remaining_capacity, units);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478 if (bst->present_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
479 seq_printf(seq, "present voltage: unknown\n");
480 else
481 seq_printf(seq, "present voltage: %d mV\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400482 (u32) bst->present_voltage);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Len Brown4be44fc2005-08-05 00:44:28 -0400484 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 kfree(bst);
486
Patrick Mocheld550d982006-06-27 00:41:40 -0400487 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488}
489
490static int acpi_battery_state_open_fs(struct inode *inode, struct file *file)
491{
492 return single_open(file, acpi_battery_read_state, PDE(inode)->data);
493}
494
Len Brown4be44fc2005-08-05 00:44:28 -0400495static int acpi_battery_read_alarm(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
Len Brown4be44fc2005-08-05 00:44:28 -0400497 struct acpi_battery *battery = (struct acpi_battery *)seq->private;
498 char *units = "?";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 if (!battery)
502 goto end;
503
504 if (!battery->flags.present) {
505 seq_printf(seq, "present: no\n");
506 goto end;
507 }
508
509 /* Battery Units */
Len Brown4be44fc2005-08-05 00:44:28 -0400510
511 units =
512 battery->flags.
513 power_unit ? ACPI_BATTERY_UNITS_AMPS : ACPI_BATTERY_UNITS_WATTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 /* Battery Alarm */
516
517 seq_printf(seq, "alarm: ");
518 if (!battery->alarm)
519 seq_printf(seq, "unsupported\n");
520 else
521 seq_printf(seq, "%d %sh\n", (u32) battery->alarm, units);
522
Len Brown4be44fc2005-08-05 00:44:28 -0400523 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400524 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525}
526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -0400528acpi_battery_write_alarm(struct file *file,
529 const char __user * buffer,
530 size_t count, loff_t * ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
Len Brown4be44fc2005-08-05 00:44:28 -0400532 int result = 0;
533 char alarm_string[12] = { '\0' };
534 struct seq_file *m = (struct seq_file *)file->private_data;
535 struct acpi_battery *battery = (struct acpi_battery *)m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 if (!battery || (count > sizeof(alarm_string) - 1))
Patrick Mocheld550d982006-06-27 00:41:40 -0400539 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541 if (!battery->flags.present)
Patrick Mocheld550d982006-06-27 00:41:40 -0400542 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544 if (copy_from_user(alarm_string, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -0400545 return -EFAULT;
Len Brown4be44fc2005-08-05 00:44:28 -0400546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 alarm_string[count] = '\0';
548
Len Brown4be44fc2005-08-05 00:44:28 -0400549 result = acpi_battery_set_alarm(battery,
550 simple_strtoul(alarm_string, NULL, 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400552 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Patrick Mocheld550d982006-06-27 00:41:40 -0400554 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555}
556
557static int acpi_battery_alarm_open_fs(struct inode *inode, struct file *file)
558{
559 return single_open(file, acpi_battery_read_alarm, PDE(inode)->data);
560}
561
Arjan van de Vend7508032006-07-04 13:06:00 -0400562static const struct file_operations acpi_battery_info_ops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400563 .open = acpi_battery_info_open_fs,
564 .read = seq_read,
565 .llseek = seq_lseek,
566 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 .owner = THIS_MODULE,
568};
569
Arjan van de Vend7508032006-07-04 13:06:00 -0400570static const struct file_operations acpi_battery_state_ops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400571 .open = acpi_battery_state_open_fs,
572 .read = seq_read,
573 .llseek = seq_lseek,
574 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 .owner = THIS_MODULE,
576};
577
Arjan van de Vend7508032006-07-04 13:06:00 -0400578static const struct file_operations acpi_battery_alarm_ops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400579 .open = acpi_battery_alarm_open_fs,
580 .read = seq_read,
581 .write = acpi_battery_write_alarm,
582 .llseek = seq_lseek,
583 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 .owner = THIS_MODULE,
585};
586
Len Brown4be44fc2005-08-05 00:44:28 -0400587static int acpi_battery_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
Len Brown4be44fc2005-08-05 00:44:28 -0400589 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592 if (!acpi_device_dir(device)) {
593 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
Len Brown4be44fc2005-08-05 00:44:28 -0400594 acpi_battery_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400596 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 acpi_device_dir(device)->owner = THIS_MODULE;
598 }
599
600 /* 'info' [R] */
601 entry = create_proc_entry(ACPI_BATTERY_FILE_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400602 S_IRUGO, acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400604 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 else {
Len Brown4be44fc2005-08-05 00:44:28 -0400606 entry->proc_fops = &acpi_battery_info_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 entry->data = acpi_driver_data(device);
608 entry->owner = THIS_MODULE;
609 }
610
611 /* 'status' [R] */
612 entry = create_proc_entry(ACPI_BATTERY_FILE_STATUS,
Len Brown4be44fc2005-08-05 00:44:28 -0400613 S_IRUGO, acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400615 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 else {
617 entry->proc_fops = &acpi_battery_state_ops;
618 entry->data = acpi_driver_data(device);
619 entry->owner = THIS_MODULE;
620 }
621
622 /* 'alarm' [R/W] */
623 entry = create_proc_entry(ACPI_BATTERY_FILE_ALARM,
Len Brown4be44fc2005-08-05 00:44:28 -0400624 S_IFREG | S_IRUGO | S_IWUSR,
625 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_alarm_ops;
630 entry->data = acpi_driver_data(device);
631 entry->owner = THIS_MODULE;
632 }
633
Patrick Mocheld550d982006-06-27 00:41:40 -0400634 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635}
636
Len Brown4be44fc2005-08-05 00:44:28 -0400637static int acpi_battery_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640 if (acpi_device_dir(device)) {
641 remove_proc_entry(ACPI_BATTERY_FILE_ALARM,
642 acpi_device_dir(device));
643 remove_proc_entry(ACPI_BATTERY_FILE_STATUS,
644 acpi_device_dir(device));
645 remove_proc_entry(ACPI_BATTERY_FILE_INFO,
646 acpi_device_dir(device));
647
648 remove_proc_entry(acpi_device_bid(device), acpi_battery_dir);
649 acpi_device_dir(device) = NULL;
650 }
651
Patrick Mocheld550d982006-06-27 00:41:40 -0400652 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653}
654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655/* --------------------------------------------------------------------------
656 Driver Interface
657 -------------------------------------------------------------------------- */
658
Len Brown4be44fc2005-08-05 00:44:28 -0400659static void acpi_battery_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
Len Brown4be44fc2005-08-05 00:44:28 -0400661 struct acpi_battery *battery = (struct acpi_battery *)data;
662 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665 if (!battery)
Patrick Mocheld550d982006-06-27 00:41:40 -0400666 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Patrick Mochel145def82006-05-19 16:54:39 -0400668 device = battery->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670 switch (event) {
671 case ACPI_BATTERY_NOTIFY_STATUS:
672 case ACPI_BATTERY_NOTIFY_INFO:
Vladimir Lebedev9fdae722006-06-27 04:49:00 -0400673 case ACPI_NOTIFY_BUS_CHECK:
674 case ACPI_NOTIFY_DEVICE_CHECK:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 acpi_battery_check(battery);
676 acpi_bus_generate_event(device, event, battery->flags.present);
677 break;
678 default:
679 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400680 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 break;
682 }
683
Patrick Mocheld550d982006-06-27 00:41:40 -0400684 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685}
686
Len Brown4be44fc2005-08-05 00:44:28 -0400687static int acpi_battery_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
Len Brown4be44fc2005-08-05 00:44:28 -0400689 int result = 0;
690 acpi_status status = 0;
691 struct acpi_battery *battery = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Len Brown4be44fc2005-08-05 00:44:28 -0400693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400695 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697 battery = kmalloc(sizeof(struct acpi_battery), GFP_KERNEL);
698 if (!battery)
Patrick Mocheld550d982006-06-27 00:41:40 -0400699 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 memset(battery, 0, sizeof(struct acpi_battery));
701
Patrick Mochel145def82006-05-19 16:54:39 -0400702 battery->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME);
704 strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS);
705 acpi_driver_data(device) = battery;
706
707 result = acpi_battery_check(battery);
708 if (result)
709 goto end;
710
711 result = acpi_battery_add_fs(device);
712 if (result)
713 goto end;
714
Patrick Mochel3b073ec2006-05-19 16:54:41 -0400715 status = acpi_install_notify_handler(device->handle,
Vladimir Lebedev9fdae722006-06-27 04:49:00 -0400716 ACPI_ALL_NOTIFY,
Len Brown4be44fc2005-08-05 00:44:28 -0400717 acpi_battery_notify, battery);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 result = -ENODEV;
720 goto end;
721 }
722
723 printk(KERN_INFO PREFIX "%s Slot [%s] (battery %s)\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400724 ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device),
725 device->status.battery_present ? "present" : "absent");
726
727 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 if (result) {
729 acpi_battery_remove_fs(device);
730 kfree(battery);
731 }
732
Patrick Mocheld550d982006-06-27 00:41:40 -0400733 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734}
735
Len Brown4be44fc2005-08-05 00:44:28 -0400736static int acpi_battery_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737{
Len Brown4be44fc2005-08-05 00:44:28 -0400738 acpi_status status = 0;
739 struct acpi_battery *battery = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
742 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400743 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Len Brown4be44fc2005-08-05 00:44:28 -0400745 battery = (struct acpi_battery *)acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Patrick Mochel3b073ec2006-05-19 16:54:41 -0400747 status = acpi_remove_notify_handler(device->handle,
Vladimir Lebedev9fdae722006-06-27 04:49:00 -0400748 ACPI_ALL_NOTIFY,
Len Brown4be44fc2005-08-05 00:44:28 -0400749 acpi_battery_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
751 acpi_battery_remove_fs(device);
752
753 kfree(battery);
754
Patrick Mocheld550d982006-06-27 00:41:40 -0400755 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756}
757
Jiri Kosina34c44152006-10-10 14:20:41 -0700758/* this is needed to learn about changes made in suspended state */
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800759static int acpi_battery_resume(struct acpi_device *device)
Jiri Kosina34c44152006-10-10 14:20:41 -0700760{
761 struct acpi_battery *battery;
762
763 if (!device)
764 return -EINVAL;
765
766 battery = device->driver_data;
767 return acpi_battery_check(battery);
768}
769
Len Brown4be44fc2005-08-05 00:44:28 -0400770static int __init acpi_battery_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
Rich Townsend3f86b832006-07-01 11:36:54 -0400772 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Pavel Machek4d8316d2006-08-14 22:37:22 -0700774 if (acpi_disabled)
775 return -ENODEV;
776
Rich Townsend3f86b832006-07-01 11:36:54 -0400777 acpi_battery_dir = acpi_lock_battery_dir();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 if (!acpi_battery_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -0400779 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
781 result = acpi_bus_register_driver(&acpi_battery_driver);
782 if (result < 0) {
Rich Townsend3f86b832006-07-01 11:36:54 -0400783 acpi_unlock_battery_dir(acpi_battery_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -0400784 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 }
786
Patrick Mocheld550d982006-06-27 00:41:40 -0400787 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788}
789
Len Brown4be44fc2005-08-05 00:44:28 -0400790static void __exit acpi_battery_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793 acpi_bus_unregister_driver(&acpi_battery_driver);
794
Rich Townsend3f86b832006-07-01 11:36:54 -0400795 acpi_unlock_battery_dir(acpi_battery_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Patrick Mocheld550d982006-06-27 00:41:40 -0400797 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798}
799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800module_init(acpi_battery_init);
801module_exit(acpi_battery_exit);