Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * acpi_battery.c - ACPI Battery Driver ($Revision: 37 $) |
| 3 | * |
| 4 | * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com> |
| 5 | * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com> |
| 6 | * |
| 7 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or (at |
| 12 | * your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, but |
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | * General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License along |
| 20 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 21 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
| 22 | * |
| 23 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 24 | */ |
| 25 | |
| 26 | #include <linux/kernel.h> |
| 27 | #include <linux/module.h> |
| 28 | #include <linux/init.h> |
| 29 | #include <linux/types.h> |
| 30 | #include <linux/proc_fs.h> |
| 31 | #include <linux/seq_file.h> |
| 32 | #include <asm/uaccess.h> |
| 33 | |
| 34 | #include <acpi/acpi_bus.h> |
| 35 | #include <acpi/acpi_drivers.h> |
| 36 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #define ACPI_BATTERY_VALUE_UNKNOWN 0xFFFFFFFF |
| 38 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #define ACPI_BATTERY_COMPONENT 0x00040000 |
| 40 | #define ACPI_BATTERY_CLASS "battery" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #define ACPI_BATTERY_DEVICE_NAME "Battery" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #define ACPI_BATTERY_NOTIFY_STATUS 0x80 |
| 43 | #define ACPI_BATTERY_NOTIFY_INFO 0x81 |
| 44 | #define ACPI_BATTERY_UNITS_WATTS "mW" |
| 45 | #define ACPI_BATTERY_UNITS_AMPS "mA" |
| 46 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | #define _COMPONENT ACPI_BATTERY_COMPONENT |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 48 | |
| 49 | #define ACPI_BATTERY_UPDATE_TIME 0 |
| 50 | |
| 51 | #define ACPI_BATTERY_NONE_UPDATE 0 |
| 52 | #define ACPI_BATTERY_EASY_UPDATE 1 |
| 53 | #define ACPI_BATTERY_INIT_UPDATE 2 |
| 54 | |
Len Brown | f52fd66 | 2007-02-12 22:42:12 -0500 | [diff] [blame] | 55 | ACPI_MODULE_NAME("battery"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
Len Brown | f52fd66 | 2007-02-12 22:42:12 -0500 | [diff] [blame] | 57 | MODULE_AUTHOR("Paul Diefenbaugh"); |
Len Brown | 7cda93e | 2007-02-12 23:50:02 -0500 | [diff] [blame] | 58 | MODULE_DESCRIPTION("ACPI Battery Driver"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | MODULE_LICENSE("GPL"); |
| 60 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 61 | static unsigned int update_time = ACPI_BATTERY_UPDATE_TIME; |
| 62 | |
| 63 | /* 0 - every time, > 0 - by update_time */ |
| 64 | module_param(update_time, uint, 0644); |
| 65 | |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 66 | extern struct proc_dir_entry *acpi_lock_battery_dir(void); |
| 67 | extern void *acpi_unlock_battery_dir(struct proc_dir_entry *acpi_battery_dir); |
| 68 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 69 | static int acpi_battery_add(struct acpi_device *device); |
| 70 | static int acpi_battery_remove(struct acpi_device *device, int type); |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 71 | static int acpi_battery_resume(struct acpi_device *device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | |
Thomas Renninger | 1ba90e3 | 2007-07-23 14:44:41 +0200 | [diff] [blame] | 73 | static const struct acpi_device_id battery_device_ids[] = { |
| 74 | {"PNP0C0A", 0}, |
| 75 | {"", 0}, |
| 76 | }; |
| 77 | MODULE_DEVICE_TABLE(acpi, battery_device_ids); |
| 78 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | static struct acpi_driver acpi_battery_driver = { |
Len Brown | c2b6705b | 2007-02-12 23:33:40 -0500 | [diff] [blame] | 80 | .name = "battery", |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 81 | .class = ACPI_BATTERY_CLASS, |
Thomas Renninger | 1ba90e3 | 2007-07-23 14:44:41 +0200 | [diff] [blame] | 82 | .ids = battery_device_ids, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 83 | .ops = { |
| 84 | .add = acpi_battery_add, |
Jiri Kosina | 34c4415 | 2006-10-10 14:20:41 -0700 | [diff] [blame] | 85 | .resume = acpi_battery_resume, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 86 | .remove = acpi_battery_remove, |
| 87 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | }; |
| 89 | |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame^] | 90 | enum acpi_battery_files { |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 91 | ACPI_BATTERY_INFO = 0, |
| 92 | ACPI_BATTERY_STATE, |
| 93 | ACPI_BATTERY_ALARM, |
| 94 | ACPI_BATTERY_NUMFILES, |
| 95 | }; |
| 96 | |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame^] | 97 | struct acpi_battery { |
| 98 | struct acpi_device *device; |
| 99 | struct mutex lock; |
| 100 | unsigned long alarm; |
| 101 | unsigned long update_time[ACPI_BATTERY_NUMFILES]; |
| 102 | int state; |
| 103 | int present_rate; |
| 104 | int remaining_capacity; |
| 105 | int present_voltage; |
| 106 | int power_unit; |
| 107 | int design_capacity; |
| 108 | int last_full_capacity; |
| 109 | int technology; |
| 110 | int design_voltage; |
| 111 | int design_capacity_warning; |
| 112 | int design_capacity_low; |
| 113 | int capacity_granularity_1; |
| 114 | int capacity_granularity_2; |
| 115 | char model_number[32]; |
| 116 | char serial_number[32]; |
| 117 | char type[32]; |
| 118 | char oem_info[32]; |
| 119 | u8 present_prev; |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 120 | u8 alarm_present; |
| 121 | u8 init_update; |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 122 | u8 update[ACPI_BATTERY_NUMFILES]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | }; |
| 124 | |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 125 | inline int acpi_battery_present(struct acpi_battery *battery) |
| 126 | { |
| 127 | return battery->device->status.battery_present; |
| 128 | } |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame^] | 129 | |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 130 | inline char *acpi_battery_power_units(struct acpi_battery *battery) |
| 131 | { |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame^] | 132 | if (battery->power_unit) |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 133 | return ACPI_BATTERY_UNITS_AMPS; |
| 134 | else |
| 135 | return ACPI_BATTERY_UNITS_WATTS; |
| 136 | } |
| 137 | |
| 138 | inline acpi_handle acpi_battery_handle(struct acpi_battery *battery) |
| 139 | { |
| 140 | return battery->device->handle; |
| 141 | } |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 142 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | /* -------------------------------------------------------------------------- |
| 144 | Battery Management |
| 145 | -------------------------------------------------------------------------- */ |
| 146 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 147 | static void acpi_battery_check_result(struct acpi_battery *battery, int result) |
| 148 | { |
| 149 | if (!battery) |
| 150 | return; |
| 151 | |
| 152 | if (result) { |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame^] | 153 | battery->init_update = 1; |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame^] | 157 | struct acpi_offsets { |
| 158 | size_t offset; /* offset inside struct acpi_sbs_battery */ |
| 159 | u8 mode; /* int or string? */ |
| 160 | }; |
| 161 | |
| 162 | static struct acpi_offsets state_offsets[] = { |
| 163 | {offsetof(struct acpi_battery, state), 0}, |
| 164 | {offsetof(struct acpi_battery, present_rate), 0}, |
| 165 | {offsetof(struct acpi_battery, remaining_capacity), 0}, |
| 166 | {offsetof(struct acpi_battery, present_voltage), 0}, |
| 167 | }; |
| 168 | |
| 169 | static struct acpi_offsets info_offsets[] = { |
| 170 | {offsetof(struct acpi_battery, power_unit), 0}, |
| 171 | {offsetof(struct acpi_battery, design_capacity), 0}, |
| 172 | {offsetof(struct acpi_battery, last_full_capacity), 0}, |
| 173 | {offsetof(struct acpi_battery, technology), 0}, |
| 174 | {offsetof(struct acpi_battery, design_voltage), 0}, |
| 175 | {offsetof(struct acpi_battery, design_capacity_warning), 0}, |
| 176 | {offsetof(struct acpi_battery, design_capacity_low), 0}, |
| 177 | {offsetof(struct acpi_battery, capacity_granularity_1), 0}, |
| 178 | {offsetof(struct acpi_battery, capacity_granularity_2), 0}, |
| 179 | {offsetof(struct acpi_battery, model_number), 1}, |
| 180 | {offsetof(struct acpi_battery, serial_number), 1}, |
| 181 | {offsetof(struct acpi_battery, type), 1}, |
| 182 | {offsetof(struct acpi_battery, oem_info), 1}, |
| 183 | }; |
| 184 | |
| 185 | static int extract_package(struct acpi_battery *battery, |
| 186 | union acpi_object *package, |
| 187 | struct acpi_offsets *offsets, int num) |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 188 | { |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame^] | 189 | int i, *x; |
| 190 | union acpi_object *element; |
| 191 | if (package->type != ACPI_TYPE_PACKAGE) |
| 192 | return -EFAULT; |
| 193 | for (i = 0; i < num; ++i) { |
| 194 | if (package->package.count <= i) |
| 195 | return -EFAULT; |
| 196 | element = &package->package.elements[i]; |
| 197 | if (offsets[i].mode) { |
| 198 | if (element->type != ACPI_TYPE_STRING && |
| 199 | element->type != ACPI_TYPE_BUFFER) |
| 200 | return -EFAULT; |
| 201 | strncpy((u8 *)battery + offsets[i].offset, |
| 202 | element->string.pointer, 32); |
| 203 | } else { |
| 204 | if (element->type != ACPI_TYPE_INTEGER) |
| 205 | return -EFAULT; |
| 206 | x = (int *)((u8 *)battery + offsets[i].offset); |
| 207 | *x = element->integer.value; |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 208 | } |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 209 | } |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 210 | return 0; |
| 211 | } |
| 212 | |
| 213 | static int acpi_battery_get_status(struct acpi_battery *battery) |
| 214 | { |
| 215 | int result = 0; |
| 216 | |
| 217 | result = acpi_bus_get_status(battery->device); |
| 218 | if (result) { |
| 219 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Evaluating _STA")); |
| 220 | return -ENODEV; |
| 221 | } |
| 222 | return result; |
| 223 | } |
| 224 | |
| 225 | static int acpi_battery_get_info(struct acpi_battery *battery) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 227 | int result = 0; |
| 228 | acpi_status status = 0; |
| 229 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 231 | battery->update_time[ACPI_BATTERY_INFO] = get_seconds(); |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 232 | if (!acpi_battery_present(battery)) |
| 233 | return 0; |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame^] | 234 | mutex_lock(&battery->lock); |
| 235 | status = acpi_evaluate_object(acpi_battery_handle(battery), "_BIF", |
| 236 | NULL, &buffer); |
| 237 | mutex_unlock(&battery->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | if (ACPI_FAILURE(status)) { |
Thomas Renninger | a6fc672 | 2006-06-26 23:58:43 -0400 | [diff] [blame] | 239 | ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BIF")); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 240 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | } |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame^] | 242 | result = extract_package(battery, buffer.pointer, |
| 243 | info_offsets, ARRAY_SIZE(info_offsets)); |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 244 | kfree(buffer.pointer); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 245 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 248 | static int acpi_battery_get_state(struct acpi_battery *battery) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 250 | int result = 0; |
| 251 | acpi_status status = 0; |
| 252 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 254 | battery->update_time[ACPI_BATTERY_STATE] = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 256 | if (!acpi_battery_present(battery)) |
| 257 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame^] | 259 | mutex_lock(&battery->lock); |
| 260 | status = acpi_evaluate_object(acpi_battery_handle(battery), "_BST", |
| 261 | NULL, &buffer); |
| 262 | mutex_unlock(&battery->lock); |
Len Brown | 5b31d89 | 2007-08-15 00:19:26 -0400 | [diff] [blame] | 263 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | if (ACPI_FAILURE(status)) { |
Thomas Renninger | a6fc672 | 2006-06-26 23:58:43 -0400 | [diff] [blame] | 265 | ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BST")); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 266 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | } |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame^] | 268 | result = extract_package(battery, buffer.pointer, |
| 269 | state_offsets, ARRAY_SIZE(state_offsets)); |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 270 | kfree(buffer.pointer); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 271 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | } |
| 273 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 274 | static int acpi_battery_get_alarm(struct acpi_battery *battery) |
| 275 | { |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 276 | battery->update_time[ACPI_BATTERY_ALARM] = get_seconds(); |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 277 | |
| 278 | return 0; |
| 279 | } |
| 280 | |
Vladimir Lebedev | 9ea7d57 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 281 | static int acpi_battery_set_alarm(struct acpi_battery *battery, |
| 282 | unsigned long alarm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 284 | acpi_status status = 0; |
| 285 | union acpi_object arg0 = { ACPI_TYPE_INTEGER }; |
| 286 | struct acpi_object_list arg_list = { 1, &arg0 }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 288 | battery->update_time[ACPI_BATTERY_ALARM] = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 290 | if (!acpi_battery_present(battery)) |
| 291 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame^] | 293 | if (!battery->alarm_present) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 294 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | |
| 296 | arg0.integer.value = alarm; |
| 297 | |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame^] | 298 | mutex_lock(&battery->lock); |
| 299 | status = acpi_evaluate_object(acpi_battery_handle(battery), "_BTP", |
| 300 | &arg_list, NULL); |
| 301 | mutex_unlock(&battery->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | if (ACPI_FAILURE(status)) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 303 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | |
| 305 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", (u32) alarm)); |
| 306 | |
| 307 | battery->alarm = alarm; |
| 308 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 309 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | } |
| 311 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 312 | static int acpi_battery_init_alarm(struct acpi_battery *battery) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 314 | int result = 0; |
| 315 | acpi_status status = AE_OK; |
| 316 | acpi_handle handle = NULL; |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 317 | unsigned long alarm = battery->alarm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 319 | /* See if alarms are supported, and if so, set default */ |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 320 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 321 | status = acpi_get_handle(acpi_battery_handle(battery), "_BTP", &handle); |
| 322 | if (ACPI_SUCCESS(status)) { |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame^] | 323 | battery->alarm_present = 1; |
| 324 | if (!alarm) { |
| 325 | alarm = battery->design_capacity_warning; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | } |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 327 | result = acpi_battery_set_alarm(battery, alarm); |
| 328 | if (result) |
| 329 | goto end; |
| 330 | } else { |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame^] | 331 | battery->alarm_present = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | } |
| 333 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 334 | end: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 336 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | } |
| 338 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 339 | static int acpi_battery_init_update(struct acpi_battery *battery) |
Vladimir Lebedev | 4bd35cd | 2007-02-10 01:43:48 -0500 | [diff] [blame] | 340 | { |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 341 | int result = 0; |
| 342 | |
| 343 | result = acpi_battery_get_status(battery); |
| 344 | if (result) |
| 345 | return result; |
| 346 | |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame^] | 347 | battery->present_prev = acpi_battery_present(battery); |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 348 | |
| 349 | if (acpi_battery_present(battery)) { |
| 350 | result = acpi_battery_get_info(battery); |
| 351 | if (result) |
| 352 | return result; |
| 353 | result = acpi_battery_get_state(battery); |
| 354 | if (result) |
| 355 | return result; |
| 356 | |
| 357 | acpi_battery_init_alarm(battery); |
| 358 | } |
| 359 | |
| 360 | return result; |
| 361 | } |
| 362 | |
| 363 | static int acpi_battery_update(struct acpi_battery *battery, |
Vladimir Lebedev | 9ea7d57 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 364 | int update, int *update_result_ptr) |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 365 | { |
| 366 | int result = 0; |
| 367 | int update_result = ACPI_BATTERY_NONE_UPDATE; |
| 368 | |
| 369 | if (!acpi_battery_present(battery)) { |
| 370 | update = 1; |
| 371 | } |
| 372 | |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame^] | 373 | if (battery->init_update) { |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 374 | result = acpi_battery_init_update(battery); |
| 375 | if (result) |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 376 | goto end; |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 377 | update_result = ACPI_BATTERY_INIT_UPDATE; |
| 378 | } else if (update) { |
| 379 | result = acpi_battery_get_status(battery); |
| 380 | if (result) |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 381 | goto end; |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame^] | 382 | if ((!battery->present_prev & acpi_battery_present(battery)) |
| 383 | || (battery->present_prev & !acpi_battery_present(battery))) { |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 384 | result = acpi_battery_init_update(battery); |
| 385 | if (result) |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 386 | goto end; |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 387 | update_result = ACPI_BATTERY_INIT_UPDATE; |
| 388 | } else { |
| 389 | update_result = ACPI_BATTERY_EASY_UPDATE; |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | end: |
| 394 | |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame^] | 395 | battery->init_update = (result != 0); |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 396 | |
| 397 | *update_result_ptr = update_result; |
| 398 | |
| 399 | return result; |
| 400 | } |
| 401 | |
| 402 | static void acpi_battery_notify_update(struct acpi_battery *battery) |
| 403 | { |
| 404 | acpi_battery_get_status(battery); |
| 405 | |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame^] | 406 | if (battery->init_update) { |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 407 | return; |
| 408 | } |
| 409 | |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame^] | 410 | if ((!battery->present_prev & |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 411 | acpi_battery_present(battery)) || |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame^] | 412 | (battery->present_prev & |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 413 | !acpi_battery_present(battery))) { |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame^] | 414 | battery->init_update = 1; |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 415 | } else { |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame^] | 416 | battery->update[ACPI_BATTERY_INFO] = 1; |
| 417 | battery->update[ACPI_BATTERY_STATE] = 1; |
| 418 | battery->update[ACPI_BATTERY_ALARM] = 1; |
Vladimir Lebedev | 4bd35cd | 2007-02-10 01:43:48 -0500 | [diff] [blame] | 419 | } |
| 420 | } |
| 421 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | /* -------------------------------------------------------------------------- |
| 423 | FS Interface (/proc) |
| 424 | -------------------------------------------------------------------------- */ |
| 425 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 426 | static struct proc_dir_entry *acpi_battery_dir; |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 427 | |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 428 | static int acpi_battery_print_info(struct seq_file *seq, int result) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | { |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 430 | struct acpi_battery *battery = seq->private; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 431 | char *units = "?"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 433 | if (result) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | goto end; |
| 435 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 436 | if (acpi_battery_present(battery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | seq_printf(seq, "present: yes\n"); |
| 438 | else { |
| 439 | seq_printf(seq, "present: no\n"); |
| 440 | goto end; |
| 441 | } |
| 442 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 443 | /* Battery Units */ |
| 444 | |
| 445 | units = acpi_battery_power_units(battery); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 446 | |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame^] | 447 | if (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | seq_printf(seq, "design capacity: unknown\n"); |
| 449 | else |
| 450 | seq_printf(seq, "design capacity: %d %sh\n", |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame^] | 451 | (u32) battery->design_capacity, units); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame^] | 453 | if (battery->last_full_capacity == ACPI_BATTERY_VALUE_UNKNOWN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | seq_printf(seq, "last full capacity: unknown\n"); |
| 455 | else |
| 456 | seq_printf(seq, "last full capacity: %d %sh\n", |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame^] | 457 | (u32) battery->last_full_capacity, units); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame^] | 459 | switch ((u32) battery->technology) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | case 0: |
| 461 | seq_printf(seq, "battery technology: non-rechargeable\n"); |
| 462 | break; |
| 463 | case 1: |
| 464 | seq_printf(seq, "battery technology: rechargeable\n"); |
| 465 | break; |
| 466 | default: |
| 467 | seq_printf(seq, "battery technology: unknown\n"); |
| 468 | break; |
| 469 | } |
| 470 | |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame^] | 471 | if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | seq_printf(seq, "design voltage: unknown\n"); |
| 473 | else |
| 474 | seq_printf(seq, "design voltage: %d mV\n", |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame^] | 475 | (u32) battery->design_voltage); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 476 | seq_printf(seq, "design capacity warning: %d %sh\n", |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame^] | 477 | (u32) battery->design_capacity_warning, units); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 478 | seq_printf(seq, "design capacity low: %d %sh\n", |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame^] | 479 | (u32) battery->design_capacity_low, units); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 480 | seq_printf(seq, "capacity granularity 1: %d %sh\n", |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame^] | 481 | (u32) battery->capacity_granularity_1, units); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 482 | seq_printf(seq, "capacity granularity 2: %d %sh\n", |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame^] | 483 | (u32) battery->capacity_granularity_2, units); |
| 484 | seq_printf(seq, "model number: %s\n", battery->model_number); |
| 485 | seq_printf(seq, "serial number: %s\n", battery->serial_number); |
| 486 | seq_printf(seq, "battery type: %s\n", battery->type); |
| 487 | seq_printf(seq, "OEM info: %s\n", battery->oem_info); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 488 | |
| 489 | end: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 491 | if (result) |
| 492 | seq_printf(seq, "ERROR: Unable to read battery info\n"); |
| 493 | |
| 494 | return result; |
| 495 | } |
| 496 | |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 497 | static int acpi_battery_print_state(struct seq_file *seq, int result) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | { |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 499 | struct acpi_battery *battery = seq->private; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 500 | char *units = "?"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 502 | if (result) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | goto end; |
| 504 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 505 | if (acpi_battery_present(battery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | seq_printf(seq, "present: yes\n"); |
| 507 | else { |
| 508 | seq_printf(seq, "present: no\n"); |
| 509 | goto end; |
| 510 | } |
| 511 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 512 | /* Battery Units */ |
| 513 | |
| 514 | units = acpi_battery_power_units(battery); |
| 515 | |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame^] | 516 | if (!(battery->state & 0x04)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | seq_printf(seq, "capacity state: ok\n"); |
| 518 | else |
| 519 | seq_printf(seq, "capacity state: critical\n"); |
| 520 | |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame^] | 521 | if ((battery->state & 0x01) && (battery->state & 0x02)) { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 522 | seq_printf(seq, |
| 523 | "charging state: charging/discharging\n"); |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame^] | 524 | } else if (battery->state & 0x01) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | seq_printf(seq, "charging state: discharging\n"); |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame^] | 526 | else if (battery->state & 0x02) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | seq_printf(seq, "charging state: charging\n"); |
| 528 | else { |
| 529 | seq_printf(seq, "charging state: charged\n"); |
| 530 | } |
| 531 | |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame^] | 532 | if (battery->present_rate == ACPI_BATTERY_VALUE_UNKNOWN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | seq_printf(seq, "present rate: unknown\n"); |
| 534 | else |
| 535 | seq_printf(seq, "present rate: %d %s\n", |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame^] | 536 | (u32) battery->present_rate, units); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame^] | 538 | if (battery->remaining_capacity == ACPI_BATTERY_VALUE_UNKNOWN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | seq_printf(seq, "remaining capacity: unknown\n"); |
| 540 | else |
| 541 | seq_printf(seq, "remaining capacity: %d %sh\n", |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame^] | 542 | (u32) battery->remaining_capacity, units); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame^] | 544 | if (battery->present_voltage == ACPI_BATTERY_VALUE_UNKNOWN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | seq_printf(seq, "present voltage: unknown\n"); |
| 546 | else |
| 547 | seq_printf(seq, "present voltage: %d mV\n", |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame^] | 548 | (u32) battery->present_voltage); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 550 | end: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 552 | if (result) { |
| 553 | seq_printf(seq, "ERROR: Unable to read battery state\n"); |
| 554 | } |
| 555 | |
| 556 | return result; |
| 557 | } |
| 558 | |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 559 | static int acpi_battery_print_alarm(struct seq_file *seq, int result) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | { |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 561 | struct acpi_battery *battery = seq->private; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 562 | char *units = "?"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 564 | if (result) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | goto end; |
| 566 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 567 | if (!acpi_battery_present(battery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | seq_printf(seq, "present: no\n"); |
| 569 | goto end; |
| 570 | } |
| 571 | |
| 572 | /* Battery Units */ |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 573 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 574 | units = acpi_battery_power_units(battery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | |
| 576 | seq_printf(seq, "alarm: "); |
| 577 | if (!battery->alarm) |
| 578 | seq_printf(seq, "unsupported\n"); |
| 579 | else |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 580 | seq_printf(seq, "%lu %sh\n", battery->alarm, units); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 582 | end: |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 583 | |
| 584 | if (result) |
| 585 | seq_printf(seq, "ERROR: Unable to read battery alarm\n"); |
| 586 | |
| 587 | return result; |
| 588 | } |
| 589 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | static ssize_t |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 591 | acpi_battery_write_alarm(struct file *file, |
| 592 | const char __user * buffer, |
| 593 | size_t count, loff_t * ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 595 | int result = 0; |
| 596 | char alarm_string[12] = { '\0' }; |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 597 | struct seq_file *m = file->private_data; |
| 598 | struct acpi_battery *battery = m->private; |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 599 | int update_result = ACPI_BATTERY_NONE_UPDATE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | if (!battery || (count > sizeof(alarm_string) - 1)) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 602 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 604 | result = acpi_battery_update(battery, 1, &update_result); |
| 605 | if (result) { |
| 606 | result = -ENODEV; |
| 607 | goto end; |
| 608 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 610 | if (!acpi_battery_present(battery)) { |
| 611 | result = -ENODEV; |
| 612 | goto end; |
| 613 | } |
| 614 | |
| 615 | if (copy_from_user(alarm_string, buffer, count)) { |
| 616 | result = -EFAULT; |
| 617 | goto end; |
| 618 | } |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 619 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | alarm_string[count] = '\0'; |
| 621 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 622 | result = acpi_battery_set_alarm(battery, |
| 623 | simple_strtoul(alarm_string, NULL, 0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | if (result) |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 625 | goto end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 627 | end: |
| 628 | |
| 629 | acpi_battery_check_result(battery, result); |
| 630 | |
| 631 | if (!result) |
Len Brown | 5b31d89 | 2007-08-15 00:19:26 -0400 | [diff] [blame] | 632 | result = count; |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 633 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | } |
| 635 | |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 636 | typedef int(*print_func)(struct seq_file *seq, int result); |
| 637 | typedef int(*get_func)(struct acpi_battery *battery); |
| 638 | |
| 639 | static struct acpi_read_mux { |
| 640 | print_func print; |
| 641 | get_func get; |
| 642 | } acpi_read_funcs[ACPI_BATTERY_NUMFILES] = { |
| 643 | {.get = acpi_battery_get_info, .print = acpi_battery_print_info}, |
| 644 | {.get = acpi_battery_get_state, .print = acpi_battery_print_state}, |
| 645 | {.get = acpi_battery_get_alarm, .print = acpi_battery_print_alarm}, |
| 646 | }; |
| 647 | |
| 648 | static int acpi_battery_read(int fid, struct seq_file *seq) |
| 649 | { |
| 650 | struct acpi_battery *battery = seq->private; |
| 651 | int result = 0; |
| 652 | int update_result = ACPI_BATTERY_NONE_UPDATE; |
| 653 | int update = 0; |
| 654 | |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 655 | update = (get_seconds() - battery->update_time[fid] >= update_time); |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame^] | 656 | update = (update | battery->update[fid]); |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 657 | |
| 658 | result = acpi_battery_update(battery, update, &update_result); |
| 659 | if (result) |
| 660 | goto end; |
| 661 | |
| 662 | if (update_result == ACPI_BATTERY_EASY_UPDATE) { |
| 663 | result = acpi_read_funcs[fid].get(battery); |
| 664 | if (result) |
| 665 | goto end; |
| 666 | } |
| 667 | |
| 668 | end: |
| 669 | result = acpi_read_funcs[fid].print(seq, result); |
| 670 | acpi_battery_check_result(battery, result); |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame^] | 671 | battery->update[fid] = result; |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 672 | return result; |
| 673 | } |
| 674 | |
| 675 | static int acpi_battery_read_info(struct seq_file *seq, void *offset) |
| 676 | { |
| 677 | return acpi_battery_read(ACPI_BATTERY_INFO, seq); |
| 678 | } |
| 679 | |
| 680 | static int acpi_battery_read_state(struct seq_file *seq, void *offset) |
| 681 | { |
| 682 | return acpi_battery_read(ACPI_BATTERY_STATE, seq); |
| 683 | } |
| 684 | |
| 685 | static int acpi_battery_read_alarm(struct seq_file *seq, void *offset) |
| 686 | { |
| 687 | return acpi_battery_read(ACPI_BATTERY_ALARM, seq); |
| 688 | } |
| 689 | |
| 690 | static int acpi_battery_info_open_fs(struct inode *inode, struct file *file) |
| 691 | { |
| 692 | return single_open(file, acpi_battery_read_info, PDE(inode)->data); |
| 693 | } |
| 694 | |
| 695 | static int acpi_battery_state_open_fs(struct inode *inode, struct file *file) |
| 696 | { |
| 697 | return single_open(file, acpi_battery_read_state, PDE(inode)->data); |
| 698 | } |
| 699 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | static int acpi_battery_alarm_open_fs(struct inode *inode, struct file *file) |
| 701 | { |
| 702 | return single_open(file, acpi_battery_read_alarm, PDE(inode)->data); |
| 703 | } |
| 704 | |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 705 | static struct battery_file { |
| 706 | struct file_operations ops; |
| 707 | mode_t mode; |
| 708 | char *name; |
| 709 | } acpi_battery_file[] = { |
| 710 | { |
| 711 | .name = "info", |
| 712 | .mode = S_IRUGO, |
| 713 | .ops = { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 714 | .open = acpi_battery_info_open_fs, |
| 715 | .read = seq_read, |
| 716 | .llseek = seq_lseek, |
| 717 | .release = single_release, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | .owner = THIS_MODULE, |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 719 | }, |
| 720 | }, |
| 721 | { |
| 722 | .name = "state", |
| 723 | .mode = S_IRUGO, |
| 724 | .ops = { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 725 | .open = acpi_battery_state_open_fs, |
| 726 | .read = seq_read, |
| 727 | .llseek = seq_lseek, |
| 728 | .release = single_release, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | .owner = THIS_MODULE, |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 730 | }, |
| 731 | }, |
| 732 | { |
| 733 | .name = "alarm", |
| 734 | .mode = S_IFREG | S_IRUGO | S_IWUSR, |
| 735 | .ops = { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 736 | .open = acpi_battery_alarm_open_fs, |
| 737 | .read = seq_read, |
| 738 | .write = acpi_battery_write_alarm, |
| 739 | .llseek = seq_lseek, |
| 740 | .release = single_release, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | .owner = THIS_MODULE, |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 742 | }, |
| 743 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | }; |
| 745 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 746 | static int acpi_battery_add_fs(struct acpi_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 748 | struct proc_dir_entry *entry = NULL; |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 749 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | if (!acpi_device_dir(device)) { |
| 752 | acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 753 | acpi_battery_dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | if (!acpi_device_dir(device)) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 755 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | acpi_device_dir(device)->owner = THIS_MODULE; |
| 757 | } |
| 758 | |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 759 | for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i) { |
| 760 | entry = create_proc_entry(acpi_battery_file[i].name, |
| 761 | acpi_battery_file[i].mode, acpi_device_dir(device)); |
| 762 | if (!entry) |
| 763 | return -ENODEV; |
| 764 | else { |
| 765 | entry->proc_fops = &acpi_battery_file[i].ops; |
| 766 | entry->data = acpi_driver_data(device); |
| 767 | entry->owner = THIS_MODULE; |
| 768 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | } |
| 770 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 771 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | } |
| 773 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 774 | static int acpi_battery_remove_fs(struct acpi_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | { |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 776 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | if (acpi_device_dir(device)) { |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 778 | for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i) { |
| 779 | remove_proc_entry(acpi_battery_file[i].name, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | acpi_device_dir(device)); |
Alexey Starikovskiy | 78490d8 | 2007-05-11 13:18:55 -0400 | [diff] [blame] | 781 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | remove_proc_entry(acpi_device_bid(device), acpi_battery_dir); |
| 783 | acpi_device_dir(device) = NULL; |
| 784 | } |
| 785 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 786 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | } |
| 788 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | /* -------------------------------------------------------------------------- |
| 790 | Driver Interface |
| 791 | -------------------------------------------------------------------------- */ |
| 792 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 793 | static void acpi_battery_notify(acpi_handle handle, u32 event, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | { |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 795 | struct acpi_battery *battery = data; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 796 | struct acpi_device *device = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | if (!battery) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 799 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | |
Patrick Mochel | 145def8 | 2006-05-19 16:54:39 -0400 | [diff] [blame] | 801 | device = battery->device; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | |
| 803 | switch (event) { |
| 804 | case ACPI_BATTERY_NOTIFY_STATUS: |
| 805 | case ACPI_BATTERY_NOTIFY_INFO: |
Vladimir Lebedev | 9fdae72 | 2006-06-27 04:49:00 -0400 | [diff] [blame] | 806 | case ACPI_NOTIFY_BUS_CHECK: |
| 807 | case ACPI_NOTIFY_DEVICE_CHECK: |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 808 | device = battery->device; |
| 809 | acpi_battery_notify_update(battery); |
Len Brown | 14e04fb | 2007-08-23 15:20:26 -0400 | [diff] [blame] | 810 | acpi_bus_generate_proc_event(device, event, |
Vladimir Lebedev | 9ea7d57 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 811 | acpi_battery_present(battery)); |
Zhang Rui | 962ce8c | 2007-08-23 01:24:31 +0800 | [diff] [blame] | 812 | acpi_bus_generate_netlink_event(device->pnp.device_class, |
| 813 | device->dev.bus_id, event, |
| 814 | acpi_battery_present(battery)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | break; |
| 816 | default: |
| 817 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 818 | "Unsupported event [0x%x]\n", event)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | break; |
| 820 | } |
| 821 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 822 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | } |
| 824 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 825 | static int acpi_battery_add(struct acpi_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 827 | int result = 0; |
| 828 | acpi_status status = 0; |
| 829 | struct acpi_battery *battery = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | if (!device) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 832 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | |
Burman Yan | 36bcbec | 2006-12-19 12:56:11 -0800 | [diff] [blame] | 834 | battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | if (!battery) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 836 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | |
Patrick Mochel | 145def8 | 2006-05-19 16:54:39 -0400 | [diff] [blame] | 838 | battery->device = device; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME); |
| 840 | strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS); |
| 841 | acpi_driver_data(device) = battery; |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame^] | 842 | mutex_init(&battery->lock); |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 843 | result = acpi_battery_get_status(battery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | if (result) |
| 845 | goto end; |
| 846 | |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame^] | 847 | battery->init_update = 1; |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 848 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | result = acpi_battery_add_fs(device); |
| 850 | if (result) |
| 851 | goto end; |
| 852 | |
Patrick Mochel | 3b073ec | 2006-05-19 16:54:41 -0400 | [diff] [blame] | 853 | status = acpi_install_notify_handler(device->handle, |
Vladimir Lebedev | 9fdae72 | 2006-06-27 04:49:00 -0400 | [diff] [blame] | 854 | ACPI_ALL_NOTIFY, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 855 | acpi_battery_notify, battery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | if (ACPI_FAILURE(status)) { |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 857 | ACPI_EXCEPTION((AE_INFO, status, "Installing notify handler")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | result = -ENODEV; |
| 859 | goto end; |
| 860 | } |
| 861 | |
| 862 | printk(KERN_INFO PREFIX "%s Slot [%s] (battery %s)\n", |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 863 | ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device), |
| 864 | device->status.battery_present ? "present" : "absent"); |
| 865 | |
| 866 | end: |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 867 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | if (result) { |
| 869 | acpi_battery_remove_fs(device); |
| 870 | kfree(battery); |
| 871 | } |
| 872 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 873 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | } |
| 875 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 876 | static int acpi_battery_remove(struct acpi_device *device, int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 878 | acpi_status status = 0; |
| 879 | struct acpi_battery *battery = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | if (!device || !acpi_driver_data(device)) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 882 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 884 | battery = acpi_driver_data(device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | |
Patrick Mochel | 3b073ec | 2006-05-19 16:54:41 -0400 | [diff] [blame] | 886 | status = acpi_remove_notify_handler(device->handle, |
Vladimir Lebedev | 9fdae72 | 2006-06-27 04:49:00 -0400 | [diff] [blame] | 887 | ACPI_ALL_NOTIFY, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 888 | acpi_battery_notify); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | |
| 890 | acpi_battery_remove_fs(device); |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame^] | 891 | mutex_destroy(&battery->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | kfree(battery); |
| 893 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 894 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | } |
| 896 | |
Jiri Kosina | 34c4415 | 2006-10-10 14:20:41 -0700 | [diff] [blame] | 897 | /* this is needed to learn about changes made in suspended state */ |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 898 | static int acpi_battery_resume(struct acpi_device *device) |
Jiri Kosina | 34c4415 | 2006-10-10 14:20:41 -0700 | [diff] [blame] | 899 | { |
| 900 | struct acpi_battery *battery; |
| 901 | |
| 902 | if (!device) |
| 903 | return -EINVAL; |
| 904 | |
| 905 | battery = device->driver_data; |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 906 | |
Alexey Starikovskiy | 038fdea | 2007-09-26 19:42:46 +0400 | [diff] [blame^] | 907 | battery->init_update = 1; |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 908 | |
| 909 | return 0; |
Jiri Kosina | 34c4415 | 2006-10-10 14:20:41 -0700 | [diff] [blame] | 910 | } |
| 911 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 912 | static int __init acpi_battery_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | { |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 914 | int result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | |
Pavel Machek | 4d8316d | 2006-08-14 22:37:22 -0700 | [diff] [blame] | 916 | if (acpi_disabled) |
| 917 | return -ENODEV; |
| 918 | |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 919 | acpi_battery_dir = acpi_lock_battery_dir(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | if (!acpi_battery_dir) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 921 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | |
| 923 | result = acpi_bus_register_driver(&acpi_battery_driver); |
| 924 | if (result < 0) { |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 925 | acpi_unlock_battery_dir(acpi_battery_dir); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 926 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | } |
| 928 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 929 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | } |
| 931 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 932 | static void __exit acpi_battery_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 934 | acpi_bus_unregister_driver(&acpi_battery_driver); |
| 935 | |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 936 | acpi_unlock_battery_dir(acpi_battery_dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 938 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | } |
| 940 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | module_init(acpi_battery_init); |
| 942 | module_exit(acpi_battery_exit); |