28 #include <linux/slab.h>
29 #include <linux/module.h>
31 #include <linux/kernel.h>
33 #ifdef CONFIG_ACPI_PROCFS_POWER
36 #include <asm/uaccess.h>
47 #define PREFIX "ACPI: "
49 #define ACPI_SBS_CLASS "sbs"
50 #define ACPI_AC_CLASS "ac_adapter"
51 #define ACPI_BATTERY_CLASS "battery"
52 #define ACPI_SBS_DEVICE_NAME "Smart Battery System"
53 #define ACPI_SBS_FILE_INFO "info"
54 #define ACPI_SBS_FILE_STATE "state"
55 #define ACPI_SBS_FILE_ALARM "alarm"
56 #define ACPI_BATTERY_DIR_NAME "BAT%i"
57 #define ACPI_AC_DIR_NAME "AC0"
59 #define ACPI_SBS_NOTIFY_STATUS 0x80
60 #define ACPI_SBS_NOTIFY_INFO 0x81
66 static unsigned int cache_time = 1000;
76 #define ACPI_SBS_BLOCK_MAX 32
87 #ifdef CONFIG_ACPI_PROCFS_POWER
115 #define to_acpi_battery(x) container_of(x, struct acpi_battery, bat)
122 #ifdef CONFIG_ACPI_PROCFS_POWER
131 #define to_acpi_sbs(x) container_of(x, struct acpi_sbs, charger)
133 static int acpi_sbs_remove(
struct acpi_device *
device,
int type);
134 static int acpi_battery_get_state(
struct acpi_battery *battery);
136 static inline int battery_scale(
int log)
144 static inline int acpi_battery_vscale(
struct acpi_battery *battery)
146 return battery_scale((battery->
spec & 0x0f00) >> 8);
149 static inline int acpi_battery_ipscale(
struct acpi_battery *battery)
151 return battery_scale((battery->
spec & 0xf000) >> 12);
154 static inline int acpi_battery_mode(
struct acpi_battery *battery)
156 return (battery->
mode & 0x8000);
159 static inline int acpi_battery_scale(
struct acpi_battery *battery)
161 return (acpi_battery_mode(battery) ? 10 : 1) *
162 acpi_battery_ipscale(battery);
165 static int sbs_get_ac_property(
struct power_supply *psy,
180 static int acpi_battery_technology(
struct acpi_battery *battery)
193 static int acpi_sbs_battery_get_property(
struct power_supply *psy,
202 acpi_battery_get_state(battery);
216 val->
intval = acpi_battery_technology(battery);
223 acpi_battery_vscale(battery) * 1000;
227 acpi_battery_vscale(battery) * 1000;
232 acpi_battery_ipscale(battery) * 1000;
233 val->
intval *= (acpi_battery_mode(battery)) ?
235 acpi_battery_vscale(battery) / 1000) : 1;
240 acpi_battery_ipscale(battery) * 1000;
241 val->
intval *= (acpi_battery_mode(battery)) ?
243 acpi_battery_vscale(battery) / 1000) : 1;
251 acpi_battery_scale(battery) * 1000;
256 acpi_battery_scale(battery) * 1000;
261 acpi_battery_scale(battery) * 1000;
354 static int acpi_manager_get_info(
struct acpi_sbs *sbs)
357 u16 battery_system_info;
360 0x04, (
u8 *)&battery_system_info);
366 static int acpi_battery_get_info(
struct acpi_battery *battery)
372 info_readers[i].
mode,
383 static int acpi_battery_get_state(
struct acpi_battery *battery)
393 state_readers[i].
mode,
406 static int acpi_battery_get_alarm(
struct acpi_battery *battery)
413 static int acpi_battery_set_alarm(
struct acpi_battery *battery)
426 if ((value & 0xf000) != sel) {
431 0x01, (
u8 *)&value, 2);
442 static int acpi_ac_get_present(
struct acpi_sbs *sbs)
448 0x13, (
u8 *) & status);
459 acpi_battery_get_alarm(battery);
461 acpi_battery_scale(battery) * 1000);
466 const char *buf,
size_t count)
470 if (
sscanf(buf,
"%ld\n", &x) == 1)
472 (1000 * acpi_battery_scale(battery));
474 acpi_battery_set_alarm(battery);
479 .attr = {.name =
"alarm", .mode = 0644},
480 .show = acpi_battery_alarm_show,
481 .store = acpi_battery_alarm_store,
488 #ifdef CONFIG_ACPI_PROCFS_POWER
499 " please retry with CONFIG_ACPI_PROCFS_POWER cleared\n");
540 static inline char *acpi_battery_units(
struct acpi_battery *battery)
542 return acpi_battery_mode(battery) ?
" mW" :
" mA";
546 static int acpi_battery_read_info(
struct seq_file *seq,
void *
offset)
555 (battery->
present) ?
"yes" :
"no");
561 acpi_battery_units(battery));
562 seq_printf(seq,
"last full capacity: %i%sh\n",
564 acpi_battery_units(battery));
565 seq_printf(seq,
"battery technology: rechargeable\n");
568 seq_printf(seq,
"design capacity warning: unknown\n");
569 seq_printf(seq,
"design capacity low: unknown\n");
571 seq_printf(seq,
"capacity granularity 1: unknown\n");
572 seq_printf(seq,
"capacity granularity 2: unknown\n");
587 return single_open(file, acpi_battery_read_info, PDE(inode)->data);
590 static int acpi_battery_read_state(
struct seq_file *seq,
void *offset)
598 (battery->
present) ?
"yes" :
"no");
602 acpi_battery_get_state(battery);
604 (battery->
state & 0x0010) ?
"critical" :
"ok");
606 (battery->
rate_now < 0) ?
"discharging" :
607 ((battery->
rate_now > 0) ?
"charging" :
"charged"));
608 rate =
abs(battery->
rate_now) * acpi_battery_ipscale(battery);
609 rate *= (acpi_battery_mode(battery))?(battery->
voltage_now *
610 acpi_battery_vscale(battery)/1000):1;
612 acpi_battery_units(battery));
613 seq_printf(seq,
"remaining capacity: %i%sh\n",
615 acpi_battery_units(battery));
617 battery->
voltage_now * acpi_battery_vscale(battery));
624 static int acpi_battery_state_open_fs(
struct inode *inode,
struct file *file)
626 return single_open(file, acpi_battery_read_state, PDE(inode)->data);
629 static int acpi_battery_read_alarm(
struct seq_file *seq,
void *offset)
642 acpi_battery_get_alarm(battery);
647 acpi_battery_scale(battery),
648 acpi_battery_units(battery));
657 acpi_battery_write_alarm(
struct file *file,
const char __user *
buffer,
658 size_t count, loff_t * ppos)
663 char alarm_string[12] = {
'\0' };
670 if (count >
sizeof(alarm_string) - 1) {
678 alarm_string[
count] = 0;
680 acpi_battery_scale(battery);
681 acpi_battery_set_alarm(battery);
689 static int acpi_battery_alarm_open_fs(
struct inode *inode,
struct file *file)
691 return single_open(file, acpi_battery_read_alarm, PDE(inode)->data);
695 .
open = acpi_battery_info_open_fs,
703 .
open = acpi_battery_state_open_fs,
711 .
open = acpi_battery_alarm_open_fs,
713 .write = acpi_battery_write_alarm,
723 static int acpi_ac_read_state(
struct seq_file *seq,
void *offset)
737 static int acpi_ac_state_open_fs(
struct inode *inode,
struct file *file)
739 return single_open(file, acpi_ac_read_state, PDE(inode)->data);
743 .
open = acpi_ac_state_open_fs,
755 static int acpi_battery_read(
struct acpi_battery *battery)
757 int result = 0, saved_present = battery->
present;
760 if (battery->
sbs->manager_present) {
764 battery->
present = state & (1 << battery->
id);
766 state |= 1 << (battery->
id + 12);
769 }
else if (battery->
id == 0)
771 if (result || !battery->
present)
774 if (saved_present != battery->
present) {
776 result = acpi_battery_get_info(battery);
780 result = acpi_battery_get_state(battery);
785 static int acpi_battery_add(
struct acpi_sbs *sbs,
int id)
792 result = acpi_battery_read(battery);
797 #ifdef CONFIG_ACPI_PROCFS_POWER
798 acpi_sbs_add_fs(&battery->proc_entry, acpi_battery_dir,
799 battery->
name, &acpi_battery_info_fops,
800 &acpi_battery_state_fops, &acpi_battery_alarm_fops,
803 battery->
bat.name = battery->
name;
805 if (!acpi_battery_mode(battery)) {
806 battery->
bat.properties = sbs_charge_battery_props;
807 battery->
bat.num_properties =
810 battery->
bat.properties = sbs_energy_battery_props;
811 battery->
bat.num_properties =
814 battery->
bat.get_property = acpi_sbs_battery_get_property;
825 battery->
name, battery->
present ?
"present" :
"absent");
829 static void acpi_battery_remove(
struct acpi_sbs *sbs,
int id)
833 if (battery->
bat.dev) {
838 #ifdef CONFIG_ACPI_PROCFS_POWER
839 if (battery->proc_entry)
840 acpi_sbs_remove_fs(&battery->proc_entry, acpi_battery_dir);
844 static int acpi_charger_add(
struct acpi_sbs *sbs)
848 result = acpi_ac_get_present(sbs);
851 #ifdef CONFIG_ACPI_PROCFS_POWER
852 result = acpi_sbs_add_fs(&sbs->charger_entry, acpi_ac_dir,
854 &acpi_ac_state_fops,
NULL, sbs);
858 sbs->
charger.name =
"sbs-charger";
860 sbs->
charger.properties = sbs_ac_props;
862 sbs->
charger.get_property = sbs_get_ac_property;
871 static void acpi_charger_remove(
struct acpi_sbs *sbs)
875 #ifdef CONFIG_ACPI_PROCFS_POWER
876 if (sbs->charger_entry)
877 acpi_sbs_remove_fs(&sbs->charger_entry, acpi_ac_dir);
881 static void acpi_sbs_callback(
void *
context)
887 u8 saved_battery_state;
888 acpi_ac_get_present(sbs);
890 #ifdef CONFIG_ACPI_PROC_EVENT
902 saved_battery_state = bat->
present;
903 acpi_battery_read(bat);
904 if (saved_battery_state == bat->
present)
906 #ifdef CONFIG_ACPI_PROC_EVENT
917 static int acpi_sbs_add(
struct acpi_device *
device)
931 sbs->
hc = acpi_driver_data(device->parent);
935 device->driver_data = sbs;
937 result = acpi_charger_add(sbs);
941 result = acpi_manager_get_info(sbs);
946 acpi_battery_add(sbs,
id);
948 acpi_battery_add(sbs, 0);
952 acpi_sbs_remove(device, 0);
956 static int acpi_sbs_remove(
struct acpi_device *device,
int type)
963 sbs = acpi_driver_data(device);
969 acpi_battery_remove(sbs,
id);
970 acpi_charger_remove(sbs);
977 static void acpi_sbs_rmdirs(
void)
979 #ifdef CONFIG_ACPI_PROCFS_POWER
984 if (acpi_battery_dir) {
986 acpi_battery_dir =
NULL;
991 #ifdef CONFIG_PM_SLEEP
992 static int acpi_sbs_resume(
struct device *dev)
997 sbs = to_acpi_device(dev)->driver_data;
998 acpi_sbs_callback(sbs);
1005 static struct acpi_driver acpi_sbs_driver = {
1008 .ids = sbs_device_ids,
1010 .add = acpi_sbs_add,
1011 .remove = acpi_sbs_remove,
1013 .drv.pm = &acpi_sbs_pm,
1016 static int __init acpi_sbs_init(
void)
1022 #ifdef CONFIG_ACPI_PROCFS_POWER
1027 if (!acpi_battery_dir) {
1040 static void __exit acpi_sbs_exit(
void)