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 | |
| 39 | #define ACPI_BATTERY_FORMAT_BIF "NNNNNNNNNSSSS" |
| 40 | #define ACPI_BATTERY_FORMAT_BST "NNNN" |
| 41 | |
| 42 | #define ACPI_BATTERY_COMPONENT 0x00040000 |
| 43 | #define ACPI_BATTERY_CLASS "battery" |
| 44 | #define ACPI_BATTERY_HID "PNP0C0A" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #define ACPI_BATTERY_DEVICE_NAME "Battery" |
| 46 | #define ACPI_BATTERY_FILE_INFO "info" |
Vladimir Lebedev | a1f0eff | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 47 | #define ACPI_BATTERY_FILE_STATE "state" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | #define ACPI_BATTERY_FILE_ALARM "alarm" |
| 49 | #define ACPI_BATTERY_NOTIFY_STATUS 0x80 |
| 50 | #define ACPI_BATTERY_NOTIFY_INFO 0x81 |
| 51 | #define ACPI_BATTERY_UNITS_WATTS "mW" |
| 52 | #define ACPI_BATTERY_UNITS_AMPS "mA" |
| 53 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | #define _COMPONENT ACPI_BATTERY_COMPONENT |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 55 | |
| 56 | #define ACPI_BATTERY_UPDATE_TIME 0 |
| 57 | |
| 58 | #define ACPI_BATTERY_NONE_UPDATE 0 |
| 59 | #define ACPI_BATTERY_EASY_UPDATE 1 |
| 60 | #define ACPI_BATTERY_INIT_UPDATE 2 |
| 61 | |
Len Brown | f52fd66 | 2007-02-12 22:42:12 -0500 | [diff] [blame] | 62 | ACPI_MODULE_NAME("battery"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
Len Brown | f52fd66 | 2007-02-12 22:42:12 -0500 | [diff] [blame] | 64 | MODULE_AUTHOR("Paul Diefenbaugh"); |
Len Brown | 7cda93e | 2007-02-12 23:50:02 -0500 | [diff] [blame] | 65 | MODULE_DESCRIPTION("ACPI Battery Driver"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | MODULE_LICENSE("GPL"); |
| 67 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 68 | static unsigned int update_time = ACPI_BATTERY_UPDATE_TIME; |
| 69 | |
| 70 | /* 0 - every time, > 0 - by update_time */ |
| 71 | module_param(update_time, uint, 0644); |
| 72 | |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 73 | extern struct proc_dir_entry *acpi_lock_battery_dir(void); |
| 74 | extern void *acpi_unlock_battery_dir(struct proc_dir_entry *acpi_battery_dir); |
| 75 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 76 | static int acpi_battery_add(struct acpi_device *device); |
| 77 | static int acpi_battery_remove(struct acpi_device *device, int type); |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 78 | static int acpi_battery_resume(struct acpi_device *device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
| 80 | static struct acpi_driver acpi_battery_driver = { |
Len Brown | c2b6705b | 2007-02-12 23:33:40 -0500 | [diff] [blame] | 81 | .name = "battery", |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 82 | .class = ACPI_BATTERY_CLASS, |
| 83 | .ids = ACPI_BATTERY_HID, |
| 84 | .ops = { |
| 85 | .add = acpi_battery_add, |
Jiri Kosina | 34c4415 | 2006-10-10 14:20:41 -0700 | [diff] [blame] | 86 | .resume = acpi_battery_resume, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 87 | .remove = acpi_battery_remove, |
| 88 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | }; |
| 90 | |
Vladimir Lebedev | a1f0eff | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 91 | struct acpi_battery_state { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 92 | acpi_integer state; |
| 93 | acpi_integer present_rate; |
| 94 | acpi_integer remaining_capacity; |
| 95 | acpi_integer present_voltage; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | struct acpi_battery_info { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 99 | acpi_integer power_unit; |
| 100 | acpi_integer design_capacity; |
| 101 | acpi_integer last_full_capacity; |
| 102 | acpi_integer battery_technology; |
| 103 | acpi_integer design_voltage; |
| 104 | acpi_integer design_capacity_warning; |
| 105 | acpi_integer design_capacity_low; |
| 106 | acpi_integer battery_capacity_granularity_1; |
| 107 | acpi_integer battery_capacity_granularity_2; |
| 108 | acpi_string model_number; |
| 109 | acpi_string serial_number; |
| 110 | acpi_string battery_type; |
| 111 | acpi_string oem_info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | }; |
| 113 | |
| 114 | struct acpi_battery_flags { |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 115 | u8 battery_present_prev; |
| 116 | u8 alarm_present; |
| 117 | u8 init_update; |
| 118 | u8 info_update; |
| 119 | u8 state_update; |
| 120 | u8 alarm_update; |
| 121 | u8 power_unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | struct acpi_battery { |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 125 | struct mutex mutex; |
Vladimir Lebedev | 9ea7d57 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 126 | struct acpi_device *device; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | struct acpi_battery_flags flags; |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 128 | struct acpi_buffer bif_data; |
| 129 | struct acpi_buffer bst_data; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 130 | unsigned long alarm; |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 131 | unsigned long info_update_time; |
| 132 | unsigned long state_update_time; |
| 133 | unsigned long alarm_update_time; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | }; |
| 135 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 136 | #define acpi_battery_present(battery) battery->device->status.battery_present |
| 137 | #define acpi_battery_present_prev(battery) battery->flags.battery_present_prev |
| 138 | #define acpi_battery_alarm_present(battery) battery->flags.alarm_present |
| 139 | #define acpi_battery_init_update_flag(battery) battery->flags.init_update |
| 140 | #define acpi_battery_info_update_flag(battery) battery->flags.info_update |
| 141 | #define acpi_battery_state_update_flag(battery) battery->flags.state_update |
| 142 | #define acpi_battery_alarm_update_flag(battery) battery->flags.alarm_update |
| 143 | #define acpi_battery_power_units(battery) battery->flags.power_unit ? \ |
| 144 | ACPI_BATTERY_UNITS_AMPS : ACPI_BATTERY_UNITS_WATTS |
| 145 | #define acpi_battery_handle(battery) battery->device->handle |
| 146 | #define acpi_battery_inserted(battery) (!acpi_battery_present_prev(battery) & acpi_battery_present(battery)) |
| 147 | #define acpi_battery_removed(battery) (acpi_battery_present_prev(battery) & !acpi_battery_present(battery)) |
| 148 | #define acpi_battery_bid(battery) acpi_device_bid(battery->device) |
| 149 | #define acpi_battery_status_str(battery) acpi_battery_present(battery) ? "present" : "absent" |
| 150 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | /* -------------------------------------------------------------------------- |
| 152 | Battery Management |
| 153 | -------------------------------------------------------------------------- */ |
| 154 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 155 | static void acpi_battery_mutex_lock(struct acpi_battery *battery) |
| 156 | { |
| 157 | mutex_lock(&battery->mutex); |
| 158 | } |
| 159 | |
| 160 | static void acpi_battery_mutex_unlock(struct acpi_battery *battery) |
| 161 | { |
| 162 | mutex_unlock(&battery->mutex); |
| 163 | } |
| 164 | |
| 165 | static void acpi_battery_check_result(struct acpi_battery *battery, int result) |
| 166 | { |
| 167 | if (!battery) |
| 168 | return; |
| 169 | |
| 170 | if (result) { |
| 171 | acpi_battery_init_update_flag(battery) = 1; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | static int acpi_battery_extract_package(struct acpi_battery *battery, |
| 176 | union acpi_object *package, |
| 177 | struct acpi_buffer *format, |
| 178 | struct acpi_buffer *data, |
| 179 | char *package_name) |
| 180 | { |
| 181 | acpi_status status = AE_OK; |
| 182 | struct acpi_buffer data_null = { 0, NULL }; |
| 183 | |
| 184 | status = acpi_extract_package(package, format, &data_null); |
| 185 | if (status != AE_BUFFER_OVERFLOW) { |
| 186 | ACPI_EXCEPTION((AE_INFO, status, "Extracting size %s", |
| 187 | package_name)); |
| 188 | return -ENODEV; |
| 189 | } |
| 190 | |
| 191 | if (data_null.length != data->length) { |
| 192 | if (data->pointer) { |
| 193 | kfree(data->pointer); |
| 194 | } |
| 195 | data->pointer = kzalloc(data_null.length, GFP_KERNEL); |
| 196 | if (!data->pointer) { |
| 197 | ACPI_EXCEPTION((AE_INFO, AE_NO_MEMORY, "kzalloc()")); |
| 198 | return -ENOMEM; |
| 199 | } |
| 200 | data->length = data_null.length; |
| 201 | } |
| 202 | |
| 203 | status = acpi_extract_package(package, format, data); |
| 204 | if (ACPI_FAILURE(status)) { |
| 205 | ACPI_EXCEPTION((AE_INFO, status, "Extracting %s", |
| 206 | package_name)); |
| 207 | return -ENODEV; |
| 208 | } |
| 209 | |
| 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 }; |
| 230 | struct acpi_buffer format = { sizeof(ACPI_BATTERY_FORMAT_BIF), |
| 231 | ACPI_BATTERY_FORMAT_BIF |
| 232 | }; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 233 | union acpi_object *package = NULL; |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 234 | struct acpi_buffer *data = NULL; |
| 235 | struct acpi_battery_info *bif = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 237 | battery->info_update_time = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 239 | if (!acpi_battery_present(battery)) |
| 240 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | |
| 242 | /* Evalute _BIF */ |
| 243 | |
Vladimir Lebedev | 9ea7d57 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 244 | status = |
| 245 | acpi_evaluate_object(acpi_battery_handle(battery), "_BIF", NULL, |
| 246 | &buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | if (ACPI_FAILURE(status)) { |
Thomas Renninger | a6fc672 | 2006-06-26 23:58:43 -0400 | [diff] [blame] | 248 | ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BIF")); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 249 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 252 | package = buffer.pointer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 254 | data = &battery->bif_data; |
| 255 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | /* Extract Package Data */ |
| 257 | |
Vladimir Lebedev | 9ea7d57 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 258 | result = |
| 259 | acpi_battery_extract_package(battery, package, &format, data, |
| 260 | "_BIF"); |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 261 | if (result) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | goto end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 264 | end: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 266 | if (buffer.pointer) { |
| 267 | kfree(buffer.pointer); |
| 268 | } |
| 269 | |
| 270 | if (!result) { |
| 271 | bif = data->pointer; |
| 272 | battery->flags.power_unit = bif->power_unit; |
| 273 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 275 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | } |
| 277 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 278 | static int acpi_battery_get_state(struct acpi_battery *battery) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 280 | int result = 0; |
| 281 | acpi_status status = 0; |
| 282 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; |
| 283 | struct acpi_buffer format = { sizeof(ACPI_BATTERY_FORMAT_BST), |
| 284 | ACPI_BATTERY_FORMAT_BST |
| 285 | }; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 286 | union acpi_object *package = NULL; |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 287 | struct acpi_buffer *data = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 289 | battery->state_update_time = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 291 | if (!acpi_battery_present(battery)) |
| 292 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | |
| 294 | /* Evalute _BST */ |
| 295 | |
Vladimir Lebedev | 9ea7d57 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 296 | status = |
| 297 | acpi_evaluate_object(acpi_battery_handle(battery), "_BST", NULL, |
| 298 | &buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | if (ACPI_FAILURE(status)) { |
Thomas Renninger | a6fc672 | 2006-06-26 23:58:43 -0400 | [diff] [blame] | 300 | ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BST")); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 301 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 304 | package = buffer.pointer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 306 | data = &battery->bst_data; |
| 307 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | /* Extract Package Data */ |
| 309 | |
Vladimir Lebedev | 9ea7d57 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 310 | result = |
| 311 | acpi_battery_extract_package(battery, package, &format, data, |
| 312 | "_BST"); |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 313 | if (result) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | goto end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 316 | end: |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 317 | if (buffer.pointer) { |
| 318 | kfree(buffer.pointer); |
| 319 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 321 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | } |
| 323 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 324 | static int acpi_battery_get_alarm(struct acpi_battery *battery) |
| 325 | { |
| 326 | battery->alarm_update_time = get_seconds(); |
| 327 | |
| 328 | return 0; |
| 329 | } |
| 330 | |
Vladimir Lebedev | 9ea7d57 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 331 | static int acpi_battery_set_alarm(struct acpi_battery *battery, |
| 332 | unsigned long alarm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 334 | acpi_status status = 0; |
| 335 | union acpi_object arg0 = { ACPI_TYPE_INTEGER }; |
| 336 | struct acpi_object_list arg_list = { 1, &arg0 }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 338 | battery->alarm_update_time = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 340 | if (!acpi_battery_present(battery)) |
| 341 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 343 | if (!acpi_battery_alarm_present(battery)) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 344 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | |
| 346 | arg0.integer.value = alarm; |
| 347 | |
Vladimir Lebedev | 9ea7d57 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 348 | status = |
| 349 | acpi_evaluate_object(acpi_battery_handle(battery), "_BTP", |
| 350 | &arg_list, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | if (ACPI_FAILURE(status)) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 352 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | |
| 354 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", (u32) alarm)); |
| 355 | |
| 356 | battery->alarm = alarm; |
| 357 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 358 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | } |
| 360 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 361 | static int acpi_battery_init_alarm(struct acpi_battery *battery) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 363 | int result = 0; |
| 364 | acpi_status status = AE_OK; |
| 365 | acpi_handle handle = NULL; |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 366 | struct acpi_battery_info *bif = battery->bif_data.pointer; |
| 367 | unsigned long alarm = battery->alarm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 369 | /* See if alarms are supported, and if so, set default */ |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 370 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 371 | status = acpi_get_handle(acpi_battery_handle(battery), "_BTP", &handle); |
| 372 | if (ACPI_SUCCESS(status)) { |
| 373 | acpi_battery_alarm_present(battery) = 1; |
| 374 | if (!alarm && bif) { |
| 375 | alarm = bif->design_capacity_warning; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | } |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 377 | result = acpi_battery_set_alarm(battery, alarm); |
| 378 | if (result) |
| 379 | goto end; |
| 380 | } else { |
| 381 | acpi_battery_alarm_present(battery) = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | } |
| 383 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 384 | end: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 386 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | } |
| 388 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 389 | static int acpi_battery_init_update(struct acpi_battery *battery) |
Vladimir Lebedev | 4bd35cd | 2007-02-10 01:43:48 -0500 | [diff] [blame] | 390 | { |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 391 | int result = 0; |
| 392 | |
| 393 | result = acpi_battery_get_status(battery); |
| 394 | if (result) |
| 395 | return result; |
| 396 | |
| 397 | acpi_battery_present_prev(battery) = acpi_battery_present(battery); |
| 398 | |
| 399 | if (acpi_battery_present(battery)) { |
| 400 | result = acpi_battery_get_info(battery); |
| 401 | if (result) |
| 402 | return result; |
| 403 | result = acpi_battery_get_state(battery); |
| 404 | if (result) |
| 405 | return result; |
| 406 | |
| 407 | acpi_battery_init_alarm(battery); |
| 408 | } |
| 409 | |
| 410 | return result; |
| 411 | } |
| 412 | |
| 413 | static int acpi_battery_update(struct acpi_battery *battery, |
Vladimir Lebedev | 9ea7d57 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 414 | int update, int *update_result_ptr) |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 415 | { |
| 416 | int result = 0; |
| 417 | int update_result = ACPI_BATTERY_NONE_UPDATE; |
| 418 | |
| 419 | if (!acpi_battery_present(battery)) { |
| 420 | update = 1; |
| 421 | } |
| 422 | |
| 423 | if (acpi_battery_init_update_flag(battery)) { |
| 424 | result = acpi_battery_init_update(battery); |
| 425 | if (result) |
| 426 | goto end;; |
| 427 | update_result = ACPI_BATTERY_INIT_UPDATE; |
| 428 | } else if (update) { |
| 429 | result = acpi_battery_get_status(battery); |
| 430 | if (result) |
| 431 | goto end;; |
Vladimir Lebedev | 9ea7d57 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 432 | if (acpi_battery_inserted(battery) |
| 433 | || acpi_battery_removed(battery)) { |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 434 | result = acpi_battery_init_update(battery); |
| 435 | if (result) |
| 436 | goto end;; |
| 437 | update_result = ACPI_BATTERY_INIT_UPDATE; |
| 438 | } else { |
| 439 | update_result = ACPI_BATTERY_EASY_UPDATE; |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | end: |
| 444 | |
| 445 | acpi_battery_init_update_flag(battery) = (result != 0); |
| 446 | |
| 447 | *update_result_ptr = update_result; |
| 448 | |
| 449 | return result; |
| 450 | } |
| 451 | |
| 452 | static void acpi_battery_notify_update(struct acpi_battery *battery) |
| 453 | { |
| 454 | acpi_battery_get_status(battery); |
| 455 | |
| 456 | if (acpi_battery_init_update_flag(battery)) { |
| 457 | return; |
| 458 | } |
| 459 | |
| 460 | if (acpi_battery_inserted(battery) || acpi_battery_removed(battery)) { |
| 461 | acpi_battery_init_update_flag(battery) = 1; |
| 462 | } else { |
| 463 | acpi_battery_info_update_flag(battery) = 1; |
| 464 | acpi_battery_state_update_flag(battery) = 1; |
| 465 | acpi_battery_alarm_update_flag(battery) = 1; |
Vladimir Lebedev | 4bd35cd | 2007-02-10 01:43:48 -0500 | [diff] [blame] | 466 | } |
| 467 | } |
| 468 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | /* -------------------------------------------------------------------------- |
| 470 | FS Interface (/proc) |
| 471 | -------------------------------------------------------------------------- */ |
| 472 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 473 | static struct proc_dir_entry *acpi_battery_dir; |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 474 | |
| 475 | static int acpi_battery_read_info_print(struct seq_file *seq, int result) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | { |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 477 | struct acpi_battery *battery = seq->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | struct acpi_battery_info *bif = NULL; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 479 | char *units = "?"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 481 | if (result) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | goto end; |
| 483 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 484 | if (acpi_battery_present(battery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | seq_printf(seq, "present: yes\n"); |
| 486 | else { |
| 487 | seq_printf(seq, "present: no\n"); |
| 488 | goto end; |
| 489 | } |
| 490 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 491 | bif = battery->bif_data.pointer; |
| 492 | if (!bif) { |
| 493 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, "BIF buffer is NULL")); |
| 494 | result = -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | goto end; |
| 496 | } |
| 497 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 498 | /* Battery Units */ |
| 499 | |
| 500 | units = acpi_battery_power_units(battery); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 501 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | if (bif->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN) |
| 503 | seq_printf(seq, "design capacity: unknown\n"); |
| 504 | else |
| 505 | seq_printf(seq, "design capacity: %d %sh\n", |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 506 | (u32) bif->design_capacity, units); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | |
| 508 | if (bif->last_full_capacity == ACPI_BATTERY_VALUE_UNKNOWN) |
| 509 | seq_printf(seq, "last full capacity: unknown\n"); |
| 510 | else |
| 511 | seq_printf(seq, "last full capacity: %d %sh\n", |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 512 | (u32) bif->last_full_capacity, units); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | |
| 514 | switch ((u32) bif->battery_technology) { |
| 515 | case 0: |
| 516 | seq_printf(seq, "battery technology: non-rechargeable\n"); |
| 517 | break; |
| 518 | case 1: |
| 519 | seq_printf(seq, "battery technology: rechargeable\n"); |
| 520 | break; |
| 521 | default: |
| 522 | seq_printf(seq, "battery technology: unknown\n"); |
| 523 | break; |
| 524 | } |
| 525 | |
| 526 | if (bif->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN) |
| 527 | seq_printf(seq, "design voltage: unknown\n"); |
| 528 | else |
| 529 | seq_printf(seq, "design voltage: %d mV\n", |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 530 | (u32) bif->design_voltage); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 531 | seq_printf(seq, "design capacity warning: %d %sh\n", |
| 532 | (u32) bif->design_capacity_warning, units); |
| 533 | seq_printf(seq, "design capacity low: %d %sh\n", |
| 534 | (u32) bif->design_capacity_low, units); |
| 535 | seq_printf(seq, "capacity granularity 1: %d %sh\n", |
| 536 | (u32) bif->battery_capacity_granularity_1, units); |
| 537 | seq_printf(seq, "capacity granularity 2: %d %sh\n", |
| 538 | (u32) bif->battery_capacity_granularity_2, units); |
| 539 | seq_printf(seq, "model number: %s\n", bif->model_number); |
| 540 | seq_printf(seq, "serial number: %s\n", bif->serial_number); |
| 541 | seq_printf(seq, "battery type: %s\n", bif->battery_type); |
| 542 | seq_printf(seq, "OEM info: %s\n", bif->oem_info); |
| 543 | |
| 544 | end: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 546 | if (result) |
| 547 | seq_printf(seq, "ERROR: Unable to read battery info\n"); |
| 548 | |
| 549 | return result; |
| 550 | } |
| 551 | |
| 552 | static int acpi_battery_read_info(struct seq_file *seq, void *offset) |
| 553 | { |
| 554 | struct acpi_battery *battery = seq->private; |
| 555 | int result = 0; |
| 556 | int update_result = ACPI_BATTERY_NONE_UPDATE; |
| 557 | int update = 0; |
| 558 | |
| 559 | acpi_battery_mutex_lock(battery); |
| 560 | |
| 561 | update = (get_seconds() - battery->info_update_time >= update_time); |
| 562 | update = (update | acpi_battery_info_update_flag(battery)); |
| 563 | |
| 564 | result = acpi_battery_update(battery, update, &update_result); |
| 565 | if (result) |
| 566 | goto end; |
| 567 | |
| 568 | /* Battery Info (_BIF) */ |
| 569 | |
| 570 | if (update_result == ACPI_BATTERY_EASY_UPDATE) { |
| 571 | result = acpi_battery_get_info(battery); |
| 572 | if (result) |
| 573 | goto end; |
| 574 | } |
| 575 | |
| 576 | end: |
| 577 | |
| 578 | result = acpi_battery_read_info_print(seq, result); |
| 579 | |
| 580 | acpi_battery_check_result(battery, result); |
| 581 | |
| 582 | acpi_battery_info_update_flag(battery) = result; |
| 583 | |
| 584 | acpi_battery_mutex_unlock(battery); |
| 585 | |
| 586 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | static int acpi_battery_info_open_fs(struct inode *inode, struct file *file) |
| 590 | { |
| 591 | return single_open(file, acpi_battery_read_info, PDE(inode)->data); |
| 592 | } |
| 593 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 594 | static int acpi_battery_read_state_print(struct seq_file *seq, int result) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | { |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 596 | struct acpi_battery *battery = seq->private; |
Vladimir Lebedev | a1f0eff | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 597 | struct acpi_battery_state *bst = NULL; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 598 | char *units = "?"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 600 | if (result) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | goto end; |
| 602 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 603 | if (acpi_battery_present(battery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | seq_printf(seq, "present: yes\n"); |
| 605 | else { |
| 606 | seq_printf(seq, "present: no\n"); |
| 607 | goto end; |
| 608 | } |
| 609 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 610 | bst = battery->bst_data.pointer; |
| 611 | if (!bst) { |
| 612 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, "BST buffer is NULL")); |
| 613 | result = -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | goto end; |
| 615 | } |
| 616 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 617 | /* Battery Units */ |
| 618 | |
| 619 | units = acpi_battery_power_units(battery); |
| 620 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | if (!(bst->state & 0x04)) |
| 622 | seq_printf(seq, "capacity state: ok\n"); |
| 623 | else |
| 624 | seq_printf(seq, "capacity state: critical\n"); |
| 625 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 626 | if ((bst->state & 0x01) && (bst->state & 0x02)) { |
| 627 | seq_printf(seq, |
| 628 | "charging state: charging/discharging\n"); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 629 | } else if (bst->state & 0x01) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | seq_printf(seq, "charging state: discharging\n"); |
| 631 | else if (bst->state & 0x02) |
| 632 | seq_printf(seq, "charging state: charging\n"); |
| 633 | else { |
| 634 | seq_printf(seq, "charging state: charged\n"); |
| 635 | } |
| 636 | |
| 637 | if (bst->present_rate == ACPI_BATTERY_VALUE_UNKNOWN) |
| 638 | seq_printf(seq, "present rate: unknown\n"); |
| 639 | else |
| 640 | seq_printf(seq, "present rate: %d %s\n", |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 641 | (u32) bst->present_rate, units); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | |
| 643 | if (bst->remaining_capacity == ACPI_BATTERY_VALUE_UNKNOWN) |
| 644 | seq_printf(seq, "remaining capacity: unknown\n"); |
| 645 | else |
| 646 | seq_printf(seq, "remaining capacity: %d %sh\n", |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 647 | (u32) bst->remaining_capacity, units); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | |
| 649 | if (bst->present_voltage == ACPI_BATTERY_VALUE_UNKNOWN) |
| 650 | seq_printf(seq, "present voltage: unknown\n"); |
| 651 | else |
| 652 | seq_printf(seq, "present voltage: %d mV\n", |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 653 | (u32) bst->present_voltage); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 655 | end: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 657 | if (result) { |
| 658 | seq_printf(seq, "ERROR: Unable to read battery state\n"); |
| 659 | } |
| 660 | |
| 661 | return result; |
| 662 | } |
| 663 | |
| 664 | static int acpi_battery_read_state(struct seq_file *seq, void *offset) |
| 665 | { |
| 666 | struct acpi_battery *battery = seq->private; |
| 667 | int result = 0; |
| 668 | int update_result = ACPI_BATTERY_NONE_UPDATE; |
| 669 | int update = 0; |
| 670 | |
| 671 | acpi_battery_mutex_lock(battery); |
| 672 | |
| 673 | update = (get_seconds() - battery->state_update_time >= update_time); |
| 674 | update = (update | acpi_battery_state_update_flag(battery)); |
| 675 | |
| 676 | result = acpi_battery_update(battery, update, &update_result); |
| 677 | if (result) |
| 678 | goto end; |
| 679 | |
| 680 | /* Battery State (_BST) */ |
| 681 | |
| 682 | if (update_result == ACPI_BATTERY_EASY_UPDATE) { |
| 683 | result = acpi_battery_get_state(battery); |
| 684 | if (result) |
| 685 | goto end; |
| 686 | } |
| 687 | |
| 688 | end: |
| 689 | |
| 690 | result = acpi_battery_read_state_print(seq, result); |
| 691 | |
| 692 | acpi_battery_check_result(battery, result); |
| 693 | |
| 694 | acpi_battery_state_update_flag(battery) = result; |
| 695 | |
| 696 | acpi_battery_mutex_unlock(battery); |
| 697 | |
| 698 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | static int acpi_battery_state_open_fs(struct inode *inode, struct file *file) |
| 702 | { |
| 703 | return single_open(file, acpi_battery_read_state, PDE(inode)->data); |
| 704 | } |
| 705 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 706 | static int acpi_battery_read_alarm_print(struct seq_file *seq, int result) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | { |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 708 | struct acpi_battery *battery = seq->private; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 709 | char *units = "?"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 711 | if (result) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | goto end; |
| 713 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 714 | if (!acpi_battery_present(battery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | seq_printf(seq, "present: no\n"); |
| 716 | goto end; |
| 717 | } |
| 718 | |
| 719 | /* Battery Units */ |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 720 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 721 | units = acpi_battery_power_units(battery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | |
| 723 | seq_printf(seq, "alarm: "); |
| 724 | if (!battery->alarm) |
| 725 | seq_printf(seq, "unsupported\n"); |
| 726 | else |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 727 | seq_printf(seq, "%lu %sh\n", battery->alarm, units); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 729 | end: |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 730 | |
| 731 | if (result) |
| 732 | seq_printf(seq, "ERROR: Unable to read battery alarm\n"); |
| 733 | |
| 734 | return result; |
| 735 | } |
| 736 | |
| 737 | static int acpi_battery_read_alarm(struct seq_file *seq, void *offset) |
| 738 | { |
| 739 | struct acpi_battery *battery = seq->private; |
| 740 | int result = 0; |
| 741 | int update_result = ACPI_BATTERY_NONE_UPDATE; |
| 742 | int update = 0; |
| 743 | |
| 744 | acpi_battery_mutex_lock(battery); |
| 745 | |
| 746 | update = (get_seconds() - battery->alarm_update_time >= update_time); |
| 747 | update = (update | acpi_battery_alarm_update_flag(battery)); |
| 748 | |
| 749 | result = acpi_battery_update(battery, update, &update_result); |
| 750 | if (result) |
| 751 | goto end; |
| 752 | |
| 753 | /* Battery Alarm */ |
| 754 | |
| 755 | if (update_result == ACPI_BATTERY_EASY_UPDATE) { |
| 756 | result = acpi_battery_get_alarm(battery); |
| 757 | if (result) |
| 758 | goto end; |
| 759 | } |
| 760 | |
| 761 | end: |
| 762 | |
| 763 | result = acpi_battery_read_alarm_print(seq, result); |
| 764 | |
| 765 | acpi_battery_check_result(battery, result); |
| 766 | |
| 767 | acpi_battery_alarm_update_flag(battery) = result; |
| 768 | |
| 769 | acpi_battery_mutex_unlock(battery); |
| 770 | |
| 771 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | } |
| 773 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | static ssize_t |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 775 | acpi_battery_write_alarm(struct file *file, |
| 776 | const char __user * buffer, |
| 777 | size_t count, loff_t * ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 779 | int result = 0; |
| 780 | char alarm_string[12] = { '\0' }; |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 781 | struct seq_file *m = file->private_data; |
| 782 | struct acpi_battery *battery = m->private; |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 783 | int update_result = ACPI_BATTERY_NONE_UPDATE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | if (!battery || (count > sizeof(alarm_string) - 1)) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 786 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 788 | acpi_battery_mutex_lock(battery); |
Vladimir Lebedev | 4bd35cd | 2007-02-10 01:43:48 -0500 | [diff] [blame] | 789 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 790 | result = acpi_battery_update(battery, 1, &update_result); |
| 791 | if (result) { |
| 792 | result = -ENODEV; |
| 793 | goto end; |
| 794 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 796 | if (!acpi_battery_present(battery)) { |
| 797 | result = -ENODEV; |
| 798 | goto end; |
| 799 | } |
| 800 | |
| 801 | if (copy_from_user(alarm_string, buffer, count)) { |
| 802 | result = -EFAULT; |
| 803 | goto end; |
| 804 | } |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 805 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | alarm_string[count] = '\0'; |
| 807 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 808 | result = acpi_battery_set_alarm(battery, |
| 809 | simple_strtoul(alarm_string, NULL, 0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | if (result) |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 811 | goto end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 813 | end: |
| 814 | |
| 815 | acpi_battery_check_result(battery, result); |
| 816 | |
| 817 | if (!result) |
| 818 | result = count; |
| 819 | |
| 820 | acpi_battery_mutex_unlock(battery); |
| 821 | |
| 822 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | } |
| 824 | |
| 825 | static int acpi_battery_alarm_open_fs(struct inode *inode, struct file *file) |
| 826 | { |
| 827 | return single_open(file, acpi_battery_read_alarm, PDE(inode)->data); |
| 828 | } |
| 829 | |
Arjan van de Ven | d750803 | 2006-07-04 13:06:00 -0400 | [diff] [blame] | 830 | static const struct file_operations acpi_battery_info_ops = { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 831 | .open = acpi_battery_info_open_fs, |
| 832 | .read = seq_read, |
| 833 | .llseek = seq_lseek, |
| 834 | .release = single_release, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | .owner = THIS_MODULE, |
| 836 | }; |
| 837 | |
Arjan van de Ven | d750803 | 2006-07-04 13:06:00 -0400 | [diff] [blame] | 838 | static const struct file_operations acpi_battery_state_ops = { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 839 | .open = acpi_battery_state_open_fs, |
| 840 | .read = seq_read, |
| 841 | .llseek = seq_lseek, |
| 842 | .release = single_release, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | .owner = THIS_MODULE, |
| 844 | }; |
| 845 | |
Arjan van de Ven | d750803 | 2006-07-04 13:06:00 -0400 | [diff] [blame] | 846 | static const struct file_operations acpi_battery_alarm_ops = { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 847 | .open = acpi_battery_alarm_open_fs, |
| 848 | .read = seq_read, |
| 849 | .write = acpi_battery_write_alarm, |
| 850 | .llseek = seq_lseek, |
| 851 | .release = single_release, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | .owner = THIS_MODULE, |
| 853 | }; |
| 854 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 855 | static int acpi_battery_add_fs(struct acpi_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 857 | struct proc_dir_entry *entry = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | if (!acpi_device_dir(device)) { |
| 860 | acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 861 | acpi_battery_dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | if (!acpi_device_dir(device)) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 863 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | acpi_device_dir(device)->owner = THIS_MODULE; |
| 865 | } |
| 866 | |
| 867 | /* 'info' [R] */ |
| 868 | entry = create_proc_entry(ACPI_BATTERY_FILE_INFO, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 869 | S_IRUGO, acpi_device_dir(device)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | if (!entry) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 871 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | else { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 873 | entry->proc_fops = &acpi_battery_info_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | entry->data = acpi_driver_data(device); |
| 875 | entry->owner = THIS_MODULE; |
| 876 | } |
| 877 | |
| 878 | /* 'status' [R] */ |
Vladimir Lebedev | a1f0eff | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 879 | entry = create_proc_entry(ACPI_BATTERY_FILE_STATE, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 880 | S_IRUGO, acpi_device_dir(device)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | if (!entry) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 882 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | else { |
| 884 | entry->proc_fops = &acpi_battery_state_ops; |
| 885 | entry->data = acpi_driver_data(device); |
| 886 | entry->owner = THIS_MODULE; |
| 887 | } |
| 888 | |
| 889 | /* 'alarm' [R/W] */ |
| 890 | entry = create_proc_entry(ACPI_BATTERY_FILE_ALARM, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 891 | S_IFREG | S_IRUGO | S_IWUSR, |
| 892 | acpi_device_dir(device)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | if (!entry) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 894 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | else { |
| 896 | entry->proc_fops = &acpi_battery_alarm_ops; |
| 897 | entry->data = acpi_driver_data(device); |
| 898 | entry->owner = THIS_MODULE; |
| 899 | } |
| 900 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 901 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | } |
| 903 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 904 | static int acpi_battery_remove_fs(struct acpi_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | if (acpi_device_dir(device)) { |
| 907 | remove_proc_entry(ACPI_BATTERY_FILE_ALARM, |
| 908 | acpi_device_dir(device)); |
Vladimir Lebedev | a1f0eff | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 909 | remove_proc_entry(ACPI_BATTERY_FILE_STATE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | acpi_device_dir(device)); |
| 911 | remove_proc_entry(ACPI_BATTERY_FILE_INFO, |
| 912 | acpi_device_dir(device)); |
| 913 | |
| 914 | remove_proc_entry(acpi_device_bid(device), acpi_battery_dir); |
| 915 | acpi_device_dir(device) = NULL; |
| 916 | } |
| 917 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 918 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | } |
| 920 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | /* -------------------------------------------------------------------------- |
| 922 | Driver Interface |
| 923 | -------------------------------------------------------------------------- */ |
| 924 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 925 | static void acpi_battery_notify(acpi_handle handle, u32 event, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | { |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 927 | struct acpi_battery *battery = data; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 928 | struct acpi_device *device = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 929 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | if (!battery) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 931 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | |
Patrick Mochel | 145def8 | 2006-05-19 16:54:39 -0400 | [diff] [blame] | 933 | device = battery->device; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 934 | |
| 935 | switch (event) { |
| 936 | case ACPI_BATTERY_NOTIFY_STATUS: |
| 937 | case ACPI_BATTERY_NOTIFY_INFO: |
Vladimir Lebedev | 9fdae72 | 2006-06-27 04:49:00 -0400 | [diff] [blame] | 938 | case ACPI_NOTIFY_BUS_CHECK: |
| 939 | case ACPI_NOTIFY_DEVICE_CHECK: |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 940 | device = battery->device; |
| 941 | acpi_battery_notify_update(battery); |
Vladimir Lebedev | 9ea7d57 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 942 | acpi_bus_generate_event(device, event, |
| 943 | acpi_battery_present(battery)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | break; |
| 945 | default: |
| 946 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 947 | "Unsupported event [0x%x]\n", event)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | break; |
| 949 | } |
| 950 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 951 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | } |
| 953 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 954 | static int acpi_battery_add(struct acpi_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 956 | int result = 0; |
| 957 | acpi_status status = 0; |
| 958 | struct acpi_battery *battery = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | if (!device) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 961 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | |
Burman Yan | 36bcbec | 2006-12-19 12:56:11 -0800 | [diff] [blame] | 963 | battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | if (!battery) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 965 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 967 | mutex_init(&battery->mutex); |
| 968 | |
| 969 | acpi_battery_mutex_lock(battery); |
| 970 | |
Patrick Mochel | 145def8 | 2006-05-19 16:54:39 -0400 | [diff] [blame] | 971 | battery->device = device; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME); |
| 973 | strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS); |
| 974 | acpi_driver_data(device) = battery; |
| 975 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 976 | result = acpi_battery_get_status(battery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | if (result) |
| 978 | goto end; |
| 979 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 980 | acpi_battery_init_update_flag(battery) = 1; |
| 981 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | result = acpi_battery_add_fs(device); |
| 983 | if (result) |
| 984 | goto end; |
| 985 | |
Patrick Mochel | 3b073ec | 2006-05-19 16:54:41 -0400 | [diff] [blame] | 986 | status = acpi_install_notify_handler(device->handle, |
Vladimir Lebedev | 9fdae72 | 2006-06-27 04:49:00 -0400 | [diff] [blame] | 987 | ACPI_ALL_NOTIFY, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 988 | acpi_battery_notify, battery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | if (ACPI_FAILURE(status)) { |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 990 | ACPI_EXCEPTION((AE_INFO, status, "Installing notify handler")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | result = -ENODEV; |
| 992 | goto end; |
| 993 | } |
| 994 | |
| 995 | printk(KERN_INFO PREFIX "%s Slot [%s] (battery %s)\n", |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 996 | ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device), |
| 997 | device->status.battery_present ? "present" : "absent"); |
| 998 | |
| 999 | end: |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 1000 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | if (result) { |
| 1002 | acpi_battery_remove_fs(device); |
| 1003 | kfree(battery); |
| 1004 | } |
| 1005 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 1006 | acpi_battery_mutex_unlock(battery); |
| 1007 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 1008 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | } |
| 1010 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1011 | static int acpi_battery_remove(struct acpi_device *device, int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1013 | acpi_status status = 0; |
| 1014 | struct acpi_battery *battery = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | if (!device || !acpi_driver_data(device)) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 1017 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 1019 | battery = acpi_driver_data(device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 1021 | acpi_battery_mutex_lock(battery); |
| 1022 | |
Patrick Mochel | 3b073ec | 2006-05-19 16:54:41 -0400 | [diff] [blame] | 1023 | status = acpi_remove_notify_handler(device->handle, |
Vladimir Lebedev | 9fdae72 | 2006-06-27 04:49:00 -0400 | [diff] [blame] | 1024 | ACPI_ALL_NOTIFY, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1025 | acpi_battery_notify); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1026 | |
| 1027 | acpi_battery_remove_fs(device); |
| 1028 | |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 1029 | if (battery->bif_data.pointer) |
| 1030 | kfree(battery->bif_data.pointer); |
| 1031 | |
| 1032 | if (battery->bst_data.pointer) |
| 1033 | kfree(battery->bst_data.pointer); |
| 1034 | |
| 1035 | acpi_battery_mutex_unlock(battery); |
| 1036 | |
| 1037 | mutex_destroy(&battery->mutex); |
| 1038 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | kfree(battery); |
| 1040 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 1041 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | } |
| 1043 | |
Jiri Kosina | 34c4415 | 2006-10-10 14:20:41 -0700 | [diff] [blame] | 1044 | /* this is needed to learn about changes made in suspended state */ |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 1045 | static int acpi_battery_resume(struct acpi_device *device) |
Jiri Kosina | 34c4415 | 2006-10-10 14:20:41 -0700 | [diff] [blame] | 1046 | { |
| 1047 | struct acpi_battery *battery; |
| 1048 | |
| 1049 | if (!device) |
| 1050 | return -EINVAL; |
| 1051 | |
| 1052 | battery = device->driver_data; |
Vladimir Lebedev | b6ce408 | 2007-02-20 15:48:06 +0300 | [diff] [blame] | 1053 | |
| 1054 | acpi_battery_init_update_flag(battery) = 1; |
| 1055 | |
| 1056 | return 0; |
Jiri Kosina | 34c4415 | 2006-10-10 14:20:41 -0700 | [diff] [blame] | 1057 | } |
| 1058 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1059 | static int __init acpi_battery_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1060 | { |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1061 | int result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | |
Pavel Machek | 4d8316d | 2006-08-14 22:37:22 -0700 | [diff] [blame] | 1063 | if (acpi_disabled) |
| 1064 | return -ENODEV; |
| 1065 | |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1066 | acpi_battery_dir = acpi_lock_battery_dir(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | if (!acpi_battery_dir) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 1068 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | |
| 1070 | result = acpi_bus_register_driver(&acpi_battery_driver); |
| 1071 | if (result < 0) { |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1072 | acpi_unlock_battery_dir(acpi_battery_dir); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 1073 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | } |
| 1075 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 1076 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | } |
| 1078 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1079 | static void __exit acpi_battery_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1081 | acpi_bus_unregister_driver(&acpi_battery_driver); |
| 1082 | |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 1083 | acpi_unlock_battery_dir(acpi_battery_dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 1085 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1086 | } |
| 1087 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1088 | module_init(acpi_battery_init); |
| 1089 | module_exit(acpi_battery_exit); |