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