Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame^] | 2 | * battery.c - ACPI Battery Driver (Revision: 2.0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame^] | 4 | * Copyright (C) 2007 Alexey Starikovskiy <astarikovskiy@suse.de> |
| 5 | * Copyright (C) 2004-2007 Vladimir Lebedev <vladimir.p.lebedev@intel.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com> |
| 7 | * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com> |
| 8 | * |
| 9 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 10 | * |
| 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. |
| 15 | * |
| 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. |
| 20 | * |
| 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. |
| 24 | * |
| 25 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 26 | */ |
| 27 | |
| 28 | #include <linux/kernel.h> |
| 29 | #include <linux/module.h> |
| 30 | #include <linux/init.h> |
| 31 | #include <linux/types.h> |
Alexey Starikovskiy | f1d4661 | 2007-09-26 19:42:52 +0400 | [diff] [blame] | 32 | #include <linux/jiffies.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <linux/proc_fs.h> |
| 34 | #include <linux/seq_file.h> |
| 35 | #include <asm/uaccess.h> |
| 36 | |
| 37 | #include <acpi/acpi_bus.h> |
| 38 | #include <acpi/acpi_drivers.h> |
| 39 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #define ACPI_BATTERY_VALUE_UNKNOWN 0xFFFFFFFF |
| 41 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #define ACPI_BATTERY_COMPONENT 0x00040000 |
| 43 | #define ACPI_BATTERY_CLASS "battery" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #define ACPI_BATTERY_DEVICE_NAME "Battery" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #define ACPI_BATTERY_NOTIFY_STATUS 0x80 |
| 46 | #define ACPI_BATTERY_NOTIFY_INFO 0x81 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | #define _COMPONENT ACPI_BATTERY_COMPONENT |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 49 | |
Len Brown | f52fd66 | 2007-02-12 22:42:12 -0500 | [diff] [blame] | 50 | ACPI_MODULE_NAME("battery"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
Len Brown | f52fd66 | 2007-02-12 22:42:12 -0500 | [diff] [blame] | 52 | MODULE_AUTHOR("Paul Diefenbaugh"); |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame^] | 53 | MODULE_AUTHOR("Alexey Starikovskiy <astarikovskiy@suse.de>"); |
Len Brown | 7cda93e | 2007-02-12 23:50:02 -0500 | [diff] [blame] | 54 | MODULE_DESCRIPTION("ACPI Battery Driver"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | MODULE_LICENSE("GPL"); |
| 56 | |
Alexey Starikovskiy | f1d4661 | 2007-09-26 19:42:52 +0400 | [diff] [blame] | 57 | static unsigned int cache_time = 1000; |
| 58 | module_param(cache_time, uint, 0644); |
| 59 | MODULE_PARM_DESC(cache_time, "cache time in milliseconds"); |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 60 | |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 61 | extern struct proc_dir_entry *acpi_lock_battery_dir(void); |
| 62 | extern void *acpi_unlock_battery_dir(struct proc_dir_entry *acpi_battery_dir); |
| 63 | |
Thomas Renninger | 1ba90e3 | 2007-07-23 14:44:41 +0200 | [diff] [blame] | 64 | static const struct acpi_device_id battery_device_ids[] = { |
| 65 | {"PNP0C0A", 0}, |
| 66 | {"", 0}, |
| 67 | }; |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame^] | 68 | |
Thomas Renninger | 1ba90e3 | 2007-07-23 14:44:41 +0200 | [diff] [blame] | 69 | MODULE_DEVICE_TABLE(acpi, battery_device_ids); |
| 70 | |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 71 | enum acpi_battery_files { |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame^] | 72 | info_tag = 0, |
| 73 | state_tag, |
| 74 | alarm_tag, |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 75 | ACPI_BATTERY_NUMFILES, |
| 76 | }; |
| 77 | |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 78 | struct acpi_battery { |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 79 | struct mutex lock; |
Alexey Starikovskiy | f1d4661 | 2007-09-26 19:42:52 +0400 | [diff] [blame] | 80 | struct acpi_device *device; |
| 81 | unsigned long update_time; |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 82 | int present_rate; |
| 83 | int remaining_capacity; |
| 84 | int present_voltage; |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 85 | int design_capacity; |
| 86 | int last_full_capacity; |
| 87 | int technology; |
| 88 | int design_voltage; |
| 89 | int design_capacity_warning; |
| 90 | int design_capacity_low; |
| 91 | int capacity_granularity_1; |
| 92 | int capacity_granularity_2; |
Alexey Starikovskiy | f1d4661 | 2007-09-26 19:42:52 +0400 | [diff] [blame] | 93 | int alarm; |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 94 | char model_number[32]; |
| 95 | char serial_number[32]; |
| 96 | char type[32]; |
| 97 | char oem_info[32]; |
Alexey Starikovskiy | f1d4661 | 2007-09-26 19:42:52 +0400 | [diff] [blame] | 98 | int state; |
| 99 | int power_unit; |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 100 | u8 alarm_present; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | }; |
| 102 | |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 103 | inline int acpi_battery_present(struct acpi_battery *battery) |
| 104 | { |
| 105 | return battery->device->status.battery_present; |
| 106 | } |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 107 | |
Alexey Starikovskiy | f1d4661 | 2007-09-26 19:42:52 +0400 | [diff] [blame] | 108 | inline char *acpi_battery_units(struct acpi_battery *battery) |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 109 | { |
Alexey Starikovskiy | f1d4661 | 2007-09-26 19:42:52 +0400 | [diff] [blame] | 110 | return (battery->power_unit)?"mA":"mW"; |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 111 | } |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 112 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | /* -------------------------------------------------------------------------- |
| 114 | Battery Management |
| 115 | -------------------------------------------------------------------------- */ |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 116 | struct acpi_offsets { |
| 117 | size_t offset; /* offset inside struct acpi_sbs_battery */ |
| 118 | u8 mode; /* int or string? */ |
| 119 | }; |
| 120 | |
| 121 | static struct acpi_offsets state_offsets[] = { |
| 122 | {offsetof(struct acpi_battery, state), 0}, |
| 123 | {offsetof(struct acpi_battery, present_rate), 0}, |
| 124 | {offsetof(struct acpi_battery, remaining_capacity), 0}, |
| 125 | {offsetof(struct acpi_battery, present_voltage), 0}, |
| 126 | }; |
| 127 | |
| 128 | static struct acpi_offsets info_offsets[] = { |
| 129 | {offsetof(struct acpi_battery, power_unit), 0}, |
| 130 | {offsetof(struct acpi_battery, design_capacity), 0}, |
| 131 | {offsetof(struct acpi_battery, last_full_capacity), 0}, |
| 132 | {offsetof(struct acpi_battery, technology), 0}, |
| 133 | {offsetof(struct acpi_battery, design_voltage), 0}, |
| 134 | {offsetof(struct acpi_battery, design_capacity_warning), 0}, |
| 135 | {offsetof(struct acpi_battery, design_capacity_low), 0}, |
| 136 | {offsetof(struct acpi_battery, capacity_granularity_1), 0}, |
| 137 | {offsetof(struct acpi_battery, capacity_granularity_2), 0}, |
| 138 | {offsetof(struct acpi_battery, model_number), 1}, |
| 139 | {offsetof(struct acpi_battery, serial_number), 1}, |
| 140 | {offsetof(struct acpi_battery, type), 1}, |
| 141 | {offsetof(struct acpi_battery, oem_info), 1}, |
| 142 | }; |
| 143 | |
| 144 | static int extract_package(struct acpi_battery *battery, |
| 145 | union acpi_object *package, |
| 146 | struct acpi_offsets *offsets, int num) |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 147 | { |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 148 | int i, *x; |
| 149 | union acpi_object *element; |
| 150 | if (package->type != ACPI_TYPE_PACKAGE) |
| 151 | return -EFAULT; |
| 152 | for (i = 0; i < num; ++i) { |
| 153 | if (package->package.count <= i) |
| 154 | return -EFAULT; |
| 155 | element = &package->package.elements[i]; |
| 156 | if (offsets[i].mode) { |
| 157 | if (element->type != ACPI_TYPE_STRING && |
| 158 | element->type != ACPI_TYPE_BUFFER) |
| 159 | return -EFAULT; |
| 160 | strncpy((u8 *)battery + offsets[i].offset, |
| 161 | element->string.pointer, 32); |
| 162 | } else { |
| 163 | if (element->type != ACPI_TYPE_INTEGER) |
| 164 | return -EFAULT; |
| 165 | x = (int *)((u8 *)battery + offsets[i].offset); |
| 166 | *x = element->integer.value; |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 167 | } |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 168 | } |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | static int acpi_battery_get_status(struct acpi_battery *battery) |
| 173 | { |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame^] | 174 | if (acpi_bus_get_status(battery->device)) { |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 175 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Evaluating _STA")); |
| 176 | return -ENODEV; |
| 177 | } |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame^] | 178 | return 0; |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | static int acpi_battery_get_info(struct acpi_battery *battery) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | { |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame^] | 183 | int result = -EFAULT; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 184 | acpi_status status = 0; |
| 185 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 187 | if (!acpi_battery_present(battery)) |
| 188 | return 0; |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 189 | mutex_lock(&battery->lock); |
Alexey Starikovskiy | f1d4661 | 2007-09-26 19:42:52 +0400 | [diff] [blame] | 190 | status = acpi_evaluate_object(battery->device->handle, "_BIF", |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 191 | NULL, &buffer); |
| 192 | mutex_unlock(&battery->lock); |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame^] | 193 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | if (ACPI_FAILURE(status)) { |
Thomas Renninger | a6fc672 | 2006-06-26 23:58:43 -0400 | [diff] [blame] | 195 | ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BIF")); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 196 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | } |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame^] | 198 | |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 199 | result = extract_package(battery, buffer.pointer, |
| 200 | info_offsets, ARRAY_SIZE(info_offsets)); |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 201 | kfree(buffer.pointer); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 202 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 205 | static int acpi_battery_get_state(struct acpi_battery *battery) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 207 | int result = 0; |
| 208 | acpi_status status = 0; |
| 209 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 211 | if (!acpi_battery_present(battery)) |
| 212 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | |
Alexey Starikovskiy | f1d4661 | 2007-09-26 19:42:52 +0400 | [diff] [blame] | 214 | if (battery->update_time && |
| 215 | time_before(jiffies, battery->update_time + |
| 216 | msecs_to_jiffies(cache_time))) |
| 217 | return 0; |
| 218 | |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 219 | mutex_lock(&battery->lock); |
Alexey Starikovskiy | f1d4661 | 2007-09-26 19:42:52 +0400 | [diff] [blame] | 220 | status = acpi_evaluate_object(battery->device->handle, "_BST", |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 221 | NULL, &buffer); |
| 222 | mutex_unlock(&battery->lock); |
Len Brown | 5b31d89 | 2007-08-15 00:19:26 -0400 | [diff] [blame] | 223 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | if (ACPI_FAILURE(status)) { |
Thomas Renninger | a6fc672 | 2006-06-26 23:58:43 -0400 | [diff] [blame] | 225 | ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BST")); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 226 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | } |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame^] | 228 | |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 229 | result = extract_package(battery, buffer.pointer, |
| 230 | state_offsets, ARRAY_SIZE(state_offsets)); |
Alexey Starikovskiy | f1d4661 | 2007-09-26 19:42:52 +0400 | [diff] [blame] | 231 | battery->update_time = jiffies; |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 232 | kfree(buffer.pointer); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 233 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame^] | 236 | static int acpi_battery_set_alarm(struct acpi_battery *battery) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 238 | acpi_status status = 0; |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame^] | 239 | union acpi_object arg0 = { .type = ACPI_TYPE_INTEGER }; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 240 | struct acpi_object_list arg_list = { 1, &arg0 }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame^] | 242 | if (!acpi_battery_present(battery)|| !battery->alarm_present) |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 243 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame^] | 245 | arg0.integer.value = battery->alarm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 247 | mutex_lock(&battery->lock); |
Alexey Starikovskiy | f1d4661 | 2007-09-26 19:42:52 +0400 | [diff] [blame] | 248 | status = acpi_evaluate_object(battery->device->handle, "_BTP", |
| 249 | &arg_list, NULL); |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 250 | mutex_unlock(&battery->lock); |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame^] | 251 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | if (ACPI_FAILURE(status)) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 253 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame^] | 255 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", battery->alarm)); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 256 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | } |
| 258 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 259 | static int acpi_battery_init_alarm(struct acpi_battery *battery) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 261 | acpi_status status = AE_OK; |
| 262 | acpi_handle handle = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 264 | /* See if alarms are supported, and if so, set default */ |
Alexey Starikovskiy | f1d4661 | 2007-09-26 19:42:52 +0400 | [diff] [blame] | 265 | status = acpi_get_handle(battery->device->handle, "_BTP", &handle); |
| 266 | if (ACPI_FAILURE(status)) { |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 267 | battery->alarm_present = 0; |
Alexey Starikovskiy | f1d4661 | 2007-09-26 19:42:52 +0400 | [diff] [blame] | 268 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | } |
Alexey Starikovskiy | f1d4661 | 2007-09-26 19:42:52 +0400 | [diff] [blame] | 270 | battery->alarm_present = 1; |
| 271 | if (!battery->alarm) |
| 272 | battery->alarm = battery->design_capacity_warning; |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame^] | 273 | return acpi_battery_set_alarm(battery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | } |
| 275 | |
Alexey Starikovskiy | f1d4661 | 2007-09-26 19:42:52 +0400 | [diff] [blame] | 276 | static int acpi_battery_update(struct acpi_battery *battery) |
Vladimir Lebedev | 4bd35cd | 2007-02-10 01:43:48 -0500 | [diff] [blame] | 277 | { |
Alexey Starikovskiy | f1d4661 | 2007-09-26 19:42:52 +0400 | [diff] [blame] | 278 | int saved_present = acpi_battery_present(battery); |
| 279 | int result = acpi_battery_get_status(battery); |
| 280 | if (result || !acpi_battery_present(battery)) |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 281 | return result; |
Alexey Starikovskiy | f1d4661 | 2007-09-26 19:42:52 +0400 | [diff] [blame] | 282 | if (saved_present != acpi_battery_present(battery) || |
| 283 | !battery->update_time) { |
| 284 | battery->update_time = 0; |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 285 | result = acpi_battery_get_info(battery); |
| 286 | if (result) |
| 287 | return result; |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 288 | acpi_battery_init_alarm(battery); |
| 289 | } |
Alexey Starikovskiy | f1d4661 | 2007-09-26 19:42:52 +0400 | [diff] [blame] | 290 | return acpi_battery_get_state(battery); |
Vladimir Lebedev | 4bd35cd | 2007-02-10 01:43:48 -0500 | [diff] [blame] | 291 | } |
| 292 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | /* -------------------------------------------------------------------------- |
| 294 | FS Interface (/proc) |
| 295 | -------------------------------------------------------------------------- */ |
| 296 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 297 | static struct proc_dir_entry *acpi_battery_dir; |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 298 | |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 299 | static int acpi_battery_print_info(struct seq_file *seq, int result) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | { |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 301 | struct acpi_battery *battery = seq->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 303 | if (result) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | goto end; |
| 305 | |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame^] | 306 | seq_printf(seq, "present: %s\n", |
| 307 | acpi_battery_present(battery)?"yes":"no"); |
| 308 | if (!acpi_battery_present(battery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | goto end; |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 310 | if (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | seq_printf(seq, "design capacity: unknown\n"); |
| 312 | else |
| 313 | seq_printf(seq, "design capacity: %d %sh\n", |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame^] | 314 | battery->design_capacity, |
| 315 | acpi_battery_units(battery)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 317 | if (battery->last_full_capacity == ACPI_BATTERY_VALUE_UNKNOWN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | seq_printf(seq, "last full capacity: unknown\n"); |
| 319 | else |
| 320 | seq_printf(seq, "last full capacity: %d %sh\n", |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame^] | 321 | battery->last_full_capacity, |
| 322 | acpi_battery_units(battery)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame^] | 324 | seq_printf(seq, "battery technology: %srechargeable\n", |
| 325 | (!battery->technology)?"non-":""); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 327 | if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | seq_printf(seq, "design voltage: unknown\n"); |
| 329 | else |
| 330 | seq_printf(seq, "design voltage: %d mV\n", |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame^] | 331 | battery->design_voltage); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 332 | seq_printf(seq, "design capacity warning: %d %sh\n", |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame^] | 333 | battery->design_capacity_warning, |
| 334 | acpi_battery_units(battery)); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 335 | seq_printf(seq, "design capacity low: %d %sh\n", |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame^] | 336 | battery->design_capacity_low, |
| 337 | acpi_battery_units(battery)); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 338 | seq_printf(seq, "capacity granularity 1: %d %sh\n", |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame^] | 339 | battery->capacity_granularity_1, |
| 340 | acpi_battery_units(battery)); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 341 | seq_printf(seq, "capacity granularity 2: %d %sh\n", |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame^] | 342 | battery->capacity_granularity_2, |
| 343 | acpi_battery_units(battery)); |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 344 | seq_printf(seq, "model number: %s\n", battery->model_number); |
| 345 | seq_printf(seq, "serial number: %s\n", battery->serial_number); |
| 346 | seq_printf(seq, "battery type: %s\n", battery->type); |
| 347 | seq_printf(seq, "OEM info: %s\n", battery->oem_info); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 348 | end: |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 349 | if (result) |
| 350 | seq_printf(seq, "ERROR: Unable to read battery info\n"); |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 351 | return result; |
| 352 | } |
| 353 | |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 354 | static int acpi_battery_print_state(struct seq_file *seq, int result) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | { |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 356 | struct acpi_battery *battery = seq->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 358 | if (result) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | goto end; |
| 360 | |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame^] | 361 | seq_printf(seq, "present: %s\n", |
| 362 | acpi_battery_present(battery)?"yes":"no"); |
| 363 | if (!acpi_battery_present(battery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | goto end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame^] | 366 | seq_printf(seq, "capacity state: %s\n", |
| 367 | (battery->state & 0x04)?"critical":"ok"); |
| 368 | if ((battery->state & 0x01) && (battery->state & 0x02)) |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 369 | seq_printf(seq, |
| 370 | "charging state: charging/discharging\n"); |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame^] | 371 | else if (battery->state & 0x01) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | seq_printf(seq, "charging state: discharging\n"); |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 373 | else if (battery->state & 0x02) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | seq_printf(seq, "charging state: charging\n"); |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame^] | 375 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | seq_printf(seq, "charging state: charged\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 378 | if (battery->present_rate == ACPI_BATTERY_VALUE_UNKNOWN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | seq_printf(seq, "present rate: unknown\n"); |
| 380 | else |
| 381 | seq_printf(seq, "present rate: %d %s\n", |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame^] | 382 | battery->present_rate, acpi_battery_units(battery)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 384 | if (battery->remaining_capacity == ACPI_BATTERY_VALUE_UNKNOWN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | seq_printf(seq, "remaining capacity: unknown\n"); |
| 386 | else |
| 387 | seq_printf(seq, "remaining capacity: %d %sh\n", |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame^] | 388 | battery->remaining_capacity, acpi_battery_units(battery)); |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 389 | if (battery->present_voltage == ACPI_BATTERY_VALUE_UNKNOWN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | seq_printf(seq, "present voltage: unknown\n"); |
| 391 | else |
| 392 | seq_printf(seq, "present voltage: %d mV\n", |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame^] | 393 | battery->present_voltage); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 394 | end: |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame^] | 395 | if (result) |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 396 | seq_printf(seq, "ERROR: Unable to read battery state\n"); |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 397 | |
| 398 | return result; |
| 399 | } |
| 400 | |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 401 | static int acpi_battery_print_alarm(struct seq_file *seq, int result) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | { |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 403 | struct acpi_battery *battery = seq->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 405 | if (result) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | goto end; |
| 407 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 408 | if (!acpi_battery_present(battery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | seq_printf(seq, "present: no\n"); |
| 410 | goto end; |
| 411 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | seq_printf(seq, "alarm: "); |
| 413 | if (!battery->alarm) |
| 414 | seq_printf(seq, "unsupported\n"); |
| 415 | else |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame^] | 416 | seq_printf(seq, "%u %sh\n", battery->alarm, |
| 417 | acpi_battery_units(battery)); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 418 | end: |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 419 | if (result) |
| 420 | seq_printf(seq, "ERROR: Unable to read battery alarm\n"); |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 421 | return result; |
| 422 | } |
| 423 | |
Alexey Starikovskiy | f1d4661 | 2007-09-26 19:42:52 +0400 | [diff] [blame] | 424 | static ssize_t acpi_battery_write_alarm(struct file *file, |
| 425 | const char __user * buffer, |
| 426 | size_t count, loff_t * ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 428 | int result = 0; |
| 429 | char alarm_string[12] = { '\0' }; |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 430 | struct seq_file *m = file->private_data; |
| 431 | struct acpi_battery *battery = m->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | if (!battery || (count > sizeof(alarm_string) - 1)) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 434 | return -EINVAL; |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 435 | if (result) { |
| 436 | result = -ENODEV; |
| 437 | goto end; |
| 438 | } |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 439 | if (!acpi_battery_present(battery)) { |
| 440 | result = -ENODEV; |
| 441 | goto end; |
| 442 | } |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 443 | if (copy_from_user(alarm_string, buffer, count)) { |
| 444 | result = -EFAULT; |
| 445 | goto end; |
| 446 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | alarm_string[count] = '\0'; |
Alexey Starikovskiy | f1d4661 | 2007-09-26 19:42:52 +0400 | [diff] [blame] | 448 | battery->alarm = simple_strtol(alarm_string, NULL, 0); |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame^] | 449 | result = acpi_battery_set_alarm(battery); |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 450 | end: |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 451 | if (!result) |
Alexey Starikovskiy | f1d4661 | 2007-09-26 19:42:52 +0400 | [diff] [blame] | 452 | return count; |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 453 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | } |
| 455 | |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 456 | typedef int(*print_func)(struct seq_file *seq, int result); |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 457 | |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame^] | 458 | static print_func acpi_print_funcs[ACPI_BATTERY_NUMFILES] = { |
| 459 | acpi_battery_print_info, |
| 460 | acpi_battery_print_state, |
| 461 | acpi_battery_print_alarm, |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 462 | }; |
| 463 | |
| 464 | static int acpi_battery_read(int fid, struct seq_file *seq) |
| 465 | { |
| 466 | struct acpi_battery *battery = seq->private; |
Alexey Starikovskiy | f1d4661 | 2007-09-26 19:42:52 +0400 | [diff] [blame] | 467 | int result = acpi_battery_update(battery); |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame^] | 468 | return acpi_print_funcs[fid](seq, result); |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 469 | } |
| 470 | |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame^] | 471 | #define DECLARE_FILE_FUNCTIONS(_name) \ |
| 472 | static int acpi_battery_read_##_name(struct seq_file *seq, void *offset) \ |
| 473 | { \ |
| 474 | return acpi_battery_read(_name##_tag, seq); \ |
| 475 | } \ |
| 476 | static int acpi_battery_##_name##_open_fs(struct inode *inode, struct file *file) \ |
| 477 | { \ |
| 478 | return single_open(file, acpi_battery_read_##_name, PDE(inode)->data); \ |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 479 | } |
| 480 | |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame^] | 481 | DECLARE_FILE_FUNCTIONS(info); |
| 482 | DECLARE_FILE_FUNCTIONS(state); |
| 483 | DECLARE_FILE_FUNCTIONS(alarm); |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 484 | |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame^] | 485 | #undef DECLARE_FILE_FUNCTIONS |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 486 | |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame^] | 487 | #define FILE_DESCRIPTION_RO(_name) \ |
| 488 | { \ |
| 489 | .name = __stringify(_name), \ |
| 490 | .mode = S_IRUGO, \ |
| 491 | .ops = { \ |
| 492 | .open = acpi_battery_##_name##_open_fs, \ |
| 493 | .read = seq_read, \ |
| 494 | .llseek = seq_lseek, \ |
| 495 | .release = single_release, \ |
| 496 | .owner = THIS_MODULE, \ |
| 497 | }, \ |
| 498 | } |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 499 | |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame^] | 500 | #define FILE_DESCRIPTION_RW(_name) \ |
| 501 | { \ |
| 502 | .name = __stringify(_name), \ |
| 503 | .mode = S_IFREG | S_IRUGO | S_IWUSR, \ |
| 504 | .ops = { \ |
| 505 | .open = acpi_battery_##_name##_open_fs, \ |
| 506 | .read = seq_read, \ |
| 507 | .llseek = seq_lseek, \ |
| 508 | .write = acpi_battery_write_##_name, \ |
| 509 | .release = single_release, \ |
| 510 | .owner = THIS_MODULE, \ |
| 511 | }, \ |
| 512 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 514 | static struct battery_file { |
| 515 | struct file_operations ops; |
| 516 | mode_t mode; |
| 517 | char *name; |
| 518 | } acpi_battery_file[] = { |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame^] | 519 | FILE_DESCRIPTION_RO(info), |
| 520 | FILE_DESCRIPTION_RO(state), |
| 521 | FILE_DESCRIPTION_RW(alarm), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | }; |
| 523 | |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame^] | 524 | #undef FILE_DESCRIPTION_RO |
| 525 | #undef FILE_DESCRIPTION_RW |
| 526 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 527 | static int acpi_battery_add_fs(struct acpi_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 529 | struct proc_dir_entry *entry = NULL; |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 530 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | if (!acpi_device_dir(device)) { |
| 533 | acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 534 | acpi_battery_dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | if (!acpi_device_dir(device)) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 536 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | acpi_device_dir(device)->owner = THIS_MODULE; |
| 538 | } |
| 539 | |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 540 | for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i) { |
| 541 | entry = create_proc_entry(acpi_battery_file[i].name, |
| 542 | acpi_battery_file[i].mode, acpi_device_dir(device)); |
| 543 | if (!entry) |
| 544 | return -ENODEV; |
| 545 | else { |
| 546 | entry->proc_fops = &acpi_battery_file[i].ops; |
| 547 | entry->data = acpi_driver_data(device); |
| 548 | entry->owner = THIS_MODULE; |
| 549 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | } |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 551 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | } |
| 553 | |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame^] | 554 | static void acpi_battery_remove_fs(struct acpi_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | { |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 556 | int i; |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame^] | 557 | if (!acpi_device_dir(device)) |
| 558 | return; |
| 559 | for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i) |
| 560 | remove_proc_entry(acpi_battery_file[i].name, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | acpi_device_dir(device)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame^] | 563 | remove_proc_entry(acpi_device_bid(device), acpi_battery_dir); |
| 564 | acpi_device_dir(device) = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | } |
| 566 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | /* -------------------------------------------------------------------------- |
| 568 | Driver Interface |
| 569 | -------------------------------------------------------------------------- */ |
| 570 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 571 | static void acpi_battery_notify(acpi_handle handle, u32 event, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | { |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 573 | struct acpi_battery *battery = data; |
Alexey Starikovskiy | f1d4661 | 2007-09-26 19:42:52 +0400 | [diff] [blame] | 574 | struct acpi_device *device; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | if (!battery) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 576 | return; |
Patrick Mochel | 145def8 | 2006-05-19 16:54:39 -0400 | [diff] [blame] | 577 | device = battery->device; |
Alexey Starikovskiy | f1d4661 | 2007-09-26 19:42:52 +0400 | [diff] [blame] | 578 | acpi_battery_update(battery); |
| 579 | acpi_bus_generate_proc_event(device, event, |
| 580 | acpi_battery_present(battery)); |
| 581 | acpi_bus_generate_netlink_event(device->pnp.device_class, |
| 582 | device->dev.bus_id, event, |
Vladimir Lebedev | 9ea7d57 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 583 | acpi_battery_present(battery)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | } |
| 585 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 586 | static int acpi_battery_add(struct acpi_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 588 | int result = 0; |
| 589 | acpi_status status = 0; |
| 590 | struct acpi_battery *battery = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | if (!device) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 592 | return -EINVAL; |
Burman Yan | 36bcbec | 2006-12-19 12:56:11 -0800 | [diff] [blame] | 593 | battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | if (!battery) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 595 | return -ENOMEM; |
Patrick Mochel | 145def8 | 2006-05-19 16:54:39 -0400 | [diff] [blame] | 596 | battery->device = device; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME); |
| 598 | strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS); |
| 599 | acpi_driver_data(device) = battery; |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 600 | mutex_init(&battery->lock); |
Alexey Starikovskiy | f1d4661 | 2007-09-26 19:42:52 +0400 | [diff] [blame] | 601 | acpi_battery_update(battery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | result = acpi_battery_add_fs(device); |
| 603 | if (result) |
| 604 | goto end; |
Patrick Mochel | 3b073ec | 2006-05-19 16:54:41 -0400 | [diff] [blame] | 605 | status = acpi_install_notify_handler(device->handle, |
Vladimir Lebedev | 9fdae72 | 2006-06-27 04:49:00 -0400 | [diff] [blame] | 606 | ACPI_ALL_NOTIFY, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 607 | acpi_battery_notify, battery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | if (ACPI_FAILURE(status)) { |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 609 | ACPI_EXCEPTION((AE_INFO, status, "Installing notify handler")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | result = -ENODEV; |
| 611 | goto end; |
| 612 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | printk(KERN_INFO PREFIX "%s Slot [%s] (battery %s)\n", |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 614 | ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device), |
| 615 | device->status.battery_present ? "present" : "absent"); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 616 | end: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | if (result) { |
| 618 | acpi_battery_remove_fs(device); |
| 619 | kfree(battery); |
| 620 | } |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 621 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | } |
| 623 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 624 | static int acpi_battery_remove(struct acpi_device *device, int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 626 | acpi_status status = 0; |
| 627 | struct acpi_battery *battery = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | if (!device || !acpi_driver_data(device)) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 630 | return -EINVAL; |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 631 | battery = acpi_driver_data(device); |
Patrick Mochel | 3b073ec | 2006-05-19 16:54:41 -0400 | [diff] [blame] | 632 | status = acpi_remove_notify_handler(device->handle, |
Vladimir Lebedev | 9fdae72 | 2006-06-27 04:49:00 -0400 | [diff] [blame] | 633 | ACPI_ALL_NOTIFY, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 634 | acpi_battery_notify); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | acpi_battery_remove_fs(device); |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame] | 636 | mutex_destroy(&battery->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | kfree(battery); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 638 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | } |
| 640 | |
Jiri Kosina | 34c4415 | 2006-10-10 14:20:41 -0700 | [diff] [blame] | 641 | /* this is needed to learn about changes made in suspended state */ |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 642 | static int acpi_battery_resume(struct acpi_device *device) |
Jiri Kosina | 34c4415 | 2006-10-10 14:20:41 -0700 | [diff] [blame] | 643 | { |
| 644 | struct acpi_battery *battery; |
Jiri Kosina | 34c4415 | 2006-10-10 14:20:41 -0700 | [diff] [blame] | 645 | if (!device) |
| 646 | return -EINVAL; |
Alexey Starikovskiy | f1d4661 | 2007-09-26 19:42:52 +0400 | [diff] [blame] | 647 | battery = acpi_driver_data(device); |
| 648 | battery->update_time = 0; |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 649 | return 0; |
Jiri Kosina | 34c4415 | 2006-10-10 14:20:41 -0700 | [diff] [blame] | 650 | } |
| 651 | |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame^] | 652 | static struct acpi_driver acpi_battery_driver = { |
| 653 | .name = "battery", |
| 654 | .class = ACPI_BATTERY_CLASS, |
| 655 | .ids = battery_device_ids, |
| 656 | .ops = { |
| 657 | .add = acpi_battery_add, |
| 658 | .resume = acpi_battery_resume, |
| 659 | .remove = acpi_battery_remove, |
| 660 | }, |
| 661 | }; |
| 662 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 663 | static int __init acpi_battery_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | { |
Pavel Machek | 4d8316d | 2006-08-14 22:37:22 -0700 | [diff] [blame] | 665 | if (acpi_disabled) |
| 666 | return -ENODEV; |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 667 | acpi_battery_dir = acpi_lock_battery_dir(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | if (!acpi_battery_dir) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 669 | return -ENODEV; |
Alexey Starikovskiy | aa650bb | 2007-09-26 19:42:58 +0400 | [diff] [blame^] | 670 | if (acpi_bus_register_driver(&acpi_battery_driver) < 0) { |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 671 | acpi_unlock_battery_dir(acpi_battery_dir); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 672 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | } |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 674 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | } |
| 676 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 677 | static void __exit acpi_battery_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | acpi_bus_unregister_driver(&acpi_battery_driver); |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 680 | acpi_unlock_battery_dir(acpi_battery_dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | } |
| 682 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | module_init(acpi_battery_init); |
| 684 | module_exit(acpi_battery_exit); |