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