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