]>
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> |
d7380965 AS |
33 | |
34 | #ifdef CONFIG_ACPI_PROCFS | |
1da177e4 LT |
35 | #include <linux/proc_fs.h> |
36 | #include <linux/seq_file.h> | |
37 | #include <asm/uaccess.h> | |
d7380965 | 38 | #endif |
1da177e4 LT |
39 | |
40 | #include <acpi/acpi_bus.h> | |
41 | #include <acpi/acpi_drivers.h> | |
42 | ||
d7380965 AS |
43 | #include <linux/power_supply.h> |
44 | ||
1da177e4 LT |
45 | #define ACPI_BATTERY_VALUE_UNKNOWN 0xFFFFFFFF |
46 | ||
1da177e4 LT |
47 | #define ACPI_BATTERY_COMPONENT 0x00040000 |
48 | #define ACPI_BATTERY_CLASS "battery" | |
1da177e4 | 49 | #define ACPI_BATTERY_DEVICE_NAME "Battery" |
1da177e4 LT |
50 | #define ACPI_BATTERY_NOTIFY_STATUS 0x80 |
51 | #define ACPI_BATTERY_NOTIFY_INFO 0x81 | |
1da177e4 | 52 | |
1da177e4 | 53 | #define _COMPONENT ACPI_BATTERY_COMPONENT |
b6ce4083 | 54 | |
f52fd66d | 55 | ACPI_MODULE_NAME("battery"); |
1da177e4 | 56 | |
f52fd66d | 57 | MODULE_AUTHOR("Paul Diefenbaugh"); |
aa650bbd | 58 | MODULE_AUTHOR("Alexey Starikovskiy <astarikovskiy@suse.de>"); |
7cda93e0 | 59 | MODULE_DESCRIPTION("ACPI Battery Driver"); |
1da177e4 LT |
60 | MODULE_LICENSE("GPL"); |
61 | ||
f1d4661a AS |
62 | static unsigned int cache_time = 1000; |
63 | module_param(cache_time, uint, 0644); | |
64 | MODULE_PARM_DESC(cache_time, "cache time in milliseconds"); | |
b6ce4083 | 65 | |
d7380965 | 66 | #ifdef CONFIG_ACPI_PROCFS |
3f86b832 RT |
67 | extern struct proc_dir_entry *acpi_lock_battery_dir(void); |
68 | extern void *acpi_unlock_battery_dir(struct proc_dir_entry *acpi_battery_dir); | |
69 | ||
d7380965 AS |
70 | enum acpi_battery_files { |
71 | info_tag = 0, | |
72 | state_tag, | |
73 | alarm_tag, | |
74 | ACPI_BATTERY_NUMFILES, | |
75 | }; | |
76 | ||
77 | #endif | |
78 | ||
1ba90e3a TR |
79 | static const struct acpi_device_id battery_device_ids[] = { |
80 | {"PNP0C0A", 0}, | |
81 | {"", 0}, | |
82 | }; | |
1ba90e3a | 83 | |
aa650bbd | 84 | MODULE_DEVICE_TABLE(acpi, battery_device_ids); |
1da177e4 | 85 | |
78490d82 | 86 | |
1da177e4 | 87 | struct acpi_battery { |
038fdea2 | 88 | struct mutex lock; |
d7380965 | 89 | struct power_supply bat; |
f1d4661a AS |
90 | struct acpi_device *device; |
91 | unsigned long update_time; | |
d7380965 AS |
92 | int current_now; |
93 | int capacity_now; | |
94 | int voltage_now; | |
038fdea2 | 95 | int design_capacity; |
d7380965 | 96 | int full_charge_capacity; |
038fdea2 AS |
97 | int technology; |
98 | int design_voltage; | |
99 | int design_capacity_warning; | |
100 | int design_capacity_low; | |
101 | int capacity_granularity_1; | |
102 | int capacity_granularity_2; | |
f1d4661a | 103 | int alarm; |
038fdea2 AS |
104 | char model_number[32]; |
105 | char serial_number[32]; | |
106 | char type[32]; | |
107 | char oem_info[32]; | |
f1d4661a AS |
108 | int state; |
109 | int power_unit; | |
038fdea2 | 110 | u8 alarm_present; |
1da177e4 LT |
111 | }; |
112 | ||
d7380965 AS |
113 | #define to_acpi_battery(x) container_of(x, struct acpi_battery, bat); |
114 | ||
78490d82 | 115 | inline int acpi_battery_present(struct acpi_battery *battery) |
b6ce4083 | 116 | { |
78490d82 AS |
117 | return battery->device->status.battery_present; |
118 | } | |
038fdea2 | 119 | |
d7380965 AS |
120 | static int acpi_battery_technology(struct acpi_battery *battery) |
121 | { | |
122 | if (!strcasecmp("NiCd", battery->type)) | |
123 | return POWER_SUPPLY_TECHNOLOGY_NiCd; | |
124 | if (!strcasecmp("NiMH", battery->type)) | |
125 | return POWER_SUPPLY_TECHNOLOGY_NiMH; | |
126 | if (!strcasecmp("LION", battery->type)) | |
127 | return POWER_SUPPLY_TECHNOLOGY_LION; | |
0bde7eee AS |
128 | if (!strcasecmp("LI-ION", battery->type)) |
129 | return POWER_SUPPLY_TECHNOLOGY_LION; | |
d7380965 AS |
130 | if (!strcasecmp("LiP", battery->type)) |
131 | return POWER_SUPPLY_TECHNOLOGY_LIPO; | |
132 | return POWER_SUPPLY_TECHNOLOGY_UNKNOWN; | |
133 | } | |
134 | ||
9104476e | 135 | static int acpi_battery_get_state(struct acpi_battery *battery); |
b19073a0 | 136 | |
d7380965 AS |
137 | static int acpi_battery_get_property(struct power_supply *psy, |
138 | enum power_supply_property psp, | |
139 | union power_supply_propval *val) | |
140 | { | |
141 | struct acpi_battery *battery = to_acpi_battery(psy); | |
142 | ||
9104476e AS |
143 | if (acpi_battery_present(battery)) { |
144 | /* run battery update only if it is present */ | |
145 | acpi_battery_get_state(battery); | |
146 | } else if (psp != POWER_SUPPLY_PROP_PRESENT) | |
d7380965 AS |
147 | return -ENODEV; |
148 | switch (psp) { | |
149 | case POWER_SUPPLY_PROP_STATUS: | |
150 | if (battery->state & 0x01) | |
151 | val->intval = POWER_SUPPLY_STATUS_DISCHARGING; | |
152 | else if (battery->state & 0x02) | |
153 | val->intval = POWER_SUPPLY_STATUS_CHARGING; | |
154 | else if (battery->state == 0) | |
155 | val->intval = POWER_SUPPLY_STATUS_FULL; | |
156 | break; | |
157 | case POWER_SUPPLY_PROP_PRESENT: | |
158 | val->intval = acpi_battery_present(battery); | |
159 | break; | |
160 | case POWER_SUPPLY_PROP_TECHNOLOGY: | |
161 | val->intval = acpi_battery_technology(battery); | |
162 | break; | |
163 | case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN: | |
164 | val->intval = battery->design_voltage * 1000; | |
165 | break; | |
166 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: | |
167 | val->intval = battery->voltage_now * 1000; | |
168 | break; | |
169 | case POWER_SUPPLY_PROP_CURRENT_NOW: | |
170 | val->intval = battery->current_now * 1000; | |
171 | break; | |
172 | case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: | |
173 | case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN: | |
174 | val->intval = battery->design_capacity * 1000; | |
175 | break; | |
176 | case POWER_SUPPLY_PROP_CHARGE_FULL: | |
177 | case POWER_SUPPLY_PROP_ENERGY_FULL: | |
178 | val->intval = battery->full_charge_capacity * 1000; | |
179 | break; | |
180 | case POWER_SUPPLY_PROP_CHARGE_NOW: | |
181 | case POWER_SUPPLY_PROP_ENERGY_NOW: | |
182 | val->intval = battery->capacity_now * 1000; | |
183 | break; | |
184 | case POWER_SUPPLY_PROP_MODEL_NAME: | |
185 | val->strval = battery->model_number; | |
186 | break; | |
187 | case POWER_SUPPLY_PROP_MANUFACTURER: | |
188 | val->strval = battery->oem_info; | |
189 | break; | |
190 | default: | |
191 | return -EINVAL; | |
192 | } | |
193 | return 0; | |
194 | } | |
195 | ||
196 | static enum power_supply_property charge_battery_props[] = { | |
197 | POWER_SUPPLY_PROP_STATUS, | |
198 | POWER_SUPPLY_PROP_PRESENT, | |
199 | POWER_SUPPLY_PROP_TECHNOLOGY, | |
200 | POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, | |
201 | POWER_SUPPLY_PROP_VOLTAGE_NOW, | |
202 | POWER_SUPPLY_PROP_CURRENT_NOW, | |
203 | POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, | |
204 | POWER_SUPPLY_PROP_CHARGE_FULL, | |
205 | POWER_SUPPLY_PROP_CHARGE_NOW, | |
206 | POWER_SUPPLY_PROP_MODEL_NAME, | |
207 | POWER_SUPPLY_PROP_MANUFACTURER, | |
208 | }; | |
209 | ||
210 | static enum power_supply_property energy_battery_props[] = { | |
211 | POWER_SUPPLY_PROP_STATUS, | |
212 | POWER_SUPPLY_PROP_PRESENT, | |
213 | POWER_SUPPLY_PROP_TECHNOLOGY, | |
214 | POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, | |
215 | POWER_SUPPLY_PROP_VOLTAGE_NOW, | |
216 | POWER_SUPPLY_PROP_CURRENT_NOW, | |
217 | POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, | |
218 | POWER_SUPPLY_PROP_ENERGY_FULL, | |
219 | POWER_SUPPLY_PROP_ENERGY_NOW, | |
220 | POWER_SUPPLY_PROP_MODEL_NAME, | |
221 | POWER_SUPPLY_PROP_MANUFACTURER, | |
222 | }; | |
223 | ||
224 | #ifdef CONFIG_ACPI_PROCFS | |
f1d4661a | 225 | inline char *acpi_battery_units(struct acpi_battery *battery) |
78490d82 | 226 | { |
f1d4661a | 227 | return (battery->power_unit)?"mA":"mW"; |
b6ce4083 | 228 | } |
d7380965 | 229 | #endif |
b6ce4083 | 230 | |
78490d82 AS |
231 | /* -------------------------------------------------------------------------- |
232 | Battery Management | |
233 | -------------------------------------------------------------------------- */ | |
038fdea2 AS |
234 | struct acpi_offsets { |
235 | size_t offset; /* offset inside struct acpi_sbs_battery */ | |
236 | u8 mode; /* int or string? */ | |
237 | }; | |
b6ce4083 | 238 | |
038fdea2 AS |
239 | static struct acpi_offsets state_offsets[] = { |
240 | {offsetof(struct acpi_battery, state), 0}, | |
d7380965 AS |
241 | {offsetof(struct acpi_battery, current_now), 0}, |
242 | {offsetof(struct acpi_battery, capacity_now), 0}, | |
243 | {offsetof(struct acpi_battery, voltage_now), 0}, | |
038fdea2 | 244 | }; |
b6ce4083 | 245 | |
038fdea2 AS |
246 | static struct acpi_offsets info_offsets[] = { |
247 | {offsetof(struct acpi_battery, power_unit), 0}, | |
248 | {offsetof(struct acpi_battery, design_capacity), 0}, | |
d7380965 | 249 | {offsetof(struct acpi_battery, full_charge_capacity), 0}, |
038fdea2 AS |
250 | {offsetof(struct acpi_battery, technology), 0}, |
251 | {offsetof(struct acpi_battery, design_voltage), 0}, | |
252 | {offsetof(struct acpi_battery, design_capacity_warning), 0}, | |
253 | {offsetof(struct acpi_battery, design_capacity_low), 0}, | |
254 | {offsetof(struct acpi_battery, capacity_granularity_1), 0}, | |
255 | {offsetof(struct acpi_battery, capacity_granularity_2), 0}, | |
256 | {offsetof(struct acpi_battery, model_number), 1}, | |
257 | {offsetof(struct acpi_battery, serial_number), 1}, | |
258 | {offsetof(struct acpi_battery, type), 1}, | |
259 | {offsetof(struct acpi_battery, oem_info), 1}, | |
260 | }; | |
b6ce4083 | 261 | |
038fdea2 AS |
262 | static int extract_package(struct acpi_battery *battery, |
263 | union acpi_object *package, | |
264 | struct acpi_offsets *offsets, int num) | |
265 | { | |
106449e8 | 266 | int i; |
038fdea2 AS |
267 | union acpi_object *element; |
268 | if (package->type != ACPI_TYPE_PACKAGE) | |
269 | return -EFAULT; | |
270 | for (i = 0; i < num; ++i) { | |
271 | if (package->package.count <= i) | |
272 | return -EFAULT; | |
273 | element = &package->package.elements[i]; | |
274 | if (offsets[i].mode) { | |
106449e8 AS |
275 | u8 *ptr = (u8 *)battery + offsets[i].offset; |
276 | if (element->type == ACPI_TYPE_STRING || | |
277 | element->type == ACPI_TYPE_BUFFER) | |
278 | strncpy(ptr, element->string.pointer, 32); | |
279 | else if (element->type == ACPI_TYPE_INTEGER) { | |
280 | strncpy(ptr, (u8 *)&element->integer.value, | |
281 | sizeof(acpi_integer)); | |
282 | ptr[sizeof(acpi_integer)] = 0; | |
283 | } else return -EFAULT; | |
038fdea2 | 284 | } else { |
106449e8 AS |
285 | if (element->type == ACPI_TYPE_INTEGER) { |
286 | int *x = (int *)((u8 *)battery + | |
287 | offsets[i].offset); | |
288 | *x = element->integer.value; | |
289 | } else return -EFAULT; | |
038fdea2 | 290 | } |
b6ce4083 | 291 | } |
b6ce4083 VL |
292 | return 0; |
293 | } | |
294 | ||
295 | static int acpi_battery_get_status(struct acpi_battery *battery) | |
296 | { | |
aa650bbd | 297 | if (acpi_bus_get_status(battery->device)) { |
b6ce4083 VL |
298 | ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Evaluating _STA")); |
299 | return -ENODEV; | |
300 | } | |
aa650bbd | 301 | return 0; |
b6ce4083 VL |
302 | } |
303 | ||
304 | static int acpi_battery_get_info(struct acpi_battery *battery) | |
1da177e4 | 305 | { |
aa650bbd | 306 | int result = -EFAULT; |
4be44fcd LB |
307 | acpi_status status = 0; |
308 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; | |
1da177e4 | 309 | |
b6ce4083 VL |
310 | if (!acpi_battery_present(battery)) |
311 | return 0; | |
038fdea2 | 312 | mutex_lock(&battery->lock); |
f1d4661a | 313 | status = acpi_evaluate_object(battery->device->handle, "_BIF", |
038fdea2 AS |
314 | NULL, &buffer); |
315 | mutex_unlock(&battery->lock); | |
aa650bbd | 316 | |
1da177e4 | 317 | if (ACPI_FAILURE(status)) { |
a6fc6720 | 318 | ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BIF")); |
d550d98d | 319 | return -ENODEV; |
1da177e4 | 320 | } |
aa650bbd | 321 | |
038fdea2 AS |
322 | result = extract_package(battery, buffer.pointer, |
323 | info_offsets, ARRAY_SIZE(info_offsets)); | |
78490d82 | 324 | kfree(buffer.pointer); |
d550d98d | 325 | return result; |
1da177e4 LT |
326 | } |
327 | ||
b6ce4083 | 328 | static int acpi_battery_get_state(struct acpi_battery *battery) |
1da177e4 | 329 | { |
4be44fcd LB |
330 | int result = 0; |
331 | acpi_status status = 0; | |
332 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; | |
1da177e4 | 333 | |
b6ce4083 VL |
334 | if (!acpi_battery_present(battery)) |
335 | return 0; | |
1da177e4 | 336 | |
f1d4661a AS |
337 | if (battery->update_time && |
338 | time_before(jiffies, battery->update_time + | |
339 | msecs_to_jiffies(cache_time))) | |
340 | return 0; | |
341 | ||
038fdea2 | 342 | mutex_lock(&battery->lock); |
f1d4661a | 343 | status = acpi_evaluate_object(battery->device->handle, "_BST", |
038fdea2 AS |
344 | NULL, &buffer); |
345 | mutex_unlock(&battery->lock); | |
5b31d895 | 346 | |
1da177e4 | 347 | if (ACPI_FAILURE(status)) { |
a6fc6720 | 348 | ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BST")); |
d550d98d | 349 | return -ENODEV; |
1da177e4 | 350 | } |
aa650bbd | 351 | |
038fdea2 AS |
352 | result = extract_package(battery, buffer.pointer, |
353 | state_offsets, ARRAY_SIZE(state_offsets)); | |
f1d4661a | 354 | battery->update_time = jiffies; |
78490d82 | 355 | kfree(buffer.pointer); |
b6ce4083 VL |
356 | return result; |
357 | } | |
1da177e4 | 358 | |
aa650bbd | 359 | static int acpi_battery_set_alarm(struct acpi_battery *battery) |
1da177e4 | 360 | { |
4be44fcd | 361 | acpi_status status = 0; |
aa650bbd | 362 | union acpi_object arg0 = { .type = ACPI_TYPE_INTEGER }; |
4be44fcd | 363 | struct acpi_object_list arg_list = { 1, &arg0 }; |
1da177e4 | 364 | |
aa650bbd | 365 | if (!acpi_battery_present(battery)|| !battery->alarm_present) |
d550d98d | 366 | return -ENODEV; |
1da177e4 | 367 | |
aa650bbd | 368 | arg0.integer.value = battery->alarm; |
1da177e4 | 369 | |
038fdea2 | 370 | mutex_lock(&battery->lock); |
f1d4661a AS |
371 | status = acpi_evaluate_object(battery->device->handle, "_BTP", |
372 | &arg_list, NULL); | |
038fdea2 | 373 | mutex_unlock(&battery->lock); |
aa650bbd | 374 | |
1da177e4 | 375 | if (ACPI_FAILURE(status)) |
d550d98d | 376 | return -ENODEV; |
1da177e4 | 377 | |
aa650bbd | 378 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", battery->alarm)); |
d550d98d | 379 | return 0; |
1da177e4 LT |
380 | } |
381 | ||
b6ce4083 | 382 | static int acpi_battery_init_alarm(struct acpi_battery *battery) |
1da177e4 | 383 | { |
4be44fcd LB |
384 | acpi_status status = AE_OK; |
385 | acpi_handle handle = NULL; | |
4be44fcd | 386 | |
b6ce4083 | 387 | /* See if alarms are supported, and if so, set default */ |
f1d4661a AS |
388 | status = acpi_get_handle(battery->device->handle, "_BTP", &handle); |
389 | if (ACPI_FAILURE(status)) { | |
038fdea2 | 390 | battery->alarm_present = 0; |
f1d4661a | 391 | return 0; |
b6ce4083 | 392 | } |
f1d4661a AS |
393 | battery->alarm_present = 1; |
394 | if (!battery->alarm) | |
395 | battery->alarm = battery->design_capacity_warning; | |
aa650bbd | 396 | return acpi_battery_set_alarm(battery); |
b6ce4083 | 397 | } |
1da177e4 | 398 | |
508df92d AB |
399 | static ssize_t acpi_battery_alarm_show(struct device *dev, |
400 | struct device_attribute *attr, | |
401 | char *buf) | |
402 | { | |
403 | struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev)); | |
404 | return sprintf(buf, "%d\n", battery->alarm * 1000); | |
405 | } | |
406 | ||
407 | static ssize_t acpi_battery_alarm_store(struct device *dev, | |
408 | struct device_attribute *attr, | |
409 | const char *buf, size_t count) | |
410 | { | |
411 | unsigned long x; | |
412 | struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev)); | |
413 | if (sscanf(buf, "%ld\n", &x) == 1) | |
414 | battery->alarm = x/1000; | |
415 | if (acpi_battery_present(battery)) | |
416 | acpi_battery_set_alarm(battery); | |
417 | return count; | |
418 | } | |
419 | ||
420 | static struct device_attribute alarm_attr = { | |
421 | .attr = {.name = "alarm", .mode = 0644, .owner = THIS_MODULE}, | |
422 | .show = acpi_battery_alarm_show, | |
423 | .store = acpi_battery_alarm_store, | |
424 | }; | |
425 | ||
426 | static int sysfs_add_battery(struct acpi_battery *battery) | |
427 | { | |
428 | int result; | |
429 | ||
430 | battery->update_time = 0; | |
431 | result = acpi_battery_get_info(battery); | |
432 | acpi_battery_init_alarm(battery); | |
433 | if (result) | |
434 | return result; | |
435 | if (battery->power_unit) { | |
436 | battery->bat.properties = charge_battery_props; | |
437 | battery->bat.num_properties = | |
438 | ARRAY_SIZE(charge_battery_props); | |
439 | } else { | |
440 | battery->bat.properties = energy_battery_props; | |
441 | battery->bat.num_properties = | |
442 | ARRAY_SIZE(energy_battery_props); | |
443 | } | |
444 | ||
445 | battery->bat.name = acpi_device_bid(battery->device); | |
446 | battery->bat.type = POWER_SUPPLY_TYPE_BATTERY; | |
447 | battery->bat.get_property = acpi_battery_get_property; | |
448 | ||
449 | result = power_supply_register(&battery->device->dev, &battery->bat); | |
450 | if (result) | |
451 | return result; | |
452 | return device_create_file(battery->bat.dev, &alarm_attr); | |
453 | } | |
454 | ||
455 | static void sysfs_remove_battery(struct acpi_battery *battery) | |
456 | { | |
457 | if (!battery->bat.dev) | |
458 | return; | |
459 | device_remove_file(battery->bat.dev, &alarm_attr); | |
460 | power_supply_unregister(&battery->bat); | |
9104476e | 461 | battery->bat.dev = NULL; |
508df92d AB |
462 | } |
463 | ||
f1d4661a | 464 | static int acpi_battery_update(struct acpi_battery *battery) |
b6ce4083 | 465 | { |
f1d4661a | 466 | int result = acpi_battery_get_status(battery); |
508df92d | 467 | if (result) |
b6ce4083 | 468 | return result; |
508df92d AB |
469 | if (!acpi_battery_present(battery)) { |
470 | sysfs_remove_battery(battery); | |
471 | return 0; | |
b6ce4083 | 472 | } |
508df92d AB |
473 | if (!battery->bat.dev) |
474 | sysfs_add_battery(battery); | |
f1d4661a | 475 | return acpi_battery_get_state(battery); |
4bd35cdb VL |
476 | } |
477 | ||
1da177e4 LT |
478 | /* -------------------------------------------------------------------------- |
479 | FS Interface (/proc) | |
480 | -------------------------------------------------------------------------- */ | |
481 | ||
d7380965 | 482 | #ifdef CONFIG_ACPI_PROCFS |
4be44fcd | 483 | static struct proc_dir_entry *acpi_battery_dir; |
b6ce4083 | 484 | |
78490d82 | 485 | static int acpi_battery_print_info(struct seq_file *seq, int result) |
1da177e4 | 486 | { |
50dd0969 | 487 | struct acpi_battery *battery = seq->private; |
1da177e4 | 488 | |
b6ce4083 | 489 | if (result) |
1da177e4 LT |
490 | goto end; |
491 | ||
aa650bbd AS |
492 | seq_printf(seq, "present: %s\n", |
493 | acpi_battery_present(battery)?"yes":"no"); | |
494 | if (!acpi_battery_present(battery)) | |
1da177e4 | 495 | goto end; |
038fdea2 | 496 | if (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN) |
1da177e4 LT |
497 | seq_printf(seq, "design capacity: unknown\n"); |
498 | else | |
499 | seq_printf(seq, "design capacity: %d %sh\n", | |
aa650bbd AS |
500 | battery->design_capacity, |
501 | acpi_battery_units(battery)); | |
1da177e4 | 502 | |
d7380965 | 503 | if (battery->full_charge_capacity == ACPI_BATTERY_VALUE_UNKNOWN) |
1da177e4 LT |
504 | seq_printf(seq, "last full capacity: unknown\n"); |
505 | else | |
506 | seq_printf(seq, "last full capacity: %d %sh\n", | |
d7380965 | 507 | battery->full_charge_capacity, |
aa650bbd AS |
508 | acpi_battery_units(battery)); |
509 | ||
510 | seq_printf(seq, "battery technology: %srechargeable\n", | |
511 | (!battery->technology)?"non-":""); | |
1da177e4 | 512 | |
038fdea2 | 513 | if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN) |
1da177e4 LT |
514 | seq_printf(seq, "design voltage: unknown\n"); |
515 | else | |
516 | seq_printf(seq, "design voltage: %d mV\n", | |
aa650bbd | 517 | battery->design_voltage); |
1da177e4 | 518 | seq_printf(seq, "design capacity warning: %d %sh\n", |
aa650bbd AS |
519 | battery->design_capacity_warning, |
520 | acpi_battery_units(battery)); | |
1da177e4 | 521 | seq_printf(seq, "design capacity low: %d %sh\n", |
aa650bbd AS |
522 | battery->design_capacity_low, |
523 | acpi_battery_units(battery)); | |
1da177e4 | 524 | seq_printf(seq, "capacity granularity 1: %d %sh\n", |
aa650bbd AS |
525 | battery->capacity_granularity_1, |
526 | acpi_battery_units(battery)); | |
1da177e4 | 527 | seq_printf(seq, "capacity granularity 2: %d %sh\n", |
aa650bbd AS |
528 | battery->capacity_granularity_2, |
529 | acpi_battery_units(battery)); | |
038fdea2 AS |
530 | seq_printf(seq, "model number: %s\n", battery->model_number); |
531 | seq_printf(seq, "serial number: %s\n", battery->serial_number); | |
532 | seq_printf(seq, "battery type: %s\n", battery->type); | |
533 | seq_printf(seq, "OEM info: %s\n", battery->oem_info); | |
4be44fcd | 534 | end: |
b6ce4083 VL |
535 | if (result) |
536 | seq_printf(seq, "ERROR: Unable to read battery info\n"); | |
b6ce4083 VL |
537 | return result; |
538 | } | |
539 | ||
78490d82 | 540 | static int acpi_battery_print_state(struct seq_file *seq, int result) |
1da177e4 | 541 | { |
50dd0969 | 542 | struct acpi_battery *battery = seq->private; |
1da177e4 | 543 | |
b6ce4083 | 544 | if (result) |
1da177e4 LT |
545 | goto end; |
546 | ||
aa650bbd AS |
547 | seq_printf(seq, "present: %s\n", |
548 | acpi_battery_present(battery)?"yes":"no"); | |
549 | if (!acpi_battery_present(battery)) | |
1da177e4 | 550 | goto end; |
b6ce4083 | 551 | |
aa650bbd AS |
552 | seq_printf(seq, "capacity state: %s\n", |
553 | (battery->state & 0x04)?"critical":"ok"); | |
554 | if ((battery->state & 0x01) && (battery->state & 0x02)) | |
4be44fcd LB |
555 | seq_printf(seq, |
556 | "charging state: charging/discharging\n"); | |
aa650bbd | 557 | else if (battery->state & 0x01) |
1da177e4 | 558 | seq_printf(seq, "charging state: discharging\n"); |
038fdea2 | 559 | else if (battery->state & 0x02) |
1da177e4 | 560 | seq_printf(seq, "charging state: charging\n"); |
aa650bbd | 561 | else |
1da177e4 | 562 | seq_printf(seq, "charging state: charged\n"); |
1da177e4 | 563 | |
d7380965 | 564 | if (battery->current_now == ACPI_BATTERY_VALUE_UNKNOWN) |
1da177e4 LT |
565 | seq_printf(seq, "present rate: unknown\n"); |
566 | else | |
567 | seq_printf(seq, "present rate: %d %s\n", | |
d7380965 | 568 | battery->current_now, acpi_battery_units(battery)); |
1da177e4 | 569 | |
d7380965 | 570 | if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN) |
1da177e4 LT |
571 | seq_printf(seq, "remaining capacity: unknown\n"); |
572 | else | |
573 | seq_printf(seq, "remaining capacity: %d %sh\n", | |
d7380965 AS |
574 | battery->capacity_now, acpi_battery_units(battery)); |
575 | if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN) | |
1da177e4 LT |
576 | seq_printf(seq, "present voltage: unknown\n"); |
577 | else | |
578 | seq_printf(seq, "present voltage: %d mV\n", | |
d7380965 | 579 | battery->voltage_now); |
4be44fcd | 580 | end: |
aa650bbd | 581 | if (result) |
b6ce4083 | 582 | seq_printf(seq, "ERROR: Unable to read battery state\n"); |
b6ce4083 VL |
583 | |
584 | return result; | |
585 | } | |
586 | ||
78490d82 | 587 | static int acpi_battery_print_alarm(struct seq_file *seq, int result) |
1da177e4 | 588 | { |
50dd0969 | 589 | struct acpi_battery *battery = seq->private; |
1da177e4 | 590 | |
b6ce4083 | 591 | if (result) |
1da177e4 LT |
592 | goto end; |
593 | ||
b6ce4083 | 594 | if (!acpi_battery_present(battery)) { |
1da177e4 LT |
595 | seq_printf(seq, "present: no\n"); |
596 | goto end; | |
597 | } | |
1da177e4 LT |
598 | seq_printf(seq, "alarm: "); |
599 | if (!battery->alarm) | |
600 | seq_printf(seq, "unsupported\n"); | |
601 | else | |
aa650bbd AS |
602 | seq_printf(seq, "%u %sh\n", battery->alarm, |
603 | acpi_battery_units(battery)); | |
4be44fcd | 604 | end: |
b6ce4083 VL |
605 | if (result) |
606 | seq_printf(seq, "ERROR: Unable to read battery alarm\n"); | |
b6ce4083 VL |
607 | return result; |
608 | } | |
609 | ||
f1d4661a AS |
610 | static ssize_t acpi_battery_write_alarm(struct file *file, |
611 | const char __user * buffer, | |
612 | size_t count, loff_t * ppos) | |
1da177e4 | 613 | { |
4be44fcd LB |
614 | int result = 0; |
615 | char alarm_string[12] = { '\0' }; | |
50dd0969 JE |
616 | struct seq_file *m = file->private_data; |
617 | struct acpi_battery *battery = m->private; | |
1da177e4 | 618 | |
1da177e4 | 619 | if (!battery || (count > sizeof(alarm_string) - 1)) |
d550d98d | 620 | return -EINVAL; |
b6ce4083 VL |
621 | if (!acpi_battery_present(battery)) { |
622 | result = -ENODEV; | |
623 | goto end; | |
624 | } | |
b6ce4083 VL |
625 | if (copy_from_user(alarm_string, buffer, count)) { |
626 | result = -EFAULT; | |
627 | goto end; | |
628 | } | |
1da177e4 | 629 | alarm_string[count] = '\0'; |
f1d4661a | 630 | battery->alarm = simple_strtol(alarm_string, NULL, 0); |
aa650bbd | 631 | result = acpi_battery_set_alarm(battery); |
b6ce4083 | 632 | end: |
b6ce4083 | 633 | if (!result) |
f1d4661a | 634 | return count; |
78490d82 AS |
635 | return result; |
636 | } | |
637 | ||
638 | typedef int(*print_func)(struct seq_file *seq, int result); | |
aa650bbd AS |
639 | |
640 | static print_func acpi_print_funcs[ACPI_BATTERY_NUMFILES] = { | |
641 | acpi_battery_print_info, | |
642 | acpi_battery_print_state, | |
643 | acpi_battery_print_alarm, | |
78490d82 | 644 | }; |
b6ce4083 | 645 | |
78490d82 AS |
646 | static int acpi_battery_read(int fid, struct seq_file *seq) |
647 | { | |
648 | struct acpi_battery *battery = seq->private; | |
f1d4661a | 649 | int result = acpi_battery_update(battery); |
aa650bbd | 650 | return acpi_print_funcs[fid](seq, result); |
78490d82 AS |
651 | } |
652 | ||
aa650bbd AS |
653 | #define DECLARE_FILE_FUNCTIONS(_name) \ |
654 | static int acpi_battery_read_##_name(struct seq_file *seq, void *offset) \ | |
655 | { \ | |
656 | return acpi_battery_read(_name##_tag, seq); \ | |
657 | } \ | |
658 | static int acpi_battery_##_name##_open_fs(struct inode *inode, struct file *file) \ | |
659 | { \ | |
660 | return single_open(file, acpi_battery_read_##_name, PDE(inode)->data); \ | |
78490d82 AS |
661 | } |
662 | ||
aa650bbd AS |
663 | DECLARE_FILE_FUNCTIONS(info); |
664 | DECLARE_FILE_FUNCTIONS(state); | |
665 | DECLARE_FILE_FUNCTIONS(alarm); | |
666 | ||
667 | #undef DECLARE_FILE_FUNCTIONS | |
668 | ||
669 | #define FILE_DESCRIPTION_RO(_name) \ | |
670 | { \ | |
671 | .name = __stringify(_name), \ | |
672 | .mode = S_IRUGO, \ | |
673 | .ops = { \ | |
674 | .open = acpi_battery_##_name##_open_fs, \ | |
675 | .read = seq_read, \ | |
676 | .llseek = seq_lseek, \ | |
677 | .release = single_release, \ | |
678 | .owner = THIS_MODULE, \ | |
679 | }, \ | |
680 | } | |
78490d82 | 681 | |
aa650bbd AS |
682 | #define FILE_DESCRIPTION_RW(_name) \ |
683 | { \ | |
684 | .name = __stringify(_name), \ | |
685 | .mode = S_IFREG | S_IRUGO | S_IWUSR, \ | |
686 | .ops = { \ | |
687 | .open = acpi_battery_##_name##_open_fs, \ | |
688 | .read = seq_read, \ | |
689 | .llseek = seq_lseek, \ | |
690 | .write = acpi_battery_write_##_name, \ | |
691 | .release = single_release, \ | |
692 | .owner = THIS_MODULE, \ | |
693 | }, \ | |
694 | } | |
1da177e4 | 695 | |
78490d82 AS |
696 | static struct battery_file { |
697 | struct file_operations ops; | |
698 | mode_t mode; | |
699 | char *name; | |
700 | } acpi_battery_file[] = { | |
aa650bbd AS |
701 | FILE_DESCRIPTION_RO(info), |
702 | FILE_DESCRIPTION_RO(state), | |
703 | FILE_DESCRIPTION_RW(alarm), | |
1da177e4 LT |
704 | }; |
705 | ||
aa650bbd AS |
706 | #undef FILE_DESCRIPTION_RO |
707 | #undef FILE_DESCRIPTION_RW | |
708 | ||
4be44fcd | 709 | static int acpi_battery_add_fs(struct acpi_device *device) |
1da177e4 | 710 | { |
4be44fcd | 711 | struct proc_dir_entry *entry = NULL; |
78490d82 | 712 | int i; |
1da177e4 | 713 | |
1da177e4 LT |
714 | if (!acpi_device_dir(device)) { |
715 | acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), | |
4be44fcd | 716 | acpi_battery_dir); |
1da177e4 | 717 | if (!acpi_device_dir(device)) |
d550d98d | 718 | return -ENODEV; |
1da177e4 LT |
719 | acpi_device_dir(device)->owner = THIS_MODULE; |
720 | } | |
721 | ||
78490d82 AS |
722 | for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i) { |
723 | entry = create_proc_entry(acpi_battery_file[i].name, | |
724 | acpi_battery_file[i].mode, acpi_device_dir(device)); | |
725 | if (!entry) | |
726 | return -ENODEV; | |
727 | else { | |
728 | entry->proc_fops = &acpi_battery_file[i].ops; | |
729 | entry->data = acpi_driver_data(device); | |
730 | entry->owner = THIS_MODULE; | |
731 | } | |
1da177e4 | 732 | } |
d550d98d | 733 | return 0; |
1da177e4 LT |
734 | } |
735 | ||
aa650bbd | 736 | static void acpi_battery_remove_fs(struct acpi_device *device) |
1da177e4 | 737 | { |
78490d82 | 738 | int i; |
aa650bbd AS |
739 | if (!acpi_device_dir(device)) |
740 | return; | |
741 | for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i) | |
742 | remove_proc_entry(acpi_battery_file[i].name, | |
1da177e4 | 743 | acpi_device_dir(device)); |
1da177e4 | 744 | |
aa650bbd AS |
745 | remove_proc_entry(acpi_device_bid(device), acpi_battery_dir); |
746 | acpi_device_dir(device) = NULL; | |
1da177e4 LT |
747 | } |
748 | ||
d7380965 | 749 | #endif |
3e58ea0d | 750 | |
1da177e4 LT |
751 | /* -------------------------------------------------------------------------- |
752 | Driver Interface | |
753 | -------------------------------------------------------------------------- */ | |
754 | ||
4be44fcd | 755 | static void acpi_battery_notify(acpi_handle handle, u32 event, void *data) |
1da177e4 | 756 | { |
50dd0969 | 757 | struct acpi_battery *battery = data; |
f1d4661a | 758 | struct acpi_device *device; |
1da177e4 | 759 | if (!battery) |
d550d98d | 760 | return; |
145def84 | 761 | device = battery->device; |
f1d4661a AS |
762 | acpi_battery_update(battery); |
763 | acpi_bus_generate_proc_event(device, event, | |
764 | acpi_battery_present(battery)); | |
765 | acpi_bus_generate_netlink_event(device->pnp.device_class, | |
766 | device->dev.bus_id, event, | |
9ea7d575 | 767 | acpi_battery_present(battery)); |
508df92d AB |
768 | /* acpi_batter_update could remove power_supply object */ |
769 | if (battery->bat.dev) | |
770 | kobject_uevent(&battery->bat.dev->kobj, KOBJ_CHANGE); | |
1da177e4 LT |
771 | } |
772 | ||
4be44fcd | 773 | static int acpi_battery_add(struct acpi_device *device) |
1da177e4 | 774 | { |
4be44fcd LB |
775 | int result = 0; |
776 | acpi_status status = 0; | |
777 | struct acpi_battery *battery = NULL; | |
1da177e4 | 778 | if (!device) |
d550d98d | 779 | return -EINVAL; |
36bcbec7 | 780 | battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL); |
1da177e4 | 781 | if (!battery) |
d550d98d | 782 | return -ENOMEM; |
145def84 | 783 | battery->device = device; |
1da177e4 LT |
784 | strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME); |
785 | strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS); | |
786 | acpi_driver_data(device) = battery; | |
038fdea2 | 787 | mutex_init(&battery->lock); |
f1d4661a | 788 | acpi_battery_update(battery); |
d7380965 | 789 | #ifdef CONFIG_ACPI_PROCFS |
1da177e4 LT |
790 | result = acpi_battery_add_fs(device); |
791 | if (result) | |
792 | goto end; | |
d7380965 | 793 | #endif |
3b073ec3 | 794 | status = acpi_install_notify_handler(device->handle, |
9fdae727 | 795 | ACPI_ALL_NOTIFY, |
4be44fcd | 796 | acpi_battery_notify, battery); |
1da177e4 | 797 | if (ACPI_FAILURE(status)) { |
b6ce4083 | 798 | ACPI_EXCEPTION((AE_INFO, status, "Installing notify handler")); |
1da177e4 LT |
799 | result = -ENODEV; |
800 | goto end; | |
801 | } | |
1da177e4 | 802 | printk(KERN_INFO PREFIX "%s Slot [%s] (battery %s)\n", |
4be44fcd LB |
803 | ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device), |
804 | device->status.battery_present ? "present" : "absent"); | |
4be44fcd | 805 | end: |
1da177e4 | 806 | if (result) { |
d7380965 | 807 | #ifdef CONFIG_ACPI_PROCFS |
1da177e4 | 808 | acpi_battery_remove_fs(device); |
d7380965 | 809 | #endif |
1da177e4 LT |
810 | kfree(battery); |
811 | } | |
d550d98d | 812 | return result; |
1da177e4 LT |
813 | } |
814 | ||
4be44fcd | 815 | static int acpi_battery_remove(struct acpi_device *device, int type) |
1da177e4 | 816 | { |
4be44fcd LB |
817 | acpi_status status = 0; |
818 | struct acpi_battery *battery = NULL; | |
1da177e4 | 819 | |
1da177e4 | 820 | if (!device || !acpi_driver_data(device)) |
d550d98d | 821 | return -EINVAL; |
50dd0969 | 822 | battery = acpi_driver_data(device); |
3b073ec3 | 823 | status = acpi_remove_notify_handler(device->handle, |
9fdae727 | 824 | ACPI_ALL_NOTIFY, |
4be44fcd | 825 | acpi_battery_notify); |
d7380965 | 826 | #ifdef CONFIG_ACPI_PROCFS |
1da177e4 | 827 | acpi_battery_remove_fs(device); |
d7380965 | 828 | #endif |
508df92d | 829 | sysfs_remove_battery(battery); |
038fdea2 | 830 | mutex_destroy(&battery->lock); |
1da177e4 | 831 | kfree(battery); |
d550d98d | 832 | return 0; |
1da177e4 LT |
833 | } |
834 | ||
34c4415a | 835 | /* this is needed to learn about changes made in suspended state */ |
5d9464a4 | 836 | static int acpi_battery_resume(struct acpi_device *device) |
34c4415a JK |
837 | { |
838 | struct acpi_battery *battery; | |
34c4415a JK |
839 | if (!device) |
840 | return -EINVAL; | |
f1d4661a AS |
841 | battery = acpi_driver_data(device); |
842 | battery->update_time = 0; | |
508df92d | 843 | acpi_battery_update(battery); |
b6ce4083 | 844 | return 0; |
34c4415a JK |
845 | } |
846 | ||
aa650bbd AS |
847 | static struct acpi_driver acpi_battery_driver = { |
848 | .name = "battery", | |
849 | .class = ACPI_BATTERY_CLASS, | |
850 | .ids = battery_device_ids, | |
851 | .ops = { | |
852 | .add = acpi_battery_add, | |
853 | .resume = acpi_battery_resume, | |
854 | .remove = acpi_battery_remove, | |
855 | }, | |
856 | }; | |
857 | ||
4be44fcd | 858 | static int __init acpi_battery_init(void) |
1da177e4 | 859 | { |
4d8316d5 PM |
860 | if (acpi_disabled) |
861 | return -ENODEV; | |
d7380965 | 862 | #ifdef CONFIG_ACPI_PROCFS |
3f86b832 | 863 | acpi_battery_dir = acpi_lock_battery_dir(); |
1da177e4 | 864 | if (!acpi_battery_dir) |
d550d98d | 865 | return -ENODEV; |
d7380965 | 866 | #endif |
aa650bbd | 867 | if (acpi_bus_register_driver(&acpi_battery_driver) < 0) { |
d7380965 | 868 | #ifdef CONFIG_ACPI_PROCFS |
3f86b832 | 869 | acpi_unlock_battery_dir(acpi_battery_dir); |
d7380965 | 870 | #endif |
d550d98d | 871 | return -ENODEV; |
1da177e4 | 872 | } |
d550d98d | 873 | return 0; |
1da177e4 LT |
874 | } |
875 | ||
4be44fcd | 876 | static void __exit acpi_battery_exit(void) |
1da177e4 | 877 | { |
1da177e4 | 878 | acpi_bus_unregister_driver(&acpi_battery_driver); |
d7380965 | 879 | #ifdef CONFIG_ACPI_PROCFS |
3f86b832 | 880 | acpi_unlock_battery_dir(acpi_battery_dir); |
d7380965 | 881 | #endif |
1da177e4 LT |
882 | } |
883 | ||
1da177e4 LT |
884 | module_init(acpi_battery_init); |
885 | module_exit(acpi_battery_exit); |