Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / acpi / battery.c
blobc1ac37ebd227dc40b7e5f070731836f4e5f600f7
1 /*
2 * battery.c - ACPI Battery Driver (Revision: 2.0)
4 * Copyright (C) 2007 Alexey Starikovskiy <astarikovskiy@suse.de>
5 * Copyright (C) 2004-2007 Vladimir Lebedev <vladimir.p.lebedev@intel.com>
6 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
7 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
9 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or (at
14 * your option) any later version.
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
25 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
28 #include <linux/kernel.h>
29 #include <linux/module.h>
30 #include <linux/init.h>
31 #include <linux/types.h>
32 #include <linux/jiffies.h>
34 #ifdef CONFIG_ACPI_PROCFS_POWER
35 #include <linux/proc_fs.h>
36 #include <linux/seq_file.h>
37 #include <asm/uaccess.h>
38 #endif
40 #include <acpi/acpi_bus.h>
41 #include <acpi/acpi_drivers.h>
43 #ifdef CONFIG_ACPI_SYSFS_POWER
44 #include <linux/power_supply.h>
45 #endif
47 #define ACPI_BATTERY_VALUE_UNKNOWN 0xFFFFFFFF
49 #define ACPI_BATTERY_COMPONENT 0x00040000
50 #define ACPI_BATTERY_CLASS "battery"
51 #define ACPI_BATTERY_DEVICE_NAME "Battery"
52 #define ACPI_BATTERY_NOTIFY_STATUS 0x80
53 #define ACPI_BATTERY_NOTIFY_INFO 0x81
55 #define _COMPONENT ACPI_BATTERY_COMPONENT
57 ACPI_MODULE_NAME("battery");
59 MODULE_AUTHOR("Paul Diefenbaugh");
60 MODULE_AUTHOR("Alexey Starikovskiy <astarikovskiy@suse.de>");
61 MODULE_DESCRIPTION("ACPI Battery Driver");
62 MODULE_LICENSE("GPL");
64 static unsigned int cache_time = 1000;
65 module_param(cache_time, uint, 0644);
66 MODULE_PARM_DESC(cache_time, "cache time in milliseconds");
68 #ifdef CONFIG_ACPI_PROCFS_POWER
69 extern struct proc_dir_entry *acpi_lock_battery_dir(void);
70 extern void *acpi_unlock_battery_dir(struct proc_dir_entry *acpi_battery_dir);
72 enum acpi_battery_files {
73 info_tag = 0,
74 state_tag,
75 alarm_tag,
76 ACPI_BATTERY_NUMFILES,
79 #endif
81 static const struct acpi_device_id battery_device_ids[] = {
82 {"PNP0C0A", 0},
83 {"", 0},
86 MODULE_DEVICE_TABLE(acpi, battery_device_ids);
89 struct acpi_battery {
90 struct mutex lock;
91 #ifdef CONFIG_ACPI_SYSFS_POWER
92 struct power_supply bat;
93 #endif
94 struct acpi_device *device;
95 unsigned long update_time;
96 int current_now;
97 int capacity_now;
98 int voltage_now;
99 int design_capacity;
100 int full_charge_capacity;
101 int technology;
102 int design_voltage;
103 int design_capacity_warning;
104 int design_capacity_low;
105 int capacity_granularity_1;
106 int capacity_granularity_2;
107 int alarm;
108 char model_number[32];
109 char serial_number[32];
110 char type[32];
111 char oem_info[32];
112 int state;
113 int power_unit;
114 u8 alarm_present;
117 #define to_acpi_battery(x) container_of(x, struct acpi_battery, bat);
119 inline int acpi_battery_present(struct acpi_battery *battery)
121 return battery->device->status.battery_present;
124 #ifdef CONFIG_ACPI_SYSFS_POWER
125 static int acpi_battery_technology(struct acpi_battery *battery)
127 if (!strcasecmp("NiCd", battery->type))
128 return POWER_SUPPLY_TECHNOLOGY_NiCd;
129 if (!strcasecmp("NiMH", battery->type))
130 return POWER_SUPPLY_TECHNOLOGY_NiMH;
131 if (!strcasecmp("LION", battery->type))
132 return POWER_SUPPLY_TECHNOLOGY_LION;
133 if (!strncasecmp("LI-ION", battery->type, 6))
134 return POWER_SUPPLY_TECHNOLOGY_LION;
135 if (!strcasecmp("LiP", battery->type))
136 return POWER_SUPPLY_TECHNOLOGY_LIPO;
137 return POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
140 static int acpi_battery_get_state(struct acpi_battery *battery);
142 static int acpi_battery_get_property(struct power_supply *psy,
143 enum power_supply_property psp,
144 union power_supply_propval *val)
146 struct acpi_battery *battery = to_acpi_battery(psy);
148 if (acpi_battery_present(battery)) {
149 /* run battery update only if it is present */
150 acpi_battery_get_state(battery);
151 } else if (psp != POWER_SUPPLY_PROP_PRESENT)
152 return -ENODEV;
153 switch (psp) {
154 case POWER_SUPPLY_PROP_STATUS:
155 if (battery->state & 0x01)
156 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
157 else if (battery->state & 0x02)
158 val->intval = POWER_SUPPLY_STATUS_CHARGING;
159 else if (battery->state == 0)
160 val->intval = POWER_SUPPLY_STATUS_FULL;
161 else
162 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
163 break;
164 case POWER_SUPPLY_PROP_PRESENT:
165 val->intval = acpi_battery_present(battery);
166 break;
167 case POWER_SUPPLY_PROP_TECHNOLOGY:
168 val->intval = acpi_battery_technology(battery);
169 break;
170 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
171 val->intval = battery->design_voltage * 1000;
172 break;
173 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
174 val->intval = battery->voltage_now * 1000;
175 break;
176 case POWER_SUPPLY_PROP_CURRENT_NOW:
177 val->intval = battery->current_now * 1000;
178 break;
179 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
180 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
181 val->intval = battery->design_capacity * 1000;
182 break;
183 case POWER_SUPPLY_PROP_CHARGE_FULL:
184 case POWER_SUPPLY_PROP_ENERGY_FULL:
185 val->intval = battery->full_charge_capacity * 1000;
186 break;
187 case POWER_SUPPLY_PROP_CHARGE_NOW:
188 case POWER_SUPPLY_PROP_ENERGY_NOW:
189 val->intval = battery->capacity_now * 1000;
190 break;
191 case POWER_SUPPLY_PROP_MODEL_NAME:
192 val->strval = battery->model_number;
193 break;
194 case POWER_SUPPLY_PROP_MANUFACTURER:
195 val->strval = battery->oem_info;
196 break;
197 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
198 val->strval = battery->serial_number;
199 break;
200 default:
201 return -EINVAL;
203 return 0;
206 static enum power_supply_property charge_battery_props[] = {
207 POWER_SUPPLY_PROP_STATUS,
208 POWER_SUPPLY_PROP_PRESENT,
209 POWER_SUPPLY_PROP_TECHNOLOGY,
210 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
211 POWER_SUPPLY_PROP_VOLTAGE_NOW,
212 POWER_SUPPLY_PROP_CURRENT_NOW,
213 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
214 POWER_SUPPLY_PROP_CHARGE_FULL,
215 POWER_SUPPLY_PROP_CHARGE_NOW,
216 POWER_SUPPLY_PROP_MODEL_NAME,
217 POWER_SUPPLY_PROP_MANUFACTURER,
218 POWER_SUPPLY_PROP_SERIAL_NUMBER,
221 static enum power_supply_property energy_battery_props[] = {
222 POWER_SUPPLY_PROP_STATUS,
223 POWER_SUPPLY_PROP_PRESENT,
224 POWER_SUPPLY_PROP_TECHNOLOGY,
225 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
226 POWER_SUPPLY_PROP_VOLTAGE_NOW,
227 POWER_SUPPLY_PROP_CURRENT_NOW,
228 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
229 POWER_SUPPLY_PROP_ENERGY_FULL,
230 POWER_SUPPLY_PROP_ENERGY_NOW,
231 POWER_SUPPLY_PROP_MODEL_NAME,
232 POWER_SUPPLY_PROP_MANUFACTURER,
233 POWER_SUPPLY_PROP_SERIAL_NUMBER,
235 #endif
237 #ifdef CONFIG_ACPI_PROCFS_POWER
238 inline char *acpi_battery_units(struct acpi_battery *battery)
240 return (battery->power_unit)?"mA":"mW";
242 #endif
244 /* --------------------------------------------------------------------------
245 Battery Management
246 -------------------------------------------------------------------------- */
247 struct acpi_offsets {
248 size_t offset; /* offset inside struct acpi_sbs_battery */
249 u8 mode; /* int or string? */
252 static struct acpi_offsets state_offsets[] = {
253 {offsetof(struct acpi_battery, state), 0},
254 {offsetof(struct acpi_battery, current_now), 0},
255 {offsetof(struct acpi_battery, capacity_now), 0},
256 {offsetof(struct acpi_battery, voltage_now), 0},
259 static struct acpi_offsets info_offsets[] = {
260 {offsetof(struct acpi_battery, power_unit), 0},
261 {offsetof(struct acpi_battery, design_capacity), 0},
262 {offsetof(struct acpi_battery, full_charge_capacity), 0},
263 {offsetof(struct acpi_battery, technology), 0},
264 {offsetof(struct acpi_battery, design_voltage), 0},
265 {offsetof(struct acpi_battery, design_capacity_warning), 0},
266 {offsetof(struct acpi_battery, design_capacity_low), 0},
267 {offsetof(struct acpi_battery, capacity_granularity_1), 0},
268 {offsetof(struct acpi_battery, capacity_granularity_2), 0},
269 {offsetof(struct acpi_battery, model_number), 1},
270 {offsetof(struct acpi_battery, serial_number), 1},
271 {offsetof(struct acpi_battery, type), 1},
272 {offsetof(struct acpi_battery, oem_info), 1},
275 static int extract_package(struct acpi_battery *battery,
276 union acpi_object *package,
277 struct acpi_offsets *offsets, int num)
279 int i;
280 union acpi_object *element;
281 if (package->type != ACPI_TYPE_PACKAGE)
282 return -EFAULT;
283 for (i = 0; i < num; ++i) {
284 if (package->package.count <= i)
285 return -EFAULT;
286 element = &package->package.elements[i];
287 if (offsets[i].mode) {
288 u8 *ptr = (u8 *)battery + offsets[i].offset;
289 if (element->type == ACPI_TYPE_STRING ||
290 element->type == ACPI_TYPE_BUFFER)
291 strncpy(ptr, element->string.pointer, 32);
292 else if (element->type == ACPI_TYPE_INTEGER) {
293 strncpy(ptr, (u8 *)&element->integer.value,
294 sizeof(acpi_integer));
295 ptr[sizeof(acpi_integer)] = 0;
296 <<<<<<< HEAD:drivers/acpi/battery.c
297 } else return -EFAULT;
298 =======
299 } else
300 *ptr = 0; /* don't have value */
301 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/acpi/battery.c
302 } else {
303 <<<<<<< HEAD:drivers/acpi/battery.c
304 if (element->type == ACPI_TYPE_INTEGER) {
305 int *x = (int *)((u8 *)battery +
306 offsets[i].offset);
307 *x = element->integer.value;
308 } else return -EFAULT;
309 =======
310 int *x = (int *)((u8 *)battery + offsets[i].offset);
311 *x = (element->type == ACPI_TYPE_INTEGER) ?
312 element->integer.value : -1;
313 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/acpi/battery.c
316 return 0;
319 static int acpi_battery_get_status(struct acpi_battery *battery)
321 if (acpi_bus_get_status(battery->device)) {
322 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Evaluating _STA"));
323 return -ENODEV;
325 return 0;
328 static int acpi_battery_get_info(struct acpi_battery *battery)
330 int result = -EFAULT;
331 acpi_status status = 0;
332 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
334 if (!acpi_battery_present(battery))
335 return 0;
336 mutex_lock(&battery->lock);
337 status = acpi_evaluate_object(battery->device->handle, "_BIF",
338 NULL, &buffer);
339 mutex_unlock(&battery->lock);
341 if (ACPI_FAILURE(status)) {
342 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BIF"));
343 return -ENODEV;
346 result = extract_package(battery, buffer.pointer,
347 info_offsets, ARRAY_SIZE(info_offsets));
348 kfree(buffer.pointer);
349 return result;
352 static int acpi_battery_get_state(struct acpi_battery *battery)
354 int result = 0;
355 acpi_status status = 0;
356 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
358 if (!acpi_battery_present(battery))
359 return 0;
361 if (battery->update_time &&
362 time_before(jiffies, battery->update_time +
363 msecs_to_jiffies(cache_time)))
364 return 0;
366 mutex_lock(&battery->lock);
367 status = acpi_evaluate_object(battery->device->handle, "_BST",
368 NULL, &buffer);
369 mutex_unlock(&battery->lock);
371 if (ACPI_FAILURE(status)) {
372 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BST"));
373 return -ENODEV;
376 result = extract_package(battery, buffer.pointer,
377 state_offsets, ARRAY_SIZE(state_offsets));
378 battery->update_time = jiffies;
379 kfree(buffer.pointer);
380 return result;
383 static int acpi_battery_set_alarm(struct acpi_battery *battery)
385 acpi_status status = 0;
386 union acpi_object arg0 = { .type = ACPI_TYPE_INTEGER };
387 struct acpi_object_list arg_list = { 1, &arg0 };
389 if (!acpi_battery_present(battery)|| !battery->alarm_present)
390 return -ENODEV;
392 arg0.integer.value = battery->alarm;
394 mutex_lock(&battery->lock);
395 status = acpi_evaluate_object(battery->device->handle, "_BTP",
396 &arg_list, NULL);
397 mutex_unlock(&battery->lock);
399 if (ACPI_FAILURE(status))
400 return -ENODEV;
402 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", battery->alarm));
403 return 0;
406 static int acpi_battery_init_alarm(struct acpi_battery *battery)
408 acpi_status status = AE_OK;
409 acpi_handle handle = NULL;
411 /* See if alarms are supported, and if so, set default */
412 status = acpi_get_handle(battery->device->handle, "_BTP", &handle);
413 if (ACPI_FAILURE(status)) {
414 battery->alarm_present = 0;
415 return 0;
417 battery->alarm_present = 1;
418 if (!battery->alarm)
419 battery->alarm = battery->design_capacity_warning;
420 return acpi_battery_set_alarm(battery);
423 #ifdef CONFIG_ACPI_SYSFS_POWER
424 static ssize_t acpi_battery_alarm_show(struct device *dev,
425 struct device_attribute *attr,
426 char *buf)
428 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
429 return sprintf(buf, "%d\n", battery->alarm * 1000);
432 static ssize_t acpi_battery_alarm_store(struct device *dev,
433 struct device_attribute *attr,
434 const char *buf, size_t count)
436 unsigned long x;
437 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
438 if (sscanf(buf, "%ld\n", &x) == 1)
439 battery->alarm = x/1000;
440 if (acpi_battery_present(battery))
441 acpi_battery_set_alarm(battery);
442 return count;
445 static struct device_attribute alarm_attr = {
446 .attr = {.name = "alarm", .mode = 0644, .owner = THIS_MODULE},
447 .show = acpi_battery_alarm_show,
448 .store = acpi_battery_alarm_store,
451 static int sysfs_add_battery(struct acpi_battery *battery)
453 int result;
455 if (battery->power_unit) {
456 battery->bat.properties = charge_battery_props;
457 battery->bat.num_properties =
458 ARRAY_SIZE(charge_battery_props);
459 } else {
460 battery->bat.properties = energy_battery_props;
461 battery->bat.num_properties =
462 ARRAY_SIZE(energy_battery_props);
465 battery->bat.name = acpi_device_bid(battery->device);
466 battery->bat.type = POWER_SUPPLY_TYPE_BATTERY;
467 battery->bat.get_property = acpi_battery_get_property;
469 result = power_supply_register(&battery->device->dev, &battery->bat);
470 if (result)
471 return result;
472 return device_create_file(battery->bat.dev, &alarm_attr);
475 static void sysfs_remove_battery(struct acpi_battery *battery)
477 if (!battery->bat.dev)
478 return;
479 device_remove_file(battery->bat.dev, &alarm_attr);
480 power_supply_unregister(&battery->bat);
481 battery->bat.dev = NULL;
483 #endif
485 static int acpi_battery_update(struct acpi_battery *battery)
487 int result;
488 result = acpi_battery_get_status(battery);
489 if (result)
490 return result;
491 #ifdef CONFIG_ACPI_SYSFS_POWER
492 if (!acpi_battery_present(battery)) {
493 sysfs_remove_battery(battery);
494 battery->update_time = 0;
495 return 0;
497 #endif
498 if (!battery->update_time) {
499 result = acpi_battery_get_info(battery);
500 if (result)
501 return result;
502 acpi_battery_init_alarm(battery);
504 #ifdef CONFIG_ACPI_SYSFS_POWER
505 if (!battery->bat.dev)
506 sysfs_add_battery(battery);
507 #endif
508 return acpi_battery_get_state(battery);
511 /* --------------------------------------------------------------------------
512 FS Interface (/proc)
513 -------------------------------------------------------------------------- */
515 #ifdef CONFIG_ACPI_PROCFS_POWER
516 static struct proc_dir_entry *acpi_battery_dir;
518 static int acpi_battery_print_info(struct seq_file *seq, int result)
520 struct acpi_battery *battery = seq->private;
522 if (result)
523 goto end;
525 seq_printf(seq, "present: %s\n",
526 acpi_battery_present(battery)?"yes":"no");
527 if (!acpi_battery_present(battery))
528 goto end;
529 if (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
530 seq_printf(seq, "design capacity: unknown\n");
531 else
532 seq_printf(seq, "design capacity: %d %sh\n",
533 battery->design_capacity,
534 acpi_battery_units(battery));
536 if (battery->full_charge_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
537 seq_printf(seq, "last full capacity: unknown\n");
538 else
539 seq_printf(seq, "last full capacity: %d %sh\n",
540 battery->full_charge_capacity,
541 acpi_battery_units(battery));
543 seq_printf(seq, "battery technology: %srechargeable\n",
544 (!battery->technology)?"non-":"");
546 if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
547 seq_printf(seq, "design voltage: unknown\n");
548 else
549 seq_printf(seq, "design voltage: %d mV\n",
550 battery->design_voltage);
551 seq_printf(seq, "design capacity warning: %d %sh\n",
552 battery->design_capacity_warning,
553 acpi_battery_units(battery));
554 seq_printf(seq, "design capacity low: %d %sh\n",
555 battery->design_capacity_low,
556 acpi_battery_units(battery));
557 seq_printf(seq, "capacity granularity 1: %d %sh\n",
558 battery->capacity_granularity_1,
559 acpi_battery_units(battery));
560 seq_printf(seq, "capacity granularity 2: %d %sh\n",
561 battery->capacity_granularity_2,
562 acpi_battery_units(battery));
563 seq_printf(seq, "model number: %s\n", battery->model_number);
564 seq_printf(seq, "serial number: %s\n", battery->serial_number);
565 seq_printf(seq, "battery type: %s\n", battery->type);
566 seq_printf(seq, "OEM info: %s\n", battery->oem_info);
567 end:
568 if (result)
569 seq_printf(seq, "ERROR: Unable to read battery info\n");
570 return result;
573 static int acpi_battery_print_state(struct seq_file *seq, int result)
575 struct acpi_battery *battery = seq->private;
577 if (result)
578 goto end;
580 seq_printf(seq, "present: %s\n",
581 acpi_battery_present(battery)?"yes":"no");
582 if (!acpi_battery_present(battery))
583 goto end;
585 seq_printf(seq, "capacity state: %s\n",
586 (battery->state & 0x04)?"critical":"ok");
587 if ((battery->state & 0x01) && (battery->state & 0x02))
588 seq_printf(seq,
589 "charging state: charging/discharging\n");
590 else if (battery->state & 0x01)
591 seq_printf(seq, "charging state: discharging\n");
592 else if (battery->state & 0x02)
593 seq_printf(seq, "charging state: charging\n");
594 else
595 seq_printf(seq, "charging state: charged\n");
597 if (battery->current_now == ACPI_BATTERY_VALUE_UNKNOWN)
598 seq_printf(seq, "present rate: unknown\n");
599 else
600 seq_printf(seq, "present rate: %d %s\n",
601 battery->current_now, acpi_battery_units(battery));
603 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN)
604 seq_printf(seq, "remaining capacity: unknown\n");
605 else
606 seq_printf(seq, "remaining capacity: %d %sh\n",
607 battery->capacity_now, acpi_battery_units(battery));
608 if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN)
609 seq_printf(seq, "present voltage: unknown\n");
610 else
611 seq_printf(seq, "present voltage: %d mV\n",
612 battery->voltage_now);
613 end:
614 if (result)
615 seq_printf(seq, "ERROR: Unable to read battery state\n");
617 return result;
620 static int acpi_battery_print_alarm(struct seq_file *seq, int result)
622 struct acpi_battery *battery = seq->private;
624 if (result)
625 goto end;
627 if (!acpi_battery_present(battery)) {
628 seq_printf(seq, "present: no\n");
629 goto end;
631 seq_printf(seq, "alarm: ");
632 if (!battery->alarm)
633 seq_printf(seq, "unsupported\n");
634 else
635 seq_printf(seq, "%u %sh\n", battery->alarm,
636 acpi_battery_units(battery));
637 end:
638 if (result)
639 seq_printf(seq, "ERROR: Unable to read battery alarm\n");
640 return result;
643 static ssize_t acpi_battery_write_alarm(struct file *file,
644 const char __user * buffer,
645 size_t count, loff_t * ppos)
647 int result = 0;
648 char alarm_string[12] = { '\0' };
649 struct seq_file *m = file->private_data;
650 struct acpi_battery *battery = m->private;
652 if (!battery || (count > sizeof(alarm_string) - 1))
653 return -EINVAL;
654 if (!acpi_battery_present(battery)) {
655 result = -ENODEV;
656 goto end;
658 if (copy_from_user(alarm_string, buffer, count)) {
659 result = -EFAULT;
660 goto end;
662 alarm_string[count] = '\0';
663 battery->alarm = simple_strtol(alarm_string, NULL, 0);
664 result = acpi_battery_set_alarm(battery);
665 end:
666 if (!result)
667 return count;
668 return result;
671 typedef int(*print_func)(struct seq_file *seq, int result);
673 static print_func acpi_print_funcs[ACPI_BATTERY_NUMFILES] = {
674 acpi_battery_print_info,
675 acpi_battery_print_state,
676 acpi_battery_print_alarm,
679 static int acpi_battery_read(int fid, struct seq_file *seq)
681 struct acpi_battery *battery = seq->private;
682 int result = acpi_battery_update(battery);
683 return acpi_print_funcs[fid](seq, result);
686 #define DECLARE_FILE_FUNCTIONS(_name) \
687 static int acpi_battery_read_##_name(struct seq_file *seq, void *offset) \
689 return acpi_battery_read(_name##_tag, seq); \
691 static int acpi_battery_##_name##_open_fs(struct inode *inode, struct file *file) \
693 return single_open(file, acpi_battery_read_##_name, PDE(inode)->data); \
696 DECLARE_FILE_FUNCTIONS(info);
697 DECLARE_FILE_FUNCTIONS(state);
698 DECLARE_FILE_FUNCTIONS(alarm);
700 #undef DECLARE_FILE_FUNCTIONS
702 #define FILE_DESCRIPTION_RO(_name) \
704 .name = __stringify(_name), \
705 .mode = S_IRUGO, \
706 .ops = { \
707 .open = acpi_battery_##_name##_open_fs, \
708 .read = seq_read, \
709 .llseek = seq_lseek, \
710 .release = single_release, \
711 .owner = THIS_MODULE, \
712 }, \
715 #define FILE_DESCRIPTION_RW(_name) \
717 .name = __stringify(_name), \
718 .mode = S_IFREG | S_IRUGO | S_IWUSR, \
719 .ops = { \
720 .open = acpi_battery_##_name##_open_fs, \
721 .read = seq_read, \
722 .llseek = seq_lseek, \
723 .write = acpi_battery_write_##_name, \
724 .release = single_release, \
725 .owner = THIS_MODULE, \
726 }, \
729 static struct battery_file {
730 struct file_operations ops;
731 mode_t mode;
732 char *name;
733 } acpi_battery_file[] = {
734 FILE_DESCRIPTION_RO(info),
735 FILE_DESCRIPTION_RO(state),
736 FILE_DESCRIPTION_RW(alarm),
739 #undef FILE_DESCRIPTION_RO
740 #undef FILE_DESCRIPTION_RW
742 static int acpi_battery_add_fs(struct acpi_device *device)
744 struct proc_dir_entry *entry = NULL;
745 int i;
747 if (!acpi_device_dir(device)) {
748 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
749 acpi_battery_dir);
750 if (!acpi_device_dir(device))
751 return -ENODEV;
752 acpi_device_dir(device)->owner = THIS_MODULE;
755 for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i) {
756 entry = create_proc_entry(acpi_battery_file[i].name,
757 acpi_battery_file[i].mode, acpi_device_dir(device));
758 if (!entry)
759 return -ENODEV;
760 else {
761 entry->proc_fops = &acpi_battery_file[i].ops;
762 entry->data = acpi_driver_data(device);
763 entry->owner = THIS_MODULE;
766 return 0;
769 static void acpi_battery_remove_fs(struct acpi_device *device)
771 int i;
772 if (!acpi_device_dir(device))
773 return;
774 for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i)
775 remove_proc_entry(acpi_battery_file[i].name,
776 acpi_device_dir(device));
778 remove_proc_entry(acpi_device_bid(device), acpi_battery_dir);
779 acpi_device_dir(device) = NULL;
782 #endif
784 /* --------------------------------------------------------------------------
785 Driver Interface
786 -------------------------------------------------------------------------- */
788 static void acpi_battery_notify(acpi_handle handle, u32 event, void *data)
790 struct acpi_battery *battery = data;
791 struct acpi_device *device;
792 if (!battery)
793 return;
794 device = battery->device;
795 acpi_battery_update(battery);
796 acpi_bus_generate_proc_event(device, event,
797 acpi_battery_present(battery));
798 acpi_bus_generate_netlink_event(device->pnp.device_class,
799 device->dev.bus_id, event,
800 acpi_battery_present(battery));
801 #ifdef CONFIG_ACPI_SYSFS_POWER
802 /* acpi_batter_update could remove power_supply object */
803 if (battery->bat.dev)
804 kobject_uevent(&battery->bat.dev->kobj, KOBJ_CHANGE);
805 #endif
808 static int acpi_battery_add(struct acpi_device *device)
810 int result = 0;
811 acpi_status status = 0;
812 struct acpi_battery *battery = NULL;
813 if (!device)
814 return -EINVAL;
815 battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL);
816 if (!battery)
817 return -ENOMEM;
818 battery->device = device;
819 strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME);
820 strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS);
821 acpi_driver_data(device) = battery;
822 mutex_init(&battery->lock);
823 acpi_battery_update(battery);
824 #ifdef CONFIG_ACPI_PROCFS_POWER
825 result = acpi_battery_add_fs(device);
826 if (result)
827 goto end;
828 #endif
829 status = acpi_install_notify_handler(device->handle,
830 ACPI_ALL_NOTIFY,
831 acpi_battery_notify, battery);
832 if (ACPI_FAILURE(status)) {
833 ACPI_EXCEPTION((AE_INFO, status, "Installing notify handler"));
834 result = -ENODEV;
835 goto end;
837 printk(KERN_INFO PREFIX "%s Slot [%s] (battery %s)\n",
838 ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device),
839 device->status.battery_present ? "present" : "absent");
840 end:
841 if (result) {
842 #ifdef CONFIG_ACPI_PROCFS_POWER
843 acpi_battery_remove_fs(device);
844 #endif
845 kfree(battery);
847 return result;
850 static int acpi_battery_remove(struct acpi_device *device, int type)
852 acpi_status status = 0;
853 struct acpi_battery *battery = NULL;
855 if (!device || !acpi_driver_data(device))
856 return -EINVAL;
857 battery = acpi_driver_data(device);
858 status = acpi_remove_notify_handler(device->handle,
859 ACPI_ALL_NOTIFY,
860 acpi_battery_notify);
861 #ifdef CONFIG_ACPI_PROCFS_POWER
862 acpi_battery_remove_fs(device);
863 #endif
864 #ifdef CONFIG_ACPI_SYSFS_POWER
865 sysfs_remove_battery(battery);
866 #endif
867 mutex_destroy(&battery->lock);
868 kfree(battery);
869 return 0;
872 /* this is needed to learn about changes made in suspended state */
873 static int acpi_battery_resume(struct acpi_device *device)
875 struct acpi_battery *battery;
876 if (!device)
877 return -EINVAL;
878 battery = acpi_driver_data(device);
879 battery->update_time = 0;
880 acpi_battery_update(battery);
881 return 0;
884 static struct acpi_driver acpi_battery_driver = {
885 .name = "battery",
886 .class = ACPI_BATTERY_CLASS,
887 .ids = battery_device_ids,
888 .ops = {
889 .add = acpi_battery_add,
890 .resume = acpi_battery_resume,
891 .remove = acpi_battery_remove,
895 static int __init acpi_battery_init(void)
897 if (acpi_disabled)
898 return -ENODEV;
899 #ifdef CONFIG_ACPI_PROCFS_POWER
900 acpi_battery_dir = acpi_lock_battery_dir();
901 if (!acpi_battery_dir)
902 return -ENODEV;
903 #endif
904 if (acpi_bus_register_driver(&acpi_battery_driver) < 0) {
905 #ifdef CONFIG_ACPI_PROCFS_POWER
906 acpi_unlock_battery_dir(acpi_battery_dir);
907 #endif
908 return -ENODEV;
910 return 0;
913 static void __exit acpi_battery_exit(void)
915 acpi_bus_unregister_driver(&acpi_battery_driver);
916 #ifdef CONFIG_ACPI_PROCFS_POWER
917 acpi_unlock_battery_dir(acpi_battery_dir);
918 #endif
921 module_init(acpi_battery_init);
922 module_exit(acpi_battery_exit);