]>
Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
aa650bbd | 2 | * battery.c - ACPI Battery Driver (Revision: 2.0) |
1da177e4 | 3 | * |
aa650bbd AS |
4 | * Copyright (C) 2007 Alexey Starikovskiy <astarikovskiy@suse.de> |
5 | * Copyright (C) 2004-2007 Vladimir Lebedev <vladimir.p.lebedev@intel.com> | |
1da177e4 LT |
6 | * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com> |
7 | * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com> | |
8 | * | |
9 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
10 | * | |
11 | * This program is free software; you can redistribute it and/or modify | |
12 | * it under the terms of the GNU General Public License as published by | |
13 | * the Free Software Foundation; either version 2 of the License, or (at | |
14 | * your option) any later version. | |
15 | * | |
16 | * This program is distributed in the hope that it will be useful, but | |
17 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
19 | * General Public License for more details. | |
20 | * | |
21 | * You should have received a copy of the GNU General Public License along | |
22 | * with this program; if not, write to the Free Software Foundation, Inc., | |
23 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. | |
24 | * | |
25 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
26 | */ | |
27 | ||
28 | #include <linux/kernel.h> | |
29 | #include <linux/module.h> | |
30 | #include <linux/init.h> | |
31 | #include <linux/types.h> | |
f1d4661a | 32 | #include <linux/jiffies.h> |
0f66af53 | 33 | #include <linux/async.h> |
bc76f90b | 34 | #include <linux/dmi.h> |
5a0e3ad6 | 35 | #include <linux/slab.h> |
25be5821 | 36 | #include <linux/suspend.h> |
d7380965 | 37 | |
fdcedbba | 38 | #ifdef CONFIG_ACPI_PROCFS_POWER |
1da177e4 LT |
39 | #include <linux/proc_fs.h> |
40 | #include <linux/seq_file.h> | |
41 | #include <asm/uaccess.h> | |
d7380965 | 42 | #endif |
1da177e4 LT |
43 | |
44 | #include <acpi/acpi_bus.h> | |
45 | #include <acpi/acpi_drivers.h> | |
d7380965 AS |
46 | #include <linux/power_supply.h> |
47 | ||
a192a958 LB |
48 | #define PREFIX "ACPI: " |
49 | ||
1da177e4 LT |
50 | #define ACPI_BATTERY_VALUE_UNKNOWN 0xFFFFFFFF |
51 | ||
1da177e4 | 52 | #define ACPI_BATTERY_CLASS "battery" |
1da177e4 | 53 | #define ACPI_BATTERY_DEVICE_NAME "Battery" |
1da177e4 LT |
54 | #define ACPI_BATTERY_NOTIFY_STATUS 0x80 |
55 | #define ACPI_BATTERY_NOTIFY_INFO 0x81 | |
c67fcd67 | 56 | #define ACPI_BATTERY_NOTIFY_THRESHOLD 0x82 |
1da177e4 | 57 | |
ae6f6187 LT |
58 | /* Battery power unit: 0 means mW, 1 means mA */ |
59 | #define ACPI_BATTERY_POWER_UNIT_MA 1 | |
60 | ||
1da177e4 | 61 | #define _COMPONENT ACPI_BATTERY_COMPONENT |
b6ce4083 | 62 | |
f52fd66d | 63 | ACPI_MODULE_NAME("battery"); |
1da177e4 | 64 | |
f52fd66d | 65 | MODULE_AUTHOR("Paul Diefenbaugh"); |
aa650bbd | 66 | MODULE_AUTHOR("Alexey Starikovskiy <astarikovskiy@suse.de>"); |
7cda93e0 | 67 | MODULE_DESCRIPTION("ACPI Battery Driver"); |
1da177e4 LT |
68 | MODULE_LICENSE("GPL"); |
69 | ||
f1d4661a AS |
70 | static unsigned int cache_time = 1000; |
71 | module_param(cache_time, uint, 0644); | |
72 | MODULE_PARM_DESC(cache_time, "cache time in milliseconds"); | |
b6ce4083 | 73 | |
fdcedbba | 74 | #ifdef CONFIG_ACPI_PROCFS_POWER |
3f86b832 RT |
75 | extern struct proc_dir_entry *acpi_lock_battery_dir(void); |
76 | extern void *acpi_unlock_battery_dir(struct proc_dir_entry *acpi_battery_dir); | |
77 | ||
d7380965 AS |
78 | enum acpi_battery_files { |
79 | info_tag = 0, | |
80 | state_tag, | |
81 | alarm_tag, | |
82 | ACPI_BATTERY_NUMFILES, | |
83 | }; | |
84 | ||
85 | #endif | |
86 | ||
1ba90e3a TR |
87 | static const struct acpi_device_id battery_device_ids[] = { |
88 | {"PNP0C0A", 0}, | |
89 | {"", 0}, | |
90 | }; | |
1ba90e3a | 91 | |
aa650bbd | 92 | MODULE_DEVICE_TABLE(acpi, battery_device_ids); |
1da177e4 | 93 | |
7b3bcc4a AS |
94 | enum { |
95 | ACPI_BATTERY_ALARM_PRESENT, | |
c67fcd67 AS |
96 | ACPI_BATTERY_XINFO_PRESENT, |
97 | /* For buggy DSDTs that report negative 16-bit values for either | |
98 | * charging or discharging current and/or report 0 as 65536 | |
99 | * due to bad math. | |
100 | */ | |
7b3bcc4a | 101 | ACPI_BATTERY_QUIRK_SIGNED16_CURRENT, |
557d5868 | 102 | ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, |
7b3bcc4a | 103 | }; |
78490d82 | 104 | |
1da177e4 | 105 | struct acpi_battery { |
038fdea2 | 106 | struct mutex lock; |
d7380965 | 107 | struct power_supply bat; |
f1d4661a | 108 | struct acpi_device *device; |
25be5821 | 109 | struct notifier_block pm_nb; |
f1d4661a | 110 | unsigned long update_time; |
7faa144a | 111 | int rate_now; |
d7380965 AS |
112 | int capacity_now; |
113 | int voltage_now; | |
038fdea2 | 114 | int design_capacity; |
d7380965 | 115 | int full_charge_capacity; |
038fdea2 AS |
116 | int technology; |
117 | int design_voltage; | |
118 | int design_capacity_warning; | |
119 | int design_capacity_low; | |
c67fcd67 AS |
120 | int cycle_count; |
121 | int measurement_accuracy; | |
122 | int max_sampling_time; | |
123 | int min_sampling_time; | |
124 | int max_averaging_interval; | |
125 | int min_averaging_interval; | |
038fdea2 AS |
126 | int capacity_granularity_1; |
127 | int capacity_granularity_2; | |
f1d4661a | 128 | int alarm; |
038fdea2 AS |
129 | char model_number[32]; |
130 | char serial_number[32]; | |
131 | char type[32]; | |
132 | char oem_info[32]; | |
f1d4661a AS |
133 | int state; |
134 | int power_unit; | |
7b3bcc4a | 135 | unsigned long flags; |
1da177e4 LT |
136 | }; |
137 | ||
d7380965 AS |
138 | #define to_acpi_battery(x) container_of(x, struct acpi_battery, bat); |
139 | ||
78490d82 | 140 | inline int acpi_battery_present(struct acpi_battery *battery) |
b6ce4083 | 141 | { |
78490d82 AS |
142 | return battery->device->status.battery_present; |
143 | } | |
038fdea2 | 144 | |
d7380965 AS |
145 | static int acpi_battery_technology(struct acpi_battery *battery) |
146 | { | |
147 | if (!strcasecmp("NiCd", battery->type)) | |
148 | return POWER_SUPPLY_TECHNOLOGY_NiCd; | |
149 | if (!strcasecmp("NiMH", battery->type)) | |
150 | return POWER_SUPPLY_TECHNOLOGY_NiMH; | |
151 | if (!strcasecmp("LION", battery->type)) | |
152 | return POWER_SUPPLY_TECHNOLOGY_LION; | |
ad40e68b | 153 | if (!strncasecmp("LI-ION", battery->type, 6)) |
0bde7eee | 154 | return POWER_SUPPLY_TECHNOLOGY_LION; |
d7380965 AS |
155 | if (!strcasecmp("LiP", battery->type)) |
156 | return POWER_SUPPLY_TECHNOLOGY_LIPO; | |
157 | return POWER_SUPPLY_TECHNOLOGY_UNKNOWN; | |
158 | } | |
159 | ||
9104476e | 160 | static int acpi_battery_get_state(struct acpi_battery *battery); |
b19073a0 | 161 | |
56f382a0 RH |
162 | static int acpi_battery_is_charged(struct acpi_battery *battery) |
163 | { | |
164 | /* either charging or discharging */ | |
165 | if (battery->state != 0) | |
166 | return 0; | |
167 | ||
168 | /* battery not reporting charge */ | |
169 | if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN || | |
170 | battery->capacity_now == 0) | |
171 | return 0; | |
172 | ||
173 | /* good batteries update full_charge as the batteries degrade */ | |
174 | if (battery->full_charge_capacity == battery->capacity_now) | |
175 | return 1; | |
176 | ||
177 | /* fallback to using design values for broken batteries */ | |
178 | if (battery->design_capacity == battery->capacity_now) | |
179 | return 1; | |
180 | ||
181 | /* we don't do any sort of metric based on percentages */ | |
182 | return 0; | |
183 | } | |
184 | ||
d7380965 AS |
185 | static int acpi_battery_get_property(struct power_supply *psy, |
186 | enum power_supply_property psp, | |
187 | union power_supply_propval *val) | |
188 | { | |
a1b4bd69 | 189 | int ret = 0; |
d7380965 AS |
190 | struct acpi_battery *battery = to_acpi_battery(psy); |
191 | ||
9104476e AS |
192 | if (acpi_battery_present(battery)) { |
193 | /* run battery update only if it is present */ | |
194 | acpi_battery_get_state(battery); | |
195 | } else if (psp != POWER_SUPPLY_PROP_PRESENT) | |
d7380965 AS |
196 | return -ENODEV; |
197 | switch (psp) { | |
198 | case POWER_SUPPLY_PROP_STATUS: | |
199 | if (battery->state & 0x01) | |
200 | val->intval = POWER_SUPPLY_STATUS_DISCHARGING; | |
201 | else if (battery->state & 0x02) | |
202 | val->intval = POWER_SUPPLY_STATUS_CHARGING; | |
56f382a0 | 203 | else if (acpi_battery_is_charged(battery)) |
d7380965 | 204 | val->intval = POWER_SUPPLY_STATUS_FULL; |
4c41d3ad RD |
205 | else |
206 | val->intval = POWER_SUPPLY_STATUS_UNKNOWN; | |
d7380965 AS |
207 | break; |
208 | case POWER_SUPPLY_PROP_PRESENT: | |
209 | val->intval = acpi_battery_present(battery); | |
210 | break; | |
211 | case POWER_SUPPLY_PROP_TECHNOLOGY: | |
212 | val->intval = acpi_battery_technology(battery); | |
213 | break; | |
c67fcd67 AS |
214 | case POWER_SUPPLY_PROP_CYCLE_COUNT: |
215 | val->intval = battery->cycle_count; | |
216 | break; | |
d7380965 | 217 | case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN: |
a1b4bd69 RW |
218 | if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN) |
219 | ret = -ENODEV; | |
220 | else | |
221 | val->intval = battery->design_voltage * 1000; | |
d7380965 AS |
222 | break; |
223 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: | |
a1b4bd69 RW |
224 | if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN) |
225 | ret = -ENODEV; | |
226 | else | |
227 | val->intval = battery->voltage_now * 1000; | |
d7380965 AS |
228 | break; |
229 | case POWER_SUPPLY_PROP_CURRENT_NOW: | |
7faa144a | 230 | case POWER_SUPPLY_PROP_POWER_NOW: |
a1b4bd69 RW |
231 | if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN) |
232 | ret = -ENODEV; | |
233 | else | |
234 | val->intval = battery->rate_now * 1000; | |
d7380965 AS |
235 | break; |
236 | case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: | |
237 | case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN: | |
a1b4bd69 RW |
238 | if (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN) |
239 | ret = -ENODEV; | |
240 | else | |
241 | val->intval = battery->design_capacity * 1000; | |
d7380965 AS |
242 | break; |
243 | case POWER_SUPPLY_PROP_CHARGE_FULL: | |
244 | case POWER_SUPPLY_PROP_ENERGY_FULL: | |
a1b4bd69 RW |
245 | if (battery->full_charge_capacity == ACPI_BATTERY_VALUE_UNKNOWN) |
246 | ret = -ENODEV; | |
247 | else | |
248 | val->intval = battery->full_charge_capacity * 1000; | |
d7380965 AS |
249 | break; |
250 | case POWER_SUPPLY_PROP_CHARGE_NOW: | |
251 | case POWER_SUPPLY_PROP_ENERGY_NOW: | |
a1b4bd69 RW |
252 | if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN) |
253 | ret = -ENODEV; | |
254 | else | |
255 | val->intval = battery->capacity_now * 1000; | |
d7380965 AS |
256 | break; |
257 | case POWER_SUPPLY_PROP_MODEL_NAME: | |
258 | val->strval = battery->model_number; | |
259 | break; | |
260 | case POWER_SUPPLY_PROP_MANUFACTURER: | |
261 | val->strval = battery->oem_info; | |
262 | break; | |
7c2670bb | 263 | case POWER_SUPPLY_PROP_SERIAL_NUMBER: |
264 | val->strval = battery->serial_number; | |
265 | break; | |
d7380965 | 266 | default: |
a1b4bd69 | 267 | ret = -EINVAL; |
d7380965 | 268 | } |
a1b4bd69 | 269 | return ret; |
d7380965 AS |
270 | } |
271 | ||
272 | static enum power_supply_property charge_battery_props[] = { | |
273 | POWER_SUPPLY_PROP_STATUS, | |
274 | POWER_SUPPLY_PROP_PRESENT, | |
275 | POWER_SUPPLY_PROP_TECHNOLOGY, | |
c67fcd67 | 276 | POWER_SUPPLY_PROP_CYCLE_COUNT, |
d7380965 AS |
277 | POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, |
278 | POWER_SUPPLY_PROP_VOLTAGE_NOW, | |
279 | POWER_SUPPLY_PROP_CURRENT_NOW, | |
280 | POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, | |
281 | POWER_SUPPLY_PROP_CHARGE_FULL, | |
282 | POWER_SUPPLY_PROP_CHARGE_NOW, | |
283 | POWER_SUPPLY_PROP_MODEL_NAME, | |
284 | POWER_SUPPLY_PROP_MANUFACTURER, | |
7c2670bb | 285 | POWER_SUPPLY_PROP_SERIAL_NUMBER, |
d7380965 AS |
286 | }; |
287 | ||
288 | static enum power_supply_property energy_battery_props[] = { | |
289 | POWER_SUPPLY_PROP_STATUS, | |
290 | POWER_SUPPLY_PROP_PRESENT, | |
291 | POWER_SUPPLY_PROP_TECHNOLOGY, | |
c67fcd67 | 292 | POWER_SUPPLY_PROP_CYCLE_COUNT, |
d7380965 AS |
293 | POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, |
294 | POWER_SUPPLY_PROP_VOLTAGE_NOW, | |
7faa144a | 295 | POWER_SUPPLY_PROP_POWER_NOW, |
d7380965 AS |
296 | POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, |
297 | POWER_SUPPLY_PROP_ENERGY_FULL, | |
298 | POWER_SUPPLY_PROP_ENERGY_NOW, | |
299 | POWER_SUPPLY_PROP_MODEL_NAME, | |
300 | POWER_SUPPLY_PROP_MANUFACTURER, | |
7c2670bb | 301 | POWER_SUPPLY_PROP_SERIAL_NUMBER, |
d7380965 AS |
302 | }; |
303 | ||
fdcedbba | 304 | #ifdef CONFIG_ACPI_PROCFS_POWER |
f1d4661a | 305 | inline char *acpi_battery_units(struct acpi_battery *battery) |
78490d82 | 306 | { |
ae6f6187 LT |
307 | return (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA) ? |
308 | "mA" : "mW"; | |
b6ce4083 | 309 | } |
d7380965 | 310 | #endif |
b6ce4083 | 311 | |
78490d82 AS |
312 | /* -------------------------------------------------------------------------- |
313 | Battery Management | |
314 | -------------------------------------------------------------------------- */ | |
038fdea2 AS |
315 | struct acpi_offsets { |
316 | size_t offset; /* offset inside struct acpi_sbs_battery */ | |
317 | u8 mode; /* int or string? */ | |
318 | }; | |
b6ce4083 | 319 | |
038fdea2 AS |
320 | static struct acpi_offsets state_offsets[] = { |
321 | {offsetof(struct acpi_battery, state), 0}, | |
7faa144a | 322 | {offsetof(struct acpi_battery, rate_now), 0}, |
d7380965 AS |
323 | {offsetof(struct acpi_battery, capacity_now), 0}, |
324 | {offsetof(struct acpi_battery, voltage_now), 0}, | |
038fdea2 | 325 | }; |
b6ce4083 | 326 | |
038fdea2 AS |
327 | static struct acpi_offsets info_offsets[] = { |
328 | {offsetof(struct acpi_battery, power_unit), 0}, | |
329 | {offsetof(struct acpi_battery, design_capacity), 0}, | |
d7380965 | 330 | {offsetof(struct acpi_battery, full_charge_capacity), 0}, |
038fdea2 AS |
331 | {offsetof(struct acpi_battery, technology), 0}, |
332 | {offsetof(struct acpi_battery, design_voltage), 0}, | |
333 | {offsetof(struct acpi_battery, design_capacity_warning), 0}, | |
334 | {offsetof(struct acpi_battery, design_capacity_low), 0}, | |
335 | {offsetof(struct acpi_battery, capacity_granularity_1), 0}, | |
336 | {offsetof(struct acpi_battery, capacity_granularity_2), 0}, | |
337 | {offsetof(struct acpi_battery, model_number), 1}, | |
338 | {offsetof(struct acpi_battery, serial_number), 1}, | |
339 | {offsetof(struct acpi_battery, type), 1}, | |
340 | {offsetof(struct acpi_battery, oem_info), 1}, | |
341 | }; | |
b6ce4083 | 342 | |
c67fcd67 AS |
343 | static struct acpi_offsets extended_info_offsets[] = { |
344 | {offsetof(struct acpi_battery, power_unit), 0}, | |
345 | {offsetof(struct acpi_battery, design_capacity), 0}, | |
346 | {offsetof(struct acpi_battery, full_charge_capacity), 0}, | |
347 | {offsetof(struct acpi_battery, technology), 0}, | |
348 | {offsetof(struct acpi_battery, design_voltage), 0}, | |
349 | {offsetof(struct acpi_battery, design_capacity_warning), 0}, | |
350 | {offsetof(struct acpi_battery, design_capacity_low), 0}, | |
351 | {offsetof(struct acpi_battery, cycle_count), 0}, | |
352 | {offsetof(struct acpi_battery, measurement_accuracy), 0}, | |
353 | {offsetof(struct acpi_battery, max_sampling_time), 0}, | |
354 | {offsetof(struct acpi_battery, min_sampling_time), 0}, | |
355 | {offsetof(struct acpi_battery, max_averaging_interval), 0}, | |
356 | {offsetof(struct acpi_battery, min_averaging_interval), 0}, | |
357 | {offsetof(struct acpi_battery, capacity_granularity_1), 0}, | |
358 | {offsetof(struct acpi_battery, capacity_granularity_2), 0}, | |
359 | {offsetof(struct acpi_battery, model_number), 1}, | |
360 | {offsetof(struct acpi_battery, serial_number), 1}, | |
361 | {offsetof(struct acpi_battery, type), 1}, | |
362 | {offsetof(struct acpi_battery, oem_info), 1}, | |
363 | }; | |
364 | ||
038fdea2 AS |
365 | static int extract_package(struct acpi_battery *battery, |
366 | union acpi_object *package, | |
367 | struct acpi_offsets *offsets, int num) | |
368 | { | |
106449e8 | 369 | int i; |
038fdea2 AS |
370 | union acpi_object *element; |
371 | if (package->type != ACPI_TYPE_PACKAGE) | |
372 | return -EFAULT; | |
373 | for (i = 0; i < num; ++i) { | |
374 | if (package->package.count <= i) | |
375 | return -EFAULT; | |
376 | element = &package->package.elements[i]; | |
377 | if (offsets[i].mode) { | |
106449e8 AS |
378 | u8 *ptr = (u8 *)battery + offsets[i].offset; |
379 | if (element->type == ACPI_TYPE_STRING || | |
380 | element->type == ACPI_TYPE_BUFFER) | |
381 | strncpy(ptr, element->string.pointer, 32); | |
382 | else if (element->type == ACPI_TYPE_INTEGER) { | |
383 | strncpy(ptr, (u8 *)&element->integer.value, | |
439913ff LM |
384 | sizeof(u64)); |
385 | ptr[sizeof(u64)] = 0; | |
b8a1bdb1 AS |
386 | } else |
387 | *ptr = 0; /* don't have value */ | |
038fdea2 | 388 | } else { |
b8a1bdb1 AS |
389 | int *x = (int *)((u8 *)battery + offsets[i].offset); |
390 | *x = (element->type == ACPI_TYPE_INTEGER) ? | |
391 | element->integer.value : -1; | |
038fdea2 | 392 | } |
b6ce4083 | 393 | } |
b6ce4083 VL |
394 | return 0; |
395 | } | |
396 | ||
397 | static int acpi_battery_get_status(struct acpi_battery *battery) | |
398 | { | |
aa650bbd | 399 | if (acpi_bus_get_status(battery->device)) { |
b6ce4083 VL |
400 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Evaluating _STA")); |
401 | return -ENODEV; | |
402 | } | |
aa650bbd | 403 | return 0; |
b6ce4083 VL |
404 | } |
405 | ||
406 | static int acpi_battery_get_info(struct acpi_battery *battery) | |
1da177e4 | 407 | { |
aa650bbd | 408 | int result = -EFAULT; |
4be44fcd | 409 | acpi_status status = 0; |
c67fcd67 AS |
410 | char *name = test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags)? |
411 | "_BIX" : "_BIF"; | |
412 | ||
4be44fcd | 413 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; |
1da177e4 | 414 | |
b6ce4083 VL |
415 | if (!acpi_battery_present(battery)) |
416 | return 0; | |
038fdea2 | 417 | mutex_lock(&battery->lock); |
c67fcd67 AS |
418 | status = acpi_evaluate_object(battery->device->handle, name, |
419 | NULL, &buffer); | |
038fdea2 | 420 | mutex_unlock(&battery->lock); |
aa650bbd | 421 | |
1da177e4 | 422 | if (ACPI_FAILURE(status)) { |
c67fcd67 | 423 | ACPI_EXCEPTION((AE_INFO, status, "Evaluating %s", name)); |
d550d98d | 424 | return -ENODEV; |
1da177e4 | 425 | } |
c67fcd67 AS |
426 | if (test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags)) |
427 | result = extract_package(battery, buffer.pointer, | |
428 | extended_info_offsets, | |
429 | ARRAY_SIZE(extended_info_offsets)); | |
430 | else | |
431 | result = extract_package(battery, buffer.pointer, | |
432 | info_offsets, ARRAY_SIZE(info_offsets)); | |
78490d82 | 433 | kfree(buffer.pointer); |
557d5868 ZR |
434 | if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags)) |
435 | battery->full_charge_capacity = battery->design_capacity; | |
d550d98d | 436 | return result; |
1da177e4 LT |
437 | } |
438 | ||
b6ce4083 | 439 | static int acpi_battery_get_state(struct acpi_battery *battery) |
1da177e4 | 440 | { |
4be44fcd LB |
441 | int result = 0; |
442 | acpi_status status = 0; | |
443 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; | |
1da177e4 | 444 | |
b6ce4083 VL |
445 | if (!acpi_battery_present(battery)) |
446 | return 0; | |
1da177e4 | 447 | |
f1d4661a AS |
448 | if (battery->update_time && |
449 | time_before(jiffies, battery->update_time + | |
450 | msecs_to_jiffies(cache_time))) | |
451 | return 0; | |
452 | ||
038fdea2 | 453 | mutex_lock(&battery->lock); |
f1d4661a | 454 | status = acpi_evaluate_object(battery->device->handle, "_BST", |
038fdea2 AS |
455 | NULL, &buffer); |
456 | mutex_unlock(&battery->lock); | |
5b31d895 | 457 | |
1da177e4 | 458 | if (ACPI_FAILURE(status)) { |
a6fc6720 | 459 | ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BST")); |
d550d98d | 460 | return -ENODEV; |
1da177e4 | 461 | } |
aa650bbd | 462 | |
038fdea2 AS |
463 | result = extract_package(battery, buffer.pointer, |
464 | state_offsets, ARRAY_SIZE(state_offsets)); | |
f1d4661a | 465 | battery->update_time = jiffies; |
78490d82 | 466 | kfree(buffer.pointer); |
bc76f90b | 467 | |
7b3bcc4a | 468 | if (test_bit(ACPI_BATTERY_QUIRK_SIGNED16_CURRENT, &battery->flags) && |
bc76f90b HM |
469 | battery->rate_now != -1) |
470 | battery->rate_now = abs((s16)battery->rate_now); | |
471 | ||
557d5868 ZR |
472 | if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags) |
473 | && battery->capacity_now >= 0 && battery->capacity_now <= 100) | |
474 | battery->capacity_now = (battery->capacity_now * | |
475 | battery->full_charge_capacity) / 100; | |
b6ce4083 VL |
476 | return result; |
477 | } | |
1da177e4 | 478 | |
aa650bbd | 479 | static int acpi_battery_set_alarm(struct acpi_battery *battery) |
1da177e4 | 480 | { |
4be44fcd | 481 | acpi_status status = 0; |
aa650bbd | 482 | union acpi_object arg0 = { .type = ACPI_TYPE_INTEGER }; |
4be44fcd | 483 | struct acpi_object_list arg_list = { 1, &arg0 }; |
1da177e4 | 484 | |
c67fcd67 | 485 | if (!acpi_battery_present(battery) || |
7b3bcc4a | 486 | !test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags)) |
d550d98d | 487 | return -ENODEV; |
1da177e4 | 488 | |
aa650bbd | 489 | arg0.integer.value = battery->alarm; |
1da177e4 | 490 | |
038fdea2 | 491 | mutex_lock(&battery->lock); |
f1d4661a AS |
492 | status = acpi_evaluate_object(battery->device->handle, "_BTP", |
493 | &arg_list, NULL); | |
038fdea2 | 494 | mutex_unlock(&battery->lock); |
aa650bbd | 495 | |
1da177e4 | 496 | if (ACPI_FAILURE(status)) |
d550d98d | 497 | return -ENODEV; |
1da177e4 | 498 | |
aa650bbd | 499 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", battery->alarm)); |
d550d98d | 500 | return 0; |
1da177e4 LT |
501 | } |
502 | ||
b6ce4083 | 503 | static int acpi_battery_init_alarm(struct acpi_battery *battery) |
1da177e4 | 504 | { |
4be44fcd LB |
505 | acpi_status status = AE_OK; |
506 | acpi_handle handle = NULL; | |
4be44fcd | 507 | |
b6ce4083 | 508 | /* See if alarms are supported, and if so, set default */ |
f1d4661a AS |
509 | status = acpi_get_handle(battery->device->handle, "_BTP", &handle); |
510 | if (ACPI_FAILURE(status)) { | |
7b3bcc4a | 511 | clear_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags); |
f1d4661a | 512 | return 0; |
b6ce4083 | 513 | } |
7b3bcc4a | 514 | set_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags); |
f1d4661a AS |
515 | if (!battery->alarm) |
516 | battery->alarm = battery->design_capacity_warning; | |
aa650bbd | 517 | return acpi_battery_set_alarm(battery); |
b6ce4083 | 518 | } |
1da177e4 | 519 | |
508df92d AB |
520 | static ssize_t acpi_battery_alarm_show(struct device *dev, |
521 | struct device_attribute *attr, | |
522 | char *buf) | |
523 | { | |
524 | struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev)); | |
525 | return sprintf(buf, "%d\n", battery->alarm * 1000); | |
526 | } | |
527 | ||
528 | static ssize_t acpi_battery_alarm_store(struct device *dev, | |
529 | struct device_attribute *attr, | |
530 | const char *buf, size_t count) | |
531 | { | |
532 | unsigned long x; | |
533 | struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev)); | |
534 | if (sscanf(buf, "%ld\n", &x) == 1) | |
535 | battery->alarm = x/1000; | |
536 | if (acpi_battery_present(battery)) | |
537 | acpi_battery_set_alarm(battery); | |
538 | return count; | |
539 | } | |
540 | ||
541 | static struct device_attribute alarm_attr = { | |
01e8ef11 | 542 | .attr = {.name = "alarm", .mode = 0644}, |
508df92d AB |
543 | .show = acpi_battery_alarm_show, |
544 | .store = acpi_battery_alarm_store, | |
545 | }; | |
546 | ||
547 | static int sysfs_add_battery(struct acpi_battery *battery) | |
548 | { | |
549 | int result; | |
550 | ||
ae6f6187 | 551 | if (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA) { |
508df92d AB |
552 | battery->bat.properties = charge_battery_props; |
553 | battery->bat.num_properties = | |
554 | ARRAY_SIZE(charge_battery_props); | |
555 | } else { | |
556 | battery->bat.properties = energy_battery_props; | |
557 | battery->bat.num_properties = | |
558 | ARRAY_SIZE(energy_battery_props); | |
559 | } | |
560 | ||
561 | battery->bat.name = acpi_device_bid(battery->device); | |
562 | battery->bat.type = POWER_SUPPLY_TYPE_BATTERY; | |
563 | battery->bat.get_property = acpi_battery_get_property; | |
564 | ||
565 | result = power_supply_register(&battery->device->dev, &battery->bat); | |
566 | if (result) | |
567 | return result; | |
568 | return device_create_file(battery->bat.dev, &alarm_attr); | |
569 | } | |
570 | ||
571 | static void sysfs_remove_battery(struct acpi_battery *battery) | |
572 | { | |
573 | if (!battery->bat.dev) | |
574 | return; | |
575 | device_remove_file(battery->bat.dev, &alarm_attr); | |
576 | power_supply_unregister(&battery->bat); | |
9104476e | 577 | battery->bat.dev = NULL; |
508df92d AB |
578 | } |
579 | ||
bc76f90b HM |
580 | static void acpi_battery_quirks(struct acpi_battery *battery) |
581 | { | |
ae6f6187 LT |
582 | if (dmi_name_in_vendors("Acer") && |
583 | battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA) { | |
7b3bcc4a | 584 | set_bit(ACPI_BATTERY_QUIRK_SIGNED16_CURRENT, &battery->flags); |
bc76f90b HM |
585 | } |
586 | } | |
587 | ||
557d5868 ZR |
588 | /* |
589 | * According to the ACPI spec, some kinds of primary batteries can | |
590 | * report percentage battery remaining capacity directly to OS. | |
591 | * In this case, it reports the Last Full Charged Capacity == 100 | |
592 | * and BatteryPresentRate == 0xFFFFFFFF. | |
593 | * | |
594 | * Now we found some battery reports percentage remaining capacity | |
595 | * even if it's rechargeable. | |
596 | * https://bugzilla.kernel.org/show_bug.cgi?id=15979 | |
597 | * | |
598 | * Handle this correctly so that they won't break userspace. | |
599 | */ | |
600 | static void acpi_battery_quirks2(struct acpi_battery *battery) | |
601 | { | |
602 | if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags)) | |
603 | return ; | |
604 | ||
605 | if (battery->full_charge_capacity == 100 && | |
606 | battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN && | |
607 | battery->capacity_now >=0 && battery->capacity_now <= 100) { | |
608 | set_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags); | |
609 | battery->full_charge_capacity = battery->design_capacity; | |
610 | battery->capacity_now = (battery->capacity_now * | |
611 | battery->full_charge_capacity) / 100; | |
612 | } | |
613 | } | |
614 | ||
f1d4661a | 615 | static int acpi_battery_update(struct acpi_battery *battery) |
b6ce4083 | 616 | { |
50b17851 | 617 | int result, old_present = acpi_battery_present(battery); |
97749cd9 | 618 | result = acpi_battery_get_status(battery); |
508df92d | 619 | if (result) |
b6ce4083 | 620 | return result; |
508df92d AB |
621 | if (!acpi_battery_present(battery)) { |
622 | sysfs_remove_battery(battery); | |
97749cd9 | 623 | battery->update_time = 0; |
508df92d | 624 | return 0; |
b6ce4083 | 625 | } |
50b17851 AS |
626 | if (!battery->update_time || |
627 | old_present != acpi_battery_present(battery)) { | |
97749cd9 AS |
628 | result = acpi_battery_get_info(battery); |
629 | if (result) | |
630 | return result; | |
bc76f90b | 631 | acpi_battery_quirks(battery); |
97749cd9 AS |
632 | acpi_battery_init_alarm(battery); |
633 | } | |
508df92d AB |
634 | if (!battery->bat.dev) |
635 | sysfs_add_battery(battery); | |
557d5868 ZR |
636 | result = acpi_battery_get_state(battery); |
637 | acpi_battery_quirks2(battery); | |
638 | return result; | |
4bd35cdb VL |
639 | } |
640 | ||
da8aeb92 RW |
641 | static void acpi_battery_refresh(struct acpi_battery *battery) |
642 | { | |
643 | if (!battery->bat.dev) | |
644 | return; | |
645 | ||
646 | acpi_battery_get_info(battery); | |
647 | /* The battery may have changed its reporting units. */ | |
648 | sysfs_remove_battery(battery); | |
649 | sysfs_add_battery(battery); | |
650 | } | |
651 | ||
1da177e4 LT |
652 | /* -------------------------------------------------------------------------- |
653 | FS Interface (/proc) | |
654 | -------------------------------------------------------------------------- */ | |
655 | ||
fdcedbba | 656 | #ifdef CONFIG_ACPI_PROCFS_POWER |
4be44fcd | 657 | static struct proc_dir_entry *acpi_battery_dir; |
b6ce4083 | 658 | |
78490d82 | 659 | static int acpi_battery_print_info(struct seq_file *seq, int result) |
1da177e4 | 660 | { |
50dd0969 | 661 | struct acpi_battery *battery = seq->private; |
1da177e4 | 662 | |
b6ce4083 | 663 | if (result) |
1da177e4 LT |
664 | goto end; |
665 | ||
aa650bbd AS |
666 | seq_printf(seq, "present: %s\n", |
667 | acpi_battery_present(battery)?"yes":"no"); | |
668 | if (!acpi_battery_present(battery)) | |
1da177e4 | 669 | goto end; |
038fdea2 | 670 | if (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN) |
1da177e4 LT |
671 | seq_printf(seq, "design capacity: unknown\n"); |
672 | else | |
673 | seq_printf(seq, "design capacity: %d %sh\n", | |
aa650bbd AS |
674 | battery->design_capacity, |
675 | acpi_battery_units(battery)); | |
1da177e4 | 676 | |
d7380965 | 677 | if (battery->full_charge_capacity == ACPI_BATTERY_VALUE_UNKNOWN) |
1da177e4 LT |
678 | seq_printf(seq, "last full capacity: unknown\n"); |
679 | else | |
680 | seq_printf(seq, "last full capacity: %d %sh\n", | |
d7380965 | 681 | battery->full_charge_capacity, |
aa650bbd AS |
682 | acpi_battery_units(battery)); |
683 | ||
684 | seq_printf(seq, "battery technology: %srechargeable\n", | |
685 | (!battery->technology)?"non-":""); | |
1da177e4 | 686 | |
038fdea2 | 687 | if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN) |
1da177e4 LT |
688 | seq_printf(seq, "design voltage: unknown\n"); |
689 | else | |
690 | seq_printf(seq, "design voltage: %d mV\n", | |
aa650bbd | 691 | battery->design_voltage); |
1da177e4 | 692 | seq_printf(seq, "design capacity warning: %d %sh\n", |
aa650bbd AS |
693 | battery->design_capacity_warning, |
694 | acpi_battery_units(battery)); | |
1da177e4 | 695 | seq_printf(seq, "design capacity low: %d %sh\n", |
aa650bbd AS |
696 | battery->design_capacity_low, |
697 | acpi_battery_units(battery)); | |
c67fcd67 | 698 | seq_printf(seq, "cycle count: %i\n", battery->cycle_count); |
1da177e4 | 699 | seq_printf(seq, "capacity granularity 1: %d %sh\n", |
aa650bbd AS |
700 | battery->capacity_granularity_1, |
701 | acpi_battery_units(battery)); | |
1da177e4 | 702 | seq_printf(seq, "capacity granularity 2: %d %sh\n", |
aa650bbd AS |
703 | battery->capacity_granularity_2, |
704 | acpi_battery_units(battery)); | |
038fdea2 AS |
705 | seq_printf(seq, "model number: %s\n", battery->model_number); |
706 | seq_printf(seq, "serial number: %s\n", battery->serial_number); | |
707 | seq_printf(seq, "battery type: %s\n", battery->type); | |
708 | seq_printf(seq, "OEM info: %s\n", battery->oem_info); | |
4be44fcd | 709 | end: |
b6ce4083 VL |
710 | if (result) |
711 | seq_printf(seq, "ERROR: Unable to read battery info\n"); | |
b6ce4083 VL |
712 | return result; |
713 | } | |
714 | ||
78490d82 | 715 | static int acpi_battery_print_state(struct seq_file *seq, int result) |
1da177e4 | 716 | { |
50dd0969 | 717 | struct acpi_battery *battery = seq->private; |
1da177e4 | 718 | |
b6ce4083 | 719 | if (result) |
1da177e4 LT |
720 | goto end; |
721 | ||
aa650bbd AS |
722 | seq_printf(seq, "present: %s\n", |
723 | acpi_battery_present(battery)?"yes":"no"); | |
724 | if (!acpi_battery_present(battery)) | |
1da177e4 | 725 | goto end; |
b6ce4083 | 726 | |
aa650bbd AS |
727 | seq_printf(seq, "capacity state: %s\n", |
728 | (battery->state & 0x04)?"critical":"ok"); | |
729 | if ((battery->state & 0x01) && (battery->state & 0x02)) | |
4be44fcd LB |
730 | seq_printf(seq, |
731 | "charging state: charging/discharging\n"); | |
aa650bbd | 732 | else if (battery->state & 0x01) |
1da177e4 | 733 | seq_printf(seq, "charging state: discharging\n"); |
038fdea2 | 734 | else if (battery->state & 0x02) |
1da177e4 | 735 | seq_printf(seq, "charging state: charging\n"); |
aa650bbd | 736 | else |
1da177e4 | 737 | seq_printf(seq, "charging state: charged\n"); |
1da177e4 | 738 | |
7faa144a | 739 | if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN) |
1da177e4 LT |
740 | seq_printf(seq, "present rate: unknown\n"); |
741 | else | |
742 | seq_printf(seq, "present rate: %d %s\n", | |
7faa144a | 743 | battery->rate_now, acpi_battery_units(battery)); |
1da177e4 | 744 | |
d7380965 | 745 | if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN) |
1da177e4 LT |
746 | seq_printf(seq, "remaining capacity: unknown\n"); |
747 | else | |
748 | seq_printf(seq, "remaining capacity: %d %sh\n", | |
d7380965 AS |
749 | battery->capacity_now, acpi_battery_units(battery)); |
750 | if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN) | |
1da177e4 LT |
751 | seq_printf(seq, "present voltage: unknown\n"); |
752 | else | |
753 | seq_printf(seq, "present voltage: %d mV\n", | |
d7380965 | 754 | battery->voltage_now); |
4be44fcd | 755 | end: |
aa650bbd | 756 | if (result) |
b6ce4083 | 757 | seq_printf(seq, "ERROR: Unable to read battery state\n"); |
b6ce4083 VL |
758 | |
759 | return result; | |
760 | } | |
761 | ||
78490d82 | 762 | static int acpi_battery_print_alarm(struct seq_file *seq, int result) |
1da177e4 | 763 | { |
50dd0969 | 764 | struct acpi_battery *battery = seq->private; |
1da177e4 | 765 | |
b6ce4083 | 766 | if (result) |
1da177e4 LT |
767 | goto end; |
768 | ||
b6ce4083 | 769 | if (!acpi_battery_present(battery)) { |
1da177e4 LT |
770 | seq_printf(seq, "present: no\n"); |
771 | goto end; | |
772 | } | |
1da177e4 LT |
773 | seq_printf(seq, "alarm: "); |
774 | if (!battery->alarm) | |
775 | seq_printf(seq, "unsupported\n"); | |
776 | else | |
aa650bbd AS |
777 | seq_printf(seq, "%u %sh\n", battery->alarm, |
778 | acpi_battery_units(battery)); | |
4be44fcd | 779 | end: |
b6ce4083 VL |
780 | if (result) |
781 | seq_printf(seq, "ERROR: Unable to read battery alarm\n"); | |
b6ce4083 VL |
782 | return result; |
783 | } | |
784 | ||
f1d4661a AS |
785 | static ssize_t acpi_battery_write_alarm(struct file *file, |
786 | const char __user * buffer, | |
787 | size_t count, loff_t * ppos) | |
1da177e4 | 788 | { |
4be44fcd LB |
789 | int result = 0; |
790 | char alarm_string[12] = { '\0' }; | |
50dd0969 JE |
791 | struct seq_file *m = file->private_data; |
792 | struct acpi_battery *battery = m->private; | |
1da177e4 | 793 | |
1da177e4 | 794 | if (!battery || (count > sizeof(alarm_string) - 1)) |
d550d98d | 795 | return -EINVAL; |
b6ce4083 VL |
796 | if (!acpi_battery_present(battery)) { |
797 | result = -ENODEV; | |
798 | goto end; | |
799 | } | |
b6ce4083 VL |
800 | if (copy_from_user(alarm_string, buffer, count)) { |
801 | result = -EFAULT; | |
802 | goto end; | |
803 | } | |
1da177e4 | 804 | alarm_string[count] = '\0'; |
f1d4661a | 805 | battery->alarm = simple_strtol(alarm_string, NULL, 0); |
aa650bbd | 806 | result = acpi_battery_set_alarm(battery); |
b6ce4083 | 807 | end: |
b6ce4083 | 808 | if (!result) |
f1d4661a | 809 | return count; |
78490d82 AS |
810 | return result; |
811 | } | |
812 | ||
813 | typedef int(*print_func)(struct seq_file *seq, int result); | |
aa650bbd AS |
814 | |
815 | static print_func acpi_print_funcs[ACPI_BATTERY_NUMFILES] = { | |
816 | acpi_battery_print_info, | |
817 | acpi_battery_print_state, | |
818 | acpi_battery_print_alarm, | |
78490d82 | 819 | }; |
b6ce4083 | 820 | |
78490d82 AS |
821 | static int acpi_battery_read(int fid, struct seq_file *seq) |
822 | { | |
823 | struct acpi_battery *battery = seq->private; | |
f1d4661a | 824 | int result = acpi_battery_update(battery); |
aa650bbd | 825 | return acpi_print_funcs[fid](seq, result); |
78490d82 AS |
826 | } |
827 | ||
aa650bbd AS |
828 | #define DECLARE_FILE_FUNCTIONS(_name) \ |
829 | static int acpi_battery_read_##_name(struct seq_file *seq, void *offset) \ | |
830 | { \ | |
831 | return acpi_battery_read(_name##_tag, seq); \ | |
832 | } \ | |
833 | static int acpi_battery_##_name##_open_fs(struct inode *inode, struct file *file) \ | |
834 | { \ | |
835 | return single_open(file, acpi_battery_read_##_name, PDE(inode)->data); \ | |
78490d82 AS |
836 | } |
837 | ||
aa650bbd AS |
838 | DECLARE_FILE_FUNCTIONS(info); |
839 | DECLARE_FILE_FUNCTIONS(state); | |
840 | DECLARE_FILE_FUNCTIONS(alarm); | |
841 | ||
842 | #undef DECLARE_FILE_FUNCTIONS | |
843 | ||
844 | #define FILE_DESCRIPTION_RO(_name) \ | |
845 | { \ | |
846 | .name = __stringify(_name), \ | |
847 | .mode = S_IRUGO, \ | |
848 | .ops = { \ | |
849 | .open = acpi_battery_##_name##_open_fs, \ | |
850 | .read = seq_read, \ | |
851 | .llseek = seq_lseek, \ | |
852 | .release = single_release, \ | |
853 | .owner = THIS_MODULE, \ | |
854 | }, \ | |
855 | } | |
78490d82 | 856 | |
aa650bbd AS |
857 | #define FILE_DESCRIPTION_RW(_name) \ |
858 | { \ | |
859 | .name = __stringify(_name), \ | |
860 | .mode = S_IFREG | S_IRUGO | S_IWUSR, \ | |
861 | .ops = { \ | |
862 | .open = acpi_battery_##_name##_open_fs, \ | |
863 | .read = seq_read, \ | |
864 | .llseek = seq_lseek, \ | |
865 | .write = acpi_battery_write_##_name, \ | |
866 | .release = single_release, \ | |
867 | .owner = THIS_MODULE, \ | |
868 | }, \ | |
869 | } | |
1da177e4 | 870 | |
78490d82 AS |
871 | static struct battery_file { |
872 | struct file_operations ops; | |
873 | mode_t mode; | |
070d8eb1 | 874 | const char *name; |
78490d82 | 875 | } acpi_battery_file[] = { |
aa650bbd AS |
876 | FILE_DESCRIPTION_RO(info), |
877 | FILE_DESCRIPTION_RO(state), | |
878 | FILE_DESCRIPTION_RW(alarm), | |
1da177e4 LT |
879 | }; |
880 | ||
aa650bbd AS |
881 | #undef FILE_DESCRIPTION_RO |
882 | #undef FILE_DESCRIPTION_RW | |
883 | ||
4be44fcd | 884 | static int acpi_battery_add_fs(struct acpi_device *device) |
1da177e4 | 885 | { |
4be44fcd | 886 | struct proc_dir_entry *entry = NULL; |
78490d82 | 887 | int i; |
1da177e4 | 888 | |
6d855fcd ZR |
889 | printk(KERN_WARNING PREFIX "Deprecated procfs I/F for battery is loaded," |
890 | " please retry with CONFIG_ACPI_PROCFS_POWER cleared\n"); | |
1da177e4 LT |
891 | if (!acpi_device_dir(device)) { |
892 | acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), | |
4be44fcd | 893 | acpi_battery_dir); |
1da177e4 | 894 | if (!acpi_device_dir(device)) |
d550d98d | 895 | return -ENODEV; |
1da177e4 LT |
896 | } |
897 | ||
78490d82 | 898 | for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i) { |
cf7acfab DL |
899 | entry = proc_create_data(acpi_battery_file[i].name, |
900 | acpi_battery_file[i].mode, | |
901 | acpi_device_dir(device), | |
902 | &acpi_battery_file[i].ops, | |
903 | acpi_driver_data(device)); | |
78490d82 AS |
904 | if (!entry) |
905 | return -ENODEV; | |
1da177e4 | 906 | } |
d550d98d | 907 | return 0; |
1da177e4 LT |
908 | } |
909 | ||
aa650bbd | 910 | static void acpi_battery_remove_fs(struct acpi_device *device) |
1da177e4 | 911 | { |
78490d82 | 912 | int i; |
aa650bbd AS |
913 | if (!acpi_device_dir(device)) |
914 | return; | |
915 | for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i) | |
916 | remove_proc_entry(acpi_battery_file[i].name, | |
1da177e4 | 917 | acpi_device_dir(device)); |
1da177e4 | 918 | |
aa650bbd AS |
919 | remove_proc_entry(acpi_device_bid(device), acpi_battery_dir); |
920 | acpi_device_dir(device) = NULL; | |
1da177e4 LT |
921 | } |
922 | ||
d7380965 | 923 | #endif |
3e58ea0d | 924 | |
1da177e4 LT |
925 | /* -------------------------------------------------------------------------- |
926 | Driver Interface | |
927 | -------------------------------------------------------------------------- */ | |
928 | ||
d9406691 | 929 | static void acpi_battery_notify(struct acpi_device *device, u32 event) |
1da177e4 | 930 | { |
d9406691 | 931 | struct acpi_battery *battery = acpi_driver_data(device); |
153e500f | 932 | struct device *old; |
d9406691 | 933 | |
1da177e4 | 934 | if (!battery) |
d550d98d | 935 | return; |
153e500f | 936 | old = battery->bat.dev; |
da8aeb92 RW |
937 | if (event == ACPI_BATTERY_NOTIFY_INFO) |
938 | acpi_battery_refresh(battery); | |
f1d4661a AS |
939 | acpi_battery_update(battery); |
940 | acpi_bus_generate_proc_event(device, event, | |
941 | acpi_battery_present(battery)); | |
942 | acpi_bus_generate_netlink_event(device->pnp.device_class, | |
0794469d | 943 | dev_name(&device->dev), event, |
9ea7d575 | 944 | acpi_battery_present(battery)); |
2345baf4 | 945 | /* acpi_battery_update could remove power_supply object */ |
153e500f | 946 | if (old && battery->bat.dev) |
f79e1cec | 947 | power_supply_changed(&battery->bat); |
1da177e4 LT |
948 | } |
949 | ||
25be5821 KM |
950 | static int battery_notify(struct notifier_block *nb, |
951 | unsigned long mode, void *_unused) | |
952 | { | |
953 | struct acpi_battery *battery = container_of(nb, struct acpi_battery, | |
954 | pm_nb); | |
955 | switch (mode) { | |
956 | case PM_POST_SUSPEND: | |
957 | sysfs_remove_battery(battery); | |
958 | sysfs_add_battery(battery); | |
959 | break; | |
960 | } | |
961 | ||
962 | return 0; | |
963 | } | |
964 | ||
4be44fcd | 965 | static int acpi_battery_add(struct acpi_device *device) |
1da177e4 | 966 | { |
4be44fcd | 967 | int result = 0; |
4be44fcd | 968 | struct acpi_battery *battery = NULL; |
c67fcd67 | 969 | acpi_handle handle; |
1da177e4 | 970 | if (!device) |
d550d98d | 971 | return -EINVAL; |
36bcbec7 | 972 | battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL); |
1da177e4 | 973 | if (!battery) |
d550d98d | 974 | return -ENOMEM; |
145def84 | 975 | battery->device = device; |
1da177e4 LT |
976 | strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME); |
977 | strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS); | |
db89b4f0 | 978 | device->driver_data = battery; |
038fdea2 | 979 | mutex_init(&battery->lock); |
c67fcd67 AS |
980 | if (ACPI_SUCCESS(acpi_get_handle(battery->device->handle, |
981 | "_BIX", &handle))) | |
982 | set_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags); | |
f1d4661a | 983 | acpi_battery_update(battery); |
fdcedbba | 984 | #ifdef CONFIG_ACPI_PROCFS_POWER |
1da177e4 | 985 | result = acpi_battery_add_fs(device); |
d7380965 | 986 | #endif |
586caae3 LB |
987 | if (!result) { |
988 | printk(KERN_INFO PREFIX "%s Slot [%s] (battery %s)\n", | |
989 | ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device), | |
990 | device->status.battery_present ? "present" : "absent"); | |
991 | } else { | |
fdcedbba | 992 | #ifdef CONFIG_ACPI_PROCFS_POWER |
1da177e4 | 993 | acpi_battery_remove_fs(device); |
d7380965 | 994 | #endif |
1da177e4 LT |
995 | kfree(battery); |
996 | } | |
25be5821 KM |
997 | |
998 | battery->pm_nb.notifier_call = battery_notify; | |
999 | register_pm_notifier(&battery->pm_nb); | |
1000 | ||
d550d98d | 1001 | return result; |
1da177e4 LT |
1002 | } |
1003 | ||
4be44fcd | 1004 | static int acpi_battery_remove(struct acpi_device *device, int type) |
1da177e4 | 1005 | { |
4be44fcd | 1006 | struct acpi_battery *battery = NULL; |
1da177e4 | 1007 | |
1da177e4 | 1008 | if (!device || !acpi_driver_data(device)) |
d550d98d | 1009 | return -EINVAL; |
50dd0969 | 1010 | battery = acpi_driver_data(device); |
25be5821 | 1011 | unregister_pm_notifier(&battery->pm_nb); |
fdcedbba | 1012 | #ifdef CONFIG_ACPI_PROCFS_POWER |
1da177e4 | 1013 | acpi_battery_remove_fs(device); |
d7380965 | 1014 | #endif |
508df92d | 1015 | sysfs_remove_battery(battery); |
038fdea2 | 1016 | mutex_destroy(&battery->lock); |
1da177e4 | 1017 | kfree(battery); |
d550d98d | 1018 | return 0; |
1da177e4 LT |
1019 | } |
1020 | ||
34c4415a | 1021 | /* this is needed to learn about changes made in suspended state */ |
5d9464a4 | 1022 | static int acpi_battery_resume(struct acpi_device *device) |
34c4415a JK |
1023 | { |
1024 | struct acpi_battery *battery; | |
34c4415a JK |
1025 | if (!device) |
1026 | return -EINVAL; | |
f1d4661a AS |
1027 | battery = acpi_driver_data(device); |
1028 | battery->update_time = 0; | |
508df92d | 1029 | acpi_battery_update(battery); |
b6ce4083 | 1030 | return 0; |
34c4415a JK |
1031 | } |
1032 | ||
aa650bbd AS |
1033 | static struct acpi_driver acpi_battery_driver = { |
1034 | .name = "battery", | |
1035 | .class = ACPI_BATTERY_CLASS, | |
1036 | .ids = battery_device_ids, | |
d9406691 | 1037 | .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS, |
aa650bbd AS |
1038 | .ops = { |
1039 | .add = acpi_battery_add, | |
1040 | .resume = acpi_battery_resume, | |
1041 | .remove = acpi_battery_remove, | |
d9406691 | 1042 | .notify = acpi_battery_notify, |
aa650bbd AS |
1043 | }, |
1044 | }; | |
1045 | ||
b0cbc861 | 1046 | static void __init acpi_battery_init_async(void *unused, async_cookie_t cookie) |
1da177e4 | 1047 | { |
4d8316d5 | 1048 | if (acpi_disabled) |
0f66af53 | 1049 | return; |
fdcedbba | 1050 | #ifdef CONFIG_ACPI_PROCFS_POWER |
3f86b832 | 1051 | acpi_battery_dir = acpi_lock_battery_dir(); |
1da177e4 | 1052 | if (!acpi_battery_dir) |
0f66af53 | 1053 | return; |
d7380965 | 1054 | #endif |
aa650bbd | 1055 | if (acpi_bus_register_driver(&acpi_battery_driver) < 0) { |
fdcedbba | 1056 | #ifdef CONFIG_ACPI_PROCFS_POWER |
3f86b832 | 1057 | acpi_unlock_battery_dir(acpi_battery_dir); |
d7380965 | 1058 | #endif |
0f66af53 | 1059 | return; |
1da177e4 | 1060 | } |
0f66af53 AV |
1061 | return; |
1062 | } | |
1063 | ||
1064 | static int __init acpi_battery_init(void) | |
1065 | { | |
1066 | async_schedule(acpi_battery_init_async, NULL); | |
d550d98d | 1067 | return 0; |
1da177e4 LT |
1068 | } |
1069 | ||
4be44fcd | 1070 | static void __exit acpi_battery_exit(void) |
1da177e4 | 1071 | { |
1da177e4 | 1072 | acpi_bus_unregister_driver(&acpi_battery_driver); |
fdcedbba | 1073 | #ifdef CONFIG_ACPI_PROCFS_POWER |
3f86b832 | 1074 | acpi_unlock_battery_dir(acpi_battery_dir); |
d7380965 | 1075 | #endif |
1da177e4 LT |
1076 | } |
1077 | ||
1da177e4 LT |
1078 | module_init(acpi_battery_init); |
1079 | module_exit(acpi_battery_exit); |