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