]>
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> |
d7380965 | 36 | |
fdcedbba | 37 | #ifdef CONFIG_ACPI_PROCFS_POWER |
1da177e4 LT |
38 | #include <linux/proc_fs.h> |
39 | #include <linux/seq_file.h> | |
40 | #include <asm/uaccess.h> | |
d7380965 | 41 | #endif |
1da177e4 LT |
42 | |
43 | #include <acpi/acpi_bus.h> | |
44 | #include <acpi/acpi_drivers.h> | |
d7380965 AS |
45 | #include <linux/power_supply.h> |
46 | ||
a192a958 LB |
47 | #define PREFIX "ACPI: " |
48 | ||
1da177e4 LT |
49 | #define ACPI_BATTERY_VALUE_UNKNOWN 0xFFFFFFFF |
50 | ||
1da177e4 | 51 | #define ACPI_BATTERY_CLASS "battery" |
1da177e4 | 52 | #define ACPI_BATTERY_DEVICE_NAME "Battery" |
1da177e4 LT |
53 | #define ACPI_BATTERY_NOTIFY_STATUS 0x80 |
54 | #define ACPI_BATTERY_NOTIFY_INFO 0x81 | |
c67fcd67 | 55 | #define ACPI_BATTERY_NOTIFY_THRESHOLD 0x82 |
1da177e4 | 56 | |
1da177e4 | 57 | #define _COMPONENT ACPI_BATTERY_COMPONENT |
b6ce4083 | 58 | |
f52fd66d | 59 | ACPI_MODULE_NAME("battery"); |
1da177e4 | 60 | |
f52fd66d | 61 | MODULE_AUTHOR("Paul Diefenbaugh"); |
aa650bbd | 62 | MODULE_AUTHOR("Alexey Starikovskiy <[email protected]>"); |
7cda93e0 | 63 | MODULE_DESCRIPTION("ACPI Battery Driver"); |
1da177e4 LT |
64 | MODULE_LICENSE("GPL"); |
65 | ||
f1d4661a AS |
66 | static unsigned int cache_time = 1000; |
67 | module_param(cache_time, uint, 0644); | |
68 | MODULE_PARM_DESC(cache_time, "cache time in milliseconds"); | |
b6ce4083 | 69 | |
fdcedbba | 70 | #ifdef CONFIG_ACPI_PROCFS_POWER |
3f86b832 RT |
71 | extern struct proc_dir_entry *acpi_lock_battery_dir(void); |
72 | extern void *acpi_unlock_battery_dir(struct proc_dir_entry *acpi_battery_dir); | |
73 | ||
d7380965 AS |
74 | enum acpi_battery_files { |
75 | info_tag = 0, | |
76 | state_tag, | |
77 | alarm_tag, | |
78 | ACPI_BATTERY_NUMFILES, | |
79 | }; | |
80 | ||
81 | #endif | |
82 | ||
1ba90e3a TR |
83 | static const struct acpi_device_id battery_device_ids[] = { |
84 | {"PNP0C0A", 0}, | |
85 | {"", 0}, | |
86 | }; | |
1ba90e3a | 87 | |
aa650bbd | 88 | MODULE_DEVICE_TABLE(acpi, battery_device_ids); |
1da177e4 | 89 | |
7b3bcc4a AS |
90 | enum { |
91 | ACPI_BATTERY_ALARM_PRESENT, | |
c67fcd67 AS |
92 | ACPI_BATTERY_XINFO_PRESENT, |
93 | /* For buggy DSDTs that report negative 16-bit values for either | |
94 | * charging or discharging current and/or report 0 as 65536 | |
95 | * due to bad math. | |
96 | */ | |
7b3bcc4a | 97 | ACPI_BATTERY_QUIRK_SIGNED16_CURRENT, |
557d5868 | 98 | ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, |
7b3bcc4a | 99 | }; |
78490d82 | 100 | |
1da177e4 | 101 | struct acpi_battery { |
038fdea2 | 102 | struct mutex lock; |
d7380965 | 103 | struct power_supply bat; |
f1d4661a AS |
104 | struct acpi_device *device; |
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 | { |
f1d4661a | 302 | return (battery->power_unit)?"mA":"mW"; |
b6ce4083 | 303 | } |
d7380965 | 304 | #endif |
b6ce4083 | 305 | |
78490d82 AS |
306 | /* -------------------------------------------------------------------------- |
307 | Battery Management | |
308 | -------------------------------------------------------------------------- */ | |
038fdea2 AS |
309 | struct acpi_offsets { |
310 | size_t offset; /* offset inside struct acpi_sbs_battery */ | |
311 | u8 mode; /* int or string? */ | |
312 | }; | |
b6ce4083 | 313 | |
038fdea2 AS |
314 | static struct acpi_offsets state_offsets[] = { |
315 | {offsetof(struct acpi_battery, state), 0}, | |
7faa144a | 316 | {offsetof(struct acpi_battery, rate_now), 0}, |
d7380965 AS |
317 | {offsetof(struct acpi_battery, capacity_now), 0}, |
318 | {offsetof(struct acpi_battery, voltage_now), 0}, | |
038fdea2 | 319 | }; |
b6ce4083 | 320 | |
038fdea2 AS |
321 | static struct acpi_offsets info_offsets[] = { |
322 | {offsetof(struct acpi_battery, power_unit), 0}, | |
323 | {offsetof(struct acpi_battery, design_capacity), 0}, | |
d7380965 | 324 | {offsetof(struct acpi_battery, full_charge_capacity), 0}, |
038fdea2 AS |
325 | {offsetof(struct acpi_battery, technology), 0}, |
326 | {offsetof(struct acpi_battery, design_voltage), 0}, | |
327 | {offsetof(struct acpi_battery, design_capacity_warning), 0}, | |
328 | {offsetof(struct acpi_battery, design_capacity_low), 0}, | |
329 | {offsetof(struct acpi_battery, capacity_granularity_1), 0}, | |
330 | {offsetof(struct acpi_battery, capacity_granularity_2), 0}, | |
331 | {offsetof(struct acpi_battery, model_number), 1}, | |
332 | {offsetof(struct acpi_battery, serial_number), 1}, | |
333 | {offsetof(struct acpi_battery, type), 1}, | |
334 | {offsetof(struct acpi_battery, oem_info), 1}, | |
335 | }; | |
b6ce4083 | 336 | |
c67fcd67 AS |
337 | static struct acpi_offsets extended_info_offsets[] = { |
338 | {offsetof(struct acpi_battery, power_unit), 0}, | |
339 | {offsetof(struct acpi_battery, design_capacity), 0}, | |
340 | {offsetof(struct acpi_battery, full_charge_capacity), 0}, | |
341 | {offsetof(struct acpi_battery, technology), 0}, | |
342 | {offsetof(struct acpi_battery, design_voltage), 0}, | |
343 | {offsetof(struct acpi_battery, design_capacity_warning), 0}, | |
344 | {offsetof(struct acpi_battery, design_capacity_low), 0}, | |
345 | {offsetof(struct acpi_battery, cycle_count), 0}, | |
346 | {offsetof(struct acpi_battery, measurement_accuracy), 0}, | |
347 | {offsetof(struct acpi_battery, max_sampling_time), 0}, | |
348 | {offsetof(struct acpi_battery, min_sampling_time), 0}, | |
349 | {offsetof(struct acpi_battery, max_averaging_interval), 0}, | |
350 | {offsetof(struct acpi_battery, min_averaging_interval), 0}, | |
351 | {offsetof(struct acpi_battery, capacity_granularity_1), 0}, | |
352 | {offsetof(struct acpi_battery, capacity_granularity_2), 0}, | |
353 | {offsetof(struct acpi_battery, model_number), 1}, | |
354 | {offsetof(struct acpi_battery, serial_number), 1}, | |
355 | {offsetof(struct acpi_battery, type), 1}, | |
356 | {offsetof(struct acpi_battery, oem_info), 1}, | |
357 | }; | |
358 | ||
038fdea2 AS |
359 | static int extract_package(struct acpi_battery *battery, |
360 | union acpi_object *package, | |
361 | struct acpi_offsets *offsets, int num) | |
362 | { | |
106449e8 | 363 | int i; |
038fdea2 AS |
364 | union acpi_object *element; |
365 | if (package->type != ACPI_TYPE_PACKAGE) | |
366 | return -EFAULT; | |
367 | for (i = 0; i < num; ++i) { | |
368 | if (package->package.count <= i) | |
369 | return -EFAULT; | |
370 | element = &package->package.elements[i]; | |
371 | if (offsets[i].mode) { | |
106449e8 AS |
372 | u8 *ptr = (u8 *)battery + offsets[i].offset; |
373 | if (element->type == ACPI_TYPE_STRING || | |
374 | element->type == ACPI_TYPE_BUFFER) | |
375 | strncpy(ptr, element->string.pointer, 32); | |
376 | else if (element->type == ACPI_TYPE_INTEGER) { | |
377 | strncpy(ptr, (u8 *)&element->integer.value, | |
439913ff LM |
378 | sizeof(u64)); |
379 | ptr[sizeof(u64)] = 0; | |
b8a1bdb1 AS |
380 | } else |
381 | *ptr = 0; /* don't have value */ | |
038fdea2 | 382 | } else { |
b8a1bdb1 AS |
383 | int *x = (int *)((u8 *)battery + offsets[i].offset); |
384 | *x = (element->type == ACPI_TYPE_INTEGER) ? | |
385 | element->integer.value : -1; | |
038fdea2 | 386 | } |
b6ce4083 | 387 | } |
b6ce4083 VL |
388 | return 0; |
389 | } | |
390 | ||
391 | static int acpi_battery_get_status(struct acpi_battery *battery) | |
392 | { | |
aa650bbd | 393 | if (acpi_bus_get_status(battery->device)) { |
b6ce4083 VL |
394 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Evaluating _STA")); |
395 | return -ENODEV; | |
396 | } | |
aa650bbd | 397 | return 0; |
b6ce4083 VL |
398 | } |
399 | ||
400 | static int acpi_battery_get_info(struct acpi_battery *battery) | |
1da177e4 | 401 | { |
aa650bbd | 402 | int result = -EFAULT; |
4be44fcd | 403 | acpi_status status = 0; |
c67fcd67 AS |
404 | char *name = test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags)? |
405 | "_BIX" : "_BIF"; | |
406 | ||
4be44fcd | 407 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; |
1da177e4 | 408 | |
b6ce4083 VL |
409 | if (!acpi_battery_present(battery)) |
410 | return 0; | |
038fdea2 | 411 | mutex_lock(&battery->lock); |
c67fcd67 AS |
412 | status = acpi_evaluate_object(battery->device->handle, name, |
413 | NULL, &buffer); | |
038fdea2 | 414 | mutex_unlock(&battery->lock); |
aa650bbd | 415 | |
1da177e4 | 416 | if (ACPI_FAILURE(status)) { |
c67fcd67 | 417 | ACPI_EXCEPTION((AE_INFO, status, "Evaluating %s", name)); |
d550d98d | 418 | return -ENODEV; |
1da177e4 | 419 | } |
c67fcd67 AS |
420 | if (test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags)) |
421 | result = extract_package(battery, buffer.pointer, | |
422 | extended_info_offsets, | |
423 | ARRAY_SIZE(extended_info_offsets)); | |
424 | else | |
425 | result = extract_package(battery, buffer.pointer, | |
426 | info_offsets, ARRAY_SIZE(info_offsets)); | |
78490d82 | 427 | kfree(buffer.pointer); |
557d5868 ZR |
428 | if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags)) |
429 | battery->full_charge_capacity = battery->design_capacity; | |
d550d98d | 430 | return result; |
1da177e4 LT |
431 | } |
432 | ||
b6ce4083 | 433 | static int acpi_battery_get_state(struct acpi_battery *battery) |
1da177e4 | 434 | { |
4be44fcd LB |
435 | int result = 0; |
436 | acpi_status status = 0; | |
437 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; | |
1da177e4 | 438 | |
b6ce4083 VL |
439 | if (!acpi_battery_present(battery)) |
440 | return 0; | |
1da177e4 | 441 | |
f1d4661a AS |
442 | if (battery->update_time && |
443 | time_before(jiffies, battery->update_time + | |
444 | msecs_to_jiffies(cache_time))) | |
445 | return 0; | |
446 | ||
038fdea2 | 447 | mutex_lock(&battery->lock); |
f1d4661a | 448 | status = acpi_evaluate_object(battery->device->handle, "_BST", |
038fdea2 AS |
449 | NULL, &buffer); |
450 | mutex_unlock(&battery->lock); | |
5b31d895 | 451 | |
1da177e4 | 452 | if (ACPI_FAILURE(status)) { |
a6fc6720 | 453 | ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BST")); |
d550d98d | 454 | return -ENODEV; |
1da177e4 | 455 | } |
aa650bbd | 456 | |
038fdea2 AS |
457 | result = extract_package(battery, buffer.pointer, |
458 | state_offsets, ARRAY_SIZE(state_offsets)); | |
f1d4661a | 459 | battery->update_time = jiffies; |
78490d82 | 460 | kfree(buffer.pointer); |
bc76f90b | 461 | |
7b3bcc4a | 462 | if (test_bit(ACPI_BATTERY_QUIRK_SIGNED16_CURRENT, &battery->flags) && |
bc76f90b HM |
463 | battery->rate_now != -1) |
464 | battery->rate_now = abs((s16)battery->rate_now); | |
465 | ||
557d5868 ZR |
466 | if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags) |
467 | && battery->capacity_now >= 0 && battery->capacity_now <= 100) | |
468 | battery->capacity_now = (battery->capacity_now * | |
469 | battery->full_charge_capacity) / 100; | |
b6ce4083 VL |
470 | return result; |
471 | } | |
1da177e4 | 472 | |
aa650bbd | 473 | static int acpi_battery_set_alarm(struct acpi_battery *battery) |
1da177e4 | 474 | { |
4be44fcd | 475 | acpi_status status = 0; |
aa650bbd | 476 | union acpi_object arg0 = { .type = ACPI_TYPE_INTEGER }; |
4be44fcd | 477 | struct acpi_object_list arg_list = { 1, &arg0 }; |
1da177e4 | 478 | |
c67fcd67 | 479 | if (!acpi_battery_present(battery) || |
7b3bcc4a | 480 | !test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags)) |
d550d98d | 481 | return -ENODEV; |
1da177e4 | 482 | |
aa650bbd | 483 | arg0.integer.value = battery->alarm; |
1da177e4 | 484 | |
038fdea2 | 485 | mutex_lock(&battery->lock); |
f1d4661a AS |
486 | status = acpi_evaluate_object(battery->device->handle, "_BTP", |
487 | &arg_list, NULL); | |
038fdea2 | 488 | mutex_unlock(&battery->lock); |
aa650bbd | 489 | |
1da177e4 | 490 | if (ACPI_FAILURE(status)) |
d550d98d | 491 | return -ENODEV; |
1da177e4 | 492 | |
aa650bbd | 493 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", battery->alarm)); |
d550d98d | 494 | return 0; |
1da177e4 LT |
495 | } |
496 | ||
b6ce4083 | 497 | static int acpi_battery_init_alarm(struct acpi_battery *battery) |
1da177e4 | 498 | { |
4be44fcd LB |
499 | acpi_status status = AE_OK; |
500 | acpi_handle handle = NULL; | |
4be44fcd | 501 | |
b6ce4083 | 502 | /* See if alarms are supported, and if so, set default */ |
f1d4661a AS |
503 | status = acpi_get_handle(battery->device->handle, "_BTP", &handle); |
504 | if (ACPI_FAILURE(status)) { | |
7b3bcc4a | 505 | clear_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags); |
f1d4661a | 506 | return 0; |
b6ce4083 | 507 | } |
7b3bcc4a | 508 | set_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags); |
f1d4661a AS |
509 | if (!battery->alarm) |
510 | battery->alarm = battery->design_capacity_warning; | |
aa650bbd | 511 | return acpi_battery_set_alarm(battery); |
b6ce4083 | 512 | } |
1da177e4 | 513 | |
508df92d AB |
514 | static ssize_t acpi_battery_alarm_show(struct device *dev, |
515 | struct device_attribute *attr, | |
516 | char *buf) | |
517 | { | |
518 | struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev)); | |
519 | return sprintf(buf, "%d\n", battery->alarm * 1000); | |
520 | } | |
521 | ||
522 | static ssize_t acpi_battery_alarm_store(struct device *dev, | |
523 | struct device_attribute *attr, | |
524 | const char *buf, size_t count) | |
525 | { | |
526 | unsigned long x; | |
527 | struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev)); | |
528 | if (sscanf(buf, "%ld\n", &x) == 1) | |
529 | battery->alarm = x/1000; | |
530 | if (acpi_battery_present(battery)) | |
531 | acpi_battery_set_alarm(battery); | |
532 | return count; | |
533 | } | |
534 | ||
535 | static struct device_attribute alarm_attr = { | |
01e8ef11 | 536 | .attr = {.name = "alarm", .mode = 0644}, |
508df92d AB |
537 | .show = acpi_battery_alarm_show, |
538 | .store = acpi_battery_alarm_store, | |
539 | }; | |
540 | ||
541 | static int sysfs_add_battery(struct acpi_battery *battery) | |
542 | { | |
543 | int result; | |
544 | ||
508df92d AB |
545 | if (battery->power_unit) { |
546 | battery->bat.properties = charge_battery_props; | |
547 | battery->bat.num_properties = | |
548 | ARRAY_SIZE(charge_battery_props); | |
549 | } else { | |
550 | battery->bat.properties = energy_battery_props; | |
551 | battery->bat.num_properties = | |
552 | ARRAY_SIZE(energy_battery_props); | |
553 | } | |
554 | ||
555 | battery->bat.name = acpi_device_bid(battery->device); | |
556 | battery->bat.type = POWER_SUPPLY_TYPE_BATTERY; | |
557 | battery->bat.get_property = acpi_battery_get_property; | |
558 | ||
559 | result = power_supply_register(&battery->device->dev, &battery->bat); | |
560 | if (result) | |
561 | return result; | |
562 | return device_create_file(battery->bat.dev, &alarm_attr); | |
563 | } | |
564 | ||
565 | static void sysfs_remove_battery(struct acpi_battery *battery) | |
566 | { | |
567 | if (!battery->bat.dev) | |
568 | return; | |
569 | device_remove_file(battery->bat.dev, &alarm_attr); | |
570 | power_supply_unregister(&battery->bat); | |
9104476e | 571 | battery->bat.dev = NULL; |
508df92d AB |
572 | } |
573 | ||
bc76f90b HM |
574 | static void acpi_battery_quirks(struct acpi_battery *battery) |
575 | { | |
bc76f90b | 576 | if (dmi_name_in_vendors("Acer") && battery->power_unit) { |
7b3bcc4a | 577 | set_bit(ACPI_BATTERY_QUIRK_SIGNED16_CURRENT, &battery->flags); |
bc76f90b HM |
578 | } |
579 | } | |
580 | ||
557d5868 ZR |
581 | /* |
582 | * According to the ACPI spec, some kinds of primary batteries can | |
583 | * report percentage battery remaining capacity directly to OS. | |
584 | * In this case, it reports the Last Full Charged Capacity == 100 | |
585 | * and BatteryPresentRate == 0xFFFFFFFF. | |
586 | * | |
587 | * Now we found some battery reports percentage remaining capacity | |
588 | * even if it's rechargeable. | |
589 | * https://bugzilla.kernel.org/show_bug.cgi?id=15979 | |
590 | * | |
591 | * Handle this correctly so that they won't break userspace. | |
592 | */ | |
593 | static void acpi_battery_quirks2(struct acpi_battery *battery) | |
594 | { | |
595 | if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags)) | |
596 | return ; | |
597 | ||
598 | if (battery->full_charge_capacity == 100 && | |
599 | battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN && | |
600 | battery->capacity_now >=0 && battery->capacity_now <= 100) { | |
601 | set_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags); | |
602 | battery->full_charge_capacity = battery->design_capacity; | |
603 | battery->capacity_now = (battery->capacity_now * | |
604 | battery->full_charge_capacity) / 100; | |
605 | } | |
606 | } | |
607 | ||
f1d4661a | 608 | static int acpi_battery_update(struct acpi_battery *battery) |
b6ce4083 | 609 | { |
50b17851 | 610 | int result, old_present = acpi_battery_present(battery); |
97749cd9 | 611 | result = acpi_battery_get_status(battery); |
508df92d | 612 | if (result) |
b6ce4083 | 613 | return result; |
508df92d AB |
614 | if (!acpi_battery_present(battery)) { |
615 | sysfs_remove_battery(battery); | |
97749cd9 | 616 | battery->update_time = 0; |
508df92d | 617 | return 0; |
b6ce4083 | 618 | } |
50b17851 AS |
619 | if (!battery->update_time || |
620 | old_present != acpi_battery_present(battery)) { | |
97749cd9 AS |
621 | result = acpi_battery_get_info(battery); |
622 | if (result) | |
623 | return result; | |
bc76f90b | 624 | acpi_battery_quirks(battery); |
97749cd9 AS |
625 | acpi_battery_init_alarm(battery); |
626 | } | |
508df92d AB |
627 | if (!battery->bat.dev) |
628 | sysfs_add_battery(battery); | |
557d5868 ZR |
629 | result = acpi_battery_get_state(battery); |
630 | acpi_battery_quirks2(battery); | |
631 | return result; | |
4bd35cdb VL |
632 | } |
633 | ||
1da177e4 LT |
634 | /* -------------------------------------------------------------------------- |
635 | FS Interface (/proc) | |
636 | -------------------------------------------------------------------------- */ | |
637 | ||
fdcedbba | 638 | #ifdef CONFIG_ACPI_PROCFS_POWER |
4be44fcd | 639 | static struct proc_dir_entry *acpi_battery_dir; |
b6ce4083 | 640 | |
78490d82 | 641 | static int acpi_battery_print_info(struct seq_file *seq, int result) |
1da177e4 | 642 | { |
50dd0969 | 643 | struct acpi_battery *battery = seq->private; |
1da177e4 | 644 | |
b6ce4083 | 645 | if (result) |
1da177e4 LT |
646 | goto end; |
647 | ||
aa650bbd AS |
648 | seq_printf(seq, "present: %s\n", |
649 | acpi_battery_present(battery)?"yes":"no"); | |
650 | if (!acpi_battery_present(battery)) | |
1da177e4 | 651 | goto end; |
038fdea2 | 652 | if (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN) |
1da177e4 LT |
653 | seq_printf(seq, "design capacity: unknown\n"); |
654 | else | |
655 | seq_printf(seq, "design capacity: %d %sh\n", | |
aa650bbd AS |
656 | battery->design_capacity, |
657 | acpi_battery_units(battery)); | |
1da177e4 | 658 | |
d7380965 | 659 | if (battery->full_charge_capacity == ACPI_BATTERY_VALUE_UNKNOWN) |
1da177e4 LT |
660 | seq_printf(seq, "last full capacity: unknown\n"); |
661 | else | |
662 | seq_printf(seq, "last full capacity: %d %sh\n", | |
d7380965 | 663 | battery->full_charge_capacity, |
aa650bbd AS |
664 | acpi_battery_units(battery)); |
665 | ||
666 | seq_printf(seq, "battery technology: %srechargeable\n", | |
667 | (!battery->technology)?"non-":""); | |
1da177e4 | 668 | |
038fdea2 | 669 | if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN) |
1da177e4 LT |
670 | seq_printf(seq, "design voltage: unknown\n"); |
671 | else | |
672 | seq_printf(seq, "design voltage: %d mV\n", | |
aa650bbd | 673 | battery->design_voltage); |
1da177e4 | 674 | seq_printf(seq, "design capacity warning: %d %sh\n", |
aa650bbd AS |
675 | battery->design_capacity_warning, |
676 | acpi_battery_units(battery)); | |
1da177e4 | 677 | seq_printf(seq, "design capacity low: %d %sh\n", |
aa650bbd AS |
678 | battery->design_capacity_low, |
679 | acpi_battery_units(battery)); | |
c67fcd67 | 680 | seq_printf(seq, "cycle count: %i\n", battery->cycle_count); |
1da177e4 | 681 | seq_printf(seq, "capacity granularity 1: %d %sh\n", |
aa650bbd AS |
682 | battery->capacity_granularity_1, |
683 | acpi_battery_units(battery)); | |
1da177e4 | 684 | seq_printf(seq, "capacity granularity 2: %d %sh\n", |
aa650bbd AS |
685 | battery->capacity_granularity_2, |
686 | acpi_battery_units(battery)); | |
038fdea2 AS |
687 | seq_printf(seq, "model number: %s\n", battery->model_number); |
688 | seq_printf(seq, "serial number: %s\n", battery->serial_number); | |
689 | seq_printf(seq, "battery type: %s\n", battery->type); | |
690 | seq_printf(seq, "OEM info: %s\n", battery->oem_info); | |
4be44fcd | 691 | end: |
b6ce4083 VL |
692 | if (result) |
693 | seq_printf(seq, "ERROR: Unable to read battery info\n"); | |
b6ce4083 VL |
694 | return result; |
695 | } | |
696 | ||
78490d82 | 697 | static int acpi_battery_print_state(struct seq_file *seq, int result) |
1da177e4 | 698 | { |
50dd0969 | 699 | struct acpi_battery *battery = seq->private; |
1da177e4 | 700 | |
b6ce4083 | 701 | if (result) |
1da177e4 LT |
702 | goto end; |
703 | ||
aa650bbd AS |
704 | seq_printf(seq, "present: %s\n", |
705 | acpi_battery_present(battery)?"yes":"no"); | |
706 | if (!acpi_battery_present(battery)) | |
1da177e4 | 707 | goto end; |
b6ce4083 | 708 | |
aa650bbd AS |
709 | seq_printf(seq, "capacity state: %s\n", |
710 | (battery->state & 0x04)?"critical":"ok"); | |
711 | if ((battery->state & 0x01) && (battery->state & 0x02)) | |
4be44fcd LB |
712 | seq_printf(seq, |
713 | "charging state: charging/discharging\n"); | |
aa650bbd | 714 | else if (battery->state & 0x01) |
1da177e4 | 715 | seq_printf(seq, "charging state: discharging\n"); |
038fdea2 | 716 | else if (battery->state & 0x02) |
1da177e4 | 717 | seq_printf(seq, "charging state: charging\n"); |
aa650bbd | 718 | else |
1da177e4 | 719 | seq_printf(seq, "charging state: charged\n"); |
1da177e4 | 720 | |
7faa144a | 721 | if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN) |
1da177e4 LT |
722 | seq_printf(seq, "present rate: unknown\n"); |
723 | else | |
724 | seq_printf(seq, "present rate: %d %s\n", | |
7faa144a | 725 | battery->rate_now, acpi_battery_units(battery)); |
1da177e4 | 726 | |
d7380965 | 727 | if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN) |
1da177e4 LT |
728 | seq_printf(seq, "remaining capacity: unknown\n"); |
729 | else | |
730 | seq_printf(seq, "remaining capacity: %d %sh\n", | |
d7380965 AS |
731 | battery->capacity_now, acpi_battery_units(battery)); |
732 | if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN) | |
1da177e4 LT |
733 | seq_printf(seq, "present voltage: unknown\n"); |
734 | else | |
735 | seq_printf(seq, "present voltage: %d mV\n", | |
d7380965 | 736 | battery->voltage_now); |
4be44fcd | 737 | end: |
aa650bbd | 738 | if (result) |
b6ce4083 | 739 | seq_printf(seq, "ERROR: Unable to read battery state\n"); |
b6ce4083 VL |
740 | |
741 | return result; | |
742 | } | |
743 | ||
78490d82 | 744 | static int acpi_battery_print_alarm(struct seq_file *seq, int result) |
1da177e4 | 745 | { |
50dd0969 | 746 | struct acpi_battery *battery = seq->private; |
1da177e4 | 747 | |
b6ce4083 | 748 | if (result) |
1da177e4 LT |
749 | goto end; |
750 | ||
b6ce4083 | 751 | if (!acpi_battery_present(battery)) { |
1da177e4 LT |
752 | seq_printf(seq, "present: no\n"); |
753 | goto end; | |
754 | } | |
1da177e4 LT |
755 | seq_printf(seq, "alarm: "); |
756 | if (!battery->alarm) | |
757 | seq_printf(seq, "unsupported\n"); | |
758 | else | |
aa650bbd AS |
759 | seq_printf(seq, "%u %sh\n", battery->alarm, |
760 | acpi_battery_units(battery)); | |
4be44fcd | 761 | end: |
b6ce4083 VL |
762 | if (result) |
763 | seq_printf(seq, "ERROR: Unable to read battery alarm\n"); | |
b6ce4083 VL |
764 | return result; |
765 | } | |
766 | ||
f1d4661a AS |
767 | static ssize_t acpi_battery_write_alarm(struct file *file, |
768 | const char __user * buffer, | |
769 | size_t count, loff_t * ppos) | |
1da177e4 | 770 | { |
4be44fcd LB |
771 | int result = 0; |
772 | char alarm_string[12] = { '\0' }; | |
50dd0969 JE |
773 | struct seq_file *m = file->private_data; |
774 | struct acpi_battery *battery = m->private; | |
1da177e4 | 775 | |
1da177e4 | 776 | if (!battery || (count > sizeof(alarm_string) - 1)) |
d550d98d | 777 | return -EINVAL; |
b6ce4083 VL |
778 | if (!acpi_battery_present(battery)) { |
779 | result = -ENODEV; | |
780 | goto end; | |
781 | } | |
b6ce4083 VL |
782 | if (copy_from_user(alarm_string, buffer, count)) { |
783 | result = -EFAULT; | |
784 | goto end; | |
785 | } | |
1da177e4 | 786 | alarm_string[count] = '\0'; |
f1d4661a | 787 | battery->alarm = simple_strtol(alarm_string, NULL, 0); |
aa650bbd | 788 | result = acpi_battery_set_alarm(battery); |
b6ce4083 | 789 | end: |
b6ce4083 | 790 | if (!result) |
f1d4661a | 791 | return count; |
78490d82 AS |
792 | return result; |
793 | } | |
794 | ||
795 | typedef int(*print_func)(struct seq_file *seq, int result); | |
aa650bbd AS |
796 | |
797 | static print_func acpi_print_funcs[ACPI_BATTERY_NUMFILES] = { | |
798 | acpi_battery_print_info, | |
799 | acpi_battery_print_state, | |
800 | acpi_battery_print_alarm, | |
78490d82 | 801 | }; |
b6ce4083 | 802 | |
78490d82 AS |
803 | static int acpi_battery_read(int fid, struct seq_file *seq) |
804 | { | |
805 | struct acpi_battery *battery = seq->private; | |
f1d4661a | 806 | int result = acpi_battery_update(battery); |
aa650bbd | 807 | return acpi_print_funcs[fid](seq, result); |
78490d82 AS |
808 | } |
809 | ||
aa650bbd AS |
810 | #define DECLARE_FILE_FUNCTIONS(_name) \ |
811 | static int acpi_battery_read_##_name(struct seq_file *seq, void *offset) \ | |
812 | { \ | |
813 | return acpi_battery_read(_name##_tag, seq); \ | |
814 | } \ | |
815 | static int acpi_battery_##_name##_open_fs(struct inode *inode, struct file *file) \ | |
816 | { \ | |
817 | return single_open(file, acpi_battery_read_##_name, PDE(inode)->data); \ | |
78490d82 AS |
818 | } |
819 | ||
aa650bbd AS |
820 | DECLARE_FILE_FUNCTIONS(info); |
821 | DECLARE_FILE_FUNCTIONS(state); | |
822 | DECLARE_FILE_FUNCTIONS(alarm); | |
823 | ||
824 | #undef DECLARE_FILE_FUNCTIONS | |
825 | ||
826 | #define FILE_DESCRIPTION_RO(_name) \ | |
827 | { \ | |
828 | .name = __stringify(_name), \ | |
829 | .mode = S_IRUGO, \ | |
830 | .ops = { \ | |
831 | .open = acpi_battery_##_name##_open_fs, \ | |
832 | .read = seq_read, \ | |
833 | .llseek = seq_lseek, \ | |
834 | .release = single_release, \ | |
835 | .owner = THIS_MODULE, \ | |
836 | }, \ | |
837 | } | |
78490d82 | 838 | |
aa650bbd AS |
839 | #define FILE_DESCRIPTION_RW(_name) \ |
840 | { \ | |
841 | .name = __stringify(_name), \ | |
842 | .mode = S_IFREG | S_IRUGO | S_IWUSR, \ | |
843 | .ops = { \ | |
844 | .open = acpi_battery_##_name##_open_fs, \ | |
845 | .read = seq_read, \ | |
846 | .llseek = seq_lseek, \ | |
847 | .write = acpi_battery_write_##_name, \ | |
848 | .release = single_release, \ | |
849 | .owner = THIS_MODULE, \ | |
850 | }, \ | |
851 | } | |
1da177e4 | 852 | |
78490d82 AS |
853 | static struct battery_file { |
854 | struct file_operations ops; | |
855 | mode_t mode; | |
070d8eb1 | 856 | const char *name; |
78490d82 | 857 | } acpi_battery_file[] = { |
aa650bbd AS |
858 | FILE_DESCRIPTION_RO(info), |
859 | FILE_DESCRIPTION_RO(state), | |
860 | FILE_DESCRIPTION_RW(alarm), | |
1da177e4 LT |
861 | }; |
862 | ||
aa650bbd AS |
863 | #undef FILE_DESCRIPTION_RO |
864 | #undef FILE_DESCRIPTION_RW | |
865 | ||
4be44fcd | 866 | static int acpi_battery_add_fs(struct acpi_device *device) |
1da177e4 | 867 | { |
4be44fcd | 868 | struct proc_dir_entry *entry = NULL; |
78490d82 | 869 | int i; |
1da177e4 | 870 | |
1da177e4 LT |
871 | if (!acpi_device_dir(device)) { |
872 | acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), | |
4be44fcd | 873 | acpi_battery_dir); |
1da177e4 | 874 | if (!acpi_device_dir(device)) |
d550d98d | 875 | return -ENODEV; |
1da177e4 LT |
876 | } |
877 | ||
78490d82 | 878 | for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i) { |
cf7acfab DL |
879 | entry = proc_create_data(acpi_battery_file[i].name, |
880 | acpi_battery_file[i].mode, | |
881 | acpi_device_dir(device), | |
882 | &acpi_battery_file[i].ops, | |
883 | acpi_driver_data(device)); | |
78490d82 AS |
884 | if (!entry) |
885 | return -ENODEV; | |
1da177e4 | 886 | } |
d550d98d | 887 | return 0; |
1da177e4 LT |
888 | } |
889 | ||
aa650bbd | 890 | static void acpi_battery_remove_fs(struct acpi_device *device) |
1da177e4 | 891 | { |
78490d82 | 892 | int i; |
aa650bbd AS |
893 | if (!acpi_device_dir(device)) |
894 | return; | |
895 | for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i) | |
896 | remove_proc_entry(acpi_battery_file[i].name, | |
1da177e4 | 897 | acpi_device_dir(device)); |
1da177e4 | 898 | |
aa650bbd AS |
899 | remove_proc_entry(acpi_device_bid(device), acpi_battery_dir); |
900 | acpi_device_dir(device) = NULL; | |
1da177e4 LT |
901 | } |
902 | ||
d7380965 | 903 | #endif |
3e58ea0d | 904 | |
1da177e4 LT |
905 | /* -------------------------------------------------------------------------- |
906 | Driver Interface | |
907 | -------------------------------------------------------------------------- */ | |
908 | ||
d9406691 | 909 | static void acpi_battery_notify(struct acpi_device *device, u32 event) |
1da177e4 | 910 | { |
d9406691 | 911 | struct acpi_battery *battery = acpi_driver_data(device); |
153e500f | 912 | struct device *old; |
d9406691 | 913 | |
1da177e4 | 914 | if (!battery) |
d550d98d | 915 | return; |
153e500f | 916 | old = battery->bat.dev; |
f1d4661a AS |
917 | acpi_battery_update(battery); |
918 | acpi_bus_generate_proc_event(device, event, | |
919 | acpi_battery_present(battery)); | |
920 | acpi_bus_generate_netlink_event(device->pnp.device_class, | |
0794469d | 921 | dev_name(&device->dev), event, |
9ea7d575 | 922 | acpi_battery_present(battery)); |
2345baf4 | 923 | /* acpi_battery_update could remove power_supply object */ |
153e500f | 924 | if (old && battery->bat.dev) |
f79e1cec | 925 | power_supply_changed(&battery->bat); |
1da177e4 LT |
926 | } |
927 | ||
4be44fcd | 928 | static int acpi_battery_add(struct acpi_device *device) |
1da177e4 | 929 | { |
4be44fcd | 930 | int result = 0; |
4be44fcd | 931 | struct acpi_battery *battery = NULL; |
c67fcd67 | 932 | acpi_handle handle; |
1da177e4 | 933 | if (!device) |
d550d98d | 934 | return -EINVAL; |
36bcbec7 | 935 | battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL); |
1da177e4 | 936 | if (!battery) |
d550d98d | 937 | return -ENOMEM; |
145def84 | 938 | battery->device = device; |
1da177e4 LT |
939 | strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME); |
940 | strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS); | |
db89b4f0 | 941 | device->driver_data = battery; |
038fdea2 | 942 | mutex_init(&battery->lock); |
c67fcd67 AS |
943 | if (ACPI_SUCCESS(acpi_get_handle(battery->device->handle, |
944 | "_BIX", &handle))) | |
945 | set_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags); | |
f1d4661a | 946 | acpi_battery_update(battery); |
fdcedbba | 947 | #ifdef CONFIG_ACPI_PROCFS_POWER |
1da177e4 | 948 | result = acpi_battery_add_fs(device); |
d7380965 | 949 | #endif |
586caae3 LB |
950 | if (!result) { |
951 | printk(KERN_INFO PREFIX "%s Slot [%s] (battery %s)\n", | |
952 | ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device), | |
953 | device->status.battery_present ? "present" : "absent"); | |
954 | } else { | |
fdcedbba | 955 | #ifdef CONFIG_ACPI_PROCFS_POWER |
1da177e4 | 956 | acpi_battery_remove_fs(device); |
d7380965 | 957 | #endif |
1da177e4 LT |
958 | kfree(battery); |
959 | } | |
d550d98d | 960 | return result; |
1da177e4 LT |
961 | } |
962 | ||
4be44fcd | 963 | static int acpi_battery_remove(struct acpi_device *device, int type) |
1da177e4 | 964 | { |
4be44fcd | 965 | struct acpi_battery *battery = NULL; |
1da177e4 | 966 | |
1da177e4 | 967 | if (!device || !acpi_driver_data(device)) |
d550d98d | 968 | return -EINVAL; |
50dd0969 | 969 | battery = acpi_driver_data(device); |
fdcedbba | 970 | #ifdef CONFIG_ACPI_PROCFS_POWER |
1da177e4 | 971 | acpi_battery_remove_fs(device); |
d7380965 | 972 | #endif |
508df92d | 973 | sysfs_remove_battery(battery); |
038fdea2 | 974 | mutex_destroy(&battery->lock); |
1da177e4 | 975 | kfree(battery); |
d550d98d | 976 | return 0; |
1da177e4 LT |
977 | } |
978 | ||
34c4415a | 979 | /* this is needed to learn about changes made in suspended state */ |
5d9464a4 | 980 | static int acpi_battery_resume(struct acpi_device *device) |
34c4415a JK |
981 | { |
982 | struct acpi_battery *battery; | |
34c4415a JK |
983 | if (!device) |
984 | return -EINVAL; | |
f1d4661a AS |
985 | battery = acpi_driver_data(device); |
986 | battery->update_time = 0; | |
508df92d | 987 | acpi_battery_update(battery); |
b6ce4083 | 988 | return 0; |
34c4415a JK |
989 | } |
990 | ||
aa650bbd AS |
991 | static struct acpi_driver acpi_battery_driver = { |
992 | .name = "battery", | |
993 | .class = ACPI_BATTERY_CLASS, | |
994 | .ids = battery_device_ids, | |
d9406691 | 995 | .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS, |
aa650bbd AS |
996 | .ops = { |
997 | .add = acpi_battery_add, | |
998 | .resume = acpi_battery_resume, | |
999 | .remove = acpi_battery_remove, | |
d9406691 | 1000 | .notify = acpi_battery_notify, |
aa650bbd AS |
1001 | }, |
1002 | }; | |
1003 | ||
b0cbc861 | 1004 | static void __init acpi_battery_init_async(void *unused, async_cookie_t cookie) |
1da177e4 | 1005 | { |
4d8316d5 | 1006 | if (acpi_disabled) |
0f66af53 | 1007 | return; |
fdcedbba | 1008 | #ifdef CONFIG_ACPI_PROCFS_POWER |
3f86b832 | 1009 | acpi_battery_dir = acpi_lock_battery_dir(); |
1da177e4 | 1010 | if (!acpi_battery_dir) |
0f66af53 | 1011 | return; |
d7380965 | 1012 | #endif |
aa650bbd | 1013 | if (acpi_bus_register_driver(&acpi_battery_driver) < 0) { |
fdcedbba | 1014 | #ifdef CONFIG_ACPI_PROCFS_POWER |
3f86b832 | 1015 | acpi_unlock_battery_dir(acpi_battery_dir); |
d7380965 | 1016 | #endif |
0f66af53 | 1017 | return; |
1da177e4 | 1018 | } |
0f66af53 AV |
1019 | return; |
1020 | } | |
1021 | ||
1022 | static int __init acpi_battery_init(void) | |
1023 | { | |
1024 | async_schedule(acpi_battery_init_async, NULL); | |
d550d98d | 1025 | return 0; |
1da177e4 LT |
1026 | } |
1027 | ||
4be44fcd | 1028 | static void __exit acpi_battery_exit(void) |
1da177e4 | 1029 | { |
1da177e4 | 1030 | acpi_bus_unregister_driver(&acpi_battery_driver); |
fdcedbba | 1031 | #ifdef CONFIG_ACPI_PROCFS_POWER |
3f86b832 | 1032 | acpi_unlock_battery_dir(acpi_battery_dir); |
d7380965 | 1033 | #endif |
1da177e4 LT |
1034 | } |
1035 | ||
1da177e4 LT |
1036 | module_init(acpi_battery_init); |
1037 | module_exit(acpi_battery_exit); |