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" |
| 45 | #define ACPI_BATTERY_DRIVER_NAME "ACPI Battery Driver" |
| 46 | #define ACPI_BATTERY_DEVICE_NAME "Battery" |
| 47 | #define ACPI_BATTERY_FILE_INFO "info" |
| 48 | #define ACPI_BATTERY_FILE_STATUS "state" |
| 49 | #define ACPI_BATTERY_FILE_ALARM "alarm" |
| 50 | #define ACPI_BATTERY_NOTIFY_STATUS 0x80 |
| 51 | #define ACPI_BATTERY_NOTIFY_INFO 0x81 |
| 52 | #define ACPI_BATTERY_UNITS_WATTS "mW" |
| 53 | #define ACPI_BATTERY_UNITS_AMPS "mA" |
| 54 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | #define _COMPONENT ACPI_BATTERY_COMPONENT |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 56 | ACPI_MODULE_NAME("acpi_battery") |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 58 | MODULE_AUTHOR("Paul Diefenbaugh"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | MODULE_DESCRIPTION(ACPI_BATTERY_DRIVER_NAME); |
| 60 | MODULE_LICENSE("GPL"); |
| 61 | |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 62 | extern struct proc_dir_entry *acpi_lock_battery_dir(void); |
| 63 | extern void *acpi_unlock_battery_dir(struct proc_dir_entry *acpi_battery_dir); |
| 64 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 65 | static int acpi_battery_add(struct acpi_device *device); |
| 66 | static int acpi_battery_remove(struct acpi_device *device, int type); |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 67 | static int acpi_battery_resume(struct acpi_device *device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
| 69 | static struct acpi_driver acpi_battery_driver = { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 70 | .name = ACPI_BATTERY_DRIVER_NAME, |
| 71 | .class = ACPI_BATTERY_CLASS, |
| 72 | .ids = ACPI_BATTERY_HID, |
| 73 | .ops = { |
| 74 | .add = acpi_battery_add, |
Jiri Kosina | 34c4415 | 2006-10-10 14:20:41 -0700 | [diff] [blame] | 75 | .resume = acpi_battery_resume, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 76 | .remove = acpi_battery_remove, |
| 77 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | struct acpi_battery_status { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 81 | acpi_integer state; |
| 82 | acpi_integer present_rate; |
| 83 | acpi_integer remaining_capacity; |
| 84 | acpi_integer present_voltage; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | struct acpi_battery_info { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 88 | acpi_integer power_unit; |
| 89 | acpi_integer design_capacity; |
| 90 | acpi_integer last_full_capacity; |
| 91 | acpi_integer battery_technology; |
| 92 | acpi_integer design_voltage; |
| 93 | acpi_integer design_capacity_warning; |
| 94 | acpi_integer design_capacity_low; |
| 95 | acpi_integer battery_capacity_granularity_1; |
| 96 | acpi_integer battery_capacity_granularity_2; |
| 97 | acpi_string model_number; |
| 98 | acpi_string serial_number; |
| 99 | acpi_string battery_type; |
| 100 | acpi_string oem_info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | struct acpi_battery_flags { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 104 | u8 present:1; /* Bay occupied? */ |
| 105 | u8 power_unit:1; /* 0=watts, 1=apms */ |
| 106 | u8 alarm:1; /* _BTP present? */ |
| 107 | u8 reserved:5; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | struct acpi_battery_trips { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 111 | unsigned long warning; |
| 112 | unsigned long low; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | struct acpi_battery { |
Patrick Mochel | 145def8 | 2006-05-19 16:54:39 -0400 | [diff] [blame] | 116 | struct acpi_device * device; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | struct acpi_battery_flags flags; |
| 118 | struct acpi_battery_trips trips; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 119 | unsigned long alarm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | struct acpi_battery_info *info; |
| 121 | }; |
| 122 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | /* -------------------------------------------------------------------------- |
| 124 | Battery Management |
| 125 | -------------------------------------------------------------------------- */ |
| 126 | |
| 127 | static int |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 128 | acpi_battery_get_info(struct acpi_battery *battery, |
| 129 | struct acpi_battery_info **bif) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 131 | int result = 0; |
| 132 | acpi_status status = 0; |
| 133 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; |
| 134 | struct acpi_buffer format = { sizeof(ACPI_BATTERY_FORMAT_BIF), |
| 135 | ACPI_BATTERY_FORMAT_BIF |
| 136 | }; |
| 137 | struct acpi_buffer data = { 0, NULL }; |
| 138 | union acpi_object *package = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | |
| 141 | if (!battery || !bif) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 142 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | |
| 144 | /* Evalute _BIF */ |
| 145 | |
Patrick Mochel | 3b073ec | 2006-05-19 16:54:41 -0400 | [diff] [blame] | 146 | status = acpi_evaluate_object(battery->device->handle, "_BIF", NULL, &buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | if (ACPI_FAILURE(status)) { |
Thomas Renninger | a6fc672 | 2006-06-26 23:58:43 -0400 | [diff] [blame] | 148 | ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BIF")); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 149 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 152 | package = buffer.pointer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | |
| 154 | /* Extract Package Data */ |
| 155 | |
| 156 | status = acpi_extract_package(package, &format, &data); |
| 157 | if (status != AE_BUFFER_OVERFLOW) { |
Thomas Renninger | a6fc672 | 2006-06-26 23:58:43 -0400 | [diff] [blame] | 158 | ACPI_EXCEPTION((AE_INFO, status, "Extracting _BIF")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | result = -ENODEV; |
| 160 | goto end; |
| 161 | } |
| 162 | |
Burman Yan | 36bcbec | 2006-12-19 12:56:11 -0800 | [diff] [blame] | 163 | data.pointer = kzalloc(data.length, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | if (!data.pointer) { |
| 165 | result = -ENOMEM; |
| 166 | goto end; |
| 167 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | |
| 169 | status = acpi_extract_package(package, &format, &data); |
| 170 | if (ACPI_FAILURE(status)) { |
Thomas Renninger | a6fc672 | 2006-06-26 23:58:43 -0400 | [diff] [blame] | 171 | ACPI_EXCEPTION((AE_INFO, status, "Extracting _BIF")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | kfree(data.pointer); |
| 173 | result = -ENODEV; |
| 174 | goto end; |
| 175 | } |
| 176 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 177 | end: |
Len Brown | 02438d8 | 2006-06-30 03:19:10 -0400 | [diff] [blame] | 178 | kfree(buffer.pointer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | |
| 180 | if (!result) |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 181 | (*bif) = data.pointer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 183 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | static int |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 187 | acpi_battery_get_status(struct acpi_battery *battery, |
| 188 | struct acpi_battery_status **bst) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 190 | int result = 0; |
| 191 | acpi_status status = 0; |
| 192 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; |
| 193 | struct acpi_buffer format = { sizeof(ACPI_BATTERY_FORMAT_BST), |
| 194 | ACPI_BATTERY_FORMAT_BST |
| 195 | }; |
| 196 | struct acpi_buffer data = { 0, NULL }; |
| 197 | union acpi_object *package = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | |
| 200 | if (!battery || !bst) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 201 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | |
| 203 | /* Evalute _BST */ |
| 204 | |
Patrick Mochel | 3b073ec | 2006-05-19 16:54:41 -0400 | [diff] [blame] | 205 | status = acpi_evaluate_object(battery->device->handle, "_BST", NULL, &buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | if (ACPI_FAILURE(status)) { |
Thomas Renninger | a6fc672 | 2006-06-26 23:58:43 -0400 | [diff] [blame] | 207 | ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BST")); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 208 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 211 | package = buffer.pointer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | |
| 213 | /* Extract Package Data */ |
| 214 | |
| 215 | status = acpi_extract_package(package, &format, &data); |
| 216 | if (status != AE_BUFFER_OVERFLOW) { |
Thomas Renninger | a6fc672 | 2006-06-26 23:58:43 -0400 | [diff] [blame] | 217 | ACPI_EXCEPTION((AE_INFO, status, "Extracting _BST")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | result = -ENODEV; |
| 219 | goto end; |
| 220 | } |
| 221 | |
Burman Yan | 36bcbec | 2006-12-19 12:56:11 -0800 | [diff] [blame] | 222 | data.pointer = kzalloc(data.length, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | if (!data.pointer) { |
| 224 | result = -ENOMEM; |
| 225 | goto end; |
| 226 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | |
| 228 | status = acpi_extract_package(package, &format, &data); |
| 229 | if (ACPI_FAILURE(status)) { |
Thomas Renninger | a6fc672 | 2006-06-26 23:58:43 -0400 | [diff] [blame] | 230 | ACPI_EXCEPTION((AE_INFO, status, "Extracting _BST")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | kfree(data.pointer); |
| 232 | result = -ENODEV; |
| 233 | goto end; |
| 234 | } |
| 235 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 236 | end: |
Len Brown | 02438d8 | 2006-06-30 03:19:10 -0400 | [diff] [blame] | 237 | kfree(buffer.pointer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | |
| 239 | if (!result) |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 240 | (*bst) = data.pointer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 242 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | } |
| 244 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | static int |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 246 | acpi_battery_set_alarm(struct acpi_battery *battery, unsigned long alarm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 248 | acpi_status status = 0; |
| 249 | union acpi_object arg0 = { ACPI_TYPE_INTEGER }; |
| 250 | struct acpi_object_list arg_list = { 1, &arg0 }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | |
| 253 | if (!battery) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 254 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | |
| 256 | if (!battery->flags.alarm) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 257 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | |
| 259 | arg0.integer.value = alarm; |
| 260 | |
Patrick Mochel | 3b073ec | 2006-05-19 16:54:41 -0400 | [diff] [blame] | 261 | status = acpi_evaluate_object(battery->device->handle, "_BTP", &arg_list, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | if (ACPI_FAILURE(status)) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 263 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | |
| 265 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", (u32) alarm)); |
| 266 | |
| 267 | battery->alarm = alarm; |
| 268 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 269 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | } |
| 271 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 272 | static int acpi_battery_check(struct acpi_battery *battery) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 274 | int result = 0; |
| 275 | acpi_status status = AE_OK; |
| 276 | acpi_handle handle = NULL; |
| 277 | struct acpi_device *device = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | struct acpi_battery_info *bif = NULL; |
| 279 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 280 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | if (!battery) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 282 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | |
Patrick Mochel | 145def8 | 2006-05-19 16:54:39 -0400 | [diff] [blame] | 284 | device = battery->device; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | |
| 286 | result = acpi_bus_get_status(device); |
| 287 | if (result) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 288 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | |
| 290 | /* Insertion? */ |
| 291 | |
| 292 | if (!battery->flags.present && device->status.battery_present) { |
| 293 | |
| 294 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Battery inserted\n")); |
| 295 | |
| 296 | /* Evalute _BIF to get certain static information */ |
| 297 | |
| 298 | result = acpi_battery_get_info(battery, &bif); |
| 299 | if (result) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 300 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | |
| 302 | battery->flags.power_unit = bif->power_unit; |
| 303 | battery->trips.warning = bif->design_capacity_warning; |
| 304 | battery->trips.low = bif->design_capacity_low; |
| 305 | kfree(bif); |
| 306 | |
| 307 | /* See if alarms are supported, and if so, set default */ |
| 308 | |
Patrick Mochel | 3b073ec | 2006-05-19 16:54:41 -0400 | [diff] [blame] | 309 | status = acpi_get_handle(battery->device->handle, "_BTP", &handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | if (ACPI_SUCCESS(status)) { |
| 311 | battery->flags.alarm = 1; |
| 312 | acpi_battery_set_alarm(battery, battery->trips.warning); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | /* Removal? */ |
| 317 | |
| 318 | else if (battery->flags.present && !device->status.battery_present) { |
| 319 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Battery removed\n")); |
| 320 | } |
| 321 | |
| 322 | battery->flags.present = device->status.battery_present; |
| 323 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 324 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | } |
| 326 | |
Vladimir Lebedev | 4bd35cd | 2007-02-10 01:43:48 -0500 | [diff] [blame^] | 327 | static void acpi_battery_check_present(struct acpi_battery *battery) |
| 328 | { |
| 329 | if (!battery->flags.present) { |
| 330 | acpi_battery_check(battery); |
| 331 | } |
| 332 | } |
| 333 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | /* -------------------------------------------------------------------------- |
| 335 | FS Interface (/proc) |
| 336 | -------------------------------------------------------------------------- */ |
| 337 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 338 | static struct proc_dir_entry *acpi_battery_dir; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | static int acpi_battery_read_info(struct seq_file *seq, void *offset) |
| 340 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 341 | int result = 0; |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 342 | struct acpi_battery *battery = seq->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | struct acpi_battery_info *bif = NULL; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 344 | char *units = "?"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | |
| 347 | if (!battery) |
| 348 | goto end; |
| 349 | |
Vladimir Lebedev | 4bd35cd | 2007-02-10 01:43:48 -0500 | [diff] [blame^] | 350 | acpi_battery_check_present(battery); |
| 351 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | if (battery->flags.present) |
| 353 | seq_printf(seq, "present: yes\n"); |
| 354 | else { |
| 355 | seq_printf(seq, "present: no\n"); |
| 356 | goto end; |
| 357 | } |
| 358 | |
| 359 | /* Battery Info (_BIF) */ |
| 360 | |
| 361 | result = acpi_battery_get_info(battery, &bif); |
| 362 | if (result || !bif) { |
| 363 | seq_printf(seq, "ERROR: Unable to read battery information\n"); |
| 364 | goto end; |
| 365 | } |
| 366 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 367 | units = |
| 368 | bif-> |
| 369 | power_unit ? ACPI_BATTERY_UNITS_AMPS : ACPI_BATTERY_UNITS_WATTS; |
| 370 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | if (bif->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN) |
| 372 | seq_printf(seq, "design capacity: unknown\n"); |
| 373 | else |
| 374 | seq_printf(seq, "design capacity: %d %sh\n", |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 375 | (u32) bif->design_capacity, units); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | |
| 377 | if (bif->last_full_capacity == ACPI_BATTERY_VALUE_UNKNOWN) |
| 378 | seq_printf(seq, "last full capacity: unknown\n"); |
| 379 | else |
| 380 | seq_printf(seq, "last full capacity: %d %sh\n", |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 381 | (u32) bif->last_full_capacity, units); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | |
| 383 | switch ((u32) bif->battery_technology) { |
| 384 | case 0: |
| 385 | seq_printf(seq, "battery technology: non-rechargeable\n"); |
| 386 | break; |
| 387 | case 1: |
| 388 | seq_printf(seq, "battery technology: rechargeable\n"); |
| 389 | break; |
| 390 | default: |
| 391 | seq_printf(seq, "battery technology: unknown\n"); |
| 392 | break; |
| 393 | } |
| 394 | |
| 395 | if (bif->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN) |
| 396 | seq_printf(seq, "design voltage: unknown\n"); |
| 397 | else |
| 398 | seq_printf(seq, "design voltage: %d mV\n", |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 399 | (u32) bif->design_voltage); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 401 | seq_printf(seq, "design capacity warning: %d %sh\n", |
| 402 | (u32) bif->design_capacity_warning, units); |
| 403 | seq_printf(seq, "design capacity low: %d %sh\n", |
| 404 | (u32) bif->design_capacity_low, units); |
| 405 | seq_printf(seq, "capacity granularity 1: %d %sh\n", |
| 406 | (u32) bif->battery_capacity_granularity_1, units); |
| 407 | seq_printf(seq, "capacity granularity 2: %d %sh\n", |
| 408 | (u32) bif->battery_capacity_granularity_2, units); |
| 409 | seq_printf(seq, "model number: %s\n", bif->model_number); |
| 410 | seq_printf(seq, "serial number: %s\n", bif->serial_number); |
| 411 | seq_printf(seq, "battery type: %s\n", bif->battery_type); |
| 412 | seq_printf(seq, "OEM info: %s\n", bif->oem_info); |
| 413 | |
| 414 | end: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | kfree(bif); |
| 416 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 417 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | static int acpi_battery_info_open_fs(struct inode *inode, struct file *file) |
| 421 | { |
| 422 | return single_open(file, acpi_battery_read_info, PDE(inode)->data); |
| 423 | } |
| 424 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 425 | static int acpi_battery_read_state(struct seq_file *seq, void *offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 427 | int result = 0; |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 428 | struct acpi_battery *battery = seq->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | struct acpi_battery_status *bst = NULL; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 430 | char *units = "?"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | |
| 433 | if (!battery) |
| 434 | goto end; |
| 435 | |
Vladimir Lebedev | 4bd35cd | 2007-02-10 01:43:48 -0500 | [diff] [blame^] | 436 | acpi_battery_check_present(battery); |
| 437 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | if (battery->flags.present) |
| 439 | seq_printf(seq, "present: yes\n"); |
| 440 | else { |
| 441 | seq_printf(seq, "present: no\n"); |
| 442 | goto end; |
| 443 | } |
| 444 | |
| 445 | /* Battery Units */ |
| 446 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 447 | units = |
| 448 | battery->flags. |
| 449 | power_unit ? ACPI_BATTERY_UNITS_AMPS : ACPI_BATTERY_UNITS_WATTS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | |
| 451 | /* Battery Status (_BST) */ |
| 452 | |
| 453 | result = acpi_battery_get_status(battery, &bst); |
| 454 | if (result || !bst) { |
| 455 | seq_printf(seq, "ERROR: Unable to read battery status\n"); |
| 456 | goto end; |
| 457 | } |
| 458 | |
| 459 | if (!(bst->state & 0x04)) |
| 460 | seq_printf(seq, "capacity state: ok\n"); |
| 461 | else |
| 462 | seq_printf(seq, "capacity state: critical\n"); |
| 463 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 464 | if ((bst->state & 0x01) && (bst->state & 0x02)) { |
| 465 | seq_printf(seq, |
| 466 | "charging state: charging/discharging\n"); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 467 | } else if (bst->state & 0x01) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | seq_printf(seq, "charging state: discharging\n"); |
| 469 | else if (bst->state & 0x02) |
| 470 | seq_printf(seq, "charging state: charging\n"); |
| 471 | else { |
| 472 | seq_printf(seq, "charging state: charged\n"); |
| 473 | } |
| 474 | |
| 475 | if (bst->present_rate == ACPI_BATTERY_VALUE_UNKNOWN) |
| 476 | seq_printf(seq, "present rate: unknown\n"); |
| 477 | else |
| 478 | seq_printf(seq, "present rate: %d %s\n", |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 479 | (u32) bst->present_rate, units); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | |
| 481 | if (bst->remaining_capacity == ACPI_BATTERY_VALUE_UNKNOWN) |
| 482 | seq_printf(seq, "remaining capacity: unknown\n"); |
| 483 | else |
| 484 | seq_printf(seq, "remaining capacity: %d %sh\n", |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 485 | (u32) bst->remaining_capacity, units); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | |
| 487 | if (bst->present_voltage == ACPI_BATTERY_VALUE_UNKNOWN) |
| 488 | seq_printf(seq, "present voltage: unknown\n"); |
| 489 | else |
| 490 | seq_printf(seq, "present voltage: %d mV\n", |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 491 | (u32) bst->present_voltage); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 493 | end: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | kfree(bst); |
| 495 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 496 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | static int acpi_battery_state_open_fs(struct inode *inode, struct file *file) |
| 500 | { |
| 501 | return single_open(file, acpi_battery_read_state, PDE(inode)->data); |
| 502 | } |
| 503 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 504 | static int acpi_battery_read_alarm(struct seq_file *seq, void *offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | { |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 506 | struct acpi_battery *battery = seq->private; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 507 | char *units = "?"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | |
| 510 | if (!battery) |
| 511 | goto end; |
| 512 | |
Vladimir Lebedev | 4bd35cd | 2007-02-10 01:43:48 -0500 | [diff] [blame^] | 513 | acpi_battery_check_present(battery); |
| 514 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | if (!battery->flags.present) { |
| 516 | seq_printf(seq, "present: no\n"); |
| 517 | goto end; |
| 518 | } |
| 519 | |
| 520 | /* Battery Units */ |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 521 | |
| 522 | units = |
| 523 | battery->flags. |
| 524 | power_unit ? ACPI_BATTERY_UNITS_AMPS : ACPI_BATTERY_UNITS_WATTS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | |
| 526 | /* Battery Alarm */ |
| 527 | |
| 528 | seq_printf(seq, "alarm: "); |
| 529 | if (!battery->alarm) |
| 530 | seq_printf(seq, "unsupported\n"); |
| 531 | else |
| 532 | seq_printf(seq, "%d %sh\n", (u32) battery->alarm, units); |
| 533 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 534 | end: |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 535 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | } |
| 537 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | static ssize_t |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 539 | acpi_battery_write_alarm(struct file *file, |
| 540 | const char __user * buffer, |
| 541 | size_t count, loff_t * ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 543 | int result = 0; |
| 544 | char alarm_string[12] = { '\0' }; |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 545 | struct seq_file *m = file->private_data; |
| 546 | struct acpi_battery *battery = m->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | |
| 549 | if (!battery || (count > sizeof(alarm_string) - 1)) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 550 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | |
Vladimir Lebedev | 4bd35cd | 2007-02-10 01:43:48 -0500 | [diff] [blame^] | 552 | acpi_battery_check_present(battery); |
| 553 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | if (!battery->flags.present) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 555 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | |
| 557 | if (copy_from_user(alarm_string, buffer, count)) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 558 | return -EFAULT; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 559 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | alarm_string[count] = '\0'; |
| 561 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 562 | result = acpi_battery_set_alarm(battery, |
| 563 | simple_strtoul(alarm_string, NULL, 0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | if (result) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 565 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 567 | return count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | static int acpi_battery_alarm_open_fs(struct inode *inode, struct file *file) |
| 571 | { |
| 572 | return single_open(file, acpi_battery_read_alarm, PDE(inode)->data); |
| 573 | } |
| 574 | |
Arjan van de Ven | d750803 | 2006-07-04 13:06:00 -0400 | [diff] [blame] | 575 | static const struct file_operations acpi_battery_info_ops = { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 576 | .open = acpi_battery_info_open_fs, |
| 577 | .read = seq_read, |
| 578 | .llseek = seq_lseek, |
| 579 | .release = single_release, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | .owner = THIS_MODULE, |
| 581 | }; |
| 582 | |
Arjan van de Ven | d750803 | 2006-07-04 13:06:00 -0400 | [diff] [blame] | 583 | static const struct file_operations acpi_battery_state_ops = { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 584 | .open = acpi_battery_state_open_fs, |
| 585 | .read = seq_read, |
| 586 | .llseek = seq_lseek, |
| 587 | .release = single_release, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | .owner = THIS_MODULE, |
| 589 | }; |
| 590 | |
Arjan van de Ven | d750803 | 2006-07-04 13:06:00 -0400 | [diff] [blame] | 591 | static const struct file_operations acpi_battery_alarm_ops = { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 592 | .open = acpi_battery_alarm_open_fs, |
| 593 | .read = seq_read, |
| 594 | .write = acpi_battery_write_alarm, |
| 595 | .llseek = seq_lseek, |
| 596 | .release = single_release, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | .owner = THIS_MODULE, |
| 598 | }; |
| 599 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 600 | static int acpi_battery_add_fs(struct acpi_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 602 | struct proc_dir_entry *entry = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | |
| 605 | if (!acpi_device_dir(device)) { |
| 606 | acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 607 | acpi_battery_dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | if (!acpi_device_dir(device)) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 609 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | acpi_device_dir(device)->owner = THIS_MODULE; |
| 611 | } |
| 612 | |
| 613 | /* 'info' [R] */ |
| 614 | entry = create_proc_entry(ACPI_BATTERY_FILE_INFO, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 615 | S_IRUGO, acpi_device_dir(device)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | if (!entry) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 617 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | else { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 619 | entry->proc_fops = &acpi_battery_info_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | entry->data = acpi_driver_data(device); |
| 621 | entry->owner = THIS_MODULE; |
| 622 | } |
| 623 | |
| 624 | /* 'status' [R] */ |
| 625 | entry = create_proc_entry(ACPI_BATTERY_FILE_STATUS, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 626 | S_IRUGO, acpi_device_dir(device)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | if (!entry) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 628 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | else { |
| 630 | entry->proc_fops = &acpi_battery_state_ops; |
| 631 | entry->data = acpi_driver_data(device); |
| 632 | entry->owner = THIS_MODULE; |
| 633 | } |
| 634 | |
| 635 | /* 'alarm' [R/W] */ |
| 636 | entry = create_proc_entry(ACPI_BATTERY_FILE_ALARM, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 637 | S_IFREG | S_IRUGO | S_IWUSR, |
| 638 | acpi_device_dir(device)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | if (!entry) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 640 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | else { |
| 642 | entry->proc_fops = &acpi_battery_alarm_ops; |
| 643 | entry->data = acpi_driver_data(device); |
| 644 | entry->owner = THIS_MODULE; |
| 645 | } |
| 646 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 647 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | } |
| 649 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 650 | static int acpi_battery_remove_fs(struct acpi_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | |
| 653 | if (acpi_device_dir(device)) { |
| 654 | remove_proc_entry(ACPI_BATTERY_FILE_ALARM, |
| 655 | acpi_device_dir(device)); |
| 656 | remove_proc_entry(ACPI_BATTERY_FILE_STATUS, |
| 657 | acpi_device_dir(device)); |
| 658 | remove_proc_entry(ACPI_BATTERY_FILE_INFO, |
| 659 | acpi_device_dir(device)); |
| 660 | |
| 661 | remove_proc_entry(acpi_device_bid(device), acpi_battery_dir); |
| 662 | acpi_device_dir(device) = NULL; |
| 663 | } |
| 664 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 665 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | } |
| 667 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | /* -------------------------------------------------------------------------- |
| 669 | Driver Interface |
| 670 | -------------------------------------------------------------------------- */ |
| 671 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 672 | static void acpi_battery_notify(acpi_handle handle, u32 event, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | { |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 674 | struct acpi_battery *battery = data; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 675 | struct acpi_device *device = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | |
| 678 | if (!battery) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 679 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | |
Patrick Mochel | 145def8 | 2006-05-19 16:54:39 -0400 | [diff] [blame] | 681 | device = battery->device; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | |
| 683 | switch (event) { |
| 684 | case ACPI_BATTERY_NOTIFY_STATUS: |
| 685 | case ACPI_BATTERY_NOTIFY_INFO: |
Vladimir Lebedev | 9fdae72 | 2006-06-27 04:49:00 -0400 | [diff] [blame] | 686 | case ACPI_NOTIFY_BUS_CHECK: |
| 687 | case ACPI_NOTIFY_DEVICE_CHECK: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | acpi_battery_check(battery); |
| 689 | acpi_bus_generate_event(device, event, battery->flags.present); |
| 690 | break; |
| 691 | default: |
| 692 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 693 | "Unsupported event [0x%x]\n", event)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | break; |
| 695 | } |
| 696 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 697 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | } |
| 699 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 700 | static int acpi_battery_add(struct acpi_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 702 | int result = 0; |
| 703 | acpi_status status = 0; |
| 704 | struct acpi_battery *battery = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 706 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | if (!device) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 708 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | |
Burman Yan | 36bcbec | 2006-12-19 12:56:11 -0800 | [diff] [blame] | 710 | battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | if (!battery) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 712 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | |
Patrick Mochel | 145def8 | 2006-05-19 16:54:39 -0400 | [diff] [blame] | 714 | battery->device = device; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME); |
| 716 | strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS); |
| 717 | acpi_driver_data(device) = battery; |
| 718 | |
| 719 | result = acpi_battery_check(battery); |
| 720 | if (result) |
| 721 | goto end; |
| 722 | |
| 723 | result = acpi_battery_add_fs(device); |
| 724 | if (result) |
| 725 | goto end; |
| 726 | |
Patrick Mochel | 3b073ec | 2006-05-19 16:54:41 -0400 | [diff] [blame] | 727 | status = acpi_install_notify_handler(device->handle, |
Vladimir Lebedev | 9fdae72 | 2006-06-27 04:49:00 -0400 | [diff] [blame] | 728 | ACPI_ALL_NOTIFY, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 729 | acpi_battery_notify, battery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | if (ACPI_FAILURE(status)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | result = -ENODEV; |
| 732 | goto end; |
| 733 | } |
| 734 | |
| 735 | printk(KERN_INFO PREFIX "%s Slot [%s] (battery %s)\n", |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 736 | ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device), |
| 737 | device->status.battery_present ? "present" : "absent"); |
| 738 | |
| 739 | end: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | if (result) { |
| 741 | acpi_battery_remove_fs(device); |
| 742 | kfree(battery); |
| 743 | } |
| 744 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 745 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | } |
| 747 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 748 | static int acpi_battery_remove(struct acpi_device *device, int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 750 | acpi_status status = 0; |
| 751 | struct acpi_battery *battery = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | |
| 754 | if (!device || !acpi_driver_data(device)) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 755 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 757 | battery = acpi_driver_data(device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | |
Patrick Mochel | 3b073ec | 2006-05-19 16:54:41 -0400 | [diff] [blame] | 759 | status = acpi_remove_notify_handler(device->handle, |
Vladimir Lebedev | 9fdae72 | 2006-06-27 04:49:00 -0400 | [diff] [blame] | 760 | ACPI_ALL_NOTIFY, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 761 | acpi_battery_notify); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | |
| 763 | acpi_battery_remove_fs(device); |
| 764 | |
| 765 | kfree(battery); |
| 766 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 767 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | } |
| 769 | |
Jiri Kosina | 34c4415 | 2006-10-10 14:20:41 -0700 | [diff] [blame] | 770 | /* this is needed to learn about changes made in suspended state */ |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 771 | static int acpi_battery_resume(struct acpi_device *device) |
Jiri Kosina | 34c4415 | 2006-10-10 14:20:41 -0700 | [diff] [blame] | 772 | { |
| 773 | struct acpi_battery *battery; |
| 774 | |
| 775 | if (!device) |
| 776 | return -EINVAL; |
| 777 | |
| 778 | battery = device->driver_data; |
| 779 | return acpi_battery_check(battery); |
| 780 | } |
| 781 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 782 | static int __init acpi_battery_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | { |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 784 | int result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | |
Pavel Machek | 4d8316d | 2006-08-14 22:37:22 -0700 | [diff] [blame] | 786 | if (acpi_disabled) |
| 787 | return -ENODEV; |
| 788 | |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 789 | acpi_battery_dir = acpi_lock_battery_dir(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | if (!acpi_battery_dir) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 791 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | |
| 793 | result = acpi_bus_register_driver(&acpi_battery_driver); |
| 794 | if (result < 0) { |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 795 | acpi_unlock_battery_dir(acpi_battery_dir); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 796 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | } |
| 798 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 799 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | } |
| 801 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 802 | static void __exit acpi_battery_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | |
| 805 | acpi_bus_unregister_driver(&acpi_battery_driver); |
| 806 | |
Rich Townsend | 3f86b83 | 2006-07-01 11:36:54 -0400 | [diff] [blame] | 807 | acpi_unlock_battery_dir(acpi_battery_dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 809 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | } |
| 811 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | module_init(acpi_battery_init); |
| 813 | module_exit(acpi_battery_exit); |