28 #include <linux/kernel.h>
29 #include <linux/module.h>
31 #include <linux/types.h>
35 #include <linux/slab.h>
37 #include <asm/unaligned.h>
39 #ifdef CONFIG_ACPI_PROCFS_POWER
42 #include <asm/uaccess.h>
49 #define PREFIX "ACPI: "
51 #define ACPI_BATTERY_VALUE_UNKNOWN 0xFFFFFFFF
53 #define ACPI_BATTERY_CLASS "battery"
54 #define ACPI_BATTERY_DEVICE_NAME "Battery"
55 #define ACPI_BATTERY_NOTIFY_STATUS 0x80
56 #define ACPI_BATTERY_NOTIFY_INFO 0x81
57 #define ACPI_BATTERY_NOTIFY_THRESHOLD 0x82
60 #define ACPI_BATTERY_POWER_UNIT_MA 1
62 #define _COMPONENT ACPI_BATTERY_COMPONENT
71 static unsigned int cache_time = 1000;
75 #ifdef CONFIG_ACPI_PROCFS_POWER
79 enum acpi_battery_files {
83 ACPI_BATTERY_NUMFILES,
147 #define to_acpi_battery(x) container_of(x, struct acpi_battery, bat)
151 return battery->
device->status.battery_present;
154 static int acpi_battery_technology(
struct acpi_battery *battery)
169 static int acpi_battery_get_state(
struct acpi_battery *battery);
171 static int acpi_battery_is_charged(
struct acpi_battery *battery)
174 if (battery->
state != 0)
194 static int acpi_battery_get_property(
struct power_supply *psy,
203 acpi_battery_get_state(battery);
208 if (battery->
state & 0x01)
210 else if (battery->
state & 0x02)
212 else if (acpi_battery_is_charged(battery))
221 val->
intval = acpi_battery_technology(battery);
322 #ifdef CONFIG_ACPI_PROCFS_POWER
323 inline char *acpi_battery_units(
struct acpi_battery *battery)
383 static int extract_package(
struct acpi_battery *battery,
391 for (i = 0; i < num; ++
i) {
392 if (package->
package.count <= i)
394 element = &package->
package.elements[
i];
395 if (offsets[i].
mode) {
403 ptr[
sizeof(
u64)] = 0;
407 int *
x = (
int *)((
u8 *)battery + offsets[
i].
offset);
415 static int acpi_battery_get_status(
struct acpi_battery *battery)
424 static int acpi_battery_get_info(
struct acpi_battery *battery)
445 result = extract_package(battery, buffer.
pointer,
446 extended_info_offsets,
449 result = extract_package(battery, buffer.
pointer,
472 static int acpi_battery_get_state(
struct acpi_battery *battery)
496 result = extract_package(battery, buffer.
pointer,
525 static int acpi_battery_set_alarm(
struct acpi_battery *battery)
549 static int acpi_battery_init_alarm(
struct acpi_battery *battery)
563 return acpi_battery_set_alarm(battery);
576 const char *buf,
size_t count)
580 if (
sscanf(buf,
"%ld\n", &x) == 1)
581 battery->
alarm = x/1000;
583 acpi_battery_set_alarm(battery);
588 .attr = {.name =
"alarm", .mode = 0644},
589 .show = acpi_battery_alarm_show,
590 .store = acpi_battery_alarm_store,
593 static int sysfs_add_battery(
struct acpi_battery *battery)
598 battery->
bat.properties = charge_battery_props;
599 battery->
bat.num_properties =
602 battery->
bat.properties = energy_battery_props;
603 battery->
bat.num_properties =
607 battery->
bat.name = acpi_device_bid(battery->
device);
609 battery->
bat.get_property = acpi_battery_get_property;
617 static void sysfs_remove_battery(
struct acpi_battery *battery)
620 if (!battery->
bat.dev) {
631 static void find_battery(
const struct dmi_header *dm,
void *
private)
637 const u8 *dmi_data = (
const u8 *)(dm + 1);
640 dmi_capacity *= dmi_data[17];
661 static void acpi_battery_quirks(
struct acpi_battery *battery)
681 if (s && !
strnicmp(s,
"ThinkPad", 8)) {
702 static int acpi_battery_update(
struct acpi_battery *battery)
705 result = acpi_battery_get_status(battery);
709 sysfs_remove_battery(battery);
715 result = acpi_battery_get_info(battery);
718 acpi_battery_init_alarm(battery);
720 if (!battery->
bat.dev) {
721 result = sysfs_add_battery(battery);
725 result = acpi_battery_get_state(battery);
726 acpi_battery_quirks(battery);
730 static void acpi_battery_refresh(
struct acpi_battery *battery)
734 if (!battery->
bat.dev)
739 acpi_battery_get_info(battery);
745 sysfs_remove_battery(battery);
746 sysfs_add_battery(battery);
753 #ifdef CONFIG_ACPI_PROCFS_POWER
756 static int acpi_battery_print_info(
struct seq_file *seq,
int result)
768 seq_printf(seq,
"design capacity: unknown\n");
772 acpi_battery_units(battery));
775 seq_printf(seq,
"last full capacity: unknown\n");
777 seq_printf(seq,
"last full capacity: %d %sh\n",
779 acpi_battery_units(battery));
781 seq_printf(seq,
"battery technology: %srechargeable\n",
789 seq_printf(seq,
"design capacity warning: %d %sh\n",
791 acpi_battery_units(battery));
792 seq_printf(seq,
"design capacity low: %d %sh\n",
794 acpi_battery_units(battery));
796 seq_printf(seq,
"capacity granularity 1: %d %sh\n",
798 acpi_battery_units(battery));
799 seq_printf(seq,
"capacity granularity 2: %d %sh\n",
801 acpi_battery_units(battery));
808 seq_printf(seq,
"ERROR: Unable to read battery info\n");
812 static int acpi_battery_print_state(
struct seq_file *seq,
int result)
825 (battery->
state & 0x04)?
"critical":
"ok");
826 if ((battery->
state & 0x01) && (battery->
state & 0x02))
828 "charging state: charging/discharging\n");
829 else if (battery->
state & 0x01)
830 seq_printf(seq,
"charging state: discharging\n");
831 else if (battery->
state & 0x02)
832 seq_printf(seq,
"charging state: charging\n");
840 battery->
rate_now, acpi_battery_units(battery));
843 seq_printf(seq,
"remaining capacity: unknown\n");
845 seq_printf(seq,
"remaining capacity: %d %sh\n",
848 seq_printf(seq,
"present voltage: unknown\n");
854 seq_printf(seq,
"ERROR: Unable to read battery state\n");
859 static int acpi_battery_print_alarm(
struct seq_file *seq,
int result)
875 acpi_battery_units(battery));
878 seq_printf(seq,
"ERROR: Unable to read battery alarm\n");
883 const char __user * buffer,
884 size_t count, loff_t * ppos)
887 char alarm_string[12] = {
'\0' };
891 if (!battery || (count >
sizeof(alarm_string) - 1))
901 alarm_string[
count] =
'\0';
903 result = acpi_battery_set_alarm(battery);
912 static print_func acpi_print_funcs[ACPI_BATTERY_NUMFILES] = {
913 acpi_battery_print_info,
914 acpi_battery_print_state,
915 acpi_battery_print_alarm,
918 static int acpi_battery_read(
int fid,
struct seq_file *seq)
921 int result = acpi_battery_update(battery);
922 return acpi_print_funcs[
fid](seq,
result);
925 #define DECLARE_FILE_FUNCTIONS(_name) \
926 static int acpi_battery_read_##_name(struct seq_file *seq, void *offset) \
928 return acpi_battery_read(_name##_tag, seq); \
930 static int acpi_battery_##_name##_open_fs(struct inode *inode, struct file *file) \
932 return single_open(file, acpi_battery_read_##_name, PDE(inode)->data); \
935 DECLARE_FILE_FUNCTIONS(
info);
936 DECLARE_FILE_FUNCTIONS(
state);
937 DECLARE_FILE_FUNCTIONS(
alarm);
939 #undef DECLARE_FILE_FUNCTIONS
941 #define FILE_DESCRIPTION_RO(_name) \
943 .name = __stringify(_name), \
946 .open = acpi_battery_##_name##_open_fs, \
948 .llseek = seq_lseek, \
949 .release = single_release, \
950 .owner = THIS_MODULE, \
954 #define FILE_DESCRIPTION_RW(_name) \
956 .name = __stringify(_name), \
957 .mode = S_IFREG | S_IRUGO | S_IWUSR, \
959 .open = acpi_battery_##_name##_open_fs, \
961 .llseek = seq_lseek, \
962 .write = acpi_battery_write_##_name, \
963 .release = single_release, \
964 .owner = THIS_MODULE, \
968 static const struct battery_file {
972 } acpi_battery_file[] = {
973 FILE_DESCRIPTION_RO(
info),
974 FILE_DESCRIPTION_RO(
state),
975 FILE_DESCRIPTION_RW(
alarm),
978 #undef FILE_DESCRIPTION_RO
979 #undef FILE_DESCRIPTION_RW
981 static int acpi_battery_add_fs(
struct acpi_device *
device)
987 " please retry with CONFIG_ACPI_PROCFS_POWER cleared\n");
988 if (!acpi_device_dir(device)) {
989 acpi_device_dir(device) =
proc_mkdir(acpi_device_bid(device),
991 if (!acpi_device_dir(device))
995 for (i = 0; i < ACPI_BATTERY_NUMFILES; ++
i) {
997 acpi_battery_file[i].mode,
998 acpi_device_dir(device),
999 &acpi_battery_file[i].ops,
1000 acpi_driver_data(device));
1007 static void acpi_battery_remove_fs(
struct acpi_device *device)
1010 if (!acpi_device_dir(device))
1012 for (i = 0; i < ACPI_BATTERY_NUMFILES; ++
i)
1014 acpi_device_dir(device));
1017 acpi_device_dir(device) =
NULL;
1026 static void acpi_battery_notify(
struct acpi_device *device,
u32 event)
1028 struct acpi_battery *battery = acpi_driver_data(device);
1033 old = battery->
bat.dev;
1035 acpi_battery_refresh(battery);
1036 acpi_battery_update(battery);
1037 acpi_bus_generate_proc_event(device, event,
1040 dev_name(&device->dev), event,
1043 if (old && battery->
bat.dev)
1048 unsigned long mode,
void *
_unused)
1055 if (battery->
bat.dev) {
1056 sysfs_remove_battery(battery);
1057 sysfs_add_battery(battery);
1065 static int acpi_battery_add(
struct acpi_device *device)
1078 device->driver_data = battery;
1084 result = acpi_battery_update(battery);
1087 #ifdef CONFIG_ACPI_PROCFS_POWER
1088 result = acpi_battery_add_fs(device);
1091 #ifdef CONFIG_ACPI_PROCFS_POWER
1092 acpi_battery_remove_fs(device);
1099 device->status.battery_present ?
"present" :
"absent");
1101 battery->
pm_nb.notifier_call = battery_notify;
1102 register_pm_notifier(&battery->
pm_nb);
1107 sysfs_remove_battery(battery);
1114 static int acpi_battery_remove(
struct acpi_device *device,
int type)
1118 if (!device || !acpi_driver_data(device))
1120 battery = acpi_driver_data(device);
1121 unregister_pm_notifier(&battery->
pm_nb);
1122 #ifdef CONFIG_ACPI_PROCFS_POWER
1123 acpi_battery_remove_fs(device);
1125 sysfs_remove_battery(battery);
1132 #ifdef CONFIG_PM_SLEEP
1134 static int acpi_battery_resume(
struct device *dev)
1141 battery = acpi_driver_data(to_acpi_device(dev));
1146 acpi_battery_update(battery);
1153 static struct acpi_driver acpi_battery_driver = {
1156 .ids = battery_device_ids,
1157 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
1159 .add = acpi_battery_add,
1160 .remove = acpi_battery_remove,
1161 .notify = acpi_battery_notify,
1163 .drv.pm = &acpi_battery_pm,
1170 #ifdef CONFIG_ACPI_PROCFS_POWER
1172 if (!acpi_battery_dir)
1176 #ifdef CONFIG_ACPI_PROCFS_POWER
1184 static int __init acpi_battery_init(
void)
1190 static void __exit acpi_battery_exit(
void)
1193 #ifdef CONFIG_ACPI_PROCFS_POWER