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