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